Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <stdbool.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <string.h> |
| 20 | #include <unistd.h> |
| 21 | |
| 22 | #include <fcntl.h> |
| 23 | #include <stdio.h> |
| 24 | |
| 25 | #include <sys/ioctl.h> |
| 26 | #include <sys/mman.h> |
| 27 | #include <sys/types.h> |
| 28 | |
| 29 | #include <linux/fb.h> |
| 30 | #include <linux/kd.h> |
| 31 | |
| 32 | #include <time.h> |
| 33 | |
Aaron Kling | 4eccd9a | 2019-06-30 12:20:09 -0500 | [diff] [blame] | 34 | #include <cutils/properties.h> |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 35 | #include <pixelflinger/pixelflinger.h> |
bigbiff | d81833a | 2021-01-17 11:06:57 -0500 | [diff] [blame] | 36 | #include "gui/placement.h" |
| 37 | #include "minuitwrp/minui.h" |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 38 | #include "graphics.h" |
Vladimir Oltean | d32b7eb | 2018-07-03 00:04:03 +0300 | [diff] [blame] | 39 | // For std::min and std::max |
| 40 | #include <algorithm> |
bigbiff | d81833a | 2021-01-17 11:06:57 -0500 | [diff] [blame] | 41 | #include "minuitwrp/truetype.hpp" |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 42 | |
| 43 | struct GRFont { |
| 44 | GRSurface* texture; |
| 45 | int cwidth; |
| 46 | int cheight; |
| 47 | }; |
| 48 | |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 49 | static minui_backend* gr_backend = NULL; |
| 50 | |
| 51 | static int overscan_percent = OVERSCAN_PERCENT; |
| 52 | static int overscan_offset_x = 0; |
| 53 | static int overscan_offset_y = 0; |
| 54 | |
| 55 | static unsigned char gr_current_r = 255; |
| 56 | static unsigned char gr_current_g = 255; |
| 57 | static unsigned char gr_current_b = 255; |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 58 | |
| 59 | GRSurface* gr_draw = NULL; |
| 60 | |
| 61 | static GGLContext *gr_context = 0; |
| 62 | GGLSurface gr_mem_surface; |
| 63 | static int gr_is_curr_clr_opaque = 0; |
| 64 | |
Aaron Kling | 4eccd9a | 2019-06-30 12:20:09 -0500 | [diff] [blame] | 65 | unsigned int gr_rotation = 0; |
| 66 | |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 67 | int gr_textEx_scaleW(int x, int y, const char *s, void* pFont, int max_width, int placement, int scale) |
| 68 | { |
| 69 | GGLContext *gl = gr_context; |
| 70 | void* vfont = pFont; |
| 71 | GRFont *font = (GRFont*) pFont; |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 72 | int y_scale = 0, measured_width, measured_height, new_height; |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 73 | |
| 74 | if (!s || strlen(s) == 0 || !font) |
| 75 | return 0; |
| 76 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 77 | measured_height = twrpTruetype::gr_ttf_getMaxFontHeight(font); |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 78 | |
| 79 | if (scale) { |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 80 | measured_width = twrpTruetype::gr_ttf_measureEx(s, vfont); |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 81 | if (measured_width > max_width) { |
| 82 | // Adjust font size down until the text fits |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 83 | void *new_font = twrpTruetype::gr_ttf_scaleFont(vfont, max_width, measured_width); |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 84 | if (!new_font) { |
| 85 | printf("gr_textEx_scaleW new_font is NULL\n"); |
| 86 | return 0; |
| 87 | } |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 88 | measured_width = twrpTruetype::gr_ttf_measureEx(s, new_font); |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 89 | // These next 2 lines adjust the y point based on the new font's height |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 90 | new_height = twrpTruetype::gr_ttf_getMaxFontHeight(new_font); |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 91 | y_scale = (measured_height - new_height) / 2; |
| 92 | vfont = new_font; |
| 93 | } |
| 94 | } else |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 95 | measured_width = twrpTruetype::gr_ttf_measureEx(s, vfont); |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 96 | |
| 97 | int x_adj = measured_width; |
| 98 | if (measured_width > max_width) |
| 99 | x_adj = max_width; |
| 100 | |
| 101 | if (placement != TOP_LEFT && placement != BOTTOM_LEFT && placement != TEXT_ONLY_RIGHT) { |
| 102 | if (placement == CENTER || placement == CENTER_X_ONLY) |
| 103 | x -= (x_adj / 2); |
| 104 | else |
| 105 | x -= x_adj; |
| 106 | } |
| 107 | |
| 108 | if (placement != TOP_LEFT && placement != TOP_RIGHT) { |
| 109 | if (placement == CENTER || placement == TEXT_ONLY_RIGHT) |
| 110 | y -= (measured_height / 2); |
| 111 | else if (placement == BOTTOM_LEFT || placement == BOTTOM_RIGHT) |
| 112 | y -= measured_height; |
| 113 | } |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 114 | return twrpTruetype::gr_ttf_textExWH(gl, x, y + y_scale, s, vfont, measured_width + x, -1, gr_draw); |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | void gr_clip(int x, int y, int w, int h) |
| 118 | { |
| 119 | GGLContext *gl = gr_context; |
Vladimir Oltean | d32b7eb | 2018-07-03 00:04:03 +0300 | [diff] [blame] | 120 | |
Aaron Kling | 4eccd9a | 2019-06-30 12:20:09 -0500 | [diff] [blame] | 121 | switch (gr_rotation) { |
| 122 | case 90: |
| 123 | gl->scissor(gl, gr_draw->width - y - h, x, h, w); |
| 124 | break; |
| 125 | case 180: |
| 126 | gl->scissor(gl, gr_draw->width - x - w, gr_draw->height - y - h, w, h); |
| 127 | break; |
| 128 | case 270: |
| 129 | gl->scissor(gl, y, gr_draw->height - x - w, h, w); |
| 130 | break; |
| 131 | default: |
| 132 | gl->scissor(gl, x, y, w, h); |
| 133 | break; |
| 134 | } |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 135 | gl->enable(gl, GGL_SCISSOR_TEST); |
| 136 | } |
| 137 | |
| 138 | void gr_noclip() |
| 139 | { |
| 140 | GGLContext *gl = gr_context; |
Vladimir Oltean | d32b7eb | 2018-07-03 00:04:03 +0300 | [diff] [blame] | 141 | gl->scissor(gl, 0, 0, |
| 142 | gr_draw->width - 2 * overscan_offset_x, |
| 143 | gr_draw->height - 2 * overscan_offset_y); |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 144 | gl->disable(gl, GGL_SCISSOR_TEST); |
| 145 | } |
| 146 | |
| 147 | void gr_line(int x0, int y0, int x1, int y1, int width) |
| 148 | { |
| 149 | GGLContext *gl = gr_context; |
Vladimir Oltean | d32b7eb | 2018-07-03 00:04:03 +0300 | [diff] [blame] | 150 | int x0_disp, y0_disp, x1_disp, y1_disp; |
| 151 | |
Aaron Kling | 4eccd9a | 2019-06-30 12:20:09 -0500 | [diff] [blame] | 152 | x0_disp = ROTATION_X_DISP(x0, y0, gr_draw->width); |
| 153 | y0_disp = ROTATION_Y_DISP(x0, y0, gr_draw->height); |
| 154 | x1_disp = ROTATION_X_DISP(x1, y1, gr_draw->width); |
| 155 | y1_disp = ROTATION_Y_DISP(x1, y1, gr_draw->height); |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 156 | |
| 157 | if(gr_is_curr_clr_opaque) |
| 158 | gl->disable(gl, GGL_BLEND); |
| 159 | |
Vladimir Oltean | d32b7eb | 2018-07-03 00:04:03 +0300 | [diff] [blame] | 160 | const int coords0[2] = { x0_disp << 4, y0_disp << 4 }; |
| 161 | const int coords1[2] = { x1_disp << 4, y1_disp << 4 }; |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 162 | gl->linex(gl, coords0, coords1, width << 4); |
| 163 | |
| 164 | if(gr_is_curr_clr_opaque) |
| 165 | gl->enable(gl, GGL_BLEND); |
| 166 | } |
| 167 | |
| 168 | gr_surface gr_render_circle(int radius, unsigned char r, unsigned char g, unsigned char b, unsigned char a) |
| 169 | { |
| 170 | int rx, ry; |
| 171 | GGLSurface *surface; |
| 172 | const int diameter = radius*2 + 1; |
| 173 | const int radius_check = radius*radius + radius*0.8; |
| 174 | const uint32_t px = (a << 24) | (b << 16) | (g << 8) | r; |
| 175 | uint32_t *data; |
| 176 | |
| 177 | surface = (GGLSurface *)malloc(sizeof(GGLSurface)); |
| 178 | memset(surface, 0, sizeof(GGLSurface)); |
| 179 | |
| 180 | data = (uint32_t *)malloc(diameter * diameter * 4); |
| 181 | memset(data, 0, diameter * diameter * 4); |
| 182 | |
| 183 | surface->version = sizeof(surface); |
| 184 | surface->width = diameter; |
| 185 | surface->height = diameter; |
| 186 | surface->stride = diameter; |
| 187 | surface->data = (GGLubyte*)data; |
notsyncing | 8e4e8ec | 2018-06-09 10:40:50 +0800 | [diff] [blame] | 188 | #if defined(RECOVERY_BGRA) |
| 189 | surface->format = GGL_PIXEL_FORMAT_BGRA_8888; |
| 190 | #else |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 191 | surface->format = GGL_PIXEL_FORMAT_RGBA_8888; |
notsyncing | 8e4e8ec | 2018-06-09 10:40:50 +0800 | [diff] [blame] | 192 | #endif |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 193 | |
| 194 | for(ry = -radius; ry <= radius; ++ry) |
| 195 | for(rx = -radius; rx <= radius; ++rx) |
| 196 | if(rx*rx+ry*ry <= radius_check) |
| 197 | *(data + diameter*(radius + ry) + (radius+rx)) = px; |
| 198 | |
| 199 | return (gr_surface)surface; |
| 200 | } |
| 201 | |
| 202 | void gr_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a) |
| 203 | { |
| 204 | GGLContext *gl = gr_context; |
| 205 | GGLint color[4]; |
| 206 | #if defined(RECOVERY_ABGR) || defined(RECOVERY_BGRA) |
| 207 | color[0] = ((b << 8) | r) + 1; |
| 208 | color[1] = ((g << 8) | g) + 1; |
| 209 | color[2] = ((r << 8) | b) + 1; |
| 210 | color[3] = ((a << 8) | a) + 1; |
| 211 | #else |
| 212 | color[0] = ((r << 8) | r) + 1; |
| 213 | color[1] = ((g << 8) | g) + 1; |
| 214 | color[2] = ((b << 8) | b) + 1; |
| 215 | color[3] = ((a << 8) | a) + 1; |
| 216 | #endif |
| 217 | gl->color4xv(gl, color); |
| 218 | |
| 219 | gr_is_curr_clr_opaque = (a == 255); |
| 220 | } |
| 221 | |
| 222 | void gr_clear() |
| 223 | { |
| 224 | if (gr_draw->pixel_bytes == 2) { |
| 225 | gr_fill(0, 0, gr_fb_width(), gr_fb_height()); |
| 226 | return; |
| 227 | } |
| 228 | |
| 229 | // This code only works on 32bpp devices |
| 230 | if (gr_current_r == gr_current_g && gr_current_r == gr_current_b) { |
| 231 | memset(gr_draw->data, gr_current_r, gr_draw->height * gr_draw->row_bytes); |
| 232 | } else { |
| 233 | unsigned char* px = gr_draw->data; |
| 234 | for (int y = 0; y < gr_draw->height; ++y) { |
| 235 | for (int x = 0; x < gr_draw->width; ++x) { |
| 236 | *px++ = gr_current_r; |
| 237 | *px++ = gr_current_g; |
| 238 | *px++ = gr_current_b; |
| 239 | px++; |
| 240 | } |
| 241 | px += gr_draw->row_bytes - (gr_draw->width * gr_draw->pixel_bytes); |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | void gr_fill(int x, int y, int w, int h) |
| 247 | { |
| 248 | GGLContext *gl = gr_context; |
Vladimir Oltean | d32b7eb | 2018-07-03 00:04:03 +0300 | [diff] [blame] | 249 | int x0_disp, y0_disp, x1_disp, y1_disp; |
| 250 | int l_disp, r_disp, t_disp, b_disp; |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 251 | |
| 252 | if(gr_is_curr_clr_opaque) |
| 253 | gl->disable(gl, GGL_BLEND); |
| 254 | |
Aaron Kling | 4eccd9a | 2019-06-30 12:20:09 -0500 | [diff] [blame] | 255 | x0_disp = ROTATION_X_DISP(x, y, gr_draw->width); |
| 256 | y0_disp = ROTATION_Y_DISP(x, y, gr_draw->height); |
| 257 | x1_disp = ROTATION_X_DISP(x + w, y + h, gr_draw->width); |
| 258 | y1_disp = ROTATION_Y_DISP(x + w, y + h, gr_draw->height); |
Vladimir Oltean | d32b7eb | 2018-07-03 00:04:03 +0300 | [diff] [blame] | 259 | l_disp = std::min(x0_disp, x1_disp); |
| 260 | r_disp = std::max(x0_disp, x1_disp); |
| 261 | t_disp = std::min(y0_disp, y1_disp); |
| 262 | b_disp = std::max(y0_disp, y1_disp); |
Vladimir Oltean | d32b7eb | 2018-07-03 00:04:03 +0300 | [diff] [blame] | 263 | gl->recti(gl, l_disp, t_disp, r_disp, b_disp); |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 264 | |
| 265 | if(gr_is_curr_clr_opaque) |
| 266 | gl->enable(gl, GGL_BLEND); |
| 267 | } |
| 268 | |
Vladimir Oltean | d32b7eb | 2018-07-03 00:04:03 +0300 | [diff] [blame] | 269 | void gr_blit(gr_surface source, int sx, int sy, int w, int h, int dx, int dy) |
| 270 | { |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 271 | if (gr_context == NULL) { |
| 272 | return; |
| 273 | } |
| 274 | |
| 275 | GGLContext *gl = gr_context; |
| 276 | GGLSurface *surface = (GGLSurface*)source; |
| 277 | |
| 278 | if(surface->format == GGL_PIXEL_FORMAT_RGBX_8888) |
| 279 | gl->disable(gl, GGL_BLEND); |
| 280 | |
Vladimir Oltean | d32b7eb | 2018-07-03 00:04:03 +0300 | [diff] [blame] | 281 | int dx0_disp, dy0_disp, dx1_disp, dy1_disp; |
| 282 | int l_disp, r_disp, t_disp, b_disp; |
| 283 | |
Aaron Kling | 4eccd9a | 2019-06-30 12:20:09 -0500 | [diff] [blame] | 284 | // Figuring out display coordinates works for gr_rotation == 0 too, |
Vladimir Oltean | d32b7eb | 2018-07-03 00:04:03 +0300 | [diff] [blame] | 285 | // and isn't as expensive as allocating and rotating another surface, |
| 286 | // so we do this anyway. |
Aaron Kling | 4eccd9a | 2019-06-30 12:20:09 -0500 | [diff] [blame] | 287 | dx0_disp = ROTATION_X_DISP(dx, dy, gr_draw->width); |
| 288 | dy0_disp = ROTATION_Y_DISP(dx, dy, gr_draw->height); |
| 289 | dx1_disp = ROTATION_X_DISP(dx + w, dy + h, gr_draw->width); |
| 290 | dy1_disp = ROTATION_Y_DISP(dx + w, dy + h, gr_draw->height); |
Vladimir Oltean | d32b7eb | 2018-07-03 00:04:03 +0300 | [diff] [blame] | 291 | l_disp = std::min(dx0_disp, dx1_disp); |
| 292 | r_disp = std::max(dx0_disp, dx1_disp); |
| 293 | t_disp = std::min(dy0_disp, dy1_disp); |
| 294 | b_disp = std::max(dy0_disp, dy1_disp); |
| 295 | |
Vladimir Oltean | d32b7eb | 2018-07-03 00:04:03 +0300 | [diff] [blame] | 296 | GGLSurface surface_rotated; |
Aaron Kling | 4eccd9a | 2019-06-30 12:20:09 -0500 | [diff] [blame] | 297 | if (gr_rotation != 0) { |
| 298 | // Do not perform relatively expensive operation if not needed |
| 299 | surface_rotated.version = sizeof(surface_rotated); |
| 300 | // Skip the **(gr_rotation == 0)** || (gr_rotation == 180) check |
| 301 | // because we are under a gr_rotation != 0 conditional compilation statement |
| 302 | surface_rotated.width = (gr_rotation == 180) ? surface->width : surface->height; |
| 303 | surface_rotated.height = (gr_rotation == 180) ? surface->height : surface->width; |
| 304 | surface_rotated.stride = surface_rotated.width; |
| 305 | surface_rotated.format = surface->format; |
| 306 | surface_rotated.data = (GGLubyte*) malloc(surface_rotated.stride * surface_rotated.height * 4); |
| 307 | surface_ROTATION_transform((gr_surface) &surface_rotated, (const gr_surface) surface, 4); |
Vladimir Oltean | d32b7eb | 2018-07-03 00:04:03 +0300 | [diff] [blame] | 308 | |
Aaron Kling | 4eccd9a | 2019-06-30 12:20:09 -0500 | [diff] [blame] | 309 | gl->bindTexture(gl, &surface_rotated); |
| 310 | } else { |
| 311 | gl->bindTexture(gl, surface); |
| 312 | } |
| 313 | |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 314 | gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE); |
| 315 | gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE); |
| 316 | gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE); |
| 317 | gl->enable(gl, GGL_TEXTURE_2D); |
Vladimir Oltean | d32b7eb | 2018-07-03 00:04:03 +0300 | [diff] [blame] | 318 | gl->texCoord2i(gl, sx - l_disp, sy - t_disp); |
| 319 | gl->recti(gl, l_disp, t_disp, r_disp, b_disp); |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 320 | gl->disable(gl, GGL_TEXTURE_2D); |
| 321 | |
Aaron Kling | 4eccd9a | 2019-06-30 12:20:09 -0500 | [diff] [blame] | 322 | if (gr_rotation != 0) |
| 323 | free(surface_rotated.data); |
Vladimir Oltean | d32b7eb | 2018-07-03 00:04:03 +0300 | [diff] [blame] | 324 | |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 325 | if(surface->format == GGL_PIXEL_FORMAT_RGBX_8888) |
| 326 | gl->enable(gl, GGL_BLEND); |
| 327 | } |
| 328 | |
| 329 | unsigned int gr_get_width(gr_surface surface) { |
| 330 | if (surface == NULL) { |
| 331 | return 0; |
| 332 | } |
| 333 | return ((GGLSurface*) surface)->width; |
| 334 | } |
| 335 | |
| 336 | unsigned int gr_get_height(gr_surface surface) { |
| 337 | if (surface == NULL) { |
| 338 | return 0; |
| 339 | } |
| 340 | return ((GGLSurface*) surface)->height; |
| 341 | } |
| 342 | |
| 343 | void gr_flip() { |
| 344 | gr_draw = gr_backend->flip(gr_backend); |
| 345 | // On double buffered back ends, when we flip, we need to tell |
| 346 | // pixel flinger to draw to the other buffer |
| 347 | gr_mem_surface.data = (GGLubyte*)gr_draw->data; |
| 348 | gr_context->colorBuffer(gr_context, &gr_mem_surface); |
| 349 | } |
| 350 | |
| 351 | static void get_memory_surface(GGLSurface* ms) { |
| 352 | ms->version = sizeof(*ms); |
| 353 | ms->width = gr_draw->width; |
| 354 | ms->height = gr_draw->height; |
| 355 | ms->stride = gr_draw->row_bytes / gr_draw->pixel_bytes; |
| 356 | ms->data = (GGLubyte*)gr_draw->data; |
| 357 | ms->format = gr_draw->format; |
| 358 | } |
| 359 | |
| 360 | int gr_init(void) |
| 361 | { |
| 362 | gr_draw = NULL; |
| 363 | |
Aaron Kling | 4eccd9a | 2019-06-30 12:20:09 -0500 | [diff] [blame] | 364 | char gr_rotation_string[PROPERTY_VALUE_MAX]; |
| 365 | char default_rotation[4]; |
| 366 | snprintf(default_rotation, 4, "%d", TW_ROTATION); |
| 367 | property_get("persist.twrp.rotation", gr_rotation_string, default_rotation); |
| 368 | gr_rotation = atoi(gr_rotation_string); |
| 369 | if (!(gr_rotation == 90 || gr_rotation == 180 || gr_rotation == 270)) |
| 370 | gr_rotation = 0; |
| 371 | |
Ethan Yonker | b386f71 | 2017-01-20 14:30:28 -0600 | [diff] [blame] | 372 | #ifdef MSM_BSP |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 373 | gr_backend = open_overlay(); |
| 374 | if (gr_backend) { |
| 375 | gr_draw = gr_backend->init(gr_backend); |
| 376 | if (!gr_draw) { |
| 377 | gr_backend->exit(gr_backend); |
| 378 | } else |
| 379 | printf("Using overlay graphics.\n"); |
| 380 | } |
Ethan Yonker | b386f71 | 2017-01-20 14:30:28 -0600 | [diff] [blame] | 381 | #endif |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 382 | |
| 383 | #ifdef HAS_ADF |
Ethan Yonker | b386f71 | 2017-01-20 14:30:28 -0600 | [diff] [blame] | 384 | if (!gr_backend || !gr_draw) { |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 385 | gr_backend = open_adf(); |
| 386 | if (gr_backend) { |
| 387 | gr_draw = gr_backend->init(gr_backend); |
| 388 | if (!gr_draw) { |
| 389 | gr_backend->exit(gr_backend); |
| 390 | } else |
| 391 | printf("Using adf graphics.\n"); |
| 392 | } |
| 393 | } |
| 394 | #else |
| 395 | #ifdef MSM_BSP |
| 396 | printf("Skipping adf graphics because TW_TARGET_USES_QCOM_BSP := true\n"); |
| 397 | #else |
| 398 | printf("Skipping adf graphics -- not present in build tree\n"); |
| 399 | #endif |
| 400 | #endif |
| 401 | |
| 402 | #ifdef HAS_DRM |
Ethan Yonker | b386f71 | 2017-01-20 14:30:28 -0600 | [diff] [blame] | 403 | if (!gr_backend || !gr_draw) { |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 404 | gr_backend = open_drm(); |
| 405 | gr_draw = gr_backend->init(gr_backend); |
| 406 | if (gr_draw) |
| 407 | printf("Using drm graphics.\n"); |
| 408 | } |
| 409 | #else |
| 410 | printf("Skipping drm graphics -- not present in build tree\n"); |
| 411 | #endif |
| 412 | |
Ethan Yonker | b386f71 | 2017-01-20 14:30:28 -0600 | [diff] [blame] | 413 | if (!gr_backend || !gr_draw) { |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 414 | gr_backend = open_fbdev(); |
| 415 | gr_draw = gr_backend->init(gr_backend); |
| 416 | if (gr_draw == NULL) { |
| 417 | return -1; |
| 418 | } else |
| 419 | printf("Using fbdev graphics.\n"); |
| 420 | } |
| 421 | |
| 422 | overscan_offset_x = gr_draw->width * overscan_percent / 100; |
| 423 | overscan_offset_y = gr_draw->height * overscan_percent / 100; |
| 424 | |
| 425 | // Set up pixelflinger |
| 426 | get_memory_surface(&gr_mem_surface); |
| 427 | gglInit(&gr_context); |
| 428 | GGLContext *gl = gr_context; |
| 429 | gl->colorBuffer(gl, &gr_mem_surface); |
| 430 | |
| 431 | gl->activeTexture(gl, 0); |
| 432 | gl->enable(gl, GGL_BLEND); |
| 433 | gl->blendFunc(gl, GGL_SRC_ALPHA, GGL_ONE_MINUS_SRC_ALPHA); |
| 434 | |
| 435 | gr_flip(); |
| 436 | gr_flip(); |
| 437 | |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 438 | return 0; |
| 439 | } |
| 440 | |
| 441 | void gr_exit(void) |
| 442 | { |
| 443 | gr_backend->exit(gr_backend); |
| 444 | } |
| 445 | |
| 446 | int gr_fb_width(void) |
| 447 | { |
Aaron Kling | 4eccd9a | 2019-06-30 12:20:09 -0500 | [diff] [blame] | 448 | return (gr_rotation == 0 || gr_rotation == 180) ? |
Vladimir Oltean | d32b7eb | 2018-07-03 00:04:03 +0300 | [diff] [blame] | 449 | gr_draw->width - 2 * overscan_offset_x : |
| 450 | gr_draw->height - 2 * overscan_offset_y; |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | int gr_fb_height(void) |
| 454 | { |
Aaron Kling | 4eccd9a | 2019-06-30 12:20:09 -0500 | [diff] [blame] | 455 | return (gr_rotation == 0 || gr_rotation == 180) ? |
Vladimir Oltean | d32b7eb | 2018-07-03 00:04:03 +0300 | [diff] [blame] | 456 | gr_draw->height - 2 * overscan_offset_y : |
| 457 | gr_draw->width - 2 * overscan_offset_x; |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | void gr_fb_blank(bool blank) |
| 461 | { |
| 462 | gr_backend->blank(gr_backend, blank); |
| 463 | } |
| 464 | |
| 465 | int gr_get_surface(gr_surface* surface) |
| 466 | { |
| 467 | GGLSurface* ms = (GGLSurface*)malloc(sizeof(GGLSurface)); |
| 468 | if (!ms) return -1; |
| 469 | |
| 470 | // Allocate the data |
| 471 | get_memory_surface(ms); |
| 472 | ms->data = (GGLubyte*)malloc(ms->stride * ms->height * gr_draw->pixel_bytes); |
| 473 | |
| 474 | // Now, copy the data |
| 475 | memcpy(ms->data, gr_mem_surface.data, gr_draw->width * gr_draw->height * gr_draw->pixel_bytes / 8); |
| 476 | |
| 477 | *surface = (gr_surface*) ms; |
| 478 | return 0; |
| 479 | } |
| 480 | |
| 481 | int gr_free_surface(gr_surface surface) |
| 482 | { |
| 483 | if (!surface) |
| 484 | return -1; |
| 485 | |
| 486 | GGLSurface* ms = (GGLSurface*) surface; |
| 487 | free(ms->data); |
| 488 | free(ms); |
| 489 | return 0; |
| 490 | } |
| 491 | |
| 492 | void gr_write_frame_to_file(int fd) |
| 493 | { |
| 494 | write(fd, gr_mem_surface.data, gr_draw->width * gr_draw->height * gr_draw->pixel_bytes / 8); |
| 495 | } |