From 4cca6f67ddbc6430fb97a90e8baa17b9d67ee67a Mon Sep 17 00:00:00 2001 From: Jason Macnak Date: Thu, 1 Aug 2024 11:47:32 -0700 Subject: [PATCH] Error when attempting to lock buffer alloc'd without CPU_ usage ... except when running with software rendering as apps do not know that they would need to request additional CPU_* usage for GPU_* usage. Bug: b/356845188 Test: cts -m CtsNativeHardwareTestCases Change-Id: I3536d80469d2187550558e9d02795896de4f9827 --- cros_gralloc/cros_gralloc_driver.cc | 26 +++++++++++++++++++++++++- cros_gralloc/cros_gralloc_driver.h | 3 +++ drv_helpers.h | 8 ++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/cros_gralloc/cros_gralloc_driver.cc b/cros_gralloc/cros_gralloc_driver.cc index ed7d954..cb8b94b 100644 --- a/cros_gralloc/cros_gralloc_driver.cc +++ b/cros_gralloc/cros_gralloc_driver.cc @@ -14,6 +14,8 @@ #include #include +#include "../drv_helpers.h" +#include "../drv_priv.h" #include "../util.h" #include "cros_gralloc_buffer_metadata.h" @@ -156,7 +158,15 @@ static void drv_destroy_and_close(struct driver *drv) close(fd); } -cros_gralloc_driver::cros_gralloc_driver() : drv_(init_try_nodes(), drv_destroy_and_close) +static bool is_running_with_software_rendering() +{ + const char *vulkan_driver = drv_get_os_option("ro.hardware.vulkan"); + return (vulkan_driver != nullptr && strstr(vulkan_driver, "pastel") != nullptr); +} + +cros_gralloc_driver::cros_gralloc_driver() + : drv_(init_try_nodes(), drv_destroy_and_close), + is_running_with_software_rendering_(is_running_with_software_rendering()) { } @@ -179,6 +189,11 @@ bool cros_gralloc_driver::get_resolved_format_and_use_flags( uint64_t resolved_use_flags; struct combination *combo; + uint64_t use_flags = descriptor->use_flags; + if (is_running_with_software_rendering_ && (use_flags & BO_USE_GPU_HW) != 0) { + use_flags |= (BO_USE_SW_READ_OFTEN | BO_USE_SW_WRITE_OFTEN); + } + drv_resolve_format_and_use_flags(drv_.get(), descriptor->drm_format, descriptor->use_flags, &resolved_format, &resolved_use_flags); @@ -469,6 +484,15 @@ int32_t cros_gralloc_driver::lock(buffer_handle_t handle, int32_t acquire_fence, return -EINVAL; } + if (!is_running_with_software_rendering_) { + if ((hnd->usage & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK)) == + 0) { + ALOGE("Attempted to lock() a buffer that was not allocated with a " + "BufferUsage::CPU_* usage."); + return -EINVAL; + } + } + auto buffer = get_buffer(hnd); if (!buffer) { ALOGE("Invalid reference (lock() called on unregistered handle)."); diff --git a/cros_gralloc/cros_gralloc_driver.h b/cros_gralloc/cros_gralloc_driver.h index 56390fc..4e1fdf2 100644 --- a/cros_gralloc/cros_gralloc_driver.h +++ b/cros_gralloc/cros_gralloc_driver.h @@ -81,6 +81,9 @@ class cros_gralloc_driver std::mutex mutex_; std::unordered_map> buffers_; std::unordered_map handles_; + + /* TODO(b/242184599): remove after SwiftShader is moved to the host. */ + const bool is_running_with_software_rendering_ = false; }; #endif diff --git a/drv_helpers.h b/drv_helpers.h index 873bd08..1c347f8 100644 --- a/drv_helpers.h +++ b/drv_helpers.h @@ -7,6 +7,10 @@ #ifndef DRV_HELPERS_H #define DRV_HELPERS_H +#ifdef __cplusplus +extern "C" { +#endif + #include #include "drv.h" @@ -72,4 +76,8 @@ struct lru_entry *lru_find(struct lru *lru, bool (*eq)(struct lru_entry *e, void void lru_insert(struct lru *lru, struct lru_entry *entry); void lru_init(struct lru *lru, int max); +#ifdef __cplusplus +} +#endif + #endif