Home | History | Annotate | Download | only in libgralloc1
      1 /*
      2  * Copyright (c) 2011-2016 The Linux Foundation. All rights reserved.
      3  * Not a Contribution
      4  *
      5  * Copyright (C) 2010 The Android Open Source Project
      6  *
      7  * Licensed under the Apache License, Version 2.0 (the "License");
      8  * you may not use this file except in compliance with the License.
      9  * You may obtain a copy of the License at
     10  *
     11  *      http://www.apache.org/licenses/LICENSE-2.0
     12  *
     13  * Unless required by applicable law or agreed to in writing, software
     14  * distributed under the License is distributed on an "AS IS" BASIS,
     15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     16  * See the License for the specific language governing permissions and
     17  * limitations under the License.
     18  */
     19 
     20 #include <utility>
     21 
     22 #include "qd_utils.h"
     23 #include "gr_priv_handle.h"
     24 #include "gr_buf_descriptor.h"
     25 #include "gr_utils.h"
     26 #include "gr_buf_mgr.h"
     27 #include "qdMetaData.h"
     28 
     29 namespace gralloc1 {
     30 
     31 BufferManager::BufferManager() {
     32   char property[PROPERTY_VALUE_MAX];
     33 
     34   // Map framebuffer memory
     35   if ((property_get("debug.gralloc.map_fb_memory", property, NULL) > 0) &&
     36       (!strncmp(property, "1", PROPERTY_VALUE_MAX) ||
     37        (!strncasecmp(property, "true", PROPERTY_VALUE_MAX)))) {
     38     map_fb_mem_ = true;
     39   }
     40 
     41   // Enable UBWC for framebuffer
     42   if ((property_get("debug.gralloc.enable_fb_ubwc", property, NULL) > 0) &&
     43       (!strncmp(property, "1", PROPERTY_VALUE_MAX) ||
     44        (!strncasecmp(property, "true", PROPERTY_VALUE_MAX)))) {
     45     ubwc_for_fb_ = true;
     46   }
     47 
     48   handles_map_.clear();
     49 }
     50 
     51 BufferManager::~BufferManager() {
     52   if (allocator_) {
     53     delete allocator_;
     54   }
     55 }
     56 
     57 bool BufferManager::Init() {
     58   allocator_ = new Allocator();
     59 
     60   return allocator_->Init();
     61 }
     62 
     63 gralloc1_error_t BufferManager::AllocateBuffers(uint32_t num_descriptors,
     64                                                 const BufferDescriptor *descriptors,
     65                                                 buffer_handle_t *out_buffers) {
     66   bool shared = true;
     67   gralloc1_error_t status = GRALLOC1_ERROR_NONE;
     68 
     69   // since GRALLOC1_CAPABILITY_TEST_ALLOCATE capability is supported
     70   // client can ask to test the allocation by passing NULL out_buffers
     71   bool test_allocate = !out_buffers;
     72 
     73   // Check if input descriptors can be supported AND
     74   // Find out if a single buffer can be shared for all the given input descriptors
     75   uint32_t i = 0;
     76   int max_buf_index = -1;
     77   shared = allocator_->CheckForBufferSharing(num_descriptors, descriptors, &max_buf_index);
     78 
     79   if (test_allocate) {
     80     status = shared ? GRALLOC1_ERROR_NOT_SHARED : status;
     81     return status;
     82   }
     83 
     84   if (shared && (max_buf_index >= 0)) {
     85     // Allocate one and duplicate/copy the handles for each descriptor
     86     if (AllocateBuffer(descriptors[max_buf_index], &out_buffers[max_buf_index])) {
     87       return GRALLOC1_ERROR_NO_RESOURCES;
     88     }
     89 
     90     for (i = 0; i < num_descriptors; i++) {
     91       // Create new handle for a given descriptor.
     92       // Current assumption is even MetaData memory would be same
     93       // Need to revisit if there is a need for own metadata memory
     94       if (i != UINT(max_buf_index)) {
     95         CreateSharedHandle(out_buffers[max_buf_index], descriptors[i], &out_buffers[i]);
     96 
     97         // since we just created handle out of existing handle add it to map
     98         locker_.lock();
     99         handles_map_.insert(std::pair<private_handle_t const *, int>(
    100             reinterpret_cast<private_handle_t const *>(out_buffers[i]), 1));
    101         locker_.unlock();
    102       }
    103     }
    104   } else {
    105     // Buffer sharing is not feasible.
    106     // Allocate seperate buffer for each descriptor
    107     for (i = 0; i < num_descriptors; i++) {
    108       if (AllocateBuffer(descriptors[i], &out_buffers[i])) {
    109         return GRALLOC1_ERROR_NO_RESOURCES;
    110       }
    111     }
    112   }
    113 
    114   // Allocation is successful. If backstore is not shared inform the client.
    115   if (!shared) {
    116     return GRALLOC1_ERROR_NOT_SHARED;
    117   }
    118 
    119   return status;
    120 }
    121 
    122 void BufferManager::CreateSharedHandle(buffer_handle_t inbuffer, const BufferDescriptor &descriptor,
    123                                        buffer_handle_t *outbuffer) {
    124   private_handle_t const *input = reinterpret_cast<private_handle_t const *>(inbuffer);
    125 
    126   // Get Buffer attributes or dimension
    127   unsigned int alignedw = 0, alignedh = 0;
    128   allocator_->GetAlignedWidthAndHeight(descriptor, &alignedw, &alignedh);
    129 
    130   // create new handle from input reference handle and given descriptor
    131   int flags = GetHandleFlags(descriptor.GetFormat(), descriptor.GetProducerUsage(),
    132                              descriptor.GetConsumerUsage());
    133   int buffer_type = GetBufferType(descriptor.GetFormat());
    134 
    135   // Duplicate the fds
    136   private_handle_t *out_hnd = new private_handle_t(
    137       dup(input->fd), input->size, flags, buffer_type, descriptor.GetFormat(), INT(alignedw),
    138       INT(alignedh), dup(input->fd_metadata), input->offset_metadata, input->base_metadata,
    139       descriptor.GetWidth(), descriptor.GetHeight(), descriptor.GetProducerUsage(),
    140       descriptor.GetConsumerUsage());
    141 
    142   *outbuffer = out_hnd;
    143 }
    144 
    145 gralloc1_error_t BufferManager::FreeBuffer(private_handle_t const *hnd) {
    146   if (allocator_->FreeBuffer(reinterpret_cast<void *>(hnd->base), hnd->size, hnd->offset,
    147                              hnd->fd) != 0) {
    148     return GRALLOC1_ERROR_BAD_HANDLE;
    149   }
    150 
    151   unsigned int meta_size = ALIGN((unsigned int)sizeof(MetaData_t), PAGE_SIZE);
    152   if (allocator_->FreeBuffer(reinterpret_cast<void *>(hnd->base_metadata), meta_size,
    153                              hnd->offset_metadata, hnd->fd_metadata) != 0) {
    154     return GRALLOC1_ERROR_BAD_HANDLE;
    155   }
    156 
    157   // delete handle also
    158   private_handle_t *handle = const_cast<private_handle_t *>(hnd);
    159   delete handle;
    160 
    161   return GRALLOC1_ERROR_NONE;
    162 }
    163 
    164 gralloc1_error_t BufferManager::MapBuffer(private_handle_t const *handle) {
    165   private_handle_t *hnd = const_cast<private_handle_t *>(handle);
    166 
    167   hnd->base = 0;
    168   hnd->base_metadata = 0;
    169   if (allocator_->MapBuffer(reinterpret_cast<void **>(&hnd->base), hnd->size, hnd->offset,
    170                             hnd->fd) != 0) {
    171     return GRALLOC1_ERROR_BAD_HANDLE;
    172   }
    173 
    174   unsigned int size = ALIGN((unsigned int)sizeof(MetaData_t), PAGE_SIZE);
    175   if (allocator_->MapBuffer(reinterpret_cast<void **>(&hnd->base_metadata), size,
    176                             hnd->offset_metadata, hnd->fd_metadata) != 0) {
    177     return GRALLOC1_ERROR_BAD_HANDLE;
    178   }
    179 
    180   return GRALLOC1_ERROR_NONE;
    181 }
    182 
    183 gralloc1_error_t BufferManager::RetainBuffer(private_handle_t const *hnd) {
    184   locker_.lock();
    185 
    186   // find if this handle is already in map
    187   auto it = handles_map_.find(hnd);
    188   if (it != handles_map_.end()) {
    189     // It's already in map, Just increment refcnt
    190     // No need to mmap the memory.
    191     it->second = it->second + 1;
    192   } else {
    193     // not present in the map. mmap and then add entry to map
    194     if (MapBuffer(hnd) == GRALLOC1_ERROR_NONE) {
    195       handles_map_.insert(std::pair<private_handle_t const *, int>(hnd, 1));
    196     }
    197   }
    198 
    199   locker_.unlock();
    200   return GRALLOC1_ERROR_NONE;
    201 }
    202 
    203 gralloc1_error_t BufferManager::ReleaseBuffer(private_handle_t const *hnd) {
    204   locker_.lock();
    205 
    206   // find if this handle is already in map
    207   auto it = handles_map_.find(hnd);
    208   if (it == handles_map_.end()) {
    209     // Corrupt handle or map.
    210     locker_.unlock();
    211     return GRALLOC1_ERROR_BAD_HANDLE;
    212   } else {
    213     it->second = it->second - 1;
    214   }
    215 
    216   if (!it->second) {
    217     handles_map_.erase(it);
    218     FreeBuffer(hnd);
    219   }
    220 
    221   locker_.unlock();
    222   return GRALLOC1_ERROR_NONE;
    223 }
    224 
    225 gralloc1_error_t BufferManager::LockBuffer(const private_handle_t *hnd,
    226                                            gralloc1_producer_usage_t prod_usage,
    227                                            gralloc1_consumer_usage_t cons_usage) {
    228   gralloc1_error_t err = GRALLOC1_ERROR_NONE;
    229 
    230   // If buffer is not meant for CPU return err
    231   if (!CpuCanAccess(prod_usage, cons_usage)) {
    232     return GRALLOC1_ERROR_BAD_VALUE;
    233   }
    234 
    235   if (hnd->base == 0) {
    236     // we need to map for real
    237     locker_.lock();
    238     err = MapBuffer(hnd);
    239     locker_.unlock();
    240   }
    241 
    242   // Invalidate if CPU reads in software and there are non-CPU
    243   // writers. No need to do this for the metadata buffer as it is
    244   // only read/written in software.
    245   if (!err && (hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION) &&
    246       (hnd->flags & private_handle_t::PRIV_FLAGS_CACHED)) {
    247     if (allocator_->CleanBuffer(reinterpret_cast<void *>(hnd->base), hnd->size, hnd->offset,
    248                                 hnd->fd, CACHE_INVALIDATE)) {
    249       return GRALLOC1_ERROR_BAD_HANDLE;
    250     }
    251   }
    252 
    253   // Mark the buffer to be flushed after CPU write.
    254   if (!err && CpuCanWrite(prod_usage)) {
    255     private_handle_t *handle = const_cast<private_handle_t *>(hnd);
    256     handle->flags |= private_handle_t::PRIV_FLAGS_NEEDS_FLUSH;
    257   }
    258 
    259   return err;
    260 }
    261 
    262 gralloc1_error_t BufferManager::UnlockBuffer(const private_handle_t *handle) {
    263   gralloc1_error_t status = GRALLOC1_ERROR_NONE;
    264 
    265   locker_.lock();
    266   private_handle_t *hnd = const_cast<private_handle_t *>(handle);
    267 
    268   if (hnd->flags & private_handle_t::PRIV_FLAGS_NEEDS_FLUSH) {
    269     if (allocator_->CleanBuffer(reinterpret_cast<void *>(hnd->base), hnd->size, hnd->offset,
    270                                 hnd->fd, CACHE_CLEAN) != 0) {
    271       status = GRALLOC1_ERROR_BAD_HANDLE;
    272     }
    273     hnd->flags &= ~private_handle_t::PRIV_FLAGS_NEEDS_FLUSH;
    274   }
    275 
    276   locker_.unlock();
    277   return status;
    278 }
    279 
    280 int BufferManager::GetDataAlignment(int format, gralloc1_producer_usage_t prod_usage,
    281                                     gralloc1_consumer_usage_t cons_usage) {
    282   int align = getpagesize();
    283   if (format == HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED) {
    284     align = 8192;
    285   }
    286 
    287   if (prod_usage & GRALLOC1_PRODUCER_USAGE_PROTECTED) {
    288     if (cons_usage & GRALLOC1_CONSUMER_USAGE_PRIVATE_SECURE_DISPLAY) {
    289       // The alignment here reflects qsee mmu V7L/V8L requirement
    290       align = SZ_2M;
    291     } else {
    292       align = SECURE_ALIGN;
    293     }
    294   }
    295 
    296   return align;
    297 }
    298 
    299 int BufferManager::GetHandleFlags(int format, gralloc1_producer_usage_t prod_usage,
    300                                   gralloc1_consumer_usage_t cons_usage) {
    301   int flags = 0;
    302   if (cons_usage & GRALLOC1_CONSUMER_USAGE_PRIVATE_EXTERNAL_ONLY) {
    303     flags |= private_handle_t::PRIV_FLAGS_EXTERNAL_ONLY;
    304   }
    305 
    306   if (cons_usage & GRALLOC1_CONSUMER_USAGE_PRIVATE_INTERNAL_ONLY) {
    307     flags |= private_handle_t::PRIV_FLAGS_INTERNAL_ONLY;
    308   }
    309 
    310   if (cons_usage & GRALLOC1_CONSUMER_USAGE_VIDEO_ENCODER) {
    311     flags |= private_handle_t::PRIV_FLAGS_VIDEO_ENCODER;
    312   }
    313 
    314   if (prod_usage & GRALLOC1_PRODUCER_USAGE_CAMERA) {
    315     flags |= private_handle_t::PRIV_FLAGS_CAMERA_WRITE;
    316   }
    317 
    318   if (prod_usage & GRALLOC1_CONSUMER_USAGE_CAMERA) {
    319     flags |= private_handle_t::PRIV_FLAGS_CAMERA_READ;
    320   }
    321 
    322   if (cons_usage & GRALLOC1_CONSUMER_USAGE_HWCOMPOSER) {
    323     flags |= private_handle_t::PRIV_FLAGS_HW_COMPOSER;
    324   }
    325 
    326   if (prod_usage & GRALLOC1_CONSUMER_USAGE_GPU_TEXTURE) {
    327     flags |= private_handle_t::PRIV_FLAGS_HW_TEXTURE;
    328   }
    329 
    330   if (prod_usage & GRALLOC1_CONSUMER_USAGE_PRIVATE_SECURE_DISPLAY) {
    331     flags |= private_handle_t::PRIV_FLAGS_SECURE_DISPLAY;
    332   }
    333 
    334   if (allocator_->IsMacroTileEnabled(format, prod_usage, cons_usage)) {
    335     flags |= private_handle_t::PRIV_FLAGS_TILE_RENDERED;
    336   }
    337 
    338   if (allocator_->IsUBwcEnabled(format, prod_usage, cons_usage)) {
    339     flags |= private_handle_t::PRIV_FLAGS_UBWC_ALIGNED;
    340   }
    341 
    342   if (prod_usage & (GRALLOC1_PRODUCER_USAGE_CPU_READ | GRALLOC1_PRODUCER_USAGE_CPU_WRITE)) {
    343     flags |= private_handle_t::PRIV_FLAGS_CPU_RENDERED;
    344   }
    345 
    346   // TODO(user): is this correct???
    347   if ((cons_usage &
    348        (GRALLOC1_CONSUMER_USAGE_VIDEO_ENCODER | GRALLOC1_CONSUMER_USAGE_CLIENT_TARGET)) ||
    349       (prod_usage & (GRALLOC1_PRODUCER_USAGE_CAMERA | GRALLOC1_PRODUCER_USAGE_GPU_RENDER_TARGET))) {
    350     flags |= private_handle_t::PRIV_FLAGS_NON_CPU_WRITER;
    351   }
    352 
    353   if (cons_usage & GRALLOC1_CONSUMER_USAGE_HWCOMPOSER) {
    354     flags |= private_handle_t::PRIV_FLAGS_DISP_CONSUMER;
    355   }
    356 
    357   if (!allocator_->UseUncached(prod_usage)) {
    358     flags |= private_handle_t::PRIV_FLAGS_CACHED;
    359   }
    360 
    361   return flags;
    362 }
    363 
    364 int BufferManager::AllocateBuffer(unsigned int size, int aligned_w, int aligned_h, int real_w,
    365                                   int real_h, int format, int bufferType,
    366                                   gralloc1_producer_usage_t prod_usage,
    367                                   gralloc1_consumer_usage_t cons_usage, buffer_handle_t *handle) {
    368   int err = 0;
    369   int flags = 0;
    370   size = ALIGN(size, PAGE_SIZE);
    371   AllocData data;
    372   data.align = (unsigned int)GetDataAlignment(format, prod_usage, cons_usage);
    373   size = ALIGN(size, data.align);
    374   data.size = size;
    375   data.handle = (uintptr_t)handle;
    376 
    377   // Allocate memory
    378   data.uncached = allocator_->UseUncached(prod_usage);
    379   err = allocator_->AllocateMem(&data, prod_usage, cons_usage);
    380   if (err) {
    381     ALOGE("gralloc failed to allocate err=%s", strerror(-err));
    382     *handle = 0;
    383     return err;
    384   }
    385 
    386   // allocate memory for MetaData
    387   AllocData e_data;
    388   e_data.size = ALIGN((unsigned int)sizeof(MetaData_t), PAGE_SIZE);
    389   e_data.handle = data.handle;
    390   e_data.align = (unsigned int)getpagesize();
    391 
    392   ColorSpace_t colorSpace = ITU_R_601;
    393   if (prod_usage & GRALLOC1_PRODUCER_USAGE_CAMERA) {
    394     colorSpace = ITU_R_601_FR;
    395   }
    396 
    397   err =
    398       allocator_->AllocateMem(&e_data, GRALLOC1_PRODUCER_USAGE_NONE, GRALLOC1_CONSUMER_USAGE_NONE);
    399   ALOGE_IF(err, "gralloc failed for e_daata error=%s", strerror(-err));
    400 
    401   flags = GetHandleFlags(format, prod_usage, cons_usage);
    402   flags |= data.alloc_type;
    403 
    404   // Create handle
    405   uint64_t eBaseAddr = (uint64_t)(e_data.base) + e_data.offset;
    406   private_handle_t *hnd = new private_handle_t(data.fd, size, flags, bufferType, format, aligned_w,
    407                                                aligned_h, e_data.fd, e_data.offset, eBaseAddr,
    408                                                real_w, real_h, prod_usage, cons_usage);
    409 
    410   hnd->offset = data.offset;
    411   hnd->base = (uint64_t)(data.base) + data.offset;
    412   hnd->gpuaddr = 0;
    413 
    414   setMetaData(hnd, UPDATE_COLOR_SPACE, reinterpret_cast<void *>(&colorSpace));
    415 
    416   *handle = hnd;
    417 
    418   // we have just allocated the buffer & mmapped. Add to map
    419   locker_.lock();
    420   handles_map_.insert(std::pair<private_handle_t const *, int>(hnd, 1));
    421   locker_.unlock();
    422 
    423   return err;
    424 }
    425 
    426 int BufferManager::GetBufferType(int inputFormat) {
    427   int buffer_type = BUFFER_TYPE_VIDEO;
    428   if (IsUncompressedRGBFormat(inputFormat)) {
    429     // RGB formats
    430     buffer_type = BUFFER_TYPE_UI;
    431   }
    432 
    433   return buffer_type;
    434 }
    435 
    436 int BufferManager::AllocateBuffer(const BufferDescriptor &descriptor, buffer_handle_t *handle,
    437                                   unsigned int bufferSize) {
    438   if (!handle)
    439     return -EINVAL;
    440 
    441   int format = descriptor.GetFormat();
    442   gralloc1_producer_usage_t prod_usage = descriptor.GetProducerUsage();
    443   gralloc1_consumer_usage_t cons_usage = descriptor.GetConsumerUsage();
    444 
    445   // Get implementation defined format
    446   int gralloc_format = allocator_->GetImplDefinedFormat(prod_usage, cons_usage, format);
    447 
    448   bool use_fb_mem = false;
    449   if ((cons_usage & GRALLOC1_CONSUMER_USAGE_CLIENT_TARGET) && map_fb_mem_) {
    450     use_fb_mem = true;
    451   }
    452 
    453   if ((cons_usage & GRALLOC1_CONSUMER_USAGE_CLIENT_TARGET) && ubwc_for_fb_) {
    454     prod_usage =
    455         (gralloc1_producer_usage_t)(prod_usage | GRALLOC1_PRODUCER_USAGE_PRIVATE_ALLOC_UBWC);
    456   }
    457 
    458   unsigned int size;
    459   unsigned int alignedw, alignedh;
    460   int buffer_type = GetBufferType(gralloc_format);
    461   allocator_->GetBufferSizeAndDimensions(descriptor, &size, &alignedw, &alignedh);
    462 
    463   size = (bufferSize >= size) ? bufferSize : size;
    464 
    465   int err = 0;
    466   if (use_fb_mem) {
    467     // TODO(user): TBD Framebuffer specific implementation in a seperate file/class
    468   } else {
    469     err = AllocateBuffer(size, INT(alignedw), INT(alignedh), descriptor.GetWidth(),
    470                          descriptor.GetHeight(), format, buffer_type, descriptor.GetProducerUsage(),
    471                          descriptor.GetConsumerUsage(), handle);
    472   }
    473 
    474   if (err < 0) {
    475     return err;
    476   }
    477 
    478   return 0;
    479 }
    480 
    481 gralloc1_error_t BufferManager::Perform(int operation, va_list args) {
    482   switch (operation) {
    483     case GRALLOC_MODULE_PERFORM_CREATE_HANDLE_FROM_BUFFER: {
    484       int fd = va_arg(args, int);
    485       unsigned int size = va_arg(args, unsigned int);
    486       unsigned int offset = va_arg(args, unsigned int);
    487       void *base = va_arg(args, void *);
    488       int width = va_arg(args, int);
    489       int height = va_arg(args, int);
    490       int format = va_arg(args, int);
    491 
    492       native_handle_t **handle = va_arg(args, native_handle_t **);
    493       private_handle_t *hnd = reinterpret_cast<private_handle_t *>(
    494           native_handle_create(private_handle_t::kNumFds, private_handle_t::NumInts()));
    495       if (hnd) {
    496         hnd->magic = private_handle_t::kMagic;
    497         hnd->fd = fd;
    498         hnd->flags = private_handle_t::PRIV_FLAGS_USES_ION;
    499         hnd->size = size;
    500         hnd->offset = offset;
    501         hnd->base = uint64_t(base) + offset;
    502         hnd->gpuaddr = 0;
    503         hnd->width = width;
    504         hnd->height = height;
    505         hnd->format = format;
    506         *handle = reinterpret_cast<native_handle_t *>(hnd);
    507       }
    508     } break;
    509 
    510     case GRALLOC_MODULE_PERFORM_GET_STRIDE: {
    511       int width = va_arg(args, int);
    512       int format = va_arg(args, int);
    513       int *stride = va_arg(args, int *);
    514       unsigned int alignedw = 0, alignedh = 0;
    515       BufferDescriptor descriptor(width, width, format);
    516       allocator_->GetAlignedWidthAndHeight(descriptor, &alignedw, &alignedh);
    517       *stride = INT(alignedw);
    518     } break;
    519 
    520     case GRALLOC_MODULE_PERFORM_GET_CUSTOM_STRIDE_FROM_HANDLE: {
    521       private_handle_t *hnd = va_arg(args, private_handle_t *);
    522       int *stride = va_arg(args, int *);
    523       if (private_handle_t::validate(hnd) != 0) {
    524         return GRALLOC1_ERROR_BAD_HANDLE;
    525       }
    526 
    527       MetaData_t *metadata = reinterpret_cast<MetaData_t *>(hnd->base_metadata);
    528       if (metadata && metadata->operation & UPDATE_BUFFER_GEOMETRY) {
    529         *stride = metadata->bufferDim.sliceWidth;
    530       } else {
    531         *stride = hnd->width;
    532       }
    533     } break;
    534 
    535     // TODO(user) : this alone should be sufficient, ask gfx to get rid of above
    536     case GRALLOC_MODULE_PERFORM_GET_CUSTOM_STRIDE_AND_HEIGHT_FROM_HANDLE: {
    537       private_handle_t *hnd = va_arg(args, private_handle_t *);
    538       int *stride = va_arg(args, int *);
    539       int *height = va_arg(args, int *);
    540       if (private_handle_t::validate(hnd) != 0) {
    541         return GRALLOC1_ERROR_BAD_HANDLE;
    542       }
    543 
    544       MetaData_t *metadata = reinterpret_cast<MetaData_t *>(hnd->base_metadata);
    545       if (metadata && metadata->operation & UPDATE_BUFFER_GEOMETRY) {
    546         *stride = metadata->bufferDim.sliceWidth;
    547         *height = metadata->bufferDim.sliceHeight;
    548       } else {
    549         *stride = hnd->width;
    550         *height = hnd->height;
    551       }
    552     } break;
    553 
    554     case GRALLOC_MODULE_PERFORM_GET_ATTRIBUTES: {
    555       // TODO(user): Usage is split now. take care of it from Gfx client.
    556       // see if we can directly expect descriptor from gfx client.
    557       int width = va_arg(args, int);
    558       int height = va_arg(args, int);
    559       int format = va_arg(args, int);
    560       uint64_t producer_usage = va_arg(args, uint64_t);
    561       uint64_t consumer_usage = va_arg(args, uint64_t);
    562       gralloc1_producer_usage_t prod_usage = static_cast<gralloc1_producer_usage_t>(producer_usage);
    563       gralloc1_consumer_usage_t cons_usage = static_cast<gralloc1_consumer_usage_t>(consumer_usage);
    564 
    565       int *aligned_width = va_arg(args, int *);
    566       int *aligned_height = va_arg(args, int *);
    567       int *tile_enabled = va_arg(args, int *);
    568       unsigned int alignedw, alignedh;
    569       BufferDescriptor descriptor(width, height, format, prod_usage, cons_usage);
    570       *tile_enabled = allocator_->IsUBwcEnabled(format, prod_usage, cons_usage) ||
    571                       allocator_->IsMacroTileEnabled(format, prod_usage, cons_usage);
    572 
    573       allocator_->GetAlignedWidthAndHeight(descriptor, &alignedw, &alignedh);
    574       *aligned_width = INT(alignedw);
    575       *aligned_height = INT(alignedh);
    576     } break;
    577 
    578     case GRALLOC_MODULE_PERFORM_GET_COLOR_SPACE_FROM_HANDLE: {
    579       private_handle_t *hnd = va_arg(args, private_handle_t *);
    580       int *color_space = va_arg(args, int *);
    581       if (private_handle_t::validate(hnd) != 0) {
    582         return GRALLOC1_ERROR_BAD_HANDLE;
    583       }
    584       MetaData_t *metadata = reinterpret_cast<MetaData_t *>(hnd->base_metadata);
    585       if (metadata && metadata->operation & UPDATE_COLOR_SPACE) {
    586         *color_space = metadata->colorSpace;
    587       }
    588     } break;
    589     case GRALLOC_MODULE_PERFORM_GET_YUV_PLANE_INFO: {
    590       private_handle_t *hnd = va_arg(args, private_handle_t *);
    591       android_ycbcr *ycbcr = va_arg(args, struct android_ycbcr *);
    592       if (private_handle_t::validate(hnd) != 0) {
    593         return GRALLOC1_ERROR_BAD_HANDLE;
    594       }
    595       if (allocator_->GetYUVPlaneInfo(hnd, ycbcr)) {
    596         return GRALLOC1_ERROR_UNDEFINED;
    597       }
    598     } break;
    599 
    600     case GRALLOC_MODULE_PERFORM_GET_MAP_SECURE_BUFFER_INFO: {
    601       private_handle_t *hnd = va_arg(args, private_handle_t *);
    602       int *map_secure_buffer = va_arg(args, int *);
    603       if (private_handle_t::validate(hnd) != 0) {
    604         return GRALLOC1_ERROR_BAD_HANDLE;
    605       }
    606       MetaData_t *metadata = reinterpret_cast<MetaData_t *>(hnd->base_metadata);
    607       if (metadata && metadata->operation & MAP_SECURE_BUFFER) {
    608         *map_secure_buffer = metadata->mapSecureBuffer;
    609       } else {
    610         *map_secure_buffer = 0;
    611       }
    612     } break;
    613 
    614     case GRALLOC_MODULE_PERFORM_GET_UBWC_FLAG: {
    615       private_handle_t *hnd = va_arg(args, private_handle_t *);
    616       int *flag = va_arg(args, int *);
    617       if (private_handle_t::validate(hnd) != 0) {
    618         return GRALLOC1_ERROR_BAD_HANDLE;
    619       }
    620       *flag = hnd->flags &private_handle_t::PRIV_FLAGS_UBWC_ALIGNED;
    621     } break;
    622 
    623     case GRALLOC_MODULE_PERFORM_GET_RGB_DATA_ADDRESS: {
    624       private_handle_t *hnd = va_arg(args, private_handle_t *);
    625       void **rgb_data = va_arg(args, void **);
    626       if (private_handle_t::validate(hnd) != 0) {
    627         return GRALLOC1_ERROR_BAD_HANDLE;
    628       }
    629       if (allocator_->GetRgbDataAddress(hnd, rgb_data)) {
    630         return GRALLOC1_ERROR_UNDEFINED;
    631       }
    632     } break;
    633 
    634     default:
    635       break;
    636   }
    637 
    638   return GRALLOC1_ERROR_NONE;
    639 }
    640 
    641 }  //  namespace gralloc1
    642