blob: 9daad1c8c29cdbc8afb246c0eaad62da2a8d1c79 [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
Iliyan Malchev33c0fe02011-05-02 18:58:16 -070021#include <system/window.h>
Mathias Agopiana8a75162009-04-10 14:24:31 -070022#include <hardware/hardware.h>
23
24#include <stdint.h>
25#include <sys/cdefs.h>
26#include <sys/types.h>
27
Mathias Agopian5337b102011-04-28 18:56:10 -070028#include <cutils/native_handle.h>
29
30#include <hardware/hardware.h>
31#include <hardware/fb.h>
32
Mathias Agopiana8a75162009-04-10 14:24:31 -070033__BEGIN_DECLS
34
Erik Gillinge9952042010-12-07 18:46:04 -080035#define GRALLOC_API_VERSION 1
36
Mathias Agopiana8a75162009-04-10 14:24:31 -070037/**
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
Mathias Agopiana8a75162009-04-10 14:24:31 -070046#define GRALLOC_HARDWARE_GPU0 "gpu0"
47
48enum {
49 /* buffer is never read in software */
Mathias Agopian988b8bd2009-05-04 14:26:56 -070050 GRALLOC_USAGE_SW_READ_NEVER = 0x00000000,
Mathias Agopiana8a75162009-04-10 14:24:31 -070051 /* 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 */
Mathias Agopian988b8bd2009-05-04 14:26:56 -070059 GRALLOC_USAGE_SW_WRITE_NEVER = 0x00000000,
Mathias Agopiana8a75162009-04-10 14:24:31 -070060 /* 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 */
Jamie Gennisaabb7022010-07-01 16:49:07 -070072 GRALLOC_USAGE_HW_2D = 0x00000400,
Jamie Gennis4b560d52011-08-10 11:41:52 -070073 /* buffer will be used by the HWComposer HAL module */
74 GRALLOC_USAGE_HW_COMPOSER = 0x00000800,
Mathias Agopiana8a75162009-04-10 14:24:31 -070075 /* buffer will be used with the framebuffer device */
76 GRALLOC_USAGE_HW_FB = 0x00001000,
77 /* mask for the software usage bit-mask */
78 GRALLOC_USAGE_HW_MASK = 0x00001F00,
Jamie Gennis95d78be2010-07-01 16:49:52 -070079
Jamie Gennis7edeaf92010-11-17 18:51:17 -080080 /* buffer should be displayed full-screen on an external display when
81 * possible
82 */
83 GRALLOC_USAGE_EXTERNAL_DISP = 0x00002000,
84
Glenn Kasten7cb277a2011-01-19 16:58:31 -080085 /* Must have a hardware-protected path to external display sink for
86 * this buffer. If a hardware-protected path is not available, then
87 * either don't composite only this buffer (preferred) to the
88 * external sink, or (less desirable) do not route the entire
89 * composition to the external sink.
90 */
91 GRALLOC_USAGE_PROTECTED = 0x00004000,
92
Jamie Gennis95d78be2010-07-01 16:49:52 -070093 /* implementation-specific private usage flags */
94 GRALLOC_USAGE_PRIVATE_0 = 0x10000000,
95 GRALLOC_USAGE_PRIVATE_1 = 0x20000000,
96 GRALLOC_USAGE_PRIVATE_2 = 0x40000000,
97 GRALLOC_USAGE_PRIVATE_3 = 0x80000000,
98 GRALLOC_USAGE_PRIVATE_MASK = 0xF0000000,
Mathias Agopiana8a75162009-04-10 14:24:31 -070099};
100
Mathias Agopiana8a75162009-04-10 14:24:31 -0700101/*****************************************************************************/
102
Mathias Agopiana8a75162009-04-10 14:24:31 -0700103/**
104 * Every hardware module must have a data structure named HAL_MODULE_INFO_SYM
105 * and the fields of this data structure must begin with hw_module_t
106 * followed by module specific information.
107 */
Mathias Agopian9d82c1a2009-08-19 11:20:55 -0700108typedef struct gralloc_module_t {
Mathias Agopiana8a75162009-04-10 14:24:31 -0700109 struct hw_module_t common;
110
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700111 /*
112 * (*registerBuffer)() must be called before a buffer_handle_t that has not
113 * been created with (*alloc_device_t::alloc)() can be used.
114 *
115 * This is intended to be used with buffer_handle_t's that have been
116 * received in this process through IPC.
117 *
118 * This function checks that the handle is indeed a valid one and prepares
119 * it for use with (*lock)() and (*unlock)().
120 *
121 * It is not necessary to call (*registerBuffer)() on a handle created
122 * with (*alloc_device_t::alloc)().
123 *
124 * returns an error if this buffer_handle_t is not valid.
125 */
126 int (*registerBuffer)(struct gralloc_module_t const* module,
127 buffer_handle_t handle);
Mathias Agopiana8a75162009-04-10 14:24:31 -0700128
129 /*
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700130 * (*unregisterBuffer)() is called once this handle is no longer needed in
131 * this process. After this call, it is an error to call (*lock)(),
132 * (*unlock)(), or (*registerBuffer)().
Mathias Agopiana8a75162009-04-10 14:24:31 -0700133 *
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700134 * This function doesn't close or free the handle itself; this is done
135 * by other means, usually through libcutils's native_handle_close() and
136 * native_handle_free().
137 *
138 * It is an error to call (*unregisterBuffer)() on a buffer that wasn't
139 * explicitly registered first.
Mathias Agopiana8a75162009-04-10 14:24:31 -0700140 */
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700141 int (*unregisterBuffer)(struct gralloc_module_t const* module,
142 buffer_handle_t handle);
Mathias Agopiana8a75162009-04-10 14:24:31 -0700143
144 /*
145 * The (*lock)() method is called before a buffer is accessed for the
146 * specified usage. This call may block, for instance if the h/w needs
147 * to finish rendering or if CPU caches need to be synchronized.
148 *
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700149 * The caller promises to modify only pixels in the area specified
150 * by (l,t,w,h).
Mathias Agopiana8a75162009-04-10 14:24:31 -0700151 *
152 * The content of the buffer outside of the specified area is NOT modified
153 * by this call.
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700154 *
155 * If usage specifies GRALLOC_USAGE_SW_*, vaddr is filled with the address
156 * of the buffer in virtual memory.
157 *
Mathias Agopian485e6982009-05-05 20:21:57 -0700158 * THREADING CONSIDERATIONS:
159 *
160 * It is legal for several different threads to lock a buffer from
161 * read access, none of the threads are blocked.
162 *
163 * However, locking a buffer simultaneously for write or read/write is
164 * undefined, but:
165 * - shall not result in termination of the process
166 * - shall not block the caller
167 * It is acceptable to return an error or to leave the buffer's content
168 * into an indeterminate state.
169 *
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700170 * If the buffer was created with a usage mask incompatible with the
171 * requested usage flags here, -EINVAL is returned.
Mathias Agopiana8a75162009-04-10 14:24:31 -0700172 *
173 */
174
175 int (*lock)(struct gralloc_module_t const* module,
176 buffer_handle_t handle, int usage,
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700177 int l, int t, int w, int h,
178 void** vaddr);
Mathias Agopiana8a75162009-04-10 14:24:31 -0700179
180
181 /*
182 * The (*unlock)() method must be called after all changes to the buffer
183 * are completed.
184 */
185
186 int (*unlock)(struct gralloc_module_t const* module,
187 buffer_handle_t handle);
188
Mathias Agopiancd2433f2009-10-29 18:29:39 -0700189
Mathias Agopian8255d9d2009-09-17 16:15:36 -0700190 /* reserved for future use */
Mathias Agopiancd2433f2009-10-29 18:29:39 -0700191 int (*perform)(struct gralloc_module_t const* module,
192 int operation, ... );
193
194 /* reserved for future use */
195 void* reserved_proc[7];
Mathias Agopian9d82c1a2009-08-19 11:20:55 -0700196} gralloc_module_t;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700197
198/*****************************************************************************/
199
200/**
201 * Every device data structure must begin with hw_device_t
202 * followed by module specific public methods and attributes.
203 */
204
Mathias Agopian9d82c1a2009-08-19 11:20:55 -0700205typedef struct alloc_device_t {
Mathias Agopiana8a75162009-04-10 14:24:31 -0700206 struct hw_device_t common;
207
208 /*
209 * (*alloc)() Allocates a buffer in graphic memory with the requested
210 * parameters and returns a buffer_handle_t and the stride in pixels to
211 * allow the implementation to satisfy hardware constraints on the width
212 * of a pixmap (eg: it may have to be multiple of 8 pixels).
213 * The CALLER TAKES OWNERSHIP of the buffer_handle_t.
214 *
215 * Returns 0 on success or -errno on error.
216 */
217
218 int (*alloc)(struct alloc_device_t* dev,
219 int w, int h, int format, int usage,
220 buffer_handle_t* handle, int* stride);
221
222 /*
223 * (*free)() Frees a previously allocated buffer.
224 * Behavior is undefined if the buffer is still mapped in any process,
225 * but shall not result in termination of the program or security breaches
226 * (allowing a process to get access to another process' buffers).
227 * THIS FUNCTION TAKES OWNERSHIP of the buffer_handle_t which becomes
228 * invalid after the call.
229 *
230 * Returns 0 on success or -errno on error.
231 */
232 int (*free)(struct alloc_device_t* dev,
233 buffer_handle_t handle);
234
Erik Gilling158549c2010-12-01 16:34:08 -0800235 /* This hook is OPTIONAL.
236 *
237 * If non NULL it will be caused by SurfaceFlinger on dumpsys
238 */
239 void (*dump)(struct alloc_device_t *dev, char *buff, int buff_len);
240
241 void* reserved_proc[7];
Mathias Agopian9d82c1a2009-08-19 11:20:55 -0700242} alloc_device_t;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700243
244
Mathias Agopiana8a75162009-04-10 14:24:31 -0700245/** convenience API for opening and closing a supported device */
246
247static inline int gralloc_open(const struct hw_module_t* module,
248 struct alloc_device_t** device) {
249 return module->methods->open(module,
250 GRALLOC_HARDWARE_GPU0, (struct hw_device_t**)device);
251}
252
253static inline int gralloc_close(struct alloc_device_t* device) {
254 return device->common.close(&device->common);
255}
256
Mathias Agopiana8a75162009-04-10 14:24:31 -0700257__END_DECLS
258
259#endif // ANDROID_ALLOC_INTERFACE_H