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> |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 22 | #include <hardware/hardware.h> |
| 23 | |
| 24 | #include <stdint.h> |
| 25 | #include <sys/cdefs.h> |
| 26 | #include <sys/types.h> |
| 27 | |
Mathias Agopian | 5337b10 | 2011-04-28 18:56:10 -0700 | [diff] [blame^] | 28 | #include <cutils/native_handle.h> |
| 29 | |
| 30 | #include <hardware/hardware.h> |
| 31 | #include <hardware/fb.h> |
| 32 | |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 33 | __BEGIN_DECLS |
| 34 | |
Erik Gilling | e995204 | 2010-12-07 18:46:04 -0800 | [diff] [blame] | 35 | #define GRALLOC_API_VERSION 1 |
| 36 | |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 37 | /** |
| 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 Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 46 | #define GRALLOC_HARDWARE_GPU0 "gpu0" |
| 47 | |
| 48 | enum { |
| 49 | /* buffer is never read in software */ |
Mathias Agopian | 988b8bd | 2009-05-04 14:26:56 -0700 | [diff] [blame] | 50 | GRALLOC_USAGE_SW_READ_NEVER = 0x00000000, |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 51 | /* 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 Agopian | 988b8bd | 2009-05-04 14:26:56 -0700 | [diff] [blame] | 59 | GRALLOC_USAGE_SW_WRITE_NEVER = 0x00000000, |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 60 | /* 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 Gennis | aabb702 | 2010-07-01 16:49:07 -0700 | [diff] [blame] | 72 | GRALLOC_USAGE_HW_2D = 0x00000400, |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 73 | /* buffer will be used with the framebuffer device */ |
| 74 | GRALLOC_USAGE_HW_FB = 0x00001000, |
| 75 | /* mask for the software usage bit-mask */ |
| 76 | GRALLOC_USAGE_HW_MASK = 0x00001F00, |
Jamie Gennis | 95d78be | 2010-07-01 16:49:52 -0700 | [diff] [blame] | 77 | |
Jamie Gennis | 7edeaf9 | 2010-11-17 18:51:17 -0800 | [diff] [blame] | 78 | /* buffer should be displayed full-screen on an external display when |
| 79 | * possible |
| 80 | */ |
| 81 | GRALLOC_USAGE_EXTERNAL_DISP = 0x00002000, |
| 82 | |
Glenn Kasten | 7cb277a | 2011-01-19 16:58:31 -0800 | [diff] [blame] | 83 | /* Must have a hardware-protected path to external display sink for |
| 84 | * this buffer. If a hardware-protected path is not available, then |
| 85 | * either don't composite only this buffer (preferred) to the |
| 86 | * external sink, or (less desirable) do not route the entire |
| 87 | * composition to the external sink. |
| 88 | */ |
| 89 | GRALLOC_USAGE_PROTECTED = 0x00004000, |
| 90 | |
Jamie Gennis | 95d78be | 2010-07-01 16:49:52 -0700 | [diff] [blame] | 91 | /* implementation-specific private usage flags */ |
| 92 | GRALLOC_USAGE_PRIVATE_0 = 0x10000000, |
| 93 | GRALLOC_USAGE_PRIVATE_1 = 0x20000000, |
| 94 | GRALLOC_USAGE_PRIVATE_2 = 0x40000000, |
| 95 | GRALLOC_USAGE_PRIVATE_3 = 0x80000000, |
| 96 | GRALLOC_USAGE_PRIVATE_MASK = 0xF0000000, |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 97 | }; |
| 98 | |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 99 | /*****************************************************************************/ |
| 100 | |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 101 | /** |
| 102 | * Every hardware module must have a data structure named HAL_MODULE_INFO_SYM |
| 103 | * and the fields of this data structure must begin with hw_module_t |
| 104 | * followed by module specific information. |
| 105 | */ |
Mathias Agopian | 9d82c1a | 2009-08-19 11:20:55 -0700 | [diff] [blame] | 106 | typedef struct gralloc_module_t { |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 107 | struct hw_module_t common; |
| 108 | |
Mathias Agopian | 988b8bd | 2009-05-04 14:26:56 -0700 | [diff] [blame] | 109 | /* |
| 110 | * (*registerBuffer)() must be called before a buffer_handle_t that has not |
| 111 | * been created with (*alloc_device_t::alloc)() can be used. |
| 112 | * |
| 113 | * This is intended to be used with buffer_handle_t's that have been |
| 114 | * received in this process through IPC. |
| 115 | * |
| 116 | * This function checks that the handle is indeed a valid one and prepares |
| 117 | * it for use with (*lock)() and (*unlock)(). |
| 118 | * |
| 119 | * It is not necessary to call (*registerBuffer)() on a handle created |
| 120 | * with (*alloc_device_t::alloc)(). |
| 121 | * |
| 122 | * returns an error if this buffer_handle_t is not valid. |
| 123 | */ |
| 124 | int (*registerBuffer)(struct gralloc_module_t const* module, |
| 125 | buffer_handle_t handle); |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 126 | |
| 127 | /* |
Mathias Agopian | 988b8bd | 2009-05-04 14:26:56 -0700 | [diff] [blame] | 128 | * (*unregisterBuffer)() is called once this handle is no longer needed in |
| 129 | * this process. After this call, it is an error to call (*lock)(), |
| 130 | * (*unlock)(), or (*registerBuffer)(). |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 131 | * |
Mathias Agopian | 988b8bd | 2009-05-04 14:26:56 -0700 | [diff] [blame] | 132 | * This function doesn't close or free the handle itself; this is done |
| 133 | * by other means, usually through libcutils's native_handle_close() and |
| 134 | * native_handle_free(). |
| 135 | * |
| 136 | * It is an error to call (*unregisterBuffer)() on a buffer that wasn't |
| 137 | * explicitly registered first. |
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 | int (*unregisterBuffer)(struct gralloc_module_t const* module, |
| 140 | buffer_handle_t handle); |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 141 | |
| 142 | /* |
| 143 | * The (*lock)() method is called before a buffer is accessed for the |
| 144 | * specified usage. This call may block, for instance if the h/w needs |
| 145 | * to finish rendering or if CPU caches need to be synchronized. |
| 146 | * |
Mathias Agopian | 988b8bd | 2009-05-04 14:26:56 -0700 | [diff] [blame] | 147 | * The caller promises to modify only pixels in the area specified |
| 148 | * by (l,t,w,h). |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 149 | * |
| 150 | * The content of the buffer outside of the specified area is NOT modified |
| 151 | * by this call. |
Mathias Agopian | 988b8bd | 2009-05-04 14:26:56 -0700 | [diff] [blame] | 152 | * |
| 153 | * If usage specifies GRALLOC_USAGE_SW_*, vaddr is filled with the address |
| 154 | * of the buffer in virtual memory. |
| 155 | * |
Mathias Agopian | 485e698 | 2009-05-05 20:21:57 -0700 | [diff] [blame] | 156 | * THREADING CONSIDERATIONS: |
| 157 | * |
| 158 | * It is legal for several different threads to lock a buffer from |
| 159 | * read access, none of the threads are blocked. |
| 160 | * |
| 161 | * However, locking a buffer simultaneously for write or read/write is |
| 162 | * undefined, but: |
| 163 | * - shall not result in termination of the process |
| 164 | * - shall not block the caller |
| 165 | * It is acceptable to return an error or to leave the buffer's content |
| 166 | * into an indeterminate state. |
| 167 | * |
Mathias Agopian | 988b8bd | 2009-05-04 14:26:56 -0700 | [diff] [blame] | 168 | * If the buffer was created with a usage mask incompatible with the |
| 169 | * requested usage flags here, -EINVAL is returned. |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 170 | * |
| 171 | */ |
| 172 | |
| 173 | int (*lock)(struct gralloc_module_t const* module, |
| 174 | buffer_handle_t handle, int usage, |
Mathias Agopian | 988b8bd | 2009-05-04 14:26:56 -0700 | [diff] [blame] | 175 | int l, int t, int w, int h, |
| 176 | void** vaddr); |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 177 | |
| 178 | |
| 179 | /* |
| 180 | * The (*unlock)() method must be called after all changes to the buffer |
| 181 | * are completed. |
| 182 | */ |
| 183 | |
| 184 | int (*unlock)(struct gralloc_module_t const* module, |
| 185 | buffer_handle_t handle); |
| 186 | |
Mathias Agopian | cd2433f | 2009-10-29 18:29:39 -0700 | [diff] [blame] | 187 | |
Mathias Agopian | 8255d9d | 2009-09-17 16:15:36 -0700 | [diff] [blame] | 188 | /* reserved for future use */ |
Mathias Agopian | cd2433f | 2009-10-29 18:29:39 -0700 | [diff] [blame] | 189 | int (*perform)(struct gralloc_module_t const* module, |
| 190 | int operation, ... ); |
| 191 | |
| 192 | /* reserved for future use */ |
| 193 | void* reserved_proc[7]; |
Mathias Agopian | 9d82c1a | 2009-08-19 11:20:55 -0700 | [diff] [blame] | 194 | } gralloc_module_t; |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 195 | |
| 196 | /*****************************************************************************/ |
| 197 | |
| 198 | /** |
| 199 | * Every device data structure must begin with hw_device_t |
| 200 | * followed by module specific public methods and attributes. |
| 201 | */ |
| 202 | |
Mathias Agopian | 9d82c1a | 2009-08-19 11:20:55 -0700 | [diff] [blame] | 203 | typedef struct alloc_device_t { |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 204 | struct hw_device_t common; |
| 205 | |
| 206 | /* |
| 207 | * (*alloc)() Allocates a buffer in graphic memory with the requested |
| 208 | * parameters and returns a buffer_handle_t and the stride in pixels to |
| 209 | * allow the implementation to satisfy hardware constraints on the width |
| 210 | * of a pixmap (eg: it may have to be multiple of 8 pixels). |
| 211 | * The CALLER TAKES OWNERSHIP of the buffer_handle_t. |
| 212 | * |
| 213 | * Returns 0 on success or -errno on error. |
| 214 | */ |
| 215 | |
| 216 | int (*alloc)(struct alloc_device_t* dev, |
| 217 | int w, int h, int format, int usage, |
| 218 | buffer_handle_t* handle, int* stride); |
| 219 | |
| 220 | /* |
| 221 | * (*free)() Frees a previously allocated buffer. |
| 222 | * Behavior is undefined if the buffer is still mapped in any process, |
| 223 | * but shall not result in termination of the program or security breaches |
| 224 | * (allowing a process to get access to another process' buffers). |
| 225 | * THIS FUNCTION TAKES OWNERSHIP of the buffer_handle_t which becomes |
| 226 | * invalid after the call. |
| 227 | * |
| 228 | * Returns 0 on success or -errno on error. |
| 229 | */ |
| 230 | int (*free)(struct alloc_device_t* dev, |
| 231 | buffer_handle_t handle); |
| 232 | |
Erik Gilling | 158549c | 2010-12-01 16:34:08 -0800 | [diff] [blame] | 233 | /* This hook is OPTIONAL. |
| 234 | * |
| 235 | * If non NULL it will be caused by SurfaceFlinger on dumpsys |
| 236 | */ |
| 237 | void (*dump)(struct alloc_device_t *dev, char *buff, int buff_len); |
| 238 | |
| 239 | void* reserved_proc[7]; |
Mathias Agopian | 9d82c1a | 2009-08-19 11:20:55 -0700 | [diff] [blame] | 240 | } alloc_device_t; |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 241 | |
| 242 | |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 243 | /** convenience API for opening and closing a supported device */ |
| 244 | |
| 245 | static inline int gralloc_open(const struct hw_module_t* module, |
| 246 | struct alloc_device_t** device) { |
| 247 | return module->methods->open(module, |
| 248 | GRALLOC_HARDWARE_GPU0, (struct hw_device_t**)device); |
| 249 | } |
| 250 | |
| 251 | static inline int gralloc_close(struct alloc_device_t* device) { |
| 252 | return device->common.close(&device->common); |
| 253 | } |
| 254 | |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 255 | __END_DECLS |
| 256 | |
| 257 | #endif // ANDROID_ALLOC_INTERFACE_H |