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