blob: c1724cc09370aff40cb1de95893b8a380ff58a75 [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
31/**
32 * The id of this module
33 */
34#define GRALLOC_HARDWARE_MODULE_ID "gralloc"
35
36/**
37 * Name of the graphics device to open
38 */
39
40#define GRALLOC_HARDWARE_FB0 "fb0"
41#define GRALLOC_HARDWARE_GPU0 "gpu0"
42
43enum {
44 /* buffer is never read in software */
Mathias Agopian988b8bd2009-05-04 14:26:56 -070045 GRALLOC_USAGE_SW_READ_NEVER = 0x00000000,
Mathias Agopiana8a75162009-04-10 14:24:31 -070046 /* 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,
52
53 /* buffer is never written in software */
Mathias Agopian988b8bd2009-05-04 14:26:56 -070054 GRALLOC_USAGE_SW_WRITE_NEVER = 0x00000000,
Mathias Agopiana8a75162009-04-10 14:24:31 -070055 /* 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,
61
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,
72};
73
74enum {
75 /* the framebuffer is mapped in memory */
76 FRAMEBUFFER_RESERVED0 = 0x00000001,
77 FRAMEBUFFER_FLAG_MAPPED = 0x00000002,
78};
79
80/*****************************************************************************/
81
82typedef const native_handle* buffer_handle_t;
83
84/**
85 * Every hardware module must have a data structure named HAL_MODULE_INFO_SYM
86 * and the fields of this data structure must begin with hw_module_t
87 * followed by module specific information.
88 */
89struct gralloc_module_t {
90 struct hw_module_t common;
91
Mathias Agopiana8a75162009-04-10 14:24:31 -070092
Mathias Agopian988b8bd2009-05-04 14:26:56 -070093 /*
94 * (*registerBuffer)() must be called before a buffer_handle_t that has not
95 * been created with (*alloc_device_t::alloc)() can be used.
96 *
97 * This is intended to be used with buffer_handle_t's that have been
98 * received in this process through IPC.
99 *
100 * This function checks that the handle is indeed a valid one and prepares
101 * it for use with (*lock)() and (*unlock)().
102 *
103 * It is not necessary to call (*registerBuffer)() on a handle created
104 * with (*alloc_device_t::alloc)().
105 *
106 * returns an error if this buffer_handle_t is not valid.
107 */
108 int (*registerBuffer)(struct gralloc_module_t const* module,
109 buffer_handle_t handle);
Mathias Agopiana8a75162009-04-10 14:24:31 -0700110
111 /*
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700112 * (*unregisterBuffer)() is called once this handle is no longer needed in
113 * this process. After this call, it is an error to call (*lock)(),
114 * (*unlock)(), or (*registerBuffer)().
Mathias Agopiana8a75162009-04-10 14:24:31 -0700115 *
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700116 * This function doesn't close or free the handle itself; this is done
117 * by other means, usually through libcutils's native_handle_close() and
118 * native_handle_free().
119 *
120 * It is an error to call (*unregisterBuffer)() on a buffer that wasn't
121 * explicitly registered first.
Mathias Agopiana8a75162009-04-10 14:24:31 -0700122 */
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700123 int (*unregisterBuffer)(struct gralloc_module_t const* module,
124 buffer_handle_t handle);
Mathias Agopiana8a75162009-04-10 14:24:31 -0700125
126 /*
127 * The (*lock)() method is called before a buffer is accessed for the
128 * specified usage. This call may block, for instance if the h/w needs
129 * to finish rendering or if CPU caches need to be synchronized.
130 *
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700131 * The caller promises to modify only pixels in the area specified
132 * by (l,t,w,h).
Mathias Agopiana8a75162009-04-10 14:24:31 -0700133 *
134 * The content of the buffer outside of the specified area is NOT modified
135 * by this call.
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700136 *
137 * If usage specifies GRALLOC_USAGE_SW_*, vaddr is filled with the address
138 * of the buffer in virtual memory.
139 *
140 * If the buffer was created with a usage mask incompatible with the
141 * requested usage flags here, -EINVAL is returned.
Mathias Agopiana8a75162009-04-10 14:24:31 -0700142 *
143 */
144
145 int (*lock)(struct gralloc_module_t const* module,
146 buffer_handle_t handle, int usage,
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700147 int l, int t, int w, int h,
148 void** vaddr);
Mathias Agopiana8a75162009-04-10 14:24:31 -0700149
150
151 /*
152 * The (*unlock)() method must be called after all changes to the buffer
153 * are completed.
154 */
155
156 int (*unlock)(struct gralloc_module_t const* module,
157 buffer_handle_t handle);
158
159};
160
161/*****************************************************************************/
162
163/**
164 * Every device data structure must begin with hw_device_t
165 * followed by module specific public methods and attributes.
166 */
167
168struct alloc_device_t {
169 struct hw_device_t common;
170
171 /*
172 * (*alloc)() Allocates a buffer in graphic memory with the requested
173 * parameters and returns a buffer_handle_t and the stride in pixels to
174 * allow the implementation to satisfy hardware constraints on the width
175 * of a pixmap (eg: it may have to be multiple of 8 pixels).
176 * The CALLER TAKES OWNERSHIP of the buffer_handle_t.
177 *
178 * Returns 0 on success or -errno on error.
179 */
180
181 int (*alloc)(struct alloc_device_t* dev,
182 int w, int h, int format, int usage,
183 buffer_handle_t* handle, int* stride);
184
185 /*
186 * (*free)() Frees a previously allocated buffer.
187 * Behavior is undefined if the buffer is still mapped in any process,
188 * but shall not result in termination of the program or security breaches
189 * (allowing a process to get access to another process' buffers).
190 * THIS FUNCTION TAKES OWNERSHIP of the buffer_handle_t which becomes
191 * invalid after the call.
192 *
193 * Returns 0 on success or -errno on error.
194 */
195 int (*free)(struct alloc_device_t* dev,
196 buffer_handle_t handle);
197
198};
199
200
201struct framebuffer_device_t {
202 struct hw_device_t common;
203
204 /* flags describing some attributes of the framebuffer */
205 const uint32_t flags;
206
207 /* dimensions of the framebuffer in pixels */
208 const uint32_t width;
209 const uint32_t height;
210
211 /* frambuffer stride in pixels */
212 const int stride;
213
214 /* framebuffer pixel format */
215 const int format;
216
217 /* resolution of the framebuffer's display panel in pixel per inch*/
218 const float xdpi;
219 const float ydpi;
220
221 /* framebuffer's display panel refresh rate in frames per second */
222 const float fps;
223
224 /* min swap interval supported by this framebuffer */
225 const int minSwapInterval;
226
227 /* max swap interval supported by this framebuffer */
228 const int maxSwapInterval;
229
230 int reserved[8];
231
232 /*
233 * requests a specific swap-interval (same definition than EGL)
234 *
235 * Returns 0 on success or -errno on error.
236 */
237 int (*setSwapInterval)(struct framebuffer_device_t* window,
238 int interval);
239
240 /*
241 * sets a rectangle evaluated during (*post)() specifying which area
242 * of the buffer passed in (*post)() needs to be posted.
243 *
244 * return -EINVAL if width or height <=0, or if left or top < 0
245 */
246 int (*setUpdateRect)(struct framebuffer_device_t* window,
247 int left, int top, int width, int height);
248
249 /*
250 * Post <buffer> to the display (display it on the screen)
251 * The buffer must have been allocated with the
252 * GRALLOC_USAGE_HW_FB usage flag.
253 * buffer must be the same width and height as the display and must NOT
254 * be locked.
255 *
256 * The buffer is shown during the next VSYNC.
257 *
258 * If the same buffer is posted again (possibly after some other buffer),
259 * post() will block until the the first post is completed.
260 *
261 * Internally, post() is expected to lock the buffer so that a
262 * subsequent call to gralloc_module_t::(*lock)() with USAGE_RENDER or
263 * USAGE_*_WRITE will block until it is safe; that is typically once this
264 * buffer is shown and another buffer has been posted.
265 *
266 * Returns 0 on success or -errno on error.
267 */
268 int (*post)(struct framebuffer_device_t* dev, buffer_handle_t buffer);
269
270 void* reserved_proc[8];
271};
272
273
274/** convenience API for opening and closing a supported device */
275
276static inline int gralloc_open(const struct hw_module_t* module,
277 struct alloc_device_t** device) {
278 return module->methods->open(module,
279 GRALLOC_HARDWARE_GPU0, (struct hw_device_t**)device);
280}
281
282static inline int gralloc_close(struct alloc_device_t* device) {
283 return device->common.close(&device->common);
284}
285
286
287static inline int framebuffer_open(const struct hw_module_t* module,
288 struct framebuffer_device_t** device) {
289 return module->methods->open(module,
290 GRALLOC_HARDWARE_FB0, (struct hw_device_t**)device);
291}
292
293static inline int framebuffer_close(struct framebuffer_device_t* device) {
294 return device->common.close(&device->common);
295}
296
297
298__END_DECLS
299
300#endif // ANDROID_ALLOC_INTERFACE_H