From d32b7ebf20217f5053aaf0597cc1a7dcf86fc2a5 Mon Sep 17 00:00:00 2001 From: Vladimir Oltean Date: Tue, 3 Jul 2018 00:04:03 +0300 Subject: 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 --- minuitwrp/truetype.cpp | 50 +++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 47 insertions(+), 3 deletions(-) (limited to 'minuitwrp/truetype.cpp') diff --git a/minuitwrp/truetype.cpp b/minuitwrp/truetype.cpp index 3e5f70719..0416b0e9f 100644 --- a/minuitwrp/truetype.cpp +++ b/minuitwrp/truetype.cpp @@ -14,6 +14,8 @@ #include #include +// For std::min and std::max +#include #define STRING_CACHE_MAX_ENTRIES 400 #define STRING_CACHE_TRUNCATE_ENTRIES 150 @@ -697,10 +699,14 @@ int gr_ttf_maxExW(const char *s, void *font, int max_width) return max_bytes; } -int gr_ttf_textExWH(void *context, int x, int y, const char *s, void *pFont, int max_width, int max_height) +int gr_ttf_textExWH(void *context, int x, int y, + const char *s, void *pFont, + int max_width, int max_height, + const gr_surface gr_draw_surface) { GGLContext *gl = (GGLContext *)context; TrueTypeFont *font = (TrueTypeFont *)pFont; + const GRSurface *gr_draw = (const GRSurface*) gr_draw_surface; // not actualy max width, but max_width + x if(max_width != -1) @@ -719,6 +725,21 @@ int gr_ttf_textExWH(void *context, int x, int y, const char *s, void *pFont, int return -1; } +#if TW_ROTATION != 0 + // Do not perform relatively expensive operation if not needed + GGLSurface string_surface_rotated; + string_surface_rotated.version = sizeof(string_surface_rotated); + // Skip the **(TW_ROTATION == 0)** || (TW_ROTATION == 180) check + // because we are under a TW_ROTATION != 0 conditional compilation statement + string_surface_rotated.width = (TW_ROTATION == 180) ? e->surface.width : e->surface.height; + string_surface_rotated.height = (TW_ROTATION == 180) ? e->surface.height : e->surface.width; + string_surface_rotated.stride = string_surface_rotated.width; + string_surface_rotated.format = e->surface.format; + // e->surface.format is GGL_PIXEL_FORMAT_A_8 (grayscale) + string_surface_rotated.data = (GGLubyte*) malloc(string_surface_rotated.stride * string_surface_rotated.height * 1); + surface_ROTATION_transform((gr_surface) &string_surface_rotated, (const gr_surface) &e->surface, 1); +#endif + int y_bottom = y + e->surface.height; int res = e->rendered_bytes; @@ -732,16 +753,39 @@ int gr_ttf_textExWH(void *context, int x, int y, const char *s, void *pFont, int } } + // Figuring out display coordinates works for TW_ROTATION == 0 too, + // and isn't as expensive as allocating and rotating another surface, + // so we do this anyway. + int x0_disp, y0_disp, x1_disp, y1_disp; + int l_disp, r_disp, t_disp, b_disp; + + x0_disp = ROTATION_X_DISP(x, y, gr_draw); + y0_disp = ROTATION_Y_DISP(x, y, gr_draw); + x1_disp = ROTATION_X_DISP(x + e->surface.width, y_bottom, gr_draw); + y1_disp = ROTATION_Y_DISP(x + e->surface.width, y_bottom, gr_draw); + l_disp = std::min(x0_disp, x1_disp); + r_disp = std::max(x0_disp, x1_disp); + t_disp = std::min(y0_disp, y1_disp); + b_disp = std::max(y0_disp, y1_disp); + +#if TW_ROTATION != 0 + gl->bindTexture(gl, &string_surface_rotated); +#else gl->bindTexture(gl, &e->surface); +#endif gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE); gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE); gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE); gl->enable(gl, GGL_TEXTURE_2D); - gl->texCoord2i(gl, -x, -y); - gl->recti(gl, x, y, x + e->surface.width, y_bottom); + gl->texCoord2i(gl, -l_disp, -t_disp); + gl->recti(gl, l_disp, t_disp, r_disp, b_disp); gl->disable(gl, GGL_TEXTURE_2D); +#if TW_ROTATION != 0 + free(string_surface_rotated.data); +#endif + pthread_mutex_unlock(&font->mutex); return res; } -- cgit v1.2.3