Chia-I Wu | 89d09dd | 2017-02-24 10:41:35 -0800 | [diff] [blame] | 1 | /* |
| 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 Ma | ed2bb4e | 2017-03-10 00:44:45 -0800 | [diff] [blame] | 17 | #include <VtsHalHidlTargetTestBase.h> |
Chia-I Wu | 89d09dd | 2017-02-24 10:41:35 -0800 | [diff] [blame] | 18 | |
| 19 | #include "VtsHalGraphicsMapperTestUtils.h" |
| 20 | |
| 21 | namespace android { |
| 22 | namespace hardware { |
| 23 | namespace graphics { |
| 24 | namespace mapper { |
| 25 | namespace V2_0 { |
| 26 | namespace tests { |
| 27 | |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 28 | Gralloc::Gralloc() { |
| 29 | init(); |
Chia-I Wu | 89d09dd | 2017-02-24 10:41:35 -0800 | [diff] [blame] | 30 | } |
| 31 | |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 32 | void Gralloc::init() { |
Zhuoyao Zhang | 94b11ca | 2017-11-13 13:50:20 -0800 | [diff] [blame] | 33 | mAllocator = ::testing::VtsHalHidlTargetTestBase::getService<IAllocator>( |
| 34 | GraphicsMapperHidlEnvironment::Instance()->getServiceName<IAllocator>()); |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 35 | ASSERT_NE(nullptr, mAllocator.get()) << "failed to get allocator service"; |
| 36 | |
Zhuoyao Zhang | 94b11ca | 2017-11-13 13:50:20 -0800 | [diff] [blame] | 37 | mMapper = ::testing::VtsHalHidlTargetTestBase::getService<IMapper>( |
| 38 | GraphicsMapperHidlEnvironment::Instance()->getServiceName<IMapper>()); |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 39 | ASSERT_NE(nullptr, mMapper.get()) << "failed to get mapper service"; |
| 40 | ASSERT_FALSE(mMapper->isRemote()) << "mapper is not in passthrough mode"; |
| 41 | } |
| 42 | |
| 43 | Gralloc::~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 Wu | 89d09dd | 2017-02-24 10:41:35 -0800 | [diff] [blame] | 48 | } |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 49 | mClonedBuffers.clear(); |
| 50 | |
| 51 | for (auto bufferHandle : mImportedBuffers) { |
| 52 | auto buffer = const_cast<native_handle_t*>(bufferHandle); |
Chia-I Wu | 5255c35 | 2017-12-15 11:33:25 -0800 | [diff] [blame^] | 53 | EXPECT_EQ(Error::NONE, mMapper->freeBuffer(buffer)) << "failed to free buffer " << buffer; |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 54 | } |
| 55 | mImportedBuffers.clear(); |
Chia-I Wu | 89d09dd | 2017-02-24 10:41:35 -0800 | [diff] [blame] | 56 | } |
| 57 | |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 58 | sp<IAllocator> Gralloc::getAllocator() const { |
| 59 | return mAllocator; |
Chia-I Wu | 89d09dd | 2017-02-24 10:41:35 -0800 | [diff] [blame] | 60 | } |
| 61 | |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 62 | std::string Gralloc::dumpDebugInfo() { |
| 63 | std::string debugInfo; |
Chia-I Wu | 5255c35 | 2017-12-15 11:33:25 -0800 | [diff] [blame^] | 64 | mAllocator->dumpDebugInfo([&](const auto& tmpDebugInfo) { debugInfo = tmpDebugInfo.c_str(); }); |
Chia-I Wu | 89d09dd | 2017-02-24 10:41:35 -0800 | [diff] [blame] | 65 | |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 66 | return debugInfo; |
Chia-I Wu | 89d09dd | 2017-02-24 10:41:35 -0800 | [diff] [blame] | 67 | } |
| 68 | |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 69 | const native_handle_t* Gralloc::cloneBuffer(const hidl_handle& rawHandle) { |
Chia-I Wu | 5255c35 | 2017-12-15 11:33:25 -0800 | [diff] [blame^] | 70 | const native_handle_t* bufferHandle = native_handle_clone(rawHandle.getNativeHandle()); |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 71 | EXPECT_NE(nullptr, bufferHandle); |
Chia-I Wu | 89d09dd | 2017-02-24 10:41:35 -0800 | [diff] [blame] | 72 | |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 73 | if (bufferHandle) { |
| 74 | mClonedBuffers.insert(bufferHandle); |
| 75 | } |
| 76 | |
| 77 | return bufferHandle; |
Chia-I Wu | 89d09dd | 2017-02-24 10:41:35 -0800 | [diff] [blame] | 78 | } |
| 79 | |
Chia-I Wu | 5255c35 | 2017-12-15 11:33:25 -0800 | [diff] [blame^] | 80 | std::vector<const native_handle_t*> Gralloc::allocate(const BufferDescriptor& descriptor, |
| 81 | uint32_t count, bool import, |
| 82 | uint32_t* outStride) { |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 83 | std::vector<const native_handle_t*> bufferHandles; |
| 84 | bufferHandles.reserve(count); |
| 85 | mAllocator->allocate( |
Chia-I Wu | 5255c35 | 2017-12-15 11:33:25 -0800 | [diff] [blame^] | 86 | descriptor, count, |
| 87 | [&](const auto& tmpError, const auto& tmpStride, const auto& tmpBuffers) { |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 88 | ASSERT_EQ(Error::NONE, tmpError) << "failed to allocate buffers"; |
| 89 | ASSERT_EQ(count, tmpBuffers.size()) << "invalid buffer array"; |
Chia-I Wu | 89d09dd | 2017-02-24 10:41:35 -0800 | [diff] [blame] | 90 | |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 91 | for (uint32_t i = 0; i < count; i++) { |
| 92 | if (import) { |
Chia-I Wu | 5255c35 | 2017-12-15 11:33:25 -0800 | [diff] [blame^] | 93 | ASSERT_NO_FATAL_FAILURE(bufferHandles.push_back(importBuffer(tmpBuffers[i]))); |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 94 | } else { |
Chia-I Wu | 5255c35 | 2017-12-15 11:33:25 -0800 | [diff] [blame^] | 95 | ASSERT_NO_FATAL_FAILURE(bufferHandles.push_back(cloneBuffer(tmpBuffers[i]))); |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 96 | } |
| 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 Wu | 89d09dd | 2017-02-24 10:41:35 -0800 | [diff] [blame] | 109 | } |
| 110 | |
Chia-I Wu | 5255c35 | 2017-12-15 11:33:25 -0800 | [diff] [blame^] | 111 | const native_handle_t* Gralloc::allocate(const IMapper::BufferDescriptorInfo& descriptorInfo, |
| 112 | bool import, uint32_t* outStride) { |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 113 | BufferDescriptor descriptor = createDescriptor(descriptorInfo); |
| 114 | if (::testing::Test::HasFatalFailure()) { |
| 115 | return nullptr; |
| 116 | } |
Chia-I Wu | 89d09dd | 2017-02-24 10:41:35 -0800 | [diff] [blame] | 117 | |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 118 | auto buffers = allocate(descriptor, 1, import, outStride); |
| 119 | if (::testing::Test::HasFatalFailure()) { |
| 120 | return nullptr; |
| 121 | } |
| 122 | |
| 123 | return buffers[0]; |
Chia-I Wu | 89d09dd | 2017-02-24 10:41:35 -0800 | [diff] [blame] | 124 | } |
| 125 | |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 126 | sp<IMapper> Gralloc::getMapper() const { |
| 127 | return mMapper; |
Chia-I Wu | 89d09dd | 2017-02-24 10:41:35 -0800 | [diff] [blame] | 128 | } |
| 129 | |
Chia-I Wu | 5255c35 | 2017-12-15 11:33:25 -0800 | [diff] [blame^] | 130 | BufferDescriptor Gralloc::createDescriptor(const IMapper::BufferDescriptorInfo& descriptorInfo) { |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 131 | BufferDescriptor descriptor; |
Chia-I Wu | 5255c35 | 2017-12-15 11:33:25 -0800 | [diff] [blame^] | 132 | 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 Wu | 89d09dd | 2017-02-24 10:41:35 -0800 | [diff] [blame] | 136 | |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 137 | return descriptor; |
Chia-I Wu | 89d09dd | 2017-02-24 10:41:35 -0800 | [diff] [blame] | 138 | } |
| 139 | |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 140 | const native_handle_t* Gralloc::importBuffer(const hidl_handle& rawHandle) { |
| 141 | const native_handle_t* bufferHandle = nullptr; |
Chia-I Wu | 5255c35 | 2017-12-15 11:33:25 -0800 | [diff] [blame^] | 142 | 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 Wu | 89d09dd | 2017-02-24 10:41:35 -0800 | [diff] [blame] | 147 | |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 148 | if (bufferHandle) { |
| 149 | mImportedBuffers.insert(bufferHandle); |
| 150 | } |
| 151 | |
| 152 | return bufferHandle; |
Chia-I Wu | 89d09dd | 2017-02-24 10:41:35 -0800 | [diff] [blame] | 153 | } |
| 154 | |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 155 | void Gralloc::freeBuffer(const native_handle_t* bufferHandle) { |
| 156 | auto buffer = const_cast<native_handle_t*>(bufferHandle); |
Chia-I Wu | 89d09dd | 2017-02-24 10:41:35 -0800 | [diff] [blame] | 157 | |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 158 | 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 Wu | 89d09dd | 2017-02-24 10:41:35 -0800 | [diff] [blame] | 166 | } |
| 167 | |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 168 | void* 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 Wu | 89d09dd | 2017-02-24 10:41:35 -0800 | [diff] [blame] | 171 | |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 172 | 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 Wu | 89d09dd | 2017-02-24 10:41:35 -0800 | [diff] [blame] | 179 | |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 180 | void* data = nullptr; |
| 181 | mMapper->lock(buffer, cpuUsage, accessRegion, acquireFenceHandle, |
| 182 | [&](const auto& tmpError, const auto& tmpData) { |
Chia-I Wu | 5255c35 | 2017-12-15 11:33:25 -0800 | [diff] [blame^] | 183 | ASSERT_EQ(Error::NONE, tmpError) << "failed to lock buffer " << buffer; |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 184 | data = tmpData; |
| 185 | }); |
Chia-I Wu | 89d09dd | 2017-02-24 10:41:35 -0800 | [diff] [blame] | 186 | |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 187 | if (acquireFence >= 0) { |
| 188 | close(acquireFence); |
Chia-I Wu | 89d09dd | 2017-02-24 10:41:35 -0800 | [diff] [blame] | 189 | } |
Chia-I Wu | 89d09dd | 2017-02-24 10:41:35 -0800 | [diff] [blame] | 190 | |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 191 | return data; |
Chia-I Wu | 89d09dd | 2017-02-24 10:41:35 -0800 | [diff] [blame] | 192 | } |
| 193 | |
Chia-I Wu | 5255c35 | 2017-12-15 11:33:25 -0800 | [diff] [blame^] | 194 | YCbCrLayout Gralloc::lockYCbCr(const native_handle_t* bufferHandle, uint64_t cpuUsage, |
| 195 | const IMapper::Rect& accessRegion, int acquireFence) { |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 196 | auto buffer = const_cast<native_handle_t*>(bufferHandle); |
Chia-I Wu | 89d09dd | 2017-02-24 10:41:35 -0800 | [diff] [blame] | 197 | |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 198 | 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 Wu | 89d09dd | 2017-02-24 10:41:35 -0800 | [diff] [blame] | 205 | |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 206 | 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 Wu | 89d09dd | 2017-02-24 10:41:35 -0800 | [diff] [blame] | 213 | |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 214 | if (acquireFence >= 0) { |
| 215 | close(acquireFence); |
| 216 | } |
Chia-I Wu | 89d09dd | 2017-02-24 10:41:35 -0800 | [diff] [blame] | 217 | |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 218 | return layout; |
| 219 | } |
| 220 | |
| 221 | int Gralloc::unlock(const native_handle_t* bufferHandle) { |
| 222 | auto buffer = const_cast<native_handle_t*>(bufferHandle); |
| 223 | |
| 224 | int releaseFence = -1; |
Chia-I Wu | 5255c35 | 2017-12-15 11:33:25 -0800 | [diff] [blame^] | 225 | mMapper->unlock(buffer, [&](const auto& tmpError, const auto& tmpReleaseFence) { |
| 226 | ASSERT_EQ(Error::NONE, tmpError) << "failed to unlock buffer " << buffer; |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 227 | |
Chia-I Wu | 5255c35 | 2017-12-15 11:33:25 -0800 | [diff] [blame^] | 228 | 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 Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 236 | } |
Chia-I Wu | 5255c35 | 2017-12-15 11:33:25 -0800 | [diff] [blame^] | 237 | } |
| 238 | }); |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 239 | |
| 240 | return releaseFence; |
Chia-I Wu | 89d09dd | 2017-02-24 10:41:35 -0800 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | } // namespace tests |
| 244 | } // namespace V2_0 |
| 245 | } // namespace mapper |
| 246 | } // namespace graphics |
| 247 | } // namespace hardware |
| 248 | } // namespace android |