blob: c0c4ac0f04cf87680ed406ac8a6c3293a8a26360 [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
19#include <android/native_window.h>
Mathias Agopian000879a2017-03-20 18:07:26 -070020
Jesse Hall79927812017-03-23 11:03:23 -070021#include <grallocusage/GrallocUsageConversion.h>
Mathias Agopian000879a2017-03-20 18:07:26 -070022// from nativewindow/includes/system/window.h
23// (not to be confused with the compatibility-only window.h from system/core/includes)
Mathias Agopian89ed4c82017-02-09 18:48:34 -080024#include <system/window.h>
25
Mathias Agopian000879a2017-03-20 18:07:26 -070026#include <private/android/AHardwareBufferHelpers.h>
Mathias Agopian89ed4c82017-02-09 18:48:34 -080027
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
108int ANativeWindow_OemStorageSet(ANativeWindow* window, uint32_t slot, intptr_t value) {
109 if (slot < 4) {
110 window->oem[slot] = value;
111 return 0;
112 }
113 return -EINVAL;
114}
115
116int ANativeWindow_OemStorageGet(ANativeWindow* window, uint32_t slot, intptr_t* value) {
117 if (slot >= 4) {
118 *value = window->oem[slot];
119 return 0;
120 }
121 return -EINVAL;
122}
123
124
125int ANativeWindow_setSwapInterval(ANativeWindow* window, int interval) {
126 return window->setSwapInterval(window, interval);
127}
128
129int ANativeWindow_query(const ANativeWindow* window, ANativeWindowQuery what, int* value) {
130 switch (what) {
131 case ANATIVEWINDOW_QUERY_MIN_UNDEQUEUED_BUFFERS:
132 case ANATIVEWINDOW_QUERY_DEFAULT_WIDTH:
133 case ANATIVEWINDOW_QUERY_DEFAULT_HEIGHT:
134 case ANATIVEWINDOW_QUERY_TRANSFORM_HINT:
135 // these are part of the VNDK API
136 break;
137 case ANATIVEWINDOW_QUERY_MIN_SWAP_INTERVAL:
138 *value = window->minSwapInterval;
139 return 0;
140 case ANATIVEWINDOW_QUERY_MAX_SWAP_INTERVAL:
141 *value = window->maxSwapInterval;
142 return 0;
143 case ANATIVEWINDOW_QUERY_XDPI:
144 *value = (int)window->xdpi;
145 return 0;
146 case ANATIVEWINDOW_QUERY_YDPI:
147 *value = (int)window->ydpi;
148 return 0;
149 default:
150 // asked for an invalid query(), one that isn't part of the VNDK
151 return -EINVAL;
152 }
153 return window->query(window, int(what), value);
154}
155
156int ANativeWindow_queryf(const ANativeWindow* window, ANativeWindowQuery what, float* value) {
157 switch (what) {
158 case ANATIVEWINDOW_QUERY_XDPI:
159 *value = window->xdpi;
160 return 0;
161 case ANATIVEWINDOW_QUERY_YDPI:
162 *value = window->ydpi;
163 return 0;
164 default:
165 break;
166 }
167
168 int i;
169 int e = ANativeWindow_query(window, what, &i);
170 if (e == 0) {
171 *value = (float)i;
172 }
173 return e;
174}
175
176int ANativeWindow_dequeueBuffer(ANativeWindow* window, ANativeWindowBuffer** buffer, int* fenceFd) {
177 return window->dequeueBuffer(window, buffer, fenceFd);
178}
179
180int ANativeWindow_queueBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer, int fenceFd) {
181 return window->queueBuffer(window, buffer, fenceFd);
182}
183
184int ANativeWindow_cancelBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer, int fenceFd) {
185 return window->cancelBuffer(window, buffer, fenceFd);
186}
187
188int ANativeWindow_setUsage(ANativeWindow* window, uint64_t usage0, uint64_t usage1) {
189 uint64_t pUsage, cUsage;
190 AHardwareBuffer_convertToGrallocUsageBits(&pUsage, &cUsage, usage0, usage1);
Jesse Hall79927812017-03-23 11:03:23 -0700191 return native_window_set_usage(window, android_convertGralloc1To0Usage(pUsage, cUsage));
Mathias Agopian000879a2017-03-20 18:07:26 -0700192}
193
194int ANativeWindow_setBufferCount(ANativeWindow* window, size_t bufferCount) {
195 return native_window_set_buffer_count(window, bufferCount);
196}
197
198int ANativeWindow_setBuffersDimensions(ANativeWindow* window, uint32_t w, uint32_t h) {
199 return native_window_set_buffers_dimensions(window, (int)w, (int)h);
200}
201
202int ANativeWindow_setBuffersFormat(ANativeWindow* window, int format) {
203 return native_window_set_buffers_format(window, format);
204}
205
206int ANativeWindow_setBuffersTimestamp(ANativeWindow* window, int64_t timestamp) {
207 return native_window_set_buffers_timestamp(window, timestamp);
208}
209
210int ANativeWindow_setBufferDataSpace(ANativeWindow* window, android_dataspace_t dataSpace) {
211 return native_window_set_buffers_data_space(window, dataSpace);
212}
213
214int ANativeWindow_setSharedBufferMode(ANativeWindow* window, bool sharedBufferMode) {
215 return native_window_set_shared_buffer_mode(window, sharedBufferMode);
216}
217
218int ANativeWindow_setAutoRefresh(ANativeWindow* window, bool autoRefresh) {
219 return native_window_set_auto_refresh(window, autoRefresh);
220}