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