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 | |
| 31 | /** |
| 32 | * The id of this module |
| 33 | */ |
| 34 | #define GRALLOC_HARDWARE_MODULE_ID "gralloc" |
| 35 | |
| 36 | /** |
| 37 | * Name of the graphics device to open |
| 38 | */ |
| 39 | |
| 40 | #define GRALLOC_HARDWARE_FB0 "fb0" |
| 41 | #define GRALLOC_HARDWARE_GPU0 "gpu0" |
| 42 | |
| 43 | enum { |
| 44 | /* buffer is never read in software */ |
| 45 | GRALLOC_USAGE_SW_READ_NEVER = 0x00000001, |
| 46 | /* buffer is rarely read in software */ |
| 47 | GRALLOC_USAGE_SW_READ_RARELY = 0x00000002, |
| 48 | /* buffer is often read in software */ |
| 49 | GRALLOC_USAGE_SW_READ_OFTEN = 0x00000003, |
| 50 | /* mask for the software read values */ |
| 51 | GRALLOC_USAGE_SW_READ_MASK = 0x0000000F, |
| 52 | |
| 53 | /* buffer is never written in software */ |
| 54 | GRALLOC_USAGE_SW_WRITE_NEVER = 0x00000010, |
| 55 | /* buffer is never written in software */ |
| 56 | GRALLOC_USAGE_SW_WRITE_RARELY = 0x00000020, |
| 57 | /* buffer is never written in software */ |
| 58 | GRALLOC_USAGE_SW_WRITE_OFTEN = 0x00000030, |
| 59 | /* mask for the software write values */ |
| 60 | GRALLOC_USAGE_SW_WRITE_MASK = 0x000000F0, |
| 61 | |
| 62 | /* buffer will be used as an OpenGL ES texture */ |
| 63 | GRALLOC_USAGE_HW_TEXTURE = 0x00000100, |
| 64 | /* buffer will be used as an OpenGL ES render target */ |
| 65 | GRALLOC_USAGE_HW_RENDER = 0x00000200, |
| 66 | /* buffer will be used by the 2D hardware blitter */ |
| 67 | GRALLOC_USAGE_HW_2D = 0x00000C00, |
| 68 | /* buffer will be used with the framebuffer device */ |
| 69 | GRALLOC_USAGE_HW_FB = 0x00001000, |
| 70 | /* mask for the software usage bit-mask */ |
| 71 | GRALLOC_USAGE_HW_MASK = 0x00001F00, |
| 72 | }; |
| 73 | |
| 74 | enum { |
| 75 | /* the framebuffer is mapped in memory */ |
| 76 | FRAMEBUFFER_RESERVED0 = 0x00000001, |
| 77 | FRAMEBUFFER_FLAG_MAPPED = 0x00000002, |
| 78 | }; |
| 79 | |
| 80 | /*****************************************************************************/ |
| 81 | |
| 82 | typedef const native_handle* buffer_handle_t; |
| 83 | |
| 84 | /** |
| 85 | * Every hardware module must have a data structure named HAL_MODULE_INFO_SYM |
| 86 | * and the fields of this data structure must begin with hw_module_t |
| 87 | * followed by module specific information. |
| 88 | */ |
| 89 | struct gralloc_module_t { |
| 90 | struct hw_module_t common; |
| 91 | |
| 92 | /* |
| 93 | * The (*map)() method maps the buffer in the caller's address space |
| 94 | * if this operation is allowed (see below). |
| 95 | * Mapped buffers are reference-counted in a given process, that is, |
| 96 | * if a the buffer is already mapped, this function must return the |
| 97 | * same address and the internal reference counter is incremented. |
| 98 | * |
| 99 | * Returns 0 on success or -errno on error. |
| 100 | */ |
| 101 | |
| 102 | int (*map)(struct gralloc_module_t const* module, |
| 103 | buffer_handle_t handle, void** vaddr); |
| 104 | |
| 105 | /* |
| 106 | * The (*unmap)() method, unmaps the buffer from the caller's address |
| 107 | * space, if the buffer has been mapped more than once, |
| 108 | * the (*unmap)() needs to be called the same number of time before |
| 109 | * the buffer is actually unmapped. |
| 110 | * |
| 111 | * Returns 0 on success or -errno on error. |
| 112 | */ |
| 113 | |
| 114 | int (*unmap)(struct gralloc_module_t const* module, buffer_handle_t handle); |
| 115 | |
| 116 | |
| 117 | /* |
| 118 | * The (*lock)() method is called before a buffer is accessed for the |
| 119 | * specified usage. This call may block, for instance if the h/w needs |
| 120 | * to finish rendering or if CPU caches need to be synchronized. |
| 121 | * |
| 122 | * The caller promises to modify ALL PIXELS and ONLY THE PIXELS in the area |
| 123 | * specified by (l,t,w,h). |
| 124 | * |
| 125 | * The content of the buffer outside of the specified area is NOT modified |
| 126 | * by this call. |
| 127 | * |
| 128 | */ |
| 129 | |
| 130 | int (*lock)(struct gralloc_module_t const* module, |
| 131 | buffer_handle_t handle, int usage, |
| 132 | int l, int t, int w, int h); |
| 133 | |
| 134 | |
| 135 | /* |
| 136 | * The (*unlock)() method must be called after all changes to the buffer |
| 137 | * are completed. |
| 138 | */ |
| 139 | |
| 140 | int (*unlock)(struct gralloc_module_t const* module, |
| 141 | buffer_handle_t handle); |
| 142 | |
| 143 | }; |
| 144 | |
| 145 | /*****************************************************************************/ |
| 146 | |
| 147 | /** |
| 148 | * Every device data structure must begin with hw_device_t |
| 149 | * followed by module specific public methods and attributes. |
| 150 | */ |
| 151 | |
| 152 | struct alloc_device_t { |
| 153 | struct hw_device_t common; |
| 154 | |
| 155 | /* |
| 156 | * (*alloc)() Allocates a buffer in graphic memory with the requested |
| 157 | * parameters and returns a buffer_handle_t and the stride in pixels to |
| 158 | * allow the implementation to satisfy hardware constraints on the width |
| 159 | * of a pixmap (eg: it may have to be multiple of 8 pixels). |
| 160 | * The CALLER TAKES OWNERSHIP of the buffer_handle_t. |
| 161 | * |
| 162 | * Returns 0 on success or -errno on error. |
| 163 | */ |
| 164 | |
| 165 | int (*alloc)(struct alloc_device_t* dev, |
| 166 | int w, int h, int format, int usage, |
| 167 | buffer_handle_t* handle, int* stride); |
| 168 | |
| 169 | /* |
| 170 | * (*free)() Frees a previously allocated buffer. |
| 171 | * Behavior is undefined if the buffer is still mapped in any process, |
| 172 | * but shall not result in termination of the program or security breaches |
| 173 | * (allowing a process to get access to another process' buffers). |
| 174 | * THIS FUNCTION TAKES OWNERSHIP of the buffer_handle_t which becomes |
| 175 | * invalid after the call. |
| 176 | * |
| 177 | * Returns 0 on success or -errno on error. |
| 178 | */ |
| 179 | int (*free)(struct alloc_device_t* dev, |
| 180 | buffer_handle_t handle); |
| 181 | |
| 182 | }; |
| 183 | |
| 184 | |
| 185 | struct framebuffer_device_t { |
| 186 | struct hw_device_t common; |
| 187 | |
| 188 | /* flags describing some attributes of the framebuffer */ |
| 189 | const uint32_t flags; |
| 190 | |
| 191 | /* dimensions of the framebuffer in pixels */ |
| 192 | const uint32_t width; |
| 193 | const uint32_t height; |
| 194 | |
| 195 | /* frambuffer stride in pixels */ |
| 196 | const int stride; |
| 197 | |
| 198 | /* framebuffer pixel format */ |
| 199 | const int format; |
| 200 | |
| 201 | /* resolution of the framebuffer's display panel in pixel per inch*/ |
| 202 | const float xdpi; |
| 203 | const float ydpi; |
| 204 | |
| 205 | /* framebuffer's display panel refresh rate in frames per second */ |
| 206 | const float fps; |
| 207 | |
| 208 | /* min swap interval supported by this framebuffer */ |
| 209 | const int minSwapInterval; |
| 210 | |
| 211 | /* max swap interval supported by this framebuffer */ |
| 212 | const int maxSwapInterval; |
| 213 | |
| 214 | int reserved[8]; |
| 215 | |
| 216 | /* |
| 217 | * requests a specific swap-interval (same definition than EGL) |
| 218 | * |
| 219 | * Returns 0 on success or -errno on error. |
| 220 | */ |
| 221 | int (*setSwapInterval)(struct framebuffer_device_t* window, |
| 222 | int interval); |
| 223 | |
| 224 | /* |
| 225 | * sets a rectangle evaluated during (*post)() specifying which area |
| 226 | * of the buffer passed in (*post)() needs to be posted. |
| 227 | * |
| 228 | * return -EINVAL if width or height <=0, or if left or top < 0 |
| 229 | */ |
| 230 | int (*setUpdateRect)(struct framebuffer_device_t* window, |
| 231 | int left, int top, int width, int height); |
| 232 | |
| 233 | /* |
| 234 | * Post <buffer> to the display (display it on the screen) |
| 235 | * The buffer must have been allocated with the |
| 236 | * GRALLOC_USAGE_HW_FB usage flag. |
| 237 | * buffer must be the same width and height as the display and must NOT |
| 238 | * be locked. |
| 239 | * |
| 240 | * The buffer is shown during the next VSYNC. |
| 241 | * |
| 242 | * If the same buffer is posted again (possibly after some other buffer), |
| 243 | * post() will block until the the first post is completed. |
| 244 | * |
| 245 | * Internally, post() is expected to lock the buffer so that a |
| 246 | * subsequent call to gralloc_module_t::(*lock)() with USAGE_RENDER or |
| 247 | * USAGE_*_WRITE will block until it is safe; that is typically once this |
| 248 | * buffer is shown and another buffer has been posted. |
| 249 | * |
| 250 | * Returns 0 on success or -errno on error. |
| 251 | */ |
| 252 | int (*post)(struct framebuffer_device_t* dev, buffer_handle_t buffer); |
| 253 | |
| 254 | void* reserved_proc[8]; |
| 255 | }; |
| 256 | |
| 257 | |
| 258 | /** convenience API for opening and closing a supported device */ |
| 259 | |
| 260 | static inline int gralloc_open(const struct hw_module_t* module, |
| 261 | struct alloc_device_t** device) { |
| 262 | return module->methods->open(module, |
| 263 | GRALLOC_HARDWARE_GPU0, (struct hw_device_t**)device); |
| 264 | } |
| 265 | |
| 266 | static inline int gralloc_close(struct alloc_device_t* device) { |
| 267 | return device->common.close(&device->common); |
| 268 | } |
| 269 | |
| 270 | |
| 271 | static inline int framebuffer_open(const struct hw_module_t* module, |
| 272 | struct framebuffer_device_t** device) { |
| 273 | return module->methods->open(module, |
| 274 | GRALLOC_HARDWARE_FB0, (struct hw_device_t**)device); |
| 275 | } |
| 276 | |
| 277 | static inline int framebuffer_close(struct framebuffer_device_t* device) { |
| 278 | return device->common.close(&device->common); |
| 279 | } |
| 280 | |
| 281 | |
| 282 | __END_DECLS |
| 283 | |
| 284 | #endif // ANDROID_ALLOC_INTERFACE_H |