Home | History | Annotate | Download | only in libgralloc
      1 /*
      2  * Copyright (C) 2010 The Android Open Source Project
      3  * Copyright (c) 2011-2014 The Linux Foundation. All rights reserved.
      4  *
      5  * Licensed under the Apache License, Version 2.0 (the "License");
      6  * you may not use this file except in compliance with the License.
      7  * You may obtain a copy of the License at
      8  *
      9  *      http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  * Unless required by applicable law or agreed to in writing, software
     12  * distributed under the License is distributed on an "AS IS" BASIS,
     13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  * See the License for the specific language governing permissions and
     15  * limitations under the License.
     16  */
     17 
     18 #include <limits.h>
     19 #include <unistd.h>
     20 #include <fcntl.h>
     21 #include <cutils/properties.h>
     22 #include <sys/mman.h>
     23 
     24 #include "gr.h"
     25 #include "gpu.h"
     26 #include "memalloc.h"
     27 #include "alloc_controller.h"
     28 #include <qdMetaData.h>
     29 
     30 using namespace gralloc;
     31 
     32 #define SZ_1M 0x100000
     33 #define SZ_2M 0x200000
     34 
     35 gpu_context_t::gpu_context_t(const private_module_t* module,
     36                              IAllocController* alloc_ctrl ) :
     37     mAllocCtrl(alloc_ctrl)
     38 {
     39     // Zero out the alloc_device_t
     40     memset(static_cast<alloc_device_t*>(this), 0, sizeof(alloc_device_t));
     41 
     42     // Initialize the procs
     43     common.tag     = HARDWARE_DEVICE_TAG;
     44     common.version = 0;
     45     common.module  = const_cast<hw_module_t*>(&module->base.common);
     46     common.close   = gralloc_close;
     47     alloc          = gralloc_alloc;
     48 #ifdef QCOM_BSP
     49     allocSize      = gralloc_alloc_size;
     50 #endif
     51     free           = gralloc_free;
     52 
     53 }
     54 
     55 int gpu_context_t::gralloc_alloc_buffer(size_t size, int usage,
     56                                         buffer_handle_t* pHandle, int bufferType,
     57                                         int format, int width, int height)
     58 {
     59     int err = 0;
     60     int flags = 0;
     61     size = roundUpToPageSize(size);
     62     alloc_data data;
     63     data.offset = 0;
     64     data.fd = -1;
     65     data.base = 0;
     66     if(format == HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED)
     67         data.align = 8192;
     68     else
     69         data.align = getpagesize();
     70 
     71     /* force 1MB alignment selectively for secure buffers, MDP5 onwards */
     72 #ifdef MDSS_TARGET
     73     if ((usage & GRALLOC_USAGE_PROTECTED) &&
     74         (usage & GRALLOC_USAGE_PRIVATE_MM_HEAP)) {
     75         data.align = ALIGN((int) data.align, SZ_2M);
     76         size = ALIGN(size, data.align);
     77     }
     78 #endif
     79 
     80     data.size = size;
     81     data.pHandle = (uintptr_t) pHandle;
     82     err = mAllocCtrl->allocate(data, usage);
     83 
     84     if (!err) {
     85         /* allocate memory for enhancement data */
     86         alloc_data eData;
     87         eData.fd = -1;
     88         eData.base = 0;
     89         eData.offset = 0;
     90         eData.size = ROUND_UP_PAGESIZE(sizeof(MetaData_t));
     91         eData.pHandle = data.pHandle;
     92         eData.align = getpagesize();
     93         int eDataUsage = GRALLOC_USAGE_PRIVATE_SYSTEM_HEAP;
     94         int eDataErr = mAllocCtrl->allocate(eData, eDataUsage);
     95         ALOGE_IF(eDataErr, "gralloc failed for eDataErr=%s",
     96                                           strerror(-eDataErr));
     97 
     98         if (usage & GRALLOC_USAGE_PRIVATE_EXTERNAL_ONLY) {
     99             flags |= private_handle_t::PRIV_FLAGS_EXTERNAL_ONLY;
    100         }
    101 
    102         ColorSpace_t colorSpace = ITU_R_601;
    103         flags |= private_handle_t::PRIV_FLAGS_ITU_R_601;
    104         if (bufferType == BUFFER_TYPE_VIDEO) {
    105             if (usage & GRALLOC_USAGE_HW_CAMERA_WRITE) {
    106 #ifndef MDSS_TARGET
    107                 colorSpace = ITU_R_601_FR;
    108                 flags |= private_handle_t::PRIV_FLAGS_ITU_R_601_FR;
    109 #else
    110                 // Per the camera spec ITU 709 format should be set only for
    111                 // video encoding.
    112                 // It should be set to ITU 601 full range format for any other
    113                 // camera buffer
    114                 //
    115                 if (usage & GRALLOC_USAGE_HW_CAMERA_MASK) {
    116                     if (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) {
    117                         flags |= private_handle_t::PRIV_FLAGS_ITU_R_709;
    118                         colorSpace = ITU_R_709;
    119                     } else {
    120                         flags |= private_handle_t::PRIV_FLAGS_ITU_R_601_FR;
    121                         colorSpace = ITU_R_601_FR;
    122                     }
    123                 }
    124 #endif
    125             }
    126         }
    127 
    128         if (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER ) {
    129             flags |= private_handle_t::PRIV_FLAGS_VIDEO_ENCODER;
    130         }
    131 
    132         if (usage & GRALLOC_USAGE_HW_CAMERA_WRITE) {
    133             flags |= private_handle_t::PRIV_FLAGS_CAMERA_WRITE;
    134         }
    135 
    136         if (usage & GRALLOC_USAGE_HW_CAMERA_READ) {
    137             flags |= private_handle_t::PRIV_FLAGS_CAMERA_READ;
    138         }
    139 
    140         if (usage & GRALLOC_USAGE_HW_COMPOSER) {
    141             flags |= private_handle_t::PRIV_FLAGS_HW_COMPOSER;
    142         }
    143 
    144         if (usage & GRALLOC_USAGE_HW_TEXTURE) {
    145             flags |= private_handle_t::PRIV_FLAGS_HW_TEXTURE;
    146         }
    147 
    148         if(usage & GRALLOC_USAGE_PRIVATE_SECURE_DISPLAY) {
    149             flags |= private_handle_t::PRIV_FLAGS_SECURE_DISPLAY;
    150         }
    151 
    152         if(isMacroTileEnabled(format, usage)) {
    153             flags |= private_handle_t::PRIV_FLAGS_TILE_RENDERED;
    154         }
    155 
    156         flags |= data.allocType;
    157         uintptr_t eBaseAddr = (uintptr_t)(eData.base) + eData.offset;
    158         private_handle_t *hnd = new private_handle_t(data.fd, size, flags,
    159                 bufferType, format, width, height, eData.fd, eData.offset,
    160                 eBaseAddr);
    161 
    162         hnd->offset = data.offset;
    163         hnd->base = (uintptr_t)(data.base) + data.offset;
    164         hnd->gpuaddr = 0;
    165         setMetaData(hnd, UPDATE_COLOR_SPACE, (void*) &colorSpace);
    166 
    167         *pHandle = hnd;
    168     }
    169 
    170     ALOGE_IF(err, "gralloc failed err=%s", strerror(-err));
    171 
    172     return err;
    173 }
    174 
    175 void gpu_context_t::getGrallocInformationFromFormat(int inputFormat,
    176                                                     int *bufferType)
    177 {
    178     *bufferType = BUFFER_TYPE_VIDEO;
    179 
    180     if (inputFormat <= HAL_PIXEL_FORMAT_BGRA_8888) {
    181         // RGB formats
    182         *bufferType = BUFFER_TYPE_UI;
    183     } else if ((inputFormat == HAL_PIXEL_FORMAT_R_8) ||
    184                (inputFormat == HAL_PIXEL_FORMAT_RG_88)) {
    185         *bufferType = BUFFER_TYPE_UI;
    186     }
    187 }
    188 
    189 int gpu_context_t::gralloc_alloc_framebuffer_locked(int usage,
    190                                                     buffer_handle_t* pHandle)
    191 {
    192     private_module_t* m = reinterpret_cast<private_module_t*>(common.module);
    193 
    194     // we don't support framebuffer allocations with graphics heap flags
    195     if (usage & GRALLOC_HEAP_MASK) {
    196         return -EINVAL;
    197     }
    198 
    199     if (m->framebuffer == NULL) {
    200         ALOGE("%s: Invalid framebuffer", __FUNCTION__);
    201         return -EINVAL;
    202     }
    203 
    204     const size_t bufferMask = m->bufferMask;
    205     const uint32_t numBuffers = m->numBuffers;
    206     size_t bufferSize = m->finfo.line_length * m->info.yres;
    207 
    208     //adreno needs FB size to be page aligned
    209     bufferSize = roundUpToPageSize(bufferSize);
    210 
    211     if (numBuffers == 1) {
    212         // If we have only one buffer, we never use page-flipping. Instead,
    213         // we return a regular buffer which will be memcpy'ed to the main
    214         // screen when post is called.
    215         int newUsage = (usage & ~GRALLOC_USAGE_HW_FB) | GRALLOC_USAGE_HW_2D;
    216         return gralloc_alloc_buffer(bufferSize, newUsage, pHandle, BUFFER_TYPE_UI,
    217                                     m->fbFormat, m->info.xres, m->info.yres);
    218     }
    219 
    220     if (bufferMask >= ((1LU<<numBuffers)-1)) {
    221         // We ran out of buffers.
    222         return -ENOMEM;
    223     }
    224 
    225     // create a "fake" handle for it
    226     uintptr_t vaddr = uintptr_t(m->framebuffer->base);
    227     private_handle_t* hnd = new private_handle_t(
    228         dup(m->framebuffer->fd), bufferSize,
    229         private_handle_t::PRIV_FLAGS_USES_PMEM |
    230         private_handle_t::PRIV_FLAGS_FRAMEBUFFER,
    231         BUFFER_TYPE_UI, m->fbFormat, m->info.xres,
    232         m->info.yres);
    233 
    234     // find a free slot
    235     for (uint32_t i=0 ; i<numBuffers ; i++) {
    236         if ((bufferMask & (1LU<<i)) == 0) {
    237             m->bufferMask |= (1LU<<i);
    238             break;
    239         }
    240         vaddr += bufferSize;
    241     }
    242     hnd->base = vaddr;
    243     hnd->offset = vaddr - uintptr_t(m->framebuffer->base);
    244     *pHandle = hnd;
    245     return 0;
    246 }
    247 
    248 
    249 int gpu_context_t::gralloc_alloc_framebuffer(int usage,
    250                                              buffer_handle_t* pHandle)
    251 {
    252     private_module_t* m = reinterpret_cast<private_module_t*>(common.module);
    253     pthread_mutex_lock(&m->lock);
    254     int err = gralloc_alloc_framebuffer_locked(usage, pHandle);
    255     pthread_mutex_unlock(&m->lock);
    256     return err;
    257 }
    258 
    259 int gpu_context_t::alloc_impl(int w, int h, int format, int usage,
    260                               buffer_handle_t* pHandle, int* pStride,
    261                               size_t bufferSize) {
    262     if (!pHandle || !pStride)
    263         return -EINVAL;
    264 
    265     size_t size;
    266     int alignedw, alignedh;
    267     int grallocFormat = format;
    268     int bufferType;
    269 
    270     //If input format is HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED then based on
    271     //the usage bits, gralloc assigns a format.
    272     if(format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED ||
    273        format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
    274         if(usage & GRALLOC_USAGE_HW_VIDEO_ENCODER)
    275             grallocFormat = HAL_PIXEL_FORMAT_NV12_ENCODEABLE; //NV12
    276         else if((usage & GRALLOC_USAGE_HW_CAMERA_MASK)
    277                 == GRALLOC_USAGE_HW_CAMERA_ZSL)
    278             grallocFormat = HAL_PIXEL_FORMAT_NV21_ZSL; //NV21 ZSL
    279         else if(usage & GRALLOC_USAGE_HW_CAMERA_READ)
    280             grallocFormat = HAL_PIXEL_FORMAT_YCrCb_420_SP; //NV21
    281         else if(usage & GRALLOC_USAGE_HW_CAMERA_WRITE)
    282             grallocFormat = HAL_PIXEL_FORMAT_YCrCb_420_SP; //NV21
    283         else if(usage & GRALLOC_USAGE_HW_COMPOSER)
    284             grallocFormat = HAL_PIXEL_FORMAT_RGBA_8888;
    285         //If flexible yuv is used for sw read/write, need map to NV21
    286         else if(format == HAL_PIXEL_FORMAT_YCbCr_420_888 &&
    287             (usage & GRALLOC_USAGE_SW_WRITE_MASK ||
    288             usage & GRALLOC_USAGE_SW_READ_MASK)) {
    289             grallocFormat = HAL_PIXEL_FORMAT_YCrCb_420_SP;
    290         }
    291     }
    292 
    293     getGrallocInformationFromFormat(grallocFormat, &bufferType);
    294     size = getBufferSizeAndDimensions(w, h, grallocFormat, usage, alignedw,
    295                    alignedh);
    296 
    297     if ((ssize_t)size <= 0)
    298         return -EINVAL;
    299     size = (bufferSize >= size)? bufferSize : size;
    300 
    301     bool useFbMem = false;
    302     char property[PROPERTY_VALUE_MAX];
    303     if((usage & GRALLOC_USAGE_HW_FB) &&
    304        (property_get("debug.gralloc.map_fb_memory", property, NULL) > 0) &&
    305        (!strncmp(property, "1", PROPERTY_VALUE_MAX ) ||
    306         (!strncasecmp(property,"true", PROPERTY_VALUE_MAX )))) {
    307         useFbMem = true;
    308     }
    309 
    310     int err = 0;
    311     if(useFbMem) {
    312         err = gralloc_alloc_framebuffer(usage, pHandle);
    313     } else {
    314         err = gralloc_alloc_buffer(size, usage, pHandle, bufferType,
    315                                    grallocFormat, alignedw, alignedh);
    316     }
    317 
    318     if (err < 0) {
    319         return err;
    320     }
    321 
    322     *pStride = alignedw;
    323     return 0;
    324 }
    325 
    326 int gpu_context_t::free_impl(private_handle_t const* hnd) {
    327     private_module_t* m = reinterpret_cast<private_module_t*>(common.module);
    328     if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) {
    329         const size_t bufferSize = m->finfo.line_length * m->info.yres;
    330         size_t index = (hnd->base - m->framebuffer->base) / bufferSize;
    331         m->bufferMask &= ~(1LU<<index);
    332     } else {
    333 
    334         terminateBuffer(&m->base, const_cast<private_handle_t*>(hnd));
    335         IMemAlloc* memalloc = mAllocCtrl->getAllocator(hnd->flags);
    336         int err = memalloc->free_buffer((void*)hnd->base, (size_t) hnd->size,
    337                                         hnd->offset, hnd->fd);
    338         if(err)
    339             return err;
    340         // free the metadata space
    341         size_t size = ROUND_UP_PAGESIZE(sizeof(MetaData_t));
    342         err = memalloc->free_buffer((void*)hnd->base_metadata,
    343                                     size, hnd->offset_metadata,
    344                                     hnd->fd_metadata);
    345         if (err)
    346             return err;
    347     }
    348     delete hnd;
    349     return 0;
    350 }
    351 
    352 int gpu_context_t::gralloc_alloc(alloc_device_t* dev, int w, int h, int format,
    353                                  int usage, buffer_handle_t* pHandle,
    354                                  int* pStride)
    355 {
    356     if (!dev) {
    357         return -EINVAL;
    358     }
    359     gpu_context_t* gpu = reinterpret_cast<gpu_context_t*>(dev);
    360     return gpu->alloc_impl(w, h, format, usage, pHandle, pStride, 0);
    361 }
    362 int gpu_context_t::gralloc_alloc_size(alloc_device_t* dev, int w, int h,
    363                                       int format, int usage,
    364                                       buffer_handle_t* pHandle, int* pStride,
    365                                       int bufferSize)
    366 {
    367     if (!dev) {
    368         return -EINVAL;
    369     }
    370     gpu_context_t* gpu = reinterpret_cast<gpu_context_t*>(dev);
    371     return gpu->alloc_impl(w, h, format, usage, pHandle, pStride, bufferSize);
    372 }
    373 
    374 
    375 int gpu_context_t::gralloc_free(alloc_device_t* dev,
    376                                 buffer_handle_t handle)
    377 {
    378     if (private_handle_t::validate(handle) < 0)
    379         return -EINVAL;
    380 
    381     private_handle_t const* hnd = reinterpret_cast<private_handle_t const*>(handle);
    382     gpu_context_t* gpu = reinterpret_cast<gpu_context_t*>(dev);
    383     return gpu->free_impl(hnd);
    384 }
    385 
    386 /*****************************************************************************/
    387 
    388 int gpu_context_t::gralloc_close(struct hw_device_t *dev)
    389 {
    390     gpu_context_t* ctx = reinterpret_cast<gpu_context_t*>(dev);
    391     if (ctx) {
    392         /* TODO: keep a list of all buffer_handle_t created, and free them
    393          * all here.
    394          */
    395         delete ctx;
    396     }
    397     return 0;
    398 }
    399