| Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright 2016 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 | #define LOG_TAG "Gralloc2" | 
|  | 18 |  | 
| Jesse Hall | 5dac781 | 2017-07-06 14:02:29 -0700 | [diff] [blame] | 19 | #include <hidl/ServiceManagement.h> | 
| Chia-I Wu | d8091b9 | 2017-05-16 14:30:34 -0700 | [diff] [blame] | 20 | #include <hwbinder/IPCThreadState.h> | 
| Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 21 | #include <ui/Gralloc2.h> | 
|  | 22 |  | 
| Craig Donner | e6ecb92 | 2017-12-27 14:59:29 -0800 | [diff] [blame] | 23 | #include <inttypes.h> | 
| Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 24 | #include <log/log.h> | 
|  | 25 | #pragma clang diagnostic push | 
|  | 26 | #pragma clang diagnostic ignored "-Wzero-length-array" | 
|  | 27 | #include <sync/sync.h> | 
|  | 28 | #pragma clang diagnostic pop | 
|  | 29 |  | 
|  | 30 | namespace android { | 
|  | 31 |  | 
|  | 32 | namespace Gralloc2 { | 
|  | 33 |  | 
| Craig Donner | e6ecb92 | 2017-12-27 14:59:29 -0800 | [diff] [blame] | 34 | namespace { | 
|  | 35 |  | 
| Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 36 | static constexpr Error kTransactionError = Error::NO_RESOURCES; | 
|  | 37 |  | 
| Craig Donner | e6ecb92 | 2017-12-27 14:59:29 -0800 | [diff] [blame] | 38 | uint64_t getValid10UsageBits() { | 
|  | 39 | static const uint64_t valid10UsageBits = []() -> uint64_t { | 
|  | 40 | using hardware::graphics::common::V1_0::BufferUsage; | 
|  | 41 | uint64_t bits = 0; | 
| Steven Moreland | 3cde875 | 2018-05-01 16:54:17 -0700 | [diff] [blame] | 42 | for (const auto bit : hardware::hidl_enum_range<BufferUsage>()) { | 
| Craig Donner | e6ecb92 | 2017-12-27 14:59:29 -0800 | [diff] [blame] | 43 | bits = bits | bit; | 
|  | 44 | } | 
| Craig Donner | e6ecb92 | 2017-12-27 14:59:29 -0800 | [diff] [blame] | 45 | return bits; | 
|  | 46 | }(); | 
|  | 47 | return valid10UsageBits; | 
|  | 48 | } | 
|  | 49 |  | 
|  | 50 | uint64_t getValid11UsageBits() { | 
|  | 51 | static const uint64_t valid11UsageBits = []() -> uint64_t { | 
|  | 52 | using hardware::graphics::common::V1_1::BufferUsage; | 
|  | 53 | uint64_t bits = 0; | 
| Steven Moreland | 3cde875 | 2018-05-01 16:54:17 -0700 | [diff] [blame] | 54 | for (const auto bit : hardware::hidl_enum_range<BufferUsage>()) { | 
| Craig Donner | e6ecb92 | 2017-12-27 14:59:29 -0800 | [diff] [blame] | 55 | bits = bits | bit; | 
|  | 56 | } | 
| Chia-I Wu | 4f55f16 | 2018-01-16 21:58:18 -0800 | [diff] [blame] | 57 | return bits; | 
| Craig Donner | e6ecb92 | 2017-12-27 14:59:29 -0800 | [diff] [blame] | 58 | }(); | 
|  | 59 | return valid11UsageBits; | 
|  | 60 | } | 
|  | 61 |  | 
|  | 62 | }  // anonymous namespace | 
|  | 63 |  | 
| Jesse Hall | 5dac781 | 2017-07-06 14:02:29 -0700 | [diff] [blame] | 64 | void Mapper::preload() { | 
|  | 65 | android::hardware::preloadPassthroughService<hardware::graphics::mapper::V2_0::IMapper>(); | 
|  | 66 | } | 
|  | 67 |  | 
| Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 68 | Mapper::Mapper() | 
|  | 69 | { | 
| Chia-I Wu | 4f55f16 | 2018-01-16 21:58:18 -0800 | [diff] [blame] | 70 | mMapper = hardware::graphics::mapper::V2_0::IMapper::getService(); | 
| Chia-I Wu | dbbe33b | 2017-09-27 15:22:21 -0700 | [diff] [blame] | 71 | if (mMapper == nullptr) { | 
|  | 72 | LOG_ALWAYS_FATAL("gralloc-mapper is missing"); | 
|  | 73 | } | 
|  | 74 | if (mMapper->isRemote()) { | 
| Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 75 | LOG_ALWAYS_FATAL("gralloc-mapper must be in passthrough mode"); | 
|  | 76 | } | 
| Chia-I Wu | dbbe33b | 2017-09-27 15:22:21 -0700 | [diff] [blame] | 77 |  | 
|  | 78 | // IMapper 2.1 is optional | 
| Chia-I Wu | 4f55f16 | 2018-01-16 21:58:18 -0800 | [diff] [blame] | 79 | mMapperV2_1 = IMapper::castFrom(mMapper); | 
| Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 80 | } | 
|  | 81 |  | 
| Craig Donner | e6ecb92 | 2017-12-27 14:59:29 -0800 | [diff] [blame] | 82 | Gralloc2::Error Mapper::validateBufferDescriptorInfo( | 
|  | 83 | const IMapper::BufferDescriptorInfo& descriptorInfo) const { | 
|  | 84 | uint64_t validUsageBits = getValid10UsageBits(); | 
|  | 85 | if (mMapperV2_1 != nullptr) { | 
|  | 86 | validUsageBits = validUsageBits | getValid11UsageBits(); | 
|  | 87 | } | 
|  | 88 |  | 
|  | 89 | if (descriptorInfo.usage & ~validUsageBits) { | 
|  | 90 | ALOGE("buffer descriptor contains invalid usage bits 0x%" PRIx64, | 
|  | 91 | descriptorInfo.usage & ~validUsageBits); | 
|  | 92 | return Error::BAD_VALUE; | 
|  | 93 | } | 
|  | 94 | return Error::NONE; | 
|  | 95 | } | 
|  | 96 |  | 
| Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 97 | Error Mapper::createDescriptor( | 
|  | 98 | const IMapper::BufferDescriptorInfo& descriptorInfo, | 
|  | 99 | BufferDescriptor* outDescriptor) const | 
|  | 100 | { | 
| Chia-I Wu | 4f55f16 | 2018-01-16 21:58:18 -0800 | [diff] [blame] | 101 | Error error = validateBufferDescriptorInfo(descriptorInfo); | 
| Craig Donner | e6ecb92 | 2017-12-27 14:59:29 -0800 | [diff] [blame] | 102 | if (error != Error::NONE) { | 
|  | 103 | return error; | 
|  | 104 | } | 
|  | 105 |  | 
| Chia-I Wu | 4f55f16 | 2018-01-16 21:58:18 -0800 | [diff] [blame] | 106 | auto hidl_cb = [&](const auto& tmpError, const auto& tmpDescriptor) | 
|  | 107 | { | 
|  | 108 | error = tmpError; | 
|  | 109 | if (error != Error::NONE) { | 
|  | 110 | return; | 
|  | 111 | } | 
| Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 112 |  | 
| Chia-I Wu | 4f55f16 | 2018-01-16 21:58:18 -0800 | [diff] [blame] | 113 | *outDescriptor = tmpDescriptor; | 
|  | 114 | }; | 
|  | 115 |  | 
|  | 116 | hardware::Return<void> ret; | 
|  | 117 | if (mMapperV2_1 != nullptr) { | 
|  | 118 | ret = mMapperV2_1->createDescriptor_2_1(descriptorInfo, hidl_cb); | 
|  | 119 | } else { | 
|  | 120 | const hardware::graphics::mapper::V2_0::IMapper::BufferDescriptorInfo info = { | 
|  | 121 | descriptorInfo.width, | 
|  | 122 | descriptorInfo.height, | 
|  | 123 | descriptorInfo.layerCount, | 
|  | 124 | static_cast<hardware::graphics::common::V1_0::PixelFormat>(descriptorInfo.format), | 
|  | 125 | descriptorInfo.usage, | 
|  | 126 | }; | 
|  | 127 | ret = mMapper->createDescriptor(info, hidl_cb); | 
|  | 128 | } | 
| Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 129 |  | 
|  | 130 | return (ret.isOk()) ? error : kTransactionError; | 
|  | 131 | } | 
|  | 132 |  | 
|  | 133 | Error Mapper::importBuffer(const hardware::hidl_handle& rawHandle, | 
|  | 134 | buffer_handle_t* outBufferHandle) const | 
|  | 135 | { | 
|  | 136 | Error error; | 
|  | 137 | auto ret = mMapper->importBuffer(rawHandle, | 
|  | 138 | [&](const auto& tmpError, const auto& tmpBuffer) | 
|  | 139 | { | 
|  | 140 | error = tmpError; | 
|  | 141 | if (error != Error::NONE) { | 
|  | 142 | return; | 
|  | 143 | } | 
|  | 144 |  | 
|  | 145 | *outBufferHandle = static_cast<buffer_handle_t>(tmpBuffer); | 
|  | 146 | }); | 
|  | 147 |  | 
|  | 148 | return (ret.isOk()) ? error : kTransactionError; | 
|  | 149 | } | 
|  | 150 |  | 
|  | 151 | void Mapper::freeBuffer(buffer_handle_t bufferHandle) const | 
|  | 152 | { | 
|  | 153 | auto buffer = const_cast<native_handle_t*>(bufferHandle); | 
|  | 154 | auto ret = mMapper->freeBuffer(buffer); | 
|  | 155 |  | 
|  | 156 | auto error = (ret.isOk()) ? static_cast<Error>(ret) : kTransactionError; | 
|  | 157 | ALOGE_IF(error != Error::NONE, "freeBuffer(%p) failed with %d", | 
|  | 158 | buffer, error); | 
|  | 159 | } | 
|  | 160 |  | 
| Chia-I Wu | dbbe33b | 2017-09-27 15:22:21 -0700 | [diff] [blame] | 161 | Error Mapper::validateBufferSize(buffer_handle_t bufferHandle, | 
|  | 162 | const IMapper::BufferDescriptorInfo& descriptorInfo, | 
|  | 163 | uint32_t stride) const | 
|  | 164 | { | 
|  | 165 | if (mMapperV2_1 == nullptr) { | 
|  | 166 | return Error::NONE; | 
|  | 167 | } | 
|  | 168 |  | 
|  | 169 | auto buffer = const_cast<native_handle_t*>(bufferHandle); | 
|  | 170 | auto ret = mMapperV2_1->validateBufferSize(buffer, descriptorInfo, stride); | 
|  | 171 |  | 
|  | 172 | return (ret.isOk()) ? static_cast<Error>(ret) : kTransactionError; | 
|  | 173 | } | 
|  | 174 |  | 
|  | 175 | void Mapper::getTransportSize(buffer_handle_t bufferHandle, | 
|  | 176 | uint32_t* outNumFds, uint32_t* outNumInts) const | 
|  | 177 | { | 
|  | 178 | *outNumFds = uint32_t(bufferHandle->numFds); | 
|  | 179 | *outNumInts = uint32_t(bufferHandle->numInts); | 
|  | 180 |  | 
|  | 181 | if (mMapperV2_1 == nullptr) { | 
|  | 182 | return; | 
|  | 183 | } | 
|  | 184 |  | 
|  | 185 | Error error; | 
|  | 186 | auto buffer = const_cast<native_handle_t*>(bufferHandle); | 
|  | 187 | auto ret = mMapperV2_1->getTransportSize(buffer, | 
|  | 188 | [&](const auto& tmpError, const auto& tmpNumFds, const auto& tmpNumInts) { | 
|  | 189 | error = tmpError; | 
|  | 190 | if (error != Error::NONE) { | 
|  | 191 | return; | 
|  | 192 | } | 
|  | 193 |  | 
|  | 194 | *outNumFds = tmpNumFds; | 
|  | 195 | *outNumInts = tmpNumInts; | 
|  | 196 | }); | 
|  | 197 |  | 
|  | 198 | if (!ret.isOk()) { | 
|  | 199 | error = kTransactionError; | 
|  | 200 | } | 
|  | 201 | ALOGE_IF(error != Error::NONE, "getTransportSize(%p) failed with %d", | 
|  | 202 | buffer, error); | 
|  | 203 | } | 
|  | 204 |  | 
| Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 205 | Error Mapper::lock(buffer_handle_t bufferHandle, uint64_t usage, | 
|  | 206 | const IMapper::Rect& accessRegion, | 
|  | 207 | int acquireFence, void** outData) const | 
|  | 208 | { | 
|  | 209 | auto buffer = const_cast<native_handle_t*>(bufferHandle); | 
|  | 210 |  | 
|  | 211 | // put acquireFence in a hidl_handle | 
|  | 212 | hardware::hidl_handle acquireFenceHandle; | 
|  | 213 | NATIVE_HANDLE_DECLARE_STORAGE(acquireFenceStorage, 1, 0); | 
|  | 214 | if (acquireFence >= 0) { | 
|  | 215 | auto h = native_handle_init(acquireFenceStorage, 1, 0); | 
|  | 216 | h->data[0] = acquireFence; | 
|  | 217 | acquireFenceHandle = h; | 
|  | 218 | } | 
|  | 219 |  | 
|  | 220 | Error error; | 
|  | 221 | auto ret = mMapper->lock(buffer, usage, accessRegion, acquireFenceHandle, | 
|  | 222 | [&](const auto& tmpError, const auto& tmpData) | 
|  | 223 | { | 
|  | 224 | error = tmpError; | 
|  | 225 | if (error != Error::NONE) { | 
|  | 226 | return; | 
|  | 227 | } | 
|  | 228 |  | 
|  | 229 | *outData = tmpData; | 
|  | 230 | }); | 
|  | 231 |  | 
|  | 232 | // we own acquireFence even on errors | 
|  | 233 | if (acquireFence >= 0) { | 
|  | 234 | close(acquireFence); | 
|  | 235 | } | 
|  | 236 |  | 
|  | 237 | return (ret.isOk()) ? error : kTransactionError; | 
|  | 238 | } | 
|  | 239 |  | 
|  | 240 | Error Mapper::lock(buffer_handle_t bufferHandle, uint64_t usage, | 
|  | 241 | const IMapper::Rect& accessRegion, | 
|  | 242 | int acquireFence, YCbCrLayout* outLayout) const | 
|  | 243 | { | 
|  | 244 | auto buffer = const_cast<native_handle_t*>(bufferHandle); | 
|  | 245 |  | 
|  | 246 | // put acquireFence in a hidl_handle | 
|  | 247 | hardware::hidl_handle acquireFenceHandle; | 
|  | 248 | NATIVE_HANDLE_DECLARE_STORAGE(acquireFenceStorage, 1, 0); | 
|  | 249 | if (acquireFence >= 0) { | 
|  | 250 | auto h = native_handle_init(acquireFenceStorage, 1, 0); | 
|  | 251 | h->data[0] = acquireFence; | 
|  | 252 | acquireFenceHandle = h; | 
|  | 253 | } | 
|  | 254 |  | 
|  | 255 | Error error; | 
|  | 256 | auto ret = mMapper->lockYCbCr(buffer, usage, accessRegion, | 
|  | 257 | acquireFenceHandle, | 
|  | 258 | [&](const auto& tmpError, const auto& tmpLayout) | 
|  | 259 | { | 
|  | 260 | error = tmpError; | 
|  | 261 | if (error != Error::NONE) { | 
|  | 262 | return; | 
|  | 263 | } | 
|  | 264 |  | 
|  | 265 | *outLayout = tmpLayout; | 
|  | 266 | }); | 
|  | 267 |  | 
|  | 268 | // we own acquireFence even on errors | 
|  | 269 | if (acquireFence >= 0) { | 
|  | 270 | close(acquireFence); | 
|  | 271 | } | 
|  | 272 |  | 
|  | 273 | return (ret.isOk()) ? error : kTransactionError; | 
|  | 274 | } | 
|  | 275 |  | 
|  | 276 | int Mapper::unlock(buffer_handle_t bufferHandle) const | 
|  | 277 | { | 
|  | 278 | auto buffer = const_cast<native_handle_t*>(bufferHandle); | 
|  | 279 |  | 
|  | 280 | int releaseFence = -1; | 
|  | 281 | Error error; | 
|  | 282 | auto ret = mMapper->unlock(buffer, | 
|  | 283 | [&](const auto& tmpError, const auto& tmpReleaseFence) | 
|  | 284 | { | 
|  | 285 | error = tmpError; | 
|  | 286 | if (error != Error::NONE) { | 
|  | 287 | return; | 
|  | 288 | } | 
|  | 289 |  | 
|  | 290 | auto fenceHandle = tmpReleaseFence.getNativeHandle(); | 
|  | 291 | if (fenceHandle && fenceHandle->numFds == 1) { | 
|  | 292 | int fd = dup(fenceHandle->data[0]); | 
|  | 293 | if (fd >= 0) { | 
|  | 294 | releaseFence = fd; | 
|  | 295 | } else { | 
|  | 296 | ALOGD("failed to dup unlock release fence"); | 
|  | 297 | sync_wait(fenceHandle->data[0], -1); | 
|  | 298 | } | 
|  | 299 | } | 
|  | 300 | }); | 
|  | 301 |  | 
|  | 302 | if (!ret.isOk()) { | 
|  | 303 | error = kTransactionError; | 
|  | 304 | } | 
|  | 305 |  | 
|  | 306 | if (error != Error::NONE) { | 
|  | 307 | ALOGE("unlock(%p) failed with %d", buffer, error); | 
|  | 308 | } | 
|  | 309 |  | 
|  | 310 | return releaseFence; | 
|  | 311 | } | 
|  | 312 |  | 
|  | 313 | Allocator::Allocator(const Mapper& mapper) | 
|  | 314 | : mMapper(mapper) | 
|  | 315 | { | 
| Chia-I Wu | cb8405e | 2017-04-17 15:20:19 -0700 | [diff] [blame] | 316 | mAllocator = IAllocator::getService(); | 
|  | 317 | if (mAllocator == nullptr) { | 
|  | 318 | LOG_ALWAYS_FATAL("gralloc-alloc is missing"); | 
| Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 319 | } | 
|  | 320 | } | 
|  | 321 |  | 
|  | 322 | std::string Allocator::dumpDebugInfo() const | 
|  | 323 | { | 
|  | 324 | std::string debugInfo; | 
|  | 325 |  | 
|  | 326 | mAllocator->dumpDebugInfo([&](const auto& tmpDebugInfo) { | 
|  | 327 | debugInfo = tmpDebugInfo.c_str(); | 
|  | 328 | }); | 
|  | 329 |  | 
|  | 330 | return debugInfo; | 
|  | 331 | } | 
|  | 332 |  | 
|  | 333 | Error Allocator::allocate(BufferDescriptor descriptor, uint32_t count, | 
|  | 334 | uint32_t* outStride, buffer_handle_t* outBufferHandles) const | 
|  | 335 | { | 
|  | 336 | Error error; | 
|  | 337 | auto ret = mAllocator->allocate(descriptor, count, | 
|  | 338 | [&](const auto& tmpError, const auto& tmpStride, | 
|  | 339 | const auto& tmpBuffers) { | 
|  | 340 | error = tmpError; | 
|  | 341 | if (tmpError != Error::NONE) { | 
|  | 342 | return; | 
|  | 343 | } | 
|  | 344 |  | 
|  | 345 | // import buffers | 
|  | 346 | for (uint32_t i = 0; i < count; i++) { | 
|  | 347 | error = mMapper.importBuffer(tmpBuffers[i], | 
|  | 348 | &outBufferHandles[i]); | 
|  | 349 | if (error != Error::NONE) { | 
|  | 350 | for (uint32_t j = 0; j < i; j++) { | 
|  | 351 | mMapper.freeBuffer(outBufferHandles[j]); | 
|  | 352 | outBufferHandles[j] = nullptr; | 
|  | 353 | } | 
|  | 354 | return; | 
|  | 355 | } | 
|  | 356 | } | 
|  | 357 |  | 
|  | 358 | *outStride = tmpStride; | 
|  | 359 | }); | 
|  | 360 |  | 
| Chia-I Wu | d8091b9 | 2017-05-16 14:30:34 -0700 | [diff] [blame] | 361 | // make sure the kernel driver sees BC_FREE_BUFFER and closes the fds now | 
|  | 362 | hardware::IPCThreadState::self()->flushCommands(); | 
|  | 363 |  | 
| Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 364 | return (ret.isOk()) ? error : kTransactionError; | 
|  | 365 | } | 
|  | 366 |  | 
|  | 367 | } // namespace Gralloc2 | 
|  | 368 |  | 
| Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 369 | } // namespace android |