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
This commit is contained in:
Jason Macnak 2024-01-22 14:39:55 -08:00
parent bd616875b5
commit 92eb16bb24
11 changed files with 359 additions and 276 deletions

View file

@ -13,7 +13,6 @@
#include <gralloctypes/Gralloc4.h>
#include <log/log.h>
#include "cros_gralloc/cros_gralloc_buffer_metadata.h"
#include "cros_gralloc/gralloc4/CrosGralloc4Utils.h"
using aidl::android::hardware::common::NativeHandle;

View file

@ -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>
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<cros_gralloc_buffer_metadata *>(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<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;
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;
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_;
@ -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<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"
@ -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<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();
@ -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. */

View file

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

View file

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

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

View file

@ -9,7 +9,6 @@
#include <android/hardware/graphics/mapper/4.0/IMapper.h>
#include <gralloctypes/Gralloc4.h>
#include "cros_gralloc/cros_gralloc_buffer_metadata.h"
#include "cros_gralloc/cros_gralloc_helpers.h"
#include "cros_gralloc/gralloc4/CrosGralloc4Utils.h"

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 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<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;
}
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<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: {
// 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<void*>(reinterpret_cast<char*>(*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<const struct cros_gralloc_buffer_metadata*>(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<struct cros_gralloc_buffer_metadata*>(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

@ -8,7 +8,6 @@
#include <memory>
#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<void> get(const cros_gralloc_buffer* crosBuffer,
const MetadataType& metadataType, get_cb hidlCb);

View file

@ -17,7 +17,6 @@
#include <memory>
#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 <typename F, StandardMetadataType TYPE>
int32_t getStandardMetadata(const cros_gralloc_buffer* crosBuffer, F&& provide,
StandardMetadata<TYPE>);
template <StandardMetadataType TYPE>
AIMapper_Error setStandardMetadata(struct cros_gralloc_buffer_metadata* 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 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<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) {
struct cros_gralloc_buffer_metadata* 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(
struct cros_gralloc_buffer_metadata* 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: {
// 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<void*>(reinterpret_cast<char*>(*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<const struct cros_gralloc_buffer_metadata*>(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<struct cros_gralloc_buffer_metadata*>(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) {