Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 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 | |
Jesse Hall | 7992781 | 2017-03-23 11:03:23 -0700 | [diff] [blame] | 19 | #include <grallocusage/GrallocUsageConversion.h> |
Mathias Agopian | 000879a | 2017-03-20 18:07:26 -0700 | [diff] [blame] | 20 | // from nativewindow/includes/system/window.h |
| 21 | // (not to be confused with the compatibility-only window.h from system/core/includes) |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 22 | #include <system/window.h> |
| 23 | |
Mathias Agopian | 000879a | 2017-03-20 18:07:26 -0700 | [diff] [blame] | 24 | #include <private/android/AHardwareBufferHelpers.h> |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 25 | |
Mathias Agopian | 453effd | 2017-04-03 15:34:13 -0700 | [diff] [blame] | 26 | #include <ui/GraphicBuffer.h> |
| 27 | |
Mathias Agopian | 000879a | 2017-03-20 18:07:26 -0700 | [diff] [blame] | 28 | using namespace android; |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 29 | |
Mathias Agopian | 000879a | 2017-03-20 18:07:26 -0700 | [diff] [blame] | 30 | static int32_t query(ANativeWindow* window, int what) { |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 31 | int value; |
| 32 | int res = window->query(window, what, &value); |
| 33 | return res < 0 ? res : value; |
| 34 | } |
| 35 | |
Peiyong Lin | 654f87b | 2018-01-30 14:21:33 -0800 | [diff] [blame] | 36 | static 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 Agopian | 000879a | 2017-03-20 18:07:26 -0700 | [diff] [blame] | 57 | /************************************************************************************************** |
| 58 | * NDK |
| 59 | **************************************************************************************************/ |
| 60 | |
| 61 | void 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 | |
| 66 | void 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 Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 71 | int32_t ANativeWindow_getWidth(ANativeWindow* window) { |
Mathias Agopian | 000879a | 2017-03-20 18:07:26 -0700 | [diff] [blame] | 72 | return query(window, NATIVE_WINDOW_WIDTH); |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | int32_t ANativeWindow_getHeight(ANativeWindow* window) { |
Mathias Agopian | 000879a | 2017-03-20 18:07:26 -0700 | [diff] [blame] | 76 | return query(window, NATIVE_WINDOW_HEIGHT); |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | int32_t ANativeWindow_getFormat(ANativeWindow* window) { |
Mathias Agopian | 000879a | 2017-03-20 18:07:26 -0700 | [diff] [blame] | 80 | return query(window, NATIVE_WINDOW_FORMAT); |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 81 | } |
| 82 | |
Mathias Agopian | 000879a | 2017-03-20 18:07:26 -0700 | [diff] [blame] | 83 | int32_t ANativeWindow_setBuffersGeometry(ANativeWindow* window, |
| 84 | int32_t width, int32_t height, int32_t format) { |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 85 | 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 | |
| 99 | int32_t ANativeWindow_lock(ANativeWindow* window, ANativeWindow_Buffer* outBuffer, |
| 100 | ARect* inOutDirtyBounds) { |
| 101 | return window->perform(window, NATIVE_WINDOW_LOCK, outBuffer, inOutDirtyBounds); |
| 102 | } |
| 103 | |
| 104 | int32_t ANativeWindow_unlockAndPost(ANativeWindow* window) { |
| 105 | return window->perform(window, NATIVE_WINDOW_UNLOCK_AND_POST); |
| 106 | } |
Jesse Hall | 09932ec | 2017-03-13 11:36:05 -0700 | [diff] [blame] | 107 | |
| 108 | int32_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 Carr | 175ed5b | 2018-04-06 13:59:00 -0700 | [diff] [blame] | 116 | 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 Agopian | 000879a | 2017-03-20 18:07:26 -0700 | [diff] [blame] | 120 | if (!window || !query(window, NATIVE_WINDOW_IS_VALID)) |
Jesse Hall | 09932ec | 2017-03-13 11:36:05 -0700 | [diff] [blame] | 121 | return -EINVAL; |
| 122 | if ((transform & ~kAllTransformBits) != 0) |
| 123 | return -EINVAL; |
| 124 | |
| 125 | return native_window_set_buffers_transform(window, transform); |
| 126 | } |
Mathias Agopian | 000879a | 2017-03-20 18:07:26 -0700 | [diff] [blame] | 127 | |
Peiyong Lin | 654f87b | 2018-01-30 14:21:33 -0800 | [diff] [blame] | 128 | int32_t ANativeWindow_setBuffersDataSpace(ANativeWindow* window, int32_t dataSpace) { |
Yi Kong | 2fe2a08 | 2018-05-31 11:47:00 -0700 | [diff] [blame] | 129 | 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 Lin | 654f87b | 2018-01-30 14:21:33 -0800 | [diff] [blame] | 135 | |
| 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 | |
| 144 | int32_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 Agopian | 000879a | 2017-03-20 18:07:26 -0700 | [diff] [blame] | 150 | /************************************************************************************************** |
| 151 | * vndk-stable |
| 152 | **************************************************************************************************/ |
| 153 | |
Mathias Agopian | 453effd | 2017-04-03 15:34:13 -0700 | [diff] [blame] | 154 | AHardwareBuffer* ANativeWindowBuffer_getHardwareBuffer(ANativeWindowBuffer* anwb) { |
| 155 | return AHardwareBuffer_from_GraphicBuffer(static_cast<GraphicBuffer*>(anwb)); |
| 156 | } |
| 157 | |
Mathias Agopian | 000879a | 2017-03-20 18:07:26 -0700 | [diff] [blame] | 158 | int 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 | |
| 166 | int 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 | |
| 175 | int ANativeWindow_setSwapInterval(ANativeWindow* window, int interval) { |
| 176 | return window->setSwapInterval(window, interval); |
| 177 | } |
| 178 | |
| 179 | int 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 | |
| 206 | int 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 | |
| 226 | int ANativeWindow_dequeueBuffer(ANativeWindow* window, ANativeWindowBuffer** buffer, int* fenceFd) { |
| 227 | return window->dequeueBuffer(window, buffer, fenceFd); |
| 228 | } |
| 229 | |
| 230 | int ANativeWindow_queueBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer, int fenceFd) { |
| 231 | return window->queueBuffer(window, buffer, fenceFd); |
| 232 | } |
| 233 | |
| 234 | int ANativeWindow_cancelBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer, int fenceFd) { |
| 235 | return window->cancelBuffer(window, buffer, fenceFd); |
| 236 | } |
| 237 | |
Mathias Agopian | 2c38b56 | 2017-04-20 16:35:39 -0700 | [diff] [blame] | 238 | int ANativeWindow_setUsage(ANativeWindow* window, uint64_t usage) { |
Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 239 | return native_window_set_usage(window, usage); |
Mathias Agopian | 000879a | 2017-03-20 18:07:26 -0700 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | int ANativeWindow_setBufferCount(ANativeWindow* window, size_t bufferCount) { |
| 243 | return native_window_set_buffer_count(window, bufferCount); |
| 244 | } |
| 245 | |
| 246 | int ANativeWindow_setBuffersDimensions(ANativeWindow* window, uint32_t w, uint32_t h) { |
| 247 | return native_window_set_buffers_dimensions(window, (int)w, (int)h); |
| 248 | } |
| 249 | |
| 250 | int ANativeWindow_setBuffersFormat(ANativeWindow* window, int format) { |
| 251 | return native_window_set_buffers_format(window, format); |
| 252 | } |
| 253 | |
| 254 | int ANativeWindow_setBuffersTimestamp(ANativeWindow* window, int64_t timestamp) { |
| 255 | return native_window_set_buffers_timestamp(window, timestamp); |
| 256 | } |
| 257 | |
Mathias Agopian | 000879a | 2017-03-20 18:07:26 -0700 | [diff] [blame] | 258 | int ANativeWindow_setSharedBufferMode(ANativeWindow* window, bool sharedBufferMode) { |
| 259 | return native_window_set_shared_buffer_mode(window, sharedBufferMode); |
| 260 | } |
| 261 | |
| 262 | int ANativeWindow_setAutoRefresh(ANativeWindow* window, bool autoRefresh) { |
| 263 | return native_window_set_auto_refresh(window, autoRefresh); |
| 264 | } |
Yiwei Zhang | 538cedc | 2019-06-24 19:35:03 -0700 | [diff] [blame] | 265 | |
| 266 | int ANativeWindow_setAutoPrerotation(ANativeWindow* window, bool autoPrerotation) { |
| 267 | return native_window_set_auto_prerotation(window, autoPrerotation); |
| 268 | } |
Alec Mouri | 9fa2cb6 | 2019-07-15 17:36:26 -0700 | [diff] [blame] | 269 | |
| 270 | /************************************************************************************************** |
| 271 | * apex-stable |
| 272 | **************************************************************************************************/ |
| 273 | |
| 274 | int ANativeWindow_getLastDequeueDuration(ANativeWindow* window) { |
| 275 | return query(window, NATIVE_WINDOW_LAST_DEQUEUE_DURATION); |
| 276 | } |
Alec Mouri | 0d1398b | 2019-08-14 10:53:50 -0700 | [diff] [blame] | 277 | |
| 278 | int ANativeWindow_getLastQueueDuration(ANativeWindow* window) { |
| 279 | return query(window, NATIVE_WINDOW_LAST_QUEUE_DURATION); |
| 280 | } |
Alec Mouri | a161966 | 2019-08-21 19:30:48 -0700 | [diff] [blame] | 281 | |
| 282 | int64_t ANativeWindow_getLastDequeueStartTime(ANativeWindow* window) { |
| 283 | int64_t time; |
| 284 | int success = window->perform(window, NATIVE_WINDOW_GET_LAST_DEQUEUE_START, &time); |
| 285 | return success < 0 ? success : time; |
| 286 | } |
Alec Mouri | 04fdb60 | 2019-08-23 19:41:43 -0700 | [diff] [blame^] | 287 | |
| 288 | int ANativeWindow_setDequeueTimeout(ANativeWindow* window, int64_t timeout) { |
| 289 | return window->perform(window, NATIVE_WINDOW_SET_DEQUEUE_TIMEOUT, timeout); |
| 290 | } |