diff options
author | Devika Krishnadas <kdevika@google.com> | 2023-10-19 23:18:25 +0000 |
---|---|---|
committer | Devika Krishnadas <kdevika@google.com> | 2023-10-21 01:15:45 +0000 |
commit | 0fe76ece0e7ccbd1f65b38101545c66707764c87 (patch) | |
tree | e3e833176300d9091a7f7262538972ec574e4745 | |
parent | 9cfa8e0ab9e24a61815831f1ed340ff983ac8bf1 (diff) | |
download | gchips-0fe76ece0e7ccbd1f65b38101545c66707764c87.tar.gz |
Allocate placeholder buffers for metadata-only allocations
Bug: 295191668
Test: aion_test
Test: CtsCameraTestCases
Change-Id: I65e3b79b2875ddbacd70dffa1cc2154b586010b7
Signed-off-by: Devika Krishnadas <kdevika@google.com>
-rw-r--r-- | gralloc4/src/Android.bp | 71 | ||||
-rw-r--r-- | gralloc4/src/allocator/mali_gralloc_ion.cpp | 16 | ||||
-rw-r--r-- | gralloc4/src/allocator/mali_gralloc_ion.h | 2 | ||||
-rw-r--r-- | gralloc4/src/core/mali_gralloc_bufferallocation.cpp | 4 | ||||
-rw-r--r-- | gralloc4/src/core/mali_gralloc_bufferallocation.h | 2 | ||||
-rw-r--r-- | gralloc4/src/hidl_common/Allocator.cpp | 3 | ||||
-rw-r--r-- | gralloc4/src/libGralloc4Wrapper/include/gralloc4/gralloc_vendor_interface.h | 50 | ||||
-rw-r--r-- | gralloc4/src/libGralloc4Wrapper/wrapper.cpp | 218 | ||||
-rw-r--r-- | gralloc4/src/mali_gralloc_usages.h | 7 |
9 files changed, 30 insertions, 343 deletions
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<hidl_handle> 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 <cutils/native_handle.h> -#include <sys/types.h> -#include <cstdint> -#include <system/graphics-base-v1.0.h> -#include <android/hardware/graphics/mapper/4.0/IMapper.h> - - -namespace android::hardware::graphics::allocator::priv { - -struct Descriptor; -Descriptor *createDescriptor(); -void deleteDescriptor(Descriptor *descriptor); - -void setProducerUsage(Descriptor &descriptor, uint64_t usage); -void setConsumerUsage(Descriptor &descriptor, uint64_t usage); -void setPlaneCount(Descriptor &descriptor, int count); -void setPlane(Descriptor &descriptor, int index, int fd, size_t size, off_t offset, int stride_byte); -void setWidth(Descriptor &descriptor, int width); -void setHeight(Descriptor &descriptor, int height); -void setStridePixel(Descriptor &descriptor, int stride_pixel); -void setFormat(Descriptor &descriptor, int format); - -buffer_handle_t createNativeHandle(const Descriptor &descriptor); - -int freeImportedHandle(void *handle); - -} // namespace android::hardware::graphics::allocator::priv - -#endif diff --git a/gralloc4/src/libGralloc4Wrapper/wrapper.cpp b/gralloc4/src/libGralloc4Wrapper/wrapper.cpp deleted file mode 100644 index f9b1b9c..0000000 --- a/gralloc4/src/libGralloc4Wrapper/wrapper.cpp +++ /dev/null @@ -1,218 +0,0 @@ -#include "gralloc4/gralloc_vendor_interface.h" -#include <vector> -#include <sys/stat.h> - -#include "core/format_info.h" -#include "core/mali_gralloc_bufferdescriptor.h" -#include "core/mali_gralloc_bufferallocation.h" -#include "allocator/mali_gralloc_ion.h" -#include "hidl_common/SharedMetadata.h" -#include "gralloc_priv.h" - -namespace android::hardware::graphics::allocator::priv { - -struct Descriptor { - unsigned int size = 0; - uint64_t producer_usage = 0; - uint64_t consumer_usage = 0; - - struct PlaneDescriptor { - int fd = -1; - size_t size = 0; - off_t offset = 0; - int stride_byte = 0; - }; - std::vector<PlaneDescriptor> planes; - - int width = 0; - int height = 0; - int stride_pixel = 0; - int format = 0; -}; - -Descriptor *createDescriptor() { return new Descriptor(); } -void deleteDescriptor(Descriptor *descriptor) { delete descriptor; } - -void setProducerUsage(Descriptor &descriptor, uint64_t usage) { - descriptor.producer_usage = usage; -} - -void setConsumerUsage(Descriptor &descriptor, uint64_t usage) { - descriptor.consumer_usage = usage; -} - -void setPlaneCount(Descriptor &descriptor, int count) { - descriptor.planes.resize(count); -} - -void setPlane(Descriptor &descriptor, int index, int fd, size_t size, off_t offset, int stride_byte) { - descriptor.planes[index].fd = fd; - descriptor.planes[index].size = size; - descriptor.planes[index].offset = offset; - descriptor.planes[index].stride_byte = stride_byte; -} - -void setWidth(Descriptor &descriptor, int width) { - descriptor.width = width; -} - -void setHeight(Descriptor &descriptor, int height) { - descriptor.height = height; -} - -void setStridePixel(Descriptor &descriptor, int stride_pixel) { - descriptor.stride_pixel = stride_pixel; -} - -void setFormat(Descriptor &descriptor, int format) { - descriptor.format = format; -} - -buffer_handle_t createNativeHandle(const Descriptor &descriptor) { - for (int i = 0; i < descriptor.planes.size(); ++i) { - struct stat st; - fstat(descriptor.planes[i].fd, &st); - off64_t fd_size = st.st_size; - if (fd_size < descriptor.planes[i].size) { - ALOGE("libGralloc4Wrapper: createNativeHandle failed: plane[%d] requested size greater than fd size.", - i); - return nullptr; - } - } - - buffer_descriptor_t buffer_descriptor; - - buffer_descriptor.pixel_stride = descriptor.stride_pixel; - buffer_descriptor.width = descriptor.width; - buffer_descriptor.height = descriptor.height; - buffer_descriptor.layer_count = 1; - buffer_descriptor.hal_format = buffer_descriptor.alloc_format - = descriptor.format; - buffer_descriptor.producer_usage = descriptor.producer_usage; - buffer_descriptor.consumer_usage = descriptor.consumer_usage; - buffer_descriptor.format_type = MALI_GRALLOC_FORMAT_TYPE_USAGE; - buffer_descriptor.signature = sizeof(buffer_descriptor_t); - - buffer_descriptor.fd_count = buffer_descriptor.plane_count - = descriptor.planes.size(); - for (int i = 0; i < descriptor.planes.size(); ++i) { - buffer_descriptor.alloc_sizes[i] = descriptor.planes[i].size; - } - - auto format_index = get_format_index(descriptor.format); - if (format_index == -1) { - ALOGE("libGralloc4Wrapper: invalid format 0x%x", - descriptor.format); - return 0; - } - for (int i = 0; i < descriptor.planes.size(); ++i) { - uint8_t bpp = formats[format_index].bpp[i]; - if (bpp == 0) { - ALOGE("libGralloc4Wrapper: format 0x%x has bpp[%d]=0", - descriptor.format, i); - return nullptr; - } - buffer_descriptor.plane_info[i] = { - .byte_stride = static_cast<uint32_t>((descriptor.planes[i].stride_byte * bpp) / 8), - .alloc_width = buffer_descriptor.width, - .alloc_height = buffer_descriptor.height, - }; - } - - if (mali_gralloc_derive_format_and_size(&buffer_descriptor)) { - ALOGE("libGralloc4Wrapper: mali_gralloc_derive_format_and_size failed"); - return nullptr; - } - - const gralloc_buffer_descriptor_t gralloc_buffer_descriptor = - reinterpret_cast<const gralloc_buffer_descriptor_t>(&buffer_descriptor); - - buffer_handle_t tmp_buffer; - bool shared_backend; - // TODO(modan@, make mali_gralloc_ion_allocate accept multiple fds) - { - int result = mali_gralloc_buffer_allocate(&gralloc_buffer_descriptor, 1, &tmp_buffer, - &shared_backend, descriptor.planes[0].fd); - if (result < 0) { - ALOGE("mali_gralloc_buffer_allocate failed"); - return nullptr; - } - } - - private_handle_t *hnd = const_cast<private_handle_t *>( - static_cast<const private_handle_t *>(tmp_buffer)); - - hnd->imapper_version = HIDL_MAPPER_VERSION_SCALED; - - hnd->reserved_region_size = buffer_descriptor.reserved_size; - hnd->attr_size = arm::mapper::common::shared_metadata_size() + hnd->reserved_region_size; - - { - int result = mali_gralloc_ion_allocate_attr(hnd); - if (result < 0) { - ALOGE("mali_gralloc_ion_allocate_attr failed"); - mali_gralloc_buffer_free(tmp_buffer); - return nullptr; - } - } - - { - auto metadata_vaddr = mmap(nullptr, hnd->attr_size, PROT_READ | PROT_WRITE, - MAP_SHARED, hnd->get_share_attr_fd(), 0); - if (metadata_vaddr == MAP_FAILED) { - ALOGE("mmap hnd->get_share_attr_fd() failed"); - mali_gralloc_buffer_free(tmp_buffer); - return nullptr; - } - - memset(metadata_vaddr, 0, hnd->attr_size); - - arm::mapper::common::shared_metadata_init(metadata_vaddr, buffer_descriptor.name); - - const uint32_t base_format = buffer_descriptor.alloc_format & MALI_GRALLOC_INTFMT_FMT_MASK; - const uint64_t usage = buffer_descriptor.consumer_usage | buffer_descriptor.producer_usage; - android_dataspace_t dataspace; - get_format_dataspace(base_format, usage, hnd->width, hnd->height, &dataspace); - - { - using arm::mapper::common::aligned_optional; - using arm::mapper::common::Dataspace; - using arm::mapper::common::shared_metadata; - (static_cast<shared_metadata *>(metadata_vaddr))->dataspace = - aligned_optional(static_cast<Dataspace>(dataspace)); - } - - munmap(metadata_vaddr, hnd->attr_size); - } - - // TODO(modan@, handle all plane offsets) - hnd->offset = hnd->plane_info[0].offset = descriptor.planes[0].offset; - hnd->layer_count = 1; - - return tmp_buffer; -} - -int freeImportedHandle(void *handle) -{ - using android::hardware::graphics::mapper::V4_0::IMapper; - using android::hardware::graphics::mapper::V4_0::Error; - - const private_handle_t *hnd = static_cast<private_handle_t *>(handle); - - static android::sp<IMapper> mapper = IMapper::getService(); - if (!mapper) - { - ALOGE("libGralloc4Wrapper: %s failed to get a mapper", __func__); - return -1; - } - - if (mapper->freeBuffer(handle) != Error::NONE) - { - ALOGE("libGralloc4Wrapper: %s couldn't freeBuffer(%p\n", __func__, handle); - return -1; - } - - return 0; -} - -} // namespace android::hardware::graphics::allocator::priv diff --git a/gralloc4/src/mali_gralloc_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 <aidl/android/hardware/graphics/common/BufferUsage.h> #include <android/hardware/graphics/common/1.2/types.h> +#include <pixel-gralloc/usage.h> + /* BufferUsage is not defined in 1.2/types.h as there are no changes from previous version */ namespace hidl_common = android::hardware::graphics::common::V1_1; - -#include <aidl/android/hardware/graphics/common/BufferUsage.h> namespace aidl_common = aidl::android::hardware::graphics::common; /* Local macro definitions to emulate Gralloc 1.0 usage interface */ @@ -78,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, |