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