diff options
author | Xin Li <delphij@google.com> | 2024-01-17 22:14:14 -0800 |
---|---|---|
committer | Xin Li <delphij@google.com> | 2024-01-17 22:14:14 -0800 |
commit | f16b249a5e346bd673ba312f71c55f02c6d4a476 (patch) | |
tree | a508cb65119b65346d5fdef28bec836659e434b5 | |
parent | 6734490de113dfea88f604496ffb4a149e5d0e1c (diff) | |
parent | b0eb37f8d49caa86f61fee461addb20503914947 (diff) | |
download | gchips-f16b249a5e346bd673ba312f71c55f02c6d4a476.tar.gz |
Merge Android 24Q1 Release (ab/11220357)temp_319669529
Bug: 319669529
Merged-In: Ia09a914932b585ebba0b38fbfebdf75d6d9a7a76
Change-Id: I4216757468b9304fb354a44a9555b53d2e3ecb2c
44 files changed, 1113 insertions, 924 deletions
diff --git a/gralloc4/interfaces/libs/drmutils/Android.bp b/gralloc4/interfaces/libs/drmutils/Android.bp index 82ba768..cff9a7e 100644 --- a/gralloc4/interfaces/libs/drmutils/Android.bp +++ b/gralloc4/interfaces/libs/drmutils/Android.bp @@ -21,6 +21,7 @@ package { cc_library_static { name: "libgralloc_drmutils", + defaults: ["android.hardware.graphics.common-ndk_shared"], cflags: [ "-Wall", "-Werror", @@ -33,7 +34,6 @@ cc_library_static { "liblog", "libdrm", "libcutils", - "android.hardware.graphics.common-V4-ndk", ], header_libs: [ "libgralloc_headers", diff --git a/gralloc4/service/aidl/Android.bp b/gralloc4/service/aidl/Android.bp index a3d7a87..ea6e461 100644 --- a/gralloc4/service/aidl/Android.bp +++ b/gralloc4/service/aidl/Android.bp @@ -3,7 +3,7 @@ package { } cc_binary { - name: "android.hardware.graphics.allocator-V1-service", + name: "android.hardware.graphics.allocator-V2-service", proprietary: true, relative_install_path: "hw", srcs: [ @@ -17,7 +17,7 @@ cc_binary { "libgralloc_headers", ], shared_libs: [ - "android.hardware.graphics.allocator-V1-ndk", + "android.hardware.graphics.allocator-V2-ndk", "android.hardware.graphics.allocator-aidl-impl", "libbinder_ndk", "liblog", diff --git a/gralloc4/service/aidl/android.hardware.graphics.allocator-aidl-service.rc b/gralloc4/service/aidl/android.hardware.graphics.allocator-aidl-service.rc index e86b68d..723fab6 100644 --- a/gralloc4/service/aidl/android.hardware.graphics.allocator-aidl-service.rc +++ b/gralloc4/service/aidl/android.hardware.graphics.allocator-aidl-service.rc @@ -1,4 +1,4 @@ -service vendor.graphics.allocator-default /vendor/bin/hw/android.hardware.graphics.allocator-V1-service +service vendor.graphics.allocator-default /vendor/bin/hw/android.hardware.graphics.allocator-V2-service class hal animation user system group graphics drmrpc diff --git a/gralloc4/service/aidl/manifest_gralloc_aidl.xml b/gralloc4/service/aidl/manifest_gralloc_aidl.xml index 6848a99..c29d370 100644 --- a/gralloc4/service/aidl/manifest_gralloc_aidl.xml +++ b/gralloc4/service/aidl/manifest_gralloc_aidl.xml @@ -1,6 +1,7 @@ <manifest version="1.0" type="device"> <hal format="aidl"> <name>android.hardware.graphics.allocator</name> + <version>2</version> <fqname>IAllocator/default</fqname> </hal> </manifest> diff --git a/gralloc4/src/4.x/Android.bp b/gralloc4/src/4.x/Android.bp index 16eec6a..fb2363b 100644 --- a/gralloc4/src/4.x/Android.bp +++ b/gralloc4/src/4.x/Android.bp @@ -92,4 +92,7 @@ cc_library_shared { include_dirs: [ "hardware/google/gchips/include", ], + cflags: [ + "-DGRALLOC_MAPPER_4=1", + ], } diff --git a/gralloc4/src/4.x/GrallocMapper.cpp b/gralloc4/src/4.x/GrallocMapper.cpp index c0b5f5f..11ee90b 100644 --- a/gralloc4/src/4.x/GrallocMapper.cpp +++ b/gralloc4/src/4.x/GrallocMapper.cpp @@ -18,6 +18,7 @@ #include "GrallocMapper.h" #include "hidl_common/BufferDescriptor.h" #include "hidl_common/MapperMetadata.h" +#include "hidl_common/Mapper.h" #include "allocator/mali_gralloc_ion.h" @@ -26,7 +27,9 @@ namespace arm namespace mapper { +namespace hidl { using android::hardware::graphics::mapper::V4_0::Error; +} // namespace hidl using android::hardware::graphics::mapper::V4_0::BufferDescriptor; using android::hardware::graphics::mapper::V4_0::IMapper; using android::hardware::Return; @@ -34,7 +37,6 @@ using android::hardware::hidl_handle; using android::hardware::hidl_vec; using android::hardware::Void; - GrallocMapper::GrallocMapper() {} GrallocMapper::~GrallocMapper() {} @@ -43,12 +45,12 @@ Return<void> GrallocMapper::createDescriptor(const BufferDescriptorInfo &descrip { if (common::validateDescriptorInfo(descriptorInfo)) { - hidl_cb(Error::NONE, common::grallocEncodeBufferDescriptor<uint8_t>(descriptorInfo)); + hidl_cb(hidl::Error::NONE, common::grallocEncodeBufferDescriptor<uint8_t>(descriptorInfo)); } else { MALI_GRALLOC_LOGE("Invalid attributes to create descriptor for Mapper 3.0"); - hidl_cb(Error::BAD_VALUE, BufferDescriptor()); + hidl_cb(hidl::Error::BAD_VALUE, BufferDescriptor()); } return Void(); @@ -56,73 +58,156 @@ Return<void> GrallocMapper::createDescriptor(const BufferDescriptorInfo &descrip Return<void> GrallocMapper::importBuffer(const hidl_handle &rawHandle, importBuffer_cb hidl_cb) { - common::importBuffer(rawHandle, hidl_cb); + if (!rawHandle.getNativeHandle()) { + hidl_cb(hidl::Error::BAD_BUFFER, nullptr); + return Void(); + } + + auto *inHandle = const_cast<native_handle_t *>(rawHandle.getNativeHandle()); + buffer_handle_t outHandle = nullptr; + + hidl::Error err = static_cast<hidl::Error>( + common::importBuffer(inHandle, &outHandle)); + + hidl_cb(err, static_cast<void*>(const_cast<native_handle_t*>(outHandle))); return Void(); } -Return<Error> GrallocMapper::freeBuffer(void *buffer) +Return<hidl::Error> GrallocMapper::freeBuffer(void *buffer) { - return common::freeBuffer(buffer); + buffer_handle_t handle = common::getBuffer(buffer); + if (handle == nullptr) return hidl::Error::BAD_BUFFER; + return static_cast<hidl::Error>(common::freeBuffer(handle)); } -Return<Error> GrallocMapper::validateBufferSize(void *buffer, const BufferDescriptorInfo &descriptorInfo, +Return<hidl::Error> GrallocMapper::validateBufferSize(void *buffer, const BufferDescriptorInfo &descriptorInfo, uint32_t in_stride) { /* All Gralloc allocated buffers must be conform to local descriptor validation */ if (!common::validateDescriptorInfo<BufferDescriptorInfo>(descriptorInfo)) { MALI_GRALLOC_LOGE("Invalid descriptor attributes for validating buffer size"); - return Error::BAD_VALUE; + return hidl::Error::BAD_VALUE; } - return common::validateBufferSize(buffer, descriptorInfo, in_stride); + return static_cast<hidl::Error>(common::validateBufferSize(buffer, descriptorInfo, in_stride)); +} + +static bool getFenceFd(const hidl_handle &fenceHandle, int *outFenceFd) { + auto const handle = fenceHandle.getNativeHandle(); + if (handle && handle->numFds > 1) { + MALI_GRALLOC_LOGE("Invalid fence handle with %d fds", + handle->numFds); + return false; + } + + *outFenceFd = (handle && handle->numFds == 1) ? handle->data[0] : -1; + return true; } Return<void> GrallocMapper::lock(void *buffer, uint64_t cpuUsage, const IMapper::Rect &accessRegion, const hidl_handle &acquireFence, lock_cb hidl_cb) { - common::lock(buffer, cpuUsage, accessRegion, acquireFence, hidl_cb); + void *outData = nullptr; + int fenceFd; + if (!getFenceFd(acquireFence, &fenceFd)) { + hidl_cb(hidl::Error::BAD_VALUE, nullptr); + return Void(); + } + + hidl::Error err = static_cast<hidl::Error>( + common::lock(static_cast<buffer_handle_t>(buffer), cpuUsage, + common::GrallocRect(accessRegion), fenceFd, &outData)); + if (err != hidl::Error::NONE) outData = nullptr; + hidl_cb(err, outData); return Void(); } +/* + * Populates the HIDL fence handle for the given fence object + * + * @param fenceFd [in] Fence file descriptor + * @param handleStorage [in] HIDL handle storage for fence + * + * @return HIDL fence handle + */ +static hidl_handle buildFenceHandle(int fenceFd, char *handleStorage) { + native_handle_t *handle = nullptr; + if (fenceFd >= 0) { + handle = native_handle_init(handleStorage, 1, 0); + handle->data[0] = fenceFd; + } + + return hidl_handle(handle); +} + Return<void> GrallocMapper::unlock(void *buffer, unlock_cb hidl_cb) { - common::unlock(buffer, hidl_cb); + int fenceFd = -1; + const native_handle_t *handle = common::getBuffer(buffer); + + if (handle == nullptr) { + hidl_cb(hidl::Error::BAD_BUFFER, nullptr); + } + + hidl::Error err = + static_cast<hidl::Error>(common::unlock(handle, &fenceFd)); + if (err == hidl::Error::NONE) { + NATIVE_HANDLE_DECLARE_STORAGE(fenceStorage, 1, 0); + hidl_cb(err, buildFenceHandle(fenceFd, fenceStorage)); + + if (fenceFd >= 0) { + close(fenceFd); + } + } else { + hidl_cb(err, nullptr); + } return Void(); } Return<void> GrallocMapper::flushLockedBuffer(void *buffer, flushLockedBuffer_cb hidl_cb) { - common::flushLockedBuffer(buffer, hidl_cb); + hidl::Error err = static_cast<hidl::Error>(common::flushLockedBuffer(static_cast<buffer_handle_t>(buffer))); + hidl_cb(err, hidl_handle{}); return Void(); } -Return<Error> GrallocMapper::rereadLockedBuffer(void *buffer) +Return<hidl::Error> GrallocMapper::rereadLockedBuffer(void *buffer) { - return common::rereadLockedBuffer(buffer); + return static_cast<hidl::Error>(common::rereadLockedBuffer(common::getBuffer(buffer))); } Return<void> GrallocMapper::get(void *buffer, const MetadataType &metadataType, IMapper::get_cb hidl_cb) { - common::get(buffer, metadataType, hidl_cb); + std::vector<uint8_t> vec; + hidl::Error err = static_cast<hidl::Error>( + common::get(common::getBuffer(buffer), common::MetadataType(metadataType), vec)); + hidl_cb(err, hidl_vec(vec)); return Void(); } -Return<Error> GrallocMapper::set(void *buffer, const MetadataType &metadataType, const hidl_vec<uint8_t> &metadata) +Return<hidl::Error> GrallocMapper::set(void *buffer, const MetadataType &metadataType, const hidl_vec<uint8_t> &metadata) { - return common::set(buffer, metadataType, metadata); + buffer_handle_t bufferHandle = common::getBuffer(buffer); + return static_cast<hidl::Error>(common::set(bufferHandle, common::MetadataType(metadataType), metadata)); } Return<void> GrallocMapper::getFromBufferDescriptorInfo(const BufferDescriptorInfo &description, const MetadataType &metadataType, getFromBufferDescriptorInfo_cb hidl_cb) { - common::getFromBufferDescriptorInfo(description, metadataType, hidl_cb); + std::vector<uint8_t> vec; + hidl::Error err = static_cast<hidl::Error>(common::getFromBufferDescriptorInfo( + description, common::MetadataType(metadataType), vec)); + hidl_cb(err, vec); return Void(); } Return<void> GrallocMapper::getTransportSize(void *buffer, getTransportSize_cb hidl_cb) { - common::getTransportSize(buffer, hidl_cb); + uint32_t outNumFds = 0, outNumInts = 0; + buffer_handle_t bufferHandle = common::getBuffer(buffer); + hidl::Error err = static_cast<hidl::Error>(common::getTransportSize(bufferHandle, &outNumFds, &outNumInts)); + hidl_cb(err, outNumFds, outNumInts); return Void(); } @@ -131,33 +216,52 @@ Return<void> GrallocMapper::isSupported(const IMapper::BufferDescriptorInfo &des if (!common::validateDescriptorInfo<BufferDescriptorInfo>(description)) { MALI_GRALLOC_LOGE("Invalid descriptor attributes for validating buffer size"); - hidl_cb(Error::BAD_VALUE, false); + hidl_cb(hidl::Error::BAD_VALUE, false); } - common::isSupported(description, hidl_cb); + hidl_cb(hidl::Error::NONE, common::isSupported(description)); return Void(); } Return<void> GrallocMapper::listSupportedMetadataTypes(listSupportedMetadataTypes_cb hidl_cb) { - common::listSupportedMetadataTypes(hidl_cb); + std::vector<common::MetadataTypeDescription> desc = common::listSupportedMetadataTypes(); + std::vector<IMapper::MetadataTypeDescription> hidl_description(desc.size()); + std::copy(desc.begin(), desc.end(), hidl_description.begin()); + hidl_cb(hidl::Error::NONE, hidl_vec(hidl_description)); return Void(); } Return<void> GrallocMapper::dumpBuffer(void *buffer, dumpBuffer_cb hidl_cb) { - common::dumpBuffer(buffer, hidl_cb); + common::BufferDump out; + hidl::Error err = static_cast<hidl::Error>(common::dumpBuffer(common::getBuffer(buffer), out)); + hidl_cb(err, static_cast<IMapper::BufferDump>(out)); return Void(); } Return<void> GrallocMapper::dumpBuffers(dumpBuffers_cb hidl_cb) { - common::dumpBuffers(hidl_cb); + auto bufferDump = common::dumpBuffers(); + std::vector<IMapper::BufferDump> outBufDump; + for (auto dump : bufferDump) { + outBufDump.push_back(static_cast<IMapper::BufferDump>(dump)); + } + hidl_cb(hidl::Error::NONE, hidl_vec(outBufDump)); return Void(); } Return<void> GrallocMapper::getReservedRegion(void *buffer, getReservedRegion_cb hidl_cb) { - common::getReservedRegion(buffer, hidl_cb); + void *reservedRegion = nullptr; + uint64_t reservedSize = 0; + hidl::Error err = static_cast<hidl::Error>( + common::getReservedRegion(static_cast<buffer_handle_t>(buffer), + &reservedRegion, reservedSize)); + if (err != hidl::Error::NONE) { + reservedRegion = nullptr; + reservedSize = 0; + } + hidl_cb(err, reservedRegion, reservedSize); return Void(); } diff --git a/gralloc4/src/4.x/GrallocMapper.h b/gralloc4/src/4.x/GrallocMapper.h index 959efa5..7c45199 100644 --- a/gralloc4/src/4.x/GrallocMapper.h +++ b/gralloc4/src/4.x/GrallocMapper.h @@ -50,9 +50,9 @@ public: Return<void> importBuffer(const hidl_handle &rawHandle, importBuffer_cb hidl_cb) override; - Return<Error> freeBuffer(void *buffer) override; + Return<hidl::Error> freeBuffer(void *buffer) override; - Return<Error> validateBufferSize(void *buffer, const IMapper::BufferDescriptorInfo &descriptorInfo, + Return<hidl::Error> validateBufferSize(void *buffer, const IMapper::BufferDescriptorInfo &descriptorInfo, uint32_t stride) override; Return<void> getTransportSize(void *buffer, getTransportSize_cb _hidl_cb) override; @@ -64,13 +64,13 @@ public: Return<void> flushLockedBuffer(void *buffer, flushLockedBuffer_cb hidl_cb) override; - Return<Error> rereadLockedBuffer(void *buffer) override; + Return<hidl::Error> rereadLockedBuffer(void *buffer) override; Return<void> isSupported(const IMapper::BufferDescriptorInfo &description, isSupported_cb hidl_cb) override; Return<void> get(void *buffer, const MetadataType &metadataType, IMapper::get_cb hidl_cb) override; - Return<Error> set(void *buffer, const MetadataType &metadataType, const hidl_vec<uint8_t> &metadata) override; + Return<hidl::Error> set(void *buffer, const MetadataType &metadataType, const hidl_vec<uint8_t> &metadata) override; Return<void> getFromBufferDescriptorInfo(BufferDescriptorInfo const &description, MetadataType const &metadataType, getFromBufferDescriptorInfo_cb hidl_cb) override; diff --git a/gralloc4/src/4.x/gralloc_mapper_hidl_header.h b/gralloc4/src/4.x/gralloc_mapper_hidl_header.h deleted file mode 100644 index 6d3d549..0000000 --- a/gralloc4/src/4.x/gralloc_mapper_hidl_header.h +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright (C) 2020 Arm Limited. All rights reserved. - * - * Copyright 2016 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef GRALLOC_MAPPER_HIDL_HEADER_H -#define GRALLOC_MAPPER_HIDL_HEADER_H - -#include <android/hardware/graphics/mapper/4.0/IMapper.h> -#include "mali_fourcc.h" -#include <gralloctypes/Gralloc4.h> - -using android::hardware::graphics::mapper::V4_0::Error; -using android::hardware::graphics::mapper::V4_0::BufferDescriptor; -using android::hardware::graphics::common::V1_2::PixelFormat; -using android::hardware::graphics::common::V1_2::BufferUsage; -using android::hardware::graphics::mapper::V4_0::IMapper; - -#endif diff --git a/gralloc4/src/Android.bp b/gralloc4/src/Android.bp index 5f89832..1f640ea 100644 --- a/gralloc4/src/Android.bp +++ b/gralloc4/src/Android.bp @@ -1,4 +1,3 @@ - /* * Copyright (C) 2020 Arm Limited. * SPDX-License-Identifier: Apache-2.0 @@ -21,62 +20,16 @@ package { } cc_library_headers { - name: "libgralloc_headers", - vendor: true, - host_supported: true, - export_include_dirs: [ - ".", - ], - header_libs: [ - "libsystem_headers", - ], - export_header_lib_headers: [ - "libsystem_headers", - ], -} - -cc_library_shared { - name: "libGralloc4Wrapper", - vendor: true, - defaults: [ - "arm_gralloc_defaults", - ], - srcs: [ - "libGralloc4Wrapper/wrapper.cpp", - "allocator/mali_gralloc_ion.cpp", - "core/format_info.cpp", - "core/mali_gralloc_formats.cpp", - "core/mali_gralloc_bufferallocation.cpp", - "core/mali_gralloc_bufferdescriptor.cpp", - "core/mali_gralloc_reference.cpp", - ":libgralloc_hidl_common_shared_metadata", - ], - cflags: [ - "-DGRALLOC_LIBRARY_BUILD=1", - "-Wthread-safety", - ], - static_libs: [ - "libgralloc_capabilities", - ], - shared_libs: [ - "liblog", - "libcutils", - "libutils", - "libsync", - "libhardware", - "libhidlbase", - "libhidltransport", - "libnativewindow", - "android.hardware.graphics.common@1.2", - "android.hardware.graphics.common-V4-ndk", - "android.hardware.graphics.mapper@4.0", - "libdmabufheap", - "libgralloctypes", - "libdrm", - ], - header_libs: [ - "google_hal_headers", - "device_kernel_headers", - ], - export_include_dirs: ["libGralloc4Wrapper/include"] + name: "libgralloc_headers", + vendor: true, + export_include_dirs: [ + ".", + ], + header_libs: [ + "libsystem_headers", + "//hardware/google/graphics/common:pixel-gralloc-headers", + ], + export_header_lib_headers: [ + "libsystem_headers", + ], } diff --git a/gralloc4/src/aidl/Android.bp b/gralloc4/src/aidl/Android.bp index e2d9d04..3c0fc26 100644 --- a/gralloc4/src/aidl/Android.bp +++ b/gralloc4/src/aidl/Android.bp @@ -10,7 +10,7 @@ cc_library_shared { "arm_gralloc_api_4x_defaults", ], shared_libs: [ - "android.hardware.graphics.allocator-V1-ndk", + "android.hardware.graphics.allocator-V2-ndk", "android.hardware.graphics.allocator@4.0", "android.hardware.graphics.mapper@4.0", "libbinder_ndk", diff --git a/gralloc4/src/aidl/GrallocAllocator.cpp b/gralloc4/src/aidl/GrallocAllocator.cpp index 6945505..2b01b80 100644 --- a/gralloc4/src/aidl/GrallocAllocator.cpp +++ b/gralloc4/src/aidl/GrallocAllocator.cpp @@ -4,9 +4,9 @@ #include <aidlcommonsupport/NativeHandle.h> #include <android/binder_ibinder.h> #include <android/binder_status.h> +#include <cutils/android_filesystem_config.h> #include <hidl/HidlSupport.h> -#include "allocator/mali_gralloc_ion.h" #include "hidl_common/Allocator.h" namespace pixel::allocator { @@ -22,6 +22,10 @@ unsigned long callingPid() { return static_cast<unsigned long>(AIBinder_getCallingPid()); } +unsigned long callingUid() { + return static_cast<unsigned long>(AIBinder_getCallingUid()); +} + GrallocAllocator::GrallocAllocator() {} GrallocAllocator::~GrallocAllocator() {} @@ -83,4 +87,111 @@ ndk::ScopedAStatus GrallocAllocator::allocate(const std::vector<uint8_t>& descri return ndk::ScopedAStatus::ok(); } +buffer_descriptor_t decodeBufferDescriptorInfo( + const AidlAllocator::BufferDescriptorInfo& descriptor) { + buffer_descriptor_t bufferDescriptor; + bufferDescriptor.width = descriptor.width; + bufferDescriptor.height = descriptor.height; + bufferDescriptor.layer_count = descriptor.layerCount; + bufferDescriptor.hal_format = static_cast<uint64_t>(descriptor.format); + bufferDescriptor.producer_usage = static_cast<uint64_t>(descriptor.usage); + bufferDescriptor.consumer_usage = bufferDescriptor.producer_usage; + bufferDescriptor.format_type = MALI_GRALLOC_FORMAT_TYPE_USAGE; + bufferDescriptor.signature = sizeof(buffer_descriptor_t); + bufferDescriptor.reserved_size = descriptor.reservedSize; + const char *str = (const char*) descriptor.name.data(); + bufferDescriptor.name = std::string(str); + return bufferDescriptor; +} + +ndk::ScopedAStatus GrallocAllocator::allocate2( + const AidlAllocator::BufferDescriptorInfo& descriptor, int32_t count, + AidlAllocator::AllocationResult* result) { + MALI_GRALLOC_LOGV("Allocation request from process: %lu", callingPid()); + + buffer_descriptor_t bufferDescriptor = decodeBufferDescriptorInfo(descriptor); + + HidlError error = HidlError::NONE; + auto hidl_cb = [&](HidlError _error, int _stride, hidl_vec<hidl_handle> _buffers) { + if (_error != HidlError::NONE) { + error = _error; + return; + } + + const uint32_t size = _buffers.size(); + + result->stride = _stride; + result->buffers.resize(size); + for (uint32_t i = 0; i < size; i++) { + // Dup here is necessary. After this callback returns common::allocate + // will free the buffer which will destroy the older fd. + result->buffers[i] = android::dupToAidl(static_cast<const native_handle*>(_buffers[i])); + } + }; + + arm::allocator::common::allocate(bufferDescriptor, count, hidl_cb); + + switch (error) { + case HidlError::NONE: + break; + + case HidlError::BAD_DESCRIPTOR: + return ndk::ScopedAStatus::fromServiceSpecificError( + static_cast<int32_t>(AidlAllocator::AllocationError::BAD_DESCRIPTOR)); + + case HidlError::NO_RESOURCES: + return ndk::ScopedAStatus::fromServiceSpecificError( + static_cast<int32_t>(AidlAllocator::AllocationError::NO_RESOURCES)); + + case HidlError::UNSUPPORTED: + return ndk::ScopedAStatus::fromServiceSpecificError( + static_cast<int32_t>(AidlAllocator::AllocationError::UNSUPPORTED)); + + default: + return ndk::ScopedAStatus::fromStatus(STATUS_UNKNOWN_ERROR); + } + + return ndk::ScopedAStatus::ok(); +} + +// TODO(b/315883761): isSupported should return false for unknown-to-HAL usage +ndk::ScopedAStatus GrallocAllocator::isSupported( + const AidlAllocator::BufferDescriptorInfo& descriptor, bool* result) { + buffer_descriptor_t bufferDescriptor = decodeBufferDescriptorInfo(descriptor); + + int isBufferDescriptorSupported = arm::allocator::common::isSupported(&bufferDescriptor); + *result = (isBufferDescriptorSupported == 0); + + if (isBufferDescriptorSupported) { + MALI_GRALLOC_LOGV("Allocation for the given description will not succeed. error %d", + isBufferDescriptorSupported); + return ndk::ScopedAStatus::fromServiceSpecificError( + static_cast<int32_t>(AidlAllocator::AllocationError::UNSUPPORTED)); + } + return ndk::ScopedAStatus::ok(); +} + +ndk::ScopedAStatus GrallocAllocator::getIMapperLibrarySuffix(std::string* result) { + *result = ""; + return ndk::ScopedAStatus::fromStatus(STATUS_UNKNOWN_ERROR); +} + +binder_status_t GrallocAllocator::dump(int fd, const char** /* args */, uint32_t numArgs) { + if (callingUid() != AID_ROOT) { + const std::string permission_denied = "Permission Denied\n"; + write(fd, permission_denied.c_str(), permission_denied.size()); + return STATUS_PERMISSION_DENIED; + } + + if (numArgs != 0) { + const std::string argument_error = "No argument expected\n"; + write(fd, argument_error.c_str(), argument_error.size()); + return STATUS_BAD_VALUE; + } + + const std::string dump_info = arm::allocator::common::dump(); + write(fd, dump_info.c_str(), dump_info.size()); + return STATUS_OK; +} + } // namespace pixel::allocator diff --git a/gralloc4/src/aidl/GrallocAllocator.h b/gralloc4/src/aidl/GrallocAllocator.h index 82d4657..91655a7 100644 --- a/gralloc4/src/aidl/GrallocAllocator.h +++ b/gralloc4/src/aidl/GrallocAllocator.h @@ -2,9 +2,11 @@ #include <aidl/android/hardware/graphics/allocator/AllocationResult.h> #include <aidl/android/hardware/graphics/allocator/BnAllocator.h> +#include <aidl/android/hardware/graphics/allocator/BufferDescriptorInfo.h> #include <aidlcommonsupport/NativeHandle.h> #include <cstdint> +#include <string> #include <vector> namespace pixel { @@ -20,6 +22,17 @@ public: virtual ndk::ScopedAStatus allocate(const std::vector<uint8_t>& descriptor, int32_t count, AidlAllocator::AllocationResult* result) override; + + virtual ndk::ScopedAStatus allocate2(const AidlAllocator::BufferDescriptorInfo& descriptor, + int32_t count, + AidlAllocator::AllocationResult* result) override; + + virtual ndk::ScopedAStatus isSupported(const AidlAllocator::BufferDescriptorInfo& descriptor, + bool* result) override; + + virtual ndk::ScopedAStatus getIMapperLibrarySuffix(std::string* result) override; + + virtual binder_status_t dump(int fd, const char** args, uint32_t numArgs) override; }; } // namespace allocator diff --git a/gralloc4/src/allocator/Android.bp b/gralloc4/src/allocator/Android.bp index 630735b..3170c38 100644 --- a/gralloc4/src/allocator/Android.bp +++ b/gralloc4/src/allocator/Android.bp @@ -51,6 +51,7 @@ arm_gralloc_allocator_cc_defaults { name: "arm_gralloc_allocator_defaults", defaults: [ "arm_gralloc_defaults", + "android.hardware.graphics.common-ndk_shared", ], soong_config_variables: { gralloc_use_ion_dma_heap: { @@ -88,7 +89,6 @@ arm_gralloc_allocator_cc_defaults { "libsync", "libutils", "libnativewindow", - "android.hardware.graphics.common-V4-ndk", ], header_libs: [ "libnativebase_headers", diff --git a/gralloc4/src/allocator/mali_gralloc_ion.cpp b/gralloc4/src/allocator/mali_gralloc_ion.cpp index 986af11..3a955d9 100644 --- a/gralloc4/src/allocator/mali_gralloc_ion.cpp +++ b/gralloc4/src/allocator/mali_gralloc_ion.cpp @@ -55,6 +55,7 @@ static const char kDmabufSensorDirectHeapName[] = "sensor_direct_heap"; static const char kDmabufFaceauthTpuHeapName[] = "faceauth_tpu-secure"; static const char kDmabufFaceauthImgHeapName[] = "faimg-secure"; static const char kDmabufFaceauthRawImgHeapName[] = "farawimg-secure"; +static const char kDmabufFaceauthEvalHeapName[] = "faeval-secure"; static const char kDmabufFaceauthPrevHeapName[] = "faprev-secure"; static const char kDmabufFaceauthModelHeapName[] = "famodel-secure"; static const char kDmabufVframeSecureHeapName[] = "vframe-secure"; @@ -87,9 +88,14 @@ std::string select_dmabuf_heap(uint64_t usage) std::string name; }; - static const std::array<HeapSpecifier, 7> exact_usage_heaps = + static const std::array<HeapSpecifier, 8> exact_usage_heaps = {{ // Faceauth heaps + { // faceauth_evaluation_heap - used mostly on debug builds + GRALLOC_USAGE_PROTECTED | GRALLOC_USAGE_HW_CAMERA_WRITE | GRALLOC_USAGE_HW_CAMERA_READ | + GS101_GRALLOC_USAGE_FACEAUTH_RAW_EVAL, + kDmabufFaceauthEvalHeapName + }, { // isp_image_heap GRALLOC_USAGE_PROTECTED | GRALLOC_USAGE_HW_CAMERA_WRITE | GS101_GRALLOC_USAGE_TPU_INPUT, kDmabufFaceauthImgHeapName @@ -200,18 +206,22 @@ std::string select_dmabuf_heap(uint64_t usage) return ""; } -int alloc_from_dmabuf_heap(uint64_t usage, size_t size, const std::string& buffer_name = "") +int alloc_from_dmabuf_heap(uint64_t usage, size_t size, const std::string& buffer_name = "", bool use_placeholder = false) { ATRACE_CALL(); if (size == 0) { return -1; } - auto heap_name = select_dmabuf_heap(usage); + auto heap_name = use_placeholder ? "system" : select_dmabuf_heap(usage); + if (use_placeholder) size = 1; + if (heap_name.empty()) { MALI_GRALLOC_LOGW("No heap found for usage: %s (0x%" PRIx64 ")", describe_usage(usage).c_str(), usage); return -EINVAL; } - ATRACE_NAME(("alloc_from_dmabuf_heap " + heap_name).c_str()); + std::stringstream tag; + tag << "heap: " << heap_name << ", bytes: " << size; + ATRACE_NAME(tag.str().c_str()); int shared_fd = get_allocator().Alloc(heap_name, size, 0); if (shared_fd < 0) { @@ -350,7 +360,7 @@ int mali_gralloc_ion_allocate_attr(private_handle_t *hnd) */ int mali_gralloc_ion_allocate(const gralloc_buffer_descriptor_t *descriptors, uint32_t numDescriptors, buffer_handle_t *pHandle, - bool *shared_backend, int ion_fd) + bool *shared_backend, bool use_placeholder) { ATRACE_CALL(); GRALLOC_UNUSED(shared_backend); @@ -394,11 +404,7 @@ int mali_gralloc_ion_allocate(const gralloc_buffer_descriptor_t *descriptors, { int& fd = hnd->fds[fidx]; - if (ion_fd >= 0 && fidx == 0) { - fd = ion_fd; - } else { - fd = alloc_from_dmabuf_heap(usage, bufDescriptor->alloc_sizes[fidx], bufDescriptor->name); - } + fd = alloc_from_dmabuf_heap(usage, bufDescriptor->alloc_sizes[fidx], bufDescriptor->name, use_placeholder); if (fd < 0) { @@ -411,6 +417,8 @@ int mali_gralloc_ion_allocate(const gralloc_buffer_descriptor_t *descriptors, } } + if (use_placeholder) return 0; + #if defined(GRALLOC_INIT_AFBC) && (GRALLOC_INIT_AFBC == 1) ATRACE_NAME("AFBC init block"); unsigned char *cpu_ptr = NULL; diff --git a/gralloc4/src/allocator/mali_gralloc_ion.h b/gralloc4/src/allocator/mali_gralloc_ion.h index d826650..06d240b 100644 --- a/gralloc4/src/allocator/mali_gralloc_ion.h +++ b/gralloc4/src/allocator/mali_gralloc_ion.h @@ -24,7 +24,7 @@ int mali_gralloc_ion_allocate_attr(private_handle_t *hnd); int mali_gralloc_ion_allocate(const gralloc_buffer_descriptor_t *descriptors, uint32_t numDescriptors, buffer_handle_t *pHandle, bool *alloc_from_backing_store, - int ion_fd = -1); + bool use_placeholder = false); void mali_gralloc_ion_free(private_handle_t * const hnd); int mali_gralloc_ion_sync_start(const private_handle_t * const hnd, const bool read, const bool write); diff --git a/gralloc4/src/capabilities/Android.bp b/gralloc4/src/capabilities/Android.bp index 2b25811..46d3376 100644 --- a/gralloc4/src/capabilities/Android.bp +++ b/gralloc4/src/capabilities/Android.bp @@ -67,6 +67,7 @@ arm_gralloc_capabilities_cc_defaults { name: "arm_gralloc_capabilities_defaults", defaults: [ "arm_gralloc_defaults", + "android.hardware.graphics.common-ndk_shared", ], soong_config_variables: { mali_gpu_support_afbc_basic: { @@ -119,7 +120,6 @@ arm_gralloc_capabilities_cc_defaults { "libcutils", "libsync", "libutils", - "android.hardware.graphics.common-V4-ndk", ], } diff --git a/gralloc4/src/core/Android.bp b/gralloc4/src/core/Android.bp index b2e5ca3..a6ee46e 100644 --- a/gralloc4/src/core/Android.bp +++ b/gralloc4/src/core/Android.bp @@ -39,6 +39,7 @@ arm_gralloc_core_cc_defaults { name: "arm_gralloc_core_defaults", defaults: [ "arm_gralloc_defaults", + "android.hardware.graphics.common-ndk_shared", ], soong_config_variables: { gralloc_ion_sync_on_lock: { @@ -69,7 +70,6 @@ arm_gralloc_core_cc_defaults { "libcutils", "libutils", "android.hardware.graphics.common@1.2", - "android.hardware.graphics.common-V4-ndk", ], target: { android: { diff --git a/gralloc4/src/core/format_info.cpp b/gralloc4/src/core/format_info.cpp index 14a1d84..b9ddbf2 100644 --- a/gralloc4/src/core/format_info.cpp +++ b/gralloc4/src/core/format_info.cpp @@ -156,7 +156,7 @@ const format_ip_support_t formats_ip_support[] = { { .id = MALI_GRALLOC_FORMAT_INTERNAL_Y410, .cpu_rd = F_LIN, .cpu_wr = F_LIN, .gpu_rd = F_LIN, .gpu_wr = F_LIN|F_AFBC, .dpu_rd = F_NONE, .dpu_wr = F_NONE, .dpu_aeu_wr = F_NONE, .vpu_rd = F_NONE, .vpu_wr = F_NONE, .cam_wr = F_NONE, }, /* Other */ { .id = MALI_GRALLOC_FORMAT_INTERNAL_RAW16, .cpu_rd = F_LIN, .cpu_wr = F_LIN, .gpu_rd = F_LIN, .gpu_wr = F_NONE, .dpu_rd = F_NONE, .dpu_wr = F_NONE, .dpu_aeu_wr = F_NONE, .vpu_rd = F_NONE, .vpu_wr = F_NONE, .cam_wr = F_LIN, }, - { .id = MALI_GRALLOC_FORMAT_INTERNAL_RAW12, .cpu_rd = F_LIN, .cpu_wr = F_LIN, .gpu_rd = F_NONE, .gpu_wr = F_NONE, .dpu_rd = F_NONE, .dpu_wr = F_NONE, .dpu_aeu_wr = F_NONE, .vpu_rd = F_NONE, .vpu_wr = F_NONE, .cam_wr = F_LIN, }, + { .id = MALI_GRALLOC_FORMAT_INTERNAL_RAW12, .cpu_rd = F_LIN, .cpu_wr = F_LIN, .gpu_rd = F_LIN, .gpu_wr = F_NONE, .dpu_rd = F_NONE, .dpu_wr = F_NONE, .dpu_aeu_wr = F_NONE, .vpu_rd = F_NONE, .vpu_wr = F_NONE, .cam_wr = F_LIN, }, { .id = MALI_GRALLOC_FORMAT_INTERNAL_RAW10, .cpu_rd = F_LIN, .cpu_wr = F_LIN, .gpu_rd = F_LIN, .gpu_wr = F_NONE, .dpu_rd = F_NONE, .dpu_wr = F_NONE, .dpu_aeu_wr = F_NONE, .vpu_rd = F_NONE, .vpu_wr = F_NONE, .cam_wr = F_LIN, }, { .id = MALI_GRALLOC_FORMAT_INTERNAL_BLOB, .cpu_rd = F_LIN, .cpu_wr = F_LIN, .gpu_rd = F_LIN, .gpu_wr = F_LIN, .dpu_rd = F_NONE, .dpu_wr = F_NONE, .dpu_aeu_wr = F_NONE, .vpu_rd = F_NONE, .vpu_wr = F_NONE, .cam_wr = F_LIN, }, /* Depth and Stencil */ @@ -238,7 +238,7 @@ static const hal_int_fmt hal_to_internal_format[] = { HAL_PIXEL_FORMAT_BLOB, false, MALI_GRALLOC_FORMAT_INTERNAL_BLOB }, { HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, true, MALI_GRALLOC_FORMAT_INTERNAL_NV12 }, { HAL_PIXEL_FORMAT_YCbCr_420_888, true, MALI_GRALLOC_FORMAT_INTERNAL_NV12 }, - { HAL_PIXEL_FORMAT_RAW_OPAQUE, false, MALI_GRALLOC_FORMAT_INTERNAL_RAW10 }, + { HAL_PIXEL_FORMAT_RAW_OPAQUE, false, MALI_GRALLOC_FORMAT_INTERNAL_UNDEFINED }, { HAL_PIXEL_FORMAT_RAW10, false, MALI_GRALLOC_FORMAT_INTERNAL_RAW10 }, { HAL_PIXEL_FORMAT_RAW12, false, MALI_GRALLOC_FORMAT_INTERNAL_RAW12 }, { HAL_PIXEL_FORMAT_YCbCr_422_888, true, MALI_GRALLOC_FORMAT_INTERNAL_YUV422_8BIT }, diff --git a/gralloc4/src/core/format_info.h b/gralloc4/src/core/format_info.h index ad2db6d..3742a38 100644 --- a/gralloc4/src/core/format_info.h +++ b/gralloc4/src/core/format_info.h @@ -35,8 +35,8 @@ typedef uint8_t format_support_flags; typedef struct { - uint16_t width; - uint16_t height; + uint64_t width; + uint64_t height; } rect_t; constexpr bool is_power2(uint8_t n) diff --git a/gralloc4/src/core/mali_gralloc_bufferallocation.cpp b/gralloc4/src/core/mali_gralloc_bufferallocation.cpp index d67eeb8..54e7f2f 100644 --- a/gralloc4/src/core/mali_gralloc_bufferallocation.cpp +++ b/gralloc4/src/core/mali_gralloc_bufferallocation.cpp @@ -82,11 +82,11 @@ static uint64_t getUniqueId() return id | counter++; } -static void afbc_buffer_align(const bool is_tiled, int *size) +template<typename T> +static void afbc_buffer_align(const bool is_tiled, T *size) { - const uint16_t AFBC_BODY_BUFFER_BYTE_ALIGNMENT = 1024; - - int buffer_byte_alignment = AFBC_BODY_BUFFER_BYTE_ALIGNMENT; + constexpr T AFBC_BODY_BUFFER_BYTE_ALIGNMENT = 1024; + T buffer_byte_alignment = AFBC_BODY_BUFFER_BYTE_ALIGNMENT; if (is_tiled) { @@ -244,13 +244,13 @@ bool get_alloc_type(const uint64_t format_ext, */ void init_afbc(uint8_t *buf, const uint64_t alloc_format, const bool is_multi_plane, - const int w, const int h) + const uint64_t w, const uint64_t h) { ATRACE_CALL(); const bool is_tiled = ((alloc_format & MALI_GRALLOC_INTFMT_AFBC_TILED_HEADERS) == MALI_GRALLOC_INTFMT_AFBC_TILED_HEADERS); const uint32_t n_headers = (w * h) / AFBC_PIXELS_PER_BLOCK; - int body_offset = n_headers * AFBC_HEADER_BUFFER_BYTES_PER_BLOCKENTRY; + uint32_t body_offset = n_headers * AFBC_HEADER_BUFFER_BYTES_PER_BLOCKENTRY; afbc_buffer_align(is_tiled, &body_offset); @@ -266,7 +266,7 @@ void init_afbc(uint8_t *buf, const uint64_t alloc_format, if ((alloc_format & MALI_GRALLOC_INTFMT_AFBC_TILED_HEADERS)) { /* Zero out body_offset for non-subsampled formats. */ - memset(headers[0], 0, sizeof(uint32_t) * 4); + memset(headers[0], 0, sizeof(size_t) * 4); } /* Map base format to AFBC header layout */ @@ -295,24 +295,14 @@ void init_afbc(uint8_t *buf, const uint64_t alloc_format, } } -static int max(int a, int b) -{ - return a > b ? a : b; -} - -static int max(int a, int b, int c) -{ - return c > max(a, b) ? c : max(a, b); -} - /* * Obtain plane allocation dimensions (in pixels). * * NOTE: pixel stride, where defined for format, is * incorporated into allocation dimensions. */ -static void get_pixel_w_h(uint32_t * const width, - uint32_t * const height, +static void get_pixel_w_h(uint64_t * const width, + uint64_t * const height, const format_info_t format, const alloc_type_t alloc_type, const uint8_t plane, @@ -345,14 +335,14 @@ static void get_pixel_w_h(uint32_t * const width, * Pixel alignment (width), * where format stride is stated in pixels. */ - int pixel_align_w = 1, pixel_align_h = 1; + uint32_t pixel_align_w = 1, pixel_align_h = 1; if (has_cpu_usage && is_primary_plane) { pixel_align_w = format.align_w_cpu; } else if (alloc_type.is_afbc()) { -#define HEADER_STRIDE_ALIGN_IN_SUPER_BLOCKS (0) + constexpr uint32_t HEADER_STRIDE_ALIGN_IN_SUPER_BLOCKS = 0; uint32_t num_sb_align = 0; if (alloc_type.is_padded && !format.is_yuv) { @@ -361,7 +351,7 @@ static void get_pixel_w_h(uint32_t * const width, */ num_sb_align = 4; } - pixel_align_w = max(HEADER_STRIDE_ALIGN_IN_SUPER_BLOCKS, num_sb_align) * sb.width; + pixel_align_w = std::max(HEADER_STRIDE_ALIGN_IN_SUPER_BLOCKS, num_sb_align) * sb.width; /* * Determine AFBC tile size when allocating tiled headers. @@ -373,13 +363,13 @@ static void get_pixel_w_h(uint32_t * const width, afbc_tile.height = format.bpp_afbc[plane] > 32 ? 4 * afbc_tile.height : 8 * afbc_tile.height; } - MALI_GRALLOC_LOGV("Plane[%hhu]: [SUB-SAMPLE] w:%d, h:%d\n", plane, *width, *height); + MALI_GRALLOC_LOGV("Plane[%hhu]: [SUB-SAMPLE] w:%" PRIu64 ", h:%" PRIu64 "\n", plane, *width, *height); MALI_GRALLOC_LOGV("Plane[%hhu]: [PIXEL_ALIGN] w:%d\n", plane, pixel_align_w); MALI_GRALLOC_LOGV("Plane[%hhu]: [LINEAR_TILE] w:%" PRIu16 "\n", plane, format.tile_size); - MALI_GRALLOC_LOGV("Plane[%hhu]: [AFBC_TILE] w:%" PRIu16 ", h:%" PRIu16 "\n", plane, afbc_tile.width, afbc_tile.height); + MALI_GRALLOC_LOGV("Plane[%hhu]: [AFBC_TILE] w:%" PRIu64 ", h:%" PRIu64 "\n", plane, afbc_tile.width, afbc_tile.height); - pixel_align_w = max(pixel_align_w, afbc_tile.width); - pixel_align_h = max(pixel_align_h, afbc_tile.height); + pixel_align_w = std::max(static_cast<uint64_t>(pixel_align_w), afbc_tile.width); + pixel_align_h = std::max(static_cast<uint64_t>(pixel_align_h), afbc_tile.height); if (AllocBaseType::AFBC_WIDEBLK == alloc_type.primary_type && !alloc_type.is_tiled) { @@ -391,18 +381,17 @@ static void get_pixel_w_h(uint32_t * const width, * Note that this branch will not be taken for multi-plane AFBC * since that requires tiled headers. */ - pixel_align_h = max(pixel_align_h, 16); + pixel_align_h = std::max(pixel_align_h, 16u); } } - *width = GRALLOC_ALIGN(*width, max(1, pixel_align_w, format.tile_size)); - *height = GRALLOC_ALIGN(*height, max(1, pixel_align_h, format.tile_size)); + *width = GRALLOC_ALIGN(*width, std::max({1u, pixel_align_w, static_cast<uint32_t>(format.tile_size)})); + *height = GRALLOC_ALIGN(*height, std::max({1u, pixel_align_h, static_cast<uint32_t>(format.tile_size)})); } - - -static uint32_t gcd(uint32_t a, uint32_t b) +template<typename T> +static T gcd(T a, T b) { - uint32_t r, t; + T r, t; if (a == b) { @@ -425,14 +414,15 @@ static uint32_t gcd(uint32_t a, uint32_t b) return a; } -uint32_t lcm(uint32_t a, uint32_t b) +template<typename T> +T lcm(T a, T b) { if (a != 0 && b != 0) { return (a * b) / gcd(a, b); } - return max(a, b); + return std::max(a, b); } @@ -447,9 +437,9 @@ uint32_t lcm(uint32_t a, uint32_t b) * constraints, the luma stride must be doubled. */ static void update_yv12_stride(int8_t plane, - uint32_t luma_stride, + size_t luma_stride, uint32_t stride_align, - uint32_t * byte_stride) + uint64_t * byte_stride) { // https://developer.android.com/reference/android/graphics/ImageFormat#YV12 if (plane == 0) { @@ -464,23 +454,66 @@ static void update_yv12_stride(int8_t plane, #endif /* - * Logs and returns true if deprecated usage bits are found + * Logs and returns false if deprecated usage bits are found * * At times, framework introduces new usage flags which are identical to what * vendor has been using internally. This method logs those bits and returns * true if there is any deprecated usage bit. - * - * TODO(layog@): This check is also performed again during format deduction. At - * that point, the allocation is not aborted, just a log is printed to ALOGE - * (matched against `VALID_USAGE`). These should be aligned. */ -static bool log_deprecated_usage_flags(uint64_t usage) { +static bool log_obsolete_usage_flags(uint64_t usage) { if (usage & DEPRECATED_MALI_GRALLOC_USAGE_FRONTBUFFER) { MALI_GRALLOC_LOGW("Using deprecated FRONTBUFFER usage bit, please upgrade to BufferUsage::FRONT_BUFFER"); - return true; + return false; + } + if (usage & UNSUPPORTED_MALI_GRALLOC_USAGE_CUBE_MAP) { + MALI_GRALLOC_LOGW("BufferUsage::GPU_CUBE_MAP is unsupported"); + return false; + } + if (usage & UNSUPPORTED_MALI_GRALLOC_USAGE_MIPMAP_COMPLETE) { + MALI_GRALLOC_LOGW("BufferUsage::GPU_MIPMAP_COMPLETE is unsupported"); + return false; + } + + return true; +} + +static bool validate_size(uint32_t layer_count, uint32_t width, uint32_t height) { + // The max size of an image can be from camera (50 Megapixels) and considering the max + // depth of 4 bytes per pixel, we get an image of size 200MB. + // We can keep twice the margin for a max size of 400MB. + uint64_t overflow_limit = 400 * (1 << 20); + + // Maximum 4 bytes per pixel buffers are supported (RGBA). This does not take care of + // alignment, but 400MB is already very generous, so there should not be an issue. + overflow_limit /= 4; + overflow_limit /= layer_count; + + if (width > overflow_limit) { + MALI_GRALLOC_LOGE("Parameters layer: %" PRIu32 ", width: %" PRIu32 ", height: %" PRIu32 " are too big", layer_count, width, height); + return false; + } + overflow_limit /= width; + + if (height > overflow_limit) { + MALI_GRALLOC_LOGE("Parameters layer: %" PRIu32 ", width: %" PRIu32 ", height: %" PRIu32 " are too big", layer_count, width, height); + return false; } - return false; + return true; +} + +static bool validate_descriptor(buffer_descriptor_t * const bufDescriptor) { + if (!log_obsolete_usage_flags(bufDescriptor->producer_usage | bufDescriptor->consumer_usage)) { + return false; + } + + // BLOB formats are used for some ML models whose size can be really large (up to 2GB) + if (bufDescriptor->hal_format != MALI_GRALLOC_FORMAT_INTERNAL_BLOB && + !validate_size(bufDescriptor->layer_count, bufDescriptor->width, bufDescriptor->height)) { + return false; + } + + return true; } /* @@ -505,7 +538,7 @@ static uint64_t update_usage_for_BIG(uint64_t usage) { return usage; } -static void align_plane_stride(plane_info_t *plane_info, int plane, const format_info_t format, uint32_t stride_align) +static void align_plane_stride(plane_info_t *plane_info, uint8_t plane, const format_info_t format, uint32_t stride_align) { plane_info[plane].byte_stride = GRALLOC_ALIGN(plane_info[plane].byte_stride * format.tile_size, stride_align) / format.tile_size; plane_info[plane].alloc_width = plane_info[plane].byte_stride * 8 / format.bpp[plane]; @@ -540,8 +573,8 @@ static void calc_allocation_size(const int width, const bool has_gpu_usage, const bool has_BIG_usage, const bool has_camera_usage, - int * const pixel_stride, - uint64_t * const size, + uint64_t * const pixel_stride, + uint64_t * const size, plane_info_t plane_info[MAX_PLANES]) { /* pixel_stride is set outside this function after this function is called */ @@ -563,7 +596,7 @@ static void calc_allocation_size(const int width, alloc_type, plane, has_cpu_usage); - MALI_GRALLOC_LOGV("Aligned w=%d, h=%d (in pixels)", + MALI_GRALLOC_LOGV("Aligned w=%" PRIu64 ", h=%" PRIu64 " (in pixels)", plane_info[plane].alloc_width, plane_info[plane].alloc_height); /* @@ -577,7 +610,7 @@ static void calc_allocation_size(const int width, else { assert((plane_info[plane].alloc_width * format.bpp[plane]) % 8 == 0); - plane_info[plane].byte_stride = (static_cast<uint64_t>(plane_info[plane].alloc_width) * format.bpp[plane]) / 8; + plane_info[plane].byte_stride = (plane_info[plane].alloc_width * format.bpp[plane]) / 8; /* * Align byte stride (uncompressed allocations only). @@ -588,7 +621,7 @@ static void calc_allocation_size(const int width, * * NOTE: Pixel stride is defined as multiple of 'align_w_cpu'. */ - uint16_t hw_align = 0; + uint32_t hw_align = 0; if (has_hw_usage) { static_assert(is_power2(YUV_BYTE_ALIGN_DEFAULT), @@ -609,7 +642,7 @@ static void calc_allocation_size(const int width, /* * The GPU requires stricter alignment on YUV and raw formats. */ - hw_align = std::max(hw_align, static_cast<uint16_t>(GPU_BYTE_ALIGN_DEFAULT)); + hw_align = std::max(hw_align, static_cast<uint32_t>(GPU_BYTE_ALIGN_DEFAULT)); } #ifdef SOC_ZUMA @@ -619,13 +652,13 @@ static void calc_allocation_size(const int width, /* * Camera ISP requires RAW buffers to have 32-byte aligned stride */ - hw_align = std::max(hw_align, static_cast<uint16_t>(CAMERA_RAW_BUFFER_BYTE_ALIGN)); + hw_align = std::max(hw_align, static_cast<uint32_t>(CAMERA_RAW_BUFFER_BYTE_ALIGN)); } #endif if (has_BIG_usage) { assert(has_hw_usage); - hw_align = lcm(hw_align, static_cast<uint16_t>(BIG_BYTE_ALIGN_DEFAULT)); + hw_align = lcm(hw_align, static_cast<uint32_t>(BIG_BYTE_ALIGN_DEFAULT)); } uint32_t cpu_align = 0; @@ -661,7 +694,7 @@ static void calc_allocation_size(const int width, } #endif } - MALI_GRALLOC_LOGV("Byte stride: %d", plane_info[plane].byte_stride); + MALI_GRALLOC_LOGV("Byte stride: %" PRIu64, plane_info[plane].byte_stride); const uint32_t sb_num = (plane_info[plane].alloc_width * plane_info[plane].alloc_height) / AFBC_PIXELS_PER_BLOCK; @@ -669,11 +702,11 @@ static void calc_allocation_size(const int width, /* * Calculate body size (per plane). */ - int body_size = 0; + size_t body_size = 0; if (alloc_type.is_afbc()) { const rect_t sb = get_afbc_sb_size(alloc_type, plane); - const int sb_bytes = GRALLOC_ALIGN((format.bpp_afbc[plane] * sb.width * sb.height) / 8, 128); + const size_t sb_bytes = GRALLOC_ALIGN((format.bpp_afbc[plane] * sb.width * sb.height) / 8, 128); body_size = sb_num * sb_bytes; /* When AFBC planes are stored in separate buffers and this is not the last plane, @@ -685,7 +718,7 @@ static void calc_allocation_size(const int width, if (alloc_type.is_frontbuffer_safe) { - int back_buffer_size = body_size; + size_t back_buffer_size = body_size; afbc_buffer_align(alloc_type.is_tiled, &back_buffer_size); body_size += back_buffer_size; } @@ -701,13 +734,13 @@ static void calc_allocation_size(const int width, } body_size = plane_info[plane].byte_stride * plane_info[plane].alloc_height; } - MALI_GRALLOC_LOGV("Body size: %d", body_size); + MALI_GRALLOC_LOGV("Body size: %zu", body_size); /* * Calculate header size (per plane). */ - int header_size = 0; + size_t header_size = 0; if (alloc_type.is_afbc()) { /* As this is AFBC, calculate header size for this plane. @@ -716,7 +749,7 @@ static void calc_allocation_size(const int width, header_size = sb_num * AFBC_HEADER_BUFFER_BYTES_PER_BLOCKENTRY; afbc_buffer_align(alloc_type.is_tiled, &header_size); } - MALI_GRALLOC_LOGV("AFBC Header size: %d", header_size); + MALI_GRALLOC_LOGV("AFBC Header size: %zu", header_size); /* * Set offset for separate chroma planes. @@ -942,7 +975,7 @@ static int prepare_descriptor_exynos_formats( { MALI_GRALLOC_LOGE("buffer with format (%s %" PRIx64 ") has size %" PRIu64 - " != byte_stride %" PRIu32 " * alloc_height %" PRIu32, + " != byte_stride %" PRIu64 " * alloc_height %" PRIu64, format_name(bufDescriptor->alloc_format), bufDescriptor->alloc_format, plane[pidx].size, plane[pidx].byte_stride, plane[pidx].alloc_height); @@ -987,10 +1020,14 @@ int mali_gralloc_derive_format_and_size(buffer_descriptor_t * const bufDescripto ATRACE_CALL(); alloc_type_t alloc_type{}; - int alloc_width = bufDescriptor->width; - int alloc_height = bufDescriptor->height; + uint64_t alloc_width = bufDescriptor->width; + uint64_t alloc_height = bufDescriptor->height; uint64_t usage = bufDescriptor->producer_usage | bufDescriptor->consumer_usage; + if (!validate_descriptor(bufDescriptor)) { + return -EINVAL; + } + /* * Select optimal internal pixel format based upon * usage and requested format. @@ -1126,7 +1163,7 @@ int mali_gralloc_derive_format_and_size(buffer_descriptor_t * const bufDescripto int mali_gralloc_buffer_allocate(const gralloc_buffer_descriptor_t *descriptors, uint32_t numDescriptors, buffer_handle_t *pHandle, bool *shared_backend, - int fd) + bool use_placeholder) { std::string atrace_log = __FUNCTION__; if (ATRACE_ENABLED()) { @@ -1156,11 +1193,6 @@ int mali_gralloc_buffer_allocate(const gralloc_buffer_descriptor_t *descriptors, bufDescriptor->consumer_usage = usage; } - if (log_deprecated_usage_flags(usage)) - { - return -EINVAL; - } - /* Derive the buffer size from descriptor parameters */ err = mali_gralloc_derive_format_and_size(bufDescriptor); if (err != 0) @@ -1170,7 +1202,7 @@ int mali_gralloc_buffer_allocate(const gralloc_buffer_descriptor_t *descriptors, } /* Allocate ION backing store memory */ - err = mali_gralloc_ion_allocate(descriptors, numDescriptors, pHandle, &shared, fd); + err = mali_gralloc_ion_allocate(descriptors, numDescriptors, pHandle, &shared, use_placeholder); if (err < 0) { return err; diff --git a/gralloc4/src/core/mali_gralloc_bufferallocation.h b/gralloc4/src/core/mali_gralloc_bufferallocation.h index 3d759de..cc028ff 100644 --- a/gralloc4/src/core/mali_gralloc_bufferallocation.h +++ b/gralloc4/src/core/mali_gralloc_bufferallocation.h @@ -100,13 +100,11 @@ int mali_gralloc_derive_format_and_size(buffer_descriptor_t * const bufDescripto int mali_gralloc_buffer_allocate(const gralloc_buffer_descriptor_t *descriptors, uint32_t numDescriptors, buffer_handle_t *pHandle, bool *shared_backend, - int fd = -1); + bool use_placeholder = false); int mali_gralloc_buffer_free(buffer_handle_t pHandle); -void init_afbc(uint8_t *buf, uint64_t internal_format, const bool is_multi_plane, int w, int h); - -uint32_t lcm(uint32_t a, uint32_t b); +void init_afbc(uint8_t *buf, uint64_t internal_format, const bool is_multi_plane, uint64_t w, uint64_t h); bool get_alloc_type(const uint64_t format_ext, const uint32_t format_idx, diff --git a/gralloc4/src/core/mali_gralloc_bufferdescriptor.h b/gralloc4/src/core/mali_gralloc_bufferdescriptor.h index 826af79..657b2af 100644 --- a/gralloc4/src/core/mali_gralloc_bufferdescriptor.h +++ b/gralloc4/src/core/mali_gralloc_bufferdescriptor.h @@ -51,7 +51,7 @@ struct buffer_descriptor_t * allocate the buffer. */ uint64_t alloc_sizes[MAX_PLANES]; - int pixel_stride; + uint64_t pixel_stride; uint64_t alloc_format; uint32_t fd_count; uint32_t plane_count; @@ -86,11 +86,11 @@ struct buffer_descriptor_t "name(%s)" "reserved_size(%" PRIu64 ") " "alloc_sizes(%" PRIu64 ", %" PRIu64 ", %" PRIu64 ")" - "pixel_stride(%d) alloc_format(0x%" PRIx64 ") fd_count(%d) " + "pixel_stride(%" PRIu64 ") alloc_format(0x%" PRIx64 ") fd_count(%d) " "plane_count(%u) " - "plane[0](offset %" PRId64 ", idx %u, size %" PRIu64 " byte_stride %u, wh %u %u)" - "plane[1](offset %" PRId64 ", idx %u, size %" PRIu64 " byte_stride %u, wh %u %u)" - "plane[2](offset %" PRId64 ", idx %u, size %" PRIu64 " byte_stride %u, wh %u %u)" + "plane[0](offset %" PRId64 ", idx %u, size %" PRIu64 " byte_stride %" PRIu64 ", wh %" PRIu64 " %" PRIu64 ")" + "plane[1](offset %" PRId64 ", idx %u, size %" PRIu64 " byte_stride %" PRIu64 ", wh %" PRIu64 " %" PRIu64 ")" + "plane[2](offset %" PRId64 ", idx %u, size %" PRIu64 " byte_stride %" PRIu64 ", wh %" PRIu64 " %" PRIu64 ")" "\n", str.c_str(), width, height, diff --git a/gralloc4/src/core/mali_gralloc_formats.cpp b/gralloc4/src/core/mali_gralloc_formats.cpp index 5048cac..e36e27d 100644 --- a/gralloc4/src/core/mali_gralloc_formats.cpp +++ b/gralloc4/src/core/mali_gralloc_formats.cpp @@ -408,8 +408,8 @@ static uint64_t get_producer_caps(const uint16_t producers) */ void mali_gralloc_adjust_dimensions(const uint64_t alloc_format, const uint64_t usage, - int* const width, - int* const height) + uint64_t* const width, + uint64_t* const height) { /* Determine producers. */ const uint16_t producers = get_producers(usage); @@ -444,7 +444,7 @@ void mali_gralloc_adjust_dimensions(const uint64_t alloc_format, } MALI_GRALLOC_LOGV("%s: alloc_format=(%s 0x%" PRIx64 ") usage=(%s 0x%" PRIx64 - ") alloc_width=%u, alloc_height=%u", + ") alloc_width=%" PRIu64 ", alloc_height=%" PRIu64 "", __FUNCTION__, format_name(alloc_format), alloc_format, describe_usage(usage).c_str(), usage, *width, *height); } @@ -1502,7 +1502,7 @@ uint64_t mali_gralloc_select_format(const uint64_t req_format, /* Reject if usage specified is outside white list of valid usages. */ if (type != MALI_GRALLOC_FORMAT_TYPE_INTERNAL && (usage & (~VALID_USAGE)) != 0) { - MALI_GRALLOC_LOGE("Invalid usage specified: %s 0x%" PRIx64, describe_usage(usage).c_str(), usage); + MALI_GRALLOC_LOGW("Invalid usage specified: %s 0x%" PRIx64, describe_usage(usage).c_str(), usage); } /* TODO: Make a function for finding formats that should be allocated as the request format */ diff --git a/gralloc4/src/core/mali_gralloc_reference.cpp b/gralloc4/src/core/mali_gralloc_reference.cpp index 6726895..c685c13 100644 --- a/gralloc4/src/core/mali_gralloc_reference.cpp +++ b/gralloc4/src/core/mali_gralloc_reference.cpp @@ -20,6 +20,7 @@ #include <android-base/thread_annotations.h> #include <hardware/gralloc1.h> +#include <unistd.h> #include <algorithm> #include <map> @@ -62,19 +63,25 @@ private: private_handle_t *hnd = static_cast<private_handle_t *>(const_cast<native_handle_t *>(handle)); + if (hnd->fd_count < 0 || hnd->fd_count > MAX_FDS) { + MALI_GRALLOC_LOGE("%s failed: invalid number of fds (%d)", __func__, hnd->fd_count); + return false; + } + int valid_fd_count = std::find(hnd->fds, hnd->fds + MAX_FDS, -1) - hnd->fds; // One fd is reserved for metadata which is not accounted for in fd_count - if (hnd->fd_count != valid_fd_count - 1) { - MALI_GRALLOC_LOGE("%s failed: count of valid buffer fds does not match fd_count", - __func__); + if (hnd->fd_count + 1 != valid_fd_count) { + MALI_GRALLOC_LOGE("%s failed: count of valid buffer fds does not match fd_count (%d != " + "%d)", + __func__, hnd->fd_count, valid_fd_count - 1); return false; } auto check_pid = [&](int fd, uint64_t allocated_size) -> bool { auto size = get_buffer_size(fd); auto size_padding = size - (off_t)allocated_size; - if ((size != -1) && ((size_padding < 0) || (size_padding > PAGE_SIZE))) { - MALI_GRALLOC_LOGE("%s failed: fd (%d) size (%jd) is not within a PAGE_SIZE of " + if ((size != -1) && ((size_padding < 0) || (size_padding > getpagesize()))) { + MALI_GRALLOC_LOGE("%s failed: fd (%d) size (%jd) is not within a page of " "expected size (%" PRIx64 ")", __func__, fd, static_cast<intmax_t>(size), allocated_size); return false; diff --git a/gralloc4/src/gralloc_helper.h b/gralloc4/src/gralloc_helper.h index ba31333..7add6a4 100644 --- a/gralloc4/src/gralloc_helper.h +++ b/gralloc4/src/gralloc_helper.h @@ -31,9 +31,4 @@ #define GRALLOC_UNUSED(x) ((void)x) -static inline size_t round_up_to_page_size(size_t x) -{ - return (x + (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1); -} - #endif /* GRALLOC_HELPER_H_ */ diff --git a/gralloc4/src/gralloc_priv.h b/gralloc4/src/gralloc_priv.h index bbef1b6..1074e42 100644 --- a/gralloc4/src/gralloc_priv.h +++ b/gralloc4/src/gralloc_priv.h @@ -28,11 +28,6 @@ #include <sys/mman.h> #include <cutils/native_handle.h> -/* Allocator = 4.0, Mapper = 4.0 and Common = 1.2 */ -#define HIDL_ALLOCATOR_VERSION_SCALED 400 -#define HIDL_MAPPER_VERSION_SCALED 400 -#define HIDL_COMMON_VERSION_SCALED 120 - #include "mali_gralloc_formats.h" #include "mali_gralloc_usages.h" #include "gralloc_helper.h" diff --git a/gralloc4/src/hidl_common/Allocator.cpp b/gralloc4/src/hidl_common/Allocator.cpp index 29cf3ca..08f2e8e 100644 --- a/gralloc4/src/hidl_common/Allocator.cpp +++ b/gralloc4/src/hidl_common/Allocator.cpp @@ -18,7 +18,13 @@ #define ATRACE_TAG ATRACE_TAG_GRAPHICS +#include <array> +#include <atomic> +#include <chrono> #include <utils/Trace.h> +#include <shared_mutex> +#include <sstream> +#include <sys/stat.h> #include "Allocator.h" #include "core/mali_gralloc_bufferallocation.h" @@ -35,6 +41,35 @@ namespace allocator namespace common { +struct BufferDetails { + std::string name; + uint64_t buffer_id; + std::vector<ino_t> inodes; + uint64_t format; + uint64_t usage; + uint32_t width; + uint32_t height; +}; + +uint64_t total_allocated = 0; +std::atomic<uint16_t> next_idx = 0; + +// There is no atomic rounding off for atomics so next_idx can overflow. allocated_buffers should be +// a power of 2. +std::array<BufferDetails, 2048> allocated_buffers; +std::shared_timed_mutex allocated_buffers_mutex; +static_assert((allocated_buffers.size() & (allocated_buffers.size() - 1)) == 0); + +ino_t get_inode(int fd) { + struct stat fd_info; + int error = fstat(fd, &fd_info); + if (error != 0) { + return error; + } + + return fd_info.st_ino; +} + void allocate(const buffer_descriptor_t &bufferDescriptor, uint32_t count, IAllocator::allocate_cb hidl_cb, std::function<int(const buffer_descriptor_t *, buffer_handle_t *)> fb_allocator) { @@ -45,6 +80,7 @@ void allocate(const buffer_descriptor_t &bufferDescriptor, uint32_t count, IAllo Error error = Error::NONE; int stride = 0; + bool use_placeholder = bufferDescriptor.producer_usage & GRALLOC_USAGE_PLACEHOLDER_BUFFER; std::vector<hidl_handle> grallocBuffers; gralloc_buffer_descriptor_t grallocBufferDescriptor[1]; @@ -66,7 +102,7 @@ void allocate(const buffer_descriptor_t &bufferDescriptor, uint32_t count, IAllo else #endif { - allocResult = mali_gralloc_buffer_allocate(grallocBufferDescriptor, 1, &tmpBuffer, nullptr); + allocResult = mali_gralloc_buffer_allocate(grallocBufferDescriptor, 1, &tmpBuffer, nullptr, use_placeholder); if (allocResult != 0) { MALI_GRALLOC_LOGE("%s, buffer allocation failed with %d", __func__, allocResult); @@ -118,6 +154,31 @@ void allocate(const buffer_descriptor_t &bufferDescriptor, uint32_t count, IAllo (static_cast<shared_metadata*>(metadata_vaddr))->dataspace = aligned_optional(static_cast<Dataspace>(dataspace)); } + { + ATRACE_NAME("Update dump details"); + const auto fd_count = hnd->fd_count + 1 /* metadata fd */; + std::vector<ino_t> inodes(fd_count); + for (size_t idx = 0; idx < fd_count; idx++) { + inodes[idx] = get_inode(hnd->fds[idx]); + } + + auto idx = next_idx.fetch_add(1); + idx %= allocated_buffers.size(); + + allocated_buffers_mutex.lock_shared(); + allocated_buffers[idx] = + BufferDetails{bufferDescriptor.name, + hnd->backing_store_id, + inodes, + bufferDescriptor.hal_format, + bufferDescriptor.producer_usage, + bufferDescriptor.width, + bufferDescriptor.height}; + allocated_buffers_mutex.unlock_shared(); + + total_allocated++; + } + munmap(metadata_vaddr, hnd->attr_size); } @@ -157,6 +218,38 @@ void allocate(const buffer_descriptor_t &bufferDescriptor, uint32_t count, IAllo } } +const std::string dump() { + using namespace std::chrono_literals; + if (!allocated_buffers_mutex.try_lock_for(100ms)) { + return ""; + } + + std::stringstream ss; + // TODO: Add logs to indicate overflow + for (int i = 0; i < std::min(total_allocated, static_cast<uint64_t>(allocated_buffers.size())); i++) { + const auto& [name, buffer_id, inodes, format, usage, width, height] = allocated_buffers[i]; + ss << "buffer_id: " << buffer_id << ", inodes: "; + for (auto it = inodes.begin(); it != inodes.end(); it++) { + if (it != inodes.begin()) { + ss << ","; + } + ss << static_cast<int>(*it); + } + ss << ", format: 0x" << std::hex << format << std::dec; + ss << ", usage: 0x" << std::hex << usage << std::dec; + ss << ", width: " << width << ", height: " << height; + ss << ", name: " << name << std::endl; + } + + allocated_buffers_mutex.unlock(); + return ss.str(); +} + +int isSupported(buffer_descriptor_t *const bufDescriptor) { + // this is used as the criteria to determine which allocations succeed. + return mali_gralloc_derive_format_and_size(bufDescriptor); +} + } // namespace common } // namespace allocator } // namespace arm diff --git a/gralloc4/src/hidl_common/Allocator.h b/gralloc4/src/hidl_common/Allocator.h index 8fdd5ba..e5ce174 100644 --- a/gralloc4/src/hidl_common/Allocator.h +++ b/gralloc4/src/hidl_common/Allocator.h @@ -54,6 +54,10 @@ using android::hardware::hidl_vec; void allocate(const buffer_descriptor_t &bufferDescriptor, uint32_t count, IAllocator::allocate_cb hidl_cb, std::function<int(const buffer_descriptor_t *, buffer_handle_t *)> fb_allocator = nullptr); +const std::string dump(); + +int isSupported(buffer_descriptor_t *const bufDescriptor); + } // namespace common } // namespace allocator } // namespace arm diff --git a/gralloc4/src/hidl_common/BufferDescriptor.h b/gralloc4/src/hidl_common/BufferDescriptor.h index 0d93811..2c63b6f 100644 --- a/gralloc4/src/hidl_common/BufferDescriptor.h +++ b/gralloc4/src/hidl_common/BufferDescriptor.h @@ -19,8 +19,7 @@ #define _GRALLOC_BUFFER_DESCRIPTOR_H_ #include "core/mali_gralloc_bufferdescriptor.h" - -#include "4.x/gralloc_mapper_hidl_header.h" +#include "hidl_common.h" #include <assert.h> #include <inttypes.h> @@ -30,27 +29,25 @@ namespace arm { namespace mapper { namespace common { -using android::hardware::hidl_vec; - const size_t DESCRIPTOR_32BIT_FIELDS = 5; const size_t DESCRIPTOR_64BIT_FIELDS = 2; const uint64_t validUsageBits = - BufferUsage::GPU_CUBE_MAP | - BufferUsage::GPU_MIPMAP_COMPLETE | - BufferUsage::CPU_READ_MASK | BufferUsage::CPU_WRITE_MASK | - BufferUsage::GPU_TEXTURE | BufferUsage::GPU_RENDER_TARGET | - BufferUsage::COMPOSER_OVERLAY | BufferUsage::COMPOSER_CLIENT_TARGET | - BufferUsage::CAMERA_INPUT | BufferUsage::CAMERA_OUTPUT | - BufferUsage::PROTECTED | - BufferUsage::COMPOSER_CURSOR | - BufferUsage::VIDEO_ENCODER | - BufferUsage::RENDERSCRIPT | - BufferUsage::VIDEO_DECODER | - BufferUsage::SENSOR_DIRECT_DATA | - BufferUsage::GPU_DATA_BUFFER | - BufferUsage::VENDOR_MASK | - BufferUsage::VENDOR_MASK_HI; + static_cast<uint64_t>(BufferUsage::GPU_CUBE_MAP) | + static_cast<uint64_t>(BufferUsage::GPU_MIPMAP_COMPLETE) | + static_cast<uint64_t>(BufferUsage::CPU_READ_MASK) | static_cast<uint64_t>(BufferUsage::CPU_WRITE_MASK) | + static_cast<uint64_t>(BufferUsage::GPU_TEXTURE) | static_cast<uint64_t>(BufferUsage::GPU_RENDER_TARGET) | + static_cast<uint64_t>(BufferUsage::COMPOSER_OVERLAY) | static_cast<uint64_t>(BufferUsage::COMPOSER_CLIENT_TARGET) | + static_cast<uint64_t>(BufferUsage::CAMERA_INPUT) | static_cast<uint64_t>(BufferUsage::CAMERA_OUTPUT) | + static_cast<uint64_t>(BufferUsage::PROTECTED) | + static_cast<uint64_t>(BufferUsage::COMPOSER_CURSOR) | + static_cast<uint64_t>(BufferUsage::VIDEO_ENCODER) | + static_cast<uint64_t>(BufferUsage::RENDERSCRIPT) | + static_cast<uint64_t>(BufferUsage::VIDEO_DECODER) | + static_cast<uint64_t>(BufferUsage::SENSOR_DIRECT_DATA) | + static_cast<uint64_t>(BufferUsage::GPU_DATA_BUFFER) | + static_cast<uint64_t>(BufferUsage::VENDOR_MASK) | + static_cast<uint64_t>(BufferUsage::VENDOR_MASK_HI); template<typename BufferDescriptorInfoT> static bool validateDescriptorInfo(const BufferDescriptorInfoT &descriptorInfo) @@ -69,7 +66,7 @@ static bool validateDescriptorInfo(const BufferDescriptorInfoT &descriptorInfo) } template <typename vecT> -static void push_descriptor_uint32(hidl_vec<vecT> *vec, size_t *pos, uint32_t val) +static void push_descriptor_uint32(frameworks_vec<vecT> *vec, size_t *pos, uint32_t val) { static_assert(sizeof(val) % sizeof(vecT) == 0, "Unsupported vector type"); memcpy(vec->data() + *pos, &val, sizeof(val)); @@ -77,7 +74,7 @@ static void push_descriptor_uint32(hidl_vec<vecT> *vec, size_t *pos, uint32_t va } template <typename vecT> -static uint32_t pop_descriptor_uint32(const hidl_vec<vecT> &vec, size_t *pos) +static uint32_t pop_descriptor_uint32(const frameworks_vec<vecT> &vec, size_t *pos) { uint32_t val; static_assert(sizeof(val) % sizeof(vecT) == 0, "Unsupported vector type"); @@ -87,7 +84,7 @@ static uint32_t pop_descriptor_uint32(const hidl_vec<vecT> &vec, size_t *pos) } template <typename vecT> -static void push_descriptor_uint64(hidl_vec<vecT> *vec, size_t *pos, uint64_t val) +static void push_descriptor_uint64(frameworks_vec<vecT> *vec, size_t *pos, uint64_t val) { static_assert(sizeof(val) % sizeof(vecT) == 0, "Unsupported vector type"); memcpy(vec->data() + *pos, &val, sizeof(val)); @@ -95,7 +92,7 @@ static void push_descriptor_uint64(hidl_vec<vecT> *vec, size_t *pos, uint64_t va } template <typename vecT> -static uint64_t pop_descriptor_uint64(const hidl_vec<vecT> &vec, size_t *pos) +static uint64_t pop_descriptor_uint64(const frameworks_vec<vecT> &vec, size_t *pos) { uint64_t val; static_assert(sizeof(val) % sizeof(vecT) == 0, "Unsupported vector type"); @@ -105,23 +102,25 @@ static uint64_t pop_descriptor_uint64(const hidl_vec<vecT> &vec, size_t *pos) } // There can only be one string at the end of the descriptor -static void push_descriptor_string(hidl_vec<uint8_t> *vec, size_t *pos, const std::string &str) +static void push_descriptor_string(frameworks_vec<uint8_t> *vec, size_t *pos, const std::string &str) { strcpy(reinterpret_cast<char *>(vec->data() + *pos), str.c_str()); *pos += strlen(str.c_str()) + 1; } -static std::string pop_descriptor_string(const hidl_vec<uint8_t> &vec, size_t *pos) +static std::string pop_descriptor_string(const frameworks_vec<uint8_t> &vec, size_t *pos) { - std::string str(reinterpret_cast<const char *>(vec.data() + *pos)); - *pos += str.size() + 1; + const char* charstr = reinterpret_cast<const char *>(vec.data() + *pos); + charstr += '\0'; + std::string str(charstr); + str.resize(strlen(charstr)); return str; } template <typename vecT, typename BufferDescriptorInfoT> -static const hidl_vec<vecT> grallocEncodeBufferDescriptor(const BufferDescriptorInfoT &descriptorInfo) +static const frameworks_vec<vecT> grallocEncodeBufferDescriptor(const BufferDescriptorInfoT &descriptorInfo) { - hidl_vec<vecT> descriptor; + frameworks_vec<vecT> descriptor; static_assert(sizeof(uint32_t) % sizeof(vecT) == 0, "Unsupported vector type"); size_t dynamic_size = 0; @@ -150,7 +149,7 @@ static const hidl_vec<vecT> grallocEncodeBufferDescriptor(const BufferDescriptor } template <typename vecT> -static bool grallocDecodeBufferDescriptor(const hidl_vec<vecT> &androidDescriptor, buffer_descriptor_t &grallocDescriptor) +static bool grallocDecodeBufferDescriptor(const frameworks_vec<vecT> &androidDescriptor, buffer_descriptor_t &grallocDescriptor) { static_assert(sizeof(uint32_t) % sizeof(vecT) == 0, "Unsupported vector type"); size_t pos = 0; diff --git a/gralloc4/src/hidl_common/Mapper.cpp b/gralloc4/src/hidl_common/Mapper.cpp index 38d5377..62cac7c 100644 --- a/gralloc4/src/hidl_common/Mapper.cpp +++ b/gralloc4/src/hidl_common/Mapper.cpp @@ -18,6 +18,7 @@ #include <inttypes.h> #include <sync/sync.h> +#include <hardware/gralloc1.h> #include "RegisteredHandlePool.h" #include "Mapper.h" #include "BufferDescriptor.h" @@ -34,6 +35,8 @@ #include "MapperMetadata.h" #include "SharedMetadata.h" +#include <cstdio> + /* GraphicBufferMapper is expected to be valid (and leaked) during process * termination. IMapper, and in turn, gRegisteredHandles must be valid as * well. Create the registered handle pool on the heap, and let @@ -49,6 +52,10 @@ namespace arm { namespace mapper { namespace common { +buffer_handle_t getBuffer(void *buffer) { + return gRegisteredHandles->get(buffer); +} + /* * Translates the register buffer API into existing gralloc implementation * @@ -101,45 +108,34 @@ static Error unregisterBuffer(buffer_handle_t bufferHandle) } /* - * Retrieves the file descriptor referring to a sync fence object - * - * @param fenceHandle [in] HIDL fence handle - * @param outFenceFd [out] Fence file descriptor. '-1' indicates no fence + * Converts a gralloc error code to a mapper error code * - * @return false, for an invalid HIDL fence handle - * true, otherwise - */ -static bool getFenceFd(const hidl_handle& fenceHandle, int* outFenceFd) -{ - auto const handle = fenceHandle.getNativeHandle(); - if (handle && handle->numFds > 1) - { - MALI_GRALLOC_LOGE("Invalid fence handle with %d fds", handle->numFds); - return false; - } - - *outFenceFd = (handle && handle->numFds == 1) ? handle->data[0] : -1; - return true; -} - -/* - * Populates the HIDL fence handle for the given fence object + * @param grallocError [in] Gralloc error as integer. * - * @param fenceFd [in] Fence file descriptor - * @param handleStorage [in] HIDL handle storage for fence + * @return Corresponding Mapper error code * - * @return HIDL fence handle + * @note There is no full 1:1 correspondence, several gralloc errors may map to Error::UNSUPPORTED. + * @note -EINVAL is mapped to Error::BAD_VALUE. */ -static hidl_handle getFenceHandle(int fenceFd, char* handleStorage) +static Error grallocErrorToMapperError(int grallocError) { - native_handle_t* handle = nullptr; - if (fenceFd >= 0) - { - handle = native_handle_init(handleStorage, 1, 0); - handle->data[0] = fenceFd; + switch(grallocError) + { + case GRALLOC1_ERROR_NONE: + return Error::NONE; + case GRALLOC1_ERROR_BAD_DESCRIPTOR: + return Error::BAD_DESCRIPTOR; + case GRALLOC1_ERROR_BAD_HANDLE: + return Error::BAD_BUFFER; + case GRALLOC1_ERROR_BAD_VALUE: + case -EINVAL: + return Error::BAD_VALUE; + case GRALLOC1_ERROR_NO_RESOURCES: + return Error::NO_RESOURCES; + default: + /* Covers NOT_SHARED, UNDEFINED, UNSUPPORTED */ + return Error::UNSUPPORTED; } - - return hidl_handle(handle); } /* @@ -147,18 +143,20 @@ static hidl_handle getFenceHandle(int fenceFd, char* handleStorage) * * @param bufferHandle [in] Buffer to lock. * @param cpuUsage [in] Specifies one or more CPU usage flags to request - * @param accessRegion [in] Portion of the buffer that the client intends to access. + * @param accessRegion [in] Portion of the buffer that the client intends to + * access. * @param fenceFd [in] Fence file descriptor * @param outData [out] CPU accessible buffer address * * @return Error::BAD_BUFFER for an invalid buffer * Error::NO_RESOURCES when unable to duplicate fence * Error::BAD_VALUE when locking fails + * Error::UNSUPPORTED when locking fails on unsupported image formats * Error::NONE on successful buffer lock */ static Error lockBuffer(buffer_handle_t bufferHandle, uint64_t cpuUsage, - const IMapper::Rect& accessRegion, int fenceFd, + const GrallocRect& accessRegion, int fenceFd, void** outData) { /* dup fenceFd as it is going to be owned by gralloc. Note that it is @@ -195,7 +193,7 @@ static Error lockBuffer(buffer_handle_t bufferHandle, } auto private_handle = private_handle_t::dynamicCast(bufferHandle); - if (private_handle->cpu_write != 0 && (cpuUsage & BufferUsage::CPU_WRITE_MASK)) + if (private_handle->cpu_write != 0 && (cpuUsage & static_cast<uint64_t>(BufferUsage::CPU_WRITE_MASK))) { if (fenceFd >= 0) { @@ -214,15 +212,22 @@ static Error lockBuffer(buffer_handle_t bufferHandle, } void* data = nullptr; - if (mali_gralloc_lock(bufferHandle, cpuUsage, accessRegion.left, accessRegion.top, accessRegion.width, - accessRegion.height, &data) < 0) + const int gralloc_err = + mali_gralloc_lock(bufferHandle, cpuUsage, accessRegion.left, accessRegion.top, + accessRegion.right - accessRegion.left, + accessRegion.bottom - accessRegion.top, &data); + const Error lock_err = grallocErrorToMapperError(gralloc_err); + + if(Error::NONE == lock_err) { - return Error::BAD_VALUE; + *outData = data; + } + else + { + MALI_GRALLOC_LOGE("Locking failed with error: %d", gralloc_err); } - *outData = data; - - return Error::NONE; + return lock_err; } /* @@ -244,141 +249,95 @@ static Error unlockBuffer(buffer_handle_t bufferHandle, return Error::BAD_BUFFER; } - auto private_handle = private_handle_t::dynamicCast(bufferHandle); + const int gralloc_err = mali_gralloc_unlock(bufferHandle); + const Error unlock_err = grallocErrorToMapperError(gralloc_err); - const int result = mali_gralloc_unlock(bufferHandle); - if (result) + if (Error::NONE == unlock_err) { - MALI_GRALLOC_LOGE("Unlocking failed with error: %d", result); - return Error::BAD_VALUE; + *outFenceFd = -1; } - - *outFenceFd = -1; - - return Error::NONE; -} - -void importBuffer(const hidl_handle& rawHandle, IMapper::importBuffer_cb hidl_cb) -{ - if (!rawHandle.getNativeHandle()) + else { - MALI_GRALLOC_LOGE("Invalid buffer handle to import"); - hidl_cb(Error::BAD_BUFFER, nullptr); - return; + MALI_GRALLOC_LOGE("Unlocking failed with error: %d", + gralloc_err); + return Error::BAD_BUFFER; } - native_handle_t* bufferHandle = native_handle_clone(rawHandle.getNativeHandle()); - if (!bufferHandle) - { - MALI_GRALLOC_LOGE("Failed to clone buffer handle"); - hidl_cb(Error::NO_RESOURCES, nullptr); - return; - } + return unlock_err; +} - const Error error = registerBuffer(bufferHandle); +Error importBuffer(const native_handle_t *inBuffer, buffer_handle_t *outBuffer) +{ + *outBuffer = const_cast<buffer_handle_t>(native_handle_clone(inBuffer)); + const Error error = registerBuffer(*outBuffer); if (error != Error::NONE) { - native_handle_close(bufferHandle); - native_handle_delete(bufferHandle); - - hidl_cb(error, nullptr); - return; + return error; } - auto *private_handle = static_cast<private_handle_t *>(bufferHandle); - - if (gRegisteredHandles->add(bufferHandle) == false) + if (gRegisteredHandles->add(*outBuffer) == false) { /* The newly cloned handle is already registered. This can only happen * when a handle previously registered was native_handle_delete'd instead * of freeBuffer'd. */ MALI_GRALLOC_LOGE("Handle %p has already been imported; potential fd leaking", - bufferHandle); - unregisterBuffer(bufferHandle); - native_handle_close(bufferHandle); - native_handle_delete(bufferHandle); - - hidl_cb(Error::NO_RESOURCES, nullptr); - return; + outBuffer); + unregisterBuffer(*outBuffer); + return Error::NO_RESOURCES; } - hidl_cb(Error::NONE, bufferHandle); + return Error::NONE; } -Error freeBuffer(void* buffer) +Error freeBuffer(buffer_handle_t bufferHandle) { - native_handle_t * const bufferHandle = gRegisteredHandles->remove(buffer); - if (!bufferHandle) + native_handle_t *handle = gRegisteredHandles->remove(bufferHandle); + if (handle == nullptr) { - MALI_GRALLOC_LOGE("Invalid buffer handle %p to freeBuffer", buffer); + MALI_GRALLOC_LOGE("Invalid buffer handle %p to freeBuffer", bufferHandle); return Error::BAD_BUFFER; } - const Error status = unregisterBuffer(bufferHandle); + const Error status = unregisterBuffer(handle); if (status != Error::NONE) { return status; } - native_handle_close(bufferHandle); - native_handle_delete(bufferHandle); + native_handle_close(handle); + native_handle_delete(handle); return Error::NONE; } -void lock(void* buffer, uint64_t cpuUsage, const IMapper::Rect& accessRegion, - const hidl_handle& acquireFence, IMapper::lock_cb hidl_cb) +Error lock(buffer_handle_t bufferHandle, uint64_t cpuUsage, const GrallocRect &accessRegion, int acquireFence, void **outData) { - buffer_handle_t bufferHandle = gRegisteredHandles->get(buffer); + *outData = nullptr; if (!bufferHandle || private_handle_t::validate(bufferHandle) < 0) { - MALI_GRALLOC_LOGE("Buffer to lock: %p is not valid", buffer); - hidl_cb(Error::BAD_BUFFER, nullptr); - return; - } - - int fenceFd; - if (!getFenceFd(acquireFence, &fenceFd)) - { - hidl_cb(Error::BAD_VALUE, nullptr); - return; + MALI_GRALLOC_LOGE("Buffer to lock: %p is not valid", + bufferHandle); + return Error::BAD_BUFFER; } - void* data = nullptr; - const Error error = lockBuffer(bufferHandle, cpuUsage, accessRegion, fenceFd, &data); - - hidl_cb(error, data); + const Error error = lockBuffer(bufferHandle, cpuUsage, accessRegion, + acquireFence, outData); + return error; } -void unlock(void* buffer, IMapper::unlock_cb hidl_cb) -{ - buffer_handle_t bufferHandle = gRegisteredHandles->get(buffer); - if (!bufferHandle) - { - MALI_GRALLOC_LOGE("Buffer to unlock: %p has not been registered with Gralloc", buffer); - hidl_cb(Error::BAD_BUFFER, nullptr); - return; +Error unlock(buffer_handle_t bufferHandle, int *releaseFence) { + if(bufferHandle == nullptr) return Error::BAD_BUFFER; + if(!gRegisteredHandles->isRegistered(bufferHandle)) { + MALI_GRALLOC_LOGE("Buffer to unlock: %p has not been registered with Gralloc", + bufferHandle); + return Error::BAD_BUFFER; } - int fenceFd; - const Error error = unlockBuffer(bufferHandle, &fenceFd); - if (error == Error::NONE) - { - NATIVE_HANDLE_DECLARE_STORAGE(fenceStorage, 1, 0); - hidl_cb(error, getFenceHandle(fenceFd, fenceStorage)); - - if (fenceFd >= 0) - { - close(fenceFd); - } - } - else - { - hidl_cb(error, nullptr); - } + const Error error = unlockBuffer(bufferHandle, releaseFence); + return error; } - +#ifdef GRALLOC_MAPPER_4 Error validateBufferSize(void* buffer, const IMapper::BufferDescriptorInfo& descriptorInfo, uint32_t in_stride) @@ -430,7 +389,7 @@ Error validateBufferSize(void* buffer, if (in_stride != 0 && (uint32_t)gralloc_buffer->stride != in_stride) { - MALI_GRALLOC_LOGE("Stride mismatch. Expected stride = %d, Buffer stride = %d", + MALI_GRALLOC_LOGE("Stride mismatch. Expected stride = %d, Buffer stride = %" PRIu64, in_stride, gralloc_buffer->stride); return Error::BAD_VALUE; } @@ -455,21 +414,21 @@ Error validateBufferSize(void* buffer, { if (gralloc_buffer->plane_info[i].byte_stride != grallocDescriptor.plane_info[i].byte_stride) { - MALI_GRALLOC_LOGE("Buffer byte stride 0x%x mismatch with desc byte stride 0x%x in plane %d ", + MALI_GRALLOC_LOGE("Buffer byte stride %" PRIu64 " mismatch with desc byte stride %" PRIu64 " in plane %d ", gralloc_buffer->plane_info[i].byte_stride, grallocDescriptor.plane_info[i].byte_stride, i); return Error::BAD_VALUE; } if (gralloc_buffer->plane_info[i].alloc_width != grallocDescriptor.plane_info[i].alloc_width) { - MALI_GRALLOC_LOGE("Buffer alloc width 0x%x mismatch with desc alloc width 0x%x in plane %d ", + MALI_GRALLOC_LOGE("Buffer alloc width %" PRIu64 " mismatch with desc alloc width %" PRIu64 " in plane %d ", gralloc_buffer->plane_info[i].alloc_width, grallocDescriptor.plane_info[i].alloc_width, i); return Error::BAD_VALUE; } if (gralloc_buffer->plane_info[i].alloc_height != grallocDescriptor.plane_info[i].alloc_height) { - MALI_GRALLOC_LOGE("Buffer alloc height 0x%x mismatch with desc alloc height 0x%x in plane %d ", + MALI_GRALLOC_LOGE("Buffer alloc height %" PRIu64 " mismatch with desc alloc height %" PRIu64 " in plane %d ", gralloc_buffer->plane_info[i].alloc_height, grallocDescriptor.plane_info[i].alloc_height, i); return Error::BAD_VALUE; } @@ -499,28 +458,32 @@ Error validateBufferSize(void* buffer, return Error::NONE; } +#endif -void getTransportSize(void* buffer, IMapper::getTransportSize_cb hidl_cb) +Error getTransportSize(buffer_handle_t bufferHandle, uint32_t *outNumFds, uint32_t *outNumInts) { + *outNumFds = 0; + *outNumInts = 0; /* The buffer must have been allocated by Gralloc */ - buffer_handle_t bufferHandle = gRegisteredHandles->get(buffer); if (!bufferHandle) { - MALI_GRALLOC_LOGE("Buffer %p is not registered with Gralloc", bufferHandle); - hidl_cb(Error::BAD_BUFFER, -1, -1); - return; + MALI_GRALLOC_LOGE("Buffer %p is not registered with Gralloc", + bufferHandle); + return Error::BAD_BUFFER; } if (private_handle_t::validate(bufferHandle) < 0) { - MALI_GRALLOC_LOGE("Buffer %p is corrupted", buffer); - hidl_cb(Error::BAD_BUFFER, -1, -1); - return; + MALI_GRALLOC_LOGE("Buffer %p is corrupted", bufferHandle); + return Error::BAD_BUFFER; } - hidl_cb(Error::NONE, bufferHandle->numFds, bufferHandle->numInts); + *outNumFds = bufferHandle->numFds; + *outNumInts = bufferHandle->numInts; + return Error::NONE; } -void isSupported(const IMapper::BufferDescriptorInfo& description, IMapper::isSupported_cb hidl_cb) +#ifdef GRALLOC_MAPPER_4 +bool isSupported(const IMapper::BufferDescriptorInfo &description) { buffer_descriptor_t grallocDescriptor; grallocDescriptor.width = description.width; @@ -536,39 +499,36 @@ void isSupported(const IMapper::BufferDescriptorInfo& description, IMapper::isSu if (result != 0) { MALI_GRALLOC_LOGV("Allocation for the given description will not succeed. error: %d", result); - hidl_cb(Error::NONE, false); + return false; } else { - hidl_cb(Error::NONE, true); + return true; } } -void flushLockedBuffer(void *buffer, IMapper::flushLockedBuffer_cb hidl_cb) +#endif +Error flushLockedBuffer(buffer_handle_t handle) { - buffer_handle_t handle = gRegisteredHandles->get(buffer); if (private_handle_t::validate(handle) < 0) { - MALI_GRALLOC_LOGE("Bandle: %p is corrupted", handle); - hidl_cb(Error::BAD_BUFFER, hidl_handle{}); - return; + MALI_GRALLOC_LOGE("Handle: %p is corrupted", handle); + return Error::BAD_BUFFER; } auto private_handle = static_cast<const private_handle_t *>(handle); if (!private_handle->cpu_write && !private_handle->cpu_read) { MALI_GRALLOC_LOGE("Attempt to call flushLockedBuffer() on an unlocked buffer (%p)", handle); - hidl_cb(Error::BAD_BUFFER, hidl_handle{}); - return; + return Error::BAD_BUFFER; } mali_gralloc_ion_sync_end(private_handle, false, true); - hidl_cb(Error::NONE, hidl_handle{}); + return Error::NONE; } -Error rereadLockedBuffer(void *buffer) +Error rereadLockedBuffer(buffer_handle_t handle) { - buffer_handle_t handle = gRegisteredHandles->get(buffer); if (private_handle_t::validate(handle) < 0) { MALI_GRALLOC_LOGE("Buffer: %p is corrupted", handle); @@ -586,31 +546,29 @@ Error rereadLockedBuffer(void *buffer) return Error::NONE; } -void get(void *buffer, const IMapper::MetadataType &metadataType, IMapper::get_cb hidl_cb) +Error get(buffer_handle_t buffer, const MetadataType &metadataType, std::vector<uint8_t> &vec) { /* The buffer must have been allocated by Gralloc */ - const private_handle_t *handle = static_cast<const private_handle_t *>(gRegisteredHandles->get(buffer)); + const private_handle_t *handle = static_cast<const private_handle_t *>(buffer); if (handle == nullptr) { MALI_GRALLOC_LOGE("Buffer: %p has not been registered with Gralloc", buffer); - hidl_cb(Error::BAD_BUFFER, hidl_vec<uint8_t>()); - return; + return Error::BAD_BUFFER; } if (mali_gralloc_reference_validate((buffer_handle_t)handle) < 0) { MALI_GRALLOC_LOGE("Buffer: %p is not imported", handle); - hidl_cb(Error::BAD_VALUE, hidl_vec<uint8_t>()); - return; + return Error::BAD_VALUE; } - get_metadata(handle, metadataType, hidl_cb); + return get_metadata(handle, metadataType, vec); } -Error set(void *buffer, const IMapper::MetadataType &metadataType, const hidl_vec<uint8_t> &metadata) +Error set(buffer_handle_t buffer, const MetadataType &metadataType, const hidl_vec<uint8_t> &metadata) { /* The buffer must have been allocated by Gralloc */ - const private_handle_t *handle = static_cast<const private_handle_t *>(gRegisteredHandles->get(buffer)); + const private_handle_t *handle = static_cast<const private_handle_t *>(buffer); if (handle == nullptr) { MALI_GRALLOC_LOGE("Buffer: %p has not been registered with Gralloc", buffer); @@ -626,135 +584,137 @@ Error set(void *buffer, const IMapper::MetadataType &metadataType, const hidl_ve return set_metadata(handle, metadataType, metadata); } -void listSupportedMetadataTypes(IMapper::listSupportedMetadataTypes_cb hidl_cb) +MetadataTypeDescription describeStandard(StandardMetadataType meta, bool isGettable, bool isSettable) +{ + return MetadataTypeDescription(MetadataType(GRALLOC4_STANDARD_METADATA_TYPE, + static_cast<uint64_t>(meta)), "", isGettable, isSettable); +} + +std::vector<MetadataTypeDescription> listSupportedMetadataTypes() { /* Returns a vector of {metadata type, description, isGettable, isSettable} * Only non-standardMetadataTypes require a description. */ - hidl_vec<IMapper::MetadataTypeDescription> descriptions = { - { android::gralloc4::MetadataType_BufferId, "", true, false }, - { android::gralloc4::MetadataType_Name, "", true, false }, - { android::gralloc4::MetadataType_Width, "", true, false }, - { android::gralloc4::MetadataType_Height, "", true, false }, - { android::gralloc4::MetadataType_LayerCount, "", true, false }, - { android::gralloc4::MetadataType_PixelFormatRequested, "", true, false }, - { android::gralloc4::MetadataType_PixelFormatFourCC, "", true, false }, - { android::gralloc4::MetadataType_PixelFormatModifier, "", true, false }, - { android::gralloc4::MetadataType_Usage, "", true, false }, - { android::gralloc4::MetadataType_AllocationSize, "", true, false }, - { android::gralloc4::MetadataType_ProtectedContent, "", true, false }, - { android::gralloc4::MetadataType_Compression, "", true, false }, - { android::gralloc4::MetadataType_Interlaced, "", true, false }, - { android::gralloc4::MetadataType_ChromaSiting, "", true, false }, - { android::gralloc4::MetadataType_PlaneLayouts, "", true, false }, - { android::gralloc4::MetadataType_Dataspace, "", true, true }, - { android::gralloc4::MetadataType_BlendMode, "", true, true }, - { android::gralloc4::MetadataType_Smpte2086, "", true, true }, - { android::gralloc4::MetadataType_Cta861_3, "", true, true }, - { android::gralloc4::MetadataType_Smpte2094_40, "", true, true }, - { android::gralloc4::MetadataType_Crop, "", true, true }, + std::array<MetadataTypeDescription, 23> descriptions = { + describeStandard(StandardMetadataType::BUFFER_ID, true, false ), + describeStandard(StandardMetadataType::NAME, true, false ), + describeStandard(StandardMetadataType::WIDTH, true, false ), + describeStandard(StandardMetadataType::STRIDE, true, false ), + describeStandard(StandardMetadataType::HEIGHT, true, false ), + describeStandard(StandardMetadataType::LAYER_COUNT, true, false ), + describeStandard(StandardMetadataType::PIXEL_FORMAT_REQUESTED, true, false ), + describeStandard(StandardMetadataType::PIXEL_FORMAT_FOURCC, true, false ), + describeStandard(StandardMetadataType::PIXEL_FORMAT_MODIFIER, true, false ), + describeStandard(StandardMetadataType::USAGE, true, false ), + describeStandard(StandardMetadataType::ALLOCATION_SIZE, true, false ), + describeStandard(StandardMetadataType::PROTECTED_CONTENT, true, false ), + describeStandard(StandardMetadataType::COMPRESSION, true, false ), + describeStandard(StandardMetadataType::INTERLACED, true, false ), + describeStandard(StandardMetadataType::CHROMA_SITING, true, false ), + describeStandard(StandardMetadataType::PLANE_LAYOUTS, true, false ), + describeStandard(StandardMetadataType::DATASPACE, true, true ), + describeStandard(StandardMetadataType::BLEND_MODE, true, true ), + describeStandard(StandardMetadataType::SMPTE2086, true, true ), + describeStandard(StandardMetadataType::CTA861_3, true, true ), + describeStandard(StandardMetadataType::SMPTE2094_40, true, true ), + describeStandard(StandardMetadataType::CROP, true, true ), /* Arm vendor metadata */ { ArmMetadataType_PLANE_FDS, - "Vector of file descriptors of each plane", true, false }, - }; - hidl_cb(Error::NONE, descriptions); - return; + "Vector of file descriptors of each plane", true, false}, + }; + return std::vector<MetadataTypeDescription>(descriptions.begin(), descriptions.end()); } -static hidl_vec<IMapper::MetadataDump> dumpBufferHelper(const private_handle_t* handle) +static BufferDump dumpBufferHelper(const private_handle_t *handle) { - hidl_vec<IMapper::MetadataType> standardMetadataTypes = { - android::gralloc4::MetadataType_BufferId, - android::gralloc4::MetadataType_Name, - android::gralloc4::MetadataType_Width, - android::gralloc4::MetadataType_Height, - android::gralloc4::MetadataType_LayerCount, - android::gralloc4::MetadataType_PixelFormatRequested, - android::gralloc4::MetadataType_PixelFormatFourCC, - android::gralloc4::MetadataType_PixelFormatModifier, - android::gralloc4::MetadataType_Usage, - android::gralloc4::MetadataType_AllocationSize, - android::gralloc4::MetadataType_ProtectedContent, - android::gralloc4::MetadataType_Compression, - android::gralloc4::MetadataType_Interlaced, - android::gralloc4::MetadataType_ChromaSiting, - android::gralloc4::MetadataType_PlaneLayouts, - android::gralloc4::MetadataType_Dataspace, - android::gralloc4::MetadataType_BlendMode, - android::gralloc4::MetadataType_Smpte2086, - android::gralloc4::MetadataType_Cta861_3, - android::gralloc4::MetadataType_Smpte2094_40, - android::gralloc4::MetadataType_Crop, + static std::array<MetadataType, 21> standardMetadataTypes = { + MetadataType(StandardMetadataType::BUFFER_ID), + MetadataType(StandardMetadataType::NAME), + MetadataType(StandardMetadataType::WIDTH), + MetadataType(StandardMetadataType::HEIGHT), + MetadataType(StandardMetadataType::LAYER_COUNT), + MetadataType(StandardMetadataType::PIXEL_FORMAT_REQUESTED), + MetadataType(StandardMetadataType::PIXEL_FORMAT_FOURCC), + MetadataType(StandardMetadataType::PIXEL_FORMAT_MODIFIER), + MetadataType(StandardMetadataType::USAGE), + MetadataType(StandardMetadataType::ALLOCATION_SIZE), + MetadataType(StandardMetadataType::PROTECTED_CONTENT), + MetadataType(StandardMetadataType::COMPRESSION), + MetadataType(StandardMetadataType::INTERLACED), + MetadataType(StandardMetadataType::CHROMA_SITING), + MetadataType(StandardMetadataType::PLANE_LAYOUTS), + MetadataType(StandardMetadataType::DATASPACE), + MetadataType(StandardMetadataType::BLEND_MODE), + MetadataType(StandardMetadataType::SMPTE2086), + MetadataType(StandardMetadataType::CTA861_3), + MetadataType(StandardMetadataType::SMPTE2094_40), + MetadataType(StandardMetadataType::CROP), }; - std::vector<IMapper::MetadataDump> metadataDumps; + std::vector<MetadataDump> metadataDumps; for (const auto& metadataType: standardMetadataTypes) { - get_metadata(handle, metadataType, [&metadataDumps, &metadataType](Error error, hidl_vec<uint8_t> metadata) { - switch(error) - { - case Error::NONE: - metadataDumps.push_back({metadataType, metadata}); - break; - case Error::UNSUPPORTED: - default: - return; - } - }); + std::vector<uint8_t> metadata; + Error error = get_metadata(handle, metadataType, metadata); + if (error == Error::NONE) + { + metadataDumps.push_back(MetadataDump(MetadataType(metadataType), metadata)); + } + else + { + return BufferDump(); + } } - return hidl_vec<IMapper::MetadataDump>(metadataDumps); + return BufferDump(metadataDumps); } -void dumpBuffer(void *buffer, IMapper::dumpBuffer_cb hidl_cb) +Error dumpBuffer(buffer_handle_t buffer, BufferDump &bufferDump) { - IMapper::BufferDump bufferDump{}; - auto handle = static_cast<const private_handle_t *>(gRegisteredHandles->get(buffer)); + auto handle = static_cast<const private_handle_t *>(buffer); if (handle == nullptr) { MALI_GRALLOC_LOGE("Buffer: %p has not been registered with Gralloc", buffer); - hidl_cb(Error::BAD_BUFFER, bufferDump); - return; + return Error::BAD_BUFFER; } - bufferDump.metadataDump = dumpBufferHelper(handle); - hidl_cb(Error::NONE, bufferDump); + bufferDump = dumpBufferHelper(handle); + return Error::NONE; } -void dumpBuffers(IMapper::dumpBuffers_cb hidl_cb) +std::vector<BufferDump> dumpBuffers() { - std::vector<IMapper::BufferDump> bufferDumps; + std::vector<BufferDump> bufferDumps; gRegisteredHandles->for_each([&bufferDumps](buffer_handle_t buffer) { - IMapper::BufferDump bufferDump { dumpBufferHelper(static_cast<const private_handle_t *>(buffer)) }; + BufferDump bufferDump { dumpBufferHelper(static_cast<const private_handle_t *>(buffer)) }; bufferDumps.push_back(bufferDump); }); - hidl_cb(Error::NONE, hidl_vec<IMapper::BufferDump>(bufferDumps)); + return bufferDumps; } -void getReservedRegion(void *buffer, IMapper::getReservedRegion_cb hidl_cb) +Error getReservedRegion(buffer_handle_t buffer, void **outReservedRegion, uint64_t &outReservedSize) { - auto handle = static_cast<const private_handle_t *>(gRegisteredHandles->get(buffer)); + auto handle = static_cast<const private_handle_t *>(buffer); if (handle == nullptr) { MALI_GRALLOC_LOGE("Buffer: %p has not been registered with Gralloc", buffer); - hidl_cb(Error::BAD_BUFFER, 0, 0); - return; + return Error::BAD_BUFFER; } else if (handle->reserved_region_size == 0) { MALI_GRALLOC_LOGE("Buffer: %p has no reserved region", buffer); - hidl_cb(Error::BAD_BUFFER, 0, 0); - return; + return Error::BAD_BUFFER; } auto metadata_addr_oe = mali_gralloc_reference_get_metadata_addr(handle); if (!metadata_addr_oe.has_value()) { - hidl_cb(Error::BAD_BUFFER, 0, 0); + return Error::BAD_BUFFER; } - void *reserved_region = static_cast<std::byte *>(metadata_addr_oe.value()) + *outReservedRegion = static_cast<std::byte *>(metadata_addr_oe.value()) + mapper::common::shared_metadata_size(); - hidl_cb(Error::NONE, reserved_region, handle->reserved_region_size); + outReservedSize = handle->reserved_region_size; + return Error::NONE; } } // namespace common diff --git a/gralloc4/src/hidl_common/Mapper.h b/gralloc4/src/hidl_common/Mapper.h index 6d114cc..c77128c 100644 --- a/gralloc4/src/hidl_common/Mapper.h +++ b/gralloc4/src/hidl_common/Mapper.h @@ -23,7 +23,9 @@ #include "mali_gralloc_log.h" #include "core/mali_gralloc_bufferdescriptor.h" -#include "4.x/gralloc_mapper_hidl_header.h" +#include "MapperMetadata.h" +#include "hidl_common.h" +#include "mali_gralloc_error.h" namespace arm { @@ -32,24 +34,42 @@ namespace mapper namespace common { -using android::hardware::hidl_handle; -using android::hardware::hidl_vec; + +using aidl::android::hardware::graphics::common::Rect; + using android::hardware::Void; +class GrallocRect { + public: + int left; + int top; + int right; + int bottom; + GrallocRect(Rect rect) { + left = rect.left; + top = rect.top; + right = rect.right; + bottom = rect.bottom; + } +#ifdef GRALLOC_MAPPER_4 + GrallocRect(IMapper::Rect rect) { + left = rect.left; + top = rect.top; + right = rect.left + rect.width; + bottom = rect.top + rect.height; + } +#endif +}; + /** * Imports a raw buffer handle to create an imported buffer handle for use with * the rest of the mapper or with other in-process libraries. * - * @param rawHandle [in] Raw buffer handle to import. - * @param hidl_cb [in] HIDL Callback function to export output information - * @param hidl_cb [in] HIDL callback function generating - - * error : NONE upon success. Otherwise, - * BAD_BUFFER for an invalid buffer - * NO_RESOURCES when the raw handle cannot be imported - * BAD_VALUE when any of the specified attributes are invalid - * buffer : Imported buffer handle + * @param bufferHandle [in] Buffer handle to import. + * @param outBuffer [in] imported Buffer */ -void importBuffer(const hidl_handle &rawHandle, IMapper::importBuffer_cb hidl_cb); +Error importBuffer(const native_handle_t *inBuffer, buffer_handle_t *outBuffer); + /** * Frees a buffer handle and releases all the resources associated with it @@ -59,7 +79,10 @@ void importBuffer(const hidl_handle &rawHandle, IMapper::importBuffer_cb hidl_cb * @return Error::BAD_BUFFER for an invalid buffer / when failed to free the buffer * Error::NONE on successful free */ -Error freeBuffer(void *buffer); +Error freeBuffer(buffer_handle_t buffer); + +buffer_handle_t getBuffer(void *buffer); +native_handle_t* removeBuffer(void **buffer); /** * Locks the given buffer for the specified CPU usage. @@ -76,8 +99,7 @@ Error freeBuffer(void *buffer); * bytesPerPixel: v3.X Only. Number of bytes per pixel in the buffer * bytesPerStride: v3.X Only. Bytes per stride of the buffer */ -void lock(void *buffer, uint64_t cpuUsage, const IMapper::Rect &accessRegion, const hidl_handle &acquireFence, - IMapper::lock_cb hidl_cb); +Error lock(buffer_handle_t buffer, uint64_t cpuUsage, const GrallocRect &accessRegion, int acquireFence, void **outData); /** * Unlocks a buffer to indicate all CPU accesses to the buffer have completed @@ -88,7 +110,7 @@ void lock(void *buffer, uint64_t cpuUsage, const IMapper::Rect &accessRegion, co * BAD_BUFFER for an invalid buffer * releaseFence: Referrs to a sync fence object */ -void unlock(void *buffer, IMapper::unlock_cb hidl_cb); +Error unlock(const native_handle_t *buffer, int *releaseFence); /** * Validates the buffer against specified descriptor attributes @@ -102,7 +124,9 @@ void unlock(void *buffer, IMapper::unlock_cb hidl_cb); * Error::BAD_BUFFER upon bad buffer input * Error::BAD_VALUE when any of the specified attributes are invalid */ +#ifdef GRALLOC_MAPPER_4 Error validateBufferSize(void *buffer, const IMapper::BufferDescriptorInfo &descriptorInfo, uint32_t stride); +#endif /** * Get the transport size of a buffer @@ -114,7 +138,7 @@ Error validateBufferSize(void *buffer, const IMapper::BufferDescriptorInfo &desc * numFds: Number of file descriptors needed for transport * numInts: Number of integers needed for transport */ -void getTransportSize(void *buffer, IMapper::getTransportSize_cb hidl_cb); +Error getTransportSize(buffer_handle_t bufferHandle, uint32_t *outNumFds, uint32_t *outNumInts); /** * Test whether the given BufferDescriptorInfo is allocatable. @@ -125,8 +149,9 @@ void getTransportSize(void *buffer, IMapper::getTransportSize_cb hidl_cb); * BAD_VALUE, Otherwise, * supported: Whether the description can be allocated */ -void isSupported(const IMapper::BufferDescriptorInfo &description, IMapper::isSupported_cb hidl_cb); - +#ifdef GRALLOC_MAPPER_4 +bool isSupported(const IMapper::BufferDescriptorInfo &description); +#endif /* TODO: implement this feature for exynos */ /** * Flushes the CPU caches of a mapped buffer. @@ -137,7 +162,7 @@ void isSupported(const IMapper::BufferDescriptorInfo &description, IMapper::isSu * has not been locked. * releaseFence: Empty fence signaling completion as all work is completed within the call. */ -void flushLockedBuffer(void *buffer, IMapper::flushLockedBuffer_cb hidl_cb); +Error flushLockedBuffer(buffer_handle_t buffer); /* TODO: implement this feature for exynos */ /** @@ -148,7 +173,7 @@ void flushLockedBuffer(void *buffer, IMapper::flushLockedBuffer_cb hidl_cb); * @return Error::NONE upon success. * Error::BAD_BUFFER for an invalid buffer or a buffer that has not been locked. */ -Error rereadLockedBuffer(void *buffer); +Error rereadLockedBuffer(buffer_handle_t handle); /** * Retrieves a Buffer's metadata value. @@ -161,7 +186,7 @@ Error rereadLockedBuffer(void *buffer); * UNSUPPORTED on error when reading or unsupported metadata type. * metadata: Vector of bytes representing the metadata value. */ -void get(void *buffer, const IMapper::MetadataType &metadataType, IMapper::get_cb hidl_cb); +Error get(buffer_handle_t buffer, const MetadataType &metadataType, std::vector<uint8_t> &vec); /** * Sets a Buffer's metadata value. @@ -174,7 +199,7 @@ void get(void *buffer, const IMapper::MetadataType &metadataType, IMapper::get_c * Error::BAD_BUFFER on invalid buffer argument. * Error::UNSUPPORTED on error when writing or unsupported metadata type. */ -Error set(void *buffer, const IMapper::MetadataType &metadataType, const hidl_vec<uint8_t> &metadata); +Error set(buffer_handle_t buffer, const MetadataType &metadataType, const frameworks_vec<uint8_t> &metadata); /** * Lists all the MetadataTypes supported by IMapper as well as a description @@ -189,7 +214,7 @@ Error set(void *buffer, const IMapper::MetadataType &metadataType, const hidl_ve * descriptions: vector of MetadataTypeDescriptions that represent the * MetadataTypes supported by the device. */ -void listSupportedMetadataTypes(IMapper::listSupportedMetadataTypes_cb hidl_cb); +std::vector<MetadataTypeDescription> listSupportedMetadataTypes(); /** * Dumps a buffer's metadata. @@ -203,7 +228,7 @@ void listSupportedMetadataTypes(IMapper::listSupportedMetadataTypes_cb hidl_cb); * resources. * bufferDump: Struct representing the metadata being dumped */ -void dumpBuffer(void *buffer, IMapper::dumpBuffer_cb hidl_cb); +Error dumpBuffer(buffer_handle_t buffer, BufferDump &out); /** * Dumps the metadata for all the buffers in the current process. @@ -215,7 +240,7 @@ void dumpBuffer(void *buffer, IMapper::dumpBuffer_cb hidl_cb); * resources. * bufferDumps: Vector of structs representing the buffers being dumped */ -void dumpBuffers(IMapper::dumpBuffers_cb hidl_cb); +std::vector<BufferDump> dumpBuffers(); /** * Returns the region of shared memory associated with the buffer that is @@ -243,7 +268,7 @@ void dumpBuffers(IMapper::dumpBuffers_cb hidl_cb); * reservedSize: the size of the reservedRegion that was requested * in the BufferDescriptorInfo. */ -void getReservedRegion(void *buffer, IMapper::getReservedRegion_cb _hidl_cb); +Error getReservedRegion(buffer_handle_t buffer, void **outReservedRegion, uint64_t &outReservedSize); } // namespace common } // namespace mapper diff --git a/gralloc4/src/hidl_common/MapperMetadata.cpp b/gralloc4/src/hidl_common/MapperMetadata.cpp index 81835a3..76a9865 100644 --- a/gralloc4/src/hidl_common/MapperMetadata.cpp +++ b/gralloc4/src/hidl_common/MapperMetadata.cpp @@ -29,6 +29,7 @@ #include "mali_gralloc_formats.h" #include <pixel-gralloc/metadata.h> +#include <pixel-gralloc/utils.h> #include <vector> @@ -52,9 +53,13 @@ using aidl::android::hardware::graphics::common::Cta861_3; using aidl::arm::graphics::ArmMetadataType; #endif -using MetadataType = android::hardware::graphics::mapper::V4_0::IMapper::MetadataType; +bool isStandardMetadataType(const MetadataType &metadataType) { + return !std::strncmp(metadataType.name.c_str(), + GRALLOC4_STANDARD_METADATA_TYPE, + metadataType.name.size()); +} -static int get_num_planes(const private_handle_t *hnd) +int get_num_planes(const private_handle_t *hnd) { if (is_exynos_format(hnd->get_alloc_format())) { @@ -282,7 +287,7 @@ static std::vector<std::vector<PlaneLayoutComponent>> plane_layout_components_fr return std::vector<std::vector<PlaneLayoutComponent>>(0); } -static android::status_t get_plane_layouts(const private_handle_t *handle, std::vector<PlaneLayout> *layouts) +android::status_t get_plane_layouts(const private_handle_t *handle, std::vector<PlaneLayout> *layouts) { const int num_planes = get_num_planes(handle); uint32_t base_format = handle->alloc_format & MALI_GRALLOC_INTFMT_FMT_MASK; @@ -317,9 +322,9 @@ static android::status_t get_plane_layouts(const private_handle_t *handle, std:: PlaneLayout layout = {.offsetInBytes = offset, .sampleIncrementInBits = sample_increment_in_bits, - .strideInBytes = handle->plane_info[plane_index].byte_stride, - .widthInSamples = handle->plane_info[plane_index].alloc_width, - .heightInSamples = handle->plane_info[plane_index].alloc_height, + .strideInBytes = static_cast<int64_t>(handle->plane_info[plane_index].byte_stride), + .widthInSamples = static_cast<int64_t>(handle->plane_info[plane_index].alloc_width), + .heightInSamples = static_cast<int64_t>(handle->plane_info[plane_index].alloc_height), .totalSizeInBytes = plane_size, .horizontalSubsampling = (plane_index == 0 ? 1 : format_info.hsub), .verticalSubsampling = (plane_index == 0 ? 1 : format_info.vsub), @@ -371,9 +376,9 @@ static android::status_t get_plane_layouts(const private_handle_t *handle, std:: PlaneLayout layout = {.offsetInBytes = handle->plane_info[plane_index].offset, .sampleIncrementInBits = sample_increment_in_bits, - .strideInBytes = handle->plane_info[plane_index].byte_stride, - .widthInSamples = handle->plane_info[plane_index].alloc_width, - .heightInSamples = handle->plane_info[plane_index].alloc_height, + .strideInBytes = static_cast<int64_t>(handle->plane_info[plane_index].byte_stride), + .widthInSamples = static_cast<int64_t>(handle->plane_info[plane_index].alloc_width), + .heightInSamples = static_cast<int64_t>(handle->plane_info[plane_index].alloc_height), .totalSizeInBytes = plane_size, .horizontalSubsampling = (plane_index == 0 ? 1 : format_info.hsub), .verticalSubsampling = (plane_index == 0 ? 1 : format_info.vsub), @@ -386,23 +391,23 @@ static android::status_t get_plane_layouts(const private_handle_t *handle, std:: return android::OK; } -static hidl_vec<uint8_t> encodePointer(void* ptr) { +static frameworks_vec<uint8_t> encodePointer(void* ptr) { constexpr uint8_t kPtrSize = sizeof(void*); - hidl_vec<uint8_t> output(kPtrSize); + frameworks_vec<uint8_t> output(kPtrSize); std::memcpy(output.data(), &ptr, kPtrSize); return output; } -void get_metadata(const private_handle_t *handle, const IMapper::MetadataType &metadataType, IMapper::get_cb hidl_cb) +Error get_metadata(const private_handle_t *handle, const MetadataType &metadataType, std::vector<uint8_t> &outVec) { android::status_t err = android::OK; - hidl_vec<uint8_t> vec; + frameworks_vec<uint8_t> vec; - if (android::gralloc4::isStandardMetadataType(metadataType)) + if (isStandardMetadataType(metadataType)) { - switch (android::gralloc4::getStandardMetadataTypeValue(metadataType)) + switch (static_cast<StandardMetadataType>(metadataType.value)) { case StandardMetadataType::BUFFER_ID: err = android::gralloc4::encodeBufferId(handle->backing_store_id, &vec); @@ -424,7 +429,7 @@ void get_metadata(const private_handle_t *handle, const IMapper::MetadataType &m err = android::gralloc4::encodeLayerCount(handle->layer_count, &vec); break; case StandardMetadataType::PIXEL_FORMAT_REQUESTED: - err = android::gralloc4::encodePixelFormatRequested(static_cast<PixelFormat>(handle->req_format), &vec); + err = android::gralloc4::encodePixelFormatRequested(static_cast<hidl::PixelFormat>(handle->req_format), &vec); break; case StandardMetadataType::PIXEL_FORMAT_FOURCC: err = android::gralloc4::encodePixelFormatFourCC(drm_fourcc_from_handle(handle), &vec); @@ -449,7 +454,7 @@ void get_metadata(const private_handle_t *handle, const IMapper::MetadataType &m { /* This is set to 1 if the buffer has protected content. */ const int is_protected = - (((handle->consumer_usage | handle->producer_usage) & BufferUsage::PROTECTED) == 0) ? 0 : 1; + (((handle->consumer_usage | handle->producer_usage) & static_cast<uint64_t>(BufferUsage::PROTECTED)) == 0) ? 0 : 1; err = android::gralloc4::encodeProtectedContent(is_protected, &vec); break; } @@ -572,7 +577,7 @@ void get_metadata(const private_handle_t *handle, const IMapper::MetadataType &m else if (metadataType.name == ::pixel::graphics::kPixelMetadataTypeName) { switch (static_cast<::pixel::graphics::MetadataType>(metadataType.value)) { case ::pixel::graphics::MetadataType::VIDEO_HDR: - vec = encodePointer(get_video_hdr(handle)); + vec = ::pixel::graphics::utils::encode(get_video_hdr(handle)); break; case ::pixel::graphics::MetadataType::VIDEO_ROI: { @@ -580,10 +585,19 @@ void get_metadata(const private_handle_t *handle, const IMapper::MetadataType &m if (roi == nullptr) { err = android::BAD_VALUE; } else { - vec = encodePointer(roi); + vec = ::pixel::graphics::utils::encode(roi); } break; } + case ::pixel::graphics::MetadataType::PLANE_DMA_BUFS: + { + std::vector<int> plane_fds(MAX_BUFFER_FDS, -1); + for (int i = 0; i < get_num_planes(handle); i++) { + plane_fds[i] = handle->fds[handle->plane_info[i].fd_idx]; + } + vec = ::pixel::graphics::utils::encode(plane_fds); + break; + } default: err = android::BAD_VALUE; } @@ -593,16 +607,16 @@ void get_metadata(const private_handle_t *handle, const IMapper::MetadataType &m err = android::BAD_VALUE; } - hidl_cb((err) ? Error::UNSUPPORTED : Error::NONE, vec); + outVec = std::vector<uint8_t>(vec); + return ((err) ? Error::UNSUPPORTED : Error::NONE); } -Error set_metadata(const private_handle_t *handle, const IMapper::MetadataType &metadataType, - const hidl_vec<uint8_t> &metadata) +Error set_metadata(const private_handle_t *handle, const MetadataType &metadataType, const frameworks_vec<uint8_t> &metadata) { - if (android::gralloc4::isStandardMetadataType(metadataType)) + if (isStandardMetadataType(metadataType)) { android::status_t err = android::OK; - switch (android::gralloc4::getStandardMetadataTypeValue(metadataType)) + switch (static_cast<StandardMetadataType>(metadataType.value)) { case StandardMetadataType::DATASPACE: { @@ -695,12 +709,12 @@ Error set_metadata(const private_handle_t *handle, const IMapper::MetadataType & } } -void getFromBufferDescriptorInfo(IMapper::BufferDescriptorInfo const &description, - IMapper::MetadataType const &metadataType, - IMapper::getFromBufferDescriptorInfo_cb hidl_cb) +#ifdef GRALLOC_MAPPER_4 +Error getFromBufferDescriptorInfo(IMapper::BufferDescriptorInfo const &description, + MetadataType const &metadataType, std::vector<uint8_t> &outVec) { /* This will hold the metadata that is returned. */ - hidl_vec<uint8_t> vec; + frameworks_vec<uint8_t> vec; buffer_descriptor_t descriptor; descriptor.width = description.width; @@ -716,8 +730,8 @@ void getFromBufferDescriptorInfo(IMapper::BufferDescriptorInfo const &descriptio if (alloc_result != 0) { MALI_GRALLOC_LOGV("Allocation for the given description will not succeed. error: %d", alloc_result); - hidl_cb(Error::BAD_VALUE, vec); - return; + outVec = vec; + return Error::BAD_VALUE; } /* Create buffer handle from the initialized descriptor without a backing store or shared metadata region. * Used to share functionality with the normal metadata get function that can only use the allocated buffer handle @@ -727,11 +741,11 @@ void getFromBufferDescriptorInfo(IMapper::BufferDescriptorInfo const &descriptio descriptor.hal_format, descriptor.alloc_format, descriptor.width, descriptor.height, descriptor.pixel_stride, descriptor.layer_count, descriptor.plane_info); - if (android::gralloc4::isStandardMetadataType(metadataType)) + if (isStandardMetadataType(metadataType)) { android::status_t err = android::OK; - switch (android::gralloc4::getStandardMetadataTypeValue(metadataType)) + switch (static_cast<StandardMetadataType>(metadataType.value)) { case StandardMetadataType::NAME: err = android::gralloc4::encodeName(description.name, &vec); @@ -746,7 +760,7 @@ void getFromBufferDescriptorInfo(IMapper::BufferDescriptorInfo const &descriptio err = android::gralloc4::encodeLayerCount(description.layerCount, &vec); break; case StandardMetadataType::PIXEL_FORMAT_REQUESTED: - err = android::gralloc4::encodePixelFormatRequested(static_cast<PixelFormat>(description.format), &vec); + err = android::gralloc4::encodePixelFormatRequested(static_cast<hidl::PixelFormat>(description.format), &vec); break; case StandardMetadataType::USAGE: err = android::gralloc4::encodeUsage(description.usage, &vec); @@ -774,7 +788,7 @@ void getFromBufferDescriptorInfo(IMapper::BufferDescriptorInfo const &descriptio { /* This is set to 1 if the buffer has protected content. */ const int is_protected = - (((partial_handle.consumer_usage | partial_handle.producer_usage) & BufferUsage::PROTECTED)) ? 1 : 0; + (((partial_handle.consumer_usage | partial_handle.producer_usage) & static_cast<uint64_t>(BufferUsage::PROTECTED))) ? 1 : 0; err = android::gralloc4::encodeProtectedContent(is_protected, &vec); break; } @@ -876,14 +890,17 @@ void getFromBufferDescriptorInfo(IMapper::BufferDescriptorInfo const &descriptio default: err = android::BAD_VALUE; } - hidl_cb((err) ? Error::UNSUPPORTED : Error::NONE, vec); + outVec = vec; + return ((err) ? Error::UNSUPPORTED : Error::NONE); } else { - hidl_cb(Error::UNSUPPORTED, vec); + outVec = vec; + return Error::UNSUPPORTED; } } +#endif // GRALLOC_MAPPER_4 } // namespace common } // namespace mapper } // namespace arm diff --git a/gralloc4/src/hidl_common/MapperMetadata.h b/gralloc4/src/hidl_common/MapperMetadata.h index cbf9b47..7027487 100644 --- a/gralloc4/src/hidl_common/MapperMetadata.h +++ b/gralloc4/src/hidl_common/MapperMetadata.h @@ -23,13 +23,16 @@ #include "mali_gralloc_log.h" #include "core/mali_gralloc_bufferdescriptor.h" #include "mali_gralloc_buffer.h" +#include "mali_gralloc_error.h" +#include <cstring> -#include "4.x/gralloc_mapper_hidl_header.h" +#include "hidl_common/hidl_common.h" +#include <algorithm> +#include <iterator> #include <aidl/arm/graphics/Compression.h> #include <aidl/arm/graphics/ArmMetadataType.h> - namespace arm { namespace mapper @@ -44,8 +47,116 @@ const static ExtendableType Compression_AFBC{ GRALLOC_ARM_COMPRESSION_TYPE_NAME, static_cast<int64_t>(aidl::arm::graphics::Compression::AFBC) }; #define GRALLOC_ARM_METADATA_TYPE_NAME "arm.graphics.ArmMetadataType" -const static IMapper::MetadataType ArmMetadataType_PLANE_FDS{ GRALLOC_ARM_METADATA_TYPE_NAME, - static_cast<int64_t>(aidl::arm::graphics::ArmMetadataType::PLANE_FDS) }; + + +class MetadataType { + public: + std::string name; + uint64_t value; +#ifdef GRALLOC_MAPPER_4 + MetadataType(const IMapper::MetadataType &meta) { + name = meta.name; + value = meta.value; + } + operator IMapper::MetadataType() const { + IMapper::MetadataType meta; + meta.name = name; + meta.value = value; + return meta; + } +#endif + MetadataType() {} + MetadataType(std::string strname, uint64_t val) { + name = strname; + value = val; + } + MetadataType(StandardMetadataType meta) : MetadataType(GRALLOC4_STANDARD_METADATA_TYPE, static_cast<uint64_t>(meta)) {} +}; + +const static MetadataType ArmMetadataType_PLANE_FDS = MetadataType(GRALLOC_ARM_METADATA_TYPE_NAME, static_cast<int64_t>(aidl::arm::graphics::ArmMetadataType::PLANE_FDS)); + +constexpr int RES_SIZE = 32; + +struct MetadataTypeDescription { + MetadataType metadataType; + const char* description; + bool isGettable; + bool isSettable; +#ifdef GRALLOC_MAPPER_4 + MetadataTypeDescription(const IMapper::MetadataTypeDescription &desc) { + metadataType = desc.metadataType; + description = (desc.description).c_str(); + isGettable = desc.isGettable; + isSettable = desc.isSettable; + } + operator IMapper::MetadataTypeDescription() const { + IMapper::MetadataTypeDescription desc; + desc.metadataType = static_cast<IMapper::MetadataType>(metadataType); + desc.description = description; + desc.isGettable = isGettable; + desc.isSettable = isSettable; + return desc; + } +#endif + MetadataTypeDescription(MetadataType meta, const char* desc, bool gettable, bool settable) { + metadataType = meta; + description = desc; + isGettable = gettable; + isSettable = settable; + } +}; + +struct MetadataDump { + MetadataType metadataType; + std::vector<uint8_t> metadata; +#ifdef GRALLOC_MAPPER_4 + MetadataDump(const IMapper::MetadataDump &meta) { + metadataType = MetadataType(meta.metadataType); + metadata = static_cast<std::vector<uint8_t> >(metadata); + } + operator IMapper::MetadataDump() const { + IMapper::MetadataDump dump; + dump.metadataType = static_cast<IMapper::MetadataType>(metadataType); + dump.metadata = hidl_vec(metadata); + return dump; + } +#endif + MetadataDump() {} + MetadataDump(MetadataType metaType, std::vector<uint8_t> &meta) { + metadataType = metaType; + metadata = meta; + } +}; + +struct BufferDump { + std::vector<MetadataDump> metadataDump; +#ifdef GRALLOC_MAPPER_4 + BufferDump(const IMapper::BufferDump &dump) { + for (auto meta : dump.metadataDump) + metadataDump.push_back(MetadataDump(meta)); + } + operator IMapper::BufferDump() const { + IMapper::BufferDump bufferdump; + std::vector<IMapper::MetadataDump> metaDump; + for (auto meta : metadataDump) { + metaDump.push_back(static_cast<IMapper::MetadataDump>(meta)); + } + bufferdump.metadataDump = metaDump; + return bufferdump; + } +#endif + BufferDump(std::vector<MetadataDump> &meta) { metadataDump = meta; } + BufferDump() {} +}; + + +int get_num_planes(const private_handle_t *hnd); + +static std::vector<std::vector<PlaneLayoutComponent>> plane_layout_components_from_handle(const private_handle_t *hnd); + +android::status_t get_plane_layouts(const private_handle_t *handle, std::vector<PlaneLayout> *layouts); + +bool isStandardMetadataType(const MetadataType &metadataType); /** * Retrieves a Buffer's metadata value. @@ -57,7 +168,7 @@ const static IMapper::MetadataType ArmMetadataType_PLANE_FDS{ GRALLOC_ARM_METADA * UNSUPPORTED on error when reading or unsupported metadata type. * metadata: Vector of bytes representing the metadata value. */ -void get_metadata(const private_handle_t *handle, const IMapper::MetadataType &metadataType, IMapper::get_cb hidl_cb); +Error get_metadata(const private_handle_t *handle, const MetadataType &metadataType, std::vector<uint8_t> &outVec); /** * Sets a Buffer's metadata value. @@ -69,8 +180,7 @@ void get_metadata(const private_handle_t *handle, const IMapper::MetadataType &m * @return Error::NONE on success. * Error::UNSUPPORTED on error when writing or unsupported metadata type. */ -Error set_metadata(const private_handle_t *handle, const IMapper::MetadataType &metadataType, - const hidl_vec<uint8_t> &metadata); +Error set_metadata(const private_handle_t *handle, const MetadataType &metadataType, const frameworks_vec<uint8_t> &metadata); /** * Query basic metadata information about a buffer form its descriptor before allocation. @@ -82,9 +192,9 @@ Error set_metadata(const private_handle_t *handle, const IMapper::MetadataType & * UNSUPPORTED on unsupported metadata type. * metadata: Vector of bytes representing the metadata value. */ -void getFromBufferDescriptorInfo(IMapper::BufferDescriptorInfo const &description, - IMapper::MetadataType const &metadataType, - IMapper::getFromBufferDescriptorInfo_cb hidl_cb); +#ifdef GRALLOC_MAPPER_4 +Error getFromBufferDescriptorInfo(IMapper::BufferDescriptorInfo const &description, MetadataType const &metadataType, std::vector<uint8_t> &outVec); +#endif } // namespace common } // namespace mapper diff --git a/gralloc4/src/hidl_common/RegisteredHandlePool.cpp b/gralloc4/src/hidl_common/RegisteredHandlePool.cpp index b598d8a..3c99e4f 100644 --- a/gralloc4/src/hidl_common/RegisteredHandlePool.cpp +++ b/gralloc4/src/hidl_common/RegisteredHandlePool.cpp @@ -24,9 +24,9 @@ bool RegisteredHandlePool::add(buffer_handle_t bufferHandle) return bufPool.insert(bufferHandle).second; } -native_handle_t* RegisteredHandlePool::remove(void* buffer) +native_handle_t* RegisteredHandlePool::remove(buffer_handle_t buffer) { - auto bufferHandle = static_cast<native_handle_t*>(buffer); + auto bufferHandle = const_cast<native_handle_t*>(buffer); std::lock_guard<std::mutex> lock(mutex); return bufPool.erase(bufferHandle) == 1 ? bufferHandle : nullptr; @@ -40,8 +40,13 @@ buffer_handle_t RegisteredHandlePool::get(const void* buffer) return bufPool.count(bufferHandle) == 1 ? bufferHandle : nullptr; } +bool RegisteredHandlePool::isRegistered(buffer_handle_t buffer) +{ + return (bufPool.find(buffer) != bufPool.end()); +} + void RegisteredHandlePool::for_each(std::function<void(const buffer_handle_t &)> fn) { std::lock_guard<std::mutex> lock(mutex); std::for_each(bufPool.begin(), bufPool.end(), fn); -}
\ No newline at end of file +} diff --git a/gralloc4/src/hidl_common/RegisteredHandlePool.h b/gralloc4/src/hidl_common/RegisteredHandlePool.h index d3fb9b0..b318e54 100644 --- a/gralloc4/src/hidl_common/RegisteredHandlePool.h +++ b/gralloc4/src/hidl_common/RegisteredHandlePool.h @@ -33,7 +33,7 @@ public: bool add(buffer_handle_t bufferHandle); /* Retrieves and removes the buffer handle from internal list */ - native_handle_t* remove(void* buffer); + native_handle_t* remove(buffer_handle_t buffer); /* Retrieves the buffer handle from internal list */ buffer_handle_t get(const void* buffer); @@ -41,6 +41,8 @@ public: /* Applies a function to each buffer handle */ void for_each(std::function<void(const buffer_handle_t &)> fn); + bool isRegistered(buffer_handle_t handle); + private: std::mutex mutex; std::unordered_set<buffer_handle_t> bufPool; diff --git a/gralloc4/src/hidl_common/SharedMetadata.h b/gralloc4/src/hidl_common/SharedMetadata.h index f3aad7c..5e72b1c 100644 --- a/gralloc4/src/hidl_common/SharedMetadata.h +++ b/gralloc4/src/hidl_common/SharedMetadata.h @@ -26,8 +26,6 @@ #include "core/mali_gralloc_bufferdescriptor.h" #include "gralloc_helper.h" -#include "4.x/gralloc_mapper_hidl_header.h" - #include "SharedMetadata_struct.h" namespace arm diff --git a/gralloc4/src/hidl_common/hidl_common.h b/gralloc4/src/hidl_common/hidl_common.h new file mode 100644 index 0000000..28850e9 --- /dev/null +++ b/gralloc4/src/hidl_common/hidl_common.h @@ -0,0 +1,42 @@ +#ifndef HIDL_COMMON +#define HIDL_COMMON + +#include "mali_fourcc.h" +#include <gralloctypes/Gralloc4.h> +#include <aidl/android/hardware/graphics/common/BufferUsage.h> +#include <aidl/android/hardware/graphics/common/PixelFormat.h> +#include <aidl/android/hardware/graphics/common/StandardMetadataType.h> + +using aidl::android::hardware::graphics::common::BufferUsage; +using aidl::android::hardware::graphics::common::PixelFormat; +using aidl::android::hardware::graphics::common::StandardMetadataType; +using aidl::android::hardware::graphics::common::ExtendableType; +using aidl::android::hardware::graphics::common::PlaneLayout; +using aidl::android::hardware::graphics::common::PlaneLayoutComponent; +using aidl::android::hardware::graphics::common::Rect; +using aidl::android::hardware::graphics::common::BlendMode; +using aidl::android::hardware::graphics::common::Dataspace; + +namespace hidl { +using PixelFormat = android::hardware::graphics::common::V1_2::PixelFormat; +} // namespace hidl + + +template <typename T> +using frameworks_vec = android::hardware::hidl_vec<T>; + +#ifdef GRALLOC_MAPPER_4 + +#include <android/hardware/graphics/mapper/4.0/IMapper.h> + +namespace hidl { +using android::hardware::graphics::mapper::V4_0::Error; +} // namespace hidl +using android::hardware::graphics::mapper::V4_0::BufferDescriptor; +using android::hardware::graphics::mapper::V4_0::IMapper; + +using frameworks_handle = android::hardware::hidl_handle; + +#endif // GRALLOC_MAPPER_4 + +#endif // HIDL_COMMON diff --git a/gralloc4/src/libGralloc4Wrapper/include/gralloc4/gralloc_vendor_interface.h b/gralloc4/src/libGralloc4Wrapper/include/gralloc4/gralloc_vendor_interface.h deleted file mode 100644 index cbd98d9..0000000 --- a/gralloc4/src/libGralloc4Wrapper/include/gralloc4/gralloc_vendor_interface.h +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright (C) 2020 Google LLC. All rights reserved. - * - * Copyright (C) 2008 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef GRALLOC_VENDOR_INTERFACE_H -#define GRALLOC_VENDOR_INTERFACE_H - -#include <cutils/native_handle.h> -#include <sys/types.h> -#include <cstdint> -#include <system/graphics-base-v1.0.h> -#include <android/hardware/graphics/mapper/4.0/IMapper.h> - - -namespace android::hardware::graphics::allocator::priv { - -struct Descriptor; -Descriptor *createDescriptor(); -void deleteDescriptor(Descriptor *descriptor); - -void setProducerUsage(Descriptor &descriptor, uint64_t usage); -void setConsumerUsage(Descriptor &descriptor, uint64_t usage); -void setPlaneCount(Descriptor &descriptor, int count); -void setPlane(Descriptor &descriptor, int index, int fd, size_t size, off_t offset, int stride_byte); -void setWidth(Descriptor &descriptor, int width); -void setHeight(Descriptor &descriptor, int height); -void setStridePixel(Descriptor &descriptor, int stride_pixel); -void setFormat(Descriptor &descriptor, int format); - -buffer_handle_t createNativeHandle(const Descriptor &descriptor); - -int freeImportedHandle(void *handle); - -} // namespace android::hardware::graphics::allocator::priv - -#endif diff --git a/gralloc4/src/libGralloc4Wrapper/wrapper.cpp b/gralloc4/src/libGralloc4Wrapper/wrapper.cpp deleted file mode 100644 index f9b1b9c..0000000 --- a/gralloc4/src/libGralloc4Wrapper/wrapper.cpp +++ /dev/null @@ -1,218 +0,0 @@ -#include "gralloc4/gralloc_vendor_interface.h" -#include <vector> -#include <sys/stat.h> - -#include "core/format_info.h" -#include "core/mali_gralloc_bufferdescriptor.h" -#include "core/mali_gralloc_bufferallocation.h" -#include "allocator/mali_gralloc_ion.h" -#include "hidl_common/SharedMetadata.h" -#include "gralloc_priv.h" - -namespace android::hardware::graphics::allocator::priv { - -struct Descriptor { - unsigned int size = 0; - uint64_t producer_usage = 0; - uint64_t consumer_usage = 0; - - struct PlaneDescriptor { - int fd = -1; - size_t size = 0; - off_t offset = 0; - int stride_byte = 0; - }; - std::vector<PlaneDescriptor> planes; - - int width = 0; - int height = 0; - int stride_pixel = 0; - int format = 0; -}; - -Descriptor *createDescriptor() { return new Descriptor(); } -void deleteDescriptor(Descriptor *descriptor) { delete descriptor; } - -void setProducerUsage(Descriptor &descriptor, uint64_t usage) { - descriptor.producer_usage = usage; -} - -void setConsumerUsage(Descriptor &descriptor, uint64_t usage) { - descriptor.consumer_usage = usage; -} - -void setPlaneCount(Descriptor &descriptor, int count) { - descriptor.planes.resize(count); -} - -void setPlane(Descriptor &descriptor, int index, int fd, size_t size, off_t offset, int stride_byte) { - descriptor.planes[index].fd = fd; - descriptor.planes[index].size = size; - descriptor.planes[index].offset = offset; - descriptor.planes[index].stride_byte = stride_byte; -} - -void setWidth(Descriptor &descriptor, int width) { - descriptor.width = width; -} - -void setHeight(Descriptor &descriptor, int height) { - descriptor.height = height; -} - -void setStridePixel(Descriptor &descriptor, int stride_pixel) { - descriptor.stride_pixel = stride_pixel; -} - -void setFormat(Descriptor &descriptor, int format) { - descriptor.format = format; -} - -buffer_handle_t createNativeHandle(const Descriptor &descriptor) { - for (int i = 0; i < descriptor.planes.size(); ++i) { - struct stat st; - fstat(descriptor.planes[i].fd, &st); - off64_t fd_size = st.st_size; - if (fd_size < descriptor.planes[i].size) { - ALOGE("libGralloc4Wrapper: createNativeHandle failed: plane[%d] requested size greater than fd size.", - i); - return nullptr; - } - } - - buffer_descriptor_t buffer_descriptor; - - buffer_descriptor.pixel_stride = descriptor.stride_pixel; - buffer_descriptor.width = descriptor.width; - buffer_descriptor.height = descriptor.height; - buffer_descriptor.layer_count = 1; - buffer_descriptor.hal_format = buffer_descriptor.alloc_format - = descriptor.format; - buffer_descriptor.producer_usage = descriptor.producer_usage; - buffer_descriptor.consumer_usage = descriptor.consumer_usage; - buffer_descriptor.format_type = MALI_GRALLOC_FORMAT_TYPE_USAGE; - buffer_descriptor.signature = sizeof(buffer_descriptor_t); - - buffer_descriptor.fd_count = buffer_descriptor.plane_count - = descriptor.planes.size(); - for (int i = 0; i < descriptor.planes.size(); ++i) { - buffer_descriptor.alloc_sizes[i] = descriptor.planes[i].size; - } - - auto format_index = get_format_index(descriptor.format); - if (format_index == -1) { - ALOGE("libGralloc4Wrapper: invalid format 0x%x", - descriptor.format); - return 0; - } - for (int i = 0; i < descriptor.planes.size(); ++i) { - uint8_t bpp = formats[format_index].bpp[i]; - if (bpp == 0) { - ALOGE("libGralloc4Wrapper: format 0x%x has bpp[%d]=0", - descriptor.format, i); - return nullptr; - } - buffer_descriptor.plane_info[i] = { - .byte_stride = static_cast<uint32_t>((descriptor.planes[i].stride_byte * bpp) / 8), - .alloc_width = buffer_descriptor.width, - .alloc_height = buffer_descriptor.height, - }; - } - - if (mali_gralloc_derive_format_and_size(&buffer_descriptor)) { - ALOGE("libGralloc4Wrapper: mali_gralloc_derive_format_and_size failed"); - return nullptr; - } - - const gralloc_buffer_descriptor_t gralloc_buffer_descriptor = - reinterpret_cast<const gralloc_buffer_descriptor_t>(&buffer_descriptor); - - buffer_handle_t tmp_buffer; - bool shared_backend; - // TODO(modan@, make mali_gralloc_ion_allocate accept multiple fds) - { - int result = mali_gralloc_buffer_allocate(&gralloc_buffer_descriptor, 1, &tmp_buffer, - &shared_backend, descriptor.planes[0].fd); - if (result < 0) { - ALOGE("mali_gralloc_buffer_allocate failed"); - return nullptr; - } - } - - private_handle_t *hnd = const_cast<private_handle_t *>( - static_cast<const private_handle_t *>(tmp_buffer)); - - hnd->imapper_version = HIDL_MAPPER_VERSION_SCALED; - - hnd->reserved_region_size = buffer_descriptor.reserved_size; - hnd->attr_size = arm::mapper::common::shared_metadata_size() + hnd->reserved_region_size; - - { - int result = mali_gralloc_ion_allocate_attr(hnd); - if (result < 0) { - ALOGE("mali_gralloc_ion_allocate_attr failed"); - mali_gralloc_buffer_free(tmp_buffer); - return nullptr; - } - } - - { - auto metadata_vaddr = mmap(nullptr, hnd->attr_size, PROT_READ | PROT_WRITE, - MAP_SHARED, hnd->get_share_attr_fd(), 0); - if (metadata_vaddr == MAP_FAILED) { - ALOGE("mmap hnd->get_share_attr_fd() failed"); - mali_gralloc_buffer_free(tmp_buffer); - return nullptr; - } - - memset(metadata_vaddr, 0, hnd->attr_size); - - arm::mapper::common::shared_metadata_init(metadata_vaddr, buffer_descriptor.name); - - const uint32_t base_format = buffer_descriptor.alloc_format & MALI_GRALLOC_INTFMT_FMT_MASK; - const uint64_t usage = buffer_descriptor.consumer_usage | buffer_descriptor.producer_usage; - android_dataspace_t dataspace; - get_format_dataspace(base_format, usage, hnd->width, hnd->height, &dataspace); - - { - using arm::mapper::common::aligned_optional; - using arm::mapper::common::Dataspace; - using arm::mapper::common::shared_metadata; - (static_cast<shared_metadata *>(metadata_vaddr))->dataspace = - aligned_optional(static_cast<Dataspace>(dataspace)); - } - - munmap(metadata_vaddr, hnd->attr_size); - } - - // TODO(modan@, handle all plane offsets) - hnd->offset = hnd->plane_info[0].offset = descriptor.planes[0].offset; - hnd->layer_count = 1; - - return tmp_buffer; -} - -int freeImportedHandle(void *handle) -{ - using android::hardware::graphics::mapper::V4_0::IMapper; - using android::hardware::graphics::mapper::V4_0::Error; - - const private_handle_t *hnd = static_cast<private_handle_t *>(handle); - - static android::sp<IMapper> mapper = IMapper::getService(); - if (!mapper) - { - ALOGE("libGralloc4Wrapper: %s failed to get a mapper", __func__); - return -1; - } - - if (mapper->freeBuffer(handle) != Error::NONE) - { - ALOGE("libGralloc4Wrapper: %s couldn't freeBuffer(%p\n", __func__, handle); - return -1; - } - - return 0; -} - -} // namespace android::hardware::graphics::allocator::priv diff --git a/gralloc4/src/mali_gralloc_buffer.h b/gralloc4/src/mali_gralloc_buffer.h index 09461d7..891c507 100644 --- a/gralloc4/src/mali_gralloc_buffer.h +++ b/gralloc4/src/mali_gralloc_buffer.h @@ -97,7 +97,7 @@ typedef struct plane_info { * For uncompressed allocations, byte_stride might contain additional * padding beyond the alloc_width. For AFBC, alignment is zero. */ - uint32_t byte_stride; + uint64_t byte_stride; /* * Dimensions of plane (in pixels). @@ -117,8 +117,8 @@ typedef struct plane_info { * be wholly within the allocation dimensions. The crop region top-left * will be relative to the start of allocation. */ - uint32_t alloc_width; - uint32_t alloc_height; + uint64_t alloc_width; + uint64_t alloc_height; } plane_info_t; struct private_handle_t; @@ -210,7 +210,7 @@ struct private_handle_t * * NOTE: 'stride' values sometimes vary significantly from plane_info[0].alloc_width. */ - int stride DEFAULT_INITIALIZER(0); + uint64_t stride DEFAULT_INITIALIZER(0); /* * Allocation properties. @@ -264,7 +264,7 @@ struct private_handle_t uint64_t _consumer_usage, uint64_t _producer_usage, int _fds[MAX_FDS], int _fd_count, int _req_format, uint64_t _alloc_format, - int _width, int _height, int _stride, + int _width, int _height, uint64_t _stride, uint64_t _layer_count, plane_info_t _plane_info[MAX_PLANES]) : private_handle_t() { @@ -396,10 +396,10 @@ struct private_handle_t "wh(%d %d) " "req_format(%#x) alloc_format(%#" PRIx64 ") " "usage_pc(0x%" PRIx64 " 0x%" PRIx64 ") " - "stride(%d) " - "psize(%" PRIu64 ") byte_stride(%d) internal_wh(%d %d) " - "psize1(%" PRIu64 ") byte_stride1(%d) internal_wh1(%d %d) " - "psize2(%" PRIu64 ") byte_stride2(%d) internal_wh2(%d %d) " + "stride(%" PRIu64 ") " + "psize(%" PRIu64 ") byte_stride(%" PRIu64 ") internal_wh(%" PRIu64 " %" PRIu64 ") " + "psize1(%" PRIu64 ") byte_stride1(%" PRIu64 ") internal_wh1(%" PRIu64 " %" PRIu64 ") " + "psize2(%" PRIu64 ") byte_stride2(%" PRIu64 ") internal_wh2(%" PRIu64 " %" PRIu64 ") " "alloc_format(0x%" PRIx64 ") " "alloc_sizes(%" PRIu64 " %" PRIu64 " %" PRIu64 ") " "layer_count(%d) " @@ -442,4 +442,8 @@ struct private_handle_t #pragma GCC diagnostic pop #endif +// The size of private_handle_t is calculated manually. This check ensures that private_handle_t has +// the same layout for 32-bit and 64-bit processes. +static_assert(sizeof(private_handle_t) == 328); + #endif /* MALI_GRALLOC_BUFFER_H_ */ diff --git a/gralloc4/src/mali_gralloc_error.h b/gralloc4/src/mali_gralloc_error.h new file mode 100644 index 0000000..6045b65 --- /dev/null +++ b/gralloc4/src/mali_gralloc_error.h @@ -0,0 +1,31 @@ +#ifndef MALI_GRALLOC_ERROR +#define MALI_GRALLOC_ERROR + +enum class Error : int32_t { + /** + * No error. + */ + NONE = 0, + /** + * Invalid BufferDescriptor. + */ + BAD_DESCRIPTOR = 1, + /** + * Invalid buffer handle. + */ + BAD_BUFFER = 2, + /** + * Invalid HardwareBufferDescription. + */ + BAD_VALUE = 3, + /** + * Resource unavailable. + */ + NO_RESOURCES = 5, + /** + * Permanent failure. + */ + UNSUPPORTED = 7, +}; + +#endif diff --git a/gralloc4/src/mali_gralloc_formats.h b/gralloc4/src/mali_gralloc_formats.h index e4120fa..a4ada1c 100644 --- a/gralloc4/src/mali_gralloc_formats.h +++ b/gralloc4/src/mali_gralloc_formats.h @@ -359,8 +359,8 @@ typedef struct mali_gralloc_format_caps mali_gralloc_format_caps; void mali_gralloc_adjust_dimensions(const uint64_t internal_format, const uint64_t usage, - int* const width, - int* const height); + uint64_t* const width, + uint64_t* const height); uint64_t mali_gralloc_select_format(const uint64_t req_format, const mali_gralloc_format_type type, diff --git a/gralloc4/src/mali_gralloc_usages.h b/gralloc4/src/mali_gralloc_usages.h index f20900d..f6559c4 100644 --- a/gralloc4/src/mali_gralloc_usages.h +++ b/gralloc4/src/mali_gralloc_usages.h @@ -27,12 +27,12 @@ * is not present. */ - +#include <aidl/android/hardware/graphics/common/BufferUsage.h> #include <android/hardware/graphics/common/1.2/types.h> +#include <pixel-gralloc/usage.h> + /* BufferUsage is not defined in 1.2/types.h as there are no changes from previous version */ namespace hidl_common = android::hardware::graphics::common::V1_1; - -#include <aidl/android/hardware/graphics/common/BufferUsage.h> namespace aidl_common = aidl::android::hardware::graphics::common; /* Local macro definitions to emulate Gralloc 1.0 usage interface */ @@ -78,11 +78,14 @@ typedef enum GRALLOC_USAGE_GOOGLE_IP_BW = GRALLOC_USAGE_PRIVATE_16, /* Alias to BO */ GRALLOC_USAGE_GOOGLE_IP_BIG = GRALLOC_USAGE_PRIVATE_16, /* Alias to BO/BW */ GRALLOC_USAGE_GOOGLE_IP_MFC = GRALLOC_USAGE_PRIVATE_17, + GRALLOC_USAGE_PLACEHOLDER_BUFFER = ::pixel::graphics::Usage::PLACEHOLDER_BUFFER, /* FaceAuth specific usages. */ GS101_GRALLOC_USAGE_TPU_INPUT = GRALLOC_USAGE_PRIVATE_5, GS101_GRALLOC_USAGE_TPU_OUTPUT = GRALLOC_USAGE_PRIVATE_3, GS101_GRALLOC_USAGE_CAMERA_STATS = GRALLOC_USAGE_PRIVATE_2, + + GS101_GRALLOC_USAGE_FACEAUTH_RAW_EVAL = ::pixel::graphics::Usage::FACEAUTH_RAW_EVAL, } mali_gralloc_usage_type; #define GRALLOC_USAGE_SW_WRITE_RARELY static_cast<uint64_t>(hidl_common::BufferUsage::CPU_WRITE_RARELY) @@ -112,6 +115,9 @@ typedef enum #define GRALLOC_USAGE_GPU_DATA_BUFFER static_cast<uint64_t>(hidl_common::BufferUsage::GPU_DATA_BUFFER) #define GRALLOC_USAGE_FRONT_BUFFER static_cast<uint64_t>(aidl_common::BufferUsage::FRONT_BUFFER) +#define UNSUPPORTED_MALI_GRALLOC_USAGE_CUBE_MAP static_cast<uint64_t>(aidl_common::BufferUsage::GPU_CUBE_MAP) +#define UNSUPPORTED_MALI_GRALLOC_USAGE_MIPMAP_COMPLETE static_cast<uint64_t>(aidl_common::BufferUsage::GPU_MIPMAP_COMPLETE) + /* Originally (Gralloc 0.x), Android did not provide an explicit DECODER usage. This was rectified in Android N-MR1/7.1 * when Gralloc 1.0 defined GRALLOC1_PRODUCER_USAGE_VIDEO_DECODER. However, libstagefright continues @@ -147,6 +153,7 @@ static const uint64_t VALID_USAGE = GS101_GRALLOC_USAGE_TPU_INPUT | /* 1U << 62 */ GS101_GRALLOC_USAGE_TPU_OUTPUT | /* 1U << 31 */ + GS101_GRALLOC_USAGE_FACEAUTH_RAW_EVAL | /* 1U << 63 */ GS101_GRALLOC_USAGE_CAMERA_STATS | /* 1U << 30 */ GRALLOC_USAGE_ROIINFO | /* 1U << 52 */ diff --git a/libvendorgraphicbuffer/gralloc4/vendor_graphicbuffer_meta.cpp b/libvendorgraphicbuffer/gralloc4/vendor_graphicbuffer_meta.cpp index eb8d663..a2bf1c4 100644 --- a/libvendorgraphicbuffer/gralloc4/vendor_graphicbuffer_meta.cpp +++ b/libvendorgraphicbuffer/gralloc4/vendor_graphicbuffer_meta.cpp @@ -25,9 +25,11 @@ #include "mali_gralloc_formats.h" #include "hidl_common/SharedMetadata.h" #include "hidl_common/SharedMetadata_struct.h" +#include "hidl_common/hidl_common.h" #include "exynos_format.h" #include <pixel-gralloc/metadata.h> +#include <pixel-gralloc/mapper.h> using namespace android; using namespace vendor::graphics; @@ -244,30 +246,15 @@ void* VendorGraphicBufferMeta::get_video_metadata(buffer_handle_t hnd) return nullptr; } - MapperMetadataType metadata_type{ - .name = ::pixel::graphics::kPixelMetadataTypeName, - .value = static_cast<int64_t>(::pixel::graphics::MetadataType::VIDEO_HDR), - }; + using namespace ::pixel::graphics; + auto out_oe = mapper::get<MetadataType::VIDEO_HDR>(handle); - Error error = Error::NONE; - void* output = nullptr; - - get_mapper()->get(handle, metadata_type, - [&](const auto& tmpError, const android::hardware::hidl_vec<uint8_t>& tmpVec) { - error = tmpError; - if (error != Error::NONE) { - return; - } - output = decodePointer(tmpVec); - }); - - - if (error != Error::NONE) { + if (!out_oe.has_value()) { ALOGE("Failed to get video HDR metadata"); return nullptr; } - return output; + return out_oe.value(); } void* VendorGraphicBufferMeta::get_video_metadata_roiinfo(buffer_handle_t hnd) @@ -277,30 +264,15 @@ void* VendorGraphicBufferMeta::get_video_metadata_roiinfo(buffer_handle_t hnd) return nullptr; } - MapperMetadataType metadata_type{ - .name = ::pixel::graphics::kPixelMetadataTypeName, - .value = static_cast<int64_t>(::pixel::graphics::MetadataType::VIDEO_ROI), - }; + using namespace ::pixel::graphics; + auto out_oe = mapper::get<MetadataType::VIDEO_ROI>(handle); - Error error = Error::NONE; - void* output = nullptr; - - get_mapper()->get(handle, metadata_type, - [&](const auto& tmpError, const android::hardware::hidl_vec<uint8_t>& tmpVec) { - error = tmpError; - if (error != Error::NONE) { - return; - } - output = decodePointer(tmpVec); - }); - - - if (error != Error::NONE) { - ALOGE("Failed to get video HDR metadata"); + if (!out_oe.has_value()) { + ALOGE("Failed to get video ROI metadata"); return nullptr; } - return output; + return out_oe.value(); } uint32_t VendorGraphicBufferMeta::get_format_fourcc(buffer_handle_t hnd) { |