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