blob: 6ab4fa6c8d781a6a3047f4fa2521cbe636e2e919 [file] [log] [blame]
Jason Macnak8174c8c2023-07-20 13:06:28 -07001// 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
22namespace gfxstream {
23
24bool 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
34bool 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
53void 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
62void 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
Jason Macnak8174c8c2023-07-20 13:06:28 -070071void 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
80void 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
89int 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
100void 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
110int 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
120int 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
130int 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
140int 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
150int 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
161void 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
Jason Macnakabe57a82024-02-02 17:02:21 -0800171int ANativeWindowHelperAndroid::queueBuffer(EGLNativeWindowType window, EGLClientBuffer buffer,
172 int fence) {
Jason Macnak8174c8c2023-07-20 13:06:28 -0700173#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
Jason Macnakabe57a82024-02-02 17:02:21 -0800185int ANativeWindowHelperAndroid::dequeueBuffer(EGLNativeWindowType window, EGLClientBuffer* buffer,
186 int* fence) {
Jason Macnak8174c8c2023-07-20 13:06:28 -0700187#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
199int 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
211int 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
Jason Macnakabe57a82024-02-02 17:02:21 -0800222ANativeWindowHelper* createPlatformANativeWindowHelper() {
223 return new ANativeWindowHelperAndroid();
224}
225
Jason Macnak8174c8c2023-07-20 13:06:28 -0700226} // namespace gfxstream