Home | History | Annotate | Download | only in gralloc
      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 #include <limits.h>
     18 #include <unistd.h>
     19 #include <fcntl.h>
     20 #include <errno.h>
     21 #include <pthread.h>
     22 #include <stdlib.h>
     23 #include <string.h>
     24 
     25 #include <sys/mman.h>
     26 #include <sys/stat.h>
     27 #include <sys/types.h>
     28 #include <sys/ioctl.h>
     29 
     30 #include <ion/ion.h>
     31 #include <linux/ion.h>
     32 #include <cutils/log.h>
     33 #include <cutils/atomic.h>
     34 
     35 #include <hardware/hardware.h>
     36 #include <hardware/gralloc.h>
     37 
     38 #include "gralloc_priv.h"
     39 #include "exynos_format.h"
     40 
     41 #define ION_HEAP_EXYNOS_CONTIG_MASK (1 << 4)
     42 #define ION_EXYNOS_FIMD_VIDEO_MASK  (1 << 28)
     43 #define ION_EXYNOS_MFC_OUTPUT_MASK  (1 << 26)
     44 #define ION_EXYNOS_MFC_INPUT_MASK   (1 << 25)
     45 #define ION_HEAP_SYSTEM_ID          0
     46 #define ION_HEAP_EXYNOS_CONTIG_ID   4
     47 #define ION_HEAP_CHUNK_ID           6
     48 #define MB_1 (1024*1024)
     49 
     50 
     51 /*****************************************************************************/
     52 
     53 struct gralloc_context_t {
     54     alloc_device_t  device;
     55     /* our private data here */
     56 };
     57 
     58 static int gralloc_alloc_buffer(alloc_device_t* dev,
     59                                 size_t size, int usage, buffer_handle_t* pHandle);
     60 
     61 /*****************************************************************************/
     62 
     63 int fb_device_open(const hw_module_t* module, const char* name,
     64                    hw_device_t** device);
     65 
     66 static int gralloc_device_open(const hw_module_t* module, const char* name,
     67                                hw_device_t** device);
     68 
     69 extern int gralloc_lock(gralloc_module_t const* module,
     70                         buffer_handle_t handle, int usage,
     71                         int l, int t, int w, int h,
     72                         void** vaddr);
     73 
     74 extern int gralloc_unlock(gralloc_module_t const* module,
     75                           buffer_handle_t handle);
     76 
     77 extern int gralloc_register_buffer(gralloc_module_t const* module,
     78                                    buffer_handle_t handle);
     79 
     80 extern int gralloc_unregister_buffer(gralloc_module_t const* module,
     81                                      buffer_handle_t handle);
     82 
     83 /*****************************************************************************/
     84 
     85 static struct hw_module_methods_t gralloc_module_methods = {
     86 open: gralloc_device_open
     87 };
     88 
     89 struct private_module_t HAL_MODULE_INFO_SYM = {
     90 base: {
     91     common: {
     92         tag: HARDWARE_MODULE_TAG,
     93         version_major: 1,
     94         version_minor: 0,
     95         id: GRALLOC_HARDWARE_MODULE_ID,
     96         name: "Graphics Memory Allocator Module",
     97         author: "The Android Open Source Project",
     98         methods: &gralloc_module_methods
     99     },
    100     registerBuffer: gralloc_register_buffer,
    101     unregisterBuffer: gralloc_unregister_buffer,
    102     lock: gralloc_lock,
    103     unlock: gralloc_unlock,
    104 },
    105 framebuffer: 0,
    106 flags: 0,
    107 numBuffers: 0,
    108 bufferMask: 0,
    109 lock: PTHREAD_MUTEX_INITIALIZER,
    110 refcount: 0,
    111 currentBuffer: 0,
    112 ionfd: -1,
    113 };
    114 
    115 /*****************************************************************************/
    116 
    117 static unsigned int _select_heap(int usage)
    118 {
    119     unsigned int heap_mask;
    120 
    121     if (usage & GRALLOC_USAGE_PROTECTED)
    122         heap_mask = (1 << ION_HEAP_EXYNOS_CONTIG_ID);
    123     else
    124         heap_mask = (1 << ION_HEAP_SYSTEM_ID) | (1 << ION_HEAP_CHUNK_ID);
    125 
    126     return heap_mask;
    127 }
    128 
    129 static int gralloc_alloc_rgb(int ionfd, int w, int h, int format, int usage,
    130                              unsigned int ion_flags, private_handle_t **hnd, int *stride)
    131 {
    132     size_t size, bpr, alignment = 0;
    133     int bpp = 0, vstride, fd, err;
    134     unsigned int heap_mask = _select_heap(usage);
    135 
    136     if (format == HAL_PIXEL_FORMAT_RGBA_8888) {
    137         bool sw_usage = !!(usage & (GRALLOC_USAGE_SW_READ_MASK |
    138                 GRALLOC_USAGE_SW_WRITE_MASK));
    139 
    140         if (usage & GRALLOC_USAGE_HW_FB) {
    141             ALOGW_IF(sw_usage,
    142                     "framebuffer target should not have SW usage bits; ignoring");
    143             format = HAL_PIXEL_FORMAT_BGRA_8888;
    144         } else if (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) {
    145             if (sw_usage)
    146                 return -EINVAL;
    147             format = HAL_PIXEL_FORMAT_BGRA_8888;
    148         }
    149     }
    150 
    151     switch (format) {
    152         case HAL_PIXEL_FORMAT_RGBA_8888:
    153         case HAL_PIXEL_FORMAT_RGBX_8888:
    154         case HAL_PIXEL_FORMAT_BGRA_8888:
    155         case HAL_PIXEL_FORMAT_sRGB_A_8888:
    156         case HAL_PIXEL_FORMAT_sRGB_X_8888:
    157             bpp = 4;
    158             break;
    159         case HAL_PIXEL_FORMAT_RGB_888:
    160             bpp = 3;
    161             break;
    162         case HAL_PIXEL_FORMAT_RGB_565:
    163         case HAL_PIXEL_FORMAT_RAW_SENSOR:
    164             bpp = 2;
    165             break;
    166         case HAL_PIXEL_FORMAT_BLOB:
    167             *stride = w;
    168             vstride = h;
    169             size = w * h;
    170             break;
    171         default:
    172             return -EINVAL;
    173     }
    174 
    175     if (format != HAL_PIXEL_FORMAT_BLOB) {
    176         bpr = ALIGN(w*bpp, 64);
    177         vstride = ALIGN(h, 16);
    178         if (vstride < h + 2)
    179             size = bpr * (h + 2);
    180         else
    181             size = bpr * vstride;
    182         *stride = bpr / bpp;
    183         size = ALIGN(size, PAGE_SIZE);
    184     }
    185 
    186     if (usage & GRALLOC_USAGE_PROTECTED) {
    187         alignment = MB_1;
    188         ion_flags |= ION_EXYNOS_FIMD_VIDEO_MASK;
    189     }
    190 
    191     err = ion_alloc_fd(ionfd, size, alignment, heap_mask, ion_flags,
    192                        &fd);
    193     *hnd = new private_handle_t(fd, size, usage, w, h, format, *stride,
    194                                 vstride);
    195 
    196     return err;
    197 }
    198 
    199 static int gralloc_alloc_framework_yuv(int ionfd, int w, int h, int format,
    200                                        int usage, unsigned int ion_flags,
    201                                        private_handle_t **hnd, int *stride)
    202 {
    203     size_t size;
    204     int err, fd;
    205     unsigned int heap_mask = _select_heap(usage);
    206 
    207     switch (format) {
    208         case HAL_PIXEL_FORMAT_YV12:
    209             *stride = ALIGN(w, 16);
    210             size = (*stride * h) + (ALIGN(*stride / 2, 16) * h);
    211             break;
    212         case HAL_PIXEL_FORMAT_YCrCb_420_SP:
    213             *stride = w;
    214             size = *stride * h * 3 / 2;
    215             break;
    216         default:
    217             ALOGE("invalid yuv format %d\n", format);
    218             return -EINVAL;
    219     }
    220 
    221     err = ion_alloc_fd(ionfd, size, 0, heap_mask, ion_flags, &fd);
    222     if (err)
    223         return err;
    224 
    225     *hnd = new private_handle_t(fd, size, usage, w, h, format, *stride, h);
    226     return err;
    227 }
    228 
    229 static int gralloc_alloc_yuv(int ionfd, int w, int h, int format,
    230                              int usage, unsigned int ion_flags,
    231                              private_handle_t **hnd, int *stride)
    232 {
    233     size_t luma_size, chroma_size;
    234     int err, planes, fd, fd1, fd2 = 0;
    235     size_t luma_vstride;
    236     unsigned int heap_mask = _select_heap(usage);
    237 
    238     *stride = ALIGN(w, 16);
    239 
    240     if (format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
    241         ALOGV("HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED : usage(%x), flags(%x)\n", usage, ion_flags);
    242         if ((usage & GRALLOC_USAGE_HW_CAMERA_ZSL) == GRALLOC_USAGE_HW_CAMERA_ZSL) {
    243             format = HAL_PIXEL_FORMAT_YCbCr_422_I; // YUYV
    244         } else if (usage & GRALLOC_USAGE_HW_TEXTURE) {
    245             format = HAL_PIXEL_FORMAT_EXYNOS_YV12;
    246         } else if (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) {
    247             format = HAL_PIXEL_FORMAT_YCbCr_420_SP; // NV12M
    248         }
    249     }
    250     switch (format) {
    251         case HAL_PIXEL_FORMAT_EXYNOS_YV12:
    252             {
    253                 *stride = ALIGN(w, 32);
    254                 luma_vstride = ALIGN(h, 16);
    255                 luma_size = luma_vstride * *stride;
    256                 chroma_size = (luma_vstride / 2) * ALIGN(*stride / 2, 16);
    257                 planes = 3;
    258                 break;
    259             }
    260         case HAL_PIXEL_FORMAT_EXYNOS_YCrCb_420_SP:
    261         case HAL_PIXEL_FORMAT_YCbCr_420_SP:
    262         case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
    263             {
    264                 size_t chroma_vstride = ALIGN(h / 2, 32);
    265                 luma_vstride = ALIGN(h, 32);
    266                 luma_size = luma_vstride * *stride;
    267                 chroma_size = chroma_vstride * *stride;
    268                 planes = 2;
    269                 break;
    270             }
    271         case HAL_PIXEL_FORMAT_YV12:
    272         case HAL_PIXEL_FORMAT_YCrCb_420_SP:
    273             return gralloc_alloc_framework_yuv(ionfd, w, h, format, usage,
    274                                                ion_flags, hnd, stride);
    275         case HAL_PIXEL_FORMAT_YCbCr_422_I:
    276             {
    277                 luma_vstride = h;
    278                 luma_size = luma_vstride * *stride * 2;
    279                 chroma_size = 0;
    280                 planes = 1;
    281                 break;
    282             }
    283         default:
    284             ALOGE("invalid yuv format %d\n", format);
    285             return -EINVAL;
    286     }
    287 
    288     if (usage & GRALLOC_USAGE_PROTECTED)
    289 	ion_flags |= ION_EXYNOS_MFC_OUTPUT_MASK;
    290 
    291     err = ion_alloc_fd(ionfd, luma_size, 0, heap_mask, ion_flags, &fd);
    292     if (err)
    293         return err;
    294     if (planes == 1) {
    295         *hnd = new private_handle_t(fd, luma_size, usage, w, h,
    296                                     format, *stride, luma_vstride);
    297     } else {
    298         err = ion_alloc_fd(ionfd, chroma_size, 0, heap_mask, ion_flags, &fd1);
    299         if (err)
    300             goto err1;
    301         if (planes == 3) {
    302             err = ion_alloc_fd(ionfd, chroma_size, 0, heap_mask, ion_flags, &fd2);
    303             if (err)
    304                 goto err2;
    305 
    306             *hnd = new private_handle_t(fd, fd1, fd2, luma_size, usage, w, h,
    307                                         format, *stride, luma_vstride);
    308         } else {
    309             *hnd = new private_handle_t(fd, fd1, luma_size, usage, w, h, format,
    310                                         *stride, luma_vstride);
    311         }
    312     }
    313     // Set chroma & gamut fields
    314     if (!err && *hnd) {
    315         if (usage & GRALLOC_USAGE_PRIVATE_CHROMA) {
    316             (*hnd)->chroma = HAL_PIXEL_CHROMA_BT601_8;
    317             (*hnd)->gamut = HAL_PIXEL_GAMUT_NARROW_8;
    318         } else {
    319             (*hnd)->chroma = HAL_PIXEL_CHROMA_BT709_8;
    320             (*hnd)->gamut = HAL_PIXEL_GAMUT_WIDE_8;
    321         }
    322     }
    323     return err;
    324 
    325 err2:
    326     close(fd1);
    327 err1:
    328     close(fd);
    329     return err;
    330 }
    331 
    332 static int gralloc_alloc(alloc_device_t* dev,
    333                          int w, int h, int format, int usage,
    334                          buffer_handle_t* pHandle, int* pStride)
    335 {
    336     int stride;
    337     int err;
    338     unsigned int ion_flags = 0;
    339     private_handle_t *hnd = NULL;
    340 
    341     if (!pHandle || !pStride)
    342         return -EINVAL;
    343 
    344     if( (usage & GRALLOC_USAGE_SW_READ_MASK) == GRALLOC_USAGE_SW_READ_OFTEN )
    345         ion_flags = ION_FLAG_CACHED | ION_FLAG_CACHED_NEEDS_SYNC;
    346 
    347     private_module_t* m = reinterpret_cast<private_module_t*>
    348         (dev->common.module);
    349     gralloc_module_t* module = reinterpret_cast<gralloc_module_t*>
    350         (dev->common.module);
    351 
    352     err = gralloc_alloc_rgb(m->ionfd, w, h, format, usage, ion_flags, &hnd,
    353                             &stride);
    354     if (err)
    355         err = gralloc_alloc_yuv(m->ionfd, w, h, format, usage, ion_flags,
    356                                 &hnd, &stride);
    357     if (err)
    358         goto err;
    359 
    360     err = gralloc_register_buffer(module, hnd);
    361     if (err)
    362         goto err;
    363 
    364     *pHandle = hnd;
    365     *pStride = stride;
    366     return 0;
    367 err:
    368     if (!hnd)
    369         return err;
    370     close(hnd->fd);
    371     if (hnd->fd1 >= 0)
    372         close(hnd->fd1);
    373     if (hnd->fd2 >= 0)
    374         close(hnd->fd2);
    375     return err;
    376 }
    377 
    378 static int gralloc_free(alloc_device_t* dev,
    379                         buffer_handle_t handle)
    380 {
    381     if (private_handle_t::validate(handle) < 0)
    382         return -EINVAL;
    383 
    384     private_handle_t const* hnd = reinterpret_cast<private_handle_t const*>(handle);
    385     gralloc_module_t* module = reinterpret_cast<gralloc_module_t*>(
    386                                                                    dev->common.module);
    387 
    388     gralloc_unregister_buffer(module, hnd);
    389 
    390     close(hnd->fd);
    391     if (hnd->fd1 >= 0)
    392         close(hnd->fd1);
    393     if (hnd->fd2 >= 0)
    394         close(hnd->fd2);
    395 
    396     delete hnd;
    397     return 0;
    398 }
    399 
    400 /*****************************************************************************/
    401 
    402 static int gralloc_close(struct hw_device_t *dev)
    403 {
    404     gralloc_context_t* ctx = reinterpret_cast<gralloc_context_t*>(dev);
    405     if (ctx) {
    406         private_module_t *p = reinterpret_cast<private_module_t*>(ctx->device.common.module);
    407         pthread_mutex_lock(&p->lock);
    408         LOG_ALWAYS_FATAL_IF(!p->refcount);
    409         p->refcount--;
    410         if (!p->refcount)
    411             close(p->ionfd);
    412         pthread_mutex_unlock(&p->lock);
    413 
    414         /* TODO: keep a list of all buffer_handle_t created, and free them
    415          * all here.
    416          */
    417         free(ctx);
    418     }
    419     return 0;
    420 }
    421 
    422 int gralloc_device_open(const hw_module_t* module, const char* name,
    423                         hw_device_t** device)
    424 {
    425     int status = -EINVAL;
    426     if (!strcmp(name, GRALLOC_HARDWARE_GPU0)) {
    427         gralloc_context_t *dev;
    428         dev = (gralloc_context_t*)malloc(sizeof(*dev));
    429 
    430         /* initialize our state here */
    431         memset(dev, 0, sizeof(*dev));
    432 
    433         /* initialize the procs */
    434         dev->device.common.tag = HARDWARE_DEVICE_TAG;
    435         dev->device.common.version = 0;
    436         dev->device.common.module = const_cast<hw_module_t*>(module);
    437         dev->device.common.close = gralloc_close;
    438 
    439         dev->device.alloc = gralloc_alloc;
    440         dev->device.free = gralloc_free;
    441 
    442         private_module_t *p = reinterpret_cast<private_module_t*>(dev->device.common.module);
    443         pthread_mutex_lock(&p->lock);
    444         if (!p->refcount)
    445             p->ionfd = ion_open();
    446         p->refcount++;
    447         pthread_mutex_unlock(&p->lock);
    448 
    449         *device = &dev->device.common;
    450         status = 0;
    451     } else {
    452         status = fb_device_open(module, name, device);
    453     }
    454     return status;
    455 }
    456