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 <cutils/native_handle.h>
23 #include <hardware/hardware.h>
26 #include <sys/cdefs.h>
27 #include <sys/types.h>
32 * The id of this module
34 #define GRALLOC_HARDWARE_MODULE_ID "gralloc"
37 * Name of the graphics device to open
40 #define GRALLOC_HARDWARE_FB0 "fb0"
41 #define GRALLOC_HARDWARE_GPU0 "gpu0"
44 /* buffer is never read in software */
45 GRALLOC_USAGE_SW_READ_NEVER = 0x00000000,
46 /* buffer is rarely read in software */
47 GRALLOC_USAGE_SW_READ_RARELY = 0x00000002,
48 /* buffer is often read in software */
49 GRALLOC_USAGE_SW_READ_OFTEN = 0x00000003,
50 /* mask for the software read values */
51 GRALLOC_USAGE_SW_READ_MASK = 0x0000000F,
53 /* buffer is never written in software */
54 GRALLOC_USAGE_SW_WRITE_NEVER = 0x00000000,
55 /* buffer is never written in software */
56 GRALLOC_USAGE_SW_WRITE_RARELY = 0x00000020,
57 /* buffer is never written in software */
58 GRALLOC_USAGE_SW_WRITE_OFTEN = 0x00000030,
59 /* mask for the software write values */
60 GRALLOC_USAGE_SW_WRITE_MASK = 0x000000F0,
62 /* buffer will be used as an OpenGL ES texture */
63 GRALLOC_USAGE_HW_TEXTURE = 0x00000100,
64 /* buffer will be used as an OpenGL ES render target */
65 GRALLOC_USAGE_HW_RENDER = 0x00000200,
66 /* buffer will be used by the 2D hardware blitter */
67 GRALLOC_USAGE_HW_2D = 0x00000C00,
68 /* buffer will be used with the framebuffer device */
69 GRALLOC_USAGE_HW_FB = 0x00001000,
70 /* mask for the software usage bit-mask */
71 GRALLOC_USAGE_HW_MASK = 0x00001F00,
74 /*****************************************************************************/
76 typedef const native_handle* buffer_handle_t;
79 /* FIXME: this only exists to work-around some issues with
80 * the video and camera frameworks. don't implement unless
81 * you know what you're doing.
83 GRALLOC_MODULE_PERFORM_CREATE_HANDLE_FROM_BUFFER = 0x080000001,
87 * Every hardware module must have a data structure named HAL_MODULE_INFO_SYM
88 * and the fields of this data structure must begin with hw_module_t
89 * followed by module specific information.
91 typedef struct gralloc_module_t {
92 struct hw_module_t common;
95 * (*registerBuffer)() must be called before a buffer_handle_t that has not
96 * been created with (*alloc_device_t::alloc)() can be used.
98 * This is intended to be used with buffer_handle_t's that have been
99 * received in this process through IPC.
101 * This function checks that the handle is indeed a valid one and prepares
102 * it for use with (*lock)() and (*unlock)().
104 * It is not necessary to call (*registerBuffer)() on a handle created
105 * with (*alloc_device_t::alloc)().
107 * returns an error if this buffer_handle_t is not valid.
109 int (*registerBuffer)(struct gralloc_module_t const* module,
110 buffer_handle_t handle);
113 * (*unregisterBuffer)() is called once this handle is no longer needed in
114 * this process. After this call, it is an error to call (*lock)(),
115 * (*unlock)(), or (*registerBuffer)().
117 * This function doesn't close or free the handle itself; this is done
118 * by other means, usually through libcutils's native_handle_close() and
119 * native_handle_free().
121 * It is an error to call (*unregisterBuffer)() on a buffer that wasn't
122 * explicitly registered first.
124 int (*unregisterBuffer)(struct gralloc_module_t const* module,
125 buffer_handle_t handle);
128 * The (*lock)() method is called before a buffer is accessed for the
129 * specified usage. This call may block, for instance if the h/w needs
130 * to finish rendering or if CPU caches need to be synchronized.
132 * The caller promises to modify only pixels in the area specified
135 * The content of the buffer outside of the specified area is NOT modified
138 * If usage specifies GRALLOC_USAGE_SW_*, vaddr is filled with the address
139 * of the buffer in virtual memory.
141 * THREADING CONSIDERATIONS:
143 * It is legal for several different threads to lock a buffer from
144 * read access, none of the threads are blocked.
146 * However, locking a buffer simultaneously for write or read/write is
148 * - shall not result in termination of the process
149 * - shall not block the caller
150 * It is acceptable to return an error or to leave the buffer's content
151 * into an indeterminate state.
153 * If the buffer was created with a usage mask incompatible with the
154 * requested usage flags here, -EINVAL is returned.
158 int (*lock)(struct gralloc_module_t const* module,
159 buffer_handle_t handle, int usage,
160 int l, int t, int w, int h,
165 * The (*unlock)() method must be called after all changes to the buffer
169 int (*unlock)(struct gralloc_module_t const* module,
170 buffer_handle_t handle);
173 /* reserved for future use */
174 int (*perform)(struct gralloc_module_t const* module,
175 int operation, ... );
177 /* reserved for future use */
178 void* reserved_proc[7];
181 /*****************************************************************************/
184 * Every device data structure must begin with hw_device_t
185 * followed by module specific public methods and attributes.
188 typedef struct alloc_device_t {
189 struct hw_device_t common;
192 * (*alloc)() Allocates a buffer in graphic memory with the requested
193 * parameters and returns a buffer_handle_t and the stride in pixels to
194 * allow the implementation to satisfy hardware constraints on the width
195 * of a pixmap (eg: it may have to be multiple of 8 pixels).
196 * The CALLER TAKES OWNERSHIP of the buffer_handle_t.
198 * Returns 0 on success or -errno on error.
201 int (*alloc)(struct alloc_device_t* dev,
202 int w, int h, int format, int usage,
203 buffer_handle_t* handle, int* stride);
206 * (*free)() Frees a previously allocated buffer.
207 * Behavior is undefined if the buffer is still mapped in any process,
208 * but shall not result in termination of the program or security breaches
209 * (allowing a process to get access to another process' buffers).
210 * THIS FUNCTION TAKES OWNERSHIP of the buffer_handle_t which becomes
211 * invalid after the call.
213 * Returns 0 on success or -errno on error.
215 int (*free)(struct alloc_device_t* dev,
216 buffer_handle_t handle);
221 typedef struct framebuffer_device_t {
222 struct hw_device_t common;
224 /* flags describing some attributes of the framebuffer */
225 const uint32_t flags;
227 /* dimensions of the framebuffer in pixels */
228 const uint32_t width;
229 const uint32_t height;
231 /* frambuffer stride in pixels */
234 /* framebuffer pixel format */
237 /* resolution of the framebuffer's display panel in pixel per inch*/
241 /* framebuffer's display panel refresh rate in frames per second */
244 /* min swap interval supported by this framebuffer */
245 const int minSwapInterval;
247 /* max swap interval supported by this framebuffer */
248 const int maxSwapInterval;
253 * requests a specific swap-interval (same definition than EGL)
255 * Returns 0 on success or -errno on error.
257 int (*setSwapInterval)(struct framebuffer_device_t* window,
261 * This hook is OPTIONAL.
263 * It is non NULL If the framebuffer driver supports "update-on-demand"
264 * and the given rectangle is the area of the screen that gets
265 * updated during (*post)().
267 * This is useful on devices that are able to DMA only a portion of
268 * the screen to the display panel, upon demand -- as opposed to
269 * constantly refreshing the panel 60 times per second, for instance.
271 * Only the area defined by this rectangle is guaranteed to be valid, that
272 * is, the driver is not allowed to post anything outside of this
275 * The rectangle evaluated during (*post)() and specifies which area
276 * of the buffer passed in (*post)() shall to be posted.
278 * return -EINVAL if width or height <=0, or if left or top < 0
280 int (*setUpdateRect)(struct framebuffer_device_t* window,
281 int left, int top, int width, int height);
284 * Post <buffer> to the display (display it on the screen)
285 * The buffer must have been allocated with the
286 * GRALLOC_USAGE_HW_FB usage flag.
287 * buffer must be the same width and height as the display and must NOT
290 * The buffer is shown during the next VSYNC.
292 * If the same buffer is posted again (possibly after some other buffer),
293 * post() will block until the the first post is completed.
295 * Internally, post() is expected to lock the buffer so that a
296 * subsequent call to gralloc_module_t::(*lock)() with USAGE_RENDER or
297 * USAGE_*_WRITE will block until it is safe; that is typically once this
298 * buffer is shown and another buffer has been posted.
300 * Returns 0 on success or -errno on error.
302 int (*post)(struct framebuffer_device_t* dev, buffer_handle_t buffer);
306 * The (*compositionComplete)() method must be called after the
307 * compositor has finished issuing GL commands for client buffers.
310 int (*compositionComplete)(struct framebuffer_device_t* dev);
313 void* reserved_proc[8];
315 } framebuffer_device_t;
318 /** convenience API for opening and closing a supported device */
320 static inline int gralloc_open(const struct hw_module_t* module,
321 struct alloc_device_t** device) {
322 return module->methods->open(module,
323 GRALLOC_HARDWARE_GPU0, (struct hw_device_t**)device);
326 static inline int gralloc_close(struct alloc_device_t* device) {
327 return device->common.close(&device->common);
331 static inline int framebuffer_open(const struct hw_module_t* module,
332 struct framebuffer_device_t** device) {
333 return module->methods->open(module,
334 GRALLOC_HARDWARE_FB0, (struct hw_device_t**)device);
337 static inline int framebuffer_close(struct framebuffer_device_t* device) {
338 return device->common.close(&device->common);
344 #endif // ANDROID_ALLOC_INTERFACE_H