Home | History | Annotate | Download | only in vts
      1 /*
      2  * Copyright (C) 2018 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 <C2Buffer.h>
     18 #include "allocator.h"
     19 
     20 union Params {
     21   struct {
     22     uint32_t capacity;
     23     C2MemoryUsage usage;
     24   } data;
     25   uint8_t array[0];
     26   Params() : data{0, {0, 0}} {}
     27   Params(uint32_t size)
     28       : data{size, {C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE}} {}
     29 };
     30 
     31 struct AllocationDtor {
     32   AllocationDtor(const std::shared_ptr<C2LinearAllocation> &alloc)
     33       : mAlloc(alloc) {}
     34 
     35   void operator()(BufferPoolAllocation *poolAlloc) { delete poolAlloc; }
     36 
     37   const std::shared_ptr<C2LinearAllocation> mAlloc;
     38 };
     39 
     40 ResultStatus VtsBufferPoolAllocator::allocate(
     41     const std::vector<uint8_t> &params,
     42     std::shared_ptr<BufferPoolAllocation> *alloc,
     43     size_t *allocSize) {
     44   Params ionParams;
     45   memcpy(&ionParams, params.data(), std::min(sizeof(Params), params.size()));
     46 
     47   std::shared_ptr<C2LinearAllocation> linearAlloc;
     48   c2_status_t status = mAllocator->newLinearAllocation(
     49       ionParams.data.capacity, ionParams.data.usage, &linearAlloc);
     50   if (status == C2_OK && linearAlloc) {
     51     BufferPoolAllocation *ptr = new BufferPoolAllocation(linearAlloc->handle());
     52     if (ptr) {
     53       *alloc = std::shared_ptr<BufferPoolAllocation>(
     54           ptr, AllocationDtor(linearAlloc));
     55       if (*alloc) {
     56         *allocSize = ionParams.data.capacity;
     57         return ResultStatus::OK;
     58       }
     59       delete ptr;
     60       return ResultStatus::NO_MEMORY;
     61     }
     62   }
     63   return ResultStatus::CRITICAL_ERROR;
     64 }
     65 
     66 bool VtsBufferPoolAllocator::compatible(const std::vector<uint8_t> &newParams,
     67                                         const std::vector<uint8_t> &oldParams) {
     68   size_t newSize = newParams.size();
     69   size_t oldSize = oldParams.size();
     70   if (newSize == oldSize) {
     71     for (size_t i = 0; i < newSize; ++i) {
     72       if (newParams[i] != oldParams[i]) {
     73         return false;
     74       }
     75     }
     76     return true;
     77   }
     78   return false;
     79 }
     80 
     81 void getVtsAllocatorParams(std::vector<uint8_t> *params) {
     82   constexpr static int kAllocationSize = 1024 * 10;
     83   Params ionParams(kAllocationSize);
     84 
     85   params->assign(ionParams.array, ionParams.array + sizeof(ionParams));
     86 }
     87