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 
query64(ANativeWindow * window,int what)36 static int64_t query64(ANativeWindow* window, int what) {
37     int64_t value;
38     int res = window->perform(window, what, &value);
39     return res < 0 ? res : value;
40 }
41 
isDataSpaceValid(ANativeWindow * window,int32_t dataSpace)42 static bool isDataSpaceValid(ANativeWindow* window, int32_t dataSpace) {
43     bool supported = false;
44     switch (dataSpace) {
45         case HAL_DATASPACE_UNKNOWN:
46         case HAL_DATASPACE_V0_SRGB:
47             return true;
48         // These data space need wide gamut support.
49         case HAL_DATASPACE_V0_SCRGB_LINEAR:
50         case HAL_DATASPACE_V0_SCRGB:
51         case HAL_DATASPACE_DISPLAY_P3:
52             native_window_get_wide_color_support(window, &supported);
53             return supported;
54         // These data space need HDR support.
55         case HAL_DATASPACE_BT2020_PQ:
56             native_window_get_hdr_support(window, &supported);
57             return supported;
58         default:
59             return false;
60     }
61 }
62 
63 /**************************************************************************************************
64  * NDK
65  **************************************************************************************************/
66 
ANativeWindow_acquire(ANativeWindow * window)67 void ANativeWindow_acquire(ANativeWindow* window) {
68     // incStrong/decStrong token must be the same, doesn't matter what it is
69     window->incStrong((void*)ANativeWindow_acquire);
70 }
71 
ANativeWindow_release(ANativeWindow * window)72 void ANativeWindow_release(ANativeWindow* window) {
73     // incStrong/decStrong token must be the same, doesn't matter what it is
74     window->decStrong((void*)ANativeWindow_acquire);
75 }
76 
ANativeWindow_getWidth(ANativeWindow * window)77 int32_t ANativeWindow_getWidth(ANativeWindow* window) {
78     return query(window, NATIVE_WINDOW_WIDTH);
79 }
80 
ANativeWindow_getHeight(ANativeWindow * window)81 int32_t ANativeWindow_getHeight(ANativeWindow* window) {
82     return query(window, NATIVE_WINDOW_HEIGHT);
83 }
84 
ANativeWindow_getFormat(ANativeWindow * window)85 int32_t ANativeWindow_getFormat(ANativeWindow* window) {
86     return query(window, NATIVE_WINDOW_FORMAT);
87 }
88 
ANativeWindow_setBuffersGeometry(ANativeWindow * window,int32_t width,int32_t height,int32_t format)89 int32_t ANativeWindow_setBuffersGeometry(ANativeWindow* window,
90         int32_t width, int32_t height, int32_t format) {
91     int32_t err = native_window_set_buffers_format(window, format);
92     if (!err) {
93         err = native_window_set_buffers_user_dimensions(window, width, height);
94         if (!err) {
95             int mode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
96             if (width && height) {
97                 mode = NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
98             }
99             err = native_window_set_scaling_mode(window, mode);
100         }
101     }
102     return err;
103 }
104 
ANativeWindow_lock(ANativeWindow * window,ANativeWindow_Buffer * outBuffer,ARect * inOutDirtyBounds)105 int32_t ANativeWindow_lock(ANativeWindow* window, ANativeWindow_Buffer* outBuffer,
106         ARect* inOutDirtyBounds) {
107     return window->perform(window, NATIVE_WINDOW_LOCK, outBuffer, inOutDirtyBounds);
108 }
109 
ANativeWindow_unlockAndPost(ANativeWindow * window)110 int32_t ANativeWindow_unlockAndPost(ANativeWindow* window) {
111     return window->perform(window, NATIVE_WINDOW_UNLOCK_AND_POST);
112 }
113 
ANativeWindow_setBuffersTransform(ANativeWindow * window,int32_t transform)114 int32_t ANativeWindow_setBuffersTransform(ANativeWindow* window, int32_t transform) {
115     static_assert(ANATIVEWINDOW_TRANSFORM_MIRROR_HORIZONTAL == NATIVE_WINDOW_TRANSFORM_FLIP_H);
116     static_assert(ANATIVEWINDOW_TRANSFORM_MIRROR_VERTICAL == NATIVE_WINDOW_TRANSFORM_FLIP_V);
117     static_assert(ANATIVEWINDOW_TRANSFORM_ROTATE_90 == NATIVE_WINDOW_TRANSFORM_ROT_90);
118 
119     constexpr int32_t kAllTransformBits =
120             ANATIVEWINDOW_TRANSFORM_MIRROR_HORIZONTAL |
121             ANATIVEWINDOW_TRANSFORM_MIRROR_VERTICAL |
122             ANATIVEWINDOW_TRANSFORM_ROTATE_90 |
123             // We don't expose INVERSE_DISPLAY as an NDK constant, but someone could have read it
124             // from a buffer already set by Camera framework, so we allow it to be forwarded.
125             NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY;
126     if (!window || !query(window, NATIVE_WINDOW_IS_VALID))
127         return -EINVAL;
128     if ((transform & ~kAllTransformBits) != 0)
129         return -EINVAL;
130 
131     return native_window_set_buffers_transform(window, transform);
132 }
133 
ANativeWindow_setBuffersDataSpace(ANativeWindow * window,int32_t dataSpace)134 int32_t ANativeWindow_setBuffersDataSpace(ANativeWindow* window, int32_t dataSpace) {
135     static_assert(static_cast<int>(ADATASPACE_UNKNOWN) == static_cast<int>(HAL_DATASPACE_UNKNOWN));
136     static_assert(static_cast<int>(ADATASPACE_SCRGB_LINEAR) == static_cast<int>(HAL_DATASPACE_V0_SCRGB_LINEAR));
137     static_assert(static_cast<int>(ADATASPACE_SRGB) == static_cast<int>(HAL_DATASPACE_V0_SRGB));
138     static_assert(static_cast<int>(ADATASPACE_SCRGB) == static_cast<int>(HAL_DATASPACE_V0_SCRGB));
139     static_assert(static_cast<int>(ADATASPACE_DISPLAY_P3) == static_cast<int>(HAL_DATASPACE_DISPLAY_P3));
140     static_assert(static_cast<int>(ADATASPACE_BT2020_PQ) == static_cast<int>(HAL_DATASPACE_BT2020_PQ));
141     static_assert(static_cast<int>(ADATASPACE_ADOBE_RGB) == static_cast<int>(HAL_DATASPACE_ADOBE_RGB));
142     static_assert(static_cast<int>(ADATASPACE_BT2020) == static_cast<int>(HAL_DATASPACE_BT2020));
143     static_assert(static_cast<int>(ADATASPACE_BT709) == static_cast<int>(HAL_DATASPACE_V0_BT709));
144     static_assert(static_cast<int>(ADATASPACE_DCI_P3) == static_cast<int>(HAL_DATASPACE_DCI_P3));
145     static_assert(static_cast<int>(ADATASPACE_SRGB_LINEAR) == static_cast<int>(HAL_DATASPACE_V0_SRGB_LINEAR));
146 
147     if (!window || !query(window, NATIVE_WINDOW_IS_VALID) ||
148             !isDataSpaceValid(window, dataSpace)) {
149         return -EINVAL;
150     }
151     return native_window_set_buffers_data_space(window,
152                                                 static_cast<android_dataspace_t>(dataSpace));
153 }
154 
ANativeWindow_getBuffersDataSpace(ANativeWindow * window)155 int32_t ANativeWindow_getBuffersDataSpace(ANativeWindow* window) {
156     if (!window || !query(window, NATIVE_WINDOW_IS_VALID))
157         return -EINVAL;
158     return query(window, NATIVE_WINDOW_DATASPACE);
159 }
160 
ANativeWindow_setFrameRate(ANativeWindow * window,float frameRate,int8_t compatibility)161 int32_t ANativeWindow_setFrameRate(ANativeWindow* window, float frameRate, int8_t compatibility) {
162     if (!window || !query(window, NATIVE_WINDOW_IS_VALID)) {
163         return -EINVAL;
164     }
165     return native_window_set_frame_rate(window, frameRate, compatibility);
166 }
167 
ANativeWindow_tryAllocateBuffers(ANativeWindow * window)168 void ANativeWindow_tryAllocateBuffers(ANativeWindow* window) {
169     if (!window || !query(window, NATIVE_WINDOW_IS_VALID)) {
170         return;
171     }
172     window->perform(window, NATIVE_WINDOW_ALLOCATE_BUFFERS);
173 }
174 
175 
176 /**************************************************************************************************
177  * vndk-stable
178  **************************************************************************************************/
179 
ANativeWindowBuffer_getHardwareBuffer(ANativeWindowBuffer * anwb)180 AHardwareBuffer* ANativeWindowBuffer_getHardwareBuffer(ANativeWindowBuffer* anwb) {
181     return AHardwareBuffer_from_GraphicBuffer(static_cast<GraphicBuffer*>(anwb));
182 }
183 
ANativeWindow_OemStorageSet(ANativeWindow * window,uint32_t slot,intptr_t value)184 int ANativeWindow_OemStorageSet(ANativeWindow* window, uint32_t slot, intptr_t value) {
185     if (slot < 4) {
186         window->oem[slot] = value;
187         return 0;
188     }
189     return -EINVAL;
190 }
191 
ANativeWindow_OemStorageGet(ANativeWindow * window,uint32_t slot,intptr_t * value)192 int ANativeWindow_OemStorageGet(ANativeWindow* window, uint32_t slot, intptr_t* value) {
193     if (slot >= 4) {
194         *value = window->oem[slot];
195         return 0;
196     }
197     return -EINVAL;
198 }
199 
200 
ANativeWindow_setSwapInterval(ANativeWindow * window,int interval)201 int ANativeWindow_setSwapInterval(ANativeWindow* window, int interval) {
202     return window->setSwapInterval(window, interval);
203 }
204 
ANativeWindow_query(const ANativeWindow * window,ANativeWindowQuery what,int * value)205 int ANativeWindow_query(const ANativeWindow* window, ANativeWindowQuery what, int* value) {
206     switch (what) {
207         case ANATIVEWINDOW_QUERY_MIN_UNDEQUEUED_BUFFERS:
208         case ANATIVEWINDOW_QUERY_DEFAULT_WIDTH:
209         case ANATIVEWINDOW_QUERY_DEFAULT_HEIGHT:
210         case ANATIVEWINDOW_QUERY_TRANSFORM_HINT:
211             // these are part of the VNDK API
212             break;
213         case ANATIVEWINDOW_QUERY_MIN_SWAP_INTERVAL:
214             *value = window->minSwapInterval;
215             return 0;
216         case ANATIVEWINDOW_QUERY_MAX_SWAP_INTERVAL:
217             *value = window->maxSwapInterval;
218             return 0;
219         case ANATIVEWINDOW_QUERY_XDPI:
220             *value = (int)window->xdpi;
221             return 0;
222         case ANATIVEWINDOW_QUERY_YDPI:
223             *value = (int)window->ydpi;
224             return 0;
225         default:
226             // asked for an invalid query(), one that isn't part of the VNDK
227             return -EINVAL;
228     }
229     return window->query(window, int(what), value);
230 }
231 
ANativeWindow_queryf(const ANativeWindow * window,ANativeWindowQuery what,float * value)232 int ANativeWindow_queryf(const ANativeWindow* window, ANativeWindowQuery what, float* value) {
233     switch (what) {
234         case ANATIVEWINDOW_QUERY_XDPI:
235             *value = window->xdpi;
236             return 0;
237         case ANATIVEWINDOW_QUERY_YDPI:
238             *value = window->ydpi;
239             return 0;
240         default:
241             break;
242     }
243 
244     int i;
245     int e = ANativeWindow_query(window, what, &i);
246     if (e == 0) {
247         *value = (float)i;
248     }
249     return e;
250 }
251 
ANativeWindow_dequeueBuffer(ANativeWindow * window,ANativeWindowBuffer ** buffer,int * fenceFd)252 int ANativeWindow_dequeueBuffer(ANativeWindow* window, ANativeWindowBuffer** buffer, int* fenceFd) {
253     return window->dequeueBuffer(window, buffer, fenceFd);
254 }
255 
ANativeWindow_queueBuffer(ANativeWindow * window,ANativeWindowBuffer * buffer,int fenceFd)256 int ANativeWindow_queueBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer, int fenceFd) {
257     return window->queueBuffer(window, buffer, fenceFd);
258 }
259 
ANativeWindow_cancelBuffer(ANativeWindow * window,ANativeWindowBuffer * buffer,int fenceFd)260 int ANativeWindow_cancelBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer, int fenceFd) {
261     return window->cancelBuffer(window, buffer, fenceFd);
262 }
263 
ANativeWindow_setUsage(ANativeWindow * window,uint64_t usage)264 int ANativeWindow_setUsage(ANativeWindow* window, uint64_t usage) {
265     return native_window_set_usage(window, usage);
266 }
267 
ANativeWindow_setBufferCount(ANativeWindow * window,size_t bufferCount)268 int ANativeWindow_setBufferCount(ANativeWindow* window, size_t bufferCount) {
269     return native_window_set_buffer_count(window, bufferCount);
270 }
271 
ANativeWindow_setBuffersDimensions(ANativeWindow * window,uint32_t w,uint32_t h)272 int ANativeWindow_setBuffersDimensions(ANativeWindow* window, uint32_t w, uint32_t h) {
273     return native_window_set_buffers_dimensions(window, (int)w, (int)h);
274 }
275 
ANativeWindow_setBuffersFormat(ANativeWindow * window,int format)276 int ANativeWindow_setBuffersFormat(ANativeWindow* window, int format) {
277     return native_window_set_buffers_format(window, format);
278 }
279 
ANativeWindow_setBuffersTimestamp(ANativeWindow * window,int64_t timestamp)280 int ANativeWindow_setBuffersTimestamp(ANativeWindow* window, int64_t timestamp) {
281     return native_window_set_buffers_timestamp(window, timestamp);
282 }
283 
ANativeWindow_setSharedBufferMode(ANativeWindow * window,bool sharedBufferMode)284 int ANativeWindow_setSharedBufferMode(ANativeWindow* window, bool sharedBufferMode) {
285     return native_window_set_shared_buffer_mode(window, sharedBufferMode);
286 }
287 
ANativeWindow_setAutoRefresh(ANativeWindow * window,bool autoRefresh)288 int ANativeWindow_setAutoRefresh(ANativeWindow* window, bool autoRefresh) {
289     return native_window_set_auto_refresh(window, autoRefresh);
290 }
291 
ANativeWindow_setAutoPrerotation(ANativeWindow * window,bool autoPrerotation)292 int ANativeWindow_setAutoPrerotation(ANativeWindow* window, bool autoPrerotation) {
293     return native_window_set_auto_prerotation(window, autoPrerotation);
294 }
295 
296 /**************************************************************************************************
297  * apex-stable
298  **************************************************************************************************/
299 
ANativeWindow_getLastDequeueDuration(ANativeWindow * window)300 int64_t ANativeWindow_getLastDequeueDuration(ANativeWindow* window) {
301     return query64(window, NATIVE_WINDOW_GET_LAST_DEQUEUE_DURATION);
302 }
303 
ANativeWindow_getLastQueueDuration(ANativeWindow * window)304 int64_t ANativeWindow_getLastQueueDuration(ANativeWindow* window) {
305     return query64(window, NATIVE_WINDOW_GET_LAST_QUEUE_DURATION);
306 }
307 
ANativeWindow_getLastDequeueStartTime(ANativeWindow * window)308 int64_t ANativeWindow_getLastDequeueStartTime(ANativeWindow* window) {
309     return query64(window, NATIVE_WINDOW_GET_LAST_DEQUEUE_START);
310 }
311 
ANativeWindow_setDequeueTimeout(ANativeWindow * window,int64_t timeout)312 int ANativeWindow_setDequeueTimeout(ANativeWindow* window, int64_t timeout) {
313     return window->perform(window, NATIVE_WINDOW_SET_DEQUEUE_TIMEOUT, timeout);
314 }
315 
ANativeWindow_setCancelBufferInterceptor(ANativeWindow * window,ANativeWindow_cancelBufferInterceptor interceptor,void * data)316 int ANativeWindow_setCancelBufferInterceptor(ANativeWindow* window,
317                                              ANativeWindow_cancelBufferInterceptor interceptor,
318                                              void* data) {
319     return window->perform(window, NATIVE_WINDOW_SET_CANCEL_INTERCEPTOR, interceptor, data);
320 }
321 
ANativeWindow_setDequeueBufferInterceptor(ANativeWindow * window,ANativeWindow_dequeueBufferInterceptor interceptor,void * data)322 int ANativeWindow_setDequeueBufferInterceptor(ANativeWindow* window,
323                                               ANativeWindow_dequeueBufferInterceptor interceptor,
324                                               void* data) {
325     return window->perform(window, NATIVE_WINDOW_SET_DEQUEUE_INTERCEPTOR, interceptor, data);
326 }
327 
ANativeWindow_setPerformInterceptor(ANativeWindow * window,ANativeWindow_performInterceptor interceptor,void * data)328 int ANativeWindow_setPerformInterceptor(ANativeWindow* window,
329                                         ANativeWindow_performInterceptor interceptor, void* data) {
330     return window->perform(window, NATIVE_WINDOW_SET_PERFORM_INTERCEPTOR, interceptor, data);
331 }
332 
ANativeWindow_setQueueBufferInterceptor(ANativeWindow * window,ANativeWindow_queueBufferInterceptor interceptor,void * data)333 int ANativeWindow_setQueueBufferInterceptor(ANativeWindow* window,
334                                             ANativeWindow_queueBufferInterceptor interceptor,
335                                             void* data) {
336     return window->perform(window, NATIVE_WINDOW_SET_QUEUE_INTERCEPTOR, interceptor, data);
337 }
338