Home | History | Annotate | Download | only in libgralloc1
      1 /*
      2  * Copyright (c) 2016, The Linux Foundation. All rights reserved.
      3 
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions are
      6  * met:
      7  *   * Redistributions of source code must retain the above copyright
      8  *     notice, this list of conditions and the following disclaimer.
      9  *   * Redistributions in binary form must reproduce the above
     10  *     copyright notice, this list of conditions and the following
     11  *     disclaimer in the documentation and/or other materials provided
     12  *     with the distribution.
     13  *   * Neither the name of The Linux Foundation nor the names of its
     14  *     contributors may be used to endorse or promote products derived
     15  *     from this software without specific prior written permission.
     16  *
     17  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
     18  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
     20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
     21  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
     24  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     25  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
     26  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
     27  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  */
     29 
     30 #include <cutils/log.h>
     31 #include <sync/sync.h>
     32 
     33 #include "gr_device_impl.h"
     34 #include "gr_buf_descriptor.h"
     35 #include "gralloc_priv.h"
     36 #include "qd_utils.h"
     37 #include "qdMetaData.h"
     38 #include "gr_utils.h"
     39 
     40 int gralloc_device_open(const struct hw_module_t *module, const char *name, hw_device_t **device);
     41 
     42 int gralloc_device_close(struct hw_device_t *device);
     43 
     44 static struct hw_module_methods_t gralloc_module_methods = {.open = gralloc_device_open};
     45 
     46 struct hw_module_t gralloc_module = {};
     47 
     48 struct private_module_t HAL_MODULE_INFO_SYM = {
     49   .base = {
     50     .tag = HARDWARE_MODULE_TAG,
     51     .version_major = 1,
     52     .version_minor = 0,
     53     .id = GRALLOC_HARDWARE_MODULE_ID,
     54     .name = "Graphics Memory Module",
     55     .author = "Code Aurora Forum",
     56     .methods = &gralloc_module_methods,
     57     .dso = 0,
     58     .reserved = {0},
     59   },
     60 };
     61 
     62 int gralloc_device_open(const struct hw_module_t *module, const char *name, hw_device_t **device) {
     63   int status = -EINVAL;
     64   if (!strcmp(name, GRALLOC_HARDWARE_MODULE_ID)) {
     65     const private_module_t *m = reinterpret_cast<const private_module_t *>(module);
     66     gralloc1::GrallocImpl * /*gralloc1_device_t*/ dev = new gralloc1::GrallocImpl(m);
     67     *device = reinterpret_cast<hw_device_t *>(dev);
     68 
     69     if (dev->Init()) {
     70       status = 0;
     71     } else {
     72       ALOGE(" Error in opening gralloc1 device");
     73       return status;
     74     }
     75   }
     76 
     77   return status;
     78 }
     79 
     80 namespace gralloc1 {
     81 
     82 GrallocImpl::GrallocImpl(const private_module_t *module) {
     83   common.tag = HARDWARE_DEVICE_TAG;
     84   common.version = 1;  // TODO(user): cross check version
     85   common.module = const_cast<hw_module_t *>(&module->base);
     86   common.close = CloseDevice;
     87   getFunction = GetFunction;
     88   getCapabilities = GetCapabilities;
     89 }
     90 
     91 bool GrallocImpl::Init() {
     92   buf_mgr_ = new BufferManager();
     93 
     94   return buf_mgr_->Init();
     95 }
     96 
     97 GrallocImpl::~GrallocImpl() {
     98   if (buf_mgr_) {
     99     delete buf_mgr_;
    100   }
    101 }
    102 
    103 int GrallocImpl::CloseDevice(hw_device_t *device) {
    104   GrallocImpl *impl = reinterpret_cast<GrallocImpl *>(device);
    105   delete impl;
    106 
    107   return 0;
    108 }
    109 
    110 void GrallocImpl::GetCapabilities(struct gralloc1_device *device, uint32_t *out_count,
    111                                   int32_t /*gralloc1_capability_t*/ *out_capabilities) {
    112   if (!device) {
    113     // Need to plan for adding more capabilities
    114     if (out_capabilities == NULL) {
    115       *out_count = 1;
    116     } else {
    117       *out_capabilities = GRALLOC1_CAPABILITY_TEST_ALLOCATE;
    118     }
    119   }
    120 
    121   return;
    122 }
    123 
    124 gralloc1_function_pointer_t GrallocImpl::GetFunction(gralloc1_device_t *device, int32_t function) {
    125   if (!device) {
    126     return NULL;
    127   }
    128 
    129   switch (function) {
    130     case GRALLOC1_FUNCTION_CREATE_DESCRIPTOR:
    131       return reinterpret_cast<gralloc1_function_pointer_t>(CreateBufferDescriptor);
    132     case GRALLOC1_FUNCTION_DESTROY_DESCRIPTOR:
    133       return reinterpret_cast<gralloc1_function_pointer_t>(DestroyBufferDescriptor);
    134     case GRALLOC1_FUNCTION_SET_CONSUMER_USAGE:
    135       return reinterpret_cast<gralloc1_function_pointer_t>(SetConsumerUsage);
    136     case GRALLOC1_FUNCTION_SET_DIMENSIONS:
    137       return reinterpret_cast<gralloc1_function_pointer_t>(SetBufferDimensions);
    138     case GRALLOC1_FUNCTION_SET_FORMAT:
    139       return reinterpret_cast<gralloc1_function_pointer_t>(SetColorFormat);
    140     case GRALLOC1_FUNCTION_SET_PRODUCER_USAGE:
    141       return reinterpret_cast<gralloc1_function_pointer_t>(SetProducerUsage);
    142     case GRALLOC1_FUNCTION_GET_BACKING_STORE:
    143       return reinterpret_cast<gralloc1_function_pointer_t>(GetBackingStore);
    144     case GRALLOC1_FUNCTION_GET_CONSUMER_USAGE:
    145       return reinterpret_cast<gralloc1_function_pointer_t>(GetConsumerUsage);
    146     case GRALLOC1_FUNCTION_GET_DIMENSIONS:
    147       return reinterpret_cast<gralloc1_function_pointer_t>(GetBufferDimensions);
    148     case GRALLOC1_FUNCTION_GET_FORMAT:
    149       return reinterpret_cast<gralloc1_function_pointer_t>(GetColorFormat);
    150     case GRALLOC1_FUNCTION_GET_PRODUCER_USAGE:
    151       return reinterpret_cast<gralloc1_function_pointer_t>(GetProducerUsage);
    152     case GRALLOC1_FUNCTION_GET_STRIDE:
    153       return reinterpret_cast<gralloc1_function_pointer_t>(GetBufferStride);
    154     case GRALLOC1_FUNCTION_ALLOCATE:
    155       return reinterpret_cast<gralloc1_function_pointer_t>(AllocateBuffers);
    156     case GRALLOC1_FUNCTION_RETAIN:
    157       return reinterpret_cast<gralloc1_function_pointer_t>(RetainBuffer);
    158     case GRALLOC1_FUNCTION_RELEASE:
    159       return reinterpret_cast<gralloc1_function_pointer_t>(ReleaseBuffer);
    160     /*  TODO(user) :definition of flex plane is not known yet
    161      *  Need to implement after clarification from Google.
    162     * case GRALLOC1_FUNCTION_GET_NUM_FLEX_PLANES:
    163       return reinterpret_cast<gralloc1_function_pointer_t> (; */
    164     case GRALLOC1_FUNCTION_LOCK:
    165       return reinterpret_cast<gralloc1_function_pointer_t>(LockBuffer);
    166     /*  TODO(user) : LOCK_YCBCR changed to LOCK_FLEX but structure is not known yet.
    167      *  Need to implement after clarification from Google.
    168     case GRALLOC1_PFN_LOCK_FLEX:
    169       return reinterpret_cast<gralloc1_function_pointer_t> (LockYCbCrBuffer;
    170     */
    171     case GRALLOC1_FUNCTION_UNLOCK:
    172       return reinterpret_cast<gralloc1_function_pointer_t>(UnlockBuffer);
    173     case GRALLOC1_FUNCTION_PERFORM:
    174       return reinterpret_cast<gralloc1_function_pointer_t>(Gralloc1Perform);
    175     default:
    176       ALOGE("%s:Gralloc Error. Client Requested for unsupported function", __FUNCTION__);
    177       return NULL;
    178   }
    179 
    180   return NULL;
    181 }
    182 
    183 gralloc1_error_t GrallocImpl::CheckDeviceAndDescriptor(gralloc1_device_t *device,
    184                                                        gralloc1_buffer_descriptor_t descriptor) {
    185   if (!device || !BUF_DESCRIPTOR(descriptor)->IsValid()) {
    186     ALOGE("Gralloc Error : device=%p, descriptor=%p", (void *)device, (void *)descriptor);
    187     return GRALLOC1_ERROR_BAD_DESCRIPTOR;
    188   }
    189 
    190   return GRALLOC1_ERROR_NONE;
    191 }
    192 
    193 gralloc1_error_t GrallocImpl::CheckDeviceAndHandle(gralloc1_device_t *device,
    194                                                    buffer_handle_t buffer) {
    195   const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer);
    196   if (!device || (private_handle_t::validate(hnd) != 0)) {
    197     ALOGE("Gralloc Error : device= %p, buffer-handle=%p", (void *)device, (void *)buffer);
    198     return GRALLOC1_ERROR_BAD_HANDLE;
    199   }
    200 
    201   return GRALLOC1_ERROR_NONE;
    202 }
    203 
    204 gralloc1_error_t GrallocImpl::CreateBufferDescriptor(gralloc1_device_t *device,
    205                                                      gralloc1_buffer_descriptor_t *out_descriptor) {
    206   if (!device) {
    207     return GRALLOC1_ERROR_BAD_DESCRIPTOR;
    208   }
    209 
    210   BufferDescriptor *descriptor = new BufferDescriptor();
    211   if (descriptor == NULL) {
    212     return GRALLOC1_ERROR_NO_RESOURCES;
    213   }
    214 
    215   *out_descriptor = reinterpret_cast<gralloc1_buffer_descriptor_t>(descriptor);
    216 
    217   return GRALLOC1_ERROR_NONE;
    218 }
    219 
    220 gralloc1_error_t GrallocImpl::DestroyBufferDescriptor(gralloc1_device_t *device,
    221                                                       gralloc1_buffer_descriptor_t descriptor) {
    222   gralloc1_error_t status = CheckDeviceAndDescriptor(device, descriptor);
    223   if (status == GRALLOC1_ERROR_NONE) {
    224     delete reinterpret_cast<BufferDescriptor *>(descriptor);
    225   }
    226 
    227   return status;
    228 }
    229 
    230 gralloc1_error_t GrallocImpl::SetConsumerUsage(gralloc1_device_t *device,
    231                                                gralloc1_buffer_descriptor_t descriptor,
    232                                                gralloc1_consumer_usage_t usage) {
    233   gralloc1_error_t status = CheckDeviceAndDescriptor(device, descriptor);
    234   if (status == GRALLOC1_ERROR_NONE) {
    235     BUF_DESCRIPTOR(descriptor)->SetConsumerUsage(usage);
    236   }
    237 
    238   return status;
    239 }
    240 
    241 gralloc1_error_t GrallocImpl::SetBufferDimensions(gralloc1_device_t *device,
    242                                                   gralloc1_buffer_descriptor_t descriptor,
    243                                                   uint32_t width, uint32_t height) {
    244   gralloc1_error_t status = CheckDeviceAndDescriptor(device, descriptor);
    245   if (status == GRALLOC1_ERROR_NONE) {
    246     BUF_DESCRIPTOR(descriptor)->SetDimensions(INT(width), INT(height));
    247   }
    248 
    249   return status;
    250 }
    251 
    252 gralloc1_error_t GrallocImpl::SetColorFormat(gralloc1_device_t *device,
    253                                              gralloc1_buffer_descriptor_t descriptor,
    254                                              int32_t format) {
    255   gralloc1_error_t status = CheckDeviceAndDescriptor(device, descriptor);
    256   if (status == GRALLOC1_ERROR_NONE) {
    257     BUF_DESCRIPTOR(descriptor)->SetColorFormat(format);
    258   }
    259 
    260   return status;
    261 }
    262 
    263 gralloc1_error_t GrallocImpl::SetProducerUsage(gralloc1_device_t *device,
    264                                                gralloc1_buffer_descriptor_t descriptor,
    265                                                gralloc1_producer_usage_t usage) {
    266   gralloc1_error_t status = CheckDeviceAndDescriptor(device, descriptor);
    267   if (status == GRALLOC1_ERROR_NONE) {
    268     BUF_DESCRIPTOR(descriptor)->SetProducerUsage(usage);
    269   }
    270 
    271   return status;
    272 }
    273 
    274 gralloc1_error_t GrallocImpl::GetBackingStore(gralloc1_device_t *device, buffer_handle_t buffer,
    275                                               gralloc1_backing_store_t *out_backstore) {
    276   if (!device || !buffer) {
    277     return GRALLOC1_ERROR_BAD_HANDLE;
    278   }
    279 
    280   *out_backstore =
    281       static_cast<gralloc1_backing_store_t>(PRIV_HANDLE_CONST(buffer)->GetBackingstore());
    282 
    283   return GRALLOC1_ERROR_NONE;
    284 }
    285 
    286 gralloc1_error_t GrallocImpl::GetConsumerUsage(gralloc1_device_t *device, buffer_handle_t buffer,
    287                                                gralloc1_consumer_usage_t *outUsage) {
    288   gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
    289   if (status == GRALLOC1_ERROR_NONE) {
    290     *outUsage = PRIV_HANDLE_CONST(buffer)->GetConsumerUsage();
    291   }
    292 
    293   return status;
    294 }
    295 
    296 gralloc1_error_t GrallocImpl::GetBufferDimensions(gralloc1_device_t *device, buffer_handle_t buffer,
    297                                                   uint32_t *outWidth, uint32_t *outHeight) {
    298   gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
    299   if (status == GRALLOC1_ERROR_NONE) {
    300     const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer);
    301     *outWidth = UINT(hnd->GetRealWidth());
    302     *outHeight = UINT(hnd->GetRealHeight());
    303   }
    304 
    305   return status;
    306 }
    307 
    308 gralloc1_error_t GrallocImpl::GetColorFormat(gralloc1_device_t *device, buffer_handle_t buffer,
    309                                              int32_t *outFormat) {
    310   gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
    311   if (status == GRALLOC1_ERROR_NONE) {
    312     *outFormat = PRIV_HANDLE_CONST(buffer)->GetColorFormat();
    313   }
    314 
    315   return status;
    316 }
    317 
    318 gralloc1_error_t GrallocImpl::GetProducerUsage(gralloc1_device_t *device, buffer_handle_t buffer,
    319                                                gralloc1_producer_usage_t *outUsage) {
    320   gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
    321   if (status == GRALLOC1_ERROR_NONE) {
    322     const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer);
    323     *outUsage = hnd->GetProducerUsage();
    324   }
    325 
    326   return status;
    327 }
    328 
    329 gralloc1_error_t GrallocImpl::GetBufferStride(gralloc1_device_t *device, buffer_handle_t buffer,
    330                                               uint32_t *outStride) {
    331   gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
    332   if (status == GRALLOC1_ERROR_NONE) {
    333     *outStride = UINT(PRIV_HANDLE_CONST(buffer)->GetStride());
    334   }
    335 
    336   return status;
    337 }
    338 
    339 gralloc1_error_t GrallocImpl::AllocateBuffers(gralloc1_device_t *device, uint32_t num_dptors,
    340                                               const gralloc1_buffer_descriptor_t *dptors,
    341                                               buffer_handle_t *outBuffers) {
    342   if (!num_dptors || !dptors) {
    343     return GRALLOC1_ERROR_BAD_DESCRIPTOR;
    344   }
    345 
    346   GrallocImpl const *dev = GRALLOC_IMPL(device);
    347   const BufferDescriptor *descriptors = reinterpret_cast<const BufferDescriptor *>(dptors);
    348   gralloc1_error_t status = dev->buf_mgr_->AllocateBuffers(num_dptors, descriptors, outBuffers);
    349 
    350   return status;
    351 }
    352 
    353 gralloc1_error_t GrallocImpl::RetainBuffer(gralloc1_device_t *device, buffer_handle_t buffer) {
    354   gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
    355   if (status == GRALLOC1_ERROR_NONE) {
    356     const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer);
    357     GrallocImpl const *dev = GRALLOC_IMPL(device);
    358     status = dev->buf_mgr_->RetainBuffer(hnd);
    359   }
    360 
    361   return status;
    362 }
    363 
    364 gralloc1_error_t GrallocImpl::ReleaseBuffer(gralloc1_device_t *device, buffer_handle_t buffer) {
    365   gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
    366   if (status == GRALLOC1_ERROR_NONE) {
    367     const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer);
    368     GrallocImpl const *dev = GRALLOC_IMPL(device);
    369     status = dev->buf_mgr_->ReleaseBuffer(hnd);
    370   }
    371 
    372   return status;
    373 }
    374 
    375 gralloc1_error_t GrallocImpl::LockBuffer(gralloc1_device_t *device, buffer_handle_t buffer,
    376                                          gralloc1_producer_usage_t prod_usage,
    377                                          gralloc1_consumer_usage_t cons_usage,
    378                                          const gralloc1_rect_t *region, void **out_data,
    379                                          int32_t acquire_fence) {
    380   gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
    381   if (status == GRALLOC1_ERROR_NONE && (acquire_fence > 0)) {
    382     int error = sync_wait(acquire_fence, 1000);
    383     if (error < 0) {
    384       ALOGE("%s: sync_wait timedout! error = %s", __FUNCTION__, strerror(errno));
    385       return GRALLOC1_ERROR_UNDEFINED;
    386     }
    387   }
    388 
    389   const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer);
    390   GrallocImpl const *dev = GRALLOC_IMPL(device);
    391 
    392   // Either producer usage or consumer usage must be *_USAGE_NONE
    393   if ((prod_usage != GRALLOC1_PRODUCER_USAGE_NONE) &&
    394       (cons_usage != GRALLOC1_CONSUMER_USAGE_NONE)) {
    395     return GRALLOC1_ERROR_BAD_VALUE;
    396   }
    397 
    398   // currently we ignore the region/rect client wants to lock
    399   if (region == NULL) {
    400     return GRALLOC1_ERROR_BAD_VALUE;
    401   }
    402 
    403   status = dev->buf_mgr_->LockBuffer(hnd, prod_usage, cons_usage);
    404 
    405   *out_data = reinterpret_cast<void *>(hnd->base);
    406 
    407   return status;
    408 }
    409 
    410 /*  TODO(user) : LOCK_YCBCR changed to LOCK_FLEX but structure definition is not known yet.
    411  *  Need to implement after clarification from Google.
    412 gralloc1_error_t GrallocImpl::LockYCbCrBuffer(gralloc1_device_t* device, buffer_handle_t buffer,
    413     gralloc1_producer_usage_t prod_usage, gralloc1_consumer_usage_t cons_usage,
    414     const gralloc1_rect_t* region, struct android_ycbcr* outYCbCr, int32_t* outAcquireFence) {
    415   gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
    416 
    417   if (status == GRALLOC1_ERROR_NONE) {
    418     void **outData = 0;
    419     status = LockBuffer(device, buffer, prod_usage, cons_usage, region, outData, outAcquireFence);
    420   }
    421 
    422   if (status == GRALLOC1_ERROR_NONE) {
    423     const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer);
    424     GrallocImpl const *dev = GRALLOC_IMPL(device);
    425     dev->allocator_->GetYUVPlaneInfo(hnd, outYCbCr);
    426   }
    427 
    428   return status;
    429 }
    430  */
    431 
    432 gralloc1_error_t GrallocImpl::UnlockBuffer(gralloc1_device_t *device, buffer_handle_t buffer,
    433                                            int32_t *release_fence) {
    434   gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
    435 
    436   if (status != GRALLOC1_ERROR_NONE) {
    437     return status;
    438   }
    439 
    440   const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer);
    441   GrallocImpl const *dev = GRALLOC_IMPL(device);
    442 
    443   *release_fence = -1;
    444 
    445   return dev->buf_mgr_->UnlockBuffer(hnd);
    446 }
    447 
    448 gralloc1_error_t GrallocImpl::Gralloc1Perform(gralloc1_device_t *device, int operation, ...) {
    449   va_list args;
    450   va_start(args, operation);
    451   GrallocImpl const *dev = GRALLOC_IMPL(device);
    452   gralloc1_error_t err = dev->buf_mgr_->Perform(operation, args);
    453   va_end(args);
    454 
    455   return err;
    456 }
    457 
    458 }  // namespace gralloc1
    459