• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2018 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.#pragma once
14 #include <system/window.h>
15 
16 #include <android/data_space.h>
17 
18 #include "AndroidHostCommon.h"
19 #define LOG_TAG "ANativeWindow"
20 
21 #include <stdio.h>
22 
query(ANativeWindow * window,int what)23 static int32_t query(ANativeWindow* window, int what) {
24     int value;
25     int res = window->query(window, what, &value);
26     return res < 0 ? res : value;
27 }
28 
isDataSpaceValid(ANativeWindow * window,int32_t dataSpace)29 static bool isDataSpaceValid(ANativeWindow* window, int32_t dataSpace) {
30     bool supported = false;
31     switch (dataSpace) {
32         case HAL_DATASPACE_UNKNOWN:
33         case HAL_DATASPACE_V0_SRGB:
34             return true;
35         // These data space need wide gamut support.
36         case HAL_DATASPACE_V0_SCRGB_LINEAR:
37         case HAL_DATASPACE_V0_SCRGB:
38         case HAL_DATASPACE_DISPLAY_P3:
39             native_window_get_wide_color_support(window, &supported);
40             return supported;
41         // These data space need HDR support.
42         case HAL_DATASPACE_BT2020_PQ:
43             native_window_get_hdr_support(window, &supported);
44             return supported;
45         default:
46             return false;
47     }
48 }
49 /**************************************************************************************************
50  * NDK
51  **************************************************************************************************/
ANativeWindow_acquire(ANativeWindow * window)52 void ANativeWindow_acquire(ANativeWindow* window) {
53     // incStrong/decStrong token must be the same, doesn't matter what it is
54     window->incStrong((void*)ANativeWindow_acquire);
55 }
ANativeWindow_release(ANativeWindow * window)56 void ANativeWindow_release(ANativeWindow* window) {
57     // incStrong/decStrong token must be the same, doesn't matter what it is
58     window->decStrong((void*)ANativeWindow_acquire);
59 }
ANativeWindow_getWidth(ANativeWindow * window)60 int32_t ANativeWindow_getWidth(ANativeWindow* window) {
61     return query(window, NATIVE_WINDOW_WIDTH);
62 }
ANativeWindow_getHeight(ANativeWindow * window)63 int32_t ANativeWindow_getHeight(ANativeWindow* window) {
64     return query(window, NATIVE_WINDOW_HEIGHT);
65 }
ANativeWindow_getFormat(ANativeWindow * window)66 int32_t ANativeWindow_getFormat(ANativeWindow* window) {
67     return query(window, NATIVE_WINDOW_FORMAT);
68 }
ANativeWindow_setBuffersGeometry(ANativeWindow * window,int32_t width,int32_t height,int32_t format)69 int32_t ANativeWindow_setBuffersGeometry(ANativeWindow* window,
70         int32_t width, int32_t height, int32_t format) {
71     int32_t err = native_window_set_buffers_format(window, format);
72     if (!err) {
73         err = native_window_set_buffers_user_dimensions(window, width, height);
74         if (!err) {
75             int mode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
76             if (width && height) {
77                 mode = NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
78             }
79             err = native_window_set_scaling_mode(window, mode);
80         }
81     }
82     return err;
83 }
ANativeWindow_lock(ANativeWindow * window,ANativeWindow_Buffer * outBuffer,ARect * inOutDirtyBounds)84 int32_t ANativeWindow_lock(ANativeWindow* window, ANativeWindow_Buffer* outBuffer,
85         ARect* inOutDirtyBounds) {
86     return window->perform(window, NATIVE_WINDOW_LOCK, outBuffer, inOutDirtyBounds);
87 }
ANativeWindow_unlockAndPost(ANativeWindow * window)88 int32_t ANativeWindow_unlockAndPost(ANativeWindow* window) {
89     return window->perform(window, NATIVE_WINDOW_UNLOCK_AND_POST);
90 }
ANativeWindow_setBuffersTransform(ANativeWindow * window,int32_t transform)91 int32_t ANativeWindow_setBuffersTransform(ANativeWindow* window, int32_t transform) {
92     static_assert(ANATIVEWINDOW_TRANSFORM_MIRROR_HORIZONTAL == NATIVE_WINDOW_TRANSFORM_FLIP_H,
93                  "ANATIVEWINDOW_TRANSFORM_MIRROR_HORIZONTAL == NATIVE_WINDOW_TRANSFORM_FLIP_H");
94     static_assert(ANATIVEWINDOW_TRANSFORM_MIRROR_VERTICAL == NATIVE_WINDOW_TRANSFORM_FLIP_V,
95                  "ANATIVEWINDOW_TRANSFORM_MIRROR_VERTICAL == NATIVE_WINDOW_TRANSFORM_FLIP_V");
96     static_assert(ANATIVEWINDOW_TRANSFORM_ROTATE_90 == NATIVE_WINDOW_TRANSFORM_ROT_90,
97                  "ANATIVEWINDOW_TRANSFORM_ROTATE_90 == NATIVE_WINDOW_TRANSFORM_ROT_90");
98     constexpr int32_t kAllTransformBits =
99             ANATIVEWINDOW_TRANSFORM_MIRROR_HORIZONTAL |
100             ANATIVEWINDOW_TRANSFORM_MIRROR_VERTICAL |
101             ANATIVEWINDOW_TRANSFORM_ROTATE_90 |
102             // We don't expose INVERSE_DISPLAY as an NDK constant, but someone could have read it
103             // from a buffer already set by Camera framework, so we allow it to be forwarded.
104             NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY;
105     if (!window || !query(window, NATIVE_WINDOW_IS_VALID))
106         return -EINVAL;
107     if ((transform & ~kAllTransformBits) != 0)
108         return -EINVAL;
109     return native_window_set_buffers_transform(window, transform);
110 }
ANativeWindow_setBuffersDataSpace(ANativeWindow * window,int32_t dataSpace)111 int32_t ANativeWindow_setBuffersDataSpace(ANativeWindow* window, int32_t dataSpace) {
112     static_assert(static_cast<int>(ADATASPACE_UNKNOWN) == static_cast<int>(HAL_DATASPACE_UNKNOWN),
113                  "static_cast<int>(ADATASPACE_UNKNOWN) == static_cast<int>(HAL_DATASPACE_UNKNOWN)");
114     static_assert(static_cast<int>(ADATASPACE_SCRGB_LINEAR) == static_cast<int>(HAL_DATASPACE_V0_SCRGB_LINEAR),
115                  "static_cast<int>(ADATASPACE_SCRGB_LINEAR) == static_cast<int>(HAL_DATASPACE_V0_SCRGB_LINEAR");
116     static_assert(static_cast<int>(ADATASPACE_SRGB) == static_cast<int>(HAL_DATASPACE_V0_SRGB),
117                  "static_cast<int>(ADATASPACE_SRGB) == static_cast<int>(HAL_DATASPACE_V0_SRGB)");
118     static_assert(static_cast<int>(ADATASPACE_SCRGB) == static_cast<int>(HAL_DATASPACE_V0_SCRGB),
119                  "static_cast<int>(ADATASPACE_SCRGB) == static_cast<int>(HAL_DATASPACE_V0_SCRGB");
120     static_assert(static_cast<int>(ADATASPACE_DISPLAY_P3) == static_cast<int>(HAL_DATASPACE_DISPLAY_P3),
121                  "static_cast<int>(ADATASPACE_DISPLAY_P3) == static_cast<int>(HAL_DATASPACE_DISPLAY_P3)");
122     static_assert(static_cast<int>(ADATASPACE_BT2020_PQ) == static_cast<int>(HAL_DATASPACE_BT2020_PQ),
123                  "static_cast<int>(ADATASPACE_BT2020_PQ) == static_cast<int>(HAL_DATASPACE_BT2020_PQ)");
124     if (!window || !query(window, NATIVE_WINDOW_IS_VALID) ||
125             !isDataSpaceValid(window, dataSpace)) {
126         return -EINVAL;
127     }
128     return native_window_set_buffers_data_space(window,
129                                                 static_cast<android_dataspace_t>(dataSpace));
130 }
ANativeWindow_getBuffersDataSpace(ANativeWindow * window)131 int32_t ANativeWindow_getBuffersDataSpace(ANativeWindow* window) {
132     if (!window || !query(window, NATIVE_WINDOW_IS_VALID))
133         return -EINVAL;
134     return query(window, NATIVE_WINDOW_DATASPACE);
135 }
136 /**************************************************************************************************
137  * vndk-stable
138  **************************************************************************************************/
ANativeWindowBuffer_getHardwareBuffer(ANativeWindowBuffer * anwb)139 AHardwareBuffer* ANativeWindowBuffer_getHardwareBuffer(ANativeWindowBuffer* anwb) {
140     fprintf(stderr, "%s: Not implemented!\n", __func__);
141     return NULL;
142 }
ANativeWindow_setSwapInterval(ANativeWindow * window,int interval)143 int ANativeWindow_setSwapInterval(ANativeWindow* window, int interval) {
144     return window->setSwapInterval(window, interval);
145 }
ANativeWindow_query(const ANativeWindow * window,ANativeWindowQuery what,int * value)146 int ANativeWindow_query(const ANativeWindow* window, ANativeWindowQuery what, int* value) {
147     switch (what) {
148         case ANATIVEWINDOW_QUERY_MIN_UNDEQUEUED_BUFFERS:
149         case ANATIVEWINDOW_QUERY_DEFAULT_WIDTH:
150         case ANATIVEWINDOW_QUERY_DEFAULT_HEIGHT:
151         case ANATIVEWINDOW_QUERY_TRANSFORM_HINT:
152             // these are part of the VNDK API
153             return window->query(window, int(what), value);
154         case ANATIVEWINDOW_QUERY_MIN_SWAP_INTERVAL:
155             *value = window->minSwapInterval;
156             return 0;
157         case ANATIVEWINDOW_QUERY_MAX_SWAP_INTERVAL:
158             *value = window->maxSwapInterval;
159             return 0;
160         case ANATIVEWINDOW_QUERY_XDPI:
161             *value = (int)window->xdpi;
162             return 0;
163         case ANATIVEWINDOW_QUERY_YDPI:
164             *value = (int)window->ydpi;
165             return 0;
166         default:
167             // asked for an invalid query(), one that isn't part of the VNDK
168             return -EINVAL;
169     }
170     return window->query(window, int(what), value);
171 }
ANativeWindow_queryf(const ANativeWindow * window,ANativeWindowQuery what,float * value)172 int ANativeWindow_queryf(const ANativeWindow* window, ANativeWindowQuery what, float* value) {
173     switch (what) {
174         case ANATIVEWINDOW_QUERY_XDPI:
175             *value = window->xdpi;
176             return 0;
177         case ANATIVEWINDOW_QUERY_YDPI:
178             *value = window->ydpi;
179             return 0;
180         default:
181             break;
182     }
183     int i;
184     int e = ANativeWindow_query(window, what, &i);
185     if (e == 0) {
186         *value = (float)i;
187     }
188     return e;
189 }
ANativeWindow_dequeueBuffer(ANativeWindow * window,ANativeWindowBuffer ** buffer,int * fenceFd)190 int ANativeWindow_dequeueBuffer(ANativeWindow* window, ANativeWindowBuffer** buffer, int* fenceFd) {
191     return window->dequeueBuffer(window, buffer, fenceFd);
192 }
ANativeWindow_queueBuffer(ANativeWindow * window,ANativeWindowBuffer * buffer,int fenceFd)193 int ANativeWindow_queueBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer, int fenceFd) {
194     return window->queueBuffer(window, buffer, fenceFd);
195 }
ANativeWindow_cancelBuffer(ANativeWindow * window,ANativeWindowBuffer * buffer,int fenceFd)196 int ANativeWindow_cancelBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer, int fenceFd) {
197     return window->cancelBuffer(window, buffer, fenceFd);
198 }
ANativeWindow_setUsage(ANativeWindow * window,uint64_t usage)199 int ANativeWindow_setUsage(ANativeWindow* window, uint64_t usage) {
200     return native_window_set_usage(window, usage);
201 }
ANativeWindow_setBufferCount(ANativeWindow * window,size_t bufferCount)202 int ANativeWindow_setBufferCount(ANativeWindow* window, size_t bufferCount) {
203     return native_window_set_buffer_count(window, bufferCount);
204 }
ANativeWindow_setBuffersDimensions(ANativeWindow * window,uint32_t w,uint32_t h)205 int ANativeWindow_setBuffersDimensions(ANativeWindow* window, uint32_t w, uint32_t h) {
206     return native_window_set_buffers_dimensions(window, (int)w, (int)h);
207 }
ANativeWindow_setBuffersFormat(ANativeWindow * window,int format)208 int ANativeWindow_setBuffersFormat(ANativeWindow* window, int format) {
209     return native_window_set_buffers_format(window, format);
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 }
ANativeWindow_setSharedBufferMode(ANativeWindow * window,bool sharedBufferMode)214 int ANativeWindow_setSharedBufferMode(ANativeWindow* window, bool sharedBufferMode) {
215     return native_window_set_shared_buffer_mode(window, sharedBufferMode);
216 }
ANativeWindow_setAutoRefresh(ANativeWindow * window,bool autoRefresh)217 int ANativeWindow_setAutoRefresh(ANativeWindow* window, bool autoRefresh) {
218     return native_window_set_auto_refresh(window, autoRefresh);
219 }