| Marissa Wall | 925bf7f | 2018-12-29 14:27:11 -0800 | [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 "Gralloc3" | 
|  | 18 |  | 
|  | 19 | #include <hidl/ServiceManagement.h> | 
|  | 20 | #include <hwbinder/IPCThreadState.h> | 
|  | 21 | #include <ui/Gralloc3.h> | 
|  | 22 |  | 
|  | 23 | #include <inttypes.h> | 
|  | 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 | using android::hardware::graphics::allocator::V3_0::IAllocator; | 
|  | 31 | using android::hardware::graphics::common::V1_1::BufferUsage; | 
|  | 32 | using android::hardware::graphics::mapper::V3_0::BufferDescriptor; | 
|  | 33 | using android::hardware::graphics::mapper::V3_0::Error; | 
|  | 34 | using android::hardware::graphics::mapper::V3_0::IMapper; | 
|  | 35 | using android::hardware::graphics::mapper::V3_0::YCbCrLayout; | 
|  | 36 |  | 
|  | 37 | namespace android { | 
|  | 38 |  | 
|  | 39 | namespace { | 
|  | 40 |  | 
|  | 41 | static constexpr Error kTransactionError = Error::NO_RESOURCES; | 
|  | 42 |  | 
|  | 43 | uint64_t getValidUsageBits() { | 
|  | 44 | static const uint64_t validUsageBits = []() -> uint64_t { | 
|  | 45 | uint64_t bits = 0; | 
|  | 46 | for (const auto bit : | 
|  | 47 | hardware::hidl_enum_range<hardware::graphics::common::V1_0::BufferUsage>()) { | 
|  | 48 | bits = bits | bit; | 
|  | 49 | } | 
|  | 50 | for (const auto bit : | 
|  | 51 | hardware::hidl_enum_range<hardware::graphics::common::V1_1::BufferUsage>()) { | 
|  | 52 | bits = bits | bit; | 
|  | 53 | } | 
|  | 54 | return bits; | 
|  | 55 | }(); | 
|  | 56 | return validUsageBits; | 
|  | 57 | } | 
|  | 58 |  | 
|  | 59 | static inline IMapper::Rect sGralloc3Rect(const Rect& rect) { | 
|  | 60 | IMapper::Rect outRect{}; | 
|  | 61 | outRect.left = rect.left; | 
|  | 62 | outRect.top = rect.top; | 
|  | 63 | outRect.width = rect.width(); | 
|  | 64 | outRect.height = rect.height(); | 
|  | 65 | return outRect; | 
|  | 66 | } | 
|  | 67 | static inline void sBufferDescriptorInfo(uint32_t width, uint32_t height, | 
|  | 68 | android::PixelFormat format, uint32_t layerCount, | 
|  | 69 | uint64_t usage, | 
|  | 70 | IMapper::BufferDescriptorInfo* outDescriptorInfo) { | 
|  | 71 | outDescriptorInfo->width = width; | 
|  | 72 | outDescriptorInfo->height = height; | 
|  | 73 | outDescriptorInfo->layerCount = layerCount; | 
|  | 74 | outDescriptorInfo->format = static_cast<hardware::graphics::common::V1_1::PixelFormat>(format); | 
|  | 75 | outDescriptorInfo->usage = usage; | 
|  | 76 | } | 
|  | 77 |  | 
|  | 78 | } // anonymous namespace | 
|  | 79 |  | 
|  | 80 | void Gralloc3Mapper::preload() { | 
|  | 81 | android::hardware::preloadPassthroughService<IMapper>(); | 
|  | 82 | } | 
|  | 83 |  | 
|  | 84 | Gralloc3Mapper::Gralloc3Mapper() { | 
|  | 85 | mMapper = IMapper::getService(); | 
|  | 86 | if (mMapper == nullptr) { | 
|  | 87 | ALOGW("mapper 3.x is not supported"); | 
|  | 88 | return; | 
|  | 89 | } | 
|  | 90 | if (mMapper->isRemote()) { | 
|  | 91 | LOG_ALWAYS_FATAL("gralloc-mapper must be in passthrough mode"); | 
|  | 92 | } | 
|  | 93 | } | 
|  | 94 |  | 
| Valerie Hau | 250c654 | 2019-01-31 14:23:43 -0800 | [diff] [blame] | 95 | bool Gralloc3Mapper::isLoaded() const { | 
| Marissa Wall | 925bf7f | 2018-12-29 14:27:11 -0800 | [diff] [blame] | 96 | return mMapper != nullptr; | 
|  | 97 | } | 
|  | 98 |  | 
|  | 99 | status_t Gralloc3Mapper::validateBufferDescriptorInfo( | 
|  | 100 | IMapper::BufferDescriptorInfo* descriptorInfo) const { | 
|  | 101 | uint64_t validUsageBits = getValidUsageBits(); | 
|  | 102 |  | 
|  | 103 | if (descriptorInfo->usage & ~validUsageBits) { | 
|  | 104 | ALOGE("buffer descriptor contains invalid usage bits 0x%" PRIx64, | 
|  | 105 | descriptorInfo->usage & ~validUsageBits); | 
|  | 106 | return BAD_VALUE; | 
|  | 107 | } | 
|  | 108 | return NO_ERROR; | 
|  | 109 | } | 
|  | 110 |  | 
|  | 111 | status_t Gralloc3Mapper::createDescriptor(void* bufferDescriptorInfo, | 
|  | 112 | void* outBufferDescriptor) const { | 
|  | 113 | IMapper::BufferDescriptorInfo* descriptorInfo = | 
|  | 114 | static_cast<IMapper::BufferDescriptorInfo*>(bufferDescriptorInfo); | 
|  | 115 | BufferDescriptor* outDescriptor = static_cast<BufferDescriptor*>(outBufferDescriptor); | 
|  | 116 |  | 
|  | 117 | status_t status = validateBufferDescriptorInfo(descriptorInfo); | 
|  | 118 | if (status != NO_ERROR) { | 
|  | 119 | return status; | 
|  | 120 | } | 
|  | 121 |  | 
|  | 122 | Error error; | 
|  | 123 | auto hidl_cb = [&](const auto& tmpError, const auto& tmpDescriptor) { | 
|  | 124 | error = tmpError; | 
|  | 125 | if (error != Error::NONE) { | 
|  | 126 | return; | 
|  | 127 | } | 
|  | 128 | *outDescriptor = tmpDescriptor; | 
|  | 129 | }; | 
|  | 130 |  | 
|  | 131 | hardware::Return<void> ret = mMapper->createDescriptor(*descriptorInfo, hidl_cb); | 
|  | 132 |  | 
|  | 133 | return static_cast<status_t>((ret.isOk()) ? error : kTransactionError); | 
|  | 134 | } | 
|  | 135 |  | 
|  | 136 | status_t Gralloc3Mapper::importBuffer(const hardware::hidl_handle& rawHandle, | 
|  | 137 | buffer_handle_t* outBufferHandle) const { | 
|  | 138 | Error error; | 
|  | 139 | auto ret = mMapper->importBuffer(rawHandle, [&](const auto& tmpError, const auto& tmpBuffer) { | 
|  | 140 | error = tmpError; | 
|  | 141 | if (error != Error::NONE) { | 
|  | 142 | return; | 
|  | 143 | } | 
|  | 144 | *outBufferHandle = static_cast<buffer_handle_t>(tmpBuffer); | 
|  | 145 | }); | 
|  | 146 |  | 
|  | 147 | return static_cast<status_t>((ret.isOk()) ? error : kTransactionError); | 
|  | 148 | } | 
|  | 149 |  | 
|  | 150 | void Gralloc3Mapper::freeBuffer(buffer_handle_t bufferHandle) const { | 
|  | 151 | auto buffer = const_cast<native_handle_t*>(bufferHandle); | 
|  | 152 | auto ret = mMapper->freeBuffer(buffer); | 
|  | 153 |  | 
|  | 154 | auto error = (ret.isOk()) ? static_cast<Error>(ret) : kTransactionError; | 
|  | 155 | ALOGE_IF(error != Error::NONE, "freeBuffer(%p) failed with %d", buffer, error); | 
|  | 156 | } | 
|  | 157 |  | 
|  | 158 | status_t Gralloc3Mapper::validateBufferSize(buffer_handle_t bufferHandle, uint32_t width, | 
|  | 159 | uint32_t height, android::PixelFormat format, | 
|  | 160 | uint32_t layerCount, uint64_t usage, | 
|  | 161 | uint32_t stride) const { | 
|  | 162 | IMapper::BufferDescriptorInfo descriptorInfo; | 
|  | 163 | sBufferDescriptorInfo(width, height, format, layerCount, usage, &descriptorInfo); | 
|  | 164 |  | 
|  | 165 | auto buffer = const_cast<native_handle_t*>(bufferHandle); | 
|  | 166 | auto ret = mMapper->validateBufferSize(buffer, descriptorInfo, stride); | 
|  | 167 |  | 
|  | 168 | return static_cast<status_t>((ret.isOk()) ? static_cast<Error>(ret) : kTransactionError); | 
|  | 169 | } | 
|  | 170 |  | 
|  | 171 | void Gralloc3Mapper::getTransportSize(buffer_handle_t bufferHandle, uint32_t* outNumFds, | 
|  | 172 | uint32_t* outNumInts) const { | 
|  | 173 | *outNumFds = uint32_t(bufferHandle->numFds); | 
|  | 174 | *outNumInts = uint32_t(bufferHandle->numInts); | 
|  | 175 |  | 
|  | 176 | Error error; | 
|  | 177 | auto buffer = const_cast<native_handle_t*>(bufferHandle); | 
|  | 178 | auto ret = mMapper->getTransportSize(buffer, | 
|  | 179 | [&](const auto& tmpError, const auto& tmpNumFds, | 
|  | 180 | const auto& tmpNumInts) { | 
|  | 181 | error = tmpError; | 
|  | 182 | if (error != Error::NONE) { | 
|  | 183 | return; | 
|  | 184 | } | 
|  | 185 | *outNumFds = tmpNumFds; | 
|  | 186 | *outNumInts = tmpNumInts; | 
|  | 187 | }); | 
|  | 188 |  | 
|  | 189 | error = (ret.isOk()) ? error : kTransactionError; | 
|  | 190 |  | 
|  | 191 | ALOGE_IF(error != Error::NONE, "getTransportSize(%p) failed with %d", buffer, error); | 
|  | 192 | } | 
|  | 193 |  | 
|  | 194 | status_t Gralloc3Mapper::lock(buffer_handle_t bufferHandle, uint64_t usage, const Rect& bounds, | 
| Valerie Hau | 0c9fc36 | 2019-01-22 09:17:19 -0800 | [diff] [blame] | 195 | int acquireFence, void** outData, int32_t* outBytesPerPixel, | 
|  | 196 | int32_t* outBytesPerStride) const { | 
| Marissa Wall | 925bf7f | 2018-12-29 14:27:11 -0800 | [diff] [blame] | 197 | auto buffer = const_cast<native_handle_t*>(bufferHandle); | 
|  | 198 |  | 
|  | 199 | IMapper::Rect accessRegion = sGralloc3Rect(bounds); | 
|  | 200 |  | 
|  | 201 | // put acquireFence in a hidl_handle | 
|  | 202 | hardware::hidl_handle acquireFenceHandle; | 
|  | 203 | NATIVE_HANDLE_DECLARE_STORAGE(acquireFenceStorage, 1, 0); | 
|  | 204 | if (acquireFence >= 0) { | 
|  | 205 | auto h = native_handle_init(acquireFenceStorage, 1, 0); | 
|  | 206 | h->data[0] = acquireFence; | 
|  | 207 | acquireFenceHandle = h; | 
|  | 208 | } | 
|  | 209 |  | 
|  | 210 | Error error; | 
|  | 211 | auto ret = mMapper->lock(buffer, usage, accessRegion, acquireFenceHandle, | 
| Valerie Hau | 0c9fc36 | 2019-01-22 09:17:19 -0800 | [diff] [blame] | 212 | [&](const auto& tmpError, const auto& tmpData, | 
|  | 213 | const auto& tmpBytesPerPixel, const auto& tmpBytesPerStride) { | 
| Marissa Wall | 925bf7f | 2018-12-29 14:27:11 -0800 | [diff] [blame] | 214 | error = tmpError; | 
|  | 215 | if (error != Error::NONE) { | 
|  | 216 | return; | 
|  | 217 | } | 
|  | 218 | *outData = tmpData; | 
| Valerie Hau | 0c9fc36 | 2019-01-22 09:17:19 -0800 | [diff] [blame] | 219 | if (outBytesPerPixel) { | 
|  | 220 | *outBytesPerPixel = tmpBytesPerPixel; | 
|  | 221 | } | 
|  | 222 | if (outBytesPerStride) { | 
|  | 223 | *outBytesPerStride = tmpBytesPerStride; | 
|  | 224 | } | 
| Marissa Wall | 925bf7f | 2018-12-29 14:27:11 -0800 | [diff] [blame] | 225 | }); | 
|  | 226 |  | 
|  | 227 | // we own acquireFence even on errors | 
|  | 228 | if (acquireFence >= 0) { | 
|  | 229 | close(acquireFence); | 
|  | 230 | } | 
|  | 231 |  | 
|  | 232 | error = (ret.isOk()) ? error : kTransactionError; | 
|  | 233 |  | 
|  | 234 | ALOGW_IF(error != Error::NONE, "lock(%p, ...) failed: %d", bufferHandle, error); | 
|  | 235 |  | 
|  | 236 | return static_cast<status_t>(error); | 
|  | 237 | } | 
|  | 238 |  | 
|  | 239 | status_t Gralloc3Mapper::lock(buffer_handle_t bufferHandle, uint64_t usage, const Rect& bounds, | 
|  | 240 | int acquireFence, android_ycbcr* ycbcr) const { | 
|  | 241 | auto buffer = const_cast<native_handle_t*>(bufferHandle); | 
|  | 242 |  | 
|  | 243 | IMapper::Rect accessRegion = sGralloc3Rect(bounds); | 
|  | 244 |  | 
|  | 245 | // put acquireFence in a hidl_handle | 
|  | 246 | hardware::hidl_handle acquireFenceHandle; | 
|  | 247 | NATIVE_HANDLE_DECLARE_STORAGE(acquireFenceStorage, 1, 0); | 
|  | 248 | if (acquireFence >= 0) { | 
|  | 249 | auto h = native_handle_init(acquireFenceStorage, 1, 0); | 
|  | 250 | h->data[0] = acquireFence; | 
|  | 251 | acquireFenceHandle = h; | 
|  | 252 | } | 
|  | 253 |  | 
|  | 254 | YCbCrLayout layout; | 
|  | 255 | Error error; | 
|  | 256 | auto ret = mMapper->lockYCbCr(buffer, usage, accessRegion, acquireFenceHandle, | 
|  | 257 | [&](const auto& tmpError, const auto& tmpLayout) { | 
|  | 258 | error = tmpError; | 
|  | 259 | if (error != Error::NONE) { | 
|  | 260 | return; | 
|  | 261 | } | 
|  | 262 |  | 
|  | 263 | layout = tmpLayout; | 
|  | 264 | }); | 
|  | 265 |  | 
|  | 266 | if (error == Error::NONE) { | 
|  | 267 | ycbcr->y = layout.y; | 
|  | 268 | ycbcr->cb = layout.cb; | 
|  | 269 | ycbcr->cr = layout.cr; | 
|  | 270 | ycbcr->ystride = static_cast<size_t>(layout.yStride); | 
|  | 271 | ycbcr->cstride = static_cast<size_t>(layout.cStride); | 
|  | 272 | ycbcr->chroma_step = static_cast<size_t>(layout.chromaStep); | 
|  | 273 | } | 
|  | 274 |  | 
|  | 275 | // we own acquireFence even on errors | 
|  | 276 | if (acquireFence >= 0) { | 
|  | 277 | close(acquireFence); | 
|  | 278 | } | 
|  | 279 |  | 
|  | 280 | return static_cast<status_t>((ret.isOk()) ? error : kTransactionError); | 
|  | 281 | } | 
|  | 282 |  | 
|  | 283 | int Gralloc3Mapper::unlock(buffer_handle_t bufferHandle) const { | 
|  | 284 | auto buffer = const_cast<native_handle_t*>(bufferHandle); | 
|  | 285 |  | 
|  | 286 | int releaseFence = -1; | 
|  | 287 | Error error; | 
|  | 288 | auto ret = mMapper->unlock(buffer, [&](const auto& tmpError, const auto& tmpReleaseFence) { | 
|  | 289 | error = tmpError; | 
|  | 290 | if (error != Error::NONE) { | 
|  | 291 | return; | 
|  | 292 | } | 
|  | 293 |  | 
|  | 294 | auto fenceHandle = tmpReleaseFence.getNativeHandle(); | 
|  | 295 | if (fenceHandle && fenceHandle->numFds == 1) { | 
|  | 296 | int fd = dup(fenceHandle->data[0]); | 
|  | 297 | if (fd >= 0) { | 
|  | 298 | releaseFence = fd; | 
|  | 299 | } else { | 
|  | 300 | ALOGD("failed to dup unlock release fence"); | 
|  | 301 | sync_wait(fenceHandle->data[0], -1); | 
|  | 302 | } | 
|  | 303 | } | 
|  | 304 | }); | 
|  | 305 |  | 
|  | 306 | if (!ret.isOk()) { | 
|  | 307 | error = kTransactionError; | 
|  | 308 | } | 
|  | 309 |  | 
|  | 310 | if (error != Error::NONE) { | 
|  | 311 | ALOGE("unlock(%p) failed with %d", buffer, error); | 
|  | 312 | } | 
|  | 313 |  | 
|  | 314 | return releaseFence; | 
|  | 315 | } | 
|  | 316 |  | 
| Valerie Hau | ddbfaeb | 2019-02-01 09:54:20 -0800 | [diff] [blame] | 317 | status_t Gralloc3Mapper::isSupported(uint32_t width, uint32_t height, android::PixelFormat format, | 
|  | 318 | uint32_t layerCount, uint64_t usage, | 
|  | 319 | bool* outSupported) const { | 
|  | 320 | IMapper::BufferDescriptorInfo descriptorInfo; | 
|  | 321 | sBufferDescriptorInfo(width, height, format, layerCount, usage, &descriptorInfo); | 
|  | 322 |  | 
|  | 323 | Error error; | 
|  | 324 | auto ret = mMapper->isSupported(descriptorInfo, | 
|  | 325 | [&](const auto& tmpError, const auto& tmpSupported) { | 
|  | 326 | error = tmpError; | 
|  | 327 | if (error != Error::NONE) { | 
|  | 328 | return; | 
|  | 329 | } | 
|  | 330 | if (outSupported) { | 
|  | 331 | *outSupported = tmpSupported; | 
|  | 332 | } | 
|  | 333 | }); | 
|  | 334 |  | 
|  | 335 | if (!ret.isOk()) { | 
|  | 336 | error = kTransactionError; | 
|  | 337 | } | 
|  | 338 |  | 
|  | 339 | if (error != Error::NONE) { | 
|  | 340 | ALOGE("isSupported(%u, %u, %d, %u, ...) failed with %d", width, height, format, layerCount, | 
|  | 341 | error); | 
|  | 342 | } | 
|  | 343 |  | 
|  | 344 | return static_cast<status_t>(error); | 
|  | 345 | } | 
|  | 346 |  | 
| Marissa Wall | 925bf7f | 2018-12-29 14:27:11 -0800 | [diff] [blame] | 347 | Gralloc3Allocator::Gralloc3Allocator(const Gralloc3Mapper& mapper) : mMapper(mapper) { | 
|  | 348 | mAllocator = IAllocator::getService(); | 
|  | 349 | if (mAllocator == nullptr) { | 
|  | 350 | ALOGW("allocator 3.x is not supported"); | 
|  | 351 | return; | 
|  | 352 | } | 
|  | 353 | } | 
|  | 354 |  | 
| Valerie Hau | 250c654 | 2019-01-31 14:23:43 -0800 | [diff] [blame] | 355 | bool Gralloc3Allocator::isLoaded() const { | 
| Marissa Wall | 925bf7f | 2018-12-29 14:27:11 -0800 | [diff] [blame] | 356 | return mAllocator != nullptr; | 
|  | 357 | } | 
|  | 358 |  | 
|  | 359 | std::string Gralloc3Allocator::dumpDebugInfo() const { | 
|  | 360 | std::string debugInfo; | 
|  | 361 |  | 
|  | 362 | mAllocator->dumpDebugInfo([&](const auto& tmpDebugInfo) { debugInfo = tmpDebugInfo.c_str(); }); | 
|  | 363 |  | 
|  | 364 | return debugInfo; | 
|  | 365 | } | 
|  | 366 |  | 
|  | 367 | status_t Gralloc3Allocator::allocate(uint32_t width, uint32_t height, android::PixelFormat format, | 
|  | 368 | uint32_t layerCount, uint64_t usage, uint32_t bufferCount, | 
|  | 369 | uint32_t* outStride, buffer_handle_t* outBufferHandles) const { | 
|  | 370 | IMapper::BufferDescriptorInfo descriptorInfo; | 
|  | 371 | sBufferDescriptorInfo(width, height, format, layerCount, usage, &descriptorInfo); | 
|  | 372 |  | 
|  | 373 | BufferDescriptor descriptor; | 
|  | 374 | status_t error = mMapper.createDescriptor(static_cast<void*>(&descriptorInfo), | 
|  | 375 | static_cast<void*>(&descriptor)); | 
|  | 376 | if (error != NO_ERROR) { | 
|  | 377 | return error; | 
|  | 378 | } | 
|  | 379 |  | 
|  | 380 | auto ret = mAllocator->allocate(descriptor, bufferCount, | 
|  | 381 | [&](const auto& tmpError, const auto& tmpStride, | 
|  | 382 | const auto& tmpBuffers) { | 
|  | 383 | error = static_cast<status_t>(tmpError); | 
|  | 384 | if (tmpError != Error::NONE) { | 
|  | 385 | return; | 
|  | 386 | } | 
|  | 387 |  | 
|  | 388 | // import buffers | 
|  | 389 | for (uint32_t i = 0; i < bufferCount; i++) { | 
|  | 390 | error = mMapper.importBuffer(tmpBuffers[i], | 
|  | 391 | &outBufferHandles[i]); | 
|  | 392 | if (error != NO_ERROR) { | 
|  | 393 | for (uint32_t j = 0; j < i; j++) { | 
|  | 394 | mMapper.freeBuffer(outBufferHandles[j]); | 
|  | 395 | outBufferHandles[j] = nullptr; | 
|  | 396 | } | 
|  | 397 | return; | 
|  | 398 | } | 
|  | 399 | } | 
|  | 400 | *outStride = tmpStride; | 
|  | 401 | }); | 
|  | 402 |  | 
|  | 403 | // make sure the kernel driver sees BC_FREE_BUFFER and closes the fds now | 
|  | 404 | hardware::IPCThreadState::self()->flushCommands(); | 
|  | 405 |  | 
|  | 406 | return (ret.isOk()) ? error : static_cast<status_t>(kTransactionError); | 
|  | 407 | } | 
|  | 408 |  | 
|  | 409 | } // namespace android |