From bd616875b5d7eac5d5d5fdfd0f858f36bccb6e89 Mon Sep 17 00:00:00 2001 From: Jason Macnak Date: Fri, 19 Jan 2024 17:57:00 -0800 Subject: gralloc: Move buffer metadata initialization into common layer ... to dedup and support upcoming change to metadata initialization. Bug: b/321158178 Test: vts -m VtsHalGraphicsAllocatorAidl_TargetTest Test: vts -m VtsHalGraphicsMapperV4_0Target Test: vts -m VtsHalGraphicsMapperStableC_TargetTest Change-Id: Ib00873babcb2ec8816b0d319e675d6884594f618 --- Android.bp | 1 + cros_gralloc/aidl/Allocator.cpp | 191 ++++++++++--------------- cros_gralloc/aidl/Allocator.h | 21 ++- cros_gralloc/cros_gralloc_buffer.cc | 28 ++++ cros_gralloc/cros_gralloc_buffer.h | 2 + cros_gralloc/cros_gralloc_buffer_metadata.h | 37 +++++ cros_gralloc/cros_gralloc_driver.cc | 17 ++- cros_gralloc/cros_gralloc_helpers.h | 12 +- cros_gralloc/gralloc0/gralloc0.cc | 2 +- cros_gralloc/gralloc4/CrosGralloc4Allocator.cc | 43 +----- cros_gralloc/gralloc4/CrosGralloc4Allocator.h | 5 - cros_gralloc/gralloc4/CrosGralloc4Mapper.cc | 22 +-- cros_gralloc/gralloc4/CrosGralloc4Mapper.h | 9 +- cros_gralloc/gralloc4/CrosGralloc4Metadata.h | 37 ----- cros_gralloc/gralloc4/CrosGralloc4Utils.cc | 3 +- cros_gralloc/mapper_stablec/Mapper.cpp | 36 ++--- 16 files changed, 214 insertions(+), 252 deletions(-) create mode 100644 cros_gralloc/cros_gralloc_buffer_metadata.h delete mode 100644 cros_gralloc/gralloc4/CrosGralloc4Metadata.h diff --git a/Android.bp b/Android.bp index 654af84..baeffc2 100644 --- a/Android.bp +++ b/Android.bp @@ -115,6 +115,7 @@ cc_defaults { "libcutils", "libdmabufheap", "libdrm", + "libgralloctypes", "libnativewindow", "libsync", "liblog", diff --git a/cros_gralloc/aidl/Allocator.cpp b/cros_gralloc/aidl/Allocator.cpp index 89c439e..186ed40 100644 --- a/cros_gralloc/aidl/Allocator.cpp +++ b/cros_gralloc/aidl/Allocator.cpp @@ -13,6 +13,7 @@ #include #include +#include "cros_gralloc/cros_gralloc_buffer_metadata.h" #include "cros_gralloc/gralloc4/CrosGralloc4Utils.h" using aidl::android::hardware::common::NativeHandle; @@ -28,44 +29,36 @@ inline ndk::ScopedAStatus ToBinderStatus(AllocationError error) { return ndk::ScopedAStatus::fromServiceSpecificError(static_cast(error)); } -} // namespace - -bool Allocator::init() { - mDriver = cros_gralloc_driver::get_instance(); - return mDriver != nullptr; -} - -// TODO(natsu): deduplicate with CrosGralloc4Allocator after the T release. -ndk::ScopedAStatus Allocator::initializeMetadata( - cros_gralloc_handle_t crosHandle, - const struct cros_gralloc_buffer_descriptor& crosDescriptor, - Dataspace initialDataspace) { - if (!mDriver) { - ALOGE("Failed to initializeMetadata. Driver is uninitialized.\n"); - return ToBinderStatus(AllocationError::NO_RESOURCES); - } - - if (!crosHandle) { - ALOGE("Failed to initializeMetadata. Invalid handle.\n"); - return ToBinderStatus(AllocationError::NO_RESOURCES); +ndk::ScopedAStatus convertToCrosDescriptor(const BufferDescriptorInfo& info, + struct cros_gralloc_buffer_descriptor& crosDescriptor) { + const BufferDescriptorInfoV4 mapperV4Descriptor = { + .name{reinterpret_cast(info.name.data())}, + .width = static_cast(info.width), + .height = static_cast(info.height), + .layerCount = static_cast(info.layerCount), + .format = static_cast<::android::hardware::graphics::common::V1_2::PixelFormat>(info.format), + .usage = static_cast(info.usage), + .reservedSize = 0, + }; + if (convertToCrosDescriptor(mapperV4Descriptor, &crosDescriptor)) { + return ToBinderStatus(AllocationError::UNSUPPORTED); } - void* addr; - uint64_t size; - int ret = mDriver->get_reserved_region(crosHandle, &addr, &size); - if (ret) { - ALOGE("Failed to getReservedRegion.\n"); - return ToBinderStatus(AllocationError::NO_RESOURCES); + for (const auto& option : info.additionalOptions) { + if (option.name != STANDARD_METADATA_DATASPACE) { + return ToBinderStatus(AllocationError::UNSUPPORTED); + } + crosDescriptor.dataspace = static_cast(option.value); } - CrosGralloc4Metadata* crosMetadata = reinterpret_cast(addr); + return ndk::ScopedAStatus::ok(); +} - snprintf(crosMetadata->name, CROS_GRALLOC4_METADATA_MAX_NAME_SIZE, "%s", - crosDescriptor.name.c_str()); - crosMetadata->dataspace = initialDataspace; - crosMetadata->blendMode = common::BlendMode::INVALID; +} // namespace - return ndk::ScopedAStatus::ok(); +bool Allocator::init() { + mDriver = cros_gralloc_driver::get_instance(); + return mDriver != nullptr; } void Allocator::releaseBufferAndHandle(native_handle_t* handle) { @@ -74,26 +67,58 @@ void Allocator::releaseBufferAndHandle(native_handle_t* handle) { native_handle_delete(handle); } -ndk::ScopedAStatus Allocator::allocate(const std::vector& descriptor, int32_t count, +ndk::ScopedAStatus Allocator::allocate(const std::vector& encodedDescriptor, int32_t count, allocator::AllocationResult* outResult) { if (!mDriver) { ALOGE("Failed to allocate. Driver is uninitialized.\n"); return ToBinderStatus(AllocationError::NO_RESOURCES); } - BufferDescriptorInfoV4 description; + BufferDescriptorInfoV4 mapperV4Descriptor; - int ret = ::android::gralloc4::decodeBufferDescriptorInfo(descriptor, &description); + int ret = ::android::gralloc4::decodeBufferDescriptorInfo(encodedDescriptor, &mapperV4Descriptor); if (ret) { ALOGE("Failed to allocate. Failed to decode buffer descriptor: %d.\n", ret); return ToBinderStatus(AllocationError::BAD_DESCRIPTOR); } + struct cros_gralloc_buffer_descriptor crosDescriptor = {}; + if (convertToCrosDescriptor(mapperV4Descriptor, &crosDescriptor)) { + return ToBinderStatus(AllocationError::UNSUPPORTED); + } + + return allocate(crosDescriptor, count, outResult); +} + +ndk::ScopedAStatus Allocator::allocate2(const BufferDescriptorInfo& descriptor, int32_t count, + allocator::AllocationResult* outResult) { + if (!mDriver) { + ALOGE("Failed to allocate. Driver is uninitialized.\n"); + return ToBinderStatus(AllocationError::NO_RESOURCES); + } + + struct cros_gralloc_buffer_descriptor crosDescriptor = {}; + + ndk::ScopedAStatus status = convertToCrosDescriptor(descriptor, crosDescriptor); + if (!status.isOk()) { + return status; + } + + return allocate(crosDescriptor, count, outResult); +} + +ndk::ScopedAStatus Allocator::allocate(const struct cros_gralloc_buffer_descriptor& descriptor, int32_t count, + allocator::AllocationResult* outResult) { + if (!mDriver) { + ALOGE("Failed to allocate. Driver is uninitialized.\n"); + return ToBinderStatus(AllocationError::NO_RESOURCES); + } + std::vector handles; handles.resize(count, nullptr); for (int32_t i = 0; i < count; i++) { - ndk::ScopedAStatus status = allocate(description, &outResult->stride, &handles[i]); + ndk::ScopedAStatus status = allocateBuffer(descriptor, &outResult->stride, &handles[i]); if (!status.isOk()) { for (int32_t j = 0; j < i; j++) { releaseBufferAndHandle(handles[j]); @@ -112,24 +137,21 @@ ndk::ScopedAStatus Allocator::allocate(const std::vector& descriptor, i return ndk::ScopedAStatus::ok(); } -ndk::ScopedAStatus Allocator::allocate(const BufferDescriptorInfoV4& descriptor, int32_t* outStride, - native_handle_t** outHandle, Dataspace initialDataspace) { +ndk::ScopedAStatus Allocator::allocateBuffer(const struct cros_gralloc_buffer_descriptor& descriptor, int32_t* outStride, + native_handle_t** outHandle) { if (!mDriver) { ALOGE("Failed to allocate. Driver is uninitialized.\n"); return ToBinderStatus(AllocationError::NO_RESOURCES); } - struct cros_gralloc_buffer_descriptor crosDescriptor; - if (convertToCrosDescriptor(descriptor, &crosDescriptor)) { - return ToBinderStatus(AllocationError::UNSUPPORTED); - } - - crosDescriptor.reserved_region_size += sizeof(CrosGralloc4Metadata); - - if (!mDriver->is_supported(&crosDescriptor)) { - const std::string drmFormatString = get_drm_format_string(crosDescriptor.drm_format); - const std::string pixelFormatString = getPixelFormatString(descriptor.format); - const std::string usageString = getUsageString(descriptor.usage); + if (!mDriver->is_supported(&descriptor)) { + const std::string drmFormatString = + get_drm_format_string(descriptor.drm_format); + const std::string pixelFormatString = ::android::hardware::graphics::common::V1_2::toString( + static_cast<::android::hardware::graphics::common::V1_2::PixelFormat>( + descriptor.droid_format)); + const std::string usageString = ::android::hardware::graphics::common::V1_2::toString<::android::hardware::graphics::common::V1_2::BufferUsage>( + static_cast(descriptor.droid_usage)); ALOGE("Failed to allocate. Unsupported combination: pixel format:%s, drm format:%s, " "usage:%s\n", pixelFormatString.c_str(), drmFormatString.c_str(), usageString.c_str()); @@ -137,80 +159,18 @@ ndk::ScopedAStatus Allocator::allocate(const BufferDescriptorInfoV4& descriptor, } native_handle_t* handle; - int ret = mDriver->allocate(&crosDescriptor, &handle); + int ret = mDriver->allocate(&descriptor, &handle); if (ret) { return ToBinderStatus(AllocationError::NO_RESOURCES); } cros_gralloc_handle_t crosHandle = cros_gralloc_convert_handle(handle); - - auto status = initializeMetadata(crosHandle, crosDescriptor, initialDataspace); - if (!status.isOk()) { - ALOGE("Failed to allocate. Failed to initialize gralloc buffer metadata."); - releaseBufferAndHandle(handle); - return status; - } - *outStride = static_cast(crosHandle->pixel_stride); *outHandle = handle; return ndk::ScopedAStatus::ok(); } -static BufferDescriptorInfoV4 convertAidlToIMapperV4Descriptor(const BufferDescriptorInfo& info) { - return BufferDescriptorInfoV4 { - .name{reinterpret_cast(info.name.data())}, - .width = static_cast(info.width), - .height = static_cast(info.height), - .layerCount = static_cast(info.layerCount), - .format = static_cast<::android::hardware::graphics::common::V1_2::PixelFormat>(info.format), - .usage = static_cast(info.usage), - .reservedSize = 0, - }; -} - -ndk::ScopedAStatus Allocator::allocate2(const BufferDescriptorInfo& descriptor, int32_t count, - allocator::AllocationResult* outResult) { - if (!mDriver) { - ALOGE("Failed to allocate. Driver is uninitialized.\n"); - return ToBinderStatus(AllocationError::NO_RESOURCES); - } - - Dataspace initialDataspace = Dataspace::UNKNOWN; - - for (const auto& option : descriptor.additionalOptions) { - if (option.name != STANDARD_METADATA_DATASPACE) { - return ToBinderStatus(AllocationError::UNSUPPORTED); - } - initialDataspace = static_cast(option.value); - } - - BufferDescriptorInfoV4 descriptionV4 = convertAidlToIMapperV4Descriptor(descriptor); - - std::vector handles; - handles.resize(count, nullptr); - - for (int32_t i = 0; i < count; i++) { - ndk::ScopedAStatus status = allocate(descriptionV4, &outResult->stride, &handles[i], - initialDataspace); - if (!status.isOk()) { - for (int32_t j = 0; j < i; j++) { - releaseBufferAndHandle(handles[j]); - } - return status; - } - } - - outResult->buffers.resize(count); - for (int32_t i = 0; i < count; i++) { - auto handle = handles[i]; - outResult->buffers[i] = ::android::dupToAidl(handle); - releaseBufferAndHandle(handle); - } - - return ndk::ScopedAStatus::ok(); -} - ndk::ScopedAStatus Allocator::isSupported(const BufferDescriptorInfo& descriptor, bool* outResult) { if (!mDriver) { @@ -225,16 +185,15 @@ ndk::ScopedAStatus Allocator::isSupported(const BufferDescriptorInfo& descriptor } } - struct cros_gralloc_buffer_descriptor crosDescriptor; - if (convertToCrosDescriptor(convertAidlToIMapperV4Descriptor(descriptor), &crosDescriptor)) { + struct cros_gralloc_buffer_descriptor crosDescriptor = {}; + ndk::ScopedAStatus status = convertToCrosDescriptor(descriptor, crosDescriptor); + if (!status.isOk()) { // Failing to convert the descriptor means the layer count, pixel format, or usage is // unsupported, thus isSupported() = false *outResult = false; return ndk::ScopedAStatus::ok(); } - crosDescriptor.reserved_region_size += sizeof(CrosGralloc4Metadata); - *outResult = mDriver->is_supported(&crosDescriptor); return ndk::ScopedAStatus::ok(); } diff --git a/cros_gralloc/aidl/Allocator.h b/cros_gralloc/aidl/Allocator.h index 85a13cd..3454aab 100644 --- a/cros_gralloc/aidl/Allocator.h +++ b/cros_gralloc/aidl/Allocator.h @@ -13,7 +13,6 @@ #include "cros_gralloc/cros_gralloc_driver.h" #include "cros_gralloc/cros_gralloc_helpers.h" -#include "cros_gralloc/gralloc4/CrosGralloc4Metadata.h" namespace aidl::android::hardware::graphics::allocator::impl { @@ -27,10 +26,10 @@ class Allocator : public BnAllocator { allocator::AllocationResult* outResult) override; ndk::ScopedAStatus allocate2(const BufferDescriptorInfo& descriptor, int32_t count, - allocator::AllocationResult* outResult) override; + allocator::AllocationResult* outResult) override; ndk::ScopedAStatus isSupported(const BufferDescriptorInfo& descriptor, - bool* outResult) override; + bool* outResult) override; ndk::ScopedAStatus getIMapperLibrarySuffix(std::string* outResult) override; @@ -39,16 +38,12 @@ class Allocator : public BnAllocator { private: using Dataspace = aidl::android::hardware::graphics::common::Dataspace; - ndk::ScopedAStatus allocate( - const ::android::hardware::graphics::mapper::V4_0::IMapper::BufferDescriptorInfo& - descriptor, - int32_t* outStride, native_handle_t** outHandle, - Dataspace initialDataspace = Dataspace::UNKNOWN); - - ndk::ScopedAStatus initializeMetadata( - cros_gralloc_handle_t crosHandle, - const struct cros_gralloc_buffer_descriptor& crosDescriptor, - Dataspace initialDataspace); + + ndk::ScopedAStatus allocate(const struct cros_gralloc_buffer_descriptor& descriptor, + int32_t count, allocator::AllocationResult* outResult); + + ndk::ScopedAStatus allocateBuffer(const struct cros_gralloc_buffer_descriptor& descriptor, + int32_t* outStride, native_handle_t** outHandle); void releaseBufferAndHandle(native_handle_t* handle); diff --git a/cros_gralloc/cros_gralloc_buffer.cc b/cros_gralloc/cros_gralloc_buffer.cc index dba8061..73fb267 100644 --- a/cros_gralloc/cros_gralloc_buffer.cc +++ b/cros_gralloc/cros_gralloc_buffer.cc @@ -11,6 +11,8 @@ #include +#include "cros_gralloc_buffer_metadata.h" + /*static*/ std::unique_ptr cros_gralloc_buffer::create(struct bo *acquire_bo, @@ -35,6 +37,32 @@ cros_gralloc_buffer::create(struct bo *acquire_bo, return buffer; } +int32_t +cros_gralloc_buffer::initialize_metadata(const struct cros_gralloc_buffer_descriptor *descriptor) +{ + void *metadata_addr; + uint64_t metadata_region_size; + int32_t ret = get_reserved_region(&metadata_addr, &metadata_region_size); + if (ret) { + ALOGE("Failed to initialize metadata: failed to get reserved region."); + return ret; + } + + if (metadata_addr == nullptr) { + ALOGE("Failed to initialize metadata: invalid metadata address."); + return -EINVAL; + } + + cros_gralloc_buffer_metadata *metadata = + reinterpret_cast(metadata_addr); + + snprintf(metadata->name, CROS_GRALLOC_BUFFER_METADATA_MAX_NAME_SIZE, "%s", + descriptor->name.c_str()); + metadata->dataspace = descriptor->dataspace; + metadata->blendMode = descriptor->blend; + return 0; +} + cros_gralloc_buffer::cros_gralloc_buffer(struct bo *acquire_bo, struct cros_gralloc_handle *acquire_handle) : bo_(acquire_bo), hnd_(acquire_handle) diff --git a/cros_gralloc/cros_gralloc_buffer.h b/cros_gralloc/cros_gralloc_buffer.h index b477faa..6ab1497 100644 --- a/cros_gralloc/cros_gralloc_buffer.h +++ b/cros_gralloc/cros_gralloc_buffer.h @@ -19,6 +19,8 @@ class cros_gralloc_buffer ~cros_gralloc_buffer(); + int32_t initialize_metadata(const struct cros_gralloc_buffer_descriptor *descriptor); + uint32_t get_id() const; uint32_t get_width() const; uint32_t get_pixel_stride() const; diff --git a/cros_gralloc/cros_gralloc_buffer_metadata.h b/cros_gralloc/cros_gralloc_buffer_metadata.h new file mode 100644 index 0000000..30877b1 --- /dev/null +++ b/cros_gralloc/cros_gralloc_buffer_metadata.h @@ -0,0 +1,37 @@ +/* + * Copyright 2022 The Chromium OS Authors. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#ifndef CROSGRALLOC4METADATA_H +#define CROSGRALLOC4METADATA_H + +#include +#include +#include +#include + +#define CROS_GRALLOC_BUFFER_METADATA_MAX_NAME_SIZE 1024 + +/* + * The metadata for cros_gralloc_buffer-s that should reside in a shared memory region + * instead of directly in cros_gralloc_handle-s. + * + * Any metadata that is mutable must be stored in this shared memory region as + * cros_gralloc_handle-s can not be tracked and updated across processes. + */ +struct cros_gralloc_buffer_metadata { + /* + * Name is stored in the shared memory metadata to simplify cros_gralloc_handle + * creation. This allows us to keep handles small while avoiding variable sized + * handles. + */ + char name[CROS_GRALLOC_BUFFER_METADATA_MAX_NAME_SIZE]; + aidl::android::hardware::graphics::common::BlendMode blendMode; + aidl::android::hardware::graphics::common::Dataspace dataspace; + std::optional cta861_3; + std::optional smpte2086; +}; + +#endif diff --git a/cros_gralloc/cros_gralloc_driver.cc b/cros_gralloc/cros_gralloc_driver.cc index 550f75a..bb54815 100644 --- a/cros_gralloc/cros_gralloc_driver.cc +++ b/cros_gralloc/cros_gralloc_driver.cc @@ -15,6 +15,7 @@ #include #include "../util.h" +#include "cros_gralloc_buffer_metadata.h" // Constants taken from pipe_loader_drm.c in Mesa @@ -267,7 +268,7 @@ int32_t cros_gralloc_driver::allocate(const struct cros_gralloc_buffer_descripto num_planes = drv_bo_get_num_planes(bo); num_fds = num_planes; - if (descriptor->reserved_region_size > 0) + if (descriptor->enable_metadata_fd) num_fds += 1; num_ints = ((sizeof(struct cros_gralloc_handle) - sizeof(native_handle_t)) / sizeof(int)) - @@ -291,7 +292,11 @@ int32_t cros_gralloc_driver::allocate(const struct cros_gralloc_buffer_descripto hnd->sizes[plane] = drv_bo_get_plane_size(bo, plane); } - hnd->reserved_region_size = descriptor->reserved_region_size; + hnd->reserved_region_size = 0; + if (descriptor->enable_metadata_fd) + hnd->reserved_region_size = + sizeof(struct cros_gralloc_buffer_metadata) + descriptor->client_metadata_size; + if (hnd->reserved_region_size > 0) { ret = create_reserved_region(descriptor->name, hnd->reserved_region_size); if (ret < 0) @@ -313,7 +318,7 @@ int32_t cros_gralloc_driver::allocate(const struct cros_gralloc_buffer_descripto hnd->magic = cros_gralloc_magic; hnd->droid_format = descriptor->droid_format; hnd->usage = descriptor->droid_usage; - hnd->total_size = descriptor->reserved_region_size + drv_bo_get_total_size(bo); + hnd->total_size = hnd->reserved_region_size + drv_bo_get_total_size(bo); buffer = cros_gralloc_buffer::create(bo, hnd); if (!buffer) { @@ -322,6 +327,12 @@ int32_t cros_gralloc_driver::allocate(const struct cros_gralloc_buffer_descripto goto destroy_hnd; } + ret = buffer->initialize_metadata(descriptor); + if (ret) { + ALOGE("Failed to allocate: failed to initialize cros_gralloc_buffer metadata."); + goto destroy_hnd; + } + { std::lock_guard lock(mutex_); diff --git a/cros_gralloc/cros_gralloc_helpers.h b/cros_gralloc/cros_gralloc_helpers.h index fe72165..501d9b1 100644 --- a/cros_gralloc/cros_gralloc_helpers.h +++ b/cros_gralloc/cros_gralloc_helpers.h @@ -10,6 +10,8 @@ #include "../drv.h" #include "cros_gralloc_handle.h" +#include +#include #include #include #include @@ -31,8 +33,16 @@ struct cros_gralloc_buffer_descriptor { int64_t droid_usage; uint32_t drm_format; uint64_t use_flags; - uint64_t reserved_region_size; + // If true, allocate an additional shared memory region for buffer metadata. + bool enable_metadata_fd = false; + // If the additional shared memory region for buffer metadata is present, the + // additional amount of space reserved for client use. + uint64_t client_metadata_size = 0; std::string name; + aidl::android::hardware::graphics::common::Dataspace dataspace = + aidl::android::hardware::graphics::common::Dataspace::UNKNOWN; + aidl::android::hardware::graphics::common::BlendMode blend = + aidl::android::hardware::graphics::common::BlendMode::INVALID; }; constexpr uint32_t cros_gralloc_magic = 0xABCDDCBA; diff --git a/cros_gralloc/gralloc0/gralloc0.cc b/cros_gralloc/gralloc0/gralloc0.cc index 5487128..b6f493d 100644 --- a/cros_gralloc/gralloc0/gralloc0.cc +++ b/cros_gralloc/gralloc0/gralloc0.cc @@ -81,7 +81,7 @@ static int gralloc0_alloc(alloc_device_t *dev, int w, int h, int format, int usa descriptor.droid_usage = usage; descriptor.drm_format = cros_gralloc_convert_format(format); descriptor.use_flags = cros_gralloc_convert_usage(usage); - descriptor.reserved_region_size = 0; + descriptor.enable_metadata_fd = false; if (!mod->driver->is_supported(&descriptor)) { ALOGE("Unsupported combination -- HAL format: %u, HAL usage: %u, " diff --git a/cros_gralloc/gralloc4/CrosGralloc4Allocator.cc b/cros_gralloc/gralloc4/CrosGralloc4Allocator.cc index 3166793..233d9b9 100644 --- a/cros_gralloc/gralloc4/CrosGralloc4Allocator.cc +++ b/cros_gralloc/gralloc4/CrosGralloc4Allocator.cc @@ -9,8 +9,8 @@ #include #include +#include "cros_gralloc/cros_gralloc_buffer_metadata.h" #include "cros_gralloc/cros_gralloc_helpers.h" -#include "cros_gralloc/gralloc4/CrosGralloc4Metadata.h" #include "cros_gralloc/gralloc4/CrosGralloc4Utils.h" using aidl::android::hardware::graphics::common::BlendMode; @@ -31,37 +31,6 @@ Error CrosGralloc4Allocator::init() { return mDriver ? Error::NONE : Error::NO_RESOURCES; } -Error CrosGralloc4Allocator::initializeMetadata( - cros_gralloc_handle_t crosHandle, - const struct cros_gralloc_buffer_descriptor& crosDescriptor) { - if (!mDriver) { - ALOGE("Failed to initializeMetadata. Driver is uninitialized."); - return Error::NO_RESOURCES; - } - - if (!crosHandle) { - ALOGE("Failed to initializeMetadata. Invalid handle."); - return Error::BAD_BUFFER; - } - - void* addr; - uint64_t size; - int ret = mDriver->get_reserved_region(crosHandle, &addr, &size); - if (ret) { - ALOGE("Failed to getReservedRegion."); - return Error::NO_RESOURCES; - } - - CrosGralloc4Metadata* crosMetadata = reinterpret_cast(addr); - - snprintf(crosMetadata->name, CROS_GRALLOC4_METADATA_MAX_NAME_SIZE, "%s", - crosDescriptor.name.c_str()); - crosMetadata->dataspace = Dataspace::UNKNOWN; - crosMetadata->blendMode = BlendMode::INVALID; - - return Error::NONE; -} - Error CrosGralloc4Allocator::allocate(const BufferDescriptorInfo& descriptor, uint32_t* outStride, hidl_handle* outHandle) { if (!mDriver) { @@ -78,8 +47,6 @@ Error CrosGralloc4Allocator::allocate(const BufferDescriptorInfo& descriptor, ui return Error::UNSUPPORTED; } - crosDescriptor.reserved_region_size += sizeof(CrosGralloc4Metadata); - if (!mDriver->is_supported(&crosDescriptor)) { std::string drmFormatString = get_drm_format_string(crosDescriptor.drm_format); std::string pixelFormatString = getPixelFormatString(descriptor.format); @@ -97,14 +64,6 @@ Error CrosGralloc4Allocator::allocate(const BufferDescriptorInfo& descriptor, ui cros_gralloc_handle_t crosHandle = cros_gralloc_convert_handle(handle); - Error error = initializeMetadata(crosHandle, crosDescriptor); - if (error != Error::NONE) { - mDriver->release(handle); - native_handle_close(handle); - native_handle_delete(handle); - return error; - } - outHandle->setTo(handle, /*shouldOwn=*/true); *outStride = crosHandle->pixel_stride; diff --git a/cros_gralloc/gralloc4/CrosGralloc4Allocator.h b/cros_gralloc/gralloc4/CrosGralloc4Allocator.h index db7294f..f2febba 100644 --- a/cros_gralloc/gralloc4/CrosGralloc4Allocator.h +++ b/cros_gralloc/gralloc4/CrosGralloc4Allocator.h @@ -11,7 +11,6 @@ #include "cros_gralloc/cros_gralloc_driver.h" #include "cros_gralloc/cros_gralloc_helpers.h" -#include "cros_gralloc/gralloc4/CrosGralloc4Metadata.h" class CrosGralloc4Allocator : public android::hardware::graphics::allocator::V4_0::IAllocator { public: @@ -23,10 +22,6 @@ class CrosGralloc4Allocator : public android::hardware::graphics::allocator::V4_ android::hardware::graphics::mapper::V4_0::Error init(); private: - android::hardware::graphics::mapper::V4_0::Error initializeMetadata( - cros_gralloc_handle_t crosHandle, - const struct cros_gralloc_buffer_descriptor& crosDescriptor); - android::hardware::graphics::mapper::V4_0::Error allocate( const android::hardware::graphics::mapper::V4_0::IMapper::BufferDescriptorInfo& description, diff --git a/cros_gralloc/gralloc4/CrosGralloc4Mapper.cc b/cros_gralloc/gralloc4/CrosGralloc4Mapper.cc index 23d6231..502776c 100644 --- a/cros_gralloc/gralloc4/CrosGralloc4Mapper.cc +++ b/cros_gralloc/gralloc4/CrosGralloc4Mapper.cc @@ -438,7 +438,7 @@ Return CrosGralloc4Mapper::get(const cros_gralloc_buffer* crosBuffer, return Void(); } - const CrosGralloc4Metadata* crosMetadata = nullptr; + const struct cros_gralloc_buffer_metadata* crosMetadata = nullptr; if (metadataType == android::gralloc4::MetadataType_BlendMode || metadataType == android::gralloc4::MetadataType_Cta861_3 || metadataType == android::gralloc4::MetadataType_Dataspace || @@ -608,7 +608,7 @@ Error CrosGralloc4Mapper::set(cros_gralloc_buffer* crosBuffer, const MetadataTyp return Error::BAD_BUFFER; } - CrosGralloc4Metadata* crosMetadata = nullptr; + struct cros_gralloc_buffer_metadata* crosMetadata = nullptr; Error error = getMutableMetadata(crosBuffer, &crosMetadata); if (error != Error::NONE) { @@ -1057,15 +1057,15 @@ Error CrosGralloc4Mapper::getReservedRegionArea(const cros_gralloc_buffer* crosB switch (area) { case ReservedRegionArea::MAPPER4_METADATA: { - // CrosGralloc4Metadata resides at the beginning reserved region. - *outSize = sizeof(CrosGralloc4Metadata); + // struct cros_gralloc_buffer_metadata resides at the beginning reserved region. + *outSize = sizeof(struct cros_gralloc_buffer_metadata); break; } case ReservedRegionArea::USER_METADATA: { - // User metadata resides after the CrosGralloc4Metadata. + // User metadata resides after the struct cros_gralloc_buffer_metadata. *outAddr = reinterpret_cast(reinterpret_cast(*outAddr) + - sizeof(CrosGralloc4Metadata)); - *outSize = *outSize - sizeof(CrosGralloc4Metadata); + sizeof(struct cros_gralloc_buffer_metadata)); + *outSize = *outSize - sizeof(struct cros_gralloc_buffer_metadata); break; } } @@ -1074,7 +1074,7 @@ Error CrosGralloc4Mapper::getReservedRegionArea(const cros_gralloc_buffer* crosB } Error CrosGralloc4Mapper::getMetadata(const cros_gralloc_buffer* crosBuffer, - const CrosGralloc4Metadata** outMetadata) { + const struct cros_gralloc_buffer_metadata** outMetadata) { void* addr = nullptr; uint64_t size; @@ -1084,12 +1084,12 @@ Error CrosGralloc4Mapper::getMetadata(const cros_gralloc_buffer* crosBuffer, return error; } - *outMetadata = reinterpret_cast(addr); + *outMetadata = reinterpret_cast(addr); return Error::NONE; } Error CrosGralloc4Mapper::getMutableMetadata(cros_gralloc_buffer* crosBuffer, - CrosGralloc4Metadata** outMetadata) { + struct cros_gralloc_buffer_metadata** outMetadata) { void* addr = nullptr; uint64_t size; @@ -1099,7 +1099,7 @@ Error CrosGralloc4Mapper::getMutableMetadata(cros_gralloc_buffer* crosBuffer, return error; } - *outMetadata = reinterpret_cast(addr); + *outMetadata = reinterpret_cast(addr); return Error::NONE; } diff --git a/cros_gralloc/gralloc4/CrosGralloc4Mapper.h b/cros_gralloc/gralloc4/CrosGralloc4Mapper.h index e4422e2..203c4c6 100644 --- a/cros_gralloc/gralloc4/CrosGralloc4Mapper.h +++ b/cros_gralloc/gralloc4/CrosGralloc4Mapper.h @@ -8,9 +8,9 @@ #include +#include "cros_gralloc/cros_gralloc_buffer_metadata.h" #include "cros_gralloc/cros_gralloc_driver.h" #include "cros_gralloc/cros_gralloc_handle.h" -#include "cros_gralloc/gralloc4/CrosGralloc4Metadata.h" class CrosGralloc4Mapper : public android::hardware::graphics::mapper::V4_0::IMapper { public: @@ -69,7 +69,7 @@ class CrosGralloc4Mapper : public android::hardware::graphics::mapper::V4_0::IMa private: enum class ReservedRegionArea { - /* CrosGralloc4Metadata */ + /* struct cros_gralloc_buffer_metadata */ MAPPER4_METADATA, /* External user metadata */ @@ -81,10 +81,11 @@ class CrosGralloc4Mapper : public android::hardware::graphics::mapper::V4_0::IMa uint64_t* outSize); android::hardware::graphics::mapper::V4_0::Error getMetadata( - const cros_gralloc_buffer* crosBuffer, const CrosGralloc4Metadata** outMetadata); + const cros_gralloc_buffer* crosBuffer, + const struct cros_gralloc_buffer_metadata** outMetadata); android::hardware::graphics::mapper::V4_0::Error getMutableMetadata( - cros_gralloc_buffer* crosBuffer, CrosGralloc4Metadata** outMetadata); + cros_gralloc_buffer* crosBuffer, struct cros_gralloc_buffer_metadata** outMetadata); android::hardware::Return get(const cros_gralloc_buffer* crosBuffer, const MetadataType& metadataType, get_cb hidlCb); diff --git a/cros_gralloc/gralloc4/CrosGralloc4Metadata.h b/cros_gralloc/gralloc4/CrosGralloc4Metadata.h deleted file mode 100644 index c908e35..0000000 --- a/cros_gralloc/gralloc4/CrosGralloc4Metadata.h +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright 2022 The Chromium OS Authors. All rights reserved. - * Use of this source code is governed by a BSD-style license that can be - * found in the LICENSE file. - */ - -#ifndef CROSGRALLOC4METADATA_H -#define CROSGRALLOC4METADATA_H - -#include -#include -#include -#include - -#define CROS_GRALLOC4_METADATA_MAX_NAME_SIZE 1024 - -/* - * The metadata for cros_gralloc_buffer-s that should reside in a shared memory region - * instead of directly in cros_gralloc_handle-s. - * - * Any metadata that is mutable must be stored in this shared memory region as - * cros_gralloc_handle-s can not be tracked and updated across processes. - */ -struct CrosGralloc4Metadata { - /* - * Name is stored in the shared memory metadata to simplify cros_gralloc_handle - * creation. This allows us to keep handles small while avoiding variable sized - * handles. - */ - char name[CROS_GRALLOC4_METADATA_MAX_NAME_SIZE]; - aidl::android::hardware::graphics::common::BlendMode blendMode; - aidl::android::hardware::graphics::common::Dataspace dataspace; - std::optional cta861_3; - std::optional smpte2086; -}; - -#endif diff --git a/cros_gralloc/gralloc4/CrosGralloc4Utils.cc b/cros_gralloc/gralloc4/CrosGralloc4Utils.cc index 25b42b5..703e50f 100644 --- a/cros_gralloc/gralloc4/CrosGralloc4Utils.cc +++ b/cros_gralloc/gralloc4/CrosGralloc4Utils.cc @@ -62,7 +62,8 @@ int convertToCrosDescriptor(const BufferDescriptorInfo& descriptor, outCrosDescriptor->height = descriptor.height; outCrosDescriptor->droid_format = static_cast(descriptor.format); outCrosDescriptor->droid_usage = descriptor.usage; - outCrosDescriptor->reserved_region_size = descriptor.reservedSize; + outCrosDescriptor->enable_metadata_fd = true; + outCrosDescriptor->client_metadata_size = descriptor.reservedSize; if (descriptor.layerCount > 1) { ALOGE("Failed to convert descriptor. Unsupported layerCount: %d", descriptor.layerCount); return -EINVAL; diff --git a/cros_gralloc/mapper_stablec/Mapper.cpp b/cros_gralloc/mapper_stablec/Mapper.cpp index c28d7f9..bc40002 100644 --- a/cros_gralloc/mapper_stablec/Mapper.cpp +++ b/cros_gralloc/mapper_stablec/Mapper.cpp @@ -17,9 +17,9 @@ #include +#include "cros_gralloc/cros_gralloc_buffer_metadata.h" #include "cros_gralloc/cros_gralloc_driver.h" #include "cros_gralloc/cros_gralloc_handle.h" -#include "cros_gralloc/gralloc4/CrosGralloc4Metadata.h" #include "cros_gralloc/gralloc4/CrosGralloc4Utils.h" using namespace ::aidl::android::hardware::graphics::common; @@ -43,7 +43,7 @@ using ::android::base::unique_fd; REQUIRE_DRIVER() \ VALIDATE_BUFFER_HANDLE(bufferHandle) -static_assert(CROS_GRALLOC4_METADATA_MAX_NAME_SIZE >= +static_assert(CROS_GRALLOC_BUFFER_METADATA_MAX_NAME_SIZE >= decltype(std::declval().name){}.size(), "Metadata name storage too small to fit a BufferDescriptorInfo::name"); @@ -110,7 +110,7 @@ class CrosGrallocMapperV5 final : public vendor::mapper::IMapperV5Impl { private: enum class ReservedRegionArea { - /* CrosGralloc4Metadata */ + /* struct cros_gralloc_buffer_metadata */ MAPPER4_METADATA, /* External user metadata */ @@ -122,17 +122,17 @@ class CrosGrallocMapperV5 final : public vendor::mapper::IMapperV5Impl { uint64_t* outSize); AIMapper_Error getCrosMetadata(const cros_gralloc_buffer* crosBuffer, - const CrosGralloc4Metadata** outMetadata); + const struct cros_gralloc_buffer_metadata** outMetadata); AIMapper_Error getMutableCrosMetadata(cros_gralloc_buffer* crosBuffer, - CrosGralloc4Metadata** outMetadata); + struct cros_gralloc_buffer_metadata** outMetadata); template int32_t getStandardMetadata(const cros_gralloc_buffer* crosBuffer, F&& provide, StandardMetadata); template - AIMapper_Error setStandardMetadata(CrosGralloc4Metadata* crosMetadata, + AIMapper_Error setStandardMetadata(struct cros_gralloc_buffer_metadata* crosMetadata, typename StandardMetadata::value_type&& value); void dumpBuffer( @@ -329,7 +329,7 @@ int32_t CrosGrallocMapperV5::getStandardMetadata(buffer_handle_t _Nonnull buffer template int32_t CrosGrallocMapperV5::getStandardMetadata(const cros_gralloc_buffer* crosBuffer, F&& provide, StandardMetadata) { - const CrosGralloc4Metadata* crosMetadata = nullptr; + const struct cros_gralloc_buffer_metadata* crosMetadata = nullptr; if constexpr (metadataType == StandardMetadataType::BLEND_MODE || metadataType == StandardMetadataType::CTA861_3 || metadataType == StandardMetadataType::DATASPACE || @@ -488,7 +488,7 @@ AIMapper_Error CrosGrallocMapperV5::setStandardMetadata(buffer_handle_t _Nonnull AIMapper_Error status = AIMAPPER_ERROR_UNSUPPORTED; mDriver->with_buffer(crosHandle, [&](cros_gralloc_buffer* crosBuffer) { - CrosGralloc4Metadata* crosMetadata = nullptr; + struct cros_gralloc_buffer_metadata* crosMetadata = nullptr; status = getMutableCrosMetadata(crosBuffer, &crosMetadata); if (status != AIMAPPER_ERROR_NONE) { return; @@ -505,7 +505,7 @@ AIMapper_Error CrosGrallocMapperV5::setStandardMetadata(buffer_handle_t _Nonnull template AIMapper_Error CrosGrallocMapperV5::setStandardMetadata( - CrosGralloc4Metadata* crosMetadata, typename StandardMetadata::value_type&& value) { + struct cros_gralloc_buffer_metadata* crosMetadata, typename StandardMetadata::value_type&& value) { if constexpr (TYPE == StandardMetadataType::BLEND_MODE) { crosMetadata->blendMode = value; } @@ -693,15 +693,15 @@ AIMapper_Error CrosGrallocMapperV5::getReservedRegionArea(const cros_gralloc_buf switch (area) { case ReservedRegionArea::MAPPER4_METADATA: { - // CrosGralloc4Metadata resides at the beginning reserved region. - *outSize = sizeof(CrosGralloc4Metadata); + // struct cros_gralloc_buffer_metadata resides at the beginning reserved region. + *outSize = sizeof(struct cros_gralloc_buffer_metadata); break; } case ReservedRegionArea::USER_METADATA: { - // User metadata resides after the CrosGralloc4Metadata. + // User metadata resides after the struct cros_gralloc_buffer_metadata. *outAddr = reinterpret_cast(reinterpret_cast(*outAddr) + - sizeof(CrosGralloc4Metadata)); - *outSize = *outSize - sizeof(CrosGralloc4Metadata); + sizeof(struct cros_gralloc_buffer_metadata)); + *outSize = *outSize - sizeof(struct cros_gralloc_buffer_metadata); break; } } @@ -710,7 +710,7 @@ AIMapper_Error CrosGrallocMapperV5::getReservedRegionArea(const cros_gralloc_buf } AIMapper_Error CrosGrallocMapperV5::getCrosMetadata(const cros_gralloc_buffer* crosBuffer, - const CrosGralloc4Metadata** outMetadata) { + const struct cros_gralloc_buffer_metadata** outMetadata) { void* addr = nullptr; uint64_t size; @@ -720,12 +720,12 @@ AIMapper_Error CrosGrallocMapperV5::getCrosMetadata(const cros_gralloc_buffer* c return error; } - *outMetadata = reinterpret_cast(addr); + *outMetadata = reinterpret_cast(addr); return AIMAPPER_ERROR_NONE; } AIMapper_Error CrosGrallocMapperV5::getMutableCrosMetadata(cros_gralloc_buffer* crosBuffer, - CrosGralloc4Metadata** outMetadata) { + struct cros_gralloc_buffer_metadata** outMetadata) { void* addr = nullptr; uint64_t size; @@ -735,7 +735,7 @@ AIMapper_Error CrosGrallocMapperV5::getMutableCrosMetadata(cros_gralloc_buffer* return error; } - *outMetadata = reinterpret_cast(addr); + *outMetadata = reinterpret_cast(addr); return AIMAPPER_ERROR_NONE; } -- cgit v1.2.3 From 92eb16bb24442955a5f99ce48f25b52ae5512e43 Mon Sep 17 00:00:00 2001 From: Jason Macnak Date: Mon, 22 Jan 2024 14:39:55 -0800 Subject: gralloc: Move buffer metadata accessing into cros_gralloc_buffer ... to hide cros_gralloc_buffer_metadata from the AIDL/HIDL Apis. No functional change expected. Bug: b/321158178 Test: vts -m VtsHalGraphicsAllocatorAidl_TargetTest Test: vts -m VtsHalGraphicsMapperV4_0Target Test: vts -m VtsHalGraphicsMapperStableC_TargetTest Change-Id: I33f17670a331385b0afe0e179cab8985b7fe78b9 --- cros_gralloc/aidl/Allocator.cpp | 1 - cros_gralloc/cros_gralloc_buffer.cc | 199 +++++++++++++++++++++++-- cros_gralloc/cros_gralloc_buffer.h | 36 ++++- cros_gralloc/cros_gralloc_buffer_metadata.h | 4 +- cros_gralloc/cros_gralloc_driver.cc | 21 --- cros_gralloc/cros_gralloc_driver.h | 3 - cros_gralloc/cros_gralloc_helpers.h | 2 + cros_gralloc/gralloc4/CrosGralloc4Allocator.cc | 1 - cros_gralloc/gralloc4/CrosGralloc4Mapper.cc | 185 ++++++++++------------- cros_gralloc/gralloc4/CrosGralloc4Mapper.h | 20 --- cros_gralloc/mapper_stablec/Mapper.cpp | 163 +++++++------------- 11 files changed, 359 insertions(+), 276 deletions(-) diff --git a/cros_gralloc/aidl/Allocator.cpp b/cros_gralloc/aidl/Allocator.cpp index 186ed40..199c7f1 100644 --- a/cros_gralloc/aidl/Allocator.cpp +++ b/cros_gralloc/aidl/Allocator.cpp @@ -13,7 +13,6 @@ #include #include -#include "cros_gralloc/cros_gralloc_buffer_metadata.h" #include "cros_gralloc/gralloc4/CrosGralloc4Utils.h" using aidl::android::hardware::common::NativeHandle; diff --git a/cros_gralloc/cros_gralloc_buffer.cc b/cros_gralloc/cros_gralloc_buffer.cc index 73fb267..dfa121b 100644 --- a/cros_gralloc/cros_gralloc_buffer.cc +++ b/cros_gralloc/cros_gralloc_buffer.cc @@ -13,6 +13,11 @@ #include "cros_gralloc_buffer_metadata.h" +using aidl::android::hardware::graphics::common::BlendMode; +using aidl::android::hardware::graphics::common::Cta861_3; +using aidl::android::hardware::graphics::common::Dataspace; +using aidl::android::hardware::graphics::common::Smpte2086; + /*static*/ std::unique_ptr cros_gralloc_buffer::create(struct bo *acquire_bo, @@ -40,26 +45,23 @@ cros_gralloc_buffer::create(struct bo *acquire_bo, int32_t cros_gralloc_buffer::initialize_metadata(const struct cros_gralloc_buffer_descriptor *descriptor) { - void *metadata_addr; - uint64_t metadata_region_size; - int32_t ret = get_reserved_region(&metadata_addr, &metadata_region_size); + struct cros_gralloc_buffer_metadata *metadata; + + int ret = get_metadata(&metadata); if (ret) { - ALOGE("Failed to initialize metadata: failed to get reserved region."); + ALOGE("Failed to initialize metadata: failed to get metadata region."); return ret; } - if (metadata_addr == nullptr) { + if (metadata == nullptr) { ALOGE("Failed to initialize metadata: invalid metadata address."); - return -EINVAL; + return -1; } - cros_gralloc_buffer_metadata *metadata = - reinterpret_cast(metadata_addr); - snprintf(metadata->name, CROS_GRALLOC_BUFFER_METADATA_MAX_NAME_SIZE, "%s", descriptor->name.c_str()); metadata->dataspace = descriptor->dataspace; - metadata->blendMode = descriptor->blend; + metadata->blend_mode = descriptor->blend; return 0; } @@ -148,6 +150,132 @@ int64_t cros_gralloc_buffer::get_android_usage() const return hnd_->usage; } +int32_t cros_gralloc_buffer::get_name(std::optional *name) const +{ + const struct cros_gralloc_buffer_metadata *metadata; + + int ret = get_metadata(&metadata); + if (ret) { + ALOGE("Failed to get_name: failed to get metadata."); + return ret; + } + + *name = metadata->name; + return 0; +} + +int32_t cros_gralloc_buffer::get_blend_mode(std::optional *blend_mode) const +{ + const struct cros_gralloc_buffer_metadata *metadata; + + int ret = get_metadata(&metadata); + if (ret) { + ALOGE("Failed to get_blend_mode: failed to get metadata."); + return ret; + } + + *blend_mode = metadata->blend_mode; + return 0; +} + +int32_t cros_gralloc_buffer::set_blend_mode(BlendMode blend_mode) +{ + struct cros_gralloc_buffer_metadata *metadata; + + int ret = get_metadata(&metadata); + if (ret) { + ALOGE("Failed to set_blend_mode: failed to get metadata."); + return ret; + } + + metadata->blend_mode = blend_mode; + return 0; +} + +int32_t cros_gralloc_buffer::get_dataspace(std::optional *dataspace) const +{ + const struct cros_gralloc_buffer_metadata *metadata; + + int ret = get_metadata(&metadata); + if (ret) { + ALOGE("Failed to get_dataspace: failed to get metadata."); + return ret; + } + + *dataspace = metadata->dataspace; + return 0; +} + +int32_t cros_gralloc_buffer::set_dataspace(Dataspace dataspace) +{ + struct cros_gralloc_buffer_metadata *metadata; + + int ret = get_metadata(&metadata); + if (ret) { + ALOGE("Failed to set_dataspace: failed to get metadata."); + return ret; + } + + metadata->dataspace = dataspace; + return 0; +} + +int32_t cros_gralloc_buffer::get_cta861_3(std::optional *cta) const +{ + const struct cros_gralloc_buffer_metadata *metadata; + + int ret = get_metadata(&metadata); + if (ret) { + ALOGE("Failed to get_cta861_3: failed to get metadata."); + return ret; + } + + *cta = metadata->cta861_3; + return 0; +} + +int32_t cros_gralloc_buffer::set_cta861_3(std::optional cta) +{ + struct cros_gralloc_buffer_metadata *metadata; + + int ret = get_metadata(&metadata); + if (ret) { + ALOGE("Failed to set_cta861_3: failed to get metadata."); + return ret; + } + + metadata->cta861_3 = cta; + return 0; +} + +int32_t cros_gralloc_buffer::get_smpte2086(std::optional *smpte) const +{ + const struct cros_gralloc_buffer_metadata *metadata; + + int ret = get_metadata(&metadata); + if (ret) { + ALOGE("Failed to get_smpte2086: failed to get metadata."); + return ret; + } + + *smpte = metadata->smpte2086; + return 0; +} + +int32_t cros_gralloc_buffer::set_smpte2086(std::optional smpte) +{ + struct cros_gralloc_buffer_metadata *metadata; + + int ret = get_metadata(&metadata); + if (ret) { + ALOGE("Failed to set_cta861_3: failed to get metadata."); + return ret; + } + + metadata->smpte2086 = smpte; + return 0; +} + int32_t cros_gralloc_buffer::increase_refcount() { return ++refcount_; @@ -270,3 +398,54 @@ int32_t cros_gralloc_buffer::get_reserved_region(void **addr, uint64_t *size) co *size = hnd_->reserved_region_size; return 0; } + +int32_t cros_gralloc_buffer::get_client_reserved_region(void **client_reserved_region_addr, + uint64_t *client_reserved_region_size) const +{ + int32_t ret = get_reserved_region(client_reserved_region_addr, client_reserved_region_size); + if (ret) { + return ret; + } + + *client_reserved_region_addr = + reinterpret_cast(reinterpret_cast(*client_reserved_region_addr) + + sizeof(struct cros_gralloc_buffer_metadata)); + *client_reserved_region_size = + *client_reserved_region_size - sizeof(struct cros_gralloc_buffer_metadata); + return 0; +} + +int32_t cros_gralloc_buffer::get_metadata(struct cros_gralloc_buffer_metadata **metadata) +{ + void *metadata_addr; + uint64_t metadata_region_size; + int32_t ret = get_reserved_region(&metadata_addr, &metadata_region_size); + if (ret) { + return ret; + } + + if (metadata_addr == nullptr) { + return -1; + } + + *metadata = reinterpret_cast(metadata_addr); + return 0; +} + +int32_t +cros_gralloc_buffer::get_metadata(const struct cros_gralloc_buffer_metadata **metadata) const +{ + void *metadata_addr; + uint64_t metadata_region_size; + int32_t ret = get_reserved_region(&metadata_addr, &metadata_region_size); + if (ret) { + return ret; + } + + if (metadata_addr == nullptr) { + return -1; + } + + *metadata = reinterpret_cast(metadata_addr); + return 0; +} diff --git a/cros_gralloc/cros_gralloc_buffer.h b/cros_gralloc/cros_gralloc_buffer.h index 6ab1497..e246602 100644 --- a/cros_gralloc/cros_gralloc_buffer.h +++ b/cros_gralloc/cros_gralloc_buffer.h @@ -8,6 +8,12 @@ #define CROS_GRALLOC_BUFFER_H #include +#include + +#include +#include +#include +#include #include "cros_gralloc_helpers.h" @@ -35,6 +41,26 @@ class cros_gralloc_buffer int32_t get_android_format() const; int64_t get_android_usage() const; + int32_t get_name(std::optional *name) const; + + int32_t get_blend_mode( + std::optional *blend_mode) const; + int32_t set_blend_mode(aidl::android::hardware::graphics::common::BlendMode blend_mode); + + int32_t get_dataspace( + std::optional *dataspace) const; + int32_t set_dataspace(aidl::android::hardware::graphics::common::Dataspace dataspace); + + int32_t + get_cta861_3(std::optional *cta) const; + int32_t + set_cta861_3(std::optional cta); + + int32_t get_smpte2086( + std::optional *smpte) const; + int32_t + set_smpte2086(std::optional smpte); + /* The new reference count is returned by both these functions. */ int32_t increase_refcount(); int32_t decrease_refcount(); @@ -48,8 +74,8 @@ class cros_gralloc_buffer int32_t invalidate(); int32_t flush(); - int32_t get_reserved_region(void **reserved_region_addr, - uint64_t *reserved_region_size) const; + int32_t get_client_reserved_region(void **client_reserved_region_addr, + uint64_t *client_reserved_region_size) const; private: cros_gralloc_buffer(struct bo *acquire_bo, struct cros_gralloc_handle *acquire_handle); @@ -57,6 +83,12 @@ class cros_gralloc_buffer cros_gralloc_buffer(cros_gralloc_buffer const &); cros_gralloc_buffer operator=(cros_gralloc_buffer const &); + int32_t get_reserved_region(void **reserved_region_addr, + uint64_t *reserved_region_size) const; + + int32_t get_metadata(struct cros_gralloc_buffer_metadata **metadata); + int32_t get_metadata(const struct cros_gralloc_buffer_metadata **metadata) const; + struct bo *bo_; /* Note: this will be nullptr for imported/retained buffers. */ diff --git a/cros_gralloc/cros_gralloc_buffer_metadata.h b/cros_gralloc/cros_gralloc_buffer_metadata.h index 30877b1..38c8801 100644 --- a/cros_gralloc/cros_gralloc_buffer_metadata.h +++ b/cros_gralloc/cros_gralloc_buffer_metadata.h @@ -12,7 +12,7 @@ #include #include -#define CROS_GRALLOC_BUFFER_METADATA_MAX_NAME_SIZE 1024 +#include "cros_gralloc_helpers.h" /* * The metadata for cros_gralloc_buffer-s that should reside in a shared memory region @@ -28,7 +28,7 @@ struct cros_gralloc_buffer_metadata { * handles. */ char name[CROS_GRALLOC_BUFFER_METADATA_MAX_NAME_SIZE]; - aidl::android::hardware::graphics::common::BlendMode blendMode; + aidl::android::hardware::graphics::common::BlendMode blend_mode; aidl::android::hardware::graphics::common::Dataspace dataspace; std::optional cta861_3; std::optional smpte2086; diff --git a/cros_gralloc/cros_gralloc_driver.cc b/cros_gralloc/cros_gralloc_driver.cc index bb54815..ed7d954 100644 --- a/cros_gralloc/cros_gralloc_driver.cc +++ b/cros_gralloc/cros_gralloc_driver.cc @@ -583,27 +583,6 @@ int32_t cros_gralloc_driver::resource_info(buffer_handle_t handle, uint32_t stri return buffer->resource_info(strides, offsets, format_modifier); } -int32_t cros_gralloc_driver::get_reserved_region(buffer_handle_t handle, - void **reserved_region_addr, - uint64_t *reserved_region_size) -{ - std::lock_guard lock(mutex_); - - auto hnd = cros_gralloc_convert_handle(handle); - if (!hnd) { - ALOGE("Invalid handle."); - return -EINVAL; - } - - auto buffer = get_buffer(hnd); - if (!buffer) { - ALOGE("Invalid reference (get_reserved_region() called on unregistered handle)."); - return -EINVAL; - } - - return buffer->get_reserved_region(reserved_region_addr, reserved_region_size); -} - uint32_t cros_gralloc_driver::get_resolved_drm_format(uint32_t drm_format, uint64_t use_flags) { uint32_t resolved_format; diff --git a/cros_gralloc/cros_gralloc_driver.h b/cros_gralloc/cros_gralloc_driver.h index 1f96178..56390fc 100644 --- a/cros_gralloc/cros_gralloc_driver.h +++ b/cros_gralloc/cros_gralloc_driver.h @@ -44,9 +44,6 @@ class cros_gralloc_driver int32_t resource_info(buffer_handle_t handle, uint32_t strides[DRV_MAX_PLANES], uint32_t offsets[DRV_MAX_PLANES], uint64_t *format_modifier); - int32_t get_reserved_region(buffer_handle_t handle, void **reserved_region_addr, - uint64_t *reserved_region_size); - uint32_t get_resolved_drm_format(uint32_t drm_format, uint64_t use_flags); void with_buffer(cros_gralloc_handle_t hnd, diff --git a/cros_gralloc/cros_gralloc_helpers.h b/cros_gralloc/cros_gralloc_helpers.h index 501d9b1..e8dd0a5 100644 --- a/cros_gralloc/cros_gralloc_helpers.h +++ b/cros_gralloc/cros_gralloc_helpers.h @@ -26,6 +26,8 @@ // Adopt BufferUsage::FRONT_BUFFER from api level 33 #define BUFFER_USAGE_FRONT_RENDERING_MASK (BUFFER_USAGE_FRONT_RENDERING | (1ULL << 32)) +#define CROS_GRALLOC_BUFFER_METADATA_MAX_NAME_SIZE 1024 + struct cros_gralloc_buffer_descriptor { uint32_t width; uint32_t height; diff --git a/cros_gralloc/gralloc4/CrosGralloc4Allocator.cc b/cros_gralloc/gralloc4/CrosGralloc4Allocator.cc index 233d9b9..852df88 100644 --- a/cros_gralloc/gralloc4/CrosGralloc4Allocator.cc +++ b/cros_gralloc/gralloc4/CrosGralloc4Allocator.cc @@ -9,7 +9,6 @@ #include #include -#include "cros_gralloc/cros_gralloc_buffer_metadata.h" #include "cros_gralloc/cros_gralloc_helpers.h" #include "cros_gralloc/gralloc4/CrosGralloc4Utils.h" diff --git a/cros_gralloc/gralloc4/CrosGralloc4Mapper.cc b/cros_gralloc/gralloc4/CrosGralloc4Mapper.cc index 502776c..f9f75b6 100644 --- a/cros_gralloc/gralloc4/CrosGralloc4Mapper.cc +++ b/cros_gralloc/gralloc4/CrosGralloc4Mapper.cc @@ -7,9 +7,12 @@ #include "cros_gralloc/gralloc4/CrosGralloc4Mapper.h" #include +#include #include #include #include +#include + #include #include @@ -17,9 +20,11 @@ #include "cros_gralloc/gralloc4/CrosGralloc4Utils.h" using aidl::android::hardware::graphics::common::BlendMode; +using aidl::android::hardware::graphics::common::Cta861_3; using aidl::android::hardware::graphics::common::Dataspace; using aidl::android::hardware::graphics::common::PlaneLayout; using aidl::android::hardware::graphics::common::Rect; +using aidl::android::hardware::graphics::common::Smpte2086; using android::hardware::hidl_handle; using android::hardware::hidl_vec; using android::hardware::Return; @@ -438,25 +443,18 @@ Return CrosGralloc4Mapper::get(const cros_gralloc_buffer* crosBuffer, return Void(); } - const struct cros_gralloc_buffer_metadata* crosMetadata = nullptr; - if (metadataType == android::gralloc4::MetadataType_BlendMode || - metadataType == android::gralloc4::MetadataType_Cta861_3 || - metadataType == android::gralloc4::MetadataType_Dataspace || - metadataType == android::gralloc4::MetadataType_Name || - metadataType == android::gralloc4::MetadataType_Smpte2086) { - Error error = getMetadata(crosBuffer, &crosMetadata); - if (error != Error::NONE) { - ALOGE("Failed to get. Failed to get buffer metadata."); - hidlCb(Error::NO_RESOURCES, encodedMetadata); - return Void(); - } - } - android::status_t status = android::NO_ERROR; if (metadataType == android::gralloc4::MetadataType_BufferId) { status = android::gralloc4::encodeBufferId(crosBuffer->get_id(), &encodedMetadata); } else if (metadataType == android::gralloc4::MetadataType_Name) { - status = android::gralloc4::encodeName(crosMetadata->name, &encodedMetadata); + std::optional name; + int ret = crosBuffer->get_name(&name); + if (ret) { + ALOGE("Failed to get. Failed to get name internal."); + status = android::UNKNOWN_ERROR; + } else { + status = android::gralloc4::encodeName(*name, &encodedMetadata); + } } else if (metadataType == android::gralloc4::MetadataType_Width) { status = android::gralloc4::encodeWidth(crosBuffer->get_width(), &encodedMetadata); } else if (metadataType == android::gralloc4::MetadataType_Height) { @@ -522,13 +520,41 @@ Return CrosGralloc4Mapper::get(const cros_gralloc_buffer* crosBuffer, status = android::gralloc4::encodeCrop(crops, &encodedMetadata); } else if (metadataType == android::gralloc4::MetadataType_Dataspace) { - status = android::gralloc4::encodeDataspace(crosMetadata->dataspace, &encodedMetadata); + std::optional dataspace; + int ret = crosBuffer->get_dataspace(&dataspace); + if (ret) { + ALOGE("Failed to get. Failed to get dataspace internal."); + status = android::UNKNOWN_ERROR; + } else { + status = android::gralloc4::encodeDataspace(*dataspace, &encodedMetadata); + } } else if (metadataType == android::gralloc4::MetadataType_BlendMode) { - status = android::gralloc4::encodeBlendMode(crosMetadata->blendMode, &encodedMetadata); + std::optional blend; + int ret = crosBuffer->get_blend_mode(&blend); + if (ret) { + ALOGE("Failed to get. Failed to get blend mode internal."); + status = android::UNKNOWN_ERROR; + } else { + status = android::gralloc4::encodeBlendMode(*blend, &encodedMetadata); + } } else if (metadataType == android::gralloc4::MetadataType_Smpte2086) { - status = android::gralloc4::encodeSmpte2086(crosMetadata->smpte2086, &encodedMetadata); + std::optional smpte; + int ret = crosBuffer->get_smpte2086(&smpte); + if (ret) { + ALOGE("Failed to get. Failed to get smpte2086 internal."); + status = android::UNKNOWN_ERROR; + } else { + status = android::gralloc4::encodeSmpte2086(smpte, &encodedMetadata); + } } else if (metadataType == android::gralloc4::MetadataType_Cta861_3) { - status = android::gralloc4::encodeCta861_3(crosMetadata->cta861_3, &encodedMetadata); + std::optional cta; + int ret = crosBuffer->get_cta861_3(&cta); + if (ret) { + ALOGE("Failed to get. Failed to get cta861_3 internal."); + status = android::UNKNOWN_ERROR; + } else { + status = android::gralloc4::encodeCta861_3(cta, &encodedMetadata); + } } else if (metadataType == android::gralloc4::MetadataType_Smpte2094_40) { status = android::gralloc4::encodeSmpte2094_40(std::nullopt, &encodedMetadata); } else { @@ -608,38 +634,54 @@ Error CrosGralloc4Mapper::set(cros_gralloc_buffer* crosBuffer, const MetadataTyp return Error::BAD_BUFFER; } - struct cros_gralloc_buffer_metadata* crosMetadata = nullptr; - - Error error = getMutableMetadata(crosBuffer, &crosMetadata); - if (error != Error::NONE) { - ALOGE("Failed to set. Failed to get buffer metadata."); - return Error::UNSUPPORTED; - } - if (metadataType == android::gralloc4::MetadataType_BlendMode) { - auto status = android::gralloc4::decodeBlendMode(encodedMetadata, &crosMetadata->blendMode); + BlendMode blend; + auto status = android::gralloc4::decodeBlendMode(encodedMetadata, &blend); if (status != android::NO_ERROR) { ALOGE("Failed to set. Failed to decode blend mode."); return Error::UNSUPPORTED; } + int ret = crosBuffer->set_blend_mode(blend); + if (ret) { + ALOGE("Failed to set. Failed to set blend mode internal."); + return Error::NO_RESOURCES; + } } else if (metadataType == android::gralloc4::MetadataType_Cta861_3) { - auto status = android::gralloc4::decodeCta861_3(encodedMetadata, &crosMetadata->cta861_3); + std::optional cta; + auto status = android::gralloc4::decodeCta861_3(encodedMetadata, &cta); if (status != android::NO_ERROR) { ALOGE("Failed to set. Failed to decode cta861_3."); return Error::UNSUPPORTED; } + int ret = crosBuffer->set_cta861_3(cta); + if (ret) { + ALOGE("Failed to set. Failed to set cta861_3 internal."); + return Error::NO_RESOURCES; + } } else if (metadataType == android::gralloc4::MetadataType_Dataspace) { - auto status = android::gralloc4::decodeDataspace(encodedMetadata, &crosMetadata->dataspace); + Dataspace dataspace; + auto status = android::gralloc4::decodeDataspace(encodedMetadata, &dataspace); if (status != android::NO_ERROR) { ALOGE("Failed to set. Failed to decode dataspace."); return Error::UNSUPPORTED; } + int ret = crosBuffer->set_dataspace(dataspace); + if (ret) { + ALOGE("Failed to set. Failed to set dataspace internal."); + return Error::NO_RESOURCES; + } } else if (metadataType == android::gralloc4::MetadataType_Smpte2086) { - auto status = android::gralloc4::decodeSmpte2086(encodedMetadata, &crosMetadata->smpte2086); + std::optional smpte; + auto status = android::gralloc4::decodeSmpte2086(encodedMetadata, &smpte); if (status != android::NO_ERROR) { ALOGE("Failed to set. Failed to decode smpte2086."); return Error::UNSUPPORTED; } + int ret = crosBuffer->set_smpte2086(smpte); + if (ret) { + ALOGE("Failed to set. Failed to set dataspace internal."); + return Error::NO_RESOURCES; + } } return Error::NONE; @@ -1034,75 +1076,6 @@ Return CrosGralloc4Mapper::dumpBuffers(dumpBuffers_cb hidlCb) { return Void(); } -Error CrosGralloc4Mapper::getReservedRegionArea(const cros_gralloc_buffer* crosBuffer, - ReservedRegionArea area, void** outAddr, - uint64_t* outSize) { - if (!mDriver) { - ALOGE("Failed to getReservedRegionArea. Driver is uninitialized."); - return Error::NO_RESOURCES; - } - - if (!crosBuffer) { - ALOGE("Failed to getReservedRegionArea. Invalid buffer."); - return Error::BAD_BUFFER; - } - - int ret = crosBuffer->get_reserved_region(outAddr, outSize); - if (ret) { - ALOGE("Failed to getReservedRegionArea."); - *outAddr = nullptr; - *outSize = 0; - return Error::NO_RESOURCES; - } - - switch (area) { - case ReservedRegionArea::MAPPER4_METADATA: { - // struct cros_gralloc_buffer_metadata resides at the beginning reserved region. - *outSize = sizeof(struct cros_gralloc_buffer_metadata); - break; - } - case ReservedRegionArea::USER_METADATA: { - // User metadata resides after the struct cros_gralloc_buffer_metadata. - *outAddr = reinterpret_cast(reinterpret_cast(*outAddr) + - sizeof(struct cros_gralloc_buffer_metadata)); - *outSize = *outSize - sizeof(struct cros_gralloc_buffer_metadata); - break; - } - } - - return Error::NONE; -} - -Error CrosGralloc4Mapper::getMetadata(const cros_gralloc_buffer* crosBuffer, - const struct cros_gralloc_buffer_metadata** outMetadata) { - void* addr = nullptr; - uint64_t size; - - Error error = - getReservedRegionArea(crosBuffer, ReservedRegionArea::MAPPER4_METADATA, &addr, &size); - if (error != Error::NONE) { - return error; - } - - *outMetadata = reinterpret_cast(addr); - return Error::NONE; -} - -Error CrosGralloc4Mapper::getMutableMetadata(cros_gralloc_buffer* crosBuffer, - struct cros_gralloc_buffer_metadata** outMetadata) { - void* addr = nullptr; - uint64_t size; - - Error error = - getReservedRegionArea(crosBuffer, ReservedRegionArea::MAPPER4_METADATA, &addr, &size); - if (error != Error::NONE) { - return error; - } - - *outMetadata = reinterpret_cast(addr); - return Error::NONE; -} - Return CrosGralloc4Mapper::getReservedRegion(void* rawHandle, getReservedRegion_cb hidlCb) { if (!mDriver) { ALOGE("Failed to getReservedRegion. Driver is uninitialized."); @@ -1128,13 +1101,17 @@ Return CrosGralloc4Mapper::getReservedRegion(void* rawHandle, getReservedR uint64_t reservedRegionSize = 0; Error error = Error::NONE; - mDriver->with_buffer(crosHandle, [&, this](cros_gralloc_buffer* crosBuffer) { - error = getReservedRegionArea(crosBuffer, ReservedRegionArea::USER_METADATA, - &reservedRegionAddr, &reservedRegionSize); + mDriver->with_buffer(crosHandle, [&](cros_gralloc_buffer* crosBuffer) { + int ret = crosBuffer->get_client_reserved_region(&reservedRegionAddr, &reservedRegionSize); + if (ret) { + reservedRegionAddr = nullptr; + reservedRegionSize = 0; + error = Error::NO_RESOURCES; + } }); if (error != Error::NONE) { - ALOGE("Failed to getReservedRegion. Failed to getReservedRegionArea."); + ALOGE("Failed to getReservedRegion."); hidlCb(Error::BAD_BUFFER, nullptr, 0); return Void(); } diff --git a/cros_gralloc/gralloc4/CrosGralloc4Mapper.h b/cros_gralloc/gralloc4/CrosGralloc4Mapper.h index 203c4c6..c8f4afe 100644 --- a/cros_gralloc/gralloc4/CrosGralloc4Mapper.h +++ b/cros_gralloc/gralloc4/CrosGralloc4Mapper.h @@ -8,7 +8,6 @@ #include -#include "cros_gralloc/cros_gralloc_buffer_metadata.h" #include "cros_gralloc/cros_gralloc_driver.h" #include "cros_gralloc/cros_gralloc_handle.h" @@ -68,25 +67,6 @@ class CrosGralloc4Mapper : public android::hardware::graphics::mapper::V4_0::IMa getReservedRegion_cb hidlCb) override; private: - enum class ReservedRegionArea { - /* struct cros_gralloc_buffer_metadata */ - MAPPER4_METADATA, - - /* External user metadata */ - USER_METADATA, - }; - - android::hardware::graphics::mapper::V4_0::Error getReservedRegionArea( - const cros_gralloc_buffer* crosBuffer, ReservedRegionArea area, void** outAddr, - uint64_t* outSize); - - android::hardware::graphics::mapper::V4_0::Error getMetadata( - const cros_gralloc_buffer* crosBuffer, - const struct cros_gralloc_buffer_metadata** outMetadata); - - android::hardware::graphics::mapper::V4_0::Error getMutableMetadata( - cros_gralloc_buffer* crosBuffer, struct cros_gralloc_buffer_metadata** outMetadata); - android::hardware::Return get(const cros_gralloc_buffer* crosBuffer, const MetadataType& metadataType, get_cb hidlCb); diff --git a/cros_gralloc/mapper_stablec/Mapper.cpp b/cros_gralloc/mapper_stablec/Mapper.cpp index bc40002..866f0dd 100644 --- a/cros_gralloc/mapper_stablec/Mapper.cpp +++ b/cros_gralloc/mapper_stablec/Mapper.cpp @@ -17,7 +17,6 @@ #include -#include "cros_gralloc/cros_gralloc_buffer_metadata.h" #include "cros_gralloc/cros_gralloc_driver.h" #include "cros_gralloc/cros_gralloc_handle.h" #include "cros_gralloc/gralloc4/CrosGralloc4Utils.h" @@ -109,30 +108,12 @@ class CrosGrallocMapperV5 final : public vendor::mapper::IMapperV5Impl { uint64_t* _Nonnull outReservedSize) override; private: - enum class ReservedRegionArea { - /* struct cros_gralloc_buffer_metadata */ - MAPPER4_METADATA, - - /* External user metadata */ - USER_METADATA, - }; - - AIMapper_Error getReservedRegionArea(const cros_gralloc_buffer* crosBuffer, - ReservedRegionArea area, void** outAddr, - uint64_t* outSize); - - AIMapper_Error getCrosMetadata(const cros_gralloc_buffer* crosBuffer, - const struct cros_gralloc_buffer_metadata** outMetadata); - - AIMapper_Error getMutableCrosMetadata(cros_gralloc_buffer* crosBuffer, - struct cros_gralloc_buffer_metadata** outMetadata); - template int32_t getStandardMetadata(const cros_gralloc_buffer* crosBuffer, F&& provide, StandardMetadata); template - AIMapper_Error setStandardMetadata(struct cros_gralloc_buffer_metadata* crosMetadata, + AIMapper_Error setStandardMetadata(cros_gralloc_buffer* crosBuffer, typename StandardMetadata::value_type&& value); void dumpBuffer( @@ -329,23 +310,16 @@ int32_t CrosGrallocMapperV5::getStandardMetadata(buffer_handle_t _Nonnull buffer template int32_t CrosGrallocMapperV5::getStandardMetadata(const cros_gralloc_buffer* crosBuffer, F&& provide, StandardMetadata) { - const struct cros_gralloc_buffer_metadata* crosMetadata = nullptr; - if constexpr (metadataType == StandardMetadataType::BLEND_MODE || - metadataType == StandardMetadataType::CTA861_3 || - metadataType == StandardMetadataType::DATASPACE || - metadataType == StandardMetadataType::NAME || - metadataType == StandardMetadataType::SMPTE2086) { - AIMapper_Error error = getCrosMetadata(crosBuffer, &crosMetadata); - if (error != AIMAPPER_ERROR_NONE) { - ALOGE("Failed to get. Failed to get buffer metadata."); - return -AIMAPPER_ERROR_NO_RESOURCES; - } - } if constexpr (metadataType == StandardMetadataType::BUFFER_ID) { return provide(crosBuffer->get_id()); } if constexpr (metadataType == StandardMetadataType::NAME) { - return provide(crosMetadata->name); + std::optional name; + if (crosBuffer->get_name(&name)) { + return -AIMAPPER_ERROR_NO_RESOURCES; + } else { + return provide(*name); + } } if constexpr (metadataType == StandardMetadataType::WIDTH) { return provide(crosBuffer->get_width()); @@ -423,16 +397,36 @@ int32_t CrosGrallocMapperV5::getStandardMetadata(const cros_gralloc_buffer* cros return provide(crops); } if constexpr (metadataType == StandardMetadataType::DATASPACE) { - return provide(crosMetadata->dataspace); + std::optional dataspace; + if (crosBuffer->get_dataspace(&dataspace)) { + return -AIMAPPER_ERROR_NO_RESOURCES; + } else { + return provide(*dataspace); + } } if constexpr (metadataType == StandardMetadataType::BLEND_MODE) { - return provide(crosMetadata->blendMode); + std::optional blend; + if (crosBuffer->get_blend_mode(&blend)) { + return -AIMAPPER_ERROR_NO_RESOURCES; + } else { + return provide(*blend); + } } if constexpr (metadataType == StandardMetadataType::SMPTE2086) { - return crosMetadata->smpte2086 ? provide(*crosMetadata->smpte2086) : 0; + std::optional smpte; + if (crosBuffer->get_smpte2086(&smpte)) { + return -AIMAPPER_ERROR_NO_RESOURCES; + } else { + return smpte ? provide(*smpte) : 0; + } } if constexpr (metadataType == StandardMetadataType::CTA861_3) { - return crosMetadata->cta861_3 ? provide(*crosMetadata->cta861_3) : 0; + std::optional cta; + if (crosBuffer->get_cta861_3(&cta)) { + return -AIMAPPER_ERROR_NO_RESOURCES; + } else { + return cta ? provide(*cta) : 0; + } } return -AIMAPPER_ERROR_UNSUPPORTED; } @@ -488,14 +482,8 @@ AIMapper_Error CrosGrallocMapperV5::setStandardMetadata(buffer_handle_t _Nonnull AIMapper_Error status = AIMAPPER_ERROR_UNSUPPORTED; mDriver->with_buffer(crosHandle, [&](cros_gralloc_buffer* crosBuffer) { - struct cros_gralloc_buffer_metadata* crosMetadata = nullptr; - status = getMutableCrosMetadata(crosBuffer, &crosMetadata); - if (status != AIMAPPER_ERROR_NONE) { - return; - } - auto applier = [&](auto&& value) -> AIMapper_Error { - return setStandardMetadata(crosMetadata, std::forward(value)); + return setStandardMetadata(crosBuffer, std::forward(value)); }; status = applyStandardMetadata(standardType, metadata, metadataSize, applier); @@ -505,19 +493,25 @@ AIMapper_Error CrosGrallocMapperV5::setStandardMetadata(buffer_handle_t _Nonnull template AIMapper_Error CrosGrallocMapperV5::setStandardMetadata( - struct cros_gralloc_buffer_metadata* crosMetadata, typename StandardMetadata::value_type&& value) { + cros_gralloc_buffer* crosBuffer, typename StandardMetadata::value_type&& value) { + int ret = 0; if constexpr (TYPE == StandardMetadataType::BLEND_MODE) { - crosMetadata->blendMode = value; + ret = crosBuffer->set_blend_mode(value); } if constexpr (TYPE == StandardMetadataType::CTA861_3) { - crosMetadata->cta861_3 = value; + ret = crosBuffer->set_cta861_3(value); } if constexpr (TYPE == StandardMetadataType::DATASPACE) { - crosMetadata->dataspace = value; + ret = crosBuffer->set_dataspace(value); } if constexpr (TYPE == StandardMetadataType::SMPTE2086) { - crosMetadata->smpte2086 = value; + ret = crosBuffer->set_smpte2086(value); } + + if (ret) { + return AIMAPPER_ERROR_NO_RESOURCES; + } + // Unsupported metadatas were already filtered before we reached this point return AIMAPPER_ERROR_NONE; } @@ -667,78 +661,23 @@ AIMapper_Error CrosGrallocMapperV5::getReservedRegion(buffer_handle_t _Nonnull b uint64_t reservedRegionSize = 0; AIMapper_Error error = AIMAPPER_ERROR_NONE; - mDriver->with_buffer(crosHandle, [&, this](cros_gralloc_buffer* crosBuffer) { - error = getReservedRegionArea(crosBuffer, ReservedRegionArea::USER_METADATA, - &reservedRegionAddr, &reservedRegionSize); + mDriver->with_buffer(crosHandle, [&](cros_gralloc_buffer* crosBuffer) { + int ret = crosBuffer->get_client_reserved_region(&reservedRegionAddr, &reservedRegionSize); + if (ret) { + reservedRegionAddr = nullptr; + reservedRegionSize = 0; + error = AIMAPPER_ERROR_NO_RESOURCES; + } }); if (error != AIMAPPER_ERROR_NONE) { - ALOGE("Failed to getReservedRegion. Failed to getReservedRegionArea."); + ALOGE("Failed to getReservedRegion. Failed to getReservedRegion."); return AIMAPPER_ERROR_BAD_BUFFER; } return AIMAPPER_ERROR_NONE; } -AIMapper_Error CrosGrallocMapperV5::getReservedRegionArea(const cros_gralloc_buffer* crosBuffer, - ReservedRegionArea area, void** outAddr, - uint64_t* outSize) { - int ret = crosBuffer->get_reserved_region(outAddr, outSize); - if (ret) { - ALOGE("Failed to getReservedRegionArea."); - *outAddr = nullptr; - *outSize = 0; - return AIMAPPER_ERROR_NO_RESOURCES; - } - - switch (area) { - case ReservedRegionArea::MAPPER4_METADATA: { - // struct cros_gralloc_buffer_metadata resides at the beginning reserved region. - *outSize = sizeof(struct cros_gralloc_buffer_metadata); - break; - } - case ReservedRegionArea::USER_METADATA: { - // User metadata resides after the struct cros_gralloc_buffer_metadata. - *outAddr = reinterpret_cast(reinterpret_cast(*outAddr) + - sizeof(struct cros_gralloc_buffer_metadata)); - *outSize = *outSize - sizeof(struct cros_gralloc_buffer_metadata); - break; - } - } - - return AIMAPPER_ERROR_NONE; -} - -AIMapper_Error CrosGrallocMapperV5::getCrosMetadata(const cros_gralloc_buffer* crosBuffer, - const struct cros_gralloc_buffer_metadata** outMetadata) { - void* addr = nullptr; - uint64_t size; - - auto error = - getReservedRegionArea(crosBuffer, ReservedRegionArea::MAPPER4_METADATA, &addr, &size); - if (error != AIMAPPER_ERROR_NONE) { - return error; - } - - *outMetadata = reinterpret_cast(addr); - return AIMAPPER_ERROR_NONE; -} - -AIMapper_Error CrosGrallocMapperV5::getMutableCrosMetadata(cros_gralloc_buffer* crosBuffer, - struct cros_gralloc_buffer_metadata** outMetadata) { - void* addr = nullptr; - uint64_t size; - - auto error = - getReservedRegionArea(crosBuffer, ReservedRegionArea::MAPPER4_METADATA, &addr, &size); - if (error != AIMAPPER_ERROR_NONE) { - return error; - } - - *outMetadata = reinterpret_cast(addr); - return AIMAPPER_ERROR_NONE; -} - extern "C" uint32_t ANDROID_HAL_MAPPER_VERSION = AIMAPPER_VERSION_5; extern "C" AIMapper_Error AIMapper_loadIMapper(AIMapper* _Nullable* _Nonnull outImplementation) { -- cgit v1.2.3 From 3a1e511437d02590996b50a763e3373b0602db7a Mon Sep 17 00:00:00 2001 From: Jason Macnak Date: Tue, 23 Jan 2024 13:32:27 -0800 Subject: gralloc: Avoid using std::optional in shared memory metadata ... to ensure the metadata is trivially copyable. Bug: b/321158178 Test: vts -m VtsHalGraphicsAllocatorAidl_TargetTest Test: vts -m VtsHalGraphicsMapperV4_0Target Test: vts -m VtsHalGraphicsMapperStableC_TargetTest Change-Id: Ib8cc1147692fa1e056108987339aff3e222ab0d7 --- cros_gralloc/cros_gralloc_buffer.cc | 6 +++-- cros_gralloc/cros_gralloc_buffer_metadata.h | 34 +++++++++++++++++++++++++++-- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/cros_gralloc/cros_gralloc_buffer.cc b/cros_gralloc/cros_gralloc_buffer.cc index dfa121b..b2c4dc8 100644 --- a/cros_gralloc/cros_gralloc_buffer.cc +++ b/cros_gralloc/cros_gralloc_buffer.cc @@ -58,6 +58,8 @@ cros_gralloc_buffer::initialize_metadata(const struct cros_gralloc_buffer_descri return -1; } + new (metadata) cros_gralloc_buffer_metadata(); + snprintf(metadata->name, CROS_GRALLOC_BUFFER_METADATA_MAX_NAME_SIZE, "%s", descriptor->name.c_str()); metadata->dataspace = descriptor->dataspace; @@ -230,7 +232,7 @@ int32_t cros_gralloc_buffer::get_cta861_3(std::optional *cta) const return ret; } - *cta = metadata->cta861_3; + *cta = metadata->cta861_3.to_std_optional(); return 0; } @@ -258,7 +260,7 @@ int32_t cros_gralloc_buffer::get_smpte2086(std::optional *smpte) cons return ret; } - *smpte = metadata->smpte2086; + *smpte = metadata->smpte2086.to_std_optional(); return 0; } diff --git a/cros_gralloc/cros_gralloc_buffer_metadata.h b/cros_gralloc/cros_gralloc_buffer_metadata.h index 38c8801..361a472 100644 --- a/cros_gralloc/cros_gralloc_buffer_metadata.h +++ b/cros_gralloc/cros_gralloc_buffer_metadata.h @@ -7,6 +7,9 @@ #ifndef CROSGRALLOC4METADATA_H #define CROSGRALLOC4METADATA_H +#include +#include + #include #include #include @@ -14,6 +17,30 @@ #include "cros_gralloc_helpers.h" +// Simple replacement for std::optional which is not guarenteed to be memory layout +// stable across ABIs. +template struct cros_buffer_optional { + + enum class state : uint32_t { VACANT, OCCUPIED }; + + cros_buffer_optional() = default; + + cros_buffer_optional(std::optional v) : occupancy(v ? state::OCCUPIED : state::VACANT) + { + if (v) { + value = *v; + } + } + + std::optional to_std_optional() const + { + return occupancy == state::VACANT ? std::nullopt : std::make_optional(value); + } + + state occupancy = state::VACANT; + T value; +}; + /* * The metadata for cros_gralloc_buffer-s that should reside in a shared memory region * instead of directly in cros_gralloc_handle-s. @@ -30,8 +57,11 @@ struct cros_gralloc_buffer_metadata { char name[CROS_GRALLOC_BUFFER_METADATA_MAX_NAME_SIZE]; aidl::android::hardware::graphics::common::BlendMode blend_mode; aidl::android::hardware::graphics::common::Dataspace dataspace; - std::optional cta861_3; - std::optional smpte2086; + cros_buffer_optional cta861_3; + cros_buffer_optional smpte2086; }; +static_assert(std::is_standard_layout_v); +static_assert(std::is_trivially_copyable_v); + #endif -- cgit v1.2.3