blob: c534889fcac4c062cf0c3f657049ebbce5efd95d [file] [log] [blame]
Chia-I Wu89d09dd2017-02-24 10:41:35 -08001/*
2 * Copyright (C) 2017 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
Yuexi Maed2bb4e2017-03-10 00:44:45 -080017#include <VtsHalHidlTargetTestBase.h>
Chia-I Wu89d09dd2017-02-24 10:41:35 -080018
19#include "VtsHalGraphicsMapperTestUtils.h"
20
21namespace android {
22namespace hardware {
23namespace graphics {
24namespace mapper {
25namespace V2_0 {
26namespace tests {
27
Chia-I Wu79d13ff2017-03-31 12:48:11 -070028Gralloc::Gralloc() {
29 init();
Chia-I Wu89d09dd2017-02-24 10:41:35 -080030}
31
Chia-I Wu79d13ff2017-03-31 12:48:11 -070032void Gralloc::init() {
33 mAllocator = ::testing::VtsHalHidlTargetTestBase::getService<IAllocator>();
34 ASSERT_NE(nullptr, mAllocator.get()) << "failed to get allocator service";
35
36 mMapper = ::testing::VtsHalHidlTargetTestBase::getService<IMapper>();
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);
Chia-I Wu89d09dd2017-02-24 10:41:35 -080046 }
Chia-I Wu79d13ff2017-03-31 12:48:11 -070047 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))
52 << "failed to free buffer " << buffer;
53 }
54 mImportedBuffers.clear();
Chia-I Wu89d09dd2017-02-24 10:41:35 -080055}
56
Chia-I Wu79d13ff2017-03-31 12:48:11 -070057sp<IAllocator> Gralloc::getAllocator() const {
58 return mAllocator;
Chia-I Wu89d09dd2017-02-24 10:41:35 -080059}
60
Chia-I Wu79d13ff2017-03-31 12:48:11 -070061std::string Gralloc::dumpDebugInfo() {
62 std::string debugInfo;
63 mAllocator->dumpDebugInfo(
64 [&](const auto& tmpDebugInfo) { debugInfo = tmpDebugInfo.c_str(); });
Chia-I Wu89d09dd2017-02-24 10:41:35 -080065
Chia-I Wu79d13ff2017-03-31 12:48:11 -070066 return debugInfo;
Chia-I Wu89d09dd2017-02-24 10:41:35 -080067}
68
Chia-I Wu79d13ff2017-03-31 12:48:11 -070069const native_handle_t* Gralloc::cloneBuffer(const hidl_handle& rawHandle) {
70 const native_handle_t* bufferHandle =
71 native_handle_clone(rawHandle.getNativeHandle());
72 EXPECT_NE(nullptr, bufferHandle);
Chia-I Wu89d09dd2017-02-24 10:41:35 -080073
Chia-I Wu79d13ff2017-03-31 12:48:11 -070074 if (bufferHandle) {
75 mClonedBuffers.insert(bufferHandle);
76 }
77
78 return bufferHandle;
Chia-I Wu89d09dd2017-02-24 10:41:35 -080079}
80
Chia-I Wu79d13ff2017-03-31 12:48:11 -070081std::vector<const native_handle_t*> Gralloc::allocate(
82 const BufferDescriptor& descriptor, uint32_t count, bool import,
83 uint32_t* outStride) {
84 std::vector<const native_handle_t*> bufferHandles;
85 bufferHandles.reserve(count);
86 mAllocator->allocate(
87 descriptor, count, [&](const auto& tmpError, const auto& tmpStride,
88 const auto& tmpBuffers) {
89 ASSERT_EQ(Error::NONE, tmpError) << "failed to allocate buffers";
90 ASSERT_EQ(count, tmpBuffers.size()) << "invalid buffer array";
Chia-I Wu89d09dd2017-02-24 10:41:35 -080091
Chia-I Wu79d13ff2017-03-31 12:48:11 -070092 for (uint32_t i = 0; i < count; i++) {
93 if (import) {
94 ASSERT_NO_FATAL_FAILURE(
95 bufferHandles.push_back(importBuffer(tmpBuffers[i])));
96 } else {
97 ASSERT_NO_FATAL_FAILURE(
98 bufferHandles.push_back(cloneBuffer(tmpBuffers[i])));
99 }
100 }
101
102 if (outStride) {
103 *outStride = tmpStride;
104 }
105 });
106
107 if (::testing::Test::HasFatalFailure()) {
108 bufferHandles.clear();
109 }
110
111 return bufferHandles;
Chia-I Wu89d09dd2017-02-24 10:41:35 -0800112}
113
Chia-I Wu79d13ff2017-03-31 12:48:11 -0700114const native_handle_t* Gralloc::allocate(
115 const IMapper::BufferDescriptorInfo& descriptorInfo, bool import,
116 uint32_t* outStride) {
117 BufferDescriptor descriptor = createDescriptor(descriptorInfo);
118 if (::testing::Test::HasFatalFailure()) {
119 return nullptr;
120 }
Chia-I Wu89d09dd2017-02-24 10:41:35 -0800121
Chia-I Wu79d13ff2017-03-31 12:48:11 -0700122 auto buffers = allocate(descriptor, 1, import, outStride);
123 if (::testing::Test::HasFatalFailure()) {
124 return nullptr;
125 }
126
127 return buffers[0];
Chia-I Wu89d09dd2017-02-24 10:41:35 -0800128}
129
Chia-I Wu79d13ff2017-03-31 12:48:11 -0700130sp<IMapper> Gralloc::getMapper() const {
131 return mMapper;
Chia-I Wu89d09dd2017-02-24 10:41:35 -0800132}
133
Chia-I Wu79d13ff2017-03-31 12:48:11 -0700134BufferDescriptor Gralloc::createDescriptor(
135 const IMapper::BufferDescriptorInfo& descriptorInfo) {
136 BufferDescriptor descriptor;
137 mMapper->createDescriptor(
138 descriptorInfo, [&](const auto& tmpError, const auto& tmpDescriptor) {
139 ASSERT_EQ(Error::NONE, tmpError) << "failed to create descriptor";
140 descriptor = tmpDescriptor;
141 });
Chia-I Wu89d09dd2017-02-24 10:41:35 -0800142
Chia-I Wu79d13ff2017-03-31 12:48:11 -0700143 return descriptor;
Chia-I Wu89d09dd2017-02-24 10:41:35 -0800144}
145
Chia-I Wu79d13ff2017-03-31 12:48:11 -0700146const native_handle_t* Gralloc::importBuffer(const hidl_handle& rawHandle) {
147 const native_handle_t* bufferHandle = nullptr;
148 mMapper->importBuffer(
149 rawHandle, [&](const auto& tmpError, const auto& tmpBuffer) {
150 ASSERT_EQ(Error::NONE, tmpError) << "failed to import buffer %p"
151 << rawHandle.getNativeHandle();
152 bufferHandle = static_cast<const native_handle_t*>(tmpBuffer);
153 });
Chia-I Wu89d09dd2017-02-24 10:41:35 -0800154
Chia-I Wu79d13ff2017-03-31 12:48:11 -0700155 if (bufferHandle) {
156 mImportedBuffers.insert(bufferHandle);
157 }
158
159 return bufferHandle;
Chia-I Wu89d09dd2017-02-24 10:41:35 -0800160}
161
Chia-I Wu79d13ff2017-03-31 12:48:11 -0700162void Gralloc::freeBuffer(const native_handle_t* bufferHandle) {
163 auto buffer = const_cast<native_handle_t*>(bufferHandle);
Chia-I Wu89d09dd2017-02-24 10:41:35 -0800164
Chia-I Wu79d13ff2017-03-31 12:48:11 -0700165 if (mImportedBuffers.erase(bufferHandle)) {
166 Error error = mMapper->freeBuffer(buffer);
167 ASSERT_EQ(Error::NONE, error) << "failed to free buffer " << buffer;
168 } else {
169 mClonedBuffers.erase(bufferHandle);
170 native_handle_close(buffer);
171 native_handle_delete(buffer);
172 }
Chia-I Wu89d09dd2017-02-24 10:41:35 -0800173}
174
Chia-I Wu79d13ff2017-03-31 12:48:11 -0700175void* Gralloc::lock(const native_handle_t* bufferHandle, uint64_t cpuUsage,
176 const IMapper::Rect& accessRegion, int acquireFence) {
177 auto buffer = const_cast<native_handle_t*>(bufferHandle);
Chia-I Wu89d09dd2017-02-24 10:41:35 -0800178
Chia-I Wu79d13ff2017-03-31 12:48:11 -0700179 NATIVE_HANDLE_DECLARE_STORAGE(acquireFenceStorage, 1, 0);
180 hidl_handle acquireFenceHandle;
181 if (acquireFence >= 0) {
182 auto h = native_handle_init(acquireFenceStorage, 1, 0);
183 h->data[0] = acquireFence;
184 acquireFenceHandle = h;
185 }
Chia-I Wu89d09dd2017-02-24 10:41:35 -0800186
Chia-I Wu79d13ff2017-03-31 12:48:11 -0700187 void* data = nullptr;
188 mMapper->lock(buffer, cpuUsage, accessRegion, acquireFenceHandle,
189 [&](const auto& tmpError, const auto& tmpData) {
Chia-I Wu89d09dd2017-02-24 10:41:35 -0800190 ASSERT_EQ(Error::NONE, tmpError)
Chia-I Wu79d13ff2017-03-31 12:48:11 -0700191 << "failed to lock buffer " << buffer;
192 data = tmpData;
193 });
Chia-I Wu89d09dd2017-02-24 10:41:35 -0800194
Chia-I Wu79d13ff2017-03-31 12:48:11 -0700195 if (acquireFence >= 0) {
196 close(acquireFence);
Chia-I Wu89d09dd2017-02-24 10:41:35 -0800197 }
Chia-I Wu89d09dd2017-02-24 10:41:35 -0800198
Chia-I Wu79d13ff2017-03-31 12:48:11 -0700199 return data;
Chia-I Wu89d09dd2017-02-24 10:41:35 -0800200}
201
Chia-I Wu79d13ff2017-03-31 12:48:11 -0700202YCbCrLayout Gralloc::lockYCbCr(const native_handle_t* bufferHandle,
203 uint64_t cpuUsage,
204 const IMapper::Rect& accessRegion,
205 int acquireFence) {
206 auto buffer = const_cast<native_handle_t*>(bufferHandle);
Chia-I Wu89d09dd2017-02-24 10:41:35 -0800207
Chia-I Wu79d13ff2017-03-31 12:48:11 -0700208 NATIVE_HANDLE_DECLARE_STORAGE(acquireFenceStorage, 1, 0);
209 hidl_handle acquireFenceHandle;
210 if (acquireFence >= 0) {
211 auto h = native_handle_init(acquireFenceStorage, 1, 0);
212 h->data[0] = acquireFence;
213 acquireFenceHandle = h;
214 }
Chia-I Wu89d09dd2017-02-24 10:41:35 -0800215
Chia-I Wu79d13ff2017-03-31 12:48:11 -0700216 YCbCrLayout layout = {};
217 mMapper->lockYCbCr(buffer, cpuUsage, accessRegion, acquireFenceHandle,
218 [&](const auto& tmpError, const auto& tmpLayout) {
219 ASSERT_EQ(Error::NONE, tmpError)
220 << "failed to lockYCbCr buffer " << buffer;
221 layout = tmpLayout;
222 });
Chia-I Wu89d09dd2017-02-24 10:41:35 -0800223
Chia-I Wu79d13ff2017-03-31 12:48:11 -0700224 if (acquireFence >= 0) {
225 close(acquireFence);
226 }
Chia-I Wu89d09dd2017-02-24 10:41:35 -0800227
Chia-I Wu79d13ff2017-03-31 12:48:11 -0700228 return layout;
229}
230
231int Gralloc::unlock(const native_handle_t* bufferHandle) {
232 auto buffer = const_cast<native_handle_t*>(bufferHandle);
233
234 int releaseFence = -1;
235 mMapper->unlock(
236 buffer, [&](const auto& tmpError, const auto& tmpReleaseFence) {
237 ASSERT_EQ(Error::NONE, tmpError) << "failed to unlock buffer "
238 << buffer;
239
240 auto fenceHandle = tmpReleaseFence.getNativeHandle();
241 if (fenceHandle) {
242 ASSERT_EQ(0, fenceHandle->numInts) << "invalid fence handle "
243 << fenceHandle;
244 if (fenceHandle->numFds == 1) {
245 releaseFence = dup(fenceHandle->data[0]);
246 ASSERT_LT(0, releaseFence) << "failed to dup fence fd";
247 } else {
248 ASSERT_EQ(0, fenceHandle->numFds)
249 << " invalid fence handle " << fenceHandle;
250 }
251 }
252 });
253
254 return releaseFence;
Chia-I Wu89d09dd2017-02-24 10:41:35 -0800255}
256
257} // namespace tests
258} // namespace V2_0
259} // namespace mapper
260} // namespace graphics
261} // namespace hardware
262} // namespace android