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
71
acquire(EGLClientBuffer buffer)72 void ANativeWindowHelperAndroid::acquire(EGLClientBuffer buffer) {
73 #if defined(__ANDROID__)
74 auto* anwb = reinterpret_cast<ANativeWindowBuffer*>(buffer);
75 anwb->incStrong(anwb);
76 #else
77 (void)buffer;
78 #endif // defined(__ANDROID__)
79 }
80
release(EGLClientBuffer buffer)81 void ANativeWindowHelperAndroid::release(EGLClientBuffer buffer) {
82 #if defined(__ANDROID__)
83 auto* anwb = reinterpret_cast<ANativeWindowBuffer*>(buffer);
84 anwb->decStrong(anwb);
85 #else
86 (void)buffer;
87 #endif // defined(__ANDROID__)
88 }
89
getConsumerUsage(EGLNativeWindowType window,int * usage)90 int ANativeWindowHelperAndroid::getConsumerUsage(EGLNativeWindowType window, int* usage) {
91 #if defined(__ANDROID__)
92 auto* anw = reinterpret_cast<ANativeWindow*>(window);
93 return anw->query(anw, NATIVE_WINDOW_CONSUMER_USAGE_BITS, usage);
94 #else
95 (void)window;
96 (void)usage;
97 return -1;
98 #endif // defined(__ANDROID__)
99 }
100
setUsage(EGLNativeWindowType window,int usage)101 void ANativeWindowHelperAndroid::setUsage(EGLNativeWindowType window, int usage) {
102 #if defined(__ANDROID__)
103 auto* anw = reinterpret_cast<ANativeWindow*>(window);
104 ANativeWindow_setUsage(anw, usage);
105 #else
106 (void)window;
107 (void)usage;
108 #endif // defined(__ANDROID__)
109 }
110
getWidth(EGLNativeWindowType window)111 int ANativeWindowHelperAndroid::getWidth(EGLNativeWindowType window) {
112 #if defined(__ANDROID__)
113 auto* anw = reinterpret_cast<ANativeWindow*>(window);
114 return ANativeWindow_getWidth(anw);
115 #else
116 (void)window;
117 return -1;
118 #endif // defined(__ANDROID__)
119 }
120
getHeight(EGLNativeWindowType window)121 int ANativeWindowHelperAndroid::getHeight(EGLNativeWindowType window) {
122 #if defined(__ANDROID__)
123 auto* anw = reinterpret_cast<ANativeWindow*>(window);
124 return ANativeWindow_getHeight(anw);
125 #else
126 (void)window;
127 return -1;
128 #endif // defined(__ANDROID__)
129 }
130
getWidth(EGLClientBuffer buffer)131 int ANativeWindowHelperAndroid::getWidth(EGLClientBuffer buffer) {
132 #if defined(__ANDROID__)
133 auto* anwb = reinterpret_cast<ANativeWindowBuffer*>(buffer);
134 return anwb->width;
135 #else
136 (void)buffer;
137 return -1;
138 #endif // defined(__ANDROID__)
139 }
140
getHeight(EGLClientBuffer buffer)141 int ANativeWindowHelperAndroid::getHeight(EGLClientBuffer buffer) {
142 #if defined(__ANDROID__)
143 auto* anwb = reinterpret_cast<ANativeWindowBuffer*>(buffer);
144 return anwb->height;
145 #else
146 (void)buffer;
147 return -1;
148 #endif // defined(__ANDROID__)
149 }
150
getFormat(EGLClientBuffer buffer,Gralloc * gralloc)151 int ANativeWindowHelperAndroid::getFormat(EGLClientBuffer buffer, Gralloc* gralloc) {
152 #if defined(__ANDROID__)
153 auto* anb = reinterpret_cast<ANativeWindowBuffer*>(buffer);
154 return gralloc->getFormat(anb->handle);
155 #else
156 (void)buffer;
157 (void)gralloc;
158 return -1;
159 #endif // defined(__ANDROID__)
160 }
161
setSwapInterval(EGLNativeWindowType window,int interval)162 void ANativeWindowHelperAndroid::setSwapInterval(EGLNativeWindowType window, int interval) {
163 #if defined(__ANDROID__)
164 auto* anw = reinterpret_cast<ANativeWindow*>(window);
165 anw->setSwapInterval(anw, interval);
166 #else
167 (void)window;
168 (void)interval;
169 #endif // defined(__ANDROID__)
170 }
171
queueBuffer(EGLNativeWindowType window,EGLClientBuffer buffer,int fence)172 int ANativeWindowHelperAndroid::queueBuffer(EGLNativeWindowType window, EGLClientBuffer buffer, 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, int* fence) {
186 #if defined(__ANDROID__)
187 auto* anw = reinterpret_cast<ANativeWindow*>(window);
188 auto* anb = reinterpret_cast<ANativeWindowBuffer**>(buffer);
189 return ANativeWindow_dequeueBuffer(anw, anb, fence);
190 #else
191 (void)window;
192 (void)buffer;
193 (void)fence;
194 return -1;
195 #endif // defined(__ANDROID__)
196 }
197
cancelBuffer(EGLNativeWindowType window,EGLClientBuffer buffer)198 int ANativeWindowHelperAndroid::cancelBuffer(EGLNativeWindowType window, EGLClientBuffer buffer) {
199 #if defined(__ANDROID__)
200 auto* anw = reinterpret_cast<ANativeWindow*>(window);
201 auto* anb = reinterpret_cast<ANativeWindowBuffer*>(buffer);
202 return ANativeWindow_cancelBuffer(anw, anb, -1);
203 #else
204 (void)window;
205 (void)buffer;
206 return -1;
207 #endif // defined(__ANDROID__)
208 }
209
getHostHandle(EGLClientBuffer buffer,Gralloc * gralloc)210 int ANativeWindowHelperAndroid::getHostHandle(EGLClientBuffer buffer, Gralloc* gralloc) {
211 #if defined(__ANDROID__)
212 auto* anb = reinterpret_cast<ANativeWindowBuffer*>(buffer);
213 return gralloc->getHostHandle(anb->handle);
214 #else
215 (void)buffer;
216 (void)gralloc;
217 return -1;
218 #endif // defined(__ANDROID__)
219 }
220
221 } // namespace gfxstream