gralloc.h revision 2861789f4febe67e39de449488681edf3e98b6e2
1/*
2 * Copyright (C) 2008 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
18#ifndef ANDROID_GRALLOC_INTERFACE_H
19#define ANDROID_GRALLOC_INTERFACE_H
20
21#include <system/window.h>
22#include <system/graphics.h>
23#include <hardware/hardware.h>
24
25#include <stdint.h>
26#include <sys/cdefs.h>
27#include <sys/types.h>
28
29#include <cutils/native_handle.h>
30
31#include <hardware/hardware.h>
32#include <hardware/fb.h>
33
34__BEGIN_DECLS
35
36/**
37 * Module versioning information for the Gralloc hardware module, based on
38 * gralloc_module_t.common.module_api_version.
39 *
40 * Version History:
41 *
42 * GRALLOC_MODULE_API_VERSION_0_1:
43 * Initial Gralloc hardware module API.
44 *
45 * GRALLOC_MODULE_API_VERSION_0_2:
46 * Add support for flexible YCbCr format with (*lock_ycbcr)() method.
47 *
48 * GRALLOC_MODULE_API_VERSION_0_3:
49 * Add support for fence passing to/from lock/unlock.
50 */
51
52#define GRALLOC_MODULE_API_VERSION_0_1  HARDWARE_MODULE_API_VERSION(0, 1)
53#define GRALLOC_MODULE_API_VERSION_0_2  HARDWARE_MODULE_API_VERSION(0, 2)
54#define GRALLOC_MODULE_API_VERSION_0_3  HARDWARE_MODULE_API_VERSION(0, 3)
55
56#define GRALLOC_DEVICE_API_VERSION_0_1  HARDWARE_DEVICE_API_VERSION(0, 1)
57
58/**
59 * The id of this module
60 */
61#define GRALLOC_HARDWARE_MODULE_ID "gralloc"
62
63/**
64 * Name of the graphics device to open
65 */
66
67#define GRALLOC_HARDWARE_GPU0 "gpu0"
68
69enum {
70    /* buffer is never read in software */
71    GRALLOC_USAGE_SW_READ_NEVER         = 0x00000000,
72    /* buffer is rarely read in software */
73    GRALLOC_USAGE_SW_READ_RARELY        = 0x00000002,
74    /* buffer is often read in software */
75    GRALLOC_USAGE_SW_READ_OFTEN         = 0x00000003,
76    /* mask for the software read values */
77    GRALLOC_USAGE_SW_READ_MASK          = 0x0000000F,
78
79    /* buffer is never written in software */
80    GRALLOC_USAGE_SW_WRITE_NEVER        = 0x00000000,
81    /* buffer is rarely written in software */
82    GRALLOC_USAGE_SW_WRITE_RARELY       = 0x00000020,
83    /* buffer is often written in software */
84    GRALLOC_USAGE_SW_WRITE_OFTEN        = 0x00000030,
85    /* mask for the software write values */
86    GRALLOC_USAGE_SW_WRITE_MASK         = 0x000000F0,
87
88    /* buffer will be used as an OpenGL ES texture */
89    GRALLOC_USAGE_HW_TEXTURE            = 0x00000100,
90    /* buffer will be used as an OpenGL ES render target */
91    GRALLOC_USAGE_HW_RENDER             = 0x00000200,
92    /* buffer will be used by the 2D hardware blitter */
93    GRALLOC_USAGE_HW_2D                 = 0x00000400,
94    /* buffer will be used by the HWComposer HAL module */
95    GRALLOC_USAGE_HW_COMPOSER           = 0x00000800,
96    /* buffer will be used with the framebuffer device */
97    GRALLOC_USAGE_HW_FB                 = 0x00001000,
98    /* buffer will be used with the HW video encoder */
99    GRALLOC_USAGE_HW_VIDEO_ENCODER      = 0x00010000,
100    /* buffer will be written by the HW camera pipeline */
101    GRALLOC_USAGE_HW_CAMERA_WRITE       = 0x00020000,
102    /* buffer will be read by the HW camera pipeline */
103    GRALLOC_USAGE_HW_CAMERA_READ        = 0x00040000,
104    /* buffer will be used as part of zero-shutter-lag queue */
105    GRALLOC_USAGE_HW_CAMERA_ZSL         = 0x00060000,
106    /* mask for the camera access values */
107    GRALLOC_USAGE_HW_CAMERA_MASK        = 0x00060000,
108    /* mask for the software usage bit-mask */
109    GRALLOC_USAGE_HW_MASK               = 0x00071F00,
110
111    /* buffer will be used as a RenderScript Allocation */
112    GRALLOC_USAGE_RENDERSCRIPT          = 0x00100000,
113
114    /* buffer should be displayed full-screen on an external display when
115     * possible
116     */
117    GRALLOC_USAGE_EXTERNAL_DISP         = 0x00002000,
118
119    /* Must have a hardware-protected path to external display sink for
120     * this buffer.  If a hardware-protected path is not available, then
121     * either don't composite only this buffer (preferred) to the
122     * external sink, or (less desirable) do not route the entire
123     * composition to the external sink.
124     */
125    GRALLOC_USAGE_PROTECTED             = 0x00004000,
126
127    /* implementation-specific private usage flags */
128    GRALLOC_USAGE_PRIVATE_0             = 0x10000000,
129    GRALLOC_USAGE_PRIVATE_1             = 0x20000000,
130    GRALLOC_USAGE_PRIVATE_2             = 0x40000000,
131    GRALLOC_USAGE_PRIVATE_3             = 0x80000000,
132    GRALLOC_USAGE_PRIVATE_MASK          = 0xF0000000,
133};
134
135/*****************************************************************************/
136
137/**
138 * Every hardware module must have a data structure named HAL_MODULE_INFO_SYM
139 * and the fields of this data structure must begin with hw_module_t
140 * followed by module specific information.
141 */
142typedef struct gralloc_module_t {
143    struct hw_module_t common;
144
145    /*
146     * (*registerBuffer)() must be called before a buffer_handle_t that has not
147     * been created with (*alloc_device_t::alloc)() can be used.
148     *
149     * This is intended to be used with buffer_handle_t's that have been
150     * received in this process through IPC.
151     *
152     * This function checks that the handle is indeed a valid one and prepares
153     * it for use with (*lock)() and (*unlock)().
154     *
155     * It is not necessary to call (*registerBuffer)() on a handle created
156     * with (*alloc_device_t::alloc)().
157     *
158     * returns an error if this buffer_handle_t is not valid.
159     */
160    int (*registerBuffer)(struct gralloc_module_t const* module,
161            buffer_handle_t handle);
162
163    /*
164     * (*unregisterBuffer)() is called once this handle is no longer needed in
165     * this process. After this call, it is an error to call (*lock)(),
166     * (*unlock)(), or (*registerBuffer)().
167     *
168     * This function doesn't close or free the handle itself; this is done
169     * by other means, usually through libcutils's native_handle_close() and
170     * native_handle_free().
171     *
172     * It is an error to call (*unregisterBuffer)() on a buffer that wasn't
173     * explicitly registered first.
174     */
175    int (*unregisterBuffer)(struct gralloc_module_t const* module,
176            buffer_handle_t handle);
177
178    /*
179     * The (*lock)() method is called before a buffer is accessed for the
180     * specified usage. This call may block, for instance if the h/w needs
181     * to finish rendering or if CPU caches need to be synchronized.
182     *
183     * The caller promises to modify only pixels in the area specified
184     * by (l,t,w,h).
185     *
186     * The content of the buffer outside of the specified area is NOT modified
187     * by this call.
188     *
189     * If usage specifies GRALLOC_USAGE_SW_*, vaddr is filled with the address
190     * of the buffer in virtual memory.
191     *
192     * Note calling (*lock)() on HAL_PIXEL_FORMAT_YCbCr_*_888 buffers will fail
193     * and return -EINVAL.  These buffers must be locked with (*lock_ycbcr)()
194     * instead.
195     *
196     * THREADING CONSIDERATIONS:
197     *
198     * It is legal for several different threads to lock a buffer from
199     * read access, none of the threads are blocked.
200     *
201     * However, locking a buffer simultaneously for write or read/write is
202     * undefined, but:
203     * - shall not result in termination of the process
204     * - shall not block the caller
205     * It is acceptable to return an error or to leave the buffer's content
206     * into an indeterminate state.
207     *
208     * If the buffer was created with a usage mask incompatible with the
209     * requested usage flags here, -EINVAL is returned.
210     *
211     */
212
213    int (*lock)(struct gralloc_module_t const* module,
214            buffer_handle_t handle, int usage,
215            int l, int t, int w, int h,
216            void** vaddr);
217
218
219    /*
220     * The (*unlock)() method must be called after all changes to the buffer
221     * are completed.
222     */
223
224    int (*unlock)(struct gralloc_module_t const* module,
225            buffer_handle_t handle);
226
227
228    /* reserved for future use */
229    int (*perform)(struct gralloc_module_t const* module,
230            int operation, ... );
231
232    /*
233     * The (*lock_ycbcr)() method is like the (*lock)() method, with the
234     * difference that it fills a struct ycbcr with a description of the buffer
235     * layout, and zeroes out the reserved fields.
236     *
237     * This will only work on buffers with HAL_PIXEL_FORMAT_YCbCr_*_888, and
238     * will return -EINVAL on any other buffer formats.
239     *
240     * Added in GRALLOC_MODULE_API_VERSION_0_2.
241     */
242
243    int (*lock_ycbcr)(struct gralloc_module_t const* module,
244            buffer_handle_t handle, int usage,
245            int l, int t, int w, int h,
246            struct android_ycbcr *ycbcr);
247
248    /*
249     * The (*lockAsync)() method is like the (*lock)() method except
250     * that the buffer's sync fence object is passed into the lock
251     * call instead of requiring the caller to wait for completion.
252     *
253     * The gralloc implementation takes ownership of the fenceFd and
254     * is responsible for closing it when no longer needed.
255     *
256     * Added in GRALLOC_MODULE_API_VERSION_0_3.
257     */
258    int (*lockAsync)(struct gralloc_module_t const* module,
259            buffer_handle_t handle, int usage,
260            int l, int t, int w, int h,
261            void** vaddr, int fenceFd);
262
263    /*
264     * The (*unlockAsync)() method is like the (*unlock)() method
265     * except that a buffer sync fence object is returned from the
266     * lock call, representing the completion of any pending work
267     * performed by the gralloc implementation.
268     *
269     * The caller takes ownership of the fenceFd and is responsible
270     * for closing it when no longer needed.
271     *
272     * Added in GRALLOC_MODULE_API_VERSION_0_3.
273     */
274    int (*unlockAsync)(struct gralloc_module_t const* module,
275            buffer_handle_t handle, int* fenceFd);
276
277    /*
278     * The (*lockAsync_ycbcr)() method is like the (*lock_ycbcr)()
279     * method except that the buffer's sync fence object is passed
280     * into the lock call instead of requiring the caller to wait for
281     * completion.
282     *
283     * The gralloc implementation takes ownership of the fenceFd and
284     * is responsible for closing it when no longer needed.
285     *
286     * Added in GRALLOC_MODULE_API_VERSION_0_3.
287     */
288    int (*lockAsync_ycbcr)(struct gralloc_module_t const* module,
289            buffer_handle_t handle, int usage,
290            int l, int t, int w, int h,
291            struct android_ycbcr *ycbcr, int fenceFd);
292
293    /* reserved for future use */
294    void* reserved_proc[3];
295} gralloc_module_t;
296
297/*****************************************************************************/
298
299/**
300 * Every device data structure must begin with hw_device_t
301 * followed by module specific public methods and attributes.
302 */
303
304typedef struct alloc_device_t {
305    struct hw_device_t common;
306
307    /*
308     * (*alloc)() Allocates a buffer in graphic memory with the requested
309     * parameters and returns a buffer_handle_t and the stride in pixels to
310     * allow the implementation to satisfy hardware constraints on the width
311     * of a pixmap (eg: it may have to be multiple of 8 pixels).
312     * The CALLER TAKES OWNERSHIP of the buffer_handle_t.
313     *
314     * If format is HAL_PIXEL_FORMAT_YCbCr_420_888, the returned stride must be
315     * 0, since the actual strides are available from the android_ycbcr
316     * structure.
317     *
318     * Returns 0 on success or -errno on error.
319     */
320
321    int (*alloc)(struct alloc_device_t* dev,
322            int w, int h, int format, int usage,
323            buffer_handle_t* handle, int* stride);
324
325    /*
326     * (*free)() Frees a previously allocated buffer.
327     * Behavior is undefined if the buffer is still mapped in any process,
328     * but shall not result in termination of the program or security breaches
329     * (allowing a process to get access to another process' buffers).
330     * THIS FUNCTION TAKES OWNERSHIP of the buffer_handle_t which becomes
331     * invalid after the call.
332     *
333     * Returns 0 on success or -errno on error.
334     */
335    int (*free)(struct alloc_device_t* dev,
336            buffer_handle_t handle);
337
338    /* This hook is OPTIONAL.
339     *
340     * If non NULL it will be caused by SurfaceFlinger on dumpsys
341     */
342    void (*dump)(struct alloc_device_t *dev, char *buff, int buff_len);
343
344    void* reserved_proc[7];
345} alloc_device_t;
346
347
348/** convenience API for opening and closing a supported device */
349
350static inline int gralloc_open(const struct hw_module_t* module,
351        struct alloc_device_t** device) {
352    return module->methods->open(module,
353            GRALLOC_HARDWARE_GPU0, (struct hw_device_t**)device);
354}
355
356static inline int gralloc_close(struct alloc_device_t* device) {
357    return device->common.close(&device->common);
358}
359
360__END_DECLS
361
362#endif  // ANDROID_GRALLOC_INTERFACE_H
363