blob: 07ac0290b45bdd7a52be6ed6bf0f9f49aaae9eb4 [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>
Alex Ray06e3bf22013-03-19 23:13:56 -070022#include <system/graphics.h>
Mathias Agopiana8a75162009-04-10 14:24:31 -070023#include <hardware/hardware.h>
24
25#include <stdint.h>
26#include <sys/cdefs.h>
27#include <sys/types.h>
28
Mathias Agopian5337b102011-04-28 18:56:10 -070029#include <cutils/native_handle.h>
30
31#include <hardware/hardware.h>
32#include <hardware/fb.h>
33
Mathias Agopiana8a75162009-04-10 14:24:31 -070034__BEGIN_DECLS
35
Alex Ray06e3bf22013-03-19 23:13:56 -070036/**
37 * Module versioning information for the Gralloc hardware module, based on
38 * gralloc_module_t.common.module_api_version.
39 *
40 * Version History:
41 *
42 * GRALLOC_MODULE_API_VERSION_0_1:
43 * Initial Gralloc hardware module API.
44 *
45 * GRALLOC_MODULE_API_VERSION_0_2:
46 * Add support for flexible YCbCr format with (*lock_ycbcr)() method.
Francis Hart2e49f9a2014-04-01 11:10:32 +030047 *
48 * GRALLOC_MODULE_API_VERSION_0_3:
49 * Add support for fence passing to/from lock/unlock.
Alex Ray06e3bf22013-03-19 23:13:56 -070050 */
51
52#define GRALLOC_MODULE_API_VERSION_0_1 HARDWARE_MODULE_API_VERSION(0, 1)
53#define GRALLOC_MODULE_API_VERSION_0_2 HARDWARE_MODULE_API_VERSION(0, 2)
Francis Hart2e49f9a2014-04-01 11:10:32 +030054#define GRALLOC_MODULE_API_VERSION_0_3 HARDWARE_MODULE_API_VERSION(0, 3)
Alex Ray06e3bf22013-03-19 23:13:56 -070055
56#define GRALLOC_DEVICE_API_VERSION_0_1 HARDWARE_DEVICE_API_VERSION(0, 1)
Erik Gillinge9952042010-12-07 18:46:04 -080057
Mathias Agopiana8a75162009-04-10 14:24:31 -070058/**
59 * The id of this module
60 */
61#define GRALLOC_HARDWARE_MODULE_ID "gralloc"
62
63/**
64 * Name of the graphics device to open
65 */
66
Mathias Agopiana8a75162009-04-10 14:24:31 -070067#define GRALLOC_HARDWARE_GPU0 "gpu0"
68
69enum {
70 /* buffer is never read in software */
Jamie Gennis29ead942011-11-21 16:50:49 -080071 GRALLOC_USAGE_SW_READ_NEVER = 0x00000000,
Mathias Agopiana8a75162009-04-10 14:24:31 -070072 /* buffer is rarely read in software */
Jamie Gennis29ead942011-11-21 16:50:49 -080073 GRALLOC_USAGE_SW_READ_RARELY = 0x00000002,
Mathias Agopiana8a75162009-04-10 14:24:31 -070074 /* buffer is often read in software */
Jamie Gennis29ead942011-11-21 16:50:49 -080075 GRALLOC_USAGE_SW_READ_OFTEN = 0x00000003,
Mathias Agopiana8a75162009-04-10 14:24:31 -070076 /* mask for the software read values */
Jamie Gennis29ead942011-11-21 16:50:49 -080077 GRALLOC_USAGE_SW_READ_MASK = 0x0000000F,
Dan Stozafd2d8592015-06-03 10:42:38 -070078
Mathias Agopiana8a75162009-04-10 14:24:31 -070079 /* buffer is never written in software */
Jamie Gennis29ead942011-11-21 16:50:49 -080080 GRALLOC_USAGE_SW_WRITE_NEVER = 0x00000000,
Greg Hackmannd6f7aad2012-12-07 10:32:58 -080081 /* buffer is rarely written in software */
Jamie Gennis29ead942011-11-21 16:50:49 -080082 GRALLOC_USAGE_SW_WRITE_RARELY = 0x00000020,
Greg Hackmannd6f7aad2012-12-07 10:32:58 -080083 /* buffer is often written in software */
Jamie Gennis29ead942011-11-21 16:50:49 -080084 GRALLOC_USAGE_SW_WRITE_OFTEN = 0x00000030,
Mathias Agopiana8a75162009-04-10 14:24:31 -070085 /* mask for the software write values */
Jamie Gennis29ead942011-11-21 16:50:49 -080086 GRALLOC_USAGE_SW_WRITE_MASK = 0x000000F0,
Mathias Agopiana8a75162009-04-10 14:24:31 -070087
88 /* buffer will be used as an OpenGL ES texture */
Jamie Gennis29ead942011-11-21 16:50:49 -080089 GRALLOC_USAGE_HW_TEXTURE = 0x00000100,
Mathias Agopiana8a75162009-04-10 14:24:31 -070090 /* buffer will be used as an OpenGL ES render target */
Jamie Gennis29ead942011-11-21 16:50:49 -080091 GRALLOC_USAGE_HW_RENDER = 0x00000200,
Mathias Agopiana8a75162009-04-10 14:24:31 -070092 /* buffer will be used by the 2D hardware blitter */
Jamie Gennis29ead942011-11-21 16:50:49 -080093 GRALLOC_USAGE_HW_2D = 0x00000400,
Jamie Gennis4b560d52011-08-10 11:41:52 -070094 /* buffer will be used by the HWComposer HAL module */
Jamie Gennis29ead942011-11-21 16:50:49 -080095 GRALLOC_USAGE_HW_COMPOSER = 0x00000800,
Mathias Agopiana8a75162009-04-10 14:24:31 -070096 /* buffer will be used with the framebuffer device */
Jamie Gennis29ead942011-11-21 16:50:49 -080097 GRALLOC_USAGE_HW_FB = 0x00001000,
Dan Stozafd2d8592015-06-03 10:42:38 -070098
99 /* buffer should be displayed full-screen on an external display when
100 * possible */
101 GRALLOC_USAGE_EXTERNAL_DISP = 0x00002000,
102
103 /* Must have a hardware-protected path to external display sink for
104 * this buffer. If a hardware-protected path is not available, then
105 * either don't composite only this buffer (preferred) to the
106 * external sink, or (less desirable) do not route the entire
107 * composition to the external sink. */
108 GRALLOC_USAGE_PROTECTED = 0x00004000,
109
110 /* buffer may be used as a cursor */
111 GRALLOC_USAGE_CURSOR = 0x00008000,
112
Jamie Gennis29ead942011-11-21 16:50:49 -0800113 /* buffer will be used with the HW video encoder */
114 GRALLOC_USAGE_HW_VIDEO_ENCODER = 0x00010000,
Eino-Ville Talvala7797d752012-07-16 14:50:50 -0700115 /* buffer will be written by the HW camera pipeline */
116 GRALLOC_USAGE_HW_CAMERA_WRITE = 0x00020000,
117 /* buffer will be read by the HW camera pipeline */
118 GRALLOC_USAGE_HW_CAMERA_READ = 0x00040000,
Eino-Ville Talvala2388a2d2012-08-28 14:01:26 -0700119 /* buffer will be used as part of zero-shutter-lag queue */
Eino-Ville Talvala7f8dd0a2012-09-04 14:21:07 -0700120 GRALLOC_USAGE_HW_CAMERA_ZSL = 0x00060000,
121 /* mask for the camera access values */
122 GRALLOC_USAGE_HW_CAMERA_MASK = 0x00060000,
Mathias Agopiana8a75162009-04-10 14:24:31 -0700123 /* mask for the software usage bit-mask */
Eino-Ville Talvala7797d752012-07-16 14:50:50 -0700124 GRALLOC_USAGE_HW_MASK = 0x00071F00,
Jamie Gennis95d78be2010-07-01 16:49:52 -0700125
Jason Sams22423852013-07-16 12:51:13 -0700126 /* buffer will be used as a RenderScript Allocation */
127 GRALLOC_USAGE_RENDERSCRIPT = 0x00100000,
128
Dan Stoza201c7d92015-05-29 12:43:19 -0700129 /* Set by the consumer to indicate to the producer that they may attach a
130 * buffer that they did not detach from the BufferQueue. Will be filtered
131 * out by GRALLOC_USAGE_ALLOC_MASK, so gralloc modules will not need to
132 * handle this flag. */
Chong Zhang2e3ed7e2015-06-02 19:00:11 -0700133 GRALLOC_USAGE_FOREIGN_BUFFERS = 0x00200000,
Dan Stoza201c7d92015-05-29 12:43:19 -0700134
135 /* Mask of all flags which could be passed to a gralloc module for buffer
136 * allocation. Any flags not in this mask do not need to be handled by
137 * gralloc modules. */
138 GRALLOC_USAGE_ALLOC_MASK = ~(GRALLOC_USAGE_FOREIGN_BUFFERS),
139
Jamie Gennis95d78be2010-07-01 16:49:52 -0700140 /* implementation-specific private usage flags */
Jamie Gennis29ead942011-11-21 16:50:49 -0800141 GRALLOC_USAGE_PRIVATE_0 = 0x10000000,
142 GRALLOC_USAGE_PRIVATE_1 = 0x20000000,
143 GRALLOC_USAGE_PRIVATE_2 = 0x40000000,
144 GRALLOC_USAGE_PRIVATE_3 = 0x80000000,
145 GRALLOC_USAGE_PRIVATE_MASK = 0xF0000000,
Daniel Hillenbranda000ff12014-11-19 08:21:27 -0500146
147#ifdef EXYNOS4_ENHANCEMENTS
148 /* SAMSUNG */
149 GRALLOC_USAGE_PRIVATE_NONECACHE = 0x00800000,
150
151 GRALLOC_USAGE_HW_FIMC1 = 0x01000000,
152 GRALLOC_USAGE_HW_ION = 0x02000000,
153 GRALLOC_USAGE_YUV_ADDR = 0x04000000,
154 GRALLOC_USAGE_CAMERA = 0x08000000,
155
156 /* SEC Private usage , for Overlay path at HWC */
157 GRALLOC_USAGE_HWC_HWOVERLAY = 0x20000000,
158#endif
Mathias Agopiana8a75162009-04-10 14:24:31 -0700159};
160
Mathias Agopiana8a75162009-04-10 14:24:31 -0700161/*****************************************************************************/
162
Mathias Agopiana8a75162009-04-10 14:24:31 -0700163/**
164 * Every hardware module must have a data structure named HAL_MODULE_INFO_SYM
165 * and the fields of this data structure must begin with hw_module_t
166 * followed by module specific information.
167 */
Mathias Agopian9d82c1a2009-08-19 11:20:55 -0700168typedef struct gralloc_module_t {
Mathias Agopiana8a75162009-04-10 14:24:31 -0700169 struct hw_module_t common;
170
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700171 /*
172 * (*registerBuffer)() must be called before a buffer_handle_t that has not
173 * been created with (*alloc_device_t::alloc)() can be used.
174 *
175 * This is intended to be used with buffer_handle_t's that have been
176 * received in this process through IPC.
177 *
178 * This function checks that the handle is indeed a valid one and prepares
179 * it for use with (*lock)() and (*unlock)().
180 *
181 * It is not necessary to call (*registerBuffer)() on a handle created
182 * with (*alloc_device_t::alloc)().
183 *
184 * returns an error if this buffer_handle_t is not valid.
185 */
186 int (*registerBuffer)(struct gralloc_module_t const* module,
187 buffer_handle_t handle);
Mathias Agopiana8a75162009-04-10 14:24:31 -0700188
189 /*
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700190 * (*unregisterBuffer)() is called once this handle is no longer needed in
191 * this process. After this call, it is an error to call (*lock)(),
192 * (*unlock)(), or (*registerBuffer)().
Mathias Agopiana8a75162009-04-10 14:24:31 -0700193 *
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700194 * This function doesn't close or free the handle itself; this is done
195 * by other means, usually through libcutils's native_handle_close() and
196 * native_handle_free().
197 *
198 * It is an error to call (*unregisterBuffer)() on a buffer that wasn't
199 * explicitly registered first.
Mathias Agopiana8a75162009-04-10 14:24:31 -0700200 */
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700201 int (*unregisterBuffer)(struct gralloc_module_t const* module,
202 buffer_handle_t handle);
Mathias Agopiana8a75162009-04-10 14:24:31 -0700203
204 /*
205 * The (*lock)() method is called before a buffer is accessed for the
206 * specified usage. This call may block, for instance if the h/w needs
207 * to finish rendering or if CPU caches need to be synchronized.
208 *
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700209 * The caller promises to modify only pixels in the area specified
210 * by (l,t,w,h).
Mathias Agopiana8a75162009-04-10 14:24:31 -0700211 *
212 * The content of the buffer outside of the specified area is NOT modified
213 * by this call.
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700214 *
215 * If usage specifies GRALLOC_USAGE_SW_*, vaddr is filled with the address
216 * of the buffer in virtual memory.
217 *
Alex Ray06e3bf22013-03-19 23:13:56 -0700218 * Note calling (*lock)() on HAL_PIXEL_FORMAT_YCbCr_*_888 buffers will fail
219 * and return -EINVAL. These buffers must be locked with (*lock_ycbcr)()
220 * instead.
221 *
Mathias Agopian485e6982009-05-05 20:21:57 -0700222 * THREADING CONSIDERATIONS:
223 *
224 * It is legal for several different threads to lock a buffer from
225 * read access, none of the threads are blocked.
226 *
227 * However, locking a buffer simultaneously for write or read/write is
228 * undefined, but:
229 * - shall not result in termination of the process
230 * - shall not block the caller
231 * It is acceptable to return an error or to leave the buffer's content
232 * into an indeterminate state.
233 *
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700234 * If the buffer was created with a usage mask incompatible with the
235 * requested usage flags here, -EINVAL is returned.
Mathias Agopiana8a75162009-04-10 14:24:31 -0700236 *
237 */
238
239 int (*lock)(struct gralloc_module_t const* module,
240 buffer_handle_t handle, int usage,
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700241 int l, int t, int w, int h,
242 void** vaddr);
Mathias Agopiana8a75162009-04-10 14:24:31 -0700243
244
245 /*
246 * The (*unlock)() method must be called after all changes to the buffer
247 * are completed.
248 */
249
250 int (*unlock)(struct gralloc_module_t const* module,
251 buffer_handle_t handle);
252
Daniel Hillenbranda000ff12014-11-19 08:21:27 -0500253#ifdef EXYNOS4_ENHANCEMENTS
254 int (*getphys) (struct gralloc_module_t const* module,
255 buffer_handle_t handle, void** paddr);
256#endif
Mathias Agopiancd2433f2009-10-29 18:29:39 -0700257
Mathias Agopian8255d9d2009-09-17 16:15:36 -0700258 /* reserved for future use */
Mathias Agopiancd2433f2009-10-29 18:29:39 -0700259 int (*perform)(struct gralloc_module_t const* module,
260 int operation, ... );
261
Alex Ray06e3bf22013-03-19 23:13:56 -0700262 /*
263 * The (*lock_ycbcr)() method is like the (*lock)() method, with the
264 * difference that it fills a struct ycbcr with a description of the buffer
265 * layout, and zeroes out the reserved fields.
266 *
Lajos Molnar811677e2015-01-30 11:01:47 -0800267 * If the buffer format is not compatible with a flexible YUV format (e.g.
268 * the buffer layout cannot be represented with the ycbcr struct), it
269 * will return -EINVAL.
270 *
271 * This method must work on buffers with HAL_PIXEL_FORMAT_YCbCr_*_888
272 * if supported by the device, as well as with any other format that is
273 * requested by the multimedia codecs when they are configured with a
274 * flexible-YUV-compatible color-format with android native buffers.
275 *
276 * Note that this method may also be called on buffers of other formats,
277 * including non-YUV formats.
Alex Ray06e3bf22013-03-19 23:13:56 -0700278 *
279 * Added in GRALLOC_MODULE_API_VERSION_0_2.
280 */
281
282 int (*lock_ycbcr)(struct gralloc_module_t const* module,
283 buffer_handle_t handle, int usage,
284 int l, int t, int w, int h,
285 struct android_ycbcr *ycbcr);
286
Francis Hart2e49f9a2014-04-01 11:10:32 +0300287 /*
288 * The (*lockAsync)() method is like the (*lock)() method except
289 * that the buffer's sync fence object is passed into the lock
290 * call instead of requiring the caller to wait for completion.
291 *
292 * The gralloc implementation takes ownership of the fenceFd and
293 * is responsible for closing it when no longer needed.
294 *
295 * Added in GRALLOC_MODULE_API_VERSION_0_3.
296 */
297 int (*lockAsync)(struct gralloc_module_t const* module,
298 buffer_handle_t handle, int usage,
299 int l, int t, int w, int h,
300 void** vaddr, int fenceFd);
301
302 /*
303 * The (*unlockAsync)() method is like the (*unlock)() method
304 * except that a buffer sync fence object is returned from the
305 * lock call, representing the completion of any pending work
306 * performed by the gralloc implementation.
307 *
308 * The caller takes ownership of the fenceFd and is responsible
309 * for closing it when no longer needed.
310 *
311 * Added in GRALLOC_MODULE_API_VERSION_0_3.
312 */
313 int (*unlockAsync)(struct gralloc_module_t const* module,
314 buffer_handle_t handle, int* fenceFd);
315
316 /*
317 * The (*lockAsync_ycbcr)() method is like the (*lock_ycbcr)()
318 * method except that the buffer's sync fence object is passed
319 * into the lock call instead of requiring the caller to wait for
320 * completion.
321 *
322 * The gralloc implementation takes ownership of the fenceFd and
323 * is responsible for closing it when no longer needed.
324 *
325 * Added in GRALLOC_MODULE_API_VERSION_0_3.
326 */
327 int (*lockAsync_ycbcr)(struct gralloc_module_t const* module,
328 buffer_handle_t handle, int usage,
329 int l, int t, int w, int h,
330 struct android_ycbcr *ycbcr, int fenceFd);
331
Mathias Agopiancd2433f2009-10-29 18:29:39 -0700332 /* reserved for future use */
Francis Hart2e49f9a2014-04-01 11:10:32 +0300333 void* reserved_proc[3];
Mathias Agopian9d82c1a2009-08-19 11:20:55 -0700334} gralloc_module_t;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700335
336/*****************************************************************************/
337
338/**
339 * Every device data structure must begin with hw_device_t
340 * followed by module specific public methods and attributes.
341 */
342
Mathias Agopian9d82c1a2009-08-19 11:20:55 -0700343typedef struct alloc_device_t {
Mathias Agopiana8a75162009-04-10 14:24:31 -0700344 struct hw_device_t common;
345
346 /*
347 * (*alloc)() Allocates a buffer in graphic memory with the requested
348 * parameters and returns a buffer_handle_t and the stride in pixels to
349 * allow the implementation to satisfy hardware constraints on the width
350 * of a pixmap (eg: it may have to be multiple of 8 pixels).
351 * The CALLER TAKES OWNERSHIP of the buffer_handle_t.
Eino-Ville Talvala6efab252013-05-06 14:05:05 -0700352 *
353 * If format is HAL_PIXEL_FORMAT_YCbCr_420_888, the returned stride must be
354 * 0, since the actual strides are available from the android_ycbcr
355 * structure.
Mathias Agopiana8a75162009-04-10 14:24:31 -0700356 *
357 * Returns 0 on success or -errno on error.
358 */
359
360 int (*alloc)(struct alloc_device_t* dev,
361 int w, int h, int format, int usage,
362 buffer_handle_t* handle, int* stride);
363
364 /*
365 * (*free)() Frees a previously allocated buffer.
366 * Behavior is undefined if the buffer is still mapped in any process,
367 * but shall not result in termination of the program or security breaches
368 * (allowing a process to get access to another process' buffers).
369 * THIS FUNCTION TAKES OWNERSHIP of the buffer_handle_t which becomes
370 * invalid after the call.
371 *
372 * Returns 0 on success or -errno on error.
373 */
374 int (*free)(struct alloc_device_t* dev,
375 buffer_handle_t handle);
376
Erik Gilling158549c2010-12-01 16:34:08 -0800377 /* This hook is OPTIONAL.
378 *
379 * If non NULL it will be caused by SurfaceFlinger on dumpsys
380 */
381 void (*dump)(struct alloc_device_t *dev, char *buff, int buff_len);
382
383 void* reserved_proc[7];
Mathias Agopian9d82c1a2009-08-19 11:20:55 -0700384} alloc_device_t;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700385
386
Mathias Agopiana8a75162009-04-10 14:24:31 -0700387/** convenience API for opening and closing a supported device */
388
389static inline int gralloc_open(const struct hw_module_t* module,
390 struct alloc_device_t** device) {
391 return module->methods->open(module,
392 GRALLOC_HARDWARE_GPU0, (struct hw_device_t**)device);
393}
394
395static inline int gralloc_close(struct alloc_device_t* device) {
396 return device->common.close(&device->common);
397}
398
Mathias Agopiana8a75162009-04-10 14:24:31 -0700399__END_DECLS
400
Glenn Kastenbb56a102011-11-30 08:31:17 -0800401#endif // ANDROID_GRALLOC_INTERFACE_H