blob: 82bb4280df299b75d8d03ac7bff8f846b3a13757 [file] [log] [blame]
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001/*
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
Tao Bao0ecbd762017-01-16 21:16:58 -080017#include "graphics.h"
18
Luke Song846012f2017-09-13 15:56:16 -070019#include <stdint.h>
Tao Baoe8020f42017-02-03 09:30:07 -080020#include <stdio.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080021#include <stdlib.h>
Elliott Hughescd3c55a2015-01-29 20:50:08 -080022#include <string.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080023
Tao Bao557fa1f2017-02-07 12:51:00 -080024#include <memory>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080025
Tao Baoed876a72018-07-31 21:32:50 -070026#include <android-base/properties.h>
27
bigbiff20edca82020-07-03 09:55:28 -040028#ifdef BOARD_USE_CUSTOM_RECOVERY_FONT
29#include BOARD_USE_CUSTOM_RECOVERY_FONT
30#else
31#include "font_10x18.h"
32#endif
33
34#ifndef MSM_BSP
Tao Bao557fa1f2017-02-07 12:51:00 -080035#include "graphics_adf.h"
Andreas Schneider58d68e52017-11-02 17:54:36 +010036#endif
Tao Bao557fa1f2017-02-07 12:51:00 -080037#include "graphics_drm.h"
38#include "graphics_fbdev.h"
Tao Bao0ecbd762017-01-16 21:16:58 -080039#include "minui/minui.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080040
Tao Bao9f426332018-06-13 10:39:44 -070041static GRFont* gr_font = nullptr;
Tao Bao557fa1f2017-02-07 12:51:00 -080042static MinuiBackend* gr_backend = nullptr;
Doug Zongker16f97c32014-03-06 16:16:05 -080043
Doug Zongkerc560a672012-12-18 16:31:27 -080044static int overscan_offset_x = 0;
45static int overscan_offset_y = 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080046
Ethan Yonker58f21322018-08-24 11:17:36 -050047#ifdef TW_NO_MINUI_CUSTOM_FONTS
Doug Zongker16f97c32014-03-06 16:16:05 -080048static unsigned char gr_current_r = 255;
49static unsigned char gr_current_g = 255;
50static unsigned char gr_current_b = 255;
Ethan Yonker58f21322018-08-24 11:17:36 -050051#endif
Doug Zongker16f97c32014-03-06 16:16:05 -080052static unsigned char gr_current_a = 255;
Ethan Yonkere96182e2015-10-13 19:32:03 -050053static unsigned char rgb_555[2];
54static unsigned char gr_current_r5 = 31;
55static unsigned char gr_current_g5 = 63;
56static unsigned char gr_current_b5 = 31;
Doug Zongker16f97c32014-03-06 16:16:05 -080057
Luke Song846012f2017-09-13 15:56:16 -070058static uint32_t gr_current = ~0;
59static constexpr uint32_t alpha_mask = 0xff000000;
Doug Zongker16f97c32014-03-06 16:16:05 -080060
Tao Bao9f426332018-06-13 10:39:44 -070061// gr_draw is owned by backends.
Tao Bao92bdb5a2018-10-21 12:12:37 -070062static GRSurface* gr_draw = nullptr;
Tao Bao44478df2018-07-31 22:01:03 -070063static GRRotation rotation = GRRotation::NONE;
Tao Baoed876a72018-07-31 21:32:50 -070064static PixelFormat pixel_format = PixelFormat::UNKNOWN;
Doug Zongker16f97c32014-03-06 16:16:05 -080065
Luke Song846012f2017-09-13 15:56:16 -070066static bool outside(int x, int y) {
Tao Bao44478df2018-07-31 22:01:03 -070067 auto swapped = (rotation == GRRotation::LEFT || rotation == GRRotation::RIGHT);
68 return x < 0 || x >= (swapped ? gr_draw->height : gr_draw->width) || y < 0 ||
69 y >= (swapped ? gr_draw->width : gr_draw->height);
Doug Zongker16f97c32014-03-06 16:16:05 -080070}
Ethan Yonker39662b22017-05-23 08:34:02 -050071
72#ifdef TW_NO_MINUI_CUSTOM_FONTS
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080073int gr_measure(const char *s)
74{
Ethan Yonker84d61ce2017-05-10 16:11:35 -050075 return gr_font->char_width * strlen(s);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080076}
77
Dima Zavin3c7f00e2011-08-30 11:58:24 -070078void gr_font_size(int *x, int *y)
79{
Ethan Yonker84d61ce2017-05-10 16:11:35 -050080 *x = gr_font->char_width;
81 *y = gr_font->char_height;
Dima Zavin3c7f00e2011-08-30 11:58:24 -070082}
Ethan Yonker84d61ce2017-05-10 16:11:35 -050083#else // TW_USE_MINUI_CUSTOM_FONTS
Luke Song846012f2017-09-13 15:56:16 -070084const GRFont* gr_sys_font() {
85 return gr_font;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080086}
87
Tao Baoed876a72018-07-31 21:32:50 -070088PixelFormat gr_pixel_format() {
89 return pixel_format;
90}
91
Luke Song846012f2017-09-13 15:56:16 -070092int gr_measure(const GRFont* font, const char* s) {
Tianjie Xu842f2a32018-05-31 18:16:28 -070093 if (font == nullptr) {
94 return -1;
95 }
96
Luke Song846012f2017-09-13 15:56:16 -070097 return font->char_width * strlen(s);
Damien Bargiacchi35fff612016-08-11 15:57:03 -070098}
99
Tianjie Xu842f2a32018-05-31 18:16:28 -0700100int gr_font_size(const GRFont* font, int* x, int* y) {
101 if (font == nullptr) {
102 return -1;
103 }
104
Luke Song846012f2017-09-13 15:56:16 -0700105 *x = font->char_width;
106 *y = font->char_height;
Tianjie Xu842f2a32018-05-31 18:16:28 -0700107 return 0;
Dima Zavin3c7f00e2011-08-30 11:58:24 -0700108}
Ethan Yonker39662b22017-05-23 08:34:02 -0500109#endif // TW_NO_MINUI_CUSTOM_FONTS
Dima Zavin3c7f00e2011-08-30 11:58:24 -0700110
Ethan Yonkere96182e2015-10-13 19:32:03 -0500111void blend_16bpp(unsigned char* px, unsigned r5, unsigned g5, unsigned b5, unsigned char a)
112{
113 unsigned char orig[2];
114 orig[0] = px[0];
115 orig[1] = px[1];
116
117 /* This code is a little easier to read
118 unsigned oldred = (orig[1] >> 3);
119 unsigned oldgreen = (((orig[0] >> 5) << 3) + (orig[1] & 0x7));
120 unsigned oldblue = (orig[0] & 0x1F);
121
122 unsigned newred = (oldred * (255-a) + r5 * a) / 255;
123 unsigned newgreen = (oldgreen * (255-a) + g5 * a) / 255;
124 unsigned newblue = (oldblue * (255-a) + b5 * a) / 255;
125 */
126
127 unsigned newred = ((orig[1] >> 3) * (255-a) + r5 * a) / 255;
128 unsigned newgreen = ((((orig[0] >> 5) << 3) + (orig[1] & 0x7)) * (255-a) + g5 * a) / 255;
129 unsigned newblue = ((orig[0] & 0x1F) * (255-a) + b5 * a) / 255;
130
131 *px++ = (newgreen << 5) + (newblue);
132 *px++ = (newred << 3) + (newgreen >> 3);
133}
134
Ethan Yonker58f21322018-08-24 11:17:36 -0500135#ifdef TW_NO_MINUI_CUSTOM_FONTS
136static void text_blend_old(unsigned char* src_p, int src_row_bytes,
Doug Zongker16f97c32014-03-06 16:16:05 -0800137 unsigned char* dst_p, int dst_row_bytes,
138 int width, int height)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800139{
Elliott Hughes01a4d082015-03-24 15:21:48 -0700140 for (int j = 0; j < height; ++j) {
Doug Zongker16f97c32014-03-06 16:16:05 -0800141 unsigned char* sx = src_p;
142 unsigned char* px = dst_p;
Elliott Hughes01a4d082015-03-24 15:21:48 -0700143 for (int i = 0; i < width; ++i) {
Doug Zongker16f97c32014-03-06 16:16:05 -0800144 unsigned char a = *sx++;
145 if (gr_current_a < 255) a = ((int)a * gr_current_a) / 255;
146 if (a == 255) {
Ethan Yonkere96182e2015-10-13 19:32:03 -0500147 if (gr_draw->pixel_bytes == 2) {
148 *px++ = rgb_555[0];
149 *px++ = rgb_555[1];
150 } else {
151 *px++ = gr_current_r;
152 *px++ = gr_current_g;
153 *px++ = gr_current_b;
154 px++;
155 }
Doug Zongker16f97c32014-03-06 16:16:05 -0800156 } else if (a > 0) {
Ethan Yonkere96182e2015-10-13 19:32:03 -0500157 if (gr_draw->pixel_bytes == 2) {
158 blend_16bpp(px, gr_current_r5, gr_current_g5, gr_current_b5, a);
159 px += gr_draw->pixel_bytes;
160 } else {
161 *px = (*px * (255-a) + gr_current_r * a) / 255;
162 ++px;
163 *px = (*px * (255-a) + gr_current_g * a) / 255;
164 ++px;
165 *px = (*px * (255-a) + gr_current_b * a) / 255;
166 ++px;
167 ++px;
168 }
Doug Zongker16f97c32014-03-06 16:16:05 -0800169 } else {
Ethan Yonkere96182e2015-10-13 19:32:03 -0500170 px += gr_draw->pixel_bytes;
Doug Zongker16f97c32014-03-06 16:16:05 -0800171 }
172 }
173 src_p += src_row_bytes;
174 dst_p += dst_row_bytes;
175 }
176}
Ethan Yonker58f21322018-08-24 11:17:36 -0500177#endif // TW_NO_MINUI_CUSTOM_FONTS
178
179// Blends gr_current onto pix value, assumes alpha as most significant byte.
Luke Song846012f2017-09-13 15:56:16 -0700180static inline uint32_t pixel_blend(uint8_t alpha, uint32_t pix) {
181 if (alpha == 255) return gr_current;
182 if (alpha == 0) return pix;
183 uint32_t pix_r = pix & 0xff;
184 uint32_t pix_g = pix & 0xff00;
185 uint32_t pix_b = pix & 0xff0000;
186 uint32_t cur_r = gr_current & 0xff;
187 uint32_t cur_g = gr_current & 0xff00;
188 uint32_t cur_b = gr_current & 0xff0000;
189
190 uint32_t out_r = (pix_r * (255 - alpha) + cur_r * alpha) / 255;
191 uint32_t out_g = (pix_g * (255 - alpha) + cur_g * alpha) / 255;
192 uint32_t out_b = (pix_b * (255 - alpha) + cur_b * alpha) / 255;
193
194 return (out_r & 0xff) | (out_g & 0xff00) | (out_b & 0xff0000) | (gr_current & 0xff000000);
195}
196
Tao Bao9f426332018-06-13 10:39:44 -0700197// Increments pixel pointer right, with current rotation.
Luke Song846012f2017-09-13 15:56:16 -0700198static void incr_x(uint32_t** p, int row_pixels) {
Tao Bao44478df2018-07-31 22:01:03 -0700199 if (rotation == GRRotation::LEFT) {
200 *p = *p - row_pixels;
201 } else if (rotation == GRRotation::RIGHT) {
202 *p = *p + row_pixels;
203 } else if (rotation == GRRotation::DOWN) {
204 *p = *p - 1;
205 } else { // GRRotation::NONE
206 *p = *p + 1;
Luke Song846012f2017-09-13 15:56:16 -0700207 }
208}
209
Tao Bao9f426332018-06-13 10:39:44 -0700210// Increments pixel pointer down, with current rotation.
Luke Song846012f2017-09-13 15:56:16 -0700211static void incr_y(uint32_t** p, int row_pixels) {
Tao Bao44478df2018-07-31 22:01:03 -0700212 if (rotation == GRRotation::LEFT) {
213 *p = *p + 1;
214 } else if (rotation == GRRotation::RIGHT) {
215 *p = *p - 1;
216 } else if (rotation == GRRotation::DOWN) {
217 *p = *p - row_pixels;
218 } else { // GRRotation::NONE
219 *p = *p + row_pixels;
Luke Song846012f2017-09-13 15:56:16 -0700220 }
221}
222
Tao Bao9f426332018-06-13 10:39:44 -0700223// Returns pixel pointer at given coordinates with rotation adjustment.
Tao Bao92bdb5a2018-10-21 12:12:37 -0700224static uint32_t* PixelAt(GRSurface* surface, int x, int y, int row_pixels) {
Luke Song846012f2017-09-13 15:56:16 -0700225 switch (rotation) {
Tao Bao44478df2018-07-31 22:01:03 -0700226 case GRRotation::NONE:
Tao Bao92bdb5a2018-10-21 12:12:37 -0700227 return reinterpret_cast<uint32_t*>(surface->data()) + y * row_pixels + x;
Tao Bao44478df2018-07-31 22:01:03 -0700228 case GRRotation::RIGHT:
Tao Bao92bdb5a2018-10-21 12:12:37 -0700229 return reinterpret_cast<uint32_t*>(surface->data()) + x * row_pixels + (surface->width - y);
Tao Bao44478df2018-07-31 22:01:03 -0700230 case GRRotation::DOWN:
Tao Bao92bdb5a2018-10-21 12:12:37 -0700231 return reinterpret_cast<uint32_t*>(surface->data()) + (surface->height - 1 - y) * row_pixels +
232 (surface->width - 1 - x);
Tao Bao44478df2018-07-31 22:01:03 -0700233 case GRRotation::LEFT:
Tao Bao92bdb5a2018-10-21 12:12:37 -0700234 return reinterpret_cast<uint32_t*>(surface->data()) + (surface->height - 1 - x) * row_pixels +
235 y;
Luke Song846012f2017-09-13 15:56:16 -0700236 default:
Tao Bao44478df2018-07-31 22:01:03 -0700237 printf("invalid rotation %d", static_cast<int>(rotation));
Luke Song846012f2017-09-13 15:56:16 -0700238 }
239 return nullptr;
240}
241
Tao Bao92bdb5a2018-10-21 12:12:37 -0700242static void TextBlend(const uint8_t* src_p, int src_row_bytes, uint32_t* dst_p, int dst_row_pixels,
243 int width, int height) {
Luke Song846012f2017-09-13 15:56:16 -0700244 uint8_t alpha_current = static_cast<uint8_t>((alpha_mask & gr_current) >> 24);
245 for (int j = 0; j < height; ++j) {
Tao Bao92bdb5a2018-10-21 12:12:37 -0700246 const uint8_t* sx = src_p;
Luke Song846012f2017-09-13 15:56:16 -0700247 uint32_t* px = dst_p;
248 for (int i = 0; i < width; ++i, incr_x(&px, dst_row_pixels)) {
249 uint8_t a = *sx++;
250 if (alpha_current < 255) a = (static_cast<uint32_t>(a) * alpha_current) / 255;
251 *px = pixel_blend(a, *px);
Doug Zongker16f97c32014-03-06 16:16:05 -0800252 }
Luke Song846012f2017-09-13 15:56:16 -0700253 src_p += src_row_bytes;
254 incr_y(&dst_p, dst_row_pixels);
255 }
Doug Zongker16f97c32014-03-06 16:16:05 -0800256}
257
Doug Zongker16f97c32014-03-06 16:16:05 -0800258
Ethan Yonker39662b22017-05-23 08:34:02 -0500259#ifdef TW_NO_MINUI_CUSTOM_FONTS
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700260void gr_text(int x, int y, const char *s, bool bold)
Doug Zongker16f97c32014-03-06 16:16:05 -0800261{
Elliott Hughes01a4d082015-03-24 15:21:48 -0700262 GRFont* font = gr_font;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800263
Elliott Hughes01a4d082015-03-24 15:21:48 -0700264 if (!font->texture || gr_current_a == 0) return;
Doug Zongker55a36ac2013-03-04 15:49:02 -0800265
Ethan Yonker84d61ce2017-05-10 16:11:35 -0500266 bold = bold && (font->texture->height != font->char_height);
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800267
Doug Zongkerc560a672012-12-18 16:31:27 -0800268 x += overscan_offset_x;
269 y += overscan_offset_y;
270
Elliott Hughes01a4d082015-03-24 15:21:48 -0700271 unsigned char ch;
272 while ((ch = *s++)) {
Ethan Yonker84d61ce2017-05-10 16:11:35 -0500273 if (outside(x, y) || outside(x+font->char_width-1, y+font->char_height-1)) break;
Doug Zongker16f97c32014-03-06 16:16:05 -0800274
Elliott Hughes01a4d082015-03-24 15:21:48 -0700275 if (ch < ' ' || ch > '~') {
276 ch = '?';
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800277 }
Elliott Hughes01a4d082015-03-24 15:21:48 -0700278
Ethan Yonker84d61ce2017-05-10 16:11:35 -0500279 unsigned char* src_p = font->texture->data + ((ch - ' ') * font->char_width) +
280 (bold ? font->char_height * font->texture->row_bytes : 0);
Elliott Hughes01a4d082015-03-24 15:21:48 -0700281 unsigned char* dst_p = gr_draw->data + y*gr_draw->row_bytes + x*gr_draw->pixel_bytes;
282
Ethan Yonker58f21322018-08-24 11:17:36 -0500283 text_blend_old(src_p, font->texture->row_bytes,
284 dst_p, gr_draw->row_bytes,
285 font->char_width, font->char_height);
Elliott Hughes01a4d082015-03-24 15:21:48 -0700286
Ethan Yonker84d61ce2017-05-10 16:11:35 -0500287 x += font->char_width;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800288 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800289}
Ethan Yonker39662b22017-05-23 08:34:02 -0500290#else //TW_NO_MINUI_CUSTOM_FONTS
Luke Song846012f2017-09-13 15:56:16 -0700291void gr_text(const GRFont* font, int x, int y, const char* s, bool bold) {
292 if (!font || !font->texture || (gr_current & alpha_mask) == 0) return;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800293
Luke Song846012f2017-09-13 15:56:16 -0700294 if (font->texture->pixel_bytes != 1) {
295 printf("gr_text: font has wrong format\n");
296 return;
297 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800298
Luke Song846012f2017-09-13 15:56:16 -0700299 bold = bold && (font->texture->height != font->char_height);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800300
Luke Song846012f2017-09-13 15:56:16 -0700301 x += overscan_offset_x;
302 y += overscan_offset_y;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800303
Luke Song846012f2017-09-13 15:56:16 -0700304 unsigned char ch;
305 while ((ch = *s++)) {
306 if (outside(x, y) || outside(x + font->char_width - 1, y + font->char_height - 1)) break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800307
Luke Song846012f2017-09-13 15:56:16 -0700308 if (ch < ' ' || ch > '~') {
309 ch = '?';
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800310 }
Luke Song846012f2017-09-13 15:56:16 -0700311
312 int row_pixels = gr_draw->row_bytes / gr_draw->pixel_bytes;
Tao Bao92bdb5a2018-10-21 12:12:37 -0700313 const uint8_t* src_p = font->texture->data() + ((ch - ' ') * font->char_width) +
314 (bold ? font->char_height * font->texture->row_bytes : 0);
315 uint32_t* dst_p = PixelAt(gr_draw, x, y, row_pixels);
Luke Song846012f2017-09-13 15:56:16 -0700316
Tao Bao92bdb5a2018-10-21 12:12:37 -0700317 TextBlend(src_p, font->texture->row_bytes, dst_p, row_pixels, font->char_width,
318 font->char_height);
Luke Song846012f2017-09-13 15:56:16 -0700319
320 x += font->char_width;
321 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800322}
Ethan Yonker39662b22017-05-23 08:34:02 -0500323#endif //TW_NO_MINUI_CUSTOM_FONTS
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800324
Tao Bao92bdb5a2018-10-21 12:12:37 -0700325void gr_texticon(int x, int y, const GRSurface* icon) {
Tao Bao9f426332018-06-13 10:39:44 -0700326 if (icon == nullptr) return;
Doug Zongker16f97c32014-03-06 16:16:05 -0800327
Luke Song846012f2017-09-13 15:56:16 -0700328 if (icon->pixel_bytes != 1) {
329 printf("gr_texticon: source has wrong format\n");
330 return;
331 }
Doug Zongker02ec6b82012-08-22 17:26:40 -0700332
Luke Song846012f2017-09-13 15:56:16 -0700333 x += overscan_offset_x;
334 y += overscan_offset_y;
Doug Zongkerc560a672012-12-18 16:31:27 -0800335
Luke Song846012f2017-09-13 15:56:16 -0700336 if (outside(x, y) || outside(x + icon->width - 1, y + icon->height - 1)) return;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700337
Luke Song846012f2017-09-13 15:56:16 -0700338 int row_pixels = gr_draw->row_bytes / gr_draw->pixel_bytes;
Tao Bao92bdb5a2018-10-21 12:12:37 -0700339 const uint8_t* src_p = icon->data();
340 uint32_t* dst_p = PixelAt(gr_draw, x, y, row_pixels);
341 TextBlend(src_p, icon->row_bytes, dst_p, row_pixels, icon->width, icon->height);
Doug Zongker16f97c32014-03-06 16:16:05 -0800342}
343
Ethan Yonker58f21322018-08-24 11:17:36 -0500344void gr_convert_rgb_555(unsigned char r, unsigned char g, unsigned char b)
Ethan Yonkere96182e2015-10-13 19:32:03 -0500345{
Ethan Yonker58f21322018-08-24 11:17:36 -0500346 gr_current_r5 = (((r & 0xFF) * 0x1F) + 0x7F) / 0xFF;
347 gr_current_g5 = (((g & 0xFF) * 0x3F) + 0x7F) / 0xFF;
348 gr_current_b5 = (((b & 0xFF) * 0x1F) + 0x7F) / 0xFF;
Ethan Yonkere96182e2015-10-13 19:32:03 -0500349
350 rgb_555[0] = (gr_current_g5 << 5) + (gr_current_b5);
351 rgb_555[1] = (gr_current_r5 << 3) + (gr_current_g5 >> 3);
352}
353
Luke Song846012f2017-09-13 15:56:16 -0700354void gr_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a) {
Ethan Yonker58f21322018-08-24 11:17:36 -0500355#ifdef TW_NO_MINUI_CUSTOM_FONTS
Tony Kuofd778e32015-02-05 21:25:56 +0800356#if defined(RECOVERY_ABGR) || defined(RECOVERY_BGRA)
Ethan Yonker58f21322018-08-24 11:17:36 -0500357 gr_current_r = b;
358 gr_current_g = g;
359 gr_current_b = r;
Tony Kuofd778e32015-02-05 21:25:56 +0800360#else
Ethan Yonker58f21322018-08-24 11:17:36 -0500361 gr_current_r = r;
362 gr_current_g = g;
363 gr_current_b = b;
Tony Kuofd778e32015-02-05 21:25:56 +0800364#endif
Ethan Yonker58f21322018-08-24 11:17:36 -0500365#endif
366
367 if (gr_draw->pixel_bytes == 2) {
368 gr_current_a = a;
369 gr_convert_rgb_555(r, g, b);
370 return;
371 }
372
Luke Song846012f2017-09-13 15:56:16 -0700373 uint32_t r32 = r, g32 = g, b32 = b, a32 = a;
Adrian Salidocc7b7eb2019-12-12 20:18:09 -0800374 if (pixel_format == PixelFormat::ARGB || pixel_format == PixelFormat::BGRA) {
Tao Baoed876a72018-07-31 21:32:50 -0700375 gr_current = (a32 << 24) | (r32 << 16) | (g32 << 8) | b32;
376 } else {
377 gr_current = (a32 << 24) | (b32 << 16) | (g32 << 8) | r32;
378 }
Doug Zongker16f97c32014-03-06 16:16:05 -0800379}
380
Luke Song846012f2017-09-13 15:56:16 -0700381void gr_clear() {
Ethan Yonker58f21322018-08-24 11:17:36 -0500382 if (gr_draw->pixel_bytes == 2) {
383 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
384 return;
385 }
Ethan Yonkere96182e2015-10-13 19:32:03 -0500386
Ethan Yonker58f21322018-08-24 11:17:36 -0500387 // This code only works on 32bpp devices
Luke Song846012f2017-09-13 15:56:16 -0700388 if ((gr_current & 0xff) == ((gr_current >> 8) & 0xff) &&
389 (gr_current & 0xff) == ((gr_current >> 16) & 0xff) &&
390 (gr_current & 0xff) == ((gr_current >> 24) & 0xff) &&
391 gr_draw->row_bytes == gr_draw->width * gr_draw->pixel_bytes) {
Tao Bao92bdb5a2018-10-21 12:12:37 -0700392 memset(gr_draw->data(), gr_current & 0xff, gr_draw->height * gr_draw->row_bytes);
Luke Song846012f2017-09-13 15:56:16 -0700393 } else {
Tao Bao92bdb5a2018-10-21 12:12:37 -0700394 uint32_t* px = reinterpret_cast<uint32_t*>(gr_draw->data());
Luke Song846012f2017-09-13 15:56:16 -0700395 int row_diff = gr_draw->row_bytes / gr_draw->pixel_bytes - gr_draw->width;
396 for (int y = 0; y < gr_draw->height; ++y) {
397 for (int x = 0; x < gr_draw->width; ++x) {
398 *px++ = gr_current;
399 }
400 px += row_diff;
Doug Zongker16f97c32014-03-06 16:16:05 -0800401 }
Luke Song846012f2017-09-13 15:56:16 -0700402 }
Doug Zongker02ec6b82012-08-22 17:26:40 -0700403}
404
Luke Song846012f2017-09-13 15:56:16 -0700405void gr_fill(int x1, int y1, int x2, int y2) {
406 x1 += overscan_offset_x;
407 y1 += overscan_offset_y;
Doug Zongkerc560a672012-12-18 16:31:27 -0800408
Luke Song846012f2017-09-13 15:56:16 -0700409 x2 += overscan_offset_x;
410 y2 += overscan_offset_y;
Doug Zongkerc560a672012-12-18 16:31:27 -0800411
Luke Song846012f2017-09-13 15:56:16 -0700412 if (outside(x1, y1) || outside(x2 - 1, y2 - 1)) return;
Doug Zongker16f97c32014-03-06 16:16:05 -0800413
Luke Song846012f2017-09-13 15:56:16 -0700414 int row_pixels = gr_draw->row_bytes / gr_draw->pixel_bytes;
Tao Bao92bdb5a2018-10-21 12:12:37 -0700415 uint32_t* p = PixelAt(gr_draw, x1, y1, row_pixels);
Luke Song846012f2017-09-13 15:56:16 -0700416 uint8_t alpha = static_cast<uint8_t>(((gr_current & alpha_mask) >> 24));
417 if (alpha > 0) {
418 for (int y = y1; y < y2; ++y) {
419 uint32_t* px = p;
420 for (int x = x1; x < x2; ++x) {
421 *px = pixel_blend(alpha, *px);
422 incr_x(&px, row_pixels);
423 }
424 incr_y(&p, row_pixels);
Doug Zongker16f97c32014-03-06 16:16:05 -0800425 }
Luke Song846012f2017-09-13 15:56:16 -0700426 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800427}
428
bigbiff20edca82020-07-03 09:55:28 -0400429void gr_blit_32to16(const GRSurface* source, int sx, int sy, int w, int h, int dx, int dy) {
Ethan Yonker58f21322018-08-24 11:17:36 -0500430 dx += overscan_offset_x;
431 dy += overscan_offset_y;
Ethan Yonkere96182e2015-10-13 19:32:03 -0500432
Ethan Yonker58f21322018-08-24 11:17:36 -0500433 if (outside(dx, dy) || outside(dx+w-1, dy+h-1)) return;
Ethan Yonkere96182e2015-10-13 19:32:03 -0500434
bigbiff20edca82020-07-03 09:55:28 -0400435 unsigned char* src_p = (unsigned char*) source->data() + sy*source->row_bytes + sx*source->pixel_bytes;
436 unsigned char* dst_p = gr_draw->data() + dy*gr_draw->row_bytes + dx*gr_draw->pixel_bytes;
Ethan Yonkere96182e2015-10-13 19:32:03 -0500437
Ethan Yonker58f21322018-08-24 11:17:36 -0500438 int i, j;
439 for (i = 0; i < h; ++i) {
440 unsigned char* spx = src_p;
441 unsigned char* dpx = dst_p;
Ethan Yonkere96182e2015-10-13 19:32:03 -0500442
Ethan Yonker58f21322018-08-24 11:17:36 -0500443 for (j = 0; j < w; ++j) {
444 unsigned a = spx[3];
445
446 if (a == 0) {
447 spx += source->pixel_bytes;
448 dpx += gr_draw->pixel_bytes;
449 } else {
450 unsigned r5 = (((*spx++ & 0xFF) * 0x1F) + 0x7F) / 0xFF;
451 unsigned g5 = (((*spx++ & 0xFF) * 0x3F) + 0x7F) / 0xFF;
452 unsigned b5 = (((*spx++ & 0xFF) * 0x1F) + 0x7F) / 0xFF;
453 spx++;
454 if (a == 255) {
455 *dpx++ = (g5 << 5) + (b5);
456 *dpx++ = (r5 << 3) + (g5 >> 3);
457 } else {
458 blend_16bpp(dpx, r5, g5, b5, a);
459 spx += source->pixel_bytes;
Ethan Yonkere96182e2015-10-13 19:32:03 -0500460 }
Ethan Yonker58f21322018-08-24 11:17:36 -0500461 }
Ethan Yonkere96182e2015-10-13 19:32:03 -0500462 }
Ethan Yonker58f21322018-08-24 11:17:36 -0500463 src_p += source->row_bytes;
464 dst_p += gr_draw->row_bytes;
465 }
Ethan Yonkere96182e2015-10-13 19:32:03 -0500466}
467
Tao Bao92bdb5a2018-10-21 12:12:37 -0700468void gr_blit(const GRSurface* source, int sx, int sy, int w, int h, int dx, int dy) {
Tao Bao9f426332018-06-13 10:39:44 -0700469 if (source == nullptr) return;
Doug Zongker16f97c32014-03-06 16:16:05 -0800470
Luke Song846012f2017-09-13 15:56:16 -0700471 if (gr_draw->pixel_bytes != source->pixel_bytes) {
Ethan Yonker58f21322018-08-24 11:17:36 -0500472 if (gr_draw->pixel_bytes == 2 && source->pixel_bytes == 4) {
473 gr_blit_32to16(source, sx, sy, w, h, dx, dy);
474 return;
475 } else {
476 printf("gr_blit: source has wrong format\n");
477 return;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800478 }
Luke Song846012f2017-09-13 15:56:16 -0700479 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800480
Luke Song846012f2017-09-13 15:56:16 -0700481 dx += overscan_offset_x;
482 dy += overscan_offset_y;
Doug Zongkerc560a672012-12-18 16:31:27 -0800483
Luke Song846012f2017-09-13 15:56:16 -0700484 if (outside(dx, dy) || outside(dx + w - 1, dy + h - 1)) return;
Doug Zongker16f97c32014-03-06 16:16:05 -0800485
Tao Bao44478df2018-07-31 22:01:03 -0700486 if (rotation != GRRotation::NONE) {
Luke Song846012f2017-09-13 15:56:16 -0700487 int src_row_pixels = source->row_bytes / source->pixel_bytes;
488 int row_pixels = gr_draw->row_bytes / gr_draw->pixel_bytes;
Tao Bao92bdb5a2018-10-21 12:12:37 -0700489 const uint32_t* src_py =
490 reinterpret_cast<const uint32_t*>(source->data()) + sy * source->row_bytes / 4 + sx;
491 uint32_t* dst_py = PixelAt(gr_draw, dx, dy, row_pixels);
Luke Song846012f2017-09-13 15:56:16 -0700492
493 for (int y = 0; y < h; y += 1) {
Tao Bao92bdb5a2018-10-21 12:12:37 -0700494 const uint32_t* src_px = src_py;
Luke Song846012f2017-09-13 15:56:16 -0700495 uint32_t* dst_px = dst_py;
496 for (int x = 0; x < w; x += 1) {
497 *dst_px = *src_px++;
498 incr_x(&dst_px, row_pixels);
499 }
500 src_py += src_row_pixels;
501 incr_y(&dst_py, row_pixels);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800502 }
Luke Song846012f2017-09-13 15:56:16 -0700503 } else {
Tao Bao92bdb5a2018-10-21 12:12:37 -0700504 const uint8_t* src_p = source->data() + sy * source->row_bytes + sx * source->pixel_bytes;
505 uint8_t* dst_p = gr_draw->data() + dy * gr_draw->row_bytes + dx * gr_draw->pixel_bytes;
Doug Zongker16f97c32014-03-06 16:16:05 -0800506
Tao Bao9f426332018-06-13 10:39:44 -0700507 for (int i = 0; i < h; ++i) {
Luke Song846012f2017-09-13 15:56:16 -0700508 memcpy(dst_p, src_p, w * source->pixel_bytes);
509 src_p += source->row_bytes;
510 dst_p += gr_draw->row_bytes;
Doug Zongker16f97c32014-03-06 16:16:05 -0800511 }
Luke Song846012f2017-09-13 15:56:16 -0700512 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800513}
514
Tao Bao9f426332018-06-13 10:39:44 -0700515unsigned int gr_get_width(const GRSurface* surface) {
516 if (surface == nullptr) {
Luke Song846012f2017-09-13 15:56:16 -0700517 return 0;
518 }
519 return surface->width;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800520}
521
Tao Bao9f426332018-06-13 10:39:44 -0700522unsigned int gr_get_height(const GRSurface* surface) {
523 if (surface == nullptr) {
Luke Song846012f2017-09-13 15:56:16 -0700524 return 0;
525 }
526 return surface->height;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800527}
528
Ethan Yonker39662b22017-05-23 08:34:02 -0500529#ifdef TW_NO_MINUI_CUSTOM_FONTS
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800530static void gr_init_font(void)
531{
Elliott Hughes07cfb8f2015-04-10 13:12:05 -0700532 gr_font = reinterpret_cast<GRFont*>(calloc(sizeof(*gr_font), 1));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800533
Doug Zongkera418aa72014-03-17 12:10:02 -0700534 int res = res_create_alpha_surface("font", &(gr_font->texture));
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800535 if (res == 0) {
536 // The font image should be a 96x2 array of character images. The
537 // columns are the printable ASCII characters 0x20 - 0x7f. The
538 // top row is regular text; the bottom row is bold.
Ethan Yonker84d61ce2017-05-10 16:11:35 -0500539 gr_font->char_width = gr_font->texture->width / 96;
540 gr_font->char_height = gr_font->texture->height / 2;
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800541 } else {
Doug Zongker55a36ac2013-03-04 15:49:02 -0800542 printf("failed to read font: res=%d\n", res);
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800543
544 // fall back to the compiled-in font.
Elliott Hughes07cfb8f2015-04-10 13:12:05 -0700545 gr_font->texture = reinterpret_cast<GRSurface*>(malloc(sizeof(*gr_font->texture)));
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800546 gr_font->texture->width = font.width;
547 gr_font->texture->height = font.height;
Doug Zongker16f97c32014-03-06 16:16:05 -0800548 gr_font->texture->row_bytes = font.width;
549 gr_font->texture->pixel_bytes = 1;
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800550
Elliott Hughes07cfb8f2015-04-10 13:12:05 -0700551 unsigned char* bits = reinterpret_cast<unsigned char*>(malloc(font.width * font.height));
552 gr_font->texture->data = reinterpret_cast<unsigned char*>(bits);
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800553
554 unsigned char data;
555 unsigned char* in = font.rundata;
556 while((data = *in++)) {
557 memset(bits, (data & 0x80) ? 255 : 0, data & 0x7f);
558 bits += (data & 0x7f);
559 }
560
Ethan Yonker84d61ce2017-05-10 16:11:35 -0500561 gr_font->char_width = font.char_width;
562 gr_font->char_height = font.char_height;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800563 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800564}
565
Ethan Yonker84d61ce2017-05-10 16:11:35 -0500566void gr_set_font(__attribute__ ((unused))const char* name) {
567 //this cm function is made to change font. Don't care, just init the font:
568 gr_init_font();
569 return;
570}
Ethan Yonker39662b22017-05-23 08:34:02 -0500571#else // TW_NO_MINUI_CUSTOM_FONTS
Damien Bargiacchid00f5eb2016-09-09 07:14:08 -0700572int gr_init_font(const char* name, GRFont** dest) {
Luke Song846012f2017-09-13 15:56:16 -0700573 GRFont* font = static_cast<GRFont*>(calloc(1, sizeof(*gr_font)));
574 if (font == nullptr) {
575 return -1;
576 }
Damien Bargiacchid00f5eb2016-09-09 07:14:08 -0700577
Luke Song846012f2017-09-13 15:56:16 -0700578 int res = res_create_alpha_surface(name, &(font->texture));
579 if (res < 0) {
580 free(font);
581 return res;
582 }
Damien Bargiacchi35fff612016-08-11 15:57:03 -0700583
Luke Song846012f2017-09-13 15:56:16 -0700584 // The font image should be a 96x2 array of character images. The
585 // columns are the printable ASCII characters 0x20 - 0x7f. The
586 // top row is regular text; the bottom row is bold.
587 font->char_width = font->texture->width / 96;
588 font->char_height = font->texture->height / 2;
Damien Bargiacchid00f5eb2016-09-09 07:14:08 -0700589
Luke Song846012f2017-09-13 15:56:16 -0700590 *dest = font;
Damien Bargiacchi35fff612016-08-11 15:57:03 -0700591
Luke Song846012f2017-09-13 15:56:16 -0700592 return 0;
Damien Bargiacchi35fff612016-08-11 15:57:03 -0700593}
bigbiff20edca82020-07-03 09:55:28 -0400594#endif // TW_NO_MINUI_CUSTOM_FONTS
Damien Bargiacchi35fff612016-08-11 15:57:03 -0700595
Doug Zongker5290f202014-03-11 13:22:04 -0700596void gr_flip() {
Tao Bao557fa1f2017-02-07 12:51:00 -0800597 gr_draw = gr_backend->Flip();
Doug Zongker5290f202014-03-11 13:22:04 -0700598}
599
Tao Bao557fa1f2017-02-07 12:51:00 -0800600int gr_init() {
Tao Baoed876a72018-07-31 21:32:50 -0700601 // pixel_format needs to be set before loading any resources or initializing backends.
Tao Bao050feb02018-09-05 21:45:42 -0700602 std::string format = android::base::GetProperty("ro.minui.pixel_format", "");
Tao Baoed876a72018-07-31 21:32:50 -0700603 if (format == "ABGR_8888") {
604 pixel_format = PixelFormat::ABGR;
605 } else if (format == "RGBX_8888") {
606 pixel_format = PixelFormat::RGBX;
Adrian Salidocc7b7eb2019-12-12 20:18:09 -0800607 } else if (format == "ARGB_8888") {
608 pixel_format = PixelFormat::ARGB;
Tao Baoed876a72018-07-31 21:32:50 -0700609 } else if (format == "BGRA_8888") {
610 pixel_format = PixelFormat::BGRA;
611 } else {
612 pixel_format = PixelFormat::UNKNOWN;
613 }
614
Tianjie Xu55a2c4e2018-03-29 11:07:50 -0700615 int ret = gr_init_font("font", &gr_font);
616 if (ret != 0) {
Tianjie Xu842f2a32018-05-31 18:16:28 -0700617 printf("Failed to init font: %d, continuing graphic backend initialization without font file\n",
618 ret);
Tianjie Xu55a2c4e2018-03-29 11:07:50 -0700619 }
Doug Zongker16f97c32014-03-06 16:16:05 -0800620
bigbiff20edca82020-07-03 09:55:28 -0400621 auto backend = std::unique_ptr<MinuiBackend>{ std::make_unique<MinuiBackendAdf>() };
Tao Bao557fa1f2017-02-07 12:51:00 -0800622 gr_draw = backend->Init();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800623
Ethan Yonkerb386f712017-01-20 14:30:28 -0600624#ifdef MSM_BSP
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500625 if (gr_draw) {
626 printf("Using overlay graphics.\n");
Ethan Yonkera59da092015-10-13 19:35:05 -0500627 }
Ethan Yonkerb386f712017-01-20 14:30:28 -0600628#endif
Ethan Yonkera59da092015-10-13 19:35:05 -0500629
Greg Hackmann41909dd2014-04-25 10:39:50 -0700630
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500631 if (!gr_draw) {
632 backend = std::make_unique<MinuiBackendDrm>();
633 gr_draw = backend->Init();
Ethan Yonkera59da092015-10-13 19:35:05 -0500634 if (gr_draw)
635 printf("Using drm graphics.\n");
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700636 }
637
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500638 if (!gr_draw) {
639 backend = std::make_unique<MinuiBackendFbdev>();
640 gr_draw = backend->Init();
641 if (gr_draw)
Ethan Yonkera59da092015-10-13 19:35:05 -0500642 printf("Using fbdev graphics.\n");
bigbiff20edca82020-07-03 09:55:28 -0400643 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800644
Tao Bao557fa1f2017-02-07 12:51:00 -0800645 if (!gr_draw) {
646 return -1;
647 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800648
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800649
Tao Bao050feb02018-09-05 21:45:42 -0700650 int overscan_percent = android::base::GetIntProperty("ro.minui.overscan_percent", 0);
Tao Bao557fa1f2017-02-07 12:51:00 -0800651 overscan_offset_x = gr_draw->width * overscan_percent / 100;
652 overscan_offset_y = gr_draw->height * overscan_percent / 100;
653
654 gr_flip();
655 gr_flip();
Tianjie Xuccf00a22018-06-05 17:10:23 -0700656 if (!gr_draw) {
657 printf("gr_init: gr_draw becomes nullptr after gr_flip\n");
658 return -1;
659 }
Tao Bao557fa1f2017-02-07 12:51:00 -0800660
Tao Baoed876a72018-07-31 21:32:50 -0700661 std::string rotation_str =
Tao Bao050feb02018-09-05 21:45:42 -0700662 android::base::GetProperty("ro.minui.default_rotation", "ROTATION_NONE");
Tao Bao44478df2018-07-31 22:01:03 -0700663 if (rotation_str == "ROTATION_RIGHT") {
664 gr_rotate(GRRotation::RIGHT);
665 } else if (rotation_str == "ROTATION_DOWN") {
666 gr_rotate(GRRotation::DOWN);
667 } else if (rotation_str == "ROTATION_LEFT") {
668 gr_rotate(GRRotation::LEFT);
Tao Baoed876a72018-07-31 21:32:50 -0700669 } else { // "ROTATION_NONE" or unknown string
Tao Bao44478df2018-07-31 22:01:03 -0700670 gr_rotate(GRRotation::NONE);
671 }
Luke Song846012f2017-09-13 15:56:16 -0700672
673 if (gr_draw->pixel_bytes != 4) {
674 printf("gr_init: Only 4-byte pixel formats supported\n");
675 }
676
Tao Bao557fa1f2017-02-07 12:51:00 -0800677 return 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800678}
679
Tao Bao557fa1f2017-02-07 12:51:00 -0800680void gr_exit() {
681 delete gr_backend;
Tao Bao9f426332018-06-13 10:39:44 -0700682 gr_backend = nullptr;
683
684 delete gr_font;
685 gr_font = nullptr;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800686}
687
Tao Bao557fa1f2017-02-07 12:51:00 -0800688int gr_fb_width() {
Tao Bao44478df2018-07-31 22:01:03 -0700689 return (rotation == GRRotation::LEFT || rotation == GRRotation::RIGHT)
690 ? gr_draw->height - 2 * overscan_offset_y
691 : gr_draw->width - 2 * overscan_offset_x;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800692}
693
Tao Bao557fa1f2017-02-07 12:51:00 -0800694int gr_fb_height() {
Tao Bao44478df2018-07-31 22:01:03 -0700695 return (rotation == GRRotation::LEFT || rotation == GRRotation::RIGHT)
696 ? gr_draw->width - 2 * overscan_offset_x
697 : gr_draw->height - 2 * overscan_offset_y;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800698}
Dima Zavin4daf48a2011-08-30 11:59:20 -0700699
Tao Bao557fa1f2017-02-07 12:51:00 -0800700void gr_fb_blank(bool blank) {
701 gr_backend->Blank(blank);
Dima Zavin4daf48a2011-08-30 11:59:20 -0700702}
Luke Song846012f2017-09-13 15:56:16 -0700703
704void gr_rotate(GRRotation rot) {
705 rotation = rot;
706}