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