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