blob: e3229caf2ddd04ca33e9c79aa6d95398135622da [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() {
Zhuoyao Zhang94b11ca2017-11-13 13:50:20 -080033 mAllocator = ::testing::VtsHalHidlTargetTestBase::getService<IAllocator>(
34 GraphicsMapperHidlEnvironment::Instance()->getServiceName<IAllocator>());
Chia-I Wu79d13ff2017-03-31 12:48:11 -070035 ASSERT_NE(nullptr, mAllocator.get()) << "failed to get allocator service";
36
Zhuoyao Zhang94b11ca2017-11-13 13:50:20 -080037 mMapper = ::testing::VtsHalHidlTargetTestBase::getService<IMapper>(
38 GraphicsMapperHidlEnvironment::Instance()->getServiceName<IMapper>());
Chia-I Wu79d13ff2017-03-31 12:48:11 -070039 ASSERT_NE(nullptr, mMapper.get()) << "failed to get mapper service";
40 ASSERT_FALSE(mMapper->isRemote()) << "mapper is not in passthrough mode";
41}
42
43Gralloc::~Gralloc() {
44 for (auto bufferHandle : mClonedBuffers) {
45 auto buffer = const_cast<native_handle_t*>(bufferHandle);
46 native_handle_close(buffer);
47 native_handle_delete(buffer);
Chia-I Wu89d09dd2017-02-24 10:41:35 -080048 }
Chia-I Wu79d13ff2017-03-31 12:48:11 -070049 mClonedBuffers.clear();
50
51 for (auto bufferHandle : mImportedBuffers) {
52 auto buffer = const_cast<native_handle_t*>(bufferHandle);
Chia-I Wu5255c352017-12-15 11:33:25 -080053 EXPECT_EQ(Error::NONE, mMapper->freeBuffer(buffer)) << "failed to free buffer " << buffer;
Chia-I Wu79d13ff2017-03-31 12:48:11 -070054 }
55 mImportedBuffers.clear();
Chia-I Wu89d09dd2017-02-24 10:41:35 -080056}
57
Chia-I Wu79d13ff2017-03-31 12:48:11 -070058sp<IAllocator> Gralloc::getAllocator() const {
59 return mAllocator;
Chia-I Wu89d09dd2017-02-24 10:41:35 -080060}
61
Chia-I Wu79d13ff2017-03-31 12:48:11 -070062std::string Gralloc::dumpDebugInfo() {
63 std::string debugInfo;
Chia-I Wu5255c352017-12-15 11:33:25 -080064 mAllocator->dumpDebugInfo([&](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) {
Chia-I Wu5255c352017-12-15 11:33:25 -080070 const native_handle_t* bufferHandle = native_handle_clone(rawHandle.getNativeHandle());
Chia-I Wu79d13ff2017-03-31 12:48:11 -070071 EXPECT_NE(nullptr, bufferHandle);
Chia-I Wu89d09dd2017-02-24 10:41:35 -080072
Chia-I Wu79d13ff2017-03-31 12:48:11 -070073 if (bufferHandle) {
74 mClonedBuffers.insert(bufferHandle);
75 }
76
77 return bufferHandle;
Chia-I Wu89d09dd2017-02-24 10:41:35 -080078}
79
Chia-I Wu5255c352017-12-15 11:33:25 -080080std::vector<const native_handle_t*> Gralloc::allocate(const BufferDescriptor& descriptor,
81 uint32_t count, bool import,
82 uint32_t* outStride) {
Chia-I Wu79d13ff2017-03-31 12:48:11 -070083 std::vector<const native_handle_t*> bufferHandles;
84 bufferHandles.reserve(count);
85 mAllocator->allocate(
Chia-I Wu5255c352017-12-15 11:33:25 -080086 descriptor, count,
87 [&](const auto& tmpError, const auto& tmpStride, const auto& tmpBuffers) {
Chia-I Wu79d13ff2017-03-31 12:48:11 -070088 ASSERT_EQ(Error::NONE, tmpError) << "failed to allocate buffers";
89 ASSERT_EQ(count, tmpBuffers.size()) << "invalid buffer array";
Chia-I Wu89d09dd2017-02-24 10:41:35 -080090
Chia-I Wu79d13ff2017-03-31 12:48:11 -070091 for (uint32_t i = 0; i < count; i++) {
92 if (import) {
Chia-I Wu5255c352017-12-15 11:33:25 -080093 ASSERT_NO_FATAL_FAILURE(bufferHandles.push_back(importBuffer(tmpBuffers[i])));
Chia-I Wu79d13ff2017-03-31 12:48:11 -070094 } else {
Chia-I Wu5255c352017-12-15 11:33:25 -080095 ASSERT_NO_FATAL_FAILURE(bufferHandles.push_back(cloneBuffer(tmpBuffers[i])));
Chia-I Wu79d13ff2017-03-31 12:48:11 -070096 }
97 }
98
99 if (outStride) {
100 *outStride = tmpStride;
101 }
102 });
103
104 if (::testing::Test::HasFatalFailure()) {
105 bufferHandles.clear();
106 }
107
108 return bufferHandles;
Chia-I Wu89d09dd2017-02-24 10:41:35 -0800109}
110
Chia-I Wu5255c352017-12-15 11:33:25 -0800111const native_handle_t* Gralloc::allocate(const IMapper::BufferDescriptorInfo& descriptorInfo,
112 bool import, uint32_t* outStride) {
Chia-I Wu79d13ff2017-03-31 12:48:11 -0700113 BufferDescriptor descriptor = createDescriptor(descriptorInfo);
114 if (::testing::Test::HasFatalFailure()) {
115 return nullptr;
116 }
Chia-I Wu89d09dd2017-02-24 10:41:35 -0800117
Chia-I Wu79d13ff2017-03-31 12:48:11 -0700118 auto buffers = allocate(descriptor, 1, import, outStride);
119 if (::testing::Test::HasFatalFailure()) {
120 return nullptr;
121 }
122
123 return buffers[0];
Chia-I Wu89d09dd2017-02-24 10:41:35 -0800124}
125
Chia-I Wu79d13ff2017-03-31 12:48:11 -0700126sp<IMapper> Gralloc::getMapper() const {
127 return mMapper;
Chia-I Wu89d09dd2017-02-24 10:41:35 -0800128}
129
Chia-I Wu5255c352017-12-15 11:33:25 -0800130BufferDescriptor Gralloc::createDescriptor(const IMapper::BufferDescriptorInfo& descriptorInfo) {
Chia-I Wu79d13ff2017-03-31 12:48:11 -0700131 BufferDescriptor descriptor;
Chia-I Wu5255c352017-12-15 11:33:25 -0800132 mMapper->createDescriptor(descriptorInfo, [&](const auto& tmpError, const auto& tmpDescriptor) {
133 ASSERT_EQ(Error::NONE, tmpError) << "failed to create descriptor";
134 descriptor = tmpDescriptor;
135 });
Chia-I Wu89d09dd2017-02-24 10:41:35 -0800136
Chia-I Wu79d13ff2017-03-31 12:48:11 -0700137 return descriptor;
Chia-I Wu89d09dd2017-02-24 10:41:35 -0800138}
139
Chia-I Wu79d13ff2017-03-31 12:48:11 -0700140const native_handle_t* Gralloc::importBuffer(const hidl_handle& rawHandle) {
141 const native_handle_t* bufferHandle = nullptr;
Chia-I Wu5255c352017-12-15 11:33:25 -0800142 mMapper->importBuffer(rawHandle, [&](const auto& tmpError, const auto& tmpBuffer) {
143 ASSERT_EQ(Error::NONE, tmpError)
144 << "failed to import buffer %p" << rawHandle.getNativeHandle();
145 bufferHandle = static_cast<const native_handle_t*>(tmpBuffer);
146 });
Chia-I Wu89d09dd2017-02-24 10:41:35 -0800147
Chia-I Wu79d13ff2017-03-31 12:48:11 -0700148 if (bufferHandle) {
149 mImportedBuffers.insert(bufferHandle);
150 }
151
152 return bufferHandle;
Chia-I Wu89d09dd2017-02-24 10:41:35 -0800153}
154
Chia-I Wu79d13ff2017-03-31 12:48:11 -0700155void Gralloc::freeBuffer(const native_handle_t* bufferHandle) {
156 auto buffer = const_cast<native_handle_t*>(bufferHandle);
Chia-I Wu89d09dd2017-02-24 10:41:35 -0800157
Chia-I Wu79d13ff2017-03-31 12:48:11 -0700158 if (mImportedBuffers.erase(bufferHandle)) {
159 Error error = mMapper->freeBuffer(buffer);
160 ASSERT_EQ(Error::NONE, error) << "failed to free buffer " << buffer;
161 } else {
162 mClonedBuffers.erase(bufferHandle);
163 native_handle_close(buffer);
164 native_handle_delete(buffer);
165 }
Chia-I Wu89d09dd2017-02-24 10:41:35 -0800166}
167
Chia-I Wu79d13ff2017-03-31 12:48:11 -0700168void* Gralloc::lock(const native_handle_t* bufferHandle, uint64_t cpuUsage,
169 const IMapper::Rect& accessRegion, int acquireFence) {
170 auto buffer = const_cast<native_handle_t*>(bufferHandle);
Chia-I Wu89d09dd2017-02-24 10:41:35 -0800171
Chia-I Wu79d13ff2017-03-31 12:48:11 -0700172 NATIVE_HANDLE_DECLARE_STORAGE(acquireFenceStorage, 1, 0);
173 hidl_handle acquireFenceHandle;
174 if (acquireFence >= 0) {
175 auto h = native_handle_init(acquireFenceStorage, 1, 0);
176 h->data[0] = acquireFence;
177 acquireFenceHandle = h;
178 }
Chia-I Wu89d09dd2017-02-24 10:41:35 -0800179
Chia-I Wu79d13ff2017-03-31 12:48:11 -0700180 void* data = nullptr;
181 mMapper->lock(buffer, cpuUsage, accessRegion, acquireFenceHandle,
182 [&](const auto& tmpError, const auto& tmpData) {
Chia-I Wu5255c352017-12-15 11:33:25 -0800183 ASSERT_EQ(Error::NONE, tmpError) << "failed to lock buffer " << buffer;
Chia-I Wu79d13ff2017-03-31 12:48:11 -0700184 data = tmpData;
185 });
Chia-I Wu89d09dd2017-02-24 10:41:35 -0800186
Chia-I Wu79d13ff2017-03-31 12:48:11 -0700187 if (acquireFence >= 0) {
188 close(acquireFence);
Chia-I Wu89d09dd2017-02-24 10:41:35 -0800189 }
Chia-I Wu89d09dd2017-02-24 10:41:35 -0800190
Chia-I Wu79d13ff2017-03-31 12:48:11 -0700191 return data;
Chia-I Wu89d09dd2017-02-24 10:41:35 -0800192}
193
Chia-I Wu5255c352017-12-15 11:33:25 -0800194YCbCrLayout Gralloc::lockYCbCr(const native_handle_t* bufferHandle, uint64_t cpuUsage,
195 const IMapper::Rect& accessRegion, int acquireFence) {
Chia-I Wu79d13ff2017-03-31 12:48:11 -0700196 auto buffer = const_cast<native_handle_t*>(bufferHandle);
Chia-I Wu89d09dd2017-02-24 10:41:35 -0800197
Chia-I Wu79d13ff2017-03-31 12:48:11 -0700198 NATIVE_HANDLE_DECLARE_STORAGE(acquireFenceStorage, 1, 0);
199 hidl_handle acquireFenceHandle;
200 if (acquireFence >= 0) {
201 auto h = native_handle_init(acquireFenceStorage, 1, 0);
202 h->data[0] = acquireFence;
203 acquireFenceHandle = h;
204 }
Chia-I Wu89d09dd2017-02-24 10:41:35 -0800205
Chia-I Wu79d13ff2017-03-31 12:48:11 -0700206 YCbCrLayout layout = {};
207 mMapper->lockYCbCr(buffer, cpuUsage, accessRegion, acquireFenceHandle,
208 [&](const auto& tmpError, const auto& tmpLayout) {
209 ASSERT_EQ(Error::NONE, tmpError)
210 << "failed to lockYCbCr buffer " << buffer;
211 layout = tmpLayout;
212 });
Chia-I Wu89d09dd2017-02-24 10:41:35 -0800213
Chia-I Wu79d13ff2017-03-31 12:48:11 -0700214 if (acquireFence >= 0) {
215 close(acquireFence);
216 }
Chia-I Wu89d09dd2017-02-24 10:41:35 -0800217
Chia-I Wu79d13ff2017-03-31 12:48:11 -0700218 return layout;
219}
220
221int Gralloc::unlock(const native_handle_t* bufferHandle) {
222 auto buffer = const_cast<native_handle_t*>(bufferHandle);
223
224 int releaseFence = -1;
Chia-I Wu5255c352017-12-15 11:33:25 -0800225 mMapper->unlock(buffer, [&](const auto& tmpError, const auto& tmpReleaseFence) {
226 ASSERT_EQ(Error::NONE, tmpError) << "failed to unlock buffer " << buffer;
Chia-I Wu79d13ff2017-03-31 12:48:11 -0700227
Chia-I Wu5255c352017-12-15 11:33:25 -0800228 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;
Chia-I Wu79d13ff2017-03-31 12:48:11 -0700236 }
Chia-I Wu5255c352017-12-15 11:33:25 -0800237 }
238 });
Chia-I Wu79d13ff2017-03-31 12:48:11 -0700239
240 return releaseFence;
Chia-I Wu89d09dd2017-02-24 10:41:35 -0800241}
242
243} // namespace tests
244} // namespace V2_0
245} // namespace mapper
246} // namespace graphics
247} // namespace hardware
248} // namespace android