gralloc.h revision 3b98ffb168119f657befb1e3204183401ef4fd4f
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 <hardware/hardware.h>
23
24#include <stdint.h>
25#include <sys/cdefs.h>
26#include <sys/types.h>
27
28#include <cutils/native_handle.h>
29
30#include <hardware/hardware.h>
31#include <hardware/fb.h>
32
33__BEGIN_DECLS
34
35#define GRALLOC_API_VERSION 1
36
37/**
38 * The id of this module
39 */
40#define GRALLOC_HARDWARE_MODULE_ID "gralloc"
41
42/**
43 * Name of the graphics device to open
44 */
45
46#define GRALLOC_HARDWARE_GPU0 "gpu0"
47
48enum {
49    /* buffer is never read in software */
50    GRALLOC_USAGE_SW_READ_NEVER         = 0x00000000,
51    /* buffer is rarely read in software */
52    GRALLOC_USAGE_SW_READ_RARELY        = 0x00000002,
53    /* buffer is often read in software */
54    GRALLOC_USAGE_SW_READ_OFTEN         = 0x00000003,
55    /* mask for the software read values */
56    GRALLOC_USAGE_SW_READ_MASK          = 0x0000000F,
57
58    /* buffer is never written in software */
59    GRALLOC_USAGE_SW_WRITE_NEVER        = 0x00000000,
60    /* buffer is never written in software */
61    GRALLOC_USAGE_SW_WRITE_RARELY       = 0x00000020,
62    /* buffer is never written in software */
63    GRALLOC_USAGE_SW_WRITE_OFTEN        = 0x00000030,
64    /* mask for the software write values */
65    GRALLOC_USAGE_SW_WRITE_MASK         = 0x000000F0,
66
67    /* buffer will be used as an OpenGL ES texture */
68    GRALLOC_USAGE_HW_TEXTURE            = 0x00000100,
69    /* buffer will be used as an OpenGL ES render target */
70    GRALLOC_USAGE_HW_RENDER             = 0x00000200,
71    /* buffer will be used by the 2D hardware blitter */
72    GRALLOC_USAGE_HW_2D                 = 0x00000400,
73    /* buffer will be used by the HWComposer HAL module */
74    GRALLOC_USAGE_HW_COMPOSER           = 0x00000800,
75    /* buffer will be used with the framebuffer device */
76    GRALLOC_USAGE_HW_FB                 = 0x00001000,
77    /* buffer will be used with the HW video encoder */
78    GRALLOC_USAGE_HW_VIDEO_ENCODER      = 0x00010000,
79    /* buffer will be written by the HW camera pipeline */
80    GRALLOC_USAGE_HW_CAMERA_WRITE       = 0x00020000,
81    /* buffer will be read by the HW camera pipeline */
82    GRALLOC_USAGE_HW_CAMERA_READ        = 0x00040000,
83    /* buffer will be used as part of zero-shutter-lag queue */
84    GRALLOC_USAGE_HW_CAMERA_ZSL         = 0x00080000,
85    /* mask for the software usage bit-mask */
86    GRALLOC_USAGE_HW_MASK               = 0x00071F00,
87
88    /* buffer should be displayed full-screen on an external display when
89     * possible
90     */
91    GRALLOC_USAGE_EXTERNAL_DISP         = 0x00002000,
92
93    /* Must have a hardware-protected path to external display sink for
94     * this buffer.  If a hardware-protected path is not available, then
95     * either don't composite only this buffer (preferred) to the
96     * external sink, or (less desirable) do not route the entire
97     * composition to the external sink.
98     */
99    GRALLOC_USAGE_PROTECTED             = 0x00004000,
100
101    /* implementation-specific private usage flags */
102    GRALLOC_USAGE_PRIVATE_0             = 0x10000000,
103    GRALLOC_USAGE_PRIVATE_1             = 0x20000000,
104    GRALLOC_USAGE_PRIVATE_2             = 0x40000000,
105    GRALLOC_USAGE_PRIVATE_3             = 0x80000000,
106    GRALLOC_USAGE_PRIVATE_MASK          = 0xF0000000,
107};
108
109/*****************************************************************************/
110
111/**
112 * Every hardware module must have a data structure named HAL_MODULE_INFO_SYM
113 * and the fields of this data structure must begin with hw_module_t
114 * followed by module specific information.
115 */
116typedef struct gralloc_module_t {
117    struct hw_module_t common;
118
119    /*
120     * (*registerBuffer)() must be called before a buffer_handle_t that has not
121     * been created with (*alloc_device_t::alloc)() can be used.
122     *
123     * This is intended to be used with buffer_handle_t's that have been
124     * received in this process through IPC.
125     *
126     * This function checks that the handle is indeed a valid one and prepares
127     * it for use with (*lock)() and (*unlock)().
128     *
129     * It is not necessary to call (*registerBuffer)() on a handle created
130     * with (*alloc_device_t::alloc)().
131     *
132     * returns an error if this buffer_handle_t is not valid.
133     */
134    int (*registerBuffer)(struct gralloc_module_t const* module,
135            buffer_handle_t handle);
136
137    /*
138     * (*unregisterBuffer)() is called once this handle is no longer needed in
139     * this process. After this call, it is an error to call (*lock)(),
140     * (*unlock)(), or (*registerBuffer)().
141     *
142     * This function doesn't close or free the handle itself; this is done
143     * by other means, usually through libcutils's native_handle_close() and
144     * native_handle_free().
145     *
146     * It is an error to call (*unregisterBuffer)() on a buffer that wasn't
147     * explicitly registered first.
148     */
149    int (*unregisterBuffer)(struct gralloc_module_t const* module,
150            buffer_handle_t handle);
151
152    /*
153     * The (*lock)() method is called before a buffer is accessed for the
154     * specified usage. This call may block, for instance if the h/w needs
155     * to finish rendering or if CPU caches need to be synchronized.
156     *
157     * The caller promises to modify only pixels in the area specified
158     * by (l,t,w,h).
159     *
160     * The content of the buffer outside of the specified area is NOT modified
161     * by this call.
162     *
163     * If usage specifies GRALLOC_USAGE_SW_*, vaddr is filled with the address
164     * of the buffer in virtual memory.
165     *
166     * THREADING CONSIDERATIONS:
167     *
168     * It is legal for several different threads to lock a buffer from
169     * read access, none of the threads are blocked.
170     *
171     * However, locking a buffer simultaneously for write or read/write is
172     * undefined, but:
173     * - shall not result in termination of the process
174     * - shall not block the caller
175     * It is acceptable to return an error or to leave the buffer's content
176     * into an indeterminate state.
177     *
178     * If the buffer was created with a usage mask incompatible with the
179     * requested usage flags here, -EINVAL is returned.
180     *
181     */
182
183    int (*lock)(struct gralloc_module_t const* module,
184            buffer_handle_t handle, int usage,
185            int l, int t, int w, int h,
186            void** vaddr);
187
188
189    /*
190     * The (*unlock)() method must be called after all changes to the buffer
191     * are completed.
192     */
193
194    int (*unlock)(struct gralloc_module_t const* module,
195            buffer_handle_t handle);
196
197
198    /* reserved for future use */
199    int (*perform)(struct gralloc_module_t const* module,
200            int operation, ... );
201
202    /* reserved for future use */
203    void* reserved_proc[7];
204} gralloc_module_t;
205
206/*****************************************************************************/
207
208/**
209 * Every device data structure must begin with hw_device_t
210 * followed by module specific public methods and attributes.
211 */
212
213typedef struct alloc_device_t {
214    struct hw_device_t common;
215
216    /*
217     * (*alloc)() Allocates a buffer in graphic memory with the requested
218     * parameters and returns a buffer_handle_t and the stride in pixels to
219     * allow the implementation to satisfy hardware constraints on the width
220     * of a pixmap (eg: it may have to be multiple of 8 pixels).
221     * The CALLER TAKES OWNERSHIP of the buffer_handle_t.
222     *
223     * Returns 0 on success or -errno on error.
224     */
225
226    int (*alloc)(struct alloc_device_t* dev,
227            int w, int h, int format, int usage,
228            buffer_handle_t* handle, int* stride);
229
230    /*
231     * (*free)() Frees a previously allocated buffer.
232     * Behavior is undefined if the buffer is still mapped in any process,
233     * but shall not result in termination of the program or security breaches
234     * (allowing a process to get access to another process' buffers).
235     * THIS FUNCTION TAKES OWNERSHIP of the buffer_handle_t which becomes
236     * invalid after the call.
237     *
238     * Returns 0 on success or -errno on error.
239     */
240    int (*free)(struct alloc_device_t* dev,
241            buffer_handle_t handle);
242
243    /* This hook is OPTIONAL.
244     *
245     * If non NULL it will be caused by SurfaceFlinger on dumpsys
246     */
247    void (*dump)(struct alloc_device_t *dev, char *buff, int buff_len);
248
249    void* reserved_proc[7];
250} alloc_device_t;
251
252
253/** convenience API for opening and closing a supported device */
254
255static inline int gralloc_open(const struct hw_module_t* module,
256        struct alloc_device_t** device) {
257    return module->methods->open(module,
258            GRALLOC_HARDWARE_GPU0, (struct hw_device_t**)device);
259}
260
261static inline int gralloc_close(struct alloc_device_t* device) {
262    return device->common.close(&device->common);
263}
264
265__END_DECLS
266
267#endif  // ANDROID_GRALLOC_INTERFACE_H
268