blob: 8073e6924c35bd803ba5a7fa402adde03b47d2d1 [file] [log] [blame]
Marissa Wall65341642019-06-20 13:21:06 -07001/*
2 * Copyright 2019 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
Marissa Wall88d87fa2019-11-05 14:57:51 -080017#include <gralloctypes/Gralloc4.h>
Marissa Wall65341642019-06-20 13:21:06 -070018#include <mapper-vts/4.0/MapperVts.h>
19
20#include <VtsHalHidlTargetTestBase.h>
21
22namespace android {
23namespace hardware {
24namespace graphics {
25namespace mapper {
26namespace V4_0 {
27namespace vts {
28
29Gralloc::Gralloc(const std::string& allocatorServiceName, const std::string& mapperServiceName,
30 bool errOnFailure) {
31 if (errOnFailure) {
32 init(allocatorServiceName, mapperServiceName);
33 } else {
34 initNoErr(allocatorServiceName, mapperServiceName);
35 }
36}
37
38void Gralloc::init(const std::string& allocatorServiceName, const std::string& mapperServiceName) {
39 mAllocator = ::testing::VtsHalHidlTargetTestBase::getService<IAllocator>(allocatorServiceName);
40 ASSERT_NE(nullptr, mAllocator.get()) << "failed to get allocator service";
41
42 mMapper = ::testing::VtsHalHidlTargetTestBase::getService<IMapper>(mapperServiceName);
43 ASSERT_NE(nullptr, mMapper.get()) << "failed to get mapper service";
44 ASSERT_FALSE(mMapper->isRemote()) << "mapper is not in passthrough mode";
45}
46
47void Gralloc::initNoErr(const std::string& allocatorServiceName,
48 const std::string& mapperServiceName) {
49 mAllocator = ::testing::VtsHalHidlTargetTestBase::getService<IAllocator>(allocatorServiceName);
50
51 mMapper = ::testing::VtsHalHidlTargetTestBase::getService<IMapper>(mapperServiceName);
52 if (mMapper.get()) {
53 ASSERT_FALSE(mMapper->isRemote()) << "mapper is not in passthrough mode";
54 }
55}
56
57Gralloc::~Gralloc() {
58 for (auto bufferHandle : mClonedBuffers) {
59 auto buffer = const_cast<native_handle_t*>(bufferHandle);
60 native_handle_close(buffer);
61 native_handle_delete(buffer);
62 }
63 mClonedBuffers.clear();
64
65 for (auto bufferHandle : mImportedBuffers) {
66 auto buffer = const_cast<native_handle_t*>(bufferHandle);
67 EXPECT_EQ(Error::NONE, mMapper->freeBuffer(buffer)) << "failed to free buffer " << buffer;
68 }
69 mImportedBuffers.clear();
70}
71
72sp<IAllocator> Gralloc::getAllocator() const {
73 return mAllocator;
74}
75
76std::string Gralloc::dumpDebugInfo() {
77 std::string debugInfo;
78 mAllocator->dumpDebugInfo([&](const auto& tmpDebugInfo) { debugInfo = tmpDebugInfo.c_str(); });
79
80 return debugInfo;
81}
82
83const native_handle_t* Gralloc::cloneBuffer(const hidl_handle& rawHandle) {
84 const native_handle_t* bufferHandle = native_handle_clone(rawHandle.getNativeHandle());
85 EXPECT_NE(nullptr, bufferHandle);
86
87 if (bufferHandle) {
88 mClonedBuffers.insert(bufferHandle);
89 }
90
91 return bufferHandle;
92}
93
94std::vector<const native_handle_t*> Gralloc::allocate(const BufferDescriptor& descriptor,
95 uint32_t count, bool import,
Marissa Wall88d87fa2019-11-05 14:57:51 -080096 bool allowFailure, uint32_t* outStride) {
Marissa Wall65341642019-06-20 13:21:06 -070097 std::vector<const native_handle_t*> bufferHandles;
98 bufferHandles.reserve(count);
Marissa Wall88d87fa2019-11-05 14:57:51 -080099 mAllocator->allocate(
100 descriptor, count,
101 [&](const auto& tmpError, const auto& tmpStride, const auto& tmpBuffers) {
102 ASSERT_EQ(Error::NONE, tmpError) << "failed to allocate buffers";
103 ASSERT_EQ(count, tmpBuffers.size()) << "invalid buffer array";
Marissa Wall65341642019-06-20 13:21:06 -0700104
Marissa Wall88d87fa2019-11-05 14:57:51 -0800105 for (uint32_t i = 0; i < count; i++) {
106 const native_handle_t* bufferHandle = nullptr;
107 if (import) {
108 if (allowFailure) {
109 bufferHandle = importBuffer(tmpBuffers[i]);
110 } else {
111 ASSERT_NO_FATAL_FAILURE(bufferHandle = importBuffer(tmpBuffers[i]));
112 }
113 } else {
114 if (allowFailure) {
115 bufferHandle = cloneBuffer(tmpBuffers[i]);
116 } else {
117 ASSERT_NO_FATAL_FAILURE(bufferHandle = cloneBuffer(tmpBuffers[i]));
118 }
119 }
120 if (bufferHandle) {
121 bufferHandles.push_back(bufferHandle);
122 }
123 }
Marissa Wall65341642019-06-20 13:21:06 -0700124
Marissa Wall88d87fa2019-11-05 14:57:51 -0800125 if (outStride) {
126 *outStride = tmpStride;
127 }
128 });
Marissa Wall65341642019-06-20 13:21:06 -0700129
130 if (::testing::Test::HasFatalFailure()) {
131 bufferHandles.clear();
132 }
133
134 return bufferHandles;
135}
136
137const native_handle_t* Gralloc::allocate(const IMapper::BufferDescriptorInfo& descriptorInfo,
Marissa Wall88d87fa2019-11-05 14:57:51 -0800138 bool import, bool allowFailure, uint32_t* outStride) {
Marissa Wall65341642019-06-20 13:21:06 -0700139 BufferDescriptor descriptor = createDescriptor(descriptorInfo);
140 if (::testing::Test::HasFatalFailure()) {
141 return nullptr;
142 }
143
Marissa Wall88d87fa2019-11-05 14:57:51 -0800144 auto buffers = allocate(descriptor, 1, import, allowFailure, outStride);
Marissa Wall65341642019-06-20 13:21:06 -0700145 if (::testing::Test::HasFatalFailure()) {
146 return nullptr;
147 }
148
Marissa Wall88d87fa2019-11-05 14:57:51 -0800149 if (buffers.size() != 1) {
150 return nullptr;
151 }
Marissa Wall65341642019-06-20 13:21:06 -0700152 return buffers[0];
153}
154
155sp<IMapper> Gralloc::getMapper() const {
156 return mMapper;
157}
158
159BufferDescriptor Gralloc::createDescriptor(const IMapper::BufferDescriptorInfo& descriptorInfo) {
160 BufferDescriptor descriptor;
161 mMapper->createDescriptor(descriptorInfo, [&](const auto& tmpError, const auto& tmpDescriptor) {
162 ASSERT_EQ(Error::NONE, tmpError) << "failed to create descriptor";
163 descriptor = tmpDescriptor;
164 });
165
166 return descriptor;
167}
168
169const native_handle_t* Gralloc::importBuffer(const hidl_handle& rawHandle) {
170 const native_handle_t* bufferHandle = nullptr;
171 mMapper->importBuffer(rawHandle, [&](const auto& tmpError, const auto& tmpBuffer) {
172 ASSERT_EQ(Error::NONE, tmpError)
173 << "failed to import buffer %p" << rawHandle.getNativeHandle();
174 bufferHandle = static_cast<const native_handle_t*>(tmpBuffer);
175 });
176
177 if (bufferHandle) {
178 mImportedBuffers.insert(bufferHandle);
179 }
180
181 return bufferHandle;
182}
183
184void Gralloc::freeBuffer(const native_handle_t* bufferHandle) {
Marissa Wall88d87fa2019-11-05 14:57:51 -0800185 if (bufferHandle == nullptr) {
186 return;
187 }
188
Marissa Wall65341642019-06-20 13:21:06 -0700189 auto buffer = const_cast<native_handle_t*>(bufferHandle);
190
191 if (mImportedBuffers.erase(bufferHandle)) {
192 Error error = mMapper->freeBuffer(buffer);
193 ASSERT_EQ(Error::NONE, error) << "failed to free buffer " << buffer;
194 } else {
195 mClonedBuffers.erase(bufferHandle);
196 native_handle_close(buffer);
197 native_handle_delete(buffer);
198 }
199}
200
201void* Gralloc::lock(const native_handle_t* bufferHandle, uint64_t cpuUsage,
Marissa Wall9c5ebfc2019-11-05 14:59:27 -0800202 const IMapper::Rect& accessRegion, int acquireFence) {
Marissa Wall65341642019-06-20 13:21:06 -0700203 auto buffer = const_cast<native_handle_t*>(bufferHandle);
204
205 NATIVE_HANDLE_DECLARE_STORAGE(acquireFenceStorage, 1, 0);
206 hidl_handle acquireFenceHandle;
207 if (acquireFence >= 0) {
208 auto h = native_handle_init(acquireFenceStorage, 1, 0);
209 h->data[0] = acquireFence;
210 acquireFenceHandle = h;
211 }
212
Marissa Wall65341642019-06-20 13:21:06 -0700213 void* data = nullptr;
214 mMapper->lock(buffer, cpuUsage, accessRegion, acquireFenceHandle,
Marissa Wall9c5ebfc2019-11-05 14:59:27 -0800215 [&](const auto& tmpError, const auto& tmpData) {
Marissa Wall65341642019-06-20 13:21:06 -0700216 ASSERT_EQ(Error::NONE, tmpError) << "failed to lock buffer " << buffer;
217 data = tmpData;
Marissa Wall65341642019-06-20 13:21:06 -0700218 });
219
220 if (acquireFence >= 0) {
221 close(acquireFence);
222 }
223
224 return data;
225}
226
Marissa Wall65341642019-06-20 13:21:06 -0700227int Gralloc::unlock(const native_handle_t* bufferHandle) {
228 auto buffer = const_cast<native_handle_t*>(bufferHandle);
229
230 int releaseFence = -1;
231 mMapper->unlock(buffer, [&](const auto& tmpError, const auto& tmpReleaseFence) {
232 ASSERT_EQ(Error::NONE, tmpError) << "failed to unlock buffer " << buffer;
233
234 auto fenceHandle = tmpReleaseFence.getNativeHandle();
235 if (fenceHandle) {
236 ASSERT_EQ(0, fenceHandle->numInts) << "invalid fence handle " << fenceHandle;
237 if (fenceHandle->numFds == 1) {
238 releaseFence = dup(fenceHandle->data[0]);
239 ASSERT_LT(0, releaseFence) << "failed to dup fence fd";
240 } else {
241 ASSERT_EQ(0, fenceHandle->numFds) << " invalid fence handle " << fenceHandle;
242 }
243 }
244 });
245
246 return releaseFence;
247}
248
249bool Gralloc::validateBufferSize(const native_handle_t* bufferHandle,
250 const IMapper::BufferDescriptorInfo& descriptorInfo,
251 uint32_t stride) {
252 auto buffer = const_cast<native_handle_t*>(bufferHandle);
253
254 Error error = mMapper->validateBufferSize(buffer, descriptorInfo, stride);
255 return error == Error::NONE;
256}
257
258void Gralloc::getTransportSize(const native_handle_t* bufferHandle, uint32_t* outNumFds,
259 uint32_t* outNumInts) {
260 auto buffer = const_cast<native_handle_t*>(bufferHandle);
261
262 *outNumFds = 0;
263 *outNumInts = 0;
264 mMapper->getTransportSize(buffer, [&](const auto& tmpError, const auto& tmpNumFds,
265 const auto& tmpNumInts) {
266 ASSERT_EQ(Error::NONE, tmpError) << "failed to get transport size";
267 ASSERT_GE(bufferHandle->numFds, int(tmpNumFds)) << "invalid numFds " << tmpNumFds;
268 ASSERT_GE(bufferHandle->numInts, int(tmpNumInts)) << "invalid numInts " << tmpNumInts;
269
270 *outNumFds = tmpNumFds;
271 *outNumInts = tmpNumInts;
272 });
273}
274
275bool Gralloc::isSupported(const IMapper::BufferDescriptorInfo& descriptorInfo) {
276 bool supported = false;
277 mMapper->isSupported(descriptorInfo, [&](const auto& tmpError, const auto& tmpSupported) {
278 ASSERT_EQ(Error::NONE, tmpError) << "failed to check is supported";
279 supported = tmpSupported;
280 });
281 return supported;
282}
283
Marissa Wall88d87fa2019-11-05 14:57:51 -0800284Error Gralloc::get(const native_handle_t* bufferHandle, const IMapper::MetadataType& metadataType,
285 hidl_vec<uint8_t>* outVec) {
286 Error err;
287 mMapper->get(const_cast<native_handle_t*>(bufferHandle), metadataType,
288 [&](const auto& tmpError, const hidl_vec<uint8_t>& tmpVec) {
289 err = tmpError;
290 *outVec = tmpVec;
291 });
292 return err;
293}
294
295Error Gralloc::set(const native_handle_t* bufferHandle, const IMapper::MetadataType& metadataType,
296 const hidl_vec<uint8_t>& vec) {
297 return mMapper->set(const_cast<native_handle_t*>(bufferHandle), metadataType, vec);
298}
299
300Error Gralloc::getFromBufferDescriptorInfo(const IMapper::BufferDescriptorInfo& descriptorInfo,
301 const IMapper::MetadataType& metadataType,
302 hidl_vec<uint8_t>* outVec) {
303 Error err;
304 mMapper->getFromBufferDescriptorInfo(
305 descriptorInfo, metadataType,
306 [&](const auto& tmpError, const hidl_vec<uint8_t>& tmpVec) {
307 err = tmpError;
308 *outVec = tmpVec;
309 });
310 return err;
311}
312
Marissa Wall65341642019-06-20 13:21:06 -0700313} // namespace vts
314} // namespace V4_0
315} // namespace mapper
316} // namespace graphics
317} // namespace hardware
318} // namespace android