Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 1 | /* |
| 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 Malchev | 33c0fe0 | 2011-05-02 18:58:16 -0700 | [diff] [blame] | 21 | #include <system/window.h> |
Alex Ray | 06e3bf2 | 2013-03-19 23:13:56 -0700 | [diff] [blame^] | 22 | #include <system/graphics.h> |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 23 | #include <hardware/hardware.h> |
| 24 | |
| 25 | #include <stdint.h> |
| 26 | #include <sys/cdefs.h> |
| 27 | #include <sys/types.h> |
| 28 | |
Mathias Agopian | 5337b10 | 2011-04-28 18:56:10 -0700 | [diff] [blame] | 29 | #include <cutils/native_handle.h> |
| 30 | |
| 31 | #include <hardware/hardware.h> |
| 32 | #include <hardware/fb.h> |
| 33 | |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 34 | __BEGIN_DECLS |
| 35 | |
Alex Ray | 06e3bf2 | 2013-03-19 23:13:56 -0700 | [diff] [blame^] | 36 | /** |
| 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. |
| 47 | */ |
| 48 | |
| 49 | #define GRALLOC_MODULE_API_VERSION_0_1 HARDWARE_MODULE_API_VERSION(0, 1) |
| 50 | #define GRALLOC_MODULE_API_VERSION_0_2 HARDWARE_MODULE_API_VERSION(0, 2) |
| 51 | |
| 52 | #define GRALLOC_DEVICE_API_VERSION_0_1 HARDWARE_DEVICE_API_VERSION(0, 1) |
Erik Gilling | e995204 | 2010-12-07 18:46:04 -0800 | [diff] [blame] | 53 | |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 54 | /** |
| 55 | * The id of this module |
| 56 | */ |
| 57 | #define GRALLOC_HARDWARE_MODULE_ID "gralloc" |
| 58 | |
| 59 | /** |
| 60 | * Name of the graphics device to open |
| 61 | */ |
| 62 | |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 63 | #define GRALLOC_HARDWARE_GPU0 "gpu0" |
| 64 | |
| 65 | enum { |
| 66 | /* buffer is never read in software */ |
Jamie Gennis | 29ead94 | 2011-11-21 16:50:49 -0800 | [diff] [blame] | 67 | GRALLOC_USAGE_SW_READ_NEVER = 0x00000000, |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 68 | /* buffer is rarely read in software */ |
Jamie Gennis | 29ead94 | 2011-11-21 16:50:49 -0800 | [diff] [blame] | 69 | GRALLOC_USAGE_SW_READ_RARELY = 0x00000002, |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 70 | /* buffer is often read in software */ |
Jamie Gennis | 29ead94 | 2011-11-21 16:50:49 -0800 | [diff] [blame] | 71 | GRALLOC_USAGE_SW_READ_OFTEN = 0x00000003, |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 72 | /* mask for the software read values */ |
Jamie Gennis | 29ead94 | 2011-11-21 16:50:49 -0800 | [diff] [blame] | 73 | GRALLOC_USAGE_SW_READ_MASK = 0x0000000F, |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 74 | |
| 75 | /* buffer is never written in software */ |
Jamie Gennis | 29ead94 | 2011-11-21 16:50:49 -0800 | [diff] [blame] | 76 | GRALLOC_USAGE_SW_WRITE_NEVER = 0x00000000, |
Greg Hackmann | d6f7aad | 2012-12-07 10:32:58 -0800 | [diff] [blame] | 77 | /* buffer is rarely written in software */ |
Jamie Gennis | 29ead94 | 2011-11-21 16:50:49 -0800 | [diff] [blame] | 78 | GRALLOC_USAGE_SW_WRITE_RARELY = 0x00000020, |
Greg Hackmann | d6f7aad | 2012-12-07 10:32:58 -0800 | [diff] [blame] | 79 | /* buffer is often written in software */ |
Jamie Gennis | 29ead94 | 2011-11-21 16:50:49 -0800 | [diff] [blame] | 80 | GRALLOC_USAGE_SW_WRITE_OFTEN = 0x00000030, |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 81 | /* mask for the software write values */ |
Jamie Gennis | 29ead94 | 2011-11-21 16:50:49 -0800 | [diff] [blame] | 82 | GRALLOC_USAGE_SW_WRITE_MASK = 0x000000F0, |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 83 | |
| 84 | /* buffer will be used as an OpenGL ES texture */ |
Jamie Gennis | 29ead94 | 2011-11-21 16:50:49 -0800 | [diff] [blame] | 85 | GRALLOC_USAGE_HW_TEXTURE = 0x00000100, |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 86 | /* buffer will be used as an OpenGL ES render target */ |
Jamie Gennis | 29ead94 | 2011-11-21 16:50:49 -0800 | [diff] [blame] | 87 | GRALLOC_USAGE_HW_RENDER = 0x00000200, |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 88 | /* buffer will be used by the 2D hardware blitter */ |
Jamie Gennis | 29ead94 | 2011-11-21 16:50:49 -0800 | [diff] [blame] | 89 | GRALLOC_USAGE_HW_2D = 0x00000400, |
Jamie Gennis | 4b560d5 | 2011-08-10 11:41:52 -0700 | [diff] [blame] | 90 | /* buffer will be used by the HWComposer HAL module */ |
Jamie Gennis | 29ead94 | 2011-11-21 16:50:49 -0800 | [diff] [blame] | 91 | GRALLOC_USAGE_HW_COMPOSER = 0x00000800, |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 92 | /* buffer will be used with the framebuffer device */ |
Jamie Gennis | 29ead94 | 2011-11-21 16:50:49 -0800 | [diff] [blame] | 93 | GRALLOC_USAGE_HW_FB = 0x00001000, |
| 94 | /* buffer will be used with the HW video encoder */ |
| 95 | GRALLOC_USAGE_HW_VIDEO_ENCODER = 0x00010000, |
Eino-Ville Talvala | 7797d75 | 2012-07-16 14:50:50 -0700 | [diff] [blame] | 96 | /* buffer will be written by the HW camera pipeline */ |
| 97 | GRALLOC_USAGE_HW_CAMERA_WRITE = 0x00020000, |
| 98 | /* buffer will be read by the HW camera pipeline */ |
| 99 | GRALLOC_USAGE_HW_CAMERA_READ = 0x00040000, |
Eino-Ville Talvala | 2388a2d | 2012-08-28 14:01:26 -0700 | [diff] [blame] | 100 | /* buffer will be used as part of zero-shutter-lag queue */ |
Eino-Ville Talvala | 7f8dd0a | 2012-09-04 14:21:07 -0700 | [diff] [blame] | 101 | GRALLOC_USAGE_HW_CAMERA_ZSL = 0x00060000, |
| 102 | /* mask for the camera access values */ |
| 103 | GRALLOC_USAGE_HW_CAMERA_MASK = 0x00060000, |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 104 | /* mask for the software usage bit-mask */ |
Eino-Ville Talvala | 7797d75 | 2012-07-16 14:50:50 -0700 | [diff] [blame] | 105 | GRALLOC_USAGE_HW_MASK = 0x00071F00, |
Jamie Gennis | 95d78be | 2010-07-01 16:49:52 -0700 | [diff] [blame] | 106 | |
Jamie Gennis | 7edeaf9 | 2010-11-17 18:51:17 -0800 | [diff] [blame] | 107 | /* buffer should be displayed full-screen on an external display when |
| 108 | * possible |
| 109 | */ |
Jamie Gennis | 29ead94 | 2011-11-21 16:50:49 -0800 | [diff] [blame] | 110 | GRALLOC_USAGE_EXTERNAL_DISP = 0x00002000, |
Jamie Gennis | 7edeaf9 | 2010-11-17 18:51:17 -0800 | [diff] [blame] | 111 | |
Glenn Kasten | 7cb277a | 2011-01-19 16:58:31 -0800 | [diff] [blame] | 112 | /* Must have a hardware-protected path to external display sink for |
| 113 | * this buffer. If a hardware-protected path is not available, then |
| 114 | * either don't composite only this buffer (preferred) to the |
| 115 | * external sink, or (less desirable) do not route the entire |
| 116 | * composition to the external sink. |
| 117 | */ |
Jamie Gennis | 29ead94 | 2011-11-21 16:50:49 -0800 | [diff] [blame] | 118 | GRALLOC_USAGE_PROTECTED = 0x00004000, |
Glenn Kasten | 7cb277a | 2011-01-19 16:58:31 -0800 | [diff] [blame] | 119 | |
Jamie Gennis | 95d78be | 2010-07-01 16:49:52 -0700 | [diff] [blame] | 120 | /* implementation-specific private usage flags */ |
Jamie Gennis | 29ead94 | 2011-11-21 16:50:49 -0800 | [diff] [blame] | 121 | GRALLOC_USAGE_PRIVATE_0 = 0x10000000, |
| 122 | GRALLOC_USAGE_PRIVATE_1 = 0x20000000, |
| 123 | GRALLOC_USAGE_PRIVATE_2 = 0x40000000, |
| 124 | GRALLOC_USAGE_PRIVATE_3 = 0x80000000, |
| 125 | GRALLOC_USAGE_PRIVATE_MASK = 0xF0000000, |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 126 | }; |
| 127 | |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 128 | /*****************************************************************************/ |
| 129 | |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 130 | /** |
| 131 | * Every hardware module must have a data structure named HAL_MODULE_INFO_SYM |
| 132 | * and the fields of this data structure must begin with hw_module_t |
| 133 | * followed by module specific information. |
| 134 | */ |
Mathias Agopian | 9d82c1a | 2009-08-19 11:20:55 -0700 | [diff] [blame] | 135 | typedef struct gralloc_module_t { |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 136 | struct hw_module_t common; |
| 137 | |
Mathias Agopian | 988b8bd | 2009-05-04 14:26:56 -0700 | [diff] [blame] | 138 | /* |
| 139 | * (*registerBuffer)() must be called before a buffer_handle_t that has not |
| 140 | * been created with (*alloc_device_t::alloc)() can be used. |
| 141 | * |
| 142 | * This is intended to be used with buffer_handle_t's that have been |
| 143 | * received in this process through IPC. |
| 144 | * |
| 145 | * This function checks that the handle is indeed a valid one and prepares |
| 146 | * it for use with (*lock)() and (*unlock)(). |
| 147 | * |
| 148 | * It is not necessary to call (*registerBuffer)() on a handle created |
| 149 | * with (*alloc_device_t::alloc)(). |
| 150 | * |
| 151 | * returns an error if this buffer_handle_t is not valid. |
| 152 | */ |
| 153 | int (*registerBuffer)(struct gralloc_module_t const* module, |
| 154 | buffer_handle_t handle); |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 155 | |
| 156 | /* |
Mathias Agopian | 988b8bd | 2009-05-04 14:26:56 -0700 | [diff] [blame] | 157 | * (*unregisterBuffer)() is called once this handle is no longer needed in |
| 158 | * this process. After this call, it is an error to call (*lock)(), |
| 159 | * (*unlock)(), or (*registerBuffer)(). |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 160 | * |
Mathias Agopian | 988b8bd | 2009-05-04 14:26:56 -0700 | [diff] [blame] | 161 | * This function doesn't close or free the handle itself; this is done |
| 162 | * by other means, usually through libcutils's native_handle_close() and |
| 163 | * native_handle_free(). |
| 164 | * |
| 165 | * It is an error to call (*unregisterBuffer)() on a buffer that wasn't |
| 166 | * explicitly registered first. |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 167 | */ |
Mathias Agopian | 988b8bd | 2009-05-04 14:26:56 -0700 | [diff] [blame] | 168 | int (*unregisterBuffer)(struct gralloc_module_t const* module, |
| 169 | buffer_handle_t handle); |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 170 | |
| 171 | /* |
| 172 | * The (*lock)() method is called before a buffer is accessed for the |
| 173 | * specified usage. This call may block, for instance if the h/w needs |
| 174 | * to finish rendering or if CPU caches need to be synchronized. |
| 175 | * |
Mathias Agopian | 988b8bd | 2009-05-04 14:26:56 -0700 | [diff] [blame] | 176 | * The caller promises to modify only pixels in the area specified |
| 177 | * by (l,t,w,h). |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 178 | * |
| 179 | * The content of the buffer outside of the specified area is NOT modified |
| 180 | * by this call. |
Mathias Agopian | 988b8bd | 2009-05-04 14:26:56 -0700 | [diff] [blame] | 181 | * |
| 182 | * If usage specifies GRALLOC_USAGE_SW_*, vaddr is filled with the address |
| 183 | * of the buffer in virtual memory. |
| 184 | * |
Alex Ray | 06e3bf2 | 2013-03-19 23:13:56 -0700 | [diff] [blame^] | 185 | * Note calling (*lock)() on HAL_PIXEL_FORMAT_YCbCr_*_888 buffers will fail |
| 186 | * and return -EINVAL. These buffers must be locked with (*lock_ycbcr)() |
| 187 | * instead. |
| 188 | * |
Mathias Agopian | 485e698 | 2009-05-05 20:21:57 -0700 | [diff] [blame] | 189 | * THREADING CONSIDERATIONS: |
| 190 | * |
| 191 | * It is legal for several different threads to lock a buffer from |
| 192 | * read access, none of the threads are blocked. |
| 193 | * |
| 194 | * However, locking a buffer simultaneously for write or read/write is |
| 195 | * undefined, but: |
| 196 | * - shall not result in termination of the process |
| 197 | * - shall not block the caller |
| 198 | * It is acceptable to return an error or to leave the buffer's content |
| 199 | * into an indeterminate state. |
| 200 | * |
Mathias Agopian | 988b8bd | 2009-05-04 14:26:56 -0700 | [diff] [blame] | 201 | * If the buffer was created with a usage mask incompatible with the |
| 202 | * requested usage flags here, -EINVAL is returned. |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 203 | * |
| 204 | */ |
| 205 | |
| 206 | int (*lock)(struct gralloc_module_t const* module, |
| 207 | buffer_handle_t handle, int usage, |
Mathias Agopian | 988b8bd | 2009-05-04 14:26:56 -0700 | [diff] [blame] | 208 | int l, int t, int w, int h, |
| 209 | void** vaddr); |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 210 | |
| 211 | |
| 212 | /* |
| 213 | * The (*unlock)() method must be called after all changes to the buffer |
| 214 | * are completed. |
| 215 | */ |
| 216 | |
| 217 | int (*unlock)(struct gralloc_module_t const* module, |
| 218 | buffer_handle_t handle); |
| 219 | |
Mathias Agopian | cd2433f | 2009-10-29 18:29:39 -0700 | [diff] [blame] | 220 | |
Mathias Agopian | 8255d9d | 2009-09-17 16:15:36 -0700 | [diff] [blame] | 221 | /* reserved for future use */ |
Mathias Agopian | cd2433f | 2009-10-29 18:29:39 -0700 | [diff] [blame] | 222 | int (*perform)(struct gralloc_module_t const* module, |
| 223 | int operation, ... ); |
| 224 | |
Alex Ray | 06e3bf2 | 2013-03-19 23:13:56 -0700 | [diff] [blame^] | 225 | /* |
| 226 | * The (*lock_ycbcr)() method is like the (*lock)() method, with the |
| 227 | * difference that it fills a struct ycbcr with a description of the buffer |
| 228 | * layout, and zeroes out the reserved fields. |
| 229 | * |
| 230 | * This will only work on buffers with HAL_PIXEL_FORMAT_YCbCr_*_888, and |
| 231 | * will return -EINVAL on any other buffer formats. |
| 232 | * |
| 233 | * Added in GRALLOC_MODULE_API_VERSION_0_2. |
| 234 | */ |
| 235 | |
| 236 | int (*lock_ycbcr)(struct gralloc_module_t const* module, |
| 237 | buffer_handle_t handle, int usage, |
| 238 | int l, int t, int w, int h, |
| 239 | struct android_ycbcr *ycbcr); |
| 240 | |
Mathias Agopian | cd2433f | 2009-10-29 18:29:39 -0700 | [diff] [blame] | 241 | /* reserved for future use */ |
Alex Ray | 06e3bf2 | 2013-03-19 23:13:56 -0700 | [diff] [blame^] | 242 | void* reserved_proc[6]; |
Mathias Agopian | 9d82c1a | 2009-08-19 11:20:55 -0700 | [diff] [blame] | 243 | } gralloc_module_t; |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 244 | |
| 245 | /*****************************************************************************/ |
| 246 | |
| 247 | /** |
| 248 | * Every device data structure must begin with hw_device_t |
| 249 | * followed by module specific public methods and attributes. |
| 250 | */ |
| 251 | |
Mathias Agopian | 9d82c1a | 2009-08-19 11:20:55 -0700 | [diff] [blame] | 252 | typedef struct alloc_device_t { |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 253 | struct hw_device_t common; |
| 254 | |
| 255 | /* |
| 256 | * (*alloc)() Allocates a buffer in graphic memory with the requested |
| 257 | * parameters and returns a buffer_handle_t and the stride in pixels to |
| 258 | * allow the implementation to satisfy hardware constraints on the width |
| 259 | * of a pixmap (eg: it may have to be multiple of 8 pixels). |
| 260 | * The CALLER TAKES OWNERSHIP of the buffer_handle_t. |
| 261 | * |
| 262 | * Returns 0 on success or -errno on error. |
| 263 | */ |
| 264 | |
| 265 | int (*alloc)(struct alloc_device_t* dev, |
| 266 | int w, int h, int format, int usage, |
| 267 | buffer_handle_t* handle, int* stride); |
| 268 | |
| 269 | /* |
| 270 | * (*free)() Frees a previously allocated buffer. |
| 271 | * Behavior is undefined if the buffer is still mapped in any process, |
| 272 | * but shall not result in termination of the program or security breaches |
| 273 | * (allowing a process to get access to another process' buffers). |
| 274 | * THIS FUNCTION TAKES OWNERSHIP of the buffer_handle_t which becomes |
| 275 | * invalid after the call. |
| 276 | * |
| 277 | * Returns 0 on success or -errno on error. |
| 278 | */ |
| 279 | int (*free)(struct alloc_device_t* dev, |
| 280 | buffer_handle_t handle); |
| 281 | |
Erik Gilling | 158549c | 2010-12-01 16:34:08 -0800 | [diff] [blame] | 282 | /* This hook is OPTIONAL. |
| 283 | * |
| 284 | * If non NULL it will be caused by SurfaceFlinger on dumpsys |
| 285 | */ |
| 286 | void (*dump)(struct alloc_device_t *dev, char *buff, int buff_len); |
| 287 | |
| 288 | void* reserved_proc[7]; |
Mathias Agopian | 9d82c1a | 2009-08-19 11:20:55 -0700 | [diff] [blame] | 289 | } alloc_device_t; |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 290 | |
| 291 | |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 292 | /** convenience API for opening and closing a supported device */ |
| 293 | |
| 294 | static inline int gralloc_open(const struct hw_module_t* module, |
| 295 | struct alloc_device_t** device) { |
| 296 | return module->methods->open(module, |
| 297 | GRALLOC_HARDWARE_GPU0, (struct hw_device_t**)device); |
| 298 | } |
| 299 | |
| 300 | static inline int gralloc_close(struct alloc_device_t* device) { |
| 301 | return device->common.close(&device->common); |
| 302 | } |
| 303 | |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 304 | __END_DECLS |
| 305 | |
Glenn Kasten | bb56a10 | 2011-11-30 08:31:17 -0800 | [diff] [blame] | 306 | #endif // ANDROID_GRALLOC_INTERFACE_H |