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 VTS_HAL_GRAPHICS_MAPPER_UTILS
18 #define VTS_HAL_GRAPHICS_MAPPER_UTILS
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 #include <utils/StrongPointer.h>
25 
26 namespace android {
27 namespace hardware {
28 namespace graphics {
29 namespace mapper {
30 namespace V2_0 {
31 namespace tests {
32 
33 using android::hardware::graphics::allocator::V2_0::IAllocator;
34 
35 // A wrapper to IAllocator and IMapper.
36 class Gralloc {
37    public:
38     Gralloc();
39     ~Gralloc();
40 
41     // IAllocator methods
42 
43     sp<IAllocator> getAllocator() const;
44 
45     std::string dumpDebugInfo();
46 
47     // When import is false, this simply calls IAllocator::allocate. When import
48     // is true, the returned buffers are also imported into the mapper.
49     //
50     // Either case, the returned buffers must be freed with freeBuffer.
51     std::vector<const native_handle_t*> allocate(
52         const BufferDescriptor& descriptor, uint32_t count, bool import = true,
53         uint32_t* outStride = nullptr);
54     const native_handle_t* allocate(
55         const IMapper::BufferDescriptorInfo& descriptorInfo, bool import = true,
56         uint32_t* outStride = nullptr);
57 
58     // IMapper methods
59 
60     sp<IMapper> getMapper() const;
61 
62     BufferDescriptor createDescriptor(
63         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);
73     YCbCrLayout lockYCbCr(const native_handle_t* bufferHandle,
74                           uint64_t cpuUsage, const IMapper::Rect& accessRegion,
75                           int acquireFence);
76     int unlock(const native_handle_t* bufferHandle);
77 
78    private:
79     void init();
80     const native_handle_t* cloneBuffer(const hidl_handle& rawHandle);
81 
82     sp<IAllocator> mAllocator;
83     sp<IMapper> mMapper;
84 
85     // Keep track of all cloned and imported handles.  When a test fails with
86     // ASSERT_*, the destructor will free the handles for the test.
87     std::unordered_set<const native_handle_t*> mClonedBuffers;
88     std::unordered_set<const native_handle_t*> mImportedBuffers;
89 };
90 
91 }  // namespace tests
92 }  // namespace V2_0
93 }  // namespace mapper
94 }  // namespace graphics
95 }  // namespace hardware
96 }  // namespace android
97 
98 #endif  // VTS_HAL_GRAPHICS_MAPPER_UTILS
99