1 // Copyright 2016 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #ifndef ANDROID_INCLUDE_HARDWARE_GOLDFISH_SYNC_H
16 #define ANDROID_INCLUDE_HARDWARE_GOLDFISH_SYNC_H
17
18 #define GOLDFISH_SYNC_VULKAN_SEMAPHORE_SYNC 0x00000001
19
20 #ifdef HOST_BUILD
21
goldfish_sync_open()22 static __inline__ int goldfish_sync_open() {
23 return 0;
24 }
25
goldfish_sync_close(int)26 static __inline__ int goldfish_sync_close(int) {
27 return 0;
28 }
29
goldfish_sync_queue_work(int,uint64_t,uint64_t,int *)30 static __inline__ int goldfish_sync_queue_work(int,
31 uint64_t,
32 uint64_t,
33 int*) {
34 return 0;
35 }
36
goldfish_sync_signal(int goldfish_sync_fd)37 static __inline__ int goldfish_sync_signal(int goldfish_sync_fd) {
38 return 0;
39 }
40
41 #else
42
43 #include <errno.h>
44 #include <linux/ioctl.h>
45 #include <linux/types.h>
46 #include <sys/cdefs.h>
47 #ifdef EMULATOR_OPENGL_POST_O
48 #include <sys/ioctl.h>
49 #include <sys/unistd.h>
50 #endif
51 #include <fcntl.h>
52
53 // Make it conflict with ioctls that are not likely to be used
54 // in the emulator.
55 //
56 // '@' 00-0F linux/radeonfb.h conflict!
57 // '@' 00-0F drivers/video/aty/aty128fb.c conflict!
58 #define GOLDFISH_SYNC_IOC_MAGIC '@'
59
60 struct goldfish_sync_ioctl_info {
61 uint64_t host_glsync_handle_in;
62 uint64_t host_syncthread_handle_in;
63 int fence_fd_out;
64 };
65
66 #define GOLDFISH_SYNC_IOC_QUEUE_WORK _IOWR(GOLDFISH_SYNC_IOC_MAGIC, 0, struct goldfish_sync_ioctl_info)
67 #define GOLDFISH_SYNC_IOC_SIGNAL _IOWR(GOLDFISH_SYNC_IOC_MAGIC, 1, struct goldfish_sync_ioctl_info)
68
goldfish_sync_open()69 static __inline__ int goldfish_sync_open() {
70 return open("/dev/goldfish_sync", O_RDWR);
71 }
72
goldfish_sync_close(int sync_fd)73 static __inline__ int goldfish_sync_close(int sync_fd) {
74 return close(sync_fd);
75 }
76
77 static unsigned int sQueueWorkIoctlCmd = GOLDFISH_SYNC_IOC_QUEUE_WORK;
78
79 // If we are running on a 64-bit kernel.
80 static unsigned int sQueueWorkIoctlCmd64Kernel = 0xc0184000;
81
goldfish_sync_queue_work(int goldfish_sync_fd,uint64_t host_glsync,uint64_t host_thread,int * fd_out)82 static __inline__ int goldfish_sync_queue_work(int goldfish_sync_fd,
83 uint64_t host_glsync,
84 uint64_t host_thread,
85 int* fd_out) {
86
87 struct goldfish_sync_ioctl_info info;
88 int err;
89
90 info.host_glsync_handle_in = host_glsync;
91 info.host_syncthread_handle_in = host_thread;
92 info.fence_fd_out = -1;
93
94 err = ioctl(goldfish_sync_fd, sQueueWorkIoctlCmd, &info);
95
96 if (err < 0 && errno == ENOTTY) {
97 sQueueWorkIoctlCmd = sQueueWorkIoctlCmd64Kernel;
98 err = ioctl(goldfish_sync_fd, sQueueWorkIoctlCmd, &info);
99 if (err < 0) {
100 sQueueWorkIoctlCmd = GOLDFISH_SYNC_IOC_QUEUE_WORK;
101 }
102 }
103
104 if (fd_out) *fd_out = info.fence_fd_out;
105
106 return err;
107 }
108
goldfish_sync_signal(int goldfish_sync_fd)109 static __inline__ int goldfish_sync_signal(int goldfish_sync_fd) {
110 return ioctl(goldfish_sync_fd, GOLDFISH_SYNC_IOC_SIGNAL, 0);
111 }
112
113 #endif // !HOST_BUILD
114
115 #endif
116