blob: 901f0e3ae70a462319918b1f67822fe5af061f57 [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
Marissa Wall65341642019-06-20 13:21:06 -070020namespace android {
21namespace hardware {
22namespace graphics {
23namespace mapper {
24namespace V4_0 {
25namespace vts {
26
27Gralloc::Gralloc(const std::string& allocatorServiceName, const std::string& mapperServiceName,
28 bool errOnFailure) {
29 if (errOnFailure) {
30 init(allocatorServiceName, mapperServiceName);
31 } else {
32 initNoErr(allocatorServiceName, mapperServiceName);
33 }
34}
35
36void Gralloc::init(const std::string& allocatorServiceName, const std::string& mapperServiceName) {
Dan Shi46245c52019-12-06 16:12:53 -080037 mAllocator = IAllocator::getService(allocatorServiceName);
Marissa Wall65341642019-06-20 13:21:06 -070038 ASSERT_NE(nullptr, mAllocator.get()) << "failed to get allocator service";
39
Dan Shi46245c52019-12-06 16:12:53 -080040 mMapper = IMapper::getService(mapperServiceName);
Marissa Wall65341642019-06-20 13:21:06 -070041 ASSERT_NE(nullptr, mMapper.get()) << "failed to get mapper service";
42 ASSERT_FALSE(mMapper->isRemote()) << "mapper is not in passthrough mode";
43}
44
45void Gralloc::initNoErr(const std::string& allocatorServiceName,
46 const std::string& mapperServiceName) {
Dan Shi46245c52019-12-06 16:12:53 -080047 mAllocator = IAllocator::getService(allocatorServiceName);
Marissa Wall65341642019-06-20 13:21:06 -070048
Dan Shi46245c52019-12-06 16:12:53 -080049 mMapper = IMapper::getService(mapperServiceName);
Marissa Wall65341642019-06-20 13:21:06 -070050 if (mMapper.get()) {
51 ASSERT_FALSE(mMapper->isRemote()) << "mapper is not in passthrough mode";
52 }
53}
54
55Gralloc::~Gralloc() {
56 for (auto bufferHandle : mClonedBuffers) {
57 auto buffer = const_cast<native_handle_t*>(bufferHandle);
58 native_handle_close(buffer);
59 native_handle_delete(buffer);
60 }
61 mClonedBuffers.clear();
62
63 for (auto bufferHandle : mImportedBuffers) {
64 auto buffer = const_cast<native_handle_t*>(bufferHandle);
65 EXPECT_EQ(Error::NONE, mMapper->freeBuffer(buffer)) << "failed to free buffer " << buffer;
66 }
67 mImportedBuffers.clear();
68}
69
70sp<IAllocator> Gralloc::getAllocator() const {
71 return mAllocator;
72}
73
Yichi Chenc6394ff2020-05-12 08:58:08 +080074const native_handle_t* Gralloc::cloneBuffer(const hidl_handle& rawHandle,
75 enum Tolerance /*tolerance*/) {
Marissa Wall65341642019-06-20 13:21:06 -070076 const native_handle_t* bufferHandle = native_handle_clone(rawHandle.getNativeHandle());
77 EXPECT_NE(nullptr, bufferHandle);
78
79 if (bufferHandle) {
80 mClonedBuffers.insert(bufferHandle);
81 }
82
83 return bufferHandle;
84}
85
86std::vector<const native_handle_t*> Gralloc::allocate(const BufferDescriptor& descriptor,
87 uint32_t count, bool import,
Yichi Chenc6394ff2020-05-12 08:58:08 +080088 enum Tolerance tolerance,
89 uint32_t* outStride) {
Marissa Wall65341642019-06-20 13:21:06 -070090 std::vector<const native_handle_t*> bufferHandles;
91 bufferHandles.reserve(count);
Yichi Chenc6394ff2020-05-12 08:58:08 +080092 mAllocator->allocate(descriptor, count,
93 [&](const auto& tmpError, const auto& tmpStride, const auto& tmpBuffers) {
94 if (canTolerate(tolerance, tmpError)) {
95 return;
96 }
Marissa Wall65341642019-06-20 13:21:06 -070097
Yichi Chenc6394ff2020-05-12 08:58:08 +080098 ASSERT_EQ(Error::NONE, tmpError) << "failed to allocate buffers";
99 ASSERT_EQ(count, tmpBuffers.size()) << "invalid buffer array";
Marissa Wall65341642019-06-20 13:21:06 -0700100
Yichi Chenc6394ff2020-05-12 08:58:08 +0800101 for (uint32_t i = 0; i < count; i++) {
102 const native_handle_t* bufferHandle = nullptr;
103 if (import) {
104 ASSERT_NO_FATAL_FAILURE(
105 bufferHandle = importBuffer(tmpBuffers[i], tolerance));
106 } else {
107 ASSERT_NO_FATAL_FAILURE(
108 bufferHandle = cloneBuffer(tmpBuffers[i], tolerance));
109 }
110 if (bufferHandle) {
111 bufferHandles.push_back(bufferHandle);
112 }
113 }
114
115 if (outStride) {
116 *outStride = tmpStride;
117 }
118 });
Marissa Wall65341642019-06-20 13:21:06 -0700119
120 if (::testing::Test::HasFatalFailure()) {
121 bufferHandles.clear();
122 }
123
124 return bufferHandles;
125}
126
127const native_handle_t* Gralloc::allocate(const IMapper::BufferDescriptorInfo& descriptorInfo,
Yichi Chenc6394ff2020-05-12 08:58:08 +0800128 bool import, enum Tolerance tolerance,
129 uint32_t* outStride) {
Marissa Wall65341642019-06-20 13:21:06 -0700130 BufferDescriptor descriptor = createDescriptor(descriptorInfo);
131 if (::testing::Test::HasFatalFailure()) {
132 return nullptr;
133 }
134
Yichi Chenc6394ff2020-05-12 08:58:08 +0800135 auto buffers = allocate(descriptor, 1, import, tolerance, outStride);
Marissa Wall65341642019-06-20 13:21:06 -0700136 if (::testing::Test::HasFatalFailure()) {
137 return nullptr;
138 }
139
Marissa Wall88d87fa2019-11-05 14:57:51 -0800140 if (buffers.size() != 1) {
141 return nullptr;
142 }
Marissa Wall65341642019-06-20 13:21:06 -0700143 return buffers[0];
144}
145
146sp<IMapper> Gralloc::getMapper() const {
147 return mMapper;
148}
149
150BufferDescriptor Gralloc::createDescriptor(const IMapper::BufferDescriptorInfo& descriptorInfo) {
151 BufferDescriptor descriptor;
152 mMapper->createDescriptor(descriptorInfo, [&](const auto& tmpError, const auto& tmpDescriptor) {
153 ASSERT_EQ(Error::NONE, tmpError) << "failed to create descriptor";
154 descriptor = tmpDescriptor;
155 });
156
157 return descriptor;
158}
159
Yichi Chenc6394ff2020-05-12 08:58:08 +0800160const native_handle_t* Gralloc::importBuffer(const hidl_handle& rawHandle,
161 enum Tolerance tolerance) {
Marissa Wall65341642019-06-20 13:21:06 -0700162 const native_handle_t* bufferHandle = nullptr;
163 mMapper->importBuffer(rawHandle, [&](const auto& tmpError, const auto& tmpBuffer) {
Yichi Chenc6394ff2020-05-12 08:58:08 +0800164 if (!canTolerate(tolerance, tmpError)) {
165 ASSERT_EQ(Error::NONE, tmpError)
166 << "failed to import buffer %p" << rawHandle.getNativeHandle();
167 }
Marissa Wall65341642019-06-20 13:21:06 -0700168 bufferHandle = static_cast<const native_handle_t*>(tmpBuffer);
169 });
170
171 if (bufferHandle) {
172 mImportedBuffers.insert(bufferHandle);
173 }
174
175 return bufferHandle;
176}
177
178void Gralloc::freeBuffer(const native_handle_t* bufferHandle) {
Marissa Wall88d87fa2019-11-05 14:57:51 -0800179 if (bufferHandle == nullptr) {
180 return;
181 }
182
Marissa Wall65341642019-06-20 13:21:06 -0700183 auto buffer = const_cast<native_handle_t*>(bufferHandle);
184
185 if (mImportedBuffers.erase(bufferHandle)) {
186 Error error = mMapper->freeBuffer(buffer);
187 ASSERT_EQ(Error::NONE, error) << "failed to free buffer " << buffer;
188 } else {
189 mClonedBuffers.erase(bufferHandle);
190 native_handle_close(buffer);
191 native_handle_delete(buffer);
192 }
193}
194
195void* Gralloc::lock(const native_handle_t* bufferHandle, uint64_t cpuUsage,
Marissa Wall9c5ebfc2019-11-05 14:59:27 -0800196 const IMapper::Rect& accessRegion, int acquireFence) {
Marissa Wall65341642019-06-20 13:21:06 -0700197 auto buffer = const_cast<native_handle_t*>(bufferHandle);
198
199 NATIVE_HANDLE_DECLARE_STORAGE(acquireFenceStorage, 1, 0);
200 hidl_handle acquireFenceHandle;
201 if (acquireFence >= 0) {
202 auto h = native_handle_init(acquireFenceStorage, 1, 0);
203 h->data[0] = acquireFence;
204 acquireFenceHandle = h;
205 }
206
Marissa Wall65341642019-06-20 13:21:06 -0700207 void* data = nullptr;
208 mMapper->lock(buffer, cpuUsage, accessRegion, acquireFenceHandle,
Marissa Wall9c5ebfc2019-11-05 14:59:27 -0800209 [&](const auto& tmpError, const auto& tmpData) {
Marissa Wall65341642019-06-20 13:21:06 -0700210 ASSERT_EQ(Error::NONE, tmpError) << "failed to lock buffer " << buffer;
211 data = tmpData;
Marissa Wall65341642019-06-20 13:21:06 -0700212 });
213
214 if (acquireFence >= 0) {
215 close(acquireFence);
216 }
217
218 return data;
219}
220
Marissa Wall65341642019-06-20 13:21:06 -0700221int Gralloc::unlock(const native_handle_t* bufferHandle) {
222 auto buffer = const_cast<native_handle_t*>(bufferHandle);
223
224 int releaseFence = -1;
225 mMapper->unlock(buffer, [&](const auto& tmpError, const auto& tmpReleaseFence) {
226 ASSERT_EQ(Error::NONE, tmpError) << "failed to unlock buffer " << buffer;
227
228 auto fenceHandle = tmpReleaseFence.getNativeHandle();
229 if (fenceHandle) {
230 ASSERT_EQ(0, fenceHandle->numInts) << "invalid fence handle " << fenceHandle;
231 if (fenceHandle->numFds == 1) {
232 releaseFence = dup(fenceHandle->data[0]);
233 ASSERT_LT(0, releaseFence) << "failed to dup fence fd";
234 } else {
235 ASSERT_EQ(0, fenceHandle->numFds) << " invalid fence handle " << fenceHandle;
236 }
237 }
238 });
239
240 return releaseFence;
241}
242
Marissa Wall2c45bb12019-10-18 13:31:36 -0700243int Gralloc::flushLockedBuffer(const native_handle_t* bufferHandle) {
244 auto buffer = const_cast<native_handle_t*>(bufferHandle);
245
246 int releaseFence = -1;
247 mMapper->flushLockedBuffer(buffer, [&](const auto& tmpError, const auto& tmpReleaseFence) {
248 ASSERT_EQ(Error::NONE, tmpError) << "failed to flush locked buffer " << buffer;
249
250 auto fenceHandle = tmpReleaseFence.getNativeHandle();
251 if (fenceHandle) {
252 ASSERT_EQ(0, fenceHandle->numInts) << "invalid fence handle " << fenceHandle;
253 if (fenceHandle->numFds == 1) {
254 releaseFence = dup(fenceHandle->data[0]);
255 ASSERT_LT(0, releaseFence) << "failed to dup fence fd";
256 } else {
257 ASSERT_EQ(0, fenceHandle->numFds) << " invalid fence handle " << fenceHandle;
258 }
259 }
260 });
261
262 return releaseFence;
263}
264
265void Gralloc::rereadLockedBuffer(const native_handle_t* bufferHandle) {
266 auto buffer = const_cast<native_handle_t*>(bufferHandle);
267
268 ASSERT_EQ(Error::NONE, mMapper->rereadLockedBuffer(buffer));
269}
270
Marissa Wall65341642019-06-20 13:21:06 -0700271bool Gralloc::validateBufferSize(const native_handle_t* bufferHandle,
272 const IMapper::BufferDescriptorInfo& descriptorInfo,
273 uint32_t stride) {
274 auto buffer = const_cast<native_handle_t*>(bufferHandle);
275
276 Error error = mMapper->validateBufferSize(buffer, descriptorInfo, stride);
277 return error == Error::NONE;
278}
279
280void Gralloc::getTransportSize(const native_handle_t* bufferHandle, uint32_t* outNumFds,
281 uint32_t* outNumInts) {
282 auto buffer = const_cast<native_handle_t*>(bufferHandle);
283
284 *outNumFds = 0;
285 *outNumInts = 0;
286 mMapper->getTransportSize(buffer, [&](const auto& tmpError, const auto& tmpNumFds,
287 const auto& tmpNumInts) {
288 ASSERT_EQ(Error::NONE, tmpError) << "failed to get transport size";
289 ASSERT_GE(bufferHandle->numFds, int(tmpNumFds)) << "invalid numFds " << tmpNumFds;
290 ASSERT_GE(bufferHandle->numInts, int(tmpNumInts)) << "invalid numInts " << tmpNumInts;
291
292 *outNumFds = tmpNumFds;
293 *outNumInts = tmpNumInts;
294 });
295}
296
297bool Gralloc::isSupported(const IMapper::BufferDescriptorInfo& descriptorInfo) {
298 bool supported = false;
299 mMapper->isSupported(descriptorInfo, [&](const auto& tmpError, const auto& tmpSupported) {
300 ASSERT_EQ(Error::NONE, tmpError) << "failed to check is supported";
301 supported = tmpSupported;
302 });
303 return supported;
304}
305
Leon Scroggins IIIcd5e4692022-02-16 16:36:27 -0500306bool Gralloc::isSupportedNoFailure(const IMapper::BufferDescriptorInfo& descriptorInfo) {
307 bool supported = false;
308 mMapper->isSupported(descriptorInfo, [&](const auto& tmpError, const auto& tmpSupported) {
309 supported = tmpSupported && tmpError == Error::NONE;
310 });
311 return supported;
312}
313
Marissa Wall88d87fa2019-11-05 14:57:51 -0800314Error Gralloc::get(const native_handle_t* bufferHandle, const IMapper::MetadataType& metadataType,
315 hidl_vec<uint8_t>* outVec) {
316 Error err;
317 mMapper->get(const_cast<native_handle_t*>(bufferHandle), metadataType,
318 [&](const auto& tmpError, const hidl_vec<uint8_t>& tmpVec) {
319 err = tmpError;
320 *outVec = tmpVec;
321 });
322 return err;
323}
324
325Error Gralloc::set(const native_handle_t* bufferHandle, const IMapper::MetadataType& metadataType,
326 const hidl_vec<uint8_t>& vec) {
327 return mMapper->set(const_cast<native_handle_t*>(bufferHandle), metadataType, vec);
328}
329
330Error Gralloc::getFromBufferDescriptorInfo(const IMapper::BufferDescriptorInfo& descriptorInfo,
331 const IMapper::MetadataType& metadataType,
332 hidl_vec<uint8_t>* outVec) {
333 Error err;
334 mMapper->getFromBufferDescriptorInfo(
335 descriptorInfo, metadataType,
336 [&](const auto& tmpError, const hidl_vec<uint8_t>& tmpVec) {
337 err = tmpError;
338 *outVec = tmpVec;
339 });
340 return err;
341}
342
Marissa Walld36e1272019-11-26 11:33:32 -0800343Error Gralloc::getReservedRegion(const native_handle_t* bufferHandle, void** outReservedRegion,
344 uint64_t* outReservedSize) {
345 Error err;
346 mMapper->getReservedRegion(
347 const_cast<native_handle_t*>(bufferHandle),
348 [&](const auto& tmpError, const auto& tmpReservedRegion, const auto& tmpReservedSize) {
349 err = tmpError;
350 *outReservedRegion = tmpReservedRegion;
351 *outReservedSize = tmpReservedSize;
352 });
353 return err;
354}
355
Marissa Wall65341642019-06-20 13:21:06 -0700356} // namespace vts
357} // namespace V4_0
358} // namespace mapper
359} // namespace graphics
360} // namespace hardware
361} // namespace android