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 
isDataSpaceValid(ANativeWindow * window,int32_t dataSpace)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 
57 /**************************************************************************************************
58  * NDK
59  **************************************************************************************************/
60 
ANativeWindow_acquire(ANativeWindow * window)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 
ANativeWindow_release(ANativeWindow * window)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 
ANativeWindow_getWidth(ANativeWindow * window)71 int32_t ANativeWindow_getWidth(ANativeWindow* window) {
72     return query(window, NATIVE_WINDOW_WIDTH);
73 }
74 
ANativeWindow_getHeight(ANativeWindow * window)75 int32_t ANativeWindow_getHeight(ANativeWindow* window) {
76     return query(window, NATIVE_WINDOW_HEIGHT);
77 }
78 
ANativeWindow_getFormat(ANativeWindow * window)79 int32_t ANativeWindow_getFormat(ANativeWindow* window) {
80     return query(window, NATIVE_WINDOW_FORMAT);
81 }
82 
ANativeWindow_setBuffersGeometry(ANativeWindow * window,int32_t width,int32_t height,int32_t format)83 int32_t ANativeWindow_setBuffersGeometry(ANativeWindow* window,
84         int32_t width, int32_t height, int32_t format) {
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 
ANativeWindow_lock(ANativeWindow * window,ANativeWindow_Buffer * outBuffer,ARect * inOutDirtyBounds)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 
ANativeWindow_unlockAndPost(ANativeWindow * window)104 int32_t ANativeWindow_unlockAndPost(ANativeWindow* window) {
105     return window->perform(window, NATIVE_WINDOW_UNLOCK_AND_POST);
106 }
107 
ANativeWindow_setBuffersTransform(ANativeWindow * window,int32_t transform)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 |
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;
120     if (!window || !query(window, NATIVE_WINDOW_IS_VALID))
121         return -EINVAL;
122     if ((transform & ~kAllTransformBits) != 0)
123         return -EINVAL;
124 
125     return native_window_set_buffers_transform(window, transform);
126 }
127 
ANativeWindow_setBuffersDataSpace(ANativeWindow * window,int32_t dataSpace)128 int32_t ANativeWindow_setBuffersDataSpace(ANativeWindow* window, int32_t dataSpace) {
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));
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 
ANativeWindow_getBuffersDataSpace(ANativeWindow * window)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 
150 /**************************************************************************************************
151  * vndk-stable
152  **************************************************************************************************/
153 
ANativeWindowBuffer_getHardwareBuffer(ANativeWindowBuffer * anwb)154 AHardwareBuffer* ANativeWindowBuffer_getHardwareBuffer(ANativeWindowBuffer* anwb) {
155     return AHardwareBuffer_from_GraphicBuffer(static_cast<GraphicBuffer*>(anwb));
156 }
157 
ANativeWindow_OemStorageSet(ANativeWindow * window,uint32_t slot,intptr_t value)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 
ANativeWindow_OemStorageGet(ANativeWindow * window,uint32_t slot,intptr_t * value)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 
ANativeWindow_setSwapInterval(ANativeWindow * window,int interval)175 int ANativeWindow_setSwapInterval(ANativeWindow* window, int interval) {
176     return window->setSwapInterval(window, interval);
177 }
178 
ANativeWindow_query(const ANativeWindow * window,ANativeWindowQuery what,int * value)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 
ANativeWindow_queryf(const ANativeWindow * window,ANativeWindowQuery what,float * value)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 
ANativeWindow_dequeueBuffer(ANativeWindow * window,ANativeWindowBuffer ** buffer,int * fenceFd)226 int ANativeWindow_dequeueBuffer(ANativeWindow* window, ANativeWindowBuffer** buffer, int* fenceFd) {
227     return window->dequeueBuffer(window, buffer, fenceFd);
228 }
229 
ANativeWindow_queueBuffer(ANativeWindow * window,ANativeWindowBuffer * buffer,int fenceFd)230 int ANativeWindow_queueBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer, int fenceFd) {
231     return window->queueBuffer(window, buffer, fenceFd);
232 }
233 
ANativeWindow_cancelBuffer(ANativeWindow * window,ANativeWindowBuffer * buffer,int fenceFd)234 int ANativeWindow_cancelBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer, int fenceFd) {
235     return window->cancelBuffer(window, buffer, fenceFd);
236 }
237 
ANativeWindow_setUsage(ANativeWindow * window,uint64_t usage)238 int ANativeWindow_setUsage(ANativeWindow* window, uint64_t usage) {
239     return native_window_set_usage(window, usage);
240 }
241 
ANativeWindow_setBufferCount(ANativeWindow * window,size_t bufferCount)242 int ANativeWindow_setBufferCount(ANativeWindow* window, size_t bufferCount) {
243     return native_window_set_buffer_count(window, bufferCount);
244 }
245 
ANativeWindow_setBuffersDimensions(ANativeWindow * window,uint32_t w,uint32_t h)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 
ANativeWindow_setBuffersFormat(ANativeWindow * window,int format)250 int ANativeWindow_setBuffersFormat(ANativeWindow* window, int format) {
251     return native_window_set_buffers_format(window, format);
252 }
253 
ANativeWindow_setBuffersTimestamp(ANativeWindow * window,int64_t timestamp)254 int ANativeWindow_setBuffersTimestamp(ANativeWindow* window, int64_t timestamp) {
255     return native_window_set_buffers_timestamp(window, timestamp);
256 }
257 
ANativeWindow_setSharedBufferMode(ANativeWindow * window,bool sharedBufferMode)258 int ANativeWindow_setSharedBufferMode(ANativeWindow* window, bool sharedBufferMode) {
259     return native_window_set_shared_buffer_mode(window, sharedBufferMode);
260 }
261 
ANativeWindow_setAutoRefresh(ANativeWindow * window,bool autoRefresh)262 int ANativeWindow_setAutoRefresh(ANativeWindow* window, bool autoRefresh) {
263     return native_window_set_auto_refresh(window, autoRefresh);
264 }
265