blob: a95612220c7a493e2c09973311ea2e033895e560 [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
21// from nativewindow/includes/system/window.h
22// (not to be confused with the compatibility-only window.h from system/core/includes)
Mathias Agopian89ed4c82017-02-09 18:48:34 -080023#include <system/window.h>
24
Mathias Agopian000879a2017-03-20 18:07:26 -070025#include <private/android/AHardwareBufferHelpers.h>
Mathias Agopian89ed4c82017-02-09 18:48:34 -080026
Mathias Agopian000879a2017-03-20 18:07:26 -070027using namespace android;
Mathias Agopian89ed4c82017-02-09 18:48:34 -080028
Mathias Agopian000879a2017-03-20 18:07:26 -070029static int32_t query(ANativeWindow* window, int what) {
Mathias Agopian89ed4c82017-02-09 18:48:34 -080030 int value;
31 int res = window->query(window, what, &value);
32 return res < 0 ? res : value;
33}
34
Mathias Agopian000879a2017-03-20 18:07:26 -070035/**************************************************************************************************
36 * NDK
37 **************************************************************************************************/
38
39void ANativeWindow_acquire(ANativeWindow* window) {
40 // incStrong/decStrong token must be the same, doesn't matter what it is
41 window->incStrong((void*)ANativeWindow_acquire);
42}
43
44void ANativeWindow_release(ANativeWindow* window) {
45 // incStrong/decStrong token must be the same, doesn't matter what it is
46 window->decStrong((void*)ANativeWindow_acquire);
47}
48
Mathias Agopian89ed4c82017-02-09 18:48:34 -080049int32_t ANativeWindow_getWidth(ANativeWindow* window) {
Mathias Agopian000879a2017-03-20 18:07:26 -070050 return query(window, NATIVE_WINDOW_WIDTH);
Mathias Agopian89ed4c82017-02-09 18:48:34 -080051}
52
53int32_t ANativeWindow_getHeight(ANativeWindow* window) {
Mathias Agopian000879a2017-03-20 18:07:26 -070054 return query(window, NATIVE_WINDOW_HEIGHT);
Mathias Agopian89ed4c82017-02-09 18:48:34 -080055}
56
57int32_t ANativeWindow_getFormat(ANativeWindow* window) {
Mathias Agopian000879a2017-03-20 18:07:26 -070058 return query(window, NATIVE_WINDOW_FORMAT);
Mathias Agopian89ed4c82017-02-09 18:48:34 -080059}
60
Mathias Agopian000879a2017-03-20 18:07:26 -070061int32_t ANativeWindow_setBuffersGeometry(ANativeWindow* window,
62 int32_t width, int32_t height, int32_t format) {
Mathias Agopian89ed4c82017-02-09 18:48:34 -080063 int32_t err = native_window_set_buffers_format(window, format);
64 if (!err) {
65 err = native_window_set_buffers_user_dimensions(window, width, height);
66 if (!err) {
67 int mode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
68 if (width && height) {
69 mode = NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
70 }
71 err = native_window_set_scaling_mode(window, mode);
72 }
73 }
74 return err;
75}
76
77int32_t ANativeWindow_lock(ANativeWindow* window, ANativeWindow_Buffer* outBuffer,
78 ARect* inOutDirtyBounds) {
79 return window->perform(window, NATIVE_WINDOW_LOCK, outBuffer, inOutDirtyBounds);
80}
81
82int32_t ANativeWindow_unlockAndPost(ANativeWindow* window) {
83 return window->perform(window, NATIVE_WINDOW_UNLOCK_AND_POST);
84}
Jesse Hall09932ec2017-03-13 11:36:05 -070085
86int32_t ANativeWindow_setBuffersTransform(ANativeWindow* window, int32_t transform) {
87 static_assert(ANATIVEWINDOW_TRANSFORM_MIRROR_HORIZONTAL == NATIVE_WINDOW_TRANSFORM_FLIP_H);
88 static_assert(ANATIVEWINDOW_TRANSFORM_MIRROR_VERTICAL == NATIVE_WINDOW_TRANSFORM_FLIP_V);
89 static_assert(ANATIVEWINDOW_TRANSFORM_ROTATE_90 == NATIVE_WINDOW_TRANSFORM_ROT_90);
90
91 constexpr int32_t kAllTransformBits =
92 ANATIVEWINDOW_TRANSFORM_MIRROR_HORIZONTAL |
93 ANATIVEWINDOW_TRANSFORM_MIRROR_VERTICAL |
94 ANATIVEWINDOW_TRANSFORM_ROTATE_90;
Mathias Agopian000879a2017-03-20 18:07:26 -070095 if (!window || !query(window, NATIVE_WINDOW_IS_VALID))
Jesse Hall09932ec2017-03-13 11:36:05 -070096 return -EINVAL;
97 if ((transform & ~kAllTransformBits) != 0)
98 return -EINVAL;
99
100 return native_window_set_buffers_transform(window, transform);
101}
Mathias Agopian000879a2017-03-20 18:07:26 -0700102
103/**************************************************************************************************
104 * vndk-stable
105 **************************************************************************************************/
106
107int ANativeWindow_OemStorageSet(ANativeWindow* window, uint32_t slot, intptr_t value) {
108 if (slot < 4) {
109 window->oem[slot] = value;
110 return 0;
111 }
112 return -EINVAL;
113}
114
115int ANativeWindow_OemStorageGet(ANativeWindow* window, uint32_t slot, intptr_t* value) {
116 if (slot >= 4) {
117 *value = window->oem[slot];
118 return 0;
119 }
120 return -EINVAL;
121}
122
123
124int ANativeWindow_setSwapInterval(ANativeWindow* window, int interval) {
125 return window->setSwapInterval(window, interval);
126}
127
128int ANativeWindow_query(const ANativeWindow* window, ANativeWindowQuery what, int* value) {
129 switch (what) {
130 case ANATIVEWINDOW_QUERY_MIN_UNDEQUEUED_BUFFERS:
131 case ANATIVEWINDOW_QUERY_DEFAULT_WIDTH:
132 case ANATIVEWINDOW_QUERY_DEFAULT_HEIGHT:
133 case ANATIVEWINDOW_QUERY_TRANSFORM_HINT:
134 // these are part of the VNDK API
135 break;
136 case ANATIVEWINDOW_QUERY_MIN_SWAP_INTERVAL:
137 *value = window->minSwapInterval;
138 return 0;
139 case ANATIVEWINDOW_QUERY_MAX_SWAP_INTERVAL:
140 *value = window->maxSwapInterval;
141 return 0;
142 case ANATIVEWINDOW_QUERY_XDPI:
143 *value = (int)window->xdpi;
144 return 0;
145 case ANATIVEWINDOW_QUERY_YDPI:
146 *value = (int)window->ydpi;
147 return 0;
148 default:
149 // asked for an invalid query(), one that isn't part of the VNDK
150 return -EINVAL;
151 }
152 return window->query(window, int(what), value);
153}
154
155int ANativeWindow_queryf(const ANativeWindow* window, ANativeWindowQuery what, float* value) {
156 switch (what) {
157 case ANATIVEWINDOW_QUERY_XDPI:
158 *value = window->xdpi;
159 return 0;
160 case ANATIVEWINDOW_QUERY_YDPI:
161 *value = window->ydpi;
162 return 0;
163 default:
164 break;
165 }
166
167 int i;
168 int e = ANativeWindow_query(window, what, &i);
169 if (e == 0) {
170 *value = (float)i;
171 }
172 return e;
173}
174
175int ANativeWindow_dequeueBuffer(ANativeWindow* window, ANativeWindowBuffer** buffer, int* fenceFd) {
176 return window->dequeueBuffer(window, buffer, fenceFd);
177}
178
179int ANativeWindow_queueBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer, int fenceFd) {
180 return window->queueBuffer(window, buffer, fenceFd);
181}
182
183int ANativeWindow_cancelBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer, int fenceFd) {
184 return window->cancelBuffer(window, buffer, fenceFd);
185}
186
187int ANativeWindow_setUsage(ANativeWindow* window, uint64_t usage0, uint64_t usage1) {
188 uint64_t pUsage, cUsage;
189 AHardwareBuffer_convertToGrallocUsageBits(&pUsage, &cUsage, usage0, usage1);
190 uint32_t gralloc_usage = uint32_t(pUsage | cUsage);
191 return native_window_set_usage(window, gralloc_usage);
192}
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}