TW_ROTATION: add flag to handle hardware-rotated display panels

* The existence of TW_ROTATION that implements this feature at the
  level of calls to libpixelflinger API closely mirrors the existence of
  ro.sf.hwrotation for surfaceflinger in LineageOS.
* A brute-force approach was previously attempted via the
  BOARD_HAS_FLIPPED_SCREEN makefile flag. That code iterated over the
  active display surface in a double-buffered setup, and performed a
  "smart" memcpy from the UI drawing surface (gr_draw) onto the display
  surface. The problem was that, without heavy loop optimizations, that
  code could have never scaled for 90 and 270 degree rotation.
  I tried and you could literally see the for loop with the naked eye
  while the display surface was updating.
* That code is now gone, but support for BOARD_HAS_FLIPPED_SCREEN := true
  is still there (now means TW_ROTATION := 180).
* This patch relies on the assumption that it is impossibly difficult
  and non-portable to rotate whole framebuffer display surfaces, in a
  way that is not dependent upon the graphics backend (adf, fbdev, drm,
  overlay etc). Therefore, it identifies the rendering primitives that
  the TWRP graphics stack exposes to the GUI application above, and
  implements hwrotation inside each of those calls instead:
    - gr_line(), gr_fill() - 2D geometric shapes (lines, rectangles)
    - gr_blit() - graphical image resources
    - gr_ttf_textExWH() - font rendering
    - gr_fb_width(), gr_fb_height() - framebuffer resolution
* The gist is to keep the backend and framebuffer (dimensions, row size
  etc) unchanged (because making changes there is asking for trouble),
  but present an altogether different reality to the calling API,
  according to the compile-time constant TW_ROTATION.
* All (x, y) API coordinates and shapes are transformed before being
  actually rendered as (x_disp, y_disp) display coordinates.
* With TW_ROTATION := 90 or 270 you can turn a landscape device into
  a portrait one, because the GUI is fooled by the reversed dimensions
  reported by gr_fb_width() and gr_fb_height() and renders the UI as
  for a different device.
* For blit and text rendering operations, figuring out the transformed
  coordinates in display space is not enough, as the surfaces that are
  to be rendered have to be rotated themselves. This is handled by
  allocating an intermediary rotated surface on each rendering
  operation (not ideal), so the code with the intermediary surface
  is compiled out for the TW_ROTATION := 0 case.
* This is still not as bad as rotating the whole framebuffer though, and
  on a msm8976 device the performance hit is not even noticeable (for
  software rendering).
* Currently there is no attempt to make a connection between the
  TW_ROTATION and the { RECOVERY_TOUCHSCREEN_SWAP_XY,
  RECOVERY_TOUCHSCREEN_FLIP_X, RECOVERY_TOUCHSCREEN_FLIP_Y } settings.

Change-Id: Ic8966ad5360c8a499649fdb16e242286640fd992
Signed-off-by: Vladimir Oltean <olteanv@gmail.com>
diff --git a/minuitwrp/graphics_utils.cpp b/minuitwrp/graphics_utils.cpp
index 67c836e..c591e53 100644
--- a/minuitwrp/graphics_utils.cpp
+++ b/minuitwrp/graphics_utils.cpp
@@ -19,6 +19,7 @@
 #include <png.h>
 #include <pixelflinger/pixelflinger.h>
 #include <linux/fb.h>
+#include <string.h>
 
 #include "minui.h"
 
@@ -121,3 +122,47 @@
         fclose(fp);
     return res;
 }
+
+#define MATRIX_ELEMENT(matrix, row, col, row_size, elem_size) \
+    (((uint8_t*) (matrix)) + (((row) * (elem_size)) * (row_size)) + ((col) * (elem_size)))
+
+#define DO_MATRIX_ROTATION(bits_per_pixel, bytes_per_pixel)                   \
+{                                                                             \
+    for (size_t y = 0; y < src->height; y++) {                                \
+        for (size_t x = 0; x < src->width; x++) {                             \
+            /* output pointer in dst->data */                                 \
+            uint##bits_per_pixel##_t       *op;                               \
+            /* input pointer from src->data */                                \
+            const uint##bits_per_pixel##_t *ip;                               \
+            /* Display coordinates (in dst) corresponding to (x, y) in src */ \
+            size_t x_disp = ROTATION_X_DISP(x, y, dst);                     \
+            size_t y_disp = ROTATION_Y_DISP(x, y, dst);                     \
+                                                                              \
+            ip = (const uint##bits_per_pixel##_t*)                            \
+                 MATRIX_ELEMENT(src->data, y, x,                              \
+                                src->stride, bytes_per_pixel);                \
+            op = (uint##bits_per_pixel##_t*)                                  \
+                 MATRIX_ELEMENT(dst->data, y_disp, x_disp,                    \
+                                dst->stride, bytes_per_pixel);                \
+            *op = *ip;                                                        \
+        }                                                                     \
+    }                                                                         \
+}
+
+void surface_ROTATION_transform(gr_surface dst_ptr, const gr_surface src_ptr,
+                                  size_t num_bytes_per_pixel)
+{
+    GGLSurface *dst = (GGLSurface*) dst_ptr;
+    const GGLSurface *src = (GGLSurface*) src_ptr;
+
+    /* Handle duplicated code via a macro.
+     * This is currently used for rotating surfaces of graphical resources
+     * (32-bit pixel format) and of font glyphs (8-bit pixel format).
+     * If you need to add handling of other pixel formats feel free to do so.
+     */
+    if (num_bytes_per_pixel == 4) {
+        DO_MATRIX_ROTATION(32, 4);
+    } else if (num_bytes_per_pixel == 1) {
+        DO_MATRIX_ROTATION(8, 1);
+    }
+}