server: Do not accept sizeof(struct WS_sockaddr_in6_old).
[wine.git] / dlls / wineandroid.drv / device.c
blob4a16e6836ed0846922352e9ee529f0d5a5198341
1 /*
2 * Android pseudo-device handling
4 * Copyright 2014-2017 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <errno.h>
26 #include <stdio.h>
27 #include <stdarg.h>
28 #include <sys/ioctl.h>
30 #define NONAMELESSUNION
31 #define NONAMELESSSTRUCT
33 #include "ntstatus.h"
34 #define WIN32_NO_STATUS
35 #include "windef.h"
36 #include "winbase.h"
37 #include "winternl.h"
38 #include "winioctl.h"
39 #include "ddk/wdm.h"
40 #include "android.h"
41 #include "wine/server.h"
42 #include "wine/debug.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(android);
46 #ifndef SYNC_IOC_WAIT
47 #define SYNC_IOC_WAIT _IOW('>', 0, __s32)
48 #endif
50 extern NTSTATUS CDECL wine_ntoskrnl_main_loop( HANDLE stop_event );
51 static HANDLE stop_event;
52 static HANDLE thread;
53 static JNIEnv *jni_env;
54 static HWND capture_window;
56 #define ANDROIDCONTROLTYPE ((ULONG)'A')
57 #define ANDROID_IOCTL(n) CTL_CODE(ANDROIDCONTROLTYPE, n, METHOD_BUFFERED, FILE_READ_ACCESS)
59 enum android_ioctl
61 IOCTL_CREATE_WINDOW,
62 IOCTL_DESTROY_WINDOW,
63 IOCTL_WINDOW_POS_CHANGED,
64 IOCTL_SET_WINDOW_PARENT,
65 IOCTL_DEQUEUE_BUFFER,
66 IOCTL_QUEUE_BUFFER,
67 IOCTL_CANCEL_BUFFER,
68 IOCTL_QUERY,
69 IOCTL_PERFORM,
70 IOCTL_SET_SWAP_INT,
71 IOCTL_SET_CAPTURE,
72 IOCTL_SET_CURSOR,
73 NB_IOCTLS
76 #define NB_CACHED_BUFFERS 4
78 struct native_buffer_wrapper;
80 /* buffer for storing a variable-size native handle inside an ioctl structure */
81 union native_handle_buffer
83 native_handle_t handle;
84 int space[256];
87 /* data about the native window in the context of the Java process */
88 struct native_win_data
90 struct ANativeWindow *parent;
91 struct ANativeWindowBuffer *buffers[NB_CACHED_BUFFERS];
92 void *mappings[NB_CACHED_BUFFERS];
93 HWND hwnd;
94 BOOL opengl;
95 int generation;
96 int api;
97 int buffer_format;
98 int swap_interval;
99 int buffer_lru[NB_CACHED_BUFFERS];
102 /* wrapper for a native window in the context of the client (non-Java) process */
103 struct native_win_wrapper
105 struct ANativeWindow win;
106 struct native_buffer_wrapper *buffers[NB_CACHED_BUFFERS];
107 struct ANativeWindowBuffer *locked_buffer;
108 HWND hwnd;
109 BOOL opengl;
110 LONG ref;
113 /* wrapper for a native buffer in the context of the client (non-Java) process */
114 struct native_buffer_wrapper
116 struct ANativeWindowBuffer buffer;
117 LONG ref;
118 HWND hwnd;
119 void *bits;
120 int buffer_id;
121 int generation;
122 union native_handle_buffer native_handle;
125 struct ioctl_header
127 int hwnd;
128 BOOL opengl;
131 struct ioctl_android_create_window
133 struct ioctl_header hdr;
134 int parent;
135 float scale;
138 struct ioctl_android_destroy_window
140 struct ioctl_header hdr;
143 struct ioctl_android_window_pos_changed
145 struct ioctl_header hdr;
146 RECT window_rect;
147 RECT client_rect;
148 RECT visible_rect;
149 int style;
150 int flags;
151 int after;
152 int owner;
155 struct ioctl_android_dequeueBuffer
157 struct ioctl_header hdr;
158 int win32;
159 int width;
160 int height;
161 int stride;
162 int format;
163 int usage;
164 int buffer_id;
165 int generation;
166 union native_handle_buffer native_handle;
169 struct ioctl_android_queueBuffer
171 struct ioctl_header hdr;
172 int buffer_id;
173 int generation;
176 struct ioctl_android_cancelBuffer
178 struct ioctl_header hdr;
179 int buffer_id;
180 int generation;
183 struct ioctl_android_query
185 struct ioctl_header hdr;
186 int what;
187 int value;
190 struct ioctl_android_perform
192 struct ioctl_header hdr;
193 int operation;
194 int args[4];
197 struct ioctl_android_set_swap_interval
199 struct ioctl_header hdr;
200 int interval;
203 struct ioctl_android_set_window_parent
205 struct ioctl_header hdr;
206 int parent;
207 float scale;
210 struct ioctl_android_set_capture
212 struct ioctl_header hdr;
215 struct ioctl_android_set_cursor
217 struct ioctl_header hdr;
218 int id;
219 int width;
220 int height;
221 int hotspotx;
222 int hotspoty;
223 int bits[1];
226 static struct gralloc_module_t *gralloc_module;
227 static struct gralloc1_device *gralloc1_device;
228 static BOOL gralloc1_caps[GRALLOC1_LAST_CAPABILITY + 1];
230 static gralloc1_error_t (*gralloc1_retain)( gralloc1_device_t *device, buffer_handle_t buffer );
231 static gralloc1_error_t (*gralloc1_release)( gralloc1_device_t *device, buffer_handle_t buffer );
232 static gralloc1_error_t (*gralloc1_lock)( gralloc1_device_t *device, buffer_handle_t buffer,
233 uint64_t producerUsage, uint64_t consumerUsage,
234 const gralloc1_rect_t *accessRegion, void **outData,
235 int32_t acquireFence );
236 static gralloc1_error_t (*gralloc1_unlock)( gralloc1_device_t *device, buffer_handle_t buffer,
237 int32_t *outReleaseFence );
239 static inline BOOL is_in_desktop_process(void)
241 return thread != NULL;
244 static inline DWORD current_client_id(void)
246 return HandleToUlong( PsGetCurrentProcessId() );
249 static inline BOOL is_client_in_process(void)
251 return current_client_id() == GetCurrentProcessId();
254 #ifdef __i386__ /* the Java VM uses %fs/%gs for its own purposes, so we need to wrap the calls */
256 static WORD orig_fs, java_fs;
257 static inline void wrap_java_call(void) { __asm__( "mov %0,%%fs" :: "r" (java_fs) ); }
258 static inline void unwrap_java_call(void) { __asm__( "mov %0,%%fs" :: "r" (orig_fs) ); }
259 static inline void init_java_thread( JavaVM *java_vm )
261 java_fs = *p_java_gdt_sel;
262 __asm__( "mov %%fs,%0" : "=r" (orig_fs) );
263 __asm__( "mov %0,%%fs" :: "r" (java_fs) );
264 (*java_vm)->AttachCurrentThread( java_vm, &jni_env, 0 );
265 if (!*p_java_gdt_sel) __asm__( "mov %%fs,%0" : "=r" (java_fs) );
266 __asm__( "mov %0,%%fs" :: "r" (orig_fs) );
269 #elif defined(__x86_64__)
271 #include <asm/prctl.h>
272 #include <asm/unistd.h>
273 static void *orig_teb, *java_teb;
274 static inline int arch_prctl( int func, void *ptr ) { return syscall( __NR_arch_prctl, func, ptr ); }
275 static inline void wrap_java_call(void) { arch_prctl( ARCH_SET_GS, java_teb ); }
276 static inline void unwrap_java_call(void) { arch_prctl( ARCH_SET_GS, orig_teb ); }
277 static inline void init_java_thread( JavaVM *java_vm )
279 arch_prctl( ARCH_GET_GS, &orig_teb );
280 (*java_vm)->AttachCurrentThread( java_vm, &jni_env, 0 );
281 arch_prctl( ARCH_GET_GS, &java_teb );
282 arch_prctl( ARCH_SET_GS, orig_teb );
285 #else
286 static inline void wrap_java_call(void) { }
287 static inline void unwrap_java_call(void) { }
288 static inline void init_java_thread( JavaVM *java_vm ) { (*java_vm)->AttachCurrentThread( java_vm, &jni_env, 0 ); }
289 #endif /* __i386__ */
291 static struct native_win_data *data_map[65536];
293 static unsigned int data_map_idx( HWND hwnd, BOOL opengl )
295 /* window handles are always even, so use low-order bit for opengl flag */
296 return LOWORD(hwnd) + !!opengl;
299 static struct native_win_data *get_native_win_data( HWND hwnd, BOOL opengl )
301 struct native_win_data *data = data_map[data_map_idx( hwnd, opengl )];
303 if (data && data->hwnd == hwnd && !data->opengl == !opengl) return data;
304 WARN( "unknown win %p opengl %u\n", hwnd, opengl );
305 return NULL;
308 static struct native_win_data *get_ioctl_native_win_data( const struct ioctl_header *hdr )
310 return get_native_win_data( LongToHandle(hdr->hwnd), hdr->opengl );
313 static int get_ioctl_win_parent( HWND parent )
315 if (parent != GetDesktopWindow() && !GetAncestor( parent, GA_PARENT ))
316 return HandleToLong( HWND_MESSAGE );
317 return HandleToLong( parent );
320 static void wait_fence_and_close( int fence )
322 __s32 timeout = 1000; /* FIXME: should be -1 for infinite timeout */
324 if (fence == -1) return;
325 ioctl( fence, SYNC_IOC_WAIT, &timeout );
326 close( fence );
329 static int duplicate_fd( HANDLE client, int fd )
331 HANDLE handle, ret = 0;
333 if (!wine_server_fd_to_handle( dup(fd), GENERIC_READ | SYNCHRONIZE, 0, &handle ))
334 DuplicateHandle( GetCurrentProcess(), handle, client, &ret,
335 0, FALSE, DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE );
337 if (!ret) return -1;
338 return HandleToLong( ret );
341 static int map_native_handle( union native_handle_buffer *dest, const native_handle_t *src,
342 HANDLE mapping, HANDLE client )
344 const size_t size = offsetof( native_handle_t, data[src->numFds + src->numInts] );
345 int i;
347 if (mapping) /* only duplicate the mapping handle */
349 HANDLE ret = 0;
350 if (!DuplicateHandle( GetCurrentProcess(), mapping, client, &ret,
351 0, FALSE, DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE ))
352 return -ENOSPC;
353 dest->handle.numFds = 0;
354 dest->handle.numInts = 1;
355 dest->handle.data[0] = HandleToLong( ret );
356 return 0;
358 if (is_client_in_process()) /* transfer the actual handle pointer */
360 dest->handle.numFds = 0;
361 dest->handle.numInts = sizeof(src) / sizeof(int);
362 memcpy( dest->handle.data, &src, sizeof(src) );
363 return 0;
365 if (size > sizeof(*dest)) return -ENOSPC;
366 memcpy( dest, src, size );
367 /* transfer file descriptors to the client process */
368 for (i = 0; i < dest->handle.numFds; i++)
369 dest->handle.data[i] = duplicate_fd( client, src->data[i] );
370 return 0;
373 static native_handle_t *unmap_native_handle( const native_handle_t *src )
375 const size_t size = offsetof( native_handle_t, data[src->numFds + src->numInts] );
376 native_handle_t *dest;
377 int i;
379 if (!is_in_desktop_process())
381 dest = malloc( size );
382 memcpy( dest, src, size );
383 /* fetch file descriptors passed from the server process */
384 for (i = 0; i < dest->numFds; i++)
385 wine_server_handle_to_fd( LongToHandle(src->data[i]), GENERIC_READ | SYNCHRONIZE,
386 &dest->data[i], NULL );
388 else memcpy( &dest, src->data, sizeof(dest) );
389 return dest;
392 static void close_native_handle( native_handle_t *handle )
394 int i;
396 for (i = 0; i < handle->numFds; i++) close( handle->data[i] );
397 free( handle );
400 /* insert a buffer index at the head of the LRU list */
401 static void insert_buffer_lru( struct native_win_data *win, int index )
403 unsigned int i;
405 for (i = 0; i < NB_CACHED_BUFFERS; i++)
407 if (win->buffer_lru[i] == index) break;
408 if (win->buffer_lru[i] == -1) break;
411 assert( i < NB_CACHED_BUFFERS );
412 memmove( win->buffer_lru + 1, win->buffer_lru, i * sizeof(win->buffer_lru[0]) );
413 win->buffer_lru[0] = index;
416 static int register_buffer( struct native_win_data *win, struct ANativeWindowBuffer *buffer,
417 HANDLE *mapping, int *is_new )
419 unsigned int i;
421 *is_new = 0;
422 for (i = 0; i < NB_CACHED_BUFFERS; i++)
424 if (win->buffers[i] == buffer) goto done;
425 if (!win->buffers[i]) break;
428 if (i == NB_CACHED_BUFFERS)
430 /* reuse the least recently used buffer */
431 i = win->buffer_lru[NB_CACHED_BUFFERS - 1];
432 assert( i < NB_CACHED_BUFFERS );
434 TRACE( "%p %p evicting buffer %p id %d from cache\n",
435 win->hwnd, win->parent, win->buffers[i], i );
436 win->buffers[i]->common.decRef( &win->buffers[i]->common );
437 if (win->mappings[i]) UnmapViewOfFile( win->mappings[i] );
440 win->buffers[i] = buffer;
441 win->mappings[i] = NULL;
443 if (mapping)
445 *mapping = CreateFileMappingW( INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0,
446 buffer->stride * buffer->height * 4, NULL );
447 win->mappings[i] = MapViewOfFile( *mapping, FILE_MAP_READ, 0, 0, 0 );
449 buffer->common.incRef( &buffer->common );
450 *is_new = 1;
451 TRACE( "%p %p %p -> %d\n", win->hwnd, win->parent, buffer, i );
453 done:
454 insert_buffer_lru( win, i );
455 return i;
458 static struct ANativeWindowBuffer *get_registered_buffer( struct native_win_data *win, int id )
460 if (id < 0 || id >= NB_CACHED_BUFFERS || !win->buffers[id])
462 ERR( "unknown buffer %d for %p %p\n", id, win->hwnd, win->parent );
463 return NULL;
465 return win->buffers[id];
468 static void release_native_window( struct native_win_data *data )
470 unsigned int i;
472 if (data->parent) pANativeWindow_release( data->parent );
473 for (i = 0; i < NB_CACHED_BUFFERS; i++)
475 if (data->buffers[i]) data->buffers[i]->common.decRef( &data->buffers[i]->common );
476 if (data->mappings[i]) UnmapViewOfFile( data->mappings[i] );
477 data->buffer_lru[i] = -1;
479 memset( data->buffers, 0, sizeof(data->buffers) );
480 memset( data->mappings, 0, sizeof(data->mappings) );
483 static void free_native_win_data( struct native_win_data *data )
485 unsigned int idx = data_map_idx( data->hwnd, data->opengl );
487 InterlockedCompareExchangePointer( (void **)&capture_window, 0, data->hwnd );
488 release_native_window( data );
489 HeapFree( GetProcessHeap(), 0, data );
490 data_map[idx] = NULL;
493 static struct native_win_data *create_native_win_data( HWND hwnd, BOOL opengl )
495 unsigned int i, idx = data_map_idx( hwnd, opengl );
496 struct native_win_data *data = data_map[idx];
498 if (data)
500 WARN( "data for %p not freed correctly\n", data->hwnd );
501 free_native_win_data( data );
503 if (!(data = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data) ))) return NULL;
504 data->hwnd = hwnd;
505 data->opengl = opengl;
506 if (!opengl) data->api = NATIVE_WINDOW_API_CPU;
507 data->buffer_format = PF_BGRA_8888;
508 data_map[idx] = data;
509 for (i = 0; i < NB_CACHED_BUFFERS; i++) data->buffer_lru[i] = -1;
510 return data;
513 static void CALLBACK register_native_window_callback( ULONG_PTR arg1, ULONG_PTR arg2, ULONG_PTR arg3 )
515 HWND hwnd = (HWND)arg1;
516 struct ANativeWindow *win = (struct ANativeWindow *)arg2;
517 BOOL opengl = arg3;
518 struct native_win_data *data = get_native_win_data( hwnd, opengl );
520 if (!win) return; /* do nothing and hold on to the window until we get a new surface */
522 if (!data || data->parent == win)
524 pANativeWindow_release( win );
525 if (data) PostMessageW( hwnd, WM_ANDROID_REFRESH, opengl, 0 );
526 TRACE( "%p -> %p win %p (unchanged)\n", hwnd, data, win );
527 return;
530 release_native_window( data );
531 data->parent = win;
532 data->generation++;
533 wrap_java_call();
534 if (data->api) win->perform( win, NATIVE_WINDOW_API_CONNECT, data->api );
535 win->perform( win, NATIVE_WINDOW_SET_BUFFERS_FORMAT, data->buffer_format );
536 win->setSwapInterval( win, data->swap_interval );
537 unwrap_java_call();
538 PostMessageW( hwnd, WM_ANDROID_REFRESH, opengl, 0 );
539 TRACE( "%p -> %p win %p\n", hwnd, data, win );
542 /* register a native window received from the Java side for use in ioctls */
543 void register_native_window( HWND hwnd, struct ANativeWindow *win, BOOL opengl )
545 NtQueueApcThread( thread, register_native_window_callback, (ULONG_PTR)hwnd, (ULONG_PTR)win, opengl );
548 void init_gralloc( const struct hw_module_t *module )
550 struct hw_device_t *device;
551 int ret;
553 TRACE( "got module %p ver %u.%u id %s name %s author %s\n",
554 module, module->module_api_version >> 8, module->module_api_version & 0xff,
555 debugstr_a(module->id), debugstr_a(module->name), debugstr_a(module->author) );
557 switch (module->module_api_version >> 8)
559 case 0:
560 gralloc_module = (struct gralloc_module_t *)module;
561 break;
562 case 1:
563 if (!(ret = module->methods->open( module, GRALLOC_HARDWARE_MODULE_ID, &device )))
565 int32_t caps[64];
566 uint32_t i, count = ARRAY_SIZE(caps);
568 gralloc1_device = (struct gralloc1_device *)device;
569 gralloc1_retain = gralloc1_device->getFunction( gralloc1_device, GRALLOC1_FUNCTION_RETAIN );
570 gralloc1_release = gralloc1_device->getFunction( gralloc1_device, GRALLOC1_FUNCTION_RELEASE );
571 gralloc1_lock = gralloc1_device->getFunction( gralloc1_device, GRALLOC1_FUNCTION_LOCK );
572 gralloc1_unlock = gralloc1_device->getFunction( gralloc1_device, GRALLOC1_FUNCTION_UNLOCK );
573 TRACE( "got device version %u funcs %p %p %p %p\n", device->version,
574 gralloc1_retain, gralloc1_release, gralloc1_lock, gralloc1_unlock );
576 gralloc1_device->getCapabilities( gralloc1_device, &count, caps );
577 if (count == ARRAY_SIZE(caps)) ERR( "too many gralloc capabilities\n" );
578 for (i = 0; i < count; i++)
579 if (caps[i] < ARRAY_SIZE(gralloc1_caps)) gralloc1_caps[caps[i]] = TRUE;
581 else ERR( "failed to open gralloc err %d\n", ret );
582 break;
583 default:
584 ERR( "unknown gralloc module version %u\n", module->module_api_version >> 8 );
585 break;
589 static int gralloc_grab_buffer( struct ANativeWindowBuffer *buffer )
591 if (gralloc1_device)
592 return gralloc1_retain( gralloc1_device, buffer->handle );
593 if (gralloc_module)
594 return gralloc_module->registerBuffer( gralloc_module, buffer->handle );
595 return -ENODEV;
598 static void gralloc_release_buffer( struct ANativeWindowBuffer *buffer )
600 if (gralloc1_device) gralloc1_release( gralloc1_device, buffer->handle );
601 else if (gralloc_module) gralloc_module->unregisterBuffer( gralloc_module, buffer->handle );
603 if (!gralloc1_caps[GRALLOC1_CAPABILITY_RELEASE_IMPLY_DELETE])
604 close_native_handle( (native_handle_t *)buffer->handle );
607 static int gralloc_lock( struct ANativeWindowBuffer *buffer, void **bits )
609 if (gralloc1_device)
611 gralloc1_rect_t rect = { 0, 0, buffer->width, buffer->height };
612 return gralloc1_lock( gralloc1_device, buffer->handle,
613 GRALLOC1_PRODUCER_USAGE_CPU_READ_OFTEN |
614 GRALLOC1_PRODUCER_USAGE_CPU_WRITE_OFTEN,
615 GRALLOC1_CONSUMER_USAGE_NONE, &rect, bits, -1 );
617 if (gralloc_module)
618 return gralloc_module->lock( gralloc_module, buffer->handle,
619 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
620 0, 0, buffer->width, buffer->height, bits );
622 *bits = ((struct native_buffer_wrapper *)buffer)->bits;
623 return 0;
626 static void gralloc_unlock( struct ANativeWindowBuffer *buffer )
628 if (gralloc1_device)
630 int fence;
631 gralloc1_unlock( gralloc1_device, buffer->handle, &fence );
632 wait_fence_and_close( fence );
634 else if (gralloc_module) gralloc_module->unlock( gralloc_module, buffer->handle );
637 /* get the capture window stored in the desktop process */
638 HWND get_capture_window(void)
640 return capture_window;
643 static NTSTATUS android_error_to_status( int err )
645 switch (err)
647 case 0: return STATUS_SUCCESS;
648 case -ENOMEM: return STATUS_NO_MEMORY;
649 case -ENOSYS: return STATUS_NOT_SUPPORTED;
650 case -EINVAL: return STATUS_INVALID_PARAMETER;
651 case -ENOENT: return STATUS_INVALID_HANDLE;
652 case -EPERM: return STATUS_ACCESS_DENIED;
653 case -ENODEV: return STATUS_NO_SUCH_DEVICE;
654 case -EEXIST: return STATUS_DUPLICATE_NAME;
655 case -EPIPE: return STATUS_PIPE_DISCONNECTED;
656 case -ENODATA: return STATUS_NO_MORE_FILES;
657 case -ETIMEDOUT: return STATUS_IO_TIMEOUT;
658 case -EBADMSG: return STATUS_INVALID_DEVICE_REQUEST;
659 case -EWOULDBLOCK: return STATUS_DEVICE_NOT_READY;
660 default:
661 FIXME( "unmapped error %d\n", err );
662 return STATUS_UNSUCCESSFUL;
666 static int status_to_android_error( NTSTATUS status )
668 switch (status)
670 case STATUS_SUCCESS: return 0;
671 case STATUS_NO_MEMORY: return -ENOMEM;
672 case STATUS_NOT_SUPPORTED: return -ENOSYS;
673 case STATUS_INVALID_PARAMETER: return -EINVAL;
674 case STATUS_BUFFER_OVERFLOW: return -EINVAL;
675 case STATUS_INVALID_HANDLE: return -ENOENT;
676 case STATUS_ACCESS_DENIED: return -EPERM;
677 case STATUS_NO_SUCH_DEVICE: return -ENODEV;
678 case STATUS_DUPLICATE_NAME: return -EEXIST;
679 case STATUS_PIPE_DISCONNECTED: return -EPIPE;
680 case STATUS_NO_MORE_FILES: return -ENODATA;
681 case STATUS_IO_TIMEOUT: return -ETIMEDOUT;
682 case STATUS_INVALID_DEVICE_REQUEST: return -EBADMSG;
683 case STATUS_DEVICE_NOT_READY: return -EWOULDBLOCK;
684 default:
685 FIXME( "unmapped status %08x\n", status );
686 return -EINVAL;
690 static jobject load_java_method( jmethodID *method, const char *name, const char *args )
692 jobject object = *p_java_object;
694 if (!*method)
696 jclass class;
698 wrap_java_call();
699 class = (*jni_env)->GetObjectClass( jni_env, object );
700 *method = (*jni_env)->GetMethodID( jni_env, class, name, args );
701 unwrap_java_call();
702 if (!*method)
704 FIXME( "method %s not found\n", name );
705 return NULL;
708 return object;
711 static void create_desktop_window( HWND hwnd )
713 static jmethodID method;
714 jobject object;
716 if (!(object = load_java_method( &method, "createDesktopWindow", "(I)V" ))) return;
718 wrap_java_call();
719 (*jni_env)->CallVoidMethod( jni_env, object, method, HandleToLong( hwnd ));
720 unwrap_java_call();
723 static NTSTATUS createWindow_ioctl( void *data, DWORD in_size, DWORD out_size, ULONG_PTR *ret_size )
725 static jmethodID method;
726 jobject object;
727 struct ioctl_android_create_window *res = data;
728 struct native_win_data *win_data;
729 DWORD pid = current_client_id();
731 if (in_size < sizeof(*res)) return STATUS_INVALID_PARAMETER;
733 if (!(win_data = create_native_win_data( LongToHandle(res->hdr.hwnd), res->hdr.opengl )))
734 return STATUS_NO_MEMORY;
736 TRACE( "hwnd %08x opengl %u parent %08x\n", res->hdr.hwnd, res->hdr.opengl, res->parent );
738 if (!(object = load_java_method( &method, "createWindow", "(IZIFI)V" ))) return STATUS_NOT_SUPPORTED;
740 wrap_java_call();
741 (*jni_env)->CallVoidMethod( jni_env, object, method, res->hdr.hwnd, res->hdr.opengl, res->parent, res->scale, pid );
742 unwrap_java_call();
743 return STATUS_SUCCESS;
746 static NTSTATUS destroyWindow_ioctl( void *data, DWORD in_size, DWORD out_size, ULONG_PTR *ret_size )
748 static jmethodID method;
749 jobject object;
750 struct ioctl_android_destroy_window *res = data;
751 struct native_win_data *win_data;
753 if (in_size < sizeof(*res)) return STATUS_INVALID_PARAMETER;
755 win_data = get_ioctl_native_win_data( &res->hdr );
757 TRACE( "hwnd %08x opengl %u\n", res->hdr.hwnd, res->hdr.opengl );
759 if (!(object = load_java_method( &method, "destroyWindow", "(I)V" ))) return STATUS_NOT_SUPPORTED;
761 wrap_java_call();
762 (*jni_env)->CallVoidMethod( jni_env, object, method, res->hdr.hwnd );
763 unwrap_java_call();
764 if (win_data) free_native_win_data( win_data );
765 return STATUS_SUCCESS;
768 static NTSTATUS windowPosChanged_ioctl( void *data, DWORD in_size, DWORD out_size, ULONG_PTR *ret_size )
770 static jmethodID method;
771 jobject object;
772 struct ioctl_android_window_pos_changed *res = data;
774 if (in_size < sizeof(*res)) return STATUS_INVALID_PARAMETER;
776 TRACE( "hwnd %08x win %s client %s visible %s style %08x flags %08x after %08x owner %08x\n",
777 res->hdr.hwnd, wine_dbgstr_rect(&res->window_rect), wine_dbgstr_rect(&res->client_rect),
778 wine_dbgstr_rect(&res->visible_rect), res->style, res->flags, res->after, res->owner );
780 if (!(object = load_java_method( &method, "windowPosChanged", "(IIIIIIIIIIIIIIIII)V" )))
781 return STATUS_NOT_SUPPORTED;
783 wrap_java_call();
784 (*jni_env)->CallVoidMethod( jni_env, object, method, res->hdr.hwnd, res->flags, res->after, res->owner, res->style,
785 res->window_rect.left, res->window_rect.top, res->window_rect.right, res->window_rect.bottom,
786 res->client_rect.left, res->client_rect.top, res->client_rect.right, res->client_rect.bottom,
787 res->visible_rect.left, res->visible_rect.top, res->visible_rect.right, res->visible_rect.bottom );
788 unwrap_java_call();
789 return STATUS_SUCCESS;
792 static NTSTATUS dequeueBuffer_ioctl( void *data, DWORD in_size, DWORD out_size, ULONG_PTR *ret_size )
794 struct ANativeWindow *parent;
795 struct ioctl_android_dequeueBuffer *res = data;
796 struct native_win_data *win_data;
797 struct ANativeWindowBuffer *buffer;
798 int fence, ret, is_new;
800 if (out_size < sizeof( *res )) return STATUS_BUFFER_OVERFLOW;
802 if (in_size < offsetof( struct ioctl_android_dequeueBuffer, native_handle ))
803 return STATUS_INVALID_PARAMETER;
805 if (!(win_data = get_ioctl_native_win_data( &res->hdr ))) return STATUS_INVALID_HANDLE;
806 if (!(parent = win_data->parent)) return STATUS_DEVICE_NOT_READY;
808 *ret_size = offsetof( struct ioctl_android_dequeueBuffer, native_handle );
809 wrap_java_call();
810 ret = parent->dequeueBuffer( parent, &buffer, &fence );
811 unwrap_java_call();
812 if (!ret)
814 HANDLE mapping = 0;
816 TRACE( "%08x got buffer %p fence %d\n", res->hdr.hwnd, buffer, fence );
817 res->width = buffer->width;
818 res->height = buffer->height;
819 res->stride = buffer->stride;
820 res->format = buffer->format;
821 res->usage = buffer->usage;
822 res->buffer_id = register_buffer( win_data, buffer, res->win32 ? &mapping : NULL, &is_new );
823 res->generation = win_data->generation;
824 if (is_new)
826 HANDLE process = OpenProcess( PROCESS_DUP_HANDLE, FALSE, current_client_id() );
827 map_native_handle( &res->native_handle, buffer->handle, mapping, process );
828 CloseHandle( process );
829 *ret_size = sizeof( *res );
831 wait_fence_and_close( fence );
832 return STATUS_SUCCESS;
834 ERR( "%08x failed %d\n", res->hdr.hwnd, ret );
835 return android_error_to_status( ret );
838 static NTSTATUS cancelBuffer_ioctl( void *data, DWORD in_size, DWORD out_size, ULONG_PTR *ret_size )
840 struct ioctl_android_cancelBuffer *res = data;
841 struct ANativeWindow *parent;
842 struct ANativeWindowBuffer *buffer;
843 struct native_win_data *win_data;
844 int ret;
846 if (in_size < sizeof(*res)) return STATUS_INVALID_PARAMETER;
848 if (!(win_data = get_ioctl_native_win_data( &res->hdr ))) return STATUS_INVALID_HANDLE;
849 if (!(parent = win_data->parent)) return STATUS_DEVICE_NOT_READY;
850 if (res->generation != win_data->generation) return STATUS_SUCCESS; /* obsolete buffer, ignore */
852 if (!(buffer = get_registered_buffer( win_data, res->buffer_id ))) return STATUS_INVALID_HANDLE;
854 TRACE( "%08x buffer %p\n", res->hdr.hwnd, buffer );
855 wrap_java_call();
856 ret = parent->cancelBuffer( parent, buffer, -1 );
857 unwrap_java_call();
858 return android_error_to_status( ret );
861 static NTSTATUS queueBuffer_ioctl( void *data, DWORD in_size, DWORD out_size, ULONG_PTR *ret_size )
863 struct ioctl_android_queueBuffer *res = data;
864 struct ANativeWindow *parent;
865 struct ANativeWindowBuffer *buffer;
866 struct native_win_data *win_data;
867 int ret;
869 if (in_size < sizeof(*res)) return STATUS_INVALID_PARAMETER;
871 if (!(win_data = get_ioctl_native_win_data( &res->hdr ))) return STATUS_INVALID_HANDLE;
872 if (!(parent = win_data->parent)) return STATUS_DEVICE_NOT_READY;
873 if (res->generation != win_data->generation) return STATUS_SUCCESS; /* obsolete buffer, ignore */
875 if (!(buffer = get_registered_buffer( win_data, res->buffer_id ))) return STATUS_INVALID_HANDLE;
877 TRACE( "%08x buffer %p mapping %p\n", res->hdr.hwnd, buffer, win_data->mappings[res->buffer_id] );
878 if (win_data->mappings[res->buffer_id])
880 void *bits;
881 ret = gralloc_lock( buffer, &bits );
882 if (ret) return android_error_to_status( ret );
883 memcpy( bits, win_data->mappings[res->buffer_id], buffer->stride * buffer->height * 4 );
884 gralloc_unlock( buffer );
886 wrap_java_call();
887 ret = parent->queueBuffer( parent, buffer, -1 );
888 unwrap_java_call();
889 return android_error_to_status( ret );
892 static NTSTATUS query_ioctl( void *data, DWORD in_size, DWORD out_size, ULONG_PTR *ret_size )
894 struct ioctl_android_query *res = data;
895 struct ANativeWindow *parent;
896 struct native_win_data *win_data;
897 int ret;
899 if (in_size < sizeof(*res)) return STATUS_INVALID_PARAMETER;
900 if (out_size < sizeof(*res)) return STATUS_BUFFER_OVERFLOW;
902 if (!(win_data = get_ioctl_native_win_data( &res->hdr ))) return STATUS_INVALID_HANDLE;
903 if (!(parent = win_data->parent)) return STATUS_DEVICE_NOT_READY;
905 *ret_size = sizeof( *res );
906 wrap_java_call();
907 ret = parent->query( parent, res->what, &res->value );
908 unwrap_java_call();
909 return android_error_to_status( ret );
912 static NTSTATUS perform_ioctl( void *data, DWORD in_size, DWORD out_size, ULONG_PTR *ret_size )
914 struct ioctl_android_perform *res = data;
915 struct ANativeWindow *parent;
916 struct native_win_data *win_data;
917 int ret = -ENOENT;
919 if (in_size < sizeof(*res)) return STATUS_INVALID_PARAMETER;
921 if (!(win_data = get_ioctl_native_win_data( &res->hdr ))) return STATUS_INVALID_HANDLE;
922 if (!(parent = win_data->parent)) return STATUS_DEVICE_NOT_READY;
924 switch (res->operation)
926 case NATIVE_WINDOW_SET_BUFFERS_FORMAT:
927 wrap_java_call();
928 ret = parent->perform( parent, res->operation, res->args[0] );
929 unwrap_java_call();
930 if (!ret) win_data->buffer_format = res->args[0];
931 break;
932 case NATIVE_WINDOW_API_CONNECT:
933 wrap_java_call();
934 ret = parent->perform( parent, res->operation, res->args[0] );
935 unwrap_java_call();
936 if (!ret) win_data->api = res->args[0];
937 break;
938 case NATIVE_WINDOW_API_DISCONNECT:
939 wrap_java_call();
940 ret = parent->perform( parent, res->operation, res->args[0] );
941 unwrap_java_call();
942 if (!ret) win_data->api = 0;
943 break;
944 case NATIVE_WINDOW_SET_USAGE:
945 case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM:
946 case NATIVE_WINDOW_SET_SCALING_MODE:
947 wrap_java_call();
948 ret = parent->perform( parent, res->operation, res->args[0] );
949 unwrap_java_call();
950 break;
951 case NATIVE_WINDOW_SET_BUFFER_COUNT:
952 wrap_java_call();
953 ret = parent->perform( parent, res->operation, (size_t)res->args[0] );
954 unwrap_java_call();
955 break;
956 case NATIVE_WINDOW_SET_BUFFERS_DIMENSIONS:
957 case NATIVE_WINDOW_SET_BUFFERS_USER_DIMENSIONS:
958 wrap_java_call();
959 ret = parent->perform( parent, res->operation, res->args[0], res->args[1] );
960 unwrap_java_call();
961 break;
962 case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY:
963 wrap_java_call();
964 ret = parent->perform( parent, res->operation, res->args[0], res->args[1], res->args[2] );
965 unwrap_java_call();
966 break;
967 case NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP:
968 wrap_java_call();
969 ret = parent->perform( parent, res->operation, res->args[0] | ((int64_t)res->args[1] << 32) );
970 unwrap_java_call();
971 break;
972 case NATIVE_WINDOW_CONNECT:
973 case NATIVE_WINDOW_DISCONNECT:
974 case NATIVE_WINDOW_UNLOCK_AND_POST:
975 wrap_java_call();
976 ret = parent->perform( parent, res->operation );
977 unwrap_java_call();
978 break;
979 case NATIVE_WINDOW_SET_CROP:
981 android_native_rect_t rect;
982 rect.left = res->args[0];
983 rect.top = res->args[1];
984 rect.right = res->args[2];
985 rect.bottom = res->args[3];
986 wrap_java_call();
987 ret = parent->perform( parent, res->operation, &rect );
988 unwrap_java_call();
989 break;
991 case NATIVE_WINDOW_LOCK:
992 default:
993 FIXME( "unsupported perform op %d\n", res->operation );
994 break;
996 return android_error_to_status( ret );
999 static NTSTATUS setSwapInterval_ioctl( void *data, DWORD in_size, DWORD out_size, ULONG_PTR *ret_size )
1001 struct ioctl_android_set_swap_interval *res = data;
1002 struct ANativeWindow *parent;
1003 struct native_win_data *win_data;
1004 int ret;
1006 if (in_size < sizeof(*res)) return STATUS_INVALID_PARAMETER;
1008 if (!(win_data = get_ioctl_native_win_data( &res->hdr ))) return STATUS_INVALID_HANDLE;
1009 win_data->swap_interval = res->interval;
1011 if (!(parent = win_data->parent)) return STATUS_SUCCESS;
1012 wrap_java_call();
1013 ret = parent->setSwapInterval( parent, res->interval );
1014 unwrap_java_call();
1015 return android_error_to_status( ret );
1018 static NTSTATUS setWindowParent_ioctl( void *data, DWORD in_size, DWORD out_size, ULONG_PTR *ret_size )
1020 static jmethodID method;
1021 jobject object;
1022 struct ioctl_android_set_window_parent *res = data;
1023 struct native_win_data *win_data;
1024 DWORD pid = current_client_id();
1026 if (in_size < sizeof(*res)) return STATUS_INVALID_PARAMETER;
1028 if (!(win_data = get_ioctl_native_win_data( &res->hdr ))) return STATUS_INVALID_HANDLE;
1030 TRACE( "hwnd %08x parent %08x\n", res->hdr.hwnd, res->parent );
1032 if (!(object = load_java_method( &method, "setParent", "(IIFI)V" ))) return STATUS_NOT_SUPPORTED;
1034 wrap_java_call();
1035 (*jni_env)->CallVoidMethod( jni_env, object, method, res->hdr.hwnd, res->parent, res->scale, pid );
1036 unwrap_java_call();
1037 return STATUS_SUCCESS;
1040 static NTSTATUS setCapture_ioctl( void *data, DWORD in_size, DWORD out_size, ULONG_PTR *ret_size )
1042 struct ioctl_android_set_capture *res = data;
1044 if (in_size < sizeof(*res)) return STATUS_INVALID_PARAMETER;
1046 if (res->hdr.hwnd && !get_ioctl_native_win_data( &res->hdr )) return STATUS_INVALID_HANDLE;
1048 TRACE( "hwnd %08x\n", res->hdr.hwnd );
1050 InterlockedExchangePointer( (void **)&capture_window, LongToHandle( res->hdr.hwnd ));
1051 return STATUS_SUCCESS;
1054 static NTSTATUS setCursor_ioctl( void *data, DWORD in_size, DWORD out_size, ULONG_PTR *ret_size )
1056 static jmethodID method;
1057 jobject object;
1058 int size;
1059 struct ioctl_android_set_cursor *res = data;
1061 if (in_size < offsetof( struct ioctl_android_set_cursor, bits )) return STATUS_INVALID_PARAMETER;
1063 if (res->width < 0 || res->height < 0 || res->width > 256 || res->height > 256)
1064 return STATUS_INVALID_PARAMETER;
1066 size = res->width * res->height;
1067 if (in_size != offsetof( struct ioctl_android_set_cursor, bits[size] ))
1068 return STATUS_INVALID_PARAMETER;
1070 TRACE( "hwnd %08x size %d\n", res->hdr.hwnd, size );
1072 if (!(object = load_java_method( &method, "setCursor", "(IIIII[I)V" )))
1073 return STATUS_NOT_SUPPORTED;
1075 wrap_java_call();
1077 if (size)
1079 jintArray array = (*jni_env)->NewIntArray( jni_env, size );
1080 (*jni_env)->SetIntArrayRegion( jni_env, array, 0, size, (jint *)res->bits );
1081 (*jni_env)->CallVoidMethod( jni_env, object, method, 0, res->width, res->height,
1082 res->hotspotx, res->hotspoty, array );
1083 (*jni_env)->DeleteLocalRef( jni_env, array );
1085 else (*jni_env)->CallVoidMethod( jni_env, object, method, res->id, 0, 0, 0, 0, 0 );
1087 unwrap_java_call();
1089 return STATUS_SUCCESS;
1092 typedef NTSTATUS (*ioctl_func)( void *in, DWORD in_size, DWORD out_size, ULONG_PTR *ret_size );
1093 static const ioctl_func ioctl_funcs[] =
1095 createWindow_ioctl, /* IOCTL_CREATE_WINDOW */
1096 destroyWindow_ioctl, /* IOCTL_DESTROY_WINDOW */
1097 windowPosChanged_ioctl, /* IOCTL_WINDOW_POS_CHANGED */
1098 setWindowParent_ioctl, /* IOCTL_SET_WINDOW_PARENT */
1099 dequeueBuffer_ioctl, /* IOCTL_DEQUEUE_BUFFER */
1100 queueBuffer_ioctl, /* IOCTL_QUEUE_BUFFER */
1101 cancelBuffer_ioctl, /* IOCTL_CANCEL_BUFFER */
1102 query_ioctl, /* IOCTL_QUERY */
1103 perform_ioctl, /* IOCTL_PERFORM */
1104 setSwapInterval_ioctl, /* IOCTL_SET_SWAP_INT */
1105 setCapture_ioctl, /* IOCTL_SET_CAPTURE */
1106 setCursor_ioctl, /* IOCTL_SET_CURSOR */
1109 static NTSTATUS WINAPI ioctl_callback( DEVICE_OBJECT *device, IRP *irp )
1111 IO_STACK_LOCATION *irpsp = IoGetCurrentIrpStackLocation( irp );
1112 DWORD code = (irpsp->Parameters.DeviceIoControl.IoControlCode - ANDROID_IOCTL(0)) >> 2;
1114 if (code < NB_IOCTLS)
1116 struct ioctl_header *header = irp->AssociatedIrp.SystemBuffer;
1117 DWORD in_size = irpsp->Parameters.DeviceIoControl.InputBufferLength;
1118 ioctl_func func = ioctl_funcs[code];
1120 if (in_size >= sizeof(*header))
1122 irp->IoStatus.Information = 0;
1123 irp->IoStatus.u.Status = func( irp->AssociatedIrp.SystemBuffer, in_size,
1124 irpsp->Parameters.DeviceIoControl.OutputBufferLength,
1125 &irp->IoStatus.Information );
1127 else irp->IoStatus.u.Status = STATUS_INVALID_PARAMETER;
1129 else
1131 FIXME( "ioctl %x not supported\n", irpsp->Parameters.DeviceIoControl.IoControlCode );
1132 irp->IoStatus.u.Status = STATUS_NOT_SUPPORTED;
1134 IoCompleteRequest( irp, IO_NO_INCREMENT );
1135 return STATUS_SUCCESS;
1138 static NTSTATUS CALLBACK init_android_driver( DRIVER_OBJECT *driver, UNICODE_STRING *name )
1140 static const WCHAR device_nameW[] = {'\\','D','e','v','i','c','e','\\','W','i','n','e','A','n','d','r','o','i','d',0 };
1141 static const WCHAR device_linkW[] = {'\\','?','?','\\','W','i','n','e','A','n','d','r','o','i','d',0 };
1143 UNICODE_STRING nameW, linkW;
1144 DEVICE_OBJECT *device;
1145 NTSTATUS status;
1147 driver->MajorFunction[IRP_MJ_DEVICE_CONTROL] = ioctl_callback;
1149 RtlInitUnicodeString( &nameW, device_nameW );
1150 RtlInitUnicodeString( &linkW, device_linkW );
1152 if ((status = IoCreateDevice( driver, 0, &nameW, 0, 0, FALSE, &device ))) return status;
1153 return IoCreateSymbolicLink( &linkW, &nameW );
1156 static DWORD CALLBACK device_thread( void *arg )
1158 static const WCHAR driver_nameW[] = {'\\','D','r','i','v','e','r','\\','W','i','n','e','A','n','d','r','o','i','d',0 };
1160 HANDLE start_event = arg;
1161 UNICODE_STRING nameW;
1162 NTSTATUS status;
1163 JavaVM *java_vm;
1164 DWORD ret;
1166 TRACE( "starting process %x\n", GetCurrentProcessId() );
1168 if (!(java_vm = *p_java_vm)) return 0; /* not running under Java */
1170 init_java_thread( java_vm );
1172 create_desktop_window( GetDesktopWindow() );
1174 RtlInitUnicodeString( &nameW, driver_nameW );
1175 if ((status = IoCreateDriver( &nameW, init_android_driver )))
1177 FIXME( "failed to create driver error %x\n", status );
1178 return status;
1181 stop_event = CreateEventW( NULL, TRUE, FALSE, NULL );
1182 SetEvent( start_event );
1184 ret = wine_ntoskrnl_main_loop( stop_event );
1186 wrap_java_call();
1187 (*java_vm)->DetachCurrentThread( java_vm );
1188 unwrap_java_call();
1189 return ret;
1192 void start_android_device(void)
1194 HANDLE handles[2];
1196 handles[0] = CreateEventW( NULL, TRUE, FALSE, NULL );
1197 handles[1] = thread = CreateThread( NULL, 0, device_thread, handles[0], 0, NULL );
1198 WaitForMultipleObjects( 2, handles, FALSE, INFINITE );
1199 CloseHandle( handles[0] );
1203 /* Client-side ioctl support */
1206 static int android_ioctl( enum android_ioctl code, void *in, DWORD in_size, void *out, DWORD *out_size )
1208 static const WCHAR deviceW[] = {'\\','\\','.','\\','W','i','n','e','A','n','d','r','o','i','d',0 };
1209 static HANDLE device;
1210 IO_STATUS_BLOCK iosb;
1211 NTSTATUS status;
1213 if (!device)
1215 HANDLE file = CreateFileW( deviceW, GENERIC_READ,
1216 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0 );
1217 if (file == INVALID_HANDLE_VALUE) return -ENOENT;
1218 if (InterlockedCompareExchangePointer( &device, file, NULL )) CloseHandle( file );
1221 status = NtDeviceIoControlFile( device, NULL, NULL, NULL, &iosb, ANDROID_IOCTL(code),
1222 in, in_size, out, out_size ? *out_size : 0 );
1223 if (status == STATUS_FILE_DELETED)
1225 WARN( "parent process is gone\n" );
1226 ExitProcess( 1 );
1228 if (out_size) *out_size = iosb.Information;
1229 return status_to_android_error( status );
1232 static void win_incRef( struct android_native_base_t *base )
1234 struct native_win_wrapper *win = (struct native_win_wrapper *)base;
1235 InterlockedIncrement( &win->ref );
1238 static void win_decRef( struct android_native_base_t *base )
1240 struct native_win_wrapper *win = (struct native_win_wrapper *)base;
1241 InterlockedDecrement( &win->ref );
1244 static void buffer_incRef( struct android_native_base_t *base )
1246 struct native_buffer_wrapper *buffer = (struct native_buffer_wrapper *)base;
1247 InterlockedIncrement( &buffer->ref );
1250 static void buffer_decRef( struct android_native_base_t *base )
1252 struct native_buffer_wrapper *buffer = (struct native_buffer_wrapper *)base;
1254 if (!InterlockedDecrement( &buffer->ref ))
1256 if (!is_in_desktop_process()) gralloc_release_buffer( &buffer->buffer );
1257 if (buffer->bits) UnmapViewOfFile( buffer->bits );
1258 HeapFree( GetProcessHeap(), 0, buffer );
1262 static int dequeueBuffer( struct ANativeWindow *window, struct ANativeWindowBuffer **buffer, int *fence )
1264 struct native_win_wrapper *win = (struct native_win_wrapper *)window;
1265 struct ioctl_android_dequeueBuffer res;
1266 DWORD size = sizeof(res);
1267 int ret, use_win32 = !gralloc_module && !gralloc1_device;
1269 res.hdr.hwnd = HandleToLong( win->hwnd );
1270 res.hdr.opengl = win->opengl;
1271 res.win32 = use_win32;
1272 ret = android_ioctl( IOCTL_DEQUEUE_BUFFER,
1273 &res, offsetof( struct ioctl_android_dequeueBuffer, native_handle ),
1274 &res, &size );
1275 if (ret) return ret;
1277 /* if we received the native handle, this is a new buffer */
1278 if (size > offsetof( struct ioctl_android_dequeueBuffer, native_handle ))
1280 struct native_buffer_wrapper *buf = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*buf) );
1282 buf->buffer.common.magic = ANDROID_NATIVE_BUFFER_MAGIC;
1283 buf->buffer.common.version = sizeof( buf->buffer );
1284 buf->buffer.common.incRef = buffer_incRef;
1285 buf->buffer.common.decRef = buffer_decRef;
1286 buf->buffer.width = res.width;
1287 buf->buffer.height = res.height;
1288 buf->buffer.stride = res.stride;
1289 buf->buffer.format = res.format;
1290 buf->buffer.usage = res.usage;
1291 buf->buffer.handle = unmap_native_handle( &res.native_handle.handle );
1292 buf->ref = 1;
1293 buf->hwnd = win->hwnd;
1294 buf->buffer_id = res.buffer_id;
1295 buf->generation = res.generation;
1296 if (win->buffers[res.buffer_id])
1297 win->buffers[res.buffer_id]->buffer.common.decRef(&win->buffers[res.buffer_id]->buffer.common);
1298 win->buffers[res.buffer_id] = buf;
1300 if (use_win32)
1302 HANDLE mapping = LongToHandle( res.native_handle.handle.data[0] );
1303 buf->bits = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 0 );
1304 CloseHandle( mapping );
1306 else if (!is_in_desktop_process())
1308 if ((ret = gralloc_grab_buffer( &buf->buffer )) < 0)
1309 WARN( "hwnd %p, buffer %p failed to register %d %s\n",
1310 win->hwnd, &buf->buffer, ret, strerror(-ret) );
1314 *buffer = &win->buffers[res.buffer_id]->buffer;
1315 *fence = -1;
1317 TRACE( "hwnd %p, buffer %p %dx%d stride %d fmt %d usage %d fence %d\n",
1318 win->hwnd, *buffer, res.width, res.height, res.stride, res.format, res.usage, *fence );
1319 return 0;
1322 static int cancelBuffer( struct ANativeWindow *window, struct ANativeWindowBuffer *buffer, int fence )
1324 struct native_win_wrapper *win = (struct native_win_wrapper *)window;
1325 struct native_buffer_wrapper *buf = (struct native_buffer_wrapper *)buffer;
1326 struct ioctl_android_cancelBuffer cancel;
1328 TRACE( "hwnd %p buffer %p %dx%d stride %d fmt %d usage %d fence %d\n",
1329 win->hwnd, buffer, buffer->width, buffer->height,
1330 buffer->stride, buffer->format, buffer->usage, fence );
1331 cancel.buffer_id = buf->buffer_id;
1332 cancel.generation = buf->generation;
1333 cancel.hdr.hwnd = HandleToLong( win->hwnd );
1334 cancel.hdr.opengl = win->opengl;
1335 wait_fence_and_close( fence );
1336 return android_ioctl( IOCTL_CANCEL_BUFFER, &cancel, sizeof(cancel), NULL, NULL );
1339 static int queueBuffer( struct ANativeWindow *window, struct ANativeWindowBuffer *buffer, int fence )
1341 struct native_win_wrapper *win = (struct native_win_wrapper *)window;
1342 struct native_buffer_wrapper *buf = (struct native_buffer_wrapper *)buffer;
1343 struct ioctl_android_queueBuffer queue;
1345 TRACE( "hwnd %p buffer %p %dx%d stride %d fmt %d usage %d fence %d\n",
1346 win->hwnd, buffer, buffer->width, buffer->height,
1347 buffer->stride, buffer->format, buffer->usage, fence );
1348 queue.buffer_id = buf->buffer_id;
1349 queue.generation = buf->generation;
1350 queue.hdr.hwnd = HandleToLong( win->hwnd );
1351 queue.hdr.opengl = win->opengl;
1352 wait_fence_and_close( fence );
1353 return android_ioctl( IOCTL_QUEUE_BUFFER, &queue, sizeof(queue), NULL, NULL );
1356 static int dequeueBuffer_DEPRECATED( struct ANativeWindow *window, struct ANativeWindowBuffer **buffer )
1358 int fence, ret = dequeueBuffer( window, buffer, &fence );
1360 if (!ret) wait_fence_and_close( fence );
1361 return ret;
1364 static int cancelBuffer_DEPRECATED( struct ANativeWindow *window, struct ANativeWindowBuffer *buffer )
1366 return cancelBuffer( window, buffer, -1 );
1369 static int lockBuffer_DEPRECATED( struct ANativeWindow *window, struct ANativeWindowBuffer *buffer )
1371 return 0; /* nothing to do */
1374 static int queueBuffer_DEPRECATED( struct ANativeWindow *window, struct ANativeWindowBuffer *buffer )
1376 return queueBuffer( window, buffer, -1 );
1379 static int setSwapInterval( struct ANativeWindow *window, int interval )
1381 struct native_win_wrapper *win = (struct native_win_wrapper *)window;
1382 struct ioctl_android_set_swap_interval swap;
1384 TRACE( "hwnd %p interval %d\n", win->hwnd, interval );
1385 swap.hdr.hwnd = HandleToLong( win->hwnd );
1386 swap.hdr.opengl = win->opengl;
1387 swap.interval = interval;
1388 return android_ioctl( IOCTL_SET_SWAP_INT, &swap, sizeof(swap), NULL, NULL );
1391 static int query( const ANativeWindow *window, int what, int *value )
1393 struct native_win_wrapper *win = (struct native_win_wrapper *)window;
1394 struct ioctl_android_query query;
1395 DWORD size = sizeof( query );
1396 int ret;
1398 query.hdr.hwnd = HandleToLong( win->hwnd );
1399 query.hdr.opengl = win->opengl;
1400 query.what = what;
1401 ret = android_ioctl( IOCTL_QUERY, &query, sizeof(query), &query, &size );
1402 TRACE( "hwnd %p what %d got %d -> %p\n", win->hwnd, what, query.value, value );
1403 if (!ret) *value = query.value;
1404 return ret;
1407 static int perform( ANativeWindow *window, int operation, ... )
1409 static const char * const names[] =
1411 "SET_USAGE", "CONNECT", "DISCONNECT", "SET_CROP", "SET_BUFFER_COUNT", "SET_BUFFERS_GEOMETRY",
1412 "SET_BUFFERS_TRANSFORM", "SET_BUFFERS_TIMESTAMP", "SET_BUFFERS_DIMENSIONS", "SET_BUFFERS_FORMAT",
1413 "SET_SCALING_MODE", "LOCK", "UNLOCK_AND_POST", "API_CONNECT", "API_DISCONNECT",
1414 "SET_BUFFERS_USER_DIMENSIONS", "SET_POST_TRANSFORM_CROP"
1417 struct native_win_wrapper *win = (struct native_win_wrapper *)window;
1418 struct ioctl_android_perform perf;
1419 va_list args;
1421 perf.hdr.hwnd = HandleToLong( win->hwnd );
1422 perf.hdr.opengl = win->opengl;
1423 perf.operation = operation;
1424 memset( perf.args, 0, sizeof(perf.args) );
1426 va_start( args, operation );
1427 switch (operation)
1429 case NATIVE_WINDOW_SET_USAGE:
1430 case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM:
1431 case NATIVE_WINDOW_SET_BUFFERS_FORMAT:
1432 case NATIVE_WINDOW_SET_SCALING_MODE:
1433 case NATIVE_WINDOW_API_CONNECT:
1434 case NATIVE_WINDOW_API_DISCONNECT:
1435 perf.args[0] = va_arg( args, int );
1436 TRACE( "hwnd %p %s arg %d\n", win->hwnd, names[operation], perf.args[0] );
1437 break;
1438 case NATIVE_WINDOW_SET_BUFFER_COUNT:
1439 perf.args[0] = va_arg( args, size_t );
1440 TRACE( "hwnd %p %s count %d\n", win->hwnd, names[operation], perf.args[0] );
1441 break;
1442 case NATIVE_WINDOW_SET_BUFFERS_DIMENSIONS:
1443 case NATIVE_WINDOW_SET_BUFFERS_USER_DIMENSIONS:
1444 perf.args[0] = va_arg( args, int );
1445 perf.args[1] = va_arg( args, int );
1446 TRACE( "hwnd %p %s arg %dx%d\n", win->hwnd, names[operation], perf.args[0], perf.args[1] );
1447 break;
1448 case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY:
1449 perf.args[0] = va_arg( args, int );
1450 perf.args[1] = va_arg( args, int );
1451 perf.args[2] = va_arg( args, int );
1452 TRACE( "hwnd %p %s arg %dx%d %d\n", win->hwnd, names[operation],
1453 perf.args[0], perf.args[1], perf.args[2] );
1454 break;
1455 case NATIVE_WINDOW_SET_CROP:
1457 android_native_rect_t *rect = va_arg( args, android_native_rect_t * );
1458 perf.args[0] = rect->left;
1459 perf.args[1] = rect->top;
1460 perf.args[2] = rect->right;
1461 perf.args[3] = rect->bottom;
1462 TRACE( "hwnd %p %s rect %d,%d-%d,%d\n", win->hwnd, names[operation],
1463 perf.args[0], perf.args[1], perf.args[2], perf.args[3] );
1464 break;
1466 case NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP:
1468 int64_t timestamp = va_arg( args, int64_t );
1469 perf.args[0] = timestamp;
1470 perf.args[1] = timestamp >> 32;
1471 TRACE( "hwnd %p %s arg %08x%08x\n", win->hwnd, names[operation], perf.args[1], perf.args[0] );
1472 break;
1474 case NATIVE_WINDOW_LOCK:
1476 struct ANativeWindowBuffer *buffer;
1477 struct ANativeWindow_Buffer *buffer_ret = va_arg( args, ANativeWindow_Buffer * );
1478 ARect *bounds = va_arg( args, ARect * );
1479 int ret = window->dequeueBuffer_DEPRECATED( window, &buffer );
1480 if (!ret)
1482 if ((ret = gralloc_lock( buffer, &buffer_ret->bits )))
1484 WARN( "gralloc->lock %p failed %d %s\n", win->hwnd, ret, strerror(-ret) );
1485 window->cancelBuffer( window, buffer, -1 );
1488 if (!ret)
1490 buffer_ret->width = buffer->width;
1491 buffer_ret->height = buffer->height;
1492 buffer_ret->stride = buffer->stride;
1493 buffer_ret->format = buffer->format;
1494 win->locked_buffer = buffer;
1495 if (bounds)
1497 bounds->left = 0;
1498 bounds->top = 0;
1499 bounds->right = buffer->width;
1500 bounds->bottom = buffer->height;
1503 va_end( args );
1504 TRACE( "hwnd %p %s bits %p ret %d %s\n", win->hwnd, names[operation], buffer_ret->bits, ret, strerror(-ret) );
1505 return ret;
1507 case NATIVE_WINDOW_UNLOCK_AND_POST:
1509 int ret = -EINVAL;
1510 if (win->locked_buffer)
1512 gralloc_unlock( win->locked_buffer );
1513 ret = window->queueBuffer( window, win->locked_buffer, -1 );
1514 win->locked_buffer = NULL;
1516 va_end( args );
1517 TRACE( "hwnd %p %s ret %d\n", win->hwnd, names[operation], ret );
1518 return ret;
1520 case NATIVE_WINDOW_CONNECT:
1521 case NATIVE_WINDOW_DISCONNECT:
1522 TRACE( "hwnd %p %s\n", win->hwnd, names[operation] );
1523 break;
1524 case NATIVE_WINDOW_SET_POST_TRANSFORM_CROP:
1525 default:
1526 FIXME( "unsupported perform hwnd %p op %d %s\n", win->hwnd, operation,
1527 operation < ARRAY_SIZE( names ) ? names[operation] : "???" );
1528 break;
1530 va_end( args );
1531 return android_ioctl( IOCTL_PERFORM, &perf, sizeof(perf), NULL, NULL );
1534 struct ANativeWindow *create_ioctl_window( HWND hwnd, BOOL opengl, float scale )
1536 struct ioctl_android_create_window req;
1537 struct native_win_wrapper *win = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*win) );
1539 if (!win) return NULL;
1541 win->win.common.magic = ANDROID_NATIVE_WINDOW_MAGIC;
1542 win->win.common.version = sizeof(ANativeWindow);
1543 win->win.common.incRef = win_incRef;
1544 win->win.common.decRef = win_decRef;
1545 win->win.setSwapInterval = setSwapInterval;
1546 win->win.dequeueBuffer_DEPRECATED = dequeueBuffer_DEPRECATED;
1547 win->win.lockBuffer_DEPRECATED = lockBuffer_DEPRECATED;
1548 win->win.queueBuffer_DEPRECATED = queueBuffer_DEPRECATED;
1549 win->win.query = query;
1550 win->win.perform = perform;
1551 win->win.cancelBuffer_DEPRECATED = cancelBuffer_DEPRECATED;
1552 win->win.dequeueBuffer = dequeueBuffer;
1553 win->win.queueBuffer = queueBuffer;
1554 win->win.cancelBuffer = cancelBuffer;
1555 win->ref = 1;
1556 win->hwnd = hwnd;
1557 win->opengl = opengl;
1558 TRACE( "-> %p %p opengl=%u\n", win, win->hwnd, opengl );
1560 req.hdr.hwnd = HandleToLong( win->hwnd );
1561 req.hdr.opengl = win->opengl;
1562 req.parent = get_ioctl_win_parent( GetAncestor( hwnd, GA_PARENT ));
1563 req.scale = scale;
1564 android_ioctl( IOCTL_CREATE_WINDOW, &req, sizeof(req), NULL, NULL );
1566 return &win->win;
1569 struct ANativeWindow *grab_ioctl_window( struct ANativeWindow *window )
1571 struct native_win_wrapper *win = (struct native_win_wrapper *)window;
1572 InterlockedIncrement( &win->ref );
1573 return window;
1576 void release_ioctl_window( struct ANativeWindow *window )
1578 struct native_win_wrapper *win = (struct native_win_wrapper *)window;
1579 unsigned int i;
1581 if (InterlockedDecrement( &win->ref ) > 0) return;
1583 TRACE( "%p %p\n", win, win->hwnd );
1584 for (i = 0; i < ARRAY_SIZE( win->buffers ); i++)
1585 if (win->buffers[i]) win->buffers[i]->buffer.common.decRef( &win->buffers[i]->buffer.common );
1587 destroy_ioctl_window( win->hwnd, win->opengl );
1588 HeapFree( GetProcessHeap(), 0, win );
1591 void destroy_ioctl_window( HWND hwnd, BOOL opengl )
1593 struct ioctl_android_destroy_window req;
1595 req.hdr.hwnd = HandleToLong( hwnd );
1596 req.hdr.opengl = opengl;
1597 android_ioctl( IOCTL_DESTROY_WINDOW, &req, sizeof(req), NULL, NULL );
1600 int ioctl_window_pos_changed( HWND hwnd, const RECT *window_rect, const RECT *client_rect,
1601 const RECT *visible_rect, UINT style, UINT flags, HWND after, HWND owner )
1603 struct ioctl_android_window_pos_changed req;
1605 req.hdr.hwnd = HandleToLong( hwnd );
1606 req.hdr.opengl = FALSE;
1607 req.window_rect = *window_rect;
1608 req.client_rect = *client_rect;
1609 req.visible_rect = *visible_rect;
1610 req.style = style;
1611 req.flags = flags;
1612 req.after = HandleToLong( after );
1613 req.owner = HandleToLong( owner );
1614 return android_ioctl( IOCTL_WINDOW_POS_CHANGED, &req, sizeof(req), NULL, NULL );
1617 int ioctl_set_window_parent( HWND hwnd, HWND parent, float scale )
1619 struct ioctl_android_set_window_parent req;
1621 req.hdr.hwnd = HandleToLong( hwnd );
1622 req.hdr.opengl = FALSE;
1623 req.parent = get_ioctl_win_parent( parent );
1624 req.scale = scale;
1625 return android_ioctl( IOCTL_SET_WINDOW_PARENT, &req, sizeof(req), NULL, NULL );
1628 int ioctl_set_capture( HWND hwnd )
1630 struct ioctl_android_set_capture req;
1632 req.hdr.hwnd = HandleToLong( hwnd );
1633 req.hdr.opengl = FALSE;
1634 return android_ioctl( IOCTL_SET_CAPTURE, &req, sizeof(req), NULL, NULL );
1637 int ioctl_set_cursor( int id, int width, int height,
1638 int hotspotx, int hotspoty, const unsigned int *bits )
1640 struct ioctl_android_set_cursor *req;
1641 unsigned int size = offsetof( struct ioctl_android_set_cursor, bits[width * height] );
1642 int ret;
1644 if (!(req = HeapAlloc( GetProcessHeap(), 0, size ))) return -ENOMEM;
1645 req->hdr.hwnd = 0; /* unused */
1646 req->hdr.opengl = FALSE;
1647 req->id = id;
1648 req->width = width;
1649 req->height = height;
1650 req->hotspotx = hotspotx;
1651 req->hotspoty = hotspoty;
1652 memcpy( req->bits, bits, width * height * sizeof(req->bits[0]) );
1653 ret = android_ioctl( IOCTL_SET_CURSOR, req, size, NULL, NULL );
1654 HeapFree( GetProcessHeap(), 0, req );
1655 return ret;