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> |
| 36 | #include "../gui/placement.h" |
| 37 | #include "minui.h" |
| 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 | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 41 | #include "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); |
| 263 | |
| 264 | gl->recti(gl, l_disp, t_disp, r_disp, b_disp); |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 265 | |
| 266 | if(gr_is_curr_clr_opaque) |
| 267 | gl->enable(gl, GGL_BLEND); |
| 268 | } |
| 269 | |
Vladimir Oltean | d32b7eb | 2018-07-03 00:04:03 +0300 | [diff] [blame] | 270 | void gr_blit(gr_surface source, int sx, int sy, int w, int h, int dx, int dy) |
| 271 | { |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 272 | if (gr_context == NULL) { |
| 273 | return; |
| 274 | } |
| 275 | |
| 276 | GGLContext *gl = gr_context; |
| 277 | GGLSurface *surface = (GGLSurface*)source; |
| 278 | |
| 279 | if(surface->format == GGL_PIXEL_FORMAT_RGBX_8888) |
| 280 | gl->disable(gl, GGL_BLEND); |
| 281 | |
Vladimir Oltean | d32b7eb | 2018-07-03 00:04:03 +0300 | [diff] [blame] | 282 | int dx0_disp, dy0_disp, dx1_disp, dy1_disp; |
| 283 | int l_disp, r_disp, t_disp, b_disp; |
| 284 | |
Aaron Kling | 4eccd9a | 2019-06-30 12:20:09 -0500 | [diff] [blame] | 285 | // Figuring out display coordinates works for gr_rotation == 0 too, |
Vladimir Oltean | d32b7eb | 2018-07-03 00:04:03 +0300 | [diff] [blame] | 286 | // and isn't as expensive as allocating and rotating another surface, |
| 287 | // so we do this anyway. |
Aaron Kling | 4eccd9a | 2019-06-30 12:20:09 -0500 | [diff] [blame] | 288 | dx0_disp = ROTATION_X_DISP(dx, dy, gr_draw->width); |
| 289 | dy0_disp = ROTATION_Y_DISP(dx, dy, gr_draw->height); |
| 290 | dx1_disp = ROTATION_X_DISP(dx + w, dy + h, gr_draw->width); |
| 291 | dy1_disp = ROTATION_Y_DISP(dx + w, dy + h, gr_draw->height); |
Vladimir Oltean | d32b7eb | 2018-07-03 00:04:03 +0300 | [diff] [blame] | 292 | l_disp = std::min(dx0_disp, dx1_disp); |
| 293 | r_disp = std::max(dx0_disp, dx1_disp); |
| 294 | t_disp = std::min(dy0_disp, dy1_disp); |
| 295 | b_disp = std::max(dy0_disp, dy1_disp); |
| 296 | |
Vladimir Oltean | d32b7eb | 2018-07-03 00:04:03 +0300 | [diff] [blame] | 297 | GGLSurface surface_rotated; |
Aaron Kling | 4eccd9a | 2019-06-30 12:20:09 -0500 | [diff] [blame] | 298 | if (gr_rotation != 0) { |
| 299 | // Do not perform relatively expensive operation if not needed |
| 300 | surface_rotated.version = sizeof(surface_rotated); |
| 301 | // Skip the **(gr_rotation == 0)** || (gr_rotation == 180) check |
| 302 | // because we are under a gr_rotation != 0 conditional compilation statement |
| 303 | surface_rotated.width = (gr_rotation == 180) ? surface->width : surface->height; |
| 304 | surface_rotated.height = (gr_rotation == 180) ? surface->height : surface->width; |
| 305 | surface_rotated.stride = surface_rotated.width; |
| 306 | surface_rotated.format = surface->format; |
| 307 | surface_rotated.data = (GGLubyte*) malloc(surface_rotated.stride * surface_rotated.height * 4); |
| 308 | surface_ROTATION_transform((gr_surface) &surface_rotated, (const gr_surface) surface, 4); |
Vladimir Oltean | d32b7eb | 2018-07-03 00:04:03 +0300 | [diff] [blame] | 309 | |
Aaron Kling | 4eccd9a | 2019-06-30 12:20:09 -0500 | [diff] [blame] | 310 | gl->bindTexture(gl, &surface_rotated); |
| 311 | } else { |
| 312 | gl->bindTexture(gl, surface); |
| 313 | } |
| 314 | |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 315 | gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE); |
| 316 | gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE); |
| 317 | gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE); |
| 318 | gl->enable(gl, GGL_TEXTURE_2D); |
Vladimir Oltean | d32b7eb | 2018-07-03 00:04:03 +0300 | [diff] [blame] | 319 | gl->texCoord2i(gl, sx - l_disp, sy - t_disp); |
| 320 | gl->recti(gl, l_disp, t_disp, r_disp, b_disp); |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 321 | gl->disable(gl, GGL_TEXTURE_2D); |
| 322 | |
Aaron Kling | 4eccd9a | 2019-06-30 12:20:09 -0500 | [diff] [blame] | 323 | if (gr_rotation != 0) |
| 324 | free(surface_rotated.data); |
Vladimir Oltean | d32b7eb | 2018-07-03 00:04:03 +0300 | [diff] [blame] | 325 | |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 326 | if(surface->format == GGL_PIXEL_FORMAT_RGBX_8888) |
| 327 | gl->enable(gl, GGL_BLEND); |
| 328 | } |
| 329 | |
| 330 | unsigned int gr_get_width(gr_surface surface) { |
| 331 | if (surface == NULL) { |
| 332 | return 0; |
| 333 | } |
| 334 | return ((GGLSurface*) surface)->width; |
| 335 | } |
| 336 | |
| 337 | unsigned int gr_get_height(gr_surface surface) { |
| 338 | if (surface == NULL) { |
| 339 | return 0; |
| 340 | } |
| 341 | return ((GGLSurface*) surface)->height; |
| 342 | } |
| 343 | |
| 344 | void gr_flip() { |
| 345 | gr_draw = gr_backend->flip(gr_backend); |
| 346 | // On double buffered back ends, when we flip, we need to tell |
| 347 | // pixel flinger to draw to the other buffer |
| 348 | gr_mem_surface.data = (GGLubyte*)gr_draw->data; |
| 349 | gr_context->colorBuffer(gr_context, &gr_mem_surface); |
| 350 | } |
| 351 | |
| 352 | static void get_memory_surface(GGLSurface* ms) { |
| 353 | ms->version = sizeof(*ms); |
| 354 | ms->width = gr_draw->width; |
| 355 | ms->height = gr_draw->height; |
| 356 | ms->stride = gr_draw->row_bytes / gr_draw->pixel_bytes; |
| 357 | ms->data = (GGLubyte*)gr_draw->data; |
| 358 | ms->format = gr_draw->format; |
| 359 | } |
| 360 | |
| 361 | int gr_init(void) |
| 362 | { |
| 363 | gr_draw = NULL; |
| 364 | |
Aaron Kling | 4eccd9a | 2019-06-30 12:20:09 -0500 | [diff] [blame] | 365 | char gr_rotation_string[PROPERTY_VALUE_MAX]; |
| 366 | char default_rotation[4]; |
| 367 | snprintf(default_rotation, 4, "%d", TW_ROTATION); |
| 368 | property_get("persist.twrp.rotation", gr_rotation_string, default_rotation); |
| 369 | gr_rotation = atoi(gr_rotation_string); |
| 370 | if (!(gr_rotation == 90 || gr_rotation == 180 || gr_rotation == 270)) |
| 371 | gr_rotation = 0; |
| 372 | |
Ethan Yonker | b386f71 | 2017-01-20 14:30:28 -0600 | [diff] [blame] | 373 | #ifdef MSM_BSP |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 374 | gr_backend = open_overlay(); |
| 375 | if (gr_backend) { |
| 376 | gr_draw = gr_backend->init(gr_backend); |
| 377 | if (!gr_draw) { |
| 378 | gr_backend->exit(gr_backend); |
| 379 | } else |
| 380 | printf("Using overlay graphics.\n"); |
| 381 | } |
Ethan Yonker | b386f71 | 2017-01-20 14:30:28 -0600 | [diff] [blame] | 382 | #endif |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 383 | |
| 384 | #ifdef HAS_ADF |
Ethan Yonker | b386f71 | 2017-01-20 14:30:28 -0600 | [diff] [blame] | 385 | if (!gr_backend || !gr_draw) { |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 386 | gr_backend = open_adf(); |
| 387 | if (gr_backend) { |
| 388 | gr_draw = gr_backend->init(gr_backend); |
| 389 | if (!gr_draw) { |
| 390 | gr_backend->exit(gr_backend); |
| 391 | } else |
| 392 | printf("Using adf graphics.\n"); |
| 393 | } |
| 394 | } |
| 395 | #else |
| 396 | #ifdef MSM_BSP |
| 397 | printf("Skipping adf graphics because TW_TARGET_USES_QCOM_BSP := true\n"); |
| 398 | #else |
| 399 | printf("Skipping adf graphics -- not present in build tree\n"); |
| 400 | #endif |
| 401 | #endif |
| 402 | |
| 403 | #ifdef HAS_DRM |
Ethan Yonker | b386f71 | 2017-01-20 14:30:28 -0600 | [diff] [blame] | 404 | if (!gr_backend || !gr_draw) { |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 405 | gr_backend = open_drm(); |
| 406 | gr_draw = gr_backend->init(gr_backend); |
| 407 | if (gr_draw) |
| 408 | printf("Using drm graphics.\n"); |
| 409 | } |
| 410 | #else |
| 411 | printf("Skipping drm graphics -- not present in build tree\n"); |
| 412 | #endif |
| 413 | |
Ethan Yonker | b386f71 | 2017-01-20 14:30:28 -0600 | [diff] [blame] | 414 | if (!gr_backend || !gr_draw) { |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 415 | gr_backend = open_fbdev(); |
| 416 | gr_draw = gr_backend->init(gr_backend); |
| 417 | if (gr_draw == NULL) { |
| 418 | return -1; |
| 419 | } else |
| 420 | printf("Using fbdev graphics.\n"); |
| 421 | } |
| 422 | |
| 423 | overscan_offset_x = gr_draw->width * overscan_percent / 100; |
| 424 | overscan_offset_y = gr_draw->height * overscan_percent / 100; |
| 425 | |
| 426 | // Set up pixelflinger |
| 427 | get_memory_surface(&gr_mem_surface); |
| 428 | gglInit(&gr_context); |
| 429 | GGLContext *gl = gr_context; |
| 430 | gl->colorBuffer(gl, &gr_mem_surface); |
| 431 | |
| 432 | gl->activeTexture(gl, 0); |
| 433 | gl->enable(gl, GGL_BLEND); |
| 434 | gl->blendFunc(gl, GGL_SRC_ALPHA, GGL_ONE_MINUS_SRC_ALPHA); |
| 435 | |
| 436 | gr_flip(); |
| 437 | gr_flip(); |
| 438 | |
¨Paul | 80bd165 | 2016-04-26 16:49:58 +0100 | [diff] [blame] | 439 | #ifdef TW_SCREEN_BLANK_ON_BOOT |
| 440 | printf("TW_SCREEN_BLANK_ON_BOOT := true\n"); |
| 441 | gr_fb_blank(true); |
| 442 | gr_fb_blank(false); |
| 443 | #endif |
| 444 | |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 445 | return 0; |
| 446 | } |
| 447 | |
| 448 | void gr_exit(void) |
| 449 | { |
| 450 | gr_backend->exit(gr_backend); |
| 451 | } |
| 452 | |
| 453 | int gr_fb_width(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->width - 2 * overscan_offset_x : |
| 457 | gr_draw->height - 2 * overscan_offset_y; |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | int gr_fb_height(void) |
| 461 | { |
Aaron Kling | 4eccd9a | 2019-06-30 12:20:09 -0500 | [diff] [blame] | 462 | return (gr_rotation == 0 || gr_rotation == 180) ? |
Vladimir Oltean | d32b7eb | 2018-07-03 00:04:03 +0300 | [diff] [blame] | 463 | gr_draw->height - 2 * overscan_offset_y : |
| 464 | gr_draw->width - 2 * overscan_offset_x; |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | void gr_fb_blank(bool blank) |
| 468 | { |
| 469 | gr_backend->blank(gr_backend, blank); |
| 470 | } |
| 471 | |
| 472 | int gr_get_surface(gr_surface* surface) |
| 473 | { |
| 474 | GGLSurface* ms = (GGLSurface*)malloc(sizeof(GGLSurface)); |
| 475 | if (!ms) return -1; |
| 476 | |
| 477 | // Allocate the data |
| 478 | get_memory_surface(ms); |
| 479 | ms->data = (GGLubyte*)malloc(ms->stride * ms->height * gr_draw->pixel_bytes); |
| 480 | |
| 481 | // Now, copy the data |
| 482 | memcpy(ms->data, gr_mem_surface.data, gr_draw->width * gr_draw->height * gr_draw->pixel_bytes / 8); |
| 483 | |
| 484 | *surface = (gr_surface*) ms; |
| 485 | return 0; |
| 486 | } |
| 487 | |
| 488 | int gr_free_surface(gr_surface surface) |
| 489 | { |
| 490 | if (!surface) |
| 491 | return -1; |
| 492 | |
| 493 | GGLSurface* ms = (GGLSurface*) surface; |
| 494 | free(ms->data); |
| 495 | free(ms); |
| 496 | return 0; |
| 497 | } |
| 498 | |
| 499 | void gr_write_frame_to_file(int fd) |
| 500 | { |
| 501 | write(fd, gr_mem_surface.data, gr_draw->width * gr_draw->height * gr_draw->pixel_bytes / 8); |
| 502 | } |