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 #pragma once
18 
19 #include <string>
20 #include <unordered_set>
21 #include <vector>
22 
23 #include <android/hardware/graphics/allocator/2.0/IAllocator.h>
24 #include <android/hardware/graphics/mapper/2.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 V2_0 {
33 namespace vts {
34 
35 using android::hardware::graphics::allocator::V2_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");
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, uint32_t count,
55                                                  bool import = true, 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);
73     YCbCrLayout lockYCbCr(const native_handle_t* bufferHandle, uint64_t cpuUsage,
74                           const IMapper::Rect& accessRegion, int acquireFence);
75     int unlock(const native_handle_t* bufferHandle);
76 
77    private:
78     void init(const std::string& allocatorServiceName, const std::string& mapperServiceName);
79     const native_handle_t* cloneBuffer(const hidl_handle& rawHandle);
80 
81     sp<IAllocator> mAllocator;
82     sp<IMapper> mMapper;
83 
84     // Keep track of all cloned and imported handles.  When a test fails with
85     // ASSERT_*, the destructor will free the handles for the test.
86     std::unordered_set<const native_handle_t*> mClonedBuffers;
87     std::unordered_set<const native_handle_t*> mImportedBuffers;
88 };
89 
90 }  // namespace vts
91 }  // namespace V2_0
92 }  // namespace mapper
93 }  // namespace graphics
94 }  // namespace hardware
95 }  // namespace android
96