Merge changes Ib8cc1147,I33f17670,Ib00873ba into main am: 24a395410e

Original change: https://android-review.googlesource.com/c/platform/external/minigbm/+/2924559

Change-Id: I52e449419359ba9c69c69253432928ecdac0b817
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
Jason Macnak 2024-05-15 16:12:34 +00:00 committed by Automerger Merge Worker
commit 696fa2523a
17 changed files with 583 additions and 506 deletions

View file

@ -116,6 +116,7 @@ cc_defaults {
"libcutils",
"libdmabufheap",
"libdrm",
"libgralloctypes",
"libnativewindow",
"libsync",
"liblog",

View file

@ -28,137 +28,9 @@ inline ndk::ScopedAStatus ToBinderStatus(AllocationError error) {
return ndk::ScopedAStatus::fromServiceSpecificError(static_cast<int32_t>(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);
}
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);
}
CrosGralloc4Metadata* crosMetadata = reinterpret_cast<CrosGralloc4Metadata*>(addr);
snprintf(crosMetadata->name, CROS_GRALLOC4_METADATA_MAX_NAME_SIZE, "%s",
crosDescriptor.name.c_str());
crosMetadata->dataspace = initialDataspace;
crosMetadata->blendMode = common::BlendMode::INVALID;
return ndk::ScopedAStatus::ok();
}
void Allocator::releaseBufferAndHandle(native_handle_t* handle) {
mDriver->release(handle);
native_handle_close(handle);
native_handle_delete(handle);
}
ndk::ScopedAStatus Allocator::allocate(const std::vector<uint8_t>& descriptor, int32_t count,
allocator::AllocationResult* outResult) {
if (!mDriver) {
ALOGE("Failed to allocate. Driver is uninitialized.\n");
return ToBinderStatus(AllocationError::NO_RESOURCES);
}
BufferDescriptorInfoV4 description;
int ret = ::android::gralloc4::decodeBufferDescriptorInfo(descriptor, &description);
if (ret) {
ALOGE("Failed to allocate. Failed to decode buffer descriptor: %d.\n", ret);
return ToBinderStatus(AllocationError::BAD_DESCRIPTOR);
}
std::vector<native_handle_t*> handles;
handles.resize(count, nullptr);
for (int32_t i = 0; i < count; i++) {
ndk::ScopedAStatus status = allocate(description, &outResult->stride, &handles[i]);
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::allocate(const BufferDescriptorInfoV4& descriptor, int32_t* outStride,
native_handle_t** outHandle, Dataspace initialDataspace) {
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);
ALOGE("Failed to allocate. Unsupported combination: pixel format:%s, drm format:%s, "
"usage:%s\n",
pixelFormatString.c_str(), drmFormatString.c_str(), usageString.c_str());
return ToBinderStatus(AllocationError::UNSUPPORTED);
}
native_handle_t* handle;
int ret = mDriver->allocate(&crosDescriptor, &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<int32_t>(crosHandle->pixel_stride);
*outHandle = handle;
return ndk::ScopedAStatus::ok();
}
static BufferDescriptorInfoV4 convertAidlToIMapperV4Descriptor(const BufferDescriptorInfo& info) {
return BufferDescriptorInfoV4 {
ndk::ScopedAStatus convertToCrosDescriptor(const BufferDescriptorInfo& info,
struct cros_gralloc_buffer_descriptor& crosDescriptor) {
const BufferDescriptorInfoV4 mapperV4Descriptor = {
.name{reinterpret_cast<const char*>(info.name.data())},
.width = static_cast<uint32_t>(info.width),
.height = static_cast<uint32_t>(info.height),
@ -167,6 +39,54 @@ static BufferDescriptorInfoV4 convertAidlToIMapperV4Descriptor(const BufferDescr
.usage = static_cast<uint64_t>(info.usage),
.reservedSize = 0,
};
if (convertToCrosDescriptor(mapperV4Descriptor, &crosDescriptor)) {
return ToBinderStatus(AllocationError::UNSUPPORTED);
}
for (const auto& option : info.additionalOptions) {
if (option.name != STANDARD_METADATA_DATASPACE) {
return ToBinderStatus(AllocationError::UNSUPPORTED);
}
crosDescriptor.dataspace = static_cast<common::Dataspace>(option.value);
}
return ndk::ScopedAStatus::ok();
}
} // namespace
bool Allocator::init() {
mDriver = cros_gralloc_driver::get_instance();
return mDriver != nullptr;
}
void Allocator::releaseBufferAndHandle(native_handle_t* handle) {
mDriver->release(handle);
native_handle_close(handle);
native_handle_delete(handle);
}
ndk::ScopedAStatus Allocator::allocate(const std::vector<uint8_t>& encodedDescriptor, int32_t count,
allocator::AllocationResult* outResult) {
if (!mDriver) {
ALOGE("Failed to allocate. Driver is uninitialized.\n");
return ToBinderStatus(AllocationError::NO_RESOURCES);
}
BufferDescriptorInfoV4 mapperV4Descriptor;
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,
@ -176,23 +96,28 @@ ndk::ScopedAStatus Allocator::allocate2(const BufferDescriptorInfo& descriptor,
return ToBinderStatus(AllocationError::NO_RESOURCES);
}
Dataspace initialDataspace = Dataspace::UNKNOWN;
struct cros_gralloc_buffer_descriptor crosDescriptor = {};
for (const auto& option : descriptor.additionalOptions) {
if (option.name != STANDARD_METADATA_DATASPACE) {
return ToBinderStatus(AllocationError::UNSUPPORTED);
}
initialDataspace = static_cast<Dataspace>(option.value);
ndk::ScopedAStatus status = convertToCrosDescriptor(descriptor, crosDescriptor);
if (!status.isOk()) {
return status;
}
BufferDescriptorInfoV4 descriptionV4 = convertAidlToIMapperV4Descriptor(descriptor);
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<native_handle_t*> handles;
handles.resize(count, nullptr);
for (int32_t i = 0; i < count; i++) {
ndk::ScopedAStatus status = allocate(descriptionV4, &outResult->stride, &handles[i],
initialDataspace);
ndk::ScopedAStatus status = allocateBuffer(descriptor, &outResult->stride, &handles[i]);
if (!status.isOk()) {
for (int32_t j = 0; j < i; j++) {
releaseBufferAndHandle(handles[j]);
@ -211,6 +136,40 @@ ndk::ScopedAStatus Allocator::allocate2(const BufferDescriptorInfo& descriptor,
return ndk::ScopedAStatus::ok();
}
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);
}
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<uint64_t>(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());
return ToBinderStatus(AllocationError::UNSUPPORTED);
}
native_handle_t* handle;
int ret = mDriver->allocate(&descriptor, &handle);
if (ret) {
return ToBinderStatus(AllocationError::NO_RESOURCES);
}
cros_gralloc_handle_t crosHandle = cros_gralloc_convert_handle(handle);
*outStride = static_cast<int32_t>(crosHandle->pixel_stride);
*outHandle = handle;
return ndk::ScopedAStatus::ok();
}
ndk::ScopedAStatus Allocator::isSupported(const BufferDescriptorInfo& descriptor,
bool* outResult) {
if (!mDriver) {
@ -225,16 +184,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();
}

View file

@ -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 {
@ -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);

View file

@ -11,6 +11,13 @@
#include <cutils/native_handle.h>
#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>
cros_gralloc_buffer::create(struct bo *acquire_bo,
@ -35,6 +42,31 @@ cros_gralloc_buffer::create(struct bo *acquire_bo,
return buffer;
}
int32_t
cros_gralloc_buffer::initialize_metadata(const struct cros_gralloc_buffer_descriptor *descriptor)
{
struct cros_gralloc_buffer_metadata *metadata;
int ret = get_metadata(&metadata);
if (ret) {
ALOGE("Failed to initialize metadata: failed to get metadata region.");
return ret;
}
if (metadata == nullptr) {
ALOGE("Failed to initialize metadata: invalid metadata address.");
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;
metadata->blend_mode = 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)
@ -120,6 +152,132 @@ int64_t cros_gralloc_buffer::get_android_usage() const
return hnd_->usage;
}
int32_t cros_gralloc_buffer::get_name(std::optional<std::string> *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<BlendMode> *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> *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<Cta861_3> *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.to_std_optional();
return 0;
}
int32_t cros_gralloc_buffer::set_cta861_3(std::optional<Cta861_3> 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<Smpte2086> *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.to_std_optional();
return 0;
}
int32_t cros_gralloc_buffer::set_smpte2086(std::optional<Smpte2086> 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_;
@ -242,3 +400,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<void *>(reinterpret_cast<char *>(*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<struct cros_gralloc_buffer_metadata *>(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<const struct cros_gralloc_buffer_metadata *>(metadata_addr);
return 0;
}

View file

@ -8,6 +8,12 @@
#define CROS_GRALLOC_BUFFER_H
#include <memory>
#include <optional>
#include <aidl/android/hardware/graphics/common/BlendMode.h>
#include <aidl/android/hardware/graphics/common/Cta861_3.h>
#include <aidl/android/hardware/graphics/common/Dataspace.h>
#include <aidl/android/hardware/graphics/common/Smpte2086.h>
#include "cros_gralloc_helpers.h"
@ -19,6 +25,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;
@ -33,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<std::string> *name) const;
int32_t get_blend_mode(
std::optional<aidl::android::hardware::graphics::common::BlendMode> *blend_mode) const;
int32_t set_blend_mode(aidl::android::hardware::graphics::common::BlendMode blend_mode);
int32_t get_dataspace(
std::optional<aidl::android::hardware::graphics::common::Dataspace> *dataspace) const;
int32_t set_dataspace(aidl::android::hardware::graphics::common::Dataspace dataspace);
int32_t
get_cta861_3(std::optional<aidl::android::hardware::graphics::common::Cta861_3> *cta) const;
int32_t
set_cta861_3(std::optional<aidl::android::hardware::graphics::common::Cta861_3> cta);
int32_t get_smpte2086(
std::optional<aidl::android::hardware::graphics::common::Smpte2086> *smpte) const;
int32_t
set_smpte2086(std::optional<aidl::android::hardware::graphics::common::Smpte2086> smpte);
/* The new reference count is returned by both these functions. */
int32_t increase_refcount();
int32_t decrease_refcount();
@ -46,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);
@ -55,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. */

View file

@ -0,0 +1,67 @@
/*
* 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 <optional>
#include <type_traits>
#include <aidl/android/hardware/graphics/common/BlendMode.h>
#include <aidl/android/hardware/graphics/common/Cta861_3.h>
#include <aidl/android/hardware/graphics/common/Dataspace.h>
#include <aidl/android/hardware/graphics/common/Smpte2086.h>
#include "cros_gralloc_helpers.h"
// Simple replacement for std::optional which is not guarenteed to be memory layout
// stable across ABIs.
template <typename T> struct cros_buffer_optional {
enum class state : uint32_t { VACANT, OCCUPIED };
cros_buffer_optional() = default;
cros_buffer_optional(std::optional<T> v) : occupancy(v ? state::OCCUPIED : state::VACANT)
{
if (v) {
value = *v;
}
}
std::optional<T> 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.
*
* 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 blend_mode;
aidl::android::hardware::graphics::common::Dataspace dataspace;
cros_buffer_optional<aidl::android::hardware::graphics::common::Cta861_3> cta861_3;
cros_buffer_optional<aidl::android::hardware::graphics::common::Smpte2086> smpte2086;
};
static_assert(std::is_standard_layout_v<cros_gralloc_buffer_metadata>);
static_assert(std::is_trivially_copyable_v<cros_gralloc_buffer_metadata>);
#endif

View file

@ -15,6 +15,7 @@
#include <xf86drm.h>
#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<std::mutex> lock(mutex_);
@ -572,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<std::mutex> 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;

View file

@ -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,

View file

@ -10,6 +10,8 @@
#include "../drv.h"
#include "cros_gralloc_handle.h"
#include <aidl/android/hardware/graphics/common/BlendMode.h>
#include <aidl/android/hardware/graphics/common/Dataspace.h>
#include <log/log.h>
#include <system/graphics.h>
#include <system/window.h>
@ -24,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;
@ -31,8 +35,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;

View file

@ -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, "

View file

@ -10,7 +10,6 @@
#include <gralloctypes/Gralloc4.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 +30,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<CrosGralloc4Metadata*>(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 +46,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 +63,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;

View file

@ -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,

View file

@ -7,9 +7,12 @@
#include "cros_gralloc/gralloc4/CrosGralloc4Mapper.h"
#include <aidl/android/hardware/graphics/common/BlendMode.h>
#include <aidl/android/hardware/graphics/common/Cta861_3.h>
#include <aidl/android/hardware/graphics/common/Dataspace.h>
#include <aidl/android/hardware/graphics/common/PlaneLayout.h>
#include <aidl/android/hardware/graphics/common/Rect.h>
#include <aidl/android/hardware/graphics/common/Smpte2086.h>
#include <cutils/native_handle.h>
#include <gralloctypes/Gralloc4.h>
@ -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<void> CrosGralloc4Mapper::get(const cros_gralloc_buffer* crosBuffer,
return Void();
}
const CrosGralloc4Metadata* 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<std::string> 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<void> 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> 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<BlendMode> 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<Smpte2086> 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<Cta861_3> 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;
}
CrosGralloc4Metadata* 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<Cta861_3> 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<Smpte2086> 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<void> 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: {
// CrosGralloc4Metadata resides at the beginning reserved region.
*outSize = sizeof(CrosGralloc4Metadata);
break;
}
case ReservedRegionArea::USER_METADATA: {
// User metadata resides after the CrosGralloc4Metadata.
*outAddr = reinterpret_cast<void*>(reinterpret_cast<char*>(*outAddr) +
sizeof(CrosGralloc4Metadata));
*outSize = *outSize - sizeof(CrosGralloc4Metadata);
break;
}
}
return Error::NONE;
}
Error CrosGralloc4Mapper::getMetadata(const cros_gralloc_buffer* crosBuffer,
const CrosGralloc4Metadata** 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<const CrosGralloc4Metadata*>(addr);
return Error::NONE;
}
Error CrosGralloc4Mapper::getMutableMetadata(cros_gralloc_buffer* crosBuffer,
CrosGralloc4Metadata** 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<CrosGralloc4Metadata*>(addr);
return Error::NONE;
}
Return<void> CrosGralloc4Mapper::getReservedRegion(void* rawHandle, getReservedRegion_cb hidlCb) {
if (!mDriver) {
ALOGE("Failed to getReservedRegion. Driver is uninitialized.");
@ -1128,13 +1101,17 @@ Return<void> 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();
}

View file

@ -10,7 +10,6 @@
#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:
@ -68,24 +67,6 @@ class CrosGralloc4Mapper : public android::hardware::graphics::mapper::V4_0::IMa
getReservedRegion_cb hidlCb) override;
private:
enum class ReservedRegionArea {
/* CrosGralloc4Metadata */
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 CrosGralloc4Metadata** outMetadata);
android::hardware::graphics::mapper::V4_0::Error getMutableMetadata(
cros_gralloc_buffer* crosBuffer, CrosGralloc4Metadata** outMetadata);
android::hardware::Return<void> get(const cros_gralloc_buffer* crosBuffer,
const MetadataType& metadataType, get_cb hidlCb);

View file

@ -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 <aidl/android/hardware/graphics/common/BlendMode.h>
#include <aidl/android/hardware/graphics/common/Cta861_3.h>
#include <aidl/android/hardware/graphics/common/Dataspace.h>
#include <aidl/android/hardware/graphics/common/Smpte2086.h>
#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<aidl::android::hardware::graphics::common::Cta861_3> cta861_3;
std::optional<aidl::android::hardware::graphics::common::Smpte2086> smpte2086;
};
#endif

View file

@ -62,7 +62,8 @@ int convertToCrosDescriptor(const BufferDescriptorInfo& descriptor,
outCrosDescriptor->height = descriptor.height;
outCrosDescriptor->droid_format = static_cast<int32_t>(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;

View file

@ -19,7 +19,6 @@
#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 +42,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<BufferDescriptorInfo>().name){}.size(),
"Metadata name storage too small to fit a BufferDescriptorInfo::name");
@ -109,30 +108,12 @@ class CrosGrallocMapperV5 final : public vendor::mapper::IMapperV5Impl {
uint64_t* _Nonnull outReservedSize) override;
private:
enum class ReservedRegionArea {
/* CrosGralloc4Metadata */
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 CrosGralloc4Metadata** outMetadata);
AIMapper_Error getMutableCrosMetadata(cros_gralloc_buffer* crosBuffer,
CrosGralloc4Metadata** outMetadata);
template <typename F, StandardMetadataType TYPE>
int32_t getStandardMetadata(const cros_gralloc_buffer* crosBuffer, F&& provide,
StandardMetadata<TYPE>);
template <StandardMetadataType TYPE>
AIMapper_Error setStandardMetadata(CrosGralloc4Metadata* crosMetadata,
AIMapper_Error setStandardMetadata(cros_gralloc_buffer* crosBuffer,
typename StandardMetadata<TYPE>::value_type&& value);
void dumpBuffer(
@ -329,23 +310,16 @@ int32_t CrosGrallocMapperV5::getStandardMetadata(buffer_handle_t _Nonnull buffer
template <typename F, StandardMetadataType metadataType>
int32_t CrosGrallocMapperV5::getStandardMetadata(const cros_gralloc_buffer* crosBuffer, F&& provide,
StandardMetadata<metadataType>) {
const CrosGralloc4Metadata* 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<std::string> 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> 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<BlendMode> 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<Smpte2086> 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<Cta861_3> 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) {
CrosGralloc4Metadata* crosMetadata = nullptr;
status = getMutableCrosMetadata(crosBuffer, &crosMetadata);
if (status != AIMAPPER_ERROR_NONE) {
return;
}
auto applier = [&]<StandardMetadataType T>(auto&& value) -> AIMapper_Error {
return setStandardMetadata<T>(crosMetadata, std::forward<decltype(value)>(value));
return setStandardMetadata<T>(crosBuffer, std::forward<decltype(value)>(value));
};
status = applyStandardMetadata(standardType, metadata, metadataSize, applier);
@ -505,19 +493,25 @@ AIMapper_Error CrosGrallocMapperV5::setStandardMetadata(buffer_handle_t _Nonnull
template <StandardMetadataType TYPE>
AIMapper_Error CrosGrallocMapperV5::setStandardMetadata(
CrosGralloc4Metadata* crosMetadata, typename StandardMetadata<TYPE>::value_type&& value) {
cros_gralloc_buffer* crosBuffer, typename StandardMetadata<TYPE>::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: {
// CrosGralloc4Metadata resides at the beginning reserved region.
*outSize = sizeof(CrosGralloc4Metadata);
break;
}
case ReservedRegionArea::USER_METADATA: {
// User metadata resides after the CrosGralloc4Metadata.
*outAddr = reinterpret_cast<void*>(reinterpret_cast<char*>(*outAddr) +
sizeof(CrosGralloc4Metadata));
*outSize = *outSize - sizeof(CrosGralloc4Metadata);
break;
}
}
return AIMAPPER_ERROR_NONE;
}
AIMapper_Error CrosGrallocMapperV5::getCrosMetadata(const cros_gralloc_buffer* crosBuffer,
const CrosGralloc4Metadata** 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<const CrosGralloc4Metadata*>(addr);
return AIMAPPER_ERROR_NONE;
}
AIMapper_Error CrosGrallocMapperV5::getMutableCrosMetadata(cros_gralloc_buffer* crosBuffer,
CrosGralloc4Metadata** 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<CrosGralloc4Metadata*>(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) {