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 | |
Chia-I Wu | 98923fc | 2018-01-14 21:45:18 -0800 | [diff] [blame] | 17 | #include <mapper-vts/2.0/MapperVts.h> |
Chia-I Wu | 89d09dd | 2017-02-24 10:41:35 -0800 | [diff] [blame] | 18 | |
Chia-I Wu | 98923fc | 2018-01-14 21:45:18 -0800 | [diff] [blame] | 19 | #include <VtsHalHidlTargetTestBase.h> |
Chia-I Wu | 89d09dd | 2017-02-24 10:41:35 -0800 | [diff] [blame] | 20 | |
| 21 | namespace android { |
| 22 | namespace hardware { |
| 23 | namespace graphics { |
| 24 | namespace mapper { |
| 25 | namespace V2_0 { |
Chia-I Wu | 98923fc | 2018-01-14 21:45:18 -0800 | [diff] [blame] | 26 | namespace vts { |
Chia-I Wu | 89d09dd | 2017-02-24 10:41:35 -0800 | [diff] [blame] | 27 | |
Chia-I Wu | 98923fc | 2018-01-14 21:45:18 -0800 | [diff] [blame] | 28 | Gralloc::Gralloc(const std::string& allocatorServiceName, const std::string& mapperServiceName) { |
| 29 | init(allocatorServiceName, mapperServiceName); |
Chia-I Wu | 89d09dd | 2017-02-24 10:41:35 -0800 | [diff] [blame] | 30 | } |
| 31 | |
Chia-I Wu | 98923fc | 2018-01-14 21:45:18 -0800 | [diff] [blame] | 32 | void Gralloc::init(const std::string& allocatorServiceName, const std::string& mapperServiceName) { |
| 33 | mAllocator = ::testing::VtsHalHidlTargetTestBase::getService<IAllocator>(allocatorServiceName); |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 34 | ASSERT_NE(nullptr, mAllocator.get()) << "failed to get allocator service"; |
| 35 | |
Chia-I Wu | 98923fc | 2018-01-14 21:45:18 -0800 | [diff] [blame] | 36 | mMapper = ::testing::VtsHalHidlTargetTestBase::getService<IMapper>(mapperServiceName); |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 37 | ASSERT_NE(nullptr, mMapper.get()) << "failed to get mapper service"; |
| 38 | ASSERT_FALSE(mMapper->isRemote()) << "mapper is not in passthrough mode"; |
| 39 | } |
| 40 | |
| 41 | Gralloc::~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 Wu | 89d09dd | 2017-02-24 10:41:35 -0800 | [diff] [blame] | 46 | } |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 47 | mClonedBuffers.clear(); |
| 48 | |
| 49 | for (auto bufferHandle : mImportedBuffers) { |
| 50 | auto buffer = const_cast<native_handle_t*>(bufferHandle); |
Chia-I Wu | 5255c35 | 2017-12-15 11:33:25 -0800 | [diff] [blame] | 51 | 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] | 52 | } |
| 53 | mImportedBuffers.clear(); |
Chia-I Wu | 89d09dd | 2017-02-24 10:41:35 -0800 | [diff] [blame] | 54 | } |
| 55 | |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 56 | sp<IAllocator> Gralloc::getAllocator() const { |
| 57 | return mAllocator; |
Chia-I Wu | 89d09dd | 2017-02-24 10:41:35 -0800 | [diff] [blame] | 58 | } |
| 59 | |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 60 | std::string Gralloc::dumpDebugInfo() { |
| 61 | std::string debugInfo; |
Chia-I Wu | 5255c35 | 2017-12-15 11:33:25 -0800 | [diff] [blame] | 62 | mAllocator->dumpDebugInfo([&](const auto& tmpDebugInfo) { debugInfo = tmpDebugInfo.c_str(); }); |
Chia-I Wu | 89d09dd | 2017-02-24 10:41:35 -0800 | [diff] [blame] | 63 | |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 64 | return debugInfo; |
Chia-I Wu | 89d09dd | 2017-02-24 10:41:35 -0800 | [diff] [blame] | 65 | } |
| 66 | |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 67 | const native_handle_t* Gralloc::cloneBuffer(const hidl_handle& rawHandle) { |
Chia-I Wu | 5255c35 | 2017-12-15 11:33:25 -0800 | [diff] [blame] | 68 | const native_handle_t* bufferHandle = native_handle_clone(rawHandle.getNativeHandle()); |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 69 | EXPECT_NE(nullptr, bufferHandle); |
Chia-I Wu | 89d09dd | 2017-02-24 10:41:35 -0800 | [diff] [blame] | 70 | |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 71 | if (bufferHandle) { |
| 72 | mClonedBuffers.insert(bufferHandle); |
| 73 | } |
| 74 | |
| 75 | return bufferHandle; |
Chia-I Wu | 89d09dd | 2017-02-24 10:41:35 -0800 | [diff] [blame] | 76 | } |
| 77 | |
Chia-I Wu | 5255c35 | 2017-12-15 11:33:25 -0800 | [diff] [blame] | 78 | std::vector<const native_handle_t*> Gralloc::allocate(const BufferDescriptor& descriptor, |
| 79 | uint32_t count, bool import, |
| 80 | uint32_t* outStride) { |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 81 | std::vector<const native_handle_t*> bufferHandles; |
| 82 | bufferHandles.reserve(count); |
| 83 | mAllocator->allocate( |
Chia-I Wu | 5255c35 | 2017-12-15 11:33:25 -0800 | [diff] [blame] | 84 | descriptor, count, |
| 85 | [&](const auto& tmpError, const auto& tmpStride, const auto& tmpBuffers) { |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 86 | ASSERT_EQ(Error::NONE, tmpError) << "failed to allocate buffers"; |
| 87 | ASSERT_EQ(count, tmpBuffers.size()) << "invalid buffer array"; |
Chia-I Wu | 89d09dd | 2017-02-24 10:41:35 -0800 | [diff] [blame] | 88 | |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 89 | for (uint32_t i = 0; i < count; i++) { |
| 90 | if (import) { |
Chia-I Wu | 5255c35 | 2017-12-15 11:33:25 -0800 | [diff] [blame] | 91 | ASSERT_NO_FATAL_FAILURE(bufferHandles.push_back(importBuffer(tmpBuffers[i]))); |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 92 | } else { |
Chia-I Wu | 5255c35 | 2017-12-15 11:33:25 -0800 | [diff] [blame] | 93 | ASSERT_NO_FATAL_FAILURE(bufferHandles.push_back(cloneBuffer(tmpBuffers[i]))); |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 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; |
Chia-I Wu | 89d09dd | 2017-02-24 10:41:35 -0800 | [diff] [blame] | 107 | } |
| 108 | |
Chia-I Wu | 5255c35 | 2017-12-15 11:33:25 -0800 | [diff] [blame] | 109 | const native_handle_t* Gralloc::allocate(const IMapper::BufferDescriptorInfo& descriptorInfo, |
| 110 | bool import, uint32_t* outStride) { |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 111 | BufferDescriptor descriptor = createDescriptor(descriptorInfo); |
| 112 | if (::testing::Test::HasFatalFailure()) { |
| 113 | return nullptr; |
| 114 | } |
Chia-I Wu | 89d09dd | 2017-02-24 10:41:35 -0800 | [diff] [blame] | 115 | |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 116 | auto buffers = allocate(descriptor, 1, import, outStride); |
| 117 | if (::testing::Test::HasFatalFailure()) { |
| 118 | return nullptr; |
| 119 | } |
| 120 | |
| 121 | return buffers[0]; |
Chia-I Wu | 89d09dd | 2017-02-24 10:41:35 -0800 | [diff] [blame] | 122 | } |
| 123 | |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 124 | sp<IMapper> Gralloc::getMapper() const { |
| 125 | return mMapper; |
Chia-I Wu | 89d09dd | 2017-02-24 10:41:35 -0800 | [diff] [blame] | 126 | } |
| 127 | |
Chia-I Wu | 5255c35 | 2017-12-15 11:33:25 -0800 | [diff] [blame] | 128 | BufferDescriptor Gralloc::createDescriptor(const IMapper::BufferDescriptorInfo& descriptorInfo) { |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 129 | BufferDescriptor descriptor; |
Chia-I Wu | 5255c35 | 2017-12-15 11:33:25 -0800 | [diff] [blame] | 130 | mMapper->createDescriptor(descriptorInfo, [&](const auto& tmpError, const auto& tmpDescriptor) { |
| 131 | ASSERT_EQ(Error::NONE, tmpError) << "failed to create descriptor"; |
| 132 | descriptor = tmpDescriptor; |
| 133 | }); |
Chia-I Wu | 89d09dd | 2017-02-24 10:41:35 -0800 | [diff] [blame] | 134 | |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 135 | return descriptor; |
Chia-I Wu | 89d09dd | 2017-02-24 10:41:35 -0800 | [diff] [blame] | 136 | } |
| 137 | |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 138 | const native_handle_t* Gralloc::importBuffer(const hidl_handle& rawHandle) { |
| 139 | const native_handle_t* bufferHandle = nullptr; |
Chia-I Wu | 5255c35 | 2017-12-15 11:33:25 -0800 | [diff] [blame] | 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 | }); |
Chia-I Wu | 89d09dd | 2017-02-24 10:41:35 -0800 | [diff] [blame] | 145 | |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 146 | if (bufferHandle) { |
| 147 | mImportedBuffers.insert(bufferHandle); |
| 148 | } |
| 149 | |
| 150 | return bufferHandle; |
Chia-I Wu | 89d09dd | 2017-02-24 10:41:35 -0800 | [diff] [blame] | 151 | } |
| 152 | |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 153 | void Gralloc::freeBuffer(const native_handle_t* bufferHandle) { |
| 154 | auto buffer = const_cast<native_handle_t*>(bufferHandle); |
Chia-I Wu | 89d09dd | 2017-02-24 10:41:35 -0800 | [diff] [blame] | 155 | |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 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 | } |
Chia-I Wu | 89d09dd | 2017-02-24 10:41:35 -0800 | [diff] [blame] | 164 | } |
| 165 | |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 166 | void* Gralloc::lock(const native_handle_t* bufferHandle, uint64_t cpuUsage, |
| 167 | const IMapper::Rect& accessRegion, int acquireFence) { |
| 168 | auto buffer = const_cast<native_handle_t*>(bufferHandle); |
Chia-I Wu | 89d09dd | 2017-02-24 10:41:35 -0800 | [diff] [blame] | 169 | |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 170 | NATIVE_HANDLE_DECLARE_STORAGE(acquireFenceStorage, 1, 0); |
| 171 | hidl_handle acquireFenceHandle; |
| 172 | if (acquireFence >= 0) { |
| 173 | auto h = native_handle_init(acquireFenceStorage, 1, 0); |
| 174 | h->data[0] = acquireFence; |
| 175 | acquireFenceHandle = h; |
| 176 | } |
Chia-I Wu | 89d09dd | 2017-02-24 10:41:35 -0800 | [diff] [blame] | 177 | |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 178 | void* data = nullptr; |
| 179 | mMapper->lock(buffer, cpuUsage, accessRegion, acquireFenceHandle, |
| 180 | [&](const auto& tmpError, const auto& tmpData) { |
Chia-I Wu | 5255c35 | 2017-12-15 11:33:25 -0800 | [diff] [blame] | 181 | ASSERT_EQ(Error::NONE, tmpError) << "failed to lock buffer " << buffer; |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 182 | data = tmpData; |
| 183 | }); |
Chia-I Wu | 89d09dd | 2017-02-24 10:41:35 -0800 | [diff] [blame] | 184 | |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 185 | if (acquireFence >= 0) { |
| 186 | close(acquireFence); |
Chia-I Wu | 89d09dd | 2017-02-24 10:41:35 -0800 | [diff] [blame] | 187 | } |
Chia-I Wu | 89d09dd | 2017-02-24 10:41:35 -0800 | [diff] [blame] | 188 | |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 189 | return data; |
Chia-I Wu | 89d09dd | 2017-02-24 10:41:35 -0800 | [diff] [blame] | 190 | } |
| 191 | |
Chia-I Wu | 5255c35 | 2017-12-15 11:33:25 -0800 | [diff] [blame] | 192 | YCbCrLayout Gralloc::lockYCbCr(const native_handle_t* bufferHandle, uint64_t cpuUsage, |
| 193 | const IMapper::Rect& accessRegion, int acquireFence) { |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 194 | auto buffer = const_cast<native_handle_t*>(bufferHandle); |
Chia-I Wu | 89d09dd | 2017-02-24 10:41:35 -0800 | [diff] [blame] | 195 | |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 196 | NATIVE_HANDLE_DECLARE_STORAGE(acquireFenceStorage, 1, 0); |
| 197 | hidl_handle acquireFenceHandle; |
| 198 | if (acquireFence >= 0) { |
| 199 | auto h = native_handle_init(acquireFenceStorage, 1, 0); |
| 200 | h->data[0] = acquireFence; |
| 201 | acquireFenceHandle = h; |
| 202 | } |
Chia-I Wu | 89d09dd | 2017-02-24 10:41:35 -0800 | [diff] [blame] | 203 | |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 204 | YCbCrLayout layout = {}; |
| 205 | mMapper->lockYCbCr(buffer, cpuUsage, accessRegion, acquireFenceHandle, |
| 206 | [&](const auto& tmpError, const auto& tmpLayout) { |
| 207 | ASSERT_EQ(Error::NONE, tmpError) |
| 208 | << "failed to lockYCbCr buffer " << buffer; |
| 209 | layout = tmpLayout; |
| 210 | }); |
Chia-I Wu | 89d09dd | 2017-02-24 10:41:35 -0800 | [diff] [blame] | 211 | |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 212 | if (acquireFence >= 0) { |
| 213 | close(acquireFence); |
| 214 | } |
Chia-I Wu | 89d09dd | 2017-02-24 10:41:35 -0800 | [diff] [blame] | 215 | |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 216 | return layout; |
| 217 | } |
| 218 | |
| 219 | int Gralloc::unlock(const native_handle_t* bufferHandle) { |
| 220 | auto buffer = const_cast<native_handle_t*>(bufferHandle); |
| 221 | |
| 222 | int releaseFence = -1; |
Chia-I Wu | 5255c35 | 2017-12-15 11:33:25 -0800 | [diff] [blame] | 223 | mMapper->unlock(buffer, [&](const auto& tmpError, const auto& tmpReleaseFence) { |
| 224 | ASSERT_EQ(Error::NONE, tmpError) << "failed to unlock buffer " << buffer; |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 225 | |
Chia-I Wu | 5255c35 | 2017-12-15 11:33:25 -0800 | [diff] [blame] | 226 | auto fenceHandle = tmpReleaseFence.getNativeHandle(); |
| 227 | if (fenceHandle) { |
| 228 | ASSERT_EQ(0, fenceHandle->numInts) << "invalid fence handle " << fenceHandle; |
| 229 | if (fenceHandle->numFds == 1) { |
| 230 | releaseFence = dup(fenceHandle->data[0]); |
| 231 | ASSERT_LT(0, releaseFence) << "failed to dup fence fd"; |
| 232 | } else { |
| 233 | ASSERT_EQ(0, fenceHandle->numFds) << " invalid fence handle " << fenceHandle; |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 234 | } |
Chia-I Wu | 5255c35 | 2017-12-15 11:33:25 -0800 | [diff] [blame] | 235 | } |
| 236 | }); |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 237 | |
| 238 | return releaseFence; |
Chia-I Wu | 89d09dd | 2017-02-24 10:41:35 -0800 | [diff] [blame] | 239 | } |
| 240 | |
Chia-I Wu | 98923fc | 2018-01-14 21:45:18 -0800 | [diff] [blame] | 241 | } // namespace vts |
Chia-I Wu | 89d09dd | 2017-02-24 10:41:35 -0800 | [diff] [blame] | 242 | } // namespace V2_0 |
| 243 | } // namespace mapper |
| 244 | } // namespace graphics |
| 245 | } // namespace hardware |
| 246 | } // namespace android |