]> git.sesse.net Git - vlc/blob - modules/video_output/android/nativewindowpriv.c
nativewindowpriv: change default min_undequeued value
[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 #if ANDROID_API <= 13
35 #include <ui/android_native_buffer.h>
36 #include <ui/egl/android_natives.h>
37 #else
38 #include <system/window.h>
39 #endif
40
41 #include <hardware/gralloc.h>
42
43 #include <android/log.h>
44
45 #define NO_ERROR 0
46 typedef int32_t status_t;
47
48 #if ANDROID_API <= 13
49 typedef android_native_buffer_t ANativeWindowBuffer_t;
50 #endif
51 typedef struct native_window_priv native_window_priv;
52
53 struct native_window_priv
54 {
55     ANativeWindow *anw;
56     gralloc_module_t const* gralloc;
57     int usage;
58 };
59
60 #define LOG_TAG "VLC/ANW"
61
62 #define LOGD(...) __android_log_print( ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__ )
63 #define LOGE(...) __android_log_print( ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__ )
64
65 #define CHECK_ERR() do {\
66     if( err != NO_ERROR ) {\
67         LOGE( "error %d in %s  line %d\n", err, __FUNCTION__, __LINE__  );\
68         return err;\
69     }\
70 } while (0)
71
72 #define CHECK_ANB() do {\
73     if( anb->common.magic != ANDROID_NATIVE_BUFFER_MAGIC &&\
74             anb->common.version != sizeof(ANativeWindowBuffer_t) ) {\
75         LOGE( "error, buffer not valid\n"  );\
76         return -EINVAL;\
77     }\
78 } while (0)
79
80 static int window_connect( ANativeWindow *anw )
81 {
82 #if ANDROID_API >= 14
83     return native_window_api_connect( anw, NATIVE_WINDOW_API_MEDIA );
84 #endif
85 }
86
87 static int window_disconnect( ANativeWindow *anw )
88 {
89 #if ANDROID_API >= 14
90     return native_window_api_disconnect( anw, NATIVE_WINDOW_API_MEDIA );
91 #endif
92 }
93
94 native_window_priv *ANativeWindowPriv_connect( void *window )
95 {
96     native_window_priv *priv;
97     hw_module_t const* module;
98     ANativeWindow *anw = (ANativeWindow *)window;
99
100     if( anw->common.magic != ANDROID_NATIVE_WINDOW_MAGIC &&
101             anw->common.version != sizeof(ANativeWindow) ) {
102         LOGE( "error, window not valid\n"  );
103         return NULL;
104     }
105
106     if ( hw_get_module( GRALLOC_HARDWARE_MODULE_ID,
107                         &module ) != 0 )
108         return NULL;
109
110     if( window_connect( anw ) != 0 ) {
111         LOGE( "native_window_api_connect FAIL"  );
112         return NULL;
113     }
114
115     priv = calloc( 1, sizeof(native_window_priv) );
116
117     if( !priv ) {
118         window_disconnect( anw );
119         return NULL;
120     }
121     priv->anw = anw;
122     priv->gralloc = (gralloc_module_t const *) module;
123
124     return priv;
125 }
126
127 int ANativeWindowPriv_disconnect( native_window_priv *priv )
128 {
129     window_disconnect( priv->anw );
130     free(priv);
131
132     return 0;
133 }
134
135 int ANativeWindowPriv_setup( native_window_priv *priv, int w, int h, int hal_format, bool is_hw, int hw_usage )
136 {
137     status_t err;
138
139     LOGD( "setup: %p, %d, %d, %X, %X\n",
140           priv->anw, w, h, hal_format, hw_usage );
141
142     if (is_hw)
143         priv->usage = hw_usage | GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
144     else
145         priv->usage= GRALLOC_USAGE_SW_READ_NEVER | GRALLOC_USAGE_SW_WRITE_OFTEN;
146 #if ANDROID_API >= 11
147     priv->usage |= GRALLOC_USAGE_EXTERNAL_DISP;
148 #endif
149
150     err = native_window_set_usage( priv->anw, priv->usage );
151     CHECK_ERR();
152
153 #if ANDROID_API <= 13
154     err = native_window_set_buffers_geometry( priv->anw, w, h, hal_format );
155     CHECK_ERR();
156 #else
157     err = native_window_set_scaling_mode( priv->anw, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW );
158     CHECK_ERR();
159
160     err = native_window_set_buffers_dimensions( priv->anw, w, h );
161     CHECK_ERR();
162
163     err = native_window_set_buffers_format( priv->anw, hal_format );
164     CHECK_ERR();
165 #endif
166
167     return 0;
168 }
169
170 int ANativeWindowPriv_getMinUndequeued( native_window_priv *priv, unsigned int *min_undequeued )
171 {
172     status_t err;
173
174 #if ANDROID_API >= 11
175     err = priv->anw->query( priv->anw, NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, min_undequeued );
176     CHECK_ERR();
177 #endif
178     /* set a minimum value of min_undequeued in case query fails */
179     if( *min_undequeued == 0 )
180         *min_undequeued = 1;
181
182     LOGD( "getMinUndequeued: %p %u", priv->anw, *min_undequeued );
183
184     return 0;
185 }
186
187 int ANativeWindowPriv_getMaxBufferCount( native_window_priv *priv, unsigned int *max_buffer_count )
188 {
189 #if ANDROID_API >= 14
190     *max_buffer_count = 32;
191 #else
192     *max_buffer_count = 15;
193 #endif
194     return 0;
195 }
196
197 int ANativeWindowPriv_setBufferCount(native_window_priv *priv, unsigned int count )
198 {
199     status_t err;
200
201     LOGD( "setBufferCount: %p %u", priv->anw, count );
202
203     err = native_window_set_buffer_count( priv->anw, count );
204     CHECK_ERR();
205
206     return 0;
207 }
208
209 int ANativeWindowPriv_setCrop( native_window_priv *priv, int ofs_x, int ofs_y, int w, int h )
210 {
211     android_native_rect_t crop;
212
213     crop.left = ofs_x;
214     crop.top = ofs_y;
215     crop.right = ofs_x + w;
216     crop.bottom = ofs_y + h;
217     return native_window_set_crop( priv->anw, &crop );
218 }
219
220 int ANativeWindowPriv_dequeue( native_window_priv *priv, void **pp_handle )
221 {
222     ANativeWindowBuffer_t *anb;
223     status_t err = NO_ERROR;
224
225 #if ANDROID_API >= 18
226     err = priv->anw->dequeueBuffer_DEPRECATED( priv->anw, &anb );
227 #else
228     err = priv->anw->dequeueBuffer( priv->anw, &anb );
229 #endif
230     CHECK_ERR();
231
232     *pp_handle = anb;
233
234     return 0;
235 }
236
237 int ANativeWindowPriv_lock( native_window_priv *priv, void *p_handle )
238 {
239     ANativeWindowBuffer_t *anb = (ANativeWindowBuffer_t *)p_handle;
240     status_t err = NO_ERROR;
241
242     CHECK_ANB();
243
244 #if ANDROID_API >= 18
245     err = priv->anw->lockBuffer_DEPRECATED( priv->anw, anb );
246 #else
247     err = priv->anw->lockBuffer( priv->anw, anb );
248 #endif
249     CHECK_ERR();
250
251     return 0;
252 }
253
254 int ANativeWindowPriv_lockData( native_window_priv *priv, void *p_handle,
255                                 ANativeWindow_Buffer *p_out_anb )
256 {
257     ANativeWindowBuffer_t *anb = (ANativeWindowBuffer_t *)p_handle;
258     status_t err = NO_ERROR;
259     void *p_data;
260
261     CHECK_ANB();
262
263     err = priv->gralloc->lock( priv->gralloc, anb->handle, priv->usage,
264                                0, 0, anb->width, anb->height, &p_data );
265     CHECK_ERR();
266     if( p_out_anb ) {
267         p_out_anb->bits = p_data;
268         p_out_anb->width = anb->width;
269         p_out_anb->height = anb->height;
270         p_out_anb->stride = anb->stride;
271         p_out_anb->format = anb->format;
272     }
273
274     return 0;
275 }
276
277 int ANativeWindowPriv_unlockData( native_window_priv *priv, void *p_handle )
278 {
279     ANativeWindowBuffer_t *anb = (ANativeWindowBuffer_t *)p_handle;
280     status_t err = NO_ERROR;
281
282     CHECK_ANB();
283
284     err = priv->gralloc->unlock(priv->gralloc, anb->handle);
285     CHECK_ERR();
286
287     return 0;
288 }
289
290 int ANativeWindowPriv_queue( native_window_priv *priv, void *p_handle )
291 {
292     ANativeWindowBuffer_t *anb = (ANativeWindowBuffer_t *)p_handle;
293     status_t err = NO_ERROR;
294
295     CHECK_ANB();
296
297 #if ANDROID_API >= 18
298     err = priv->anw->queueBuffer_DEPRECATED( priv->anw, anb );
299 #else
300     err = priv->anw->queueBuffer( priv->anw, anb );
301 #endif
302     CHECK_ERR();
303
304     return 0;
305 }
306
307 int ANativeWindowPriv_cancel( native_window_priv *priv, void *p_handle )
308 {
309     ANativeWindowBuffer_t *anb = (ANativeWindowBuffer_t *)p_handle;
310     status_t err = NO_ERROR;
311
312     CHECK_ANB();
313
314 #if ANDROID_API >= 18
315     err = priv->anw->cancelBuffer_DEPRECATED( priv->anw, anb );
316 #else
317     err = priv->anw->cancelBuffer( priv->anw, anb );
318 #endif
319     CHECK_ERR();
320
321     return 0;
322 }
323
324 int ANativeWindowPriv_setOrientation( native_window_priv *priv, int orientation )
325 {
326     status_t err = NO_ERROR;
327     int transform;
328
329     switch( orientation )
330     {
331         case 90:
332             transform = NATIVE_WINDOW_TRANSFORM_ROT_90;
333             break;
334         case 180:
335             transform = NATIVE_WINDOW_TRANSFORM_ROT_180;
336             break;
337         case 270:
338             transform = NATIVE_WINDOW_TRANSFORM_ROT_270;
339             break;
340         default:
341             transform = 0;
342     }
343
344     err = native_window_set_buffers_transform( priv->anw, transform );
345     CHECK_ERR();
346
347     return 0;
348 }