tests: Don't initialize static variables to 0.
[wine.git] / dlls / wineandroid.drv / device.c
blob0b8bf67ef1d3719f400cbe3e7361c3d1b48abd87
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/library.h"
43 #include "wine/debug.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(android);
47 #ifndef SYNC_IOC_WAIT
48 #define SYNC_IOC_WAIT _IOW('>', 0, __s32)
49 #endif
51 extern NTSTATUS CDECL wine_ntoskrnl_main_loop( HANDLE stop_event );
52 static HANDLE stop_event;
53 static HANDLE thread;
54 static JNIEnv *jni_env;
55 static HWND capture_window;
57 #define ANDROIDCONTROLTYPE ((ULONG)'A')
58 #define ANDROID_IOCTL(n) CTL_CODE(ANDROIDCONTROLTYPE, n, METHOD_BUFFERED, FILE_READ_ACCESS)
60 enum android_ioctl
62 IOCTL_CREATE_WINDOW,
63 IOCTL_DESTROY_WINDOW,
64 IOCTL_WINDOW_POS_CHANGED,
65 IOCTL_SET_WINDOW_PARENT,
66 IOCTL_DEQUEUE_BUFFER,
67 IOCTL_QUEUE_BUFFER,
68 IOCTL_CANCEL_BUFFER,
69 IOCTL_QUERY,
70 IOCTL_PERFORM,
71 IOCTL_SET_SWAP_INT,
72 IOCTL_SET_CAPTURE,
73 IOCTL_SET_CURSOR,
74 NB_IOCTLS
77 #define NB_CACHED_BUFFERS 4
79 struct native_buffer_wrapper;
81 /* buffer for storing a variable-size native handle inside an ioctl structure */
82 union native_handle_buffer
84 native_handle_t handle;
85 int space[256];
88 /* data about the native window in the context of the Java process */
89 struct native_win_data
91 struct ANativeWindow *parent;
92 struct ANativeWindowBuffer *buffers[NB_CACHED_BUFFERS];
93 void *mappings[NB_CACHED_BUFFERS];
94 HWND hwnd;
95 BOOL opengl;
96 int generation;
97 int api;
98 int buffer_format;
99 int swap_interval;
100 int buffer_lru[NB_CACHED_BUFFERS];
103 /* wrapper for a native window in the context of the client (non-Java) process */
104 struct native_win_wrapper
106 struct ANativeWindow win;
107 struct native_buffer_wrapper *buffers[NB_CACHED_BUFFERS];
108 struct ANativeWindowBuffer *locked_buffer;
109 HWND hwnd;
110 BOOL opengl;
111 LONG ref;
114 /* wrapper for a native buffer in the context of the client (non-Java) process */
115 struct native_buffer_wrapper
117 struct ANativeWindowBuffer buffer;
118 LONG ref;
119 HWND hwnd;
120 void *bits;
121 int buffer_id;
122 int generation;
123 union native_handle_buffer native_handle;
126 struct ioctl_header
128 int hwnd;
129 BOOL opengl;
132 struct ioctl_android_create_window
134 struct ioctl_header hdr;
135 int parent;
136 float scale;
139 struct ioctl_android_destroy_window
141 struct ioctl_header hdr;
144 struct ioctl_android_window_pos_changed
146 struct ioctl_header hdr;
147 RECT window_rect;
148 RECT client_rect;
149 RECT visible_rect;
150 int style;
151 int flags;
152 int after;
153 int owner;
156 struct ioctl_android_dequeueBuffer
158 struct ioctl_header hdr;
159 int win32;
160 int width;
161 int height;
162 int stride;
163 int format;
164 int usage;
165 int buffer_id;
166 int generation;
167 union native_handle_buffer native_handle;
170 struct ioctl_android_queueBuffer
172 struct ioctl_header hdr;
173 int buffer_id;
174 int generation;
177 struct ioctl_android_cancelBuffer
179 struct ioctl_header hdr;
180 int buffer_id;
181 int generation;
184 struct ioctl_android_query
186 struct ioctl_header hdr;
187 int what;
188 int value;
191 struct ioctl_android_perform
193 struct ioctl_header hdr;
194 int operation;
195 int args[4];
198 struct ioctl_android_set_swap_interval
200 struct ioctl_header hdr;
201 int interval;
204 struct ioctl_android_set_window_parent
206 struct ioctl_header hdr;
207 int parent;
208 float scale;
211 struct ioctl_android_set_capture
213 struct ioctl_header hdr;
216 struct ioctl_android_set_cursor
218 struct ioctl_header hdr;
219 int id;
220 int width;
221 int height;
222 int hotspotx;
223 int hotspoty;
224 int bits[1];
227 static struct gralloc_module_t *gralloc_module;
228 static struct gralloc1_device *gralloc1_device;
229 static BOOL gralloc1_caps[GRALLOC1_LAST_CAPABILITY + 1];
231 static gralloc1_error_t (*gralloc1_retain)( gralloc1_device_t *device, buffer_handle_t buffer );
232 static gralloc1_error_t (*gralloc1_release)( gralloc1_device_t *device, buffer_handle_t buffer );
233 static gralloc1_error_t (*gralloc1_lock)( gralloc1_device_t *device, buffer_handle_t buffer,
234 uint64_t producerUsage, uint64_t consumerUsage,
235 const gralloc1_rect_t *accessRegion, void **outData,
236 int32_t acquireFence );
237 static gralloc1_error_t (*gralloc1_unlock)( gralloc1_device_t *device, buffer_handle_t buffer,
238 int32_t *outReleaseFence );
240 static inline BOOL is_in_desktop_process(void)
242 return thread != NULL;
245 static inline DWORD current_client_id(void)
247 return HandleToUlong( PsGetCurrentProcessId() );
250 static inline BOOL is_client_in_process(void)
252 return current_client_id() == GetCurrentProcessId();
255 #ifdef __i386__ /* the Java VM uses %fs/%gs for its own purposes, so we need to wrap the calls */
257 static WORD orig_fs, java_fs;
258 static inline void wrap_java_call(void) { wine_set_fs( java_fs ); }
259 static inline void unwrap_java_call(void) { wine_set_fs( orig_fs ); }
260 static inline void init_java_thread( JavaVM *java_vm )
262 orig_fs = wine_get_fs();
263 (*java_vm)->AttachCurrentThread( java_vm, &jni_env, 0 );
264 java_fs = wine_get_fs();
265 wine_set_fs( orig_fs );
268 #elif defined(__x86_64__)
270 #include <asm/prctl.h>
271 #include <asm/unistd.h>
272 static void *orig_teb, *java_teb;
273 static inline int arch_prctl( int func, void *ptr ) { return syscall( __NR_arch_prctl, func, ptr ); }
274 static inline void wrap_java_call(void) { arch_prctl( ARCH_SET_GS, java_teb ); }
275 static inline void unwrap_java_call(void) { arch_prctl( ARCH_SET_GS, orig_teb ); }
276 static inline void init_java_thread( JavaVM *java_vm )
278 arch_prctl( ARCH_GET_GS, &orig_teb );
279 (*java_vm)->AttachCurrentThread( java_vm, &jni_env, 0 );
280 arch_prctl( ARCH_GET_GS, &java_teb );
281 arch_prctl( ARCH_SET_GS, orig_teb );
284 #else
285 static inline void wrap_java_call(void) { }
286 static inline void unwrap_java_call(void) { }
287 static inline void init_java_thread( JavaVM *java_vm ) { (*java_vm)->AttachCurrentThread( java_vm, &jni_env, 0 ); }
288 #endif /* __i386__ */
290 static struct native_win_data *data_map[65536];
292 static unsigned int data_map_idx( HWND hwnd, BOOL opengl )
294 /* window handles are always even, so use low-order bit for opengl flag */
295 return LOWORD(hwnd) + !!opengl;
298 static struct native_win_data *get_native_win_data( HWND hwnd, BOOL opengl )
300 struct native_win_data *data = data_map[data_map_idx( hwnd, opengl )];
302 if (data && data->hwnd == hwnd && !data->opengl == !opengl) return data;
303 WARN( "unknown win %p opengl %u\n", hwnd, opengl );
304 return NULL;
307 static struct native_win_data *get_ioctl_native_win_data( const struct ioctl_header *hdr )
309 return get_native_win_data( LongToHandle(hdr->hwnd), hdr->opengl );
312 static int get_ioctl_win_parent( HWND parent )
314 if (parent != GetDesktopWindow() && !GetAncestor( parent, GA_PARENT ))
315 return HandleToLong( HWND_MESSAGE );
316 return HandleToLong( parent );
319 static void wait_fence_and_close( int fence )
321 __s32 timeout = 1000; /* FIXME: should be -1 for infinite timeout */
323 if (fence == -1) return;
324 ioctl( fence, SYNC_IOC_WAIT, &timeout );
325 close( fence );
328 static int duplicate_fd( HANDLE client, int fd )
330 HANDLE handle, ret = 0;
332 if (!wine_server_fd_to_handle( dup(fd), GENERIC_READ | SYNCHRONIZE, 0, &handle ))
333 DuplicateHandle( GetCurrentProcess(), handle, client, &ret,
334 DUPLICATE_SAME_ACCESS, FALSE, DUP_HANDLE_CLOSE_SOURCE );
336 if (!ret) return -1;
337 return HandleToLong( ret );
340 static int map_native_handle( union native_handle_buffer *dest, const native_handle_t *src,
341 HANDLE mapping, HANDLE client )
343 const size_t size = offsetof( native_handle_t, data[src->numFds + src->numInts] );
344 int i;
346 if (mapping) /* only duplicate the mapping handle */
348 HANDLE ret = 0;
349 if (!DuplicateHandle( GetCurrentProcess(), mapping, client, &ret,
350 DUPLICATE_SAME_ACCESS, FALSE, DUP_HANDLE_CLOSE_SOURCE ))
351 return -ENOSPC;
352 dest->handle.numFds = 0;
353 dest->handle.numInts = 1;
354 dest->handle.data[0] = HandleToLong( ret );
355 return 0;
357 if (is_client_in_process()) /* transfer the actual handle pointer */
359 dest->handle.numFds = 0;
360 dest->handle.numInts = sizeof(src) / sizeof(int);
361 memcpy( dest->handle.data, &src, sizeof(src) );
362 return 0;
364 if (size > sizeof(*dest)) return -ENOSPC;
365 memcpy( dest, src, size );
366 /* transfer file descriptors to the client process */
367 for (i = 0; i < dest->handle.numFds; i++)
368 dest->handle.data[i] = duplicate_fd( client, src->data[i] );
369 return 0;
372 static native_handle_t *unmap_native_handle( const native_handle_t *src )
374 const size_t size = offsetof( native_handle_t, data[src->numFds + src->numInts] );
375 native_handle_t *dest;
376 int i;
378 if (!is_in_desktop_process())
380 dest = malloc( size );
381 memcpy( dest, src, size );
382 /* fetch file descriptors passed from the server process */
383 for (i = 0; i < dest->numFds; i++)
384 wine_server_handle_to_fd( LongToHandle(src->data[i]), GENERIC_READ | SYNCHRONIZE,
385 &dest->data[i], NULL );
387 else memcpy( &dest, src->data, sizeof(dest) );
388 return dest;
391 static void close_native_handle( native_handle_t *handle )
393 int i;
395 for (i = 0; i < handle->numFds; i++) close( handle->data[i] );
396 free( handle );
399 /* insert a buffer index at the head of the LRU list */
400 static void insert_buffer_lru( struct native_win_data *win, int index )
402 unsigned int i;
404 for (i = 0; i < NB_CACHED_BUFFERS; i++)
406 if (win->buffer_lru[i] == index) break;
407 if (win->buffer_lru[i] == -1) break;
410 assert( i < NB_CACHED_BUFFERS );
411 memmove( win->buffer_lru + 1, win->buffer_lru, i * sizeof(win->buffer_lru[0]) );
412 win->buffer_lru[0] = index;
415 static int register_buffer( struct native_win_data *win, struct ANativeWindowBuffer *buffer,
416 HANDLE *mapping, int *is_new )
418 unsigned int i;
420 *is_new = 0;
421 for (i = 0; i < NB_CACHED_BUFFERS; i++)
423 if (win->buffers[i] == buffer) goto done;
424 if (!win->buffers[i]) break;
427 if (i == NB_CACHED_BUFFERS)
429 /* reuse the least recently used buffer */
430 i = win->buffer_lru[NB_CACHED_BUFFERS - 1];
431 assert( i < NB_CACHED_BUFFERS );
433 TRACE( "%p %p evicting buffer %p id %d from cache\n",
434 win->hwnd, win->parent, win->buffers[i], i );
435 win->buffers[i]->common.decRef( &win->buffers[i]->common );
436 if (win->mappings[i]) UnmapViewOfFile( win->mappings[i] );
439 win->buffers[i] = buffer;
440 win->mappings[i] = NULL;
442 if (mapping)
444 *mapping = CreateFileMappingW( INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0,
445 buffer->stride * buffer->height * 4, NULL );
446 win->mappings[i] = MapViewOfFile( *mapping, FILE_MAP_READ, 0, 0, 0 );
448 buffer->common.incRef( &buffer->common );
449 *is_new = 1;
450 TRACE( "%p %p %p -> %d\n", win->hwnd, win->parent, buffer, i );
452 done:
453 insert_buffer_lru( win, i );
454 return i;
457 static struct ANativeWindowBuffer *get_registered_buffer( struct native_win_data *win, int id )
459 if (id < 0 || id >= NB_CACHED_BUFFERS || !win->buffers[id])
461 ERR( "unknown buffer %d for %p %p\n", id, win->hwnd, win->parent );
462 return NULL;
464 return win->buffers[id];
467 static void release_native_window( struct native_win_data *data )
469 unsigned int i;
471 if (data->parent) pANativeWindow_release( data->parent );
472 for (i = 0; i < NB_CACHED_BUFFERS; i++)
474 if (data->buffers[i]) data->buffers[i]->common.decRef( &data->buffers[i]->common );
475 if (data->mappings[i]) UnmapViewOfFile( data->mappings[i] );
476 data->buffer_lru[i] = -1;
478 memset( data->buffers, 0, sizeof(data->buffers) );
479 memset( data->mappings, 0, sizeof(data->mappings) );
482 static void free_native_win_data( struct native_win_data *data )
484 unsigned int idx = data_map_idx( data->hwnd, data->opengl );
486 InterlockedCompareExchangePointer( (void **)&capture_window, 0, data->hwnd );
487 release_native_window( data );
488 HeapFree( GetProcessHeap(), 0, data );
489 data_map[idx] = NULL;
492 static struct native_win_data *create_native_win_data( HWND hwnd, BOOL opengl )
494 unsigned int i, idx = data_map_idx( hwnd, opengl );
495 struct native_win_data *data = data_map[idx];
497 if (data)
499 WARN( "data for %p not freed correctly\n", data->hwnd );
500 free_native_win_data( data );
502 if (!(data = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data) ))) return NULL;
503 data->hwnd = hwnd;
504 data->opengl = opengl;
505 if (!opengl) data->api = NATIVE_WINDOW_API_CPU;
506 data->buffer_format = PF_BGRA_8888;
507 data_map[idx] = data;
508 for (i = 0; i < NB_CACHED_BUFFERS; i++) data->buffer_lru[i] = -1;
509 return data;
512 static void CALLBACK register_native_window_callback( ULONG_PTR arg1, ULONG_PTR arg2, ULONG_PTR arg3 )
514 HWND hwnd = (HWND)arg1;
515 struct ANativeWindow *win = (struct ANativeWindow *)arg2;
516 BOOL opengl = arg3;
517 struct native_win_data *data = get_native_win_data( hwnd, opengl );
519 if (!win) return; /* do nothing and hold on to the window until we get a new surface */
521 if (!data || data->parent == win)
523 pANativeWindow_release( win );
524 if (data) PostMessageW( hwnd, WM_ANDROID_REFRESH, opengl, 0 );
525 TRACE( "%p -> %p win %p (unchanged)\n", hwnd, data, win );
526 return;
529 release_native_window( data );
530 data->parent = win;
531 data->generation++;
532 wrap_java_call();
533 if (data->api) win->perform( win, NATIVE_WINDOW_API_CONNECT, data->api );
534 win->perform( win, NATIVE_WINDOW_SET_BUFFERS_FORMAT, data->buffer_format );
535 win->setSwapInterval( win, data->swap_interval );
536 unwrap_java_call();
537 PostMessageW( hwnd, WM_ANDROID_REFRESH, opengl, 0 );
538 TRACE( "%p -> %p win %p\n", hwnd, data, win );
541 /* register a native window received from the Java side for use in ioctls */
542 void register_native_window( HWND hwnd, struct ANativeWindow *win, BOOL opengl )
544 NtQueueApcThread( thread, register_native_window_callback, (ULONG_PTR)hwnd, (ULONG_PTR)win, opengl );
547 void init_gralloc( const struct hw_module_t *module )
549 struct hw_device_t *device;
550 int ret;
552 TRACE( "got module %p ver %u.%u id %s name %s author %s\n",
553 module, module->module_api_version >> 8, module->module_api_version & 0xff,
554 debugstr_a(module->id), debugstr_a(module->name), debugstr_a(module->author) );
556 switch (module->module_api_version >> 8)
558 case 0:
559 gralloc_module = (struct gralloc_module_t *)module;
560 break;
561 case 1:
562 if (!(ret = module->methods->open( module, GRALLOC_HARDWARE_MODULE_ID, &device )))
564 int32_t caps[64];
565 uint32_t i, count = ARRAY_SIZE(caps);
567 gralloc1_device = (struct gralloc1_device *)device;
568 gralloc1_retain = gralloc1_device->getFunction( gralloc1_device, GRALLOC1_FUNCTION_RETAIN );
569 gralloc1_release = gralloc1_device->getFunction( gralloc1_device, GRALLOC1_FUNCTION_RELEASE );
570 gralloc1_lock = gralloc1_device->getFunction( gralloc1_device, GRALLOC1_FUNCTION_LOCK );
571 gralloc1_unlock = gralloc1_device->getFunction( gralloc1_device, GRALLOC1_FUNCTION_UNLOCK );
572 TRACE( "got device version %u funcs %p %p %p %p\n", device->version,
573 gralloc1_retain, gralloc1_release, gralloc1_lock, gralloc1_unlock );
575 gralloc1_device->getCapabilities( gralloc1_device, &count, caps );
576 if (count == ARRAY_SIZE(caps)) ERR( "too many gralloc capabilities\n" );
577 for (i = 0; i < count; i++)
578 if (caps[i] < ARRAY_SIZE(gralloc1_caps)) gralloc1_caps[caps[i]] = TRUE;
580 else ERR( "failed to open gralloc err %d\n", ret );
581 break;
582 default:
583 ERR( "unknown gralloc module version %u\n", module->module_api_version >> 8 );
584 break;
588 static int gralloc_grab_buffer( struct ANativeWindowBuffer *buffer )
590 if (gralloc1_device)
591 return gralloc1_retain( gralloc1_device, buffer->handle );
592 if (gralloc_module)
593 return gralloc_module->registerBuffer( gralloc_module, buffer->handle );
594 return -ENODEV;
597 static void gralloc_release_buffer( struct ANativeWindowBuffer *buffer )
599 if (gralloc1_device) gralloc1_release( gralloc1_device, buffer->handle );
600 else if (gralloc_module) gralloc_module->unregisterBuffer( gralloc_module, buffer->handle );
602 if (!gralloc1_caps[GRALLOC1_CAPABILITY_RELEASE_IMPLY_DELETE])
603 close_native_handle( (native_handle_t *)buffer->handle );
606 static int gralloc_lock( struct ANativeWindowBuffer *buffer, void **bits )
608 if (gralloc1_device)
610 gralloc1_rect_t rect = { 0, 0, buffer->width, buffer->height };
611 return gralloc1_lock( gralloc1_device, buffer->handle,
612 GRALLOC1_PRODUCER_USAGE_CPU_READ_OFTEN |
613 GRALLOC1_PRODUCER_USAGE_CPU_WRITE_OFTEN,
614 GRALLOC1_CONSUMER_USAGE_NONE, &rect, bits, -1 );
616 if (gralloc_module)
617 return gralloc_module->lock( gralloc_module, buffer->handle,
618 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
619 0, 0, buffer->width, buffer->height, bits );
621 *bits = ((struct native_buffer_wrapper *)buffer)->bits;
622 return 0;
625 static void gralloc_unlock( struct ANativeWindowBuffer *buffer )
627 if (gralloc1_device)
629 int fence;
630 gralloc1_unlock( gralloc1_device, buffer->handle, &fence );
631 wait_fence_and_close( fence );
633 else if (gralloc_module) gralloc_module->unlock( gralloc_module, buffer->handle );
636 /* get the capture window stored in the desktop process */
637 HWND get_capture_window(void)
639 return capture_window;
642 static NTSTATUS android_error_to_status( int err )
644 switch (err)
646 case 0: return STATUS_SUCCESS;
647 case -ENOMEM: return STATUS_NO_MEMORY;
648 case -ENOSYS: return STATUS_NOT_SUPPORTED;
649 case -EINVAL: return STATUS_INVALID_PARAMETER;
650 case -ENOENT: return STATUS_INVALID_HANDLE;
651 case -EPERM: return STATUS_ACCESS_DENIED;
652 case -ENODEV: return STATUS_NO_SUCH_DEVICE;
653 case -EEXIST: return STATUS_DUPLICATE_NAME;
654 case -EPIPE: return STATUS_PIPE_DISCONNECTED;
655 case -ENODATA: return STATUS_NO_MORE_FILES;
656 case -ETIMEDOUT: return STATUS_IO_TIMEOUT;
657 case -EBADMSG: return STATUS_INVALID_DEVICE_REQUEST;
658 case -EWOULDBLOCK: return STATUS_DEVICE_NOT_READY;
659 default:
660 FIXME( "unmapped error %d\n", err );
661 return STATUS_UNSUCCESSFUL;
665 static int status_to_android_error( NTSTATUS status )
667 switch (status)
669 case STATUS_SUCCESS: return 0;
670 case STATUS_NO_MEMORY: return -ENOMEM;
671 case STATUS_NOT_SUPPORTED: return -ENOSYS;
672 case STATUS_INVALID_PARAMETER: return -EINVAL;
673 case STATUS_BUFFER_OVERFLOW: return -EINVAL;
674 case STATUS_INVALID_HANDLE: return -ENOENT;
675 case STATUS_ACCESS_DENIED: return -EPERM;
676 case STATUS_NO_SUCH_DEVICE: return -ENODEV;
677 case STATUS_DUPLICATE_NAME: return -EEXIST;
678 case STATUS_PIPE_DISCONNECTED: return -EPIPE;
679 case STATUS_NO_MORE_FILES: return -ENODATA;
680 case STATUS_IO_TIMEOUT: return -ETIMEDOUT;
681 case STATUS_INVALID_DEVICE_REQUEST: return -EBADMSG;
682 case STATUS_DEVICE_NOT_READY: return -EWOULDBLOCK;
683 default:
684 FIXME( "unmapped status %08x\n", status );
685 return -EINVAL;
689 static jobject load_java_method( jmethodID *method, const char *name, const char *args )
691 jobject object = wine_get_java_object();
693 if (!*method)
695 jclass class;
697 wrap_java_call();
698 class = (*jni_env)->GetObjectClass( jni_env, object );
699 *method = (*jni_env)->GetMethodID( jni_env, class, name, args );
700 unwrap_java_call();
701 if (!*method)
703 FIXME( "method %s not found\n", name );
704 return NULL;
707 return object;
710 static void create_desktop_window( HWND hwnd )
712 static jmethodID method;
713 jobject object;
715 if (!(object = load_java_method( &method, "createDesktopWindow", "(I)V" ))) return;
717 wrap_java_call();
718 (*jni_env)->CallVoidMethod( jni_env, object, method, HandleToLong( hwnd ));
719 unwrap_java_call();
722 static NTSTATUS createWindow_ioctl( void *data, DWORD in_size, DWORD out_size, ULONG_PTR *ret_size )
724 static jmethodID method;
725 jobject object;
726 struct ioctl_android_create_window *res = data;
727 struct native_win_data *win_data;
728 DWORD pid = current_client_id();
730 if (in_size < sizeof(*res)) return STATUS_INVALID_PARAMETER;
732 if (!(win_data = create_native_win_data( LongToHandle(res->hdr.hwnd), res->hdr.opengl )))
733 return STATUS_NO_MEMORY;
735 TRACE( "hwnd %08x opengl %u parent %08x\n", res->hdr.hwnd, res->hdr.opengl, res->parent );
737 if (!(object = load_java_method( &method, "createWindow", "(IZIFI)V" ))) return STATUS_NOT_SUPPORTED;
739 wrap_java_call();
740 (*jni_env)->CallVoidMethod( jni_env, object, method, res->hdr.hwnd, res->hdr.opengl, res->parent, res->scale, pid );
741 unwrap_java_call();
742 return STATUS_SUCCESS;
745 static NTSTATUS destroyWindow_ioctl( void *data, DWORD in_size, DWORD out_size, ULONG_PTR *ret_size )
747 static jmethodID method;
748 jobject object;
749 struct ioctl_android_destroy_window *res = data;
750 struct native_win_data *win_data;
752 if (in_size < sizeof(*res)) return STATUS_INVALID_PARAMETER;
754 win_data = get_ioctl_native_win_data( &res->hdr );
756 TRACE( "hwnd %08x opengl %u\n", res->hdr.hwnd, res->hdr.opengl );
758 if (!(object = load_java_method( &method, "destroyWindow", "(I)V" ))) return STATUS_NOT_SUPPORTED;
760 wrap_java_call();
761 (*jni_env)->CallVoidMethod( jni_env, object, method, res->hdr.hwnd );
762 unwrap_java_call();
763 if (win_data) free_native_win_data( win_data );
764 return STATUS_SUCCESS;
767 static NTSTATUS windowPosChanged_ioctl( void *data, DWORD in_size, DWORD out_size, ULONG_PTR *ret_size )
769 static jmethodID method;
770 jobject object;
771 struct ioctl_android_window_pos_changed *res = data;
773 if (in_size < sizeof(*res)) return STATUS_INVALID_PARAMETER;
775 TRACE( "hwnd %08x win %s client %s visible %s style %08x flags %08x after %08x owner %08x\n",
776 res->hdr.hwnd, wine_dbgstr_rect(&res->window_rect), wine_dbgstr_rect(&res->client_rect),
777 wine_dbgstr_rect(&res->visible_rect), res->style, res->flags, res->after, res->owner );
779 if (!(object = load_java_method( &method, "windowPosChanged", "(IIIIIIIIIIIIIIIII)V" )))
780 return STATUS_NOT_SUPPORTED;
782 wrap_java_call();
783 (*jni_env)->CallVoidMethod( jni_env, object, method, res->hdr.hwnd, res->flags, res->after, res->owner, res->style,
784 res->window_rect.left, res->window_rect.top, res->window_rect.right, res->window_rect.bottom,
785 res->client_rect.left, res->client_rect.top, res->client_rect.right, res->client_rect.bottom,
786 res->visible_rect.left, res->visible_rect.top, res->visible_rect.right, res->visible_rect.bottom );
787 unwrap_java_call();
788 return STATUS_SUCCESS;
791 static NTSTATUS dequeueBuffer_ioctl( void *data, DWORD in_size, DWORD out_size, ULONG_PTR *ret_size )
793 struct ANativeWindow *parent;
794 struct ioctl_android_dequeueBuffer *res = data;
795 struct native_win_data *win_data;
796 struct ANativeWindowBuffer *buffer;
797 int fence, ret, is_new;
799 if (out_size < sizeof( *res )) return STATUS_BUFFER_OVERFLOW;
801 if (in_size < offsetof( struct ioctl_android_dequeueBuffer, native_handle ))
802 return STATUS_INVALID_PARAMETER;
804 if (!(win_data = get_ioctl_native_win_data( &res->hdr ))) return STATUS_INVALID_HANDLE;
805 if (!(parent = win_data->parent)) return STATUS_DEVICE_NOT_READY;
807 *ret_size = offsetof( struct ioctl_android_dequeueBuffer, native_handle );
808 wrap_java_call();
809 ret = parent->dequeueBuffer( parent, &buffer, &fence );
810 unwrap_java_call();
811 if (!ret)
813 HANDLE mapping = 0;
815 TRACE( "%08x got buffer %p fence %d\n", res->hdr.hwnd, buffer, fence );
816 res->width = buffer->width;
817 res->height = buffer->height;
818 res->stride = buffer->stride;
819 res->format = buffer->format;
820 res->usage = buffer->usage;
821 res->buffer_id = register_buffer( win_data, buffer, res->win32 ? &mapping : NULL, &is_new );
822 res->generation = win_data->generation;
823 if (is_new)
825 HANDLE process = OpenProcess( PROCESS_DUP_HANDLE, FALSE, current_client_id() );
826 map_native_handle( &res->native_handle, buffer->handle, mapping, process );
827 CloseHandle( process );
828 *ret_size = sizeof( *res );
830 wait_fence_and_close( fence );
831 return STATUS_SUCCESS;
833 ERR( "%08x failed %d\n", res->hdr.hwnd, ret );
834 return android_error_to_status( ret );
837 static NTSTATUS cancelBuffer_ioctl( void *data, DWORD in_size, DWORD out_size, ULONG_PTR *ret_size )
839 struct ioctl_android_cancelBuffer *res = data;
840 struct ANativeWindow *parent;
841 struct ANativeWindowBuffer *buffer;
842 struct native_win_data *win_data;
843 int ret;
845 if (in_size < sizeof(*res)) return STATUS_INVALID_PARAMETER;
847 if (!(win_data = get_ioctl_native_win_data( &res->hdr ))) return STATUS_INVALID_HANDLE;
848 if (!(parent = win_data->parent)) return STATUS_DEVICE_NOT_READY;
849 if (res->generation != win_data->generation) return STATUS_SUCCESS; /* obsolete buffer, ignore */
851 if (!(buffer = get_registered_buffer( win_data, res->buffer_id ))) return STATUS_INVALID_HANDLE;
853 TRACE( "%08x buffer %p\n", res->hdr.hwnd, buffer );
854 wrap_java_call();
855 ret = parent->cancelBuffer( parent, buffer, -1 );
856 unwrap_java_call();
857 return android_error_to_status( ret );
860 static NTSTATUS queueBuffer_ioctl( void *data, DWORD in_size, DWORD out_size, ULONG_PTR *ret_size )
862 struct ioctl_android_queueBuffer *res = data;
863 struct ANativeWindow *parent;
864 struct ANativeWindowBuffer *buffer;
865 struct native_win_data *win_data;
866 int ret;
868 if (in_size < sizeof(*res)) return STATUS_INVALID_PARAMETER;
870 if (!(win_data = get_ioctl_native_win_data( &res->hdr ))) return STATUS_INVALID_HANDLE;
871 if (!(parent = win_data->parent)) return STATUS_DEVICE_NOT_READY;
872 if (res->generation != win_data->generation) return STATUS_SUCCESS; /* obsolete buffer, ignore */
874 if (!(buffer = get_registered_buffer( win_data, res->buffer_id ))) return STATUS_INVALID_HANDLE;
876 TRACE( "%08x buffer %p mapping %p\n", res->hdr.hwnd, buffer, win_data->mappings[res->buffer_id] );
877 if (win_data->mappings[res->buffer_id])
879 void *bits;
880 ret = gralloc_lock( buffer, &bits );
881 if (ret) return android_error_to_status( ret );
882 memcpy( bits, win_data->mappings[res->buffer_id], buffer->stride * buffer->height * 4 );
883 gralloc_unlock( buffer );
885 wrap_java_call();
886 ret = parent->queueBuffer( parent, buffer, -1 );
887 unwrap_java_call();
888 return android_error_to_status( ret );
891 static NTSTATUS query_ioctl( void *data, DWORD in_size, DWORD out_size, ULONG_PTR *ret_size )
893 struct ioctl_android_query *res = data;
894 struct ANativeWindow *parent;
895 struct native_win_data *win_data;
896 int ret;
898 if (in_size < sizeof(*res)) return STATUS_INVALID_PARAMETER;
899 if (out_size < sizeof(*res)) return STATUS_BUFFER_OVERFLOW;
901 if (!(win_data = get_ioctl_native_win_data( &res->hdr ))) return STATUS_INVALID_HANDLE;
902 if (!(parent = win_data->parent)) return STATUS_DEVICE_NOT_READY;
904 *ret_size = sizeof( *res );
905 wrap_java_call();
906 ret = parent->query( parent, res->what, &res->value );
907 unwrap_java_call();
908 return android_error_to_status( ret );
911 static NTSTATUS perform_ioctl( void *data, DWORD in_size, DWORD out_size, ULONG_PTR *ret_size )
913 struct ioctl_android_perform *res = data;
914 struct ANativeWindow *parent;
915 struct native_win_data *win_data;
916 int ret = -ENOENT;
918 if (in_size < sizeof(*res)) return STATUS_INVALID_PARAMETER;
920 if (!(win_data = get_ioctl_native_win_data( &res->hdr ))) return STATUS_INVALID_HANDLE;
921 if (!(parent = win_data->parent)) return STATUS_DEVICE_NOT_READY;
923 switch (res->operation)
925 case NATIVE_WINDOW_SET_BUFFERS_FORMAT:
926 wrap_java_call();
927 ret = parent->perform( parent, res->operation, res->args[0] );
928 unwrap_java_call();
929 if (!ret) win_data->buffer_format = res->args[0];
930 break;
931 case NATIVE_WINDOW_API_CONNECT:
932 wrap_java_call();
933 ret = parent->perform( parent, res->operation, res->args[0] );
934 unwrap_java_call();
935 if (!ret) win_data->api = res->args[0];
936 break;
937 case NATIVE_WINDOW_API_DISCONNECT:
938 wrap_java_call();
939 ret = parent->perform( parent, res->operation, res->args[0] );
940 unwrap_java_call();
941 if (!ret) win_data->api = 0;
942 break;
943 case NATIVE_WINDOW_SET_USAGE:
944 case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM:
945 case NATIVE_WINDOW_SET_SCALING_MODE:
946 wrap_java_call();
947 ret = parent->perform( parent, res->operation, res->args[0] );
948 unwrap_java_call();
949 break;
950 case NATIVE_WINDOW_SET_BUFFER_COUNT:
951 wrap_java_call();
952 ret = parent->perform( parent, res->operation, (size_t)res->args[0] );
953 unwrap_java_call();
954 break;
955 case NATIVE_WINDOW_SET_BUFFERS_DIMENSIONS:
956 case NATIVE_WINDOW_SET_BUFFERS_USER_DIMENSIONS:
957 wrap_java_call();
958 ret = parent->perform( parent, res->operation, res->args[0], res->args[1] );
959 unwrap_java_call();
960 break;
961 case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY:
962 wrap_java_call();
963 ret = parent->perform( parent, res->operation, res->args[0], res->args[1], res->args[2] );
964 unwrap_java_call();
965 break;
966 case NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP:
967 wrap_java_call();
968 ret = parent->perform( parent, res->operation, res->args[0] | ((int64_t)res->args[1] << 32) );
969 unwrap_java_call();
970 break;
971 case NATIVE_WINDOW_CONNECT:
972 case NATIVE_WINDOW_DISCONNECT:
973 case NATIVE_WINDOW_UNLOCK_AND_POST:
974 wrap_java_call();
975 ret = parent->perform( parent, res->operation );
976 unwrap_java_call();
977 break;
978 case NATIVE_WINDOW_SET_CROP:
980 android_native_rect_t rect;
981 rect.left = res->args[0];
982 rect.top = res->args[1];
983 rect.right = res->args[2];
984 rect.bottom = res->args[3];
985 wrap_java_call();
986 ret = parent->perform( parent, res->operation, &rect );
987 unwrap_java_call();
988 break;
990 case NATIVE_WINDOW_LOCK:
991 default:
992 FIXME( "unsupported perform op %d\n", res->operation );
993 break;
995 return android_error_to_status( ret );
998 static NTSTATUS setSwapInterval_ioctl( void *data, DWORD in_size, DWORD out_size, ULONG_PTR *ret_size )
1000 struct ioctl_android_set_swap_interval *res = data;
1001 struct ANativeWindow *parent;
1002 struct native_win_data *win_data;
1003 int ret;
1005 if (in_size < sizeof(*res)) return STATUS_INVALID_PARAMETER;
1007 if (!(win_data = get_ioctl_native_win_data( &res->hdr ))) return STATUS_INVALID_HANDLE;
1008 win_data->swap_interval = res->interval;
1010 if (!(parent = win_data->parent)) return STATUS_SUCCESS;
1011 wrap_java_call();
1012 ret = parent->setSwapInterval( parent, res->interval );
1013 unwrap_java_call();
1014 return android_error_to_status( ret );
1017 static NTSTATUS setWindowParent_ioctl( void *data, DWORD in_size, DWORD out_size, ULONG_PTR *ret_size )
1019 static jmethodID method;
1020 jobject object;
1021 struct ioctl_android_set_window_parent *res = data;
1022 struct native_win_data *win_data;
1023 DWORD pid = current_client_id();
1025 if (in_size < sizeof(*res)) return STATUS_INVALID_PARAMETER;
1027 if (!(win_data = get_ioctl_native_win_data( &res->hdr ))) return STATUS_INVALID_HANDLE;
1029 TRACE( "hwnd %08x parent %08x\n", res->hdr.hwnd, res->parent );
1031 if (!(object = load_java_method( &method, "setParent", "(IIFI)V" ))) return STATUS_NOT_SUPPORTED;
1033 wrap_java_call();
1034 (*jni_env)->CallVoidMethod( jni_env, object, method, res->hdr.hwnd, res->parent, res->scale, pid );
1035 unwrap_java_call();
1036 return STATUS_SUCCESS;
1039 static NTSTATUS setCapture_ioctl( void *data, DWORD in_size, DWORD out_size, ULONG_PTR *ret_size )
1041 struct ioctl_android_set_capture *res = data;
1043 if (in_size < sizeof(*res)) return STATUS_INVALID_PARAMETER;
1045 if (res->hdr.hwnd && !get_ioctl_native_win_data( &res->hdr )) return STATUS_INVALID_HANDLE;
1047 TRACE( "hwnd %08x\n", res->hdr.hwnd );
1049 InterlockedExchangePointer( (void **)&capture_window, LongToHandle( res->hdr.hwnd ));
1050 return STATUS_SUCCESS;
1053 static NTSTATUS setCursor_ioctl( void *data, DWORD in_size, DWORD out_size, ULONG_PTR *ret_size )
1055 static jmethodID method;
1056 jobject object;
1057 int size;
1058 struct ioctl_android_set_cursor *res = data;
1060 if (in_size < offsetof( struct ioctl_android_set_cursor, bits )) return STATUS_INVALID_PARAMETER;
1062 if (res->width < 0 || res->height < 0 || res->width > 256 || res->height > 256)
1063 return STATUS_INVALID_PARAMETER;
1065 size = res->width * res->height;
1066 if (in_size != offsetof( struct ioctl_android_set_cursor, bits[size] ))
1067 return STATUS_INVALID_PARAMETER;
1069 TRACE( "hwnd %08x size %d\n", res->hdr.hwnd, size );
1071 if (!(object = load_java_method( &method, "setCursor", "(IIIII[I)V" )))
1072 return STATUS_NOT_SUPPORTED;
1074 wrap_java_call();
1076 if (size)
1078 jintArray array = (*jni_env)->NewIntArray( jni_env, size );
1079 (*jni_env)->SetIntArrayRegion( jni_env, array, 0, size, (jint *)res->bits );
1080 (*jni_env)->CallVoidMethod( jni_env, object, method, 0, res->width, res->height,
1081 res->hotspotx, res->hotspoty, array );
1082 (*jni_env)->DeleteLocalRef( jni_env, array );
1084 else (*jni_env)->CallVoidMethod( jni_env, object, method, res->id, 0, 0, 0, 0, 0 );
1086 unwrap_java_call();
1088 return STATUS_SUCCESS;
1091 typedef NTSTATUS (*ioctl_func)( void *in, DWORD in_size, DWORD out_size, ULONG_PTR *ret_size );
1092 static const ioctl_func ioctl_funcs[] =
1094 createWindow_ioctl, /* IOCTL_CREATE_WINDOW */
1095 destroyWindow_ioctl, /* IOCTL_DESTROY_WINDOW */
1096 windowPosChanged_ioctl, /* IOCTL_WINDOW_POS_CHANGED */
1097 setWindowParent_ioctl, /* IOCTL_SET_WINDOW_PARENT */
1098 dequeueBuffer_ioctl, /* IOCTL_DEQUEUE_BUFFER */
1099 queueBuffer_ioctl, /* IOCTL_QUEUE_BUFFER */
1100 cancelBuffer_ioctl, /* IOCTL_CANCEL_BUFFER */
1101 query_ioctl, /* IOCTL_QUERY */
1102 perform_ioctl, /* IOCTL_PERFORM */
1103 setSwapInterval_ioctl, /* IOCTL_SET_SWAP_INT */
1104 setCapture_ioctl, /* IOCTL_SET_CAPTURE */
1105 setCursor_ioctl, /* IOCTL_SET_CURSOR */
1108 static NTSTATUS WINAPI ioctl_callback( DEVICE_OBJECT *device, IRP *irp )
1110 IO_STACK_LOCATION *irpsp = IoGetCurrentIrpStackLocation( irp );
1111 DWORD code = (irpsp->Parameters.DeviceIoControl.IoControlCode - ANDROID_IOCTL(0)) >> 2;
1113 if (code < NB_IOCTLS)
1115 struct ioctl_header *header = irp->AssociatedIrp.SystemBuffer;
1116 DWORD in_size = irpsp->Parameters.DeviceIoControl.InputBufferLength;
1117 ioctl_func func = ioctl_funcs[code];
1119 if (in_size >= sizeof(*header))
1121 irp->IoStatus.Information = 0;
1122 irp->IoStatus.u.Status = func( irp->AssociatedIrp.SystemBuffer, in_size,
1123 irpsp->Parameters.DeviceIoControl.OutputBufferLength,
1124 &irp->IoStatus.Information );
1126 else irp->IoStatus.u.Status = STATUS_INVALID_PARAMETER;
1128 else
1130 FIXME( "ioctl %x not supported\n", irpsp->Parameters.DeviceIoControl.IoControlCode );
1131 irp->IoStatus.u.Status = STATUS_NOT_SUPPORTED;
1133 IoCompleteRequest( irp, IO_NO_INCREMENT );
1134 return STATUS_SUCCESS;
1137 static NTSTATUS CALLBACK init_android_driver( DRIVER_OBJECT *driver, UNICODE_STRING *name )
1139 static const WCHAR device_nameW[] = {'\\','D','e','v','i','c','e','\\','W','i','n','e','A','n','d','r','o','i','d',0 };
1140 static const WCHAR device_linkW[] = {'\\','?','?','\\','W','i','n','e','A','n','d','r','o','i','d',0 };
1142 UNICODE_STRING nameW, linkW;
1143 DEVICE_OBJECT *device;
1144 NTSTATUS status;
1146 driver->MajorFunction[IRP_MJ_DEVICE_CONTROL] = ioctl_callback;
1148 RtlInitUnicodeString( &nameW, device_nameW );
1149 RtlInitUnicodeString( &linkW, device_linkW );
1151 if ((status = IoCreateDevice( driver, 0, &nameW, 0, 0, FALSE, &device ))) return status;
1152 return IoCreateSymbolicLink( &linkW, &nameW );
1155 static DWORD CALLBACK device_thread( void *arg )
1157 static const WCHAR driver_nameW[] = {'\\','D','r','i','v','e','r','\\','W','i','n','e','A','n','d','r','o','i','d',0 };
1159 HANDLE start_event = arg;
1160 UNICODE_STRING nameW;
1161 NTSTATUS status;
1162 JavaVM *java_vm;
1163 DWORD ret;
1165 TRACE( "starting process %x\n", GetCurrentProcessId() );
1167 if (!(java_vm = wine_get_java_vm())) return 0; /* not running under Java */
1169 init_java_thread( java_vm );
1171 create_desktop_window( GetDesktopWindow() );
1173 RtlInitUnicodeString( &nameW, driver_nameW );
1174 if ((status = IoCreateDriver( &nameW, init_android_driver )))
1176 FIXME( "failed to create driver error %x\n", status );
1177 return status;
1180 stop_event = CreateEventW( NULL, TRUE, FALSE, NULL );
1181 SetEvent( start_event );
1183 ret = wine_ntoskrnl_main_loop( stop_event );
1185 wrap_java_call();
1186 (*java_vm)->DetachCurrentThread( java_vm );
1187 unwrap_java_call();
1188 return ret;
1191 void start_android_device(void)
1193 HANDLE handles[2];
1195 handles[0] = CreateEventW( NULL, TRUE, FALSE, NULL );
1196 handles[1] = thread = CreateThread( NULL, 0, device_thread, handles[0], 0, NULL );
1197 WaitForMultipleObjects( 2, handles, FALSE, INFINITE );
1198 CloseHandle( handles[0] );
1202 /* Client-side ioctl support */
1205 static int android_ioctl( enum android_ioctl code, void *in, DWORD in_size, void *out, DWORD *out_size )
1207 static const WCHAR deviceW[] = {'\\','\\','.','\\','W','i','n','e','A','n','d','r','o','i','d',0 };
1208 static HANDLE device;
1209 IO_STATUS_BLOCK iosb;
1210 NTSTATUS status;
1212 if (!device)
1214 HANDLE file = CreateFileW( deviceW, GENERIC_READ,
1215 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0 );
1216 if (file == INVALID_HANDLE_VALUE) return -ENOENT;
1217 if (InterlockedCompareExchangePointer( &device, file, NULL )) CloseHandle( file );
1220 status = NtDeviceIoControlFile( device, NULL, NULL, NULL, &iosb, ANDROID_IOCTL(code),
1221 in, in_size, out, out_size ? *out_size : 0 );
1222 if (status == STATUS_FILE_DELETED)
1224 WARN( "parent process is gone\n" );
1225 ExitProcess( 1 );
1227 if (out_size) *out_size = iosb.Information;
1228 return status_to_android_error( status );
1231 static void win_incRef( struct android_native_base_t *base )
1233 struct native_win_wrapper *win = (struct native_win_wrapper *)base;
1234 InterlockedIncrement( &win->ref );
1237 static void win_decRef( struct android_native_base_t *base )
1239 struct native_win_wrapper *win = (struct native_win_wrapper *)base;
1240 InterlockedDecrement( &win->ref );
1243 static void buffer_incRef( struct android_native_base_t *base )
1245 struct native_buffer_wrapper *buffer = (struct native_buffer_wrapper *)base;
1246 InterlockedIncrement( &buffer->ref );
1249 static void buffer_decRef( struct android_native_base_t *base )
1251 struct native_buffer_wrapper *buffer = (struct native_buffer_wrapper *)base;
1253 if (!InterlockedDecrement( &buffer->ref ))
1255 if (!is_in_desktop_process()) gralloc_release_buffer( &buffer->buffer );
1256 if (buffer->bits) UnmapViewOfFile( buffer->bits );
1257 HeapFree( GetProcessHeap(), 0, buffer );
1261 static int dequeueBuffer( struct ANativeWindow *window, struct ANativeWindowBuffer **buffer, int *fence )
1263 struct native_win_wrapper *win = (struct native_win_wrapper *)window;
1264 struct ioctl_android_dequeueBuffer res;
1265 DWORD size = sizeof(res);
1266 int ret, use_win32 = !gralloc_module && !gralloc1_device;
1268 res.hdr.hwnd = HandleToLong( win->hwnd );
1269 res.hdr.opengl = win->opengl;
1270 res.win32 = use_win32;
1271 ret = android_ioctl( IOCTL_DEQUEUE_BUFFER,
1272 &res, offsetof( struct ioctl_android_dequeueBuffer, native_handle ),
1273 &res, &size );
1274 if (ret) return ret;
1276 /* if we received the native handle, this is a new buffer */
1277 if (size > offsetof( struct ioctl_android_dequeueBuffer, native_handle ))
1279 struct native_buffer_wrapper *buf = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*buf) );
1281 buf->buffer.common.magic = ANDROID_NATIVE_BUFFER_MAGIC;
1282 buf->buffer.common.version = sizeof( buf->buffer );
1283 buf->buffer.common.incRef = buffer_incRef;
1284 buf->buffer.common.decRef = buffer_decRef;
1285 buf->buffer.width = res.width;
1286 buf->buffer.height = res.height;
1287 buf->buffer.stride = res.stride;
1288 buf->buffer.format = res.format;
1289 buf->buffer.usage = res.usage;
1290 buf->buffer.handle = unmap_native_handle( &res.native_handle.handle );
1291 buf->ref = 1;
1292 buf->hwnd = win->hwnd;
1293 buf->buffer_id = res.buffer_id;
1294 buf->generation = res.generation;
1295 if (win->buffers[res.buffer_id])
1296 win->buffers[res.buffer_id]->buffer.common.decRef(&win->buffers[res.buffer_id]->buffer.common);
1297 win->buffers[res.buffer_id] = buf;
1299 if (use_win32)
1301 HANDLE mapping = LongToHandle( res.native_handle.handle.data[0] );
1302 buf->bits = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 0 );
1303 CloseHandle( mapping );
1305 else if (!is_in_desktop_process())
1307 if ((ret = gralloc_grab_buffer( &buf->buffer )) < 0)
1308 WARN( "hwnd %p, buffer %p failed to register %d %s\n",
1309 win->hwnd, &buf->buffer, ret, strerror(-ret) );
1313 *buffer = &win->buffers[res.buffer_id]->buffer;
1314 *fence = -1;
1316 TRACE( "hwnd %p, buffer %p %dx%d stride %d fmt %d usage %d fence %d\n",
1317 win->hwnd, *buffer, res.width, res.height, res.stride, res.format, res.usage, *fence );
1318 return 0;
1321 static int cancelBuffer( struct ANativeWindow *window, struct ANativeWindowBuffer *buffer, int fence )
1323 struct native_win_wrapper *win = (struct native_win_wrapper *)window;
1324 struct native_buffer_wrapper *buf = (struct native_buffer_wrapper *)buffer;
1325 struct ioctl_android_cancelBuffer cancel;
1327 TRACE( "hwnd %p buffer %p %dx%d stride %d fmt %d usage %d fence %d\n",
1328 win->hwnd, buffer, buffer->width, buffer->height,
1329 buffer->stride, buffer->format, buffer->usage, fence );
1330 cancel.buffer_id = buf->buffer_id;
1331 cancel.generation = buf->generation;
1332 cancel.hdr.hwnd = HandleToLong( win->hwnd );
1333 cancel.hdr.opengl = win->opengl;
1334 wait_fence_and_close( fence );
1335 return android_ioctl( IOCTL_CANCEL_BUFFER, &cancel, sizeof(cancel), NULL, NULL );
1338 static int queueBuffer( struct ANativeWindow *window, struct ANativeWindowBuffer *buffer, int fence )
1340 struct native_win_wrapper *win = (struct native_win_wrapper *)window;
1341 struct native_buffer_wrapper *buf = (struct native_buffer_wrapper *)buffer;
1342 struct ioctl_android_queueBuffer queue;
1344 TRACE( "hwnd %p buffer %p %dx%d stride %d fmt %d usage %d fence %d\n",
1345 win->hwnd, buffer, buffer->width, buffer->height,
1346 buffer->stride, buffer->format, buffer->usage, fence );
1347 queue.buffer_id = buf->buffer_id;
1348 queue.generation = buf->generation;
1349 queue.hdr.hwnd = HandleToLong( win->hwnd );
1350 queue.hdr.opengl = win->opengl;
1351 wait_fence_and_close( fence );
1352 return android_ioctl( IOCTL_QUEUE_BUFFER, &queue, sizeof(queue), NULL, NULL );
1355 static int dequeueBuffer_DEPRECATED( struct ANativeWindow *window, struct ANativeWindowBuffer **buffer )
1357 int fence, ret = dequeueBuffer( window, buffer, &fence );
1359 if (!ret) wait_fence_and_close( fence );
1360 return ret;
1363 static int cancelBuffer_DEPRECATED( struct ANativeWindow *window, struct ANativeWindowBuffer *buffer )
1365 return cancelBuffer( window, buffer, -1 );
1368 static int lockBuffer_DEPRECATED( struct ANativeWindow *window, struct ANativeWindowBuffer *buffer )
1370 return 0; /* nothing to do */
1373 static int queueBuffer_DEPRECATED( struct ANativeWindow *window, struct ANativeWindowBuffer *buffer )
1375 return queueBuffer( window, buffer, -1 );
1378 static int setSwapInterval( struct ANativeWindow *window, int interval )
1380 struct native_win_wrapper *win = (struct native_win_wrapper *)window;
1381 struct ioctl_android_set_swap_interval swap;
1383 TRACE( "hwnd %p interval %d\n", win->hwnd, interval );
1384 swap.hdr.hwnd = HandleToLong( win->hwnd );
1385 swap.hdr.opengl = win->opengl;
1386 swap.interval = interval;
1387 return android_ioctl( IOCTL_SET_SWAP_INT, &swap, sizeof(swap), NULL, NULL );
1390 static int query( const ANativeWindow *window, int what, int *value )
1392 struct native_win_wrapper *win = (struct native_win_wrapper *)window;
1393 struct ioctl_android_query query;
1394 DWORD size = sizeof( query );
1395 int ret;
1397 query.hdr.hwnd = HandleToLong( win->hwnd );
1398 query.hdr.opengl = win->opengl;
1399 query.what = what;
1400 ret = android_ioctl( IOCTL_QUERY, &query, sizeof(query), &query, &size );
1401 TRACE( "hwnd %p what %d got %d -> %p\n", win->hwnd, what, query.value, value );
1402 if (!ret) *value = query.value;
1403 return ret;
1406 static int perform( ANativeWindow *window, int operation, ... )
1408 static const char * const names[] =
1410 "SET_USAGE", "CONNECT", "DISCONNECT", "SET_CROP", "SET_BUFFER_COUNT", "SET_BUFFERS_GEOMETRY",
1411 "SET_BUFFERS_TRANSFORM", "SET_BUFFERS_TIMESTAMP", "SET_BUFFERS_DIMENSIONS", "SET_BUFFERS_FORMAT",
1412 "SET_SCALING_MODE", "LOCK", "UNLOCK_AND_POST", "API_CONNECT", "API_DISCONNECT",
1413 "SET_BUFFERS_USER_DIMENSIONS", "SET_POST_TRANSFORM_CROP"
1416 struct native_win_wrapper *win = (struct native_win_wrapper *)window;
1417 struct ioctl_android_perform perf;
1418 va_list args;
1420 perf.hdr.hwnd = HandleToLong( win->hwnd );
1421 perf.hdr.opengl = win->opengl;
1422 perf.operation = operation;
1423 memset( perf.args, 0, sizeof(perf.args) );
1425 va_start( args, operation );
1426 switch (operation)
1428 case NATIVE_WINDOW_SET_USAGE:
1429 case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM:
1430 case NATIVE_WINDOW_SET_BUFFERS_FORMAT:
1431 case NATIVE_WINDOW_SET_SCALING_MODE:
1432 case NATIVE_WINDOW_API_CONNECT:
1433 case NATIVE_WINDOW_API_DISCONNECT:
1434 perf.args[0] = va_arg( args, int );
1435 TRACE( "hwnd %p %s arg %d\n", win->hwnd, names[operation], perf.args[0] );
1436 break;
1437 case NATIVE_WINDOW_SET_BUFFER_COUNT:
1438 perf.args[0] = va_arg( args, size_t );
1439 TRACE( "hwnd %p %s count %d\n", win->hwnd, names[operation], perf.args[0] );
1440 break;
1441 case NATIVE_WINDOW_SET_BUFFERS_DIMENSIONS:
1442 case NATIVE_WINDOW_SET_BUFFERS_USER_DIMENSIONS:
1443 perf.args[0] = va_arg( args, int );
1444 perf.args[1] = va_arg( args, int );
1445 TRACE( "hwnd %p %s arg %dx%d\n", win->hwnd, names[operation], perf.args[0], perf.args[1] );
1446 break;
1447 case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY:
1448 perf.args[0] = va_arg( args, int );
1449 perf.args[1] = va_arg( args, int );
1450 perf.args[2] = va_arg( args, int );
1451 TRACE( "hwnd %p %s arg %dx%d %d\n", win->hwnd, names[operation],
1452 perf.args[0], perf.args[1], perf.args[2] );
1453 break;
1454 case NATIVE_WINDOW_SET_CROP:
1456 android_native_rect_t *rect = va_arg( args, android_native_rect_t * );
1457 perf.args[0] = rect->left;
1458 perf.args[1] = rect->top;
1459 perf.args[2] = rect->right;
1460 perf.args[3] = rect->bottom;
1461 TRACE( "hwnd %p %s rect %d,%d-%d,%d\n", win->hwnd, names[operation],
1462 perf.args[0], perf.args[1], perf.args[2], perf.args[3] );
1463 break;
1465 case NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP:
1467 int64_t timestamp = va_arg( args, int64_t );
1468 perf.args[0] = timestamp;
1469 perf.args[1] = timestamp >> 32;
1470 TRACE( "hwnd %p %s arg %08x%08x\n", win->hwnd, names[operation], perf.args[1], perf.args[0] );
1471 break;
1473 case NATIVE_WINDOW_LOCK:
1475 struct ANativeWindowBuffer *buffer;
1476 struct ANativeWindow_Buffer *buffer_ret = va_arg( args, ANativeWindow_Buffer * );
1477 ARect *bounds = va_arg( args, ARect * );
1478 int ret = window->dequeueBuffer_DEPRECATED( window, &buffer );
1479 if (!ret)
1481 if ((ret = gralloc_lock( buffer, &buffer_ret->bits )))
1483 WARN( "gralloc->lock %p failed %d %s\n", win->hwnd, ret, strerror(-ret) );
1484 window->cancelBuffer( window, buffer, -1 );
1487 if (!ret)
1489 buffer_ret->width = buffer->width;
1490 buffer_ret->height = buffer->height;
1491 buffer_ret->stride = buffer->stride;
1492 buffer_ret->format = buffer->format;
1493 win->locked_buffer = buffer;
1494 if (bounds)
1496 bounds->left = 0;
1497 bounds->top = 0;
1498 bounds->right = buffer->width;
1499 bounds->bottom = buffer->height;
1502 va_end( args );
1503 TRACE( "hwnd %p %s bits %p ret %d %s\n", win->hwnd, names[operation], buffer_ret->bits, ret, strerror(-ret) );
1504 return ret;
1506 case NATIVE_WINDOW_UNLOCK_AND_POST:
1508 int ret = -EINVAL;
1509 if (win->locked_buffer)
1511 gralloc_unlock( win->locked_buffer );
1512 ret = window->queueBuffer( window, win->locked_buffer, -1 );
1513 win->locked_buffer = NULL;
1515 va_end( args );
1516 TRACE( "hwnd %p %s ret %d\n", win->hwnd, names[operation], ret );
1517 return ret;
1519 case NATIVE_WINDOW_CONNECT:
1520 case NATIVE_WINDOW_DISCONNECT:
1521 TRACE( "hwnd %p %s\n", win->hwnd, names[operation] );
1522 break;
1523 case NATIVE_WINDOW_SET_POST_TRANSFORM_CROP:
1524 default:
1525 FIXME( "unsupported perform hwnd %p op %d %s\n", win->hwnd, operation,
1526 operation < ARRAY_SIZE( names ) ? names[operation] : "???" );
1527 break;
1529 va_end( args );
1530 return android_ioctl( IOCTL_PERFORM, &perf, sizeof(perf), NULL, NULL );
1533 struct ANativeWindow *create_ioctl_window( HWND hwnd, BOOL opengl, float scale )
1535 struct ioctl_android_create_window req;
1536 struct native_win_wrapper *win = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*win) );
1538 if (!win) return NULL;
1540 win->win.common.magic = ANDROID_NATIVE_WINDOW_MAGIC;
1541 win->win.common.version = sizeof(ANativeWindow);
1542 win->win.common.incRef = win_incRef;
1543 win->win.common.decRef = win_decRef;
1544 win->win.setSwapInterval = setSwapInterval;
1545 win->win.dequeueBuffer_DEPRECATED = dequeueBuffer_DEPRECATED;
1546 win->win.lockBuffer_DEPRECATED = lockBuffer_DEPRECATED;
1547 win->win.queueBuffer_DEPRECATED = queueBuffer_DEPRECATED;
1548 win->win.query = query;
1549 win->win.perform = perform;
1550 win->win.cancelBuffer_DEPRECATED = cancelBuffer_DEPRECATED;
1551 win->win.dequeueBuffer = dequeueBuffer;
1552 win->win.queueBuffer = queueBuffer;
1553 win->win.cancelBuffer = cancelBuffer;
1554 win->ref = 1;
1555 win->hwnd = hwnd;
1556 win->opengl = opengl;
1557 TRACE( "-> %p %p opengl=%u\n", win, win->hwnd, opengl );
1559 req.hdr.hwnd = HandleToLong( win->hwnd );
1560 req.hdr.opengl = win->opengl;
1561 req.parent = get_ioctl_win_parent( GetAncestor( hwnd, GA_PARENT ));
1562 req.scale = scale;
1563 android_ioctl( IOCTL_CREATE_WINDOW, &req, sizeof(req), NULL, NULL );
1565 return &win->win;
1568 struct ANativeWindow *grab_ioctl_window( struct ANativeWindow *window )
1570 struct native_win_wrapper *win = (struct native_win_wrapper *)window;
1571 InterlockedIncrement( &win->ref );
1572 return window;
1575 void release_ioctl_window( struct ANativeWindow *window )
1577 struct native_win_wrapper *win = (struct native_win_wrapper *)window;
1578 unsigned int i;
1580 if (InterlockedDecrement( &win->ref ) > 0) return;
1582 TRACE( "%p %p\n", win, win->hwnd );
1583 for (i = 0; i < ARRAY_SIZE( win->buffers ); i++)
1584 if (win->buffers[i]) win->buffers[i]->buffer.common.decRef( &win->buffers[i]->buffer.common );
1586 destroy_ioctl_window( win->hwnd, win->opengl );
1587 HeapFree( GetProcessHeap(), 0, win );
1590 void destroy_ioctl_window( HWND hwnd, BOOL opengl )
1592 struct ioctl_android_destroy_window req;
1594 req.hdr.hwnd = HandleToLong( hwnd );
1595 req.hdr.opengl = opengl;
1596 android_ioctl( IOCTL_DESTROY_WINDOW, &req, sizeof(req), NULL, NULL );
1599 int ioctl_window_pos_changed( HWND hwnd, const RECT *window_rect, const RECT *client_rect,
1600 const RECT *visible_rect, UINT style, UINT flags, HWND after, HWND owner )
1602 struct ioctl_android_window_pos_changed req;
1604 req.hdr.hwnd = HandleToLong( hwnd );
1605 req.hdr.opengl = FALSE;
1606 req.window_rect = *window_rect;
1607 req.client_rect = *client_rect;
1608 req.visible_rect = *visible_rect;
1609 req.style = style;
1610 req.flags = flags;
1611 req.after = HandleToLong( after );
1612 req.owner = HandleToLong( owner );
1613 return android_ioctl( IOCTL_WINDOW_POS_CHANGED, &req, sizeof(req), NULL, NULL );
1616 int ioctl_set_window_parent( HWND hwnd, HWND parent, float scale )
1618 struct ioctl_android_set_window_parent req;
1620 req.hdr.hwnd = HandleToLong( hwnd );
1621 req.hdr.opengl = FALSE;
1622 req.parent = get_ioctl_win_parent( parent );
1623 req.scale = scale;
1624 return android_ioctl( IOCTL_SET_WINDOW_PARENT, &req, sizeof(req), NULL, NULL );
1627 int ioctl_set_capture( HWND hwnd )
1629 struct ioctl_android_set_capture req;
1631 req.hdr.hwnd = HandleToLong( hwnd );
1632 req.hdr.opengl = FALSE;
1633 return android_ioctl( IOCTL_SET_CAPTURE, &req, sizeof(req), NULL, NULL );
1636 int ioctl_set_cursor( int id, int width, int height,
1637 int hotspotx, int hotspoty, const unsigned int *bits )
1639 struct ioctl_android_set_cursor *req;
1640 unsigned int size = offsetof( struct ioctl_android_set_cursor, bits[width * height] );
1641 int ret;
1643 if (!(req = HeapAlloc( GetProcessHeap(), 0, size ))) return -ENOMEM;
1644 req->hdr.hwnd = 0; /* unused */
1645 req->hdr.opengl = FALSE;
1646 req->id = id;
1647 req->width = width;
1648 req->height = height;
1649 req->hotspotx = hotspotx;
1650 req->hotspoty = hotspoty;
1651 memcpy( req->bits, bits, width * height * sizeof(req->bits[0]) );
1652 ret = android_ioctl( IOCTL_SET_CURSOR, req, size, NULL, NULL );
1653 HeapFree( GetProcessHeap(), 0, req );
1654 return ret;