blob: 8a5f54ebac014e090abf96c769cb4eaf63301c06 [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
74std::string Gralloc::dumpDebugInfo() {
75 std::string debugInfo;
76 mAllocator->dumpDebugInfo([&](const auto& tmpDebugInfo) { debugInfo = tmpDebugInfo.c_str(); });
77
78 return debugInfo;
79}
80
81const native_handle_t* Gralloc::cloneBuffer(const hidl_handle& rawHandle) {
82 const native_handle_t* bufferHandle = native_handle_clone(rawHandle.getNativeHandle());
83 EXPECT_NE(nullptr, bufferHandle);
84
85 if (bufferHandle) {
86 mClonedBuffers.insert(bufferHandle);
87 }
88
89 return bufferHandle;
90}
91
92std::vector<const native_handle_t*> Gralloc::allocate(const BufferDescriptor& descriptor,
93 uint32_t count, bool import,
Marissa Wall88d87fa2019-11-05 14:57:51 -080094 bool allowFailure, uint32_t* outStride) {
Marissa Wall65341642019-06-20 13:21:06 -070095 std::vector<const native_handle_t*> bufferHandles;
96 bufferHandles.reserve(count);
Marissa Wall88d87fa2019-11-05 14:57:51 -080097 mAllocator->allocate(
98 descriptor, count,
99 [&](const auto& tmpError, const auto& tmpStride, const auto& tmpBuffers) {
100 ASSERT_EQ(Error::NONE, tmpError) << "failed to allocate buffers";
101 ASSERT_EQ(count, tmpBuffers.size()) << "invalid buffer array";
Marissa Wall65341642019-06-20 13:21:06 -0700102
Marissa Wall88d87fa2019-11-05 14:57:51 -0800103 for (uint32_t i = 0; i < count; i++) {
104 const native_handle_t* bufferHandle = nullptr;
105 if (import) {
106 if (allowFailure) {
107 bufferHandle = importBuffer(tmpBuffers[i]);
108 } else {
109 ASSERT_NO_FATAL_FAILURE(bufferHandle = importBuffer(tmpBuffers[i]));
110 }
111 } else {
112 if (allowFailure) {
113 bufferHandle = cloneBuffer(tmpBuffers[i]);
114 } else {
115 ASSERT_NO_FATAL_FAILURE(bufferHandle = cloneBuffer(tmpBuffers[i]));
116 }
117 }
118 if (bufferHandle) {
119 bufferHandles.push_back(bufferHandle);
120 }
121 }
Marissa Wall65341642019-06-20 13:21:06 -0700122
Marissa Wall88d87fa2019-11-05 14:57:51 -0800123 if (outStride) {
124 *outStride = tmpStride;
125 }
126 });
Marissa Wall65341642019-06-20 13:21:06 -0700127
128 if (::testing::Test::HasFatalFailure()) {
129 bufferHandles.clear();
130 }
131
132 return bufferHandles;
133}
134
135const native_handle_t* Gralloc::allocate(const IMapper::BufferDescriptorInfo& descriptorInfo,
Marissa Wall88d87fa2019-11-05 14:57:51 -0800136 bool import, bool allowFailure, uint32_t* outStride) {
Marissa Wall65341642019-06-20 13:21:06 -0700137 BufferDescriptor descriptor = createDescriptor(descriptorInfo);
138 if (::testing::Test::HasFatalFailure()) {
139 return nullptr;
140 }
141
Marissa Wall88d87fa2019-11-05 14:57:51 -0800142 auto buffers = allocate(descriptor, 1, import, allowFailure, outStride);
Marissa Wall65341642019-06-20 13:21:06 -0700143 if (::testing::Test::HasFatalFailure()) {
144 return nullptr;
145 }
146
Marissa Wall88d87fa2019-11-05 14:57:51 -0800147 if (buffers.size() != 1) {
148 return nullptr;
149 }
Marissa Wall65341642019-06-20 13:21:06 -0700150 return buffers[0];
151}
152
153sp<IMapper> Gralloc::getMapper() const {
154 return mMapper;
155}
156
157BufferDescriptor Gralloc::createDescriptor(const IMapper::BufferDescriptorInfo& descriptorInfo) {
158 BufferDescriptor descriptor;
159 mMapper->createDescriptor(descriptorInfo, [&](const auto& tmpError, const auto& tmpDescriptor) {
160 ASSERT_EQ(Error::NONE, tmpError) << "failed to create descriptor";
161 descriptor = tmpDescriptor;
162 });
163
164 return descriptor;
165}
166
167const native_handle_t* Gralloc::importBuffer(const hidl_handle& rawHandle) {
168 const native_handle_t* bufferHandle = nullptr;
169 mMapper->importBuffer(rawHandle, [&](const auto& tmpError, const auto& tmpBuffer) {
170 ASSERT_EQ(Error::NONE, tmpError)
171 << "failed to import buffer %p" << rawHandle.getNativeHandle();
172 bufferHandle = static_cast<const native_handle_t*>(tmpBuffer);
173 });
174
175 if (bufferHandle) {
176 mImportedBuffers.insert(bufferHandle);
177 }
178
179 return bufferHandle;
180}
181
182void Gralloc::freeBuffer(const native_handle_t* bufferHandle) {
Marissa Wall88d87fa2019-11-05 14:57:51 -0800183 if (bufferHandle == nullptr) {
184 return;
185 }
186
Marissa Wall65341642019-06-20 13:21:06 -0700187 auto buffer = const_cast<native_handle_t*>(bufferHandle);
188
189 if (mImportedBuffers.erase(bufferHandle)) {
190 Error error = mMapper->freeBuffer(buffer);
191 ASSERT_EQ(Error::NONE, error) << "failed to free buffer " << buffer;
192 } else {
193 mClonedBuffers.erase(bufferHandle);
194 native_handle_close(buffer);
195 native_handle_delete(buffer);
196 }
197}
198
199void* Gralloc::lock(const native_handle_t* bufferHandle, uint64_t cpuUsage,
Marissa Wall9c5ebfc2019-11-05 14:59:27 -0800200 const IMapper::Rect& accessRegion, int acquireFence) {
Marissa Wall65341642019-06-20 13:21:06 -0700201 auto buffer = const_cast<native_handle_t*>(bufferHandle);
202
203 NATIVE_HANDLE_DECLARE_STORAGE(acquireFenceStorage, 1, 0);
204 hidl_handle acquireFenceHandle;
205 if (acquireFence >= 0) {
206 auto h = native_handle_init(acquireFenceStorage, 1, 0);
207 h->data[0] = acquireFence;
208 acquireFenceHandle = h;
209 }
210
Marissa Wall65341642019-06-20 13:21:06 -0700211 void* data = nullptr;
212 mMapper->lock(buffer, cpuUsage, accessRegion, acquireFenceHandle,
Marissa Wall9c5ebfc2019-11-05 14:59:27 -0800213 [&](const auto& tmpError, const auto& tmpData) {
Marissa Wall65341642019-06-20 13:21:06 -0700214 ASSERT_EQ(Error::NONE, tmpError) << "failed to lock buffer " << buffer;
215 data = tmpData;
Marissa Wall65341642019-06-20 13:21:06 -0700216 });
217
218 if (acquireFence >= 0) {
219 close(acquireFence);
220 }
221
222 return data;
223}
224
Marissa Wall65341642019-06-20 13:21:06 -0700225int Gralloc::unlock(const native_handle_t* bufferHandle) {
226 auto buffer = const_cast<native_handle_t*>(bufferHandle);
227
228 int releaseFence = -1;
229 mMapper->unlock(buffer, [&](const auto& tmpError, const auto& tmpReleaseFence) {
230 ASSERT_EQ(Error::NONE, tmpError) << "failed to unlock buffer " << buffer;
231
232 auto fenceHandle = tmpReleaseFence.getNativeHandle();
233 if (fenceHandle) {
234 ASSERT_EQ(0, fenceHandle->numInts) << "invalid fence handle " << fenceHandle;
235 if (fenceHandle->numFds == 1) {
236 releaseFence = dup(fenceHandle->data[0]);
237 ASSERT_LT(0, releaseFence) << "failed to dup fence fd";
238 } else {
239 ASSERT_EQ(0, fenceHandle->numFds) << " invalid fence handle " << fenceHandle;
240 }
241 }
242 });
243
244 return releaseFence;
245}
246
Marissa Wall2c45bb12019-10-18 13:31:36 -0700247int Gralloc::flushLockedBuffer(const native_handle_t* bufferHandle) {
248 auto buffer = const_cast<native_handle_t*>(bufferHandle);
249
250 int releaseFence = -1;
251 mMapper->flushLockedBuffer(buffer, [&](const auto& tmpError, const auto& tmpReleaseFence) {
252 ASSERT_EQ(Error::NONE, tmpError) << "failed to flush locked buffer " << buffer;
253
254 auto fenceHandle = tmpReleaseFence.getNativeHandle();
255 if (fenceHandle) {
256 ASSERT_EQ(0, fenceHandle->numInts) << "invalid fence handle " << fenceHandle;
257 if (fenceHandle->numFds == 1) {
258 releaseFence = dup(fenceHandle->data[0]);
259 ASSERT_LT(0, releaseFence) << "failed to dup fence fd";
260 } else {
261 ASSERT_EQ(0, fenceHandle->numFds) << " invalid fence handle " << fenceHandle;
262 }
263 }
264 });
265
266 return releaseFence;
267}
268
269void Gralloc::rereadLockedBuffer(const native_handle_t* bufferHandle) {
270 auto buffer = const_cast<native_handle_t*>(bufferHandle);
271
272 ASSERT_EQ(Error::NONE, mMapper->rereadLockedBuffer(buffer));
273}
274
Marissa Wall65341642019-06-20 13:21:06 -0700275bool Gralloc::validateBufferSize(const native_handle_t* bufferHandle,
276 const IMapper::BufferDescriptorInfo& descriptorInfo,
277 uint32_t stride) {
278 auto buffer = const_cast<native_handle_t*>(bufferHandle);
279
280 Error error = mMapper->validateBufferSize(buffer, descriptorInfo, stride);
281 return error == Error::NONE;
282}
283
284void Gralloc::getTransportSize(const native_handle_t* bufferHandle, uint32_t* outNumFds,
285 uint32_t* outNumInts) {
286 auto buffer = const_cast<native_handle_t*>(bufferHandle);
287
288 *outNumFds = 0;
289 *outNumInts = 0;
290 mMapper->getTransportSize(buffer, [&](const auto& tmpError, const auto& tmpNumFds,
291 const auto& tmpNumInts) {
292 ASSERT_EQ(Error::NONE, tmpError) << "failed to get transport size";
293 ASSERT_GE(bufferHandle->numFds, int(tmpNumFds)) << "invalid numFds " << tmpNumFds;
294 ASSERT_GE(bufferHandle->numInts, int(tmpNumInts)) << "invalid numInts " << tmpNumInts;
295
296 *outNumFds = tmpNumFds;
297 *outNumInts = tmpNumInts;
298 });
299}
300
301bool Gralloc::isSupported(const IMapper::BufferDescriptorInfo& descriptorInfo) {
302 bool supported = false;
303 mMapper->isSupported(descriptorInfo, [&](const auto& tmpError, const auto& tmpSupported) {
304 ASSERT_EQ(Error::NONE, tmpError) << "failed to check is supported";
305 supported = tmpSupported;
306 });
307 return supported;
308}
309
Marissa Wall88d87fa2019-11-05 14:57:51 -0800310Error Gralloc::get(const native_handle_t* bufferHandle, const IMapper::MetadataType& metadataType,
311 hidl_vec<uint8_t>* outVec) {
312 Error err;
313 mMapper->get(const_cast<native_handle_t*>(bufferHandle), metadataType,
314 [&](const auto& tmpError, const hidl_vec<uint8_t>& tmpVec) {
315 err = tmpError;
316 *outVec = tmpVec;
317 });
318 return err;
319}
320
321Error Gralloc::set(const native_handle_t* bufferHandle, const IMapper::MetadataType& metadataType,
322 const hidl_vec<uint8_t>& vec) {
323 return mMapper->set(const_cast<native_handle_t*>(bufferHandle), metadataType, vec);
324}
325
326Error Gralloc::getFromBufferDescriptorInfo(const IMapper::BufferDescriptorInfo& descriptorInfo,
327 const IMapper::MetadataType& metadataType,
328 hidl_vec<uint8_t>* outVec) {
329 Error err;
330 mMapper->getFromBufferDescriptorInfo(
331 descriptorInfo, metadataType,
332 [&](const auto& tmpError, const hidl_vec<uint8_t>& tmpVec) {
333 err = tmpError;
334 *outVec = tmpVec;
335 });
336 return err;
337}
338
Marissa Walld36e1272019-11-26 11:33:32 -0800339Error Gralloc::getReservedRegion(const native_handle_t* bufferHandle, void** outReservedRegion,
340 uint64_t* outReservedSize) {
341 Error err;
342 mMapper->getReservedRegion(
343 const_cast<native_handle_t*>(bufferHandle),
344 [&](const auto& tmpError, const auto& tmpReservedRegion, const auto& tmpReservedSize) {
345 err = tmpError;
346 *outReservedRegion = tmpReservedRegion;
347 *outReservedSize = tmpReservedSize;
348 });
349 return err;
350}
351
Marissa Wall65341642019-06-20 13:21:06 -0700352} // namespace vts
353} // namespace V4_0
354} // namespace mapper
355} // namespace graphics
356} // namespace hardware
357} // namespace android