blob: b387b703bf6fa3905e04e2b6ba2a54c4f269fa78 [file] [log] [blame]
Ethan Yonkerfbb43532015-12-28 21:54:50 +01001/*
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 Kling35475b12019-06-30 12:20:09 -050034#include <cutils/properties.h>
Ethan Yonkerfbb43532015-12-28 21:54:50 +010035#include <pixelflinger/pixelflinger.h>
36#include "../gui/placement.h"
37#include "minui.h"
38#include "graphics.h"
Vladimir Olteand32b7eb2018-07-03 00:04:03 +030039// For std::min and std::max
40#include <algorithm>
Ethan Yonkerfbb43532015-12-28 21:54:50 +010041
42struct GRFont {
43 GRSurface* texture;
44 int cwidth;
45 int cheight;
46};
47
Ethan Yonkerfbb43532015-12-28 21:54:50 +010048static minui_backend* gr_backend = NULL;
49
50static int overscan_percent = OVERSCAN_PERCENT;
51static int overscan_offset_x = 0;
52static int overscan_offset_y = 0;
53
54static unsigned char gr_current_r = 255;
55static unsigned char gr_current_g = 255;
56static unsigned char gr_current_b = 255;
Ethan Yonkerfbb43532015-12-28 21:54:50 +010057
58GRSurface* gr_draw = NULL;
59
60static GGLContext *gr_context = 0;
61GGLSurface gr_mem_surface;
62static int gr_is_curr_clr_opaque = 0;
63
Aaron Kling35475b12019-06-30 12:20:09 -050064unsigned int gr_rotation = 0;
65
Ethan Yonkerfbb43532015-12-28 21:54:50 +010066int gr_textEx_scaleW(int x, int y, const char *s, void* pFont, int max_width, int placement, int scale)
67{
68 GGLContext *gl = gr_context;
69 void* vfont = pFont;
70 GRFont *font = (GRFont*) pFont;
Ethan Yonker58f21322018-08-24 11:17:36 -050071 int y_scale = 0, measured_width, measured_height, new_height;
Ethan Yonkerfbb43532015-12-28 21:54:50 +010072
73 if (!s || strlen(s) == 0 || !font)
74 return 0;
75
76 measured_height = gr_ttf_getMaxFontHeight(font);
77
78 if (scale) {
79 measured_width = gr_ttf_measureEx(s, vfont);
80 if (measured_width > max_width) {
81 // Adjust font size down until the text fits
82 void *new_font = gr_ttf_scaleFont(vfont, max_width, measured_width);
83 if (!new_font) {
84 printf("gr_textEx_scaleW new_font is NULL\n");
85 return 0;
86 }
87 measured_width = gr_ttf_measureEx(s, new_font);
88 // These next 2 lines adjust the y point based on the new font's height
89 new_height = gr_ttf_getMaxFontHeight(new_font);
90 y_scale = (measured_height - new_height) / 2;
91 vfont = new_font;
92 }
93 } else
94 measured_width = gr_ttf_measureEx(s, vfont);
95
96 int x_adj = measured_width;
97 if (measured_width > max_width)
98 x_adj = max_width;
99
100 if (placement != TOP_LEFT && placement != BOTTOM_LEFT && placement != TEXT_ONLY_RIGHT) {
101 if (placement == CENTER || placement == CENTER_X_ONLY)
102 x -= (x_adj / 2);
103 else
104 x -= x_adj;
105 }
106
107 if (placement != TOP_LEFT && placement != TOP_RIGHT) {
108 if (placement == CENTER || placement == TEXT_ONLY_RIGHT)
109 y -= (measured_height / 2);
110 else if (placement == BOTTOM_LEFT || placement == BOTTOM_RIGHT)
111 y -= measured_height;
112 }
Vladimir Olteand32b7eb2018-07-03 00:04:03 +0300113 return gr_ttf_textExWH(gl, x, y + y_scale, s, vfont, measured_width + x, -1, gr_draw);
Ethan Yonkerfbb43532015-12-28 21:54:50 +0100114}
115
116void gr_clip(int x, int y, int w, int h)
117{
118 GGLContext *gl = gr_context;
Vladimir Olteand32b7eb2018-07-03 00:04:03 +0300119
Aaron Kling35475b12019-06-30 12:20:09 -0500120 switch (gr_rotation) {
121 case 90:
122 gl->scissor(gl, gr_draw->width - y - h, x, h, w);
123 break;
124 case 180:
125 gl->scissor(gl, gr_draw->width - x - w, gr_draw->height - y - h, w, h);
126 break;
127 case 270:
128 gl->scissor(gl, y, gr_draw->height - x - w, h, w);
129 break;
130 default:
131 gl->scissor(gl, x, y, w, h);
132 break;
133 }
Ethan Yonkerfbb43532015-12-28 21:54:50 +0100134 gl->enable(gl, GGL_SCISSOR_TEST);
135}
136
137void gr_noclip()
138{
139 GGLContext *gl = gr_context;
Vladimir Olteand32b7eb2018-07-03 00:04:03 +0300140 gl->scissor(gl, 0, 0,
141 gr_draw->width - 2 * overscan_offset_x,
142 gr_draw->height - 2 * overscan_offset_y);
Ethan Yonkerfbb43532015-12-28 21:54:50 +0100143 gl->disable(gl, GGL_SCISSOR_TEST);
144}
145
146void gr_line(int x0, int y0, int x1, int y1, int width)
147{
148 GGLContext *gl = gr_context;
Vladimir Olteand32b7eb2018-07-03 00:04:03 +0300149 int x0_disp, y0_disp, x1_disp, y1_disp;
150
Aaron Kling35475b12019-06-30 12:20:09 -0500151 x0_disp = ROTATION_X_DISP(x0, y0, gr_draw->width);
152 y0_disp = ROTATION_Y_DISP(x0, y0, gr_draw->height);
153 x1_disp = ROTATION_X_DISP(x1, y1, gr_draw->width);
154 y1_disp = ROTATION_Y_DISP(x1, y1, gr_draw->height);
Ethan Yonkerfbb43532015-12-28 21:54:50 +0100155
156 if(gr_is_curr_clr_opaque)
157 gl->disable(gl, GGL_BLEND);
158
Vladimir Olteand32b7eb2018-07-03 00:04:03 +0300159 const int coords0[2] = { x0_disp << 4, y0_disp << 4 };
160 const int coords1[2] = { x1_disp << 4, y1_disp << 4 };
Ethan Yonkerfbb43532015-12-28 21:54:50 +0100161 gl->linex(gl, coords0, coords1, width << 4);
162
163 if(gr_is_curr_clr_opaque)
164 gl->enable(gl, GGL_BLEND);
165}
166
167gr_surface gr_render_circle(int radius, unsigned char r, unsigned char g, unsigned char b, unsigned char a)
168{
169 int rx, ry;
170 GGLSurface *surface;
171 const int diameter = radius*2 + 1;
172 const int radius_check = radius*radius + radius*0.8;
173 const uint32_t px = (a << 24) | (b << 16) | (g << 8) | r;
174 uint32_t *data;
175
176 surface = (GGLSurface *)malloc(sizeof(GGLSurface));
177 memset(surface, 0, sizeof(GGLSurface));
178
179 data = (uint32_t *)malloc(diameter * diameter * 4);
180 memset(data, 0, diameter * diameter * 4);
181
182 surface->version = sizeof(surface);
183 surface->width = diameter;
184 surface->height = diameter;
185 surface->stride = diameter;
186 surface->data = (GGLubyte*)data;
notsyncing8e4e8ec2018-06-09 10:40:50 +0800187#if defined(RECOVERY_BGRA)
188 surface->format = GGL_PIXEL_FORMAT_BGRA_8888;
189#else
Ethan Yonkerfbb43532015-12-28 21:54:50 +0100190 surface->format = GGL_PIXEL_FORMAT_RGBA_8888;
notsyncing8e4e8ec2018-06-09 10:40:50 +0800191#endif
Ethan Yonkerfbb43532015-12-28 21:54:50 +0100192
193 for(ry = -radius; ry <= radius; ++ry)
194 for(rx = -radius; rx <= radius; ++rx)
195 if(rx*rx+ry*ry <= radius_check)
196 *(data + diameter*(radius + ry) + (radius+rx)) = px;
197
198 return (gr_surface)surface;
199}
200
201void gr_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
202{
203 GGLContext *gl = gr_context;
204 GGLint color[4];
205#if defined(RECOVERY_ABGR) || defined(RECOVERY_BGRA)
206 color[0] = ((b << 8) | r) + 1;
207 color[1] = ((g << 8) | g) + 1;
208 color[2] = ((r << 8) | b) + 1;
209 color[3] = ((a << 8) | a) + 1;
210#else
211 color[0] = ((r << 8) | r) + 1;
212 color[1] = ((g << 8) | g) + 1;
213 color[2] = ((b << 8) | b) + 1;
214 color[3] = ((a << 8) | a) + 1;
215#endif
216 gl->color4xv(gl, color);
217
218 gr_is_curr_clr_opaque = (a == 255);
219}
220
221void gr_clear()
222{
223 if (gr_draw->pixel_bytes == 2) {
224 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
225 return;
226 }
227
228 // This code only works on 32bpp devices
229 if (gr_current_r == gr_current_g && gr_current_r == gr_current_b) {
230 memset(gr_draw->data, gr_current_r, gr_draw->height * gr_draw->row_bytes);
231 } else {
232 unsigned char* px = gr_draw->data;
233 for (int y = 0; y < gr_draw->height; ++y) {
234 for (int x = 0; x < gr_draw->width; ++x) {
235 *px++ = gr_current_r;
236 *px++ = gr_current_g;
237 *px++ = gr_current_b;
238 px++;
239 }
240 px += gr_draw->row_bytes - (gr_draw->width * gr_draw->pixel_bytes);
241 }
242 }
243}
244
245void gr_fill(int x, int y, int w, int h)
246{
247 GGLContext *gl = gr_context;
Vladimir Olteand32b7eb2018-07-03 00:04:03 +0300248 int x0_disp, y0_disp, x1_disp, y1_disp;
249 int l_disp, r_disp, t_disp, b_disp;
Ethan Yonkerfbb43532015-12-28 21:54:50 +0100250
251 if(gr_is_curr_clr_opaque)
252 gl->disable(gl, GGL_BLEND);
253
Aaron Kling35475b12019-06-30 12:20:09 -0500254 x0_disp = ROTATION_X_DISP(x, y, gr_draw->width);
255 y0_disp = ROTATION_Y_DISP(x, y, gr_draw->height);
256 x1_disp = ROTATION_X_DISP(x + w, y + h, gr_draw->width);
257 y1_disp = ROTATION_Y_DISP(x + w, y + h, gr_draw->height);
Vladimir Olteand32b7eb2018-07-03 00:04:03 +0300258 l_disp = std::min(x0_disp, x1_disp);
259 r_disp = std::max(x0_disp, x1_disp);
260 t_disp = std::min(y0_disp, y1_disp);
261 b_disp = std::max(y0_disp, y1_disp);
262
263 gl->recti(gl, l_disp, t_disp, r_disp, b_disp);
Ethan Yonkerfbb43532015-12-28 21:54:50 +0100264
265 if(gr_is_curr_clr_opaque)
266 gl->enable(gl, GGL_BLEND);
267}
268
Vladimir Olteand32b7eb2018-07-03 00:04:03 +0300269void gr_blit(gr_surface source, int sx, int sy, int w, int h, int dx, int dy)
270{
Ethan Yonkerfbb43532015-12-28 21:54:50 +0100271 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 Olteand32b7eb2018-07-03 00:04:03 +0300281 int dx0_disp, dy0_disp, dx1_disp, dy1_disp;
282 int l_disp, r_disp, t_disp, b_disp;
283
Aaron Kling35475b12019-06-30 12:20:09 -0500284 // Figuring out display coordinates works for gr_rotation == 0 too,
Vladimir Olteand32b7eb2018-07-03 00:04:03 +0300285 // and isn't as expensive as allocating and rotating another surface,
286 // so we do this anyway.
Aaron Kling35475b12019-06-30 12:20:09 -0500287 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 Olteand32b7eb2018-07-03 00:04:03 +0300291 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 Olteand32b7eb2018-07-03 00:04:03 +0300296 GGLSurface surface_rotated;
Aaron Kling35475b12019-06-30 12:20:09 -0500297 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 Olteand32b7eb2018-07-03 00:04:03 +0300308
Aaron Kling35475b12019-06-30 12:20:09 -0500309 gl->bindTexture(gl, &surface_rotated);
310 } else {
311 gl->bindTexture(gl, surface);
312 }
313
Ethan Yonkerfbb43532015-12-28 21:54:50 +0100314 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 Olteand32b7eb2018-07-03 00:04:03 +0300318 gl->texCoord2i(gl, sx - l_disp, sy - t_disp);
319 gl->recti(gl, l_disp, t_disp, r_disp, b_disp);
Ethan Yonkerfbb43532015-12-28 21:54:50 +0100320 gl->disable(gl, GGL_TEXTURE_2D);
321
Aaron Kling35475b12019-06-30 12:20:09 -0500322 if (gr_rotation != 0)
323 free(surface_rotated.data);
Vladimir Olteand32b7eb2018-07-03 00:04:03 +0300324
Ethan Yonkerfbb43532015-12-28 21:54:50 +0100325 if(surface->format == GGL_PIXEL_FORMAT_RGBX_8888)
326 gl->enable(gl, GGL_BLEND);
327}
328
329unsigned int gr_get_width(gr_surface surface) {
330 if (surface == NULL) {
331 return 0;
332 }
333 return ((GGLSurface*) surface)->width;
334}
335
336unsigned int gr_get_height(gr_surface surface) {
337 if (surface == NULL) {
338 return 0;
339 }
340 return ((GGLSurface*) surface)->height;
341}
342
343void 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
351static 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
360int gr_init(void)
361{
362 gr_draw = NULL;
363
Aaron Kling35475b12019-06-30 12:20:09 -0500364 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 Yonkerb386f712017-01-20 14:30:28 -0600372#ifdef MSM_BSP
Ethan Yonkerfbb43532015-12-28 21:54:50 +0100373 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 Yonkerb386f712017-01-20 14:30:28 -0600381#endif
Ethan Yonkerfbb43532015-12-28 21:54:50 +0100382
383#ifdef HAS_ADF
Ethan Yonkerb386f712017-01-20 14:30:28 -0600384 if (!gr_backend || !gr_draw) {
Ethan Yonkerfbb43532015-12-28 21:54:50 +0100385 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 Yonkerb386f712017-01-20 14:30:28 -0600403 if (!gr_backend || !gr_draw) {
Ethan Yonkerfbb43532015-12-28 21:54:50 +0100404 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 Yonkerb386f712017-01-20 14:30:28 -0600413 if (!gr_backend || !gr_draw) {
Ethan Yonkerfbb43532015-12-28 21:54:50 +0100414 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 Yonkerfbb43532015-12-28 21:54:50 +0100438 return 0;
439}
440
441void gr_exit(void)
442{
443 gr_backend->exit(gr_backend);
444}
445
446int gr_fb_width(void)
447{
Aaron Kling35475b12019-06-30 12:20:09 -0500448 return (gr_rotation == 0 || gr_rotation == 180) ?
Vladimir Olteand32b7eb2018-07-03 00:04:03 +0300449 gr_draw->width - 2 * overscan_offset_x :
450 gr_draw->height - 2 * overscan_offset_y;
Ethan Yonkerfbb43532015-12-28 21:54:50 +0100451}
452
453int gr_fb_height(void)
454{
Aaron Kling35475b12019-06-30 12:20:09 -0500455 return (gr_rotation == 0 || gr_rotation == 180) ?
Vladimir Olteand32b7eb2018-07-03 00:04:03 +0300456 gr_draw->height - 2 * overscan_offset_y :
457 gr_draw->width - 2 * overscan_offset_x;
Ethan Yonkerfbb43532015-12-28 21:54:50 +0100458}
459
460void gr_fb_blank(bool blank)
461{
462 gr_backend->blank(gr_backend, blank);
463}
464
465int 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
481int 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
492void 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}