blob: 988d2a08767e07de3360bdb9b75e6eca1aa520e3 [file] [log] [blame]
Mathias Agopian89ed4c82017-02-09 18:48:34 -08001/*
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
Jesse Hall79927812017-03-23 11:03:23 -070019#include <grallocusage/GrallocUsageConversion.h>
Mathias Agopian000879a2017-03-20 18:07:26 -070020// from nativewindow/includes/system/window.h
21// (not to be confused with the compatibility-only window.h from system/core/includes)
Mathias Agopian89ed4c82017-02-09 18:48:34 -080022#include <system/window.h>
23
Mathias Agopian000879a2017-03-20 18:07:26 -070024#include <private/android/AHardwareBufferHelpers.h>
Mathias Agopian89ed4c82017-02-09 18:48:34 -080025
Mathias Agopian453effd2017-04-03 15:34:13 -070026#include <ui/GraphicBuffer.h>
27
Mathias Agopian000879a2017-03-20 18:07:26 -070028using namespace android;
Mathias Agopian89ed4c82017-02-09 18:48:34 -080029
Mathias Agopian000879a2017-03-20 18:07:26 -070030static int32_t query(ANativeWindow* window, int what) {
Mathias Agopian89ed4c82017-02-09 18:48:34 -080031 int value;
32 int res = window->query(window, what, &value);
33 return res < 0 ? res : value;
34}
35
Alec Mouri72670c52019-08-31 01:54:33 -070036static 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
Peiyong Lin654f87b2018-01-30 14:21:33 -080042static 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
Mathias Agopian000879a2017-03-20 18:07:26 -070063/**************************************************************************************************
64 * NDK
65 **************************************************************************************************/
66
67void 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
72void 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
Mathias Agopian89ed4c82017-02-09 18:48:34 -080077int32_t ANativeWindow_getWidth(ANativeWindow* window) {
Mathias Agopian000879a2017-03-20 18:07:26 -070078 return query(window, NATIVE_WINDOW_WIDTH);
Mathias Agopian89ed4c82017-02-09 18:48:34 -080079}
80
81int32_t ANativeWindow_getHeight(ANativeWindow* window) {
Mathias Agopian000879a2017-03-20 18:07:26 -070082 return query(window, NATIVE_WINDOW_HEIGHT);
Mathias Agopian89ed4c82017-02-09 18:48:34 -080083}
84
85int32_t ANativeWindow_getFormat(ANativeWindow* window) {
Mathias Agopian000879a2017-03-20 18:07:26 -070086 return query(window, NATIVE_WINDOW_FORMAT);
Mathias Agopian89ed4c82017-02-09 18:48:34 -080087}
88
Mathias Agopian000879a2017-03-20 18:07:26 -070089int32_t ANativeWindow_setBuffersGeometry(ANativeWindow* window,
90 int32_t width, int32_t height, int32_t format) {
Mathias Agopian89ed4c82017-02-09 18:48:34 -080091 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
105int32_t ANativeWindow_lock(ANativeWindow* window, ANativeWindow_Buffer* outBuffer,
106 ARect* inOutDirtyBounds) {
107 return window->perform(window, NATIVE_WINDOW_LOCK, outBuffer, inOutDirtyBounds);
108}
109
110int32_t ANativeWindow_unlockAndPost(ANativeWindow* window) {
111 return window->perform(window, NATIVE_WINDOW_UNLOCK_AND_POST);
112}
Jesse Hall09932ec2017-03-13 11:36:05 -0700113
114int32_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 |
Robert Carr175ed5b2018-04-06 13:59:00 -0700122 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;
Mathias Agopian000879a2017-03-20 18:07:26 -0700126 if (!window || !query(window, NATIVE_WINDOW_IS_VALID))
Jesse Hall09932ec2017-03-13 11:36:05 -0700127 return -EINVAL;
128 if ((transform & ~kAllTransformBits) != 0)
129 return -EINVAL;
130
131 return native_window_set_buffers_transform(window, transform);
132}
Mathias Agopian000879a2017-03-20 18:07:26 -0700133
Peiyong Lin654f87b2018-01-30 14:21:33 -0800134int32_t ANativeWindow_setBuffersDataSpace(ANativeWindow* window, int32_t dataSpace) {
Yi Kong2fe2a082018-05-31 11:47:00 -0700135 static_assert(static_cast<int>(ADATASPACE_UNKNOWN) == static_cast<int>(HAL_DATASPACE_UNKNOWN));
Sally Qic4e8a2e2021-09-27 13:11:35 -0700136 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));
Sally Qic4e8a2e2021-09-27 13:11:35 -0700147 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));
Yi Kong2fe2a082018-05-31 11:47:00 -0700161 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));
Peiyong Lin91a2b3d2019-12-12 21:33:11 -0800165 static_assert(static_cast<int>(ADATASPACE_ADOBE_RGB) == static_cast<int>(HAL_DATASPACE_ADOBE_RGB));
Sally Qic4e8a2e2021-09-27 13:11:35 -0700166 static_assert(static_cast<int>(ADATASPACE_JFIF) == static_cast<int>(HAL_DATASPACE_V0_JFIF));
167 static_assert(static_cast<int>(ADATASPACE_BT601_625) == static_cast<int>(HAL_DATASPACE_V0_BT601_625));
168 static_assert(static_cast<int>(ADATASPACE_BT601_525) == static_cast<int>(HAL_DATASPACE_V0_BT601_525));
Peiyong Lin91a2b3d2019-12-12 21:33:11 -0800169 static_assert(static_cast<int>(ADATASPACE_BT2020) == static_cast<int>(HAL_DATASPACE_BT2020));
170 static_assert(static_cast<int>(ADATASPACE_BT709) == static_cast<int>(HAL_DATASPACE_V0_BT709));
171 static_assert(static_cast<int>(ADATASPACE_DCI_P3) == static_cast<int>(HAL_DATASPACE_DCI_P3));
172 static_assert(static_cast<int>(ADATASPACE_SRGB_LINEAR) == static_cast<int>(HAL_DATASPACE_V0_SRGB_LINEAR));
Sally Qi95770602021-11-15 11:16:26 -0800173 static_assert(static_cast<int>(ADATASPACE_BT2020_HLG) ==
174 static_cast<int>(HAL_DATASPACE_BT2020_HLG));
175 static_assert(static_cast<int>(ADATASPACE_BT2020_ITU_HLG) ==
176 static_cast<int>(HAL_DATASPACE_BT2020_ITU_HLG));
Peiyong Lin654f87b2018-01-30 14:21:33 -0800177
178 if (!window || !query(window, NATIVE_WINDOW_IS_VALID) ||
179 !isDataSpaceValid(window, dataSpace)) {
180 return -EINVAL;
181 }
182 return native_window_set_buffers_data_space(window,
183 static_cast<android_dataspace_t>(dataSpace));
184}
185
186int32_t ANativeWindow_getBuffersDataSpace(ANativeWindow* window) {
187 if (!window || !query(window, NATIVE_WINDOW_IS_VALID))
188 return -EINVAL;
189 return query(window, NATIVE_WINDOW_DATASPACE);
190}
191
Steven Thomas62a4cf82020-01-31 12:04:03 -0800192int32_t ANativeWindow_setFrameRate(ANativeWindow* window, float frameRate, int8_t compatibility) {
Marin Shalamanovc5986772021-03-16 16:09:49 +0100193 return ANativeWindow_setFrameRateWithChangeStrategy(window, frameRate, compatibility,
194 ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS);
Steven Thomas6d88a482019-12-02 22:00:47 -0800195}
196
Alec Mourid9d85722020-02-13 13:57:19 -0800197void ANativeWindow_tryAllocateBuffers(ANativeWindow* window) {
198 if (!window || !query(window, NATIVE_WINDOW_IS_VALID)) {
199 return;
200 }
201 window->perform(window, NATIVE_WINDOW_ALLOCATE_BUFFERS);
202}
203
Marin Shalamanovc5986772021-03-16 16:09:49 +0100204int32_t ANativeWindow_setFrameRateWithChangeStrategy(ANativeWindow* window, float frameRate,
205 int8_t compatibility, int8_t changeFrameRateStrategy) {
Marin Shalamanov46084422020-10-13 12:33:42 +0200206 if (!window || !query(window, NATIVE_WINDOW_IS_VALID)) {
207 return -EINVAL;
208 }
Marin Shalamanovc5986772021-03-16 16:09:49 +0100209 return native_window_set_frame_rate(window, frameRate, compatibility, changeFrameRateStrategy);
Marin Shalamanov46084422020-10-13 12:33:42 +0200210}
Alec Mourid9d85722020-02-13 13:57:19 -0800211
Mathias Agopian000879a2017-03-20 18:07:26 -0700212/**************************************************************************************************
213 * vndk-stable
214 **************************************************************************************************/
215
Mathias Agopian453effd2017-04-03 15:34:13 -0700216AHardwareBuffer* ANativeWindowBuffer_getHardwareBuffer(ANativeWindowBuffer* anwb) {
217 return AHardwareBuffer_from_GraphicBuffer(static_cast<GraphicBuffer*>(anwb));
218}
219
Mathias Agopian000879a2017-03-20 18:07:26 -0700220int ANativeWindow_OemStorageSet(ANativeWindow* window, uint32_t slot, intptr_t value) {
221 if (slot < 4) {
222 window->oem[slot] = value;
223 return 0;
224 }
225 return -EINVAL;
226}
227
228int ANativeWindow_OemStorageGet(ANativeWindow* window, uint32_t slot, intptr_t* value) {
229 if (slot >= 4) {
230 *value = window->oem[slot];
231 return 0;
232 }
233 return -EINVAL;
234}
235
236
237int ANativeWindow_setSwapInterval(ANativeWindow* window, int interval) {
238 return window->setSwapInterval(window, interval);
239}
240
241int ANativeWindow_query(const ANativeWindow* window, ANativeWindowQuery what, int* value) {
242 switch (what) {
243 case ANATIVEWINDOW_QUERY_MIN_UNDEQUEUED_BUFFERS:
244 case ANATIVEWINDOW_QUERY_DEFAULT_WIDTH:
245 case ANATIVEWINDOW_QUERY_DEFAULT_HEIGHT:
246 case ANATIVEWINDOW_QUERY_TRANSFORM_HINT:
Peiyong Lin90a90f12021-06-03 18:11:56 +0000247 case ANATIVEWINDOW_QUERY_BUFFER_AGE:
Mathias Agopian000879a2017-03-20 18:07:26 -0700248 // these are part of the VNDK API
249 break;
250 case ANATIVEWINDOW_QUERY_MIN_SWAP_INTERVAL:
251 *value = window->minSwapInterval;
252 return 0;
253 case ANATIVEWINDOW_QUERY_MAX_SWAP_INTERVAL:
254 *value = window->maxSwapInterval;
255 return 0;
256 case ANATIVEWINDOW_QUERY_XDPI:
257 *value = (int)window->xdpi;
258 return 0;
259 case ANATIVEWINDOW_QUERY_YDPI:
260 *value = (int)window->ydpi;
261 return 0;
262 default:
263 // asked for an invalid query(), one that isn't part of the VNDK
264 return -EINVAL;
265 }
266 return window->query(window, int(what), value);
267}
268
269int ANativeWindow_queryf(const ANativeWindow* window, ANativeWindowQuery what, float* value) {
270 switch (what) {
271 case ANATIVEWINDOW_QUERY_XDPI:
272 *value = window->xdpi;
273 return 0;
274 case ANATIVEWINDOW_QUERY_YDPI:
275 *value = window->ydpi;
276 return 0;
277 default:
278 break;
279 }
280
281 int i;
282 int e = ANativeWindow_query(window, what, &i);
283 if (e == 0) {
284 *value = (float)i;
285 }
286 return e;
287}
288
289int ANativeWindow_dequeueBuffer(ANativeWindow* window, ANativeWindowBuffer** buffer, int* fenceFd) {
290 return window->dequeueBuffer(window, buffer, fenceFd);
291}
292
293int ANativeWindow_queueBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer, int fenceFd) {
294 return window->queueBuffer(window, buffer, fenceFd);
295}
296
297int ANativeWindow_cancelBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer, int fenceFd) {
298 return window->cancelBuffer(window, buffer, fenceFd);
299}
300
Mathias Agopian2c38b562017-04-20 16:35:39 -0700301int ANativeWindow_setUsage(ANativeWindow* window, uint64_t usage) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700302 return native_window_set_usage(window, usage);
Mathias Agopian000879a2017-03-20 18:07:26 -0700303}
304
305int ANativeWindow_setBufferCount(ANativeWindow* window, size_t bufferCount) {
306 return native_window_set_buffer_count(window, bufferCount);
307}
308
309int ANativeWindow_setBuffersDimensions(ANativeWindow* window, uint32_t w, uint32_t h) {
310 return native_window_set_buffers_dimensions(window, (int)w, (int)h);
311}
312
313int ANativeWindow_setBuffersFormat(ANativeWindow* window, int format) {
314 return native_window_set_buffers_format(window, format);
315}
316
317int ANativeWindow_setBuffersTimestamp(ANativeWindow* window, int64_t timestamp) {
318 return native_window_set_buffers_timestamp(window, timestamp);
319}
320
Mathias Agopian000879a2017-03-20 18:07:26 -0700321int ANativeWindow_setSharedBufferMode(ANativeWindow* window, bool sharedBufferMode) {
322 return native_window_set_shared_buffer_mode(window, sharedBufferMode);
323}
324
325int ANativeWindow_setAutoRefresh(ANativeWindow* window, bool autoRefresh) {
326 return native_window_set_auto_refresh(window, autoRefresh);
327}
Yiwei Zhang538cedc2019-06-24 19:35:03 -0700328
329int ANativeWindow_setAutoPrerotation(ANativeWindow* window, bool autoPrerotation) {
330 return native_window_set_auto_prerotation(window, autoPrerotation);
331}
Alec Mouri9fa2cb62019-07-15 17:36:26 -0700332
333/**************************************************************************************************
334 * apex-stable
335 **************************************************************************************************/
336
Alec Mouri72670c52019-08-31 01:54:33 -0700337int64_t ANativeWindow_getLastDequeueDuration(ANativeWindow* window) {
338 return query64(window, NATIVE_WINDOW_GET_LAST_DEQUEUE_DURATION);
Alec Mouri9fa2cb62019-07-15 17:36:26 -0700339}
Alec Mouri0d1398b2019-08-14 10:53:50 -0700340
Alec Mouri72670c52019-08-31 01:54:33 -0700341int64_t ANativeWindow_getLastQueueDuration(ANativeWindow* window) {
342 return query64(window, NATIVE_WINDOW_GET_LAST_QUEUE_DURATION);
Alec Mouri0d1398b2019-08-14 10:53:50 -0700343}
Alec Mouria1619662019-08-21 19:30:48 -0700344
345int64_t ANativeWindow_getLastDequeueStartTime(ANativeWindow* window) {
Alec Mouri72670c52019-08-31 01:54:33 -0700346 return query64(window, NATIVE_WINDOW_GET_LAST_DEQUEUE_START);
Alec Mouria1619662019-08-21 19:30:48 -0700347}
Alec Mouri04fdb602019-08-23 19:41:43 -0700348
349int ANativeWindow_setDequeueTimeout(ANativeWindow* window, int64_t timeout) {
350 return window->perform(window, NATIVE_WINDOW_SET_DEQUEUE_TIMEOUT, timeout);
351}
Alec Mouri09d122a2019-11-25 10:00:53 -0800352
353int ANativeWindow_setCancelBufferInterceptor(ANativeWindow* window,
354 ANativeWindow_cancelBufferInterceptor interceptor,
355 void* data) {
356 return window->perform(window, NATIVE_WINDOW_SET_CANCEL_INTERCEPTOR, interceptor, data);
357}
358
359int ANativeWindow_setDequeueBufferInterceptor(ANativeWindow* window,
360 ANativeWindow_dequeueBufferInterceptor interceptor,
361 void* data) {
362 return window->perform(window, NATIVE_WINDOW_SET_DEQUEUE_INTERCEPTOR, interceptor, data);
363}
364
365int ANativeWindow_setPerformInterceptor(ANativeWindow* window,
366 ANativeWindow_performInterceptor interceptor, void* data) {
367 return window->perform(window, NATIVE_WINDOW_SET_PERFORM_INTERCEPTOR, interceptor, data);
368}
369
370int ANativeWindow_setQueueBufferInterceptor(ANativeWindow* window,
371 ANativeWindow_queueBufferInterceptor interceptor,
372 void* data) {
373 return window->perform(window, NATIVE_WINDOW_SET_QUEUE_INTERCEPTOR, interceptor, data);
374}