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 | |
| 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 | |
Erik Gilling | e995204 | 2010-12-07 18:46:04 -0800 | [diff] [blame] | 31 | #define GRALLOC_API_VERSION 1 |
| 32 | |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 33 | /** |
| 34 | * The id of this module |
| 35 | */ |
| 36 | #define GRALLOC_HARDWARE_MODULE_ID "gralloc" |
| 37 | |
| 38 | /** |
| 39 | * Name of the graphics device to open |
| 40 | */ |
| 41 | |
| 42 | #define GRALLOC_HARDWARE_FB0 "fb0" |
| 43 | #define GRALLOC_HARDWARE_GPU0 "gpu0" |
| 44 | |
| 45 | enum { |
| 46 | /* buffer is never read in software */ |
Mathias Agopian | 988b8bd | 2009-05-04 14:26:56 -0700 | [diff] [blame] | 47 | GRALLOC_USAGE_SW_READ_NEVER = 0x00000000, |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 48 | /* buffer is rarely read in software */ |
| 49 | GRALLOC_USAGE_SW_READ_RARELY = 0x00000002, |
| 50 | /* buffer is often read in software */ |
| 51 | GRALLOC_USAGE_SW_READ_OFTEN = 0x00000003, |
| 52 | /* mask for the software read values */ |
| 53 | GRALLOC_USAGE_SW_READ_MASK = 0x0000000F, |
| 54 | |
| 55 | /* buffer is never written in software */ |
Mathias Agopian | 988b8bd | 2009-05-04 14:26:56 -0700 | [diff] [blame] | 56 | GRALLOC_USAGE_SW_WRITE_NEVER = 0x00000000, |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 57 | /* buffer is never written in software */ |
| 58 | GRALLOC_USAGE_SW_WRITE_RARELY = 0x00000020, |
| 59 | /* buffer is never written in software */ |
| 60 | GRALLOC_USAGE_SW_WRITE_OFTEN = 0x00000030, |
| 61 | /* mask for the software write values */ |
| 62 | GRALLOC_USAGE_SW_WRITE_MASK = 0x000000F0, |
| 63 | |
| 64 | /* buffer will be used as an OpenGL ES texture */ |
| 65 | GRALLOC_USAGE_HW_TEXTURE = 0x00000100, |
| 66 | /* buffer will be used as an OpenGL ES render target */ |
| 67 | GRALLOC_USAGE_HW_RENDER = 0x00000200, |
| 68 | /* buffer will be used by the 2D hardware blitter */ |
Jamie Gennis | aabb702 | 2010-07-01 16:49:07 -0700 | [diff] [blame] | 69 | GRALLOC_USAGE_HW_2D = 0x00000400, |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 70 | /* buffer will be used with the framebuffer device */ |
| 71 | GRALLOC_USAGE_HW_FB = 0x00001000, |
| 72 | /* mask for the software usage bit-mask */ |
| 73 | GRALLOC_USAGE_HW_MASK = 0x00001F00, |
Jamie Gennis | 95d78be | 2010-07-01 16:49:52 -0700 | [diff] [blame] | 74 | |
Jamie Gennis | 7edeaf9 | 2010-11-17 18:51:17 -0800 | [diff] [blame] | 75 | /* buffer should be displayed full-screen on an external display when |
| 76 | * possible |
| 77 | */ |
| 78 | GRALLOC_USAGE_EXTERNAL_DISP = 0x00002000, |
| 79 | |
Glenn Kasten | 7cb277a | 2011-01-19 16:58:31 -0800 | [diff] [blame^] | 80 | /* Must have a hardware-protected path to external display sink for |
| 81 | * this buffer. If a hardware-protected path is not available, then |
| 82 | * either don't composite only this buffer (preferred) to the |
| 83 | * external sink, or (less desirable) do not route the entire |
| 84 | * composition to the external sink. |
| 85 | */ |
| 86 | GRALLOC_USAGE_PROTECTED = 0x00004000, |
| 87 | |
Jamie Gennis | 95d78be | 2010-07-01 16:49:52 -0700 | [diff] [blame] | 88 | /* implementation-specific private usage flags */ |
| 89 | GRALLOC_USAGE_PRIVATE_0 = 0x10000000, |
| 90 | GRALLOC_USAGE_PRIVATE_1 = 0x20000000, |
| 91 | GRALLOC_USAGE_PRIVATE_2 = 0x40000000, |
| 92 | GRALLOC_USAGE_PRIVATE_3 = 0x80000000, |
| 93 | GRALLOC_USAGE_PRIVATE_MASK = 0xF0000000, |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 94 | }; |
| 95 | |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 96 | /*****************************************************************************/ |
| 97 | |
| 98 | typedef const native_handle* buffer_handle_t; |
| 99 | |
Mathias Agopian | cd2433f | 2009-10-29 18:29:39 -0700 | [diff] [blame] | 100 | enum { |
| 101 | /* FIXME: this only exists to work-around some issues with |
| 102 | * the video and camera frameworks. don't implement unless |
| 103 | * you know what you're doing. |
| 104 | */ |
| 105 | GRALLOC_MODULE_PERFORM_CREATE_HANDLE_FROM_BUFFER = 0x080000001, |
| 106 | }; |
| 107 | |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 108 | /** |
| 109 | * Every hardware module must have a data structure named HAL_MODULE_INFO_SYM |
| 110 | * and the fields of this data structure must begin with hw_module_t |
| 111 | * followed by module specific information. |
| 112 | */ |
Mathias Agopian | 9d82c1a | 2009-08-19 11:20:55 -0700 | [diff] [blame] | 113 | typedef struct gralloc_module_t { |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 114 | struct hw_module_t common; |
| 115 | |
Mathias Agopian | 988b8bd | 2009-05-04 14:26:56 -0700 | [diff] [blame] | 116 | /* |
| 117 | * (*registerBuffer)() must be called before a buffer_handle_t that has not |
| 118 | * been created with (*alloc_device_t::alloc)() can be used. |
| 119 | * |
| 120 | * This is intended to be used with buffer_handle_t's that have been |
| 121 | * received in this process through IPC. |
| 122 | * |
| 123 | * This function checks that the handle is indeed a valid one and prepares |
| 124 | * it for use with (*lock)() and (*unlock)(). |
| 125 | * |
| 126 | * It is not necessary to call (*registerBuffer)() on a handle created |
| 127 | * with (*alloc_device_t::alloc)(). |
| 128 | * |
| 129 | * returns an error if this buffer_handle_t is not valid. |
| 130 | */ |
| 131 | int (*registerBuffer)(struct gralloc_module_t const* module, |
| 132 | buffer_handle_t handle); |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 133 | |
| 134 | /* |
Mathias Agopian | 988b8bd | 2009-05-04 14:26:56 -0700 | [diff] [blame] | 135 | * (*unregisterBuffer)() is called once this handle is no longer needed in |
| 136 | * this process. After this call, it is an error to call (*lock)(), |
| 137 | * (*unlock)(), or (*registerBuffer)(). |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 138 | * |
Mathias Agopian | 988b8bd | 2009-05-04 14:26:56 -0700 | [diff] [blame] | 139 | * This function doesn't close or free the handle itself; this is done |
| 140 | * by other means, usually through libcutils's native_handle_close() and |
| 141 | * native_handle_free(). |
| 142 | * |
| 143 | * It is an error to call (*unregisterBuffer)() on a buffer that wasn't |
| 144 | * explicitly registered first. |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 145 | */ |
Mathias Agopian | 988b8bd | 2009-05-04 14:26:56 -0700 | [diff] [blame] | 146 | int (*unregisterBuffer)(struct gralloc_module_t const* module, |
| 147 | buffer_handle_t handle); |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 148 | |
| 149 | /* |
| 150 | * The (*lock)() method is called before a buffer is accessed for the |
| 151 | * specified usage. This call may block, for instance if the h/w needs |
| 152 | * to finish rendering or if CPU caches need to be synchronized. |
| 153 | * |
Mathias Agopian | 988b8bd | 2009-05-04 14:26:56 -0700 | [diff] [blame] | 154 | * The caller promises to modify only pixels in the area specified |
| 155 | * by (l,t,w,h). |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 156 | * |
| 157 | * The content of the buffer outside of the specified area is NOT modified |
| 158 | * by this call. |
Mathias Agopian | 988b8bd | 2009-05-04 14:26:56 -0700 | [diff] [blame] | 159 | * |
| 160 | * If usage specifies GRALLOC_USAGE_SW_*, vaddr is filled with the address |
| 161 | * of the buffer in virtual memory. |
| 162 | * |
Mathias Agopian | 485e698 | 2009-05-05 20:21:57 -0700 | [diff] [blame] | 163 | * THREADING CONSIDERATIONS: |
| 164 | * |
| 165 | * It is legal for several different threads to lock a buffer from |
| 166 | * read access, none of the threads are blocked. |
| 167 | * |
| 168 | * However, locking a buffer simultaneously for write or read/write is |
| 169 | * undefined, but: |
| 170 | * - shall not result in termination of the process |
| 171 | * - shall not block the caller |
| 172 | * It is acceptable to return an error or to leave the buffer's content |
| 173 | * into an indeterminate state. |
| 174 | * |
Mathias Agopian | 988b8bd | 2009-05-04 14:26:56 -0700 | [diff] [blame] | 175 | * If the buffer was created with a usage mask incompatible with the |
| 176 | * requested usage flags here, -EINVAL is returned. |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 177 | * |
| 178 | */ |
| 179 | |
| 180 | int (*lock)(struct gralloc_module_t const* module, |
| 181 | buffer_handle_t handle, int usage, |
Mathias Agopian | 988b8bd | 2009-05-04 14:26:56 -0700 | [diff] [blame] | 182 | int l, int t, int w, int h, |
| 183 | void** vaddr); |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 184 | |
| 185 | |
| 186 | /* |
| 187 | * The (*unlock)() method must be called after all changes to the buffer |
| 188 | * are completed. |
| 189 | */ |
| 190 | |
| 191 | int (*unlock)(struct gralloc_module_t const* module, |
| 192 | buffer_handle_t handle); |
| 193 | |
Mathias Agopian | cd2433f | 2009-10-29 18:29:39 -0700 | [diff] [blame] | 194 | |
Mathias Agopian | 8255d9d | 2009-09-17 16:15:36 -0700 | [diff] [blame] | 195 | /* reserved for future use */ |
Mathias Agopian | cd2433f | 2009-10-29 18:29:39 -0700 | [diff] [blame] | 196 | int (*perform)(struct gralloc_module_t const* module, |
| 197 | int operation, ... ); |
| 198 | |
| 199 | /* reserved for future use */ |
| 200 | void* reserved_proc[7]; |
Mathias Agopian | 9d82c1a | 2009-08-19 11:20:55 -0700 | [diff] [blame] | 201 | } gralloc_module_t; |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 202 | |
| 203 | /*****************************************************************************/ |
| 204 | |
| 205 | /** |
| 206 | * Every device data structure must begin with hw_device_t |
| 207 | * followed by module specific public methods and attributes. |
| 208 | */ |
| 209 | |
Mathias Agopian | 9d82c1a | 2009-08-19 11:20:55 -0700 | [diff] [blame] | 210 | typedef struct alloc_device_t { |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 211 | struct hw_device_t common; |
| 212 | |
| 213 | /* |
| 214 | * (*alloc)() Allocates a buffer in graphic memory with the requested |
| 215 | * parameters and returns a buffer_handle_t and the stride in pixels to |
| 216 | * allow the implementation to satisfy hardware constraints on the width |
| 217 | * of a pixmap (eg: it may have to be multiple of 8 pixels). |
| 218 | * The CALLER TAKES OWNERSHIP of the buffer_handle_t. |
| 219 | * |
| 220 | * Returns 0 on success or -errno on error. |
| 221 | */ |
| 222 | |
| 223 | int (*alloc)(struct alloc_device_t* dev, |
| 224 | int w, int h, int format, int usage, |
| 225 | buffer_handle_t* handle, int* stride); |
| 226 | |
| 227 | /* |
| 228 | * (*free)() Frees a previously allocated buffer. |
| 229 | * Behavior is undefined if the buffer is still mapped in any process, |
| 230 | * but shall not result in termination of the program or security breaches |
| 231 | * (allowing a process to get access to another process' buffers). |
| 232 | * THIS FUNCTION TAKES OWNERSHIP of the buffer_handle_t which becomes |
| 233 | * invalid after the call. |
| 234 | * |
| 235 | * Returns 0 on success or -errno on error. |
| 236 | */ |
| 237 | int (*free)(struct alloc_device_t* dev, |
| 238 | buffer_handle_t handle); |
| 239 | |
Erik Gilling | 158549c | 2010-12-01 16:34:08 -0800 | [diff] [blame] | 240 | /* This hook is OPTIONAL. |
| 241 | * |
| 242 | * If non NULL it will be caused by SurfaceFlinger on dumpsys |
| 243 | */ |
| 244 | void (*dump)(struct alloc_device_t *dev, char *buff, int buff_len); |
| 245 | |
| 246 | void* reserved_proc[7]; |
Mathias Agopian | 9d82c1a | 2009-08-19 11:20:55 -0700 | [diff] [blame] | 247 | } alloc_device_t; |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 248 | |
| 249 | |
Mathias Agopian | 9d82c1a | 2009-08-19 11:20:55 -0700 | [diff] [blame] | 250 | typedef struct framebuffer_device_t { |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 251 | struct hw_device_t common; |
| 252 | |
| 253 | /* flags describing some attributes of the framebuffer */ |
| 254 | const uint32_t flags; |
| 255 | |
| 256 | /* dimensions of the framebuffer in pixels */ |
| 257 | const uint32_t width; |
| 258 | const uint32_t height; |
| 259 | |
| 260 | /* frambuffer stride in pixels */ |
| 261 | const int stride; |
| 262 | |
| 263 | /* framebuffer pixel format */ |
| 264 | const int format; |
| 265 | |
| 266 | /* resolution of the framebuffer's display panel in pixel per inch*/ |
| 267 | const float xdpi; |
| 268 | const float ydpi; |
| 269 | |
| 270 | /* framebuffer's display panel refresh rate in frames per second */ |
| 271 | const float fps; |
| 272 | |
| 273 | /* min swap interval supported by this framebuffer */ |
| 274 | const int minSwapInterval; |
| 275 | |
| 276 | /* max swap interval supported by this framebuffer */ |
| 277 | const int maxSwapInterval; |
| 278 | |
| 279 | int reserved[8]; |
| 280 | |
| 281 | /* |
| 282 | * requests a specific swap-interval (same definition than EGL) |
| 283 | * |
| 284 | * Returns 0 on success or -errno on error. |
| 285 | */ |
| 286 | int (*setSwapInterval)(struct framebuffer_device_t* window, |
| 287 | int interval); |
| 288 | |
| 289 | /* |
Mathias Agopian | f5cf8f8 | 2009-05-07 17:39:31 -0700 | [diff] [blame] | 290 | * This hook is OPTIONAL. |
| 291 | * |
| 292 | * It is non NULL If the framebuffer driver supports "update-on-demand" |
| 293 | * and the given rectangle is the area of the screen that gets |
| 294 | * updated during (*post)(). |
| 295 | * |
| 296 | * This is useful on devices that are able to DMA only a portion of |
| 297 | * the screen to the display panel, upon demand -- as opposed to |
| 298 | * constantly refreshing the panel 60 times per second, for instance. |
| 299 | * |
Mathias Agopian | 8255d9d | 2009-09-17 16:15:36 -0700 | [diff] [blame] | 300 | * Only the area defined by this rectangle is guaranteed to be valid, that |
Mathias Agopian | f5cf8f8 | 2009-05-07 17:39:31 -0700 | [diff] [blame] | 301 | * is, the driver is not allowed to post anything outside of this |
| 302 | * rectangle. |
| 303 | * |
| 304 | * The rectangle evaluated during (*post)() and specifies which area |
| 305 | * of the buffer passed in (*post)() shall to be posted. |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 306 | * |
| 307 | * return -EINVAL if width or height <=0, or if left or top < 0 |
| 308 | */ |
| 309 | int (*setUpdateRect)(struct framebuffer_device_t* window, |
| 310 | int left, int top, int width, int height); |
| 311 | |
| 312 | /* |
| 313 | * Post <buffer> to the display (display it on the screen) |
| 314 | * The buffer must have been allocated with the |
| 315 | * GRALLOC_USAGE_HW_FB usage flag. |
| 316 | * buffer must be the same width and height as the display and must NOT |
| 317 | * be locked. |
| 318 | * |
| 319 | * The buffer is shown during the next VSYNC. |
| 320 | * |
| 321 | * If the same buffer is posted again (possibly after some other buffer), |
| 322 | * post() will block until the the first post is completed. |
| 323 | * |
| 324 | * Internally, post() is expected to lock the buffer so that a |
| 325 | * subsequent call to gralloc_module_t::(*lock)() with USAGE_RENDER or |
| 326 | * USAGE_*_WRITE will block until it is safe; that is typically once this |
| 327 | * buffer is shown and another buffer has been posted. |
| 328 | * |
| 329 | * Returns 0 on success or -errno on error. |
| 330 | */ |
| 331 | int (*post)(struct framebuffer_device_t* dev, buffer_handle_t buffer); |
| 332 | |
Mathias Agopian | 8255d9d | 2009-09-17 16:15:36 -0700 | [diff] [blame] | 333 | |
| 334 | /* |
| 335 | * The (*compositionComplete)() method must be called after the |
| 336 | * compositor has finished issuing GL commands for client buffers. |
| 337 | */ |
| 338 | |
| 339 | int (*compositionComplete)(struct framebuffer_device_t* dev); |
| 340 | |
Erik Gilling | 158549c | 2010-12-01 16:34:08 -0800 | [diff] [blame] | 341 | /* |
| 342 | * This hook is OPTIONAL. |
| 343 | * |
| 344 | * If non NULL it will be caused by SurfaceFlinger on dumpsys |
| 345 | */ |
| 346 | void (*dump)(struct framebuffer_device_t* dev, char *buff, int buff_len); |
Mathias Agopian | 8255d9d | 2009-09-17 16:15:36 -0700 | [diff] [blame] | 347 | |
Erik Gilling | 158549c | 2010-12-01 16:34:08 -0800 | [diff] [blame] | 348 | void* reserved_proc[7]; |
Mathias Agopian | 9d82c1a | 2009-08-19 11:20:55 -0700 | [diff] [blame] | 349 | |
| 350 | } framebuffer_device_t; |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 351 | |
| 352 | |
| 353 | /** convenience API for opening and closing a supported device */ |
| 354 | |
| 355 | static inline int gralloc_open(const struct hw_module_t* module, |
| 356 | struct alloc_device_t** device) { |
| 357 | return module->methods->open(module, |
| 358 | GRALLOC_HARDWARE_GPU0, (struct hw_device_t**)device); |
| 359 | } |
| 360 | |
| 361 | static inline int gralloc_close(struct alloc_device_t* device) { |
| 362 | return device->common.close(&device->common); |
| 363 | } |
| 364 | |
| 365 | |
| 366 | static inline int framebuffer_open(const struct hw_module_t* module, |
| 367 | struct framebuffer_device_t** device) { |
| 368 | return module->methods->open(module, |
| 369 | GRALLOC_HARDWARE_FB0, (struct hw_device_t**)device); |
| 370 | } |
| 371 | |
| 372 | static inline int framebuffer_close(struct framebuffer_device_t* device) { |
| 373 | return device->common.close(&device->common); |
| 374 | } |
| 375 | |
| 376 | |
| 377 | __END_DECLS |
| 378 | |
| 379 | #endif // ANDROID_ALLOC_INTERFACE_H |