diff options
Diffstat (limited to 'gralloc4/src')
-rw-r--r-- | gralloc4/src/allocator/mali_gralloc_ion.cpp | 1 | ||||
-rw-r--r-- | gralloc4/src/core/exynos_format_allocation.h | 4 | ||||
-rw-r--r-- | gralloc4/src/core/mali_gralloc_bufferaccess.cpp | 9 | ||||
-rw-r--r-- | gralloc4/src/core/mali_gralloc_reference.cpp | 65 | ||||
-rw-r--r-- | gralloc4/src/core/mali_gralloc_reference.h | 2 | ||||
-rw-r--r-- | gralloc4/src/core/mfc_macros_local.h | 59 | ||||
-rw-r--r-- | gralloc4/src/hidl_common/Mapper.cpp | 25 |
7 files changed, 151 insertions, 14 deletions
diff --git a/gralloc4/src/allocator/mali_gralloc_ion.cpp b/gralloc4/src/allocator/mali_gralloc_ion.cpp index afb6620..3123848 100644 --- a/gralloc4/src/allocator/mali_gralloc_ion.cpp +++ b/gralloc4/src/allocator/mali_gralloc_ion.cpp @@ -726,6 +726,7 @@ 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; } return -err; diff --git a/gralloc4/src/core/exynos_format_allocation.h b/gralloc4/src/core/exynos_format_allocation.h index 099e732..30b2c15 100644 --- a/gralloc4/src/core/exynos_format_allocation.h +++ b/gralloc4/src/core/exynos_format_allocation.h @@ -16,7 +16,11 @@ #pragma once +#if __has_include(<video/mfc_macros.h>) #include <video/mfc_macros.h> +#else +#include "mfc_macros_local.h" +#endif #define PLANE_SIZE(w, h) ((w) * (h)) #define S2B_PLANE_SIZE(w, h) (GRALLOC_ALIGN((w) / 4, 16) * (GRALLOC_ALIGN(h, 16))) diff --git a/gralloc4/src/core/mali_gralloc_bufferaccess.cpp b/gralloc4/src/core/mali_gralloc_bufferaccess.cpp index bc6920e..6740556 100644 --- a/gralloc4/src/core/mali_gralloc_bufferaccess.cpp +++ b/gralloc4/src/core/mali_gralloc_bufferaccess.cpp @@ -24,6 +24,7 @@ #include "mali_gralloc_buffer.h" #include "mali_gralloc_formats.h" #include "mali_gralloc_usages.h" +#include "mali_gralloc_reference.h" #include "allocator/mali_gralloc_ion.h" #include "gralloc_helper.h" #include "format_info.h" @@ -240,13 +241,7 @@ int mali_gralloc_lock(buffer_handle_t buffer, return -EINVAL; } - /* Mapping is done during reference retain instead of lock */ -#if 0 - if (ion_map_for_lock(hnd) < 0) - { - return -EINVAL; - } -#endif + mali_gralloc_reference_map(buffer); *vaddr = (void *)hnd->bases[0]; diff --git a/gralloc4/src/core/mali_gralloc_reference.cpp b/gralloc4/src/core/mali_gralloc_reference.cpp index 22d8aa0..57b8f73 100644 --- a/gralloc4/src/core/mali_gralloc_reference.cpp +++ b/gralloc4/src/core/mali_gralloc_reference.cpp @@ -23,6 +23,8 @@ #include "allocator/mali_gralloc_shared_memory.h" #include "mali_gralloc_bufferallocation.h" #include "mali_gralloc_debug.h" +#include "mali_gralloc_reference.h" +#include "mali_gralloc_usages.h" static pthread_mutex_t s_map_lock = PTHREAD_MUTEX_INITIALIZER; @@ -36,23 +38,52 @@ int mali_gralloc_reference_retain(buffer_handle_t handle) private_handle_t *hnd = (private_handle_t *)handle; pthread_mutex_lock(&s_map_lock); + int retval = 0; if (hnd->allocating_pid == getpid() || hnd->remote_pid == getpid()) { hnd->ref_count++; - pthread_mutex_unlock(&s_map_lock); - return 0; } else { hnd->remote_pid = getpid(); hnd->ref_count = 1; + + // Reset the handle bases, this is used to check if a buffer is mapped + for (int fidx = 0; fidx < hnd->fd_count; fidx++) { + hnd->bases[fidx] = 0; + } } - int retval= mali_gralloc_ion_map(hnd); + pthread_mutex_unlock(&s_map_lock); + + // TODO(b/187145254): CPU_READ/WRITE buffer is not being properly locked from + // MFC. This is a WA for the time being. + constexpr auto cpu_access_usage = ( + GRALLOC_USAGE_SW_WRITE_OFTEN | + GRALLOC_USAGE_SW_READ_OFTEN | + GRALLOC_USAGE_SW_WRITE_RARELY | + GRALLOC_USAGE_SW_READ_RARELY + ); + + if (hnd->get_usage() & cpu_access_usage) + retval = mali_gralloc_reference_map(handle); + + return retval; +} + +int mali_gralloc_reference_map(buffer_handle_t handle) { + private_handle_t *hnd = (private_handle_t *)handle; + + pthread_mutex_lock(&s_map_lock); + + if (hnd->bases[0]) { + MALI_GRALLOC_LOGV("Buffer is already mapped"); + pthread_mutex_unlock(&s_map_lock); + return 0; + } - /* Import ION handle to let ION driver know who's using the buffer */ - import_exynos_ion_handles(hnd); + int retval = mali_gralloc_ion_map(hnd); pthread_mutex_unlock(&s_map_lock); @@ -83,7 +114,6 @@ int mali_gralloc_reference_release(buffer_handle_t handle, bool canFree) if (hnd->ref_count == 0 && canFree) { - free_exynos_ion_handles(hnd); mali_gralloc_dump_buffer_erase(hnd); mali_gralloc_buffer_free(handle); delete handle; @@ -97,7 +127,6 @@ int mali_gralloc_reference_release(buffer_handle_t handle, bool canFree) if (hnd->ref_count == 0) { mali_gralloc_ion_unmap(hnd); - free_exynos_ion_handles(hnd); /* TODO: Make this unmapping of shared meta fd into a function? */ if (hnd->attr_base) @@ -116,3 +145,25 @@ int mali_gralloc_reference_release(buffer_handle_t handle, bool canFree) pthread_mutex_unlock(&s_map_lock); return 0; } + +int mali_gralloc_reference_validate(buffer_handle_t handle) +{ + if (private_handle_t::validate(handle) < 0) + { + MALI_GRALLOC_LOGE("Reference invalid buffer %p, returning error", handle); + return -EINVAL; + } + + const auto *hnd = (private_handle_t *)handle; + pthread_mutex_lock(&s_map_lock); + + if (hnd->allocating_pid == getpid() || hnd->remote_pid == getpid()) { + pthread_mutex_unlock(&s_map_lock); + return 0; + } else { + pthread_mutex_unlock(&s_map_lock); + MALI_GRALLOC_LOGE("Reference unimported buffer %p, returning error", handle); + return -EINVAL; + } +} + diff --git a/gralloc4/src/core/mali_gralloc_reference.h b/gralloc4/src/core/mali_gralloc_reference.h index f2afc61..85bc1c9 100644 --- a/gralloc4/src/core/mali_gralloc_reference.h +++ b/gralloc4/src/core/mali_gralloc_reference.h @@ -23,5 +23,7 @@ int mali_gralloc_reference_retain(buffer_handle_t handle); int mali_gralloc_reference_release(buffer_handle_t handle, bool canFree); +int mali_gralloc_reference_validate(buffer_handle_t handle); +int mali_gralloc_reference_map(buffer_handle_t handle); #endif /* MALI_GRALLOC_REFERENCE_H_ */ diff --git a/gralloc4/src/core/mfc_macros_local.h b/gralloc4/src/core/mfc_macros_local.h new file mode 100644 index 0000000..ab66d17 --- /dev/null +++ b/gralloc4/src/core/mfc_macros_local.h @@ -0,0 +1,59 @@ +/* + * Copyright (C) 2020 Samsung Electronics Co. Ltd. + * Copyright (C) 2021 Google LLC. + * + * 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. + */ + +#pragma once + +#if __has_include(<video/mfc_macros.h>) +#error "This header should not be included if mfc_macros is available in UAPI" +#endif + +/* helper macros */ +#ifndef __ALIGN_UP +#define __ALIGN_UP(x, a) (((x) + ((a) - 1)) & ~((a) - 1)) +#endif + +/* SBWC align macros */ +#define SBWC_8B_STRIDE(w) (128 * (((w) + 31) / 32)) +#define SBWC_10B_STRIDE(w) (160 * (((w) + 31) / 32)) +#define SBWC_HEADER_STRIDE(w) ((((((w) + 63) / 64) + 15) / 16) * 16) + +#define SBWC_Y_VSTRIDE_BLOCKS(h) ((__ALIGN_UP((h), 16) + 3) / 4) +#define SBWC_CBCR_VSTRIDE_BLOCKS(h) (((__ALIGN_UP((h), 16) / 2) + 3) / 4) + +#define SBWC_8B_Y_SIZE(w, h) ((SBWC_8B_STRIDE(w) * ((__ALIGN_UP((h), 16) + 3) / 4)) + 64) +#define SBWC_8B_Y_HEADER_SIZE(w, h) __ALIGN_UP(((SBWC_HEADER_STRIDE(w) * ((__ALIGN_UP((h), 16) + 3) / 4)) + 256), 32) +#define SBWC_8B_CBCR_SIZE(w, h) ((SBWC_8B_STRIDE(w) * (((__ALIGN_UP((h), 16) / 2) + 3) / 4)) + 64) +#define SBWC_8B_CBCR_HEADER_SIZE(w, h) ((SBWC_HEADER_STRIDE(w) * (((__ALIGN_UP((h), 16) / 2) + 3) / 4)) + 128) + +#define SBWC_10B_Y_SIZE(w, h) ((SBWC_10B_STRIDE(w) * ((__ALIGN_UP((h), 16) + 3) / 4)) + 64) +#define SBWC_10B_Y_HEADER_SIZE(w, h) __ALIGN_UP((((__ALIGN_UP((w), 32) * __ALIGN_UP((h), 16) * 2) + 256) - SBWC_10B_Y_SIZE(w, h)), 32) +#define SBWC_10B_CBCR_SIZE(w, h) ((SBWC_10B_STRIDE(w) * (((__ALIGN_UP((h), 16) / 2) + 3) / 4)) + 64) +#define SBWC_10B_CBCR_HEADER_SIZE(w, h) (((__ALIGN_UP((w), 32) * __ALIGN_UP((h), 16)) + 256) - SBWC_10B_CBCR_SIZE(w, h)) + +/* SBWC Lossy align macros */ +#define SBWCL_8B_STRIDE(w, r) (((128 * (r)) / 100) * (((w) + 31) / 32)) +#define SBWCL_10B_STRIDE(w, r) (((160 * (r)) / 100) * (((w) + 31) / 32)) + +#define SBWCL_8B_Y_SIZE(w, h, r) ((SBWCL_8B_STRIDE(w, r) * ((__ALIGN_UP((h), 8) + 3) / 4)) + 64) +#define SBWCL_8B_CBCR_SIZE(w, h, r) ((SBWCL_8B_STRIDE(w, r) * (((__ALIGN_UP((h), 8) / 2) + 3) / 4)) + 64) + +#define SBWCL_10B_Y_SIZE(w, h, r) ((SBWCL_10B_STRIDE(w, r) * ((__ALIGN_UP((h), 8) + 3) / 4)) + 64) +#define SBWCL_10B_CBCR_SIZE(w, h, r) ((SBWCL_10B_STRIDE(w, r) * (((__ALIGN_UP((h), 8) / 2) + 3) / 4)) + 64) + +#define SBWCL_8B_CBCR_BASE(base, w, h, r) ((base) + SBWCL_8B_Y_SIZE(w, h, r)) +#define SBWCL_10B_CBCR_BASE(base, w, h, r) ((base) + SBWCL_10B_Y_SIZE(w, h, r)) + diff --git a/gralloc4/src/hidl_common/Mapper.cpp b/gralloc4/src/hidl_common/Mapper.cpp index b11361d..80ea7e1 100644 --- a/gralloc4/src/hidl_common/Mapper.cpp +++ b/gralloc4/src/hidl_common/Mapper.cpp @@ -184,6 +184,16 @@ static Error lockBuffer(buffer_handle_t bufferHandle, return Error::BAD_BUFFER; } + if (mali_gralloc_reference_validate(bufferHandle) < 0) + { + if (fenceFd >= 0) + { + close(fenceFd); + } + MALI_GRALLOC_LOGE("Buffer: %p is not imported", bufferHandle); + return Error::BAD_VALUE; + } + auto private_handle = private_handle_t::dynamicCast(bufferHandle); if (private_handle->cpu_write != 0 && (cpuUsage & BufferUsage::CPU_WRITE_MASK)) { @@ -613,6 +623,14 @@ void get(void *buffer, const IMapper::MetadataType &metadataType, IMapper::get_c hidl_cb(Error::BAD_BUFFER, hidl_vec<uint8_t>()); return; } + + if (mali_gralloc_reference_validate((buffer_handle_t)handle) < 0) + { + MALI_GRALLOC_LOGE("Buffer: %p is not imported", handle); + hidl_cb(Error::BAD_VALUE, hidl_vec<uint8_t>()); + return; + } + get_metadata(handle, metadataType, hidl_cb); } @@ -625,6 +643,13 @@ Error set(void *buffer, const IMapper::MetadataType &metadataType, const hidl_ve MALI_GRALLOC_LOGE("Buffer: %p has not been registered with Gralloc", buffer); return Error::BAD_BUFFER; } + + if (mali_gralloc_reference_validate((buffer_handle_t)handle) < 0) + { + MALI_GRALLOC_LOGE("Buffer: %p is not imported", handle); + return Error::BAD_VALUE; + } + return set_metadata(handle, metadataType, metadata); } |