blob: 6f196b86d55594a44c28a198581beecad85f6525 [file] [log] [blame]
John Reck0ff95c92022-12-08 11:45:29 -05001/*
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
30using namespace aidl::android::hardware::graphics::allocator;
31using namespace aidl::android::hardware::graphics::common;
32using namespace ::android::hardware::graphics::mapper;
33
34namespace android {
35
36static const auto kIAllocatorServiceName = IAllocator::descriptor + std::string("/default");
37static const auto kIAllocatorMinimumVersion = 2;
38
39// TODO(b/72323293, b/72703005): Remove these invalid bits from callers
40static constexpr uint64_t kRemovedUsageBits = static_cast<uint64_t>((1 << 10) | (1 << 13));
41
42typedef AIMapper_Error (*AIMapper_loadIMapperFn)(AIMapper *_Nullable *_Nonnull outImplementation);
43
44struct Gralloc5 {
45 std::shared_ptr<IAllocator> allocator;
46 AIMapper *mapper = nullptr;
47};
48
49static 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
76static 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
96static 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
118template <StandardMetadataType T>
119static 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
145template <StandardMetadataType T>
146static 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
166Gralloc5Allocator::Gralloc5Allocator(const Gralloc5Mapper &mapper) : mMapper(mapper) {
167 mAllocator = getInstance().allocator;
168}
169
170bool Gralloc5Allocator::isLoaded() const {
171 return mAllocator != nullptr;
172}
173
174static 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
185static 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
207std::string Gralloc5Allocator::dumpDebugInfo(bool less) const {
208 return mMapper.dumpBuffers(less);
209}
210
211status_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
273void 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
279Gralloc5Mapper::Gralloc5Mapper() {
280 mMapper = getInstance().mapper;
281}
282
283bool Gralloc5Mapper::isLoaded() const {
284 return mMapper != nullptr && mMapper->version >= AIMAPPER_VERSION_5;
285}
286
287std::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
294std::string Gralloc5Mapper::dumpBuffers(bool less) const {
295 // TODO(b/261858392): Implement
296 (void)less;
297 return {};
298}
299
300status_t Gralloc5Mapper::importBuffer(const native_handle_t *rawHandle,
301 buffer_handle_t *outBufferHandle) const {
302 return mMapper->v5.importBuffer(rawHandle, outBufferHandle);
303}
304
305void Gralloc5Mapper::freeBuffer(buffer_handle_t bufferHandle) const {
306 mMapper->v5.freeBuffer(bufferHandle);
307}
308
309status_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 {
355 (void)stride;
356 // TODO(b/261856851): Add StandardMetadataType::STRIDE && enable this
357 // auto value = getStandardMetadata<StandardMetadataType::STRIDE>(mMapper,
358 // bufferHandle); if (static_cast<BufferUsage>(usage) != value) {
359 // ALOGW("Layer count didn't match, expected %" PRIu64 " got %" PRId64, usage,
360 // static_cast<int64_t>(value.value_or(BufferUsage::CPU_READ_NEVER)));
361 // return BAD_VALUE;
362 // }
363 }
364 return OK;
365}
366
367void Gralloc5Mapper::getTransportSize(buffer_handle_t bufferHandle, uint32_t *outNumFds,
368 uint32_t *outNumInts) const {
369 mMapper->v5.getTransportSize(bufferHandle, outNumFds, outNumInts);
370}
371
372status_t Gralloc5Mapper::lock(buffer_handle_t bufferHandle, uint64_t usage, const Rect &bounds,
373 int acquireFence, void **outData, int32_t *outBytesPerPixel,
374 int32_t *outBytesPerStride) const {
375 std::vector<ui::PlaneLayout> planeLayouts;
376 status_t err = getPlaneLayouts(bufferHandle, &planeLayouts);
377
378 if (err == NO_ERROR && !planeLayouts.empty()) {
379 if (outBytesPerPixel) {
380 int32_t bitsPerPixel = planeLayouts.front().sampleIncrementInBits;
381 for (const auto &planeLayout : planeLayouts) {
382 if (bitsPerPixel != planeLayout.sampleIncrementInBits) {
383 bitsPerPixel = -1;
384 }
385 }
386 if (bitsPerPixel >= 0 && bitsPerPixel % 8 == 0) {
387 *outBytesPerPixel = bitsPerPixel / 8;
388 } else {
389 *outBytesPerPixel = -1;
390 }
391 }
392 if (outBytesPerStride) {
393 int32_t bytesPerStride = planeLayouts.front().strideInBytes;
394 for (const auto &planeLayout : planeLayouts) {
395 if (bytesPerStride != planeLayout.strideInBytes) {
396 bytesPerStride = -1;
397 }
398 }
399 if (bytesPerStride >= 0) {
400 *outBytesPerStride = bytesPerStride;
401 } else {
402 *outBytesPerStride = -1;
403 }
404 }
405 }
406
407 auto status = mMapper->v5.lock(bufferHandle, usage, bounds, acquireFence, outData);
408
409 ALOGW_IF(status != AIMAPPER_ERROR_NONE, "lock(%p, ...) failed: %d", bufferHandle, status);
410 return static_cast<status_t>(status);
411}
412
413status_t Gralloc5Mapper::lock(buffer_handle_t bufferHandle, uint64_t usage, const Rect &bounds,
414 int acquireFence, android_ycbcr *outYcbcr) const {
415 if (!outYcbcr) {
416 return BAD_VALUE;
417 }
418
419 // TODO(b/262279301): Change the return type of ::unlock to unique_fd instead of int so that
420 // ignoring the return value "just works" instead
421 auto unlock = [this](buffer_handle_t bufferHandle) {
422 int fence = this->unlock(bufferHandle);
423 if (fence != -1) {
424 ::close(fence);
425 }
426 };
427
428 std::vector<ui::PlaneLayout> planeLayouts;
429 status_t error = getPlaneLayouts(bufferHandle, &planeLayouts);
430 if (error != NO_ERROR) {
431 return error;
432 }
433
434 void *data = nullptr;
435 error = lock(bufferHandle, usage, bounds, acquireFence, &data, nullptr, nullptr);
436 if (error != NO_ERROR) {
437 return error;
438 }
439
440 android_ycbcr ycbcr;
441
442 ycbcr.y = nullptr;
443 ycbcr.cb = nullptr;
444 ycbcr.cr = nullptr;
445 ycbcr.ystride = 0;
446 ycbcr.cstride = 0;
447 ycbcr.chroma_step = 0;
448
449 for (const auto &planeLayout : planeLayouts) {
450 for (const auto &planeLayoutComponent : planeLayout.components) {
451 if (!gralloc4::isStandardPlaneLayoutComponentType(planeLayoutComponent.type)) {
452 continue;
453 }
454
455 uint8_t *tmpData = static_cast<uint8_t *>(data) + planeLayout.offsetInBytes;
456
457 // Note that `offsetInBits` may not be a multiple of 8 for packed formats (e.g. P010)
458 // but we still want to point to the start of the first byte.
459 tmpData += (planeLayoutComponent.offsetInBits / 8);
460
461 uint64_t sampleIncrementInBytes;
462
463 auto type = static_cast<PlaneLayoutComponentType>(planeLayoutComponent.type.value);
464 switch (type) {
465 case PlaneLayoutComponentType::Y:
466 if ((ycbcr.y != nullptr) || (planeLayout.sampleIncrementInBits % 8 != 0)) {
467 unlock(bufferHandle);
468 return BAD_VALUE;
469 }
470 ycbcr.y = tmpData;
471 ycbcr.ystride = planeLayout.strideInBytes;
472 break;
473
474 case PlaneLayoutComponentType::CB:
475 case PlaneLayoutComponentType::CR:
476 if (planeLayout.sampleIncrementInBits % 8 != 0) {
477 unlock(bufferHandle);
478 return BAD_VALUE;
479 }
480
481 sampleIncrementInBytes = planeLayout.sampleIncrementInBits / 8;
482 if ((sampleIncrementInBytes != 1) && (sampleIncrementInBytes != 2) &&
483 (sampleIncrementInBytes != 4)) {
484 unlock(bufferHandle);
485 return BAD_VALUE;
486 }
487
488 if (ycbcr.cstride == 0 && ycbcr.chroma_step == 0) {
489 ycbcr.cstride = planeLayout.strideInBytes;
490 ycbcr.chroma_step = sampleIncrementInBytes;
491 } else {
492 if ((static_cast<int64_t>(ycbcr.cstride) != planeLayout.strideInBytes) ||
493 (ycbcr.chroma_step != sampleIncrementInBytes)) {
494 unlock(bufferHandle);
495 return BAD_VALUE;
496 }
497 }
498
499 if (type == PlaneLayoutComponentType::CB) {
500 if (ycbcr.cb != nullptr) {
501 unlock(bufferHandle);
502 return BAD_VALUE;
503 }
504 ycbcr.cb = tmpData;
505 } else {
506 if (ycbcr.cr != nullptr) {
507 unlock(bufferHandle);
508 return BAD_VALUE;
509 }
510 ycbcr.cr = tmpData;
511 }
512 break;
513 default:
514 break;
515 };
516 }
517 }
518
519 *outYcbcr = ycbcr;
520 return OK;
521}
522
523int Gralloc5Mapper::unlock(buffer_handle_t bufferHandle) const {
524 int fence = -1;
525 AIMapper_Error error = mMapper->v5.unlock(bufferHandle, &fence);
526 if (error != AIMAPPER_ERROR_NONE) {
527 ALOGW("unlock failed with error %d", error);
528 }
529 return fence;
530}
531
532status_t Gralloc5Mapper::isSupported(uint32_t width, uint32_t height, PixelFormat format,
533 uint32_t layerCount, uint64_t usage,
534 bool *outSupported) const {
535 auto descriptorInfo = makeDescriptor("", width, height, format, layerCount, usage);
536 if (!descriptorInfo) {
537 *outSupported = false;
538 return OK;
539 }
540 auto status = getInstance().allocator->isSupported(*descriptorInfo, outSupported);
541 if (!status.isOk()) {
542 ALOGW("IAllocator::isSupported error %d (%s)", status.getStatus(), status.getMessage());
543 *outSupported = false;
544 }
545 return OK;
546}
547
548status_t Gralloc5Mapper::getBufferId(buffer_handle_t bufferHandle, uint64_t *outBufferId) const {
549 auto value = getStandardMetadata<StandardMetadataType::BUFFER_ID>(mMapper, bufferHandle);
550 if (value.has_value()) {
551 *outBufferId = *value;
552 return OK;
553 }
554 return UNKNOWN_TRANSACTION;
555}
556
557status_t Gralloc5Mapper::getName(buffer_handle_t bufferHandle, std::string *outName) const {
558 auto value = getStandardMetadata<StandardMetadataType::NAME>(mMapper, bufferHandle);
559 if (value.has_value()) {
560 *outName = *value;
561 return OK;
562 }
563 return UNKNOWN_TRANSACTION;
564}
565
566status_t Gralloc5Mapper::getWidth(buffer_handle_t bufferHandle, uint64_t *outWidth) const {
567 auto value = getStandardMetadata<StandardMetadataType::WIDTH>(mMapper, bufferHandle);
568 if (value.has_value()) {
569 *outWidth = *value;
570 return OK;
571 }
572 return UNKNOWN_TRANSACTION;
573}
574
575status_t Gralloc5Mapper::getHeight(buffer_handle_t bufferHandle, uint64_t *outHeight) const {
576 auto value = getStandardMetadata<StandardMetadataType::HEIGHT>(mMapper, bufferHandle);
577 if (value.has_value()) {
578 *outHeight = *value;
579 return OK;
580 }
581 return UNKNOWN_TRANSACTION;
582}
583
584status_t Gralloc5Mapper::getLayerCount(buffer_handle_t bufferHandle,
585 uint64_t *outLayerCount) const {
586 auto value = getStandardMetadata<StandardMetadataType::LAYER_COUNT>(mMapper, bufferHandle);
587 if (value.has_value()) {
588 *outLayerCount = *value;
589 return OK;
590 }
591 return UNKNOWN_TRANSACTION;
592}
593
594status_t Gralloc5Mapper::getPixelFormatRequested(buffer_handle_t bufferHandle,
595 ui::PixelFormat *outPixelFormatRequested) const {
596 auto value = getStandardMetadata<StandardMetadataType::PIXEL_FORMAT_REQUESTED>(mMapper,
597 bufferHandle);
598 if (value.has_value()) {
599 *outPixelFormatRequested = static_cast<ui::PixelFormat>(*value);
600 return OK;
601 }
602 return UNKNOWN_TRANSACTION;
603}
604
605status_t Gralloc5Mapper::getPixelFormatFourCC(buffer_handle_t bufferHandle,
606 uint32_t *outPixelFormatFourCC) const {
607 auto value =
608 getStandardMetadata<StandardMetadataType::PIXEL_FORMAT_FOURCC>(mMapper, bufferHandle);
609 if (value.has_value()) {
610 *outPixelFormatFourCC = *value;
611 return OK;
612 }
613 return UNKNOWN_TRANSACTION;
614}
615
616status_t Gralloc5Mapper::getPixelFormatModifier(buffer_handle_t bufferHandle,
617 uint64_t *outPixelFormatModifier) const {
618 auto value =
619 getStandardMetadata<StandardMetadataType::PIXEL_FORMAT_MODIFIER>(mMapper, bufferHandle);
620 if (value.has_value()) {
621 *outPixelFormatModifier = *value;
622 return OK;
623 }
624 return UNKNOWN_TRANSACTION;
625}
626
627status_t Gralloc5Mapper::getUsage(buffer_handle_t bufferHandle, uint64_t *outUsage) const {
628 auto value = getStandardMetadata<StandardMetadataType::USAGE>(mMapper, bufferHandle);
629 if (value.has_value()) {
630 *outUsage = static_cast<uint64_t>(*value);
631 return OK;
632 }
633 return UNKNOWN_TRANSACTION;
634}
635
636status_t Gralloc5Mapper::getAllocationSize(buffer_handle_t bufferHandle,
637 uint64_t *outAllocationSize) const {
638 auto value = getStandardMetadata<StandardMetadataType::ALLOCATION_SIZE>(mMapper, bufferHandle);
639 if (value.has_value()) {
640 *outAllocationSize = *value;
641 return OK;
642 }
643 return UNKNOWN_TRANSACTION;
644}
645
646status_t Gralloc5Mapper::getProtectedContent(buffer_handle_t bufferHandle,
647 uint64_t *outProtectedContent) const {
648 auto value =
649 getStandardMetadata<StandardMetadataType::PROTECTED_CONTENT>(mMapper, bufferHandle);
650 if (value.has_value()) {
651 *outProtectedContent = *value;
652 return OK;
653 }
654 return UNKNOWN_TRANSACTION;
655}
656
657status_t Gralloc5Mapper::getCompression(
658 buffer_handle_t bufferHandle,
659 aidl::android::hardware::graphics::common::ExtendableType *outCompression) const {
660 auto value = getStandardMetadata<StandardMetadataType::COMPRESSION>(mMapper, bufferHandle);
661 if (value.has_value()) {
662 *outCompression = *value;
663 return OK;
664 }
665 return UNKNOWN_TRANSACTION;
666}
667
668status_t Gralloc5Mapper::getCompression(buffer_handle_t bufferHandle,
669 ui::Compression *outCompression) const {
670 auto value = getStandardMetadata<StandardMetadataType::COMPRESSION>(mMapper, bufferHandle);
671 if (!value.has_value()) {
672 return UNKNOWN_TRANSACTION;
673 }
674 if (!gralloc4::isStandardCompression(*value)) {
675 return BAD_TYPE;
676 }
677 *outCompression = gralloc4::getStandardCompressionValue(*value);
678 return OK;
679}
680
681status_t Gralloc5Mapper::getInterlaced(
682 buffer_handle_t bufferHandle,
683 aidl::android::hardware::graphics::common::ExtendableType *outInterlaced) const {
684 auto value = getStandardMetadata<StandardMetadataType::INTERLACED>(mMapper, bufferHandle);
685 if (value.has_value()) {
686 *outInterlaced = *value;
687 return OK;
688 }
689 return UNKNOWN_TRANSACTION;
690}
691
692status_t Gralloc5Mapper::getInterlaced(buffer_handle_t bufferHandle,
693 ui::Interlaced *outInterlaced) const {
694 if (!outInterlaced) {
695 return BAD_VALUE;
696 }
697 ExtendableType interlaced;
698 status_t error = getInterlaced(bufferHandle, &interlaced);
699 if (error) {
700 return error;
701 }
702 if (!gralloc4::isStandardInterlaced(interlaced)) {
703 return BAD_TYPE;
704 }
705 *outInterlaced = gralloc4::getStandardInterlacedValue(interlaced);
706 return NO_ERROR;
707}
708
709status_t Gralloc5Mapper::getChromaSiting(
710 buffer_handle_t bufferHandle,
711 aidl::android::hardware::graphics::common::ExtendableType *outChromaSiting) const {
712 auto value = getStandardMetadata<StandardMetadataType::CHROMA_SITING>(mMapper, bufferHandle);
713 if (value.has_value()) {
714 *outChromaSiting = *value;
715 return OK;
716 }
717 return UNKNOWN_TRANSACTION;
718}
719
720status_t Gralloc5Mapper::getChromaSiting(buffer_handle_t bufferHandle,
721 ui::ChromaSiting *outChromaSiting) const {
722 if (!outChromaSiting) {
723 return BAD_VALUE;
724 }
725 ExtendableType chromaSiting;
726 status_t error = getChromaSiting(bufferHandle, &chromaSiting);
727 if (error) {
728 return error;
729 }
730 if (!gralloc4::isStandardChromaSiting(chromaSiting)) {
731 return BAD_TYPE;
732 }
733 *outChromaSiting = gralloc4::getStandardChromaSitingValue(chromaSiting);
734 return NO_ERROR;
735}
736
737status_t Gralloc5Mapper::getPlaneLayouts(buffer_handle_t bufferHandle,
738 std::vector<ui::PlaneLayout> *outPlaneLayouts) const {
739 auto value = getStandardMetadata<StandardMetadataType::PLANE_LAYOUTS>(mMapper, bufferHandle);
740 if (value.has_value()) {
741 *outPlaneLayouts = *value;
742 return OK;
743 }
744 return UNKNOWN_TRANSACTION;
745}
746
747status_t Gralloc5Mapper::getDataspace(buffer_handle_t bufferHandle,
748 ui::Dataspace *outDataspace) const {
749 auto value = getStandardMetadata<StandardMetadataType::DATASPACE>(mMapper, bufferHandle);
750 if (value.has_value()) {
751 *outDataspace = static_cast<ui::Dataspace>(*value);
752 return OK;
753 }
754 return UNKNOWN_TRANSACTION;
755}
756
757status_t Gralloc5Mapper::setDataspace(buffer_handle_t bufferHandle, ui::Dataspace dataspace) const {
758 return setStandardMetadata<StandardMetadataType::DATASPACE>(mMapper, bufferHandle,
759 static_cast<Dataspace>(dataspace));
760}
761
762status_t Gralloc5Mapper::getBlendMode(buffer_handle_t bufferHandle,
763 ui::BlendMode *outBlendMode) const {
764 auto value = getStandardMetadata<StandardMetadataType::BLEND_MODE>(mMapper, bufferHandle);
765 if (value.has_value()) {
766 *outBlendMode = static_cast<ui::BlendMode>(*value);
767 return OK;
768 }
769 return UNKNOWN_TRANSACTION;
770}
771
772status_t Gralloc5Mapper::getSmpte2086(buffer_handle_t bufferHandle,
773 std::optional<ui::Smpte2086> *outSmpte2086) const {
774 auto value = getStandardMetadata<StandardMetadataType::SMPTE2086>(mMapper, bufferHandle);
775 if (value.has_value()) {
776 *outSmpte2086 = *value;
777 return OK;
778 }
779 return UNKNOWN_TRANSACTION;
780}
781
782status_t Gralloc5Mapper::setSmpte2086(buffer_handle_t bufferHandle,
783 std::optional<ui::Smpte2086> smpte2086) const {
784 return setStandardMetadata<StandardMetadataType::SMPTE2086>(mMapper, bufferHandle, smpte2086);
785}
786
787status_t Gralloc5Mapper::getCta861_3(buffer_handle_t bufferHandle,
788 std::optional<ui::Cta861_3> *outCta861_3) const {
789 auto value = getStandardMetadata<StandardMetadataType::CTA861_3>(mMapper, bufferHandle);
790 if (value.has_value()) {
791 *outCta861_3 = *value;
792 return OK;
793 }
794 return UNKNOWN_TRANSACTION;
795}
796
797status_t Gralloc5Mapper::setCta861_3(buffer_handle_t bufferHandle,
798 std::optional<ui::Cta861_3> cta861_3) const {
799 return setStandardMetadata<StandardMetadataType::CTA861_3>(mMapper, bufferHandle, cta861_3);
800}
801
802status_t Gralloc5Mapper::getSmpte2094_40(
803 buffer_handle_t bufferHandle, std::optional<std::vector<uint8_t>> *outSmpte2094_40) const {
804 auto value = getStandardMetadata<StandardMetadataType::SMPTE2094_40>(mMapper, bufferHandle);
805 if (value.has_value()) {
806 *outSmpte2094_40 = std::move(*value);
807 return OK;
808 }
809 return UNKNOWN_TRANSACTION;
810}
811
812status_t Gralloc5Mapper::setSmpte2094_40(buffer_handle_t bufferHandle,
813 std::optional<std::vector<uint8_t>> smpte2094_40) const {
814 return setStandardMetadata<StandardMetadataType::SMPTE2094_40>(mMapper, bufferHandle,
815 smpte2094_40);
816}
817
818status_t Gralloc5Mapper::getSmpte2094_10(
819 buffer_handle_t bufferHandle, std::optional<std::vector<uint8_t>> *outSmpte2094_10) const {
820 auto value = getStandardMetadata<StandardMetadataType::SMPTE2094_10>(mMapper, bufferHandle);
821 if (value.has_value()) {
822 *outSmpte2094_10 = std::move(*value);
823 return OK;
824 }
825 return UNKNOWN_TRANSACTION;
826}
827
828status_t Gralloc5Mapper::setSmpte2094_10(buffer_handle_t bufferHandle,
829 std::optional<std::vector<uint8_t>> smpte2094_10) const {
830 return setStandardMetadata<StandardMetadataType::SMPTE2094_10>(mMapper, bufferHandle,
831 smpte2094_10);
832}
833
834status_t Gralloc5Mapper::getDefaultPixelFormatFourCC(uint32_t, uint32_t, PixelFormat, uint32_t,
835 uint64_t, uint32_t *) const {
836 // TODO(b/261857910): Remove
837 return UNKNOWN_TRANSACTION;
838}
839
840status_t Gralloc5Mapper::getDefaultPixelFormatModifier(uint32_t, uint32_t, PixelFormat, uint32_t,
841 uint64_t, uint64_t *) const {
842 // TODO(b/261857910): Remove
843 return UNKNOWN_TRANSACTION;
844}
845
846status_t Gralloc5Mapper::getDefaultAllocationSize(uint32_t, uint32_t, PixelFormat, uint32_t,
847 uint64_t, uint64_t *) const {
848 // TODO(b/261857910): Remove
849 return UNKNOWN_TRANSACTION;
850}
851
852status_t Gralloc5Mapper::getDefaultProtectedContent(uint32_t, uint32_t, PixelFormat, uint32_t,
853 uint64_t, uint64_t *) const {
854 // TODO(b/261857910): Remove
855 return UNKNOWN_TRANSACTION;
856}
857
858status_t Gralloc5Mapper::getDefaultCompression(
859 uint32_t, uint32_t, PixelFormat, uint32_t, uint64_t,
860 aidl::android::hardware::graphics::common::ExtendableType *) const {
861 // TODO(b/261857910): Remove
862 return UNKNOWN_TRANSACTION;
863}
864
865status_t Gralloc5Mapper::getDefaultCompression(uint32_t, uint32_t, PixelFormat, uint32_t, uint64_t,
866 ui::Compression *) const {
867 // TODO(b/261857910): Remove
868 return UNKNOWN_TRANSACTION;
869}
870
871status_t Gralloc5Mapper::getDefaultInterlaced(
872 uint32_t, uint32_t, PixelFormat, uint32_t, uint64_t,
873 aidl::android::hardware::graphics::common::ExtendableType *) const {
874 // TODO(b/261857910): Remove
875 return UNKNOWN_TRANSACTION;
876}
877
878status_t Gralloc5Mapper::getDefaultInterlaced(uint32_t, uint32_t, PixelFormat, uint32_t, uint64_t,
879 ui::Interlaced *) const {
880 // TODO(b/261857910): Remove
881 return UNKNOWN_TRANSACTION;
882}
883
884status_t Gralloc5Mapper::getDefaultChromaSiting(
885 uint32_t, uint32_t, PixelFormat, uint32_t, uint64_t,
886 aidl::android::hardware::graphics::common::ExtendableType *) const {
887 // TODO(b/261857910): Remove
888 return UNKNOWN_TRANSACTION;
889}
890
891status_t Gralloc5Mapper::getDefaultChromaSiting(uint32_t, uint32_t, PixelFormat, uint32_t, uint64_t,
892 ui::ChromaSiting *) const {
893 // TODO(b/261857910): Remove
894 return UNKNOWN_TRANSACTION;
895}
896
897status_t Gralloc5Mapper::getDefaultPlaneLayouts(uint32_t, uint32_t, PixelFormat, uint32_t, uint64_t,
898 std::vector<ui::PlaneLayout> *) const {
899 // TODO(b/261857910): Remove
900 return UNKNOWN_TRANSACTION;
901}
902
903} // namespace android