Peng Xu | f3d3f9d | 2017-08-21 18:35:28 -0700 | [diff] [blame] | 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( |
| 46 | const mapper2::BufferDescriptor& descriptor, uint32_t count, bool import = true, |
| 47 | uint32_t* outStride = nullptr); |
| 48 | const native_handle_t* allocate( |
| 49 | const mapper2::IMapper::BufferDescriptorInfo& descriptorInfo, bool import = true, |
| 50 | uint32_t* outStride = nullptr); |
| 51 | |
| 52 | mapper2::BufferDescriptor createDescriptor( |
| 53 | const mapper2::IMapper::BufferDescriptorInfo& descriptorInfo); |
| 54 | |
| 55 | const native_handle_t* importBuffer(const hardware::hidl_handle& rawHandle); |
| 56 | void freeBuffer(const native_handle_t* bufferHandle); |
| 57 | |
| 58 | // We use fd instead of hardware::hidl_handle in these functions to pass fences |
| 59 | // in and out of the mapper. The ownership of the fd is always transferred |
| 60 | // with each of these functions. |
| 61 | void* lock(const native_handle_t* bufferHandle, uint64_t cpuUsage, |
| 62 | const mapper2::IMapper::Rect& accessRegion, int acquireFence); |
| 63 | |
| 64 | int unlock(const native_handle_t* bufferHandle); |
| 65 | |
| 66 | private: |
| 67 | void init(); |
| 68 | const native_handle_t* cloneBuffer(const hardware::hidl_handle& rawHandle); |
| 69 | |
| 70 | sp<allocator2::IAllocator> mAllocator; |
| 71 | sp<mapper2::IMapper> mMapper; |
| 72 | |
| 73 | // Keep track of all cloned and imported handles. When a test fails with |
| 74 | // ASSERT_*, the destructor will free the handles for the test. |
| 75 | std::unordered_set<const native_handle_t*> mClonedBuffers; |
| 76 | std::unordered_set<const native_handle_t*> mImportedBuffers; |
| 77 | }; |
| 78 | |
| 79 | } // namespace android |
| 80 | #endif // GRALLO_WRAPPER_H_ |