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