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