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  * @addtogroup NativeActivity Native Activity
19  * @{
20  */
21 
22 /**
23  * @file native_window.h
24  * @brief API for accessing a native window.
25  */
26 
27 #ifndef ANDROID_NATIVE_WINDOW_H
28 #define ANDROID_NATIVE_WINDOW_H
29 
30 #include <sys/cdefs.h>
31 
32 #include <android/data_space.h>
33 #include <android/hardware_buffer.h>
34 #include <android/rect.h>
35 
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39 
40 /**
41  * Legacy window pixel format names, kept for backwards compatibility.
42  * New code and APIs should use AHARDWAREBUFFER_FORMAT_*.
43  */
44 enum {
45     // NOTE: these values must match the values from graphics/common/x.x/types.hal
46 
47     /** Red: 8 bits, Green: 8 bits, Blue: 8 bits, Alpha: 8 bits. **/
48     WINDOW_FORMAT_RGBA_8888          = AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM,
49     /** Red: 8 bits, Green: 8 bits, Blue: 8 bits, Unused: 8 bits. **/
50     WINDOW_FORMAT_RGBX_8888          = AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM,
51     /** Red: 5 bits, Green: 6 bits, Blue: 5 bits. **/
52     WINDOW_FORMAT_RGB_565            = AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM,
53 };
54 
55 /**
56  * Transforms that can be applied to buffers as they are displayed to a window.
57  *
58  * Supported transforms are any combination of horizontal mirror, vertical
59  * mirror, and clockwise 90 degree rotation, in that order. Rotations of 180
60  * and 270 degrees are made up of those basic transforms.
61  */
62 enum ANativeWindowTransform {
63     ANATIVEWINDOW_TRANSFORM_IDENTITY            = 0x00,
64     ANATIVEWINDOW_TRANSFORM_MIRROR_HORIZONTAL   = 0x01,
65     ANATIVEWINDOW_TRANSFORM_MIRROR_VERTICAL     = 0x02,
66     ANATIVEWINDOW_TRANSFORM_ROTATE_90           = 0x04,
67 
68     ANATIVEWINDOW_TRANSFORM_ROTATE_180          = ANATIVEWINDOW_TRANSFORM_MIRROR_HORIZONTAL |
69                                                   ANATIVEWINDOW_TRANSFORM_MIRROR_VERTICAL,
70     ANATIVEWINDOW_TRANSFORM_ROTATE_270          = ANATIVEWINDOW_TRANSFORM_ROTATE_180 |
71                                                   ANATIVEWINDOW_TRANSFORM_ROTATE_90,
72 };
73 
74 struct ANativeWindow;
75 /**
76  * Opaque type that provides access to a native window.
77  *
78  * A pointer can be obtained using {@link ANativeWindow_fromSurface()}.
79  */
80 typedef struct ANativeWindow ANativeWindow;
81 
82 /**
83  * Struct that represents a windows buffer.
84  *
85  * A pointer can be obtained using {@link ANativeWindow_lock()}.
86  */
87 typedef struct ANativeWindow_Buffer {
88     /// The number of pixels that are shown horizontally.
89     int32_t width;
90 
91     /// The number of pixels that are shown vertically.
92     int32_t height;
93 
94     /// The number of *pixels* that a line in the buffer takes in
95     /// memory. This may be >= width.
96     int32_t stride;
97 
98     /// The format of the buffer. One of AHARDWAREBUFFER_FORMAT_*
99     int32_t format;
100 
101     /// The actual bits.
102     void* bits;
103 
104     /// Do not touch.
105     uint32_t reserved[6];
106 } ANativeWindow_Buffer;
107 
108 /**
109  * Acquire a reference on the given {@link ANativeWindow} object. This prevents the object
110  * from being deleted until the reference is removed.
111  */
112 void ANativeWindow_acquire(ANativeWindow* window);
113 
114 /**
115  * Remove a reference that was previously acquired with {@link ANativeWindow_acquire()}.
116  */
117 void ANativeWindow_release(ANativeWindow* window);
118 
119 /**
120  * Return the current width in pixels of the window surface.
121  *
122  * \return negative value on error.
123  */
124 int32_t ANativeWindow_getWidth(ANativeWindow* window);
125 
126 /**
127  * Return the current height in pixels of the window surface.
128  *
129  * \return a negative value on error.
130  */
131 int32_t ANativeWindow_getHeight(ANativeWindow* window);
132 
133 /**
134  * Return the current pixel format (AHARDWAREBUFFER_FORMAT_*) of the window surface.
135  *
136  * \return a negative value on error.
137  */
138 int32_t ANativeWindow_getFormat(ANativeWindow* window);
139 
140 /**
141  * Change the format and size of the window buffers.
142  *
143  * The width and height control the number of pixels in the buffers, not the
144  * dimensions of the window on screen. If these are different than the
145  * window's physical size, then its buffer will be scaled to match that size
146  * when compositing it to the screen. The width and height must be either both zero
147  * or both non-zero.
148  *
149  * For all of these parameters, if 0 is supplied then the window's base
150  * value will come back in force.
151  *
152  * \param width width of the buffers in pixels.
153  * \param height height of the buffers in pixels.
154  * \param format one of AHARDWAREBUFFER_FORMAT_* constants.
155  * \return 0 for success, or a negative value on error.
156  */
157 int32_t ANativeWindow_setBuffersGeometry(ANativeWindow* window,
158         int32_t width, int32_t height, int32_t format);
159 
160 /**
161  * Lock the window's next drawing surface for writing.
162  * inOutDirtyBounds is used as an in/out parameter, upon entering the
163  * function, it contains the dirty region, that is, the region the caller
164  * intends to redraw. When the function returns, inOutDirtyBounds is updated
165  * with the actual area the caller needs to redraw -- this region is often
166  * extended by {@link ANativeWindow_lock}.
167  *
168  * \return 0 for success, or a negative value on error.
169  */
170 int32_t ANativeWindow_lock(ANativeWindow* window, ANativeWindow_Buffer* outBuffer,
171         ARect* inOutDirtyBounds);
172 
173 /**
174  * Unlock the window's drawing surface after previously locking it,
175  * posting the new buffer to the display.
176  *
177  * \return 0 for success, or a negative value on error.
178  */
179 int32_t ANativeWindow_unlockAndPost(ANativeWindow* window);
180 
181 #if __ANDROID_API__ >= __ANDROID_API_O__
182 
183 /**
184  * Set a transform that will be applied to future buffers posted to the window.
185  *
186  * \param transform combination of {@link ANativeWindowTransform} flags
187  * \return 0 for success, or -EINVAL if \p transform is invalid
188  */
189 int32_t ANativeWindow_setBuffersTransform(ANativeWindow* window, int32_t transform);
190 
191 #endif // __ANDROID_API__ >= __ANDROID_API_O__
192 
193 #if __ANDROID_API__ >= __ANDROID_API_P__
194 
195 /**
196  * All buffers queued after this call will be associated with the dataSpace
197  * parameter specified.
198  *
199  * dataSpace specifies additional information about the buffer.
200  * For example, it can be used to convey the color space of the image data in
201  * the buffer, or it can be used to indicate that the buffers contain depth
202  * measurement data instead of color images. The default dataSpace is 0,
203  * ADATASPACE_UNKNOWN, unless it has been overridden by the producer.
204  *
205  * \param dataSpace data space of all buffers queued after this call.
206  * \return 0 for success, -EINVAL if window is invalid or the dataspace is not
207  * supported.
208  */
209 int32_t ANativeWindow_setBuffersDataSpace(ANativeWindow* window, int32_t dataSpace);
210 
211 /**
212  * Get the dataspace of the buffers in window.
213  * \return the dataspace of buffers in window, ADATASPACE_UNKNOWN is returned if
214  * dataspace is unknown, or -EINVAL if window is invalid.
215  */
216 int32_t ANativeWindow_getBuffersDataSpace(ANativeWindow* window);
217 
218 #endif // __ANDROID_API__ >= __ANDROID_API_P__
219 
220 #ifdef __cplusplus
221 };
222 #endif
223 
224 #endif // ANDROID_NATIVE_WINDOW_H
225 
226 /** @} */
227