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 /**
18  * @defgroup ANativeWindow Native Window
19  *
20  * ANativeWindow represents the producer end of an image queue.
21  * It is the C counterpart of the android.view.Surface object in Java,
22  * and can be converted both ways. Depending on the consumer, images
23  * submitted to ANativeWindow can be shown on the display or sent to
24  * other consumers, such as video encoders.
25  * @{
26  */
27 
28 /**
29  * @file native_window.h
30  * @brief API for accessing a native window.
31  */
32 
33 #ifndef ANDROID_NATIVE_WINDOW_H
34 #define ANDROID_NATIVE_WINDOW_H
35 
36 #include <stdint.h>
37 #include <sys/cdefs.h>
38 
39 #include <android/data_space.h>
40 #include <android/hardware_buffer.h>
41 #include <android/rect.h>
42 
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46 
47 /**
48  * Legacy window pixel format names, kept for backwards compatibility.
49  * New code and APIs should use AHARDWAREBUFFER_FORMAT_*.
50  */
51 enum ANativeWindow_LegacyFormat {
52     // NOTE: these values must match the values from graphics/common/x.x/types.hal
53 
54     /** Red: 8 bits, Green: 8 bits, Blue: 8 bits, Alpha: 8 bits. **/
55     WINDOW_FORMAT_RGBA_8888          = AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM,
56     /** Red: 8 bits, Green: 8 bits, Blue: 8 bits, Unused: 8 bits. **/
57     WINDOW_FORMAT_RGBX_8888          = AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM,
58     /** Red: 5 bits, Green: 6 bits, Blue: 5 bits. **/
59     WINDOW_FORMAT_RGB_565            = AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM,
60 };
61 
62 /**
63  * Transforms that can be applied to buffers as they are displayed to a window.
64  *
65  * Supported transforms are any combination of horizontal mirror, vertical
66  * mirror, and clockwise 90 degree rotation, in that order. Rotations of 180
67  * and 270 degrees are made up of those basic transforms.
68  */
69 enum ANativeWindowTransform {
70     ANATIVEWINDOW_TRANSFORM_IDENTITY            = 0x00,
71     ANATIVEWINDOW_TRANSFORM_MIRROR_HORIZONTAL   = 0x01,
72     ANATIVEWINDOW_TRANSFORM_MIRROR_VERTICAL     = 0x02,
73     ANATIVEWINDOW_TRANSFORM_ROTATE_90           = 0x04,
74 
75     ANATIVEWINDOW_TRANSFORM_ROTATE_180          = ANATIVEWINDOW_TRANSFORM_MIRROR_HORIZONTAL |
76                                                   ANATIVEWINDOW_TRANSFORM_MIRROR_VERTICAL,
77     ANATIVEWINDOW_TRANSFORM_ROTATE_270          = ANATIVEWINDOW_TRANSFORM_ROTATE_180 |
78                                                   ANATIVEWINDOW_TRANSFORM_ROTATE_90,
79 };
80 
81 struct ANativeWindow;
82 /**
83  * Opaque type that provides access to a native window.
84  *
85  * A pointer can be obtained using {@link ANativeWindow_fromSurface()}.
86  */
87 typedef struct ANativeWindow ANativeWindow;
88 
89 /**
90  * Struct that represents a windows buffer.
91  *
92  * A pointer can be obtained using {@link ANativeWindow_lock()}.
93  */
94 typedef struct ANativeWindow_Buffer {
95     /// The number of pixels that are shown horizontally.
96     int32_t width;
97 
98     /// The number of pixels that are shown vertically.
99     int32_t height;
100 
101     /// The number of *pixels* that a line in the buffer takes in
102     /// memory. This may be >= width.
103     int32_t stride;
104 
105     /// The format of the buffer. One of AHardwareBuffer_Format.
106     int32_t format;
107 
108     /// The actual bits.
109     void* bits;
110 
111     /// Do not touch.
112     uint32_t reserved[6];
113 } ANativeWindow_Buffer;
114 
115 /**
116  * Acquire a reference on the given {@link ANativeWindow} object. This prevents the object
117  * from being deleted until the reference is removed.
118  */
119 void ANativeWindow_acquire(ANativeWindow* window);
120 
121 /**
122  * Remove a reference that was previously acquired with {@link ANativeWindow_acquire()}.
123  */
124 void ANativeWindow_release(ANativeWindow* window);
125 
126 /**
127  * Return the current width in pixels of the window surface.
128  *
129  * \return negative value on error.
130  */
131 int32_t ANativeWindow_getWidth(ANativeWindow* window);
132 
133 /**
134  * Return the current height in pixels of the window surface.
135  *
136  * \return a negative value on error.
137  */
138 int32_t ANativeWindow_getHeight(ANativeWindow* window);
139 
140 /**
141  * Return the current pixel format (AHARDWAREBUFFER_FORMAT_*) of the window surface.
142  *
143  * \return a negative value on error.
144  */
145 int32_t ANativeWindow_getFormat(ANativeWindow* window);
146 
147 /**
148  * Change the format and size of the window buffers.
149  *
150  * The width and height control the number of pixels in the buffers, not the
151  * dimensions of the window on screen. If these are different than the
152  * window's physical size, then its buffer will be scaled to match that size
153  * when compositing it to the screen. The width and height must be either both zero
154  * or both non-zero.
155  *
156  * For all of these parameters, if 0 is supplied then the window's base
157  * value will come back in force.
158  *
159  * \param width width of the buffers in pixels.
160  * \param height height of the buffers in pixels.
161  * \param format one of the AHardwareBuffer_Format constants.
162  * \return 0 for success, or a negative value on error.
163  */
164 int32_t ANativeWindow_setBuffersGeometry(ANativeWindow* window,
165         int32_t width, int32_t height, int32_t format);
166 
167 /**
168  * Lock the window's next drawing surface for writing.
169  * inOutDirtyBounds is used as an in/out parameter, upon entering the
170  * function, it contains the dirty region, that is, the region the caller
171  * intends to redraw. When the function returns, inOutDirtyBounds is updated
172  * with the actual area the caller needs to redraw -- this region is often
173  * extended by {@link ANativeWindow_lock}.
174  *
175  * \return 0 for success, or a negative value on error.
176  */
177 int32_t ANativeWindow_lock(ANativeWindow* window, ANativeWindow_Buffer* outBuffer,
178         ARect* inOutDirtyBounds);
179 
180 /**
181  * Unlock the window's drawing surface after previously locking it,
182  * posting the new buffer to the display.
183  *
184  * \return 0 for success, or a negative value on error.
185  */
186 int32_t ANativeWindow_unlockAndPost(ANativeWindow* window);
187 
188 /**
189  * Set a transform that will be applied to future buffers posted to the window.
190  *
191  * Available since API level 26.
192  *
193  * \param transform combination of {@link ANativeWindowTransform} flags
194  * \return 0 for success, or -EINVAL if \p transform is invalid
195  */
196 int32_t ANativeWindow_setBuffersTransform(ANativeWindow* window, int32_t transform) __INTRODUCED_IN(26);
197 
198 /**
199  * All buffers queued after this call will be associated with the dataSpace
200  * parameter specified.
201  *
202  * dataSpace specifies additional information about the buffer.
203  * For example, it can be used to convey the color space of the image data in
204  * the buffer, or it can be used to indicate that the buffers contain depth
205  * measurement data instead of color images. The default dataSpace is 0,
206  * ADATASPACE_UNKNOWN, unless it has been overridden by the producer.
207  *
208  * Available since API level 28.
209  *
210  * \param dataSpace data space of all buffers queued after this call.
211  * \return 0 for success, -EINVAL if window is invalid or the dataspace is not
212  * supported.
213  */
214 int32_t ANativeWindow_setBuffersDataSpace(ANativeWindow* window, int32_t dataSpace) __INTRODUCED_IN(28);
215 
216 /**
217  * Get the dataspace of the buffers in window.
218  *
219  * Available since API level 28.
220  *
221  * \return the dataspace of buffers in window, ADATASPACE_UNKNOWN is returned if
222  * dataspace is unknown, or -EINVAL if window is invalid.
223  */
224 int32_t ANativeWindow_getBuffersDataSpace(ANativeWindow* window) __INTRODUCED_IN(28);
225 
226 /** Compatibility value for ANativeWindow_setFrameRate. */
227 enum ANativeWindow_FrameRateCompatibility {
228     /**
229      * There are no inherent restrictions on the frame rate of this window. When
230      * the system selects a frame rate other than what the app requested, the
231      * app will be able to run at the system frame rate without requiring pull
232      * down. This value should be used when displaying game content, UIs, and
233      * anything that isn't video.
234      */
235     ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT = 0,
236     /**
237      * This window is being used to display content with an inherently fixed
238      * frame rate, e.g.\ a video that has a specific frame rate. When the system
239      * selects a frame rate other than what the app requested, the app will need
240      * to do pull down or use some other technique to adapt to the system's
241      * frame rate. The user experience is likely to be worse (e.g. more frame
242      * stuttering) than it would be if the system had chosen the app's requested
243      * frame rate. This value should be used for video content.
244      */
245     ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_FIXED_SOURCE = 1
246 };
247 
248 /**
249  * Sets the intended frame rate for this window.
250  *
251  * On devices that are capable of running the display at different refresh
252  * rates, the system may choose a display refresh rate to better match this
253  * window's frame rate. Usage of this API won't introduce frame rate throttling,
254  * or affect other aspects of the application's frame production
255  * pipeline. However, because the system may change the display refresh rate,
256  * calls to this function may result in changes to Choreographer callback
257  * timings, and changes to the time interval at which the system releases
258  * buffers back to the application.
259  *
260  * Note that this only has an effect for windows presented on the display. If
261  * this ANativeWindow is consumed by something other than the system compositor,
262  * e.g. a media codec, this call has no effect.
263  *
264  * Available since API level 30.
265  *
266  * \param frameRate The intended frame rate of this window, in frames per
267  * second. 0 is a special value that indicates the app will accept the system's
268  * choice for the display frame rate, which is the default behavior if this
269  * function isn't called. The frameRate param does <em>not</em> need to be a
270  * valid refresh rate for this device's display - e.g., it's fine to pass 30fps
271  * to a device that can only run the display at 60fps.
272  *
273  * \param compatibility The frame rate compatibility of this window. The
274  * compatibility value may influence the system's choice of display refresh
275  * rate. See the ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_* values for more info.
276  *
277  * \return 0 for success, -EINVAL if the window, frame rate, or compatibility
278  * value are invalid.
279  */
280 int32_t ANativeWindow_setFrameRate(ANativeWindow* window, float frameRate, int8_t compatibility)
281         __INTRODUCED_IN(30);
282 
283 /**
284  * Provides a hint to the window that buffers should be preallocated ahead of
285  * time. Note that the window implementation is not guaranteed to preallocate
286  * any buffers, for instance if an implementation disallows allocation of new
287  * buffers, or if there is insufficient memory in the system to preallocate
288  * additional buffers
289  *
290  * Available since API level 30.
291  */
292 void ANativeWindow_tryAllocateBuffers(ANativeWindow* window);
293 
294 #ifdef __cplusplus
295 };
296 #endif
297 
298 #endif // ANDROID_NATIVE_WINDOW_H
299 
300 /** @} */
301