1 /*
2  * Copyright 2018 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 #include <android-base/properties.h>
18 #include <mapper-vts/3.0/MapperVts.h>
19 #include "gtest/gtest.h"
20 
21 namespace android {
22 namespace hardware {
23 namespace graphics {
24 namespace mapper {
25 namespace V3_0 {
26 namespace vts {
27 
Gralloc(const std::string & allocatorServiceName,const std::string & mapperServiceName,bool errOnFailure)28 Gralloc::Gralloc(const std::string& allocatorServiceName, const std::string& mapperServiceName,
29                  bool errOnFailure) {
30     if (errOnFailure) {
31         init(allocatorServiceName, mapperServiceName);
32     } else {
33         initNoErr(allocatorServiceName, mapperServiceName);
34     }
35 }
36 
init(const std::string & allocatorServiceName,const std::string & mapperServiceName)37 void Gralloc::init(const std::string& allocatorServiceName, const std::string& mapperServiceName) {
38     mAllocator = IAllocator::getService(allocatorServiceName);
39     ASSERT_NE(nullptr, mAllocator.get()) << "failed to get allocator service";
40 
41     mMapper = IMapper::getService(mapperServiceName);
42     ASSERT_NE(nullptr, mMapper.get()) << "failed to get mapper service";
43     ASSERT_FALSE(mMapper->isRemote()) << "mapper is not in passthrough mode";
44 }
45 
initNoErr(const std::string & allocatorServiceName,const std::string & mapperServiceName)46 void Gralloc::initNoErr(const std::string& allocatorServiceName,
47                         const std::string& mapperServiceName) {
48     mAllocator = IAllocator::getService(allocatorServiceName);
49 
50     mMapper = IMapper::getService(mapperServiceName);
51     if (mMapper.get()) {
52         ASSERT_FALSE(mMapper->isRemote()) << "mapper is not in passthrough mode";
53     }
54 }
55 
~Gralloc()56 Gralloc::~Gralloc() {
57     for (auto bufferHandle : mClonedBuffers) {
58         auto buffer = const_cast<native_handle_t*>(bufferHandle);
59         native_handle_close(buffer);
60         native_handle_delete(buffer);
61     }
62     mClonedBuffers.clear();
63 
64     for (auto bufferHandle : mImportedBuffers) {
65         auto buffer = const_cast<native_handle_t*>(bufferHandle);
66         EXPECT_EQ(Error::NONE, mMapper->freeBuffer(buffer)) << "failed to free buffer " << buffer;
67     }
68     mImportedBuffers.clear();
69 }
70 
getAllocator() const71 sp<IAllocator> Gralloc::getAllocator() const {
72     return mAllocator;
73 }
74 
dumpDebugInfo()75 std::string Gralloc::dumpDebugInfo() {
76     std::string debugInfo;
77     mAllocator->dumpDebugInfo([&](const auto& tmpDebugInfo) { debugInfo = tmpDebugInfo.c_str(); });
78 
79     return debugInfo;
80 }
81 
cloneBuffer(const hidl_handle & rawHandle)82 const native_handle_t* Gralloc::cloneBuffer(const hidl_handle& rawHandle) {
83     const native_handle_t* bufferHandle = native_handle_clone(rawHandle.getNativeHandle());
84     EXPECT_NE(nullptr, bufferHandle);
85 
86     if (bufferHandle) {
87         mClonedBuffers.insert(bufferHandle);
88     }
89 
90     return bufferHandle;
91 }
92 
allocate(const BufferDescriptor & descriptor,uint32_t count,bool import,uint32_t * outStride)93 std::vector<const native_handle_t*> Gralloc::allocate(const BufferDescriptor& descriptor,
94                                                       uint32_t count, bool import,
95                                                       uint32_t* outStride) {
96     std::vector<const native_handle_t*> bufferHandles;
97     bufferHandles.reserve(count);
98     mAllocator->allocate(
99             descriptor, count,
100             [&](const auto& tmpError, const auto& tmpStride, const auto& tmpBuffers) {
101                 if (tmpError != Error::NONE) {
102                     GTEST_FAIL() << "failed to allocate buffers";
103                 }
104                 ASSERT_EQ(count, tmpBuffers.size()) << "invalid buffer array";
105 
106                 for (uint32_t i = 0; i < count; i++) {
107                     if (import) {
108                         ASSERT_NO_FATAL_FAILURE(
109                                 bufferHandles.push_back(importBuffer(tmpBuffers[i])));
110                     } else {
111                         ASSERT_NO_FATAL_FAILURE(
112                                 bufferHandles.push_back(cloneBuffer(tmpBuffers[i])));
113                     }
114                 }
115 
116                 if (outStride) {
117                     *outStride = tmpStride;
118                 }
119             });
120 
121     if (::testing::Test::HasFatalFailure()) {
122         bufferHandles.clear();
123     }
124 
125     return bufferHandles;
126 }
127 
allocate(const IMapper::BufferDescriptorInfo & descriptorInfo,bool import,uint32_t * outStride)128 const native_handle_t* Gralloc::allocate(const IMapper::BufferDescriptorInfo& descriptorInfo,
129                                          bool import, uint32_t* outStride) {
130     BufferDescriptor descriptor = createDescriptor(descriptorInfo);
131     if (::testing::Test::HasFatalFailure()) {
132         return nullptr;
133     }
134 
135     auto buffers = allocate(descriptor, 1, import, outStride);
136     if (::testing::Test::HasFatalFailure() || ::testing::Test::IsSkipped()) {
137         return nullptr;
138     }
139 
140     return buffers[0];
141 }
142 
getMapper() const143 sp<IMapper> Gralloc::getMapper() const {
144     return mMapper;
145 }
146 
createDescriptor(const IMapper::BufferDescriptorInfo & descriptorInfo)147 BufferDescriptor Gralloc::createDescriptor(const IMapper::BufferDescriptorInfo& descriptorInfo) {
148     BufferDescriptor descriptor;
149     mMapper->createDescriptor(descriptorInfo, [&](const auto& tmpError, const auto& tmpDescriptor) {
150         ASSERT_EQ(Error::NONE, tmpError) << "failed to create descriptor";
151         descriptor = tmpDescriptor;
152     });
153 
154     return descriptor;
155 }
156 
importBuffer(const hidl_handle & rawHandle)157 const native_handle_t* Gralloc::importBuffer(const hidl_handle& rawHandle) {
158     const native_handle_t* bufferHandle = nullptr;
159     mMapper->importBuffer(rawHandle, [&](const auto& tmpError, const auto& tmpBuffer) {
160         ASSERT_EQ(Error::NONE, tmpError)
161             << "failed to import buffer %p" << rawHandle.getNativeHandle();
162         bufferHandle = static_cast<const native_handle_t*>(tmpBuffer);
163     });
164 
165     if (bufferHandle) {
166         mImportedBuffers.insert(bufferHandle);
167     }
168 
169     return bufferHandle;
170 }
171 
freeBuffer(const native_handle_t * bufferHandle)172 void Gralloc::freeBuffer(const native_handle_t* bufferHandle) {
173     auto buffer = const_cast<native_handle_t*>(bufferHandle);
174 
175     if (mImportedBuffers.erase(bufferHandle)) {
176         Error error = mMapper->freeBuffer(buffer);
177         ASSERT_EQ(Error::NONE, error) << "failed to free buffer " << buffer;
178     } else {
179         mClonedBuffers.erase(bufferHandle);
180         native_handle_close(buffer);
181         native_handle_delete(buffer);
182     }
183 }
184 
lock(const native_handle_t * bufferHandle,uint64_t cpuUsage,const IMapper::Rect & accessRegion,int acquireFence,int32_t * outBytesPerPixel,int32_t * outBytesPerStride)185 void* Gralloc::lock(const native_handle_t* bufferHandle, uint64_t cpuUsage,
186                     const IMapper::Rect& accessRegion, int acquireFence, int32_t* outBytesPerPixel,
187                     int32_t* outBytesPerStride) {
188     auto buffer = const_cast<native_handle_t*>(bufferHandle);
189 
190     NATIVE_HANDLE_DECLARE_STORAGE(acquireFenceStorage, 1, 0);
191     hidl_handle acquireFenceHandle;
192     if (acquireFence >= 0) {
193         auto h = native_handle_init(acquireFenceStorage, 1, 0);
194         h->data[0] = acquireFence;
195         acquireFenceHandle = h;
196     }
197 
198     *outBytesPerPixel = -1;
199     *outBytesPerStride = -1;
200 
201     void* data = nullptr;
202     mMapper->lock(buffer, cpuUsage, accessRegion, acquireFenceHandle,
203                   [&](const auto& tmpError, const auto& tmpData, int32_t tmpBytesPerPixel,
204                       int32_t tmpBytesPerStride) {
205                       ASSERT_EQ(Error::NONE, tmpError) << "failed to lock buffer " << buffer;
206                       data = tmpData;
207                       *outBytesPerPixel = tmpBytesPerPixel;
208                       *outBytesPerStride = tmpBytesPerStride;
209                   });
210 
211     if (acquireFence >= 0) {
212         close(acquireFence);
213     }
214 
215     return data;
216 }
217 
lockYCbCr(const native_handle_t * bufferHandle,uint64_t cpuUsage,const IMapper::Rect & accessRegion,int acquireFence)218 YCbCrLayout Gralloc::lockYCbCr(const native_handle_t* bufferHandle, uint64_t cpuUsage,
219                                const IMapper::Rect& accessRegion, int acquireFence) {
220     auto buffer = const_cast<native_handle_t*>(bufferHandle);
221 
222     NATIVE_HANDLE_DECLARE_STORAGE(acquireFenceStorage, 1, 0);
223     hidl_handle acquireFenceHandle;
224     if (acquireFence >= 0) {
225         auto h = native_handle_init(acquireFenceStorage, 1, 0);
226         h->data[0] = acquireFence;
227         acquireFenceHandle = h;
228     }
229 
230     YCbCrLayout layout = {};
231     mMapper->lockYCbCr(buffer, cpuUsage, accessRegion, acquireFenceHandle,
232                        [&](const auto& tmpError, const auto& tmpLayout) {
233                            ASSERT_EQ(Error::NONE, tmpError)
234                                << "failed to lockYCbCr buffer " << buffer;
235                            layout = tmpLayout;
236                        });
237 
238     if (acquireFence >= 0) {
239         close(acquireFence);
240     }
241 
242     return layout;
243 }
244 
unlock(const native_handle_t * bufferHandle)245 int Gralloc::unlock(const native_handle_t* bufferHandle) {
246     auto buffer = const_cast<native_handle_t*>(bufferHandle);
247 
248     int releaseFence = -1;
249     mMapper->unlock(buffer, [&](const auto& tmpError, const auto& tmpReleaseFence) {
250         ASSERT_EQ(Error::NONE, tmpError) << "failed to unlock buffer " << buffer;
251 
252         auto fenceHandle = tmpReleaseFence.getNativeHandle();
253         if (fenceHandle) {
254             ASSERT_EQ(0, fenceHandle->numInts) << "invalid fence handle " << fenceHandle;
255             if (fenceHandle->numFds == 1) {
256                 releaseFence = dup(fenceHandle->data[0]);
257                 ASSERT_LT(0, releaseFence) << "failed to dup fence fd";
258             } else {
259                 ASSERT_EQ(0, fenceHandle->numFds) << " invalid fence handle " << fenceHandle;
260             }
261         }
262     });
263 
264     return releaseFence;
265 }
266 
validateBufferSize(const native_handle_t * bufferHandle,const IMapper::BufferDescriptorInfo & descriptorInfo,uint32_t stride)267 bool Gralloc::validateBufferSize(const native_handle_t* bufferHandle,
268                                  const IMapper::BufferDescriptorInfo& descriptorInfo,
269                                  uint32_t stride) {
270     auto buffer = const_cast<native_handle_t*>(bufferHandle);
271 
272     Error error = mMapper->validateBufferSize(buffer, descriptorInfo, stride);
273     return error == Error::NONE;
274 }
275 
getTransportSize(const native_handle_t * bufferHandle,uint32_t * outNumFds,uint32_t * outNumInts)276 void Gralloc::getTransportSize(const native_handle_t* bufferHandle, uint32_t* outNumFds,
277                                uint32_t* outNumInts) {
278     auto buffer = const_cast<native_handle_t*>(bufferHandle);
279 
280     *outNumFds = 0;
281     *outNumInts = 0;
282     mMapper->getTransportSize(
283         buffer, [&](const auto& tmpError, const auto& tmpNumFds, const auto& tmpNumInts) {
284             ASSERT_EQ(Error::NONE, tmpError) << "failed to get transport size";
285             ASSERT_GE(bufferHandle->numFds, int(tmpNumFds)) << "invalid numFds " << tmpNumFds;
286             ASSERT_GE(bufferHandle->numInts, int(tmpNumInts)) << "invalid numInts " << tmpNumInts;
287 
288             *outNumFds = tmpNumFds;
289             *outNumInts = tmpNumInts;
290         });
291 }
292 
isSupported(const IMapper::BufferDescriptorInfo & descriptorInfo)293 bool Gralloc::isSupported(const IMapper::BufferDescriptorInfo& descriptorInfo) {
294     bool supported = false;
295     mMapper->isSupported(descriptorInfo, [&](const auto& tmpError, const auto& tmpSupported) {
296         ASSERT_EQ(Error::NONE, tmpError) << "failed to check is supported";
297         supported = tmpSupported;
298     });
299     return supported;
300 }
301 
302 }  // namespace vts
303 }  // namespace V3_0
304 }  // namespace mapper
305 }  // namespace graphics
306 }  // namespace hardware
307 }  // namespace android
308