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