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 <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>
23
24 #include <private/android/AHardwareBufferHelpers.h>
25
26 #include <ui/GraphicBuffer.h>
27
28 using namespace android;
29
query(ANativeWindow * window,int what)30 static int32_t query(ANativeWindow* window, int what) {
31 int value;
32 int res = window->query(window, what, &value);
33 return res < 0 ? res : value;
34 }
35
36 /**************************************************************************************************
37 * NDK
38 **************************************************************************************************/
39
ANativeWindow_acquire(ANativeWindow * window)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);
43 }
44
ANativeWindow_release(ANativeWindow * window)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);
48 }
49
ANativeWindow_getWidth(ANativeWindow * window)50 int32_t ANativeWindow_getWidth(ANativeWindow* window) {
51 return query(window, NATIVE_WINDOW_WIDTH);
52 }
53
ANativeWindow_getHeight(ANativeWindow * window)54 int32_t ANativeWindow_getHeight(ANativeWindow* window) {
55 return query(window, NATIVE_WINDOW_HEIGHT);
56 }
57
ANativeWindow_getFormat(ANativeWindow * window)58 int32_t ANativeWindow_getFormat(ANativeWindow* window) {
59 return query(window, NATIVE_WINDOW_FORMAT);
60 }
61
ANativeWindow_setBuffersGeometry(ANativeWindow * window,int32_t width,int32_t height,int32_t 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);
65 if (!err) {
66 err = native_window_set_buffers_user_dimensions(window, width, height);
67 if (!err) {
68 int mode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
69 if (width && height) {
70 mode = NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
71 }
72 err = native_window_set_scaling_mode(window, mode);
73 }
74 }
75 return err;
76 }
77
ANativeWindow_lock(ANativeWindow * window,ANativeWindow_Buffer * outBuffer,ARect * inOutDirtyBounds)78 int32_t ANativeWindow_lock(ANativeWindow* window, ANativeWindow_Buffer* outBuffer,
79 ARect* inOutDirtyBounds) {
80 return window->perform(window, NATIVE_WINDOW_LOCK, outBuffer, inOutDirtyBounds);
81 }
82
ANativeWindow_unlockAndPost(ANativeWindow * window)83 int32_t ANativeWindow_unlockAndPost(ANativeWindow* window) {
84 return window->perform(window, NATIVE_WINDOW_UNLOCK_AND_POST);
85 }
86
ANativeWindow_setBuffersTransform(ANativeWindow * window,int32_t transform)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);
91
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))
97 return -EINVAL;
98 if ((transform & ~kAllTransformBits) != 0)
99 return -EINVAL;
100
101 return native_window_set_buffers_transform(window, transform);
102 }
103
104 /**************************************************************************************************
105 * vndk-stable
106 **************************************************************************************************/
107
ANativeWindowBuffer_getHardwareBuffer(ANativeWindowBuffer * anwb)108 AHardwareBuffer* ANativeWindowBuffer_getHardwareBuffer(ANativeWindowBuffer* anwb) {
109 return AHardwareBuffer_from_GraphicBuffer(static_cast<GraphicBuffer*>(anwb));
110 }
111
ANativeWindow_OemStorageSet(ANativeWindow * window,uint32_t slot,intptr_t value)112 int ANativeWindow_OemStorageSet(ANativeWindow* window, uint32_t slot, intptr_t value) {
113 if (slot < 4) {
114 window->oem[slot] = value;
115 return 0;
116 }
117 return -EINVAL;
118 }
119
ANativeWindow_OemStorageGet(ANativeWindow * window,uint32_t slot,intptr_t * value)120 int ANativeWindow_OemStorageGet(ANativeWindow* window, uint32_t slot, intptr_t* value) {
121 if (slot >= 4) {
122 *value = window->oem[slot];
123 return 0;
124 }
125 return -EINVAL;
126 }
127
128
ANativeWindow_setSwapInterval(ANativeWindow * window,int interval)129 int ANativeWindow_setSwapInterval(ANativeWindow* window, int interval) {
130 return window->setSwapInterval(window, interval);
131 }
132
ANativeWindow_query(const ANativeWindow * window,ANativeWindowQuery what,int * value)133 int ANativeWindow_query(const ANativeWindow* window, ANativeWindowQuery what, int* value) {
134 switch (what) {
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
140 break;
141 case ANATIVEWINDOW_QUERY_MIN_SWAP_INTERVAL:
142 *value = window->minSwapInterval;
143 return 0;
144 case ANATIVEWINDOW_QUERY_MAX_SWAP_INTERVAL:
145 *value = window->maxSwapInterval;
146 return 0;
147 case ANATIVEWINDOW_QUERY_XDPI:
148 *value = (int)window->xdpi;
149 return 0;
150 case ANATIVEWINDOW_QUERY_YDPI:
151 *value = (int)window->ydpi;
152 return 0;
153 default:
154 // asked for an invalid query(), one that isn't part of the VNDK
155 return -EINVAL;
156 }
157 return window->query(window, int(what), value);
158 }
159
ANativeWindow_queryf(const ANativeWindow * window,ANativeWindowQuery what,float * value)160 int ANativeWindow_queryf(const ANativeWindow* window, ANativeWindowQuery what, float* value) {
161 switch (what) {
162 case ANATIVEWINDOW_QUERY_XDPI:
163 *value = window->xdpi;
164 return 0;
165 case ANATIVEWINDOW_QUERY_YDPI:
166 *value = window->ydpi;
167 return 0;
168 default:
169 break;
170 }
171
172 int i;
173 int e = ANativeWindow_query(window, what, &i);
174 if (e == 0) {
175 *value = (float)i;
176 }
177 return e;
178 }
179
ANativeWindow_dequeueBuffer(ANativeWindow * window,ANativeWindowBuffer ** buffer,int * fenceFd)180 int ANativeWindow_dequeueBuffer(ANativeWindow* window, ANativeWindowBuffer** buffer, int* fenceFd) {
181 return window->dequeueBuffer(window, buffer, fenceFd);
182 }
183
ANativeWindow_queueBuffer(ANativeWindow * window,ANativeWindowBuffer * buffer,int fenceFd)184 int ANativeWindow_queueBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer, int fenceFd) {
185 return window->queueBuffer(window, buffer, fenceFd);
186 }
187
ANativeWindow_cancelBuffer(ANativeWindow * window,ANativeWindowBuffer * buffer,int fenceFd)188 int ANativeWindow_cancelBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer, int fenceFd) {
189 return window->cancelBuffer(window, buffer, fenceFd);
190 }
191
ANativeWindow_setUsage(ANativeWindow * window,uint64_t usage)192 int ANativeWindow_setUsage(ANativeWindow* window, uint64_t usage) {
193 return native_window_set_usage(window, usage);
194 }
195
ANativeWindow_setBufferCount(ANativeWindow * window,size_t bufferCount)196 int ANativeWindow_setBufferCount(ANativeWindow* window, size_t bufferCount) {
197 return native_window_set_buffer_count(window, bufferCount);
198 }
199
ANativeWindow_setBuffersDimensions(ANativeWindow * window,uint32_t w,uint32_t h)200 int ANativeWindow_setBuffersDimensions(ANativeWindow* window, uint32_t w, uint32_t h) {
201 return native_window_set_buffers_dimensions(window, (int)w, (int)h);
202 }
203
ANativeWindow_setBuffersFormat(ANativeWindow * window,int format)204 int ANativeWindow_setBuffersFormat(ANativeWindow* window, int format) {
205 return native_window_set_buffers_format(window, format);
206 }
207
ANativeWindow_setBuffersTimestamp(ANativeWindow * window,int64_t timestamp)208 int ANativeWindow_setBuffersTimestamp(ANativeWindow* window, int64_t timestamp) {
209 return native_window_set_buffers_timestamp(window, timestamp);
210 }
211
ANativeWindow_setBufferDataSpace(ANativeWindow * window,android_dataspace_t dataSpace)212 int ANativeWindow_setBufferDataSpace(ANativeWindow* window, android_dataspace_t dataSpace) {
213 return native_window_set_buffers_data_space(window, dataSpace);
214 }
215
ANativeWindow_setSharedBufferMode(ANativeWindow * window,bool sharedBufferMode)216 int ANativeWindow_setSharedBufferMode(ANativeWindow* window, bool sharedBufferMode) {
217 return native_window_set_shared_buffer_mode(window, sharedBufferMode);
218 }
219
ANativeWindow_setAutoRefresh(ANativeWindow * window,bool autoRefresh)220 int ANativeWindow_setAutoRefresh(ANativeWindow* window, bool autoRefresh) {
221 return native_window_set_auto_refresh(window, autoRefresh);
222 }
223