blob: c6994c30b41729e73ad312d21475cc12a0661e4a [file] [log] [blame]
Mathias Agopian89ed4c82017-02-09 18:48:34 -08001/*
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
Jesse Hall79927812017-03-23 11:03:23 -070019#include <grallocusage/GrallocUsageConversion.h>
Mathias Agopian000879a2017-03-20 18:07:26 -070020// from nativewindow/includes/system/window.h
21// (not to be confused with the compatibility-only window.h from system/core/includes)
Mathias Agopian89ed4c82017-02-09 18:48:34 -080022#include <system/window.h>
23
Mathias Agopian000879a2017-03-20 18:07:26 -070024#include <private/android/AHardwareBufferHelpers.h>
Mathias Agopian89ed4c82017-02-09 18:48:34 -080025
Mathias Agopian453effd2017-04-03 15:34:13 -070026#include <ui/GraphicBuffer.h>
27
Mathias Agopian000879a2017-03-20 18:07:26 -070028using namespace android;
Mathias Agopian89ed4c82017-02-09 18:48:34 -080029
Mathias Agopian000879a2017-03-20 18:07:26 -070030static int32_t query(ANativeWindow* window, int what) {
Mathias Agopian89ed4c82017-02-09 18:48:34 -080031 int value;
32 int res = window->query(window, what, &value);
33 return res < 0 ? res : value;
34}
35
Mathias Agopian000879a2017-03-20 18:07:26 -070036/**************************************************************************************************
37 * NDK
38 **************************************************************************************************/
39
40void 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
45void 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
Mathias Agopian89ed4c82017-02-09 18:48:34 -080050int32_t ANativeWindow_getWidth(ANativeWindow* window) {
Mathias Agopian000879a2017-03-20 18:07:26 -070051 return query(window, NATIVE_WINDOW_WIDTH);
Mathias Agopian89ed4c82017-02-09 18:48:34 -080052}
53
54int32_t ANativeWindow_getHeight(ANativeWindow* window) {
Mathias Agopian000879a2017-03-20 18:07:26 -070055 return query(window, NATIVE_WINDOW_HEIGHT);
Mathias Agopian89ed4c82017-02-09 18:48:34 -080056}
57
58int32_t ANativeWindow_getFormat(ANativeWindow* window) {
Mathias Agopian000879a2017-03-20 18:07:26 -070059 return query(window, NATIVE_WINDOW_FORMAT);
Mathias Agopian89ed4c82017-02-09 18:48:34 -080060}
61
Mathias Agopian000879a2017-03-20 18:07:26 -070062int32_t ANativeWindow_setBuffersGeometry(ANativeWindow* window,
63 int32_t width, int32_t height, int32_t format) {
Mathias Agopian89ed4c82017-02-09 18:48:34 -080064 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
78int32_t ANativeWindow_lock(ANativeWindow* window, ANativeWindow_Buffer* outBuffer,
79 ARect* inOutDirtyBounds) {
80 return window->perform(window, NATIVE_WINDOW_LOCK, outBuffer, inOutDirtyBounds);
81}
82
83int32_t ANativeWindow_unlockAndPost(ANativeWindow* window) {
84 return window->perform(window, NATIVE_WINDOW_UNLOCK_AND_POST);
85}
Jesse Hall09932ec2017-03-13 11:36:05 -070086
87int32_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;
Mathias Agopian000879a2017-03-20 18:07:26 -070096 if (!window || !query(window, NATIVE_WINDOW_IS_VALID))
Jesse Hall09932ec2017-03-13 11:36:05 -070097 return -EINVAL;
98 if ((transform & ~kAllTransformBits) != 0)
99 return -EINVAL;
100
101 return native_window_set_buffers_transform(window, transform);
102}
Mathias Agopian000879a2017-03-20 18:07:26 -0700103
104/**************************************************************************************************
105 * vndk-stable
106 **************************************************************************************************/
107
Mathias Agopian453effd2017-04-03 15:34:13 -0700108AHardwareBuffer* ANativeWindowBuffer_getHardwareBuffer(ANativeWindowBuffer* anwb) {
109 return AHardwareBuffer_from_GraphicBuffer(static_cast<GraphicBuffer*>(anwb));
110}
111
Mathias Agopian000879a2017-03-20 18:07:26 -0700112int 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
120int 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
129int ANativeWindow_setSwapInterval(ANativeWindow* window, int interval) {
130 return window->setSwapInterval(window, interval);
131}
132
133int 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
160int 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
180int ANativeWindow_dequeueBuffer(ANativeWindow* window, ANativeWindowBuffer** buffer, int* fenceFd) {
181 return window->dequeueBuffer(window, buffer, fenceFd);
182}
183
184int ANativeWindow_queueBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer, int fenceFd) {
185 return window->queueBuffer(window, buffer, fenceFd);
186}
187
188int ANativeWindow_cancelBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer, int fenceFd) {
189 return window->cancelBuffer(window, buffer, fenceFd);
190}
191
Mathias Agopian2c38b562017-04-20 16:35:39 -0700192int ANativeWindow_setUsage(ANativeWindow* window, uint64_t usage) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700193 return native_window_set_usage(window, usage);
Mathias Agopian000879a2017-03-20 18:07:26 -0700194}
195
196int ANativeWindow_setBufferCount(ANativeWindow* window, size_t bufferCount) {
197 return native_window_set_buffer_count(window, bufferCount);
198}
199
200int ANativeWindow_setBuffersDimensions(ANativeWindow* window, uint32_t w, uint32_t h) {
201 return native_window_set_buffers_dimensions(window, (int)w, (int)h);
202}
203
204int ANativeWindow_setBuffersFormat(ANativeWindow* window, int format) {
205 return native_window_set_buffers_format(window, format);
206}
207
208int ANativeWindow_setBuffersTimestamp(ANativeWindow* window, int64_t timestamp) {
209 return native_window_set_buffers_timestamp(window, timestamp);
210}
211
212int ANativeWindow_setBufferDataSpace(ANativeWindow* window, android_dataspace_t dataSpace) {
213 return native_window_set_buffers_data_space(window, dataSpace);
214}
215
216int ANativeWindow_setSharedBufferMode(ANativeWindow* window, bool sharedBufferMode) {
217 return native_window_set_shared_buffer_mode(window, sharedBufferMode);
218}
219
220int ANativeWindow_setAutoRefresh(ANativeWindow* window, bool autoRefresh) {
221 return native_window_set_auto_refresh(window, autoRefresh);
222}