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