summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYiwei Zhang <zzyiwei@chromium.org>2021-07-01 20:43:04 +0000
committerCommit Bot <commit-bot@chromium.org>2021-07-02 18:26:18 +0000
commit61f9752a1cdd833c4254cdae69846cda1e172794 (patch)
treed45d6b5b0e4aab7b7cff30e2640b939b6d97c41d
parentbb9d4af4529b3019d830b8ef7e39479436b642af (diff)
downloadminigbm-61f9752a1cdd833c4254cdae69846cda1e172794.tar.gz
minigbm: refactor cros_gralloc_driver to be a singleton
Only a single cros_gralloc_driver instance is allowed to exist in a process. However, currently when Gralloc3.0 or Gralloc4.0 is used, it's possible for 2 independent driver instances to be created. This change refactors cros_gralloc_driver to be a singleton and ensures the driver initialization only happens once per process. BUG=b:191895066 TEST=No regression of gralloc0 in ARCVM Change-Id: I3cb8b7c3d13717cfe531b557b9d69b72efdbf3da Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/minigbm/+/3002266 Tested-by: Yiwei Zhang <zzyiwei@chromium.org> Tested-by: Jason Macnak <natsu@google.com> Tested-by: John Stultz <john.stultz@linaro.org> Reviewed-by: Gurchetan Singh <gurchetansingh@chromium.org> Reviewed-by: Jason Macnak <natsu@google.com> Commit-Queue: Yiwei Zhang <zzyiwei@chromium.org>
-rw-r--r--cros_gralloc/cros_gralloc_driver.cc43
-rw-r--r--cros_gralloc/cros_gralloc_driver.h12
-rw-r--r--cros_gralloc/gralloc0/gralloc0.cc8
-rw-r--r--cros_gralloc/gralloc3/CrosGralloc3Allocator.cc7
-rw-r--r--cros_gralloc/gralloc3/CrosGralloc3Allocator.h4
-rw-r--r--cros_gralloc/gralloc3/CrosGralloc3Mapper.cc7
-rw-r--r--cros_gralloc/gralloc3/CrosGralloc3Mapper.h4
-rw-r--r--cros_gralloc/gralloc4/CrosGralloc4Allocator.cc7
-rw-r--r--cros_gralloc/gralloc4/CrosGralloc4Allocator.h4
-rw-r--r--cros_gralloc/gralloc4/CrosGralloc4Mapper.cc56
-rw-r--r--cros_gralloc/gralloc4/CrosGralloc4Mapper.h5
11 files changed, 43 insertions, 114 deletions
diff --git a/cros_gralloc/cros_gralloc_driver.cc b/cros_gralloc/cros_gralloc_driver.cc
index f0c0392..aa53537 100644
--- a/cros_gralloc/cros_gralloc_driver.cc
+++ b/cros_gralloc/cros_gralloc_driver.cc
@@ -45,21 +45,16 @@ int memfd_create_wrapper(const char *name, unsigned int flags)
return fd;
}
-cros_gralloc_driver::cros_gralloc_driver() : drv_(nullptr)
+cros_gralloc_driver *cros_gralloc_driver::get_instance()
{
-}
+ static cros_gralloc_driver s_instance;
-cros_gralloc_driver::~cros_gralloc_driver()
-{
- buffers_.clear();
- handles_.clear();
-
- if (drv_) {
- int fd = drv_get_fd(drv_);
- drv_destroy(drv_);
- drv_ = nullptr;
- close(fd);
+ if (!s_instance.is_initialized()) {
+ drv_log("Failed to initialize driver.\n");
+ return nullptr;
}
+
+ return &s_instance;
}
static struct driver *init_try_node(int idx, char const *str)
@@ -84,7 +79,7 @@ static struct driver *init_try_node(int idx, char const *str)
return drv;
}
-int32_t cros_gralloc_driver::init()
+cros_gralloc_driver::cros_gralloc_driver()
{
/*
* Create a driver from render nodes first, then try card
@@ -105,17 +100,33 @@ int32_t cros_gralloc_driver::init()
for (uint32_t i = min_render_node; i < max_render_node; i++) {
drv_ = init_try_node(i, render_nodes_fmt);
if (drv_)
- return 0;
+ return;
}
// Try card nodes... for vkms mostly.
for (uint32_t i = min_card_node; i < max_card_node; i++) {
drv_ = init_try_node(i, card_nodes_fmt);
if (drv_)
- return 0;
+ return;
}
+}
- return -ENODEV;
+cros_gralloc_driver::~cros_gralloc_driver()
+{
+ buffers_.clear();
+ handles_.clear();
+
+ if (drv_) {
+ int fd = drv_get_fd(drv_);
+ drv_destroy(drv_);
+ drv_ = nullptr;
+ close(fd);
+ }
+}
+
+bool cros_gralloc_driver::is_initialized()
+{
+ return drv_ != nullptr;
}
bool cros_gralloc_driver::is_supported(const struct cros_gralloc_buffer_descriptor *descriptor)
diff --git a/cros_gralloc/cros_gralloc_driver.h b/cros_gralloc/cros_gralloc_driver.h
index ef9e21f..580605c 100644
--- a/cros_gralloc/cros_gralloc_driver.h
+++ b/cros_gralloc/cros_gralloc_driver.h
@@ -16,10 +16,7 @@
class cros_gralloc_driver
{
public:
- cros_gralloc_driver();
- ~cros_gralloc_driver();
-
- int32_t init();
+ static cros_gralloc_driver *get_instance();
bool is_supported(const struct cros_gralloc_buffer_descriptor *descriptor);
int32_t allocate(const struct cros_gralloc_buffer_descriptor *descriptor,
buffer_handle_t *out_handle);
@@ -47,12 +44,13 @@ class cros_gralloc_driver
void for_each_handle(const std::function<void(cros_gralloc_handle_t)> &function);
private:
- cros_gralloc_driver(cros_gralloc_driver const &);
- cros_gralloc_driver operator=(cros_gralloc_driver const &);
+ cros_gralloc_driver();
+ ~cros_gralloc_driver();
+ bool is_initialized();
cros_gralloc_buffer *get_buffer(cros_gralloc_handle_t hnd);
void emplace_buffer(struct bo *bo, struct cros_gralloc_handle *hnd);
- struct driver *drv_;
+ struct driver *drv_ = nullptr;
std::mutex mutex_;
std::unordered_map<uint32_t, cros_gralloc_buffer *> buffers_;
std::unordered_map<cros_gralloc_handle_t, std::pair<cros_gralloc_buffer *, int32_t>>
diff --git a/cros_gralloc/gralloc0/gralloc0.cc b/cros_gralloc/gralloc0/gralloc0.cc
index 386a90a..8570150 100644
--- a/cros_gralloc/gralloc0/gralloc0.cc
+++ b/cros_gralloc/gralloc0/gralloc0.cc
@@ -15,7 +15,7 @@
struct gralloc0_module {
gralloc_module_t base;
std::unique_ptr<alloc_device_t> alloc;
- std::unique_ptr<cros_gralloc_driver> driver;
+ cros_gralloc_driver *driver;
bool initialized;
std::mutex initialization_mutex;
};
@@ -224,11 +224,9 @@ static int gralloc0_init(struct gralloc0_module *mod, bool initialize_alloc)
if (mod->initialized)
return 0;
- mod->driver = std::make_unique<cros_gralloc_driver>();
- if (mod->driver->init()) {
- drv_log("Failed to initialize driver.\n");
+ mod->driver = cros_gralloc_driver::get_instance();
+ if (!mod->driver)
return -ENODEV;
- }
if (initialize_alloc) {
mod->alloc = std::make_unique<alloc_device_t>();
diff --git a/cros_gralloc/gralloc3/CrosGralloc3Allocator.cc b/cros_gralloc/gralloc3/CrosGralloc3Allocator.cc
index 57c49e9..bb9ff97 100644
--- a/cros_gralloc/gralloc3/CrosGralloc3Allocator.cc
+++ b/cros_gralloc/gralloc3/CrosGralloc3Allocator.cc
@@ -24,13 +24,6 @@ using android::hardware::graphics::mapper::V3_0::Error;
using BufferDescriptorInfo =
android::hardware::graphics::mapper::V3_0::IMapper::BufferDescriptorInfo;
-CrosGralloc3Allocator::CrosGralloc3Allocator() : mDriver(std::make_unique<cros_gralloc_driver>()) {
- if (mDriver->init()) {
- drv_log("Failed to initialize driver.\n");
- mDriver = nullptr;
- }
-}
-
Error CrosGralloc3Allocator::allocate(const BufferDescriptorInfo& descriptor, uint32_t* outStride,
hidl_handle* outHandle) {
if (!mDriver) {
diff --git a/cros_gralloc/gralloc3/CrosGralloc3Allocator.h b/cros_gralloc/gralloc3/CrosGralloc3Allocator.h
index 655143c..24a34bd 100644
--- a/cros_gralloc/gralloc3/CrosGralloc3Allocator.h
+++ b/cros_gralloc/gralloc3/CrosGralloc3Allocator.h
@@ -11,7 +11,7 @@
class CrosGralloc3Allocator : public android::hardware::graphics::allocator::V3_0::IAllocator {
public:
- CrosGralloc3Allocator();
+ CrosGralloc3Allocator() = default;
android::hardware::Return<void> allocate(
const android::hardware::hidl_vec<uint32_t>& descriptor, uint32_t count,
@@ -25,5 +25,5 @@ class CrosGralloc3Allocator : public android::hardware::graphics::allocator::V3_
description,
uint32_t* outStride, android::hardware::hidl_handle* outHandle);
- std::unique_ptr<cros_gralloc_driver> mDriver;
+ cros_gralloc_driver* mDriver = cros_gralloc_driver::get_instance();
};
diff --git a/cros_gralloc/gralloc3/CrosGralloc3Mapper.cc b/cros_gralloc/gralloc3/CrosGralloc3Mapper.cc
index 08da016..a0b19f0 100644
--- a/cros_gralloc/gralloc3/CrosGralloc3Mapper.cc
+++ b/cros_gralloc/gralloc3/CrosGralloc3Mapper.cc
@@ -23,13 +23,6 @@ using android::hardware::graphics::mapper::V3_0::Error;
using android::hardware::graphics::mapper::V3_0::IMapper;
using android::hardware::graphics::mapper::V3_0::YCbCrLayout;
-CrosGralloc3Mapper::CrosGralloc3Mapper() : mDriver(std::make_unique<cros_gralloc_driver>()) {
- if (mDriver->init()) {
- drv_log("Failed to initialize driver.\n");
- mDriver = nullptr;
- }
-}
-
Return<void> CrosGralloc3Mapper::createDescriptor(const BufferDescriptorInfo& description,
createDescriptor_cb hidlCb) {
hidl_vec<uint32_t> descriptor;
diff --git a/cros_gralloc/gralloc3/CrosGralloc3Mapper.h b/cros_gralloc/gralloc3/CrosGralloc3Mapper.h
index 7ec92d5..54db61e 100644
--- a/cros_gralloc/gralloc3/CrosGralloc3Mapper.h
+++ b/cros_gralloc/gralloc3/CrosGralloc3Mapper.h
@@ -13,7 +13,7 @@
class CrosGralloc3Mapper : public android::hardware::graphics::mapper::V3_0::IMapper {
public:
- CrosGralloc3Mapper();
+ CrosGralloc3Mapper() = default;
android::hardware::Return<void> createDescriptor(const BufferDescriptorInfo& description,
createDescriptor_cb hidlCb) override;
@@ -58,7 +58,7 @@ class CrosGralloc3Mapper : public android::hardware::graphics::mapper::V3_0::IMa
const Rect& accessRegion,
const android::hardware::hidl_handle& acquireFence);
- std::unique_ptr<cros_gralloc_driver> mDriver;
+ cros_gralloc_driver* mDriver = cros_gralloc_driver::get_instance();
};
extern "C" android::hardware::graphics::mapper::V3_0::IMapper* HIDL_FETCH_IMapper(const char* name);
diff --git a/cros_gralloc/gralloc4/CrosGralloc4Allocator.cc b/cros_gralloc/gralloc4/CrosGralloc4Allocator.cc
index e7e5f3a..200b939 100644
--- a/cros_gralloc/gralloc4/CrosGralloc4Allocator.cc
+++ b/cros_gralloc/gralloc4/CrosGralloc4Allocator.cc
@@ -23,13 +23,6 @@ using android::hardware::graphics::mapper::V4_0::Error;
using BufferDescriptorInfo =
android::hardware::graphics::mapper::V4_0::IMapper::BufferDescriptorInfo;
-CrosGralloc4Allocator::CrosGralloc4Allocator() : mDriver(std::make_unique<cros_gralloc_driver>()) {
- if (mDriver->init()) {
- drv_log("Failed to initialize driver.\n");
- mDriver = nullptr;
- }
-}
-
Error CrosGralloc4Allocator::allocate(const BufferDescriptorInfo& descriptor, uint32_t* outStride,
hidl_handle* outHandle) {
if (!mDriver) {
diff --git a/cros_gralloc/gralloc4/CrosGralloc4Allocator.h b/cros_gralloc/gralloc4/CrosGralloc4Allocator.h
index 21ad7ad..cb3b4b8 100644
--- a/cros_gralloc/gralloc4/CrosGralloc4Allocator.h
+++ b/cros_gralloc/gralloc4/CrosGralloc4Allocator.h
@@ -11,7 +11,7 @@
class CrosGralloc4Allocator : public android::hardware::graphics::allocator::V4_0::IAllocator {
public:
- CrosGralloc4Allocator();
+ CrosGralloc4Allocator() = default;
android::hardware::Return<void> allocate(const android::hardware::hidl_vec<uint8_t>& descriptor,
uint32_t count, allocate_cb hidl_cb) override;
@@ -22,5 +22,5 @@ class CrosGralloc4Allocator : public android::hardware::graphics::allocator::V4_
description,
uint32_t* outStride, android::hardware::hidl_handle* outHandle);
- std::unique_ptr<cros_gralloc_driver> mDriver;
+ cros_gralloc_driver* mDriver = cros_gralloc_driver::get_instance();
};
diff --git a/cros_gralloc/gralloc4/CrosGralloc4Mapper.cc b/cros_gralloc/gralloc4/CrosGralloc4Mapper.cc
index 1bfd442..807eba1 100644
--- a/cros_gralloc/gralloc4/CrosGralloc4Mapper.cc
+++ b/cros_gralloc/gralloc4/CrosGralloc4Mapper.cc
@@ -31,62 +31,6 @@ using android::hardware::graphics::common::V1_2::PixelFormat;
using android::hardware::graphics::mapper::V4_0::Error;
using android::hardware::graphics::mapper::V4_0::IMapper;
-namespace {
-
-// Provides a single instance of cros_gralloc_driver to all active instances of
-// CrosGralloc4Mapper in a single process while destroying the cros_gralloc_driver
-// when there are no active instances of CrosGralloc4Mapper.
-class DriverProvider {
- public:
- static DriverProvider* Get() {
- static DriverProvider* instance = new DriverProvider();
- return instance;
- }
-
- cros_gralloc_driver* GetAndReferenceDriver() {
- std::lock_guard<std::mutex> lock(mMutex);
- if (!mDriver) {
- mDriver = std::make_unique<cros_gralloc_driver>();
- if (mDriver->init()) {
- drv_log("Failed to initialize driver.\n");
- mDriver.reset();
- return nullptr;
- }
- }
-
- ++mReferenceCount;
- return mDriver.get();
- }
-
- void UnreferenceDriver() {
- std::lock_guard<std::mutex> lock(mMutex);
-
- --mReferenceCount;
-
- if (mReferenceCount == 0) {
- mDriver.reset();
- }
- }
-
- private:
- DriverProvider() = default;
-
- std::mutex mMutex;
- std::unique_ptr<cros_gralloc_driver> mDriver;
- std::size_t mReferenceCount = 0;
-};
-
-} // namespace
-
-CrosGralloc4Mapper::CrosGralloc4Mapper() {
- mDriver = DriverProvider::Get()->GetAndReferenceDriver();
-}
-
-CrosGralloc4Mapper::~CrosGralloc4Mapper() {
- mDriver = nullptr;
- DriverProvider::Get()->UnreferenceDriver();
-}
-
Return<void> CrosGralloc4Mapper::createDescriptor(const BufferDescriptorInfo& description,
createDescriptor_cb hidlCb) {
hidl_vec<uint8_t> descriptor;
diff --git a/cros_gralloc/gralloc4/CrosGralloc4Mapper.h b/cros_gralloc/gralloc4/CrosGralloc4Mapper.h
index 3c159a2..0641b29 100644
--- a/cros_gralloc/gralloc4/CrosGralloc4Mapper.h
+++ b/cros_gralloc/gralloc4/CrosGralloc4Mapper.h
@@ -11,8 +11,7 @@
class CrosGralloc4Mapper : public android::hardware::graphics::mapper::V4_0::IMapper {
public:
- CrosGralloc4Mapper();
- ~CrosGralloc4Mapper();
+ CrosGralloc4Mapper() = default;
android::hardware::Return<void> createDescriptor(const BufferDescriptorInfo& description,
createDescriptor_cb hidlCb) override;
@@ -75,7 +74,7 @@ class CrosGralloc4Mapper : public android::hardware::graphics::mapper::V4_0::IMa
int getResolvedDrmFormat(android::hardware::graphics::common::V1_2::PixelFormat pixelFormat,
uint64_t bufferUsage, uint32_t* outDrmFormat);
- cros_gralloc_driver* mDriver = nullptr;
+ cros_gralloc_driver* mDriver = cros_gralloc_driver::get_instance();
};
extern "C" android::hardware::graphics::mapper::V4_0::IMapper* HIDL_FETCH_IMapper(const char* name);