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