blob: 5c91d587bc0efe7b0c7f87d62ff75dd3a85cdf70 [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
Peiyong Lin654f87b2018-01-30 14:21:33 -080036static bool isDataSpaceValid(ANativeWindow* window, int32_t dataSpace) {
37 bool supported = false;
38 switch (dataSpace) {
39 case HAL_DATASPACE_UNKNOWN:
40 case HAL_DATASPACE_V0_SRGB:
41 return true;
42 // These data space need wide gamut support.
43 case HAL_DATASPACE_V0_SCRGB_LINEAR:
44 case HAL_DATASPACE_V0_SCRGB:
45 case HAL_DATASPACE_DISPLAY_P3:
46 native_window_get_wide_color_support(window, &supported);
47 return supported;
48 // These data space need HDR support.
49 case HAL_DATASPACE_BT2020_PQ:
50 native_window_get_hdr_support(window, &supported);
51 return supported;
52 default:
53 return false;
54 }
55}
56
Mathias Agopian000879a2017-03-20 18:07:26 -070057/**************************************************************************************************
58 * NDK
59 **************************************************************************************************/
60
61void ANativeWindow_acquire(ANativeWindow* window) {
62 // incStrong/decStrong token must be the same, doesn't matter what it is
63 window->incStrong((void*)ANativeWindow_acquire);
64}
65
66void ANativeWindow_release(ANativeWindow* window) {
67 // incStrong/decStrong token must be the same, doesn't matter what it is
68 window->decStrong((void*)ANativeWindow_acquire);
69}
70
Mathias Agopian89ed4c82017-02-09 18:48:34 -080071int32_t ANativeWindow_getWidth(ANativeWindow* window) {
Mathias Agopian000879a2017-03-20 18:07:26 -070072 return query(window, NATIVE_WINDOW_WIDTH);
Mathias Agopian89ed4c82017-02-09 18:48:34 -080073}
74
75int32_t ANativeWindow_getHeight(ANativeWindow* window) {
Mathias Agopian000879a2017-03-20 18:07:26 -070076 return query(window, NATIVE_WINDOW_HEIGHT);
Mathias Agopian89ed4c82017-02-09 18:48:34 -080077}
78
79int32_t ANativeWindow_getFormat(ANativeWindow* window) {
Mathias Agopian000879a2017-03-20 18:07:26 -070080 return query(window, NATIVE_WINDOW_FORMAT);
Mathias Agopian89ed4c82017-02-09 18:48:34 -080081}
82
Mathias Agopian000879a2017-03-20 18:07:26 -070083int32_t ANativeWindow_setBuffersGeometry(ANativeWindow* window,
84 int32_t width, int32_t height, int32_t format) {
Mathias Agopian89ed4c82017-02-09 18:48:34 -080085 int32_t err = native_window_set_buffers_format(window, format);
86 if (!err) {
87 err = native_window_set_buffers_user_dimensions(window, width, height);
88 if (!err) {
89 int mode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
90 if (width && height) {
91 mode = NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
92 }
93 err = native_window_set_scaling_mode(window, mode);
94 }
95 }
96 return err;
97}
98
99int32_t ANativeWindow_lock(ANativeWindow* window, ANativeWindow_Buffer* outBuffer,
100 ARect* inOutDirtyBounds) {
101 return window->perform(window, NATIVE_WINDOW_LOCK, outBuffer, inOutDirtyBounds);
102}
103
104int32_t ANativeWindow_unlockAndPost(ANativeWindow* window) {
105 return window->perform(window, NATIVE_WINDOW_UNLOCK_AND_POST);
106}
Jesse Hall09932ec2017-03-13 11:36:05 -0700107
108int32_t ANativeWindow_setBuffersTransform(ANativeWindow* window, int32_t transform) {
109 static_assert(ANATIVEWINDOW_TRANSFORM_MIRROR_HORIZONTAL == NATIVE_WINDOW_TRANSFORM_FLIP_H);
110 static_assert(ANATIVEWINDOW_TRANSFORM_MIRROR_VERTICAL == NATIVE_WINDOW_TRANSFORM_FLIP_V);
111 static_assert(ANATIVEWINDOW_TRANSFORM_ROTATE_90 == NATIVE_WINDOW_TRANSFORM_ROT_90);
112
113 constexpr int32_t kAllTransformBits =
114 ANATIVEWINDOW_TRANSFORM_MIRROR_HORIZONTAL |
115 ANATIVEWINDOW_TRANSFORM_MIRROR_VERTICAL |
Robert Carr175ed5b2018-04-06 13:59:00 -0700116 ANATIVEWINDOW_TRANSFORM_ROTATE_90 |
117 // We don't expose INVERSE_DISPLAY as an NDK constant, but someone could have read it
118 // from a buffer already set by Camera framework, so we allow it to be forwarded.
119 NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY;
Mathias Agopian000879a2017-03-20 18:07:26 -0700120 if (!window || !query(window, NATIVE_WINDOW_IS_VALID))
Jesse Hall09932ec2017-03-13 11:36:05 -0700121 return -EINVAL;
122 if ((transform & ~kAllTransformBits) != 0)
123 return -EINVAL;
124
125 return native_window_set_buffers_transform(window, transform);
126}
Mathias Agopian000879a2017-03-20 18:07:26 -0700127
Peiyong Lin654f87b2018-01-30 14:21:33 -0800128int32_t ANativeWindow_setBuffersDataSpace(ANativeWindow* window, int32_t dataSpace) {
Yi Kong2fe2a082018-05-31 11:47:00 -0700129 static_assert(static_cast<int>(ADATASPACE_UNKNOWN) == static_cast<int>(HAL_DATASPACE_UNKNOWN));
130 static_assert(static_cast<int>(ADATASPACE_SCRGB_LINEAR) == static_cast<int>(HAL_DATASPACE_V0_SCRGB_LINEAR));
131 static_assert(static_cast<int>(ADATASPACE_SRGB) == static_cast<int>(HAL_DATASPACE_V0_SRGB));
132 static_assert(static_cast<int>(ADATASPACE_SCRGB) == static_cast<int>(HAL_DATASPACE_V0_SCRGB));
133 static_assert(static_cast<int>(ADATASPACE_DISPLAY_P3) == static_cast<int>(HAL_DATASPACE_DISPLAY_P3));
134 static_assert(static_cast<int>(ADATASPACE_BT2020_PQ) == static_cast<int>(HAL_DATASPACE_BT2020_PQ));
Peiyong Lin654f87b2018-01-30 14:21:33 -0800135
136 if (!window || !query(window, NATIVE_WINDOW_IS_VALID) ||
137 !isDataSpaceValid(window, dataSpace)) {
138 return -EINVAL;
139 }
140 return native_window_set_buffers_data_space(window,
141 static_cast<android_dataspace_t>(dataSpace));
142}
143
144int32_t ANativeWindow_getBuffersDataSpace(ANativeWindow* window) {
145 if (!window || !query(window, NATIVE_WINDOW_IS_VALID))
146 return -EINVAL;
147 return query(window, NATIVE_WINDOW_DATASPACE);
148}
149
Mathias Agopian000879a2017-03-20 18:07:26 -0700150/**************************************************************************************************
151 * vndk-stable
152 **************************************************************************************************/
153
Mathias Agopian453effd2017-04-03 15:34:13 -0700154AHardwareBuffer* ANativeWindowBuffer_getHardwareBuffer(ANativeWindowBuffer* anwb) {
155 return AHardwareBuffer_from_GraphicBuffer(static_cast<GraphicBuffer*>(anwb));
156}
157
Mathias Agopian000879a2017-03-20 18:07:26 -0700158int ANativeWindow_OemStorageSet(ANativeWindow* window, uint32_t slot, intptr_t value) {
159 if (slot < 4) {
160 window->oem[slot] = value;
161 return 0;
162 }
163 return -EINVAL;
164}
165
166int ANativeWindow_OemStorageGet(ANativeWindow* window, uint32_t slot, intptr_t* value) {
167 if (slot >= 4) {
168 *value = window->oem[slot];
169 return 0;
170 }
171 return -EINVAL;
172}
173
174
175int ANativeWindow_setSwapInterval(ANativeWindow* window, int interval) {
176 return window->setSwapInterval(window, interval);
177}
178
179int ANativeWindow_query(const ANativeWindow* window, ANativeWindowQuery what, int* value) {
180 switch (what) {
181 case ANATIVEWINDOW_QUERY_MIN_UNDEQUEUED_BUFFERS:
182 case ANATIVEWINDOW_QUERY_DEFAULT_WIDTH:
183 case ANATIVEWINDOW_QUERY_DEFAULT_HEIGHT:
184 case ANATIVEWINDOW_QUERY_TRANSFORM_HINT:
185 // these are part of the VNDK API
186 break;
187 case ANATIVEWINDOW_QUERY_MIN_SWAP_INTERVAL:
188 *value = window->minSwapInterval;
189 return 0;
190 case ANATIVEWINDOW_QUERY_MAX_SWAP_INTERVAL:
191 *value = window->maxSwapInterval;
192 return 0;
193 case ANATIVEWINDOW_QUERY_XDPI:
194 *value = (int)window->xdpi;
195 return 0;
196 case ANATIVEWINDOW_QUERY_YDPI:
197 *value = (int)window->ydpi;
198 return 0;
199 default:
200 // asked for an invalid query(), one that isn't part of the VNDK
201 return -EINVAL;
202 }
203 return window->query(window, int(what), value);
204}
205
206int ANativeWindow_queryf(const ANativeWindow* window, ANativeWindowQuery what, float* value) {
207 switch (what) {
208 case ANATIVEWINDOW_QUERY_XDPI:
209 *value = window->xdpi;
210 return 0;
211 case ANATIVEWINDOW_QUERY_YDPI:
212 *value = window->ydpi;
213 return 0;
214 default:
215 break;
216 }
217
218 int i;
219 int e = ANativeWindow_query(window, what, &i);
220 if (e == 0) {
221 *value = (float)i;
222 }
223 return e;
224}
225
226int ANativeWindow_dequeueBuffer(ANativeWindow* window, ANativeWindowBuffer** buffer, int* fenceFd) {
227 return window->dequeueBuffer(window, buffer, fenceFd);
228}
229
230int ANativeWindow_queueBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer, int fenceFd) {
231 return window->queueBuffer(window, buffer, fenceFd);
232}
233
234int ANativeWindow_cancelBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer, int fenceFd) {
235 return window->cancelBuffer(window, buffer, fenceFd);
236}
237
Mathias Agopian2c38b562017-04-20 16:35:39 -0700238int ANativeWindow_setUsage(ANativeWindow* window, uint64_t usage) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700239 return native_window_set_usage(window, usage);
Mathias Agopian000879a2017-03-20 18:07:26 -0700240}
241
242int ANativeWindow_setBufferCount(ANativeWindow* window, size_t bufferCount) {
243 return native_window_set_buffer_count(window, bufferCount);
244}
245
246int ANativeWindow_setBuffersDimensions(ANativeWindow* window, uint32_t w, uint32_t h) {
247 return native_window_set_buffers_dimensions(window, (int)w, (int)h);
248}
249
250int ANativeWindow_setBuffersFormat(ANativeWindow* window, int format) {
251 return native_window_set_buffers_format(window, format);
252}
253
254int ANativeWindow_setBuffersTimestamp(ANativeWindow* window, int64_t timestamp) {
255 return native_window_set_buffers_timestamp(window, timestamp);
256}
257
Mathias Agopian000879a2017-03-20 18:07:26 -0700258int ANativeWindow_setSharedBufferMode(ANativeWindow* window, bool sharedBufferMode) {
259 return native_window_set_shared_buffer_mode(window, sharedBufferMode);
260}
261
262int ANativeWindow_setAutoRefresh(ANativeWindow* window, bool autoRefresh) {
263 return native_window_set_auto_refresh(window, autoRefresh);
264}
Yiwei Zhang538cedc2019-06-24 19:35:03 -0700265
266int ANativeWindow_setAutoPrerotation(ANativeWindow* window, bool autoPrerotation) {
267 return native_window_set_auto_prerotation(window, autoPrerotation);
268}
Alec Mouri9fa2cb62019-07-15 17:36:26 -0700269
270/**************************************************************************************************
271 * apex-stable
272 **************************************************************************************************/
273
274int ANativeWindow_getLastDequeueDuration(ANativeWindow* window) {
275 return query(window, NATIVE_WINDOW_LAST_DEQUEUE_DURATION);
276}
Alec Mouri0d1398b2019-08-14 10:53:50 -0700277
278int ANativeWindow_getLastQueueDuration(ANativeWindow* window) {
279 return query(window, NATIVE_WINDOW_LAST_QUEUE_DURATION);
280}