John Reck | 0ff95c9 | 2022-12-08 11:45:29 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2022 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 "Gralloc5" |
| 18 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 19 | |
| 20 | #include <ui/Gralloc5.h> |
| 21 | |
| 22 | #include <aidlcommonsupport/NativeHandle.h> |
| 23 | #include <android/binder_manager.h> |
| 24 | #include <android/hardware/graphics/mapper/utils/IMapperMetadataTypes.h> |
Justin Yun | 404dbf2 | 2024-03-19 13:01:43 +0900 | [diff] [blame] | 25 | #include <android/llndk-versioning.h> |
John Reck | 0ff95c9 | 2022-12-08 11:45:29 -0500 | [diff] [blame] | 26 | #include <binder/IPCThreadState.h> |
| 27 | #include <dlfcn.h> |
| 28 | #include <ui/FatVector.h> |
| 29 | #include <vndksupport/linker.h> |
| 30 | |
| 31 | using namespace aidl::android::hardware::graphics::allocator; |
| 32 | using namespace aidl::android::hardware::graphics::common; |
| 33 | using namespace ::android::hardware::graphics::mapper; |
| 34 | |
John Reck | edcd885 | 2023-07-25 16:03:44 -0400 | [diff] [blame] | 35 | using ADataspace = aidl::android::hardware::graphics::common::Dataspace; |
| 36 | using APixelFormat = aidl::android::hardware::graphics::common::PixelFormat; |
| 37 | |
John Reck | 0ff95c9 | 2022-12-08 11:45:29 -0500 | [diff] [blame] | 38 | namespace android { |
| 39 | |
| 40 | static const auto kIAllocatorServiceName = IAllocator::descriptor + std::string("/default"); |
| 41 | static const auto kIAllocatorMinimumVersion = 2; |
John Reck | edcd885 | 2023-07-25 16:03:44 -0400 | [diff] [blame] | 42 | constexpr const char* kStandardMetadataName = |
| 43 | "android.hardware.graphics.common.StandardMetadataType"; |
John Reck | 0ff95c9 | 2022-12-08 11:45:29 -0500 | [diff] [blame] | 44 | |
| 45 | // TODO(b/72323293, b/72703005): Remove these invalid bits from callers |
| 46 | static constexpr uint64_t kRemovedUsageBits = static_cast<uint64_t>((1 << 10) | (1 << 13)); |
| 47 | |
| 48 | typedef AIMapper_Error (*AIMapper_loadIMapperFn)(AIMapper *_Nullable *_Nonnull outImplementation); |
| 49 | |
| 50 | struct Gralloc5 { |
| 51 | std::shared_ptr<IAllocator> allocator; |
| 52 | AIMapper *mapper = nullptr; |
| 53 | }; |
| 54 | |
| 55 | static std::shared_ptr<IAllocator> waitForAllocator() { |
| 56 | if (__builtin_available(android 31, *)) { |
| 57 | if (!AServiceManager_isDeclared(kIAllocatorServiceName.c_str())) { |
| 58 | return nullptr; |
| 59 | } |
| 60 | auto allocator = IAllocator::fromBinder( |
| 61 | ndk::SpAIBinder(AServiceManager_waitForService(kIAllocatorServiceName.c_str()))); |
| 62 | if (!allocator) { |
| 63 | ALOGE("AIDL IAllocator declared but failed to get service"); |
| 64 | return nullptr; |
| 65 | } |
| 66 | |
| 67 | int32_t version = 0; |
| 68 | if (!allocator->getInterfaceVersion(&version).isOk()) { |
| 69 | ALOGE("Failed to query interface version"); |
| 70 | return nullptr; |
| 71 | } |
| 72 | if (version < kIAllocatorMinimumVersion) { |
| 73 | return nullptr; |
| 74 | } |
| 75 | return allocator; |
| 76 | } else { |
| 77 | // TODO: LOG_ALWAYS_FATAL("libui is not backwards compatible"); |
| 78 | return nullptr; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | static void *loadIMapperLibrary() { |
| 83 | static void *imapperLibrary = []() -> void * { |
| 84 | auto allocator = waitForAllocator(); |
| 85 | std::string mapperSuffix; |
| 86 | auto status = allocator->getIMapperLibrarySuffix(&mapperSuffix); |
| 87 | if (!status.isOk()) { |
| 88 | ALOGE("Failed to get IMapper library suffix"); |
| 89 | return nullptr; |
| 90 | } |
| 91 | |
Jooyung Han | 75fc06f | 2024-02-03 03:56:59 +0900 | [diff] [blame] | 92 | void* so = nullptr; |
Jooyung Han | 4f61be2 | 2024-06-17 13:29:11 +0900 | [diff] [blame^] | 93 | if API_LEVEL_AT_LEAST (__ANDROID_API_V__, 202404) { |
Jooyung Han | 75fc06f | 2024-02-03 03:56:59 +0900 | [diff] [blame] | 94 | so = AServiceManager_openDeclaredPassthroughHal("mapper", mapperSuffix.c_str(), |
| 95 | RTLD_LOCAL | RTLD_NOW); |
| 96 | } else { |
| 97 | std::string lib_name = "mapper." + mapperSuffix + ".so"; |
| 98 | so = android_load_sphal_library(lib_name.c_str(), RTLD_LOCAL | RTLD_NOW); |
| 99 | } |
John Reck | 0ff95c9 | 2022-12-08 11:45:29 -0500 | [diff] [blame] | 100 | if (!so) { |
Jooyung Han | 75fc06f | 2024-02-03 03:56:59 +0900 | [diff] [blame] | 101 | ALOGE("Failed to load mapper.%s.so", mapperSuffix.c_str()); |
John Reck | 0ff95c9 | 2022-12-08 11:45:29 -0500 | [diff] [blame] | 102 | } |
| 103 | return so; |
| 104 | }(); |
| 105 | return imapperLibrary; |
| 106 | } |
| 107 | |
| 108 | static const Gralloc5 &getInstance() { |
| 109 | static Gralloc5 instance = []() { |
| 110 | auto allocator = waitForAllocator(); |
| 111 | if (!allocator) { |
| 112 | return Gralloc5{}; |
| 113 | } |
| 114 | void *so = loadIMapperLibrary(); |
| 115 | if (!so) { |
| 116 | return Gralloc5{}; |
| 117 | } |
| 118 | auto loadIMapper = (AIMapper_loadIMapperFn)dlsym(so, "AIMapper_loadIMapper"); |
| 119 | AIMapper *mapper = nullptr; |
| 120 | AIMapper_Error error = loadIMapper(&mapper); |
| 121 | if (error != AIMAPPER_ERROR_NONE) { |
| 122 | ALOGE("AIMapper_loadIMapper failed %d", error); |
| 123 | return Gralloc5{}; |
| 124 | } |
| 125 | return Gralloc5{std::move(allocator), mapper}; |
| 126 | }(); |
| 127 | return instance; |
| 128 | } |
| 129 | |
| 130 | template <StandardMetadataType T> |
| 131 | static auto getStandardMetadata(AIMapper *mapper, buffer_handle_t bufferHandle) |
| 132 | -> decltype(StandardMetadata<T>::value::decode(nullptr, 0)) { |
| 133 | using Value = typename StandardMetadata<T>::value; |
| 134 | // TODO: Tune for common-case better |
| 135 | FatVector<uint8_t, 128> buffer; |
| 136 | int32_t sizeRequired = mapper->v5.getStandardMetadata(bufferHandle, static_cast<int64_t>(T), |
| 137 | buffer.data(), buffer.size()); |
| 138 | if (sizeRequired < 0) { |
| 139 | ALOGW_IF(-AIMAPPER_ERROR_UNSUPPORTED != sizeRequired, |
| 140 | "Unexpected error %d from valid getStandardMetadata call", -sizeRequired); |
| 141 | return std::nullopt; |
| 142 | } |
| 143 | if ((size_t)sizeRequired > buffer.size()) { |
| 144 | buffer.resize(sizeRequired); |
| 145 | sizeRequired = mapper->v5.getStandardMetadata(bufferHandle, static_cast<int64_t>(T), |
| 146 | buffer.data(), buffer.size()); |
| 147 | } |
| 148 | if (sizeRequired < 0 || (size_t)sizeRequired > buffer.size()) { |
| 149 | ALOGW("getStandardMetadata failed, received %d with buffer size %zd", sizeRequired, |
| 150 | buffer.size()); |
| 151 | // Generate a fail type |
| 152 | return std::nullopt; |
| 153 | } |
| 154 | return Value::decode(buffer.data(), sizeRequired); |
| 155 | } |
| 156 | |
| 157 | template <StandardMetadataType T> |
| 158 | static AIMapper_Error setStandardMetadata(AIMapper *mapper, buffer_handle_t bufferHandle, |
| 159 | const typename StandardMetadata<T>::value_type &value) { |
| 160 | using Value = typename StandardMetadata<T>::value; |
| 161 | int32_t sizeRequired = Value::encode(value, nullptr, 0); |
| 162 | if (sizeRequired < 0) { |
| 163 | ALOGW("Failed to calculate required size"); |
| 164 | return static_cast<AIMapper_Error>(-sizeRequired); |
| 165 | } |
| 166 | FatVector<uint8_t, 128> buffer; |
| 167 | buffer.resize(sizeRequired); |
| 168 | sizeRequired = Value::encode(value, buffer.data(), buffer.size()); |
| 169 | if (sizeRequired < 0 || (size_t)sizeRequired > buffer.size()) { |
| 170 | ALOGW("Failed to encode with calculated size %d; buffer size %zd", sizeRequired, |
| 171 | buffer.size()); |
| 172 | return static_cast<AIMapper_Error>(-sizeRequired); |
| 173 | } |
| 174 | return mapper->v5.setStandardMetadata(bufferHandle, static_cast<int64_t>(T), buffer.data(), |
| 175 | sizeRequired); |
| 176 | } |
| 177 | |
| 178 | Gralloc5Allocator::Gralloc5Allocator(const Gralloc5Mapper &mapper) : mMapper(mapper) { |
| 179 | mAllocator = getInstance().allocator; |
| 180 | } |
| 181 | |
| 182 | bool Gralloc5Allocator::isLoaded() const { |
| 183 | return mAllocator != nullptr; |
| 184 | } |
| 185 | |
| 186 | static uint64_t getValidUsageBits() { |
| 187 | static const uint64_t validUsageBits = []() -> uint64_t { |
| 188 | uint64_t bits = 0; |
| 189 | for (const auto bit : ndk::enum_range<BufferUsage>{}) { |
| 190 | bits |= static_cast<int64_t>(bit); |
| 191 | } |
| 192 | return bits; |
| 193 | }(); |
| 194 | return validUsageBits | kRemovedUsageBits; |
| 195 | } |
| 196 | |
| 197 | static std::optional<BufferDescriptorInfo> makeDescriptor(std::string requestorName, uint32_t width, |
| 198 | uint32_t height, PixelFormat format, |
| 199 | uint32_t layerCount, uint64_t usage) { |
| 200 | uint64_t validUsageBits = getValidUsageBits(); |
| 201 | if (usage & ~validUsageBits) { |
| 202 | ALOGE("buffer descriptor contains invalid usage bits 0x%" PRIx64, usage & ~validUsageBits); |
| 203 | return std::nullopt; |
| 204 | } |
| 205 | |
| 206 | BufferDescriptorInfo descriptorInfo{ |
| 207 | .width = static_cast<int32_t>(width), |
| 208 | .height = static_cast<int32_t>(height), |
| 209 | .layerCount = static_cast<int32_t>(layerCount), |
| 210 | .format = static_cast<::aidl::android::hardware::graphics::common::PixelFormat>(format), |
| 211 | .usage = static_cast<BufferUsage>(usage), |
| 212 | }; |
| 213 | auto nameLength = std::min(requestorName.length(), descriptorInfo.name.size() - 1); |
| 214 | memcpy(descriptorInfo.name.data(), requestorName.data(), nameLength); |
| 215 | requestorName.data()[nameLength] = 0; |
| 216 | return descriptorInfo; |
| 217 | } |
| 218 | |
| 219 | std::string Gralloc5Allocator::dumpDebugInfo(bool less) const { |
| 220 | return mMapper.dumpBuffers(less); |
| 221 | } |
| 222 | |
| 223 | status_t Gralloc5Allocator::allocate(std::string requestorName, uint32_t width, uint32_t height, |
| 224 | android::PixelFormat format, uint32_t layerCount, |
| 225 | uint64_t usage, uint32_t bufferCount, uint32_t *outStride, |
| 226 | buffer_handle_t *outBufferHandles, bool importBuffers) const { |
| 227 | auto descriptorInfo = makeDescriptor(requestorName, width, height, format, layerCount, usage); |
| 228 | if (!descriptorInfo) { |
| 229 | return BAD_VALUE; |
| 230 | } |
| 231 | |
| 232 | AllocationResult result; |
| 233 | auto status = mAllocator->allocate2(*descriptorInfo, bufferCount, &result); |
| 234 | if (!status.isOk()) { |
| 235 | auto error = status.getExceptionCode(); |
| 236 | if (error == EX_SERVICE_SPECIFIC) { |
| 237 | error = status.getServiceSpecificError(); |
| 238 | } |
| 239 | if (error == OK) { |
| 240 | error = UNKNOWN_ERROR; |
| 241 | } |
| 242 | return error; |
| 243 | } |
| 244 | |
| 245 | if (importBuffers) { |
| 246 | for (uint32_t i = 0; i < bufferCount; i++) { |
| 247 | auto handle = makeFromAidl(result.buffers[i]); |
| 248 | auto error = mMapper.importBuffer(handle, &outBufferHandles[i]); |
| 249 | native_handle_delete(handle); |
| 250 | if (error != NO_ERROR) { |
| 251 | for (uint32_t j = 0; j < i; j++) { |
| 252 | mMapper.freeBuffer(outBufferHandles[j]); |
| 253 | outBufferHandles[j] = nullptr; |
| 254 | } |
| 255 | return error; |
| 256 | } |
| 257 | } |
| 258 | } else { |
| 259 | for (uint32_t i = 0; i < bufferCount; i++) { |
| 260 | outBufferHandles[i] = dupFromAidl(result.buffers[i]); |
| 261 | if (!outBufferHandles[i]) { |
| 262 | for (uint32_t j = 0; j < i; j++) { |
| 263 | auto buffer = const_cast<native_handle_t *>(outBufferHandles[j]); |
| 264 | native_handle_close(buffer); |
| 265 | native_handle_delete(buffer); |
| 266 | outBufferHandles[j] = nullptr; |
| 267 | } |
| 268 | return NO_MEMORY; |
| 269 | } |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | *outStride = result.stride; |
| 274 | |
| 275 | // Release all the resources held by AllocationResult (specifically any remaining FDs) |
| 276 | result = {}; |
| 277 | // make sure the kernel driver sees BC_FREE_BUFFER and closes the fds now |
| 278 | // TODO: Re-enable this at some point if it's necessary. We can't do it now because libui |
| 279 | // is marked apex_available (b/214400477) and libbinder isn't (which of course is correct) |
| 280 | // IPCThreadState::self()->flushCommands(); |
| 281 | |
| 282 | return OK; |
| 283 | } |
| 284 | |
| 285 | void Gralloc5Mapper::preload() { |
| 286 | // TODO(b/261858155): Implement. We can't bounce off of IAllocator for this because zygote can't |
| 287 | // use binder. So when an alternate strategy of retrieving the library prefix is available, |
| 288 | // use that here. |
| 289 | } |
| 290 | |
| 291 | Gralloc5Mapper::Gralloc5Mapper() { |
| 292 | mMapper = getInstance().mapper; |
| 293 | } |
| 294 | |
| 295 | bool Gralloc5Mapper::isLoaded() const { |
| 296 | return mMapper != nullptr && mMapper->version >= AIMAPPER_VERSION_5; |
| 297 | } |
| 298 | |
John Reck | edcd885 | 2023-07-25 16:03:44 -0400 | [diff] [blame] | 299 | static bool isStandardMetadata(AIMapper_MetadataType metadataType) { |
| 300 | return strcmp(kStandardMetadataName, metadataType.name) == 0; |
| 301 | } |
| 302 | |
| 303 | struct DumpBufferResult { |
| 304 | uint64_t bufferId; |
| 305 | std::string name; |
| 306 | uint64_t width; |
| 307 | uint64_t height; |
| 308 | uint64_t layerCount; |
| 309 | APixelFormat pixelFormatRequested; |
| 310 | uint32_t pixelFormatFourCC; |
| 311 | uint64_t pixelFormatModifier; |
| 312 | BufferUsage usage; |
| 313 | ADataspace dataspace; |
| 314 | uint64_t allocationSize; |
| 315 | uint64_t protectedContent; |
| 316 | ExtendableType compression; |
| 317 | ExtendableType interlaced; |
| 318 | ExtendableType chromaSiting; |
| 319 | std::vector<ui::PlaneLayout> planeLayouts; |
| 320 | }; |
| 321 | |
| 322 | #define DECODE_TO(name, output) \ |
| 323 | case StandardMetadataType::name: \ |
| 324 | output = StandardMetadata<StandardMetadataType::name>::value ::decode(value, valueSize) \ |
| 325 | .value(); \ |
| 326 | break |
| 327 | |
| 328 | static void dumpBufferCommon(DumpBufferResult* outResult, AIMapper_MetadataType metadataType, |
| 329 | const void* value, size_t valueSize) { |
| 330 | if (!isStandardMetadata(metadataType)) { |
| 331 | return; |
| 332 | } |
| 333 | StandardMetadataType type = (StandardMetadataType)metadataType.value; |
| 334 | switch (type) { |
| 335 | DECODE_TO(BUFFER_ID, outResult->bufferId); |
| 336 | DECODE_TO(NAME, outResult->name); |
| 337 | DECODE_TO(WIDTH, outResult->width); |
| 338 | DECODE_TO(HEIGHT, outResult->height); |
| 339 | DECODE_TO(LAYER_COUNT, outResult->layerCount); |
| 340 | DECODE_TO(PIXEL_FORMAT_REQUESTED, outResult->pixelFormatRequested); |
| 341 | DECODE_TO(PIXEL_FORMAT_FOURCC, outResult->pixelFormatFourCC); |
| 342 | DECODE_TO(PIXEL_FORMAT_MODIFIER, outResult->pixelFormatModifier); |
| 343 | DECODE_TO(USAGE, outResult->usage); |
| 344 | DECODE_TO(DATASPACE, outResult->dataspace); |
| 345 | DECODE_TO(ALLOCATION_SIZE, outResult->allocationSize); |
| 346 | DECODE_TO(PROTECTED_CONTENT, outResult->protectedContent); |
| 347 | DECODE_TO(COMPRESSION, outResult->compression); |
| 348 | DECODE_TO(INTERLACED, outResult->interlaced); |
| 349 | DECODE_TO(CHROMA_SITING, outResult->chromaSiting); |
| 350 | DECODE_TO(PLANE_LAYOUTS, outResult->planeLayouts); |
| 351 | default: |
| 352 | break; |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | #undef DECODE_TO |
| 357 | |
| 358 | template <typename EnumT, typename = std::enable_if_t<std::is_enum<EnumT>{}>> |
| 359 | constexpr std::underlying_type_t<EnumT> to_underlying(EnumT e) noexcept { |
| 360 | return static_cast<std::underlying_type_t<EnumT>>(e); |
| 361 | } |
| 362 | |
| 363 | static void writeDumpToStream(const DumpBufferResult& bufferDump, std::ostream& outDump, |
| 364 | bool less) { |
| 365 | double allocationSizeKiB = static_cast<double>(bufferDump.allocationSize) / 1024; |
| 366 | |
| 367 | outDump << "+ name:" << bufferDump.name << ", id:" << bufferDump.bufferId |
| 368 | << ", size:" << std::fixed << allocationSizeKiB << "KiB, w/h:" << bufferDump.width |
| 369 | << "x" << bufferDump.height << ", usage: 0x" << std::hex |
| 370 | << to_underlying(bufferDump.usage) << std::dec |
| 371 | << ", req fmt:" << to_underlying(bufferDump.pixelFormatRequested) |
| 372 | << ", fourcc/mod:" << bufferDump.pixelFormatFourCC << "/" |
| 373 | << bufferDump.pixelFormatModifier << ", dataspace: 0x" << std::hex |
| 374 | << to_underlying(bufferDump.dataspace) << std::dec << ", compressed: "; |
| 375 | |
| 376 | if (less) { |
| 377 | bool isCompressed = !gralloc4::isStandardCompression(bufferDump.compression) || |
| 378 | (gralloc4::getStandardCompressionValue(bufferDump.compression) != |
| 379 | ui::Compression::NONE); |
| 380 | outDump << std::boolalpha << isCompressed << "\n"; |
| 381 | } else { |
| 382 | outDump << gralloc4::getCompressionName(bufferDump.compression) << "\n"; |
| 383 | } |
| 384 | |
| 385 | if (!less) { |
| 386 | bool firstPlane = true; |
| 387 | for (const auto& planeLayout : bufferDump.planeLayouts) { |
| 388 | if (firstPlane) { |
| 389 | firstPlane = false; |
| 390 | outDump << "\tplanes: "; |
| 391 | } else { |
| 392 | outDump << "\t "; |
| 393 | } |
| 394 | |
| 395 | for (size_t i = 0; i < planeLayout.components.size(); i++) { |
| 396 | const auto& planeLayoutComponent = planeLayout.components[i]; |
| 397 | outDump << gralloc4::getPlaneLayoutComponentTypeName(planeLayoutComponent.type); |
| 398 | if (i < planeLayout.components.size() - 1) { |
| 399 | outDump << "/"; |
| 400 | } else { |
| 401 | outDump << ":\t"; |
| 402 | } |
| 403 | } |
| 404 | outDump << " w/h:" << planeLayout.widthInSamples << "x" << planeLayout.heightInSamples |
| 405 | << ", stride:" << planeLayout.strideInBytes |
| 406 | << " bytes, size:" << planeLayout.totalSizeInBytes; |
| 407 | outDump << ", inc:" << planeLayout.sampleIncrementInBits |
| 408 | << " bits, subsampling w/h:" << planeLayout.horizontalSubsampling << "x" |
| 409 | << planeLayout.verticalSubsampling; |
| 410 | outDump << "\n"; |
| 411 | } |
| 412 | |
| 413 | outDump << "\tlayer cnt: " << bufferDump.layerCount |
| 414 | << ", protected content: " << bufferDump.protectedContent |
| 415 | << ", interlaced: " << gralloc4::getInterlacedName(bufferDump.interlaced) |
| 416 | << ", chroma siting:" << gralloc4::getChromaSitingName(bufferDump.chromaSiting) |
| 417 | << "\n"; |
| 418 | } |
| 419 | } |
| 420 | |
John Reck | 0ff95c9 | 2022-12-08 11:45:29 -0500 | [diff] [blame] | 421 | std::string Gralloc5Mapper::dumpBuffer(buffer_handle_t bufferHandle, bool less) const { |
John Reck | edcd885 | 2023-07-25 16:03:44 -0400 | [diff] [blame] | 422 | DumpBufferResult bufferInfo; |
| 423 | AIMapper_DumpBufferCallback dumpBuffer = [](void* contextPtr, |
| 424 | AIMapper_MetadataType metadataType, |
| 425 | const void* _Nonnull value, size_t valueSize) { |
| 426 | DumpBufferResult* context = reinterpret_cast<DumpBufferResult*>(contextPtr); |
| 427 | dumpBufferCommon(context, metadataType, value, valueSize); |
| 428 | }; |
| 429 | AIMapper_Error error = mMapper->v5.dumpBuffer(bufferHandle, dumpBuffer, &bufferInfo); |
| 430 | if (error != AIMAPPER_ERROR_NONE) { |
| 431 | ALOGE("Error dumping buffer: %d", error); |
| 432 | return std::string{}; |
| 433 | } |
| 434 | std::ostringstream stream; |
| 435 | stream.precision(2); |
| 436 | writeDumpToStream(bufferInfo, stream, less); |
| 437 | return stream.str(); |
John Reck | 0ff95c9 | 2022-12-08 11:45:29 -0500 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | std::string Gralloc5Mapper::dumpBuffers(bool less) const { |
John Reck | edcd885 | 2023-07-25 16:03:44 -0400 | [diff] [blame] | 441 | class DumpAllBuffersContext { |
| 442 | private: |
| 443 | bool mHasPending = false; |
| 444 | DumpBufferResult mPending; |
| 445 | std::vector<DumpBufferResult> mResults; |
| 446 | |
| 447 | public: |
| 448 | DumpAllBuffersContext() { mResults.reserve(10); } |
| 449 | |
| 450 | void commit() { |
| 451 | if (mHasPending) { |
| 452 | mResults.push_back(mPending); |
| 453 | mHasPending = false; |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | DumpBufferResult* write() { |
| 458 | mHasPending = true; |
| 459 | return &mPending; |
| 460 | } |
| 461 | |
| 462 | const std::vector<DumpBufferResult>& results() { |
| 463 | commit(); |
| 464 | return mResults; |
| 465 | } |
| 466 | } context; |
| 467 | |
| 468 | AIMapper_BeginDumpBufferCallback beginCallback = [](void* contextPtr) { |
| 469 | DumpAllBuffersContext* context = reinterpret_cast<DumpAllBuffersContext*>(contextPtr); |
| 470 | context->commit(); |
| 471 | }; |
| 472 | |
| 473 | AIMapper_DumpBufferCallback dumpBuffer = [](void* contextPtr, |
| 474 | AIMapper_MetadataType metadataType, |
| 475 | const void* _Nonnull value, size_t valueSize) { |
| 476 | DumpAllBuffersContext* context = reinterpret_cast<DumpAllBuffersContext*>(contextPtr); |
| 477 | dumpBufferCommon(context->write(), metadataType, value, valueSize); |
| 478 | }; |
| 479 | |
| 480 | AIMapper_Error error = mMapper->v5.dumpAllBuffers(beginCallback, dumpBuffer, &context); |
| 481 | if (error != AIMAPPER_ERROR_NONE) { |
| 482 | ALOGE("Error dumping buffers: %d", error); |
| 483 | return std::string{}; |
| 484 | } |
| 485 | uint64_t totalAllocationSize = 0; |
| 486 | std::ostringstream stream; |
| 487 | stream.precision(2); |
| 488 | stream << "Imported gralloc buffers:\n"; |
| 489 | |
| 490 | for (const auto& bufferDump : context.results()) { |
| 491 | writeDumpToStream(bufferDump, stream, less); |
| 492 | totalAllocationSize += bufferDump.allocationSize; |
| 493 | } |
| 494 | |
| 495 | double totalAllocationSizeKiB = static_cast<double>(totalAllocationSize) / 1024; |
| 496 | stream << "Total imported by gralloc: " << totalAllocationSizeKiB << "KiB\n"; |
| 497 | return stream.str(); |
John Reck | 0ff95c9 | 2022-12-08 11:45:29 -0500 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | status_t Gralloc5Mapper::importBuffer(const native_handle_t *rawHandle, |
| 501 | buffer_handle_t *outBufferHandle) const { |
| 502 | return mMapper->v5.importBuffer(rawHandle, outBufferHandle); |
| 503 | } |
| 504 | |
| 505 | void Gralloc5Mapper::freeBuffer(buffer_handle_t bufferHandle) const { |
| 506 | mMapper->v5.freeBuffer(bufferHandle); |
| 507 | } |
| 508 | |
| 509 | status_t Gralloc5Mapper::validateBufferSize(buffer_handle_t bufferHandle, uint32_t width, |
| 510 | uint32_t height, PixelFormat format, |
| 511 | uint32_t layerCount, uint64_t usage, |
| 512 | uint32_t stride) const { |
| 513 | { |
| 514 | auto value = getStandardMetadata<StandardMetadataType::WIDTH>(mMapper, bufferHandle); |
| 515 | if (width != value) { |
| 516 | ALOGW("Width didn't match, expected %d got %" PRId64, width, value.value_or(-1)); |
| 517 | return BAD_VALUE; |
| 518 | } |
| 519 | } |
| 520 | { |
| 521 | auto value = getStandardMetadata<StandardMetadataType::HEIGHT>(mMapper, bufferHandle); |
| 522 | if (height != value) { |
| 523 | ALOGW("Height didn't match, expected %d got %" PRId64, height, value.value_or(-1)); |
| 524 | return BAD_VALUE; |
| 525 | } |
| 526 | } |
| 527 | { |
John Reck | 54a9814 | 2023-11-21 11:13:40 -0500 | [diff] [blame] | 528 | auto expected = static_cast<APixelFormat>(format); |
| 529 | if (expected != APixelFormat::IMPLEMENTATION_DEFINED) { |
| 530 | auto value = |
| 531 | getStandardMetadata<StandardMetadataType::PIXEL_FORMAT_REQUESTED>(mMapper, |
| 532 | bufferHandle); |
| 533 | if (expected != value) { |
| 534 | ALOGW("Format didn't match, expected %d got %s", format, |
| 535 | value.has_value() ? toString(*value).c_str() : "<null>"); |
| 536 | return BAD_VALUE; |
| 537 | } |
John Reck | 0ff95c9 | 2022-12-08 11:45:29 -0500 | [diff] [blame] | 538 | } |
| 539 | } |
| 540 | { |
| 541 | auto value = getStandardMetadata<StandardMetadataType::LAYER_COUNT>(mMapper, bufferHandle); |
| 542 | if (layerCount != value) { |
| 543 | ALOGW("Layer count didn't match, expected %d got %" PRId64, layerCount, |
| 544 | value.value_or(-1)); |
| 545 | return BAD_VALUE; |
| 546 | } |
| 547 | } |
John Reck | 47390ec | 2023-05-24 13:46:37 -0400 | [diff] [blame] | 548 | // TODO: This can false-positive fail if the allocator adjusted the USAGE bits internally |
| 549 | // Investigate further & re-enable or remove, but for now ignoring usage should be OK |
| 550 | (void)usage; |
| 551 | // { |
| 552 | // auto value = getStandardMetadata<StandardMetadataType::USAGE>(mMapper, bufferHandle); |
| 553 | // if (static_cast<BufferUsage>(usage) != value) { |
| 554 | // ALOGW("Usage didn't match, expected %" PRIu64 " got %" PRId64, usage, |
| 555 | // static_cast<int64_t>(value.value_or(BufferUsage::CPU_READ_NEVER))); |
| 556 | // return BAD_VALUE; |
| 557 | // } |
| 558 | // } |
John Reck | 0ff95c9 | 2022-12-08 11:45:29 -0500 | [diff] [blame] | 559 | { |
John Reck | e225b28 | 2023-03-24 16:15:17 -0400 | [diff] [blame] | 560 | auto value = getStandardMetadata<StandardMetadataType::STRIDE>(mMapper, bufferHandle); |
| 561 | if (stride != value) { |
| 562 | ALOGW("Stride didn't match, expected %" PRIu32 " got %" PRId32, stride, |
| 563 | value.value_or(-1)); |
| 564 | return BAD_VALUE; |
| 565 | } |
John Reck | 0ff95c9 | 2022-12-08 11:45:29 -0500 | [diff] [blame] | 566 | } |
| 567 | return OK; |
| 568 | } |
| 569 | |
| 570 | void Gralloc5Mapper::getTransportSize(buffer_handle_t bufferHandle, uint32_t *outNumFds, |
| 571 | uint32_t *outNumInts) const { |
| 572 | mMapper->v5.getTransportSize(bufferHandle, outNumFds, outNumInts); |
| 573 | } |
| 574 | |
| 575 | status_t Gralloc5Mapper::lock(buffer_handle_t bufferHandle, uint64_t usage, const Rect &bounds, |
| 576 | int acquireFence, void **outData, int32_t *outBytesPerPixel, |
| 577 | int32_t *outBytesPerStride) const { |
| 578 | std::vector<ui::PlaneLayout> planeLayouts; |
| 579 | status_t err = getPlaneLayouts(bufferHandle, &planeLayouts); |
| 580 | |
| 581 | if (err == NO_ERROR && !planeLayouts.empty()) { |
| 582 | if (outBytesPerPixel) { |
| 583 | int32_t bitsPerPixel = planeLayouts.front().sampleIncrementInBits; |
| 584 | for (const auto &planeLayout : planeLayouts) { |
| 585 | if (bitsPerPixel != planeLayout.sampleIncrementInBits) { |
| 586 | bitsPerPixel = -1; |
| 587 | } |
| 588 | } |
| 589 | if (bitsPerPixel >= 0 && bitsPerPixel % 8 == 0) { |
| 590 | *outBytesPerPixel = bitsPerPixel / 8; |
| 591 | } else { |
| 592 | *outBytesPerPixel = -1; |
| 593 | } |
| 594 | } |
| 595 | if (outBytesPerStride) { |
| 596 | int32_t bytesPerStride = planeLayouts.front().strideInBytes; |
| 597 | for (const auto &planeLayout : planeLayouts) { |
| 598 | if (bytesPerStride != planeLayout.strideInBytes) { |
| 599 | bytesPerStride = -1; |
| 600 | } |
| 601 | } |
| 602 | if (bytesPerStride >= 0) { |
| 603 | *outBytesPerStride = bytesPerStride; |
| 604 | } else { |
| 605 | *outBytesPerStride = -1; |
| 606 | } |
| 607 | } |
| 608 | } |
| 609 | |
| 610 | auto status = mMapper->v5.lock(bufferHandle, usage, bounds, acquireFence, outData); |
| 611 | |
| 612 | ALOGW_IF(status != AIMAPPER_ERROR_NONE, "lock(%p, ...) failed: %d", bufferHandle, status); |
| 613 | return static_cast<status_t>(status); |
| 614 | } |
| 615 | |
| 616 | status_t Gralloc5Mapper::lock(buffer_handle_t bufferHandle, uint64_t usage, const Rect &bounds, |
| 617 | int acquireFence, android_ycbcr *outYcbcr) const { |
| 618 | if (!outYcbcr) { |
| 619 | return BAD_VALUE; |
| 620 | } |
| 621 | |
| 622 | // TODO(b/262279301): Change the return type of ::unlock to unique_fd instead of int so that |
| 623 | // ignoring the return value "just works" instead |
| 624 | auto unlock = [this](buffer_handle_t bufferHandle) { |
| 625 | int fence = this->unlock(bufferHandle); |
| 626 | if (fence != -1) { |
| 627 | ::close(fence); |
| 628 | } |
| 629 | }; |
| 630 | |
| 631 | std::vector<ui::PlaneLayout> planeLayouts; |
| 632 | status_t error = getPlaneLayouts(bufferHandle, &planeLayouts); |
| 633 | if (error != NO_ERROR) { |
| 634 | return error; |
| 635 | } |
| 636 | |
| 637 | void *data = nullptr; |
| 638 | error = lock(bufferHandle, usage, bounds, acquireFence, &data, nullptr, nullptr); |
| 639 | if (error != NO_ERROR) { |
| 640 | return error; |
| 641 | } |
| 642 | |
| 643 | android_ycbcr ycbcr; |
| 644 | |
| 645 | ycbcr.y = nullptr; |
| 646 | ycbcr.cb = nullptr; |
| 647 | ycbcr.cr = nullptr; |
| 648 | ycbcr.ystride = 0; |
| 649 | ycbcr.cstride = 0; |
| 650 | ycbcr.chroma_step = 0; |
| 651 | |
| 652 | for (const auto &planeLayout : planeLayouts) { |
| 653 | for (const auto &planeLayoutComponent : planeLayout.components) { |
| 654 | if (!gralloc4::isStandardPlaneLayoutComponentType(planeLayoutComponent.type)) { |
| 655 | continue; |
| 656 | } |
| 657 | |
| 658 | uint8_t *tmpData = static_cast<uint8_t *>(data) + planeLayout.offsetInBytes; |
| 659 | |
| 660 | // Note that `offsetInBits` may not be a multiple of 8 for packed formats (e.g. P010) |
| 661 | // but we still want to point to the start of the first byte. |
| 662 | tmpData += (planeLayoutComponent.offsetInBits / 8); |
| 663 | |
| 664 | uint64_t sampleIncrementInBytes; |
| 665 | |
| 666 | auto type = static_cast<PlaneLayoutComponentType>(planeLayoutComponent.type.value); |
| 667 | switch (type) { |
| 668 | case PlaneLayoutComponentType::Y: |
| 669 | if ((ycbcr.y != nullptr) || (planeLayout.sampleIncrementInBits % 8 != 0)) { |
| 670 | unlock(bufferHandle); |
| 671 | return BAD_VALUE; |
| 672 | } |
| 673 | ycbcr.y = tmpData; |
| 674 | ycbcr.ystride = planeLayout.strideInBytes; |
| 675 | break; |
| 676 | |
| 677 | case PlaneLayoutComponentType::CB: |
| 678 | case PlaneLayoutComponentType::CR: |
| 679 | if (planeLayout.sampleIncrementInBits % 8 != 0) { |
| 680 | unlock(bufferHandle); |
| 681 | return BAD_VALUE; |
| 682 | } |
| 683 | |
| 684 | sampleIncrementInBytes = planeLayout.sampleIncrementInBits / 8; |
| 685 | if ((sampleIncrementInBytes != 1) && (sampleIncrementInBytes != 2) && |
| 686 | (sampleIncrementInBytes != 4)) { |
| 687 | unlock(bufferHandle); |
| 688 | return BAD_VALUE; |
| 689 | } |
| 690 | |
| 691 | if (ycbcr.cstride == 0 && ycbcr.chroma_step == 0) { |
| 692 | ycbcr.cstride = planeLayout.strideInBytes; |
| 693 | ycbcr.chroma_step = sampleIncrementInBytes; |
| 694 | } else { |
| 695 | if ((static_cast<int64_t>(ycbcr.cstride) != planeLayout.strideInBytes) || |
| 696 | (ycbcr.chroma_step != sampleIncrementInBytes)) { |
| 697 | unlock(bufferHandle); |
| 698 | return BAD_VALUE; |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | if (type == PlaneLayoutComponentType::CB) { |
| 703 | if (ycbcr.cb != nullptr) { |
| 704 | unlock(bufferHandle); |
| 705 | return BAD_VALUE; |
| 706 | } |
| 707 | ycbcr.cb = tmpData; |
| 708 | } else { |
| 709 | if (ycbcr.cr != nullptr) { |
| 710 | unlock(bufferHandle); |
| 711 | return BAD_VALUE; |
| 712 | } |
| 713 | ycbcr.cr = tmpData; |
| 714 | } |
| 715 | break; |
| 716 | default: |
| 717 | break; |
| 718 | }; |
| 719 | } |
| 720 | } |
| 721 | |
| 722 | *outYcbcr = ycbcr; |
| 723 | return OK; |
| 724 | } |
| 725 | |
| 726 | int Gralloc5Mapper::unlock(buffer_handle_t bufferHandle) const { |
| 727 | int fence = -1; |
| 728 | AIMapper_Error error = mMapper->v5.unlock(bufferHandle, &fence); |
| 729 | if (error != AIMAPPER_ERROR_NONE) { |
| 730 | ALOGW("unlock failed with error %d", error); |
| 731 | } |
| 732 | return fence; |
| 733 | } |
| 734 | |
| 735 | status_t Gralloc5Mapper::isSupported(uint32_t width, uint32_t height, PixelFormat format, |
| 736 | uint32_t layerCount, uint64_t usage, |
| 737 | bool *outSupported) const { |
| 738 | auto descriptorInfo = makeDescriptor("", width, height, format, layerCount, usage); |
| 739 | if (!descriptorInfo) { |
| 740 | *outSupported = false; |
| 741 | return OK; |
| 742 | } |
| 743 | auto status = getInstance().allocator->isSupported(*descriptorInfo, outSupported); |
| 744 | if (!status.isOk()) { |
| 745 | ALOGW("IAllocator::isSupported error %d (%s)", status.getStatus(), status.getMessage()); |
| 746 | *outSupported = false; |
| 747 | } |
| 748 | return OK; |
| 749 | } |
| 750 | |
| 751 | status_t Gralloc5Mapper::getBufferId(buffer_handle_t bufferHandle, uint64_t *outBufferId) const { |
| 752 | auto value = getStandardMetadata<StandardMetadataType::BUFFER_ID>(mMapper, bufferHandle); |
| 753 | if (value.has_value()) { |
| 754 | *outBufferId = *value; |
| 755 | return OK; |
| 756 | } |
| 757 | return UNKNOWN_TRANSACTION; |
| 758 | } |
| 759 | |
| 760 | status_t Gralloc5Mapper::getName(buffer_handle_t bufferHandle, std::string *outName) const { |
| 761 | auto value = getStandardMetadata<StandardMetadataType::NAME>(mMapper, bufferHandle); |
| 762 | if (value.has_value()) { |
| 763 | *outName = *value; |
| 764 | return OK; |
| 765 | } |
| 766 | return UNKNOWN_TRANSACTION; |
| 767 | } |
| 768 | |
| 769 | status_t Gralloc5Mapper::getWidth(buffer_handle_t bufferHandle, uint64_t *outWidth) const { |
| 770 | auto value = getStandardMetadata<StandardMetadataType::WIDTH>(mMapper, bufferHandle); |
| 771 | if (value.has_value()) { |
| 772 | *outWidth = *value; |
| 773 | return OK; |
| 774 | } |
| 775 | return UNKNOWN_TRANSACTION; |
| 776 | } |
| 777 | |
| 778 | status_t Gralloc5Mapper::getHeight(buffer_handle_t bufferHandle, uint64_t *outHeight) const { |
| 779 | auto value = getStandardMetadata<StandardMetadataType::HEIGHT>(mMapper, bufferHandle); |
| 780 | if (value.has_value()) { |
| 781 | *outHeight = *value; |
| 782 | return OK; |
| 783 | } |
| 784 | return UNKNOWN_TRANSACTION; |
| 785 | } |
| 786 | |
| 787 | status_t Gralloc5Mapper::getLayerCount(buffer_handle_t bufferHandle, |
| 788 | uint64_t *outLayerCount) const { |
| 789 | auto value = getStandardMetadata<StandardMetadataType::LAYER_COUNT>(mMapper, bufferHandle); |
| 790 | if (value.has_value()) { |
| 791 | *outLayerCount = *value; |
| 792 | return OK; |
| 793 | } |
| 794 | return UNKNOWN_TRANSACTION; |
| 795 | } |
| 796 | |
| 797 | status_t Gralloc5Mapper::getPixelFormatRequested(buffer_handle_t bufferHandle, |
| 798 | ui::PixelFormat *outPixelFormatRequested) const { |
| 799 | auto value = getStandardMetadata<StandardMetadataType::PIXEL_FORMAT_REQUESTED>(mMapper, |
| 800 | bufferHandle); |
| 801 | if (value.has_value()) { |
| 802 | *outPixelFormatRequested = static_cast<ui::PixelFormat>(*value); |
| 803 | return OK; |
| 804 | } |
| 805 | return UNKNOWN_TRANSACTION; |
| 806 | } |
| 807 | |
| 808 | status_t Gralloc5Mapper::getPixelFormatFourCC(buffer_handle_t bufferHandle, |
| 809 | uint32_t *outPixelFormatFourCC) const { |
| 810 | auto value = |
| 811 | getStandardMetadata<StandardMetadataType::PIXEL_FORMAT_FOURCC>(mMapper, bufferHandle); |
| 812 | if (value.has_value()) { |
| 813 | *outPixelFormatFourCC = *value; |
| 814 | return OK; |
| 815 | } |
| 816 | return UNKNOWN_TRANSACTION; |
| 817 | } |
| 818 | |
| 819 | status_t Gralloc5Mapper::getPixelFormatModifier(buffer_handle_t bufferHandle, |
| 820 | uint64_t *outPixelFormatModifier) const { |
| 821 | auto value = |
| 822 | getStandardMetadata<StandardMetadataType::PIXEL_FORMAT_MODIFIER>(mMapper, bufferHandle); |
| 823 | if (value.has_value()) { |
| 824 | *outPixelFormatModifier = *value; |
| 825 | return OK; |
| 826 | } |
| 827 | return UNKNOWN_TRANSACTION; |
| 828 | } |
| 829 | |
| 830 | status_t Gralloc5Mapper::getUsage(buffer_handle_t bufferHandle, uint64_t *outUsage) const { |
| 831 | auto value = getStandardMetadata<StandardMetadataType::USAGE>(mMapper, bufferHandle); |
| 832 | if (value.has_value()) { |
| 833 | *outUsage = static_cast<uint64_t>(*value); |
| 834 | return OK; |
| 835 | } |
| 836 | return UNKNOWN_TRANSACTION; |
| 837 | } |
| 838 | |
| 839 | status_t Gralloc5Mapper::getAllocationSize(buffer_handle_t bufferHandle, |
| 840 | uint64_t *outAllocationSize) const { |
| 841 | auto value = getStandardMetadata<StandardMetadataType::ALLOCATION_SIZE>(mMapper, bufferHandle); |
| 842 | if (value.has_value()) { |
| 843 | *outAllocationSize = *value; |
| 844 | return OK; |
| 845 | } |
| 846 | return UNKNOWN_TRANSACTION; |
| 847 | } |
| 848 | |
| 849 | status_t Gralloc5Mapper::getProtectedContent(buffer_handle_t bufferHandle, |
| 850 | uint64_t *outProtectedContent) const { |
| 851 | auto value = |
| 852 | getStandardMetadata<StandardMetadataType::PROTECTED_CONTENT>(mMapper, bufferHandle); |
| 853 | if (value.has_value()) { |
| 854 | *outProtectedContent = *value; |
| 855 | return OK; |
| 856 | } |
| 857 | return UNKNOWN_TRANSACTION; |
| 858 | } |
| 859 | |
| 860 | status_t Gralloc5Mapper::getCompression( |
| 861 | buffer_handle_t bufferHandle, |
| 862 | aidl::android::hardware::graphics::common::ExtendableType *outCompression) const { |
| 863 | auto value = getStandardMetadata<StandardMetadataType::COMPRESSION>(mMapper, bufferHandle); |
| 864 | if (value.has_value()) { |
| 865 | *outCompression = *value; |
| 866 | return OK; |
| 867 | } |
| 868 | return UNKNOWN_TRANSACTION; |
| 869 | } |
| 870 | |
| 871 | status_t Gralloc5Mapper::getCompression(buffer_handle_t bufferHandle, |
| 872 | ui::Compression *outCompression) const { |
| 873 | auto value = getStandardMetadata<StandardMetadataType::COMPRESSION>(mMapper, bufferHandle); |
| 874 | if (!value.has_value()) { |
| 875 | return UNKNOWN_TRANSACTION; |
| 876 | } |
| 877 | if (!gralloc4::isStandardCompression(*value)) { |
| 878 | return BAD_TYPE; |
| 879 | } |
| 880 | *outCompression = gralloc4::getStandardCompressionValue(*value); |
| 881 | return OK; |
| 882 | } |
| 883 | |
| 884 | status_t Gralloc5Mapper::getInterlaced( |
| 885 | buffer_handle_t bufferHandle, |
| 886 | aidl::android::hardware::graphics::common::ExtendableType *outInterlaced) const { |
| 887 | auto value = getStandardMetadata<StandardMetadataType::INTERLACED>(mMapper, bufferHandle); |
| 888 | if (value.has_value()) { |
| 889 | *outInterlaced = *value; |
| 890 | return OK; |
| 891 | } |
| 892 | return UNKNOWN_TRANSACTION; |
| 893 | } |
| 894 | |
| 895 | status_t Gralloc5Mapper::getInterlaced(buffer_handle_t bufferHandle, |
| 896 | ui::Interlaced *outInterlaced) const { |
| 897 | if (!outInterlaced) { |
| 898 | return BAD_VALUE; |
| 899 | } |
| 900 | ExtendableType interlaced; |
| 901 | status_t error = getInterlaced(bufferHandle, &interlaced); |
| 902 | if (error) { |
| 903 | return error; |
| 904 | } |
| 905 | if (!gralloc4::isStandardInterlaced(interlaced)) { |
| 906 | return BAD_TYPE; |
| 907 | } |
| 908 | *outInterlaced = gralloc4::getStandardInterlacedValue(interlaced); |
| 909 | return NO_ERROR; |
| 910 | } |
| 911 | |
| 912 | status_t Gralloc5Mapper::getChromaSiting( |
| 913 | buffer_handle_t bufferHandle, |
| 914 | aidl::android::hardware::graphics::common::ExtendableType *outChromaSiting) const { |
| 915 | auto value = getStandardMetadata<StandardMetadataType::CHROMA_SITING>(mMapper, bufferHandle); |
| 916 | if (value.has_value()) { |
| 917 | *outChromaSiting = *value; |
| 918 | return OK; |
| 919 | } |
| 920 | return UNKNOWN_TRANSACTION; |
| 921 | } |
| 922 | |
| 923 | status_t Gralloc5Mapper::getChromaSiting(buffer_handle_t bufferHandle, |
| 924 | ui::ChromaSiting *outChromaSiting) const { |
| 925 | if (!outChromaSiting) { |
| 926 | return BAD_VALUE; |
| 927 | } |
| 928 | ExtendableType chromaSiting; |
| 929 | status_t error = getChromaSiting(bufferHandle, &chromaSiting); |
| 930 | if (error) { |
| 931 | return error; |
| 932 | } |
| 933 | if (!gralloc4::isStandardChromaSiting(chromaSiting)) { |
| 934 | return BAD_TYPE; |
| 935 | } |
| 936 | *outChromaSiting = gralloc4::getStandardChromaSitingValue(chromaSiting); |
| 937 | return NO_ERROR; |
| 938 | } |
| 939 | |
| 940 | status_t Gralloc5Mapper::getPlaneLayouts(buffer_handle_t bufferHandle, |
| 941 | std::vector<ui::PlaneLayout> *outPlaneLayouts) const { |
| 942 | auto value = getStandardMetadata<StandardMetadataType::PLANE_LAYOUTS>(mMapper, bufferHandle); |
| 943 | if (value.has_value()) { |
| 944 | *outPlaneLayouts = *value; |
| 945 | return OK; |
| 946 | } |
| 947 | return UNKNOWN_TRANSACTION; |
| 948 | } |
| 949 | |
| 950 | status_t Gralloc5Mapper::getDataspace(buffer_handle_t bufferHandle, |
| 951 | ui::Dataspace *outDataspace) const { |
| 952 | auto value = getStandardMetadata<StandardMetadataType::DATASPACE>(mMapper, bufferHandle); |
| 953 | if (value.has_value()) { |
| 954 | *outDataspace = static_cast<ui::Dataspace>(*value); |
| 955 | return OK; |
| 956 | } |
| 957 | return UNKNOWN_TRANSACTION; |
| 958 | } |
| 959 | |
| 960 | status_t Gralloc5Mapper::setDataspace(buffer_handle_t bufferHandle, ui::Dataspace dataspace) const { |
| 961 | return setStandardMetadata<StandardMetadataType::DATASPACE>(mMapper, bufferHandle, |
| 962 | static_cast<Dataspace>(dataspace)); |
| 963 | } |
| 964 | |
| 965 | status_t Gralloc5Mapper::getBlendMode(buffer_handle_t bufferHandle, |
| 966 | ui::BlendMode *outBlendMode) const { |
| 967 | auto value = getStandardMetadata<StandardMetadataType::BLEND_MODE>(mMapper, bufferHandle); |
| 968 | if (value.has_value()) { |
| 969 | *outBlendMode = static_cast<ui::BlendMode>(*value); |
| 970 | return OK; |
| 971 | } |
| 972 | return UNKNOWN_TRANSACTION; |
| 973 | } |
| 974 | |
| 975 | status_t Gralloc5Mapper::getSmpte2086(buffer_handle_t bufferHandle, |
| 976 | std::optional<ui::Smpte2086> *outSmpte2086) const { |
| 977 | auto value = getStandardMetadata<StandardMetadataType::SMPTE2086>(mMapper, bufferHandle); |
| 978 | if (value.has_value()) { |
| 979 | *outSmpte2086 = *value; |
| 980 | return OK; |
| 981 | } |
| 982 | return UNKNOWN_TRANSACTION; |
| 983 | } |
| 984 | |
| 985 | status_t Gralloc5Mapper::setSmpte2086(buffer_handle_t bufferHandle, |
| 986 | std::optional<ui::Smpte2086> smpte2086) const { |
| 987 | return setStandardMetadata<StandardMetadataType::SMPTE2086>(mMapper, bufferHandle, smpte2086); |
| 988 | } |
| 989 | |
| 990 | status_t Gralloc5Mapper::getCta861_3(buffer_handle_t bufferHandle, |
| 991 | std::optional<ui::Cta861_3> *outCta861_3) const { |
| 992 | auto value = getStandardMetadata<StandardMetadataType::CTA861_3>(mMapper, bufferHandle); |
| 993 | if (value.has_value()) { |
| 994 | *outCta861_3 = *value; |
| 995 | return OK; |
| 996 | } |
| 997 | return UNKNOWN_TRANSACTION; |
| 998 | } |
| 999 | |
| 1000 | status_t Gralloc5Mapper::setCta861_3(buffer_handle_t bufferHandle, |
| 1001 | std::optional<ui::Cta861_3> cta861_3) const { |
| 1002 | return setStandardMetadata<StandardMetadataType::CTA861_3>(mMapper, bufferHandle, cta861_3); |
| 1003 | } |
| 1004 | |
| 1005 | status_t Gralloc5Mapper::getSmpte2094_40( |
| 1006 | buffer_handle_t bufferHandle, std::optional<std::vector<uint8_t>> *outSmpte2094_40) const { |
| 1007 | auto value = getStandardMetadata<StandardMetadataType::SMPTE2094_40>(mMapper, bufferHandle); |
| 1008 | if (value.has_value()) { |
| 1009 | *outSmpte2094_40 = std::move(*value); |
| 1010 | return OK; |
| 1011 | } |
| 1012 | return UNKNOWN_TRANSACTION; |
| 1013 | } |
| 1014 | |
| 1015 | status_t Gralloc5Mapper::setSmpte2094_40(buffer_handle_t bufferHandle, |
| 1016 | std::optional<std::vector<uint8_t>> smpte2094_40) const { |
| 1017 | return setStandardMetadata<StandardMetadataType::SMPTE2094_40>(mMapper, bufferHandle, |
| 1018 | smpte2094_40); |
| 1019 | } |
| 1020 | |
| 1021 | status_t Gralloc5Mapper::getSmpte2094_10( |
| 1022 | buffer_handle_t bufferHandle, std::optional<std::vector<uint8_t>> *outSmpte2094_10) const { |
| 1023 | auto value = getStandardMetadata<StandardMetadataType::SMPTE2094_10>(mMapper, bufferHandle); |
| 1024 | if (value.has_value()) { |
| 1025 | *outSmpte2094_10 = std::move(*value); |
| 1026 | return OK; |
| 1027 | } |
| 1028 | return UNKNOWN_TRANSACTION; |
| 1029 | } |
| 1030 | |
| 1031 | status_t Gralloc5Mapper::setSmpte2094_10(buffer_handle_t bufferHandle, |
| 1032 | std::optional<std::vector<uint8_t>> smpte2094_10) const { |
| 1033 | return setStandardMetadata<StandardMetadataType::SMPTE2094_10>(mMapper, bufferHandle, |
| 1034 | smpte2094_10); |
| 1035 | } |
| 1036 | |
John Reck | 0ff95c9 | 2022-12-08 11:45:29 -0500 | [diff] [blame] | 1037 | } // namespace android |