1/* 2 * Copyright (C) 2017 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#pragma once 18 19#include <string> 20#include <unordered_set> 21#include <vector> 22 23#include <android/hardware/graphics/allocator/2.0/IAllocator.h> 24#include <android/hardware/graphics/mapper/2.0/IMapper.h> 25#include <utils/StrongPointer.h> 26 27namespace android { 28namespace hardware { 29namespace graphics { 30namespace mapper { 31namespace V2_0 { 32namespace vts { 33 34using android::hardware::graphics::allocator::V2_0::IAllocator; 35 36// A wrapper to IAllocator and IMapper. 37class Gralloc { 38 public: 39 Gralloc(const std::string& allocatorServiceName = "default", 40 const std::string& mapperServiceName = "default"); 41 ~Gralloc(); 42 43 // IAllocator methods 44 45 sp<IAllocator> getAllocator() const; 46 47 std::string dumpDebugInfo(); 48 49 // When import is false, this simply calls IAllocator::allocate. When import 50 // is true, the returned buffers are also imported into the mapper. 51 // 52 // Either case, the returned buffers must be freed with freeBuffer. 53 std::vector<const native_handle_t*> allocate(const BufferDescriptor& descriptor, uint32_t count, 54 bool import = true, uint32_t* outStride = nullptr); 55 const native_handle_t* allocate(const IMapper::BufferDescriptorInfo& descriptorInfo, 56 bool import = true, uint32_t* outStride = nullptr); 57 58 // IMapper methods 59 60 sp<IMapper> getMapper() const; 61 62 BufferDescriptor createDescriptor(const IMapper::BufferDescriptorInfo& descriptorInfo); 63 64 const native_handle_t* importBuffer(const hidl_handle& rawHandle); 65 void freeBuffer(const native_handle_t* bufferHandle); 66 67 // We use fd instead of hidl_handle in these functions to pass fences 68 // in and out of the mapper. The ownership of the fd is always transferred 69 // with each of these functions. 70 void* lock(const native_handle_t* bufferHandle, uint64_t cpuUsage, 71 const IMapper::Rect& accessRegion, int acquireFence); 72 YCbCrLayout lockYCbCr(const native_handle_t* bufferHandle, uint64_t cpuUsage, 73 const IMapper::Rect& accessRegion, int acquireFence); 74 int unlock(const native_handle_t* bufferHandle); 75 76 private: 77 void init(const std::string& allocatorServiceName, const std::string& mapperServiceName); 78 const native_handle_t* cloneBuffer(const hidl_handle& rawHandle); 79 80 sp<IAllocator> mAllocator; 81 sp<IMapper> mMapper; 82 83 // Keep track of all cloned and imported handles. When a test fails with 84 // ASSERT_*, the destructor will free the handles for the test. 85 std::unordered_set<const native_handle_t*> mClonedBuffers; 86 std::unordered_set<const native_handle_t*> mImportedBuffers; 87}; 88 89} // namespace vts 90} // namespace V2_0 91} // namespace mapper 92} // namespace graphics 93} // namespace hardware 94} // namespace android 95