window.h revision 208ec5ec564597bdf8b478a424cc9ccc09547bac
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    NATIVE_WINDOW_SET_BUFFERS_DIMENSIONS,
169    NATIVE_WINDOW_SET_BUFFERS_FORMAT,
170};
171
172/* parameter for NATIVE_WINDOW_[DIS]CONNECT */
173enum {
174    NATIVE_WINDOW_API_EGL = 1
175};
176
177/* parameter for NATIVE_WINDOW_SET_BUFFERS_TRANSFORM */
178enum {
179    /* flip source image horizontally */
180    NATIVE_WINDOW_TRANSFORM_FLIP_H = HAL_TRANSFORM_FLIP_H ,
181    /* flip source image vertically */
182    NATIVE_WINDOW_TRANSFORM_FLIP_V = HAL_TRANSFORM_FLIP_V,
183    /* rotate source image 90 degrees clock-wise */
184    NATIVE_WINDOW_TRANSFORM_ROT_90 = HAL_TRANSFORM_ROT_90,
185    /* rotate source image 180 degrees */
186    NATIVE_WINDOW_TRANSFORM_ROT_180 = HAL_TRANSFORM_ROT_180,
187    /* rotate source image 270 degrees clock-wise */
188    NATIVE_WINDOW_TRANSFORM_ROT_270 = HAL_TRANSFORM_ROT_270,
189};
190
191/* values returned by the NATIVE_WINDOW_CONCRETE_TYPE query */
192enum {
193    NATIVE_WINDOW_FRAMEBUFFER,                  // FramebufferNativeWindow
194    NATIVE_WINDOW_SURFACE,                      // Surface
195    NATIVE_WINDOW_SURFACE_TEXTURE_CLIENT,       // SurfaceTextureClient
196};
197
198/* parameter for NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP
199 *
200 * Special timestamp value to indicate that timestamps should be auto-generated
201 * by the native window when queueBuffer is called.  This is equal to INT64_MIN,
202 * defined directly to avoid problems with C99/C++ inclusion of stdint.h.
203 */
204static const int64_t NATIVE_WINDOW_TIMESTAMP_AUTO = (-9223372036854775807LL-1);
205
206struct ANativeWindow
207{
208#ifdef __cplusplus
209    ANativeWindow()
210        : flags(0), minSwapInterval(0), maxSwapInterval(0), xdpi(0), ydpi(0)
211    {
212        common.magic = ANDROID_NATIVE_WINDOW_MAGIC;
213        common.version = sizeof(ANativeWindow);
214        memset(common.reserved, 0, sizeof(common.reserved));
215    }
216
217    // Implement the methods that sp<ANativeWindow> expects so that it
218    // can be used to automatically refcount ANativeWindow's.
219    void incStrong(const void* id) const {
220        common.incRef(const_cast<android_native_base_t*>(&common));
221    }
222    void decStrong(const void* id) const {
223        common.decRef(const_cast<android_native_base_t*>(&common));
224    }
225#endif
226
227    struct android_native_base_t common;
228
229    /* flags describing some attributes of this surface or its updater */
230    const uint32_t flags;
231
232    /* min swap interval supported by this updated */
233    const int   minSwapInterval;
234
235    /* max swap interval supported by this updated */
236    const int   maxSwapInterval;
237
238    /* horizontal and vertical resolution in DPI */
239    const float xdpi;
240    const float ydpi;
241
242    /* Some storage reserved for the OEM's driver. */
243    intptr_t    oem[4];
244
245    /*
246     * Set the swap interval for this surface.
247     *
248     * Returns 0 on success or -errno on error.
249     */
250    int     (*setSwapInterval)(struct ANativeWindow* window,
251                int interval);
252
253    /*
254     * hook called by EGL to acquire a buffer. After this call, the buffer
255     * is not locked, so its content cannot be modified.
256     * this call may block if no buffers are available.
257     *
258     * Returns 0 on success or -errno on error.
259     */
260    int     (*dequeueBuffer)(struct ANativeWindow* window,
261                struct ANativeWindowBuffer** buffer);
262
263    /*
264     * hook called by EGL to lock a buffer. This MUST be called before modifying
265     * the content of a buffer. The buffer must have been acquired with
266     * dequeueBuffer first.
267     *
268     * Returns 0 on success or -errno on error.
269     */
270    int     (*lockBuffer)(struct ANativeWindow* window,
271                struct ANativeWindowBuffer* buffer);
272   /*
273    * hook called by EGL when modifications to the render buffer are done.
274    * This unlocks and post the buffer.
275    *
276    * Buffers MUST be queued in the same order than they were dequeued.
277    *
278    * Returns 0 on success or -errno on error.
279    */
280    int     (*queueBuffer)(struct ANativeWindow* window,
281                struct ANativeWindowBuffer* buffer);
282
283    /*
284     * hook used to retrieve information about the native window.
285     *
286     * Returns 0 on success or -errno on error.
287     */
288    int     (*query)(const struct ANativeWindow* window,
289                int what, int* value);
290
291    /*
292     * hook used to perform various operations on the surface.
293     * (*perform)() is a generic mechanism to add functionality to
294     * ANativeWindow while keeping backward binary compatibility.
295     *
296     * DO NOT CALL THIS HOOK DIRECTLY.  Instead, use the helper functions
297     * defined below.
298     *
299     *  (*perform)() returns -ENOENT if the 'what' parameter is not supported
300     *  by the surface's implementation.
301     *
302     * The valid operations are:
303     *     NATIVE_WINDOW_SET_USAGE
304     *     NATIVE_WINDOW_CONNECT
305     *     NATIVE_WINDOW_DISCONNECT
306     *     NATIVE_WINDOW_SET_CROP
307     *     NATIVE_WINDOW_SET_BUFFER_COUNT
308     *     NATIVE_WINDOW_SET_BUFFERS_GEOMETRY
309     *     NATIVE_WINDOW_SET_BUFFERS_TRANSFORM
310     *     NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP
311     *     NATIVE_WINDOW_SET_BUFFERS_DIMENSIONS
312     *     NATIVE_WINDOW_SET_BUFFERS_FORMAT
313     *
314     */
315
316    int     (*perform)(struct ANativeWindow* window,
317                int operation, ... );
318
319    /*
320     * hook used to cancel a buffer that has been dequeued.
321     * No synchronization is performed between dequeue() and cancel(), so
322     * either external synchronization is needed, or these functions must be
323     * called from the same thread.
324     */
325    int     (*cancelBuffer)(struct ANativeWindow* window,
326                struct ANativeWindowBuffer* buffer);
327
328
329    void* reserved_proc[2];
330};
331
332 /* Backwards compatibility: use ANativeWindow (struct ANativeWindow in C).
333  * android_native_window_t is deprecated.
334  */
335typedef struct ANativeWindow ANativeWindow;
336typedef struct ANativeWindow android_native_window_t;
337
338/*
339 *  native_window_set_usage(..., usage)
340 *  Sets the intended usage flags for the next buffers
341 *  acquired with (*lockBuffer)() and on.
342 *  By default (if this function is never called), a usage of
343 *      GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE
344 *  is assumed.
345 *  Calling this function will usually cause following buffers to be
346 *  reallocated.
347 */
348
349static inline int native_window_set_usage(
350        struct ANativeWindow* window, int usage)
351{
352    return window->perform(window, NATIVE_WINDOW_SET_USAGE, usage);
353}
354
355/*
356 * native_window_connect(..., NATIVE_WINDOW_API_EGL)
357 * Must be called by EGL when the window is made current.
358 * Returns -EINVAL if for some reason the window cannot be connected, which
359 * can happen if it's connected to some other API.
360 */
361static inline int native_window_connect(
362        struct ANativeWindow* window, int api)
363{
364    return window->perform(window, NATIVE_WINDOW_CONNECT, api);
365}
366
367/*
368 * native_window_disconnect(..., NATIVE_WINDOW_API_EGL)
369 * Must be called by EGL when the window is made not current.
370 * An error is returned if for instance the window wasn't connected in the
371 * first place.
372 */
373static inline int native_window_disconnect(
374        struct ANativeWindow* window, int api)
375{
376    return window->perform(window, NATIVE_WINDOW_DISCONNECT, api);
377}
378
379/*
380 * native_window_set_crop(..., crop)
381 * Sets which region of the next queued buffers needs to be considered.
382 * A buffer's crop region is scaled to match the surface's size.
383 *
384 * The specified crop region applies to all buffers queued after it is called.
385 *
386 * if 'crop' is NULL, subsequently queued buffers won't be cropped.
387 *
388 * An error is returned if for instance the crop region is invalid,
389 * out of the buffer's bound or if the window is invalid.
390 */
391static inline int native_window_set_crop(
392        struct ANativeWindow* window,
393        android_native_rect_t const * crop)
394{
395    return window->perform(window, NATIVE_WINDOW_SET_CROP, crop);
396}
397
398/*
399 * native_window_set_buffer_count(..., count)
400 * Sets the number of buffers associated with this native window.
401 */
402static inline int native_window_set_buffer_count(
403        struct ANativeWindow* window,
404        size_t bufferCount)
405{
406    return window->perform(window, NATIVE_WINDOW_SET_BUFFER_COUNT, bufferCount);
407}
408
409/*
410 * native_window_set_buffers_geometry(..., int w, int h, int format)
411 * All buffers dequeued after this call will have the dimensions and format
412 * specified.  A successful call to this function has the same effect as calling
413 * native_window_set_buffers_size and native_window_set_buffers_format.
414 *
415 * XXX: This function is deprecated.  The native_window_set_buffers_dimensions
416 * and native_window_set_buffers_format functions should be used instead.
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_dimensions(..., int w, int h)
428 * All buffers dequeued after this call will have the dimensions specified.
429 * In particular, all buffers will have a fixed-size, independent form the
430 * native-window size. They will be appropriately scaled to the window-size
431 * upon window composition.
432 *
433 * If w and h are 0, the normal behavior is restored. That is, dequeued buffers
434 * following this call will be sized to match the window's size.
435 *
436 * Calling this function will reset the window crop to a NULL value, which
437 * disables cropping of the buffers.
438 */
439static inline int native_window_set_buffers_dimensions(
440        struct ANativeWindow* window,
441        int w, int h)
442{
443    return window->perform(window, NATIVE_WINDOW_SET_BUFFERS_DIMENSIONS,
444            w, h);
445}
446
447/*
448 * native_window_set_buffers_format(..., int format)
449 * All buffers dequeued after this call will have the format specified.
450 *
451 * If the specified format is 0, the default buffer format will be used.
452 */
453static inline int native_window_set_buffers_format(
454        struct ANativeWindow* window,
455        int format)
456{
457    return window->perform(window, NATIVE_WINDOW_SET_BUFFERS_FORMAT, format);
458}
459
460/*
461 * native_window_set_buffers_transform(..., int transform)
462 * All buffers queued after this call will be displayed transformed according
463 * to the transform parameter specified.
464 */
465static inline int native_window_set_buffers_transform(
466        struct ANativeWindow* window,
467        int transform)
468{
469    return window->perform(window, NATIVE_WINDOW_SET_BUFFERS_TRANSFORM,
470            transform);
471}
472
473/*
474 * native_window_set_buffers_timestamp(..., int64_t timestamp)
475 * All buffers queued after this call will be associated with the timestamp
476 * parameter specified. If the timestamp is set to NATIVE_WINDOW_TIMESTAMP_AUTO
477 * (the default), timestamps will be generated automatically when queueBuffer is
478 * called. The timestamp is measured in nanoseconds, and is normally monotonically
479 * increasing. The timestamp should be unaffected by time-of-day adjustments,
480 * and for a camera should be strictly monotonic but for a media player may be
481 * reset when the position is set.
482 */
483static inline int native_window_set_buffers_timestamp(
484        struct ANativeWindow* window,
485        int64_t timestamp)
486{
487    return window->perform(window, NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP,
488            timestamp);
489}
490
491__END_DECLS
492
493#endif /* SYSTEM_CORE_INCLUDE_ANDROID_WINDOW_H */
494