]> git.sesse.net Git - vlc/blob - modules/video_output/android/nativewindowpriv.c
nativewindowpriv: better use of defines (cosmetics)
[vlc] / modules / video_output / android / nativewindowpriv.c
1 /*****************************************************************************
2  * nativewindowpriv.c: Wrapper to android native window private api
3  *****************************************************************************
4  * Copyright (C) 2011 VLC authors and VideoLAN
5  *
6  * Authors: Thomas Guillem <guillem@archos.com>
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU Lesser General Public License as published by
10  * the Free Software Foundation; either version 2.1 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21  *****************************************************************************/
22
23 /*****************************************************************************
24  * Preamble
25  *****************************************************************************/
26
27 #include <errno.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <stdbool.h>
31
32 #include <android/native_window.h>
33
34 #define ANDROID_HC_OR_LATER (ANDROID_API >= 11)
35 #define ANDROID_ICS_OR_LATER (ANDROID_API >= 14)
36 #define ANDROID_JBMR2_OR_LATER (ANDROID_API >= 18)
37
38 #if ANDROID_ICS_OR_LATER
39 #include <system/window.h>
40 #else
41 #include <ui/android_native_buffer.h>
42 #include <ui/egl/android_natives.h>
43 #endif
44
45 #include <hardware/gralloc.h>
46
47 #include <android/log.h>
48
49 #define NO_ERROR 0
50 typedef int32_t status_t;
51
52 #if !ANDROID_ICS_OR_LATER
53 typedef android_native_buffer_t ANativeWindowBuffer_t;
54 #endif
55 typedef struct native_window_priv native_window_priv;
56
57 struct native_window_priv
58 {
59     ANativeWindow *anw;
60     gralloc_module_t const* gralloc;
61     int usage;
62 };
63
64 #define LOG_TAG "VLC/ANW"
65
66 #define LOGD(...) __android_log_print( ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__ )
67 #define LOGE(...) __android_log_print( ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__ )
68
69 #define CHECK_ERR() do {\
70     if( err != NO_ERROR ) {\
71         LOGE( "error %d in %s  line %d\n", err, __FUNCTION__, __LINE__  );\
72         return err;\
73     }\
74 } while (0)
75
76 #define CHECK_ANB() do {\
77     if( anb->common.magic != ANDROID_NATIVE_BUFFER_MAGIC &&\
78             anb->common.version != sizeof(ANativeWindowBuffer_t) ) {\
79         LOGE( "error, buffer not valid\n"  );\
80         return -EINVAL;\
81     }\
82 } while (0)
83
84 static int window_connect( ANativeWindow *anw )
85 {
86 #if ANDROID_ICS_OR_LATER
87     return native_window_api_connect( anw, NATIVE_WINDOW_API_MEDIA );
88 #endif
89 }
90
91 static int window_disconnect( ANativeWindow *anw )
92 {
93 #if ANDROID_ICS_OR_LATER
94     return native_window_api_disconnect( anw, NATIVE_WINDOW_API_MEDIA );
95 #endif
96 }
97
98 native_window_priv *ANativeWindowPriv_connect( void *window )
99 {
100     native_window_priv *priv;
101     hw_module_t const* module;
102     ANativeWindow *anw = (ANativeWindow *)window;
103
104     if( anw->common.magic != ANDROID_NATIVE_WINDOW_MAGIC &&
105             anw->common.version != sizeof(ANativeWindow) ) {
106         LOGE( "error, window not valid\n"  );
107         return NULL;
108     }
109
110     if ( hw_get_module( GRALLOC_HARDWARE_MODULE_ID,
111                         &module ) != 0 )
112         return NULL;
113
114     if( window_connect( anw ) != 0 ) {
115         LOGE( "native_window_api_connect FAIL"  );
116         return NULL;
117     }
118
119     priv = calloc( 1, sizeof(native_window_priv) );
120
121     if( !priv ) {
122         window_disconnect( anw );
123         return NULL;
124     }
125     priv->anw = anw;
126     priv->gralloc = (gralloc_module_t const *) module;
127
128     return priv;
129 }
130
131 int ANativeWindowPriv_disconnect( native_window_priv *priv )
132 {
133     window_disconnect( priv->anw );
134     free(priv);
135
136     return 0;
137 }
138
139 int ANativeWindowPriv_setup( native_window_priv *priv, int w, int h, int hal_format, bool is_hw, int hw_usage )
140 {
141     status_t err;
142
143     LOGD( "setup: %p, %d, %d, %X, %X\n",
144           priv->anw, w, h, hal_format, hw_usage );
145
146     if (is_hw)
147         priv->usage = hw_usage | GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
148     else
149         priv->usage= GRALLOC_USAGE_SW_READ_NEVER | GRALLOC_USAGE_SW_WRITE_OFTEN;
150 #if ANDROID_HC_OR_LATER
151     priv->usage |= GRALLOC_USAGE_EXTERNAL_DISP;
152 #endif
153
154     err = native_window_set_usage( priv->anw, priv->usage );
155     CHECK_ERR();
156
157 #if ANDROID_ICS_OR_LATER
158     err = native_window_set_scaling_mode( priv->anw, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW );
159     CHECK_ERR();
160
161     err = native_window_set_buffers_dimensions( priv->anw, w, h );
162     CHECK_ERR();
163
164     err = native_window_set_buffers_format( priv->anw, hal_format );
165     CHECK_ERR();
166 #else
167     err = native_window_set_buffers_geometry( priv->anw, w, h, hal_format );
168     CHECK_ERR();
169 #endif
170
171     return 0;
172 }
173
174 int ANativeWindowPriv_getMinUndequeued( native_window_priv *priv, unsigned int *min_undequeued )
175 {
176     status_t err;
177
178 #if ANDROID_HC_OR_LATER
179     err = priv->anw->query( priv->anw, NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, min_undequeued );
180     CHECK_ERR();
181 #endif
182     /* set a minimum value of min_undequeued in case query fails */
183     if( *min_undequeued == 0 )
184         *min_undequeued = 1;
185
186     LOGD( "getMinUndequeued: %p %u", priv->anw, *min_undequeued );
187
188     return 0;
189 }
190
191 int ANativeWindowPriv_getMaxBufferCount( native_window_priv *priv, unsigned int *max_buffer_count )
192 {
193 #if ANDROID_ICS_OR_LATER
194     *max_buffer_count = 32;
195 #else
196     *max_buffer_count = 15;
197 #endif
198     return 0;
199 }
200
201 int ANativeWindowPriv_setBufferCount(native_window_priv *priv, unsigned int count )
202 {
203     status_t err;
204
205     LOGD( "setBufferCount: %p %u", priv->anw, count );
206
207     err = native_window_set_buffer_count( priv->anw, count );
208     CHECK_ERR();
209
210     return 0;
211 }
212
213 int ANativeWindowPriv_setCrop( native_window_priv *priv, int ofs_x, int ofs_y, int w, int h )
214 {
215     android_native_rect_t crop;
216
217     crop.left = ofs_x;
218     crop.top = ofs_y;
219     crop.right = ofs_x + w;
220     crop.bottom = ofs_y + h;
221     return native_window_set_crop( priv->anw, &crop );
222 }
223
224 int ANativeWindowPriv_dequeue( native_window_priv *priv, void **pp_handle )
225 {
226     ANativeWindowBuffer_t *anb;
227     status_t err = NO_ERROR;
228
229 #if ANDROID_JBMR2_OR_LATER
230     err = priv->anw->dequeueBuffer_DEPRECATED( priv->anw, &anb );
231 #else
232     err = priv->anw->dequeueBuffer( priv->anw, &anb );
233 #endif
234     CHECK_ERR();
235
236     *pp_handle = anb;
237
238     return 0;
239 }
240
241 int ANativeWindowPriv_lock( native_window_priv *priv, void *p_handle )
242 {
243     ANativeWindowBuffer_t *anb = (ANativeWindowBuffer_t *)p_handle;
244     status_t err = NO_ERROR;
245
246     CHECK_ANB();
247
248 #if ANDROID_JBMR2_OR_LATER
249     err = priv->anw->lockBuffer_DEPRECATED( priv->anw, anb );
250 #else
251     err = priv->anw->lockBuffer( priv->anw, anb );
252 #endif
253     CHECK_ERR();
254
255     return 0;
256 }
257
258 int ANativeWindowPriv_lockData( native_window_priv *priv, void *p_handle,
259                                 ANativeWindow_Buffer *p_out_anb )
260 {
261     ANativeWindowBuffer_t *anb = (ANativeWindowBuffer_t *)p_handle;
262     status_t err = NO_ERROR;
263     void *p_data;
264
265     CHECK_ANB();
266
267     err = priv->gralloc->lock( priv->gralloc, anb->handle, priv->usage,
268                                0, 0, anb->width, anb->height, &p_data );
269     CHECK_ERR();
270     if( p_out_anb ) {
271         p_out_anb->bits = p_data;
272         p_out_anb->width = anb->width;
273         p_out_anb->height = anb->height;
274         p_out_anb->stride = anb->stride;
275         p_out_anb->format = anb->format;
276     }
277
278     return 0;
279 }
280
281 int ANativeWindowPriv_unlockData( native_window_priv *priv, void *p_handle )
282 {
283     ANativeWindowBuffer_t *anb = (ANativeWindowBuffer_t *)p_handle;
284     status_t err = NO_ERROR;
285
286     CHECK_ANB();
287
288     err = priv->gralloc->unlock(priv->gralloc, anb->handle);
289     CHECK_ERR();
290
291     return 0;
292 }
293
294 int ANativeWindowPriv_queue( native_window_priv *priv, void *p_handle )
295 {
296     ANativeWindowBuffer_t *anb = (ANativeWindowBuffer_t *)p_handle;
297     status_t err = NO_ERROR;
298
299     CHECK_ANB();
300
301 #if ANDROID_JBMR2_OR_LATER
302     err = priv->anw->queueBuffer_DEPRECATED( priv->anw, anb );
303 #else
304     err = priv->anw->queueBuffer( priv->anw, anb );
305 #endif
306     CHECK_ERR();
307
308     return 0;
309 }
310
311 int ANativeWindowPriv_cancel( native_window_priv *priv, void *p_handle )
312 {
313     ANativeWindowBuffer_t *anb = (ANativeWindowBuffer_t *)p_handle;
314     status_t err = NO_ERROR;
315
316     CHECK_ANB();
317
318 #if ANDROID_JBMR2_OR_LATER
319     err = priv->anw->cancelBuffer_DEPRECATED( priv->anw, anb );
320 #else
321     err = priv->anw->cancelBuffer( priv->anw, anb );
322 #endif
323     CHECK_ERR();
324
325     return 0;
326 }
327
328 int ANativeWindowPriv_setOrientation( native_window_priv *priv, int orientation )
329 {
330     status_t err = NO_ERROR;
331     int transform;
332
333     switch( orientation )
334     {
335         case 90:
336             transform = NATIVE_WINDOW_TRANSFORM_ROT_90;
337             break;
338         case 180:
339             transform = NATIVE_WINDOW_TRANSFORM_ROT_180;
340             break;
341         case 270:
342             transform = NATIVE_WINDOW_TRANSFORM_ROT_270;
343             break;
344         default:
345             transform = 0;
346     }
347
348     err = native_window_set_buffers_transform( priv->anw, transform );
349     CHECK_ERR();
350
351     return 0;
352 }