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
This commit is contained in:
Jason Macnak 2024-08-01 11:47:32 -07:00
parent fd19358a25
commit 4cca6f67dd
3 changed files with 36 additions and 1 deletions

View file

@ -14,6 +14,8 @@
#include <syscall.h>
#include <xf86drm.h>
#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).");

View file

@ -81,6 +81,9 @@ class cros_gralloc_driver
std::mutex mutex_;
std::unordered_map<uint32_t, std::unique_ptr<cros_gralloc_buffer>> buffers_;
std::unordered_map<cros_gralloc_handle_t, cros_gralloc_imported_handle_info> handles_;
/* TODO(b/242184599): remove after SwiftShader is moved to the host. */
const bool is_running_with_software_rendering_ = false;
};
#endif

View file

@ -7,6 +7,10 @@
#ifndef DRV_HELPERS_H
#define DRV_HELPERS_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stdbool.h>
#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