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 | |
Marissa Wall | 22b2de1 | 2019-12-02 18:11:43 -0800 | [diff] [blame] | 30 | using aidl::android::hardware::graphics::common::ExtendableType; |
| 31 | using aidl::android::hardware::graphics::common::PlaneLayoutComponentType; |
| 32 | using aidl::android::hardware::graphics::common::StandardMetadataType; |
| 33 | using android::hardware::hidl_vec; |
Marissa Wall | 87c8ba7 | 2019-06-20 14:20:52 -0700 | [diff] [blame] | 34 | using android::hardware::graphics::allocator::V4_0::IAllocator; |
| 35 | using android::hardware::graphics::common::V1_2::BufferUsage; |
| 36 | using android::hardware::graphics::mapper::V4_0::BufferDescriptor; |
| 37 | using android::hardware::graphics::mapper::V4_0::Error; |
| 38 | using android::hardware::graphics::mapper::V4_0::IMapper; |
Marissa Wall | 22b2de1 | 2019-12-02 18:11:43 -0800 | [diff] [blame] | 39 | using BufferDump = android::hardware::graphics::mapper::V4_0::IMapper::BufferDump; |
| 40 | using MetadataDump = android::hardware::graphics::mapper::V4_0::IMapper::MetadataDump; |
| 41 | using MetadataType = android::hardware::graphics::mapper::V4_0::IMapper::MetadataType; |
| 42 | using MetadataTypeDescription = |
| 43 | android::hardware::graphics::mapper::V4_0::IMapper::MetadataTypeDescription; |
Marissa Wall | 87c8ba7 | 2019-06-20 14:20:52 -0700 | [diff] [blame] | 44 | |
| 45 | namespace android { |
| 46 | |
| 47 | namespace { |
| 48 | |
| 49 | static constexpr Error kTransactionError = Error::NO_RESOURCES; |
| 50 | |
| 51 | uint64_t getValidUsageBits() { |
| 52 | static const uint64_t validUsageBits = []() -> uint64_t { |
| 53 | uint64_t bits = 0; |
| 54 | for (const auto bit : |
| 55 | hardware::hidl_enum_range<hardware::graphics::common::V1_2::BufferUsage>()) { |
| 56 | bits = bits | bit; |
| 57 | } |
| 58 | return bits; |
| 59 | }(); |
| 60 | return validUsageBits; |
| 61 | } |
| 62 | |
| 63 | static inline IMapper::Rect sGralloc4Rect(const Rect& rect) { |
| 64 | IMapper::Rect outRect{}; |
| 65 | outRect.left = rect.left; |
| 66 | outRect.top = rect.top; |
| 67 | outRect.width = rect.width(); |
| 68 | outRect.height = rect.height(); |
| 69 | return outRect; |
| 70 | } |
Marissa Wall | 22b2de1 | 2019-12-02 18:11:43 -0800 | [diff] [blame] | 71 | static inline void sBufferDescriptorInfo(std::string name, uint32_t width, uint32_t height, |
| 72 | PixelFormat format, uint32_t layerCount, uint64_t usage, |
Marissa Wall | 87c8ba7 | 2019-06-20 14:20:52 -0700 | [diff] [blame] | 73 | IMapper::BufferDescriptorInfo* outDescriptorInfo) { |
Marissa Wall | 22b2de1 | 2019-12-02 18:11:43 -0800 | [diff] [blame] | 74 | outDescriptorInfo->name = name; |
Marissa Wall | 87c8ba7 | 2019-06-20 14:20:52 -0700 | [diff] [blame] | 75 | outDescriptorInfo->width = width; |
| 76 | outDescriptorInfo->height = height; |
| 77 | outDescriptorInfo->layerCount = layerCount; |
| 78 | outDescriptorInfo->format = static_cast<hardware::graphics::common::V1_2::PixelFormat>(format); |
| 79 | outDescriptorInfo->usage = usage; |
| 80 | } |
| 81 | |
| 82 | } // anonymous namespace |
| 83 | |
| 84 | void Gralloc4Mapper::preload() { |
| 85 | android::hardware::preloadPassthroughService<IMapper>(); |
| 86 | } |
| 87 | |
| 88 | Gralloc4Mapper::Gralloc4Mapper() { |
| 89 | mMapper = IMapper::getService(); |
| 90 | if (mMapper == nullptr) { |
| 91 | ALOGI("mapper 4.x is not supported"); |
| 92 | return; |
| 93 | } |
| 94 | if (mMapper->isRemote()) { |
| 95 | LOG_ALWAYS_FATAL("gralloc-mapper must be in passthrough mode"); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | bool Gralloc4Mapper::isLoaded() const { |
| 100 | return mMapper != nullptr; |
| 101 | } |
| 102 | |
| 103 | status_t Gralloc4Mapper::validateBufferDescriptorInfo( |
| 104 | IMapper::BufferDescriptorInfo* descriptorInfo) const { |
| 105 | uint64_t validUsageBits = getValidUsageBits(); |
| 106 | |
| 107 | if (descriptorInfo->usage & ~validUsageBits) { |
| 108 | ALOGE("buffer descriptor contains invalid usage bits 0x%" PRIx64, |
| 109 | descriptorInfo->usage & ~validUsageBits); |
| 110 | return BAD_VALUE; |
| 111 | } |
| 112 | return NO_ERROR; |
| 113 | } |
| 114 | |
| 115 | status_t Gralloc4Mapper::createDescriptor(void* bufferDescriptorInfo, |
| 116 | void* outBufferDescriptor) const { |
| 117 | IMapper::BufferDescriptorInfo* descriptorInfo = |
| 118 | static_cast<IMapper::BufferDescriptorInfo*>(bufferDescriptorInfo); |
| 119 | BufferDescriptor* outDescriptor = static_cast<BufferDescriptor*>(outBufferDescriptor); |
| 120 | |
| 121 | status_t status = validateBufferDescriptorInfo(descriptorInfo); |
| 122 | if (status != NO_ERROR) { |
| 123 | return status; |
| 124 | } |
| 125 | |
| 126 | Error error; |
| 127 | auto hidl_cb = [&](const auto& tmpError, const auto& tmpDescriptor) { |
| 128 | error = tmpError; |
| 129 | if (error != Error::NONE) { |
| 130 | return; |
| 131 | } |
| 132 | *outDescriptor = tmpDescriptor; |
| 133 | }; |
| 134 | |
| 135 | hardware::Return<void> ret = mMapper->createDescriptor(*descriptorInfo, hidl_cb); |
| 136 | |
| 137 | return static_cast<status_t>((ret.isOk()) ? error : kTransactionError); |
| 138 | } |
| 139 | |
| 140 | status_t Gralloc4Mapper::importBuffer(const hardware::hidl_handle& rawHandle, |
| 141 | buffer_handle_t* outBufferHandle) const { |
| 142 | Error error; |
| 143 | auto ret = mMapper->importBuffer(rawHandle, [&](const auto& tmpError, const auto& tmpBuffer) { |
| 144 | error = tmpError; |
| 145 | if (error != Error::NONE) { |
| 146 | return; |
| 147 | } |
| 148 | *outBufferHandle = static_cast<buffer_handle_t>(tmpBuffer); |
| 149 | }); |
| 150 | |
| 151 | return static_cast<status_t>((ret.isOk()) ? error : kTransactionError); |
| 152 | } |
| 153 | |
| 154 | void Gralloc4Mapper::freeBuffer(buffer_handle_t bufferHandle) const { |
| 155 | auto buffer = const_cast<native_handle_t*>(bufferHandle); |
| 156 | auto ret = mMapper->freeBuffer(buffer); |
| 157 | |
| 158 | auto error = (ret.isOk()) ? static_cast<Error>(ret) : kTransactionError; |
| 159 | ALOGE_IF(error != Error::NONE, "freeBuffer(%p) failed with %d", buffer, error); |
| 160 | } |
| 161 | |
| 162 | status_t Gralloc4Mapper::validateBufferSize(buffer_handle_t bufferHandle, uint32_t width, |
Marissa Wall | 22b2de1 | 2019-12-02 18:11:43 -0800 | [diff] [blame] | 163 | uint32_t height, PixelFormat format, |
Marissa Wall | 87c8ba7 | 2019-06-20 14:20:52 -0700 | [diff] [blame] | 164 | uint32_t layerCount, uint64_t usage, |
| 165 | uint32_t stride) const { |
| 166 | IMapper::BufferDescriptorInfo descriptorInfo; |
Marissa Wall | 22b2de1 | 2019-12-02 18:11:43 -0800 | [diff] [blame] | 167 | sBufferDescriptorInfo("validateBufferSize", width, height, format, layerCount, usage, |
| 168 | &descriptorInfo); |
Marissa Wall | 87c8ba7 | 2019-06-20 14:20:52 -0700 | [diff] [blame] | 169 | |
| 170 | auto buffer = const_cast<native_handle_t*>(bufferHandle); |
| 171 | auto ret = mMapper->validateBufferSize(buffer, descriptorInfo, stride); |
| 172 | |
| 173 | return static_cast<status_t>((ret.isOk()) ? static_cast<Error>(ret) : kTransactionError); |
| 174 | } |
| 175 | |
| 176 | void Gralloc4Mapper::getTransportSize(buffer_handle_t bufferHandle, uint32_t* outNumFds, |
| 177 | uint32_t* outNumInts) const { |
| 178 | *outNumFds = uint32_t(bufferHandle->numFds); |
| 179 | *outNumInts = uint32_t(bufferHandle->numInts); |
| 180 | |
| 181 | Error error; |
| 182 | auto buffer = const_cast<native_handle_t*>(bufferHandle); |
| 183 | auto ret = mMapper->getTransportSize(buffer, |
| 184 | [&](const auto& tmpError, const auto& tmpNumFds, |
| 185 | const auto& tmpNumInts) { |
| 186 | error = tmpError; |
| 187 | if (error != Error::NONE) { |
| 188 | return; |
| 189 | } |
| 190 | *outNumFds = tmpNumFds; |
| 191 | *outNumInts = tmpNumInts; |
| 192 | }); |
| 193 | |
| 194 | error = (ret.isOk()) ? error : kTransactionError; |
| 195 | |
| 196 | ALOGE_IF(error != Error::NONE, "getTransportSize(%p) failed with %d", buffer, error); |
| 197 | } |
| 198 | |
| 199 | status_t Gralloc4Mapper::lock(buffer_handle_t bufferHandle, uint64_t usage, const Rect& bounds, |
| 200 | int acquireFence, void** outData, int32_t* outBytesPerPixel, |
| 201 | int32_t* outBytesPerStride) const { |
Marissa Wall | 22b2de1 | 2019-12-02 18:11:43 -0800 | [diff] [blame] | 202 | std::vector<ui::PlaneLayout> planeLayouts; |
| 203 | status_t err = getPlaneLayouts(bufferHandle, &planeLayouts); |
| 204 | |
| 205 | if (err != NO_ERROR && !planeLayouts.empty()) { |
| 206 | if (outBytesPerPixel) { |
| 207 | int32_t bitsPerPixel = planeLayouts.front().sampleIncrementInBits; |
| 208 | for (const auto& planeLayout : planeLayouts) { |
| 209 | if (bitsPerPixel != planeLayout.sampleIncrementInBits) { |
| 210 | bitsPerPixel = -1; |
| 211 | } |
| 212 | } |
| 213 | if (bitsPerPixel >= 0 && bitsPerPixel % 8 == 0) { |
| 214 | *outBytesPerPixel = bitsPerPixel / 8; |
| 215 | } else { |
| 216 | *outBytesPerPixel = -1; |
| 217 | } |
| 218 | } |
| 219 | if (outBytesPerStride) { |
| 220 | int32_t bytesPerStride = planeLayouts.front().strideInBytes; |
| 221 | for (const auto& planeLayout : planeLayouts) { |
| 222 | if (bytesPerStride != planeLayout.strideInBytes) { |
| 223 | bytesPerStride = -1; |
| 224 | } |
| 225 | } |
| 226 | if (bytesPerStride >= 0) { |
| 227 | *outBytesPerStride = bytesPerStride; |
| 228 | } else { |
| 229 | *outBytesPerStride = -1; |
| 230 | } |
| 231 | } |
Marissa Wall | 20611c6 | 2019-11-05 15:06:24 -0800 | [diff] [blame] | 232 | } |
| 233 | |
Marissa Wall | 87c8ba7 | 2019-06-20 14:20:52 -0700 | [diff] [blame] | 234 | auto buffer = const_cast<native_handle_t*>(bufferHandle); |
| 235 | |
| 236 | IMapper::Rect accessRegion = sGralloc4Rect(bounds); |
| 237 | |
| 238 | // put acquireFence in a hidl_handle |
| 239 | hardware::hidl_handle acquireFenceHandle; |
| 240 | NATIVE_HANDLE_DECLARE_STORAGE(acquireFenceStorage, 1, 0); |
| 241 | if (acquireFence >= 0) { |
| 242 | auto h = native_handle_init(acquireFenceStorage, 1, 0); |
| 243 | h->data[0] = acquireFence; |
| 244 | acquireFenceHandle = h; |
| 245 | } |
| 246 | |
| 247 | Error error; |
| 248 | auto ret = mMapper->lock(buffer, usage, accessRegion, acquireFenceHandle, |
Marissa Wall | 20611c6 | 2019-11-05 15:06:24 -0800 | [diff] [blame] | 249 | [&](const auto& tmpError, const auto& tmpData) { |
Marissa Wall | 87c8ba7 | 2019-06-20 14:20:52 -0700 | [diff] [blame] | 250 | error = tmpError; |
| 251 | if (error != Error::NONE) { |
| 252 | return; |
| 253 | } |
| 254 | *outData = tmpData; |
Marissa Wall | 87c8ba7 | 2019-06-20 14:20:52 -0700 | [diff] [blame] | 255 | }); |
| 256 | |
| 257 | // we own acquireFence even on errors |
| 258 | if (acquireFence >= 0) { |
| 259 | close(acquireFence); |
| 260 | } |
| 261 | |
| 262 | error = (ret.isOk()) ? error : kTransactionError; |
| 263 | |
| 264 | ALOGW_IF(error != Error::NONE, "lock(%p, ...) failed: %d", bufferHandle, error); |
| 265 | |
| 266 | return static_cast<status_t>(error); |
| 267 | } |
| 268 | |
Marissa Wall | 22b2de1 | 2019-12-02 18:11:43 -0800 | [diff] [blame] | 269 | status_t Gralloc4Mapper::lock(buffer_handle_t bufferHandle, uint64_t usage, const Rect& bounds, |
| 270 | int acquireFence, android_ycbcr* outYcbcr) const { |
| 271 | if (!outYcbcr) { |
| 272 | return BAD_VALUE; |
| 273 | } |
| 274 | |
| 275 | std::vector<ui::PlaneLayout> planeLayouts; |
| 276 | status_t error = getPlaneLayouts(bufferHandle, &planeLayouts); |
| 277 | if (error != NO_ERROR) { |
| 278 | return error; |
| 279 | } |
| 280 | |
| 281 | void* data = nullptr; |
| 282 | error = lock(bufferHandle, usage, bounds, acquireFence, &data, nullptr, nullptr); |
| 283 | if (error != NO_ERROR) { |
| 284 | return error; |
| 285 | } |
| 286 | |
| 287 | android_ycbcr ycbcr; |
| 288 | |
| 289 | ycbcr.y = nullptr; |
| 290 | ycbcr.cb = nullptr; |
| 291 | ycbcr.cr = nullptr; |
| 292 | ycbcr.ystride = 0; |
| 293 | ycbcr.cstride = 0; |
| 294 | ycbcr.chroma_step = 0; |
| 295 | |
| 296 | for (const auto& planeLayout : planeLayouts) { |
| 297 | for (const auto& planeLayoutComponent : planeLayout.components) { |
| 298 | if (!gralloc4::isStandardPlaneLayoutComponentType(planeLayoutComponent.type)) { |
| 299 | continue; |
| 300 | } |
| 301 | if (0 != planeLayoutComponent.offsetInBits % 8) { |
| 302 | unlock(bufferHandle); |
| 303 | return BAD_VALUE; |
| 304 | } |
| 305 | |
| 306 | uint8_t* tmpData = static_cast<uint8_t*>(data) + planeLayout.offsetInBytes + |
| 307 | (planeLayoutComponent.offsetInBits / 8); |
| 308 | uint64_t sampleIncrementInBytes; |
| 309 | |
| 310 | auto type = static_cast<PlaneLayoutComponentType>(planeLayoutComponent.type.value); |
| 311 | switch (type) { |
| 312 | case PlaneLayoutComponentType::Y: |
| 313 | if ((ycbcr.y != nullptr) || (planeLayoutComponent.sizeInBits != 8) || |
| 314 | (planeLayout.sampleIncrementInBits != 8)) { |
| 315 | unlock(bufferHandle); |
| 316 | return BAD_VALUE; |
| 317 | } |
| 318 | ycbcr.y = tmpData; |
| 319 | ycbcr.ystride = planeLayout.strideInBytes; |
| 320 | break; |
| 321 | |
| 322 | case PlaneLayoutComponentType::CB: |
| 323 | case PlaneLayoutComponentType::CR: |
| 324 | if (planeLayout.sampleIncrementInBits % 8 != 0) { |
| 325 | unlock(bufferHandle); |
| 326 | return BAD_VALUE; |
| 327 | } |
| 328 | |
| 329 | sampleIncrementInBytes = planeLayout.sampleIncrementInBits / 8; |
| 330 | if ((sampleIncrementInBytes != 1) && (sampleIncrementInBytes != 2)) { |
| 331 | unlock(bufferHandle); |
| 332 | return BAD_VALUE; |
| 333 | } |
| 334 | |
| 335 | if (ycbcr.cstride == 0 && ycbcr.chroma_step == 0) { |
| 336 | ycbcr.cstride = planeLayout.strideInBytes; |
| 337 | ycbcr.chroma_step = sampleIncrementInBytes; |
| 338 | } else { |
| 339 | if ((static_cast<int64_t>(ycbcr.cstride) != planeLayout.strideInBytes) || |
| 340 | (ycbcr.chroma_step != sampleIncrementInBytes)) { |
| 341 | unlock(bufferHandle); |
| 342 | return BAD_VALUE; |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | if (type == PlaneLayoutComponentType::CB) { |
| 347 | if (ycbcr.cb != nullptr) { |
| 348 | unlock(bufferHandle); |
| 349 | return BAD_VALUE; |
| 350 | } |
| 351 | ycbcr.cb = tmpData; |
| 352 | } else { |
| 353 | if (ycbcr.cr != nullptr) { |
| 354 | unlock(bufferHandle); |
| 355 | return BAD_VALUE; |
| 356 | } |
| 357 | ycbcr.cr = tmpData; |
| 358 | } |
| 359 | break; |
| 360 | default: |
| 361 | break; |
| 362 | }; |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | *outYcbcr = ycbcr; |
Marissa Wall | 20611c6 | 2019-11-05 15:06:24 -0800 | [diff] [blame] | 367 | return static_cast<status_t>(Error::UNSUPPORTED); |
Marissa Wall | 87c8ba7 | 2019-06-20 14:20:52 -0700 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | int Gralloc4Mapper::unlock(buffer_handle_t bufferHandle) const { |
| 371 | auto buffer = const_cast<native_handle_t*>(bufferHandle); |
| 372 | |
| 373 | int releaseFence = -1; |
| 374 | Error error; |
| 375 | auto ret = mMapper->unlock(buffer, [&](const auto& tmpError, const auto& tmpReleaseFence) { |
| 376 | error = tmpError; |
| 377 | if (error != Error::NONE) { |
| 378 | return; |
| 379 | } |
| 380 | |
| 381 | auto fenceHandle = tmpReleaseFence.getNativeHandle(); |
| 382 | if (fenceHandle && fenceHandle->numFds == 1) { |
| 383 | int fd = dup(fenceHandle->data[0]); |
| 384 | if (fd >= 0) { |
| 385 | releaseFence = fd; |
| 386 | } else { |
| 387 | ALOGD("failed to dup unlock release fence"); |
| 388 | sync_wait(fenceHandle->data[0], -1); |
| 389 | } |
| 390 | } |
| 391 | }); |
| 392 | |
| 393 | if (!ret.isOk()) { |
| 394 | error = kTransactionError; |
| 395 | } |
| 396 | |
| 397 | if (error != Error::NONE) { |
| 398 | ALOGE("unlock(%p) failed with %d", buffer, error); |
| 399 | } |
| 400 | |
| 401 | return releaseFence; |
| 402 | } |
| 403 | |
Marissa Wall | 22b2de1 | 2019-12-02 18:11:43 -0800 | [diff] [blame] | 404 | status_t Gralloc4Mapper::isSupported(uint32_t width, uint32_t height, PixelFormat format, |
Marissa Wall | 87c8ba7 | 2019-06-20 14:20:52 -0700 | [diff] [blame] | 405 | uint32_t layerCount, uint64_t usage, |
| 406 | bool* outSupported) const { |
| 407 | IMapper::BufferDescriptorInfo descriptorInfo; |
Marissa Wall | 22b2de1 | 2019-12-02 18:11:43 -0800 | [diff] [blame] | 408 | sBufferDescriptorInfo("isSupported", width, height, format, layerCount, usage, &descriptorInfo); |
Marissa Wall | 87c8ba7 | 2019-06-20 14:20:52 -0700 | [diff] [blame] | 409 | |
| 410 | Error error; |
| 411 | auto ret = mMapper->isSupported(descriptorInfo, |
| 412 | [&](const auto& tmpError, const auto& tmpSupported) { |
| 413 | error = tmpError; |
| 414 | if (error != Error::NONE) { |
| 415 | return; |
| 416 | } |
| 417 | if (outSupported) { |
| 418 | *outSupported = tmpSupported; |
| 419 | } |
| 420 | }); |
| 421 | |
| 422 | if (!ret.isOk()) { |
| 423 | error = kTransactionError; |
| 424 | } |
| 425 | |
| 426 | if (error != Error::NONE) { |
| 427 | ALOGE("isSupported(%u, %u, %d, %u, ...) failed with %d", width, height, format, layerCount, |
| 428 | error); |
| 429 | } |
| 430 | |
| 431 | return static_cast<status_t>(error); |
| 432 | } |
| 433 | |
Marissa Wall | 22b2de1 | 2019-12-02 18:11:43 -0800 | [diff] [blame] | 434 | template <class T> |
| 435 | status_t Gralloc4Mapper::get(buffer_handle_t bufferHandle, const MetadataType& metadataType, |
| 436 | DecodeFunction<T> decodeFunction, T* outMetadata) const { |
| 437 | if (!outMetadata) { |
| 438 | return BAD_VALUE; |
| 439 | } |
| 440 | |
| 441 | hidl_vec<uint8_t> vec; |
| 442 | Error error; |
| 443 | auto ret = mMapper->get(const_cast<native_handle_t*>(bufferHandle), metadataType, |
| 444 | [&](const auto& tmpError, const hidl_vec<uint8_t>& tmpVec) { |
| 445 | error = tmpError; |
| 446 | vec = tmpVec; |
| 447 | }); |
| 448 | |
| 449 | if (!ret.isOk()) { |
| 450 | error = kTransactionError; |
| 451 | } |
| 452 | |
| 453 | if (error != Error::NONE) { |
| 454 | ALOGE("get(%s, %" PRIu64 ", ...) failed with %d", metadataType.name.c_str(), |
| 455 | metadataType.value, error); |
| 456 | return static_cast<status_t>(error); |
| 457 | } |
| 458 | |
| 459 | return decodeFunction(vec, outMetadata); |
| 460 | } |
| 461 | |
| 462 | status_t Gralloc4Mapper::getBufferId(buffer_handle_t bufferHandle, uint64_t* outBufferId) const { |
| 463 | return get(bufferHandle, gralloc4::MetadataType_BufferId, gralloc4::decodeBufferId, |
| 464 | outBufferId); |
| 465 | } |
| 466 | |
| 467 | status_t Gralloc4Mapper::getName(buffer_handle_t bufferHandle, std::string* outName) const { |
| 468 | return get(bufferHandle, gralloc4::MetadataType_Name, gralloc4::decodeName, outName); |
| 469 | } |
| 470 | |
| 471 | status_t Gralloc4Mapper::getWidth(buffer_handle_t bufferHandle, uint64_t* outWidth) const { |
| 472 | return get(bufferHandle, gralloc4::MetadataType_Width, gralloc4::decodeWidth, outWidth); |
| 473 | } |
| 474 | |
| 475 | status_t Gralloc4Mapper::getHeight(buffer_handle_t bufferHandle, uint64_t* outHeight) const { |
| 476 | return get(bufferHandle, gralloc4::MetadataType_Height, gralloc4::decodeHeight, outHeight); |
| 477 | } |
| 478 | |
| 479 | status_t Gralloc4Mapper::getLayerCount(buffer_handle_t bufferHandle, |
| 480 | uint64_t* outLayerCount) const { |
| 481 | return get(bufferHandle, gralloc4::MetadataType_LayerCount, gralloc4::decodeLayerCount, |
| 482 | outLayerCount); |
| 483 | } |
| 484 | |
| 485 | status_t Gralloc4Mapper::getPixelFormatRequested(buffer_handle_t bufferHandle, |
| 486 | ui::PixelFormat* outPixelFormatRequested) const { |
| 487 | return get(bufferHandle, gralloc4::MetadataType_PixelFormatRequested, |
| 488 | gralloc4::decodePixelFormatRequested, outPixelFormatRequested); |
| 489 | } |
| 490 | |
| 491 | status_t Gralloc4Mapper::getPixelFormatFourCC(buffer_handle_t bufferHandle, |
| 492 | uint32_t* outPixelFormatFourCC) const { |
| 493 | return get(bufferHandle, gralloc4::MetadataType_PixelFormatFourCC, |
| 494 | gralloc4::decodePixelFormatFourCC, outPixelFormatFourCC); |
| 495 | } |
| 496 | |
| 497 | status_t Gralloc4Mapper::getPixelFormatModifier(buffer_handle_t bufferHandle, |
| 498 | uint64_t* outPixelFormatModifier) const { |
| 499 | return get(bufferHandle, gralloc4::MetadataType_PixelFormatModifier, |
| 500 | gralloc4::decodePixelFormatModifier, outPixelFormatModifier); |
| 501 | } |
| 502 | |
| 503 | status_t Gralloc4Mapper::getUsage(buffer_handle_t bufferHandle, uint64_t* outUsage) const { |
| 504 | return get(bufferHandle, gralloc4::MetadataType_Usage, gralloc4::decodeUsage, outUsage); |
| 505 | } |
| 506 | |
| 507 | status_t Gralloc4Mapper::getAllocationSize(buffer_handle_t bufferHandle, |
| 508 | uint64_t* outAllocationSize) const { |
| 509 | return get(bufferHandle, gralloc4::MetadataType_AllocationSize, gralloc4::decodeAllocationSize, |
| 510 | outAllocationSize); |
| 511 | } |
| 512 | |
| 513 | status_t Gralloc4Mapper::getProtectedContent(buffer_handle_t bufferHandle, |
| 514 | uint64_t* outProtectedContent) const { |
| 515 | return get(bufferHandle, gralloc4::MetadataType_ProtectedContent, |
| 516 | gralloc4::decodeProtectedContent, outProtectedContent); |
| 517 | } |
| 518 | |
| 519 | status_t Gralloc4Mapper::getCompression(buffer_handle_t bufferHandle, |
| 520 | ExtendableType* outCompression) const { |
| 521 | return get(bufferHandle, gralloc4::MetadataType_Compression, gralloc4::decodeCompression, |
| 522 | outCompression); |
| 523 | } |
| 524 | |
| 525 | status_t Gralloc4Mapper::getCompression(buffer_handle_t bufferHandle, |
| 526 | ui::Compression* outCompression) const { |
| 527 | if (!outCompression) { |
| 528 | return BAD_VALUE; |
| 529 | } |
| 530 | ExtendableType compression; |
| 531 | status_t error = getCompression(bufferHandle, &compression); |
| 532 | if (error) { |
| 533 | return error; |
| 534 | } |
| 535 | if (!gralloc4::isStandardCompression(compression)) { |
| 536 | return BAD_TYPE; |
| 537 | } |
| 538 | *outCompression = gralloc4::getStandardCompressionValue(compression); |
| 539 | return NO_ERROR; |
| 540 | } |
| 541 | |
| 542 | status_t Gralloc4Mapper::getInterlaced(buffer_handle_t bufferHandle, |
| 543 | ExtendableType* outInterlaced) const { |
| 544 | return get(bufferHandle, gralloc4::MetadataType_Interlaced, gralloc4::decodeInterlaced, |
| 545 | outInterlaced); |
| 546 | } |
| 547 | |
| 548 | status_t Gralloc4Mapper::getInterlaced(buffer_handle_t bufferHandle, |
| 549 | ui::Interlaced* outInterlaced) const { |
| 550 | if (!outInterlaced) { |
| 551 | return BAD_VALUE; |
| 552 | } |
| 553 | ExtendableType interlaced; |
| 554 | status_t error = getInterlaced(bufferHandle, &interlaced); |
| 555 | if (error) { |
| 556 | return error; |
| 557 | } |
| 558 | if (!gralloc4::isStandardInterlaced(interlaced)) { |
| 559 | return BAD_TYPE; |
| 560 | } |
| 561 | *outInterlaced = gralloc4::getStandardInterlacedValue(interlaced); |
| 562 | return NO_ERROR; |
| 563 | } |
| 564 | |
| 565 | status_t Gralloc4Mapper::getChromaSiting(buffer_handle_t bufferHandle, |
| 566 | ExtendableType* outChromaSiting) const { |
| 567 | return get(bufferHandle, gralloc4::MetadataType_ChromaSiting, gralloc4::decodeChromaSiting, |
| 568 | outChromaSiting); |
| 569 | } |
| 570 | |
| 571 | status_t Gralloc4Mapper::getChromaSiting(buffer_handle_t bufferHandle, |
| 572 | ui::ChromaSiting* outChromaSiting) const { |
| 573 | if (!outChromaSiting) { |
| 574 | return BAD_VALUE; |
| 575 | } |
| 576 | ExtendableType chromaSiting; |
| 577 | status_t error = getChromaSiting(bufferHandle, &chromaSiting); |
| 578 | if (error) { |
| 579 | return error; |
| 580 | } |
| 581 | if (!gralloc4::isStandardChromaSiting(chromaSiting)) { |
| 582 | return BAD_TYPE; |
| 583 | } |
| 584 | *outChromaSiting = gralloc4::getStandardChromaSitingValue(chromaSiting); |
| 585 | return NO_ERROR; |
| 586 | } |
| 587 | |
| 588 | status_t Gralloc4Mapper::getPlaneLayouts(buffer_handle_t bufferHandle, |
| 589 | std::vector<ui::PlaneLayout>* outPlaneLayouts) const { |
| 590 | return get(bufferHandle, gralloc4::MetadataType_PlaneLayouts, gralloc4::decodePlaneLayouts, |
| 591 | outPlaneLayouts); |
| 592 | } |
| 593 | |
| 594 | status_t Gralloc4Mapper::getDataspace(buffer_handle_t bufferHandle, |
| 595 | ui::Dataspace* outDataspace) const { |
| 596 | if (!outDataspace) { |
| 597 | return BAD_VALUE; |
| 598 | } |
| 599 | aidl::android::hardware::graphics::common::Dataspace dataspace; |
| 600 | status_t error = get(bufferHandle, gralloc4::MetadataType_Dataspace, gralloc4::decodeDataspace, |
| 601 | &dataspace); |
| 602 | if (error) { |
| 603 | return error; |
| 604 | } |
| 605 | |
| 606 | // Gralloc4 uses stable AIDL dataspace but the rest of the system still uses HIDL dataspace |
| 607 | *outDataspace = static_cast<ui::Dataspace>(dataspace); |
| 608 | return NO_ERROR; |
| 609 | } |
| 610 | |
| 611 | status_t Gralloc4Mapper::getBlendMode(buffer_handle_t bufferHandle, |
| 612 | ui::BlendMode* outBlendMode) const { |
| 613 | return get(bufferHandle, gralloc4::MetadataType_BlendMode, gralloc4::decodeBlendMode, |
| 614 | outBlendMode); |
| 615 | } |
| 616 | |
Marissa Wall | ef785e1 | 2019-12-12 14:26:59 -0800 | [diff] [blame^] | 617 | status_t Gralloc4Mapper::getSmpte2086(buffer_handle_t bufferHandle, |
| 618 | std::optional<ui::Smpte2086>* outSmpte2086) const { |
| 619 | return get(bufferHandle, gralloc4::MetadataType_Smpte2086, gralloc4::decodeSmpte2086, |
| 620 | outSmpte2086); |
| 621 | } |
| 622 | |
| 623 | status_t Gralloc4Mapper::getCta861_3(buffer_handle_t bufferHandle, |
| 624 | std::optional<ui::Cta861_3>* outCta861_3) const { |
| 625 | return get(bufferHandle, gralloc4::MetadataType_Cta861_3, gralloc4::decodeCta861_3, |
| 626 | outCta861_3); |
| 627 | } |
| 628 | |
| 629 | status_t Gralloc4Mapper::getSmpte2094_40( |
| 630 | buffer_handle_t bufferHandle, std::optional<std::vector<uint8_t>>* outSmpte2094_40) const { |
| 631 | return get(bufferHandle, gralloc4::MetadataType_Smpte2094_40, gralloc4::decodeSmpte2094_40, |
| 632 | outSmpte2094_40); |
| 633 | } |
| 634 | |
Marissa Wall | 22b2de1 | 2019-12-02 18:11:43 -0800 | [diff] [blame] | 635 | template <class T> |
| 636 | status_t Gralloc4Mapper::getDefault(uint32_t width, uint32_t height, PixelFormat format, |
| 637 | uint32_t layerCount, uint64_t usage, |
| 638 | const MetadataType& metadataType, |
| 639 | DecodeFunction<T> decodeFunction, T* outMetadata) const { |
| 640 | if (!outMetadata) { |
| 641 | return BAD_VALUE; |
| 642 | } |
| 643 | |
| 644 | IMapper::BufferDescriptorInfo descriptorInfo; |
| 645 | sBufferDescriptorInfo("getDefault", width, height, format, layerCount, usage, &descriptorInfo); |
| 646 | |
| 647 | hidl_vec<uint8_t> vec; |
| 648 | Error error; |
| 649 | auto ret = mMapper->getFromBufferDescriptorInfo(descriptorInfo, metadataType, |
| 650 | [&](const auto& tmpError, |
| 651 | const hidl_vec<uint8_t>& tmpVec) { |
| 652 | error = tmpError; |
| 653 | vec = tmpVec; |
| 654 | }); |
| 655 | |
| 656 | if (!ret.isOk()) { |
| 657 | error = kTransactionError; |
| 658 | } |
| 659 | |
| 660 | if (error != Error::NONE) { |
| 661 | ALOGE("getDefault(%s, %" PRIu64 ", ...) failed with %d", metadataType.name.c_str(), |
| 662 | metadataType.value, error); |
| 663 | return static_cast<status_t>(error); |
| 664 | } |
| 665 | |
| 666 | return decodeFunction(vec, outMetadata); |
| 667 | } |
| 668 | |
| 669 | status_t Gralloc4Mapper::getDefaultPixelFormatFourCC(uint32_t width, uint32_t height, |
| 670 | PixelFormat format, uint32_t layerCount, |
| 671 | uint64_t usage, |
| 672 | uint32_t* outPixelFormatFourCC) const { |
| 673 | return getDefault(width, height, format, layerCount, usage, |
| 674 | gralloc4::MetadataType_PixelFormatFourCC, gralloc4::decodePixelFormatFourCC, |
| 675 | outPixelFormatFourCC); |
| 676 | } |
| 677 | |
| 678 | status_t Gralloc4Mapper::getDefaultPixelFormatModifier(uint32_t width, uint32_t height, |
| 679 | PixelFormat format, uint32_t layerCount, |
| 680 | uint64_t usage, |
| 681 | uint64_t* outPixelFormatModifier) const { |
| 682 | return getDefault(width, height, format, layerCount, usage, |
| 683 | gralloc4::MetadataType_PixelFormatModifier, |
| 684 | gralloc4::decodePixelFormatModifier, outPixelFormatModifier); |
| 685 | } |
| 686 | |
| 687 | status_t Gralloc4Mapper::getDefaultAllocationSize(uint32_t width, uint32_t height, |
| 688 | PixelFormat format, uint32_t layerCount, |
| 689 | uint64_t usage, |
| 690 | uint64_t* outAllocationSize) const { |
| 691 | return getDefault(width, height, format, layerCount, usage, |
| 692 | gralloc4::MetadataType_AllocationSize, gralloc4::decodeAllocationSize, |
| 693 | outAllocationSize); |
| 694 | } |
| 695 | |
| 696 | status_t Gralloc4Mapper::getDefaultProtectedContent(uint32_t width, uint32_t height, |
| 697 | PixelFormat format, uint32_t layerCount, |
| 698 | uint64_t usage, |
| 699 | uint64_t* outProtectedContent) const { |
| 700 | return getDefault(width, height, format, layerCount, usage, |
| 701 | gralloc4::MetadataType_ProtectedContent, gralloc4::decodeProtectedContent, |
| 702 | outProtectedContent); |
| 703 | } |
| 704 | |
| 705 | status_t Gralloc4Mapper::getDefaultCompression(uint32_t width, uint32_t height, PixelFormat format, |
| 706 | uint32_t layerCount, uint64_t usage, |
| 707 | ExtendableType* outCompression) const { |
| 708 | return getDefault(width, height, format, layerCount, usage, gralloc4::MetadataType_Compression, |
| 709 | gralloc4::decodeCompression, outCompression); |
| 710 | } |
| 711 | |
| 712 | status_t Gralloc4Mapper::getDefaultCompression(uint32_t width, uint32_t height, PixelFormat format, |
| 713 | uint32_t layerCount, uint64_t usage, |
| 714 | ui::Compression* outCompression) const { |
| 715 | if (!outCompression) { |
| 716 | return BAD_VALUE; |
| 717 | } |
| 718 | ExtendableType compression; |
| 719 | status_t error = getDefaultCompression(width, height, format, layerCount, usage, &compression); |
| 720 | if (error) { |
| 721 | return error; |
| 722 | } |
| 723 | if (!gralloc4::isStandardCompression(compression)) { |
| 724 | return BAD_TYPE; |
| 725 | } |
| 726 | *outCompression = gralloc4::getStandardCompressionValue(compression); |
| 727 | return NO_ERROR; |
| 728 | } |
| 729 | |
| 730 | status_t Gralloc4Mapper::getDefaultInterlaced(uint32_t width, uint32_t height, PixelFormat format, |
| 731 | uint32_t layerCount, uint64_t usage, |
| 732 | ExtendableType* outInterlaced) const { |
| 733 | return getDefault(width, height, format, layerCount, usage, gralloc4::MetadataType_Interlaced, |
| 734 | gralloc4::decodeInterlaced, outInterlaced); |
| 735 | } |
| 736 | |
| 737 | status_t Gralloc4Mapper::getDefaultInterlaced(uint32_t width, uint32_t height, PixelFormat format, |
| 738 | uint32_t layerCount, uint64_t usage, |
| 739 | ui::Interlaced* outInterlaced) const { |
| 740 | if (!outInterlaced) { |
| 741 | return BAD_VALUE; |
| 742 | } |
| 743 | ExtendableType interlaced; |
| 744 | status_t error = getDefaultInterlaced(width, height, format, layerCount, usage, &interlaced); |
| 745 | if (error) { |
| 746 | return error; |
| 747 | } |
| 748 | if (!gralloc4::isStandardInterlaced(interlaced)) { |
| 749 | return BAD_TYPE; |
| 750 | } |
| 751 | *outInterlaced = gralloc4::getStandardInterlacedValue(interlaced); |
| 752 | return NO_ERROR; |
| 753 | } |
| 754 | |
| 755 | status_t Gralloc4Mapper::getDefaultChromaSiting(uint32_t width, uint32_t height, PixelFormat format, |
| 756 | uint32_t layerCount, uint64_t usage, |
| 757 | ExtendableType* outChromaSiting) const { |
| 758 | return getDefault(width, height, format, layerCount, usage, gralloc4::MetadataType_ChromaSiting, |
| 759 | gralloc4::decodeChromaSiting, outChromaSiting); |
| 760 | } |
| 761 | |
| 762 | status_t Gralloc4Mapper::getDefaultChromaSiting(uint32_t width, uint32_t height, PixelFormat format, |
| 763 | uint32_t layerCount, uint64_t usage, |
| 764 | ui::ChromaSiting* outChromaSiting) const { |
| 765 | if (!outChromaSiting) { |
| 766 | return BAD_VALUE; |
| 767 | } |
| 768 | ExtendableType chromaSiting; |
| 769 | status_t error = |
| 770 | getDefaultChromaSiting(width, height, format, layerCount, usage, &chromaSiting); |
| 771 | if (error) { |
| 772 | return error; |
| 773 | } |
| 774 | if (!gralloc4::isStandardChromaSiting(chromaSiting)) { |
| 775 | return BAD_TYPE; |
| 776 | } |
| 777 | *outChromaSiting = gralloc4::getStandardChromaSitingValue(chromaSiting); |
| 778 | return NO_ERROR; |
| 779 | } |
| 780 | |
| 781 | status_t Gralloc4Mapper::getDefaultPlaneLayouts( |
| 782 | uint32_t width, uint32_t height, PixelFormat format, uint32_t layerCount, uint64_t usage, |
| 783 | std::vector<ui::PlaneLayout>* outPlaneLayouts) const { |
| 784 | return getDefault(width, height, format, layerCount, usage, gralloc4::MetadataType_PlaneLayouts, |
| 785 | gralloc4::decodePlaneLayouts, outPlaneLayouts); |
| 786 | } |
| 787 | |
| 788 | std::vector<MetadataTypeDescription> Gralloc4Mapper::listSupportedMetadataTypes() const { |
| 789 | hidl_vec<MetadataTypeDescription> descriptions; |
| 790 | Error error; |
| 791 | auto ret = mMapper->listSupportedMetadataTypes( |
| 792 | [&](const auto& tmpError, const auto& tmpDescriptions) { |
| 793 | error = tmpError; |
| 794 | descriptions = tmpDescriptions; |
| 795 | }); |
| 796 | |
| 797 | if (!ret.isOk()) { |
| 798 | error = kTransactionError; |
| 799 | } |
| 800 | |
| 801 | if (error != Error::NONE) { |
| 802 | ALOGE("listSupportedMetadataType() failed with %d", error); |
| 803 | return {}; |
| 804 | } |
| 805 | |
| 806 | return static_cast<std::vector<MetadataTypeDescription>>(descriptions); |
| 807 | } |
| 808 | |
| 809 | template <class T> |
| 810 | status_t Gralloc4Mapper::metadataDumpHelper(const BufferDump& bufferDump, |
| 811 | StandardMetadataType metadataType, |
| 812 | DecodeFunction<T> decodeFunction, T* outT) const { |
| 813 | const auto& metadataDump = bufferDump.metadataDump; |
| 814 | |
| 815 | auto itr = |
| 816 | std::find_if(metadataDump.begin(), metadataDump.end(), |
| 817 | [&](const MetadataDump& tmpMetadataDump) { |
| 818 | if (!gralloc4::isStandardMetadataType(tmpMetadataDump.metadataType)) { |
| 819 | return false; |
| 820 | } |
| 821 | return metadataType == |
| 822 | gralloc4::getStandardMetadataTypeValue( |
| 823 | tmpMetadataDump.metadataType); |
| 824 | }); |
| 825 | if (itr == metadataDump.end()) { |
| 826 | return BAD_VALUE; |
| 827 | } |
| 828 | |
| 829 | return decodeFunction(itr->metadata, outT); |
| 830 | } |
| 831 | |
| 832 | status_t Gralloc4Mapper::bufferDumpHelper(const BufferDump& bufferDump, std::ostringstream* outDump, |
| 833 | uint64_t* outAllocationSize, bool less) const { |
| 834 | uint64_t bufferId; |
| 835 | std::string name; |
| 836 | uint64_t width; |
| 837 | uint64_t height; |
| 838 | uint64_t layerCount; |
| 839 | ui::PixelFormat pixelFormatRequested; |
| 840 | uint32_t pixelFormatFourCC; |
| 841 | uint64_t pixelFormatModifier; |
| 842 | uint64_t usage; |
| 843 | uint64_t allocationSize; |
| 844 | uint64_t protectedContent; |
| 845 | ExtendableType compression; |
| 846 | ExtendableType interlaced; |
| 847 | ExtendableType chromaSiting; |
| 848 | std::vector<ui::PlaneLayout> planeLayouts; |
| 849 | |
| 850 | status_t error = metadataDumpHelper(bufferDump, StandardMetadataType::BUFFER_ID, |
| 851 | gralloc4::decodeBufferId, &bufferId); |
| 852 | if (error != NO_ERROR) { |
| 853 | return error; |
| 854 | } |
| 855 | error = metadataDumpHelper(bufferDump, StandardMetadataType::NAME, gralloc4::decodeName, &name); |
| 856 | if (error != NO_ERROR) { |
| 857 | return error; |
| 858 | } |
| 859 | error = metadataDumpHelper(bufferDump, StandardMetadataType::WIDTH, gralloc4::decodeWidth, |
| 860 | &width); |
| 861 | if (error != NO_ERROR) { |
| 862 | return error; |
| 863 | } |
| 864 | error = metadataDumpHelper(bufferDump, StandardMetadataType::HEIGHT, gralloc4::decodeHeight, |
| 865 | &height); |
| 866 | if (error != NO_ERROR) { |
| 867 | return error; |
| 868 | } |
| 869 | error = metadataDumpHelper(bufferDump, StandardMetadataType::LAYER_COUNT, |
| 870 | gralloc4::decodeLayerCount, &layerCount); |
| 871 | if (error != NO_ERROR) { |
| 872 | return error; |
| 873 | } |
| 874 | error = metadataDumpHelper(bufferDump, StandardMetadataType::PIXEL_FORMAT_REQUESTED, |
| 875 | gralloc4::decodePixelFormatRequested, &pixelFormatRequested); |
| 876 | if (error != NO_ERROR) { |
| 877 | return error; |
| 878 | } |
| 879 | error = metadataDumpHelper(bufferDump, StandardMetadataType::PIXEL_FORMAT_FOURCC, |
| 880 | gralloc4::decodePixelFormatFourCC, &pixelFormatFourCC); |
| 881 | if (error != NO_ERROR) { |
| 882 | return error; |
| 883 | } |
| 884 | error = metadataDumpHelper(bufferDump, StandardMetadataType::PIXEL_FORMAT_MODIFIER, |
| 885 | gralloc4::decodePixelFormatModifier, &pixelFormatModifier); |
| 886 | if (error != NO_ERROR) { |
| 887 | return error; |
| 888 | } |
| 889 | error = metadataDumpHelper(bufferDump, StandardMetadataType::USAGE, gralloc4::decodeUsage, |
| 890 | &usage); |
| 891 | if (error != NO_ERROR) { |
| 892 | return error; |
| 893 | } |
| 894 | error = metadataDumpHelper(bufferDump, StandardMetadataType::ALLOCATION_SIZE, |
| 895 | gralloc4::decodeAllocationSize, &allocationSize); |
| 896 | if (error != NO_ERROR) { |
| 897 | return error; |
| 898 | } |
| 899 | error = metadataDumpHelper(bufferDump, StandardMetadataType::PROTECTED_CONTENT, |
| 900 | gralloc4::decodeProtectedContent, &protectedContent); |
| 901 | if (error != NO_ERROR) { |
| 902 | return error; |
| 903 | } |
| 904 | error = metadataDumpHelper(bufferDump, StandardMetadataType::COMPRESSION, |
| 905 | gralloc4::decodeCompression, &compression); |
| 906 | if (error != NO_ERROR) { |
| 907 | return error; |
| 908 | } |
| 909 | error = metadataDumpHelper(bufferDump, StandardMetadataType::INTERLACED, |
| 910 | gralloc4::decodeInterlaced, &interlaced); |
| 911 | if (error != NO_ERROR) { |
| 912 | return error; |
| 913 | } |
| 914 | error = metadataDumpHelper(bufferDump, StandardMetadataType::CHROMA_SITING, |
| 915 | gralloc4::decodeChromaSiting, &chromaSiting); |
| 916 | if (error != NO_ERROR) { |
| 917 | return error; |
| 918 | } |
| 919 | error = metadataDumpHelper(bufferDump, StandardMetadataType::PLANE_LAYOUTS, |
| 920 | gralloc4::decodePlaneLayouts, &planeLayouts); |
| 921 | if (error != NO_ERROR) { |
| 922 | return error; |
| 923 | } |
| 924 | |
| 925 | if (outAllocationSize) { |
| 926 | *outAllocationSize = allocationSize; |
| 927 | } |
| 928 | double allocationSizeKiB = static_cast<double>(allocationSize) / 1024; |
| 929 | |
| 930 | *outDump << "+ name:" << name << ", id:" << bufferId << ", size:" << allocationSizeKiB |
| 931 | << "KiB, w/h:" << width << "x" << height << ", usage: 0x" << std::hex << usage |
| 932 | << std::dec << ", req fmt:" << static_cast<int32_t>(pixelFormatRequested) |
| 933 | << ", fourcc/mod:" << pixelFormatFourCC << "/" << pixelFormatModifier |
| 934 | << ", compressed: "; |
| 935 | |
| 936 | if (less) { |
| 937 | bool isCompressed = !gralloc4::isStandardCompression(compression) || |
| 938 | (gralloc4::getStandardCompressionValue(compression) != ui::Compression::NONE); |
| 939 | *outDump << std::boolalpha << isCompressed << "\n"; |
| 940 | } else { |
| 941 | *outDump << gralloc4::getCompressionName(compression) << "\n"; |
| 942 | } |
| 943 | |
| 944 | bool firstPlane = true; |
| 945 | for (const auto& planeLayout : planeLayouts) { |
| 946 | if (firstPlane) { |
| 947 | firstPlane = false; |
| 948 | *outDump << "\tplanes: "; |
| 949 | } else { |
| 950 | *outDump << "\t "; |
| 951 | } |
| 952 | |
| 953 | for (size_t i = 0; i < planeLayout.components.size(); i++) { |
| 954 | const auto& planeLayoutComponent = planeLayout.components[i]; |
| 955 | *outDump << gralloc4::getPlaneLayoutComponentTypeName(planeLayoutComponent.type); |
| 956 | if (i < planeLayout.components.size() - 1) { |
| 957 | *outDump << "/"; |
| 958 | } else { |
| 959 | *outDump << ":\t"; |
| 960 | } |
| 961 | } |
| 962 | *outDump << " w/h:" << planeLayout.widthInSamples << "x" << planeLayout.heightInSamples |
| 963 | << ", stride:" << planeLayout.strideInBytes |
| 964 | << " bytes, size:" << planeLayout.totalSizeInBytes; |
| 965 | if (!less) { |
| 966 | *outDump << ", inc:" << planeLayout.sampleIncrementInBits |
| 967 | << " bits, subsampling w/h:" << planeLayout.horizontalSubsampling << "x" |
| 968 | << planeLayout.verticalSubsampling; |
| 969 | } |
| 970 | *outDump << "\n"; |
| 971 | } |
| 972 | |
| 973 | if (!less) { |
| 974 | *outDump << "\tlayer cnt: " << layerCount << ", protected content: " << protectedContent |
| 975 | << ", interlaced: " << gralloc4::getInterlacedName(interlaced) |
| 976 | << ", chroma siting:" << gralloc4::getChromaSitingName(chromaSiting) << "\n"; |
| 977 | } |
| 978 | |
| 979 | return NO_ERROR; |
| 980 | } |
| 981 | |
| 982 | std::string Gralloc4Mapper::dumpBuffer(buffer_handle_t bufferHandle, bool less) const { |
| 983 | auto buffer = const_cast<native_handle_t*>(bufferHandle); |
| 984 | |
| 985 | BufferDump bufferDump; |
| 986 | Error error; |
| 987 | auto ret = mMapper->dumpBuffer(buffer, [&](const auto& tmpError, const auto& tmpBufferDump) { |
| 988 | error = tmpError; |
| 989 | bufferDump = tmpBufferDump; |
| 990 | }); |
| 991 | |
| 992 | if (!ret.isOk()) { |
| 993 | error = kTransactionError; |
| 994 | } |
| 995 | |
| 996 | if (error != Error::NONE) { |
| 997 | ALOGE("dumpBuffer() failed with %d", error); |
| 998 | return ""; |
| 999 | } |
| 1000 | |
| 1001 | std::ostringstream stream; |
| 1002 | stream.precision(2); |
| 1003 | |
| 1004 | status_t err = bufferDumpHelper(bufferDump, &stream, nullptr, less); |
| 1005 | if (err != NO_ERROR) { |
| 1006 | ALOGE("bufferDumpHelper() failed with %d", err); |
| 1007 | return ""; |
| 1008 | } |
| 1009 | |
| 1010 | return stream.str(); |
| 1011 | } |
| 1012 | |
| 1013 | std::string Gralloc4Mapper::dumpBuffers(bool less) const { |
| 1014 | hidl_vec<BufferDump> bufferDumps; |
| 1015 | Error error; |
| 1016 | auto ret = mMapper->dumpBuffers([&](const auto& tmpError, const auto& tmpBufferDump) { |
| 1017 | error = tmpError; |
| 1018 | bufferDumps = tmpBufferDump; |
| 1019 | }); |
| 1020 | |
| 1021 | if (!ret.isOk()) { |
| 1022 | error = kTransactionError; |
| 1023 | } |
| 1024 | |
| 1025 | if (error != Error::NONE) { |
| 1026 | ALOGE("dumpBuffer() failed with %d", error); |
| 1027 | return ""; |
| 1028 | } |
| 1029 | |
| 1030 | uint64_t totalAllocationSize = 0; |
| 1031 | std::ostringstream stream; |
| 1032 | stream.precision(2); |
| 1033 | |
| 1034 | stream << "Imported gralloc buffers:\n"; |
| 1035 | |
| 1036 | for (const auto& bufferDump : bufferDumps) { |
| 1037 | uint64_t allocationSize = 0; |
| 1038 | status_t err = bufferDumpHelper(bufferDump, &stream, &allocationSize, less); |
| 1039 | if (err != NO_ERROR) { |
| 1040 | ALOGE("bufferDumpHelper() failed with %d", err); |
| 1041 | return ""; |
| 1042 | } |
| 1043 | totalAllocationSize += allocationSize; |
| 1044 | } |
| 1045 | |
| 1046 | double totalAllocationSizeKiB = static_cast<double>(totalAllocationSize) / 1024; |
| 1047 | stream << "Total imported by gralloc: " << totalAllocationSizeKiB << "KiB\n"; |
| 1048 | return stream.str(); |
| 1049 | } |
| 1050 | |
Marissa Wall | 87c8ba7 | 2019-06-20 14:20:52 -0700 | [diff] [blame] | 1051 | Gralloc4Allocator::Gralloc4Allocator(const Gralloc4Mapper& mapper) : mMapper(mapper) { |
| 1052 | mAllocator = IAllocator::getService(); |
| 1053 | if (mAllocator == nullptr) { |
| 1054 | ALOGW("allocator 3.x is not supported"); |
| 1055 | return; |
| 1056 | } |
| 1057 | } |
| 1058 | |
| 1059 | bool Gralloc4Allocator::isLoaded() const { |
| 1060 | return mAllocator != nullptr; |
| 1061 | } |
| 1062 | |
Marissa Wall | 22b2de1 | 2019-12-02 18:11:43 -0800 | [diff] [blame] | 1063 | std::string Gralloc4Allocator::dumpDebugInfo(bool less) const { |
| 1064 | return mMapper.dumpBuffers(less); |
Marissa Wall | 87c8ba7 | 2019-06-20 14:20:52 -0700 | [diff] [blame] | 1065 | } |
| 1066 | |
Marissa Wall | 22b2de1 | 2019-12-02 18:11:43 -0800 | [diff] [blame] | 1067 | status_t Gralloc4Allocator::allocate(std::string requestorName, uint32_t width, uint32_t height, |
| 1068 | android::PixelFormat format, uint32_t layerCount, |
| 1069 | uint64_t usage, uint32_t bufferCount, uint32_t* outStride, |
| 1070 | buffer_handle_t* outBufferHandles, bool importBuffers) const { |
Marissa Wall | 87c8ba7 | 2019-06-20 14:20:52 -0700 | [diff] [blame] | 1071 | IMapper::BufferDescriptorInfo descriptorInfo; |
Marissa Wall | 22b2de1 | 2019-12-02 18:11:43 -0800 | [diff] [blame] | 1072 | sBufferDescriptorInfo(requestorName, width, height, format, layerCount, usage, &descriptorInfo); |
Marissa Wall | 87c8ba7 | 2019-06-20 14:20:52 -0700 | [diff] [blame] | 1073 | |
| 1074 | BufferDescriptor descriptor; |
| 1075 | status_t error = mMapper.createDescriptor(static_cast<void*>(&descriptorInfo), |
| 1076 | static_cast<void*>(&descriptor)); |
| 1077 | if (error != NO_ERROR) { |
| 1078 | return error; |
| 1079 | } |
| 1080 | |
| 1081 | auto ret = mAllocator->allocate(descriptor, bufferCount, |
| 1082 | [&](const auto& tmpError, const auto& tmpStride, |
| 1083 | const auto& tmpBuffers) { |
| 1084 | error = static_cast<status_t>(tmpError); |
| 1085 | if (tmpError != Error::NONE) { |
| 1086 | return; |
| 1087 | } |
| 1088 | |
Marissa Wall | bfcf81f | 2019-11-27 10:36:29 -0800 | [diff] [blame] | 1089 | if (importBuffers) { |
| 1090 | for (uint32_t i = 0; i < bufferCount; i++) { |
| 1091 | error = mMapper.importBuffer(tmpBuffers[i], |
| 1092 | &outBufferHandles[i]); |
| 1093 | if (error != NO_ERROR) { |
| 1094 | for (uint32_t j = 0; j < i; j++) { |
| 1095 | mMapper.freeBuffer(outBufferHandles[j]); |
| 1096 | outBufferHandles[j] = nullptr; |
| 1097 | } |
| 1098 | return; |
Marissa Wall | 87c8ba7 | 2019-06-20 14:20:52 -0700 | [diff] [blame] | 1099 | } |
Marissa Wall | bfcf81f | 2019-11-27 10:36:29 -0800 | [diff] [blame] | 1100 | } |
| 1101 | } else { |
| 1102 | for (uint32_t i = 0; i < bufferCount; i++) { |
| 1103 | outBufferHandles[i] = native_handle_clone( |
| 1104 | tmpBuffers[i].getNativeHandle()); |
| 1105 | if (!outBufferHandles[i]) { |
| 1106 | for (uint32_t j = 0; j < i; j++) { |
| 1107 | auto buffer = const_cast<native_handle_t*>( |
| 1108 | outBufferHandles[j]); |
| 1109 | native_handle_close(buffer); |
| 1110 | native_handle_delete(buffer); |
| 1111 | outBufferHandles[j] = nullptr; |
| 1112 | } |
| 1113 | } |
Marissa Wall | 87c8ba7 | 2019-06-20 14:20:52 -0700 | [diff] [blame] | 1114 | } |
| 1115 | } |
| 1116 | *outStride = tmpStride; |
| 1117 | }); |
| 1118 | |
| 1119 | // make sure the kernel driver sees BC_FREE_BUFFER and closes the fds now |
| 1120 | hardware::IPCThreadState::self()->flushCommands(); |
| 1121 | |
| 1122 | return (ret.isOk()) ? error : static_cast<status_t>(kTransactionError); |
| 1123 | } |
| 1124 | |
| 1125 | } // namespace android |