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