1 /*
2  * Copyright (C) 2017 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 #ifndef ANDROID_VNDK_NATIVEWINDOW_ANATIVEWINDOW_H
18 #define ANDROID_VNDK_NATIVEWINDOW_ANATIVEWINDOW_H
19 
20 #include <nativebase/nativebase.h>
21 
22 // vndk is a superset of the NDK
23 #include <android/native_window.h>
24 
25 
26 __BEGIN_DECLS
27 
28 /*
29  * Convert this ANativeWindowBuffer into a AHardwareBuffer
30  */
31 AHardwareBuffer* ANativeWindowBuffer_getHardwareBuffer(ANativeWindowBuffer* anwb);
32 
33 /*****************************************************************************/
34 
35 /*
36  * Stores a value into one of the 4 available slots
37  * Retrieve the value with ANativeWindow_OemStorageGet()
38  *
39  * slot: 0 to 3
40  *
41  * Returns 0 on success or -errno on error.
42  */
43 int ANativeWindow_OemStorageSet(ANativeWindow* window, uint32_t slot, intptr_t value);
44 
45 
46 /*
47  * Retrieves a value from one of the 4 available slots
48  * By default the returned value is 0 if it wasn't set by ANativeWindow_OemStorageSet()
49  *
50  * slot: 0 to 3
51  *
52  * Returns 0 on success or -errno on error.
53  */
54 int ANativeWindow_OemStorageGet(ANativeWindow* window, uint32_t slot, intptr_t* value);
55 
56 
57 /*
58  * Set the swap interval for this surface.
59  *
60  * Returns 0 on success or -errno on error.
61  */
62 int ANativeWindow_setSwapInterval(ANativeWindow* window, int interval);
63 
64 
65 /*
66  * queries that can be used with ANativeWindow_query() and ANativeWindow_queryf()
67  */
68 enum ANativeWindowQuery {
69     /* The minimum number of buffers that must remain un-dequeued after a buffer
70      * has been queued.  This value applies only if set_buffer_count was used to
71      * override the number of buffers and if a buffer has since been queued.
72      * Users of the set_buffer_count ANativeWindow method should query this
73      * value before calling set_buffer_count.  If it is necessary to have N
74      * buffers simultaneously dequeued as part of the steady-state operation,
75      * and this query returns M then N+M buffers should be requested via
76      * native_window_set_buffer_count.
77      *
78      * Note that this value does NOT apply until a single buffer has been
79      * queued.  In particular this means that it is possible to:
80      *
81      * 1. Query M = min undequeued buffers
82      * 2. Set the buffer count to N + M
83      * 3. Dequeue all N + M buffers
84      * 4. Cancel M buffers
85      * 5. Queue, dequeue, queue, dequeue, ad infinitum
86      */
87     ANATIVEWINDOW_QUERY_MIN_UNDEQUEUED_BUFFERS = 3,
88 
89     /*
90      * Default width of ANativeWindow buffers, these are the
91      * dimensions of the window buffers irrespective of the
92      * ANativeWindow_setBuffersDimensions() call and match the native window
93      * size.
94      */
95     ANATIVEWINDOW_QUERY_DEFAULT_WIDTH = 6,
96     ANATIVEWINDOW_QUERY_DEFAULT_HEIGHT = 7,
97 
98     /*
99      * transformation that will most-likely be applied to buffers. This is only
100      * a hint, the actual transformation applied might be different.
101      *
102      * INTENDED USE:
103      *
104      * The transform hint can be used by a producer, for instance the GLES
105      * driver, to pre-rotate the rendering such that the final transformation
106      * in the composer is identity. This can be very useful when used in
107      * conjunction with the h/w composer HAL, in situations where it
108      * cannot handle arbitrary rotations.
109      *
110      * 1. Before dequeuing a buffer, the GL driver (or any other ANW client)
111      *    queries the ANW for NATIVE_WINDOW_TRANSFORM_HINT.
112      *
113      * 2. The GL driver overrides the width and height of the ANW to
114      *    account for NATIVE_WINDOW_TRANSFORM_HINT. This is done by querying
115      *    NATIVE_WINDOW_DEFAULT_{WIDTH | HEIGHT}, swapping the dimensions
116      *    according to NATIVE_WINDOW_TRANSFORM_HINT and calling
117      *    native_window_set_buffers_dimensions().
118      *
119      * 3. The GL driver dequeues a buffer of the new pre-rotated size.
120      *
121      * 4. The GL driver renders to the buffer such that the image is
122      *    already transformed, that is applying NATIVE_WINDOW_TRANSFORM_HINT
123      *    to the rendering.
124      *
125      * 5. The GL driver calls native_window_set_transform to apply
126      *    inverse transformation to the buffer it just rendered.
127      *    In order to do this, the GL driver needs
128      *    to calculate the inverse of NATIVE_WINDOW_TRANSFORM_HINT, this is
129      *    done easily:
130      *
131      *        int hintTransform, inverseTransform;
132      *        query(..., NATIVE_WINDOW_TRANSFORM_HINT, &hintTransform);
133      *        inverseTransform = hintTransform;
134      *        if (hintTransform & HAL_TRANSFORM_ROT_90)
135      *            inverseTransform ^= HAL_TRANSFORM_ROT_180;
136      *
137      *
138      * 6. The GL driver queues the pre-transformed buffer.
139      *
140      * 7. The composer combines the buffer transform with the display
141      *    transform.  If the buffer transform happens to cancel out the
142      *    display transform then no rotation is needed.
143      *
144      */
145     ANATIVEWINDOW_QUERY_TRANSFORM_HINT = 8,
146 
147     /*
148      * Returns the age of the contents of the most recently dequeued buffer as
149      * the number of frames that have elapsed since it was last queued. For
150      * example, if the window is double-buffered, the age of any given buffer in
151      * steady state will be 2. If the dequeued buffer has never been queued, its
152      * age will be 0.
153      */
154     ANATIVEWINDOW_QUERY_BUFFER_AGE = 13,
155 
156     /* min swap interval supported by this compositor */
157     ANATIVEWINDOW_QUERY_MIN_SWAP_INTERVAL = 0x10000,
158 
159     /* max swap interval supported by this compositor */
160     ANATIVEWINDOW_QUERY_MAX_SWAP_INTERVAL = 0x10001,
161 
162     /* horizontal resolution in DPI. value is float, use queryf() */
163     ANATIVEWINDOW_QUERY_XDPI = 0x10002,
164 
165     /* vertical resolution in DPI. value is float, use queryf() */
166     ANATIVEWINDOW_QUERY_YDPI = 0x10003,
167 };
168 
169 typedef enum ANativeWindowQuery ANativeWindowQuery;
170 
171 /*
172  * hook used to retrieve information about the native window.
173  *
174  * Returns 0 on success or -errno on error.
175  */
176 int ANativeWindow_query(const ANativeWindow* window, ANativeWindowQuery query, int* value);
177 int ANativeWindow_queryf(const ANativeWindow* window, ANativeWindowQuery query, float* value);
178 
179 
180 /*
181  * Hook called by EGL to acquire a buffer. This call may block if no
182  * buffers are available.
183  *
184  * The window holds a reference to the buffer between dequeueBuffer and
185  * either queueBuffer or cancelBuffer, so clients only need their own
186  * reference if they might use the buffer after queueing or canceling it.
187  * Holding a reference to a buffer after queueing or canceling it is only
188  * allowed if a specific buffer count has been set.
189  *
190  * The libsync fence file descriptor returned in the int pointed to by the
191  * fenceFd argument will refer to the fence that must signal before the
192  * dequeued buffer may be written to.  A value of -1 indicates that the
193  * caller may access the buffer immediately without waiting on a fence.  If
194  * a valid file descriptor is returned (i.e. any value except -1) then the
195  * caller is responsible for closing the file descriptor.
196  *
197  * Returns 0 on success or -errno on error.
198  */
199 int ANativeWindow_dequeueBuffer(ANativeWindow* window, ANativeWindowBuffer** buffer, int* fenceFd);
200 
201 
202 /*
203  * Hook called by EGL when modifications to the render buffer are done.
204  * This unlocks and post the buffer.
205  *
206  * The window holds a reference to the buffer between dequeueBuffer and
207  * either queueBuffer or cancelBuffer, so clients only need their own
208  * reference if they might use the buffer after queueing or canceling it.
209  * Holding a reference to a buffer after queueing or canceling it is only
210  * allowed if a specific buffer count has been set.
211  *
212  * The fenceFd argument specifies a libsync fence file descriptor for a
213  * fence that must signal before the buffer can be accessed.  If the buffer
214  * can be accessed immediately then a value of -1 should be used.  The
215  * caller must not use the file descriptor after it is passed to
216  * queueBuffer, and the ANativeWindow implementation is responsible for
217  * closing it.
218  *
219  * Returns 0 on success or -errno on error.
220  */
221 int ANativeWindow_queueBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer, int fenceFd);
222 
223 
224 /*
225  * Hook used to cancel a buffer that has been dequeued.
226  * No synchronization is performed between dequeue() and cancel(), so
227  * either external synchronization is needed, or these functions must be
228  * called from the same thread.
229  *
230  * The window holds a reference to the buffer between dequeueBuffer and
231  * either queueBuffer or cancelBuffer, so clients only need their own
232  * reference if they might use the buffer after queueing or canceling it.
233  * Holding a reference to a buffer after queueing or canceling it is only
234  * allowed if a specific buffer count has been set.
235  *
236  * The fenceFd argument specifies a libsync fence file decsriptor for a
237  * fence that must signal before the buffer can be accessed.  If the buffer
238  * can be accessed immediately then a value of -1 should be used.
239  *
240  * Note that if the client has not waited on the fence that was returned
241  * from dequeueBuffer, that same fence should be passed to cancelBuffer to
242  * ensure that future uses of the buffer are preceded by a wait on that
243  * fence.  The caller must not use the file descriptor after it is passed
244  * to cancelBuffer, and the ANativeWindow implementation is responsible for
245  * closing it.
246  *
247  * Returns 0 on success or -errno on error.
248  */
249 int ANativeWindow_cancelBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer, int fenceFd);
250 
251 /*
252  *  Sets the intended usage flags for the next buffers.
253  *
254  *  usage: one of AHARDWAREBUFFER_USAGE_* constant
255  *
256  *  By default (if this function is never called), a usage of
257  *      AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE | AHARDWAREBUFFER_USAGE_GPU_COLOR_OUTPUT
258  *  is assumed.
259  *
260  *  Calling this function will usually cause following buffers to be
261  *  reallocated.
262  */
263 int ANativeWindow_setUsage(ANativeWindow* window, uint64_t usage);
264 
265 
266 /*
267  * Sets the number of buffers associated with this native window.
268  */
269 int ANativeWindow_setBufferCount(ANativeWindow* window, size_t bufferCount);
270 
271 
272 /*
273  * All buffers dequeued after this call will have the dimensions specified.
274  * In particular, all buffers will have a fixed-size, independent from the
275  * native-window size. They will be scaled according to the scaling mode
276  * (see native_window_set_scaling_mode) upon window composition.
277  *
278  * If w and h are 0, the normal behavior is restored. That is, dequeued buffers
279  * following this call will be sized to match the window's size.
280  *
281  * Calling this function will reset the window crop to a NULL value, which
282  * disables cropping of the buffers.
283  */
284 int ANativeWindow_setBuffersDimensions(ANativeWindow* window, uint32_t w, uint32_t h);
285 
286 
287 /*
288  * All buffers dequeued after this call will have the format specified.
289  * format: one of AHARDWAREBUFFER_FORMAT_* constant
290  *
291  * If the specified format is 0, the default buffer format will be used.
292  */
293 int ANativeWindow_setBuffersFormat(ANativeWindow* window, int format);
294 
295 
296 /*
297  * All buffers queued after this call will be associated with the timestamp in nanosecond
298  * parameter specified. If the timestamp is set to NATIVE_WINDOW_TIMESTAMP_AUTO
299  * (the default), timestamps will be generated automatically when queueBuffer is
300  * called. The timestamp is measured in nanoseconds, and is normally monotonically
301  * increasing. The timestamp should be unaffected by time-of-day adjustments,
302  * and for a camera should be strictly monotonic but for a media player may be
303  * reset when the position is set.
304  */
305 int ANativeWindow_setBuffersTimestamp(ANativeWindow* window, int64_t timestamp);
306 
307 
308 /*
309  * Enable/disable shared buffer mode
310  */
311 int ANativeWindow_setSharedBufferMode(ANativeWindow* window, bool sharedBufferMode);
312 
313 
314 /*
315  * Enable/disable auto refresh when in shared buffer mode
316  */
317 int ANativeWindow_setAutoRefresh(ANativeWindow* window, bool autoRefresh);
318 
319 /*
320  * Enable/disable the auto prerotation at buffer allocation when the buffer size
321  * is driven by the consumer.
322  *
323  * When buffer size is driven by the consumer and the transform hint specifies
324  * a 90 or 270 degree rotation, if auto prerotation is enabled, the width and
325  * height used for dequeueBuffer will be additionally swapped.
326  */
327 int ANativeWindow_setAutoPrerotation(ANativeWindow* window, bool autoPrerotation);
328 
329 /*****************************************************************************/
330 
331 __END_DECLS
332 
333 #endif /* ANDROID_VNDK_NATIVEWINDOW_ANATIVEWINDOW_H */
334