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