blob: 38ba6cab77c9c188eadedcc4b778d26447a32b5f [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
Tao Bao557fa1f2017-02-07 12:51:00 -080034#include "graphics_drm.h"
35#include "graphics_fbdev.h"
Tao Bao0ecbd762017-01-16 21:16:58 -080036#include "minui/minui.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080037
Tao Bao9f426332018-06-13 10:39:44 -070038static GRFont* gr_font = nullptr;
Tao Bao557fa1f2017-02-07 12:51:00 -080039static MinuiBackend* gr_backend = nullptr;
Doug Zongker16f97c32014-03-06 16:16:05 -080040
Doug Zongkerc560a672012-12-18 16:31:27 -080041static int overscan_offset_x = 0;
42static int overscan_offset_y = 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080043
Ethan Yonker58f21322018-08-24 11:17:36 -050044#ifdef TW_NO_MINUI_CUSTOM_FONTS
Doug Zongker16f97c32014-03-06 16:16:05 -080045static unsigned char gr_current_r = 255;
46static unsigned char gr_current_g = 255;
47static unsigned char gr_current_b = 255;
Ethan Yonker58f21322018-08-24 11:17:36 -050048#endif
Doug Zongker16f97c32014-03-06 16:16:05 -080049static unsigned char gr_current_a = 255;
Ethan Yonkere96182e2015-10-13 19:32:03 -050050static unsigned char rgb_555[2];
51static unsigned char gr_current_r5 = 31;
52static unsigned char gr_current_g5 = 63;
53static unsigned char gr_current_b5 = 31;
Doug Zongker16f97c32014-03-06 16:16:05 -080054
Luke Song846012f2017-09-13 15:56:16 -070055static uint32_t gr_current = ~0;
56static constexpr uint32_t alpha_mask = 0xff000000;
Doug Zongker16f97c32014-03-06 16:16:05 -080057
Tao Bao9f426332018-06-13 10:39:44 -070058// gr_draw is owned by backends.
Tao Bao92bdb5a2018-10-21 12:12:37 -070059static GRSurface* gr_draw = nullptr;
Tao Bao44478df2018-07-31 22:01:03 -070060static GRRotation rotation = GRRotation::NONE;
Tao Baoed876a72018-07-31 21:32:50 -070061static PixelFormat pixel_format = PixelFormat::UNKNOWN;
Doug Zongker16f97c32014-03-06 16:16:05 -080062
Luke Song846012f2017-09-13 15:56:16 -070063static bool outside(int x, int y) {
Tao Bao44478df2018-07-31 22:01:03 -070064 auto swapped = (rotation == GRRotation::LEFT || rotation == GRRotation::RIGHT);
65 return x < 0 || x >= (swapped ? gr_draw->height : gr_draw->width) || y < 0 ||
66 y >= (swapped ? gr_draw->width : gr_draw->height);
Doug Zongker16f97c32014-03-06 16:16:05 -080067}
Ethan Yonker39662b22017-05-23 08:34:02 -050068
69#ifdef TW_NO_MINUI_CUSTOM_FONTS
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080070int gr_measure(const char *s)
71{
Ethan Yonker84d61ce2017-05-10 16:11:35 -050072 return gr_font->char_width * strlen(s);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080073}
74
Dima Zavin3c7f00e2011-08-30 11:58:24 -070075void gr_font_size(int *x, int *y)
76{
Ethan Yonker84d61ce2017-05-10 16:11:35 -050077 *x = gr_font->char_width;
78 *y = gr_font->char_height;
Dima Zavin3c7f00e2011-08-30 11:58:24 -070079}
Ethan Yonker84d61ce2017-05-10 16:11:35 -050080#else // TW_USE_MINUI_CUSTOM_FONTS
Luke Song846012f2017-09-13 15:56:16 -070081const GRFont* gr_sys_font() {
82 return gr_font;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080083}
84
Tao Baoed876a72018-07-31 21:32:50 -070085PixelFormat gr_pixel_format() {
86 return pixel_format;
87}
88
Luke Song846012f2017-09-13 15:56:16 -070089int gr_measure(const GRFont* font, const char* s) {
Tianjie Xu842f2a32018-05-31 18:16:28 -070090 if (font == nullptr) {
91 return -1;
92 }
93
Luke Song846012f2017-09-13 15:56:16 -070094 return font->char_width * strlen(s);
Damien Bargiacchi35fff612016-08-11 15:57:03 -070095}
96
Tianjie Xu842f2a32018-05-31 18:16:28 -070097int gr_font_size(const GRFont* font, int* x, int* y) {
98 if (font == nullptr) {
99 return -1;
100 }
101
Luke Song846012f2017-09-13 15:56:16 -0700102 *x = font->char_width;
103 *y = font->char_height;
Tianjie Xu842f2a32018-05-31 18:16:28 -0700104 return 0;
Dima Zavin3c7f00e2011-08-30 11:58:24 -0700105}
Ethan Yonker39662b22017-05-23 08:34:02 -0500106#endif // TW_NO_MINUI_CUSTOM_FONTS
Dima Zavin3c7f00e2011-08-30 11:58:24 -0700107
Ethan Yonkere96182e2015-10-13 19:32:03 -0500108void blend_16bpp(unsigned char* px, unsigned r5, unsigned g5, unsigned b5, unsigned char a)
109{
110 unsigned char orig[2];
111 orig[0] = px[0];
112 orig[1] = px[1];
113
114 /* This code is a little easier to read
115 unsigned oldred = (orig[1] >> 3);
116 unsigned oldgreen = (((orig[0] >> 5) << 3) + (orig[1] & 0x7));
117 unsigned oldblue = (orig[0] & 0x1F);
118
119 unsigned newred = (oldred * (255-a) + r5 * a) / 255;
120 unsigned newgreen = (oldgreen * (255-a) + g5 * a) / 255;
121 unsigned newblue = (oldblue * (255-a) + b5 * a) / 255;
122 */
123
124 unsigned newred = ((orig[1] >> 3) * (255-a) + r5 * a) / 255;
125 unsigned newgreen = ((((orig[0] >> 5) << 3) + (orig[1] & 0x7)) * (255-a) + g5 * a) / 255;
126 unsigned newblue = ((orig[0] & 0x1F) * (255-a) + b5 * a) / 255;
127
128 *px++ = (newgreen << 5) + (newblue);
129 *px++ = (newred << 3) + (newgreen >> 3);
130}
131
Ethan Yonker58f21322018-08-24 11:17:36 -0500132#ifdef TW_NO_MINUI_CUSTOM_FONTS
133static void text_blend_old(unsigned char* src_p, int src_row_bytes,
Doug Zongker16f97c32014-03-06 16:16:05 -0800134 unsigned char* dst_p, int dst_row_bytes,
135 int width, int height)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800136{
Elliott Hughes01a4d082015-03-24 15:21:48 -0700137 for (int j = 0; j < height; ++j) {
Doug Zongker16f97c32014-03-06 16:16:05 -0800138 unsigned char* sx = src_p;
139 unsigned char* px = dst_p;
Elliott Hughes01a4d082015-03-24 15:21:48 -0700140 for (int i = 0; i < width; ++i) {
Doug Zongker16f97c32014-03-06 16:16:05 -0800141 unsigned char a = *sx++;
142 if (gr_current_a < 255) a = ((int)a * gr_current_a) / 255;
143 if (a == 255) {
Ethan Yonkere96182e2015-10-13 19:32:03 -0500144 if (gr_draw->pixel_bytes == 2) {
145 *px++ = rgb_555[0];
146 *px++ = rgb_555[1];
147 } else {
148 *px++ = gr_current_r;
149 *px++ = gr_current_g;
150 *px++ = gr_current_b;
151 px++;
152 }
Doug Zongker16f97c32014-03-06 16:16:05 -0800153 } else if (a > 0) {
Ethan Yonkere96182e2015-10-13 19:32:03 -0500154 if (gr_draw->pixel_bytes == 2) {
155 blend_16bpp(px, gr_current_r5, gr_current_g5, gr_current_b5, a);
156 px += gr_draw->pixel_bytes;
157 } else {
158 *px = (*px * (255-a) + gr_current_r * a) / 255;
159 ++px;
160 *px = (*px * (255-a) + gr_current_g * a) / 255;
161 ++px;
162 *px = (*px * (255-a) + gr_current_b * a) / 255;
163 ++px;
164 ++px;
165 }
Doug Zongker16f97c32014-03-06 16:16:05 -0800166 } else {
Ethan Yonkere96182e2015-10-13 19:32:03 -0500167 px += gr_draw->pixel_bytes;
Doug Zongker16f97c32014-03-06 16:16:05 -0800168 }
169 }
170 src_p += src_row_bytes;
171 dst_p += dst_row_bytes;
172 }
173}
Ethan Yonker58f21322018-08-24 11:17:36 -0500174#endif // TW_NO_MINUI_CUSTOM_FONTS
175
176// Blends gr_current onto pix value, assumes alpha as most significant byte.
Luke Song846012f2017-09-13 15:56:16 -0700177static inline uint32_t pixel_blend(uint8_t alpha, uint32_t pix) {
178 if (alpha == 255) return gr_current;
179 if (alpha == 0) return pix;
180 uint32_t pix_r = pix & 0xff;
181 uint32_t pix_g = pix & 0xff00;
182 uint32_t pix_b = pix & 0xff0000;
183 uint32_t cur_r = gr_current & 0xff;
184 uint32_t cur_g = gr_current & 0xff00;
185 uint32_t cur_b = gr_current & 0xff0000;
186
187 uint32_t out_r = (pix_r * (255 - alpha) + cur_r * alpha) / 255;
188 uint32_t out_g = (pix_g * (255 - alpha) + cur_g * alpha) / 255;
189 uint32_t out_b = (pix_b * (255 - alpha) + cur_b * alpha) / 255;
190
191 return (out_r & 0xff) | (out_g & 0xff00) | (out_b & 0xff0000) | (gr_current & 0xff000000);
192}
193
Tao Bao9f426332018-06-13 10:39:44 -0700194// Increments pixel pointer right, with current rotation.
Luke Song846012f2017-09-13 15:56:16 -0700195static void incr_x(uint32_t** p, int row_pixels) {
Tao Bao44478df2018-07-31 22:01:03 -0700196 if (rotation == GRRotation::LEFT) {
197 *p = *p - row_pixels;
198 } else if (rotation == GRRotation::RIGHT) {
199 *p = *p + row_pixels;
200 } else if (rotation == GRRotation::DOWN) {
201 *p = *p - 1;
202 } else { // GRRotation::NONE
203 *p = *p + 1;
Luke Song846012f2017-09-13 15:56:16 -0700204 }
205}
206
Tao Bao9f426332018-06-13 10:39:44 -0700207// Increments pixel pointer down, with current rotation.
Luke Song846012f2017-09-13 15:56:16 -0700208static void incr_y(uint32_t** p, int row_pixels) {
Tao Bao44478df2018-07-31 22:01:03 -0700209 if (rotation == GRRotation::LEFT) {
210 *p = *p + 1;
211 } else if (rotation == GRRotation::RIGHT) {
212 *p = *p - 1;
213 } else if (rotation == GRRotation::DOWN) {
214 *p = *p - row_pixels;
215 } else { // GRRotation::NONE
216 *p = *p + row_pixels;
Luke Song846012f2017-09-13 15:56:16 -0700217 }
218}
219
Tao Bao9f426332018-06-13 10:39:44 -0700220// Returns pixel pointer at given coordinates with rotation adjustment.
Tao Bao92bdb5a2018-10-21 12:12:37 -0700221static uint32_t* PixelAt(GRSurface* surface, int x, int y, int row_pixels) {
Luke Song846012f2017-09-13 15:56:16 -0700222 switch (rotation) {
Tao Bao44478df2018-07-31 22:01:03 -0700223 case GRRotation::NONE:
Tao Bao92bdb5a2018-10-21 12:12:37 -0700224 return reinterpret_cast<uint32_t*>(surface->data()) + y * row_pixels + x;
Tao Bao44478df2018-07-31 22:01:03 -0700225 case GRRotation::RIGHT:
Tao Bao92bdb5a2018-10-21 12:12:37 -0700226 return reinterpret_cast<uint32_t*>(surface->data()) + x * row_pixels + (surface->width - y);
Tao Bao44478df2018-07-31 22:01:03 -0700227 case GRRotation::DOWN:
Tao Bao92bdb5a2018-10-21 12:12:37 -0700228 return reinterpret_cast<uint32_t*>(surface->data()) + (surface->height - 1 - y) * row_pixels +
229 (surface->width - 1 - x);
Tao Bao44478df2018-07-31 22:01:03 -0700230 case GRRotation::LEFT:
Tao Bao92bdb5a2018-10-21 12:12:37 -0700231 return reinterpret_cast<uint32_t*>(surface->data()) + (surface->height - 1 - x) * row_pixels +
232 y;
Luke Song846012f2017-09-13 15:56:16 -0700233 default:
Tao Bao44478df2018-07-31 22:01:03 -0700234 printf("invalid rotation %d", static_cast<int>(rotation));
Luke Song846012f2017-09-13 15:56:16 -0700235 }
236 return nullptr;
237}
238
Tao Bao92bdb5a2018-10-21 12:12:37 -0700239static void TextBlend(const uint8_t* src_p, int src_row_bytes, uint32_t* dst_p, int dst_row_pixels,
240 int width, int height) {
Luke Song846012f2017-09-13 15:56:16 -0700241 uint8_t alpha_current = static_cast<uint8_t>((alpha_mask & gr_current) >> 24);
242 for (int j = 0; j < height; ++j) {
Tao Bao92bdb5a2018-10-21 12:12:37 -0700243 const uint8_t* sx = src_p;
Luke Song846012f2017-09-13 15:56:16 -0700244 uint32_t* px = dst_p;
245 for (int i = 0; i < width; ++i, incr_x(&px, dst_row_pixels)) {
246 uint8_t a = *sx++;
247 if (alpha_current < 255) a = (static_cast<uint32_t>(a) * alpha_current) / 255;
248 *px = pixel_blend(a, *px);
Doug Zongker16f97c32014-03-06 16:16:05 -0800249 }
Luke Song846012f2017-09-13 15:56:16 -0700250 src_p += src_row_bytes;
251 incr_y(&dst_p, dst_row_pixels);
252 }
Doug Zongker16f97c32014-03-06 16:16:05 -0800253}
254
Doug Zongker16f97c32014-03-06 16:16:05 -0800255
Ethan Yonker39662b22017-05-23 08:34:02 -0500256#ifdef TW_NO_MINUI_CUSTOM_FONTS
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700257void gr_text(int x, int y, const char *s, bool bold)
Doug Zongker16f97c32014-03-06 16:16:05 -0800258{
Elliott Hughes01a4d082015-03-24 15:21:48 -0700259 GRFont* font = gr_font;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800260
Elliott Hughes01a4d082015-03-24 15:21:48 -0700261 if (!font->texture || gr_current_a == 0) return;
Doug Zongker55a36ac2013-03-04 15:49:02 -0800262
Ethan Yonker84d61ce2017-05-10 16:11:35 -0500263 bold = bold && (font->texture->height != font->char_height);
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800264
Doug Zongkerc560a672012-12-18 16:31:27 -0800265 x += overscan_offset_x;
266 y += overscan_offset_y;
267
Elliott Hughes01a4d082015-03-24 15:21:48 -0700268 unsigned char ch;
269 while ((ch = *s++)) {
Ethan Yonker84d61ce2017-05-10 16:11:35 -0500270 if (outside(x, y) || outside(x+font->char_width-1, y+font->char_height-1)) break;
Doug Zongker16f97c32014-03-06 16:16:05 -0800271
Elliott Hughes01a4d082015-03-24 15:21:48 -0700272 if (ch < ' ' || ch > '~') {
273 ch = '?';
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800274 }
Elliott Hughes01a4d082015-03-24 15:21:48 -0700275
Ethan Yonker84d61ce2017-05-10 16:11:35 -0500276 unsigned char* src_p = font->texture->data + ((ch - ' ') * font->char_width) +
277 (bold ? font->char_height * font->texture->row_bytes : 0);
Elliott Hughes01a4d082015-03-24 15:21:48 -0700278 unsigned char* dst_p = gr_draw->data + y*gr_draw->row_bytes + x*gr_draw->pixel_bytes;
279
Ethan Yonker58f21322018-08-24 11:17:36 -0500280 text_blend_old(src_p, font->texture->row_bytes,
281 dst_p, gr_draw->row_bytes,
282 font->char_width, font->char_height);
Elliott Hughes01a4d082015-03-24 15:21:48 -0700283
Ethan Yonker84d61ce2017-05-10 16:11:35 -0500284 x += font->char_width;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800285 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800286}
Ethan Yonker39662b22017-05-23 08:34:02 -0500287#else //TW_NO_MINUI_CUSTOM_FONTS
Luke Song846012f2017-09-13 15:56:16 -0700288void gr_text(const GRFont* font, int x, int y, const char* s, bool bold) {
289 if (!font || !font->texture || (gr_current & alpha_mask) == 0) return;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800290
Luke Song846012f2017-09-13 15:56:16 -0700291 if (font->texture->pixel_bytes != 1) {
292 printf("gr_text: font has wrong format\n");
293 return;
294 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800295
Luke Song846012f2017-09-13 15:56:16 -0700296 bold = bold && (font->texture->height != font->char_height);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800297
Luke Song846012f2017-09-13 15:56:16 -0700298 x += overscan_offset_x;
299 y += overscan_offset_y;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800300
Luke Song846012f2017-09-13 15:56:16 -0700301 unsigned char ch;
302 while ((ch = *s++)) {
303 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 -0800304
Luke Song846012f2017-09-13 15:56:16 -0700305 if (ch < ' ' || ch > '~') {
306 ch = '?';
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800307 }
Luke Song846012f2017-09-13 15:56:16 -0700308
309 int row_pixels = gr_draw->row_bytes / gr_draw->pixel_bytes;
Tao Bao92bdb5a2018-10-21 12:12:37 -0700310 const uint8_t* src_p = font->texture->data() + ((ch - ' ') * font->char_width) +
311 (bold ? font->char_height * font->texture->row_bytes : 0);
312 uint32_t* dst_p = PixelAt(gr_draw, x, y, row_pixels);
Luke Song846012f2017-09-13 15:56:16 -0700313
Tao Bao92bdb5a2018-10-21 12:12:37 -0700314 TextBlend(src_p, font->texture->row_bytes, dst_p, row_pixels, font->char_width,
315 font->char_height);
Luke Song846012f2017-09-13 15:56:16 -0700316
317 x += font->char_width;
318 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800319}
Ethan Yonker39662b22017-05-23 08:34:02 -0500320#endif //TW_NO_MINUI_CUSTOM_FONTS
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800321
Tao Bao92bdb5a2018-10-21 12:12:37 -0700322void gr_texticon(int x, int y, const GRSurface* icon) {
Tao Bao9f426332018-06-13 10:39:44 -0700323 if (icon == nullptr) return;
Doug Zongker16f97c32014-03-06 16:16:05 -0800324
Luke Song846012f2017-09-13 15:56:16 -0700325 if (icon->pixel_bytes != 1) {
326 printf("gr_texticon: source has wrong format\n");
327 return;
328 }
Doug Zongker02ec6b82012-08-22 17:26:40 -0700329
Luke Song846012f2017-09-13 15:56:16 -0700330 x += overscan_offset_x;
331 y += overscan_offset_y;
Doug Zongkerc560a672012-12-18 16:31:27 -0800332
Luke Song846012f2017-09-13 15:56:16 -0700333 if (outside(x, y) || outside(x + icon->width - 1, y + icon->height - 1)) return;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700334
Luke Song846012f2017-09-13 15:56:16 -0700335 int row_pixels = gr_draw->row_bytes / gr_draw->pixel_bytes;
Tao Bao92bdb5a2018-10-21 12:12:37 -0700336 const uint8_t* src_p = icon->data();
337 uint32_t* dst_p = PixelAt(gr_draw, x, y, row_pixels);
338 TextBlend(src_p, icon->row_bytes, dst_p, row_pixels, icon->width, icon->height);
Doug Zongker16f97c32014-03-06 16:16:05 -0800339}
340
Ethan Yonker58f21322018-08-24 11:17:36 -0500341void gr_convert_rgb_555(unsigned char r, unsigned char g, unsigned char b)
Ethan Yonkere96182e2015-10-13 19:32:03 -0500342{
Ethan Yonker58f21322018-08-24 11:17:36 -0500343 gr_current_r5 = (((r & 0xFF) * 0x1F) + 0x7F) / 0xFF;
344 gr_current_g5 = (((g & 0xFF) * 0x3F) + 0x7F) / 0xFF;
345 gr_current_b5 = (((b & 0xFF) * 0x1F) + 0x7F) / 0xFF;
Ethan Yonkere96182e2015-10-13 19:32:03 -0500346
347 rgb_555[0] = (gr_current_g5 << 5) + (gr_current_b5);
348 rgb_555[1] = (gr_current_r5 << 3) + (gr_current_g5 >> 3);
349}
350
Luke Song846012f2017-09-13 15:56:16 -0700351void gr_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a) {
Ethan Yonker58f21322018-08-24 11:17:36 -0500352#ifdef TW_NO_MINUI_CUSTOM_FONTS
Tony Kuofd778e32015-02-05 21:25:56 +0800353#if defined(RECOVERY_ABGR) || defined(RECOVERY_BGRA)
Ethan Yonker58f21322018-08-24 11:17:36 -0500354 gr_current_r = b;
355 gr_current_g = g;
356 gr_current_b = r;
Tony Kuofd778e32015-02-05 21:25:56 +0800357#else
Ethan Yonker58f21322018-08-24 11:17:36 -0500358 gr_current_r = r;
359 gr_current_g = g;
360 gr_current_b = b;
Tony Kuofd778e32015-02-05 21:25:56 +0800361#endif
Ethan Yonker58f21322018-08-24 11:17:36 -0500362#endif
363
364 if (gr_draw->pixel_bytes == 2) {
365 gr_current_a = a;
366 gr_convert_rgb_555(r, g, b);
367 return;
368 }
369
Luke Song846012f2017-09-13 15:56:16 -0700370 uint32_t r32 = r, g32 = g, b32 = b, a32 = a;
Adrian Salidocc7b7eb2019-12-12 20:18:09 -0800371 if (pixel_format == PixelFormat::ARGB || pixel_format == PixelFormat::BGRA) {
Tao Baoed876a72018-07-31 21:32:50 -0700372 gr_current = (a32 << 24) | (r32 << 16) | (g32 << 8) | b32;
373 } else {
374 gr_current = (a32 << 24) | (b32 << 16) | (g32 << 8) | r32;
375 }
Doug Zongker16f97c32014-03-06 16:16:05 -0800376}
377
Luke Song846012f2017-09-13 15:56:16 -0700378void gr_clear() {
Ethan Yonker58f21322018-08-24 11:17:36 -0500379 if (gr_draw->pixel_bytes == 2) {
380 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
381 return;
382 }
Ethan Yonkere96182e2015-10-13 19:32:03 -0500383
Ethan Yonker58f21322018-08-24 11:17:36 -0500384 // This code only works on 32bpp devices
Luke Song846012f2017-09-13 15:56:16 -0700385 if ((gr_current & 0xff) == ((gr_current >> 8) & 0xff) &&
386 (gr_current & 0xff) == ((gr_current >> 16) & 0xff) &&
387 (gr_current & 0xff) == ((gr_current >> 24) & 0xff) &&
388 gr_draw->row_bytes == gr_draw->width * gr_draw->pixel_bytes) {
Tao Bao92bdb5a2018-10-21 12:12:37 -0700389 memset(gr_draw->data(), gr_current & 0xff, gr_draw->height * gr_draw->row_bytes);
Luke Song846012f2017-09-13 15:56:16 -0700390 } else {
Tao Bao92bdb5a2018-10-21 12:12:37 -0700391 uint32_t* px = reinterpret_cast<uint32_t*>(gr_draw->data());
Luke Song846012f2017-09-13 15:56:16 -0700392 int row_diff = gr_draw->row_bytes / gr_draw->pixel_bytes - gr_draw->width;
393 for (int y = 0; y < gr_draw->height; ++y) {
394 for (int x = 0; x < gr_draw->width; ++x) {
395 *px++ = gr_current;
396 }
397 px += row_diff;
Doug Zongker16f97c32014-03-06 16:16:05 -0800398 }
Luke Song846012f2017-09-13 15:56:16 -0700399 }
Doug Zongker02ec6b82012-08-22 17:26:40 -0700400}
401
Luke Song846012f2017-09-13 15:56:16 -0700402void gr_fill(int x1, int y1, int x2, int y2) {
403 x1 += overscan_offset_x;
404 y1 += overscan_offset_y;
Doug Zongkerc560a672012-12-18 16:31:27 -0800405
Luke Song846012f2017-09-13 15:56:16 -0700406 x2 += overscan_offset_x;
407 y2 += overscan_offset_y;
Doug Zongkerc560a672012-12-18 16:31:27 -0800408
Luke Song846012f2017-09-13 15:56:16 -0700409 if (outside(x1, y1) || outside(x2 - 1, y2 - 1)) return;
Doug Zongker16f97c32014-03-06 16:16:05 -0800410
Luke Song846012f2017-09-13 15:56:16 -0700411 int row_pixels = gr_draw->row_bytes / gr_draw->pixel_bytes;
Tao Bao92bdb5a2018-10-21 12:12:37 -0700412 uint32_t* p = PixelAt(gr_draw, x1, y1, row_pixels);
Luke Song846012f2017-09-13 15:56:16 -0700413 uint8_t alpha = static_cast<uint8_t>(((gr_current & alpha_mask) >> 24));
414 if (alpha > 0) {
415 for (int y = y1; y < y2; ++y) {
416 uint32_t* px = p;
417 for (int x = x1; x < x2; ++x) {
418 *px = pixel_blend(alpha, *px);
419 incr_x(&px, row_pixels);
420 }
421 incr_y(&p, row_pixels);
Doug Zongker16f97c32014-03-06 16:16:05 -0800422 }
Luke Song846012f2017-09-13 15:56:16 -0700423 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800424}
425
bigbiff20edca82020-07-03 09:55:28 -0400426void 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 -0500427 dx += overscan_offset_x;
428 dy += overscan_offset_y;
Ethan Yonkere96182e2015-10-13 19:32:03 -0500429
Ethan Yonker58f21322018-08-24 11:17:36 -0500430 if (outside(dx, dy) || outside(dx+w-1, dy+h-1)) return;
Ethan Yonkere96182e2015-10-13 19:32:03 -0500431
bigbiff20edca82020-07-03 09:55:28 -0400432 unsigned char* src_p = (unsigned char*) source->data() + sy*source->row_bytes + sx*source->pixel_bytes;
433 unsigned char* dst_p = gr_draw->data() + dy*gr_draw->row_bytes + dx*gr_draw->pixel_bytes;
Ethan Yonkere96182e2015-10-13 19:32:03 -0500434
Ethan Yonker58f21322018-08-24 11:17:36 -0500435 int i, j;
436 for (i = 0; i < h; ++i) {
437 unsigned char* spx = src_p;
438 unsigned char* dpx = dst_p;
Ethan Yonkere96182e2015-10-13 19:32:03 -0500439
Ethan Yonker58f21322018-08-24 11:17:36 -0500440 for (j = 0; j < w; ++j) {
441 unsigned a = spx[3];
442
443 if (a == 0) {
444 spx += source->pixel_bytes;
445 dpx += gr_draw->pixel_bytes;
446 } else {
447 unsigned r5 = (((*spx++ & 0xFF) * 0x1F) + 0x7F) / 0xFF;
448 unsigned g5 = (((*spx++ & 0xFF) * 0x3F) + 0x7F) / 0xFF;
449 unsigned b5 = (((*spx++ & 0xFF) * 0x1F) + 0x7F) / 0xFF;
450 spx++;
451 if (a == 255) {
452 *dpx++ = (g5 << 5) + (b5);
453 *dpx++ = (r5 << 3) + (g5 >> 3);
454 } else {
455 blend_16bpp(dpx, r5, g5, b5, a);
456 spx += source->pixel_bytes;
Ethan Yonkere96182e2015-10-13 19:32:03 -0500457 }
Ethan Yonker58f21322018-08-24 11:17:36 -0500458 }
Ethan Yonkere96182e2015-10-13 19:32:03 -0500459 }
Ethan Yonker58f21322018-08-24 11:17:36 -0500460 src_p += source->row_bytes;
461 dst_p += gr_draw->row_bytes;
462 }
Ethan Yonkere96182e2015-10-13 19:32:03 -0500463}
464
Tao Bao92bdb5a2018-10-21 12:12:37 -0700465void gr_blit(const GRSurface* source, int sx, int sy, int w, int h, int dx, int dy) {
Tao Bao9f426332018-06-13 10:39:44 -0700466 if (source == nullptr) return;
Doug Zongker16f97c32014-03-06 16:16:05 -0800467
Luke Song846012f2017-09-13 15:56:16 -0700468 if (gr_draw->pixel_bytes != source->pixel_bytes) {
Ethan Yonker58f21322018-08-24 11:17:36 -0500469 if (gr_draw->pixel_bytes == 2 && source->pixel_bytes == 4) {
470 gr_blit_32to16(source, sx, sy, w, h, dx, dy);
471 return;
472 } else {
473 printf("gr_blit: source has wrong format\n");
474 return;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800475 }
Luke Song846012f2017-09-13 15:56:16 -0700476 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800477
Luke Song846012f2017-09-13 15:56:16 -0700478 dx += overscan_offset_x;
479 dy += overscan_offset_y;
Doug Zongkerc560a672012-12-18 16:31:27 -0800480
Luke Song846012f2017-09-13 15:56:16 -0700481 if (outside(dx, dy) || outside(dx + w - 1, dy + h - 1)) return;
Doug Zongker16f97c32014-03-06 16:16:05 -0800482
Tao Bao44478df2018-07-31 22:01:03 -0700483 if (rotation != GRRotation::NONE) {
Luke Song846012f2017-09-13 15:56:16 -0700484 int src_row_pixels = source->row_bytes / source->pixel_bytes;
485 int row_pixels = gr_draw->row_bytes / gr_draw->pixel_bytes;
Tao Bao92bdb5a2018-10-21 12:12:37 -0700486 const uint32_t* src_py =
487 reinterpret_cast<const uint32_t*>(source->data()) + sy * source->row_bytes / 4 + sx;
488 uint32_t* dst_py = PixelAt(gr_draw, dx, dy, row_pixels);
Luke Song846012f2017-09-13 15:56:16 -0700489
490 for (int y = 0; y < h; y += 1) {
Tao Bao92bdb5a2018-10-21 12:12:37 -0700491 const uint32_t* src_px = src_py;
Luke Song846012f2017-09-13 15:56:16 -0700492 uint32_t* dst_px = dst_py;
493 for (int x = 0; x < w; x += 1) {
494 *dst_px = *src_px++;
495 incr_x(&dst_px, row_pixels);
496 }
497 src_py += src_row_pixels;
498 incr_y(&dst_py, row_pixels);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800499 }
Luke Song846012f2017-09-13 15:56:16 -0700500 } else {
Tao Bao92bdb5a2018-10-21 12:12:37 -0700501 const uint8_t* src_p = source->data() + sy * source->row_bytes + sx * source->pixel_bytes;
502 uint8_t* dst_p = gr_draw->data() + dy * gr_draw->row_bytes + dx * gr_draw->pixel_bytes;
Doug Zongker16f97c32014-03-06 16:16:05 -0800503
Tao Bao9f426332018-06-13 10:39:44 -0700504 for (int i = 0; i < h; ++i) {
Luke Song846012f2017-09-13 15:56:16 -0700505 memcpy(dst_p, src_p, w * source->pixel_bytes);
506 src_p += source->row_bytes;
507 dst_p += gr_draw->row_bytes;
Doug Zongker16f97c32014-03-06 16:16:05 -0800508 }
Luke Song846012f2017-09-13 15:56:16 -0700509 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800510}
511
Tao Bao9f426332018-06-13 10:39:44 -0700512unsigned int gr_get_width(const GRSurface* surface) {
513 if (surface == nullptr) {
Luke Song846012f2017-09-13 15:56:16 -0700514 return 0;
515 }
516 return surface->width;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800517}
518
Tao Bao9f426332018-06-13 10:39:44 -0700519unsigned int gr_get_height(const GRSurface* surface) {
520 if (surface == nullptr) {
Luke Song846012f2017-09-13 15:56:16 -0700521 return 0;
522 }
523 return surface->height;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800524}
525
Ethan Yonker39662b22017-05-23 08:34:02 -0500526#ifdef TW_NO_MINUI_CUSTOM_FONTS
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800527static void gr_init_font(void)
528{
Elliott Hughes07cfb8f2015-04-10 13:12:05 -0700529 gr_font = reinterpret_cast<GRFont*>(calloc(sizeof(*gr_font), 1));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800530
Doug Zongkera418aa72014-03-17 12:10:02 -0700531 int res = res_create_alpha_surface("font", &(gr_font->texture));
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800532 if (res == 0) {
533 // The font image should be a 96x2 array of character images. The
534 // columns are the printable ASCII characters 0x20 - 0x7f. The
535 // top row is regular text; the bottom row is bold.
Ethan Yonker84d61ce2017-05-10 16:11:35 -0500536 gr_font->char_width = gr_font->texture->width / 96;
537 gr_font->char_height = gr_font->texture->height / 2;
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800538 } else {
Doug Zongker55a36ac2013-03-04 15:49:02 -0800539 printf("failed to read font: res=%d\n", res);
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800540
541 // fall back to the compiled-in font.
Elliott Hughes07cfb8f2015-04-10 13:12:05 -0700542 gr_font->texture = reinterpret_cast<GRSurface*>(malloc(sizeof(*gr_font->texture)));
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800543 gr_font->texture->width = font.width;
544 gr_font->texture->height = font.height;
Doug Zongker16f97c32014-03-06 16:16:05 -0800545 gr_font->texture->row_bytes = font.width;
546 gr_font->texture->pixel_bytes = 1;
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800547
Elliott Hughes07cfb8f2015-04-10 13:12:05 -0700548 unsigned char* bits = reinterpret_cast<unsigned char*>(malloc(font.width * font.height));
549 gr_font->texture->data = reinterpret_cast<unsigned char*>(bits);
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800550
551 unsigned char data;
552 unsigned char* in = font.rundata;
553 while((data = *in++)) {
554 memset(bits, (data & 0x80) ? 255 : 0, data & 0x7f);
555 bits += (data & 0x7f);
556 }
557
Ethan Yonker84d61ce2017-05-10 16:11:35 -0500558 gr_font->char_width = font.char_width;
559 gr_font->char_height = font.char_height;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800560 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800561}
562
Ethan Yonker84d61ce2017-05-10 16:11:35 -0500563void gr_set_font(__attribute__ ((unused))const char* name) {
564 //this cm function is made to change font. Don't care, just init the font:
565 gr_init_font();
566 return;
567}
Ethan Yonker39662b22017-05-23 08:34:02 -0500568#else // TW_NO_MINUI_CUSTOM_FONTS
Damien Bargiacchid00f5eb2016-09-09 07:14:08 -0700569int gr_init_font(const char* name, GRFont** dest) {
Luke Song846012f2017-09-13 15:56:16 -0700570 GRFont* font = static_cast<GRFont*>(calloc(1, sizeof(*gr_font)));
571 if (font == nullptr) {
572 return -1;
573 }
Damien Bargiacchid00f5eb2016-09-09 07:14:08 -0700574
Luke Song846012f2017-09-13 15:56:16 -0700575 int res = res_create_alpha_surface(name, &(font->texture));
576 if (res < 0) {
577 free(font);
578 return res;
579 }
Damien Bargiacchi35fff612016-08-11 15:57:03 -0700580
Luke Song846012f2017-09-13 15:56:16 -0700581 // The font image should be a 96x2 array of character images. The
582 // columns are the printable ASCII characters 0x20 - 0x7f. The
583 // top row is regular text; the bottom row is bold.
584 font->char_width = font->texture->width / 96;
585 font->char_height = font->texture->height / 2;
Damien Bargiacchid00f5eb2016-09-09 07:14:08 -0700586
Luke Song846012f2017-09-13 15:56:16 -0700587 *dest = font;
Damien Bargiacchi35fff612016-08-11 15:57:03 -0700588
Luke Song846012f2017-09-13 15:56:16 -0700589 return 0;
Damien Bargiacchi35fff612016-08-11 15:57:03 -0700590}
bigbiff20edca82020-07-03 09:55:28 -0400591#endif // TW_NO_MINUI_CUSTOM_FONTS
Damien Bargiacchi35fff612016-08-11 15:57:03 -0700592
Doug Zongker5290f202014-03-11 13:22:04 -0700593void gr_flip() {
Tao Bao557fa1f2017-02-07 12:51:00 -0800594 gr_draw = gr_backend->Flip();
Doug Zongker5290f202014-03-11 13:22:04 -0700595}
596
Tao Bao557fa1f2017-02-07 12:51:00 -0800597int gr_init() {
Tao Baoed876a72018-07-31 21:32:50 -0700598 // pixel_format needs to be set before loading any resources or initializing backends.
Tao Bao050feb02018-09-05 21:45:42 -0700599 std::string format = android::base::GetProperty("ro.minui.pixel_format", "");
Tao Baoed876a72018-07-31 21:32:50 -0700600 if (format == "ABGR_8888") {
601 pixel_format = PixelFormat::ABGR;
602 } else if (format == "RGBX_8888") {
603 pixel_format = PixelFormat::RGBX;
Adrian Salidocc7b7eb2019-12-12 20:18:09 -0800604 } else if (format == "ARGB_8888") {
605 pixel_format = PixelFormat::ARGB;
Tao Baoed876a72018-07-31 21:32:50 -0700606 } else if (format == "BGRA_8888") {
607 pixel_format = PixelFormat::BGRA;
608 } else {
609 pixel_format = PixelFormat::UNKNOWN;
610 }
611
Tianjie Xu55a2c4e2018-03-29 11:07:50 -0700612 int ret = gr_init_font("font", &gr_font);
613 if (ret != 0) {
Tianjie Xu842f2a32018-05-31 18:16:28 -0700614 printf("Failed to init font: %d, continuing graphic backend initialization without font file\n",
615 ret);
Tianjie Xu55a2c4e2018-03-29 11:07:50 -0700616 }
Doug Zongker16f97c32014-03-06 16:16:05 -0800617
Marissa Wall025ce542020-02-28 11:22:46 -0800618 auto backend = std::unique_ptr<MinuiBackend>{ std::make_unique<MinuiBackendDrm>() };
Tao Bao557fa1f2017-02-07 12:51:00 -0800619 gr_draw = backend->Init();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800620
Ethan Yonkerb386f712017-01-20 14:30:28 -0600621#ifdef MSM_BSP
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500622 if (gr_draw) {
623 printf("Using overlay graphics.\n");
Ethan Yonkera59da092015-10-13 19:35:05 -0500624 }
Ethan Yonkerb386f712017-01-20 14:30:28 -0600625#endif
Ethan Yonkera59da092015-10-13 19:35:05 -0500626
Greg Hackmann41909dd2014-04-25 10:39:50 -0700627
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500628 if (!gr_draw) {
629 backend = std::make_unique<MinuiBackendDrm>();
630 gr_draw = backend->Init();
Ethan Yonkera59da092015-10-13 19:35:05 -0500631 if (gr_draw)
632 printf("Using drm graphics.\n");
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700633 }
634
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500635 if (!gr_draw) {
636 backend = std::make_unique<MinuiBackendFbdev>();
637 gr_draw = backend->Init();
638 if (gr_draw)
Ethan Yonkera59da092015-10-13 19:35:05 -0500639 printf("Using fbdev graphics.\n");
bigbiff20edca82020-07-03 09:55:28 -0400640 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800641
Tao Bao557fa1f2017-02-07 12:51:00 -0800642 if (!gr_draw) {
643 return -1;
644 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800645
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800646
Tao Bao050feb02018-09-05 21:45:42 -0700647 int overscan_percent = android::base::GetIntProperty("ro.minui.overscan_percent", 0);
Tao Bao557fa1f2017-02-07 12:51:00 -0800648 overscan_offset_x = gr_draw->width * overscan_percent / 100;
649 overscan_offset_y = gr_draw->height * overscan_percent / 100;
650
651 gr_flip();
652 gr_flip();
Tianjie Xuccf00a22018-06-05 17:10:23 -0700653 if (!gr_draw) {
654 printf("gr_init: gr_draw becomes nullptr after gr_flip\n");
655 return -1;
656 }
Tao Bao557fa1f2017-02-07 12:51:00 -0800657
Tao Baoed876a72018-07-31 21:32:50 -0700658 std::string rotation_str =
Tao Bao050feb02018-09-05 21:45:42 -0700659 android::base::GetProperty("ro.minui.default_rotation", "ROTATION_NONE");
Tao Bao44478df2018-07-31 22:01:03 -0700660 if (rotation_str == "ROTATION_RIGHT") {
661 gr_rotate(GRRotation::RIGHT);
662 } else if (rotation_str == "ROTATION_DOWN") {
663 gr_rotate(GRRotation::DOWN);
664 } else if (rotation_str == "ROTATION_LEFT") {
665 gr_rotate(GRRotation::LEFT);
Tao Baoed876a72018-07-31 21:32:50 -0700666 } else { // "ROTATION_NONE" or unknown string
Tao Bao44478df2018-07-31 22:01:03 -0700667 gr_rotate(GRRotation::NONE);
668 }
Luke Song846012f2017-09-13 15:56:16 -0700669
670 if (gr_draw->pixel_bytes != 4) {
671 printf("gr_init: Only 4-byte pixel formats supported\n");
672 }
673
Tao Bao557fa1f2017-02-07 12:51:00 -0800674 return 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800675}
676
Tao Bao557fa1f2017-02-07 12:51:00 -0800677void gr_exit() {
678 delete gr_backend;
Tao Bao9f426332018-06-13 10:39:44 -0700679 gr_backend = nullptr;
680
681 delete gr_font;
682 gr_font = nullptr;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800683}
684
Tao Bao557fa1f2017-02-07 12:51:00 -0800685int gr_fb_width() {
Tao Bao44478df2018-07-31 22:01:03 -0700686 return (rotation == GRRotation::LEFT || rotation == GRRotation::RIGHT)
687 ? gr_draw->height - 2 * overscan_offset_y
688 : gr_draw->width - 2 * overscan_offset_x;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800689}
690
Tao Bao557fa1f2017-02-07 12:51:00 -0800691int gr_fb_height() {
Tao Bao44478df2018-07-31 22:01:03 -0700692 return (rotation == GRRotation::LEFT || rotation == GRRotation::RIGHT)
693 ? gr_draw->width - 2 * overscan_offset_x
694 : gr_draw->height - 2 * overscan_offset_y;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800695}
Dima Zavin4daf48a2011-08-30 11:59:20 -0700696
Tao Bao557fa1f2017-02-07 12:51:00 -0800697void gr_fb_blank(bool blank) {
698 gr_backend->Blank(blank);
Dima Zavin4daf48a2011-08-30 11:59:20 -0700699}
Luke Song846012f2017-09-13 15:56:16 -0700700
701void gr_rotate(GRRotation rot) {
702 rotation = rot;
703}