diff options
Diffstat (limited to 'minui/graphics.cpp')
-rw-r--r-- | minui/graphics.cpp | 94 |
1 files changed, 82 insertions, 12 deletions
diff --git a/minui/graphics.cpp b/minui/graphics.cpp index dce1e619a..b24c2b114 100644 --- a/minui/graphics.cpp +++ b/minui/graphics.cpp @@ -36,12 +36,14 @@ static int overscan_offset_x = 0; static int overscan_offset_y = 0; static uint32_t gr_current = ~0; -static constexpr uint32_t alpha_mask = 0xff000000; // gr_draw is owned by backends. 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); @@ -76,7 +78,7 @@ int gr_font_size(const GRFont* font, int* x, int* y) { } // Blends gr_current onto pix value, assumes alpha as most significant byte. -static inline uint32_t pixel_blend(uint8_t alpha, uint32_t pix) { +static inline uint32_t pixel_blend_argb(uint8_t alpha, uint32_t pix) { if (alpha == 255) return gr_current; if (alpha == 0) return pix; uint32_t pix_r = pix & 0xff; @@ -93,6 +95,48 @@ static inline uint32_t pixel_blend(uint8_t alpha, uint32_t pix) { return (out_r & 0xff) | (out_g & 0xff00) | (out_b & 0xff0000) | (gr_current & 0xff000000); } +static inline uint32_t pixel_blend_rgba(uint8_t alpha, uint32_t pix) { + if (alpha == 255) return gr_current; + if (alpha == 0) return pix; + uint32_t pix_r = pix & 0xff00; + uint32_t pix_g = pix & 0xff0000; + uint32_t pix_b = pix & 0xff000000; + uint32_t cur_r = gr_current & 0xff00; + uint32_t cur_g = gr_current & 0xff0000; + uint32_t cur_b = gr_current & 0xff000000; + + uint32_t out_r = (pix_r * (255 - alpha) + cur_r * alpha) / 255; + uint32_t out_g = (pix_g * (255 - alpha) + cur_g * alpha) / 255; + uint32_t out_b = (pix_b * (255 - alpha) + cur_b * alpha) / 255; + + return (gr_current & 0xff) | (out_r & 0xff00) | (out_g & 0xff0000) | (out_b & 0xff000000); +} + +static inline uint32_t pixel_blend(uint8_t alpha, uint32_t pix) { + if (pixel_format == PixelFormat::RGBA) { + return pixel_blend_rgba(alpha, pix); + } + return pixel_blend_argb(alpha, pix); +} + +static inline uint32_t get_alphamask() { + if (pixel_format == PixelFormat::RGBA) { + return 0x000000ff; + } + return 0xff000000; +} + +static inline uint8_t get_alpha_shift() { + if (pixel_format == PixelFormat::RGBA) { + return 0; + } + return 24; +} + +static inline uint8_t get_alpha(uint32_t pix) { + return static_cast<uint8_t>((pix & (gr_current & get_alphamask())) >> get_alpha_shift()); +} + // Increments pixel pointer right, with current rotation. static void incr_x(uint32_t** p, int row_pixels) { if (rotation == GRRotation::LEFT) { @@ -140,7 +184,7 @@ static uint32_t* PixelAt(GRSurface* surface, int x, int y, int row_pixels) { static void TextBlend(const uint8_t* src_p, int src_row_bytes, uint32_t* dst_p, int dst_row_pixels, int width, int height) { - uint8_t alpha_current = static_cast<uint8_t>((alpha_mask & gr_current) >> 24); + uint8_t alpha_current = get_alpha(gr_current); for (int j = 0; j < height; ++j) { const uint8_t* sx = src_p; uint32_t* px = dst_p; @@ -155,7 +199,7 @@ static void TextBlend(const uint8_t* src_p, int src_row_bytes, uint32_t* dst_p, } void gr_text(const GRFont* font, int x, int y, const char* s, bool bold) { - if (!font || !font->texture || (gr_current & alpha_mask) == 0) return; + if (!font || !font->texture || (gr_current & get_alphamask()) == 0) return; if (font->texture->pixel_bytes != 1) { printf("gr_text: font has wrong format\n"); @@ -210,6 +254,8 @@ void gr_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a uint32_t r32 = r, g32 = g, b32 = b, a32 = a; if (pixel_format == PixelFormat::ARGB || pixel_format == PixelFormat::BGRA) { gr_current = (a32 << 24) | (r32 << 16) | (g32 << 8) | b32; + } else if (pixel_format == PixelFormat::RGBA) { + gr_current = (b32 << 24) | (g32 << 16) | (r32 << 8) | a32; } else { gr_current = (a32 << 24) | (b32 << 16) | (g32 << 8) | r32; } @@ -244,7 +290,7 @@ void gr_fill(int x1, int y1, int x2, int y2) { int row_pixels = gr_draw->row_bytes / gr_draw->pixel_bytes; uint32_t* p = PixelAt(gr_draw, x1, y1, row_pixels); - uint8_t alpha = static_cast<uint8_t>(((gr_current & alpha_mask) >> 24)); + uint8_t alpha = get_alpha(gr_current); if (alpha > 0) { for (int y = y1; y < y2; ++y) { uint32_t* px = p; @@ -340,7 +386,22 @@ void gr_flip() { gr_draw = gr_backend->Flip(); } +std::unique_ptr<MinuiBackend> create_backend(GraphicsBackend backend) { + switch (backend) { + case GraphicsBackend::DRM: + return std::make_unique<MinuiBackendDrm>(); + case GraphicsBackend::FBDEV: + return std::make_unique<MinuiBackendFbdev>(); + default: + return nullptr; + } +} + int gr_init() { + return gr_init(default_backends); +} + +int gr_init(std::initializer_list<GraphicsBackend> 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") { @@ -351,6 +412,8 @@ int gr_init() { pixel_format = PixelFormat::ARGB; } else if (format == "BGRA_8888") { pixel_format = PixelFormat::BGRA; + } else if (format == "RGBA_8888") { + pixel_format = PixelFormat::RGBA; } else { pixel_format = PixelFormat::UNKNOWN; } @@ -361,19 +424,22 @@ int gr_init() { ret); } - auto backend = std::unique_ptr<MinuiBackend>{ std::make_unique<MinuiBackendDrm>() }; - gr_draw = backend->Init(); - - if (!gr_draw) { - backend = std::make_unique<MinuiBackendFbdev>(); - gr_draw = backend->Init(); + std::unique_ptr<MinuiBackend> 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; @@ -429,6 +495,10 @@ void gr_fb_blank(bool blank) { gr_backend->Blank(blank); } +void gr_fb_blank(bool blank, int index) { + gr_backend->Blank(blank, static_cast<MinuiBackend::DrmConnector>(index)); +} + void gr_rotate(GRRotation rot) { rotation = rot; } |