From 299b54a761cdcfd6a9a348279798884afbd5ec35 Mon Sep 17 00:00:00 2001 From: Ankit Goyal Date: Thu, 16 Feb 2023 16:20:56 -0800 Subject: gralloc4: Warn about multifd PlaneLayout once Bug: 237824580 Test: Boot to home Test: Camera viewfinder, video and shot Change-Id: I7f7dc8bb955779ac00e6e7545c54c6ba3d99b4d6 --- gralloc4/src/hidl_common/MapperMetadata.cpp | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/gralloc4/src/hidl_common/MapperMetadata.cpp b/gralloc4/src/hidl_common/MapperMetadata.cpp index 1123270..fb79930 100644 --- a/gralloc4/src/hidl_common/MapperMetadata.cpp +++ b/gralloc4/src/hidl_common/MapperMetadata.cpp @@ -305,11 +305,15 @@ static android::status_t get_plane_layouts(const private_handle_t *handle, std:: int64_t sample_increment_in_bits = format_info.bpp[plane_index]; int64_t offset = handle->plane_info[plane_index].offset; - uint8_t fd_count = get_exynos_fd_count(base_format); - if (fd_count != 1) { - MALI_GRALLOC_LOGW("Offsets in plane layouts of multi-fd format (%s %" PRIu64 - ") are not reliable. This can lead to image corruption.", - format_name(base_format), handle->alloc_format); + static bool warn_multifd = true; + if (warn_multifd) { + uint8_t fd_count = get_exynos_fd_count(base_format); + if (fd_count != 1) { + warn_multifd = false; + MALI_GRALLOC_LOGW("Offsets in plane layouts of multi-fd format (%s %" PRIu64 + ") are not reliable. This can lead to image corruption.", + format_name(base_format), handle->alloc_format); + } } PlaneLayout layout = {.offsetInBytes = offset, -- cgit v1.2.3 From 75a9484061cba9b04c5610ec7d7b4493dcdd0a85 Mon Sep 17 00:00:00 2001 From: Ankit Goyal Date: Mon, 27 Mar 2023 22:43:10 +0000 Subject: Revert "Treat RAW_OPAQUE as RAW10 buffer" This reverts commit 42dd530ed49d24d801400b466339918f7bb1c228. Reason for revert: No longer needed (b/254345790) Change-Id: Ia7666f68c1b5a4a4102661d69df3eff3fad23029 --- gralloc4/src/core/format_info.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gralloc4/src/core/format_info.cpp b/gralloc4/src/core/format_info.cpp index 84fae5f..abb271a 100644 --- a/gralloc4/src/core/format_info.cpp +++ b/gralloc4/src/core/format_info.cpp @@ -236,7 +236,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 }, -- cgit v1.2.3 From d5daa5ba2dd92d42793c5362209779442596137f Mon Sep 17 00:00:00 2001 From: Ankit Goyal Date: Fri, 17 Jun 2022 18:26:38 -0700 Subject: Add dump for allocated gralloc buffers Bug: 272368496 Test: adb shell dumpsys android.hardware.graphics.allocator.IAllocator/default Change-Id: Id8333d852c5fee5c0eee78f9d4a0b8c9627da018 --- gralloc4/src/aidl/GrallocAllocator.cpp | 23 +++++++++ gralloc4/src/aidl/GrallocAllocator.h | 2 + gralloc4/src/hidl_common/Allocator.cpp | 87 ++++++++++++++++++++++++++++++++++ gralloc4/src/hidl_common/Allocator.h | 2 + 4 files changed, 114 insertions(+) diff --git a/gralloc4/src/aidl/GrallocAllocator.cpp b/gralloc4/src/aidl/GrallocAllocator.cpp index 6945505..fb1d5b7 100644 --- a/gralloc4/src/aidl/GrallocAllocator.cpp +++ b/gralloc4/src/aidl/GrallocAllocator.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include "allocator/mali_gralloc_ion.h" @@ -22,6 +23,10 @@ unsigned long callingPid() { return static_cast(AIBinder_getCallingPid()); } +unsigned long callingUid() { + return static_cast(AIBinder_getCallingUid()); +} + GrallocAllocator::GrallocAllocator() {} GrallocAllocator::~GrallocAllocator() {} @@ -83,4 +88,22 @@ ndk::ScopedAStatus GrallocAllocator::allocate(const std::vector& descri return ndk::ScopedAStatus::ok(); } +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..dadd4b9 100644 --- a/gralloc4/src/aidl/GrallocAllocator.h +++ b/gralloc4/src/aidl/GrallocAllocator.h @@ -20,6 +20,8 @@ public: virtual ndk::ScopedAStatus allocate(const std::vector& descriptor, int32_t count, AidlAllocator::AllocationResult* result) override; + + virtual binder_status_t dump(int fd, const char** args, uint32_t numArgs) override; }; } // namespace allocator diff --git a/gralloc4/src/hidl_common/Allocator.cpp b/gralloc4/src/hidl_common/Allocator.cpp index 3b8e62a..facab73 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 +#include +#include #include +#include +#include +#include #include "SharedMetadata.h" #include "Allocator.h" @@ -36,6 +42,35 @@ namespace allocator namespace common { +struct BufferDetails { + std::string name; + uint64_t buffer_id; + std::vector inodes; + uint64_t format; + uint64_t usage; + uint32_t width; + uint32_t height; +}; + +uint64_t total_allocated = 0; +std::atomic 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 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 fb_allocator) { @@ -113,6 +148,31 @@ void allocate(const buffer_descriptor_t &bufferDescriptor, uint32_t count, IAllo mapper::common::set_dataspace(hnd, static_cast(dataspace)); + { + ATRACE_NAME("Update dump details"); + const auto fd_count = hnd->fd_count + 1 /* metadata fd */; + std::vector 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++; + } + /* * We need to set attr_base to MAP_FAILED before the HIDL callback * to avoid sending an invalid pointer to the client process. @@ -160,6 +220,33 @@ 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, 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(*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(); +} + } // namespace common } // namespace allocator } // namespace arm diff --git a/gralloc4/src/hidl_common/Allocator.h b/gralloc4/src/hidl_common/Allocator.h index 8fdd5ba..079457c 100644 --- a/gralloc4/src/hidl_common/Allocator.h +++ b/gralloc4/src/hidl_common/Allocator.h @@ -54,6 +54,8 @@ using android::hardware::hidl_vec; void allocate(const buffer_descriptor_t &bufferDescriptor, uint32_t count, IAllocator::allocate_cb hidl_cb, std::function fb_allocator = nullptr); +const std::string dump(); + } // namespace common } // namespace allocator } // namespace arm -- cgit v1.2.3 From 052ade67b9c21e1c693964d5ec687f2a1f2d7e33 Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Tue, 20 Jun 2023 14:40:40 +0000 Subject: gralloc4: Only free allocated handles in error path If we encounter an error while allocating handles, some error paths could end up trying to freeing more handles that we allocated so far. Also add some extra checks and logging in mali_grallic_ion_allocate() to catch incorrect calls to this function. Bug: 241512108 Test: manual Change-Id: I5886deb4ba6ba957b5a349d8c97f56f8ed795466 --- gralloc4/src/allocator/mali_gralloc_ion.cpp | 76 ++++++++++++++--------------- 1 file changed, 37 insertions(+), 39 deletions(-) diff --git a/gralloc4/src/allocator/mali_gralloc_ion.cpp b/gralloc4/src/allocator/mali_gralloc_ion.cpp index da9aba5..2d63a70 100644 --- a/gralloc4/src/allocator/mali_gralloc_ion.cpp +++ b/gralloc4/src/allocator/mali_gralloc_ion.cpp @@ -48,6 +48,7 @@ #include "mali_gralloc_ion.h" #include +#include #include static const char kDmabufSensorDirectHeapName[] = "sensor_direct_heap"; @@ -369,61 +370,57 @@ int mali_gralloc_ion_allocate(const gralloc_buffer_descriptor_t *descriptors, unsigned int priv_heap_flag = 0; uint64_t usage; uint32_t i; - int fds[MAX_FDS]; - std::fill(fds, fds + MAX_FDS, -1); for (i = 0; i < numDescriptors; i++) { - buffer_descriptor_t *bufDescriptor = (buffer_descriptor_t *)(descriptors[i]); - usage = bufDescriptor->consumer_usage | bufDescriptor->producer_usage; - - for (int fidx = 0; fidx < bufDescriptor->fd_count; fidx++) - { - if (ion_fd >= 0 && fidx == 0) { - fds[fidx] = ion_fd; - } else { - fds[fidx] = alloc_from_dmabuf_heap(usage, bufDescriptor->alloc_sizes[fidx], bufDescriptor->name); - } - if (fds[fidx] < 0) - { - MALI_GRALLOC_LOGE("ion_alloc failed"); - - for (int cidx = 0; cidx < fidx; cidx++) - { - close(fds[cidx]); - } - - /* need to free already allocated memory. not just this one */ - mali_gralloc_ion_free_internal(pHandle, numDescriptors); - - return -1; - } - } + buffer_descriptor_t *bufDescriptor = reinterpret_cast(descriptors[i]); + assert(bufDescriptor); + assert(bufDescriptor->fd_count >= 0); + assert(bufDescriptor->fd_count <= MAX_FDS); - private_handle_t *hnd = new private_handle_t( + auto hnd = new private_handle_t( priv_heap_flag, bufDescriptor->alloc_sizes, bufDescriptor->consumer_usage, bufDescriptor->producer_usage, - fds, bufDescriptor->fd_count, + nullptr, bufDescriptor->fd_count, bufDescriptor->hal_format, bufDescriptor->alloc_format, bufDescriptor->width, bufDescriptor->height, bufDescriptor->pixel_stride, bufDescriptor->layer_count, bufDescriptor->plane_info); - if (NULL == hnd) + /* Reset the number of valid filedescriptors, we will increment + * it each time a valid fd is added, so we can rely on the + * cleanup functions to close open fds. */ + hnd->set_numfds(0); + + if (nullptr == hnd) { MALI_GRALLOC_LOGE("Private handle could not be created for descriptor:%d in non-shared usecase", i); + mali_gralloc_ion_free_internal(pHandle, i); + return -1; + } + + pHandle[i] = hnd; + usage = bufDescriptor->consumer_usage | bufDescriptor->producer_usage; + + for (uint32_t fidx = 0; fidx < bufDescriptor->fd_count; fidx++) + { + int& fd = hnd->fds[fidx]; - /* Close the obtained shared file descriptor for the current handle */ - for (int j = 0; j < bufDescriptor->fd_count; j++) + if (ion_fd >= 0 && fidx == 0) { + fd = ion_fd; + } else { + fd = alloc_from_dmabuf_heap(usage, bufDescriptor->alloc_sizes[fidx], bufDescriptor->name); + } + + if (fd < 0) { - close(fds[j]); + MALI_GRALLOC_LOGE("ion_alloc failed for fds[%u] = %d", fidx, fd); + mali_gralloc_ion_free_internal(pHandle, i + 1); + return -1; } - mali_gralloc_ion_free_internal(pHandle, numDescriptors); - return -1; + hnd->incr_numfds(1); } - - pHandle[i] = hnd; } #if defined(GRALLOC_INIT_AFBC) && (GRALLOC_INIT_AFBC == 1) @@ -431,8 +428,8 @@ int mali_gralloc_ion_allocate(const gralloc_buffer_descriptor_t *descriptors, unsigned char *cpu_ptr = NULL; for (i = 0; i < numDescriptors; i++) { - buffer_descriptor_t *bufDescriptor = (buffer_descriptor_t *)(descriptors[i]); - private_handle_t *hnd = (private_handle_t *)(pHandle[i]); + buffer_descriptor_t *bufDescriptor = reinterpret_cast(descriptors[i]); + const private_handle_t *hnd = static_cast(pHandle[i]); usage = bufDescriptor->consumer_usage | bufDescriptor->producer_usage; @@ -459,6 +456,7 @@ int mali_gralloc_ion_allocate(const gralloc_buffer_descriptor_t *descriptors, ATRACE_NAME("data init"); /* For separated plane YUV, there is a header to initialise per plane. */ const plane_info_t *plane_info = bufDescriptor->plane_info; + assert(plane_info); const bool is_multi_plane = hnd->is_multi_plane(); for (int i = 0; i < MAX_PLANES && (i == 0 || plane_info[i].byte_stride != 0); i++) { -- cgit v1.2.3 From e69b06d49c7ad50f61eaf30aeabaa1033ac1a6e4 Mon Sep 17 00:00:00 2001 From: Sam Dubey Date: Wed, 28 Jun 2023 12:02:11 +0000 Subject: Revert "gralloc4: Only free allocated handles in error path" This reverts commit 052ade67b9c21e1c693964d5ec687f2a1f2d7e33. Reason for revert: Seems to be causing b/289138624. Will verify first. Change-Id: Ibda71220b06ec9847255b628dd1eab01eaef1f6f --- gralloc4/src/allocator/mali_gralloc_ion.cpp | 76 +++++++++++++++-------------- 1 file changed, 39 insertions(+), 37 deletions(-) diff --git a/gralloc4/src/allocator/mali_gralloc_ion.cpp b/gralloc4/src/allocator/mali_gralloc_ion.cpp index 2d63a70..da9aba5 100644 --- a/gralloc4/src/allocator/mali_gralloc_ion.cpp +++ b/gralloc4/src/allocator/mali_gralloc_ion.cpp @@ -48,7 +48,6 @@ #include "mali_gralloc_ion.h" #include -#include #include static const char kDmabufSensorDirectHeapName[] = "sensor_direct_heap"; @@ -370,57 +369,61 @@ int mali_gralloc_ion_allocate(const gralloc_buffer_descriptor_t *descriptors, unsigned int priv_heap_flag = 0; uint64_t usage; uint32_t i; + int fds[MAX_FDS]; + std::fill(fds, fds + MAX_FDS, -1); for (i = 0; i < numDescriptors; i++) { - buffer_descriptor_t *bufDescriptor = reinterpret_cast(descriptors[i]); - assert(bufDescriptor); - assert(bufDescriptor->fd_count >= 0); - assert(bufDescriptor->fd_count <= MAX_FDS); + buffer_descriptor_t *bufDescriptor = (buffer_descriptor_t *)(descriptors[i]); + usage = bufDescriptor->consumer_usage | bufDescriptor->producer_usage; + + for (int fidx = 0; fidx < bufDescriptor->fd_count; fidx++) + { + if (ion_fd >= 0 && fidx == 0) { + fds[fidx] = ion_fd; + } else { + fds[fidx] = alloc_from_dmabuf_heap(usage, bufDescriptor->alloc_sizes[fidx], bufDescriptor->name); + } + if (fds[fidx] < 0) + { + MALI_GRALLOC_LOGE("ion_alloc failed"); + + for (int cidx = 0; cidx < fidx; cidx++) + { + close(fds[cidx]); + } + + /* need to free already allocated memory. not just this one */ + mali_gralloc_ion_free_internal(pHandle, numDescriptors); + + return -1; + } + } - auto hnd = new private_handle_t( + private_handle_t *hnd = new private_handle_t( priv_heap_flag, bufDescriptor->alloc_sizes, bufDescriptor->consumer_usage, bufDescriptor->producer_usage, - nullptr, bufDescriptor->fd_count, + fds, bufDescriptor->fd_count, bufDescriptor->hal_format, bufDescriptor->alloc_format, bufDescriptor->width, bufDescriptor->height, bufDescriptor->pixel_stride, bufDescriptor->layer_count, bufDescriptor->plane_info); - /* Reset the number of valid filedescriptors, we will increment - * it each time a valid fd is added, so we can rely on the - * cleanup functions to close open fds. */ - hnd->set_numfds(0); - - if (nullptr == hnd) + if (NULL == hnd) { MALI_GRALLOC_LOGE("Private handle could not be created for descriptor:%d in non-shared usecase", i); - mali_gralloc_ion_free_internal(pHandle, i); - return -1; - } - - pHandle[i] = hnd; - usage = bufDescriptor->consumer_usage | bufDescriptor->producer_usage; - - for (uint32_t fidx = 0; fidx < bufDescriptor->fd_count; fidx++) - { - 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); - } - - if (fd < 0) + /* Close the obtained shared file descriptor for the current handle */ + for (int j = 0; j < bufDescriptor->fd_count; j++) { - MALI_GRALLOC_LOGE("ion_alloc failed for fds[%u] = %d", fidx, fd); - mali_gralloc_ion_free_internal(pHandle, i + 1); - return -1; + close(fds[j]); } - hnd->incr_numfds(1); + mali_gralloc_ion_free_internal(pHandle, numDescriptors); + return -1; } + + pHandle[i] = hnd; } #if defined(GRALLOC_INIT_AFBC) && (GRALLOC_INIT_AFBC == 1) @@ -428,8 +431,8 @@ int mali_gralloc_ion_allocate(const gralloc_buffer_descriptor_t *descriptors, unsigned char *cpu_ptr = NULL; for (i = 0; i < numDescriptors; i++) { - buffer_descriptor_t *bufDescriptor = reinterpret_cast(descriptors[i]); - const private_handle_t *hnd = static_cast(pHandle[i]); + buffer_descriptor_t *bufDescriptor = (buffer_descriptor_t *)(descriptors[i]); + private_handle_t *hnd = (private_handle_t *)(pHandle[i]); usage = bufDescriptor->consumer_usage | bufDescriptor->producer_usage; @@ -456,7 +459,6 @@ int mali_gralloc_ion_allocate(const gralloc_buffer_descriptor_t *descriptors, ATRACE_NAME("data init"); /* For separated plane YUV, there is a header to initialise per plane. */ const plane_info_t *plane_info = bufDescriptor->plane_info; - assert(plane_info); const bool is_multi_plane = hnd->is_multi_plane(); for (int i = 0; i < MAX_PLANES && (i == 0 || plane_info[i].byte_stride != 0); i++) { -- cgit v1.2.3 From c83d4dede02f1bec379976cc764a57db985014a9 Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Tue, 20 Jun 2023 14:40:40 +0000 Subject: gralloc4: Only free allocated handles in error path If we encounter an error while allocating handles, some error paths could end up trying to freeing more handles that we allocated so far. Also add some extra checks and logging in mali_grallic_ion_allocate() to catch incorrect calls to this function, and ensure the array of filedescriptors in handles is initialized. Bug: 241512108 Test: v2/android-platinum/health/unit/camera Change-Id: Idd2b09ed11424b110d485e10656771aafe0b20a9 --- gralloc4/src/allocator/mali_gralloc_ion.cpp | 76 ++++++++++++++--------------- gralloc4/src/mali_gralloc_buffer.h | 2 + 2 files changed, 39 insertions(+), 39 deletions(-) diff --git a/gralloc4/src/allocator/mali_gralloc_ion.cpp b/gralloc4/src/allocator/mali_gralloc_ion.cpp index da9aba5..2d63a70 100644 --- a/gralloc4/src/allocator/mali_gralloc_ion.cpp +++ b/gralloc4/src/allocator/mali_gralloc_ion.cpp @@ -48,6 +48,7 @@ #include "mali_gralloc_ion.h" #include +#include #include static const char kDmabufSensorDirectHeapName[] = "sensor_direct_heap"; @@ -369,61 +370,57 @@ int mali_gralloc_ion_allocate(const gralloc_buffer_descriptor_t *descriptors, unsigned int priv_heap_flag = 0; uint64_t usage; uint32_t i; - int fds[MAX_FDS]; - std::fill(fds, fds + MAX_FDS, -1); for (i = 0; i < numDescriptors; i++) { - buffer_descriptor_t *bufDescriptor = (buffer_descriptor_t *)(descriptors[i]); - usage = bufDescriptor->consumer_usage | bufDescriptor->producer_usage; - - for (int fidx = 0; fidx < bufDescriptor->fd_count; fidx++) - { - if (ion_fd >= 0 && fidx == 0) { - fds[fidx] = ion_fd; - } else { - fds[fidx] = alloc_from_dmabuf_heap(usage, bufDescriptor->alloc_sizes[fidx], bufDescriptor->name); - } - if (fds[fidx] < 0) - { - MALI_GRALLOC_LOGE("ion_alloc failed"); - - for (int cidx = 0; cidx < fidx; cidx++) - { - close(fds[cidx]); - } - - /* need to free already allocated memory. not just this one */ - mali_gralloc_ion_free_internal(pHandle, numDescriptors); - - return -1; - } - } + buffer_descriptor_t *bufDescriptor = reinterpret_cast(descriptors[i]); + assert(bufDescriptor); + assert(bufDescriptor->fd_count >= 0); + assert(bufDescriptor->fd_count <= MAX_FDS); - private_handle_t *hnd = new private_handle_t( + auto hnd = new private_handle_t( priv_heap_flag, bufDescriptor->alloc_sizes, bufDescriptor->consumer_usage, bufDescriptor->producer_usage, - fds, bufDescriptor->fd_count, + nullptr, bufDescriptor->fd_count, bufDescriptor->hal_format, bufDescriptor->alloc_format, bufDescriptor->width, bufDescriptor->height, bufDescriptor->pixel_stride, bufDescriptor->layer_count, bufDescriptor->plane_info); - if (NULL == hnd) + /* Reset the number of valid filedescriptors, we will increment + * it each time a valid fd is added, so we can rely on the + * cleanup functions to close open fds. */ + hnd->set_numfds(0); + + if (nullptr == hnd) { MALI_GRALLOC_LOGE("Private handle could not be created for descriptor:%d in non-shared usecase", i); + mali_gralloc_ion_free_internal(pHandle, i); + return -1; + } + + pHandle[i] = hnd; + usage = bufDescriptor->consumer_usage | bufDescriptor->producer_usage; + + for (uint32_t fidx = 0; fidx < bufDescriptor->fd_count; fidx++) + { + int& fd = hnd->fds[fidx]; - /* Close the obtained shared file descriptor for the current handle */ - for (int j = 0; j < bufDescriptor->fd_count; j++) + if (ion_fd >= 0 && fidx == 0) { + fd = ion_fd; + } else { + fd = alloc_from_dmabuf_heap(usage, bufDescriptor->alloc_sizes[fidx], bufDescriptor->name); + } + + if (fd < 0) { - close(fds[j]); + MALI_GRALLOC_LOGE("ion_alloc failed for fds[%u] = %d", fidx, fd); + mali_gralloc_ion_free_internal(pHandle, i + 1); + return -1; } - mali_gralloc_ion_free_internal(pHandle, numDescriptors); - return -1; + hnd->incr_numfds(1); } - - pHandle[i] = hnd; } #if defined(GRALLOC_INIT_AFBC) && (GRALLOC_INIT_AFBC == 1) @@ -431,8 +428,8 @@ int mali_gralloc_ion_allocate(const gralloc_buffer_descriptor_t *descriptors, unsigned char *cpu_ptr = NULL; for (i = 0; i < numDescriptors; i++) { - buffer_descriptor_t *bufDescriptor = (buffer_descriptor_t *)(descriptors[i]); - private_handle_t *hnd = (private_handle_t *)(pHandle[i]); + buffer_descriptor_t *bufDescriptor = reinterpret_cast(descriptors[i]); + const private_handle_t *hnd = static_cast(pHandle[i]); usage = bufDescriptor->consumer_usage | bufDescriptor->producer_usage; @@ -459,6 +456,7 @@ int mali_gralloc_ion_allocate(const gralloc_buffer_descriptor_t *descriptors, ATRACE_NAME("data init"); /* For separated plane YUV, there is a header to initialise per plane. */ const plane_info_t *plane_info = bufDescriptor->plane_info; + assert(plane_info); const bool is_multi_plane = hnd->is_multi_plane(); for (int i = 0; i < MAX_PLANES && (i == 0 || plane_info[i].byte_stride != 0); i++) { diff --git a/gralloc4/src/mali_gralloc_buffer.h b/gralloc4/src/mali_gralloc_buffer.h index 9cc7920..c4db9fc 100644 --- a/gralloc4/src/mali_gralloc_buffer.h +++ b/gralloc4/src/mali_gralloc_buffer.h @@ -286,6 +286,8 @@ struct private_handle_t if (_fds) memcpy(fds, _fds, sizeof(fds)); + else + memset(fds, -1, sizeof(fds)); if (_alloc_sizes) memcpy(alloc_sizes, _alloc_sizes, sizeof(alloc_sizes)); -- cgit v1.2.3 From 5a125f9cef31eacc48aaa10763c4dea481bf3535 Mon Sep 17 00:00:00 2001 From: Ankit Goyal Date: Wed, 28 Jun 2023 16:29:39 -0700 Subject: gralloc4: Add bytes to dmabuf atrace Test: Perfetto Change-Id: I5c045f919776f3b5861e33ffc45144be6419d799 --- gralloc4/src/allocator/mali_gralloc_ion.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/gralloc4/src/allocator/mali_gralloc_ion.cpp b/gralloc4/src/allocator/mali_gralloc_ion.cpp index da9aba5..098fc34 100644 --- a/gralloc4/src/allocator/mali_gralloc_ion.cpp +++ b/gralloc4/src/allocator/mali_gralloc_ion.cpp @@ -210,7 +210,9 @@ int alloc_from_dmabuf_heap(uint64_t usage, size_t size, const std::string& buffe 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) { -- cgit v1.2.3 From f4e27814d8f0bb0389c45dd1e66c7d57dff35c2e Mon Sep 17 00:00:00 2001 From: Ankit Goyal Date: Fri, 7 Jul 2023 10:00:16 -0700 Subject: gralloc4: Tune down dump static size to 2048 Fix: 288199015 Test: Builds Change-Id: Iefc45b3b517ba0bfab1178923f826f31ca594c2e --- gralloc4/src/hidl_common/Allocator.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gralloc4/src/hidl_common/Allocator.cpp b/gralloc4/src/hidl_common/Allocator.cpp index facab73..abca841 100644 --- a/gralloc4/src/hidl_common/Allocator.cpp +++ b/gralloc4/src/hidl_common/Allocator.cpp @@ -57,7 +57,7 @@ std::atomic 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 allocated_buffers; +std::array allocated_buffers; std::shared_timed_mutex allocated_buffers_mutex; static_assert((allocated_buffers.size() & (allocated_buffers.size() - 1)) == 0); -- cgit v1.2.3 From 6cde8c106ee95636e9b0fa1d9050d55db1398d06 Mon Sep 17 00:00:00 2001 From: Sally Qi Date: Mon, 17 Jul 2023 11:31:41 +0800 Subject: Use android.hardware.graphics.common wrap. Bug: 291745893 Test: builds Change-Id: If8b08b67d6a5ab0b4044f844f960f3659401c397 --- gralloc4/interfaces/libs/drmutils/Android.bp | 2 +- gralloc4/src/Android.bp | 2 +- gralloc4/src/allocator/Android.bp | 2 +- gralloc4/src/capabilities/Android.bp | 2 +- gralloc4/src/core/Android.bp | 2 +- 5 files changed, 5 insertions(+), 5 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/src/Android.bp b/gralloc4/src/Android.bp index a813fc8..ecd2fbd 100644 --- a/gralloc4/src/Android.bp +++ b/gralloc4/src/Android.bp @@ -40,6 +40,7 @@ cc_library_shared { vendor: true, defaults: [ "arm_gralloc_defaults", + "android.hardware.graphics.common-ndk_shared", ], srcs: [ "libGralloc4Wrapper/wrapper.cpp", @@ -69,7 +70,6 @@ cc_library_shared { "libhidltransport", "libnativewindow", "android.hardware.graphics.common@1.2", - "android.hardware.graphics.common-V4-ndk", "android.hardware.graphics.mapper@4.0", "libdmabufheap", "libgralloctypes", diff --git a/gralloc4/src/allocator/Android.bp b/gralloc4/src/allocator/Android.bp index fe42411..5ff23f8 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: { @@ -89,7 +90,6 @@ arm_gralloc_allocator_cc_defaults { "libsync", "libutils", "libnativewindow", - "android.hardware.graphics.common-V4-ndk", ], header_libs: [ "libnativebase_headers", 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: { -- cgit v1.2.3 From c73acb2d163f395a327e41483f5b939641ee2105 Mon Sep 17 00:00:00 2001 From: Devika Krishnadas Date: Wed, 21 Jun 2023 22:50:29 +0000 Subject: gralloc4: Upgrade Allocator to AIDL2 Bug: 287353739 Test: atest VtsHalGraphicsMapperV4_0TargetTest Test: ag/24139814 Change-Id: I9a47233ac1ae8ee3236c5d1437edf0e39fc13d5b Signed-off-by: Devika Krishnadas --- gralloc4/service/aidl/Android.bp | 5 +- ...oid.hardware.graphics.allocator-aidl-service.rc | 2 +- gralloc4/service/aidl/manifest_gralloc_aidl.xml | 1 + gralloc4/src/aidl/Android.bp | 2 +- gralloc4/src/aidl/GrallocAllocator.cpp | 87 +++++++++++++++++++++- gralloc4/src/aidl/GrallocAllocator.h | 11 +++ gralloc4/src/hidl_common/Allocator.cpp | 5 ++ gralloc4/src/hidl_common/Allocator.h | 2 + 8 files changed, 110 insertions(+), 5 deletions(-) diff --git a/gralloc4/service/aidl/Android.bp b/gralloc4/service/aidl/Android.bp index a3d7a87..4262685 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", @@ -28,5 +28,6 @@ cc_binary { ], required: [ "android.hardware.graphics.allocator-aidl-impl", + "android.hardware.graphics.mapper@4.0-impl", ], } 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 @@ android.hardware.graphics.allocator + 2 IAllocator/default 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 fb1d5b7..f535379 100644 --- a/gralloc4/src/aidl/GrallocAllocator.cpp +++ b/gralloc4/src/aidl/GrallocAllocator.cpp @@ -7,7 +7,6 @@ #include #include -#include "allocator/mali_gralloc_ion.h" #include "hidl_common/Allocator.h" namespace pixel::allocator { @@ -88,6 +87,92 @@ ndk::ScopedAStatus GrallocAllocator::allocate(const std::vector& 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(descriptor.format); + bufferDescriptor.producer_usage = static_cast(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; + bufferDescriptor.name = std::string(std::begin(descriptor.name), std::end(descriptor.name)); + 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 _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(_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(AidlAllocator::AllocationError::BAD_DESCRIPTOR)); + + case HidlError::NO_RESOURCES: + return ndk::ScopedAStatus::fromServiceSpecificError( + static_cast(AidlAllocator::AllocationError::NO_RESOURCES)); + + case HidlError::UNSUPPORTED: + return ndk::ScopedAStatus::fromServiceSpecificError( + static_cast(AidlAllocator::AllocationError::UNSUPPORTED)); + + default: + return ndk::ScopedAStatus::fromStatus(STATUS_UNKNOWN_ERROR); + } + + return ndk::ScopedAStatus::ok(); +} + +ndk::ScopedAStatus GrallocAllocator::isSupported( + const AidlAllocator::BufferDescriptorInfo& descriptor, bool* result) { + buffer_descriptor_t bufferDescriptor = decodeBufferDescriptorInfo(descriptor); + + *result = arm::allocator::common::isSupported(&bufferDescriptor); + + if (*result) { + MALI_GRALLOC_LOGV("Allocation for the given description will not succeed. error %d", + *result); + return ndk::ScopedAStatus::fromServiceSpecificError( + static_cast(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"; diff --git a/gralloc4/src/aidl/GrallocAllocator.h b/gralloc4/src/aidl/GrallocAllocator.h index dadd4b9..91655a7 100644 --- a/gralloc4/src/aidl/GrallocAllocator.h +++ b/gralloc4/src/aidl/GrallocAllocator.h @@ -2,9 +2,11 @@ #include #include +#include #include #include +#include #include namespace pixel { @@ -21,6 +23,15 @@ public: virtual ndk::ScopedAStatus allocate(const std::vector& 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; }; diff --git a/gralloc4/src/hidl_common/Allocator.cpp b/gralloc4/src/hidl_common/Allocator.cpp index abca841..a7d789d 100644 --- a/gralloc4/src/hidl_common/Allocator.cpp +++ b/gralloc4/src/hidl_common/Allocator.cpp @@ -247,6 +247,11 @@ const std::string dump() { 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 079457c..e5ce174 100644 --- a/gralloc4/src/hidl_common/Allocator.h +++ b/gralloc4/src/hidl_common/Allocator.h @@ -56,6 +56,8 @@ void allocate(const buffer_descriptor_t &bufferDescriptor, uint32_t count, IAllo const std::string dump(); +int isSupported(buffer_descriptor_t *const bufDescriptor); + } // namespace common } // namespace allocator } // namespace arm -- cgit v1.2.3 From b04e88e3f68b9d6f42bbde910756380386663a68 Mon Sep 17 00:00:00 2001 From: Priyanka Advani Date: Mon, 24 Jul 2023 19:52:02 +0000 Subject: Revert "gralloc4: Upgrade Allocator to AIDL2" Revert submission 23772493-allocator-aidl2 Reason for revert: Probable culprit for b/292565867, b/292569168 Reverted changes: /q/submissionid:23772493-allocator-aidl2 Change-Id: I41c525992f5d6e3f367f15fe261dc28b225c93f3 --- gralloc4/service/aidl/Android.bp | 5 +- ...oid.hardware.graphics.allocator-aidl-service.rc | 2 +- gralloc4/service/aidl/manifest_gralloc_aidl.xml | 1 - gralloc4/src/aidl/Android.bp | 2 +- gralloc4/src/aidl/GrallocAllocator.cpp | 87 +--------------------- gralloc4/src/aidl/GrallocAllocator.h | 11 --- gralloc4/src/hidl_common/Allocator.cpp | 5 -- gralloc4/src/hidl_common/Allocator.h | 2 - 8 files changed, 5 insertions(+), 110 deletions(-) diff --git a/gralloc4/service/aidl/Android.bp b/gralloc4/service/aidl/Android.bp index 4262685..a3d7a87 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-V2-service", + name: "android.hardware.graphics.allocator-V1-service", proprietary: true, relative_install_path: "hw", srcs: [ @@ -17,7 +17,7 @@ cc_binary { "libgralloc_headers", ], shared_libs: [ - "android.hardware.graphics.allocator-V2-ndk", + "android.hardware.graphics.allocator-V1-ndk", "android.hardware.graphics.allocator-aidl-impl", "libbinder_ndk", "liblog", @@ -28,6 +28,5 @@ cc_binary { ], required: [ "android.hardware.graphics.allocator-aidl-impl", - "android.hardware.graphics.mapper@4.0-impl", ], } 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 723fab6..e86b68d 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-V2-service +service vendor.graphics.allocator-default /vendor/bin/hw/android.hardware.graphics.allocator-V1-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 c29d370..6848a99 100644 --- a/gralloc4/service/aidl/manifest_gralloc_aidl.xml +++ b/gralloc4/service/aidl/manifest_gralloc_aidl.xml @@ -1,7 +1,6 @@ android.hardware.graphics.allocator - 2 IAllocator/default diff --git a/gralloc4/src/aidl/Android.bp b/gralloc4/src/aidl/Android.bp index 3c0fc26..e2d9d04 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-V2-ndk", + "android.hardware.graphics.allocator-V1-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 f535379..fb1d5b7 100644 --- a/gralloc4/src/aidl/GrallocAllocator.cpp +++ b/gralloc4/src/aidl/GrallocAllocator.cpp @@ -7,6 +7,7 @@ #include #include +#include "allocator/mali_gralloc_ion.h" #include "hidl_common/Allocator.h" namespace pixel::allocator { @@ -87,92 +88,6 @@ ndk::ScopedAStatus GrallocAllocator::allocate(const std::vector& 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(descriptor.format); - bufferDescriptor.producer_usage = static_cast(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; - bufferDescriptor.name = std::string(std::begin(descriptor.name), std::end(descriptor.name)); - 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 _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(_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(AidlAllocator::AllocationError::BAD_DESCRIPTOR)); - - case HidlError::NO_RESOURCES: - return ndk::ScopedAStatus::fromServiceSpecificError( - static_cast(AidlAllocator::AllocationError::NO_RESOURCES)); - - case HidlError::UNSUPPORTED: - return ndk::ScopedAStatus::fromServiceSpecificError( - static_cast(AidlAllocator::AllocationError::UNSUPPORTED)); - - default: - return ndk::ScopedAStatus::fromStatus(STATUS_UNKNOWN_ERROR); - } - - return ndk::ScopedAStatus::ok(); -} - -ndk::ScopedAStatus GrallocAllocator::isSupported( - const AidlAllocator::BufferDescriptorInfo& descriptor, bool* result) { - buffer_descriptor_t bufferDescriptor = decodeBufferDescriptorInfo(descriptor); - - *result = arm::allocator::common::isSupported(&bufferDescriptor); - - if (*result) { - MALI_GRALLOC_LOGV("Allocation for the given description will not succeed. error %d", - *result); - return ndk::ScopedAStatus::fromServiceSpecificError( - static_cast(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"; diff --git a/gralloc4/src/aidl/GrallocAllocator.h b/gralloc4/src/aidl/GrallocAllocator.h index 91655a7..dadd4b9 100644 --- a/gralloc4/src/aidl/GrallocAllocator.h +++ b/gralloc4/src/aidl/GrallocAllocator.h @@ -2,11 +2,9 @@ #include #include -#include #include #include -#include #include namespace pixel { @@ -23,15 +21,6 @@ public: virtual ndk::ScopedAStatus allocate(const std::vector& 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; }; diff --git a/gralloc4/src/hidl_common/Allocator.cpp b/gralloc4/src/hidl_common/Allocator.cpp index a7d789d..abca841 100644 --- a/gralloc4/src/hidl_common/Allocator.cpp +++ b/gralloc4/src/hidl_common/Allocator.cpp @@ -247,11 +247,6 @@ const std::string dump() { 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 e5ce174..079457c 100644 --- a/gralloc4/src/hidl_common/Allocator.h +++ b/gralloc4/src/hidl_common/Allocator.h @@ -56,8 +56,6 @@ void allocate(const buffer_descriptor_t &bufferDescriptor, uint32_t count, IAllo const std::string dump(); -int isSupported(buffer_descriptor_t *const bufDescriptor); - } // namespace common } // namespace allocator } // namespace arm -- cgit v1.2.3 From 18a82cb27995d1381003e8f9b06d0bdfcd19dd13 Mon Sep 17 00:00:00 2001 From: Solti Date: Wed, 26 Jul 2023 18:01:53 +0000 Subject: block_unsupported_usage The CL blocks the deprecated useages to fix failures in CtsNativeHardwareTestCases when running with ANGLE driver on Pixel. Before the fix, there are 47 failures per ABI. [ref](http://android-build/test_investigate/?invocationId=I46300010183374279&testResultId=TR42128830015765965) After the fix, the failure count is reduced to 5 per ABI. [ref](http://android-build/test_investigate/?invocationId=I72100010183393307&testResultId=TR49528829864738389&redirect=http://fusion2/cb6ac3e3-01f2-494b-9874-5fc3f1429061) Test: https://android-build.googleplex.com/builds/abtd/run/L13200000962314288, cmd: abtd test --test_location=remote --reference_build_id=10588663 --tag=fixNativeHardwareTestCases --v2_test_name=angle_CtsNativeHardwareTestCases_git_main th:cl:24213503:oriole-trunk_staging-userdebug:git_main "$PROD_CONFIG" Bug: b/266837699 Change-Id: I4818ac196f16997212f9895d1a2f8185ea8e881d --- gralloc4/src/core/mali_gralloc_bufferallocation.cpp | 19 +++++++++++++------ gralloc4/src/mali_gralloc_usages.h | 3 +++ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/gralloc4/src/core/mali_gralloc_bufferallocation.cpp b/gralloc4/src/core/mali_gralloc_bufferallocation.cpp index 4a0618b..7a3d3fd 100644 --- a/gralloc4/src/core/mali_gralloc_bufferallocation.cpp +++ b/gralloc4/src/core/mali_gralloc_bufferallocation.cpp @@ -475,11 +475,19 @@ static void update_yv12_stride(int8_t plane, * 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; } + if (usage & UNSUPPORTED_MALI_GRALLOC_USAGE_CUBE_MAP) { + MALI_GRALLOC_LOGW("BufferUsage::GPU_CUBE_MAP is unsupported"); + return true; + } + if (usage & UNSUPPORTED_MALI_GRALLOC_USAGE_MIPMAP_COMPLETE) { + MALI_GRALLOC_LOGW("BufferUsage::GPU_MIPMAP_COMPLETE is unsupported"); + return true; + } return false; } @@ -992,6 +1000,10 @@ int mali_gralloc_derive_format_and_size(buffer_descriptor_t * const bufDescripto int alloc_height = bufDescriptor->height; uint64_t usage = bufDescriptor->producer_usage | bufDescriptor->consumer_usage; + if (log_obsolete_usage_flags(usage)) { + return -EINVAL; + } + /* * Select optimal internal pixel format based upon * usage and requested format. @@ -1157,11 +1169,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) diff --git a/gralloc4/src/mali_gralloc_usages.h b/gralloc4/src/mali_gralloc_usages.h index f20900d..4bab4d3 100644 --- a/gralloc4/src/mali_gralloc_usages.h +++ b/gralloc4/src/mali_gralloc_usages.h @@ -112,6 +112,9 @@ typedef enum #define GRALLOC_USAGE_GPU_DATA_BUFFER static_cast(hidl_common::BufferUsage::GPU_DATA_BUFFER) #define GRALLOC_USAGE_FRONT_BUFFER static_cast(aidl_common::BufferUsage::FRONT_BUFFER) +#define UNSUPPORTED_MALI_GRALLOC_USAGE_CUBE_MAP static_cast(aidl_common::BufferUsage::GPU_CUBE_MAP) +#define UNSUPPORTED_MALI_GRALLOC_USAGE_MIPMAP_COMPLETE static_cast(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 -- cgit v1.2.3 From c760f0c11020fe865f0aa9fa5e258d26f015c410 Mon Sep 17 00:00:00 2001 From: Ankit Goyal Date: Wed, 2 Aug 2023 15:48:49 -0700 Subject: includes: Remove private_handle_t definition to be exposed to users Bug: 289448217 Test: Builds Test: Boot to home Change-Id: Ia9c91ce5dec55eb3784791b8e887f9542ceb9fbb --- include/gralloc3_priv.h | 269 ------------------------------------------------ 1 file changed, 269 deletions(-) diff --git a/include/gralloc3_priv.h b/include/gralloc3_priv.h index ac053a1..f623359 100644 --- a/include/gralloc3_priv.h +++ b/include/gralloc3_priv.h @@ -122,273 +122,4 @@ typedef enum MALI_YUV_BT2020_WIDE } mali_gralloc_yuv_info; -typedef struct private_module -{ - gralloc1_module_t base; - - struct private_handle_t *framebuffer; - uint32_t flags; - uint32_t numBuffers; - uint32_t bufferMask; - pthread_mutex_t lock; - buffer_handle_t currentBuffer; - mali_dpy_type dpy_type; - - struct fb_var_screeninfo info; - struct fb_fix_screeninfo finfo; - float xdpi; - float ydpi; - float fps; - int swapInterval; - uint64_t fbdev_format; - int ionfd; -} private_module_t; - -/* - * Maximum number of pixel format planes. - * Plane [0]: Single plane formats (inc. RGB, YUV) and Y - * Plane [1]: U/V, UV - * Plane [2]: V/U - */ -#define MAX_PLANES 3 - -typedef struct plane_info { - - /* - * Offset to plane (in bytes), - * from the start of the allocation. - */ - uint32_t offset; - - /* - * Byte Stride: number of bytes between two vertically adjacent - * pixels in given plane. This can be mathematically described by: - * - * byte_stride = ALIGN((alloc_width * bpp)/8, alignment) - * - * where, - * - * alloc_width: width of plane in pixels (c.f. pixel_stride) - * bpp: average bits per pixel - * alignment (in bytes): dependent upon pixel format and usage - * - * For uncompressed allocations, byte_stride might contain additional - * padding beyond the alloc_width. For AFBC, alignment is zero. - */ - uint32_t byte_stride; - - /* - * Dimensions of plane (in pixels). - * - * For single plane formats, pixels equates to luma samples. - * For multi-plane formats, pixels equates to the number of sample sites - * for the corresponding plane, even if subsampled. - * - * AFBC compressed formats: requested width/height are rounded-up - * to a whole AFBC superblock/tile (next superblock at minimum). - * Uncompressed formats: dimensions typically match width and height - * but might require pixel stride alignment. - * - * See 'byte_stride' for relationship between byte_stride and alloc_width. - * - * Any crop rectangle defined by GRALLOC_ARM_BUFFER_ATTR_CROP_RECT must - * 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; -} plane_info_t; - -struct private_handle_t : public native_handle -{ - enum - { - PRIV_FLAGS_FRAMEBUFFER = 1U << 0, - PRIV_FLAGS_USES_ION_COMPOUND_HEAP = 1U << 1, - PRIV_FLAGS_USES_ION = 1U << 2, - PRIV_FLAGS_USES_ION_DMA_HEAP = 1U << 3, - PRIV_FLAGS_USES_2PRIVATE_DATA = 1U << 4, - PRIV_FLAGS_USES_3PRIVATE_DATA = 1U << 5, - }; - - /* - * Shared file descriptor for dma_buf sharing. This must be the first element in the - * structure so that binder knows where it is and can properly share it between - * processes. - * DO NOT MOVE THIS ELEMENT! - */ - int fd; - int fd1; - int fd2; - int fd3; - int fd4; - - // ints - int magic; - int flags; - - /* - * Input properties. - * - * req_format: Pixel format, base + private modifiers. - * width/height: Buffer dimensions. - * producer/consumer_usage: Buffer usage (indicates IP) - */ - int width; - int height; - /* LSI integration: Needed by Camera */ - int frameworkFormat; - - uint64_t producer_usage; - uint64_t consumer_usage; - - union - { - int format; - uint64_t internal_format; - }; - - /* - * Allocation properties. - * - * alloc_format: Pixel format (base + modifiers). NOTE: base might differ from requested - * format (req_format) where fallback to single-plane format was required. - * plane_info: Per plane allocation information. - * size: Total bytes allocated for buffer (inc. all planes, layers. etc.). - * layer_count: Number of layers allocated to buffer. - * All layers are the same size (in bytes). - * Multi-layers supported in v1.0, where GRALLOC1_CAPABILITY_LAYERED_BUFFERS is enabled. - * Layer size: 'size' / 'layer_count'. - * Layer (n) offset: n * ('size' / 'layer_count'), n=0 for the first layer. - * - */ - uint64_t alloc_format; - union - { - plane_info_t plane_info[MAX_PLANES]; - struct - { - int plane_offset; - int byte_stride; - int alloc_width; - int vstride; - }; - }; - int size; - int size1; - int size2; - uint32_t stride; - uint32_t layer_count; - - union - { - void *base; - uint64_t bases[3]; - }; - - uint64_t backing_store_id; - int backing_store_size; - int cpu_read; /**< Buffer is locked for CPU read when non-zero. */ - int cpu_write; /**< Buffer is locked for CPU write when non-zero. */ - int allocating_pid; - int remote_pid; - int ref_count; - // locally mapped shared attribute area - union - { - void *attr_base; - uint64_t padding3; - }; - - mali_gralloc_yuv_info yuv_info; - - // Following members is for framebuffer only - int fb_fd; - union - { - off_t offset; - uint64_t padding4; - }; - - /* - * min_pgsz denotes minimum phys_page size used by this buffer. - * if buffer memory is physical contiguous set min_pgsz to buff->size - * if not sure buff's real phys_page size, you can use SZ_4K for safe. - */ - int min_pgsz; - - int is_compressible; - - ion_user_handle_t ion_handles[3]; - - int PRIVATE_1 = 0; - int PRIVATE_2 = 0; - int plane_count = 0; - - static int validate(const native_handle *h) - { -#define GRALLOC_ARM_NUM_FDS 2 -#define NUM_INTS_IN_PRIVATE_HANDLE ((sizeof(struct private_handle_t) - sizeof(native_handle)) / sizeof(int) - GRALLOC_ARM_NUM_FDS) - - static const int sNumFds = GRALLOC_ARM_NUM_FDS; - static const int sMagic = 0x3141592; - const private_handle_t *hnd = (const private_handle_t *)h; - - if (!h || h->version != sizeof(native_handle) || - hnd->numInts + hnd->numFds != NUM_INTS_IN_PRIVATE_HANDLE + sNumFds || - hnd->magic != sMagic) - { - return -EINVAL; - } - - return 0; - } - - static private_handle_t *dynamicCast(const native_handle *in) - { - if (validate(in) == 0) - { - return (private_handle_t *)in; - } - - return NULL; - } - - int get_num_ion_fds() const - { - return numFds - 1; - } - - void dump(const char *str) const - { - ALOGD("[%s] " - "fd(%d %d %d %d) " - "flags(%d) " - "wh(%d %d) " - "req_format(0x%x) " - "usage_pc(0x%" PRIx64 " 0x%" PRIx64 ") " - "format(0x%x) " - "interal_format(0x%" PRIx64 ") " - "stride(%d) byte_stride(%d) internal_wh(%d %d) " - "alloc_format(0x%" PRIx64 ") " - "size(%d %d %d) " - "layer_count(%d) plane_count(%d)" - "bases(0x%" PRIx64 " 0x%" PRIx64 " 0x%" PRIx64 ") " - "\n", - str, - fd, fd1, fd2, fd3, - flags, - width, height, - frameworkFormat, - producer_usage, consumer_usage, - format, internal_format, - stride, plane_info[0].byte_stride, plane_info[0].alloc_width, plane_info[0].alloc_height, - alloc_format, - size, size1, size2, - layer_count, plane_count, - bases[0], bases[1], bases[2] - ); - } -}; - #endif /* GRALLOC3_PRIV_H_ */ -- cgit v1.2.3 From fa52847b0fce0f6a536ed176ad88f415e50f4839 Mon Sep 17 00:00:00 2001 From: Ankit Goyal Date: Wed, 2 Aug 2023 16:54:37 -0700 Subject: libvendorgraphicbuffer: Remove custom lock APIs Bug: 289448430 Test: Camera smoke tests Change-Id: Iccd28247dfb10559e70739515de010ebc397c18a --- libvendorgraphicbuffer/Android.bp | 1 - .../gralloc4/vendor_graphicbuffer_mapper.cpp | 102 --------------------- .../include/VendorGraphicBuffer.h | 19 +--- 3 files changed, 1 insertion(+), 121 deletions(-) delete mode 100644 libvendorgraphicbuffer/gralloc4/vendor_graphicbuffer_mapper.cpp diff --git a/libvendorgraphicbuffer/Android.bp b/libvendorgraphicbuffer/Android.bp index 53e76aa..de3721a 100644 --- a/libvendorgraphicbuffer/Android.bp +++ b/libvendorgraphicbuffer/Android.bp @@ -22,7 +22,6 @@ package { cc_library_shared { name: "libvendorgraphicbuffer", srcs: [ - "gralloc4/vendor_graphicbuffer_mapper.cpp", "gralloc4/vendor_graphicbuffer_meta.cpp", ], shared_libs: [ diff --git a/libvendorgraphicbuffer/gralloc4/vendor_graphicbuffer_mapper.cpp b/libvendorgraphicbuffer/gralloc4/vendor_graphicbuffer_mapper.cpp deleted file mode 100644 index 713384a..0000000 --- a/libvendorgraphicbuffer/gralloc4/vendor_graphicbuffer_mapper.cpp +++ /dev/null @@ -1,102 +0,0 @@ -/* - * Copyright (C) 2020 Samsung Electronics Co. Ltd. - * - * 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. - */ - -#include -#include "VendorGraphicBuffer.h" -#include "mali_gralloc_formats.h" -#include "mali_gralloc_buffer.h" -#include "exynos_format.h" -#include "gralloc_helper.h" -#include - -using namespace android; -using namespace vendor::graphics; - - -status_t VendorGraphicBufferMapper::lock64(buffer_handle_t handle, uint64_t usage, const Rect& bounds, - void** vaddr, int32_t* outBytesPerPixel, int32_t* outBytesPerStride) -{ - return lockAsync(handle, usage, usage, bounds, vaddr, - -1, outBytesPerPixel, outBytesPerStride); -} - -status_t VendorGraphicBufferMapper::lockYCbCr64(buffer_handle_t handle, - uint64_t usage, const Rect& bounds, android_ycbcr *ycbcr) -{ - status_t err = getGrallocMapper().lock(handle, usage, bounds, -1, ycbcr); - - if (!(usage & VendorGraphicBufferUsage::VIDEO_PRIVATE_DATA)) - return err; - - /* TODO: clean this typecasting... */ - const private_handle_t * const hnd = (private_handle_t *)handle; - - switch (hnd->alloc_format & MALI_GRALLOC_INTFMT_FMT_MASK) - { - case HAL_PIXEL_FORMAT_EXYNOS_YCrCb_420_SP_M_SBWC: - case HAL_PIXEL_FORMAT_EXYNOS_YCrCb_420_SP_M_10B_SBWC: - ycbcr->cb = (void *)(hnd->attr_base); - break; - case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_SBWC: - case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_10B_SBWC: - case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_SBWC_L50: - case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_SBWC_L75: - case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_10B_SBWC_L40: - case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_10B_SBWC_L60: - case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_10B_SBWC_L80: - ycbcr->cr = (void *)(hnd->attr_base); - break; - case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_SBWC: - ycbcr->cr = (void *)(hnd->attr_base); - break; - case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_10B_SBWC: - ycbcr->cr = (void *)(hnd->attr_base); - break; - case HAL_PIXEL_FORMAT_YCrCb_420_SP: - ycbcr->cr = (void *)(hnd->attr_base); - break; - case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_SBWC_L50: - case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_SBWC_L75: - ycbcr->cr = (void *)(hnd->attr_base); - break; - case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_10B_SBWC_L40: - case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_10B_SBWC_L60: - case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_10B_SBWC_L80: - ycbcr->cr = (void *)(hnd->attr_base); - break; - case HAL_PIXEL_FORMAT_EXYNOS_YCrCb_420_SP_M: - case HAL_PIXEL_FORMAT_EXYNOS_YCrCb_420_SP_M_FULL: - ycbcr->cb = (void *)(hnd->attr_base); - break; - case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M: - case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_S10B: - ycbcr->cr = (void *)(hnd->attr_base); - break; - case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN: - ycbcr->cr = (void *)(hnd->attr_base); - break; - case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_S10B: - ycbcr->cr = (void *)(hnd->attr_base); - break; - case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_P010_M: - ycbcr->cr = (void *)(hnd->attr_base); - break; - default: - break; - } - - return err; -} diff --git a/libvendorgraphicbuffer/include/VendorGraphicBuffer.h b/libvendorgraphicbuffer/include/VendorGraphicBuffer.h index 83e2382..ab4452c 100644 --- a/libvendorgraphicbuffer/include/VendorGraphicBuffer.h +++ b/libvendorgraphicbuffer/include/VendorGraphicBuffer.h @@ -155,24 +155,7 @@ public: static int free_buffer(buffer_handle_t); }; - -/* Mapper extension class to allow locking with 64-bit usages */ -class VendorGraphicBufferMapper : public android::GraphicBufferMapper -{ -public: - static inline VendorGraphicBufferMapper& get() - { - return static_cast(getInstance()); - } - - android::status_t lock64(buffer_handle_t handle, uint64_t usage, const android::Rect& bounds, - void** vaddr, int32_t* outBytesPerPixel = nullptr, - int32_t* outBytesPerStride = nullptr); - - android::status_t lockYCbCr64(buffer_handle_t handle, - uint64_t usage, const android::Rect& bounds, android_ycbcr *ycbcr); -}; - +typedef class android::GraphicBufferMapper VendorGraphicBufferMapper; typedef class android::GraphicBufferAllocator VendorGraphicBufferAllocator; } /* namespace graphics */ -- cgit v1.2.3 From adbaea973869082d020881cea3617cb944781097 Mon Sep 17 00:00:00 2001 From: Ankit Goyal Date: Wed, 9 Aug 2023 15:35:12 -0700 Subject: libvendorgraphicbuffer: Remove implementation for get_video_metadata_fd Bug: 289448426 Test: non-AV1 video playback Change-Id: Ifaf08a4073f90ae02b4417b437ba1389f3256528 --- .../gralloc4/vendor_graphicbuffer_meta.cpp | 14 ++------------ libvendorgraphicbuffer/include/VendorGraphicBuffer.h | 4 +++- 2 files changed, 5 insertions(+), 13 deletions(-) diff --git a/libvendorgraphicbuffer/gralloc4/vendor_graphicbuffer_meta.cpp b/libvendorgraphicbuffer/gralloc4/vendor_graphicbuffer_meta.cpp index 62c550a..664f130 100644 --- a/libvendorgraphicbuffer/gralloc4/vendor_graphicbuffer_meta.cpp +++ b/libvendorgraphicbuffer/gralloc4/vendor_graphicbuffer_meta.cpp @@ -69,19 +69,9 @@ android::sp get_mapper() { return mapper; } -int VendorGraphicBufferMeta::get_video_metadata_fd(buffer_handle_t hnd) +int VendorGraphicBufferMeta::get_video_metadata_fd(buffer_handle_t /*hnd*/) { - const auto *gralloc_hnd = convertNativeHandleToPrivateHandle(hnd); - - if (!gralloc_hnd) - return -EINVAL; - - uint64_t usage = gralloc_hnd->producer_usage | gralloc_hnd->consumer_usage; - - if (usage & VendorGraphicBufferUsage::VIDEO_PRIVATE_DATA) - return gralloc_hnd->get_share_attr_fd(); - else - return -EINVAL; + __builtin_trap(); } int VendorGraphicBufferMeta::get_dataspace(buffer_handle_t hnd) diff --git a/libvendorgraphicbuffer/include/VendorGraphicBuffer.h b/libvendorgraphicbuffer/include/VendorGraphicBuffer.h index ab4452c..0f6a104 100644 --- a/libvendorgraphicbuffer/include/VendorGraphicBuffer.h +++ b/libvendorgraphicbuffer/include/VendorGraphicBuffer.h @@ -147,10 +147,12 @@ public: * When gralloc3 is used, will always return nullptr */ static void* get_video_metadata_roiinfo(buffer_handle_t); - static int get_video_metadata_fd(buffer_handle_t); static int get_dataspace(buffer_handle_t); static int set_dataspace(buffer_handle_t hnd, android_dataspace_t dataspace); + // There should be no users of this function. It'll generate a trap. + static int get_video_metadata_fd(buffer_handle_t); + static buffer_handle_t import_buffer(buffer_handle_t); static int free_buffer(buffer_handle_t); }; -- cgit v1.2.3 From bc54b138095a3f943765070e345115ba7b85b200 Mon Sep 17 00:00:00 2001 From: Ankit Goyal Date: Thu, 10 Aug 2023 22:13:04 -0700 Subject: gralloc4: Add support for custom video metadata Bug: 289448426 Test: ag/24398869 Change-Id: I5a9e7b8a336f26705660c9e5986957c4fa6e8e0e --- gralloc4/Android.bp | 1 + gralloc4/src/hidl_common/MapperMetadata.cpp | 104 +++++++--------------------- gralloc4/src/hidl_common/SharedMetadata.cpp | 12 ++++ gralloc4/src/hidl_common/SharedMetadata.h | 4 ++ 4 files changed, 43 insertions(+), 78 deletions(-) diff --git a/gralloc4/Android.bp b/gralloc4/Android.bp index d97cbdb..7cdc78f 100644 --- a/gralloc4/Android.bp +++ b/gralloc4/Android.bp @@ -93,6 +93,7 @@ arm_gralloc_cc_defaults { }, header_libs: [ "libgralloc_headers", + "pixel-gralloc-headers", ], target: { android: { diff --git a/gralloc4/src/hidl_common/MapperMetadata.cpp b/gralloc4/src/hidl_common/MapperMetadata.cpp index fb79930..81835a3 100644 --- a/gralloc4/src/hidl_common/MapperMetadata.cpp +++ b/gralloc4/src/hidl_common/MapperMetadata.cpp @@ -28,9 +28,8 @@ #include "exynos_format.h" #include "mali_gralloc_formats.h" -#if 0 -#include "aidl/arm/graphics/ArmMetadataType.h" -#endif +#include + #include namespace arm @@ -387,67 +386,22 @@ static android::status_t get_plane_layouts(const private_handle_t *handle, std:: return android::OK; } -#if 0 -static android::status_t get_plane_fds(const private_handle_t *hnd, std::vector *fds) -{ - const int num_planes = get_num_planes(hnd); - - fds->resize(num_planes, static_cast(hnd->share_fd)); - - return android::OK; -} - -/* Encode the number of fds as an int64_t followed by the int64_t fds themselves */ -static android::status_t encodeArmPlaneFds(const std::vector& fds, hidl_vec* output) -{ - int64_t n_fds = fds.size(); - - output->resize((n_fds + 1) * sizeof(int64_t)); - - memcpy(output->data(), &n_fds, sizeof(n_fds)); - memcpy(output->data() + sizeof(n_fds), fds.data(), sizeof(int64_t) * n_fds); - - return android::OK; -} - -/* TODO GPUCORE-22819 Move to test code */ -android::status_t decodeArmPlaneFds(hidl_vec& input, std::vector* fds) -{ - int64_t size = 0; - - memcpy(input.data(), &size, sizeof(int64_t)); - if (size < 0) - { - return android::BAD_VALUE; - } - - fds->resize(size); - - uint8_t *tmp = input.data() + sizeof(int64_t); - memcpy(fds->data(), tmp, sizeof(int64_t) * size); - - return android::OK; -} +static hidl_vec encodePointer(void* ptr) { + constexpr uint8_t kPtrSize = sizeof(void*); -static bool isArmMetadataType(const MetadataType& metadataType) -{ - return metadataType.name == GRALLOC_ARM_METADATA_TYPE_NAME; -} + hidl_vec output(kPtrSize); + std::memcpy(output.data(), &ptr, kPtrSize); -static ArmMetadataType getArmMetadataTypeValue(const MetadataType& metadataType) { - return static_cast(metadataType.value); + return output; } -#endif void get_metadata(const private_handle_t *handle, const IMapper::MetadataType &metadataType, IMapper::get_cb hidl_cb) { - /* This will hold the metadata that is returned. */ + android::status_t err = android::OK; hidl_vec vec; if (android::gralloc4::isStandardMetadataType(metadataType)) { - android::status_t err = android::OK; - switch (android::gralloc4::getStandardMetadataTypeValue(metadataType)) { case StandardMetadataType::BUFFER_ID: @@ -614,38 +568,32 @@ void get_metadata(const private_handle_t *handle, const IMapper::MetadataType &m default: err = android::BAD_VALUE; } - hidl_cb((err) ? Error::UNSUPPORTED : Error::NONE, vec); } - /* TODO: either remove or add support for get_plane_fds */ -#if 0 - else if (isArmMetadataType(metadataType)) - { - android::status_t err = android::OK; - - switch (getArmMetadataTypeValue(metadataType)) - { - case ArmMetadataType::PLANE_FDS: - { - std::vector fds; - - err = get_plane_fds(handle, &fds); - if (!err) + 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)); + break; + case ::pixel::graphics::MetadataType::VIDEO_ROI: { - err = encodeArmPlaneFds(fds, &vec); + auto roi = get_video_roiinfo(handle); + if (roi == nullptr) { + err = android::BAD_VALUE; + } else { + vec = encodePointer(roi); + } + break; } - break; - } - default: - err = android::BAD_VALUE; + default: + err = android::BAD_VALUE; } - hidl_cb((err) ? Error::UNSUPPORTED : Error::NONE, vec); } -#endif else { - /* If known vendor type, return it */ - hidl_cb(Error::UNSUPPORTED, vec); + err = android::BAD_VALUE; } + + hidl_cb((err) ? Error::UNSUPPORTED : Error::NONE, vec); } Error set_metadata(const private_handle_t *handle, const IMapper::MetadataType &metadataType, diff --git a/gralloc4/src/hidl_common/SharedMetadata.cpp b/gralloc4/src/hidl_common/SharedMetadata.cpp index b5964f7..27ed972 100644 --- a/gralloc4/src/hidl_common/SharedMetadata.cpp +++ b/gralloc4/src/hidl_common/SharedMetadata.cpp @@ -18,6 +18,7 @@ #include "SharedMetadata.h" #include "mali_gralloc_log.h" +#include "mali_gralloc_usages.h" //#include namespace arm @@ -160,6 +161,17 @@ android::status_t set_smpte2094_40(const private_handle_t *hnd, const std::optio return android::OK; } +void* get_video_hdr(const private_handle_t *hnd) { + auto *metadata = reinterpret_cast(hnd->attr_base); + return &(metadata->video_private_data); +} + +void* get_video_roiinfo(const private_handle_t *hnd) { + if (!(hnd->get_usage() & GRALLOC_USAGE_ROIINFO)) + return nullptr; + + return static_cast(hnd->attr_base) + sizeof(shared_metadata) + hnd->reserved_region_size; +} } // namespace common } // namespace mapper } // namespace arm diff --git a/gralloc4/src/hidl_common/SharedMetadata.h b/gralloc4/src/hidl_common/SharedMetadata.h index e1f6d0b..9961733 100644 --- a/gralloc4/src/hidl_common/SharedMetadata.h +++ b/gralloc4/src/hidl_common/SharedMetadata.h @@ -68,6 +68,10 @@ android::status_t set_cta861_3(const private_handle_t *hnd, const std::optional< void get_smpte2094_40(const private_handle_t *hnd, std::optional> *smpte2094_40); android::status_t set_smpte2094_40(const private_handle_t *hnd, const std::optional> &smpte2094_40); +void* get_video_hdr(const private_handle_t *hnd); + +void* get_video_roiinfo(const private_handle_t *hnd); + } // namespace common } // namespace mapper } // namespace arm -- cgit v1.2.3 From cacbca63fa43553cf60f03e6b790bf49c8a3dbb8 Mon Sep 17 00:00:00 2001 From: Ankit Goyal Date: Thu, 10 Aug 2023 22:14:23 -0700 Subject: libvendorgraphicbuffer: Use metadata queries for custom video metadata Bug: 289448426 Test: ag/24398869 Change-Id: I264b1a83a5d7b2d5de04db7b0f56da95f8d41873 --- libvendorgraphicbuffer/Android.bp | 1 + .../gralloc4/vendor_graphicbuffer_meta.cpp | 76 +++++++++++++++++++--- 2 files changed, 67 insertions(+), 10 deletions(-) diff --git a/libvendorgraphicbuffer/Android.bp b/libvendorgraphicbuffer/Android.bp index de3721a..0230bfb 100644 --- a/libvendorgraphicbuffer/Android.bp +++ b/libvendorgraphicbuffer/Android.bp @@ -43,6 +43,7 @@ cc_library_shared { ], header_libs: [ "libgralloc_headers", + "pixel-gralloc-headers", ], include_dirs: [ "hardware/google/gchips/include", diff --git a/libvendorgraphicbuffer/gralloc4/vendor_graphicbuffer_meta.cpp b/libvendorgraphicbuffer/gralloc4/vendor_graphicbuffer_meta.cpp index 664f130..eb8d663 100644 --- a/libvendorgraphicbuffer/gralloc4/vendor_graphicbuffer_meta.cpp +++ b/libvendorgraphicbuffer/gralloc4/vendor_graphicbuffer_meta.cpp @@ -27,6 +27,8 @@ #include "hidl_common/SharedMetadata_struct.h" #include "exynos_format.h" +#include + using namespace android; using namespace vendor::graphics; @@ -38,6 +40,7 @@ using android::gralloc4::decodePixelFormatFourCC; using android::gralloc4::decodePixelFormatModifier; using android::hardware::graphics::mapper::V4_0::IMapper; using android::hardware::graphics::mapper::V4_0::Error; +using MapperMetadataType = android::hardware::graphics::mapper::V4_0::IMapper::MetadataType; #define UNUSED(x) ((void)x) #define SZ_4k 0x1000 @@ -71,6 +74,7 @@ android::sp get_mapper() { int VendorGraphicBufferMeta::get_video_metadata_fd(buffer_handle_t /*hnd*/) { + ALOGE("%s function is obsolete and should not be used", __FUNCTION__); __builtin_trap(); } @@ -223,28 +227,80 @@ uint64_t VendorGraphicBufferMeta::get_usage(buffer_handle_t hnd) return gralloc_hnd->producer_usage | gralloc_hnd->consumer_usage; } +void* decodePointer(const android::hardware::hidl_vec& tmpVec) { + constexpr uint8_t kPtrSize = sizeof(void*); + assert(tmpVec.size() == kPtrSize); + + void* data_ptr; + std::memcpy(&data_ptr, tmpVec.data(), kPtrSize); + + return data_ptr; +} + void* VendorGraphicBufferMeta::get_video_metadata(buffer_handle_t hnd) { - const auto *gralloc_hnd = convertNativeHandleToPrivateHandle(hnd); + native_handle_t* handle = const_cast(hnd); + if (!handle) { + return nullptr; + } + + MapperMetadataType metadata_type{ + .name = ::pixel::graphics::kPixelMetadataTypeName, + .value = static_cast(::pixel::graphics::MetadataType::VIDEO_HDR), + }; + + Error error = Error::NONE; + void* output = nullptr; + + get_mapper()->get(handle, metadata_type, + [&](const auto& tmpError, const android::hardware::hidl_vec& tmpVec) { + error = tmpError; + if (error != Error::NONE) { + return; + } + output = decodePointer(tmpVec); + }); + - if (gralloc_hnd == nullptr) + if (error != Error::NONE) { + ALOGE("Failed to get video HDR metadata"); return nullptr; + } - return gralloc_hnd->attr_base; + return output; } void* VendorGraphicBufferMeta::get_video_metadata_roiinfo(buffer_handle_t hnd) { - const auto *gralloc_hnd = convertNativeHandleToPrivateHandle(hnd); - - if (gralloc_hnd == nullptr) + native_handle_t* handle = const_cast(hnd); + if (!handle) { return nullptr; + } + + MapperMetadataType metadata_type{ + .name = ::pixel::graphics::kPixelMetadataTypeName, + .value = static_cast(::pixel::graphics::MetadataType::VIDEO_ROI), + }; - if (gralloc_hnd->get_usage() & VendorGraphicBufferUsage::ROIINFO) - return static_cast(gralloc_hnd->attr_base) + - sizeof(shared_metadata) + gralloc_hnd->reserved_region_size; + Error error = Error::NONE; + void* output = nullptr; + + get_mapper()->get(handle, metadata_type, + [&](const auto& tmpError, const android::hardware::hidl_vec& tmpVec) { + error = tmpError; + if (error != Error::NONE) { + return; + } + output = decodePointer(tmpVec); + }); + + + if (error != Error::NONE) { + ALOGE("Failed to get video HDR metadata"); + return nullptr; + } - return nullptr; + return output; } uint32_t VendorGraphicBufferMeta::get_format_fourcc(buffer_handle_t hnd) { -- cgit v1.2.3 From 3a72a130c95f5fbacb5a16f0019d422c18fc5336 Mon Sep 17 00:00:00 2001 From: Ankit Goyal Date: Tue, 15 Aug 2023 15:15:22 -0700 Subject: gralloc4: Cap the maximum size of allocation Bug: 283102307 Test: Tested with gralloc_dump_buffer_meta Change-Id: I647e707e66028941b7840fe7b2c5a5884c004c14 --- .../src/core/mali_gralloc_bufferallocation.cpp | 53 ++++++++++++++++++---- 1 file changed, 43 insertions(+), 10 deletions(-) diff --git a/gralloc4/src/core/mali_gralloc_bufferallocation.cpp b/gralloc4/src/core/mali_gralloc_bufferallocation.cpp index 7a3d3fd..5b6f6a6 100644 --- a/gralloc4/src/core/mali_gralloc_bufferallocation.cpp +++ b/gralloc4/src/core/mali_gralloc_bufferallocation.cpp @@ -465,31 +465,64 @@ 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_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 true; + return false; } if (usage & UNSUPPORTED_MALI_GRALLOC_USAGE_MIPMAP_COMPLETE) { MALI_GRALLOC_LOGW("BufferUsage::GPU_MIPMAP_COMPLETE is unsupported"); - return true; + return false; } - 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 true; +} + +static bool validate_descriptor(buffer_descriptor_t * const bufDescriptor) { + if (!log_obsolete_usage_flags(bufDescriptor->producer_usage | bufDescriptor->consumer_usage)) { + return false; + } + + if (!validate_size(bufDescriptor->layer_count, bufDescriptor->width, bufDescriptor->height)) { + return false; + } + + return true; } /* @@ -1000,7 +1033,7 @@ int mali_gralloc_derive_format_and_size(buffer_descriptor_t * const bufDescripto int alloc_height = bufDescriptor->height; uint64_t usage = bufDescriptor->producer_usage | bufDescriptor->consumer_usage; - if (log_obsolete_usage_flags(usage)) { + if (!validate_descriptor(bufDescriptor)) { return -EINVAL; } -- cgit v1.2.3 From 5fb0d18fd77d8525f2447d78eae7680ef2da6c6d Mon Sep 17 00:00:00 2001 From: Ankit Goyal Date: Thu, 17 Aug 2023 14:51:05 -0700 Subject: gralloc4: Remove bases from handle Bug: 213170949 Test: Boot to home Test: VtsHalGraphicsMapperV4_0TargetTest Change-Id: I0a0a071c5fc6e304c025a53217cd798452b0c2c3 --- gralloc4/src/allocator/mali_gralloc_ion.cpp | 40 +++++++---------- gralloc4/src/allocator/mali_gralloc_ion.h | 4 +- gralloc4/src/core/mali_gralloc_bufferaccess.cpp | 11 ++++- gralloc4/src/core/mali_gralloc_reference.cpp | 60 ++++++++++++++++++------- gralloc4/src/core/mali_gralloc_reference.h | 5 ++- gralloc4/src/hidl_common/Mapper.cpp | 8 ---- gralloc4/src/libGralloc4Wrapper/wrapper.cpp | 23 ---------- gralloc4/src/mali_gralloc_buffer.h | 6 +-- 8 files changed, 74 insertions(+), 83 deletions(-) diff --git a/gralloc4/src/allocator/mali_gralloc_ion.cpp b/gralloc4/src/allocator/mali_gralloc_ion.cpp index 43fd1be..85409d7 100644 --- a/gralloc4/src/allocator/mali_gralloc_ion.cpp +++ b/gralloc4/src/allocator/mali_gralloc_ion.cpp @@ -301,20 +301,8 @@ void mali_gralloc_ion_free(private_handle_t * const hnd) { for (int i = 0; i < hnd->fd_count; i++) { - void* mapped_addr = reinterpret_cast(hnd->bases[i]); - - /* Buffer might be unregistered already so we need to assure we have a valid handle */ - if (mapped_addr != nullptr) - { - if (munmap(mapped_addr, hnd->alloc_sizes[i]) != 0) - { - /* TODO: more detailed error logs */ - MALI_GRALLOC_LOGE("Failed to munmap handle %p", hnd); - } - } close(hnd->fds[i]); hnd->fds[i] = -1; - hnd->bases[i] = 0; } delete hnd; } @@ -482,15 +470,17 @@ int mali_gralloc_ion_allocate(const gralloc_buffer_descriptor_t *descriptors, return 0; } -int mali_gralloc_ion_map(private_handle_t *hnd) +std::array mali_gralloc_ion_map(private_handle_t *hnd) { - uint64_t usage = hnd->producer_usage | hnd->consumer_usage; + std::array vaddrs; + vaddrs.fill(nullptr); + uint64_t usage = hnd->producer_usage | hnd->consumer_usage; /* Do not allow cpu access to secure buffers */ if (usage & (GRALLOC_USAGE_PROTECTED | GRALLOC_USAGE_NOZEROED) && !(usage & GRALLOC_USAGE_PRIVATE_NONSECURE)) { - return 0; + return vaddrs; } for (int fidx = 0; fidx < hnd->fd_count; fidx++) { @@ -507,38 +497,38 @@ int mali_gralloc_ion_map(private_handle_t *hnd) for (int cidx = 0; cidx < fidx; fidx++) { - munmap((void*)hnd->bases[cidx], hnd->alloc_sizes[cidx]); - hnd->bases[cidx] = 0; + munmap((void*)vaddrs[cidx], hnd->alloc_sizes[cidx]); + vaddrs[cidx] = 0; } - return -err; + return vaddrs; } - hnd->bases[fidx] = uintptr_t(mappedAddress); + vaddrs[fidx] = mappedAddress; } - return 0; + return vaddrs; } -void mali_gralloc_ion_unmap(private_handle_t *hnd) +void mali_gralloc_ion_unmap(private_handle_t *hnd, std::array& vaddrs) { for (int i = 0; i < hnd->fd_count; i++) { int err = 0; - if (hnd->bases[i]) + if (vaddrs[i]) { - err = munmap((void*)hnd->bases[i], hnd->alloc_sizes[i]); + err = munmap(vaddrs[i], hnd->alloc_sizes[i]); } if (err) { MALI_GRALLOC_LOGE("Could not munmap base:%p size:%" PRIu64 " '%s'", - (void*)hnd->bases[i], hnd->alloc_sizes[i], strerror(errno)); + (void*)vaddrs[i], hnd->alloc_sizes[i], strerror(errno)); } else { - hnd->bases[i] = 0; + vaddrs[i] = 0; } } diff --git a/gralloc4/src/allocator/mali_gralloc_ion.h b/gralloc4/src/allocator/mali_gralloc_ion.h index 5f55c2f..d826650 100644 --- a/gralloc4/src/allocator/mali_gralloc_ion.h +++ b/gralloc4/src/allocator/mali_gralloc_ion.h @@ -30,8 +30,8 @@ int mali_gralloc_ion_sync_start(const private_handle_t * const hnd, const bool read, const bool write); int mali_gralloc_ion_sync_end(const private_handle_t * const hnd, const bool read, const bool write); -int mali_gralloc_ion_map(private_handle_t *hnd); -void mali_gralloc_ion_unmap(private_handle_t *hnd); +std::array mali_gralloc_ion_map(private_handle_t *hnd); +void mali_gralloc_ion_unmap(private_handle_t *hnd, std::array& vaddrs); int mali_gralloc_attr_allocate(void); #endif /* MALI_GRALLOC_ION_H_ */ diff --git a/gralloc4/src/core/mali_gralloc_bufferaccess.cpp b/gralloc4/src/core/mali_gralloc_bufferaccess.cpp index adda46b..344ab2a 100644 --- a/gralloc4/src/core/mali_gralloc_bufferaccess.cpp +++ b/gralloc4/src/core/mali_gralloc_bufferaccess.cpp @@ -221,9 +221,16 @@ int mali_gralloc_lock(buffer_handle_t buffer, return -EINVAL; } - mali_gralloc_reference_map(buffer); + if (mali_gralloc_reference_map(buffer) != 0) { + return -EINVAL; + } - *vaddr = (void *)hnd->bases[0]; + std::optional buf_addr = mali_gralloc_reference_get_buf_addr(buffer); + if (!buf_addr.has_value()) { + MALI_GRALLOC_LOGE("BUG: Invalid buffer address on a just mapped buffer"); + return -EINVAL; + } + *vaddr = buf_addr.value(); buffer_sync(hnd, get_tx_direction(usage)); } diff --git a/gralloc4/src/core/mali_gralloc_reference.cpp b/gralloc4/src/core/mali_gralloc_reference.cpp index b84cbb4..84f4c14 100644 --- a/gralloc4/src/core/mali_gralloc_reference.cpp +++ b/gralloc4/src/core/mali_gralloc_reference.cpp @@ -35,7 +35,7 @@ private: // should become the only place where address mapping is maintained and can be // queried from. struct MappedData { - void *bases[MAX_BUFFER_FDS] = {}; + std::array bases; size_t alloc_sizes[MAX_BUFFER_FDS] = {}; uint64_t ref_count = 0; }; @@ -94,7 +94,8 @@ private: } int map_locked(buffer_handle_t handle) REQUIRES(lock) { - private_handle_t *hnd = (private_handle_t *)handle; + private_handle_t *hnd = + reinterpret_cast(const_cast(handle)); auto it = buffer_map.find(hnd); if (it == buffer_map.end()) { @@ -116,13 +117,12 @@ private: return -EINVAL; } - int error = mali_gralloc_ion_map(hnd); - if (error != 0) { - return error; + data.bases = mali_gralloc_ion_map(hnd); + if (data.bases[0] == nullptr) { + return -EINVAL; } for (auto i = 0; i < MAX_BUFFER_FDS; i++) { - data.bases[i] = reinterpret_cast(hnd->bases[i]); data.alloc_sizes[i] = hnd->alloc_sizes[i]; } @@ -135,7 +135,7 @@ private: return -EINVAL; } - const auto *hnd = (private_handle_t *)handle; + const auto *hnd = reinterpret_cast(const_cast(handle)); auto it = buffer_map.find(hnd); if (it == buffer_map.end()) { MALI_GRALLOC_LOGE("Reference unimported buffer %p, returning error", handle); @@ -145,8 +145,7 @@ private: auto &data = *(it->second.get()); if (data.bases[0] != nullptr) { for (auto i = 0; i < MAX_BUFFER_FDS; i++) { - if (data.bases[i] != reinterpret_cast(hnd->bases[i]) || - data.alloc_sizes[i] != hnd->alloc_sizes[i]) { + if (data.alloc_sizes[i] != hnd->alloc_sizes[i]) { MALI_GRALLOC_LOGE( "Validation failed: Buffer attributes inconsistent with mapper"); return -EINVAL; @@ -154,7 +153,7 @@ private: } } else { for (auto i = 0; i < MAX_BUFFER_FDS; i++) { - if (hnd->bases[i] != 0 || data.bases[i] != nullptr) { + if (data.bases[i] != nullptr) { MALI_GRALLOC_LOGE("Validation failed: Expected nullptr for unmapped buffer"); return -EINVAL; } @@ -177,7 +176,8 @@ public: } std::lock_guard _l(lock); - private_handle_t *hnd = (private_handle_t *)handle; + private_handle_t *hnd = + reinterpret_cast(const_cast(handle)); auto it = buffer_map.find(hnd); if (it == buffer_map.end()) { @@ -189,10 +189,6 @@ public: MALI_GRALLOC_LOGE("Failed to create buffer data mapping"); return -EINVAL; } - - for (int i = 0; i < MAX_BUFFER_FDS; i++) { - hnd->bases[i] = 0; - } } else if (it->second->ref_count == 0) { MALI_GRALLOC_LOGE("BUG: Import counter of an imported buffer is 0, expect errors"); } @@ -224,7 +220,8 @@ public: return error; } - private_handle_t *hnd = (private_handle_t *)handle; + private_handle_t *hnd = + reinterpret_cast(const_cast(handle)); auto it = buffer_map.find(hnd); if (it == buffer_map.end()) { MALI_GRALLOC_LOGE("Trying to release a non-imported buffer"); @@ -240,7 +237,7 @@ public: data.ref_count--; if (data.ref_count == 0) { if (data.bases[0] != nullptr) { - mali_gralloc_ion_unmap(hnd); + mali_gralloc_ion_unmap(hnd, data.bases); } /* TODO: Make this unmapping of shared meta fd into a function? */ @@ -258,6 +255,31 @@ public: std::lock_guard _l(lock); return validate_locked(handle); } + + std::optional get_buf_addr(buffer_handle_t handle) { + std::lock_guard _l(lock); + + auto error = validate_locked(handle); + if (error != 0) { + return {}; + } + + private_handle_t *hnd = + reinterpret_cast(const_cast(handle)); + auto it = buffer_map.find(hnd); + if (it == buffer_map.end()) { + MALI_GRALLOC_LOGE("BUG: Cannot find a validated buffer"); + return {}; + } + + const auto &data = *(it->second.get()); + if (data.bases[0] == nullptr) { + MALI_GRALLOC_LOGE("BUG: Called %s for an un-mapped buffer", __FUNCTION__); + return {}; + } + + return data.bases[0]; + } }; int mali_gralloc_reference_retain(buffer_handle_t handle) { @@ -275,3 +297,7 @@ int mali_gralloc_reference_release(buffer_handle_t handle) { int mali_gralloc_reference_validate(buffer_handle_t handle) { return BufferManager::getInstance().validate(handle); } + +std::optional mali_gralloc_reference_get_buf_addr(buffer_handle_t handle) { + return BufferManager::getInstance().get_buf_addr(handle); +} diff --git a/gralloc4/src/core/mali_gralloc_reference.h b/gralloc4/src/core/mali_gralloc_reference.h index acf8e82..257bb6b 100644 --- a/gralloc4/src/core/mali_gralloc_reference.h +++ b/gralloc4/src/core/mali_gralloc_reference.h @@ -19,11 +19,14 @@ #ifndef MALI_GRALLOC_REFERENCE_H_ #define MALI_GRALLOC_REFERENCE_H_ -#include "gralloc_priv.h" +#include +#include int mali_gralloc_reference_retain(buffer_handle_t handle); int mali_gralloc_reference_release(buffer_handle_t handle); int mali_gralloc_reference_validate(buffer_handle_t handle); int mali_gralloc_reference_map(buffer_handle_t handle); +std::optional mali_gralloc_reference_get_buf_addr(buffer_handle_t handle); + #endif /* MALI_GRALLOC_REFERENCE_H_ */ diff --git a/gralloc4/src/hidl_common/Mapper.cpp b/gralloc4/src/hidl_common/Mapper.cpp index f6ebe6f..aa8e6d2 100644 --- a/gralloc4/src/hidl_common/Mapper.cpp +++ b/gralloc4/src/hidl_common/Mapper.cpp @@ -245,14 +245,6 @@ static Error unlockBuffer(buffer_handle_t bufferHandle, } auto private_handle = private_handle_t::dynamicCast(bufferHandle); -#if 0 - if (!private_handle->cpu_write && !private_handle->cpu_read) - { - MALI_GRALLOC_LOGW("Attempt to call unlock*() on an unlocked buffer (%p)", bufferHandle); - - /* TODO: handle simulatneous locks differently. May be keep a global lock count per buffer? */ - } -#endif const int result = mali_gralloc_unlock(bufferHandle); if (result) diff --git a/gralloc4/src/libGralloc4Wrapper/wrapper.cpp b/gralloc4/src/libGralloc4Wrapper/wrapper.cpp index c9d3381..7d286e0 100644 --- a/gralloc4/src/libGralloc4Wrapper/wrapper.cpp +++ b/gralloc4/src/libGralloc4Wrapper/wrapper.cpp @@ -195,15 +195,6 @@ int freeImportedHandle(void *handle) const private_handle_t *hnd = static_cast(handle); - struct UnmapWork { void *base; size_t alloc_size; }; - std::vector work(hnd->fd_count); - - for (size_t i = 0; i < hnd->fd_count; ++i) - { - work[i].base = reinterpret_cast(hnd->bases[i]); - work[i].alloc_size = hnd->alloc_sizes[i]; - } - static android::sp mapper = IMapper::getService(); if (!mapper) { @@ -217,20 +208,6 @@ int freeImportedHandle(void *handle) return -1; } - { - bool unmapFailed = false; - for (const UnmapWork &w : work) - { - if (!w.base) { continue; } - if (int ret = munmap(w.base, w.alloc_size); ret) - { - ALOGE("libGralloc4Wrapper: %s couldn't unmap address %p (size %zu): %s", __func__, w.base, w.alloc_size, strerror(ret)); - unmapFailed = true; - } - } - if (unmapFailed) { return -1; } - } - return 0; } diff --git a/gralloc4/src/mali_gralloc_buffer.h b/gralloc4/src/mali_gralloc_buffer.h index c4db9fc..11c41f5 100644 --- a/gralloc4/src/mali_gralloc_buffer.h +++ b/gralloc4/src/mali_gralloc_buffer.h @@ -236,7 +236,6 @@ struct private_handle_t // locally mapped shared attribute area int ion_handles[MAX_BUFFER_FDS]; - uint64_t bases[MAX_BUFFER_FDS]; uint64_t alloc_sizes[MAX_BUFFER_FDS]; void *attr_base __attribute__((aligned (8))) DEFAULT_INITIALIZER(nullptr); @@ -292,7 +291,6 @@ struct private_handle_t if (_alloc_sizes) memcpy(alloc_sizes, _alloc_sizes, sizeof(alloc_sizes)); - memset(bases, 0, sizeof(bases)); memset(ion_handles, 0, sizeof(ion_handles)); } @@ -406,7 +404,6 @@ struct private_handle_t "alloc_format(0x%" PRIx64 ") " "alloc_sizes(%" PRIu64 " %" PRIu64 " %" PRIu64 ") " "layer_count(%d) " - "bases(%p %p %p %p) " "\n", str, numInts, numFds, fd_count, @@ -421,8 +418,7 @@ struct private_handle_t plane_info[2].size, plane_info[2].byte_stride, plane_info[2].alloc_width, plane_info[2].alloc_height, alloc_format, alloc_sizes[0], alloc_sizes[1], alloc_sizes[2], - layer_count, - (void*)bases[0], (void*)bases[1], (void*)bases[2], attr_base + layer_count ); } -- cgit v1.2.3 From c3daffd8ba9188f72c9d89d8782a8a9f41196f08 Mon Sep 17 00:00:00 2001 From: Ankit Goyal Date: Fri, 18 Aug 2023 13:25:04 -0700 Subject: gralloc4: Defer metadata mmap and remove metadata vaddr from handle Fix: 213170949 Fix: 290275019 Test: Boot to home Test: VtsHalGraphicsMapperV4_0TargetTest Test: aion_test Change-Id: I559bb9081bc51fdf2d304b5cde17ac4f8cf1e138 --- gralloc4/src/Android.bp | 1 - gralloc4/src/allocator/Android.bp | 1 - .../src/allocator/mali_gralloc_shared_memory.cpp | 37 ---- .../src/allocator/mali_gralloc_shared_memory.h | 31 ---- .../src/core/mali_gralloc_bufferallocation.cpp | 5 +- gralloc4/src/core/mali_gralloc_reference.cpp | 189 ++++++++++++++------- gralloc4/src/core/mali_gralloc_reference.h | 1 + gralloc4/src/hidl_common/Allocator.cpp | 27 ++- gralloc4/src/hidl_common/Mapper.cpp | 27 +-- gralloc4/src/hidl_common/SharedMetadata.cpp | 32 ++-- gralloc4/src/hidl_common/SharedMetadata.h | 1 - gralloc4/src/libGralloc4Wrapper/wrapper.cpp | 20 ++- gralloc4/src/mali_gralloc_buffer.h | 1 - 13 files changed, 177 insertions(+), 196 deletions(-) delete mode 100644 gralloc4/src/allocator/mali_gralloc_shared_memory.cpp delete mode 100644 gralloc4/src/allocator/mali_gralloc_shared_memory.h diff --git a/gralloc4/src/Android.bp b/gralloc4/src/Android.bp index ecd2fbd..4109439 100644 --- a/gralloc4/src/Android.bp +++ b/gralloc4/src/Android.bp @@ -45,7 +45,6 @@ cc_library_shared { srcs: [ "libGralloc4Wrapper/wrapper.cpp", "allocator/mali_gralloc_ion.cpp", - "allocator/mali_gralloc_shared_memory.cpp", "core/format_info.cpp", "core/mali_gralloc_formats.cpp", "core/mali_gralloc_bufferallocation.cpp", diff --git a/gralloc4/src/allocator/Android.bp b/gralloc4/src/allocator/Android.bp index 5ff23f8..3170c38 100644 --- a/gralloc4/src/allocator/Android.bp +++ b/gralloc4/src/allocator/Android.bp @@ -77,7 +77,6 @@ arm_gralloc_allocator_cc_defaults { }, srcs: [ "mali_gralloc_ion.cpp", - "mali_gralloc_shared_memory.cpp", ], static_libs: [ "libarect", diff --git a/gralloc4/src/allocator/mali_gralloc_shared_memory.cpp b/gralloc4/src/allocator/mali_gralloc_shared_memory.cpp deleted file mode 100644 index b681cd9..0000000 --- a/gralloc4/src/allocator/mali_gralloc_shared_memory.cpp +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright (C) 2020 Arm Limited. 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. - */ - -#include "mali_gralloc_shared_memory.h" -#include "mali_gralloc_log.h" -#include "mali_gralloc_buffer.h" -#include "mali_gralloc_ion.h" -#include "gralloc_helper.h" - -/* TODO: move shared attr memory allocation function here */ - -void gralloc_shared_memory_free(private_handle_t *hnd) -{ - if (hnd->attr_base) - { - munmap(hnd->attr_base, hnd->attr_size); - hnd->attr_base = nullptr; - } - - hnd->close_share_attr_fd(); - hnd->set_share_attr_fd(-1); -} diff --git a/gralloc4/src/allocator/mali_gralloc_shared_memory.h b/gralloc4/src/allocator/mali_gralloc_shared_memory.h deleted file mode 100644 index 28e0a62..0000000 --- a/gralloc4/src/allocator/mali_gralloc_shared_memory.h +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright (C) 2020 Arm Limited. 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_SHARED_MEMORY_H_ -#define GRALLOC_SHARED_MEMORY_H_ - -#include -#include -#include "mali_gralloc_buffer.h" - -/* - * Frees resources acquired from gralloc_shared_memory_allocate. - */ -void gralloc_shared_memory_free(private_handle_t *hnd); - -#endif /* GRALLOC_SHARED_MEMORY_H_ */ diff --git a/gralloc4/src/core/mali_gralloc_bufferallocation.cpp b/gralloc4/src/core/mali_gralloc_bufferallocation.cpp index 5b6f6a6..4786560 100644 --- a/gralloc4/src/core/mali_gralloc_bufferallocation.cpp +++ b/gralloc4/src/core/mali_gralloc_bufferallocation.cpp @@ -30,7 +30,6 @@ #include "mali_gralloc_bufferallocation.h" #include "allocator/mali_gralloc_ion.h" -#include "allocator/mali_gralloc_shared_memory.h" #include "mali_gralloc_buffer.h" #include "mali_gralloc_bufferdescriptor.h" #include "mali_gralloc_log.h" @@ -1256,8 +1255,8 @@ int mali_gralloc_buffer_free(buffer_handle_t pHandle) return -1; } - gralloc_shared_memory_free(hnd); - mali_gralloc_ion_free(hnd); + native_handle_close(hnd); + native_handle_delete(hnd); return 0; } diff --git a/gralloc4/src/core/mali_gralloc_reference.cpp b/gralloc4/src/core/mali_gralloc_reference.cpp index 84f4c14..6726895 100644 --- a/gralloc4/src/core/mali_gralloc_reference.cpp +++ b/gralloc4/src/core/mali_gralloc_reference.cpp @@ -37,6 +37,10 @@ private: struct MappedData { std::array bases; size_t alloc_sizes[MAX_BUFFER_FDS] = {}; + + void *metadata_vaddr; + size_t metadata_size; + uint64_t ref_count = 0; }; @@ -52,7 +56,9 @@ private: return size; } - static bool dmabuf_sanity_check(buffer_handle_t handle) { + // TODO(b/296934447): AION buffers set the size of the buffer themselves. That size exceeds the + // size of the actual allocated dmabuf. + static bool dmabuf_sanity_check(buffer_handle_t handle, bool skip_buffer_size_check = false) { private_handle_t *hnd = static_cast(const_cast(handle)); @@ -77,10 +83,13 @@ private: }; // Check client facing dmabufs - for (auto i = 0; i < hnd->fd_count; i++) { - if (!check_pid(hnd->fds[i], hnd->alloc_sizes[i])) { - MALI_GRALLOC_LOGE("%s failed: Size check failed for alloc_sizes[%d]", __func__, i); - return false; + if (!skip_buffer_size_check) { + for (auto i = 0; i < hnd->fd_count; i++) { + if (!check_pid(hnd->fds[i], hnd->alloc_sizes[i])) { + MALI_GRALLOC_LOGE("%s failed: Size check failed for alloc_sizes[%d]", __func__, + i); + return false; + } } } @@ -93,53 +102,75 @@ private: return true; } - int map_locked(buffer_handle_t handle) REQUIRES(lock) { - private_handle_t *hnd = - reinterpret_cast(const_cast(handle)); - auto it = buffer_map.find(hnd); - - if (it == buffer_map.end()) { - MALI_GRALLOC_LOGE("BUG: Map called without importing buffer"); - return -EINVAL; - } - - auto &data = *(it->second.get()); - if (data.ref_count == 0) { - MALI_GRALLOC_LOGE("BUG: Found an imported buffer with ref count 0, expect errors"); + bool map_buffer_locked(buffer_handle_t handle) REQUIRES(lock) { + auto data_oe = get_validated_data_locked(handle); + if (!data_oe.has_value()) { + return false; } + MappedData &data = data_oe.value(); // Return early if buffer is already mapped if (data.bases[0] != nullptr) { - return 0; + return true; } if (!dmabuf_sanity_check(handle)) { - return -EINVAL; + return false; } + private_handle_t *hnd = + reinterpret_cast(const_cast(handle)); data.bases = mali_gralloc_ion_map(hnd); if (data.bases[0] == nullptr) { - return -EINVAL; + return false; } for (auto i = 0; i < MAX_BUFFER_FDS; i++) { data.alloc_sizes[i] = hnd->alloc_sizes[i]; } - return 0; + return true; } - int validate_locked(buffer_handle_t handle) REQUIRES(lock) { + bool map_metadata_locked(buffer_handle_t handle) REQUIRES(lock) { + auto data_oe = get_validated_data_locked(handle); + if (!data_oe.has_value()) { + return false; + } + MappedData &data = data_oe.value(); + + // Return early if buffer is already mapped + if (data.metadata_vaddr != nullptr) { + return true; + } + + if (!dmabuf_sanity_check(handle, /*skip_buffer_size_check=*/true)) { + return false; + } + + private_handle_t *hnd = + reinterpret_cast(const_cast(handle)); + data.metadata_vaddr = mmap(nullptr, hnd->attr_size, PROT_READ | PROT_WRITE, MAP_SHARED, + hnd->get_share_attr_fd(), 0); + if (data.metadata_vaddr == nullptr) { + return false; + } + + data.metadata_size = hnd->attr_size; + return true; + } + + bool validate_locked(buffer_handle_t handle) REQUIRES(lock) { if (private_handle_t::validate(handle) < 0) { MALI_GRALLOC_LOGE("Reference invalid buffer %p, returning error", handle); - return -EINVAL; + return false; } const auto *hnd = reinterpret_cast(const_cast(handle)); auto it = buffer_map.find(hnd); if (it == buffer_map.end()) { MALI_GRALLOC_LOGE("Reference unimported buffer %p, returning error", handle); - return -EINVAL; + return false; } auto &data = *(it->second.get()); @@ -148,19 +179,41 @@ private: if (data.alloc_sizes[i] != hnd->alloc_sizes[i]) { MALI_GRALLOC_LOGE( "Validation failed: Buffer attributes inconsistent with mapper"); - return -EINVAL; + return false; } } } else { for (auto i = 0; i < MAX_BUFFER_FDS; i++) { if (data.bases[i] != nullptr) { MALI_GRALLOC_LOGE("Validation failed: Expected nullptr for unmapped buffer"); - return -EINVAL; + return false; } } } - return 0; + return true; + } + + std::optional> get_validated_data_locked( + buffer_handle_t handle) REQUIRES(lock) { + if (!validate_locked(handle)) { + return {}; + } + + private_handle_t *hnd = + reinterpret_cast(const_cast(handle)); + auto it = buffer_map.find(hnd); + if (it == buffer_map.end()) { + MALI_GRALLOC_LOGE("Trying to release a non-imported buffer"); + return {}; + } + + MappedData &data = *(it->second.get()); + if (data.ref_count == 0) { + MALI_GRALLOC_LOGE("BUG: Found an imported buffer with ref count 0, expect errors"); + } + + return data; } public: @@ -200,35 +253,22 @@ public: int map(buffer_handle_t handle) EXCLUDES(lock) { std::lock_guard _l(lock); - auto error = validate_locked(handle); - if (error != 0) { - return error; + if (!map_buffer_locked(handle)) { + return -EINVAL; } - return map_locked(handle); + return 0; } int release(buffer_handle_t handle) EXCLUDES(lock) { std::lock_guard _l(lock); - // Always call locked variant of validate from this function. On calling - // the other validate variant, an attacker might launch a timing attack - // where they would try to time their attack between the return of - // validate and before taking the lock in this function again. - auto error = validate_locked(handle); - if (error != 0) { - return error; - } - - private_handle_t *hnd = - reinterpret_cast(const_cast(handle)); - auto it = buffer_map.find(hnd); - if (it == buffer_map.end()) { - MALI_GRALLOC_LOGE("Trying to release a non-imported buffer"); + auto data_oe = get_validated_data_locked(handle); + if (!data_oe.has_value()) { return -EINVAL; } + MappedData &data = data_oe.value(); - auto &data = *(it->second.get()); if (data.ref_count == 0) { MALI_GRALLOC_LOGE("BUG: Reference held for buffer whose counter is 0"); return -EINVAL; @@ -236,15 +276,19 @@ public: data.ref_count--; if (data.ref_count == 0) { + private_handle_t *hnd = + reinterpret_cast(const_cast(handle)); + auto it = buffer_map.find(hnd); + if (data.bases[0] != nullptr) { mali_gralloc_ion_unmap(hnd, data.bases); } - /* TODO: Make this unmapping of shared meta fd into a function? */ - if (hnd->attr_base) { - munmap(hnd->attr_base, hnd->attr_size); - hnd->attr_base = nullptr; + if (data.metadata_vaddr != nullptr) { + munmap(data.metadata_vaddr, data.metadata_size); + data.metadata_vaddr = nullptr; } + buffer_map.erase(it); } @@ -253,26 +297,23 @@ public: int validate(buffer_handle_t handle) EXCLUDES(lock) { std::lock_guard _l(lock); - return validate_locked(handle); + + if (!validate_locked(handle)) { + return -EINVAL; + } + + return 0; } std::optional get_buf_addr(buffer_handle_t handle) { std::lock_guard _l(lock); - auto error = validate_locked(handle); - if (error != 0) { - return {}; - } - - private_handle_t *hnd = - reinterpret_cast(const_cast(handle)); - auto it = buffer_map.find(hnd); - if (it == buffer_map.end()) { - MALI_GRALLOC_LOGE("BUG: Cannot find a validated buffer"); + auto data_oe = get_validated_data_locked(handle); + if (!data_oe.has_value()) { return {}; } + MappedData &data = data_oe.value(); - const auto &data = *(it->second.get()); if (data.bases[0] == nullptr) { MALI_GRALLOC_LOGE("BUG: Called %s for an un-mapped buffer", __FUNCTION__); return {}; @@ -280,6 +321,24 @@ public: return data.bases[0]; } + + std::optional get_metadata_addr(buffer_handle_t handle) { + std::lock_guard _l(lock); + + auto data_oe = get_validated_data_locked(handle); + if (!data_oe.has_value()) { + return {}; + } + MappedData &data = data_oe.value(); + + if (data.metadata_vaddr == nullptr) { + if (!map_metadata_locked(handle)) { + return {}; + } + } + + return data.metadata_vaddr; + } }; int mali_gralloc_reference_retain(buffer_handle_t handle) { @@ -301,3 +360,7 @@ int mali_gralloc_reference_validate(buffer_handle_t handle) { std::optional mali_gralloc_reference_get_buf_addr(buffer_handle_t handle) { return BufferManager::getInstance().get_buf_addr(handle); } + +std::optional mali_gralloc_reference_get_metadata_addr(buffer_handle_t handle) { + return BufferManager::getInstance().get_metadata_addr(handle); +} diff --git a/gralloc4/src/core/mali_gralloc_reference.h b/gralloc4/src/core/mali_gralloc_reference.h index 257bb6b..bddcead 100644 --- a/gralloc4/src/core/mali_gralloc_reference.h +++ b/gralloc4/src/core/mali_gralloc_reference.h @@ -28,5 +28,6 @@ int mali_gralloc_reference_validate(buffer_handle_t handle); int mali_gralloc_reference_map(buffer_handle_t handle); std::optional mali_gralloc_reference_get_buf_addr(buffer_handle_t handle); +std::optional mali_gralloc_reference_get_metadata_addr(buffer_handle_t handle); #endif /* MALI_GRALLOC_REFERENCE_H_ */ diff --git a/gralloc4/src/hidl_common/Allocator.cpp b/gralloc4/src/hidl_common/Allocator.cpp index abca841..670bf82 100644 --- a/gralloc4/src/hidl_common/Allocator.cpp +++ b/gralloc4/src/hidl_common/Allocator.cpp @@ -26,14 +26,13 @@ #include #include -#include "SharedMetadata.h" #include "Allocator.h" #include "core/mali_gralloc_bufferallocation.h" #include "core/mali_gralloc_bufferdescriptor.h" #include "core/format_info.h" #include "allocator/mali_gralloc_ion.h" -#include "allocator/mali_gralloc_shared_memory.h" #include "gralloc_priv.h" +#include "SharedMetadata.h" namespace arm { @@ -134,19 +133,25 @@ void allocate(const buffer_descriptor_t &bufferDescriptor, uint32_t count, IAllo mali_gralloc_ion_allocate_attr(hnd); /* TODO: error check for failure */ - hnd->attr_base = mmap(nullptr, hnd->attr_size, PROT_READ | PROT_WRITE, + void* metadata_vaddr = mmap(nullptr, hnd->attr_size, PROT_READ | PROT_WRITE, MAP_SHARED, hnd->get_share_attr_fd(), 0); - memset(hnd->attr_base, 0, hnd->attr_size); + memset(metadata_vaddr, 0, hnd->attr_size); - mapper::common::shared_metadata_init(hnd->attr_base, bufferDescriptor.name); + mapper::common::shared_metadata_init(metadata_vaddr, bufferDescriptor.name); const uint32_t base_format = bufferDescriptor.alloc_format & MALI_GRALLOC_INTFMT_FMT_MASK; const uint64_t usage = bufferDescriptor.consumer_usage | bufferDescriptor.producer_usage; android_dataspace_t dataspace; get_format_dataspace(base_format, usage, hnd->width, hnd->height, &dataspace); - mapper::common::set_dataspace(hnd, static_cast(dataspace)); + // TODO: set_dataspace API in mapper expects a buffer to be first imported before it can set the dataspace + { + using mapper::common::shared_metadata; + using mapper::common::aligned_optional; + using mapper::common::Dataspace; + (static_cast(metadata_vaddr))->dataspace = aligned_optional(static_cast(dataspace)); + } { ATRACE_NAME("Update dump details"); @@ -173,15 +178,7 @@ void allocate(const buffer_descriptor_t &bufferDescriptor, uint32_t count, IAllo total_allocated++; } - /* - * We need to set attr_base to MAP_FAILED before the HIDL callback - * to avoid sending an invalid pointer to the client process. - * - * hnd->attr_base = mmap(...); - * hidl_callback(hnd); // client receives hnd->attr_base = - */ - munmap(hnd->attr_base, hnd->attr_size); - hnd->attr_base = 0; + munmap(metadata_vaddr, hnd->attr_size); } int tmpStride = 0; diff --git a/gralloc4/src/hidl_common/Mapper.cpp b/gralloc4/src/hidl_common/Mapper.cpp index aa8e6d2..38d5377 100644 --- a/gralloc4/src/hidl_common/Mapper.cpp +++ b/gralloc4/src/hidl_common/Mapper.cpp @@ -286,15 +286,6 @@ void importBuffer(const hidl_handle& rawHandle, IMapper::importBuffer_cb hidl_cb } auto *private_handle = static_cast(bufferHandle); - private_handle->attr_base = mmap(nullptr, private_handle->attr_size, PROT_READ | PROT_WRITE, - MAP_SHARED, private_handle->get_share_attr_fd(), 0); - if (private_handle->attr_base == MAP_FAILED) - { - native_handle_close(bufferHandle); - native_handle_delete(bufferHandle); - hidl_cb(Error::NO_RESOURCES, nullptr); - return; - } if (gRegisteredHandles->add(bufferHandle) == false) { @@ -324,16 +315,6 @@ Error freeBuffer(void* buffer) return Error::BAD_BUFFER; } - { - auto *private_handle = static_cast(bufferHandle); - int ret = munmap(private_handle->attr_base, private_handle->attr_size); - if (ret < 0) - { - MALI_GRALLOC_LOGW("munmap: %s", strerror(errno)); - } - private_handle->attr_base = MAP_FAILED; - } - const Error status = unregisterBuffer(bufferHandle); if (status != Error::NONE) { @@ -765,7 +746,13 @@ void getReservedRegion(void *buffer, IMapper::getReservedRegion_cb hidl_cb) hidl_cb(Error::BAD_BUFFER, 0, 0); return; } - void *reserved_region = static_cast(handle->attr_base) + + auto metadata_addr_oe = mali_gralloc_reference_get_metadata_addr(handle); + if (!metadata_addr_oe.has_value()) { + hidl_cb(Error::BAD_BUFFER, 0, 0); + } + + void *reserved_region = static_cast(metadata_addr_oe.value()) + mapper::common::shared_metadata_size(); hidl_cb(Error::NONE, reserved_region, handle->reserved_region_size); } diff --git a/gralloc4/src/hidl_common/SharedMetadata.cpp b/gralloc4/src/hidl_common/SharedMetadata.cpp index 27ed972..6960b08 100644 --- a/gralloc4/src/hidl_common/SharedMetadata.cpp +++ b/gralloc4/src/hidl_common/SharedMetadata.cpp @@ -17,6 +17,7 @@ */ #include "SharedMetadata.h" +#include "core/mali_gralloc_reference.h" #include "mali_gralloc_log.h" #include "mali_gralloc_usages.h" //#include @@ -40,19 +41,19 @@ size_t shared_metadata_size() void get_name(const private_handle_t *hnd, std::string *name) { - auto *metadata = reinterpret_cast(hnd->attr_base); + auto *metadata = reinterpret_cast(mali_gralloc_reference_get_metadata_addr(hnd).value()); *name = metadata->get_name(); } void get_crop_rect(const private_handle_t *hnd, std::optional *crop) { - auto *metadata = reinterpret_cast(hnd->attr_base); + auto *metadata = reinterpret_cast(mali_gralloc_reference_get_metadata_addr(hnd).value()); *crop = metadata->crop.to_std_optional(); } android::status_t set_crop_rect(const private_handle_t *hnd, const Rect &crop) { - auto *metadata = reinterpret_cast(hnd->attr_base); + auto *metadata = reinterpret_cast(mali_gralloc_reference_get_metadata_addr(hnd).value()); if (crop.top < 0 || crop.left < 0 || crop.left > crop.right || crop.right > hnd->plane_info[0].alloc_width || @@ -70,31 +71,31 @@ android::status_t set_crop_rect(const private_handle_t *hnd, const Rect &crop) void get_dataspace(const private_handle_t *hnd, std::optional *dataspace) { - auto *metadata = reinterpret_cast(hnd->attr_base); + auto *metadata = reinterpret_cast(mali_gralloc_reference_get_metadata_addr(hnd).value()); *dataspace = metadata->dataspace.to_std_optional(); } void set_dataspace(const private_handle_t *hnd, const Dataspace &dataspace) { - auto *metadata = reinterpret_cast(hnd->attr_base); + auto *metadata = reinterpret_cast(mali_gralloc_reference_get_metadata_addr(hnd).value()); metadata->dataspace = aligned_optional(dataspace); } void get_blend_mode(const private_handle_t *hnd, std::optional *blend_mode) { - auto *metadata = reinterpret_cast(hnd->attr_base); + auto *metadata = reinterpret_cast(mali_gralloc_reference_get_metadata_addr(hnd).value()); *blend_mode = metadata->blend_mode.to_std_optional(); } void set_blend_mode(const private_handle_t *hnd, const BlendMode &blend_mode) { - auto *metadata = reinterpret_cast(hnd->attr_base); + auto *metadata = reinterpret_cast(mali_gralloc_reference_get_metadata_addr(hnd).value()); metadata->blend_mode = aligned_optional(blend_mode); } void get_smpte2086(const private_handle_t *hnd, std::optional *smpte2086) { - auto *metadata = reinterpret_cast(hnd->attr_base); + auto *metadata = reinterpret_cast(mali_gralloc_reference_get_metadata_addr(hnd).value()); *smpte2086 = metadata->smpte2086.to_std_optional(); } @@ -105,7 +106,7 @@ android::status_t set_smpte2086(const private_handle_t *hnd, const std::optional return android::BAD_VALUE; } - auto *metadata = reinterpret_cast(hnd->attr_base); + auto *metadata = reinterpret_cast(mali_gralloc_reference_get_metadata_addr(hnd).value()); metadata->smpte2086 = aligned_optional(smpte2086); return android::OK; @@ -113,7 +114,7 @@ android::status_t set_smpte2086(const private_handle_t *hnd, const std::optional void get_cta861_3(const private_handle_t *hnd, std::optional *cta861_3) { - auto *metadata = reinterpret_cast(hnd->attr_base); + auto *metadata = reinterpret_cast(mali_gralloc_reference_get_metadata_addr(hnd).value()); *cta861_3 = metadata->cta861_3.to_std_optional(); } @@ -124,7 +125,7 @@ android::status_t set_cta861_3(const private_handle_t *hnd, const std::optional< return android::BAD_VALUE; } - auto *metadata = reinterpret_cast(hnd->attr_base); + auto *metadata = reinterpret_cast(mali_gralloc_reference_get_metadata_addr(hnd).value()); metadata->cta861_3 = aligned_optional(cta861_3); return android::OK; @@ -132,7 +133,7 @@ android::status_t set_cta861_3(const private_handle_t *hnd, const std::optional< void get_smpte2094_40(const private_handle_t *hnd, std::optional> *smpte2094_40) { - auto *metadata = reinterpret_cast(hnd->attr_base); + auto *metadata = reinterpret_cast(mali_gralloc_reference_get_metadata_addr(hnd).value()); if (metadata->smpte2094_40.size > 0) { const uint8_t *begin = metadata->smpte2094_40.data(); @@ -147,7 +148,7 @@ void get_smpte2094_40(const private_handle_t *hnd, std::optional> &smpte2094_40) { - auto *metadata = reinterpret_cast(hnd->attr_base); + auto *metadata = reinterpret_cast(mali_gralloc_reference_get_metadata_addr(hnd).value()); const size_t size = smpte2094_40.has_value() ? smpte2094_40->size() : 0; if (size > metadata->smpte2094_40.capacity()) { @@ -162,7 +163,7 @@ android::status_t set_smpte2094_40(const private_handle_t *hnd, const std::optio } void* get_video_hdr(const private_handle_t *hnd) { - auto *metadata = reinterpret_cast(hnd->attr_base); + auto *metadata = reinterpret_cast(mali_gralloc_reference_get_metadata_addr(hnd).value()); return &(metadata->video_private_data); } @@ -170,7 +171,8 @@ void* get_video_roiinfo(const private_handle_t *hnd) { if (!(hnd->get_usage() & GRALLOC_USAGE_ROIINFO)) return nullptr; - return static_cast(hnd->attr_base) + sizeof(shared_metadata) + hnd->reserved_region_size; + auto *metadata = static_cast(mali_gralloc_reference_get_metadata_addr(hnd).value()); + return metadata + sizeof(shared_metadata) + hnd->reserved_region_size; } } // namespace common } // namespace mapper diff --git a/gralloc4/src/hidl_common/SharedMetadata.h b/gralloc4/src/hidl_common/SharedMetadata.h index 9961733..f3aad7c 100644 --- a/gralloc4/src/hidl_common/SharedMetadata.h +++ b/gralloc4/src/hidl_common/SharedMetadata.h @@ -23,7 +23,6 @@ #include #include "gralloctypes/Gralloc4.h" #include "mali_gralloc_buffer.h" -#include "allocator/mali_gralloc_shared_memory.h" #include "core/mali_gralloc_bufferdescriptor.h" #include "gralloc_helper.h" diff --git a/gralloc4/src/libGralloc4Wrapper/wrapper.cpp b/gralloc4/src/libGralloc4Wrapper/wrapper.cpp index 7d286e0..f9b1b9c 100644 --- a/gralloc4/src/libGralloc4Wrapper/wrapper.cpp +++ b/gralloc4/src/libGralloc4Wrapper/wrapper.cpp @@ -5,7 +5,6 @@ #include "core/format_info.h" #include "core/mali_gralloc_bufferdescriptor.h" #include "core/mali_gralloc_bufferallocation.h" -#include "core/mali_gralloc_reference.h" #include "allocator/mali_gralloc_ion.h" #include "hidl_common/SharedMetadata.h" #include "gralloc_priv.h" @@ -158,27 +157,32 @@ buffer_handle_t createNativeHandle(const Descriptor &descriptor) { } { - hnd->attr_base = mmap(nullptr, hnd->attr_size, PROT_READ | PROT_WRITE, + auto metadata_vaddr = mmap(nullptr, hnd->attr_size, PROT_READ | PROT_WRITE, MAP_SHARED, hnd->get_share_attr_fd(), 0); - if (hnd->attr_base == MAP_FAILED) { + if (metadata_vaddr == MAP_FAILED) { ALOGE("mmap hnd->get_share_attr_fd() failed"); mali_gralloc_buffer_free(tmp_buffer); return nullptr; } - memset(hnd->attr_base, 0, hnd->attr_size); + memset(metadata_vaddr, 0, hnd->attr_size); - arm::mapper::common::shared_metadata_init(hnd->attr_base, buffer_descriptor.name); + 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); - arm::mapper::common::set_dataspace(hnd, static_cast(dataspace)); + { + using arm::mapper::common::aligned_optional; + using arm::mapper::common::Dataspace; + using arm::mapper::common::shared_metadata; + (static_cast(metadata_vaddr))->dataspace = + aligned_optional(static_cast(dataspace)); + } - munmap(hnd->attr_base, hnd->attr_size); - hnd->attr_base = 0; + munmap(metadata_vaddr, hnd->attr_size); } // TODO(modan@, handle all plane offsets) diff --git a/gralloc4/src/mali_gralloc_buffer.h b/gralloc4/src/mali_gralloc_buffer.h index 11c41f5..09461d7 100644 --- a/gralloc4/src/mali_gralloc_buffer.h +++ b/gralloc4/src/mali_gralloc_buffer.h @@ -238,7 +238,6 @@ struct private_handle_t int ion_handles[MAX_BUFFER_FDS]; uint64_t alloc_sizes[MAX_BUFFER_FDS]; - void *attr_base __attribute__((aligned (8))) DEFAULT_INITIALIZER(nullptr); off_t offset __attribute__((aligned (8))) DEFAULT_INITIALIZER(0); /* Size of the attribute shared region in bytes. */ -- cgit v1.2.3 From 63af059d6405341b55c568625770887780037b99 Mon Sep 17 00:00:00 2001 From: Ankit Goyal Date: Mon, 28 Aug 2023 17:38:28 -0700 Subject: gralloc4: Fix data types used for sizing buffer The scheme followed is: 1. All the inputs retain their original data types. 2. All the calculated dimensions are in uint64_t. 3. All the sizes are in size_t. 4. All the alignment requirements are in uint32_t. Bug: 297275730 Test: Boot to home Test: VtsHalGraphicsMapperV4_0TargetTest Test: b/297275730#comment15 Test: aion_test Change-Id: Iba6898177768dccd49c7240f11e9301d4d0f99d1 --- gralloc4/src/allocator/mali_gralloc_ion.cpp | 4 +- gralloc4/src/core/format_info.h | 4 +- .../src/core/mali_gralloc_bufferallocation.cpp | 104 ++++++++++----------- gralloc4/src/core/mali_gralloc_bufferallocation.h | 4 +- gralloc4/src/core/mali_gralloc_bufferdescriptor.h | 14 +-- gralloc4/src/core/mali_gralloc_formats.cpp | 6 +- gralloc4/src/hidl_common/Allocator.cpp | 2 +- gralloc4/src/hidl_common/Mapper.cpp | 10 +- gralloc4/src/hidl_common/MapperMetadata.cpp | 12 +-- gralloc4/src/mali_gralloc_buffer.h | 26 +++--- gralloc4/src/mali_gralloc_formats.h | 4 +- 11 files changed, 89 insertions(+), 101 deletions(-) diff --git a/gralloc4/src/allocator/mali_gralloc_ion.cpp b/gralloc4/src/allocator/mali_gralloc_ion.cpp index 85409d7..5c7c0e5 100644 --- a/gralloc4/src/allocator/mali_gralloc_ion.cpp +++ b/gralloc4/src/allocator/mali_gralloc_ion.cpp @@ -491,7 +491,7 @@ std::array mali_gralloc_ion_map(private_handle_t *hnd) if (MAP_FAILED == mappedAddress) { int err = errno; - MALI_GRALLOC_LOGE("mmap( fds[%d]:%d size:%" PRIu64 " ) failed with %s", + MALI_GRALLOC_LOGE("mmap( fds[%d]:%d size:%zu ) failed with %s", fidx, hnd->fds[fidx], hnd->alloc_sizes[fidx], strerror(err)); hnd->dump("map fail"); @@ -523,7 +523,7 @@ void mali_gralloc_ion_unmap(private_handle_t *hnd, std::arrayalloc_sizes[i], strerror(errno)); } else 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 4786560..2a413f6 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 +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(pixel_align_w), afbc_tile.width); + pixel_align_h = std::max(static_cast(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(format.tile_size)})); + *height = GRALLOC_ALIGN(*height, std::max({1u, pixel_align_h, static_cast(format.tile_size)})); } - - -static uint32_t gcd(uint32_t a, uint32_t b) +template +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 +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) + size_t * byte_stride) { // https://developer.android.com/reference/android/graphics/ImageFormat#YV12 if (plane == 0) { @@ -546,7 +536,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]; @@ -581,8 +571,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, + size_t * const pixel_stride, + size_t * const size, plane_info_t plane_info[MAX_PLANES]) { /* pixel_stride is set outside this function after this function is called */ @@ -604,7 +594,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); /* @@ -629,7 +619,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), @@ -650,7 +640,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(GPU_BYTE_ALIGN_DEFAULT)); + hw_align = std::max(hw_align, static_cast(GPU_BYTE_ALIGN_DEFAULT)); } #ifdef SOC_ZUMA @@ -660,13 +650,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(CAMERA_RAW_BUFFER_BYTE_ALIGN)); + hw_align = std::max(hw_align, static_cast(CAMERA_RAW_BUFFER_BYTE_ALIGN)); } #endif if (has_BIG_usage) { assert(has_hw_usage); - hw_align = lcm(hw_align, static_cast(BIG_BYTE_ALIGN_DEFAULT)); + hw_align = lcm(hw_align, static_cast(BIG_BYTE_ALIGN_DEFAULT)); } uint32_t cpu_align = 0; @@ -702,7 +692,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: %zu", plane_info[plane].byte_stride); const uint32_t sb_num = (plane_info[plane].alloc_width * plane_info[plane].alloc_height) / AFBC_PIXELS_PER_BLOCK; @@ -710,11 +700,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, @@ -726,7 +716,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; } @@ -742,13 +732,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. @@ -757,7 +747,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. @@ -772,7 +762,7 @@ static void calc_allocation_size(const int width, * Size must be updated after offset. */ *size += body_size + header_size; - MALI_GRALLOC_LOGV("size=%" PRIu64, *size); + MALI_GRALLOC_LOGV("size=%zu", *size); } } @@ -982,8 +972,8 @@ static int prepare_descriptor_exynos_formats( else { MALI_GRALLOC_LOGE("buffer with format (%s %" PRIx64 - ") has size %" PRIu64 - " != byte_stride %" PRIu32 " * alloc_height %" PRIu32, + ") has size %zu" + " != byte_stride %zu * alloc_height %" PRIu64, format_name(bufDescriptor->alloc_format), bufDescriptor->alloc_format, plane[pidx].size, plane[pidx].byte_stride, plane[pidx].alloc_height); @@ -1028,8 +1018,8 @@ 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)) { diff --git a/gralloc4/src/core/mali_gralloc_bufferallocation.h b/gralloc4/src/core/mali_gralloc_bufferallocation.h index 3d759de..cfea2cf 100644 --- a/gralloc4/src/core/mali_gralloc_bufferallocation.h +++ b/gralloc4/src/core/mali_gralloc_bufferallocation.h @@ -104,9 +104,7 @@ int mali_gralloc_buffer_allocate(const gralloc_buffer_descriptor_t *descriptors, 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..ca75e67 100644 --- a/gralloc4/src/core/mali_gralloc_bufferdescriptor.h +++ b/gralloc4/src/core/mali_gralloc_bufferdescriptor.h @@ -50,8 +50,8 @@ struct buffer_descriptor_t * Calculated values that will be passed to the allocator in order to * allocate the buffer. */ - uint64_t alloc_sizes[MAX_PLANES]; - int pixel_stride; + size_t alloc_sizes[MAX_PLANES]; + size_t pixel_stride; uint64_t alloc_format; uint32_t fd_count; uint32_t plane_count; @@ -85,12 +85,12 @@ struct buffer_descriptor_t "format_type(%u) " "name(%s)" "reserved_size(%" PRIu64 ") " - "alloc_sizes(%" PRIu64 ", %" PRIu64 ", %" PRIu64 ")" - "pixel_stride(%d) alloc_format(0x%" PRIx64 ") fd_count(%d) " + "alloc_sizes(%zu, %zu, %zu)" + "pixel_stride(%zu) 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 %zu byte_stride %zu, wh %" PRIu64 " %" PRIu64 ")" + "plane[1](offset %" PRId64 ", idx %u, size %zu byte_stride %zu, wh %" PRIu64 " %" PRIu64 ")" + "plane[2](offset %" PRId64 ", idx %u, size %zu byte_stride %zu, 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..4bcc9ce 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); } diff --git a/gralloc4/src/hidl_common/Allocator.cpp b/gralloc4/src/hidl_common/Allocator.cpp index 670bf82..0f7340a 100644 --- a/gralloc4/src/hidl_common/Allocator.cpp +++ b/gralloc4/src/hidl_common/Allocator.cpp @@ -225,7 +225,7 @@ const std::string dump() { std::stringstream ss; // TODO: Add logs to indicate overflow - for (int i = 0; i < std::min(total_allocated, allocated_buffers.size()); i++) { + for (int i = 0; i < std::min(total_allocated, static_cast(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++) { diff --git a/gralloc4/src/hidl_common/Mapper.cpp b/gralloc4/src/hidl_common/Mapper.cpp index 38d5377..b655ebf 100644 --- a/gralloc4/src/hidl_common/Mapper.cpp +++ b/gralloc4/src/hidl_common/Mapper.cpp @@ -422,7 +422,7 @@ Error validateBufferSize(void* buffer, { if (gralloc_buffer->alloc_sizes[i] < grallocDescriptor.alloc_sizes[i]) { - MALI_GRALLOC_LOGW("Buf size mismatch. fd_idx(%d) Buffer size = %" PRIu64 ", Descriptor (derived) size = %" PRIu64, + MALI_GRALLOC_LOGW("Buf size mismatch. fd_idx(%d) Buffer size = %zu, Descriptor (derived) size = %zu", i, gralloc_buffer->alloc_sizes[i], grallocDescriptor.alloc_sizes[i]); return Error::BAD_VALUE; } @@ -430,7 +430,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 = %zu", in_stride, gralloc_buffer->stride); return Error::BAD_VALUE; } @@ -455,21 +455,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 0x%zu mismatch with desc byte stride 0x%zu 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 0x%" PRIu64 " mismatch with desc alloc width 0x%" 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 0x%" PRIu64 " mismatch with desc alloc height 0x%" PRIu64 " in plane %d ", gralloc_buffer->plane_info[i].alloc_height, grallocDescriptor.plane_info[i].alloc_height, i); return Error::BAD_VALUE; } diff --git a/gralloc4/src/hidl_common/MapperMetadata.cpp b/gralloc4/src/hidl_common/MapperMetadata.cpp index 81835a3..d748772 100644 --- a/gralloc4/src/hidl_common/MapperMetadata.cpp +++ b/gralloc4/src/hidl_common/MapperMetadata.cpp @@ -317,9 +317,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(handle->plane_info[plane_index].byte_stride), + .widthInSamples = static_cast(handle->plane_info[plane_index].alloc_width), + .heightInSamples = static_cast(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 +371,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(handle->plane_info[plane_index].byte_stride), + .widthInSamples = static_cast(handle->plane_info[plane_index].alloc_width), + .heightInSamples = static_cast(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), diff --git a/gralloc4/src/mali_gralloc_buffer.h b/gralloc4/src/mali_gralloc_buffer.h index 09461d7..6f11f82 100644 --- a/gralloc4/src/mali_gralloc_buffer.h +++ b/gralloc4/src/mali_gralloc_buffer.h @@ -80,7 +80,7 @@ typedef struct plane_info { int64_t offset; uint32_t fd_idx; - uint64_t size; + size_t size; /* * Byte Stride: number of bytes between two vertically adjacent @@ -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; + size_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); + size_t stride DEFAULT_INITIALIZER(0); /* * Allocation properties. @@ -236,7 +236,7 @@ struct private_handle_t // locally mapped shared attribute area int ion_handles[MAX_BUFFER_FDS]; - uint64_t alloc_sizes[MAX_BUFFER_FDS]; + size_t alloc_sizes[MAX_BUFFER_FDS]; off_t offset __attribute__((aligned (8))) DEFAULT_INITIALIZER(0); @@ -260,11 +260,11 @@ struct private_handle_t private_handle_t( int _flags, - uint64_t _alloc_sizes[MAX_BUFFER_FDS], + size_t _alloc_sizes[MAX_BUFFER_FDS], 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, size_t _stride, uint64_t _layer_count, plane_info_t _plane_info[MAX_PLANES]) : private_handle_t() { @@ -396,12 +396,12 @@ 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(%zu) " + "psize(%zu) byte_stride(%zu) internal_wh(%" PRIu64 " %" PRIu64 ") " + "psize1(%zu) byte_stride1(%zu) internal_wh1(%" PRIu64 " %" PRIu64 ") " + "psize2(%zu) byte_stride2(%zu) internal_wh2(%" PRIu64 " %" PRIu64 ") " "alloc_format(0x%" PRIx64 ") " - "alloc_sizes(%" PRIu64 " %" PRIu64 " %" PRIu64 ") " + "alloc_sizes(%zu %zu %zu) " "layer_count(%d) " "\n", str, 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, -- cgit v1.2.3 From a162bf4b2a6d0cde9d5d47cd2c9b826b41b7f45d Mon Sep 17 00:00:00 2001 From: Ankit Goyal Date: Thu, 24 Aug 2023 16:12:30 -0700 Subject: gralloc4: Skip size validation for BLOB formats Bug: 297275730 Test: b/297275730#comment15 Change-Id: I530a053ca4c3ea26dc6a35470375a53c5796e102 --- gralloc4/src/core/mali_gralloc_bufferallocation.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/gralloc4/src/core/mali_gralloc_bufferallocation.cpp b/gralloc4/src/core/mali_gralloc_bufferallocation.cpp index 2a413f6..273dc20 100644 --- a/gralloc4/src/core/mali_gralloc_bufferallocation.cpp +++ b/gralloc4/src/core/mali_gralloc_bufferallocation.cpp @@ -507,7 +507,9 @@ static bool validate_descriptor(buffer_descriptor_t * const bufDescriptor) { return false; } - if (!validate_size(bufDescriptor->layer_count, bufDescriptor->width, bufDescriptor->height)) { + // 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; } -- cgit v1.2.3 From 28ca9f826c627f4f9b9e8cb241c14757bc498cf8 Mon Sep 17 00:00:00 2001 From: Ankit Goyal Date: Wed, 30 Aug 2023 17:45:20 -0700 Subject: gralloc4: Use fixed width data types for native handle Gralloc handle needs to maintain interop between 64 and 32 bit processes and so the handle layout has to be consistent between the two. Bug: 298121718 Test: ANGLE test passes Change-Id: I9b12819360885c8ea7bb513ab00225cf220119ff --- gralloc4/src/allocator/mali_gralloc_ion.cpp | 4 ++-- .../src/core/mali_gralloc_bufferallocation.cpp | 14 ++++++------ gralloc4/src/core/mali_gralloc_bufferdescriptor.h | 14 ++++++------ gralloc4/src/hidl_common/Mapper.cpp | 6 ++--- gralloc4/src/mali_gralloc_buffer.h | 26 +++++++++++++--------- 5 files changed, 34 insertions(+), 30 deletions(-) diff --git a/gralloc4/src/allocator/mali_gralloc_ion.cpp b/gralloc4/src/allocator/mali_gralloc_ion.cpp index 5c7c0e5..85409d7 100644 --- a/gralloc4/src/allocator/mali_gralloc_ion.cpp +++ b/gralloc4/src/allocator/mali_gralloc_ion.cpp @@ -491,7 +491,7 @@ std::array mali_gralloc_ion_map(private_handle_t *hnd) if (MAP_FAILED == mappedAddress) { int err = errno; - MALI_GRALLOC_LOGE("mmap( fds[%d]:%d size:%zu ) failed with %s", + MALI_GRALLOC_LOGE("mmap( fds[%d]:%d size:%" PRIu64 " ) failed with %s", fidx, hnd->fds[fidx], hnd->alloc_sizes[fidx], strerror(err)); hnd->dump("map fail"); @@ -523,7 +523,7 @@ void mali_gralloc_ion_unmap(private_handle_t *hnd, std::arrayalloc_sizes[i], strerror(errno)); } else diff --git a/gralloc4/src/core/mali_gralloc_bufferallocation.cpp b/gralloc4/src/core/mali_gralloc_bufferallocation.cpp index 273dc20..29e3092 100644 --- a/gralloc4/src/core/mali_gralloc_bufferallocation.cpp +++ b/gralloc4/src/core/mali_gralloc_bufferallocation.cpp @@ -439,7 +439,7 @@ T lcm(T a, T b) static void update_yv12_stride(int8_t plane, size_t luma_stride, uint32_t stride_align, - size_t * byte_stride) + uint64_t * byte_stride) { // https://developer.android.com/reference/android/graphics/ImageFormat#YV12 if (plane == 0) { @@ -573,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, - size_t * const pixel_stride, - size_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 */ @@ -694,7 +694,7 @@ static void calc_allocation_size(const int width, } #endif } - MALI_GRALLOC_LOGV("Byte stride: %zu", 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; @@ -764,7 +764,7 @@ static void calc_allocation_size(const int width, * Size must be updated after offset. */ *size += body_size + header_size; - MALI_GRALLOC_LOGV("size=%zu", *size); + MALI_GRALLOC_LOGV("size=%" PRIu64, *size); } } @@ -974,8 +974,8 @@ static int prepare_descriptor_exynos_formats( else { MALI_GRALLOC_LOGE("buffer with format (%s %" PRIx64 - ") has size %zu" - " != byte_stride %zu * alloc_height %" PRIu64, + ") has size %" PRIu64 + " != 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); diff --git a/gralloc4/src/core/mali_gralloc_bufferdescriptor.h b/gralloc4/src/core/mali_gralloc_bufferdescriptor.h index ca75e67..657b2af 100644 --- a/gralloc4/src/core/mali_gralloc_bufferdescriptor.h +++ b/gralloc4/src/core/mali_gralloc_bufferdescriptor.h @@ -50,8 +50,8 @@ struct buffer_descriptor_t * Calculated values that will be passed to the allocator in order to * allocate the buffer. */ - size_t alloc_sizes[MAX_PLANES]; - size_t pixel_stride; + uint64_t alloc_sizes[MAX_PLANES]; + uint64_t pixel_stride; uint64_t alloc_format; uint32_t fd_count; uint32_t plane_count; @@ -85,12 +85,12 @@ struct buffer_descriptor_t "format_type(%u) " "name(%s)" "reserved_size(%" PRIu64 ") " - "alloc_sizes(%zu, %zu, %zu)" - "pixel_stride(%zu) alloc_format(0x%" PRIx64 ") fd_count(%d) " + "alloc_sizes(%" PRIu64 ", %" PRIu64 ", %" PRIu64 ")" + "pixel_stride(%" PRIu64 ") alloc_format(0x%" PRIx64 ") fd_count(%d) " "plane_count(%u) " - "plane[0](offset %" PRId64 ", idx %u, size %zu byte_stride %zu, wh %" PRIu64 " %" PRIu64 ")" - "plane[1](offset %" PRId64 ", idx %u, size %zu byte_stride %zu, wh %" PRIu64 " %" PRIu64 ")" - "plane[2](offset %" PRId64 ", idx %u, size %zu byte_stride %zu, wh %" PRIu64 " %" PRIu64 ")" + "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/hidl_common/Mapper.cpp b/gralloc4/src/hidl_common/Mapper.cpp index b655ebf..37d006b 100644 --- a/gralloc4/src/hidl_common/Mapper.cpp +++ b/gralloc4/src/hidl_common/Mapper.cpp @@ -422,7 +422,7 @@ Error validateBufferSize(void* buffer, { if (gralloc_buffer->alloc_sizes[i] < grallocDescriptor.alloc_sizes[i]) { - MALI_GRALLOC_LOGW("Buf size mismatch. fd_idx(%d) Buffer size = %zu, Descriptor (derived) size = %zu", + MALI_GRALLOC_LOGW("Buf size mismatch. fd_idx(%d) Buffer size = %" PRIu64 ", Descriptor (derived) size = %" PRIu64, i, gralloc_buffer->alloc_sizes[i], grallocDescriptor.alloc_sizes[i]); return Error::BAD_VALUE; } @@ -430,7 +430,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 = %zu", + MALI_GRALLOC_LOGE("Stride mismatch. Expected stride = %d, Buffer stride = %" PRIu64, in_stride, gralloc_buffer->stride); return Error::BAD_VALUE; } @@ -455,7 +455,7 @@ 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%zu mismatch with desc byte stride 0x%zu in plane %d ", + MALI_GRALLOC_LOGE("Buffer byte stride 0x%" PRIx64 " mismatch with desc byte stride 0x%" PRIx64 " in plane %d ", gralloc_buffer->plane_info[i].byte_stride, grallocDescriptor.plane_info[i].byte_stride, i); return Error::BAD_VALUE; } diff --git a/gralloc4/src/mali_gralloc_buffer.h b/gralloc4/src/mali_gralloc_buffer.h index 6f11f82..891c507 100644 --- a/gralloc4/src/mali_gralloc_buffer.h +++ b/gralloc4/src/mali_gralloc_buffer.h @@ -80,7 +80,7 @@ typedef struct plane_info { int64_t offset; uint32_t fd_idx; - size_t size; + uint64_t size; /* * Byte Stride: number of bytes between two vertically adjacent @@ -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. */ - size_t byte_stride; + uint64_t byte_stride; /* * Dimensions of plane (in pixels). @@ -210,7 +210,7 @@ struct private_handle_t * * NOTE: 'stride' values sometimes vary significantly from plane_info[0].alloc_width. */ - size_t stride DEFAULT_INITIALIZER(0); + uint64_t stride DEFAULT_INITIALIZER(0); /* * Allocation properties. @@ -236,7 +236,7 @@ struct private_handle_t // locally mapped shared attribute area int ion_handles[MAX_BUFFER_FDS]; - size_t alloc_sizes[MAX_BUFFER_FDS]; + uint64_t alloc_sizes[MAX_BUFFER_FDS]; off_t offset __attribute__((aligned (8))) DEFAULT_INITIALIZER(0); @@ -260,11 +260,11 @@ struct private_handle_t private_handle_t( int _flags, - size_t _alloc_sizes[MAX_BUFFER_FDS], + uint64_t _alloc_sizes[MAX_BUFFER_FDS], 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, size_t _stride, + int _width, int _height, uint64_t _stride, uint64_t _layer_count, plane_info_t _plane_info[MAX_PLANES]) : private_handle_t() { @@ -396,12 +396,12 @@ struct private_handle_t "wh(%d %d) " "req_format(%#x) alloc_format(%#" PRIx64 ") " "usage_pc(0x%" PRIx64 " 0x%" PRIx64 ") " - "stride(%zu) " - "psize(%zu) byte_stride(%zu) internal_wh(%" PRIu64 " %" PRIu64 ") " - "psize1(%zu) byte_stride1(%zu) internal_wh1(%" PRIu64 " %" PRIu64 ") " - "psize2(%zu) byte_stride2(%zu) internal_wh2(%" PRIu64 " %" PRIu64 ") " + "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(%zu %zu %zu) " + "alloc_sizes(%" PRIu64 " %" PRIu64 " %" PRIu64 ") " "layer_count(%d) " "\n", str, @@ -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_ */ -- cgit v1.2.3 From fbbe8bdff14790c710ff1e76ce4cf6f4794cd73f Mon Sep 17 00:00:00 2001 From: Vadim Caen Date: Tue, 12 Sep 2023 17:39:17 +0200 Subject: Match logging numeric format with the log message Some log messages were prepending 0x for decimal values. This CL changes the logging format for width, height and stride to be shown in decimal without prepending the 0x (hexadecimal) prefix Test: N/A Log message only Bug: N/A Change-Id: Ica9e8ff92cb80f59b4e2aec023dec268cc8ff610 --- gralloc4/src/hidl_common/Mapper.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/gralloc4/src/hidl_common/Mapper.cpp b/gralloc4/src/hidl_common/Mapper.cpp index 37d006b..76aa0b4 100644 --- a/gralloc4/src/hidl_common/Mapper.cpp +++ b/gralloc4/src/hidl_common/Mapper.cpp @@ -455,21 +455,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%" PRIx64 " mismatch with desc byte stride 0x%" PRIx64 " 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%" PRIu64 " mismatch with desc alloc width 0x%" PRIu64 " 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%" PRIu64 " mismatch with desc alloc height 0x%" PRIu64 " 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; } -- cgit v1.2.3 From 03783e0c8f23f8222793979c3624482250b20f2c Mon Sep 17 00:00:00 2001 From: Shashank Sharma Date: Wed, 30 Aug 2023 21:57:03 +0000 Subject: gralloc4: Implement new metadata to get dma buf fds This will allow us to decouple gralloc config interface from gralloc4 private_handle_t. Bug: 176048725 Test: Boots to homescreen Change-Id: Iaed7e94b8f0907787fb3f089850801c2bb427b65 Signed-off-by: Shashank Sharma --- gralloc4/src/hidl_common/MapperMetadata.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/gralloc4/src/hidl_common/MapperMetadata.cpp b/gralloc4/src/hidl_common/MapperMetadata.cpp index d748772..affcd4a 100644 --- a/gralloc4/src/hidl_common/MapperMetadata.cpp +++ b/gralloc4/src/hidl_common/MapperMetadata.cpp @@ -584,6 +584,15 @@ void get_metadata(const private_handle_t *handle, const IMapper::MetadataType &m } break; } + case ::pixel::graphics::MetadataType::PLANE_DMA_BUFS: + { + std::vector 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::util::encodeVector(plane_fds); + break; + } default: err = android::BAD_VALUE; } -- cgit v1.2.3 From 961d2216f4ddc4ca26440fece61bb9002277df24 Mon Sep 17 00:00:00 2001 From: Kalesh Singh Date: Tue, 19 Sep 2023 09:29:00 -0700 Subject: gralloc4: Remove PAGE_SIZE 4096 assumption bionic provides PAGE_SIZE macro which hard codes the page-size to 4096. PAGE_SIZE is being removed as no other libc provides this and Android is moving towards being page-size-agnostic. Make gralloc query the page size form the auxillary vector using getpagesize() instead. Remove unused round_up_to_page_size() function. Bug: 301096752 Test: manual; build page agnostic target Change-Id: Id857b982587fdfeaafc7be5785ddade966b30841 Signed-off-by: Kalesh Singh --- gralloc4/src/core/mali_gralloc_reference.cpp | 6 ++++-- gralloc4/src/gralloc_helper.h | 5 ----- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/gralloc4/src/core/mali_gralloc_reference.cpp b/gralloc4/src/core/mali_gralloc_reference.cpp index 6726895..359a455 100644 --- a/gralloc4/src/core/mali_gralloc_reference.cpp +++ b/gralloc4/src/core/mali_gralloc_reference.cpp @@ -25,6 +25,8 @@ #include #include +#include + #include "allocator/mali_gralloc_ion.h" #include "mali_gralloc_buffer.h" @@ -73,8 +75,8 @@ private: 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(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_ */ -- cgit v1.2.3 From ee05c7d45190085476a61abbea4b3ca341627678 Mon Sep 17 00:00:00 2001 From: Devika Krishnadas Date: Fri, 29 Sep 2023 01:42:29 +0000 Subject: Add post-submit test for gralloc Bug: 274647426 Change-Id: I52b5d92ac581905dc757103c5732b50c348c022d Signed-off-by: Devika Krishnadas --- gralloc4/TEST_MAPPING | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 gralloc4/TEST_MAPPING diff --git a/gralloc4/TEST_MAPPING b/gralloc4/TEST_MAPPING new file mode 100644 index 0000000..5489057 --- /dev/null +++ b/gralloc4/TEST_MAPPING @@ -0,0 +1,7 @@ +{ + "postsubmit": [ + { + "name": "VtsHalGraphicsMapperV4_0TargetTest" + } + ] +} \ No newline at end of file -- cgit v1.2.3 From ea5b3a7a702f465f06d7174e53ef8ba76529cb11 Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Fri, 23 Jun 2023 15:22:29 +0000 Subject: gralloc4: Fix possible overflow in dmabuf_sanity_check() Bug: 252764814 Test: manual verification using poc Change-Id: Iff2e22fe28b854d3058d136e8222862c2f0c05f7 --- gralloc4/src/core/mali_gralloc_reference.cpp | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/gralloc4/src/core/mali_gralloc_reference.cpp b/gralloc4/src/core/mali_gralloc_reference.cpp index 359a455..c685c13 100644 --- a/gralloc4/src/core/mali_gralloc_reference.cpp +++ b/gralloc4/src/core/mali_gralloc_reference.cpp @@ -20,13 +20,12 @@ #include #include +#include #include #include #include -#include - #include "allocator/mali_gralloc_ion.h" #include "mali_gralloc_buffer.h" @@ -64,11 +63,17 @@ private: private_handle_t *hnd = static_cast(const_cast(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; } -- cgit v1.2.3 From b02314c22da225bcc0951307a26fe99a9f431a9c Mon Sep 17 00:00:00 2001 From: Ankit Goyal Date: Thu, 5 Oct 2023 22:06:46 -0700 Subject: gralloc4: Use standard encode/decode Bug: 176048725 Test: Boot to home Test: Custom logs in the graphics driver Change-Id: Idc5ddf3b6379b48153bc3b86df28007522f2e5fb --- gralloc4/src/hidl_common/MapperMetadata.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/gralloc4/src/hidl_common/MapperMetadata.cpp b/gralloc4/src/hidl_common/MapperMetadata.cpp index affcd4a..fdff90a 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 +#include #include @@ -572,7 +573,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,7 +581,7 @@ 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; } @@ -590,7 +591,7 @@ void get_metadata(const private_handle_t *handle, const IMapper::MetadataType &m for (int i = 0; i < get_num_planes(handle); i++) { plane_fds[i] = handle->fds[handle->plane_info[i].fd_idx]; } - vec = ::pixel::graphics::util::encodeVector(plane_fds); + vec = ::pixel::graphics::utils::encode(plane_fds); break; } default: -- cgit v1.2.3 From e4bb448a7b080f306010bbd54f843698128a75f2 Mon Sep 17 00:00:00 2001 From: Ankit Goyal Date: Thu, 5 Oct 2023 22:07:35 -0700 Subject: libvendorgraphicbuffer: Use mapper::get for pixel metadata Bug: 176048725 Test: Boot to home Test: Custom logs in the graphics driver Change-Id: I8760dacef8cacd16f41e8e242587e7bc88ee1830 --- .../gralloc4/vendor_graphicbuffer_meta.cpp | 49 +++++----------------- 1 file changed, 10 insertions(+), 39 deletions(-) diff --git a/libvendorgraphicbuffer/gralloc4/vendor_graphicbuffer_meta.cpp b/libvendorgraphicbuffer/gralloc4/vendor_graphicbuffer_meta.cpp index eb8d663..a39b4db 100644 --- a/libvendorgraphicbuffer/gralloc4/vendor_graphicbuffer_meta.cpp +++ b/libvendorgraphicbuffer/gralloc4/vendor_graphicbuffer_meta.cpp @@ -28,6 +28,7 @@ #include "exynos_format.h" #include +#include using namespace android; using namespace vendor::graphics; @@ -244,30 +245,15 @@ void* VendorGraphicBufferMeta::get_video_metadata(buffer_handle_t hnd) return nullptr; } - MapperMetadataType metadata_type{ - .name = ::pixel::graphics::kPixelMetadataTypeName, - .value = static_cast(::pixel::graphics::MetadataType::VIDEO_HDR), - }; + using namespace ::pixel::graphics; + auto out_oe = mapper::get(handle); - Error error = Error::NONE; - void* output = nullptr; - - get_mapper()->get(handle, metadata_type, - [&](const auto& tmpError, const android::hardware::hidl_vec& 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 +263,15 @@ void* VendorGraphicBufferMeta::get_video_metadata_roiinfo(buffer_handle_t hnd) return nullptr; } - MapperMetadataType metadata_type{ - .name = ::pixel::graphics::kPixelMetadataTypeName, - .value = static_cast(::pixel::graphics::MetadataType::VIDEO_ROI), - }; + using namespace ::pixel::graphics; + auto out_oe = mapper::get(handle); - Error error = Error::NONE; - void* output = nullptr; - - get_mapper()->get(handle, metadata_type, - [&](const auto& tmpError, const android::hardware::hidl_vec& 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) { -- cgit v1.2.3 From 04d757f3a582db8f656513fdee6155ef22837566 Mon Sep 17 00:00:00 2001 From: Devika Krishnadas Date: Thu, 28 Sep 2023 21:55:38 +0000 Subject: Support Dry allocation Bug: 295191668 Test: aion_test Test: CtsCameraTestCases Change-Id: Iaee32ab37f9a65d19342cc606b47a87ae30ed12b Signed-off-by: Devika Krishnadas --- gralloc4/src/Android.bp | 71 ++----- gralloc4/src/aidl/Android.bp | 4 +- gralloc4/src/allocator/mali_gralloc_ion.cpp | 12 +- gralloc4/src/allocator/mali_gralloc_ion.h | 2 +- .../src/core/mali_gralloc_bufferallocation.cpp | 4 +- gralloc4/src/core/mali_gralloc_bufferallocation.h | 2 +- gralloc4/src/hidl_common/Allocator.cpp | 13 +- .../include/gralloc4/gralloc_vendor_interface.h | 50 ----- gralloc4/src/libGralloc4Wrapper/wrapper.cpp | 218 --------------------- gralloc4/src/mali_gralloc_buffer.h | 13 +- gralloc4/src/mali_gralloc_usages.h | 7 +- 11 files changed, 50 insertions(+), 346 deletions(-) delete mode 100644 gralloc4/src/libGralloc4Wrapper/include/gralloc4/gralloc_vendor_interface.h delete mode 100644 gralloc4/src/libGralloc4Wrapper/wrapper.cpp diff --git a/gralloc4/src/Android.bp b/gralloc4/src/Android.bp index 4109439..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", - "android.hardware.graphics.common-ndk_shared", - ], - 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.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..71c6a38 100644 --- a/gralloc4/src/aidl/Android.bp +++ b/gralloc4/src/aidl/Android.bp @@ -9,6 +9,9 @@ cc_library_shared { defaults: [ "arm_gralloc_api_4x_defaults", ], + header_libs: [ + "pixel-gralloc-headers", + ], shared_libs: [ "android.hardware.graphics.allocator-V1-ndk", "android.hardware.graphics.allocator@4.0", @@ -27,4 +30,3 @@ cc_library_shared { "hardware/google/gchips/include", ], } - diff --git a/gralloc4/src/allocator/mali_gralloc_ion.cpp b/gralloc4/src/allocator/mali_gralloc_ion.cpp index 85409d7..f4ab5f0 100644 --- a/gralloc4/src/allocator/mali_gralloc_ion.cpp +++ b/gralloc4/src/allocator/mali_gralloc_ion.cpp @@ -352,7 +352,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 is_dry) { ATRACE_CALL(); GRALLOC_UNUSED(shared_backend); @@ -375,7 +375,7 @@ int mali_gralloc_ion_allocate(const gralloc_buffer_descriptor_t *descriptors, nullptr, bufDescriptor->fd_count, bufDescriptor->hal_format, bufDescriptor->alloc_format, bufDescriptor->width, bufDescriptor->height, bufDescriptor->pixel_stride, - bufDescriptor->layer_count, bufDescriptor->plane_info); + bufDescriptor->layer_count, bufDescriptor->plane_info, is_dry); /* Reset the number of valid filedescriptors, we will increment * it each time a valid fd is added, so we can rely on the @@ -391,16 +391,13 @@ int mali_gralloc_ion_allocate(const gralloc_buffer_descriptor_t *descriptors, pHandle[i] = hnd; usage = bufDescriptor->consumer_usage | bufDescriptor->producer_usage; + if (is_dry) continue; for (uint32_t fidx = 0; fidx < bufDescriptor->fd_count; fidx++) { 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); if (fd < 0) { @@ -412,6 +409,7 @@ int mali_gralloc_ion_allocate(const gralloc_buffer_descriptor_t *descriptors, hnd->incr_numfds(1); } } + if(is_dry) return 0; #if defined(GRALLOC_INIT_AFBC) && (GRALLOC_INIT_AFBC == 1) ATRACE_NAME("AFBC init block"); diff --git a/gralloc4/src/allocator/mali_gralloc_ion.h b/gralloc4/src/allocator/mali_gralloc_ion.h index d826650..ae1d37b 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 is_dry=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/core/mali_gralloc_bufferallocation.cpp b/gralloc4/src/core/mali_gralloc_bufferallocation.cpp index 29e3092..d25354e 100644 --- a/gralloc4/src/core/mali_gralloc_bufferallocation.cpp +++ b/gralloc4/src/core/mali_gralloc_bufferallocation.cpp @@ -1163,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 is_dry) { std::string atrace_log = __FUNCTION__; if (ATRACE_ENABLED()) { @@ -1202,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, is_dry); if (err < 0) { return err; diff --git a/gralloc4/src/core/mali_gralloc_bufferallocation.h b/gralloc4/src/core/mali_gralloc_bufferallocation.h index cfea2cf..a125efc 100644 --- a/gralloc4/src/core/mali_gralloc_bufferallocation.h +++ b/gralloc4/src/core/mali_gralloc_bufferallocation.h @@ -100,7 +100,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 = -1); + bool is_dry = false); int mali_gralloc_buffer_free(buffer_handle_t pHandle); diff --git a/gralloc4/src/hidl_common/Allocator.cpp b/gralloc4/src/hidl_common/Allocator.cpp index 0f7340a..78ef905 100644 --- a/gralloc4/src/hidl_common/Allocator.cpp +++ b/gralloc4/src/hidl_common/Allocator.cpp @@ -80,9 +80,11 @@ void allocate(const buffer_descriptor_t &bufferDescriptor, uint32_t count, IAllo Error error = Error::NONE; int stride = 0; + bool is_dry = bufferDescriptor.producer_usage & GRALLOC_USAGE_ALLOCATE_DRY; + buffer_descriptor_t *bufDesc = const_cast(&bufferDescriptor); + std::vector grallocBuffers; gralloc_buffer_descriptor_t grallocBufferDescriptor[1]; - grallocBufferDescriptor[0] = (gralloc_buffer_descriptor_t)(&bufferDescriptor); grallocBuffers.reserve(count); @@ -101,7 +103,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, is_dry); if (allocResult != 0) { MALI_GRALLOC_LOGE("%s, buffer allocation failed with %d", __func__, allocResult); @@ -179,6 +181,13 @@ void allocate(const buffer_descriptor_t &bufferDescriptor, uint32_t count, IAllo } munmap(metadata_vaddr, hnd->attr_size); + + /* Must set this to false to ensure that when this + * buffer is passed back to mapper, metadata_fd_idx is + * not 0 + */ + hnd->is_dry = false; + } int tmpStride = 0; 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 -#include -#include -#include -#include - - -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 -#include - -#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 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((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(&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( - static_cast(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(metadata_vaddr))->dataspace = - aligned_optional(static_cast(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(handle); - - static android::sp 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 891c507..182518c 100644 --- a/gralloc4/src/mali_gralloc_buffer.h +++ b/gralloc4/src/mali_gralloc_buffer.h @@ -42,6 +42,8 @@ #define SZ_4K 0x00001000 #define SZ_2M 0x00200000 +#define DRY_BUF_SHARE_ATTR_IDX 0 + /* * Maximum number of pixel format planes. * Plane [0]: Single plane formats (inc. RGB, YUV) and Y @@ -257,6 +259,7 @@ struct private_handle_t * to the number of fds. */ static const int sMagic = 0x3141592; + bool is_dry = false; private_handle_t( int _flags, @@ -265,7 +268,7 @@ struct private_handle_t int _fds[MAX_FDS], int _fd_count, int _req_format, uint64_t _alloc_format, int _width, int _height, uint64_t _stride, - uint64_t _layer_count, plane_info_t _plane_info[MAX_PLANES]) + uint64_t _layer_count, plane_info_t _plane_info[MAX_PLANES], bool _is_dry = false) : private_handle_t() { flags = _flags; @@ -279,7 +282,8 @@ struct private_handle_t alloc_format = _alloc_format; layer_count = _layer_count; version = sizeof(native_handle); - set_numfds(fd_count); + is_dry = _is_dry; + set_numfds(is_dry ? 0 : fd_count); memcpy(plane_info, _plane_info, sizeof(plane_info_t) * MAX_PLANES); if (_fds) @@ -350,6 +354,7 @@ struct private_handle_t int get_share_attr_fd_index() const { + if (is_dry) return DRY_BUF_SHARE_ATTR_IDX; /* share_attr can be at idx 1 to MAX_FDS */ if (fd_count <= 0 || fd_count > MAX_FDS) return -1; @@ -359,6 +364,8 @@ struct private_handle_t int get_share_attr_fd() const { + if (is_dry) return fds[DRY_BUF_SHARE_ATTR_IDX]; + int idx = get_share_attr_fd_index(); if (idx <= 0) @@ -444,6 +451,6 @@ struct private_handle_t // 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); +static_assert(sizeof(private_handle_t) == 336); #endif /* MALI_GRALLOC_BUFFER_H_ */ diff --git a/gralloc4/src/mali_gralloc_usages.h b/gralloc4/src/mali_gralloc_usages.h index 4bab4d3..4b2669f 100644 --- a/gralloc4/src/mali_gralloc_usages.h +++ b/gralloc4/src/mali_gralloc_usages.h @@ -28,11 +28,12 @@ */ +#include #include +#include + /* 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 namespace aidl_common = aidl::android::hardware::graphics::common; /* Local macro definitions to emulate Gralloc 1.0 usage interface */ @@ -78,6 +79,7 @@ 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_ALLOCATE_DRY = ::pixel::graphics::Usage::ALLOCATE_DRY, /* FaceAuth specific usages. */ GS101_GRALLOC_USAGE_TPU_INPUT = GRALLOC_USAGE_PRIVATE_5, @@ -155,6 +157,7 @@ static const uint64_t VALID_USAGE = GRALLOC_USAGE_ROIINFO | /* 1U << 52 */ MALI_GRALLOC_USAGE_AFBC_PADDING | /* 1U << 53 */ MALI_GRALLOC_USAGE_FORCE_BACKBUFFER | /* 1U << 54 */ + GRALLOC_USAGE_ALLOCATE_DRY | /* 1U << 28 */ MALI_GRALLOC_USAGE_NO_AFBC | /* 1U << 29 */ 0; -- cgit v1.2.3 From 9cfa8e0ab9e24a61815831f1ed340ff983ac8bf1 Mon Sep 17 00:00:00 2001 From: Devika Krishnadas Date: Thu, 19 Oct 2023 23:45:13 +0000 Subject: Revert "Support Dry allocation" This reverts commit 04d757f3a582db8f656513fdee6155ef22837566. Reason for revert: modify patch logic Change-Id: I328f2e716340c05ede73a78ca5e1b4ce5ac4a223 --- gralloc4/src/Android.bp | 71 +++++-- gralloc4/src/aidl/Android.bp | 4 +- gralloc4/src/allocator/mali_gralloc_ion.cpp | 12 +- gralloc4/src/allocator/mali_gralloc_ion.h | 2 +- .../src/core/mali_gralloc_bufferallocation.cpp | 4 +- gralloc4/src/core/mali_gralloc_bufferallocation.h | 2 +- gralloc4/src/hidl_common/Allocator.cpp | 13 +- .../include/gralloc4/gralloc_vendor_interface.h | 50 +++++ gralloc4/src/libGralloc4Wrapper/wrapper.cpp | 218 +++++++++++++++++++++ gralloc4/src/mali_gralloc_buffer.h | 13 +- gralloc4/src/mali_gralloc_usages.h | 7 +- 11 files changed, 346 insertions(+), 50 deletions(-) create mode 100644 gralloc4/src/libGralloc4Wrapper/include/gralloc4/gralloc_vendor_interface.h create mode 100644 gralloc4/src/libGralloc4Wrapper/wrapper.cpp diff --git a/gralloc4/src/Android.bp b/gralloc4/src/Android.bp index 1f640ea..4109439 100644 --- a/gralloc4/src/Android.bp +++ b/gralloc4/src/Android.bp @@ -1,3 +1,4 @@ + /* * Copyright (C) 2020 Arm Limited. * SPDX-License-Identifier: Apache-2.0 @@ -20,16 +21,62 @@ package { } cc_library_headers { - 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", - ], + 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", + "android.hardware.graphics.common-ndk_shared", + ], + 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.mapper@4.0", + "libdmabufheap", + "libgralloctypes", + "libdrm", + ], + header_libs: [ + "google_hal_headers", + "device_kernel_headers", + ], + export_include_dirs: ["libGralloc4Wrapper/include"] } diff --git a/gralloc4/src/aidl/Android.bp b/gralloc4/src/aidl/Android.bp index 71c6a38..e2d9d04 100644 --- a/gralloc4/src/aidl/Android.bp +++ b/gralloc4/src/aidl/Android.bp @@ -9,9 +9,6 @@ cc_library_shared { defaults: [ "arm_gralloc_api_4x_defaults", ], - header_libs: [ - "pixel-gralloc-headers", - ], shared_libs: [ "android.hardware.graphics.allocator-V1-ndk", "android.hardware.graphics.allocator@4.0", @@ -30,3 +27,4 @@ cc_library_shared { "hardware/google/gchips/include", ], } + diff --git a/gralloc4/src/allocator/mali_gralloc_ion.cpp b/gralloc4/src/allocator/mali_gralloc_ion.cpp index f4ab5f0..85409d7 100644 --- a/gralloc4/src/allocator/mali_gralloc_ion.cpp +++ b/gralloc4/src/allocator/mali_gralloc_ion.cpp @@ -352,7 +352,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, bool is_dry) + bool *shared_backend, int ion_fd) { ATRACE_CALL(); GRALLOC_UNUSED(shared_backend); @@ -375,7 +375,7 @@ int mali_gralloc_ion_allocate(const gralloc_buffer_descriptor_t *descriptors, nullptr, bufDescriptor->fd_count, bufDescriptor->hal_format, bufDescriptor->alloc_format, bufDescriptor->width, bufDescriptor->height, bufDescriptor->pixel_stride, - bufDescriptor->layer_count, bufDescriptor->plane_info, is_dry); + bufDescriptor->layer_count, bufDescriptor->plane_info); /* Reset the number of valid filedescriptors, we will increment * it each time a valid fd is added, so we can rely on the @@ -391,13 +391,16 @@ int mali_gralloc_ion_allocate(const gralloc_buffer_descriptor_t *descriptors, pHandle[i] = hnd; usage = bufDescriptor->consumer_usage | bufDescriptor->producer_usage; - if (is_dry) continue; for (uint32_t fidx = 0; fidx < bufDescriptor->fd_count; fidx++) { int& fd = hnd->fds[fidx]; - fd = alloc_from_dmabuf_heap(usage, bufDescriptor->alloc_sizes[fidx], bufDescriptor->name); + if (ion_fd >= 0 && fidx == 0) { + fd = ion_fd; + } else { + fd = alloc_from_dmabuf_heap(usage, bufDescriptor->alloc_sizes[fidx], bufDescriptor->name); + } if (fd < 0) { @@ -409,7 +412,6 @@ int mali_gralloc_ion_allocate(const gralloc_buffer_descriptor_t *descriptors, hnd->incr_numfds(1); } } - if(is_dry) return 0; #if defined(GRALLOC_INIT_AFBC) && (GRALLOC_INIT_AFBC == 1) ATRACE_NAME("AFBC init block"); diff --git a/gralloc4/src/allocator/mali_gralloc_ion.h b/gralloc4/src/allocator/mali_gralloc_ion.h index ae1d37b..d826650 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, - bool is_dry=false); + int ion_fd = -1); 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/core/mali_gralloc_bufferallocation.cpp b/gralloc4/src/core/mali_gralloc_bufferallocation.cpp index d25354e..29e3092 100644 --- a/gralloc4/src/core/mali_gralloc_bufferallocation.cpp +++ b/gralloc4/src/core/mali_gralloc_bufferallocation.cpp @@ -1163,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, - bool is_dry) + int fd) { std::string atrace_log = __FUNCTION__; if (ATRACE_ENABLED()) { @@ -1202,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, is_dry); + err = mali_gralloc_ion_allocate(descriptors, numDescriptors, pHandle, &shared, fd); if (err < 0) { return err; diff --git a/gralloc4/src/core/mali_gralloc_bufferallocation.h b/gralloc4/src/core/mali_gralloc_bufferallocation.h index a125efc..cfea2cf 100644 --- a/gralloc4/src/core/mali_gralloc_bufferallocation.h +++ b/gralloc4/src/core/mali_gralloc_bufferallocation.h @@ -100,7 +100,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, - bool is_dry = false); + int fd = -1); int mali_gralloc_buffer_free(buffer_handle_t pHandle); diff --git a/gralloc4/src/hidl_common/Allocator.cpp b/gralloc4/src/hidl_common/Allocator.cpp index 78ef905..0f7340a 100644 --- a/gralloc4/src/hidl_common/Allocator.cpp +++ b/gralloc4/src/hidl_common/Allocator.cpp @@ -80,11 +80,9 @@ void allocate(const buffer_descriptor_t &bufferDescriptor, uint32_t count, IAllo Error error = Error::NONE; int stride = 0; - bool is_dry = bufferDescriptor.producer_usage & GRALLOC_USAGE_ALLOCATE_DRY; - buffer_descriptor_t *bufDesc = const_cast(&bufferDescriptor); - std::vector grallocBuffers; gralloc_buffer_descriptor_t grallocBufferDescriptor[1]; + grallocBufferDescriptor[0] = (gralloc_buffer_descriptor_t)(&bufferDescriptor); grallocBuffers.reserve(count); @@ -103,7 +101,7 @@ void allocate(const buffer_descriptor_t &bufferDescriptor, uint32_t count, IAllo else #endif { - allocResult = mali_gralloc_buffer_allocate(grallocBufferDescriptor, 1, &tmpBuffer, nullptr, is_dry); + allocResult = mali_gralloc_buffer_allocate(grallocBufferDescriptor, 1, &tmpBuffer, nullptr); if (allocResult != 0) { MALI_GRALLOC_LOGE("%s, buffer allocation failed with %d", __func__, allocResult); @@ -181,13 +179,6 @@ void allocate(const buffer_descriptor_t &bufferDescriptor, uint32_t count, IAllo } munmap(metadata_vaddr, hnd->attr_size); - - /* Must set this to false to ensure that when this - * buffer is passed back to mapper, metadata_fd_idx is - * not 0 - */ - hnd->is_dry = false; - } int tmpStride = 0; diff --git a/gralloc4/src/libGralloc4Wrapper/include/gralloc4/gralloc_vendor_interface.h b/gralloc4/src/libGralloc4Wrapper/include/gralloc4/gralloc_vendor_interface.h new file mode 100644 index 0000000..cbd98d9 --- /dev/null +++ b/gralloc4/src/libGralloc4Wrapper/include/gralloc4/gralloc_vendor_interface.h @@ -0,0 +1,50 @@ +/* + * 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 +#include +#include +#include +#include + + +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 new file mode 100644 index 0000000..f9b1b9c --- /dev/null +++ b/gralloc4/src/libGralloc4Wrapper/wrapper.cpp @@ -0,0 +1,218 @@ +#include "gralloc4/gralloc_vendor_interface.h" +#include +#include + +#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 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((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(&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( + static_cast(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(metadata_vaddr))->dataspace = + aligned_optional(static_cast(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(handle); + + static android::sp 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 182518c..891c507 100644 --- a/gralloc4/src/mali_gralloc_buffer.h +++ b/gralloc4/src/mali_gralloc_buffer.h @@ -42,8 +42,6 @@ #define SZ_4K 0x00001000 #define SZ_2M 0x00200000 -#define DRY_BUF_SHARE_ATTR_IDX 0 - /* * Maximum number of pixel format planes. * Plane [0]: Single plane formats (inc. RGB, YUV) and Y @@ -259,7 +257,6 @@ struct private_handle_t * to the number of fds. */ static const int sMagic = 0x3141592; - bool is_dry = false; private_handle_t( int _flags, @@ -268,7 +265,7 @@ struct private_handle_t int _fds[MAX_FDS], int _fd_count, int _req_format, uint64_t _alloc_format, int _width, int _height, uint64_t _stride, - uint64_t _layer_count, plane_info_t _plane_info[MAX_PLANES], bool _is_dry = false) + uint64_t _layer_count, plane_info_t _plane_info[MAX_PLANES]) : private_handle_t() { flags = _flags; @@ -282,8 +279,7 @@ struct private_handle_t alloc_format = _alloc_format; layer_count = _layer_count; version = sizeof(native_handle); - is_dry = _is_dry; - set_numfds(is_dry ? 0 : fd_count); + set_numfds(fd_count); memcpy(plane_info, _plane_info, sizeof(plane_info_t) * MAX_PLANES); if (_fds) @@ -354,7 +350,6 @@ struct private_handle_t int get_share_attr_fd_index() const { - if (is_dry) return DRY_BUF_SHARE_ATTR_IDX; /* share_attr can be at idx 1 to MAX_FDS */ if (fd_count <= 0 || fd_count > MAX_FDS) return -1; @@ -364,8 +359,6 @@ struct private_handle_t int get_share_attr_fd() const { - if (is_dry) return fds[DRY_BUF_SHARE_ATTR_IDX]; - int idx = get_share_attr_fd_index(); if (idx <= 0) @@ -451,6 +444,6 @@ struct private_handle_t // 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) == 336); +static_assert(sizeof(private_handle_t) == 328); #endif /* MALI_GRALLOC_BUFFER_H_ */ diff --git a/gralloc4/src/mali_gralloc_usages.h b/gralloc4/src/mali_gralloc_usages.h index 4b2669f..4bab4d3 100644 --- a/gralloc4/src/mali_gralloc_usages.h +++ b/gralloc4/src/mali_gralloc_usages.h @@ -28,12 +28,11 @@ */ -#include #include -#include - /* 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 namespace aidl_common = aidl::android::hardware::graphics::common; /* Local macro definitions to emulate Gralloc 1.0 usage interface */ @@ -79,7 +78,6 @@ 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_ALLOCATE_DRY = ::pixel::graphics::Usage::ALLOCATE_DRY, /* FaceAuth specific usages. */ GS101_GRALLOC_USAGE_TPU_INPUT = GRALLOC_USAGE_PRIVATE_5, @@ -157,7 +155,6 @@ static const uint64_t VALID_USAGE = GRALLOC_USAGE_ROIINFO | /* 1U << 52 */ MALI_GRALLOC_USAGE_AFBC_PADDING | /* 1U << 53 */ MALI_GRALLOC_USAGE_FORCE_BACKBUFFER | /* 1U << 54 */ - GRALLOC_USAGE_ALLOCATE_DRY | /* 1U << 28 */ MALI_GRALLOC_USAGE_NO_AFBC | /* 1U << 29 */ 0; -- cgit v1.2.3 From 0fe76ece0e7ccbd1f65b38101545c66707764c87 Mon Sep 17 00:00:00 2001 From: Devika Krishnadas Date: Thu, 19 Oct 2023 23:18:25 +0000 Subject: Allocate placeholder buffers for metadata-only allocations Bug: 295191668 Test: aion_test Test: CtsCameraTestCases Change-Id: I65e3b79b2875ddbacd70dffa1cc2154b586010b7 Signed-off-by: Devika Krishnadas --- gralloc4/src/Android.bp | 71 ++----- gralloc4/src/allocator/mali_gralloc_ion.cpp | 16 +- gralloc4/src/allocator/mali_gralloc_ion.h | 2 +- .../src/core/mali_gralloc_bufferallocation.cpp | 4 +- gralloc4/src/core/mali_gralloc_bufferallocation.h | 2 +- gralloc4/src/hidl_common/Allocator.cpp | 3 +- .../include/gralloc4/gralloc_vendor_interface.h | 50 ----- gralloc4/src/libGralloc4Wrapper/wrapper.cpp | 218 --------------------- gralloc4/src/mali_gralloc_usages.h | 7 +- 9 files changed, 30 insertions(+), 343 deletions(-) delete mode 100644 gralloc4/src/libGralloc4Wrapper/include/gralloc4/gralloc_vendor_interface.h delete mode 100644 gralloc4/src/libGralloc4Wrapper/wrapper.cpp diff --git a/gralloc4/src/Android.bp b/gralloc4/src/Android.bp index 4109439..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", - "android.hardware.graphics.common-ndk_shared", - ], - 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.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/allocator/mali_gralloc_ion.cpp b/gralloc4/src/allocator/mali_gralloc_ion.cpp index 85409d7..7d04eb0 100644 --- a/gralloc4/src/allocator/mali_gralloc_ion.cpp +++ b/gralloc4/src/allocator/mali_gralloc_ion.cpp @@ -200,12 +200,14 @@ 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; @@ -352,7 +354,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); @@ -396,11 +398,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) { @@ -413,6 +411,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/core/mali_gralloc_bufferallocation.cpp b/gralloc4/src/core/mali_gralloc_bufferallocation.cpp index 29e3092..54e7f2f 100644 --- a/gralloc4/src/core/mali_gralloc_bufferallocation.cpp +++ b/gralloc4/src/core/mali_gralloc_bufferallocation.cpp @@ -1163,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()) { @@ -1202,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 cfea2cf..cc028ff 100644 --- a/gralloc4/src/core/mali_gralloc_bufferallocation.h +++ b/gralloc4/src/core/mali_gralloc_bufferallocation.h @@ -100,7 +100,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 = -1); + bool use_placeholder = false); int mali_gralloc_buffer_free(buffer_handle_t pHandle); diff --git a/gralloc4/src/hidl_common/Allocator.cpp b/gralloc4/src/hidl_common/Allocator.cpp index 0f7340a..d854255 100644 --- a/gralloc4/src/hidl_common/Allocator.cpp +++ b/gralloc4/src/hidl_common/Allocator.cpp @@ -80,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 grallocBuffers; gralloc_buffer_descriptor_t grallocBufferDescriptor[1]; @@ -101,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); 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 -#include -#include -#include -#include - - -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 -#include - -#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 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((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(&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( - static_cast(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(metadata_vaddr))->dataspace = - aligned_optional(static_cast(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(handle); - - static android::sp 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_usages.h b/gralloc4/src/mali_gralloc_usages.h index 4bab4d3..a6dad28 100644 --- a/gralloc4/src/mali_gralloc_usages.h +++ b/gralloc4/src/mali_gralloc_usages.h @@ -27,12 +27,12 @@ * is not present. */ - +#include #include +#include + /* 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 namespace aidl_common = aidl::android::hardware::graphics::common; /* Local macro definitions to emulate Gralloc 1.0 usage interface */ @@ -78,6 +78,7 @@ 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, -- cgit v1.2.3 From ea8b8475cc986b303c4896d56a3c0dcae1aaff83 Mon Sep 17 00:00:00 2001 From: Ankit Goyal Date: Thu, 12 Oct 2023 14:36:10 -0700 Subject: gralloc4: Add support for faeval-secure heap Bug: 302685353 Test: Manually tested with buffer dump Change-Id: I4273cee408caae3a1ab840e4e61ad0005608466d --- gralloc4/src/allocator/mali_gralloc_ion.cpp | 8 +++++++- gralloc4/src/mali_gralloc_usages.h | 3 +++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/gralloc4/src/allocator/mali_gralloc_ion.cpp b/gralloc4/src/allocator/mali_gralloc_ion.cpp index 7d04eb0..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 exact_usage_heaps = + static const std::array 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 diff --git a/gralloc4/src/mali_gralloc_usages.h b/gralloc4/src/mali_gralloc_usages.h index a6dad28..f6559c4 100644 --- a/gralloc4/src/mali_gralloc_usages.h +++ b/gralloc4/src/mali_gralloc_usages.h @@ -84,6 +84,8 @@ typedef enum 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(hidl_common::BufferUsage::CPU_WRITE_RARELY) @@ -151,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 */ -- cgit v1.2.3 From 177effbccc1d77d6c9c222355e1e79edea4ae375 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Wagner?= Date: Wed, 1 Nov 2023 18:10:01 +0000 Subject: Correct the error check on the lockBuffer path The backend can return -EINVAL or a (positive) gralloc error code. Account for it. Bug: 308432951 Test: New PTS EGLAHBLockTest Change-Id: I992af22eae67926c796d2eff072db108f7406be2 --- gralloc4/src/hidl_common/Mapper.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gralloc4/src/hidl_common/Mapper.cpp b/gralloc4/src/hidl_common/Mapper.cpp index 38d5377..e308cfe 100644 --- a/gralloc4/src/hidl_common/Mapper.cpp +++ b/gralloc4/src/hidl_common/Mapper.cpp @@ -215,7 +215,7 @@ 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) + accessRegion.height, &data) != 0) { return Error::BAD_VALUE; } -- cgit v1.2.3 From fe14dc20cf2e9bc07ddb3aea8cfc05fb959f0618 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Wagner?= Date: Wed, 1 Nov 2023 18:05:52 +0000 Subject: Preserve as many error codes as possible in map/unmap paths. While the given implementation only returns -EINVAL or GRALLOC1_ERROR_UNSUPPORTED from the backend functions we don't have to drop the latter. Though -EINVAL can't be preserved as long as the return Error is strongly typed and the type not extended. Bug: 308432951 Test: New PTS EGLAHBLockTest Change-Id: I066270d4d026e31fbf3e080cfa839236a4d7297f --- gralloc4/src/hidl_common/Mapper.cpp | 67 ++++++++++++++++++++++++++++++------- 1 file changed, 54 insertions(+), 13 deletions(-) diff --git a/gralloc4/src/hidl_common/Mapper.cpp b/gralloc4/src/hidl_common/Mapper.cpp index e308cfe..36829ea 100644 --- a/gralloc4/src/hidl_common/Mapper.cpp +++ b/gralloc4/src/hidl_common/Mapper.cpp @@ -18,6 +18,7 @@ #include #include +#include #include "RegisteredHandlePool.h" #include "Mapper.h" #include "BufferDescriptor.h" @@ -142,6 +143,37 @@ static hidl_handle getFenceHandle(int fenceFd, char* handleStorage) return hidl_handle(handle); } +/* + * Converts a gralloc error code to a mapper error code + * + * @param grallocError [in] Gralloc error as integer. + * + * @return Corresponding Mapper error code + * + * @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 Error grallocErrorToMapperError(int grallocError) +{ + 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; + } +} + /* * Locks the given buffer for the specified CPU usage. * @@ -154,6 +186,7 @@ static hidl_handle getFenceHandle(int fenceFd, char* handleStorage) * @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, @@ -214,15 +247,20 @@ 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.width, accessRegion.height, &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; } /* @@ -246,16 +284,19 @@ static Error unlockBuffer(buffer_handle_t bufferHandle, auto private_handle = private_handle_t::dynamicCast(bufferHandle); - const int result = mali_gralloc_unlock(bufferHandle); - if (result) + const int gralloc_err = mali_gralloc_unlock(bufferHandle); + const Error unlock_err = grallocErrorToMapperError(gralloc_err); + + if (Error::NONE == unlock_err) { - MALI_GRALLOC_LOGE("Unlocking failed with error: %d", result); - return Error::BAD_VALUE; + *outFenceFd = -1; + } + else + { + MALI_GRALLOC_LOGE("Unlocking failed with error: %d", gralloc_err); } - *outFenceFd = -1; - - return Error::NONE; + return unlock_err; } void importBuffer(const hidl_handle& rawHandle, IMapper::importBuffer_cb hidl_cb) -- cgit v1.2.3 From 8e6973cbaabd9c038e69082699d330462492463d Mon Sep 17 00:00:00 2001 From: Devika Krishnadas Date: Tue, 19 Sep 2023 23:26:28 +0000 Subject: Revert^2 "gralloc4: Upgrade Allocator to AIDL2" b04e88e3f68b9d6f42bbde910756380386663a68 Bug: 287353739 Test: VtsHalGraphicsMapperV4_0TargetTest Change-Id: I11acdf0240f7429c41697745201438fd17c403f5 --- gralloc4/service/aidl/Android.bp | 4 +- ...oid.hardware.graphics.allocator-aidl-service.rc | 2 +- gralloc4/service/aidl/manifest_gralloc_aidl.xml | 1 + gralloc4/src/aidl/Android.bp | 2 +- gralloc4/src/aidl/GrallocAllocator.cpp | 89 +++++++++++++++++++++- gralloc4/src/aidl/GrallocAllocator.h | 11 +++ gralloc4/src/hidl_common/Allocator.cpp | 5 ++ gralloc4/src/hidl_common/Allocator.h | 2 + 8 files changed, 111 insertions(+), 5 deletions(-) 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 @@ android.hardware.graphics.allocator + 2 IAllocator/default 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 fb1d5b7..8e82036 100644 --- a/gralloc4/src/aidl/GrallocAllocator.cpp +++ b/gralloc4/src/aidl/GrallocAllocator.cpp @@ -7,7 +7,6 @@ #include #include -#include "allocator/mali_gralloc_ion.h" #include "hidl_common/Allocator.h" namespace pixel::allocator { @@ -88,6 +87,94 @@ ndk::ScopedAStatus GrallocAllocator::allocate(const std::vector& 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(descriptor.format); + bufferDescriptor.producer_usage = static_cast(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 _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(_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(AidlAllocator::AllocationError::BAD_DESCRIPTOR)); + + case HidlError::NO_RESOURCES: + return ndk::ScopedAStatus::fromServiceSpecificError( + static_cast(AidlAllocator::AllocationError::NO_RESOURCES)); + + case HidlError::UNSUPPORTED: + return ndk::ScopedAStatus::fromServiceSpecificError( + static_cast(AidlAllocator::AllocationError::UNSUPPORTED)); + + default: + return ndk::ScopedAStatus::fromStatus(STATUS_UNKNOWN_ERROR); + } + + return ndk::ScopedAStatus::ok(); +} + +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; + + if (isBufferDescriptorSupported) { + MALI_GRALLOC_LOGV("Allocation for the given description will not succeed. error %d", + isBufferDescriptorSupported); + return ndk::ScopedAStatus::fromServiceSpecificError( + static_cast(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"; diff --git a/gralloc4/src/aidl/GrallocAllocator.h b/gralloc4/src/aidl/GrallocAllocator.h index dadd4b9..91655a7 100644 --- a/gralloc4/src/aidl/GrallocAllocator.h +++ b/gralloc4/src/aidl/GrallocAllocator.h @@ -2,9 +2,11 @@ #include #include +#include #include #include +#include #include namespace pixel { @@ -21,6 +23,15 @@ public: virtual ndk::ScopedAStatus allocate(const std::vector& 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; }; diff --git a/gralloc4/src/hidl_common/Allocator.cpp b/gralloc4/src/hidl_common/Allocator.cpp index d854255..08f2e8e 100644 --- a/gralloc4/src/hidl_common/Allocator.cpp +++ b/gralloc4/src/hidl_common/Allocator.cpp @@ -245,6 +245,11 @@ const std::string dump() { 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 079457c..e5ce174 100644 --- a/gralloc4/src/hidl_common/Allocator.h +++ b/gralloc4/src/hidl_common/Allocator.h @@ -56,6 +56,8 @@ void allocate(const buffer_descriptor_t &bufferDescriptor, uint32_t count, IAllo const std::string dump(); +int isSupported(buffer_descriptor_t *const bufDescriptor); + } // namespace common } // namespace allocator } // namespace arm -- cgit v1.2.3 From 4248dff0397a145495d5aae58922df21dad8346f Mon Sep 17 00:00:00 2001 From: Devika Krishnadas Date: Thu, 9 Nov 2023 08:02:51 +0000 Subject: Revert "Add post-submit test for gralloc" This reverts commit ee05c7d45190085476a61abbea4b3ca341627678. Reason for revert: Change-Id: I0322203d15f0e7748aef979a4653b8ab884d33a5 --- gralloc4/TEST_MAPPING | 7 ------- 1 file changed, 7 deletions(-) delete mode 100644 gralloc4/TEST_MAPPING diff --git a/gralloc4/TEST_MAPPING b/gralloc4/TEST_MAPPING deleted file mode 100644 index 5489057..0000000 --- a/gralloc4/TEST_MAPPING +++ /dev/null @@ -1,7 +0,0 @@ -{ - "postsubmit": [ - { - "name": "VtsHalGraphicsMapperV4_0TargetTest" - } - ] -} \ No newline at end of file -- cgit v1.2.3 From 173ac5328cc4e98d1b48dc42c1a199adfc0f6149 Mon Sep 17 00:00:00 2001 From: Devika Krishnadas Date: Tue, 29 Aug 2023 02:46:44 +0000 Subject: Gralloc4: Common Mapper code should not depend on hidl Bug: 287532965 Test: VtsHalGraphicsMapperV4_0TargetTest Change-Id: I92154b051437eeee3835e7f5bbd3bdc920444053 Signed-off-by: Devika Krishnadas --- gralloc4/src/4.x/Android.bp | 3 + gralloc4/src/4.x/GrallocMapper.cpp | 154 ++++++-- gralloc4/src/4.x/GrallocMapper.h | 8 +- gralloc4/src/4.x/gralloc_mapper_hidl_header.h | 32 -- gralloc4/src/gralloc_priv.h | 5 - gralloc4/src/hidl_common/BufferDescriptor.h | 59 ++- gralloc4/src/hidl_common/Mapper.cpp | 407 ++++++++------------- gralloc4/src/hidl_common/Mapper.h | 79 ++-- gralloc4/src/hidl_common/MapperMetadata.cpp | 63 ++-- gralloc4/src/hidl_common/MapperMetadata.h | 130 ++++++- gralloc4/src/hidl_common/RegisteredHandlePool.cpp | 11 +- gralloc4/src/hidl_common/RegisteredHandlePool.h | 4 +- gralloc4/src/hidl_common/SharedMetadata.h | 2 - gralloc4/src/hidl_common/hidl_common.h | 42 +++ gralloc4/src/mali_gralloc_error.h | 31 ++ .../gralloc4/vendor_graphicbuffer_meta.cpp | 1 + 16 files changed, 619 insertions(+), 412 deletions(-) delete mode 100644 gralloc4/src/4.x/gralloc_mapper_hidl_header.h create mode 100644 gralloc4/src/hidl_common/hidl_common.h create mode 100644 gralloc4/src/mali_gralloc_error.h 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 GrallocMapper::createDescriptor(const BufferDescriptorInfo &descrip { if (common::validateDescriptorInfo(descriptorInfo)) { - hidl_cb(Error::NONE, common::grallocEncodeBufferDescriptor(descriptorInfo)); + hidl_cb(hidl::Error::NONE, common::grallocEncodeBufferDescriptor(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 GrallocMapper::createDescriptor(const BufferDescriptorInfo &descrip Return 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(rawHandle.getNativeHandle()); + buffer_handle_t outHandle = nullptr; + + hidl::Error err = static_cast( + common::importBuffer(inHandle, &outHandle)); + + hidl_cb(err, static_cast(const_cast(outHandle))); return Void(); } -Return GrallocMapper::freeBuffer(void *buffer) +Return 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(common::freeBuffer(handle)); } -Return GrallocMapper::validateBufferSize(void *buffer, const BufferDescriptorInfo &descriptorInfo, +Return 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(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(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 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( + common::lock(static_cast(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 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(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 GrallocMapper::flushLockedBuffer(void *buffer, flushLockedBuffer_cb hidl_cb) { - common::flushLockedBuffer(buffer, hidl_cb); + hidl::Error err = static_cast(common::flushLockedBuffer(static_cast(buffer))); + hidl_cb(err, hidl_handle{}); return Void(); } -Return GrallocMapper::rereadLockedBuffer(void *buffer) +Return GrallocMapper::rereadLockedBuffer(void *buffer) { - return common::rereadLockedBuffer(buffer); + return static_cast(common::rereadLockedBuffer(common::getBuffer(buffer))); } Return GrallocMapper::get(void *buffer, const MetadataType &metadataType, IMapper::get_cb hidl_cb) { - common::get(buffer, metadataType, hidl_cb); + std::vector vec; + hidl::Error err = static_cast( + common::get(common::getBuffer(buffer), common::MetadataType(metadataType), vec)); + hidl_cb(err, hidl_vec(vec)); return Void(); } -Return GrallocMapper::set(void *buffer, const MetadataType &metadataType, const hidl_vec &metadata) +Return GrallocMapper::set(void *buffer, const MetadataType &metadataType, const hidl_vec &metadata) { - return common::set(buffer, metadataType, metadata); + buffer_handle_t bufferHandle = common::getBuffer(buffer); + return static_cast(common::set(bufferHandle, common::MetadataType(metadataType), metadata)); } Return GrallocMapper::getFromBufferDescriptorInfo(const BufferDescriptorInfo &description, const MetadataType &metadataType, getFromBufferDescriptorInfo_cb hidl_cb) { - common::getFromBufferDescriptorInfo(description, metadataType, hidl_cb); + std::vector vec; + hidl::Error err = static_cast(common::getFromBufferDescriptorInfo( + description, common::MetadataType(metadataType), vec)); + hidl_cb(err, vec); return Void(); } Return 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(common::getTransportSize(bufferHandle, &outNumFds, &outNumInts)); + hidl_cb(err, outNumFds, outNumInts); return Void(); } @@ -131,33 +216,52 @@ Return GrallocMapper::isSupported(const IMapper::BufferDescriptorInfo &des if (!common::validateDescriptorInfo(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 GrallocMapper::listSupportedMetadataTypes(listSupportedMetadataTypes_cb hidl_cb) { - common::listSupportedMetadataTypes(hidl_cb); + std::vector desc = common::listSupportedMetadataTypes(); + std::vector 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 GrallocMapper::dumpBuffer(void *buffer, dumpBuffer_cb hidl_cb) { - common::dumpBuffer(buffer, hidl_cb); + common::BufferDump out; + hidl::Error err = static_cast(common::dumpBuffer(common::getBuffer(buffer), out)); + hidl_cb(err, static_cast(out)); return Void(); } Return GrallocMapper::dumpBuffers(dumpBuffers_cb hidl_cb) { - common::dumpBuffers(hidl_cb); + auto bufferDump = common::dumpBuffers(); + std::vector outBufDump; + for (auto dump : bufferDump) { + outBufDump.push_back(static_cast(dump)); + } + hidl_cb(hidl::Error::NONE, hidl_vec(outBufDump)); return Void(); } Return 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( + common::getReservedRegion(static_cast(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 importBuffer(const hidl_handle &rawHandle, importBuffer_cb hidl_cb) override; - Return freeBuffer(void *buffer) override; + Return freeBuffer(void *buffer) override; - Return validateBufferSize(void *buffer, const IMapper::BufferDescriptorInfo &descriptorInfo, + Return validateBufferSize(void *buffer, const IMapper::BufferDescriptorInfo &descriptorInfo, uint32_t stride) override; Return getTransportSize(void *buffer, getTransportSize_cb _hidl_cb) override; @@ -64,13 +64,13 @@ public: Return flushLockedBuffer(void *buffer, flushLockedBuffer_cb hidl_cb) override; - Return rereadLockedBuffer(void *buffer) override; + Return rereadLockedBuffer(void *buffer) override; Return isSupported(const IMapper::BufferDescriptorInfo &description, isSupported_cb hidl_cb) override; Return get(void *buffer, const MetadataType &metadataType, IMapper::get_cb hidl_cb) override; - Return set(void *buffer, const MetadataType &metadataType, const hidl_vec &metadata) override; + Return set(void *buffer, const MetadataType &metadataType, const hidl_vec &metadata) override; Return 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 -#include "mali_fourcc.h" -#include - -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/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 #include -/* 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/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 #include @@ -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(BufferUsage::GPU_CUBE_MAP) | + static_cast(BufferUsage::GPU_MIPMAP_COMPLETE) | + static_cast(BufferUsage::CPU_READ_MASK) | static_cast(BufferUsage::CPU_WRITE_MASK) | + static_cast(BufferUsage::GPU_TEXTURE) | static_cast(BufferUsage::GPU_RENDER_TARGET) | + static_cast(BufferUsage::COMPOSER_OVERLAY) | static_cast(BufferUsage::COMPOSER_CLIENT_TARGET) | + static_cast(BufferUsage::CAMERA_INPUT) | static_cast(BufferUsage::CAMERA_OUTPUT) | + static_cast(BufferUsage::PROTECTED) | + static_cast(BufferUsage::COMPOSER_CURSOR) | + static_cast(BufferUsage::VIDEO_ENCODER) | + static_cast(BufferUsage::RENDERSCRIPT) | + static_cast(BufferUsage::VIDEO_DECODER) | + static_cast(BufferUsage::SENSOR_DIRECT_DATA) | + static_cast(BufferUsage::GPU_DATA_BUFFER) | + static_cast(BufferUsage::VENDOR_MASK) | + static_cast(BufferUsage::VENDOR_MASK_HI); template static bool validateDescriptorInfo(const BufferDescriptorInfoT &descriptorInfo) @@ -69,7 +66,7 @@ static bool validateDescriptorInfo(const BufferDescriptorInfoT &descriptorInfo) } template -static void push_descriptor_uint32(hidl_vec *vec, size_t *pos, uint32_t val) +static void push_descriptor_uint32(frameworks_vec *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 *vec, size_t *pos, uint32_t va } template -static uint32_t pop_descriptor_uint32(const hidl_vec &vec, size_t *pos) +static uint32_t pop_descriptor_uint32(const frameworks_vec &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 &vec, size_t *pos) } template -static void push_descriptor_uint64(hidl_vec *vec, size_t *pos, uint64_t val) +static void push_descriptor_uint64(frameworks_vec *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 *vec, size_t *pos, uint64_t va } template -static uint64_t pop_descriptor_uint64(const hidl_vec &vec, size_t *pos) +static uint64_t pop_descriptor_uint64(const frameworks_vec &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 &vec, size_t *pos) } // There can only be one string at the end of the descriptor -static void push_descriptor_string(hidl_vec *vec, size_t *pos, const std::string &str) +static void push_descriptor_string(frameworks_vec *vec, size_t *pos, const std::string &str) { strcpy(reinterpret_cast(vec->data() + *pos), str.c_str()); *pos += strlen(str.c_str()) + 1; } -static std::string pop_descriptor_string(const hidl_vec &vec, size_t *pos) +static std::string pop_descriptor_string(const frameworks_vec &vec, size_t *pos) { - std::string str(reinterpret_cast(vec.data() + *pos)); - *pos += str.size() + 1; + const char* charstr = reinterpret_cast(vec.data() + *pos); + charstr += '\0'; + std::string str(charstr); + str.resize(strlen(charstr)); return str; } template -static const hidl_vec grallocEncodeBufferDescriptor(const BufferDescriptorInfoT &descriptorInfo) +static const frameworks_vec grallocEncodeBufferDescriptor(const BufferDescriptorInfoT &descriptorInfo) { - hidl_vec descriptor; + frameworks_vec 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 grallocEncodeBufferDescriptor(const BufferDescriptor } template -static bool grallocDecodeBufferDescriptor(const hidl_vec &androidDescriptor, buffer_descriptor_t &grallocDescriptor) +static bool grallocDecodeBufferDescriptor(const frameworks_vec &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 874f4db..288b5a3 100644 --- a/gralloc4/src/hidl_common/Mapper.cpp +++ b/gralloc4/src/hidl_common/Mapper.cpp @@ -35,6 +35,8 @@ #include "MapperMetadata.h" #include "SharedMetadata.h" +#include + /* 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 @@ -50,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,48 +107,6 @@ static Error unregisterBuffer(buffer_handle_t bufferHandle) return Error::NONE; } -/* - * 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 - * - * @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 fenceFd [in] Fence file descriptor - * @param handleStorage [in] HIDL handle storage for fence - * - * @return HIDL fence handle - */ -static hidl_handle getFenceHandle(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); -} - /* * Converts a gralloc error code to a mapper error code * @@ -179,7 +143,8 @@ static Error grallocErrorToMapperError(int grallocError) * * @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 * @@ -191,7 +156,7 @@ static Error grallocErrorToMapperError(int grallocError) */ 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 @@ -228,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(BufferUsage::CPU_WRITE_MASK))) { if (fenceFd >= 0) { @@ -248,7 +213,7 @@ static Error lockBuffer(buffer_handle_t bufferHandle, void* data = nullptr; const int gralloc_err = mali_gralloc_lock(bufferHandle, cpuUsage, accessRegion.left, accessRegion.top, - accessRegion.width, accessRegion.height, &data); + accessRegion.right, accessRegion.bottom, &data); const Error lock_err = grallocErrorToMapperError(gralloc_err); if(Error::NONE == lock_err) @@ -282,8 +247,6 @@ 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); @@ -293,133 +256,86 @@ static Error unlockBuffer(buffer_handle_t bufferHandle, } else { - MALI_GRALLOC_LOGE("Unlocking failed with error: %d", gralloc_err); + MALI_GRALLOC_LOGE("Unlocking failed with error: %d", + gralloc_err); + return Error::BAD_BUFFER; } return unlock_err; } -void importBuffer(const hidl_handle& rawHandle, IMapper::importBuffer_cb hidl_cb) +Error importBuffer(const native_handle_t *inBuffer, buffer_handle_t *outBuffer) { - if (!rawHandle.getNativeHandle()) - { - MALI_GRALLOC_LOGE("Invalid buffer handle to import"); - hidl_cb(Error::BAD_BUFFER, nullptr); - return; - } - - 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; - } - - const Error error = registerBuffer(bufferHandle); + *outBuffer = const_cast(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(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) @@ -540,28 +456,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; @@ -577,39 +497,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(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); @@ -627,31 +544,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 &vec) { /* The buffer must have been allocated by Gralloc */ - const private_handle_t *handle = static_cast(gRegisteredHandles->get(buffer)); + const private_handle_t *handle = static_cast(buffer); if (handle == nullptr) { MALI_GRALLOC_LOGE("Buffer: %p has not been registered with Gralloc", buffer); - hidl_cb(Error::BAD_BUFFER, hidl_vec()); - 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()); - 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 &metadata) +Error set(buffer_handle_t buffer, const MetadataType &metadataType, const hidl_vec &metadata) { /* The buffer must have been allocated by Gralloc */ - const private_handle_t *handle = static_cast(gRegisteredHandles->get(buffer)); + const private_handle_t *handle = static_cast(buffer); if (handle == nullptr) { MALI_GRALLOC_LOGE("Buffer: %p has not been registered with Gralloc", buffer); @@ -667,135 +582,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(meta)), "", isGettable, isSettable); +} + +std::vector listSupportedMetadataTypes() { /* Returns a vector of {metadata type, description, isGettable, isSettable} * Only non-standardMetadataTypes require a description. */ - hidl_vec 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 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(descriptions.begin(), descriptions.end()); } -static hidl_vec dumpBufferHelper(const private_handle_t* handle) +static BufferDump dumpBufferHelper(const private_handle_t *handle) { - hidl_vec 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 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 metadataDumps; + std::vector metadataDumps; for (const auto& metadataType: standardMetadataTypes) { - get_metadata(handle, metadataType, [&metadataDumps, &metadataType](Error error, hidl_vec metadata) { - switch(error) - { - case Error::NONE: - metadataDumps.push_back({metadataType, metadata}); - break; - case Error::UNSUPPORTED: - default: - return; - } - }); + std::vector 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(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(gRegisteredHandles->get(buffer)); + auto handle = static_cast(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 dumpBuffers() { - std::vector bufferDumps; + std::vector bufferDumps; gRegisteredHandles->for_each([&bufferDumps](buffer_handle_t buffer) { - IMapper::BufferDump bufferDump { dumpBufferHelper(static_cast(buffer)) }; + BufferDump bufferDump { dumpBufferHelper(static_cast(buffer)) }; bufferDumps.push_back(bufferDump); }); - hidl_cb(Error::NONE, hidl_vec(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(gRegisteredHandles->get(buffer)); + auto handle = static_cast(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(metadata_addr_oe.value()) + *outReservedRegion = static_cast(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 &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 &metadata); +Error set(buffer_handle_t buffer, const MetadataType &metadataType, const frameworks_vec &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 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 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 fdff90a..76a9865 100644 --- a/gralloc4/src/hidl_common/MapperMetadata.cpp +++ b/gralloc4/src/hidl_common/MapperMetadata.cpp @@ -53,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())) { @@ -283,7 +287,7 @@ static std::vector> plane_layout_components_fr return std::vector>(0); } -static android::status_t get_plane_layouts(const private_handle_t *handle, std::vector *layouts) +android::status_t get_plane_layouts(const private_handle_t *handle, std::vector *layouts) { const int num_planes = get_num_planes(handle); uint32_t base_format = handle->alloc_format & MALI_GRALLOC_INTFMT_FMT_MASK; @@ -387,23 +391,23 @@ static android::status_t get_plane_layouts(const private_handle_t *handle, std:: return android::OK; } -static hidl_vec encodePointer(void* ptr) { +static frameworks_vec encodePointer(void* ptr) { constexpr uint8_t kPtrSize = sizeof(void*); - hidl_vec output(kPtrSize); + frameworks_vec 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 &outVec) { android::status_t err = android::OK; - hidl_vec vec; + frameworks_vec vec; - if (android::gralloc4::isStandardMetadataType(metadataType)) + if (isStandardMetadataType(metadataType)) { - switch (android::gralloc4::getStandardMetadataTypeValue(metadataType)) + switch (static_cast(metadataType.value)) { case StandardMetadataType::BUFFER_ID: err = android::gralloc4::encodeBufferId(handle->backing_store_id, &vec); @@ -425,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(handle->req_format), &vec); + err = android::gralloc4::encodePixelFormatRequested(static_cast(handle->req_format), &vec); break; case StandardMetadataType::PIXEL_FORMAT_FOURCC: err = android::gralloc4::encodePixelFormatFourCC(drm_fourcc_from_handle(handle), &vec); @@ -450,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(BufferUsage::PROTECTED)) == 0) ? 0 : 1; err = android::gralloc4::encodeProtectedContent(is_protected, &vec); break; } @@ -603,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(vec); + return ((err) ? Error::UNSUPPORTED : Error::NONE); } -Error set_metadata(const private_handle_t *handle, const IMapper::MetadataType &metadataType, - const hidl_vec &metadata) +Error set_metadata(const private_handle_t *handle, const MetadataType &metadataType, const frameworks_vec &metadata) { - if (android::gralloc4::isStandardMetadataType(metadataType)) + if (isStandardMetadataType(metadataType)) { android::status_t err = android::OK; - switch (android::gralloc4::getStandardMetadataTypeValue(metadataType)) + switch (static_cast(metadataType.value)) { case StandardMetadataType::DATASPACE: { @@ -705,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 &outVec) { /* This will hold the metadata that is returned. */ - hidl_vec vec; + frameworks_vec vec; buffer_descriptor_t descriptor; descriptor.width = description.width; @@ -726,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 @@ -737,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(metadataType.value)) { case StandardMetadataType::NAME: err = android::gralloc4::encodeName(description.name, &vec); @@ -756,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(description.format), &vec); + err = android::gralloc4::encodePixelFormatRequested(static_cast(description.format), &vec); break; case StandardMetadataType::USAGE: err = android::gralloc4::encodeUsage(description.usage, &vec); @@ -784,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(BufferUsage::PROTECTED))) ? 1 : 0; err = android::gralloc4::encodeProtectedContent(is_protected, &vec); break; } @@ -886,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 -#include "4.x/gralloc_mapper_hidl_header.h" +#include "hidl_common/hidl_common.h" +#include +#include #include #include - namespace arm { namespace mapper @@ -44,8 +47,116 @@ const static ExtendableType Compression_AFBC{ GRALLOC_ARM_COMPRESSION_TYPE_NAME, static_cast(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(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(meta)) {} +}; + +const static MetadataType ArmMetadataType_PLANE_FDS = MetadataType(GRALLOC_ARM_METADATA_TYPE_NAME, static_cast(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(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 metadata; +#ifdef GRALLOC_MAPPER_4 + MetadataDump(const IMapper::MetadataDump &meta) { + metadataType = MetadataType(meta.metadataType); + metadata = static_cast >(metadata); + } + operator IMapper::MetadataDump() const { + IMapper::MetadataDump dump; + dump.metadataType = static_cast(metadataType); + dump.metadata = hidl_vec(metadata); + return dump; + } +#endif + MetadataDump() {} + MetadataDump(MetadataType metaType, std::vector &meta) { + metadataType = metaType; + metadata = meta; + } +}; + +struct BufferDump { + std::vector 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 metaDump; + for (auto meta : metadataDump) { + metaDump.push_back(static_cast(meta)); + } + bufferdump.metadataDump = metaDump; + return bufferdump; + } +#endif + BufferDump(std::vector &meta) { metadataDump = meta; } + BufferDump() {} +}; + + +int get_num_planes(const private_handle_t *hnd); + +static std::vector> plane_layout_components_from_handle(const private_handle_t *hnd); + +android::status_t get_plane_layouts(const private_handle_t *handle, std::vector *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 &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 &metadata); +Error set_metadata(const private_handle_t *handle, const MetadataType &metadataType, const frameworks_vec &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 &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(buffer); + auto bufferHandle = const_cast(buffer); std::lock_guard 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 fn) { std::lock_guard 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 fn); + bool isRegistered(buffer_handle_t handle); + private: std::mutex mutex; std::unordered_set 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 +#include +#include +#include + +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 +using frameworks_vec = android::hardware::hidl_vec; + +#ifdef GRALLOC_MAPPER_4 + +#include + +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/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/libvendorgraphicbuffer/gralloc4/vendor_graphicbuffer_meta.cpp b/libvendorgraphicbuffer/gralloc4/vendor_graphicbuffer_meta.cpp index a39b4db..a2bf1c4 100644 --- a/libvendorgraphicbuffer/gralloc4/vendor_graphicbuffer_meta.cpp +++ b/libvendorgraphicbuffer/gralloc4/vendor_graphicbuffer_meta.cpp @@ -25,6 +25,7 @@ #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 -- cgit v1.2.3 From e25bf4c087d75cb08f539cfe035fc14c03b28bbf Mon Sep 17 00:00:00 2001 From: Devika Krishnadas Date: Mon, 20 Nov 2023 18:02:36 +0000 Subject: BufferAccess inputs should be width/height and not right/left Bug: 311658952 Change-Id: I0e18f866f9f6db136141210b07321cd4fe466726 Signed-off-by: Devika Krishnadas --- gralloc4/src/hidl_common/Mapper.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/gralloc4/src/hidl_common/Mapper.cpp b/gralloc4/src/hidl_common/Mapper.cpp index 288b5a3..62cac7c 100644 --- a/gralloc4/src/hidl_common/Mapper.cpp +++ b/gralloc4/src/hidl_common/Mapper.cpp @@ -212,8 +212,10 @@ static Error lockBuffer(buffer_handle_t bufferHandle, } void* data = nullptr; - const int gralloc_err = mali_gralloc_lock(bufferHandle, cpuUsage, accessRegion.left, accessRegion.top, - accessRegion.right, accessRegion.bottom, &data); + 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) -- cgit v1.2.3 From 299507996fa4f1a8c9dfa1eaee8b0c23d2802eed Mon Sep 17 00:00:00 2001 From: Devika Krishnadas Date: Mon, 4 Dec 2023 21:23:15 +0000 Subject: Use memfd on placeholder buffers Bug: 314828124 Test: aion_test, video smoke test, camera smoke test Change-Id: I7d9873a3bc19381d4aec345c04de2c7e9a6e2655 Signed-off-by: Devika Krishnadas --- gralloc4/src/allocator/mali_gralloc_ion.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/gralloc4/src/allocator/mali_gralloc_ion.cpp b/gralloc4/src/allocator/mali_gralloc_ion.cpp index 3a955d9..c66c54a 100644 --- a/gralloc4/src/allocator/mali_gralloc_ion.cpp +++ b/gralloc4/src/allocator/mali_gralloc_ion.cpp @@ -30,6 +30,7 @@ #include #include +#include #include #include @@ -222,7 +223,12 @@ int alloc_from_dmabuf_heap(uint64_t usage, size_t size, const std::string& buffe 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); + int shared_fd = -1; + + // memfd requires matching sepolicy allowing r/w access to tmpfs. + if (use_placeholder) shared_fd = memfd_create(heap_name.c_str(), 0); + else shared_fd = get_allocator().Alloc(heap_name, size, 0); + if (shared_fd < 0) { ALOGE("Allocation failed for heap %s error: %d\n", heap_name.c_str(), shared_fd); -- cgit v1.2.3 From 5cea14d897338b8f540067284e7c5fa907c8068f Mon Sep 17 00:00:00 2001 From: Ankit Goyal Date: Thu, 7 Dec 2023 19:09:44 -0800 Subject: gralloc4: Warn about invalid usage (rather than error) Bug: 315351672 Test: Builds Change-Id: I229a771f5bc7028242736ad6f8ed725eef4f91a3 --- gralloc4/src/core/mali_gralloc_formats.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gralloc4/src/core/mali_gralloc_formats.cpp b/gralloc4/src/core/mali_gralloc_formats.cpp index 4bcc9ce..e36e27d 100644 --- a/gralloc4/src/core/mali_gralloc_formats.cpp +++ b/gralloc4/src/core/mali_gralloc_formats.cpp @@ -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 */ -- cgit v1.2.3 From cb2a6bab6d7e4d27a9df7dcf182ae954ffb6296b Mon Sep 17 00:00:00 2001 From: Ankit Goyal Date: Wed, 8 Nov 2023 09:21:55 -0800 Subject: gralloc4: Allow GPU to read RAW12 Bug: 309685681 Test: b/309685681#comment2 Change-Id: Ic341e6f9deb1b21f5dafb5f5124ba699acb61bd9 --- gralloc4/src/core/format_info.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gralloc4/src/core/format_info.cpp b/gralloc4/src/core/format_info.cpp index 0b07879..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 */ -- cgit v1.2.3 From bfc9e372fe754a893b0002c662747b878edb8c03 Mon Sep 17 00:00:00 2001 From: Devika Krishnadas Date: Tue, 12 Dec 2023 02:15:56 +0000 Subject: Fix errors in Allocator::isSupported Bug: 315883761 Test: VtsHalGraphicsAllocatorAidl_TargetTest Change-Id: I5bcbb2edb38d86e0016e66aff8f9ff5713f162d0 Signed-off-by: Devika Krishnadas --- gralloc4/src/aidl/GrallocAllocator.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/gralloc4/src/aidl/GrallocAllocator.cpp b/gralloc4/src/aidl/GrallocAllocator.cpp index 8e82036..2b01b80 100644 --- a/gralloc4/src/aidl/GrallocAllocator.cpp +++ b/gralloc4/src/aidl/GrallocAllocator.cpp @@ -154,12 +154,13 @@ ndk::ScopedAStatus GrallocAllocator::allocate2( 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; + *result = (isBufferDescriptorSupported == 0); if (isBufferDescriptorSupported) { MALI_GRALLOC_LOGV("Allocation for the given description will not succeed. error %d", -- cgit v1.2.3 From 2dc6bcd1aed4223ae190ce9ee0114dee339e000f Mon Sep 17 00:00:00 2001 From: Sean Callanan Date: Wed, 13 Dec 2023 23:47:28 +0000 Subject: Revert "Use memfd on placeholder buffers" Revert submission 25548829-placeholder-use-memfd Reason for revert: b/315351672 Reverted changes: /q/submissionid:25548829-placeholder-use-memfd Change-Id: Ia09a914932b585ebba0b38fbfebdf75d6d9a7a76 --- gralloc4/src/allocator/mali_gralloc_ion.cpp | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/gralloc4/src/allocator/mali_gralloc_ion.cpp b/gralloc4/src/allocator/mali_gralloc_ion.cpp index c66c54a..3a955d9 100644 --- a/gralloc4/src/allocator/mali_gralloc_ion.cpp +++ b/gralloc4/src/allocator/mali_gralloc_ion.cpp @@ -30,7 +30,6 @@ #include #include -#include #include #include @@ -223,12 +222,7 @@ int alloc_from_dmabuf_heap(uint64_t usage, size_t size, const std::string& buffe std::stringstream tag; tag << "heap: " << heap_name << ", bytes: " << size; ATRACE_NAME(tag.str().c_str()); - int shared_fd = -1; - - // memfd requires matching sepolicy allowing r/w access to tmpfs. - if (use_placeholder) shared_fd = memfd_create(heap_name.c_str(), 0); - else shared_fd = get_allocator().Alloc(heap_name, size, 0); - + int shared_fd = get_allocator().Alloc(heap_name, size, 0); if (shared_fd < 0) { ALOGE("Allocation failed for heap %s error: %d\n", heap_name.c_str(), shared_fd); -- cgit v1.2.3