nativewindowpriv: don't connect before ics
[vlc.git] / modules / video_output / android / nativewindowpriv.c
blob989236a594e277d4c542539c5f9e185a59d242fe
1 /*****************************************************************************
2 * nativewindowpriv.c: Wrapper to android native window private api
3 *****************************************************************************
4 * Copyright (C) 2011 VLC authors and VideoLAN
6 * Authors: Thomas Guillem <guillem@archos.com>
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.
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.
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 *****************************************************************************/
23 /*****************************************************************************
24 * Preamble
25 *****************************************************************************/
27 #include <errno.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <stdbool.h>
32 #include <android/native_window.h>
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
41 #include <hardware/gralloc.h>
43 #include <android/log.h>
45 #define NO_ERROR 0
46 typedef int32_t status_t;
48 #if ANDROID_API <= 13
49 typedef android_native_buffer_t ANativeWindowBuffer_t;
50 #endif
51 typedef struct native_window_priv native_window_priv;
53 struct native_window_priv
55 ANativeWindow *anw;
56 gralloc_module_t const* gralloc;
57 int usage;
60 #define LOG_TAG "VLC/ANW"
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__ )
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;\
70 } while (0)
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;\
78 } while (0)
80 static int window_connect( ANativeWindow *anw )
82 #if ANDROID_API >= 14
83 return native_window_api_connect( anw, NATIVE_WINDOW_API_MEDIA );
84 #endif
87 static int window_disconnect( ANativeWindow *anw )
89 #if ANDROID_API >= 14
90 return native_window_api_disconnect( anw, NATIVE_WINDOW_API_MEDIA );
91 #endif
94 native_window_priv *ANativeWindowPriv_connect( void *window )
96 native_window_priv *priv;
97 hw_module_t const* module;
98 ANativeWindow *anw = (ANativeWindow *)window;
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;
106 if ( hw_get_module( GRALLOC_HARDWARE_MODULE_ID,
107 &module ) != 0 )
108 return NULL;
110 if( window_connect( anw ) != 0 ) {
111 LOGE( "native_window_api_connect FAIL" );
112 return NULL;
115 priv = calloc( 1, sizeof(native_window_priv) );
117 if( !priv ) {
118 window_disconnect( anw );
119 return NULL;
121 priv->anw = anw;
122 priv->gralloc = (gralloc_module_t const *) module;
124 return priv;
127 int ANativeWindowPriv_disconnect( native_window_priv *priv )
129 window_disconnect( priv->anw );
130 free(priv);
132 return 0;
135 int ANativeWindowPriv_setup( native_window_priv *priv, int w, int h, int hal_format, bool is_hw, int hw_usage )
137 status_t err;
139 LOGD( "setup: %p, %d, %d, %X, %X\n",
140 priv->anw, w, h, hal_format, hw_usage );
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
150 err = native_window_set_usage( priv->anw, priv->usage );
151 CHECK_ERR();
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();
160 err = native_window_set_buffers_dimensions( priv->anw, w, h );
161 CHECK_ERR();
163 err = native_window_set_buffers_format( priv->anw, hal_format );
164 CHECK_ERR();
165 #endif
167 return 0;
170 int ANativeWindowPriv_getMinUndequeued( native_window_priv *priv, unsigned int *min_undequeued )
172 status_t err;
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 = 2;
182 LOGD( "getMinUndequeued: %p %u", priv->anw, *min_undequeued );
184 return 0;
187 int ANativeWindowPriv_setBufferCount(native_window_priv *priv, unsigned int count )
189 status_t err;
191 LOGD( "setBufferCount: %p %u", priv->anw, count );
193 err = native_window_set_buffer_count( priv->anw, count );
194 CHECK_ERR();
196 return 0;
199 int ANativeWindowPriv_setCrop( native_window_priv *priv, int ofs_x, int ofs_y, int w, int h )
201 android_native_rect_t crop;
203 crop.left = ofs_x;
204 crop.top = ofs_y;
205 crop.right = ofs_x + w;
206 crop.bottom = ofs_y + h;
207 return native_window_set_crop( priv->anw, &crop );
210 int ANativeWindowPriv_dequeue( native_window_priv *priv, void **pp_handle )
212 ANativeWindowBuffer_t *anb;
213 status_t err = NO_ERROR;
215 #if ANDROID_API >= 18
216 err = priv->anw->dequeueBuffer_DEPRECATED( priv->anw, &anb );
217 #else
218 err = priv->anw->dequeueBuffer( priv->anw, &anb );
219 #endif
220 CHECK_ERR();
222 *pp_handle = anb;
224 return 0;
227 int ANativeWindowPriv_lock( native_window_priv *priv, void *p_handle )
229 ANativeWindowBuffer_t *anb = (ANativeWindowBuffer_t *)p_handle;
230 status_t err = NO_ERROR;
232 CHECK_ANB();
234 #if ANDROID_API >= 18
235 err = priv->anw->lockBuffer_DEPRECATED( priv->anw, anb );
236 #else
237 err = priv->anw->lockBuffer( priv->anw, anb );
238 #endif
239 CHECK_ERR();
241 return 0;
244 int ANativeWindowPriv_lockData( native_window_priv *priv, void *p_handle,
245 ANativeWindow_Buffer *p_out_anb )
247 ANativeWindowBuffer_t *anb = (ANativeWindowBuffer_t *)p_handle;
248 status_t err = NO_ERROR;
249 void *p_data;
251 CHECK_ANB();
253 err = priv->gralloc->lock( priv->gralloc, anb->handle, priv->usage,
254 0, 0, anb->width, anb->height, &p_data );
255 CHECK_ERR();
256 if( p_out_anb ) {
257 p_out_anb->bits = p_data;
258 p_out_anb->width = anb->width;
259 p_out_anb->height = anb->height;
260 p_out_anb->stride = anb->stride;
261 p_out_anb->format = anb->format;
264 return 0;
267 int ANativeWindowPriv_unlockData( native_window_priv *priv, void *p_handle )
269 ANativeWindowBuffer_t *anb = (ANativeWindowBuffer_t *)p_handle;
270 status_t err = NO_ERROR;
272 CHECK_ANB();
274 err = priv->gralloc->unlock(priv->gralloc, anb->handle);
275 CHECK_ERR();
277 return 0;
280 int ANativeWindowPriv_queue( native_window_priv *priv, void *p_handle )
282 ANativeWindowBuffer_t *anb = (ANativeWindowBuffer_t *)p_handle;
283 status_t err = NO_ERROR;
285 CHECK_ANB();
287 #if ANDROID_API >= 18
288 err = priv->anw->queueBuffer_DEPRECATED( priv->anw, anb );
289 #else
290 err = priv->anw->queueBuffer( priv->anw, anb );
291 #endif
292 CHECK_ERR();
294 return 0;
297 int ANativeWindowPriv_cancel( native_window_priv *priv, void *p_handle )
299 ANativeWindowBuffer_t *anb = (ANativeWindowBuffer_t *)p_handle;
300 status_t err = NO_ERROR;
302 CHECK_ANB();
304 #if ANDROID_API >= 18
305 err = priv->anw->cancelBuffer_DEPRECATED( priv->anw, anb );
306 #else
307 err = priv->anw->cancelBuffer( priv->anw, anb );
308 #endif
309 CHECK_ERR();
311 return 0;
314 int ANativeWindowPriv_setOrientation( native_window_priv *priv, int orientation )
316 status_t err = NO_ERROR;
317 int transform;
319 switch( orientation )
321 case 90:
322 transform = NATIVE_WINDOW_TRANSFORM_ROT_90;
323 break;
324 case 180:
325 transform = NATIVE_WINDOW_TRANSFORM_ROT_180;
326 break;
327 case 270:
328 transform = NATIVE_WINDOW_TRANSFORM_ROT_270;
329 break;
330 default:
331 transform = 0;
334 err = native_window_set_buffers_transform( priv->anw, transform );
335 CHECK_ERR();
337 return 0;