From 6f7362ab7ba2a8576476a69db35c3eef0f3af56d Mon Sep 17 00:00:00 2001 From: Chihhang Chuang Date: Fri, 24 Sep 2021 00:11:51 +0800 Subject: Add support to use preferred graphics backend - Add #gr_init(backends) for users to use preferred graphics backend. Bug: 196777741 Test: build, flash, and boot device. Change-Id: I2621e879bbc7dce926a263a97a1013985d75592a --- minui/graphics.cpp | 35 ++++++++++++++++++++++++++++------- minui/include/minui/minui.h | 15 ++++++++++++--- 2 files changed, 40 insertions(+), 10 deletions(-) diff --git a/minui/graphics.cpp b/minui/graphics.cpp index dce1e619a..f25694ab5 100644 --- a/minui/graphics.cpp +++ b/minui/graphics.cpp @@ -42,6 +42,9 @@ static constexpr uint32_t alpha_mask = 0xff000000; static GRSurface* gr_draw = nullptr; static GRRotation rotation = GRRotation::NONE; static PixelFormat pixel_format = PixelFormat::UNKNOWN; +// The graphics backend list that provides fallback options for the default backend selection. +// For example, it will fist try DRM, then try FBDEV if DRM is unavailable. +constexpr auto default_backends = { GraphicsBackend::DRM, GraphicsBackend::FBDEV }; static bool outside(int x, int y) { auto swapped = (rotation == GRRotation::LEFT || rotation == GRRotation::RIGHT); @@ -340,7 +343,22 @@ void gr_flip() { gr_draw = gr_backend->Flip(); } +std::unique_ptr create_backend(GraphicsBackend backend) { + switch (backend) { + case GraphicsBackend::DRM: + return std::make_unique(); + case GraphicsBackend::FBDEV: + return std::make_unique(); + default: + return nullptr; + } +} + int gr_init() { + return gr_init(default_backends); +} + +int gr_init(std::initializer_list backends) { // pixel_format needs to be set before loading any resources or initializing backends. std::string format = android::base::GetProperty("ro.minui.pixel_format", ""); if (format == "ABGR_8888") { @@ -361,19 +379,22 @@ int gr_init() { ret); } - auto backend = std::unique_ptr{ std::make_unique() }; - gr_draw = backend->Init(); - - if (!gr_draw) { - backend = std::make_unique(); - gr_draw = backend->Init(); + std::unique_ptr minui_backend; + for (GraphicsBackend backend : backends) { + minui_backend = create_backend(backend); + if (!minui_backend) { + printf("gr_init: minui_backend %d is a nullptr\n", backend); + continue; + } + gr_draw = minui_backend->Init(); + if (gr_draw) break; } if (!gr_draw) { return -1; } - gr_backend = backend.release(); + gr_backend = minui_backend.release(); int overscan_percent = android::base::GetIntProperty("ro.minui.overscan_percent", 0); overscan_offset_x = gr_draw->width * overscan_percent / 100; diff --git a/minui/include/minui/minui.h b/minui/include/minui/minui.h index 163e41dc6..5470457e7 100644 --- a/minui/include/minui/minui.h +++ b/minui/include/minui/minui.h @@ -104,10 +104,19 @@ enum class PixelFormat : int { ARGB = 4, }; -// Initializes the graphics backend and loads font file. Returns 0 on success, or -1 on error. Note -// that the font initialization failure would be non-fatal, as caller may not need to draw any text -// at all. Caller can check the font initialization result via gr_sys_font() as needed. +enum class GraphicsBackend : int { + UNKNOWN = 0, + DRM = 1, + FBDEV = 2, +}; + +// Initializes the default graphics backend and loads font file. Returns 0 on success, or -1 on +// error. Note that the font initialization failure would be non-fatal, as caller may not need to +// draw any text at all. Caller can check the font initialization result via gr_sys_font() as +// needed. int gr_init(); +// Supports backend selection for minui client. +int gr_init(std::initializer_list backends); // Frees the allocated resources. The function is idempotent, and safe to be called if gr_init() // didn't finish successfully. -- cgit v1.2.3