blob: 2598451ad3340d0308d561148a52ed8c9450d22b [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 |
116 ANATIVEWINDOW_TRANSFORM_ROTATE_90;
Mathias Agopian000879a2017-03-20 18:07:26 -0700117 if (!window || !query(window, NATIVE_WINDOW_IS_VALID))
Jesse Hall09932ec2017-03-13 11:36:05 -0700118 return -EINVAL;
119 if ((transform & ~kAllTransformBits) != 0)
120 return -EINVAL;
121
122 return native_window_set_buffers_transform(window, transform);
123}
Mathias Agopian000879a2017-03-20 18:07:26 -0700124
Peiyong Lin654f87b2018-01-30 14:21:33 -0800125int32_t ANativeWindow_setBuffersDataSpace(ANativeWindow* window, int32_t dataSpace) {
126 static_assert(ADATASPACE_UNKNOWN == HAL_DATASPACE_UNKNOWN);
127 static_assert(ADATASPACE_SCRGB_LINEAR == HAL_DATASPACE_V0_SCRGB_LINEAR);
128 static_assert(ADATASPACE_SRGB == HAL_DATASPACE_V0_SRGB);
129 static_assert(ADATASPACE_SCRGB == HAL_DATASPACE_V0_SCRGB);
130 static_assert(ADATASPACE_DISPLAY_P3 == HAL_DATASPACE_DISPLAY_P3);
131 static_assert(ADATASPACE_BT2020_PQ == HAL_DATASPACE_BT2020_PQ);
132
133 if (!window || !query(window, NATIVE_WINDOW_IS_VALID) ||
134 !isDataSpaceValid(window, dataSpace)) {
135 return -EINVAL;
136 }
137 return native_window_set_buffers_data_space(window,
138 static_cast<android_dataspace_t>(dataSpace));
139}
140
141int32_t ANativeWindow_getBuffersDataSpace(ANativeWindow* window) {
142 if (!window || !query(window, NATIVE_WINDOW_IS_VALID))
143 return -EINVAL;
144 return query(window, NATIVE_WINDOW_DATASPACE);
145}
146
Mathias Agopian000879a2017-03-20 18:07:26 -0700147/**************************************************************************************************
148 * vndk-stable
149 **************************************************************************************************/
150
Mathias Agopian453effd2017-04-03 15:34:13 -0700151AHardwareBuffer* ANativeWindowBuffer_getHardwareBuffer(ANativeWindowBuffer* anwb) {
152 return AHardwareBuffer_from_GraphicBuffer(static_cast<GraphicBuffer*>(anwb));
153}
154
Mathias Agopian000879a2017-03-20 18:07:26 -0700155int ANativeWindow_OemStorageSet(ANativeWindow* window, uint32_t slot, intptr_t value) {
156 if (slot < 4) {
157 window->oem[slot] = value;
158 return 0;
159 }
160 return -EINVAL;
161}
162
163int ANativeWindow_OemStorageGet(ANativeWindow* window, uint32_t slot, intptr_t* value) {
164 if (slot >= 4) {
165 *value = window->oem[slot];
166 return 0;
167 }
168 return -EINVAL;
169}
170
171
172int ANativeWindow_setSwapInterval(ANativeWindow* window, int interval) {
173 return window->setSwapInterval(window, interval);
174}
175
176int ANativeWindow_query(const ANativeWindow* window, ANativeWindowQuery what, int* value) {
177 switch (what) {
178 case ANATIVEWINDOW_QUERY_MIN_UNDEQUEUED_BUFFERS:
179 case ANATIVEWINDOW_QUERY_DEFAULT_WIDTH:
180 case ANATIVEWINDOW_QUERY_DEFAULT_HEIGHT:
181 case ANATIVEWINDOW_QUERY_TRANSFORM_HINT:
182 // these are part of the VNDK API
183 break;
184 case ANATIVEWINDOW_QUERY_MIN_SWAP_INTERVAL:
185 *value = window->minSwapInterval;
186 return 0;
187 case ANATIVEWINDOW_QUERY_MAX_SWAP_INTERVAL:
188 *value = window->maxSwapInterval;
189 return 0;
190 case ANATIVEWINDOW_QUERY_XDPI:
191 *value = (int)window->xdpi;
192 return 0;
193 case ANATIVEWINDOW_QUERY_YDPI:
194 *value = (int)window->ydpi;
195 return 0;
196 default:
197 // asked for an invalid query(), one that isn't part of the VNDK
198 return -EINVAL;
199 }
200 return window->query(window, int(what), value);
201}
202
203int ANativeWindow_queryf(const ANativeWindow* window, ANativeWindowQuery what, float* value) {
204 switch (what) {
205 case ANATIVEWINDOW_QUERY_XDPI:
206 *value = window->xdpi;
207 return 0;
208 case ANATIVEWINDOW_QUERY_YDPI:
209 *value = window->ydpi;
210 return 0;
211 default:
212 break;
213 }
214
215 int i;
216 int e = ANativeWindow_query(window, what, &i);
217 if (e == 0) {
218 *value = (float)i;
219 }
220 return e;
221}
222
223int ANativeWindow_dequeueBuffer(ANativeWindow* window, ANativeWindowBuffer** buffer, int* fenceFd) {
224 return window->dequeueBuffer(window, buffer, fenceFd);
225}
226
227int ANativeWindow_queueBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer, int fenceFd) {
228 return window->queueBuffer(window, buffer, fenceFd);
229}
230
231int ANativeWindow_cancelBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer, int fenceFd) {
232 return window->cancelBuffer(window, buffer, fenceFd);
233}
234
Mathias Agopian2c38b562017-04-20 16:35:39 -0700235int ANativeWindow_setUsage(ANativeWindow* window, uint64_t usage) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700236 return native_window_set_usage(window, usage);
Mathias Agopian000879a2017-03-20 18:07:26 -0700237}
238
239int ANativeWindow_setBufferCount(ANativeWindow* window, size_t bufferCount) {
240 return native_window_set_buffer_count(window, bufferCount);
241}
242
243int ANativeWindow_setBuffersDimensions(ANativeWindow* window, uint32_t w, uint32_t h) {
244 return native_window_set_buffers_dimensions(window, (int)w, (int)h);
245}
246
247int ANativeWindow_setBuffersFormat(ANativeWindow* window, int format) {
248 return native_window_set_buffers_format(window, format);
249}
250
251int ANativeWindow_setBuffersTimestamp(ANativeWindow* window, int64_t timestamp) {
252 return native_window_set_buffers_timestamp(window, timestamp);
253}
254
Mathias Agopian000879a2017-03-20 18:07:26 -0700255int ANativeWindow_setSharedBufferMode(ANativeWindow* window, bool sharedBufferMode) {
256 return native_window_set_shared_buffer_mode(window, sharedBufferMode);
257}
258
259int ANativeWindow_setAutoRefresh(ANativeWindow* window, bool autoRefresh) {
260 return native_window_set_auto_refresh(window, autoRefresh);
261}