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