Merge tag 'android-13.0.0_r3' into android-12.1

Android 13.0.0 Release 3 (TP1A.220624.021.A1)
diff --git a/minui/Android.bp b/minui/Android.bp
old mode 100755
new mode 100644
index f68f6c8..02fb363
--- a/minui/Android.bp
+++ b/minui/Android.bp
@@ -24,6 +24,7 @@
 cc_library {
     name: "libminui",
     recovery_available: true,
+    vendor_available: true,
 
     defaults: [
         "recovery_defaults",
@@ -51,4 +52,15 @@
         "libpng",
         "libz",
     ],
+
+    target: {
+        vendor: {
+            exclude_static_libs: [
+                "libsync",
+            ],
+            shared_libs: [
+                "libsync",
+            ],
+        },
+    },
 }
diff --git a/minui/events.cpp b/minui/events.cpp
old mode 100755
new mode 100644
index cee4d73..b307a49
--- a/minui/events.cpp
+++ b/minui/events.cpp
@@ -29,7 +29,9 @@
 
 #include <functional>
 #include <memory>
+#include <string>
 
+#include <android-base/strings.h>
 #include <android-base/unique_fd.h>
 
 #include "minui/minui.h"
@@ -47,9 +49,6 @@
 struct FdInfo {
   android::base::unique_fd fd;
   ev_callback cb;
-#ifdef TW_USE_MINUI_WITH_DATA
-  void* data;
-#endif
 };
 
 static bool g_allow_touch_inputs = true;
@@ -68,7 +67,6 @@
   return (array[bit / BITS_PER_LONG] & (1UL << (bit % BITS_PER_LONG))) != 0;
 }
 
-#ifdef TW_USE_MINUI_WITH_OPTIONAL_TOUCH_EVENTS
 static bool should_add_input_device(int fd, bool allow_touch_inputs) {
   // Use unsigned long to match ioctl's parameter type.
   unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)];  // NOLINT
@@ -122,12 +120,12 @@
     }
     offset += sizeof(inotify_event) + pevent->len;
 
-    pevent->name[pevent->len] = '\0';
-    if (strncmp(pevent->name, "event", 5)) {
+    std::string event_name(pevent->name, pevent->len);
+    if (!android::base::StartsWith(event_name, "event")) {
       continue;
     }
 
-    android::base::unique_fd dfd(openat(dirfd(dir.get()), pevent->name, O_RDONLY));
+    android::base::unique_fd dfd(openat(dirfd(dir.get()), event_name.c_str(), O_RDONLY));
     if (dfd == -1) {
       break;
     }
@@ -144,14 +142,6 @@
 }
 
 int ev_init(ev_callback input_cb, bool allow_touch_inputs) {
-#else
-#ifdef TW_USE_MINUI_WITH_DATA
-int ev_init(ev_callback input_cb, void* data) {
-#else
-int ev_init(ev_callback input_cb) {
-#endif
-  bool allow_touch_inputs = false;
-#endif
   g_epoll_fd.reset();
 
   android::base::unique_fd epoll_fd(epoll_create1(EPOLL_CLOEXEC));
@@ -180,11 +170,9 @@
     android::base::unique_fd fd(openat(dirfd(dir.get()), de->d_name, O_RDONLY | O_CLOEXEC));
     if (fd == -1) continue;
 
-#ifdef TW_USE_MINUI_WITH_OPTIONAL_TOUCH_EVENTS
     if (!should_add_input_device(fd, allow_touch_inputs)) {
       continue;
     }
-#endif
 
     epoll_event ev;
     ev.events = EPOLLIN | EPOLLWAKEUP;
@@ -209,9 +197,7 @@
 
   g_saved_input_cb = input_cb;
   g_allow_touch_inputs = allow_touch_inputs;
-#ifdef TW_USE_MINUI_WITH_OPTIONAL_TOUCH_EVENTS
   ev_add_fd(std::move(inotify_fd), inotify_cb);
-#endif
 
   return 0;
 }
@@ -281,11 +267,36 @@
   return -1;
 }
 
-#ifdef TW_USE_MINUI_WITH_DATA
-int ev_sync_key_state(ev_set_key_callback set_key_cb, void* data) {
-#else
+int ev_sync_sw_state(const ev_set_sw_callback& set_sw_cb) {
+  // Use unsigned long to match ioctl's parameter type.
+  unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)];  // NOLINT
+  unsigned long sw_bits[BITS_TO_LONGS(SW_MAX)];  // NOLINT
+
+  for (size_t i = 0; i < g_ev_dev_count; ++i) {
+    memset(ev_bits, 0, sizeof(ev_bits));
+    memset(sw_bits, 0, sizeof(sw_bits));
+
+    if (ioctl(ev_fdinfo[i].fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) == -1) {
+      continue;
+    }
+    if (!test_bit(EV_SW, ev_bits)) {
+      continue;
+    }
+    if (ioctl(ev_fdinfo[i].fd, EVIOCGSW(sizeof(sw_bits)), sw_bits) == -1) {
+      continue;
+    }
+
+    for (int code = 0; code <= SW_MAX; code++) {
+      if (test_bit(code, sw_bits)) {
+        set_sw_cb(code, 1);
+      }
+    }
+  }
+
+  return 0;
+}
+
 int ev_sync_key_state(const ev_set_key_callback& set_key_cb) {
-#endif
   // Use unsigned long to match ioctl's parameter type.
   unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)];    // NOLINT
   unsigned long key_bits[BITS_TO_LONGS(KEY_MAX)];  // NOLINT
@@ -306,11 +317,7 @@
 
     for (int code = 0; code <= KEY_MAX; code++) {
       if (test_bit(code, key_bits)) {
-#ifdef TW_USE_MINUI_WITH_DATA
-        set_key_cb(code, 1, data);
-#else
         set_key_cb(code, 1);
-#endif
       }
     }
   }
@@ -347,7 +354,6 @@
   }
 }
 
-#ifdef TW_USE_MINUI_WITH_OPTIONAL_TOUCH_EVENTS
 void ev_iterate_touch_inputs(const std::function<void(int)>& action) {
   for (size_t i = 0; i < g_ev_dev_count; ++i) {
     // Use unsigned long to match ioctl's parameter type.
@@ -371,4 +377,3 @@
     }
   }
 }
-#endif
diff --git a/minui/graphics.cpp b/minui/graphics.cpp
old mode 100755
new mode 100644
index 38ba6ca..41a3661
--- a/minui/graphics.cpp
+++ b/minui/graphics.cpp
@@ -25,12 +25,6 @@
 
 #include <android-base/properties.h>
 
-#ifdef BOARD_USE_CUSTOM_RECOVERY_FONT
-#include BOARD_USE_CUSTOM_RECOVERY_FONT
-#else
-#include "font_10x18.h"
-#endif
-
 #include "graphics_drm.h"
 #include "graphics_fbdev.h"
 #include "minui/minui.h"
@@ -41,24 +35,15 @@
 static int overscan_offset_x = 0;
 static int overscan_offset_y = 0;
 
-#ifdef TW_NO_MINUI_CUSTOM_FONTS
-static unsigned char gr_current_r = 255;
-static unsigned char gr_current_g = 255;
-static unsigned char gr_current_b = 255;
-#endif
-static unsigned char gr_current_a = 255;
-static unsigned char rgb_555[2];
-static unsigned char gr_current_r5 = 31;
-static unsigned char gr_current_g5 = 63;
-static unsigned char gr_current_b5 = 31;
-
 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);
@@ -66,18 +51,6 @@
          y >= (swapped ? gr_draw->width : gr_draw->height);
 }
 
-#ifdef TW_NO_MINUI_CUSTOM_FONTS
-int gr_measure(const char *s)
-{
-    return gr_font->char_width * strlen(s);
-}
-
-void gr_font_size(int *x, int *y)
-{
-    *x = gr_font->char_width;
-    *y = gr_font->char_height;
-}
-#else // TW_USE_MINUI_CUSTOM_FONTS
 const GRFont* gr_sys_font() {
   return gr_font;
 }
@@ -103,78 +76,9 @@
   *y = font->char_height;
   return 0;
 }
-#endif // TW_NO_MINUI_CUSTOM_FONTS
-
-void blend_16bpp(unsigned char* px, unsigned r5, unsigned g5, unsigned b5, unsigned char a)
-{
-    unsigned char orig[2];
-    orig[0] = px[0];
-    orig[1] = px[1];
-
-    /* This code is a little easier to read
-    unsigned oldred = (orig[1] >> 3);
-    unsigned oldgreen = (((orig[0] >> 5) << 3) + (orig[1] & 0x7));
-    unsigned oldblue = (orig[0] & 0x1F);
-
-    unsigned newred = (oldred * (255-a) + r5 * a) / 255;
-    unsigned newgreen = (oldgreen * (255-a) + g5 * a) / 255;
-    unsigned newblue = (oldblue * (255-a) + b5 * a) / 255;
-    */
-
-    unsigned newred = ((orig[1] >> 3) * (255-a) + r5 * a) / 255;
-    unsigned newgreen = ((((orig[0] >> 5) << 3) + (orig[1] & 0x7)) * (255-a) + g5 * a) / 255;
-    unsigned newblue = ((orig[0] & 0x1F) * (255-a) + b5 * a) / 255;
-
-    *px++ = (newgreen << 5) + (newblue);
-    *px++ = (newred << 3) + (newgreen >> 3);
-}
-
-#ifdef TW_NO_MINUI_CUSTOM_FONTS
-static void text_blend_old(unsigned char* src_p, int src_row_bytes,
-                       unsigned char* dst_p, int dst_row_bytes,
-                       int width, int height)
-{
-    for (int j = 0; j < height; ++j) {
-        unsigned char* sx = src_p;
-        unsigned char* px = dst_p;
-        for (int i = 0; i < width; ++i) {
-            unsigned char a = *sx++;
-            if (gr_current_a < 255) a = ((int)a * gr_current_a) / 255;
-            if (a == 255) {
-                if (gr_draw->pixel_bytes == 2) {
-                    *px++ = rgb_555[0];
-                    *px++ = rgb_555[1];
-                } else {
-                    *px++ = gr_current_r;
-                    *px++ = gr_current_g;
-                    *px++ = gr_current_b;
-                    px++;
-                }
-            } else if (a > 0) {
-                if (gr_draw->pixel_bytes == 2) {
-                    blend_16bpp(px, gr_current_r5, gr_current_g5, gr_current_b5, a);
-                    px += gr_draw->pixel_bytes;
-                } else {
-                    *px = (*px * (255-a) + gr_current_r * a) / 255;
-                    ++px;
-                    *px = (*px * (255-a) + gr_current_g * a) / 255;
-                    ++px;
-                    *px = (*px * (255-a) + gr_current_b * a) / 255;
-                    ++px;
-                    ++px;
-                }
-            } else {
-                px += gr_draw->pixel_bytes;
-            }
-        }
-        src_p += src_row_bytes;
-        dst_p += dst_row_bytes;
-    }
-}
-#endif // TW_NO_MINUI_CUSTOM_FONTS
 
 // 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;
@@ -191,6 +95,48 @@
   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) {
@@ -238,7 +184,7 @@
 
 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;
@@ -252,41 +198,8 @@
   }
 }
 
-
-#ifdef TW_NO_MINUI_CUSTOM_FONTS
-void gr_text(int x, int y, const char *s, bool bold)
-{
-    GRFont* font = gr_font;
-
-    if (!font->texture || gr_current_a == 0) return;
-
-    bold = bold && (font->texture->height != font->char_height);
-
-    x += overscan_offset_x;
-    y += overscan_offset_y;
-
-    unsigned char ch;
-    while ((ch = *s++)) {
-        if (outside(x, y) || outside(x+font->char_width-1, y+font->char_height-1)) break;
-
-        if (ch < ' ' || ch > '~') {
-            ch = '?';
-        }
-
-        unsigned char* src_p = font->texture->data + ((ch - ' ') * font->char_width) +
-                               (bold ? font->char_height * font->texture->row_bytes : 0);
-        unsigned char* dst_p = gr_draw->data + y*gr_draw->row_bytes + x*gr_draw->pixel_bytes;
-
-        text_blend_old(src_p, font->texture->row_bytes,
-                       dst_p, gr_draw->row_bytes,
-                       font->char_width, font->char_height);
-
-        x += font->char_width;
-    }
-}
-#else //TW_NO_MINUI_CUSTOM_FONTS
 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");
@@ -317,7 +230,6 @@
     x += font->char_width;
   }
 }
-#endif //TW_NO_MINUI_CUSTOM_FONTS
 
 void gr_texticon(int x, int y, const GRSurface* icon) {
   if (icon == nullptr) return;
@@ -338,50 +250,18 @@
   TextBlend(src_p, icon->row_bytes, dst_p, row_pixels, icon->width, icon->height);
 }
 
-void gr_convert_rgb_555(unsigned char r, unsigned char g, unsigned char b)
-{
-    gr_current_r5 = (((r & 0xFF) * 0x1F) + 0x7F) / 0xFF;
-    gr_current_g5 = (((g & 0xFF) * 0x3F) + 0x7F) / 0xFF;
-    gr_current_b5 = (((b & 0xFF) * 0x1F) + 0x7F) / 0xFF;
-
-    rgb_555[0] = (gr_current_g5 << 5) + (gr_current_b5);
-    rgb_555[1] = (gr_current_r5 << 3) + (gr_current_g5 >> 3);
-}
-
 void gr_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a) {
-#ifdef TW_NO_MINUI_CUSTOM_FONTS
-#if defined(RECOVERY_ABGR) || defined(RECOVERY_BGRA)
-  gr_current_r = b;
-  gr_current_g = g;
-  gr_current_b = r;
-#else
-  gr_current_r = r;
-  gr_current_g = g;
-  gr_current_b = b;
-#endif
-#endif
-
-  if (gr_draw->pixel_bytes == 2) {
-	gr_current_a = a;
-    gr_convert_rgb_555(r, g, b);
-    return;
-  }
-
   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;
   }
 }
 
 void gr_clear() {
-  if (gr_draw->pixel_bytes == 2) {
-    gr_fill(0, 0, gr_fb_width(), gr_fb_height());
-    return;
-  }
-
-  // This code only works on 32bpp devices
   if ((gr_current & 0xff) == ((gr_current >> 8) & 0xff) &&
       (gr_current & 0xff) == ((gr_current >> 16) & 0xff) &&
       (gr_current & 0xff) == ((gr_current >> 24) & 0xff) &&
@@ -410,7 +290,7 @@
 
   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;
@@ -423,56 +303,12 @@
   }
 }
 
-void gr_blit_32to16(const GRSurface* source, int sx, int sy, int w, int h, int dx, int dy) {
-  dx += overscan_offset_x;
-  dy += overscan_offset_y;
-
-  if (outside(dx, dy) || outside(dx+w-1, dy+h-1)) return;
-
-  unsigned char* src_p = (unsigned char*) source->data() + sy*source->row_bytes + sx*source->pixel_bytes;
-  unsigned char* dst_p = gr_draw->data() + dy*gr_draw->row_bytes + dx*gr_draw->pixel_bytes;
-
-  int i, j;
-  for (i = 0; i < h; ++i) {
-    unsigned char* spx = src_p;
-    unsigned char* dpx = dst_p;
-
-    for (j = 0; j < w; ++j) {
-      unsigned a = spx[3];
-
-      if (a == 0) {
-        spx += source->pixel_bytes;
-        dpx += gr_draw->pixel_bytes;
-      } else {
-        unsigned r5 = (((*spx++ & 0xFF) * 0x1F) + 0x7F) / 0xFF;
-        unsigned g5 = (((*spx++ & 0xFF) * 0x3F) + 0x7F) / 0xFF;
-        unsigned b5 = (((*spx++ & 0xFF) * 0x1F) + 0x7F) / 0xFF;
-        spx++;
-        if (a == 255) {
-          *dpx++ = (g5 << 5) + (b5);
-          *dpx++ = (r5 << 3) + (g5 >> 3);
-        } else {
-          blend_16bpp(dpx, r5, g5, b5, a);
-          spx += source->pixel_bytes;
-        }
-      }
-    }
-    src_p += source->row_bytes;
-    dst_p += gr_draw->row_bytes;
-  }
-}
-
 void gr_blit(const GRSurface* source, int sx, int sy, int w, int h, int dx, int dy) {
   if (source == nullptr) return;
 
   if (gr_draw->pixel_bytes != source->pixel_bytes) {
-    if (gr_draw->pixel_bytes == 2 && source->pixel_bytes == 4) {
-      gr_blit_32to16(source, sx, sy, w, h, dx, dy);
-      return;
-    } else {
-      printf("gr_blit: source has wrong format\n");
-      return;
-    }
+    printf("gr_blit: source has wrong format\n");
+    return;
   }
 
   dx += overscan_offset_x;
@@ -523,49 +359,6 @@
   return surface->height;
 }
 
-#ifdef TW_NO_MINUI_CUSTOM_FONTS
-static void gr_init_font(void)
-{
-    gr_font = reinterpret_cast<GRFont*>(calloc(sizeof(*gr_font), 1));
-
-    int res = res_create_alpha_surface("font", &(gr_font->texture));
-    if (res == 0) {
-        // The font image should be a 96x2 array of character images.  The
-        // columns are the printable ASCII characters 0x20 - 0x7f.  The
-        // top row is regular text; the bottom row is bold.
-        gr_font->char_width = gr_font->texture->width / 96;
-        gr_font->char_height = gr_font->texture->height / 2;
-    } else {
-        printf("failed to read font: res=%d\n", res);
-
-        // fall back to the compiled-in font.
-        gr_font->texture = reinterpret_cast<GRSurface*>(malloc(sizeof(*gr_font->texture)));
-        gr_font->texture->width = font.width;
-        gr_font->texture->height = font.height;
-        gr_font->texture->row_bytes = font.width;
-        gr_font->texture->pixel_bytes = 1;
-
-        unsigned char* bits = reinterpret_cast<unsigned char*>(malloc(font.width * font.height));
-        gr_font->texture->data = reinterpret_cast<unsigned char*>(bits);
-
-        unsigned char data;
-        unsigned char* in = font.rundata;
-        while((data = *in++)) {
-            memset(bits, (data & 0x80) ? 255 : 0, data & 0x7f);
-            bits += (data & 0x7f);
-        }
-
-        gr_font->char_width = font.char_width;
-        gr_font->char_height = font.char_height;
-    }
-}
-
-void gr_set_font(__attribute__ ((unused))const char* name) {
-	//this cm function is made to change font. Don't care, just init the font:
-	gr_init_font();
-	return;
-}
-#else // TW_NO_MINUI_CUSTOM_FONTS
 int gr_init_font(const char* name, GRFont** dest) {
   GRFont* font = static_cast<GRFont*>(calloc(1, sizeof(*gr_font)));
   if (font == nullptr) {
@@ -588,13 +381,27 @@
 
   return 0;
 }
-#endif // TW_NO_MINUI_CUSTOM_FONTS
 
 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") {
@@ -605,6 +412,8 @@
     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;
   }
@@ -615,34 +424,22 @@
            ret);
   }
 
-  auto backend = std::unique_ptr<MinuiBackend>{ std::make_unique<MinuiBackendDrm>() };
-  gr_draw = backend->Init();
-
-#ifdef MSM_BSP
-    if (gr_draw) {
-        printf("Using overlay graphics.\n");
+  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;
     }
-#endif
-
-
-    if (!gr_draw) {
-        backend = std::make_unique<MinuiBackendDrm>();
-        gr_draw = backend->Init();
-        if (gr_draw)
-            printf("Using drm graphics.\n");
-    }
-
-    if (!gr_draw) {
-        backend = std::make_unique<MinuiBackendFbdev>();
-        gr_draw = backend->Init();
-        if (gr_draw)
-            printf("Using fbdev graphics.\n");
-    }
+    gr_draw = minui_backend->Init();
+    if (gr_draw) break;
+  }
 
   if (!gr_draw) {
     return -1;
   }
 
+  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;
@@ -698,6 +495,14 @@
   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;
 }
+
+bool gr_has_multiple_connectors() {
+  return gr_backend->HasMultipleConnectors();
+}
diff --git a/minui/graphics.h b/minui/graphics.h
index 3c45a40..ff063ae 100644
--- a/minui/graphics.h
+++ b/minui/graphics.h
@@ -21,6 +21,12 @@
 
 class MinuiBackend {
  public:
+  enum DrmConnector {
+    DRM_MAIN = 0,
+    DRM_SEC,
+    DRM_MAX,
+  };
+
   // Initializes the backend and returns a GRSurface* to draw into.
   virtual GRSurface* Init() = 0;
 
@@ -28,11 +34,17 @@
   // be displayed, and returns a new drawing surface.
   virtual GRSurface* Flip() = 0;
 
-  // Blank (or unblank) the screen.
+  // Blank (or unblank) the default screen.
   virtual void Blank(bool) = 0;
 
+  // Blank (or unblank) the specific screen.
+  virtual void Blank(bool blank, DrmConnector index) = 0;
+
+  // Return true if the device supports multiple connectors.
+  virtual bool HasMultipleConnectors() = 0;
+
   // Device cleanup when drawing is done.
-  virtual ~MinuiBackend() {};
+  virtual ~MinuiBackend() = default;
 };
 
 #endif  // _GRAPHICS_H_
diff --git a/minui/graphics_drm.cpp b/minui/graphics_drm.cpp
old mode 100755
new mode 100644
index 95759e3..6c3a5bd
--- a/minui/graphics_drm.cpp
+++ b/minui/graphics_drm.cpp
@@ -105,6 +105,8 @@
     perror("Failed to DRM_IOCTL_MODE_CREATE_DUMB");
     return nullptr;
   }
+  printf("Allocating buffer with resolution %d x %d pitch: %d bpp: %d, size: %llu\n", width, height,
+         create_dumb.pitch, create_dumb.bpp, create_dumb.size);
 
   // Cannot use std::make_unique to access non-public ctor.
   auto surface = std::unique_ptr<GRSurfaceDrm>(new GRSurfaceDrm(
@@ -128,13 +130,14 @@
     return nullptr;
   }
 
-  auto mmapped = mmap(nullptr, surface->height * surface->row_bytes, PROT_READ | PROT_WRITE,
-                      MAP_SHARED, drm_fd, map_dumb.offset);
+  auto mmapped =
+      mmap(nullptr, create_dumb.size, PROT_READ | PROT_WRITE, MAP_SHARED, drm_fd, map_dumb.offset);
   if (mmapped == MAP_FAILED) {
     perror("Failed to mmap()");
     return nullptr;
   }
   surface->mmapped_buffer_ = static_cast<uint8_t*>(mmapped);
+  printf("Framebuffer of size %llu allocated @ %p\n", create_dumb.size, surface->mmapped_buffer_);
   return surface;
 }
 
@@ -150,23 +153,55 @@
 }
 
 bool MinuiBackendDrm::DrmEnableCrtc(int drm_fd, drmModeCrtc* crtc,
-                                    const std::unique_ptr<GRSurfaceDrm>& surface) {
+                                    const std::unique_ptr<GRSurfaceDrm>& surface,
+                                    uint32_t* connector_id) {
   if (drmModeSetCrtc(drm_fd, crtc->crtc_id, surface->fb_id, 0, 0,  // x,y
-                     &main_monitor_connector->connector_id,
-                     1,  // connector_count
-                     &main_monitor_crtc->mode) != 0) {
-    perror("Failed to drmModeSetCrtc");
+                     connector_id, 1,                              // connector_count
+                     &crtc->mode) != 0) {
+    fprintf(stderr, "Failed to drmModeSetCrtc(%d)\n", *connector_id);
     return false;
   }
+
   return true;
 }
 
 void MinuiBackendDrm::Blank(bool blank) {
-  if (blank) {
-    DrmDisableCrtc(drm_fd, main_monitor_crtc);
-  } else {
-    DrmEnableCrtc(drm_fd, main_monitor_crtc, GRSurfaceDrms[current_buffer]);
+  Blank(blank, DRM_MAIN);
+}
+
+void MinuiBackendDrm::Blank(bool blank, DrmConnector index) {
+  const auto* drmInterface = &drm[DRM_MAIN];
+
+  switch (index) {
+    case DRM_MAIN:
+      drmInterface = &drm[DRM_MAIN];
+      break;
+    case DRM_SEC:
+      drmInterface = &drm[DRM_SEC];
+      break;
+    default:
+      fprintf(stderr, "Invalid index: %d\n", index);
+      return;
   }
+
+  if (!drmInterface->monitor_connector) {
+    fprintf(stderr, "Unsupported. index = %d\n", index);
+    return;
+  }
+
+  if (blank) {
+    DrmDisableCrtc(drm_fd, drmInterface->monitor_crtc);
+  } else {
+    DrmEnableCrtc(drm_fd, drmInterface->monitor_crtc,
+                  drmInterface->GRSurfaceDrms[drmInterface->current_buffer],
+                  &drmInterface->monitor_connector->connector_id);
+
+    active_display = index;
+  }
+}
+
+bool MinuiBackendDrm::HasMultipleConnectors() {
+  return (drm[DRM_SEC].GRSurfaceDrms[0] && drm[DRM_SEC].GRSurfaceDrms[1]);
 }
 
 static drmModeCrtc* find_crtc_for_connector(int fd, drmModeRes* resources,
@@ -207,18 +242,21 @@
   return nullptr;
 }
 
-static drmModeConnector* find_used_connector_by_type(int fd, drmModeRes* resources, unsigned type) {
+std::vector<drmModeConnector*> find_used_connector_by_type(int fd, drmModeRes* resources,
+                                                           unsigned type) {
+  std::vector<drmModeConnector*> drmConnectors;
   for (int i = 0; i < resources->count_connectors; i++) {
     drmModeConnector* connector = drmModeGetConnector(fd, resources->connectors[i]);
     if (connector) {
       if ((connector->connector_type == type) && (connector->connection == DRM_MODE_CONNECTED) &&
           (connector->count_modes > 0)) {
-        return connector;
+        drmConnectors.push_back(connector);
+      } else {
+        drmModeFreeConnector(connector);
       }
-      drmModeFreeConnector(connector);
     }
   }
-  return nullptr;
+  return drmConnectors;
 }
 
 static drmModeConnector* find_first_connected_connector(int fd, drmModeRes* resources) {
@@ -236,8 +274,7 @@
   return nullptr;
 }
 
-drmModeConnector* MinuiBackendDrm::FindMainMonitor(int fd, drmModeRes* resources,
-                                                   uint32_t* mode_index) {
+bool MinuiBackendDrm::FindAndSetMonitor(int fd, drmModeRes* resources) {
   /* Look for LVDS/eDP/DSI connectors. Those are the main screens. */
   static constexpr unsigned kConnectorPriority[] = {
     DRM_MODE_CONNECTOR_LVDS,
@@ -245,30 +282,41 @@
     DRM_MODE_CONNECTOR_DSI,
   };
 
-  drmModeConnector* main_monitor_connector = nullptr;
-  unsigned i = 0;
-  do {
-    main_monitor_connector = find_used_connector_by_type(fd, resources, kConnectorPriority[i]);
-    i++;
-  } while (!main_monitor_connector && i < arraysize(kConnectorPriority));
-
-  /* If we didn't find a connector, grab the first one that is connected. */
-  if (!main_monitor_connector) {
-    main_monitor_connector = find_first_connected_connector(fd, resources);
-  }
-
-  /* If we still didn't find a connector, give up and return. */
-  if (!main_monitor_connector) return nullptr;
-
-  *mode_index = 0;
-  for (int modes = 0; modes < main_monitor_connector->count_modes; modes++) {
-    if (main_monitor_connector->modes[modes].type & DRM_MODE_TYPE_PREFERRED) {
-      *mode_index = modes;
-      break;
+  std::vector<drmModeConnector*> drmConnectors;
+  for (int i = 0; i < arraysize(kConnectorPriority) && drmConnectors.size() < DRM_MAX; i++) {
+    auto connectors = find_used_connector_by_type(fd, resources, kConnectorPriority[i]);
+    for (auto connector : connectors) {
+      drmConnectors.push_back(connector);
+      if (drmConnectors.size() >= DRM_MAX) break;
     }
   }
 
-  return main_monitor_connector;
+  /* If we didn't find a connector, grab the first one that is connected. */
+  if (drmConnectors.empty()) {
+    drmModeConnector* connector = find_first_connected_connector(fd, resources);
+    if (connector) {
+      drmConnectors.push_back(connector);
+    }
+  }
+
+  for (int drm_index = 0; drm_index < drmConnectors.size(); drm_index++) {
+    drm[drm_index].monitor_connector = drmConnectors[drm_index];
+
+    drm[drm_index].selected_mode = 0;
+    for (int modes = 0; modes < drmConnectors[drm_index]->count_modes; modes++) {
+      printf("Display Mode %d resolution: %d x %d @ %d FPS\n", modes,
+             drmConnectors[drm_index]->modes[modes].hdisplay,
+             drmConnectors[drm_index]->modes[modes].vdisplay,
+             drmConnectors[drm_index]->modes[modes].vrefresh);
+      if (drmConnectors[drm_index]->modes[modes].type & DRM_MODE_TYPE_PREFERRED) {
+        printf("Choosing display mode #%d\n", modes);
+        drm[drm_index].selected_mode = modes;
+        break;
+      }
+    }
+  }
+
+  return drmConnectors.size() > 0;
 }
 
 void MinuiBackendDrm::DisableNonMainCrtcs(int fd, drmModeRes* resources, drmModeCrtc* main_crtc) {
@@ -319,46 +367,49 @@
     return nullptr;
   }
 
-  uint32_t selected_mode;
-  main_monitor_connector = FindMainMonitor(drm_fd, res, &selected_mode);
-  if (!main_monitor_connector) {
-    fprintf(stderr, "Failed to find main_monitor_connector\n");
+  if (!FindAndSetMonitor(drm_fd, res)) {
+    fprintf(stderr, "Failed to find main monitor_connector\n");
     drmModeFreeResources(res);
-    close(drm_fd);
     return nullptr;
   }
 
-  main_monitor_crtc = find_crtc_for_connector(drm_fd, res, main_monitor_connector);
-  if (!main_monitor_crtc) {
-    fprintf(stderr, "Failed to find main_monitor_crtc\n");
-    drmModeFreeResources(res);
-    close(drm_fd);
-    return nullptr;
+  for (int i = 0; i < DRM_MAX; i++) {
+    if (drm[i].monitor_connector) {
+      drm[i].monitor_crtc = find_crtc_for_connector(drm_fd, res, drm[i].monitor_connector);
+      if (!drm[i].monitor_crtc) {
+        fprintf(stderr, "Failed to find monitor_crtc, drm index=%d\n", i);
+        drmModeFreeResources(res);
+        return nullptr;
+      }
+
+      drm[i].monitor_crtc->mode = drm[i].monitor_connector->modes[drm[i].selected_mode];
+
+      int width = drm[i].monitor_crtc->mode.hdisplay;
+      int height = drm[i].monitor_crtc->mode.vdisplay;
+
+      drm[i].GRSurfaceDrms[0] = GRSurfaceDrm::Create(drm_fd, width, height);
+      drm[i].GRSurfaceDrms[1] = GRSurfaceDrm::Create(drm_fd, width, height);
+      if (!drm[i].GRSurfaceDrms[0] || !drm[i].GRSurfaceDrms[1]) {
+        fprintf(stderr, "Failed to create GRSurfaceDrm, drm index=%d\n", i);
+        drmModeFreeResources(res);
+        return nullptr;
+      }
+
+      drm[i].current_buffer = 0;
+    }
   }
 
-  DisableNonMainCrtcs(drm_fd, res, main_monitor_crtc);
-
-  main_monitor_crtc->mode = main_monitor_connector->modes[selected_mode];
-
-  int width = main_monitor_crtc->mode.hdisplay;
-  int height = main_monitor_crtc->mode.vdisplay;
+  DisableNonMainCrtcs(drm_fd, res, drm[DRM_MAIN].monitor_crtc);
 
   drmModeFreeResources(res);
 
-  GRSurfaceDrms[0] = GRSurfaceDrm::Create(drm_fd, width, height);
-  GRSurfaceDrms[1] = GRSurfaceDrm::Create(drm_fd, width, height);
-  if (!GRSurfaceDrms[0] || !GRSurfaceDrms[1]) {
-    return nullptr;
-  }
-
-  current_buffer = 0;
-
   // We will likely encounter errors in the backend functions (i.e. Flip) if EnableCrtc fails.
-  if (!DrmEnableCrtc(drm_fd, main_monitor_crtc, GRSurfaceDrms[1])) {
+  if (!DrmEnableCrtc(drm_fd, drm[DRM_MAIN].monitor_crtc, drm[DRM_MAIN].GRSurfaceDrms[1],
+                     &drm[DRM_MAIN].monitor_connector->connector_id)) {
     return nullptr;
   }
 
-  return GRSurfaceDrms[0].get();
+  return drm[DRM_MAIN].GRSurfaceDrms[0].get();
 }
 
 static void page_flip_complete(__unused int fd,
@@ -370,10 +421,19 @@
 }
 
 GRSurface* MinuiBackendDrm::Flip() {
+  GRSurface* surface = NULL;
+  DrmInterface* current_drm = &drm[active_display];
   bool ongoing_flip = true;
-  if (drmModePageFlip(drm_fd, main_monitor_crtc->crtc_id, GRSurfaceDrms[current_buffer]->fb_id,
+
+  if (!current_drm->monitor_connector) {
+    fprintf(stderr, "Unsupported. active_display = %d\n", active_display);
+    return nullptr;
+  }
+
+  if (drmModePageFlip(drm_fd, current_drm->monitor_crtc->crtc_id,
+                      current_drm->GRSurfaceDrms[current_drm->current_buffer]->fb_id,
                       DRM_MODE_PAGE_FLIP_EVENT, &ongoing_flip) != 0) {
-    perror("Failed to drmModePageFlip");
+    fprintf(stderr, "Failed to drmModePageFlip, active_display=%d", active_display);
     return nullptr;
   }
 
@@ -399,14 +459,19 @@
     }
   }
 
-  current_buffer = 1 - current_buffer;
-  return GRSurfaceDrms[current_buffer].get();
+  current_drm->current_buffer = 1 - current_drm->current_buffer;
+  surface = current_drm->GRSurfaceDrms[current_drm->current_buffer].get();
+  return surface;
 }
 
 MinuiBackendDrm::~MinuiBackendDrm() {
-  DrmDisableCrtc(drm_fd, main_monitor_crtc);
-  drmModeFreeCrtc(main_monitor_crtc);
-  drmModeFreeConnector(main_monitor_connector);
+  for (int i = 0; i < DRM_MAX; i++) {
+    if (drm[i].monitor_connector) {
+      DrmDisableCrtc(drm_fd, drm[i].monitor_crtc);
+      drmModeFreeCrtc(drm[i].monitor_crtc);
+      drmModeFreeConnector(drm[i].monitor_connector);
+    }
+  }
   close(drm_fd);
   drm_fd = -1;
 }
diff --git a/minui/graphics_drm.h b/minui/graphics_drm.h
index 57ba39b..a8c9886 100644
--- a/minui/graphics_drm.h
+++ b/minui/graphics_drm.h
@@ -59,16 +59,24 @@
   GRSurface* Init() override;
   GRSurface* Flip() override;
   void Blank(bool) override;
+  void Blank(bool blank, DrmConnector index) override;
+  bool HasMultipleConnectors() override;
 
  private:
   void DrmDisableCrtc(int drm_fd, drmModeCrtc* crtc);
-  bool DrmEnableCrtc(int drm_fd, drmModeCrtc* crtc, const std::unique_ptr<GRSurfaceDrm>& surface);
+  bool DrmEnableCrtc(int drm_fd, drmModeCrtc* crtc, const std::unique_ptr<GRSurfaceDrm>& surface,
+                     uint32_t* conntcors);
   void DisableNonMainCrtcs(int fd, drmModeRes* resources, drmModeCrtc* main_crtc);
-  drmModeConnector* FindMainMonitor(int fd, drmModeRes* resources, uint32_t* mode_index);
+  bool FindAndSetMonitor(int fd, drmModeRes* resources);
 
-  std::unique_ptr<GRSurfaceDrm> GRSurfaceDrms[2];
-  int current_buffer{ 0 };
-  drmModeCrtc* main_monitor_crtc{ nullptr };
-  drmModeConnector* main_monitor_connector{ nullptr };
+  struct DrmInterface {
+    std::unique_ptr<GRSurfaceDrm> GRSurfaceDrms[2];
+    int current_buffer{ 0 };
+    drmModeCrtc* monitor_crtc{ nullptr };
+    drmModeConnector* monitor_connector{ nullptr };
+    uint32_t selected_mode{ 0 };
+  } drm[DRM_MAX];
+
   int drm_fd{ -1 };
+  DrmConnector active_display = DRM_MAIN;
 };
diff --git a/minui/graphics_fbdev.cpp b/minui/graphics_fbdev.cpp
index 353a070..4a7d325 100644
--- a/minui/graphics_fbdev.cpp
+++ b/minui/graphics_fbdev.cpp
@@ -39,25 +39,17 @@
 }
 
 void MinuiBackendFbdev::Blank(bool blank) {
-#if defined(TW_NO_SCREEN_BLANK) && defined(TW_BRIGHTNESS_PATH) && defined(TW_MAX_BRIGHTNESS)
-    int fd;
-    char brightness[4];
-    snprintf(brightness, 4, "%03d", TW_MAX_BRIGHTNESS/2);
+  int ret = ioctl(fb_fd, FBIOBLANK, blank ? FB_BLANK_POWERDOWN : FB_BLANK_UNBLANK);
+  if (ret < 0) perror("ioctl(): blank");
+}
 
-    fd = open(TW_BRIGHTNESS_PATH, O_RDWR);
-    if (fd < 0) {
-        perror("cannot open LCD backlight");
-        return;
-    }
-    write(fd, blank ? "000" : brightness, 3);
-    close(fd);
-#else
-    int ret;
+void MinuiBackendFbdev::Blank(bool blank, DrmConnector index) {
+  fprintf(stderr, "Unsupported multiple connectors, blank = %d, index = %d\n", blank, index);
+}
 
-    ret = ioctl(fb_fd, FBIOBLANK, blank ? FB_BLANK_POWERDOWN : FB_BLANK_UNBLANK);
-    if (ret < 0)
-        perror("ioctl(): blank");
-#endif
+bool MinuiBackendFbdev::HasMultipleConnectors() {
+  fprintf(stderr, "Unsupported multiple connectors\n");
+  return false;
 }
 
 void MinuiBackendFbdev::SetDisplayedFramebuffer(size_t n) {
@@ -148,8 +140,6 @@
   SetDisplayedFramebuffer(0);
 
   printf("framebuffer: %d (%zu x %zu)\n", fb_fd.get(), gr_draw->width, gr_draw->height);
-
-  Blank(true);
   Blank(false);
 
   return gr_draw;
diff --git a/minui/graphics_fbdev.h b/minui/graphics_fbdev.h
index 596ba74..c772428 100644
--- a/minui/graphics_fbdev.h
+++ b/minui/graphics_fbdev.h
@@ -56,6 +56,8 @@
   GRSurface* Init() override;
   GRSurface* Flip() override;
   void Blank(bool) override;
+  void Blank(bool blank, DrmConnector index) override;
+  bool HasMultipleConnectors() override;
 
  private:
   void SetDisplayedFramebuffer(size_t n);
diff --git a/minui/include/minui/minui.h b/minui/include/minui/minui.h
old mode 100755
new mode 100644
index ecc0c9e..2353ed3
--- a/minui/include/minui/minui.h
+++ b/minui/include/minui/minui.h
@@ -15,10 +15,6 @@
  */
 
 #pragma once
-#ifndef _MINUI_H_
-#define _MINUI_H_
-
-#ifndef TW_USE_MINUI_21
 
 #include <stdint.h>
 #include <stdlib.h>
@@ -106,12 +102,22 @@
   RGBX = 2,
   BGRA = 3,
   ARGB = 4,
+  RGBA = 5, // LSB Alpha
 };
 
-// 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<GraphicsBackend> backends);
 
 // Frees the allocated resources. The function is idempotent, and safe to be called if gr_init()
 // didn't finish successfully.
@@ -122,19 +128,14 @@
 
 void gr_flip();
 void gr_fb_blank(bool blank);
+void gr_fb_blank(bool blank, int index);
+bool gr_has_multiple_connectors();
 
 // Clears entire surface to current color.
 void gr_clear();
 void gr_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a);
 void gr_fill(int x1, int y1, int x2, int y2);
 
-void gr_texticon(int x, int y, GRSurface* icon);
-#ifdef TW_NO_MINUI_CUSTOM_FONTS
-void gr_text(int x, int y, const char *s, bool bold);
-int gr_measure(const char *s);
-void gr_font_size(int *x, int *y);
-void gr_set_font(__attribute__ ((unused))const char* name);
-#else
 void gr_texticon(int x, int y, const GRSurface* icon);
 
 const GRFont* gr_sys_font();
@@ -144,7 +145,7 @@
 int gr_measure(const GRFont* font, const char* s);
 // Returns -1 if font is nullptr.
 int gr_font_size(const GRFont* font, int* x, int* y);
-#endif
+
 void gr_blit(const GRSurface* source, int sx, int sy, int w, int h, int dx, int dy);
 unsigned int gr_get_width(const GRSurface* surface);
 unsigned int gr_get_height(const GRSurface* surface);
@@ -161,31 +162,17 @@
 
 struct input_event;
 
-#ifdef TW_USE_MINUI_WITH_DATA
-typedef int (*ev_callback)(int fd, uint32_t epevents, void* data);
-typedef int (*ev_set_key_callback)(int code, int value, void* data);
-
-int ev_init(ev_callback input_cb, void* data);
-int ev_add_fd(int fd, ev_callback cb, void* data);
-int ev_sync_key_state(ev_set_key_callback set_key_cb, void* data);
-#else
 using ev_callback = std::function<int(int fd, uint32_t epevents)>;
 using ev_set_key_callback = std::function<int(int code, int value)>;
+using ev_set_sw_callback = std::function<int(int code, int value)>;
 
-#ifdef TW_USE_MINUI_WITH_OPTIONAL_TOUCH_EVENTS
 int ev_init(ev_callback input_cb, bool allow_touch_inputs = false);
 void ev_exit();
 int ev_add_fd(android::base::unique_fd&& fd, ev_callback cb);
 void ev_iterate_available_keys(const std::function<void(int)>& f);
 void ev_iterate_touch_inputs(const std::function<void(int)>& action);
-#else
-int ev_init(ev_callback input_cb);
-#endif
-int ev_add_fd(int fd, ev_callback cb);
 int ev_sync_key_state(const ev_set_key_callback& set_key_cb);
-#endif
-void ev_exit();
-void ev_iterate_available_keys(const std::function<void(int)>& f);
+int ev_sync_sw_state(const ev_set_sw_callback& set_sw_cb);
 
 // 'timeout' has the same semantics as poll(2).
 //    0 : don't block
@@ -220,9 +207,7 @@
 // should have a 'Frames' text chunk whose value is the number of
 // frames this image represents.  The pixel data itself is interlaced
 // by row.
-int res_create_multi_display_surface(const char* name, int* frames,
-                                     int* fps, GRSurface*** pSurface);
-int res_create_multi_display_surface(const char* name, int* frames,
+int res_create_multi_display_surface(const char* name, int* frames, int* fps,
                                      GRSurface*** pSurface);
 
 // Load a single alpha surface from a grayscale PNG image.
@@ -243,95 +228,3 @@
 // Free a surface allocated by any of the res_create_*_surface()
 // functions.
 void res_free_surface(GRSurface* surface);
-#else //ifndef TW_USE_MINUI_21
-
-// This the old minui21/minui.h for compatibility with building TWRP
-// in pre 6.0 trees.
-
-#include <stdbool.h>
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-typedef void* gr_surface;
-typedef unsigned short gr_pixel;
-
-int gr_init(void);
-void gr_exit(void);
-
-int gr_fb_width(void);
-int gr_fb_height(void);
-gr_pixel *gr_fb_data(void);
-void gr_flip(void);
-void gr_fb_blank(bool blank);
-
-void gr_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a);
-void gr_fill(int x1, int y1, int x2, int y2);
-
-// system/core/charger uses different gr_print signatures in diferent
-// Android versions, either with or without int bold.
-int gr_text(int x, int y, const char *s, ...);
-int gr_text_impl(int x, int y, const char *s, int bold);
-
- void gr_texticon(int x, int y, gr_surface icon);
-int gr_measure(const char *s);
-void gr_font_size(int *x, int *y);
-void gr_get_memory_surface(gr_surface);
-
-void gr_blit(gr_surface source, int sx, int sy, int w, int h, int dx, int dy);
-unsigned int gr_get_width(gr_surface surface);
-unsigned int gr_get_height(gr_surface surface);
-
-// input event structure, include <linux/input.h> for the definition.
-// see http://www.mjmwired.net/kernel/Documentation/input/ for info.
-struct input_event;
-
-typedef int (*ev_callback)(int fd, uint32_t epevents, void *data);
-typedef int (*ev_set_key_callback)(int code, int value, void *data);
-
-int ev_init(ev_callback input_cb, void *data);
-void ev_exit(void);
-int ev_add_fd(int fd, ev_callback cb, void *data);
-int ev_sync_key_state(ev_set_key_callback set_key_cb, void *data);
-
-/* timeout has the same semantics as for poll
- *    0 : don't block
- *  < 0 : block forever
- *  > 0 : block for 'timeout' milliseconds
- */
-int ev_wait(int timeout);
-
-int ev_get_input(int fd, uint32_t epevents, struct input_event *ev);
-void ev_dispatch(void);
-int ev_get_epollfd(void);
-
-// Resources
-
-// Returns 0 if no error, else negative.
-int res_create_surface(const char* name, gr_surface* pSurface);
-
-// Load an array of display surfaces from a single PNG image.  The PNG
-// should have a 'Frames' text chunk whose value is the number of
-// frames this image represents.  The pixel data itself is interlaced
-// by row.
-int res_create_multi_display_surface(const char* name,
-                                     int* frames, gr_surface** pSurface);
-
-int res_create_localized_surface(const char* name, gr_surface* pSurface);
-void res_free_surface(gr_surface surface);
-static inline int res_create_display_surface(const char* name, gr_surface* pSurface) {
-    return res_create_surface(name, pSurface);
-}
-
-// These are new graphics functions from 5.0 that were not available in
-// 4.4 that are required by charger and healthd
-void gr_clear();
-
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif // ifndef TW_USE_MINUI_21
-#endif // ifndef _MINUI_H_
diff --git a/minui/resources.cpp b/minui/resources.cpp
old mode 100755
new mode 100644
index fe47df1..1521c8f
--- a/minui/resources.cpp
+++ b/minui/resources.cpp
@@ -153,32 +153,57 @@
                                int width) {
   const uint8_t* ip = input_row;
   uint8_t* op = output_row;
+  PixelFormat pixel_format = gr_pixel_format();
 
   switch (channels) {
     case 1:
       // expand gray level to RGBX
       for (int x = 0; x < width; ++x) {
-        *op++ = *ip;
-        *op++ = *ip;
-        *op++ = *ip;
-        *op++ = 0xff;
+        if (pixel_format == PixelFormat::RGBA) {
+          *op++ = 0xff;
+          *op++ = *ip;
+          *op++ = *ip;
+          *op++ = *ip;
+        } else {
+          *op++ = *ip;
+          *op++ = *ip;
+          *op++ = *ip;
+          *op++ = 0xff;
+        }
         ip++;
       }
       break;
 
     case 3:
-      // expand RGBA to RGBX
       for (int x = 0; x < width; ++x) {
-        *op++ = *ip++;
-        *op++ = *ip++;
-        *op++ = *ip++;
-        *op++ = 0xff;
+        // expand RGBA to RGBX
+        if (pixel_format == PixelFormat::RGBA) {
+            *op++ = 0xff;
+            *op++ = *ip++;
+            *op++ = *ip++;
+            *op++ = *ip++;
+        } else {
+            *op++ = *ip++;
+            *op++ = *ip++;
+            *op++ = *ip++;
+            *op++ = 0xff;
+        }
       }
       break;
 
     case 4:
-      // copy RGBA to RGBX
-      memcpy(output_row, input_row, width * 4);
+      if (pixel_format == PixelFormat::RGBA) {
+        for (int x = 0; x < width; ++x) {
+            *op++ = *(ip + 3);
+            *op++ = *ip++;
+            *op++ = *ip++;
+            *op++ = *ip++;
+            ip++;
+        }
+      } else {
+        // copy RGBA to RGBX
+        memcpy(output_row, input_row, width * 4);
+      }
       break;
   }
 }
@@ -201,6 +226,8 @@
   PixelFormat pixel_format = gr_pixel_format();
   if (pixel_format == PixelFormat::ARGB || pixel_format == PixelFormat::BGRA) {
     png_set_bgr(png_ptr);
+  } else if (pixel_format == PixelFormat::RGBA) {
+    png_set_swap_alpha(png_ptr);
   }
 
   for (png_uint_32 y = 0; y < height; ++y) {
@@ -273,6 +300,8 @@
 
   if (gr_pixel_format() == PixelFormat::ARGB || gr_pixel_format() == PixelFormat::BGRA) {
     png_set_bgr(png_ptr);
+  } else if (gr_pixel_format() == PixelFormat::RGBA) {
+    png_set_swap_alpha(png_ptr);
   }
 
   for (png_uint_32 y = 0; y < height; ++y) {
@@ -297,12 +326,6 @@
   return result;
 }
 
-int res_create_multi_display_surface(const char* name, int* frames,
-        GRSurface*** pSurface) {
-    int fps = 0;
-    return res_create_multi_display_surface(name, frames, &fps, pSurface);
-}
-
 int res_create_alpha_surface(const char* name, GRSurface** pSurface) {
   *pSurface = nullptr;
 
@@ -322,11 +345,6 @@
     return -8;
   }
 
-  PixelFormat pixel_format = gr_pixel_format();
-  if (pixel_format == PixelFormat::ARGB || pixel_format == PixelFormat::BGRA) {
-    png_set_bgr(png_ptr);
-  }
-
   for (png_uint_32 y = 0; y < height; ++y) {
     uint8_t* p_row = surface->data() + y * surface->row_bytes;
     png_read_row(png_ptr, p_row, nullptr);