1 /*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #define LOG_TAG "ANativeWindow"
18
19 #include <grallocusage/GrallocUsageConversion.h>
20 // from nativewindow/includes/system/window.h
21 // (not to be confused with the compatibility-only window.h from system/core/includes)
22 #include <system/window.h>
23
24 #include <private/android/AHardwareBufferHelpers.h>
25
26 #include <ui/GraphicBuffer.h>
27
28 using namespace android;
29
query(ANativeWindow * window,int what)30 static int32_t query(ANativeWindow* window, int what) {
31 int value;
32 int res = window->query(window, what, &value);
33 return res < 0 ? res : value;
34 }
35
query64(ANativeWindow * window,int what)36 static int64_t query64(ANativeWindow* window, int what) {
37 int64_t value;
38 int res = window->perform(window, what, &value);
39 return res < 0 ? res : value;
40 }
41
isDataSpaceValid(ANativeWindow * window,int32_t dataSpace)42 static bool isDataSpaceValid(ANativeWindow* window, int32_t dataSpace) {
43 bool supported = false;
44 switch (dataSpace) {
45 case HAL_DATASPACE_UNKNOWN:
46 case HAL_DATASPACE_V0_SRGB:
47 return true;
48 // These data space need wide gamut support.
49 case HAL_DATASPACE_V0_SCRGB_LINEAR:
50 case HAL_DATASPACE_V0_SCRGB:
51 case HAL_DATASPACE_DISPLAY_P3:
52 native_window_get_wide_color_support(window, &supported);
53 return supported;
54 // These data space need HDR support.
55 case HAL_DATASPACE_BT2020_PQ:
56 native_window_get_hdr_support(window, &supported);
57 return supported;
58 default:
59 return false;
60 }
61 }
62
63 /**************************************************************************************************
64 * NDK
65 **************************************************************************************************/
66
ANativeWindow_acquire(ANativeWindow * window)67 void ANativeWindow_acquire(ANativeWindow* window) {
68 // incStrong/decStrong token must be the same, doesn't matter what it is
69 window->incStrong((void*)ANativeWindow_acquire);
70 }
71
ANativeWindow_release(ANativeWindow * window)72 void ANativeWindow_release(ANativeWindow* window) {
73 // incStrong/decStrong token must be the same, doesn't matter what it is
74 window->decStrong((void*)ANativeWindow_acquire);
75 }
76
ANativeWindow_getWidth(ANativeWindow * window)77 int32_t ANativeWindow_getWidth(ANativeWindow* window) {
78 return query(window, NATIVE_WINDOW_WIDTH);
79 }
80
ANativeWindow_getHeight(ANativeWindow * window)81 int32_t ANativeWindow_getHeight(ANativeWindow* window) {
82 return query(window, NATIVE_WINDOW_HEIGHT);
83 }
84
ANativeWindow_getFormat(ANativeWindow * window)85 int32_t ANativeWindow_getFormat(ANativeWindow* window) {
86 return query(window, NATIVE_WINDOW_FORMAT);
87 }
88
ANativeWindow_setBuffersGeometry(ANativeWindow * window,int32_t width,int32_t height,int32_t format)89 int32_t ANativeWindow_setBuffersGeometry(ANativeWindow* window,
90 int32_t width, int32_t height, int32_t format) {
91 int32_t err = native_window_set_buffers_format(window, format);
92 if (!err) {
93 err = native_window_set_buffers_user_dimensions(window, width, height);
94 if (!err) {
95 int mode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
96 if (width && height) {
97 mode = NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
98 }
99 err = native_window_set_scaling_mode(window, mode);
100 }
101 }
102 return err;
103 }
104
ANativeWindow_lock(ANativeWindow * window,ANativeWindow_Buffer * outBuffer,ARect * inOutDirtyBounds)105 int32_t ANativeWindow_lock(ANativeWindow* window, ANativeWindow_Buffer* outBuffer,
106 ARect* inOutDirtyBounds) {
107 return window->perform(window, NATIVE_WINDOW_LOCK, outBuffer, inOutDirtyBounds);
108 }
109
ANativeWindow_unlockAndPost(ANativeWindow * window)110 int32_t ANativeWindow_unlockAndPost(ANativeWindow* window) {
111 return window->perform(window, NATIVE_WINDOW_UNLOCK_AND_POST);
112 }
113
ANativeWindow_setBuffersTransform(ANativeWindow * window,int32_t transform)114 int32_t ANativeWindow_setBuffersTransform(ANativeWindow* window, int32_t transform) {
115 static_assert(ANATIVEWINDOW_TRANSFORM_MIRROR_HORIZONTAL == NATIVE_WINDOW_TRANSFORM_FLIP_H);
116 static_assert(ANATIVEWINDOW_TRANSFORM_MIRROR_VERTICAL == NATIVE_WINDOW_TRANSFORM_FLIP_V);
117 static_assert(ANATIVEWINDOW_TRANSFORM_ROTATE_90 == NATIVE_WINDOW_TRANSFORM_ROT_90);
118
119 constexpr int32_t kAllTransformBits =
120 ANATIVEWINDOW_TRANSFORM_MIRROR_HORIZONTAL |
121 ANATIVEWINDOW_TRANSFORM_MIRROR_VERTICAL |
122 ANATIVEWINDOW_TRANSFORM_ROTATE_90 |
123 // We don't expose INVERSE_DISPLAY as an NDK constant, but someone could have read it
124 // from a buffer already set by Camera framework, so we allow it to be forwarded.
125 NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY;
126 if (!window || !query(window, NATIVE_WINDOW_IS_VALID))
127 return -EINVAL;
128 if ((transform & ~kAllTransformBits) != 0)
129 return -EINVAL;
130
131 return native_window_set_buffers_transform(window, transform);
132 }
133
ANativeWindow_setBuffersDataSpace(ANativeWindow * window,int32_t dataSpace)134 int32_t ANativeWindow_setBuffersDataSpace(ANativeWindow* window, int32_t dataSpace) {
135 static_assert(static_cast<int>(ADATASPACE_UNKNOWN) == static_cast<int>(HAL_DATASPACE_UNKNOWN));
136 static_assert(static_cast<int>(STANDARD_MASK) == static_cast<int>(HAL_DATASPACE_STANDARD_MASK));
137 static_assert(static_cast<int>(STANDARD_UNSPECIFIED) == static_cast<int>(HAL_DATASPACE_STANDARD_UNSPECIFIED));
138 static_assert(static_cast<int>(STANDARD_BT709) == static_cast<int>(HAL_DATASPACE_STANDARD_BT709));
139 static_assert(static_cast<int>(STANDARD_BT601_625) == static_cast<int>(HAL_DATASPACE_STANDARD_BT601_625));
140 static_assert(static_cast<int>(STANDARD_BT601_625_UNADJUSTED) == static_cast<int>(HAL_DATASPACE_STANDARD_BT601_625_UNADJUSTED));
141 static_assert(static_cast<int>(STANDARD_BT601_525) == static_cast<int>(HAL_DATASPACE_STANDARD_BT601_525));
142 static_assert(static_cast<int>(STANDARD_BT601_525_UNADJUSTED) == static_cast<int>(HAL_DATASPACE_STANDARD_BT601_525_UNADJUSTED));
143 static_assert(static_cast<int>(STANDARD_BT470M) == static_cast<int>(HAL_DATASPACE_STANDARD_BT470M));
144 static_assert(static_cast<int>(STANDARD_FILM) == static_cast<int>(HAL_DATASPACE_STANDARD_FILM));
145 static_assert(static_cast<int>(STANDARD_DCI_P3) == static_cast<int>(HAL_DATASPACE_STANDARD_DCI_P3));
146 static_assert(static_cast<int>(STANDARD_ADOBE_RGB) == static_cast<int>(HAL_DATASPACE_STANDARD_ADOBE_RGB));
147 static_assert(static_cast<int>(TRANSFER_MASK) == static_cast<int>(HAL_DATASPACE_TRANSFER_MASK));
148 static_assert(static_cast<int>(TRANSFER_UNSPECIFIED) == static_cast<int>(HAL_DATASPACE_TRANSFER_UNSPECIFIED));
149 static_assert(static_cast<int>(TRANSFER_LINEAR) == static_cast<int>(HAL_DATASPACE_TRANSFER_LINEAR));
150 static_assert(static_cast<int>(TRANSFER_SMPTE_170M) == static_cast<int>(HAL_DATASPACE_TRANSFER_SMPTE_170M));
151 static_assert(static_cast<int>(TRANSFER_GAMMA2_2) == static_cast<int>(HAL_DATASPACE_TRANSFER_GAMMA2_2));
152 static_assert(static_cast<int>(TRANSFER_GAMMA2_6) == static_cast<int>(HAL_DATASPACE_TRANSFER_GAMMA2_6));
153 static_assert(static_cast<int>(TRANSFER_GAMMA2_8) == static_cast<int>(HAL_DATASPACE_TRANSFER_GAMMA2_8));
154 static_assert(static_cast<int>(TRANSFER_ST2084) == static_cast<int>(HAL_DATASPACE_TRANSFER_ST2084));
155 static_assert(static_cast<int>(TRANSFER_HLG) == static_cast<int>(HAL_DATASPACE_TRANSFER_HLG));
156 static_assert(static_cast<int>(RANGE_MASK) == static_cast<int>(HAL_DATASPACE_RANGE_MASK));
157 static_assert(static_cast<int>(RANGE_UNSPECIFIED) == static_cast<int>(HAL_DATASPACE_RANGE_UNSPECIFIED));
158 static_assert(static_cast<int>(RANGE_FULL) == static_cast<int>(HAL_DATASPACE_RANGE_FULL));
159 static_assert(static_cast<int>(RANGE_LIMITED) == static_cast<int>(HAL_DATASPACE_RANGE_LIMITED));
160 static_assert(static_cast<int>(RANGE_EXTENDED) == static_cast<int>(HAL_DATASPACE_RANGE_EXTENDED));
161 static_assert(static_cast<int>(ADATASPACE_SRGB) == static_cast<int>(HAL_DATASPACE_V0_SRGB));
162 static_assert(static_cast<int>(ADATASPACE_SCRGB) == static_cast<int>(HAL_DATASPACE_V0_SCRGB));
163 static_assert(static_cast<int>(ADATASPACE_DISPLAY_P3) == static_cast<int>(HAL_DATASPACE_DISPLAY_P3));
164 static_assert(static_cast<int>(ADATASPACE_BT2020_PQ) == static_cast<int>(HAL_DATASPACE_BT2020_PQ));
165 static_assert(static_cast<int>(ADATASPACE_BT2020_ITU_PQ) ==
166 static_cast<int>(HAL_DATASPACE_BT2020_ITU_PQ));
167 static_assert(static_cast<int>(ADATASPACE_ADOBE_RGB) == static_cast<int>(HAL_DATASPACE_ADOBE_RGB));
168 static_assert(static_cast<int>(ADATASPACE_JFIF) == static_cast<int>(HAL_DATASPACE_V0_JFIF));
169 static_assert(static_cast<int>(ADATASPACE_BT601_625) == static_cast<int>(HAL_DATASPACE_V0_BT601_625));
170 static_assert(static_cast<int>(ADATASPACE_BT601_525) == static_cast<int>(HAL_DATASPACE_V0_BT601_525));
171 static_assert(static_cast<int>(ADATASPACE_BT2020) == static_cast<int>(HAL_DATASPACE_BT2020));
172 static_assert(static_cast<int>(ADATASPACE_BT709) == static_cast<int>(HAL_DATASPACE_V0_BT709));
173 static_assert(static_cast<int>(ADATASPACE_DCI_P3) == static_cast<int>(HAL_DATASPACE_DCI_P3));
174 static_assert(static_cast<int>(ADATASPACE_SRGB_LINEAR) == static_cast<int>(HAL_DATASPACE_V0_SRGB_LINEAR));
175 static_assert(static_cast<int>(ADATASPACE_BT2020_HLG) ==
176 static_cast<int>(HAL_DATASPACE_BT2020_HLG));
177 static_assert(static_cast<int>(ADATASPACE_BT2020_ITU_HLG) ==
178 static_cast<int>(HAL_DATASPACE_BT2020_ITU_HLG));
179 static_assert(static_cast<int>(DEPTH) == static_cast<int>(HAL_DATASPACE_DEPTH));
180 static_assert(static_cast<int>(DYNAMIC_DEPTH) == static_cast<int>(HAL_DATASPACE_DYNAMIC_DEPTH));
181
182 if (!window || !query(window, NATIVE_WINDOW_IS_VALID) ||
183 !isDataSpaceValid(window, dataSpace)) {
184 return -EINVAL;
185 }
186 return native_window_set_buffers_data_space(window,
187 static_cast<android_dataspace_t>(dataSpace));
188 }
189
ANativeWindow_getBuffersDataSpace(ANativeWindow * window)190 int32_t ANativeWindow_getBuffersDataSpace(ANativeWindow* window) {
191 if (!window || !query(window, NATIVE_WINDOW_IS_VALID))
192 return -EINVAL;
193 return query(window, NATIVE_WINDOW_DATASPACE);
194 }
195
ANativeWindow_setFrameRate(ANativeWindow * window,float frameRate,int8_t compatibility)196 int32_t ANativeWindow_setFrameRate(ANativeWindow* window, float frameRate, int8_t compatibility) {
197 return ANativeWindow_setFrameRateWithChangeStrategy(window, frameRate, compatibility,
198 ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS);
199 }
200
ANativeWindow_tryAllocateBuffers(ANativeWindow * window)201 void ANativeWindow_tryAllocateBuffers(ANativeWindow* window) {
202 if (!window || !query(window, NATIVE_WINDOW_IS_VALID)) {
203 return;
204 }
205 window->perform(window, NATIVE_WINDOW_ALLOCATE_BUFFERS);
206 }
207
ANativeWindow_setFrameRateWithChangeStrategy(ANativeWindow * window,float frameRate,int8_t compatibility,int8_t changeFrameRateStrategy)208 int32_t ANativeWindow_setFrameRateWithChangeStrategy(ANativeWindow* window, float frameRate,
209 int8_t compatibility, int8_t changeFrameRateStrategy) {
210 if (!window || !query(window, NATIVE_WINDOW_IS_VALID)) {
211 return -EINVAL;
212 }
213 return native_window_set_frame_rate(window, frameRate, compatibility, changeFrameRateStrategy);
214 }
215
216 /**************************************************************************************************
217 * vndk-stable
218 **************************************************************************************************/
219
ANativeWindowBuffer_getHardwareBuffer(ANativeWindowBuffer * anwb)220 AHardwareBuffer* ANativeWindowBuffer_getHardwareBuffer(ANativeWindowBuffer* anwb) {
221 return AHardwareBuffer_from_GraphicBuffer(static_cast<GraphicBuffer*>(anwb));
222 }
223
ANativeWindow_OemStorageSet(ANativeWindow * window,uint32_t slot,intptr_t value)224 int ANativeWindow_OemStorageSet(ANativeWindow* window, uint32_t slot, intptr_t value) {
225 if (slot < 4) {
226 window->oem[slot] = value;
227 return 0;
228 }
229 return -EINVAL;
230 }
231
ANativeWindow_OemStorageGet(ANativeWindow * window,uint32_t slot,intptr_t * value)232 int ANativeWindow_OemStorageGet(ANativeWindow* window, uint32_t slot, intptr_t* value) {
233 if (slot >= 4) {
234 *value = window->oem[slot];
235 return 0;
236 }
237 return -EINVAL;
238 }
239
240
ANativeWindow_setSwapInterval(ANativeWindow * window,int interval)241 int ANativeWindow_setSwapInterval(ANativeWindow* window, int interval) {
242 return window->setSwapInterval(window, interval);
243 }
244
ANativeWindow_query(const ANativeWindow * window,ANativeWindowQuery what,int * value)245 int ANativeWindow_query(const ANativeWindow* window, ANativeWindowQuery what, int* value) {
246 switch (what) {
247 case ANATIVEWINDOW_QUERY_MIN_UNDEQUEUED_BUFFERS:
248 case ANATIVEWINDOW_QUERY_DEFAULT_WIDTH:
249 case ANATIVEWINDOW_QUERY_DEFAULT_HEIGHT:
250 case ANATIVEWINDOW_QUERY_TRANSFORM_HINT:
251 case ANATIVEWINDOW_QUERY_BUFFER_AGE:
252 // these are part of the VNDK API
253 break;
254 case ANATIVEWINDOW_QUERY_MIN_SWAP_INTERVAL:
255 *value = window->minSwapInterval;
256 return 0;
257 case ANATIVEWINDOW_QUERY_MAX_SWAP_INTERVAL:
258 *value = window->maxSwapInterval;
259 return 0;
260 case ANATIVEWINDOW_QUERY_XDPI:
261 *value = (int)window->xdpi;
262 return 0;
263 case ANATIVEWINDOW_QUERY_YDPI:
264 *value = (int)window->ydpi;
265 return 0;
266 default:
267 // asked for an invalid query(), one that isn't part of the VNDK
268 return -EINVAL;
269 }
270 return window->query(window, int(what), value);
271 }
272
ANativeWindow_queryf(const ANativeWindow * window,ANativeWindowQuery what,float * value)273 int ANativeWindow_queryf(const ANativeWindow* window, ANativeWindowQuery what, float* value) {
274 switch (what) {
275 case ANATIVEWINDOW_QUERY_XDPI:
276 *value = window->xdpi;
277 return 0;
278 case ANATIVEWINDOW_QUERY_YDPI:
279 *value = window->ydpi;
280 return 0;
281 default:
282 break;
283 }
284
285 int i;
286 int e = ANativeWindow_query(window, what, &i);
287 if (e == 0) {
288 *value = (float)i;
289 }
290 return e;
291 }
292
ANativeWindow_dequeueBuffer(ANativeWindow * window,ANativeWindowBuffer ** buffer,int * fenceFd)293 int ANativeWindow_dequeueBuffer(ANativeWindow* window, ANativeWindowBuffer** buffer, int* fenceFd) {
294 return window->dequeueBuffer(window, buffer, fenceFd);
295 }
296
ANativeWindow_queueBuffer(ANativeWindow * window,ANativeWindowBuffer * buffer,int fenceFd)297 int ANativeWindow_queueBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer, int fenceFd) {
298 return window->queueBuffer(window, buffer, fenceFd);
299 }
300
ANativeWindow_cancelBuffer(ANativeWindow * window,ANativeWindowBuffer * buffer,int fenceFd)301 int ANativeWindow_cancelBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer, int fenceFd) {
302 return window->cancelBuffer(window, buffer, fenceFd);
303 }
304
ANativeWindow_setUsage(ANativeWindow * window,uint64_t usage)305 int ANativeWindow_setUsage(ANativeWindow* window, uint64_t usage) {
306 return native_window_set_usage(window, usage);
307 }
308
ANativeWindow_setBufferCount(ANativeWindow * window,size_t bufferCount)309 int ANativeWindow_setBufferCount(ANativeWindow* window, size_t bufferCount) {
310 return native_window_set_buffer_count(window, bufferCount);
311 }
312
ANativeWindow_setBuffersDimensions(ANativeWindow * window,uint32_t w,uint32_t h)313 int ANativeWindow_setBuffersDimensions(ANativeWindow* window, uint32_t w, uint32_t h) {
314 return native_window_set_buffers_dimensions(window, (int)w, (int)h);
315 }
316
ANativeWindow_setBuffersFormat(ANativeWindow * window,int format)317 int ANativeWindow_setBuffersFormat(ANativeWindow* window, int format) {
318 return native_window_set_buffers_format(window, format);
319 }
320
ANativeWindow_setBuffersTimestamp(ANativeWindow * window,int64_t timestamp)321 int ANativeWindow_setBuffersTimestamp(ANativeWindow* window, int64_t timestamp) {
322 return native_window_set_buffers_timestamp(window, timestamp);
323 }
324
ANativeWindow_setSharedBufferMode(ANativeWindow * window,bool sharedBufferMode)325 int ANativeWindow_setSharedBufferMode(ANativeWindow* window, bool sharedBufferMode) {
326 return native_window_set_shared_buffer_mode(window, sharedBufferMode);
327 }
328
ANativeWindow_setAutoRefresh(ANativeWindow * window,bool autoRefresh)329 int ANativeWindow_setAutoRefresh(ANativeWindow* window, bool autoRefresh) {
330 return native_window_set_auto_refresh(window, autoRefresh);
331 }
332
ANativeWindow_setAutoPrerotation(ANativeWindow * window,bool autoPrerotation)333 int ANativeWindow_setAutoPrerotation(ANativeWindow* window, bool autoPrerotation) {
334 return native_window_set_auto_prerotation(window, autoPrerotation);
335 }
336
337 /**************************************************************************************************
338 * apex-stable
339 **************************************************************************************************/
340
ANativeWindow_getLastDequeueDuration(ANativeWindow * window)341 int64_t ANativeWindow_getLastDequeueDuration(ANativeWindow* window) {
342 return query64(window, NATIVE_WINDOW_GET_LAST_DEQUEUE_DURATION);
343 }
344
ANativeWindow_getLastQueueDuration(ANativeWindow * window)345 int64_t ANativeWindow_getLastQueueDuration(ANativeWindow* window) {
346 return query64(window, NATIVE_WINDOW_GET_LAST_QUEUE_DURATION);
347 }
348
ANativeWindow_getLastDequeueStartTime(ANativeWindow * window)349 int64_t ANativeWindow_getLastDequeueStartTime(ANativeWindow* window) {
350 return query64(window, NATIVE_WINDOW_GET_LAST_DEQUEUE_START);
351 }
352
ANativeWindow_setDequeueTimeout(ANativeWindow * window,int64_t timeout)353 int ANativeWindow_setDequeueTimeout(ANativeWindow* window, int64_t timeout) {
354 return window->perform(window, NATIVE_WINDOW_SET_DEQUEUE_TIMEOUT, timeout);
355 }
356
ANativeWindow_setCancelBufferInterceptor(ANativeWindow * window,ANativeWindow_cancelBufferInterceptor interceptor,void * data)357 int ANativeWindow_setCancelBufferInterceptor(ANativeWindow* window,
358 ANativeWindow_cancelBufferInterceptor interceptor,
359 void* data) {
360 return window->perform(window, NATIVE_WINDOW_SET_CANCEL_INTERCEPTOR, interceptor, data);
361 }
362
ANativeWindow_setDequeueBufferInterceptor(ANativeWindow * window,ANativeWindow_dequeueBufferInterceptor interceptor,void * data)363 int ANativeWindow_setDequeueBufferInterceptor(ANativeWindow* window,
364 ANativeWindow_dequeueBufferInterceptor interceptor,
365 void* data) {
366 return window->perform(window, NATIVE_WINDOW_SET_DEQUEUE_INTERCEPTOR, interceptor, data);
367 }
368
ANativeWindow_setPerformInterceptor(ANativeWindow * window,ANativeWindow_performInterceptor interceptor,void * data)369 int ANativeWindow_setPerformInterceptor(ANativeWindow* window,
370 ANativeWindow_performInterceptor interceptor, void* data) {
371 return window->perform(window, NATIVE_WINDOW_SET_PERFORM_INTERCEPTOR, interceptor, data);
372 }
373
ANativeWindow_setQueueBufferInterceptor(ANativeWindow * window,ANativeWindow_queueBufferInterceptor interceptor,void * data)374 int ANativeWindow_setQueueBufferInterceptor(ANativeWindow* window,
375 ANativeWindow_queueBufferInterceptor interceptor,
376 void* data) {
377 return window->perform(window, NATIVE_WINDOW_SET_QUEUE_INTERCEPTOR, interceptor, data);
378 }
379