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> |
| 25 | #include <binder/IPCThreadState.h> |
| 26 | #include <dlfcn.h> |
| 27 | #include <ui/FatVector.h> |
| 28 | #include <vndksupport/linker.h> |
| 29 | |
| 30 | using namespace aidl::android::hardware::graphics::allocator; |
| 31 | using namespace aidl::android::hardware::graphics::common; |
| 32 | using namespace ::android::hardware::graphics::mapper; |
| 33 | |
| 34 | namespace android { |
| 35 | |
| 36 | static const auto kIAllocatorServiceName = IAllocator::descriptor + std::string("/default"); |
| 37 | static const auto kIAllocatorMinimumVersion = 2; |
| 38 | |
| 39 | // TODO(b/72323293, b/72703005): Remove these invalid bits from callers |
| 40 | static constexpr uint64_t kRemovedUsageBits = static_cast<uint64_t>((1 << 10) | (1 << 13)); |
| 41 | |
| 42 | typedef AIMapper_Error (*AIMapper_loadIMapperFn)(AIMapper *_Nullable *_Nonnull outImplementation); |
| 43 | |
| 44 | struct Gralloc5 { |
| 45 | std::shared_ptr<IAllocator> allocator; |
| 46 | AIMapper *mapper = nullptr; |
| 47 | }; |
| 48 | |
| 49 | static std::shared_ptr<IAllocator> waitForAllocator() { |
| 50 | if (__builtin_available(android 31, *)) { |
| 51 | if (!AServiceManager_isDeclared(kIAllocatorServiceName.c_str())) { |
| 52 | return nullptr; |
| 53 | } |
| 54 | auto allocator = IAllocator::fromBinder( |
| 55 | ndk::SpAIBinder(AServiceManager_waitForService(kIAllocatorServiceName.c_str()))); |
| 56 | if (!allocator) { |
| 57 | ALOGE("AIDL IAllocator declared but failed to get service"); |
| 58 | return nullptr; |
| 59 | } |
| 60 | |
| 61 | int32_t version = 0; |
| 62 | if (!allocator->getInterfaceVersion(&version).isOk()) { |
| 63 | ALOGE("Failed to query interface version"); |
| 64 | return nullptr; |
| 65 | } |
| 66 | if (version < kIAllocatorMinimumVersion) { |
| 67 | return nullptr; |
| 68 | } |
| 69 | return allocator; |
| 70 | } else { |
| 71 | // TODO: LOG_ALWAYS_FATAL("libui is not backwards compatible"); |
| 72 | return nullptr; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | static void *loadIMapperLibrary() { |
| 77 | static void *imapperLibrary = []() -> void * { |
| 78 | auto allocator = waitForAllocator(); |
| 79 | std::string mapperSuffix; |
| 80 | auto status = allocator->getIMapperLibrarySuffix(&mapperSuffix); |
| 81 | if (!status.isOk()) { |
| 82 | ALOGE("Failed to get IMapper library suffix"); |
| 83 | return nullptr; |
| 84 | } |
| 85 | |
| 86 | std::string lib_name = "mapper." + mapperSuffix + ".so"; |
| 87 | void *so = android_load_sphal_library(lib_name.c_str(), RTLD_LOCAL | RTLD_NOW); |
| 88 | if (!so) { |
| 89 | ALOGE("Failed to load %s", lib_name.c_str()); |
| 90 | } |
| 91 | return so; |
| 92 | }(); |
| 93 | return imapperLibrary; |
| 94 | } |
| 95 | |
| 96 | static const Gralloc5 &getInstance() { |
| 97 | static Gralloc5 instance = []() { |
| 98 | auto allocator = waitForAllocator(); |
| 99 | if (!allocator) { |
| 100 | return Gralloc5{}; |
| 101 | } |
| 102 | void *so = loadIMapperLibrary(); |
| 103 | if (!so) { |
| 104 | return Gralloc5{}; |
| 105 | } |
| 106 | auto loadIMapper = (AIMapper_loadIMapperFn)dlsym(so, "AIMapper_loadIMapper"); |
| 107 | AIMapper *mapper = nullptr; |
| 108 | AIMapper_Error error = loadIMapper(&mapper); |
| 109 | if (error != AIMAPPER_ERROR_NONE) { |
| 110 | ALOGE("AIMapper_loadIMapper failed %d", error); |
| 111 | return Gralloc5{}; |
| 112 | } |
| 113 | return Gralloc5{std::move(allocator), mapper}; |
| 114 | }(); |
| 115 | return instance; |
| 116 | } |
| 117 | |
| 118 | template <StandardMetadataType T> |
| 119 | static auto getStandardMetadata(AIMapper *mapper, buffer_handle_t bufferHandle) |
| 120 | -> decltype(StandardMetadata<T>::value::decode(nullptr, 0)) { |
| 121 | using Value = typename StandardMetadata<T>::value; |
| 122 | // TODO: Tune for common-case better |
| 123 | FatVector<uint8_t, 128> buffer; |
| 124 | int32_t sizeRequired = mapper->v5.getStandardMetadata(bufferHandle, static_cast<int64_t>(T), |
| 125 | buffer.data(), buffer.size()); |
| 126 | if (sizeRequired < 0) { |
| 127 | ALOGW_IF(-AIMAPPER_ERROR_UNSUPPORTED != sizeRequired, |
| 128 | "Unexpected error %d from valid getStandardMetadata call", -sizeRequired); |
| 129 | return std::nullopt; |
| 130 | } |
| 131 | if ((size_t)sizeRequired > buffer.size()) { |
| 132 | buffer.resize(sizeRequired); |
| 133 | sizeRequired = mapper->v5.getStandardMetadata(bufferHandle, static_cast<int64_t>(T), |
| 134 | buffer.data(), buffer.size()); |
| 135 | } |
| 136 | if (sizeRequired < 0 || (size_t)sizeRequired > buffer.size()) { |
| 137 | ALOGW("getStandardMetadata failed, received %d with buffer size %zd", sizeRequired, |
| 138 | buffer.size()); |
| 139 | // Generate a fail type |
| 140 | return std::nullopt; |
| 141 | } |
| 142 | return Value::decode(buffer.data(), sizeRequired); |
| 143 | } |
| 144 | |
| 145 | template <StandardMetadataType T> |
| 146 | static AIMapper_Error setStandardMetadata(AIMapper *mapper, buffer_handle_t bufferHandle, |
| 147 | const typename StandardMetadata<T>::value_type &value) { |
| 148 | using Value = typename StandardMetadata<T>::value; |
| 149 | int32_t sizeRequired = Value::encode(value, nullptr, 0); |
| 150 | if (sizeRequired < 0) { |
| 151 | ALOGW("Failed to calculate required size"); |
| 152 | return static_cast<AIMapper_Error>(-sizeRequired); |
| 153 | } |
| 154 | FatVector<uint8_t, 128> buffer; |
| 155 | buffer.resize(sizeRequired); |
| 156 | sizeRequired = Value::encode(value, buffer.data(), buffer.size()); |
| 157 | if (sizeRequired < 0 || (size_t)sizeRequired > buffer.size()) { |
| 158 | ALOGW("Failed to encode with calculated size %d; buffer size %zd", sizeRequired, |
| 159 | buffer.size()); |
| 160 | return static_cast<AIMapper_Error>(-sizeRequired); |
| 161 | } |
| 162 | return mapper->v5.setStandardMetadata(bufferHandle, static_cast<int64_t>(T), buffer.data(), |
| 163 | sizeRequired); |
| 164 | } |
| 165 | |
| 166 | Gralloc5Allocator::Gralloc5Allocator(const Gralloc5Mapper &mapper) : mMapper(mapper) { |
| 167 | mAllocator = getInstance().allocator; |
| 168 | } |
| 169 | |
| 170 | bool Gralloc5Allocator::isLoaded() const { |
| 171 | return mAllocator != nullptr; |
| 172 | } |
| 173 | |
| 174 | static uint64_t getValidUsageBits() { |
| 175 | static const uint64_t validUsageBits = []() -> uint64_t { |
| 176 | uint64_t bits = 0; |
| 177 | for (const auto bit : ndk::enum_range<BufferUsage>{}) { |
| 178 | bits |= static_cast<int64_t>(bit); |
| 179 | } |
| 180 | return bits; |
| 181 | }(); |
| 182 | return validUsageBits | kRemovedUsageBits; |
| 183 | } |
| 184 | |
| 185 | static std::optional<BufferDescriptorInfo> makeDescriptor(std::string requestorName, uint32_t width, |
| 186 | uint32_t height, PixelFormat format, |
| 187 | uint32_t layerCount, uint64_t usage) { |
| 188 | uint64_t validUsageBits = getValidUsageBits(); |
| 189 | if (usage & ~validUsageBits) { |
| 190 | ALOGE("buffer descriptor contains invalid usage bits 0x%" PRIx64, usage & ~validUsageBits); |
| 191 | return std::nullopt; |
| 192 | } |
| 193 | |
| 194 | BufferDescriptorInfo descriptorInfo{ |
| 195 | .width = static_cast<int32_t>(width), |
| 196 | .height = static_cast<int32_t>(height), |
| 197 | .layerCount = static_cast<int32_t>(layerCount), |
| 198 | .format = static_cast<::aidl::android::hardware::graphics::common::PixelFormat>(format), |
| 199 | .usage = static_cast<BufferUsage>(usage), |
| 200 | }; |
| 201 | auto nameLength = std::min(requestorName.length(), descriptorInfo.name.size() - 1); |
| 202 | memcpy(descriptorInfo.name.data(), requestorName.data(), nameLength); |
| 203 | requestorName.data()[nameLength] = 0; |
| 204 | return descriptorInfo; |
| 205 | } |
| 206 | |
| 207 | std::string Gralloc5Allocator::dumpDebugInfo(bool less) const { |
| 208 | return mMapper.dumpBuffers(less); |
| 209 | } |
| 210 | |
| 211 | status_t Gralloc5Allocator::allocate(std::string requestorName, uint32_t width, uint32_t height, |
| 212 | android::PixelFormat format, uint32_t layerCount, |
| 213 | uint64_t usage, uint32_t bufferCount, uint32_t *outStride, |
| 214 | buffer_handle_t *outBufferHandles, bool importBuffers) const { |
| 215 | auto descriptorInfo = makeDescriptor(requestorName, width, height, format, layerCount, usage); |
| 216 | if (!descriptorInfo) { |
| 217 | return BAD_VALUE; |
| 218 | } |
| 219 | |
| 220 | AllocationResult result; |
| 221 | auto status = mAllocator->allocate2(*descriptorInfo, bufferCount, &result); |
| 222 | if (!status.isOk()) { |
| 223 | auto error = status.getExceptionCode(); |
| 224 | if (error == EX_SERVICE_SPECIFIC) { |
| 225 | error = status.getServiceSpecificError(); |
| 226 | } |
| 227 | if (error == OK) { |
| 228 | error = UNKNOWN_ERROR; |
| 229 | } |
| 230 | return error; |
| 231 | } |
| 232 | |
| 233 | if (importBuffers) { |
| 234 | for (uint32_t i = 0; i < bufferCount; i++) { |
| 235 | auto handle = makeFromAidl(result.buffers[i]); |
| 236 | auto error = mMapper.importBuffer(handle, &outBufferHandles[i]); |
| 237 | native_handle_delete(handle); |
| 238 | if (error != NO_ERROR) { |
| 239 | for (uint32_t j = 0; j < i; j++) { |
| 240 | mMapper.freeBuffer(outBufferHandles[j]); |
| 241 | outBufferHandles[j] = nullptr; |
| 242 | } |
| 243 | return error; |
| 244 | } |
| 245 | } |
| 246 | } else { |
| 247 | for (uint32_t i = 0; i < bufferCount; i++) { |
| 248 | outBufferHandles[i] = dupFromAidl(result.buffers[i]); |
| 249 | if (!outBufferHandles[i]) { |
| 250 | for (uint32_t j = 0; j < i; j++) { |
| 251 | auto buffer = const_cast<native_handle_t *>(outBufferHandles[j]); |
| 252 | native_handle_close(buffer); |
| 253 | native_handle_delete(buffer); |
| 254 | outBufferHandles[j] = nullptr; |
| 255 | } |
| 256 | return NO_MEMORY; |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | *outStride = result.stride; |
| 262 | |
| 263 | // Release all the resources held by AllocationResult (specifically any remaining FDs) |
| 264 | result = {}; |
| 265 | // make sure the kernel driver sees BC_FREE_BUFFER and closes the fds now |
| 266 | // TODO: Re-enable this at some point if it's necessary. We can't do it now because libui |
| 267 | // is marked apex_available (b/214400477) and libbinder isn't (which of course is correct) |
| 268 | // IPCThreadState::self()->flushCommands(); |
| 269 | |
| 270 | return OK; |
| 271 | } |
| 272 | |
| 273 | void Gralloc5Mapper::preload() { |
| 274 | // TODO(b/261858155): Implement. We can't bounce off of IAllocator for this because zygote can't |
| 275 | // use binder. So when an alternate strategy of retrieving the library prefix is available, |
| 276 | // use that here. |
| 277 | } |
| 278 | |
| 279 | Gralloc5Mapper::Gralloc5Mapper() { |
| 280 | mMapper = getInstance().mapper; |
| 281 | } |
| 282 | |
| 283 | bool Gralloc5Mapper::isLoaded() const { |
| 284 | return mMapper != nullptr && mMapper->version >= AIMAPPER_VERSION_5; |
| 285 | } |
| 286 | |
| 287 | std::string Gralloc5Mapper::dumpBuffer(buffer_handle_t bufferHandle, bool less) const { |
| 288 | // TODO(b/261858392): Implement |
| 289 | (void)bufferHandle; |
| 290 | (void)less; |
| 291 | return {}; |
| 292 | } |
| 293 | |
| 294 | std::string Gralloc5Mapper::dumpBuffers(bool less) const { |
| 295 | // TODO(b/261858392): Implement |
| 296 | (void)less; |
| 297 | return {}; |
| 298 | } |
| 299 | |
| 300 | status_t Gralloc5Mapper::importBuffer(const native_handle_t *rawHandle, |
| 301 | buffer_handle_t *outBufferHandle) const { |
| 302 | return mMapper->v5.importBuffer(rawHandle, outBufferHandle); |
| 303 | } |
| 304 | |
| 305 | void Gralloc5Mapper::freeBuffer(buffer_handle_t bufferHandle) const { |
| 306 | mMapper->v5.freeBuffer(bufferHandle); |
| 307 | } |
| 308 | |
| 309 | status_t Gralloc5Mapper::validateBufferSize(buffer_handle_t bufferHandle, uint32_t width, |
| 310 | uint32_t height, PixelFormat format, |
| 311 | uint32_t layerCount, uint64_t usage, |
| 312 | uint32_t stride) const { |
| 313 | { |
| 314 | auto value = getStandardMetadata<StandardMetadataType::WIDTH>(mMapper, bufferHandle); |
| 315 | if (width != value) { |
| 316 | ALOGW("Width didn't match, expected %d got %" PRId64, width, value.value_or(-1)); |
| 317 | return BAD_VALUE; |
| 318 | } |
| 319 | } |
| 320 | { |
| 321 | auto value = getStandardMetadata<StandardMetadataType::HEIGHT>(mMapper, bufferHandle); |
| 322 | if (height != value) { |
| 323 | ALOGW("Height didn't match, expected %d got %" PRId64, height, value.value_or(-1)); |
| 324 | return BAD_VALUE; |
| 325 | } |
| 326 | } |
| 327 | { |
| 328 | auto value = |
| 329 | getStandardMetadata<StandardMetadataType::PIXEL_FORMAT_REQUESTED>(mMapper, |
| 330 | bufferHandle); |
| 331 | if (static_cast<::aidl::android::hardware::graphics::common::PixelFormat>(format) != |
| 332 | value) { |
| 333 | ALOGW("Format didn't match, expected %d got %s", format, |
| 334 | value.has_value() ? toString(*value).c_str() : "<null>"); |
| 335 | return BAD_VALUE; |
| 336 | } |
| 337 | } |
| 338 | { |
| 339 | auto value = getStandardMetadata<StandardMetadataType::LAYER_COUNT>(mMapper, bufferHandle); |
| 340 | if (layerCount != value) { |
| 341 | ALOGW("Layer count didn't match, expected %d got %" PRId64, layerCount, |
| 342 | value.value_or(-1)); |
| 343 | return BAD_VALUE; |
| 344 | } |
| 345 | } |
| 346 | { |
| 347 | auto value = getStandardMetadata<StandardMetadataType::USAGE>(mMapper, bufferHandle); |
| 348 | if (static_cast<BufferUsage>(usage) != value) { |
| 349 | ALOGW("Usage didn't match, expected %" PRIu64 " got %" PRId64, usage, |
| 350 | static_cast<int64_t>(value.value_or(BufferUsage::CPU_READ_NEVER))); |
| 351 | return BAD_VALUE; |
| 352 | } |
| 353 | } |
| 354 | { |
John Reck | e225b28 | 2023-03-24 16:15:17 -0400 | [diff] [blame^] | 355 | auto value = getStandardMetadata<StandardMetadataType::STRIDE>(mMapper, bufferHandle); |
| 356 | if (stride != value) { |
| 357 | ALOGW("Stride didn't match, expected %" PRIu32 " got %" PRId32, stride, |
| 358 | value.value_or(-1)); |
| 359 | return BAD_VALUE; |
| 360 | } |
John Reck | 0ff95c9 | 2022-12-08 11:45:29 -0500 | [diff] [blame] | 361 | } |
| 362 | return OK; |
| 363 | } |
| 364 | |
| 365 | void Gralloc5Mapper::getTransportSize(buffer_handle_t bufferHandle, uint32_t *outNumFds, |
| 366 | uint32_t *outNumInts) const { |
| 367 | mMapper->v5.getTransportSize(bufferHandle, outNumFds, outNumInts); |
| 368 | } |
| 369 | |
| 370 | status_t Gralloc5Mapper::lock(buffer_handle_t bufferHandle, uint64_t usage, const Rect &bounds, |
| 371 | int acquireFence, void **outData, int32_t *outBytesPerPixel, |
| 372 | int32_t *outBytesPerStride) const { |
| 373 | std::vector<ui::PlaneLayout> planeLayouts; |
| 374 | status_t err = getPlaneLayouts(bufferHandle, &planeLayouts); |
| 375 | |
| 376 | if (err == NO_ERROR && !planeLayouts.empty()) { |
| 377 | if (outBytesPerPixel) { |
| 378 | int32_t bitsPerPixel = planeLayouts.front().sampleIncrementInBits; |
| 379 | for (const auto &planeLayout : planeLayouts) { |
| 380 | if (bitsPerPixel != planeLayout.sampleIncrementInBits) { |
| 381 | bitsPerPixel = -1; |
| 382 | } |
| 383 | } |
| 384 | if (bitsPerPixel >= 0 && bitsPerPixel % 8 == 0) { |
| 385 | *outBytesPerPixel = bitsPerPixel / 8; |
| 386 | } else { |
| 387 | *outBytesPerPixel = -1; |
| 388 | } |
| 389 | } |
| 390 | if (outBytesPerStride) { |
| 391 | int32_t bytesPerStride = planeLayouts.front().strideInBytes; |
| 392 | for (const auto &planeLayout : planeLayouts) { |
| 393 | if (bytesPerStride != planeLayout.strideInBytes) { |
| 394 | bytesPerStride = -1; |
| 395 | } |
| 396 | } |
| 397 | if (bytesPerStride >= 0) { |
| 398 | *outBytesPerStride = bytesPerStride; |
| 399 | } else { |
| 400 | *outBytesPerStride = -1; |
| 401 | } |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | auto status = mMapper->v5.lock(bufferHandle, usage, bounds, acquireFence, outData); |
| 406 | |
| 407 | ALOGW_IF(status != AIMAPPER_ERROR_NONE, "lock(%p, ...) failed: %d", bufferHandle, status); |
| 408 | return static_cast<status_t>(status); |
| 409 | } |
| 410 | |
| 411 | status_t Gralloc5Mapper::lock(buffer_handle_t bufferHandle, uint64_t usage, const Rect &bounds, |
| 412 | int acquireFence, android_ycbcr *outYcbcr) const { |
| 413 | if (!outYcbcr) { |
| 414 | return BAD_VALUE; |
| 415 | } |
| 416 | |
| 417 | // TODO(b/262279301): Change the return type of ::unlock to unique_fd instead of int so that |
| 418 | // ignoring the return value "just works" instead |
| 419 | auto unlock = [this](buffer_handle_t bufferHandle) { |
| 420 | int fence = this->unlock(bufferHandle); |
| 421 | if (fence != -1) { |
| 422 | ::close(fence); |
| 423 | } |
| 424 | }; |
| 425 | |
| 426 | std::vector<ui::PlaneLayout> planeLayouts; |
| 427 | status_t error = getPlaneLayouts(bufferHandle, &planeLayouts); |
| 428 | if (error != NO_ERROR) { |
| 429 | return error; |
| 430 | } |
| 431 | |
| 432 | void *data = nullptr; |
| 433 | error = lock(bufferHandle, usage, bounds, acquireFence, &data, nullptr, nullptr); |
| 434 | if (error != NO_ERROR) { |
| 435 | return error; |
| 436 | } |
| 437 | |
| 438 | android_ycbcr ycbcr; |
| 439 | |
| 440 | ycbcr.y = nullptr; |
| 441 | ycbcr.cb = nullptr; |
| 442 | ycbcr.cr = nullptr; |
| 443 | ycbcr.ystride = 0; |
| 444 | ycbcr.cstride = 0; |
| 445 | ycbcr.chroma_step = 0; |
| 446 | |
| 447 | for (const auto &planeLayout : planeLayouts) { |
| 448 | for (const auto &planeLayoutComponent : planeLayout.components) { |
| 449 | if (!gralloc4::isStandardPlaneLayoutComponentType(planeLayoutComponent.type)) { |
| 450 | continue; |
| 451 | } |
| 452 | |
| 453 | uint8_t *tmpData = static_cast<uint8_t *>(data) + planeLayout.offsetInBytes; |
| 454 | |
| 455 | // Note that `offsetInBits` may not be a multiple of 8 for packed formats (e.g. P010) |
| 456 | // but we still want to point to the start of the first byte. |
| 457 | tmpData += (planeLayoutComponent.offsetInBits / 8); |
| 458 | |
| 459 | uint64_t sampleIncrementInBytes; |
| 460 | |
| 461 | auto type = static_cast<PlaneLayoutComponentType>(planeLayoutComponent.type.value); |
| 462 | switch (type) { |
| 463 | case PlaneLayoutComponentType::Y: |
| 464 | if ((ycbcr.y != nullptr) || (planeLayout.sampleIncrementInBits % 8 != 0)) { |
| 465 | unlock(bufferHandle); |
| 466 | return BAD_VALUE; |
| 467 | } |
| 468 | ycbcr.y = tmpData; |
| 469 | ycbcr.ystride = planeLayout.strideInBytes; |
| 470 | break; |
| 471 | |
| 472 | case PlaneLayoutComponentType::CB: |
| 473 | case PlaneLayoutComponentType::CR: |
| 474 | if (planeLayout.sampleIncrementInBits % 8 != 0) { |
| 475 | unlock(bufferHandle); |
| 476 | return BAD_VALUE; |
| 477 | } |
| 478 | |
| 479 | sampleIncrementInBytes = planeLayout.sampleIncrementInBits / 8; |
| 480 | if ((sampleIncrementInBytes != 1) && (sampleIncrementInBytes != 2) && |
| 481 | (sampleIncrementInBytes != 4)) { |
| 482 | unlock(bufferHandle); |
| 483 | return BAD_VALUE; |
| 484 | } |
| 485 | |
| 486 | if (ycbcr.cstride == 0 && ycbcr.chroma_step == 0) { |
| 487 | ycbcr.cstride = planeLayout.strideInBytes; |
| 488 | ycbcr.chroma_step = sampleIncrementInBytes; |
| 489 | } else { |
| 490 | if ((static_cast<int64_t>(ycbcr.cstride) != planeLayout.strideInBytes) || |
| 491 | (ycbcr.chroma_step != sampleIncrementInBytes)) { |
| 492 | unlock(bufferHandle); |
| 493 | return BAD_VALUE; |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | if (type == PlaneLayoutComponentType::CB) { |
| 498 | if (ycbcr.cb != nullptr) { |
| 499 | unlock(bufferHandle); |
| 500 | return BAD_VALUE; |
| 501 | } |
| 502 | ycbcr.cb = tmpData; |
| 503 | } else { |
| 504 | if (ycbcr.cr != nullptr) { |
| 505 | unlock(bufferHandle); |
| 506 | return BAD_VALUE; |
| 507 | } |
| 508 | ycbcr.cr = tmpData; |
| 509 | } |
| 510 | break; |
| 511 | default: |
| 512 | break; |
| 513 | }; |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | *outYcbcr = ycbcr; |
| 518 | return OK; |
| 519 | } |
| 520 | |
| 521 | int Gralloc5Mapper::unlock(buffer_handle_t bufferHandle) const { |
| 522 | int fence = -1; |
| 523 | AIMapper_Error error = mMapper->v5.unlock(bufferHandle, &fence); |
| 524 | if (error != AIMAPPER_ERROR_NONE) { |
| 525 | ALOGW("unlock failed with error %d", error); |
| 526 | } |
| 527 | return fence; |
| 528 | } |
| 529 | |
| 530 | status_t Gralloc5Mapper::isSupported(uint32_t width, uint32_t height, PixelFormat format, |
| 531 | uint32_t layerCount, uint64_t usage, |
| 532 | bool *outSupported) const { |
| 533 | auto descriptorInfo = makeDescriptor("", width, height, format, layerCount, usage); |
| 534 | if (!descriptorInfo) { |
| 535 | *outSupported = false; |
| 536 | return OK; |
| 537 | } |
| 538 | auto status = getInstance().allocator->isSupported(*descriptorInfo, outSupported); |
| 539 | if (!status.isOk()) { |
| 540 | ALOGW("IAllocator::isSupported error %d (%s)", status.getStatus(), status.getMessage()); |
| 541 | *outSupported = false; |
| 542 | } |
| 543 | return OK; |
| 544 | } |
| 545 | |
| 546 | status_t Gralloc5Mapper::getBufferId(buffer_handle_t bufferHandle, uint64_t *outBufferId) const { |
| 547 | auto value = getStandardMetadata<StandardMetadataType::BUFFER_ID>(mMapper, bufferHandle); |
| 548 | if (value.has_value()) { |
| 549 | *outBufferId = *value; |
| 550 | return OK; |
| 551 | } |
| 552 | return UNKNOWN_TRANSACTION; |
| 553 | } |
| 554 | |
| 555 | status_t Gralloc5Mapper::getName(buffer_handle_t bufferHandle, std::string *outName) const { |
| 556 | auto value = getStandardMetadata<StandardMetadataType::NAME>(mMapper, bufferHandle); |
| 557 | if (value.has_value()) { |
| 558 | *outName = *value; |
| 559 | return OK; |
| 560 | } |
| 561 | return UNKNOWN_TRANSACTION; |
| 562 | } |
| 563 | |
| 564 | status_t Gralloc5Mapper::getWidth(buffer_handle_t bufferHandle, uint64_t *outWidth) const { |
| 565 | auto value = getStandardMetadata<StandardMetadataType::WIDTH>(mMapper, bufferHandle); |
| 566 | if (value.has_value()) { |
| 567 | *outWidth = *value; |
| 568 | return OK; |
| 569 | } |
| 570 | return UNKNOWN_TRANSACTION; |
| 571 | } |
| 572 | |
| 573 | status_t Gralloc5Mapper::getHeight(buffer_handle_t bufferHandle, uint64_t *outHeight) const { |
| 574 | auto value = getStandardMetadata<StandardMetadataType::HEIGHT>(mMapper, bufferHandle); |
| 575 | if (value.has_value()) { |
| 576 | *outHeight = *value; |
| 577 | return OK; |
| 578 | } |
| 579 | return UNKNOWN_TRANSACTION; |
| 580 | } |
| 581 | |
| 582 | status_t Gralloc5Mapper::getLayerCount(buffer_handle_t bufferHandle, |
| 583 | uint64_t *outLayerCount) const { |
| 584 | auto value = getStandardMetadata<StandardMetadataType::LAYER_COUNT>(mMapper, bufferHandle); |
| 585 | if (value.has_value()) { |
| 586 | *outLayerCount = *value; |
| 587 | return OK; |
| 588 | } |
| 589 | return UNKNOWN_TRANSACTION; |
| 590 | } |
| 591 | |
| 592 | status_t Gralloc5Mapper::getPixelFormatRequested(buffer_handle_t bufferHandle, |
| 593 | ui::PixelFormat *outPixelFormatRequested) const { |
| 594 | auto value = getStandardMetadata<StandardMetadataType::PIXEL_FORMAT_REQUESTED>(mMapper, |
| 595 | bufferHandle); |
| 596 | if (value.has_value()) { |
| 597 | *outPixelFormatRequested = static_cast<ui::PixelFormat>(*value); |
| 598 | return OK; |
| 599 | } |
| 600 | return UNKNOWN_TRANSACTION; |
| 601 | } |
| 602 | |
| 603 | status_t Gralloc5Mapper::getPixelFormatFourCC(buffer_handle_t bufferHandle, |
| 604 | uint32_t *outPixelFormatFourCC) const { |
| 605 | auto value = |
| 606 | getStandardMetadata<StandardMetadataType::PIXEL_FORMAT_FOURCC>(mMapper, bufferHandle); |
| 607 | if (value.has_value()) { |
| 608 | *outPixelFormatFourCC = *value; |
| 609 | return OK; |
| 610 | } |
| 611 | return UNKNOWN_TRANSACTION; |
| 612 | } |
| 613 | |
| 614 | status_t Gralloc5Mapper::getPixelFormatModifier(buffer_handle_t bufferHandle, |
| 615 | uint64_t *outPixelFormatModifier) const { |
| 616 | auto value = |
| 617 | getStandardMetadata<StandardMetadataType::PIXEL_FORMAT_MODIFIER>(mMapper, bufferHandle); |
| 618 | if (value.has_value()) { |
| 619 | *outPixelFormatModifier = *value; |
| 620 | return OK; |
| 621 | } |
| 622 | return UNKNOWN_TRANSACTION; |
| 623 | } |
| 624 | |
| 625 | status_t Gralloc5Mapper::getUsage(buffer_handle_t bufferHandle, uint64_t *outUsage) const { |
| 626 | auto value = getStandardMetadata<StandardMetadataType::USAGE>(mMapper, bufferHandle); |
| 627 | if (value.has_value()) { |
| 628 | *outUsage = static_cast<uint64_t>(*value); |
| 629 | return OK; |
| 630 | } |
| 631 | return UNKNOWN_TRANSACTION; |
| 632 | } |
| 633 | |
| 634 | status_t Gralloc5Mapper::getAllocationSize(buffer_handle_t bufferHandle, |
| 635 | uint64_t *outAllocationSize) const { |
| 636 | auto value = getStandardMetadata<StandardMetadataType::ALLOCATION_SIZE>(mMapper, bufferHandle); |
| 637 | if (value.has_value()) { |
| 638 | *outAllocationSize = *value; |
| 639 | return OK; |
| 640 | } |
| 641 | return UNKNOWN_TRANSACTION; |
| 642 | } |
| 643 | |
| 644 | status_t Gralloc5Mapper::getProtectedContent(buffer_handle_t bufferHandle, |
| 645 | uint64_t *outProtectedContent) const { |
| 646 | auto value = |
| 647 | getStandardMetadata<StandardMetadataType::PROTECTED_CONTENT>(mMapper, bufferHandle); |
| 648 | if (value.has_value()) { |
| 649 | *outProtectedContent = *value; |
| 650 | return OK; |
| 651 | } |
| 652 | return UNKNOWN_TRANSACTION; |
| 653 | } |
| 654 | |
| 655 | status_t Gralloc5Mapper::getCompression( |
| 656 | buffer_handle_t bufferHandle, |
| 657 | aidl::android::hardware::graphics::common::ExtendableType *outCompression) const { |
| 658 | auto value = getStandardMetadata<StandardMetadataType::COMPRESSION>(mMapper, bufferHandle); |
| 659 | if (value.has_value()) { |
| 660 | *outCompression = *value; |
| 661 | return OK; |
| 662 | } |
| 663 | return UNKNOWN_TRANSACTION; |
| 664 | } |
| 665 | |
| 666 | status_t Gralloc5Mapper::getCompression(buffer_handle_t bufferHandle, |
| 667 | ui::Compression *outCompression) const { |
| 668 | auto value = getStandardMetadata<StandardMetadataType::COMPRESSION>(mMapper, bufferHandle); |
| 669 | if (!value.has_value()) { |
| 670 | return UNKNOWN_TRANSACTION; |
| 671 | } |
| 672 | if (!gralloc4::isStandardCompression(*value)) { |
| 673 | return BAD_TYPE; |
| 674 | } |
| 675 | *outCompression = gralloc4::getStandardCompressionValue(*value); |
| 676 | return OK; |
| 677 | } |
| 678 | |
| 679 | status_t Gralloc5Mapper::getInterlaced( |
| 680 | buffer_handle_t bufferHandle, |
| 681 | aidl::android::hardware::graphics::common::ExtendableType *outInterlaced) const { |
| 682 | auto value = getStandardMetadata<StandardMetadataType::INTERLACED>(mMapper, bufferHandle); |
| 683 | if (value.has_value()) { |
| 684 | *outInterlaced = *value; |
| 685 | return OK; |
| 686 | } |
| 687 | return UNKNOWN_TRANSACTION; |
| 688 | } |
| 689 | |
| 690 | status_t Gralloc5Mapper::getInterlaced(buffer_handle_t bufferHandle, |
| 691 | ui::Interlaced *outInterlaced) const { |
| 692 | if (!outInterlaced) { |
| 693 | return BAD_VALUE; |
| 694 | } |
| 695 | ExtendableType interlaced; |
| 696 | status_t error = getInterlaced(bufferHandle, &interlaced); |
| 697 | if (error) { |
| 698 | return error; |
| 699 | } |
| 700 | if (!gralloc4::isStandardInterlaced(interlaced)) { |
| 701 | return BAD_TYPE; |
| 702 | } |
| 703 | *outInterlaced = gralloc4::getStandardInterlacedValue(interlaced); |
| 704 | return NO_ERROR; |
| 705 | } |
| 706 | |
| 707 | status_t Gralloc5Mapper::getChromaSiting( |
| 708 | buffer_handle_t bufferHandle, |
| 709 | aidl::android::hardware::graphics::common::ExtendableType *outChromaSiting) const { |
| 710 | auto value = getStandardMetadata<StandardMetadataType::CHROMA_SITING>(mMapper, bufferHandle); |
| 711 | if (value.has_value()) { |
| 712 | *outChromaSiting = *value; |
| 713 | return OK; |
| 714 | } |
| 715 | return UNKNOWN_TRANSACTION; |
| 716 | } |
| 717 | |
| 718 | status_t Gralloc5Mapper::getChromaSiting(buffer_handle_t bufferHandle, |
| 719 | ui::ChromaSiting *outChromaSiting) const { |
| 720 | if (!outChromaSiting) { |
| 721 | return BAD_VALUE; |
| 722 | } |
| 723 | ExtendableType chromaSiting; |
| 724 | status_t error = getChromaSiting(bufferHandle, &chromaSiting); |
| 725 | if (error) { |
| 726 | return error; |
| 727 | } |
| 728 | if (!gralloc4::isStandardChromaSiting(chromaSiting)) { |
| 729 | return BAD_TYPE; |
| 730 | } |
| 731 | *outChromaSiting = gralloc4::getStandardChromaSitingValue(chromaSiting); |
| 732 | return NO_ERROR; |
| 733 | } |
| 734 | |
| 735 | status_t Gralloc5Mapper::getPlaneLayouts(buffer_handle_t bufferHandle, |
| 736 | std::vector<ui::PlaneLayout> *outPlaneLayouts) const { |
| 737 | auto value = getStandardMetadata<StandardMetadataType::PLANE_LAYOUTS>(mMapper, bufferHandle); |
| 738 | if (value.has_value()) { |
| 739 | *outPlaneLayouts = *value; |
| 740 | return OK; |
| 741 | } |
| 742 | return UNKNOWN_TRANSACTION; |
| 743 | } |
| 744 | |
| 745 | status_t Gralloc5Mapper::getDataspace(buffer_handle_t bufferHandle, |
| 746 | ui::Dataspace *outDataspace) const { |
| 747 | auto value = getStandardMetadata<StandardMetadataType::DATASPACE>(mMapper, bufferHandle); |
| 748 | if (value.has_value()) { |
| 749 | *outDataspace = static_cast<ui::Dataspace>(*value); |
| 750 | return OK; |
| 751 | } |
| 752 | return UNKNOWN_TRANSACTION; |
| 753 | } |
| 754 | |
| 755 | status_t Gralloc5Mapper::setDataspace(buffer_handle_t bufferHandle, ui::Dataspace dataspace) const { |
| 756 | return setStandardMetadata<StandardMetadataType::DATASPACE>(mMapper, bufferHandle, |
| 757 | static_cast<Dataspace>(dataspace)); |
| 758 | } |
| 759 | |
| 760 | status_t Gralloc5Mapper::getBlendMode(buffer_handle_t bufferHandle, |
| 761 | ui::BlendMode *outBlendMode) const { |
| 762 | auto value = getStandardMetadata<StandardMetadataType::BLEND_MODE>(mMapper, bufferHandle); |
| 763 | if (value.has_value()) { |
| 764 | *outBlendMode = static_cast<ui::BlendMode>(*value); |
| 765 | return OK; |
| 766 | } |
| 767 | return UNKNOWN_TRANSACTION; |
| 768 | } |
| 769 | |
| 770 | status_t Gralloc5Mapper::getSmpte2086(buffer_handle_t bufferHandle, |
| 771 | std::optional<ui::Smpte2086> *outSmpte2086) const { |
| 772 | auto value = getStandardMetadata<StandardMetadataType::SMPTE2086>(mMapper, bufferHandle); |
| 773 | if (value.has_value()) { |
| 774 | *outSmpte2086 = *value; |
| 775 | return OK; |
| 776 | } |
| 777 | return UNKNOWN_TRANSACTION; |
| 778 | } |
| 779 | |
| 780 | status_t Gralloc5Mapper::setSmpte2086(buffer_handle_t bufferHandle, |
| 781 | std::optional<ui::Smpte2086> smpte2086) const { |
| 782 | return setStandardMetadata<StandardMetadataType::SMPTE2086>(mMapper, bufferHandle, smpte2086); |
| 783 | } |
| 784 | |
| 785 | status_t Gralloc5Mapper::getCta861_3(buffer_handle_t bufferHandle, |
| 786 | std::optional<ui::Cta861_3> *outCta861_3) const { |
| 787 | auto value = getStandardMetadata<StandardMetadataType::CTA861_3>(mMapper, bufferHandle); |
| 788 | if (value.has_value()) { |
| 789 | *outCta861_3 = *value; |
| 790 | return OK; |
| 791 | } |
| 792 | return UNKNOWN_TRANSACTION; |
| 793 | } |
| 794 | |
| 795 | status_t Gralloc5Mapper::setCta861_3(buffer_handle_t bufferHandle, |
| 796 | std::optional<ui::Cta861_3> cta861_3) const { |
| 797 | return setStandardMetadata<StandardMetadataType::CTA861_3>(mMapper, bufferHandle, cta861_3); |
| 798 | } |
| 799 | |
| 800 | status_t Gralloc5Mapper::getSmpte2094_40( |
| 801 | buffer_handle_t bufferHandle, std::optional<std::vector<uint8_t>> *outSmpte2094_40) const { |
| 802 | auto value = getStandardMetadata<StandardMetadataType::SMPTE2094_40>(mMapper, bufferHandle); |
| 803 | if (value.has_value()) { |
| 804 | *outSmpte2094_40 = std::move(*value); |
| 805 | return OK; |
| 806 | } |
| 807 | return UNKNOWN_TRANSACTION; |
| 808 | } |
| 809 | |
| 810 | status_t Gralloc5Mapper::setSmpte2094_40(buffer_handle_t bufferHandle, |
| 811 | std::optional<std::vector<uint8_t>> smpte2094_40) const { |
| 812 | return setStandardMetadata<StandardMetadataType::SMPTE2094_40>(mMapper, bufferHandle, |
| 813 | smpte2094_40); |
| 814 | } |
| 815 | |
| 816 | status_t Gralloc5Mapper::getSmpte2094_10( |
| 817 | buffer_handle_t bufferHandle, std::optional<std::vector<uint8_t>> *outSmpte2094_10) const { |
| 818 | auto value = getStandardMetadata<StandardMetadataType::SMPTE2094_10>(mMapper, bufferHandle); |
| 819 | if (value.has_value()) { |
| 820 | *outSmpte2094_10 = std::move(*value); |
| 821 | return OK; |
| 822 | } |
| 823 | return UNKNOWN_TRANSACTION; |
| 824 | } |
| 825 | |
| 826 | status_t Gralloc5Mapper::setSmpte2094_10(buffer_handle_t bufferHandle, |
| 827 | std::optional<std::vector<uint8_t>> smpte2094_10) const { |
| 828 | return setStandardMetadata<StandardMetadataType::SMPTE2094_10>(mMapper, bufferHandle, |
| 829 | smpte2094_10); |
| 830 | } |
| 831 | |
John Reck | 0ff95c9 | 2022-12-08 11:45:29 -0500 | [diff] [blame] | 832 | } // namespace android |