blob: a6d9c1f3e0438c9ce3160cdca598d95570a63087 [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 */
Jamie Gennis29ead942011-11-21 16:50:49 -080050 GRALLOC_USAGE_SW_READ_NEVER = 0x00000000,
Mathias Agopiana8a75162009-04-10 14:24:31 -070051 /* buffer is rarely read in software */
Jamie Gennis29ead942011-11-21 16:50:49 -080052 GRALLOC_USAGE_SW_READ_RARELY = 0x00000002,
Mathias Agopiana8a75162009-04-10 14:24:31 -070053 /* buffer is often read in software */
Jamie Gennis29ead942011-11-21 16:50:49 -080054 GRALLOC_USAGE_SW_READ_OFTEN = 0x00000003,
Mathias Agopiana8a75162009-04-10 14:24:31 -070055 /* mask for the software read values */
Jamie Gennis29ead942011-11-21 16:50:49 -080056 GRALLOC_USAGE_SW_READ_MASK = 0x0000000F,
Mathias Agopiana8a75162009-04-10 14:24:31 -070057
58 /* buffer is never written in software */
Jamie Gennis29ead942011-11-21 16:50:49 -080059 GRALLOC_USAGE_SW_WRITE_NEVER = 0x00000000,
Greg Hackmannd6f7aad2012-12-07 10:32:58 -080060 /* buffer is rarely written in software */
Jamie Gennis29ead942011-11-21 16:50:49 -080061 GRALLOC_USAGE_SW_WRITE_RARELY = 0x00000020,
Greg Hackmannd6f7aad2012-12-07 10:32:58 -080062 /* buffer is often written in software */
Jamie Gennis29ead942011-11-21 16:50:49 -080063 GRALLOC_USAGE_SW_WRITE_OFTEN = 0x00000030,
Mathias Agopiana8a75162009-04-10 14:24:31 -070064 /* mask for the software write values */
Jamie Gennis29ead942011-11-21 16:50:49 -080065 GRALLOC_USAGE_SW_WRITE_MASK = 0x000000F0,
Mathias Agopiana8a75162009-04-10 14:24:31 -070066
67 /* buffer will be used as an OpenGL ES texture */
Jamie Gennis29ead942011-11-21 16:50:49 -080068 GRALLOC_USAGE_HW_TEXTURE = 0x00000100,
Mathias Agopiana8a75162009-04-10 14:24:31 -070069 /* buffer will be used as an OpenGL ES render target */
Jamie Gennis29ead942011-11-21 16:50:49 -080070 GRALLOC_USAGE_HW_RENDER = 0x00000200,
Mathias Agopiana8a75162009-04-10 14:24:31 -070071 /* buffer will be used by the 2D hardware blitter */
Jamie Gennis29ead942011-11-21 16:50:49 -080072 GRALLOC_USAGE_HW_2D = 0x00000400,
Jamie Gennis4b560d52011-08-10 11:41:52 -070073 /* buffer will be used by the HWComposer HAL module */
Jamie Gennis29ead942011-11-21 16:50:49 -080074 GRALLOC_USAGE_HW_COMPOSER = 0x00000800,
Mathias Agopiana8a75162009-04-10 14:24:31 -070075 /* buffer will be used with the framebuffer device */
Jamie Gennis29ead942011-11-21 16:50:49 -080076 GRALLOC_USAGE_HW_FB = 0x00001000,
77 /* buffer will be used with the HW video encoder */
78 GRALLOC_USAGE_HW_VIDEO_ENCODER = 0x00010000,
Eino-Ville Talvala7797d752012-07-16 14:50:50 -070079 /* 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,
Eino-Ville Talvala2388a2d2012-08-28 14:01:26 -070083 /* buffer will be used as part of zero-shutter-lag queue */
Eino-Ville Talvala7f8dd0a2012-09-04 14:21:07 -070084 GRALLOC_USAGE_HW_CAMERA_ZSL = 0x00060000,
85 /* mask for the camera access values */
86 GRALLOC_USAGE_HW_CAMERA_MASK = 0x00060000,
Mathias Agopiana8a75162009-04-10 14:24:31 -070087 /* mask for the software usage bit-mask */
Eino-Ville Talvala7797d752012-07-16 14:50:50 -070088 GRALLOC_USAGE_HW_MASK = 0x00071F00,
Jamie Gennis95d78be2010-07-01 16:49:52 -070089
Jamie Gennis7edeaf92010-11-17 18:51:17 -080090 /* buffer should be displayed full-screen on an external display when
91 * possible
92 */
Jamie Gennis29ead942011-11-21 16:50:49 -080093 GRALLOC_USAGE_EXTERNAL_DISP = 0x00002000,
Jamie Gennis7edeaf92010-11-17 18:51:17 -080094
Glenn Kasten7cb277a2011-01-19 16:58:31 -080095 /* 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.
100 */
Jamie Gennis29ead942011-11-21 16:50:49 -0800101 GRALLOC_USAGE_PROTECTED = 0x00004000,
Glenn Kasten7cb277a2011-01-19 16:58:31 -0800102
Jamie Gennis95d78be2010-07-01 16:49:52 -0700103 /* implementation-specific private usage flags */
Jamie Gennis29ead942011-11-21 16:50:49 -0800104 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,
Mathias Agopiana8a75162009-04-10 14:24:31 -0700109};
110
Mathias Agopiana8a75162009-04-10 14:24:31 -0700111/*****************************************************************************/
112
Mathias Agopiana8a75162009-04-10 14:24:31 -0700113/**
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.
117 */
Mathias Agopian9d82c1a2009-08-19 11:20:55 -0700118typedef struct gralloc_module_t {
Mathias Agopiana8a75162009-04-10 14:24:31 -0700119 struct hw_module_t common;
120
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700121 /*
122 * (*registerBuffer)() must be called before a buffer_handle_t that has not
123 * been created with (*alloc_device_t::alloc)() can be used.
124 *
125 * This is intended to be used with buffer_handle_t's that have been
126 * received in this process through IPC.
127 *
128 * This function checks that the handle is indeed a valid one and prepares
129 * it for use with (*lock)() and (*unlock)().
130 *
131 * It is not necessary to call (*registerBuffer)() on a handle created
132 * with (*alloc_device_t::alloc)().
133 *
134 * returns an error if this buffer_handle_t is not valid.
135 */
136 int (*registerBuffer)(struct gralloc_module_t const* module,
137 buffer_handle_t handle);
Mathias Agopiana8a75162009-04-10 14:24:31 -0700138
139 /*
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700140 * (*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)().
Mathias Agopiana8a75162009-04-10 14:24:31 -0700143 *
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700144 * 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().
147 *
148 * It is an error to call (*unregisterBuffer)() on a buffer that wasn't
149 * explicitly registered first.
Mathias Agopiana8a75162009-04-10 14:24:31 -0700150 */
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700151 int (*unregisterBuffer)(struct gralloc_module_t const* module,
152 buffer_handle_t handle);
Mathias Agopiana8a75162009-04-10 14:24:31 -0700153
154 /*
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.
158 *
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700159 * The caller promises to modify only pixels in the area specified
160 * by (l,t,w,h).
Mathias Agopiana8a75162009-04-10 14:24:31 -0700161 *
162 * The content of the buffer outside of the specified area is NOT modified
163 * by this call.
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700164 *
165 * If usage specifies GRALLOC_USAGE_SW_*, vaddr is filled with the address
166 * of the buffer in virtual memory.
167 *
Mathias Agopian485e6982009-05-05 20:21:57 -0700168 * THREADING CONSIDERATIONS:
169 *
170 * It is legal for several different threads to lock a buffer from
171 * read access, none of the threads are blocked.
172 *
173 * However, locking a buffer simultaneously for write or read/write is
174 * undefined, but:
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.
179 *
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700180 * If the buffer was created with a usage mask incompatible with the
181 * requested usage flags here, -EINVAL is returned.
Mathias Agopiana8a75162009-04-10 14:24:31 -0700182 *
183 */
184
185 int (*lock)(struct gralloc_module_t const* module,
186 buffer_handle_t handle, int usage,
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700187 int l, int t, int w, int h,
188 void** vaddr);
Mathias Agopiana8a75162009-04-10 14:24:31 -0700189
190
191 /*
192 * The (*unlock)() method must be called after all changes to the buffer
193 * are completed.
194 */
195
196 int (*unlock)(struct gralloc_module_t const* module,
197 buffer_handle_t handle);
198
Mathias Agopiancd2433f2009-10-29 18:29:39 -0700199
Mathias Agopian8255d9d2009-09-17 16:15:36 -0700200 /* reserved for future use */
Mathias Agopiancd2433f2009-10-29 18:29:39 -0700201 int (*perform)(struct gralloc_module_t const* module,
202 int operation, ... );
203
204 /* reserved for future use */
205 void* reserved_proc[7];
Mathias Agopian9d82c1a2009-08-19 11:20:55 -0700206} gralloc_module_t;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700207
208/*****************************************************************************/
209
210/**
211 * Every device data structure must begin with hw_device_t
212 * followed by module specific public methods and attributes.
213 */
214
Mathias Agopian9d82c1a2009-08-19 11:20:55 -0700215typedef struct alloc_device_t {
Mathias Agopiana8a75162009-04-10 14:24:31 -0700216 struct hw_device_t common;
217
218 /*
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.
224 *
225 * Returns 0 on success or -errno on error.
226 */
227
228 int (*alloc)(struct alloc_device_t* dev,
229 int w, int h, int format, int usage,
230 buffer_handle_t* handle, int* stride);
231
232 /*
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.
239 *
240 * Returns 0 on success or -errno on error.
241 */
242 int (*free)(struct alloc_device_t* dev,
243 buffer_handle_t handle);
244
Erik Gilling158549c2010-12-01 16:34:08 -0800245 /* This hook is OPTIONAL.
246 *
247 * If non NULL it will be caused by SurfaceFlinger on dumpsys
248 */
249 void (*dump)(struct alloc_device_t *dev, char *buff, int buff_len);
250
251 void* reserved_proc[7];
Mathias Agopian9d82c1a2009-08-19 11:20:55 -0700252} alloc_device_t;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700253
254
Mathias Agopiana8a75162009-04-10 14:24:31 -0700255/** convenience API for opening and closing a supported device */
256
257static 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);
261}
262
263static inline int gralloc_close(struct alloc_device_t* device) {
264 return device->common.close(&device->common);
265}
266
Mathias Agopiana8a75162009-04-10 14:24:31 -0700267__END_DECLS
268
Glenn Kastenbb56a102011-11-30 08:31:17 -0800269#endif // ANDROID_GRALLOC_INTERFACE_H