2 * Copyright (C) 2010 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #define LOG_TAG "ANativeWindow"
19 #include <grallocusage/GrallocUsageConversion.h>
20 // from nativewindow/includes/system/window.h
21 // (not to be confused with the compatibility-only window.h from system/core/includes)
22 #include <system/window.h>
24 #include <private/android/AHardwareBufferHelpers.h>
26 #include <ui/GraphicBuffer.h>
28 using namespace android;
30 static int32_t query(ANativeWindow* window, int what) {
32 int res = window->query(window, what, &value);
33 return res < 0 ? res : value;
36 /**************************************************************************************************
38 **************************************************************************************************/
40 void ANativeWindow_acquire(ANativeWindow* window) {
41 // incStrong/decStrong token must be the same, doesn't matter what it is
42 window->incStrong((void*)ANativeWindow_acquire);
45 void ANativeWindow_release(ANativeWindow* window) {
46 // incStrong/decStrong token must be the same, doesn't matter what it is
47 window->decStrong((void*)ANativeWindow_acquire);
50 int32_t ANativeWindow_getWidth(ANativeWindow* window) {
51 return query(window, NATIVE_WINDOW_WIDTH);
54 int32_t ANativeWindow_getHeight(ANativeWindow* window) {
55 return query(window, NATIVE_WINDOW_HEIGHT);
58 int32_t ANativeWindow_getFormat(ANativeWindow* window) {
59 return query(window, NATIVE_WINDOW_FORMAT);
62 int32_t ANativeWindow_setBuffersGeometry(ANativeWindow* window,
63 int32_t width, int32_t height, int32_t format) {
64 int32_t err = native_window_set_buffers_format(window, format);
66 err = native_window_set_buffers_user_dimensions(window, width, height);
68 int mode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
69 if (width && height) {
70 mode = NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
72 err = native_window_set_scaling_mode(window, mode);
78 int32_t ANativeWindow_lock(ANativeWindow* window, ANativeWindow_Buffer* outBuffer,
79 ARect* inOutDirtyBounds) {
80 return window->perform(window, NATIVE_WINDOW_LOCK, outBuffer, inOutDirtyBounds);
83 int32_t ANativeWindow_unlockAndPost(ANativeWindow* window) {
84 return window->perform(window, NATIVE_WINDOW_UNLOCK_AND_POST);
87 int32_t ANativeWindow_setBuffersTransform(ANativeWindow* window, int32_t transform) {
88 static_assert(ANATIVEWINDOW_TRANSFORM_MIRROR_HORIZONTAL == NATIVE_WINDOW_TRANSFORM_FLIP_H);
89 static_assert(ANATIVEWINDOW_TRANSFORM_MIRROR_VERTICAL == NATIVE_WINDOW_TRANSFORM_FLIP_V);
90 static_assert(ANATIVEWINDOW_TRANSFORM_ROTATE_90 == NATIVE_WINDOW_TRANSFORM_ROT_90);
92 constexpr int32_t kAllTransformBits =
93 ANATIVEWINDOW_TRANSFORM_MIRROR_HORIZONTAL |
94 ANATIVEWINDOW_TRANSFORM_MIRROR_VERTICAL |
95 ANATIVEWINDOW_TRANSFORM_ROTATE_90;
96 if (!window || !query(window, NATIVE_WINDOW_IS_VALID))
98 if ((transform & ~kAllTransformBits) != 0)
101 return native_window_set_buffers_transform(window, transform);
104 /**************************************************************************************************
106 **************************************************************************************************/
108 AHardwareBuffer* ANativeWindowBuffer_getHardwareBuffer(ANativeWindowBuffer* anwb) {
109 return AHardwareBuffer_from_GraphicBuffer(static_cast<GraphicBuffer*>(anwb));
112 int ANativeWindow_OemStorageSet(ANativeWindow* window, uint32_t slot, intptr_t value) {
114 window->oem[slot] = value;
120 int ANativeWindow_OemStorageGet(ANativeWindow* window, uint32_t slot, intptr_t* value) {
122 *value = window->oem[slot];
129 int ANativeWindow_setSwapInterval(ANativeWindow* window, int interval) {
130 return window->setSwapInterval(window, interval);
133 int ANativeWindow_query(const ANativeWindow* window, ANativeWindowQuery what, int* value) {
135 case ANATIVEWINDOW_QUERY_MIN_UNDEQUEUED_BUFFERS:
136 case ANATIVEWINDOW_QUERY_DEFAULT_WIDTH:
137 case ANATIVEWINDOW_QUERY_DEFAULT_HEIGHT:
138 case ANATIVEWINDOW_QUERY_TRANSFORM_HINT:
139 // these are part of the VNDK API
141 case ANATIVEWINDOW_QUERY_MIN_SWAP_INTERVAL:
142 *value = window->minSwapInterval;
144 case ANATIVEWINDOW_QUERY_MAX_SWAP_INTERVAL:
145 *value = window->maxSwapInterval;
147 case ANATIVEWINDOW_QUERY_XDPI:
148 *value = (int)window->xdpi;
150 case ANATIVEWINDOW_QUERY_YDPI:
151 *value = (int)window->ydpi;
154 // asked for an invalid query(), one that isn't part of the VNDK
157 return window->query(window, int(what), value);
160 int ANativeWindow_queryf(const ANativeWindow* window, ANativeWindowQuery what, float* value) {
162 case ANATIVEWINDOW_QUERY_XDPI:
163 *value = window->xdpi;
165 case ANATIVEWINDOW_QUERY_YDPI:
166 *value = window->ydpi;
173 int e = ANativeWindow_query(window, what, &i);
180 int ANativeWindow_dequeueBuffer(ANativeWindow* window, ANativeWindowBuffer** buffer, int* fenceFd) {
181 return window->dequeueBuffer(window, buffer, fenceFd);
184 int ANativeWindow_queueBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer, int fenceFd) {
185 return window->queueBuffer(window, buffer, fenceFd);
188 int ANativeWindow_cancelBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer, int fenceFd) {
189 return window->cancelBuffer(window, buffer, fenceFd);
192 int ANativeWindow_setUsage(ANativeWindow* window, uint64_t usage) {
193 return native_window_set_usage(window, usage);
196 int ANativeWindow_setBufferCount(ANativeWindow* window, size_t bufferCount) {
197 return native_window_set_buffer_count(window, bufferCount);
200 int ANativeWindow_setBuffersDimensions(ANativeWindow* window, uint32_t w, uint32_t h) {
201 return native_window_set_buffers_dimensions(window, (int)w, (int)h);
204 int ANativeWindow_setBuffersFormat(ANativeWindow* window, int format) {
205 return native_window_set_buffers_format(window, format);
208 int ANativeWindow_setBuffersTimestamp(ANativeWindow* window, int64_t timestamp) {
209 return native_window_set_buffers_timestamp(window, timestamp);
212 int ANativeWindow_setBufferDataSpace(ANativeWindow* window, android_dataspace_t dataSpace) {
213 return native_window_set_buffers_data_space(window, dataSpace);
216 int ANativeWindow_setSharedBufferMode(ANativeWindow* window, bool sharedBufferMode) {
217 return native_window_set_shared_buffer_mode(window, sharedBufferMode);
220 int ANativeWindow_setAutoRefresh(ANativeWindow* window, bool autoRefresh) {
221 return native_window_set_auto_refresh(window, autoRefresh);