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>
This commit is contained in:
Vladimir Oltean
2019-03-18 11:39:38 -05:00
committed by Ethan Yonker
parent 19874f1469
commit d32b7ebf20
7 changed files with 225 additions and 54 deletions
-27
View File
@@ -293,7 +293,6 @@ static GRSurface* fbdev_flip(minui_backend* backend __unused) {
ucfb_vaddr[idx + 2] = tmp;
}
#endif
#ifndef BOARD_HAS_FLIPPED_SCREEN
if (double_buffered) {
// Copy from the in-memory surface to the framebuffer.
memcpy(gr_framebuffer[1-displayed_buffer].data, gr_draw->data,
@@ -304,32 +303,6 @@ static GRSurface* fbdev_flip(minui_backend* backend __unused) {
memcpy(gr_framebuffer[0].data, gr_draw->data,
gr_draw->height * gr_draw->row_bytes);
}
#else
int gr_active_fb = 0;
if (double_buffered)
gr_active_fb = 1-displayed_buffer;
/* flip buffer 180 degrees for devices with physically inverted screens */
unsigned int row_pixels = gr_draw->row_bytes / gr_framebuffer[0].pixel_bytes;
if (gr_framebuffer[0].pixel_bytes == 4) {
for (unsigned int y = 0; y < gr_draw->height; ++y) {
uint32_t* dst = reinterpret_cast<uint32_t*>(gr_framebuffer[gr_active_fb].data) + y * row_pixels;
uint32_t* src = reinterpret_cast<uint32_t*>(gr_draw->data) + (gr_draw->height - y - 1) * row_pixels + gr_draw->width;
for (unsigned int x = 0; x < gr_draw->width; ++x)
*(dst++) = *(--src);
}
} else {
for (unsigned int y = 0; y < gr_draw->height; ++y) {
uint16_t* dst = reinterpret_cast<uint16_t*>(gr_framebuffer[gr_active_fb].data) + y * row_pixels;
uint16_t* src = reinterpret_cast<uint16_t*>(gr_draw->data) + (gr_draw->height - y - 1) * row_pixels + gr_draw->width;
for (unsigned int x = 0; x < gr_draw->width; ++x)
*(dst++) = *(--src);
}
}
if (double_buffered)
set_displayed_framebuffer(1-displayed_buffer);
#endif
return gr_draw;
}