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