blob: 48e5736f8d0a613c94cf7ba2da4752f3731ad83e [file] [log] [blame]
Marissa Wallbd1ca512018-12-30 10:59:41 -08001/*
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
Alec Mouri18339992022-04-04 21:53:57 +000017#include <android-base/properties.h>
Marissa Wallbd1ca512018-12-30 10:59:41 -080018#include <mapper-vts/3.0/MapperVts.h>
Alec Mouri18339992022-04-04 21:53:57 +000019#include "gtest/gtest.h"
Marissa Wallbd1ca512018-12-30 10:59:41 -080020
Marissa Wallbd1ca512018-12-30 10:59:41 -080021namespace android {
22namespace hardware {
23namespace graphics {
24namespace mapper {
25namespace V3_0 {
26namespace vts {
27
Valerie Haudca469c2019-06-13 09:49:44 -070028Gralloc::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 }
Marissa Wallbd1ca512018-12-30 10:59:41 -080035}
36
37void Gralloc::init(const std::string& allocatorServiceName, const std::string& mapperServiceName) {
Dan Shi5a955bc2019-12-06 15:43:01 -080038 mAllocator = IAllocator::getService(allocatorServiceName);
Marissa Wallbd1ca512018-12-30 10:59:41 -080039 ASSERT_NE(nullptr, mAllocator.get()) << "failed to get allocator service";
40
Dan Shi5a955bc2019-12-06 15:43:01 -080041 mMapper = IMapper::getService(mapperServiceName);
Marissa Wallbd1ca512018-12-30 10:59:41 -080042 ASSERT_NE(nullptr, mMapper.get()) << "failed to get mapper service";
43 ASSERT_FALSE(mMapper->isRemote()) << "mapper is not in passthrough mode";
44}
45
Valerie Haudca469c2019-06-13 09:49:44 -070046void Gralloc::initNoErr(const std::string& allocatorServiceName,
47 const std::string& mapperServiceName) {
Dan Shi5a955bc2019-12-06 15:43:01 -080048 mAllocator = IAllocator::getService(allocatorServiceName);
Valerie Haudca469c2019-06-13 09:49:44 -070049
Dan Shi5a955bc2019-12-06 15:43:01 -080050 mMapper = IMapper::getService(mapperServiceName);
Valerie Haudca469c2019-06-13 09:49:44 -070051 if (mMapper.get()) {
52 ASSERT_FALSE(mMapper->isRemote()) << "mapper is not in passthrough mode";
53 }
54}
55
Marissa Wallbd1ca512018-12-30 10:59:41 -080056Gralloc::~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
71sp<IAllocator> Gralloc::getAllocator() const {
72 return mAllocator;
73}
74
75std::string Gralloc::dumpDebugInfo() {
76 std::string debugInfo;
77 mAllocator->dumpDebugInfo([&](const auto& tmpDebugInfo) { debugInfo = tmpDebugInfo.c_str(); });
78
79 return debugInfo;
80}
81
82const 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
93std::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(
Alec Mouri18339992022-04-04 21:53:57 +000099 descriptor, count,
100 [&](const auto& tmpError, const auto& tmpStride, const auto& tmpBuffers) {
101 if (tmpError != Error::NONE) {
Alec Mouri960c14d2023-01-30 22:09:42 +0000102 GTEST_FAIL() << "failed to allocate buffers";
Marissa Wallbd1ca512018-12-30 10:59:41 -0800103 }
Alec Mouri18339992022-04-04 21:53:57 +0000104 ASSERT_EQ(count, tmpBuffers.size()) << "invalid buffer array";
Marissa Wallbd1ca512018-12-30 10:59:41 -0800105
Alec Mouri18339992022-04-04 21:53:57 +0000106 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 });
Marissa Wallbd1ca512018-12-30 10:59:41 -0800120
121 if (::testing::Test::HasFatalFailure()) {
122 bufferHandles.clear();
123 }
124
125 return bufferHandles;
126}
127
128const 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);
Alec Mouri18339992022-04-04 21:53:57 +0000136 if (::testing::Test::HasFatalFailure() || ::testing::Test::IsSkipped()) {
Marissa Wallbd1ca512018-12-30 10:59:41 -0800137 return nullptr;
138 }
139
140 return buffers[0];
141}
142
143sp<IMapper> Gralloc::getMapper() const {
144 return mMapper;
145}
146
147BufferDescriptor 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
157const 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
172void 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
185void* Gralloc::lock(const native_handle_t* bufferHandle, uint64_t cpuUsage,
Marissa Wall69292fa2018-12-30 12:37:18 -0800186 const IMapper::Rect& accessRegion, int acquireFence, int32_t* outBytesPerPixel,
187 int32_t* outBytesPerStride) {
Marissa Wallbd1ca512018-12-30 10:59:41 -0800188 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
Marissa Walla6a30b12018-12-30 12:48:46 -0800198 *outBytesPerPixel = -1;
199 *outBytesPerStride = -1;
200
Marissa Wallbd1ca512018-12-30 10:59:41 -0800201 void* data = nullptr;
202 mMapper->lock(buffer, cpuUsage, accessRegion, acquireFenceHandle,
Marissa Wall69292fa2018-12-30 12:37:18 -0800203 [&](const auto& tmpError, const auto& tmpData, int32_t tmpBytesPerPixel,
204 int32_t tmpBytesPerStride) {
Marissa Wallbd1ca512018-12-30 10:59:41 -0800205 ASSERT_EQ(Error::NONE, tmpError) << "failed to lock buffer " << buffer;
206 data = tmpData;
Marissa Wall69292fa2018-12-30 12:37:18 -0800207 *outBytesPerPixel = tmpBytesPerPixel;
208 *outBytesPerStride = tmpBytesPerStride;
Marissa Wallbd1ca512018-12-30 10:59:41 -0800209 });
210
211 if (acquireFence >= 0) {
212 close(acquireFence);
213 }
214
215 return data;
216}
217
218YCbCrLayout 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
245int 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
267bool 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
276void 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
Marissa Walla6a30b12018-12-30 12:48:46 -0800293bool 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
Marissa Wallbd1ca512018-12-30 10:59:41 -0800302} // namespace vts
303} // namespace V3_0
304} // namespace mapper
305} // namespace graphics
306} // namespace hardware
307} // namespace android