Rebecca Schultz Zavin | 2480ecc | 2012-08-14 16:08:04 -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 | #ifdef HAVE_ANDROID_OS // just want PAGE_SIZE define |
| 18 | # include <asm/page.h> |
| 19 | #else |
| 20 | # include <sys/user.h> |
| 21 | #endif |
| 22 | #include <limits.h> |
| 23 | #include <unistd.h> |
| 24 | #include <fcntl.h> |
| 25 | #include <errno.h> |
| 26 | #include <pthread.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
| 29 | |
| 30 | #include <sys/mman.h> |
| 31 | #include <sys/stat.h> |
| 32 | #include <sys/types.h> |
| 33 | #include <sys/ioctl.h> |
| 34 | |
| 35 | #include <ion/ion.h> |
| 36 | #include <linux/ion.h> |
| 37 | #include <cutils/log.h> |
| 38 | #include <cutils/atomic.h> |
| 39 | |
| 40 | #include <hardware/hardware.h> |
| 41 | #include <hardware/gralloc.h> |
| 42 | |
| 43 | #include "gralloc_priv.h" |
| 44 | #include "exynos_format.h" |
| 45 | #include "gr.h" |
| 46 | |
| 47 | /*****************************************************************************/ |
| 48 | |
| 49 | struct gralloc_context_t { |
| 50 | alloc_device_t device; |
| 51 | /* our private data here */ |
| 52 | }; |
| 53 | |
| 54 | static int gralloc_alloc_buffer(alloc_device_t* dev, |
| 55 | size_t size, int usage, buffer_handle_t* pHandle); |
| 56 | |
| 57 | /*****************************************************************************/ |
| 58 | |
| 59 | int fb_device_open(const hw_module_t* module, const char* name, |
| 60 | hw_device_t** device); |
| 61 | |
| 62 | static int gralloc_device_open(const hw_module_t* module, const char* name, |
| 63 | hw_device_t** device); |
| 64 | |
| 65 | extern int gralloc_lock(gralloc_module_t const* module, |
| 66 | buffer_handle_t handle, int usage, |
| 67 | int l, int t, int w, int h, |
| 68 | void** vaddr); |
| 69 | |
| 70 | extern int gralloc_unlock(gralloc_module_t const* module, |
| 71 | buffer_handle_t handle); |
| 72 | |
| 73 | extern int gralloc_register_buffer(gralloc_module_t const* module, |
| 74 | buffer_handle_t handle); |
| 75 | |
| 76 | extern int gralloc_unregister_buffer(gralloc_module_t const* module, |
| 77 | buffer_handle_t handle); |
| 78 | |
| 79 | /*****************************************************************************/ |
| 80 | |
| 81 | static struct hw_module_methods_t gralloc_module_methods = { |
| 82 | open: gralloc_device_open |
| 83 | }; |
| 84 | |
| 85 | struct private_module_t HAL_MODULE_INFO_SYM = { |
| 86 | base: { |
| 87 | common: { |
| 88 | tag: HARDWARE_MODULE_TAG, |
| 89 | version_major: 1, |
| 90 | version_minor: 0, |
| 91 | id: GRALLOC_HARDWARE_MODULE_ID, |
| 92 | name: "Graphics Memory Allocator Module", |
| 93 | author: "The Android Open Source Project", |
| 94 | methods: &gralloc_module_methods |
| 95 | }, |
Rebecca Schultz Zavin | ec68ab2 | 2012-08-27 10:58:52 -0700 | [diff] [blame] | 96 | registerBuffer: gralloc_register_buffer, |
| 97 | unregisterBuffer: gralloc_unregister_buffer, |
| 98 | lock: gralloc_lock, |
| 99 | unlock: gralloc_unlock, |
Rebecca Schultz Zavin | 2480ecc | 2012-08-14 16:08:04 -0700 | [diff] [blame] | 100 | }, |
Rebecca Schultz Zavin | ec68ab2 | 2012-08-27 10:58:52 -0700 | [diff] [blame] | 101 | framebuffer: 0, |
| 102 | flags: 0, |
| 103 | numBuffers: 0, |
| 104 | bufferMask: 0, |
| 105 | lock: PTHREAD_MUTEX_INITIALIZER, |
| 106 | currentBuffer: 0, |
| 107 | ionfd: -1, |
Rebecca Schultz Zavin | 2480ecc | 2012-08-14 16:08:04 -0700 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | /*****************************************************************************/ |
| 111 | |
| 112 | #define ALIGN(x, a) (((x) + (a - 1)) & ~(a - 1)) |
| 113 | |
Rebecca Schultz Zavin | da3d871 | 2012-08-15 17:03:47 -0700 | [diff] [blame] | 114 | static int gralloc_alloc_rgb(int ionfd, int w, int h, int format, int usage, |
| 115 | unsigned int ion_flags, private_handle_t **hnd, int *stride) |
Rebecca Schultz Zavin | 2480ecc | 2012-08-14 16:08:04 -0700 | [diff] [blame] | 116 | { |
| 117 | size_t size, bpr; |
Rebecca Schultz Zavin | 70212e5 | 2012-08-15 11:17:04 -0700 | [diff] [blame] | 118 | int bpp = 0, vstride, fd, err; |
Rebecca Schultz Zavin | 2480ecc | 2012-08-14 16:08:04 -0700 | [diff] [blame] | 119 | switch (format) { |
| 120 | case HAL_PIXEL_FORMAT_RGBA_8888: |
| 121 | case HAL_PIXEL_FORMAT_RGBX_8888: |
| 122 | case HAL_PIXEL_FORMAT_BGRA_8888: |
| 123 | bpp = 4; |
| 124 | break; |
| 125 | case HAL_PIXEL_FORMAT_RGB_888: |
| 126 | bpp = 3; |
| 127 | break; |
| 128 | case HAL_PIXEL_FORMAT_RGB_565: |
| 129 | case HAL_PIXEL_FORMAT_RGBA_5551: |
| 130 | case HAL_PIXEL_FORMAT_RGBA_4444: |
| 131 | case HAL_PIXEL_FORMAT_RAW_SENSOR: |
| 132 | bpp = 2; |
| 133 | break; |
| 134 | case HAL_PIXEL_FORMAT_BLOB: |
| 135 | bpp = 1; |
| 136 | break; |
| 137 | default: |
| 138 | return -EINVAL; |
| 139 | } |
| 140 | bpr = ALIGN(w*bpp, 16); |
Rebecca Schultz Zavin | 70212e5 | 2012-08-15 11:17:04 -0700 | [diff] [blame] | 141 | vstride = ALIGN(h, 16); |
| 142 | size = bpr * vstride; |
Rebecca Schultz Zavin | 2480ecc | 2012-08-14 16:08:04 -0700 | [diff] [blame] | 143 | *stride = bpr / bpp; |
| 144 | size = ALIGN(size, PAGE_SIZE); |
| 145 | |
Rebecca Schultz Zavin | da3d871 | 2012-08-15 17:03:47 -0700 | [diff] [blame] | 146 | err = ion_alloc_fd(ionfd, size, 0, 1 << ION_HEAP_TYPE_SYSTEM, ion_flags, |
| 147 | &fd); |
| 148 | *hnd = new private_handle_t(fd, size, usage, w, h, format, *stride, |
| 149 | vstride); |
Rebecca Schultz Zavin | 2480ecc | 2012-08-14 16:08:04 -0700 | [diff] [blame] | 150 | |
| 151 | return err; |
| 152 | } |
| 153 | |
Rebecca Schultz Zavin | 55a3039 | 2012-08-27 15:23:32 -0700 | [diff] [blame^] | 154 | static int gralloc_alloc_framework_yuv(int ionfd, int w, int h, int format, |
| 155 | int usage, unsigned int ion_flags, |
| 156 | private_handle_t **hnd, int *stride) |
| 157 | { |
| 158 | size_t size; |
| 159 | int err, fd; |
| 160 | |
| 161 | switch (format) { |
| 162 | case HAL_PIXEL_FORMAT_YV12: |
| 163 | *stride = ALIGN(w, 16); |
| 164 | break; |
| 165 | case HAL_PIXEL_FORMAT_YCrCb_420_SP: |
| 166 | *stride = w; |
| 167 | break; |
| 168 | default: |
| 169 | ALOGE("invalid yuv format %d\n", format); |
| 170 | return -EINVAL; |
| 171 | } |
| 172 | |
| 173 | size = *stride * h * 3 / 2; |
| 174 | err = ion_alloc_fd(ionfd, size, 0, 1 << ION_HEAP_TYPE_SYSTEM, |
| 175 | ion_flags, &fd); |
| 176 | if (err) |
| 177 | return err; |
| 178 | |
| 179 | *hnd = new private_handle_t(fd, size, usage, w, h, format, *stride, h); |
| 180 | return err; |
| 181 | } |
| 182 | |
| 183 | static int gralloc_alloc_yuv(int ionfd, int w, int h, int format, |
| 184 | int usage, unsigned int ion_flags, |
| 185 | private_handle_t **hnd, int *stride) |
Rebecca Schultz Zavin | 2480ecc | 2012-08-14 16:08:04 -0700 | [diff] [blame] | 186 | { |
| 187 | size_t luma_size, chroma_size; |
| 188 | int err, planes, fd, fd1, fd2 = 0; |
Rebecca Schultz Zavin | 70212e5 | 2012-08-15 11:17:04 -0700 | [diff] [blame] | 189 | size_t luma_vstride; |
Rebecca Schultz Zavin | 2480ecc | 2012-08-14 16:08:04 -0700 | [diff] [blame] | 190 | *stride = ALIGN(w, 16); |
| 191 | |
| 192 | switch (format) { |
Rebecca Schultz Zavin | c853be7 | 2012-08-23 00:03:05 -0700 | [diff] [blame] | 193 | case HAL_PIXEL_FORMAT_EXYNOS_YV12: |
Rebecca Schultz Zavin | 2480ecc | 2012-08-14 16:08:04 -0700 | [diff] [blame] | 194 | { |
Rebecca Schultz Zavin | 70212e5 | 2012-08-15 11:17:04 -0700 | [diff] [blame] | 195 | luma_vstride = ALIGN(h, 16); |
| 196 | luma_size = luma_vstride * *stride; |
| 197 | chroma_size = (luma_vstride / 2) * ALIGN(*stride / 2, 16); |
Rebecca Schultz Zavin | 2480ecc | 2012-08-14 16:08:04 -0700 | [diff] [blame] | 198 | planes = 3; |
| 199 | break; |
| 200 | } |
Rebecca Schultz Zavin | c853be7 | 2012-08-23 00:03:05 -0700 | [diff] [blame] | 201 | case HAL_PIXEL_FORMAT_EXYNOS_YCrCb_420_SP: |
Rebecca Schultz Zavin | 2480ecc | 2012-08-14 16:08:04 -0700 | [diff] [blame] | 202 | case HAL_PIXEL_FORMAT_YCbCr_420_SP: |
| 203 | case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED: |
| 204 | { |
Rebecca Schultz Zavin | 2480ecc | 2012-08-14 16:08:04 -0700 | [diff] [blame] | 205 | size_t chroma_vstride = ALIGN(h / 2, 32); |
Rebecca Schultz Zavin | 70212e5 | 2012-08-15 11:17:04 -0700 | [diff] [blame] | 206 | luma_vstride = ALIGN(h, 32); |
Rebecca Schultz Zavin | 2480ecc | 2012-08-14 16:08:04 -0700 | [diff] [blame] | 207 | luma_size = luma_vstride * *stride; |
| 208 | chroma_size = chroma_vstride * *stride; |
| 209 | planes = 2; |
| 210 | break; |
| 211 | } |
Rebecca Schultz Zavin | 55a3039 | 2012-08-27 15:23:32 -0700 | [diff] [blame^] | 212 | case HAL_PIXEL_FORMAT_YV12: |
| 213 | case HAL_PIXEL_FORMAT_YCrCb_420_SP: |
| 214 | return gralloc_alloc_framework_yuv(ionfd, w, h, format, usage, |
| 215 | ion_flags, hnd, stride); |
Rebecca Schultz Zavin | 2480ecc | 2012-08-14 16:08:04 -0700 | [diff] [blame] | 216 | default: |
| 217 | ALOGE("invalid yuv format %d\n", format); |
| 218 | return -EINVAL; |
| 219 | } |
| 220 | |
Rebecca Schultz Zavin | da3d871 | 2012-08-15 17:03:47 -0700 | [diff] [blame] | 221 | err = ion_alloc_fd(ionfd, luma_size, 0, 1 << ION_HEAP_TYPE_SYSTEM, |
| 222 | ion_flags, &fd); |
Rebecca Schultz Zavin | 2480ecc | 2012-08-14 16:08:04 -0700 | [diff] [blame] | 223 | if (err) |
| 224 | return err; |
Rebecca Schultz Zavin | da3d871 | 2012-08-15 17:03:47 -0700 | [diff] [blame] | 225 | err = ion_alloc_fd(ionfd, chroma_size, 0, 1 << ION_HEAP_TYPE_SYSTEM, |
| 226 | ion_flags, |
Rebecca Schultz Zavin | 2480ecc | 2012-08-14 16:08:04 -0700 | [diff] [blame] | 227 | &fd1); |
| 228 | if (err) |
| 229 | goto err1; |
| 230 | if (planes == 3) { |
| 231 | err = ion_alloc_fd(ionfd, chroma_size, 0, 1 << ION_HEAP_TYPE_SYSTEM, |
Rebecca Schultz Zavin | da3d871 | 2012-08-15 17:03:47 -0700 | [diff] [blame] | 232 | ion_flags, &fd2); |
Rebecca Schultz Zavin | 2480ecc | 2012-08-14 16:08:04 -0700 | [diff] [blame] | 233 | if (err) |
| 234 | goto err2; |
Rebecca Schultz Zavin | 2480ecc | 2012-08-14 16:08:04 -0700 | [diff] [blame] | 235 | |
Rebecca Schultz Zavin | ec68ab2 | 2012-08-27 10:58:52 -0700 | [diff] [blame] | 236 | *hnd = new private_handle_t(fd, fd1, fd2, luma_size, usage, w, h, |
| 237 | format, *stride, luma_vstride); |
| 238 | } else { |
| 239 | *hnd = new private_handle_t(fd, fd1, luma_size, usage, w, h, format, |
| 240 | *stride, luma_vstride); |
| 241 | } |
Rebecca Schultz Zavin | 2480ecc | 2012-08-14 16:08:04 -0700 | [diff] [blame] | 242 | return err; |
| 243 | |
| 244 | err2: |
| 245 | close(fd1); |
| 246 | err1: |
| 247 | close(fd); |
| 248 | return err; |
| 249 | } |
| 250 | |
| 251 | static int gralloc_alloc(alloc_device_t* dev, |
| 252 | int w, int h, int format, int usage, |
| 253 | buffer_handle_t* pHandle, int* pStride) |
| 254 | { |
| 255 | int stride; |
| 256 | int err; |
Rebecca Schultz Zavin | da3d871 | 2012-08-15 17:03:47 -0700 | [diff] [blame] | 257 | unsigned int ion_flags = 0; |
Rebecca Schultz Zavin | 2480ecc | 2012-08-14 16:08:04 -0700 | [diff] [blame] | 258 | private_handle_t *hnd; |
| 259 | |
| 260 | if (!pHandle || !pStride) |
| 261 | return -EINVAL; |
| 262 | |
| 263 | if( (usage & GRALLOC_USAGE_SW_READ_MASK) == GRALLOC_USAGE_SW_READ_OFTEN ) |
Rebecca Schultz Zavin | da3d871 | 2012-08-15 17:03:47 -0700 | [diff] [blame] | 264 | ion_flags = ION_FLAG_CACHED; |
Rebecca Schultz Zavin | 2480ecc | 2012-08-14 16:08:04 -0700 | [diff] [blame] | 265 | |
| 266 | private_module_t* m = reinterpret_cast<private_module_t*> |
| 267 | (dev->common.module); |
| 268 | gralloc_module_t* module = reinterpret_cast<gralloc_module_t*> |
| 269 | (dev->common.module); |
| 270 | |
Rebecca Schultz Zavin | da3d871 | 2012-08-15 17:03:47 -0700 | [diff] [blame] | 271 | err = gralloc_alloc_rgb(m->ionfd, w, h, format, usage, ion_flags, &hnd, |
| 272 | &stride); |
Rebecca Schultz Zavin | 2480ecc | 2012-08-14 16:08:04 -0700 | [diff] [blame] | 273 | if (err) |
Rebecca Schultz Zavin | da3d871 | 2012-08-15 17:03:47 -0700 | [diff] [blame] | 274 | err = gralloc_alloc_yuv(m->ionfd, w, h, format, usage, ion_flags, |
| 275 | &hnd, &stride); |
Rebecca Schultz Zavin | 2480ecc | 2012-08-14 16:08:04 -0700 | [diff] [blame] | 276 | if (err) |
| 277 | return err; |
| 278 | |
| 279 | err = grallocMap(module, hnd); |
| 280 | |
| 281 | if (err != 0) |
| 282 | goto err; |
| 283 | |
| 284 | *pHandle = hnd; |
| 285 | *pStride = stride; |
| 286 | return 0; |
| 287 | err: |
| 288 | close(hnd->fd); |
Rebecca Schultz Zavin | ec68ab2 | 2012-08-27 10:58:52 -0700 | [diff] [blame] | 289 | if (hnd->fd1 >= 0) |
Rebecca Schultz Zavin | 2480ecc | 2012-08-14 16:08:04 -0700 | [diff] [blame] | 290 | close(hnd->fd1); |
Rebecca Schultz Zavin | ec68ab2 | 2012-08-27 10:58:52 -0700 | [diff] [blame] | 291 | if (hnd->fd2 >= 0) |
Rebecca Schultz Zavin | 2480ecc | 2012-08-14 16:08:04 -0700 | [diff] [blame] | 292 | close(hnd->fd2); |
| 293 | return err; |
| 294 | } |
| 295 | |
| 296 | static int gralloc_free(alloc_device_t* dev, |
| 297 | buffer_handle_t handle) |
| 298 | { |
| 299 | if (private_handle_t::validate(handle) < 0) |
| 300 | return -EINVAL; |
| 301 | |
| 302 | private_handle_t const* hnd = reinterpret_cast<private_handle_t const*>(handle); |
| 303 | gralloc_module_t* module = reinterpret_cast<gralloc_module_t*>( |
| 304 | dev->common.module); |
| 305 | |
| 306 | grallocUnmap(module, const_cast<private_handle_t*>(hnd)); |
| 307 | close(hnd->fd); |
Rebecca Schultz Zavin | ec68ab2 | 2012-08-27 10:58:52 -0700 | [diff] [blame] | 308 | if (hnd->fd1 >= 0) |
Rebecca Schultz Zavin | 2480ecc | 2012-08-14 16:08:04 -0700 | [diff] [blame] | 309 | close(hnd->fd1); |
Rebecca Schultz Zavin | ec68ab2 | 2012-08-27 10:58:52 -0700 | [diff] [blame] | 310 | if (hnd->fd2 >= 0) |
Rebecca Schultz Zavin | 2480ecc | 2012-08-14 16:08:04 -0700 | [diff] [blame] | 311 | close(hnd->fd2); |
| 312 | |
| 313 | delete hnd; |
| 314 | return 0; |
| 315 | } |
| 316 | |
| 317 | /*****************************************************************************/ |
| 318 | |
| 319 | static int gralloc_close(struct hw_device_t *dev) |
| 320 | { |
| 321 | gralloc_context_t* ctx = reinterpret_cast<gralloc_context_t*>(dev); |
| 322 | if (ctx) { |
| 323 | /* TODO: keep a list of all buffer_handle_t created, and free them |
| 324 | * all here. |
| 325 | */ |
| 326 | free(ctx); |
| 327 | } |
| 328 | return 0; |
| 329 | } |
| 330 | |
| 331 | int gralloc_device_open(const hw_module_t* module, const char* name, |
| 332 | hw_device_t** device) |
| 333 | { |
| 334 | int status = -EINVAL; |
| 335 | if (!strcmp(name, GRALLOC_HARDWARE_GPU0)) { |
| 336 | gralloc_context_t *dev; |
| 337 | dev = (gralloc_context_t*)malloc(sizeof(*dev)); |
| 338 | |
| 339 | /* initialize our state here */ |
| 340 | memset(dev, 0, sizeof(*dev)); |
| 341 | |
| 342 | /* initialize the procs */ |
| 343 | dev->device.common.tag = HARDWARE_DEVICE_TAG; |
| 344 | dev->device.common.version = 0; |
| 345 | dev->device.common.module = const_cast<hw_module_t*>(module); |
| 346 | dev->device.common.close = gralloc_close; |
| 347 | |
| 348 | dev->device.alloc = gralloc_alloc; |
| 349 | dev->device.free = gralloc_free; |
| 350 | |
| 351 | private_module_t *p = reinterpret_cast<private_module_t*>(dev->device.common.module); |
| 352 | p->ionfd = ion_open(); |
| 353 | |
| 354 | *device = &dev->device.common; |
| 355 | status = 0; |
| 356 | } else { |
| 357 | status = fb_device_open(module, name, device); |
| 358 | } |
| 359 | return status; |
| 360 | } |