window.h revision 0ab886bdabd167b32aad90ce5d4e00ce8392858c
1/*
2 * Copyright (C) 2011 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 SYSTEM_CORE_INCLUDE_ANDROID_WINDOW_H
18#define SYSTEM_CORE_INCLUDE_ANDROID_WINDOW_H
19
20#include <stdint.h>
21#include <sys/cdefs.h>
22#include <system/graphics.h>
23#include <cutils/native_handle.h>
24
25__BEGIN_DECLS
26
27/*****************************************************************************/
28
29#define ANDROID_NATIVE_MAKE_CONSTANT(a,b,c,d) \
30    (((unsigned)(a)<<24)|((unsigned)(b)<<16)|((unsigned)(c)<<8)|(unsigned)(d))
31
32#define ANDROID_NATIVE_WINDOW_MAGIC \
33    ANDROID_NATIVE_MAKE_CONSTANT('_','w','n','d')
34
35#define ANDROID_NATIVE_BUFFER_MAGIC \
36    ANDROID_NATIVE_MAKE_CONSTANT('_','b','f','r')
37
38// ---------------------------------------------------------------------------
39
40typedef const native_handle_t* buffer_handle_t;
41
42// ---------------------------------------------------------------------------
43
44typedef struct android_native_rect_t
45{
46    int32_t left;
47    int32_t top;
48    int32_t right;
49    int32_t bottom;
50} android_native_rect_t;
51
52// ---------------------------------------------------------------------------
53
54typedef struct android_native_base_t
55{
56    /* a magic value defined by the actual EGL native type */
57    int magic;
58
59    /* the sizeof() of the actual EGL native type */
60    int version;
61
62    void* reserved[4];
63
64    /* reference-counting interface */
65    void (*incRef)(struct android_native_base_t* base);
66    void (*decRef)(struct android_native_base_t* base);
67} android_native_base_t;
68
69typedef struct ANativeWindowBuffer
70{
71#ifdef __cplusplus
72    ANativeWindowBuffer() {
73        common.magic = ANDROID_NATIVE_BUFFER_MAGIC;
74        common.version = sizeof(ANativeWindowBuffer);
75        memset(common.reserved, 0, sizeof(common.reserved));
76    }
77
78    // Implement the methods that sp<ANativeWindowBuffer> expects so that it
79    // can be used to automatically refcount ANativeWindowBuffer's.
80    void incStrong(const void* id) const {
81        common.incRef(const_cast<android_native_base_t*>(&common));
82    }
83    void decStrong(const void* id) const {
84        common.decRef(const_cast<android_native_base_t*>(&common));
85    }
86#endif
87
88    struct android_native_base_t common;
89
90    int width;
91    int height;
92    int stride;
93    int format;
94    int usage;
95
96    void* reserved[2];
97
98    buffer_handle_t handle;
99
100    void* reserved_proc[8];
101} ANativeWindowBuffer_t;
102
103// Old typedef for backwards compatibility.
104typedef ANativeWindowBuffer_t android_native_buffer_t;
105
106// ---------------------------------------------------------------------------
107
108/* attributes queriable with query() */
109enum {
110    NATIVE_WINDOW_WIDTH     = 0,
111    NATIVE_WINDOW_HEIGHT,
112    NATIVE_WINDOW_FORMAT,
113
114    /* The minimum number of buffers that must remain un-dequeued after a buffer
115     * has been queued.  This value applies only if set_buffer_count was used to
116     * override the number of buffers and if a buffer has since been queued.
117     * Users of the set_buffer_count ANativeWindow method should query this
118     * value before calling set_buffer_count.  If it is necessary to have N
119     * buffers simultaneously dequeued as part of the steady-state operation,
120     * and this query returns M then N+M buffers should be requested via
121     * native_window_set_buffer_count.
122     *
123     * Note that this value does NOT apply until a single buffer has been
124     * queued.  In particular this means that it is possible to:
125     *
126     * 1. Query M = min undequeued buffers
127     * 2. Set the buffer count to N + M
128     * 3. Dequeue all N + M buffers
129     * 4. Cancel M buffers
130     * 5. Queue, dequeue, queue, dequeue, ad infinitum
131     */
132    NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS,
133
134    /* Check whether queueBuffer operations on the ANativeWindow send the buffer
135     * to the window compositor.  The query sets the returned 'value' argument
136     * to 1 if the ANativeWindow DOES send queued buffers directly to the window
137     * compositor and 0 if the buffers do not go directly to the window
138     * compositor.
139     *
140     * This can be used to determine whether protected buffer content should be
141     * sent to the ANativeWindow.  Note, however, that a result of 1 does NOT
142     * indicate that queued buffers will be protected from applications or users
143     * capturing their contents.  If that behavior is desired then some other
144     * mechanism (e.g. the GRALLOC_USAGE_PROTECTED flag) should be used in
145     * conjunction with this query.
146     */
147    NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER,
148
149    /* Get the concrete type of a ANativeWindow.  See below for the list of
150     * possible return values.
151     *
152     * This query should not be used outside the Android framework and will
153     * likely be removed in the near future.
154     */
155    NATIVE_WINDOW_CONCRETE_TYPE,
156};
157
158/* valid operations for the (*perform)() hook */
159enum {
160    NATIVE_WINDOW_SET_USAGE  = 0,
161    NATIVE_WINDOW_CONNECT,
162    NATIVE_WINDOW_DISCONNECT,
163    NATIVE_WINDOW_SET_CROP,
164    NATIVE_WINDOW_SET_BUFFER_COUNT,
165    NATIVE_WINDOW_SET_BUFFERS_GEOMETRY,
166    NATIVE_WINDOW_SET_BUFFERS_TRANSFORM,
167    NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP,
168};
169
170/* parameter for NATIVE_WINDOW_[DIS]CONNECT */
171enum {
172    NATIVE_WINDOW_API_EGL = 1
173};
174
175/* parameter for NATIVE_WINDOW_SET_BUFFERS_TRANSFORM */
176enum {
177    /* flip source image horizontally */
178    NATIVE_WINDOW_TRANSFORM_FLIP_H = HAL_TRANSFORM_FLIP_H ,
179    /* flip source image vertically */
180    NATIVE_WINDOW_TRANSFORM_FLIP_V = HAL_TRANSFORM_FLIP_V,
181    /* rotate source image 90 degrees clock-wise */
182    NATIVE_WINDOW_TRANSFORM_ROT_90 = HAL_TRANSFORM_ROT_90,
183    /* rotate source image 180 degrees */
184    NATIVE_WINDOW_TRANSFORM_ROT_180 = HAL_TRANSFORM_ROT_180,
185    /* rotate source image 270 degrees clock-wise */
186    NATIVE_WINDOW_TRANSFORM_ROT_270 = HAL_TRANSFORM_ROT_270,
187};
188
189/* values returned by the NATIVE_WINDOW_CONCRETE_TYPE query */
190enum {
191    NATIVE_WINDOW_FRAMEBUFFER,                  // FramebufferNativeWindow
192    NATIVE_WINDOW_SURFACE,                      // Surface
193    NATIVE_WINDOW_SURFACE_TEXTURE_CLIENT,       // SurfaceTextureClient
194};
195
196/* parameter for NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP
197 *
198 * Special timestamp value to indicate that timestamps should be auto-generated
199 * by the native window when queueBuffer is called.  This is equal to INT64_MIN,
200 * defined directly to avoid problems with C99/C++ inclusion of stdint.h.
201 */
202static const int64_t NATIVE_WINDOW_TIMESTAMP_AUTO = (-9223372036854775807LL-1);
203
204struct ANativeWindow
205{
206#ifdef __cplusplus
207    ANativeWindow()
208        : flags(0), minSwapInterval(0), maxSwapInterval(0), xdpi(0), ydpi(0)
209    {
210        common.magic = ANDROID_NATIVE_WINDOW_MAGIC;
211        common.version = sizeof(ANativeWindow);
212        memset(common.reserved, 0, sizeof(common.reserved));
213    }
214
215    // Implement the methods that sp<ANativeWindow> expects so that it
216    // can be used to automatically refcount ANativeWindow's.
217    void incStrong(const void* id) const {
218        common.incRef(const_cast<android_native_base_t*>(&common));
219    }
220    void decStrong(const void* id) const {
221        common.decRef(const_cast<android_native_base_t*>(&common));
222    }
223#endif
224
225    struct android_native_base_t common;
226
227    /* flags describing some attributes of this surface or its updater */
228    const uint32_t flags;
229
230    /* min swap interval supported by this updated */
231    const int   minSwapInterval;
232
233    /* max swap interval supported by this updated */
234    const int   maxSwapInterval;
235
236    /* horizontal and vertical resolution in DPI */
237    const float xdpi;
238    const float ydpi;
239
240    /* Some storage reserved for the OEM's driver. */
241    intptr_t    oem[4];
242
243    /*
244     * Set the swap interval for this surface.
245     *
246     * Returns 0 on success or -errno on error.
247     */
248    int     (*setSwapInterval)(struct ANativeWindow* window,
249                int interval);
250
251    /*
252     * hook called by EGL to acquire a buffer. After this call, the buffer
253     * is not locked, so its content cannot be modified.
254     * this call may block if no buffers are available.
255     *
256     * Returns 0 on success or -errno on error.
257     */
258    int     (*dequeueBuffer)(struct ANativeWindow* window,
259                struct ANativeWindowBuffer** buffer);
260
261    /*
262     * hook called by EGL to lock a buffer. This MUST be called before modifying
263     * the content of a buffer. The buffer must have been acquired with
264     * dequeueBuffer first.
265     *
266     * Returns 0 on success or -errno on error.
267     */
268    int     (*lockBuffer)(struct ANativeWindow* window,
269                struct ANativeWindowBuffer* buffer);
270   /*
271    * hook called by EGL when modifications to the render buffer are done.
272    * This unlocks and post the buffer.
273    *
274    * Buffers MUST be queued in the same order than they were dequeued.
275    *
276    * Returns 0 on success or -errno on error.
277    */
278    int     (*queueBuffer)(struct ANativeWindow* window,
279                struct ANativeWindowBuffer* buffer);
280
281    /*
282     * hook used to retrieve information about the native window.
283     *
284     * Returns 0 on success or -errno on error.
285     */
286    int     (*query)(const struct ANativeWindow* window,
287                int what, int* value);
288
289    /*
290     * hook used to perform various operations on the surface.
291     * (*perform)() is a generic mechanism to add functionality to
292     * ANativeWindow while keeping backward binary compatibility.
293     *
294     * DO NOT CALL THIS HOOK DIRECTLY.  Instead, use the helper functions
295     * defined below.
296     *
297     *  (*perform)() returns -ENOENT if the 'what' parameter is not supported
298     *  by the surface's implementation.
299     *
300     * The valid operations are:
301     *     NATIVE_WINDOW_SET_USAGE
302     *     NATIVE_WINDOW_CONNECT
303     *     NATIVE_WINDOW_DISCONNECT
304     *     NATIVE_WINDOW_SET_CROP
305     *     NATIVE_WINDOW_SET_BUFFER_COUNT
306     *     NATIVE_WINDOW_SET_BUFFERS_GEOMETRY
307     *     NATIVE_WINDOW_SET_BUFFERS_TRANSFORM
308     *     NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP
309     *
310     */
311
312    int     (*perform)(struct ANativeWindow* window,
313                int operation, ... );
314
315    /*
316     * hook used to cancel a buffer that has been dequeued.
317     * No synchronization is performed between dequeue() and cancel(), so
318     * either external synchronization is needed, or these functions must be
319     * called from the same thread.
320     */
321    int     (*cancelBuffer)(struct ANativeWindow* window,
322                struct ANativeWindowBuffer* buffer);
323
324
325    void* reserved_proc[2];
326};
327
328 /* Backwards compatibility: use ANativeWindow (struct ANativeWindow in C).
329  * android_native_window_t is deprecated.
330  */
331typedef struct ANativeWindow ANativeWindow;
332typedef struct ANativeWindow android_native_window_t;
333
334/*
335 *  native_window_set_usage(..., usage)
336 *  Sets the intended usage flags for the next buffers
337 *  acquired with (*lockBuffer)() and on.
338 *  By default (if this function is never called), a usage of
339 *      GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE
340 *  is assumed.
341 *  Calling this function will usually cause following buffers to be
342 *  reallocated.
343 */
344
345static inline int native_window_set_usage(
346        struct ANativeWindow* window, int usage)
347{
348    return window->perform(window, NATIVE_WINDOW_SET_USAGE, usage);
349}
350
351/*
352 * native_window_connect(..., NATIVE_WINDOW_API_EGL)
353 * Must be called by EGL when the window is made current.
354 * Returns -EINVAL if for some reason the window cannot be connected, which
355 * can happen if it's connected to some other API.
356 */
357static inline int native_window_connect(
358        struct ANativeWindow* window, int api)
359{
360    return window->perform(window, NATIVE_WINDOW_CONNECT, api);
361}
362
363/*
364 * native_window_disconnect(..., NATIVE_WINDOW_API_EGL)
365 * Must be called by EGL when the window is made not current.
366 * An error is returned if for instance the window wasn't connected in the
367 * first place.
368 */
369static inline int native_window_disconnect(
370        struct ANativeWindow* window, int api)
371{
372    return window->perform(window, NATIVE_WINDOW_DISCONNECT, api);
373}
374
375/*
376 * native_window_set_crop(..., crop)
377 * Sets which region of the next queued buffers needs to be considered.
378 * A buffer's crop region is scaled to match the surface's size.
379 *
380 * The specified crop region applies to all buffers queued after it is called.
381 *
382 * if 'crop' is NULL, subsequently queued buffers won't be cropped.
383 *
384 * An error is returned if for instance the crop region is invalid,
385 * out of the buffer's bound or if the window is invalid.
386 */
387static inline int native_window_set_crop(
388        struct ANativeWindow* window,
389        android_native_rect_t const * crop)
390{
391    return window->perform(window, NATIVE_WINDOW_SET_CROP, crop);
392}
393
394/*
395 * native_window_set_buffer_count(..., count)
396 * Sets the number of buffers associated with this native window.
397 */
398static inline int native_window_set_buffer_count(
399        struct ANativeWindow* window,
400        size_t bufferCount)
401{
402    return window->perform(window, NATIVE_WINDOW_SET_BUFFER_COUNT, bufferCount);
403}
404
405/*
406 * native_window_set_buffers_geometry(..., int w, int h, int format)
407 * All buffers dequeued after this call will have the geometry specified.
408 * In particular, all buffers will have a fixed-size, independent form the
409 * native-window size. They will be appropriately scaled to the window-size
410 * upon composition.
411 *
412 * If all parameters are 0, the normal behavior is restored. That is,
413 * dequeued buffers following this call will be sized to the window's size.
414 *
415 * Calling this function will reset the window crop to a NULL value, which
416 * disables cropping of the buffers.
417 */
418static inline int native_window_set_buffers_geometry(
419        struct ANativeWindow* window,
420        int w, int h, int format)
421{
422    return window->perform(window, NATIVE_WINDOW_SET_BUFFERS_GEOMETRY,
423            w, h, format);
424}
425
426/*
427 * native_window_set_buffers_transform(..., int transform)
428 * All buffers queued after this call will be displayed transformed according
429 * to the transform parameter specified.
430 */
431static inline int native_window_set_buffers_transform(
432        struct ANativeWindow* window,
433        int transform)
434{
435    return window->perform(window, NATIVE_WINDOW_SET_BUFFERS_TRANSFORM,
436            transform);
437}
438
439/*
440 * native_window_set_buffers_timestamp(..., int64_t timestamp)
441 * All buffers queued after this call will be associated with the timestamp
442 * parameter specified. If the timestamp is set to NATIVE_WINDOW_TIMESTAMP_AUTO
443 * (the default), timestamps will be generated automatically when queueBuffer is
444 * called. The timestamp is measured in nanoseconds, and must be monotonically
445 * increasing.
446 */
447static inline int native_window_set_buffers_timestamp(
448        struct ANativeWindow* window,
449        int64_t timestamp)
450{
451    return window->perform(window, NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP,
452            timestamp);
453}
454
455__END_DECLS
456
457#endif SYSTEM_CORE_INCLUDE_ANDROID_WINDOW_H
458