1 /*
2 * Copyright 2023 Google LLC
3 * SPDX-License-Identifier: MIT
4 */
5
6 #include "ANativeWindowAndroid.h"
7 #include "util/detect_os.h"
8
9 #if DETECT_OS_ANDROID
10 #include <android/native_window.h>
11 #include <system/window.h>
12 #endif // DETECT_OS_ANDROID
13
14 namespace gfxstream {
15
isValid(EGLNativeWindowType window)16 bool ANativeWindowHelperAndroid::isValid(EGLNativeWindowType window) {
17 #if DETECT_OS_ANDROID
18 auto* anw = reinterpret_cast<ANativeWindow*>(window);
19 return anw->common.magic == ANDROID_NATIVE_WINDOW_MAGIC;
20 #else
21 (void)window;
22 return false;
23 #endif // DETECT_OS_ANDROID
24 }
25
isValid(EGLClientBuffer buffer)26 bool ANativeWindowHelperAndroid::isValid(EGLClientBuffer buffer) {
27 #if DETECT_OS_ANDROID
28 auto* anwb = reinterpret_cast<ANativeWindowBuffer*>(buffer);
29 if (anwb->common.magic != ANDROID_NATIVE_BUFFER_MAGIC) {
30 return false;
31 }
32 if (anwb->common.version != sizeof(android_native_buffer_t)) {
33 return false;
34 }
35 if (anwb->handle == nullptr) {
36 return false;
37 }
38 return true;
39 #else
40 (void)buffer;
41 return false;
42 #endif // DETECT_OS_ANDROID
43 }
44
acquire(EGLNativeWindowType window)45 void ANativeWindowHelperAndroid::acquire(EGLNativeWindowType window) {
46 #if DETECT_OS_ANDROID
47 auto* anw = reinterpret_cast<ANativeWindow*>(window);
48 ANativeWindow_acquire(anw);
49 #else
50 (void)window;
51 #endif // DETECT_OS_ANDROID
52 }
53
release(EGLNativeWindowType window)54 void ANativeWindowHelperAndroid::release(EGLNativeWindowType window) {
55 #if DETECT_OS_ANDROID
56 auto* anw = reinterpret_cast<ANativeWindow*>(window);
57 ANativeWindow_release(anw);
58 #else
59 (void)window;
60 #endif // DETECT_OS_ANDROID
61 }
62
acquire(EGLClientBuffer buffer)63 void ANativeWindowHelperAndroid::acquire(EGLClientBuffer buffer) {
64 #if DETECT_OS_ANDROID
65 auto* anwb = reinterpret_cast<ANativeWindowBuffer*>(buffer);
66 anwb->incStrong(anwb);
67 #else
68 (void)buffer;
69 #endif // DETECT_OS_ANDROID
70 }
71
release(EGLClientBuffer buffer)72 void ANativeWindowHelperAndroid::release(EGLClientBuffer buffer) {
73 #if DETECT_OS_ANDROID
74 auto* anwb = reinterpret_cast<ANativeWindowBuffer*>(buffer);
75 anwb->decStrong(anwb);
76 #else
77 (void)buffer;
78 #endif // DETECT_OS_ANDROID
79 }
80
getConsumerUsage(EGLNativeWindowType window,int * usage)81 int ANativeWindowHelperAndroid::getConsumerUsage(EGLNativeWindowType window, int* usage) {
82 #if DETECT_OS_ANDROID
83 auto* anw = reinterpret_cast<ANativeWindow*>(window);
84 return anw->query(anw, NATIVE_WINDOW_CONSUMER_USAGE_BITS, usage);
85 #else
86 (void)window;
87 (void)usage;
88 return -1;
89 #endif // DETECT_OS_ANDROID
90 }
91
setUsage(EGLNativeWindowType window,int usage)92 void ANativeWindowHelperAndroid::setUsage(EGLNativeWindowType window, int usage) {
93 #if DETECT_OS_ANDROID
94 auto* anw = reinterpret_cast<ANativeWindow*>(window);
95 ANativeWindow_setUsage(anw, usage);
96 #else
97 (void)window;
98 (void)usage;
99 #endif // DETECT_OS_ANDROID
100 }
101
getWidth(EGLNativeWindowType window)102 int ANativeWindowHelperAndroid::getWidth(EGLNativeWindowType window) {
103 #if DETECT_OS_ANDROID
104 auto* anw = reinterpret_cast<ANativeWindow*>(window);
105 return ANativeWindow_getWidth(anw);
106 #else
107 (void)window;
108 return -1;
109 #endif // DETECT_OS_ANDROID
110 }
111
getHeight(EGLNativeWindowType window)112 int ANativeWindowHelperAndroid::getHeight(EGLNativeWindowType window) {
113 #if DETECT_OS_ANDROID
114 auto* anw = reinterpret_cast<ANativeWindow*>(window);
115 return ANativeWindow_getHeight(anw);
116 #else
117 (void)window;
118 return -1;
119 #endif // DETECT_OS_ANDROID
120 }
121
getWidth(EGLClientBuffer buffer)122 int ANativeWindowHelperAndroid::getWidth(EGLClientBuffer buffer) {
123 #if DETECT_OS_ANDROID
124 auto* anwb = reinterpret_cast<ANativeWindowBuffer*>(buffer);
125 return anwb->width;
126 #else
127 (void)buffer;
128 return -1;
129 #endif // DETECT_OS_ANDROID
130 }
131
getHeight(EGLClientBuffer buffer)132 int ANativeWindowHelperAndroid::getHeight(EGLClientBuffer buffer) {
133 #if DETECT_OS_ANDROID
134 auto* anwb = reinterpret_cast<ANativeWindowBuffer*>(buffer);
135 return anwb->height;
136 #else
137 (void)buffer;
138 return -1;
139 #endif // DETECT_OS_ANDROID
140 }
141
getFormat(EGLClientBuffer buffer,Gralloc * gralloc)142 int ANativeWindowHelperAndroid::getFormat(EGLClientBuffer buffer, Gralloc* gralloc) {
143 #if DETECT_OS_ANDROID
144 auto* anb = reinterpret_cast<ANativeWindowBuffer*>(buffer);
145 return gralloc->getFormat(anb->handle);
146 #else
147 (void)buffer;
148 (void)gralloc;
149 return -1;
150 #endif // DETECT_OS_ANDROID
151 }
152
setSwapInterval(EGLNativeWindowType window,int interval)153 void ANativeWindowHelperAndroid::setSwapInterval(EGLNativeWindowType window, int interval) {
154 #if DETECT_OS_ANDROID
155 auto* anw = reinterpret_cast<ANativeWindow*>(window);
156 anw->setSwapInterval(anw, interval);
157 #else
158 (void)window;
159 (void)interval;
160 #endif // DETECT_OS_ANDROID
161 }
162
queueBuffer(EGLNativeWindowType window,EGLClientBuffer buffer,int fence)163 int ANativeWindowHelperAndroid::queueBuffer(EGLNativeWindowType window, EGLClientBuffer buffer,
164 int fence) {
165 #if DETECT_OS_ANDROID
166 auto* anw = reinterpret_cast<ANativeWindow*>(window);
167 auto* anb = reinterpret_cast<ANativeWindowBuffer*>(buffer);
168 return ANativeWindow_queueBuffer(anw, anb, fence);
169 #else
170 (void)window;
171 (void)buffer;
172 (void)fence;
173 return -1;
174 #endif // DETECT_OS_ANDROID
175 }
176
dequeueBuffer(EGLNativeWindowType window,EGLClientBuffer * buffer,int * fence)177 int ANativeWindowHelperAndroid::dequeueBuffer(EGLNativeWindowType window, EGLClientBuffer* buffer,
178 int* fence) {
179 #if DETECT_OS_ANDROID
180 auto* anw = reinterpret_cast<ANativeWindow*>(window);
181 auto* anb = reinterpret_cast<ANativeWindowBuffer**>(buffer);
182 return ANativeWindow_dequeueBuffer(anw, anb, fence);
183 #else
184 (void)window;
185 (void)buffer;
186 (void)fence;
187 return -1;
188 #endif // DETECT_OS_ANDROID
189 }
190
cancelBuffer(EGLNativeWindowType window,EGLClientBuffer buffer)191 int ANativeWindowHelperAndroid::cancelBuffer(EGLNativeWindowType window, EGLClientBuffer buffer) {
192 #if DETECT_OS_ANDROID
193 auto* anw = reinterpret_cast<ANativeWindow*>(window);
194 auto* anb = reinterpret_cast<ANativeWindowBuffer*>(buffer);
195 return ANativeWindow_cancelBuffer(anw, anb, -1);
196 #else
197 (void)window;
198 (void)buffer;
199 return -1;
200 #endif // DETECT_OS_ANDROID
201 }
202
getHostHandle(EGLClientBuffer buffer,Gralloc * gralloc)203 int ANativeWindowHelperAndroid::getHostHandle(EGLClientBuffer buffer, Gralloc* gralloc) {
204 #if DETECT_OS_ANDROID
205 auto* anb = reinterpret_cast<ANativeWindowBuffer*>(buffer);
206 return gralloc->getHostHandle(anb->handle);
207 #else
208 (void)buffer;
209 (void)gralloc;
210 return -1;
211 #endif // DETECT_OS_ANDROID
212 }
213
createPlatformANativeWindowHelper()214 ANativeWindowHelper* createPlatformANativeWindowHelper() {
215 return new ANativeWindowHelperAndroid();
216 }
217
218 } // namespace gfxstream
219