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 | |
| 19 | #include <android/native_window.h> |
Mathias Agopian | 000879a | 2017-03-20 18:07:26 -0700 | [diff] [blame^] | 20 | |
| 21 | // from nativewindow/includes/system/window.h |
| 22 | // (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] | 23 | #include <system/window.h> |
| 24 | |
Mathias Agopian | 000879a | 2017-03-20 18:07:26 -0700 | [diff] [blame^] | 25 | #include <private/android/AHardwareBufferHelpers.h> |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 26 | |
Mathias Agopian | 000879a | 2017-03-20 18:07:26 -0700 | [diff] [blame^] | 27 | using namespace android; |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 28 | |
Mathias Agopian | 000879a | 2017-03-20 18:07:26 -0700 | [diff] [blame^] | 29 | static int32_t query(ANativeWindow* window, int what) { |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 30 | int value; |
| 31 | int res = window->query(window, what, &value); |
| 32 | return res < 0 ? res : value; |
| 33 | } |
| 34 | |
Mathias Agopian | 000879a | 2017-03-20 18:07:26 -0700 | [diff] [blame^] | 35 | /************************************************************************************************** |
| 36 | * NDK |
| 37 | **************************************************************************************************/ |
| 38 | |
| 39 | void ANativeWindow_acquire(ANativeWindow* window) { |
| 40 | // incStrong/decStrong token must be the same, doesn't matter what it is |
| 41 | window->incStrong((void*)ANativeWindow_acquire); |
| 42 | } |
| 43 | |
| 44 | void ANativeWindow_release(ANativeWindow* window) { |
| 45 | // incStrong/decStrong token must be the same, doesn't matter what it is |
| 46 | window->decStrong((void*)ANativeWindow_acquire); |
| 47 | } |
| 48 | |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 49 | int32_t ANativeWindow_getWidth(ANativeWindow* window) { |
Mathias Agopian | 000879a | 2017-03-20 18:07:26 -0700 | [diff] [blame^] | 50 | return query(window, NATIVE_WINDOW_WIDTH); |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | int32_t ANativeWindow_getHeight(ANativeWindow* window) { |
Mathias Agopian | 000879a | 2017-03-20 18:07:26 -0700 | [diff] [blame^] | 54 | return query(window, NATIVE_WINDOW_HEIGHT); |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | int32_t ANativeWindow_getFormat(ANativeWindow* window) { |
Mathias Agopian | 000879a | 2017-03-20 18:07:26 -0700 | [diff] [blame^] | 58 | return query(window, NATIVE_WINDOW_FORMAT); |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 59 | } |
| 60 | |
Mathias Agopian | 000879a | 2017-03-20 18:07:26 -0700 | [diff] [blame^] | 61 | int32_t ANativeWindow_setBuffersGeometry(ANativeWindow* window, |
| 62 | int32_t width, int32_t height, int32_t format) { |
Mathias Agopian | 89ed4c8 | 2017-02-09 18:48:34 -0800 | [diff] [blame] | 63 | int32_t err = native_window_set_buffers_format(window, format); |
| 64 | if (!err) { |
| 65 | err = native_window_set_buffers_user_dimensions(window, width, height); |
| 66 | if (!err) { |
| 67 | int mode = NATIVE_WINDOW_SCALING_MODE_FREEZE; |
| 68 | if (width && height) { |
| 69 | mode = NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW; |
| 70 | } |
| 71 | err = native_window_set_scaling_mode(window, mode); |
| 72 | } |
| 73 | } |
| 74 | return err; |
| 75 | } |
| 76 | |
| 77 | int32_t ANativeWindow_lock(ANativeWindow* window, ANativeWindow_Buffer* outBuffer, |
| 78 | ARect* inOutDirtyBounds) { |
| 79 | return window->perform(window, NATIVE_WINDOW_LOCK, outBuffer, inOutDirtyBounds); |
| 80 | } |
| 81 | |
| 82 | int32_t ANativeWindow_unlockAndPost(ANativeWindow* window) { |
| 83 | return window->perform(window, NATIVE_WINDOW_UNLOCK_AND_POST); |
| 84 | } |
Jesse Hall | 09932ec | 2017-03-13 11:36:05 -0700 | [diff] [blame] | 85 | |
| 86 | int32_t ANativeWindow_setBuffersTransform(ANativeWindow* window, int32_t transform) { |
| 87 | static_assert(ANATIVEWINDOW_TRANSFORM_MIRROR_HORIZONTAL == NATIVE_WINDOW_TRANSFORM_FLIP_H); |
| 88 | static_assert(ANATIVEWINDOW_TRANSFORM_MIRROR_VERTICAL == NATIVE_WINDOW_TRANSFORM_FLIP_V); |
| 89 | static_assert(ANATIVEWINDOW_TRANSFORM_ROTATE_90 == NATIVE_WINDOW_TRANSFORM_ROT_90); |
| 90 | |
| 91 | constexpr int32_t kAllTransformBits = |
| 92 | ANATIVEWINDOW_TRANSFORM_MIRROR_HORIZONTAL | |
| 93 | ANATIVEWINDOW_TRANSFORM_MIRROR_VERTICAL | |
| 94 | ANATIVEWINDOW_TRANSFORM_ROTATE_90; |
Mathias Agopian | 000879a | 2017-03-20 18:07:26 -0700 | [diff] [blame^] | 95 | if (!window || !query(window, NATIVE_WINDOW_IS_VALID)) |
Jesse Hall | 09932ec | 2017-03-13 11:36:05 -0700 | [diff] [blame] | 96 | return -EINVAL; |
| 97 | if ((transform & ~kAllTransformBits) != 0) |
| 98 | return -EINVAL; |
| 99 | |
| 100 | return native_window_set_buffers_transform(window, transform); |
| 101 | } |
Mathias Agopian | 000879a | 2017-03-20 18:07:26 -0700 | [diff] [blame^] | 102 | |
| 103 | /************************************************************************************************** |
| 104 | * vndk-stable |
| 105 | **************************************************************************************************/ |
| 106 | |
| 107 | int ANativeWindow_OemStorageSet(ANativeWindow* window, uint32_t slot, intptr_t value) { |
| 108 | if (slot < 4) { |
| 109 | window->oem[slot] = value; |
| 110 | return 0; |
| 111 | } |
| 112 | return -EINVAL; |
| 113 | } |
| 114 | |
| 115 | int ANativeWindow_OemStorageGet(ANativeWindow* window, uint32_t slot, intptr_t* value) { |
| 116 | if (slot >= 4) { |
| 117 | *value = window->oem[slot]; |
| 118 | return 0; |
| 119 | } |
| 120 | return -EINVAL; |
| 121 | } |
| 122 | |
| 123 | |
| 124 | int ANativeWindow_setSwapInterval(ANativeWindow* window, int interval) { |
| 125 | return window->setSwapInterval(window, interval); |
| 126 | } |
| 127 | |
| 128 | int ANativeWindow_query(const ANativeWindow* window, ANativeWindowQuery what, int* value) { |
| 129 | switch (what) { |
| 130 | case ANATIVEWINDOW_QUERY_MIN_UNDEQUEUED_BUFFERS: |
| 131 | case ANATIVEWINDOW_QUERY_DEFAULT_WIDTH: |
| 132 | case ANATIVEWINDOW_QUERY_DEFAULT_HEIGHT: |
| 133 | case ANATIVEWINDOW_QUERY_TRANSFORM_HINT: |
| 134 | // these are part of the VNDK API |
| 135 | break; |
| 136 | case ANATIVEWINDOW_QUERY_MIN_SWAP_INTERVAL: |
| 137 | *value = window->minSwapInterval; |
| 138 | return 0; |
| 139 | case ANATIVEWINDOW_QUERY_MAX_SWAP_INTERVAL: |
| 140 | *value = window->maxSwapInterval; |
| 141 | return 0; |
| 142 | case ANATIVEWINDOW_QUERY_XDPI: |
| 143 | *value = (int)window->xdpi; |
| 144 | return 0; |
| 145 | case ANATIVEWINDOW_QUERY_YDPI: |
| 146 | *value = (int)window->ydpi; |
| 147 | return 0; |
| 148 | default: |
| 149 | // asked for an invalid query(), one that isn't part of the VNDK |
| 150 | return -EINVAL; |
| 151 | } |
| 152 | return window->query(window, int(what), value); |
| 153 | } |
| 154 | |
| 155 | int ANativeWindow_queryf(const ANativeWindow* window, ANativeWindowQuery what, float* value) { |
| 156 | switch (what) { |
| 157 | case ANATIVEWINDOW_QUERY_XDPI: |
| 158 | *value = window->xdpi; |
| 159 | return 0; |
| 160 | case ANATIVEWINDOW_QUERY_YDPI: |
| 161 | *value = window->ydpi; |
| 162 | return 0; |
| 163 | default: |
| 164 | break; |
| 165 | } |
| 166 | |
| 167 | int i; |
| 168 | int e = ANativeWindow_query(window, what, &i); |
| 169 | if (e == 0) { |
| 170 | *value = (float)i; |
| 171 | } |
| 172 | return e; |
| 173 | } |
| 174 | |
| 175 | int ANativeWindow_dequeueBuffer(ANativeWindow* window, ANativeWindowBuffer** buffer, int* fenceFd) { |
| 176 | return window->dequeueBuffer(window, buffer, fenceFd); |
| 177 | } |
| 178 | |
| 179 | int ANativeWindow_queueBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer, int fenceFd) { |
| 180 | return window->queueBuffer(window, buffer, fenceFd); |
| 181 | } |
| 182 | |
| 183 | int ANativeWindow_cancelBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer, int fenceFd) { |
| 184 | return window->cancelBuffer(window, buffer, fenceFd); |
| 185 | } |
| 186 | |
| 187 | int ANativeWindow_setUsage(ANativeWindow* window, uint64_t usage0, uint64_t usage1) { |
| 188 | uint64_t pUsage, cUsage; |
| 189 | AHardwareBuffer_convertToGrallocUsageBits(&pUsage, &cUsage, usage0, usage1); |
| 190 | uint32_t gralloc_usage = uint32_t(pUsage | cUsage); |
| 191 | return native_window_set_usage(window, gralloc_usage); |
| 192 | } |
| 193 | |
| 194 | int ANativeWindow_setBufferCount(ANativeWindow* window, size_t bufferCount) { |
| 195 | return native_window_set_buffer_count(window, bufferCount); |
| 196 | } |
| 197 | |
| 198 | int ANativeWindow_setBuffersDimensions(ANativeWindow* window, uint32_t w, uint32_t h) { |
| 199 | return native_window_set_buffers_dimensions(window, (int)w, (int)h); |
| 200 | } |
| 201 | |
| 202 | int ANativeWindow_setBuffersFormat(ANativeWindow* window, int format) { |
| 203 | return native_window_set_buffers_format(window, format); |
| 204 | } |
| 205 | |
| 206 | int ANativeWindow_setBuffersTimestamp(ANativeWindow* window, int64_t timestamp) { |
| 207 | return native_window_set_buffers_timestamp(window, timestamp); |
| 208 | } |
| 209 | |
| 210 | int ANativeWindow_setBufferDataSpace(ANativeWindow* window, android_dataspace_t dataSpace) { |
| 211 | return native_window_set_buffers_data_space(window, dataSpace); |
| 212 | } |
| 213 | |
| 214 | int ANativeWindow_setSharedBufferMode(ANativeWindow* window, bool sharedBufferMode) { |
| 215 | return native_window_set_shared_buffer_mode(window, sharedBufferMode); |
| 216 | } |
| 217 | |
| 218 | int ANativeWindow_setAutoRefresh(ANativeWindow* window, bool autoRefresh) { |
| 219 | return native_window_set_auto_refresh(window, autoRefresh); |
| 220 | } |