blob: f4fc21534f667708e6f06ffe83feb0a2eaaa7ba9 [file] [log] [blame]
Mathias Agopiana8a75162009-04-10 14:24:31 -07001/*
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 <cutils/native_handle.h>
22
23#include <hardware/hardware.h>
24
25#include <stdint.h>
26#include <sys/cdefs.h>
27#include <sys/types.h>
28
29__BEGIN_DECLS
30
Erik Gillinge9952042010-12-07 18:46:04 -080031#define GRALLOC_API_VERSION 1
32
Mathias Agopiana8a75162009-04-10 14:24:31 -070033/**
34 * The id of this module
35 */
36#define GRALLOC_HARDWARE_MODULE_ID "gralloc"
37
38/**
39 * Name of the graphics device to open
40 */
41
42#define GRALLOC_HARDWARE_FB0 "fb0"
43#define GRALLOC_HARDWARE_GPU0 "gpu0"
44
45enum {
46 /* buffer is never read in software */
Mathias Agopian988b8bd2009-05-04 14:26:56 -070047 GRALLOC_USAGE_SW_READ_NEVER = 0x00000000,
Mathias Agopiana8a75162009-04-10 14:24:31 -070048 /* buffer is rarely read in software */
49 GRALLOC_USAGE_SW_READ_RARELY = 0x00000002,
50 /* buffer is often read in software */
51 GRALLOC_USAGE_SW_READ_OFTEN = 0x00000003,
52 /* mask for the software read values */
53 GRALLOC_USAGE_SW_READ_MASK = 0x0000000F,
54
55 /* buffer is never written in software */
Mathias Agopian988b8bd2009-05-04 14:26:56 -070056 GRALLOC_USAGE_SW_WRITE_NEVER = 0x00000000,
Mathias Agopiana8a75162009-04-10 14:24:31 -070057 /* buffer is never written in software */
58 GRALLOC_USAGE_SW_WRITE_RARELY = 0x00000020,
59 /* buffer is never written in software */
60 GRALLOC_USAGE_SW_WRITE_OFTEN = 0x00000030,
61 /* mask for the software write values */
62 GRALLOC_USAGE_SW_WRITE_MASK = 0x000000F0,
63
64 /* buffer will be used as an OpenGL ES texture */
65 GRALLOC_USAGE_HW_TEXTURE = 0x00000100,
66 /* buffer will be used as an OpenGL ES render target */
67 GRALLOC_USAGE_HW_RENDER = 0x00000200,
68 /* buffer will be used by the 2D hardware blitter */
Jamie Gennisaabb7022010-07-01 16:49:07 -070069 GRALLOC_USAGE_HW_2D = 0x00000400,
Mathias Agopiana8a75162009-04-10 14:24:31 -070070 /* buffer will be used with the framebuffer device */
71 GRALLOC_USAGE_HW_FB = 0x00001000,
72 /* mask for the software usage bit-mask */
73 GRALLOC_USAGE_HW_MASK = 0x00001F00,
Jamie Gennis95d78be2010-07-01 16:49:52 -070074
Jamie Gennis7edeaf92010-11-17 18:51:17 -080075 /* buffer should be displayed full-screen on an external display when
76 * possible
77 */
78 GRALLOC_USAGE_EXTERNAL_DISP = 0x00002000,
79
Jamie Gennis95d78be2010-07-01 16:49:52 -070080 /* implementation-specific private usage flags */
81 GRALLOC_USAGE_PRIVATE_0 = 0x10000000,
82 GRALLOC_USAGE_PRIVATE_1 = 0x20000000,
83 GRALLOC_USAGE_PRIVATE_2 = 0x40000000,
84 GRALLOC_USAGE_PRIVATE_3 = 0x80000000,
85 GRALLOC_USAGE_PRIVATE_MASK = 0xF0000000,
Mathias Agopiana8a75162009-04-10 14:24:31 -070086};
87
Mathias Agopiana8a75162009-04-10 14:24:31 -070088/*****************************************************************************/
89
90typedef const native_handle* buffer_handle_t;
91
Mathias Agopiancd2433f2009-10-29 18:29:39 -070092enum {
93 /* FIXME: this only exists to work-around some issues with
94 * the video and camera frameworks. don't implement unless
95 * you know what you're doing.
96 */
97 GRALLOC_MODULE_PERFORM_CREATE_HANDLE_FROM_BUFFER = 0x080000001,
98};
99
Mathias Agopiana8a75162009-04-10 14:24:31 -0700100/**
101 * Every hardware module must have a data structure named HAL_MODULE_INFO_SYM
102 * and the fields of this data structure must begin with hw_module_t
103 * followed by module specific information.
104 */
Mathias Agopian9d82c1a2009-08-19 11:20:55 -0700105typedef struct gralloc_module_t {
Mathias Agopiana8a75162009-04-10 14:24:31 -0700106 struct hw_module_t common;
107
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700108 /*
109 * (*registerBuffer)() must be called before a buffer_handle_t that has not
110 * been created with (*alloc_device_t::alloc)() can be used.
111 *
112 * This is intended to be used with buffer_handle_t's that have been
113 * received in this process through IPC.
114 *
115 * This function checks that the handle is indeed a valid one and prepares
116 * it for use with (*lock)() and (*unlock)().
117 *
118 * It is not necessary to call (*registerBuffer)() on a handle created
119 * with (*alloc_device_t::alloc)().
120 *
121 * returns an error if this buffer_handle_t is not valid.
122 */
123 int (*registerBuffer)(struct gralloc_module_t const* module,
124 buffer_handle_t handle);
Mathias Agopiana8a75162009-04-10 14:24:31 -0700125
126 /*
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700127 * (*unregisterBuffer)() is called once this handle is no longer needed in
128 * this process. After this call, it is an error to call (*lock)(),
129 * (*unlock)(), or (*registerBuffer)().
Mathias Agopiana8a75162009-04-10 14:24:31 -0700130 *
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700131 * This function doesn't close or free the handle itself; this is done
132 * by other means, usually through libcutils's native_handle_close() and
133 * native_handle_free().
134 *
135 * It is an error to call (*unregisterBuffer)() on a buffer that wasn't
136 * explicitly registered first.
Mathias Agopiana8a75162009-04-10 14:24:31 -0700137 */
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700138 int (*unregisterBuffer)(struct gralloc_module_t const* module,
139 buffer_handle_t handle);
Mathias Agopiana8a75162009-04-10 14:24:31 -0700140
141 /*
142 * The (*lock)() method is called before a buffer is accessed for the
143 * specified usage. This call may block, for instance if the h/w needs
144 * to finish rendering or if CPU caches need to be synchronized.
145 *
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700146 * The caller promises to modify only pixels in the area specified
147 * by (l,t,w,h).
Mathias Agopiana8a75162009-04-10 14:24:31 -0700148 *
149 * The content of the buffer outside of the specified area is NOT modified
150 * by this call.
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700151 *
152 * If usage specifies GRALLOC_USAGE_SW_*, vaddr is filled with the address
153 * of the buffer in virtual memory.
154 *
Mathias Agopian485e6982009-05-05 20:21:57 -0700155 * THREADING CONSIDERATIONS:
156 *
157 * It is legal for several different threads to lock a buffer from
158 * read access, none of the threads are blocked.
159 *
160 * However, locking a buffer simultaneously for write or read/write is
161 * undefined, but:
162 * - shall not result in termination of the process
163 * - shall not block the caller
164 * It is acceptable to return an error or to leave the buffer's content
165 * into an indeterminate state.
166 *
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700167 * If the buffer was created with a usage mask incompatible with the
168 * requested usage flags here, -EINVAL is returned.
Mathias Agopiana8a75162009-04-10 14:24:31 -0700169 *
170 */
171
172 int (*lock)(struct gralloc_module_t const* module,
173 buffer_handle_t handle, int usage,
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700174 int l, int t, int w, int h,
175 void** vaddr);
Mathias Agopiana8a75162009-04-10 14:24:31 -0700176
177
178 /*
179 * The (*unlock)() method must be called after all changes to the buffer
180 * are completed.
181 */
182
183 int (*unlock)(struct gralloc_module_t const* module,
184 buffer_handle_t handle);
185
Mathias Agopiancd2433f2009-10-29 18:29:39 -0700186
Mathias Agopian8255d9d2009-09-17 16:15:36 -0700187 /* reserved for future use */
Mathias Agopiancd2433f2009-10-29 18:29:39 -0700188 int (*perform)(struct gralloc_module_t const* module,
189 int operation, ... );
190
191 /* reserved for future use */
192 void* reserved_proc[7];
Mathias Agopian9d82c1a2009-08-19 11:20:55 -0700193} gralloc_module_t;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700194
195/*****************************************************************************/
196
197/**
198 * Every device data structure must begin with hw_device_t
199 * followed by module specific public methods and attributes.
200 */
201
Mathias Agopian9d82c1a2009-08-19 11:20:55 -0700202typedef struct alloc_device_t {
Mathias Agopiana8a75162009-04-10 14:24:31 -0700203 struct hw_device_t common;
204
205 /*
206 * (*alloc)() Allocates a buffer in graphic memory with the requested
207 * parameters and returns a buffer_handle_t and the stride in pixels to
208 * allow the implementation to satisfy hardware constraints on the width
209 * of a pixmap (eg: it may have to be multiple of 8 pixels).
210 * The CALLER TAKES OWNERSHIP of the buffer_handle_t.
211 *
212 * Returns 0 on success or -errno on error.
213 */
214
215 int (*alloc)(struct alloc_device_t* dev,
216 int w, int h, int format, int usage,
217 buffer_handle_t* handle, int* stride);
218
219 /*
220 * (*free)() Frees a previously allocated buffer.
221 * Behavior is undefined if the buffer is still mapped in any process,
222 * but shall not result in termination of the program or security breaches
223 * (allowing a process to get access to another process' buffers).
224 * THIS FUNCTION TAKES OWNERSHIP of the buffer_handle_t which becomes
225 * invalid after the call.
226 *
227 * Returns 0 on success or -errno on error.
228 */
229 int (*free)(struct alloc_device_t* dev,
230 buffer_handle_t handle);
231
Erik Gilling158549c2010-12-01 16:34:08 -0800232 /* This hook is OPTIONAL.
233 *
234 * If non NULL it will be caused by SurfaceFlinger on dumpsys
235 */
236 void (*dump)(struct alloc_device_t *dev, char *buff, int buff_len);
237
238 void* reserved_proc[7];
Mathias Agopian9d82c1a2009-08-19 11:20:55 -0700239} alloc_device_t;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700240
241
Mathias Agopian9d82c1a2009-08-19 11:20:55 -0700242typedef struct framebuffer_device_t {
Mathias Agopiana8a75162009-04-10 14:24:31 -0700243 struct hw_device_t common;
244
245 /* flags describing some attributes of the framebuffer */
246 const uint32_t flags;
247
248 /* dimensions of the framebuffer in pixels */
249 const uint32_t width;
250 const uint32_t height;
251
252 /* frambuffer stride in pixels */
253 const int stride;
254
255 /* framebuffer pixel format */
256 const int format;
257
258 /* resolution of the framebuffer's display panel in pixel per inch*/
259 const float xdpi;
260 const float ydpi;
261
262 /* framebuffer's display panel refresh rate in frames per second */
263 const float fps;
264
265 /* min swap interval supported by this framebuffer */
266 const int minSwapInterval;
267
268 /* max swap interval supported by this framebuffer */
269 const int maxSwapInterval;
270
271 int reserved[8];
272
273 /*
274 * requests a specific swap-interval (same definition than EGL)
275 *
276 * Returns 0 on success or -errno on error.
277 */
278 int (*setSwapInterval)(struct framebuffer_device_t* window,
279 int interval);
280
281 /*
Mathias Agopianf5cf8f82009-05-07 17:39:31 -0700282 * This hook is OPTIONAL.
283 *
284 * It is non NULL If the framebuffer driver supports "update-on-demand"
285 * and the given rectangle is the area of the screen that gets
286 * updated during (*post)().
287 *
288 * This is useful on devices that are able to DMA only a portion of
289 * the screen to the display panel, upon demand -- as opposed to
290 * constantly refreshing the panel 60 times per second, for instance.
291 *
Mathias Agopian8255d9d2009-09-17 16:15:36 -0700292 * Only the area defined by this rectangle is guaranteed to be valid, that
Mathias Agopianf5cf8f82009-05-07 17:39:31 -0700293 * is, the driver is not allowed to post anything outside of this
294 * rectangle.
295 *
296 * The rectangle evaluated during (*post)() and specifies which area
297 * of the buffer passed in (*post)() shall to be posted.
Mathias Agopiana8a75162009-04-10 14:24:31 -0700298 *
299 * return -EINVAL if width or height <=0, or if left or top < 0
300 */
301 int (*setUpdateRect)(struct framebuffer_device_t* window,
302 int left, int top, int width, int height);
303
304 /*
305 * Post <buffer> to the display (display it on the screen)
306 * The buffer must have been allocated with the
307 * GRALLOC_USAGE_HW_FB usage flag.
308 * buffer must be the same width and height as the display and must NOT
309 * be locked.
310 *
311 * The buffer is shown during the next VSYNC.
312 *
313 * If the same buffer is posted again (possibly after some other buffer),
314 * post() will block until the the first post is completed.
315 *
316 * Internally, post() is expected to lock the buffer so that a
317 * subsequent call to gralloc_module_t::(*lock)() with USAGE_RENDER or
318 * USAGE_*_WRITE will block until it is safe; that is typically once this
319 * buffer is shown and another buffer has been posted.
320 *
321 * Returns 0 on success or -errno on error.
322 */
323 int (*post)(struct framebuffer_device_t* dev, buffer_handle_t buffer);
324
Mathias Agopian8255d9d2009-09-17 16:15:36 -0700325
326 /*
327 * The (*compositionComplete)() method must be called after the
328 * compositor has finished issuing GL commands for client buffers.
329 */
330
331 int (*compositionComplete)(struct framebuffer_device_t* dev);
332
Erik Gilling158549c2010-12-01 16:34:08 -0800333 /*
334 * This hook is OPTIONAL.
335 *
336 * If non NULL it will be caused by SurfaceFlinger on dumpsys
337 */
338 void (*dump)(struct framebuffer_device_t* dev, char *buff, int buff_len);
Mathias Agopian8255d9d2009-09-17 16:15:36 -0700339
Erik Gilling158549c2010-12-01 16:34:08 -0800340 void* reserved_proc[7];
Mathias Agopian9d82c1a2009-08-19 11:20:55 -0700341
342} framebuffer_device_t;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700343
344
345/** convenience API for opening and closing a supported device */
346
347static inline int gralloc_open(const struct hw_module_t* module,
348 struct alloc_device_t** device) {
349 return module->methods->open(module,
350 GRALLOC_HARDWARE_GPU0, (struct hw_device_t**)device);
351}
352
353static inline int gralloc_close(struct alloc_device_t* device) {
354 return device->common.close(&device->common);
355}
356
357
358static inline int framebuffer_open(const struct hw_module_t* module,
359 struct framebuffer_device_t** device) {
360 return module->methods->open(module,
361 GRALLOC_HARDWARE_FB0, (struct hw_device_t**)device);
362}
363
364static inline int framebuffer_close(struct framebuffer_device_t* device) {
365 return device->common.close(&device->common);
366}
367
368
369__END_DECLS
370
371#endif // ANDROID_ALLOC_INTERFACE_H