2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #ifndef ANDROID_GRALLOC_INTERFACE_H
19 #define ANDROID_GRALLOC_INTERFACE_H
21 #include <system/window.h>
22 #include <hardware/hardware.h>
25 #include <sys/cdefs.h>
26 #include <sys/types.h>
28 #include <cutils/native_handle.h>
30 #include <hardware/hardware.h>
31 #include <hardware/fb.h>
35 #define GRALLOC_API_VERSION 1
38 * The id of this module
40 #define GRALLOC_HARDWARE_MODULE_ID "gralloc"
43 * Name of the graphics device to open
46 #define GRALLOC_HARDWARE_GPU0 "gpu0"
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,
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,
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
= 0x00060000,
85 /* mask for the camera access values */
86 GRALLOC_USAGE_HW_CAMERA_MASK
= 0x00060000,
87 /* mask for the software usage bit-mask */
88 GRALLOC_USAGE_HW_MASK
= 0x00071F00,
90 /* buffer should be displayed full-screen on an external display when
93 GRALLOC_USAGE_EXTERNAL_DISP
= 0x00002000,
95 /* Must have a hardware-protected path to external display sink for
96 * this buffer. If a hardware-protected path is not available, then
97 * either don't composite only this buffer (preferred) to the
98 * external sink, or (less desirable) do not route the entire
99 * composition to the external sink.
101 GRALLOC_USAGE_PROTECTED
= 0x00004000,
103 /* implementation-specific private usage flags */
104 GRALLOC_USAGE_PRIVATE_0
= 0x10000000,
105 GRALLOC_USAGE_PRIVATE_1
= 0x20000000,
106 GRALLOC_USAGE_PRIVATE_2
= 0x40000000,
107 GRALLOC_USAGE_PRIVATE_3
= 0x80000000,
108 GRALLOC_USAGE_PRIVATE_MASK
= 0xF0000000,
111 /*****************************************************************************/
114 * Every hardware module must have a data structure named HAL_MODULE_INFO_SYM
115 * and the fields of this data structure must begin with hw_module_t
116 * followed by module specific information.
118 typedef struct gralloc_module_t
{
119 struct hw_module_t common
;
122 * (*registerBuffer)() must be called before a buffer_handle_t that has not
123 * been created with (*alloc_device_t::alloc)() can be used.
125 * This is intended to be used with buffer_handle_t's that have been
126 * received in this process through IPC.
128 * This function checks that the handle is indeed a valid one and prepares
129 * it for use with (*lock)() and (*unlock)().
131 * It is not necessary to call (*registerBuffer)() on a handle created
132 * with (*alloc_device_t::alloc)().
134 * returns an error if this buffer_handle_t is not valid.
136 int (*registerBuffer
)(struct gralloc_module_t
const* module
,
137 buffer_handle_t handle
);
140 * (*unregisterBuffer)() is called once this handle is no longer needed in
141 * this process. After this call, it is an error to call (*lock)(),
142 * (*unlock)(), or (*registerBuffer)().
144 * This function doesn't close or free the handle itself; this is done
145 * by other means, usually through libcutils's native_handle_close() and
146 * native_handle_free().
148 * It is an error to call (*unregisterBuffer)() on a buffer that wasn't
149 * explicitly registered first.
151 int (*unregisterBuffer
)(struct gralloc_module_t
const* module
,
152 buffer_handle_t handle
);
155 * The (*lock)() method is called before a buffer is accessed for the
156 * specified usage. This call may block, for instance if the h/w needs
157 * to finish rendering or if CPU caches need to be synchronized.
159 * The caller promises to modify only pixels in the area specified
162 * The content of the buffer outside of the specified area is NOT modified
165 * If usage specifies GRALLOC_USAGE_SW_*, vaddr is filled with the address
166 * of the buffer in virtual memory.
168 * THREADING CONSIDERATIONS:
170 * It is legal for several different threads to lock a buffer from
171 * read access, none of the threads are blocked.
173 * However, locking a buffer simultaneously for write or read/write is
175 * - shall not result in termination of the process
176 * - shall not block the caller
177 * It is acceptable to return an error or to leave the buffer's content
178 * into an indeterminate state.
180 * If the buffer was created with a usage mask incompatible with the
181 * requested usage flags here, -EINVAL is returned.
185 int (*lock
)(struct gralloc_module_t
const* module
,
186 buffer_handle_t handle
, int usage
,
187 int l
, int t
, int w
, int h
,
192 * The (*unlock)() method must be called after all changes to the buffer
196 int (*unlock
)(struct gralloc_module_t
const* module
,
197 buffer_handle_t handle
);
200 /* reserved for future use */
201 int (*perform
)(struct gralloc_module_t
const* module
,
202 int operation
, ... );
204 /* reserved for future use */
205 void* reserved_proc
[7];
208 /*****************************************************************************/
211 * Every device data structure must begin with hw_device_t
212 * followed by module specific public methods and attributes.
215 typedef struct alloc_device_t
{
216 struct hw_device_t common
;
219 * (*alloc)() Allocates a buffer in graphic memory with the requested
220 * parameters and returns a buffer_handle_t and the stride in pixels to
221 * allow the implementation to satisfy hardware constraints on the width
222 * of a pixmap (eg: it may have to be multiple of 8 pixels).
223 * The CALLER TAKES OWNERSHIP of the buffer_handle_t.
225 * Returns 0 on success or -errno on error.
228 int (*alloc
)(struct alloc_device_t
* dev
,
229 int w
, int h
, int format
, int usage
,
230 buffer_handle_t
* handle
, int* stride
);
233 * (*free)() Frees a previously allocated buffer.
234 * Behavior is undefined if the buffer is still mapped in any process,
235 * but shall not result in termination of the program or security breaches
236 * (allowing a process to get access to another process' buffers).
237 * THIS FUNCTION TAKES OWNERSHIP of the buffer_handle_t which becomes
238 * invalid after the call.
240 * Returns 0 on success or -errno on error.
242 int (*free
)(struct alloc_device_t
* dev
,
243 buffer_handle_t handle
);
245 /* This hook is OPTIONAL.
247 * If non NULL it will be caused by SurfaceFlinger on dumpsys
249 void (*dump
)(struct alloc_device_t
*dev
, char *buff
, int buff_len
);
251 void* reserved_proc
[7];
255 /** convenience API for opening and closing a supported device */
257 static inline int gralloc_open(const struct hw_module_t
* module
,
258 struct alloc_device_t
** device
) {
259 return module
->methods
->open(module
,
260 GRALLOC_HARDWARE_GPU0
, (struct hw_device_t
**)device
);
263 static inline int gralloc_close(struct alloc_device_t
* device
) {
264 return device
->common
.close(&device
->common
);
269 #endif // ANDROID_GRALLOC_INTERFACE_H