ntdll: Add stub for RtlGetUnloadEventTraceEx.
[wine.git] / dlls / wineandroid.drv / device.c
blob0800ad7947506e5e070894d9c97c70bc3e70a715
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 if (win) pANativeWindow_release( win );
524 if (data && win) 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 if (win)
534 wrap_java_call();
535 if (data->api) win->perform( win, NATIVE_WINDOW_API_CONNECT, data->api );
536 win->perform( win, NATIVE_WINDOW_SET_BUFFERS_FORMAT, data->buffer_format );
537 win->setSwapInterval( win, data->swap_interval );
538 unwrap_java_call();
539 PostMessageW( hwnd, WM_ANDROID_REFRESH, opengl, 0 );
541 TRACE( "%p -> %p win %p\n", hwnd, data, win );
544 /* register a native window received from the Java side for use in ioctls */
545 void register_native_window( HWND hwnd, struct ANativeWindow *win, BOOL opengl )
547 NtQueueApcThread( thread, register_native_window_callback, (ULONG_PTR)hwnd, (ULONG_PTR)win, opengl );
550 void init_gralloc( const struct hw_module_t *module )
552 struct hw_device_t *device;
553 int ret;
555 TRACE( "got module %p ver %u.%u id %s name %s author %s\n",
556 module, module->module_api_version >> 8, module->module_api_version & 0xff,
557 debugstr_a(module->id), debugstr_a(module->name), debugstr_a(module->author) );
559 switch (module->module_api_version >> 8)
561 case 0:
562 gralloc_module = (struct gralloc_module_t *)module;
563 break;
564 case 1:
565 if (!(ret = module->methods->open( module, GRALLOC_HARDWARE_MODULE_ID, &device )))
567 int32_t caps[64];
568 uint32_t i, count = ARRAY_SIZE(caps);
570 gralloc1_device = (struct gralloc1_device *)device;
571 gralloc1_retain = gralloc1_device->getFunction( gralloc1_device, GRALLOC1_FUNCTION_RETAIN );
572 gralloc1_release = gralloc1_device->getFunction( gralloc1_device, GRALLOC1_FUNCTION_RELEASE );
573 gralloc1_lock = gralloc1_device->getFunction( gralloc1_device, GRALLOC1_FUNCTION_LOCK );
574 gralloc1_unlock = gralloc1_device->getFunction( gralloc1_device, GRALLOC1_FUNCTION_UNLOCK );
575 TRACE( "got device version %u funcs %p %p %p %p\n", device->version,
576 gralloc1_retain, gralloc1_release, gralloc1_lock, gralloc1_unlock );
578 gralloc1_device->getCapabilities( gralloc1_device, &count, caps );
579 if (count == ARRAY_SIZE(caps)) ERR( "too many gralloc capabilities\n" );
580 for (i = 0; i < count; i++)
581 if (caps[i] < ARRAY_SIZE(gralloc1_caps)) gralloc1_caps[caps[i]] = TRUE;
583 else ERR( "failed to open gralloc err %d\n", ret );
584 break;
585 default:
586 ERR( "unknown gralloc module version %u\n", module->module_api_version >> 8 );
587 break;
591 static int gralloc_grab_buffer( struct ANativeWindowBuffer *buffer )
593 if (gralloc1_device)
594 return gralloc1_retain( gralloc1_device, buffer->handle );
595 if (gralloc_module)
596 return gralloc_module->registerBuffer( gralloc_module, buffer->handle );
597 return -ENODEV;
600 static void gralloc_release_buffer( struct ANativeWindowBuffer *buffer )
602 if (gralloc1_device) gralloc1_release( gralloc1_device, buffer->handle );
603 else if (gralloc_module) gralloc_module->unregisterBuffer( gralloc_module, buffer->handle );
605 if (!gralloc1_caps[GRALLOC1_CAPABILITY_RELEASE_IMPLY_DELETE])
606 close_native_handle( (native_handle_t *)buffer->handle );
609 static int gralloc_lock( struct ANativeWindowBuffer *buffer, void **bits )
611 if (gralloc1_device)
613 gralloc1_rect_t rect = { 0, 0, buffer->width, buffer->height };
614 return gralloc1_lock( gralloc1_device, buffer->handle,
615 GRALLOC1_PRODUCER_USAGE_CPU_READ_OFTEN |
616 GRALLOC1_PRODUCER_USAGE_CPU_WRITE_OFTEN,
617 GRALLOC1_CONSUMER_USAGE_NONE, &rect, bits, -1 );
619 if (gralloc_module)
620 return gralloc_module->lock( gralloc_module, buffer->handle,
621 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
622 0, 0, buffer->width, buffer->height, bits );
624 *bits = ((struct native_buffer_wrapper *)buffer)->bits;
625 return 0;
628 static void gralloc_unlock( struct ANativeWindowBuffer *buffer )
630 if (gralloc1_device)
632 int fence;
633 gralloc1_unlock( gralloc1_device, buffer->handle, &fence );
634 wait_fence_and_close( fence );
636 else if (gralloc_module) gralloc_module->unlock( gralloc_module, buffer->handle );
639 /* get the capture window stored in the desktop process */
640 HWND get_capture_window(void)
642 return capture_window;
645 static NTSTATUS android_error_to_status( int err )
647 switch (err)
649 case 0: return STATUS_SUCCESS;
650 case -ENOMEM: return STATUS_NO_MEMORY;
651 case -ENOSYS: return STATUS_NOT_SUPPORTED;
652 case -EINVAL: return STATUS_INVALID_PARAMETER;
653 case -ENOENT: return STATUS_INVALID_HANDLE;
654 case -EPERM: return STATUS_ACCESS_DENIED;
655 case -ENODEV: return STATUS_NO_SUCH_DEVICE;
656 case -EEXIST: return STATUS_DUPLICATE_NAME;
657 case -EPIPE: return STATUS_PIPE_DISCONNECTED;
658 case -ENODATA: return STATUS_NO_MORE_FILES;
659 case -ETIMEDOUT: return STATUS_IO_TIMEOUT;
660 case -EBADMSG: return STATUS_INVALID_DEVICE_REQUEST;
661 case -EWOULDBLOCK: return STATUS_DEVICE_NOT_READY;
662 default:
663 FIXME( "unmapped error %d\n", err );
664 return STATUS_UNSUCCESSFUL;
668 static int status_to_android_error( NTSTATUS status )
670 switch (status)
672 case STATUS_SUCCESS: return 0;
673 case STATUS_NO_MEMORY: return -ENOMEM;
674 case STATUS_NOT_SUPPORTED: return -ENOSYS;
675 case STATUS_INVALID_PARAMETER: return -EINVAL;
676 case STATUS_BUFFER_OVERFLOW: return -EINVAL;
677 case STATUS_INVALID_HANDLE: return -ENOENT;
678 case STATUS_ACCESS_DENIED: return -EPERM;
679 case STATUS_NO_SUCH_DEVICE: return -ENODEV;
680 case STATUS_DUPLICATE_NAME: return -EEXIST;
681 case STATUS_PIPE_DISCONNECTED: return -EPIPE;
682 case STATUS_NO_MORE_FILES: return -ENODATA;
683 case STATUS_IO_TIMEOUT: return -ETIMEDOUT;
684 case STATUS_INVALID_DEVICE_REQUEST: return -EBADMSG;
685 case STATUS_DEVICE_NOT_READY: return -EWOULDBLOCK;
686 default:
687 FIXME( "unmapped status %08x\n", status );
688 return -EINVAL;
692 static jobject load_java_method( jmethodID *method, const char *name, const char *args )
694 jobject object = wine_get_java_object();
696 if (!*method)
698 jclass class;
700 wrap_java_call();
701 class = (*jni_env)->GetObjectClass( jni_env, object );
702 *method = (*jni_env)->GetMethodID( jni_env, class, name, args );
703 unwrap_java_call();
704 if (!*method)
706 FIXME( "method %s not found\n", name );
707 return NULL;
710 return object;
713 static void create_desktop_window( HWND hwnd )
715 static jmethodID method;
716 jobject object;
718 if (!(object = load_java_method( &method, "createDesktopWindow", "(I)V" ))) return;
720 wrap_java_call();
721 (*jni_env)->CallVoidMethod( jni_env, object, method, HandleToLong( hwnd ));
722 unwrap_java_call();
725 static NTSTATUS createWindow_ioctl( void *data, DWORD in_size, DWORD out_size, ULONG_PTR *ret_size )
727 static jmethodID method;
728 jobject object;
729 struct ioctl_android_create_window *res = data;
730 struct native_win_data *win_data;
731 DWORD pid = current_client_id();
733 if (in_size < sizeof(*res)) return STATUS_INVALID_PARAMETER;
735 if (!(win_data = create_native_win_data( LongToHandle(res->hdr.hwnd), res->hdr.opengl )))
736 return STATUS_NO_MEMORY;
738 TRACE( "hwnd %08x opengl %u parent %08x\n", res->hdr.hwnd, res->hdr.opengl, res->parent );
740 if (!(object = load_java_method( &method, "createWindow", "(IZIFI)V" ))) return STATUS_NOT_SUPPORTED;
742 wrap_java_call();
743 (*jni_env)->CallVoidMethod( jni_env, object, method, res->hdr.hwnd, res->hdr.opengl, res->parent, res->scale, pid );
744 unwrap_java_call();
745 return STATUS_SUCCESS;
748 static NTSTATUS destroyWindow_ioctl( void *data, DWORD in_size, DWORD out_size, ULONG_PTR *ret_size )
750 static jmethodID method;
751 jobject object;
752 struct ioctl_android_destroy_window *res = data;
753 struct native_win_data *win_data;
755 if (in_size < sizeof(*res)) return STATUS_INVALID_PARAMETER;
757 win_data = get_ioctl_native_win_data( &res->hdr );
759 TRACE( "hwnd %08x opengl %u\n", res->hdr.hwnd, res->hdr.opengl );
761 if (!(object = load_java_method( &method, "destroyWindow", "(I)V" ))) return STATUS_NOT_SUPPORTED;
763 wrap_java_call();
764 (*jni_env)->CallVoidMethod( jni_env, object, method, res->hdr.hwnd );
765 unwrap_java_call();
766 if (win_data) free_native_win_data( win_data );
767 return STATUS_SUCCESS;
770 static NTSTATUS windowPosChanged_ioctl( void *data, DWORD in_size, DWORD out_size, ULONG_PTR *ret_size )
772 static jmethodID method;
773 jobject object;
774 struct ioctl_android_window_pos_changed *res = data;
776 if (in_size < sizeof(*res)) return STATUS_INVALID_PARAMETER;
778 TRACE( "hwnd %08x win %s client %s visible %s style %08x flags %08x after %08x owner %08x\n",
779 res->hdr.hwnd, wine_dbgstr_rect(&res->window_rect), wine_dbgstr_rect(&res->client_rect),
780 wine_dbgstr_rect(&res->visible_rect), res->style, res->flags, res->after, res->owner );
782 if (!(object = load_java_method( &method, "windowPosChanged", "(IIIIIIIIIIIIIIIII)V" )))
783 return STATUS_NOT_SUPPORTED;
785 wrap_java_call();
786 (*jni_env)->CallVoidMethod( jni_env, object, method, res->hdr.hwnd, res->flags, res->after, res->owner, res->style,
787 res->window_rect.left, res->window_rect.top, res->window_rect.right, res->window_rect.bottom,
788 res->client_rect.left, res->client_rect.top, res->client_rect.right, res->client_rect.bottom,
789 res->visible_rect.left, res->visible_rect.top, res->visible_rect.right, res->visible_rect.bottom );
790 unwrap_java_call();
791 return STATUS_SUCCESS;
794 static NTSTATUS dequeueBuffer_ioctl( void *data, DWORD in_size, DWORD out_size, ULONG_PTR *ret_size )
796 struct ANativeWindow *parent;
797 struct ioctl_android_dequeueBuffer *res = data;
798 struct native_win_data *win_data;
799 struct ANativeWindowBuffer *buffer;
800 int fence, ret, is_new;
802 if (out_size < sizeof( *res )) return STATUS_BUFFER_OVERFLOW;
804 if (in_size < offsetof( struct ioctl_android_dequeueBuffer, native_handle ))
805 return STATUS_INVALID_PARAMETER;
807 if (!(win_data = get_ioctl_native_win_data( &res->hdr ))) return STATUS_INVALID_HANDLE;
808 if (!(parent = win_data->parent)) return STATUS_DEVICE_NOT_READY;
810 *ret_size = offsetof( struct ioctl_android_dequeueBuffer, native_handle );
811 wrap_java_call();
812 ret = parent->dequeueBuffer( parent, &buffer, &fence );
813 unwrap_java_call();
814 if (!ret)
816 HANDLE mapping = 0;
818 TRACE( "%08x got buffer %p fence %d\n", res->hdr.hwnd, buffer, fence );
819 res->width = buffer->width;
820 res->height = buffer->height;
821 res->stride = buffer->stride;
822 res->format = buffer->format;
823 res->usage = buffer->usage;
824 res->buffer_id = register_buffer( win_data, buffer, res->win32 ? &mapping : NULL, &is_new );
825 res->generation = win_data->generation;
826 if (is_new)
828 HANDLE process = OpenProcess( PROCESS_DUP_HANDLE, FALSE, current_client_id() );
829 map_native_handle( &res->native_handle, buffer->handle, mapping, process );
830 CloseHandle( process );
831 *ret_size = sizeof( *res );
833 wait_fence_and_close( fence );
834 return STATUS_SUCCESS;
836 ERR( "%08x failed %d\n", res->hdr.hwnd, ret );
837 return android_error_to_status( ret );
840 static NTSTATUS cancelBuffer_ioctl( void *data, DWORD in_size, DWORD out_size, ULONG_PTR *ret_size )
842 struct ioctl_android_cancelBuffer *res = data;
843 struct ANativeWindow *parent;
844 struct ANativeWindowBuffer *buffer;
845 struct native_win_data *win_data;
846 int ret;
848 if (in_size < sizeof(*res)) return STATUS_INVALID_PARAMETER;
850 if (!(win_data = get_ioctl_native_win_data( &res->hdr ))) return STATUS_INVALID_HANDLE;
851 if (!(parent = win_data->parent)) return STATUS_DEVICE_NOT_READY;
852 if (res->generation != win_data->generation) return STATUS_SUCCESS; /* obsolete buffer, ignore */
854 if (!(buffer = get_registered_buffer( win_data, res->buffer_id ))) return STATUS_INVALID_HANDLE;
856 TRACE( "%08x buffer %p\n", res->hdr.hwnd, buffer );
857 wrap_java_call();
858 ret = parent->cancelBuffer( parent, buffer, -1 );
859 unwrap_java_call();
860 return android_error_to_status( ret );
863 static NTSTATUS queueBuffer_ioctl( void *data, DWORD in_size, DWORD out_size, ULONG_PTR *ret_size )
865 struct ioctl_android_queueBuffer *res = data;
866 struct ANativeWindow *parent;
867 struct ANativeWindowBuffer *buffer;
868 struct native_win_data *win_data;
869 int ret;
871 if (in_size < sizeof(*res)) return STATUS_INVALID_PARAMETER;
873 if (!(win_data = get_ioctl_native_win_data( &res->hdr ))) return STATUS_INVALID_HANDLE;
874 if (!(parent = win_data->parent)) return STATUS_DEVICE_NOT_READY;
875 if (res->generation != win_data->generation) return STATUS_SUCCESS; /* obsolete buffer, ignore */
877 if (!(buffer = get_registered_buffer( win_data, res->buffer_id ))) return STATUS_INVALID_HANDLE;
879 TRACE( "%08x buffer %p mapping %p\n", res->hdr.hwnd, buffer, win_data->mappings[res->buffer_id] );
880 if (win_data->mappings[res->buffer_id])
882 void *bits;
883 ret = gralloc_lock( buffer, &bits );
884 if (ret) return android_error_to_status( ret );
885 memcpy( bits, win_data->mappings[res->buffer_id], buffer->stride * buffer->height * 4 );
886 gralloc_unlock( buffer );
888 wrap_java_call();
889 ret = parent->queueBuffer( parent, buffer, -1 );
890 unwrap_java_call();
891 return android_error_to_status( ret );
894 static NTSTATUS query_ioctl( void *data, DWORD in_size, DWORD out_size, ULONG_PTR *ret_size )
896 struct ioctl_android_query *res = data;
897 struct ANativeWindow *parent;
898 struct native_win_data *win_data;
899 int ret;
901 if (in_size < sizeof(*res)) return STATUS_INVALID_PARAMETER;
902 if (out_size < sizeof(*res)) return STATUS_BUFFER_OVERFLOW;
904 if (!(win_data = get_ioctl_native_win_data( &res->hdr ))) return STATUS_INVALID_HANDLE;
905 if (!(parent = win_data->parent)) return STATUS_DEVICE_NOT_READY;
907 *ret_size = sizeof( *res );
908 wrap_java_call();
909 ret = parent->query( parent, res->what, &res->value );
910 unwrap_java_call();
911 return android_error_to_status( ret );
914 static NTSTATUS perform_ioctl( void *data, DWORD in_size, DWORD out_size, ULONG_PTR *ret_size )
916 struct ioctl_android_perform *res = data;
917 struct ANativeWindow *parent;
918 struct native_win_data *win_data;
919 int ret = -ENOENT;
921 if (in_size < sizeof(*res)) return STATUS_INVALID_PARAMETER;
923 if (!(win_data = get_ioctl_native_win_data( &res->hdr ))) return STATUS_INVALID_HANDLE;
924 if (!(parent = win_data->parent)) return STATUS_DEVICE_NOT_READY;
926 switch (res->operation)
928 case NATIVE_WINDOW_SET_BUFFERS_FORMAT:
929 wrap_java_call();
930 ret = parent->perform( parent, res->operation, res->args[0] );
931 unwrap_java_call();
932 if (!ret) win_data->buffer_format = res->args[0];
933 break;
934 case NATIVE_WINDOW_API_CONNECT:
935 wrap_java_call();
936 ret = parent->perform( parent, res->operation, res->args[0] );
937 unwrap_java_call();
938 if (!ret) win_data->api = res->args[0];
939 break;
940 case NATIVE_WINDOW_API_DISCONNECT:
941 wrap_java_call();
942 ret = parent->perform( parent, res->operation, res->args[0] );
943 unwrap_java_call();
944 if (!ret) win_data->api = 0;
945 break;
946 case NATIVE_WINDOW_SET_USAGE:
947 case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM:
948 case NATIVE_WINDOW_SET_SCALING_MODE:
949 wrap_java_call();
950 ret = parent->perform( parent, res->operation, res->args[0] );
951 unwrap_java_call();
952 break;
953 case NATIVE_WINDOW_SET_BUFFER_COUNT:
954 wrap_java_call();
955 ret = parent->perform( parent, res->operation, (size_t)res->args[0] );
956 unwrap_java_call();
957 break;
958 case NATIVE_WINDOW_SET_BUFFERS_DIMENSIONS:
959 case NATIVE_WINDOW_SET_BUFFERS_USER_DIMENSIONS:
960 wrap_java_call();
961 ret = parent->perform( parent, res->operation, res->args[0], res->args[1] );
962 unwrap_java_call();
963 break;
964 case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY:
965 wrap_java_call();
966 ret = parent->perform( parent, res->operation, res->args[0], res->args[1], res->args[2] );
967 unwrap_java_call();
968 break;
969 case NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP:
970 wrap_java_call();
971 ret = parent->perform( parent, res->operation, res->args[0] | ((int64_t)res->args[1] << 32) );
972 unwrap_java_call();
973 break;
974 case NATIVE_WINDOW_CONNECT:
975 case NATIVE_WINDOW_DISCONNECT:
976 case NATIVE_WINDOW_UNLOCK_AND_POST:
977 wrap_java_call();
978 ret = parent->perform( parent, res->operation );
979 unwrap_java_call();
980 break;
981 case NATIVE_WINDOW_SET_CROP:
983 android_native_rect_t rect;
984 rect.left = res->args[0];
985 rect.top = res->args[1];
986 rect.right = res->args[2];
987 rect.bottom = res->args[3];
988 wrap_java_call();
989 ret = parent->perform( parent, res->operation, &rect );
990 unwrap_java_call();
991 break;
993 case NATIVE_WINDOW_LOCK:
994 default:
995 FIXME( "unsupported perform op %d\n", res->operation );
996 break;
998 return android_error_to_status( ret );
1001 static NTSTATUS setSwapInterval_ioctl( void *data, DWORD in_size, DWORD out_size, ULONG_PTR *ret_size )
1003 struct ioctl_android_set_swap_interval *res = data;
1004 struct ANativeWindow *parent;
1005 struct native_win_data *win_data;
1006 int ret;
1008 if (in_size < sizeof(*res)) return STATUS_INVALID_PARAMETER;
1010 if (!(win_data = get_ioctl_native_win_data( &res->hdr ))) return STATUS_INVALID_HANDLE;
1011 win_data->swap_interval = res->interval;
1013 if (!(parent = win_data->parent)) return STATUS_SUCCESS;
1014 wrap_java_call();
1015 ret = parent->setSwapInterval( parent, res->interval );
1016 unwrap_java_call();
1017 return android_error_to_status( ret );
1020 static NTSTATUS setWindowParent_ioctl( void *data, DWORD in_size, DWORD out_size, ULONG_PTR *ret_size )
1022 static jmethodID method;
1023 jobject object;
1024 struct ioctl_android_set_window_parent *res = data;
1025 struct native_win_data *win_data;
1026 DWORD pid = current_client_id();
1028 if (in_size < sizeof(*res)) return STATUS_INVALID_PARAMETER;
1030 if (!(win_data = get_ioctl_native_win_data( &res->hdr ))) return STATUS_INVALID_HANDLE;
1032 TRACE( "hwnd %08x parent %08x\n", res->hdr.hwnd, res->parent );
1034 if (!(object = load_java_method( &method, "setParent", "(IIFI)V" ))) return STATUS_NOT_SUPPORTED;
1036 wrap_java_call();
1037 (*jni_env)->CallVoidMethod( jni_env, object, method, res->hdr.hwnd, res->parent, res->scale, pid );
1038 unwrap_java_call();
1039 return STATUS_SUCCESS;
1042 static NTSTATUS setCapture_ioctl( void *data, DWORD in_size, DWORD out_size, ULONG_PTR *ret_size )
1044 struct ioctl_android_set_capture *res = data;
1046 if (in_size < sizeof(*res)) return STATUS_INVALID_PARAMETER;
1048 if (res->hdr.hwnd && !get_ioctl_native_win_data( &res->hdr )) return STATUS_INVALID_HANDLE;
1050 TRACE( "hwnd %08x\n", res->hdr.hwnd );
1052 InterlockedExchangePointer( (void **)&capture_window, LongToHandle( res->hdr.hwnd ));
1053 return STATUS_SUCCESS;
1056 static NTSTATUS setCursor_ioctl( void *data, DWORD in_size, DWORD out_size, ULONG_PTR *ret_size )
1058 static jmethodID method;
1059 jobject object;
1060 int size;
1061 struct ioctl_android_set_cursor *res = data;
1063 if (in_size < offsetof( struct ioctl_android_set_cursor, bits )) return STATUS_INVALID_PARAMETER;
1065 if (res->width < 0 || res->height < 0 || res->width > 256 || res->height > 256)
1066 return STATUS_INVALID_PARAMETER;
1068 size = res->width * res->height;
1069 if (in_size != offsetof( struct ioctl_android_set_cursor, bits[size] ))
1070 return STATUS_INVALID_PARAMETER;
1072 TRACE( "hwnd %08x size %d\n", res->hdr.hwnd, size );
1074 if (!(object = load_java_method( &method, "setCursor", "(IIIII[I)V" )))
1075 return STATUS_NOT_SUPPORTED;
1077 wrap_java_call();
1079 if (size)
1081 jintArray array = (*jni_env)->NewIntArray( jni_env, size );
1082 (*jni_env)->SetIntArrayRegion( jni_env, array, 0, size, (jint *)res->bits );
1083 (*jni_env)->CallVoidMethod( jni_env, object, method, 0, res->width, res->height,
1084 res->hotspotx, res->hotspoty, array );
1085 (*jni_env)->DeleteLocalRef( jni_env, array );
1087 else (*jni_env)->CallVoidMethod( jni_env, object, method, res->id, 0, 0, 0, 0, 0 );
1089 unwrap_java_call();
1091 return STATUS_SUCCESS;
1094 typedef NTSTATUS (*ioctl_func)( void *in, DWORD in_size, DWORD out_size, ULONG_PTR *ret_size );
1095 static const ioctl_func ioctl_funcs[] =
1097 createWindow_ioctl, /* IOCTL_CREATE_WINDOW */
1098 destroyWindow_ioctl, /* IOCTL_DESTROY_WINDOW */
1099 windowPosChanged_ioctl, /* IOCTL_WINDOW_POS_CHANGED */
1100 setWindowParent_ioctl, /* IOCTL_SET_WINDOW_PARENT */
1101 dequeueBuffer_ioctl, /* IOCTL_DEQUEUE_BUFFER */
1102 queueBuffer_ioctl, /* IOCTL_QUEUE_BUFFER */
1103 cancelBuffer_ioctl, /* IOCTL_CANCEL_BUFFER */
1104 query_ioctl, /* IOCTL_QUERY */
1105 perform_ioctl, /* IOCTL_PERFORM */
1106 setSwapInterval_ioctl, /* IOCTL_SET_SWAP_INT */
1107 setCapture_ioctl, /* IOCTL_SET_CAPTURE */
1108 setCursor_ioctl, /* IOCTL_SET_CURSOR */
1111 static NTSTATUS WINAPI ioctl_callback( DEVICE_OBJECT *device, IRP *irp )
1113 IO_STACK_LOCATION *irpsp = IoGetCurrentIrpStackLocation( irp );
1114 DWORD code = (irpsp->Parameters.DeviceIoControl.IoControlCode - ANDROID_IOCTL(0)) >> 2;
1116 if (code < NB_IOCTLS)
1118 struct ioctl_header *header = irp->AssociatedIrp.SystemBuffer;
1119 DWORD in_size = irpsp->Parameters.DeviceIoControl.InputBufferLength;
1120 ioctl_func func = ioctl_funcs[code];
1122 if (in_size >= sizeof(*header))
1124 irp->IoStatus.Information = 0;
1125 irp->IoStatus.u.Status = func( irp->AssociatedIrp.SystemBuffer, in_size,
1126 irpsp->Parameters.DeviceIoControl.OutputBufferLength,
1127 &irp->IoStatus.Information );
1129 else irp->IoStatus.u.Status = STATUS_INVALID_PARAMETER;
1131 else
1133 FIXME( "ioctl %x not supported\n", irpsp->Parameters.DeviceIoControl.IoControlCode );
1134 irp->IoStatus.u.Status = STATUS_NOT_SUPPORTED;
1136 IoCompleteRequest( irp, IO_NO_INCREMENT );
1137 return STATUS_SUCCESS;
1140 static NTSTATUS CALLBACK init_android_driver( DRIVER_OBJECT *driver, UNICODE_STRING *name )
1142 static const WCHAR device_nameW[] = {'\\','D','e','v','i','c','e','\\','W','i','n','e','A','n','d','r','o','i','d',0 };
1143 static const WCHAR device_linkW[] = {'\\','?','?','\\','W','i','n','e','A','n','d','r','o','i','d',0 };
1145 UNICODE_STRING nameW, linkW;
1146 DEVICE_OBJECT *device;
1147 NTSTATUS status;
1149 driver->MajorFunction[IRP_MJ_DEVICE_CONTROL] = ioctl_callback;
1151 RtlInitUnicodeString( &nameW, device_nameW );
1152 RtlInitUnicodeString( &linkW, device_linkW );
1154 if ((status = IoCreateDevice( driver, 0, &nameW, 0, 0, FALSE, &device ))) return status;
1155 return IoCreateSymbolicLink( &linkW, &nameW );
1158 static DWORD CALLBACK device_thread( void *arg )
1160 static const WCHAR driver_nameW[] = {'\\','D','r','i','v','e','r','\\','W','i','n','e','A','n','d','r','o','i','d',0 };
1162 HANDLE start_event = arg;
1163 UNICODE_STRING nameW;
1164 NTSTATUS status;
1165 JavaVM *java_vm;
1166 DWORD ret;
1168 TRACE( "starting process %x\n", GetCurrentProcessId() );
1170 if (!(java_vm = wine_get_java_vm())) return 0; /* not running under Java */
1172 init_java_thread( java_vm );
1174 create_desktop_window( GetDesktopWindow() );
1176 RtlInitUnicodeString( &nameW, driver_nameW );
1177 if ((status = IoCreateDriver( &nameW, init_android_driver )))
1179 FIXME( "failed to create driver error %x\n", status );
1180 return status;
1183 stop_event = CreateEventW( NULL, TRUE, FALSE, NULL );
1184 SetEvent( start_event );
1186 ret = wine_ntoskrnl_main_loop( stop_event );
1188 wrap_java_call();
1189 (*java_vm)->DetachCurrentThread( java_vm );
1190 unwrap_java_call();
1191 return ret;
1194 void start_android_device(void)
1196 HANDLE handles[2];
1198 handles[0] = CreateEventW( NULL, TRUE, FALSE, NULL );
1199 handles[1] = thread = CreateThread( NULL, 0, device_thread, handles[0], 0, NULL );
1200 WaitForMultipleObjects( 2, handles, FALSE, INFINITE );
1201 CloseHandle( handles[0] );
1205 /* Client-side ioctl support */
1208 static int android_ioctl( enum android_ioctl code, void *in, DWORD in_size, void *out, DWORD *out_size )
1210 static const WCHAR deviceW[] = {'\\','\\','.','\\','W','i','n','e','A','n','d','r','o','i','d',0 };
1211 static HANDLE device;
1212 IO_STATUS_BLOCK iosb;
1213 NTSTATUS status;
1215 if (!device)
1217 HANDLE file = CreateFileW( deviceW, GENERIC_READ,
1218 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0 );
1219 if (file == INVALID_HANDLE_VALUE) return -ENOENT;
1220 if (InterlockedCompareExchangePointer( &device, file, NULL )) CloseHandle( file );
1223 status = NtDeviceIoControlFile( device, NULL, NULL, NULL, &iosb, ANDROID_IOCTL(code),
1224 in, in_size, out, out_size ? *out_size : 0 );
1225 if (status == STATUS_FILE_DELETED)
1227 WARN( "parent process is gone\n" );
1228 ExitProcess( 1 );
1230 if (out_size) *out_size = iosb.Information;
1231 return status_to_android_error( status );
1234 static void win_incRef( struct android_native_base_t *base )
1236 struct native_win_wrapper *win = (struct native_win_wrapper *)base;
1237 InterlockedIncrement( &win->ref );
1240 static void win_decRef( struct android_native_base_t *base )
1242 struct native_win_wrapper *win = (struct native_win_wrapper *)base;
1243 InterlockedDecrement( &win->ref );
1246 static void buffer_incRef( struct android_native_base_t *base )
1248 struct native_buffer_wrapper *buffer = (struct native_buffer_wrapper *)base;
1249 InterlockedIncrement( &buffer->ref );
1252 static void buffer_decRef( struct android_native_base_t *base )
1254 struct native_buffer_wrapper *buffer = (struct native_buffer_wrapper *)base;
1256 if (!InterlockedDecrement( &buffer->ref ))
1258 if (!is_in_desktop_process()) gralloc_release_buffer( &buffer->buffer );
1259 if (buffer->bits) UnmapViewOfFile( buffer->bits );
1260 HeapFree( GetProcessHeap(), 0, buffer );
1264 static int dequeueBuffer( struct ANativeWindow *window, struct ANativeWindowBuffer **buffer, int *fence )
1266 struct native_win_wrapper *win = (struct native_win_wrapper *)window;
1267 struct ioctl_android_dequeueBuffer res;
1268 DWORD size = sizeof(res);
1269 int ret, use_win32 = !gralloc_module && !gralloc1_device;
1271 res.hdr.hwnd = HandleToLong( win->hwnd );
1272 res.hdr.opengl = win->opengl;
1273 res.win32 = use_win32;
1274 ret = android_ioctl( IOCTL_DEQUEUE_BUFFER,
1275 &res, offsetof( struct ioctl_android_dequeueBuffer, native_handle ),
1276 &res, &size );
1277 if (ret) return ret;
1279 /* if we received the native handle, this is a new buffer */
1280 if (size > offsetof( struct ioctl_android_dequeueBuffer, native_handle ))
1282 struct native_buffer_wrapper *buf = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*buf) );
1284 buf->buffer.common.magic = ANDROID_NATIVE_BUFFER_MAGIC;
1285 buf->buffer.common.version = sizeof( buf->buffer );
1286 buf->buffer.common.incRef = buffer_incRef;
1287 buf->buffer.common.decRef = buffer_decRef;
1288 buf->buffer.width = res.width;
1289 buf->buffer.height = res.height;
1290 buf->buffer.stride = res.stride;
1291 buf->buffer.format = res.format;
1292 buf->buffer.usage = res.usage;
1293 buf->buffer.handle = unmap_native_handle( &res.native_handle.handle );
1294 buf->ref = 1;
1295 buf->hwnd = win->hwnd;
1296 buf->buffer_id = res.buffer_id;
1297 buf->generation = res.generation;
1298 if (win->buffers[res.buffer_id])
1299 win->buffers[res.buffer_id]->buffer.common.decRef(&win->buffers[res.buffer_id]->buffer.common);
1300 win->buffers[res.buffer_id] = buf;
1302 if (use_win32)
1304 HANDLE mapping = LongToHandle( res.native_handle.handle.data[0] );
1305 buf->bits = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 0 );
1306 CloseHandle( mapping );
1308 else if (!is_in_desktop_process())
1310 if ((ret = gralloc_grab_buffer( &buf->buffer )) < 0)
1311 WARN( "hwnd %p, buffer %p failed to register %d %s\n",
1312 win->hwnd, &buf->buffer, ret, strerror(-ret) );
1316 *buffer = &win->buffers[res.buffer_id]->buffer;
1317 *fence = -1;
1319 TRACE( "hwnd %p, buffer %p %dx%d stride %d fmt %d usage %d fence %d\n",
1320 win->hwnd, *buffer, res.width, res.height, res.stride, res.format, res.usage, *fence );
1321 return 0;
1324 static int cancelBuffer( struct ANativeWindow *window, struct ANativeWindowBuffer *buffer, int fence )
1326 struct native_win_wrapper *win = (struct native_win_wrapper *)window;
1327 struct native_buffer_wrapper *buf = (struct native_buffer_wrapper *)buffer;
1328 struct ioctl_android_cancelBuffer cancel;
1330 TRACE( "hwnd %p buffer %p %dx%d stride %d fmt %d usage %d fence %d\n",
1331 win->hwnd, buffer, buffer->width, buffer->height,
1332 buffer->stride, buffer->format, buffer->usage, fence );
1333 cancel.buffer_id = buf->buffer_id;
1334 cancel.generation = buf->generation;
1335 cancel.hdr.hwnd = HandleToLong( win->hwnd );
1336 cancel.hdr.opengl = win->opengl;
1337 wait_fence_and_close( fence );
1338 return android_ioctl( IOCTL_CANCEL_BUFFER, &cancel, sizeof(cancel), NULL, NULL );
1341 static int queueBuffer( struct ANativeWindow *window, struct ANativeWindowBuffer *buffer, int fence )
1343 struct native_win_wrapper *win = (struct native_win_wrapper *)window;
1344 struct native_buffer_wrapper *buf = (struct native_buffer_wrapper *)buffer;
1345 struct ioctl_android_queueBuffer queue;
1347 TRACE( "hwnd %p buffer %p %dx%d stride %d fmt %d usage %d fence %d\n",
1348 win->hwnd, buffer, buffer->width, buffer->height,
1349 buffer->stride, buffer->format, buffer->usage, fence );
1350 queue.buffer_id = buf->buffer_id;
1351 queue.generation = buf->generation;
1352 queue.hdr.hwnd = HandleToLong( win->hwnd );
1353 queue.hdr.opengl = win->opengl;
1354 wait_fence_and_close( fence );
1355 return android_ioctl( IOCTL_QUEUE_BUFFER, &queue, sizeof(queue), NULL, NULL );
1358 static int dequeueBuffer_DEPRECATED( struct ANativeWindow *window, struct ANativeWindowBuffer **buffer )
1360 int fence, ret = dequeueBuffer( window, buffer, &fence );
1362 if (!ret) wait_fence_and_close( fence );
1363 return ret;
1366 static int cancelBuffer_DEPRECATED( struct ANativeWindow *window, struct ANativeWindowBuffer *buffer )
1368 return cancelBuffer( window, buffer, -1 );
1371 static int lockBuffer_DEPRECATED( struct ANativeWindow *window, struct ANativeWindowBuffer *buffer )
1373 return 0; /* nothing to do */
1376 static int queueBuffer_DEPRECATED( struct ANativeWindow *window, struct ANativeWindowBuffer *buffer )
1378 return queueBuffer( window, buffer, -1 );
1381 static int setSwapInterval( struct ANativeWindow *window, int interval )
1383 struct native_win_wrapper *win = (struct native_win_wrapper *)window;
1384 struct ioctl_android_set_swap_interval swap;
1386 TRACE( "hwnd %p interval %d\n", win->hwnd, interval );
1387 swap.hdr.hwnd = HandleToLong( win->hwnd );
1388 swap.hdr.opengl = win->opengl;
1389 swap.interval = interval;
1390 return android_ioctl( IOCTL_SET_SWAP_INT, &swap, sizeof(swap), NULL, NULL );
1393 static int query( const ANativeWindow *window, int what, int *value )
1395 struct native_win_wrapper *win = (struct native_win_wrapper *)window;
1396 struct ioctl_android_query query;
1397 DWORD size = sizeof( query );
1398 int ret;
1400 query.hdr.hwnd = HandleToLong( win->hwnd );
1401 query.hdr.opengl = win->opengl;
1402 query.what = what;
1403 ret = android_ioctl( IOCTL_QUERY, &query, sizeof(query), &query, &size );
1404 TRACE( "hwnd %p what %d got %d -> %p\n", win->hwnd, what, query.value, value );
1405 if (!ret) *value = query.value;
1406 return ret;
1409 static int perform( ANativeWindow *window, int operation, ... )
1411 static const char * const names[] =
1413 "SET_USAGE", "CONNECT", "DISCONNECT", "SET_CROP", "SET_BUFFER_COUNT", "SET_BUFFERS_GEOMETRY",
1414 "SET_BUFFERS_TRANSFORM", "SET_BUFFERS_TIMESTAMP", "SET_BUFFERS_DIMENSIONS", "SET_BUFFERS_FORMAT",
1415 "SET_SCALING_MODE", "LOCK", "UNLOCK_AND_POST", "API_CONNECT", "API_DISCONNECT",
1416 "SET_BUFFERS_USER_DIMENSIONS", "SET_POST_TRANSFORM_CROP"
1419 struct native_win_wrapper *win = (struct native_win_wrapper *)window;
1420 struct ioctl_android_perform perf;
1421 va_list args;
1423 perf.hdr.hwnd = HandleToLong( win->hwnd );
1424 perf.hdr.opengl = win->opengl;
1425 perf.operation = operation;
1426 memset( perf.args, 0, sizeof(perf.args) );
1428 va_start( args, operation );
1429 switch (operation)
1431 case NATIVE_WINDOW_SET_USAGE:
1432 case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM:
1433 case NATIVE_WINDOW_SET_BUFFERS_FORMAT:
1434 case NATIVE_WINDOW_SET_SCALING_MODE:
1435 case NATIVE_WINDOW_API_CONNECT:
1436 case NATIVE_WINDOW_API_DISCONNECT:
1437 perf.args[0] = va_arg( args, int );
1438 TRACE( "hwnd %p %s arg %d\n", win->hwnd, names[operation], perf.args[0] );
1439 break;
1440 case NATIVE_WINDOW_SET_BUFFER_COUNT:
1441 perf.args[0] = va_arg( args, size_t );
1442 TRACE( "hwnd %p %s count %d\n", win->hwnd, names[operation], perf.args[0] );
1443 break;
1444 case NATIVE_WINDOW_SET_BUFFERS_DIMENSIONS:
1445 case NATIVE_WINDOW_SET_BUFFERS_USER_DIMENSIONS:
1446 perf.args[0] = va_arg( args, int );
1447 perf.args[1] = va_arg( args, int );
1448 TRACE( "hwnd %p %s arg %dx%d\n", win->hwnd, names[operation], perf.args[0], perf.args[1] );
1449 break;
1450 case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY:
1451 perf.args[0] = va_arg( args, int );
1452 perf.args[1] = va_arg( args, int );
1453 perf.args[2] = va_arg( args, int );
1454 TRACE( "hwnd %p %s arg %dx%d %d\n", win->hwnd, names[operation],
1455 perf.args[0], perf.args[1], perf.args[2] );
1456 break;
1457 case NATIVE_WINDOW_SET_CROP:
1459 android_native_rect_t *rect = va_arg( args, android_native_rect_t * );
1460 perf.args[0] = rect->left;
1461 perf.args[1] = rect->top;
1462 perf.args[2] = rect->right;
1463 perf.args[3] = rect->bottom;
1464 TRACE( "hwnd %p %s rect %d,%d-%d,%d\n", win->hwnd, names[operation],
1465 perf.args[0], perf.args[1], perf.args[2], perf.args[3] );
1466 break;
1468 case NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP:
1470 int64_t timestamp = va_arg( args, int64_t );
1471 perf.args[0] = timestamp;
1472 perf.args[1] = timestamp >> 32;
1473 TRACE( "hwnd %p %s arg %08x%08x\n", win->hwnd, names[operation], perf.args[1], perf.args[0] );
1474 break;
1476 case NATIVE_WINDOW_LOCK:
1478 struct ANativeWindowBuffer *buffer;
1479 struct ANativeWindow_Buffer *buffer_ret = va_arg( args, ANativeWindow_Buffer * );
1480 ARect *bounds = va_arg( args, ARect * );
1481 int ret = window->dequeueBuffer_DEPRECATED( window, &buffer );
1482 if (!ret)
1484 if ((ret = gralloc_lock( buffer, &buffer_ret->bits )))
1486 WARN( "gralloc->lock %p failed %d %s\n", win->hwnd, ret, strerror(-ret) );
1487 window->cancelBuffer( window, buffer, -1 );
1490 if (!ret)
1492 buffer_ret->width = buffer->width;
1493 buffer_ret->height = buffer->height;
1494 buffer_ret->stride = buffer->stride;
1495 buffer_ret->format = buffer->format;
1496 win->locked_buffer = buffer;
1497 if (bounds)
1499 bounds->left = 0;
1500 bounds->top = 0;
1501 bounds->right = buffer->width;
1502 bounds->bottom = buffer->height;
1505 va_end( args );
1506 TRACE( "hwnd %p %s bits %p ret %d %s\n", win->hwnd, names[operation], buffer_ret->bits, ret, strerror(-ret) );
1507 return ret;
1509 case NATIVE_WINDOW_UNLOCK_AND_POST:
1511 int ret = -EINVAL;
1512 if (win->locked_buffer)
1514 gralloc_unlock( win->locked_buffer );
1515 ret = window->queueBuffer( window, win->locked_buffer, -1 );
1516 win->locked_buffer = NULL;
1518 va_end( args );
1519 TRACE( "hwnd %p %s ret %d\n", win->hwnd, names[operation], ret );
1520 return ret;
1522 case NATIVE_WINDOW_CONNECT:
1523 case NATIVE_WINDOW_DISCONNECT:
1524 TRACE( "hwnd %p %s\n", win->hwnd, names[operation] );
1525 break;
1526 case NATIVE_WINDOW_SET_POST_TRANSFORM_CROP:
1527 default:
1528 FIXME( "unsupported perform hwnd %p op %d %s\n", win->hwnd, operation,
1529 operation < ARRAY_SIZE( names ) ? names[operation] : "???" );
1530 break;
1532 va_end( args );
1533 return android_ioctl( IOCTL_PERFORM, &perf, sizeof(perf), NULL, NULL );
1536 struct ANativeWindow *create_ioctl_window( HWND hwnd, BOOL opengl, float scale )
1538 struct ioctl_android_create_window req;
1539 struct native_win_wrapper *win = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*win) );
1541 if (!win) return NULL;
1543 win->win.common.magic = ANDROID_NATIVE_WINDOW_MAGIC;
1544 win->win.common.version = sizeof(ANativeWindow);
1545 win->win.common.incRef = win_incRef;
1546 win->win.common.decRef = win_decRef;
1547 win->win.setSwapInterval = setSwapInterval;
1548 win->win.dequeueBuffer_DEPRECATED = dequeueBuffer_DEPRECATED;
1549 win->win.lockBuffer_DEPRECATED = lockBuffer_DEPRECATED;
1550 win->win.queueBuffer_DEPRECATED = queueBuffer_DEPRECATED;
1551 win->win.query = query;
1552 win->win.perform = perform;
1553 win->win.cancelBuffer_DEPRECATED = cancelBuffer_DEPRECATED;
1554 win->win.dequeueBuffer = dequeueBuffer;
1555 win->win.queueBuffer = queueBuffer;
1556 win->win.cancelBuffer = cancelBuffer;
1557 win->ref = 1;
1558 win->hwnd = hwnd;
1559 win->opengl = opengl;
1560 TRACE( "-> %p %p opengl=%u\n", win, win->hwnd, opengl );
1562 req.hdr.hwnd = HandleToLong( win->hwnd );
1563 req.hdr.opengl = win->opengl;
1564 req.parent = get_ioctl_win_parent( GetAncestor( hwnd, GA_PARENT ));
1565 req.scale = scale;
1566 android_ioctl( IOCTL_CREATE_WINDOW, &req, sizeof(req), NULL, NULL );
1568 return &win->win;
1571 struct ANativeWindow *grab_ioctl_window( struct ANativeWindow *window )
1573 struct native_win_wrapper *win = (struct native_win_wrapper *)window;
1574 InterlockedIncrement( &win->ref );
1575 return window;
1578 void release_ioctl_window( struct ANativeWindow *window )
1580 struct native_win_wrapper *win = (struct native_win_wrapper *)window;
1581 unsigned int i;
1583 if (InterlockedDecrement( &win->ref ) > 0) return;
1585 TRACE( "%p %p\n", win, win->hwnd );
1586 for (i = 0; i < ARRAY_SIZE( win->buffers ); i++)
1587 if (win->buffers[i]) win->buffers[i]->buffer.common.decRef( &win->buffers[i]->buffer.common );
1589 destroy_ioctl_window( win->hwnd, win->opengl );
1590 HeapFree( GetProcessHeap(), 0, win );
1593 void destroy_ioctl_window( HWND hwnd, BOOL opengl )
1595 struct ioctl_android_destroy_window req;
1597 req.hdr.hwnd = HandleToLong( hwnd );
1598 req.hdr.opengl = opengl;
1599 android_ioctl( IOCTL_DESTROY_WINDOW, &req, sizeof(req), NULL, NULL );
1602 int ioctl_window_pos_changed( HWND hwnd, const RECT *window_rect, const RECT *client_rect,
1603 const RECT *visible_rect, UINT style, UINT flags, HWND after, HWND owner )
1605 struct ioctl_android_window_pos_changed req;
1607 req.hdr.hwnd = HandleToLong( hwnd );
1608 req.hdr.opengl = FALSE;
1609 req.window_rect = *window_rect;
1610 req.client_rect = *client_rect;
1611 req.visible_rect = *visible_rect;
1612 req.style = style;
1613 req.flags = flags;
1614 req.after = HandleToLong( after );
1615 req.owner = HandleToLong( owner );
1616 return android_ioctl( IOCTL_WINDOW_POS_CHANGED, &req, sizeof(req), NULL, NULL );
1619 int ioctl_set_window_parent( HWND hwnd, HWND parent, float scale )
1621 struct ioctl_android_set_window_parent req;
1623 req.hdr.hwnd = HandleToLong( hwnd );
1624 req.hdr.opengl = FALSE;
1625 req.parent = get_ioctl_win_parent( parent );
1626 req.scale = scale;
1627 return android_ioctl( IOCTL_SET_WINDOW_PARENT, &req, sizeof(req), NULL, NULL );
1630 int ioctl_set_capture( HWND hwnd )
1632 struct ioctl_android_set_capture req;
1634 req.hdr.hwnd = HandleToLong( hwnd );
1635 req.hdr.opengl = FALSE;
1636 return android_ioctl( IOCTL_SET_CAPTURE, &req, sizeof(req), NULL, NULL );
1639 int ioctl_set_cursor( int id, int width, int height,
1640 int hotspotx, int hotspoty, const unsigned int *bits )
1642 struct ioctl_android_set_cursor *req;
1643 unsigned int size = offsetof( struct ioctl_android_set_cursor, bits[width * height] );
1644 int ret;
1646 if (!(req = HeapAlloc( GetProcessHeap(), 0, size ))) return -ENOMEM;
1647 req->hdr.hwnd = 0; /* unused */
1648 req->hdr.opengl = FALSE;
1649 req->id = id;
1650 req->width = width;
1651 req->height = height;
1652 req->hotspotx = hotspotx;
1653 req->hotspoty = hotspoty;
1654 memcpy( req->bits, bits, width * height * sizeof(req->bits[0]) );
1655 ret = android_ioctl( IOCTL_SET_CURSOR, req, size, NULL, NULL );
1656 HeapFree( GetProcessHeap(), 0, req );
1657 return ret;