blob: 95251764dc472b6e91599cb524dd9c78dae809c5 [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
Dima Zavin4daf48a2011-08-30 11:59:20 -070017#include <stdbool.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080018#include <stdlib.h>
Elliott Hughescd3c55a2015-01-29 20:50:08 -080019#include <string.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080020#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
Doug Zongker16f97c32014-03-06 16:16:05 -080032#include <time.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080033
maxwen39624d42015-12-01 22:02:07 +010034#ifdef BOARD_USE_CUSTOM_RECOVERY_FONT
35#include BOARD_USE_CUSTOM_RECOVERY_FONT
36#else
Doug Zongker6fd59ac2013-03-06 15:01:11 -080037#include "font_10x18.h"
maxwen39624d42015-12-01 22:02:07 +010038#endif
39
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080040#include "minui.h"
Doug Zongker5290f202014-03-11 13:22:04 -070041#include "graphics.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080042
Doug Zongker16f97c32014-03-06 16:16:05 -080043static GRFont* gr_font = NULL;
Doug Zongker5290f202014-03-11 13:22:04 -070044static minui_backend* gr_backend = NULL;
Doug Zongker16f97c32014-03-06 16:16:05 -080045
Doug Zongkerc560a672012-12-18 16:31:27 -080046static int overscan_percent = OVERSCAN_PERCENT;
47static int overscan_offset_x = 0;
48static int overscan_offset_y = 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080049
Doug Zongker16f97c32014-03-06 16:16:05 -080050static unsigned char gr_current_r = 255;
51static unsigned char gr_current_g = 255;
52static unsigned char gr_current_b = 255;
53static unsigned char gr_current_a = 255;
Ethan Yonkere96182e2015-10-13 19:32:03 -050054static unsigned char rgb_555[2];
55static unsigned char gr_current_r5 = 31;
56static unsigned char gr_current_g5 = 63;
57static unsigned char gr_current_b5 = 31;
Doug Zongker16f97c32014-03-06 16:16:05 -080058
Doug Zongker5290f202014-03-11 13:22:04 -070059static GRSurface* gr_draw = NULL;
Doug Zongker16f97c32014-03-06 16:16:05 -080060
61static bool outside(int x, int y)
62{
63 return x < 0 || x >= gr_draw->width || y < 0 || y >= gr_draw->height;
64}
Ethan Yonker39662b22017-05-23 08:34:02 -050065
66#ifdef TW_NO_MINUI_CUSTOM_FONTS
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080067int gr_measure(const char *s)
68{
Ethan Yonker84d61ce2017-05-10 16:11:35 -050069 return gr_font->char_width * strlen(s);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080070}
71
Dima Zavin3c7f00e2011-08-30 11:58:24 -070072void gr_font_size(int *x, int *y)
73{
Ethan Yonker84d61ce2017-05-10 16:11:35 -050074 *x = gr_font->char_width;
75 *y = gr_font->char_height;
Dima Zavin3c7f00e2011-08-30 11:58:24 -070076}
Ethan Yonker84d61ce2017-05-10 16:11:35 -050077#else // TW_USE_MINUI_CUSTOM_FONTS
Damien Bargiacchi35fff612016-08-11 15:57:03 -070078const GRFont* gr_sys_font()
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080079{
Damien Bargiacchi35fff612016-08-11 15:57:03 -070080 return gr_font;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080081}
82
Damien Bargiacchi35fff612016-08-11 15:57:03 -070083int gr_measure(const GRFont* font, const char *s)
Dima Zavin3c7f00e2011-08-30 11:58:24 -070084{
Damien Bargiacchi35fff612016-08-11 15:57:03 -070085 return font->char_width * strlen(s);
86}
87
88void gr_font_size(const GRFont* font, int *x, int *y)
89{
90 *x = font->char_width;
91 *y = font->char_height;
Dima Zavin3c7f00e2011-08-30 11:58:24 -070092}
Ethan Yonker39662b22017-05-23 08:34:02 -050093#endif // TW_NO_MINUI_CUSTOM_FONTS
Dima Zavin3c7f00e2011-08-30 11:58:24 -070094
Ethan Yonkere96182e2015-10-13 19:32:03 -050095void blend_16bpp(unsigned char* px, unsigned r5, unsigned g5, unsigned b5, unsigned char a)
96{
97 unsigned char orig[2];
98 orig[0] = px[0];
99 orig[1] = px[1];
100
101 /* This code is a little easier to read
102 unsigned oldred = (orig[1] >> 3);
103 unsigned oldgreen = (((orig[0] >> 5) << 3) + (orig[1] & 0x7));
104 unsigned oldblue = (orig[0] & 0x1F);
105
106 unsigned newred = (oldred * (255-a) + r5 * a) / 255;
107 unsigned newgreen = (oldgreen * (255-a) + g5 * a) / 255;
108 unsigned newblue = (oldblue * (255-a) + b5 * a) / 255;
109 */
110
111 unsigned newred = ((orig[1] >> 3) * (255-a) + r5 * a) / 255;
112 unsigned newgreen = ((((orig[0] >> 5) << 3) + (orig[1] & 0x7)) * (255-a) + g5 * a) / 255;
113 unsigned newblue = ((orig[0] & 0x1F) * (255-a) + b5 * a) / 255;
114
115 *px++ = (newgreen << 5) + (newblue);
116 *px++ = (newred << 3) + (newgreen >> 3);
117}
118
Doug Zongker16f97c32014-03-06 16:16:05 -0800119static void text_blend(unsigned char* src_p, int src_row_bytes,
120 unsigned char* dst_p, int dst_row_bytes,
121 int width, int height)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800122{
Elliott Hughes01a4d082015-03-24 15:21:48 -0700123 for (int j = 0; j < height; ++j) {
Doug Zongker16f97c32014-03-06 16:16:05 -0800124 unsigned char* sx = src_p;
125 unsigned char* px = dst_p;
Elliott Hughes01a4d082015-03-24 15:21:48 -0700126 for (int i = 0; i < width; ++i) {
Doug Zongker16f97c32014-03-06 16:16:05 -0800127 unsigned char a = *sx++;
128 if (gr_current_a < 255) a = ((int)a * gr_current_a) / 255;
129 if (a == 255) {
Ethan Yonkere96182e2015-10-13 19:32:03 -0500130 if (gr_draw->pixel_bytes == 2) {
131 *px++ = rgb_555[0];
132 *px++ = rgb_555[1];
133 } else {
134 *px++ = gr_current_r;
135 *px++ = gr_current_g;
136 *px++ = gr_current_b;
137 px++;
138 }
Doug Zongker16f97c32014-03-06 16:16:05 -0800139 } else if (a > 0) {
Ethan Yonkere96182e2015-10-13 19:32:03 -0500140 if (gr_draw->pixel_bytes == 2) {
141 blend_16bpp(px, gr_current_r5, gr_current_g5, gr_current_b5, a);
142 px += gr_draw->pixel_bytes;
143 } else {
144 *px = (*px * (255-a) + gr_current_r * a) / 255;
145 ++px;
146 *px = (*px * (255-a) + gr_current_g * a) / 255;
147 ++px;
148 *px = (*px * (255-a) + gr_current_b * a) / 255;
149 ++px;
150 ++px;
151 }
Doug Zongker16f97c32014-03-06 16:16:05 -0800152 } else {
Ethan Yonkere96182e2015-10-13 19:32:03 -0500153 px += gr_draw->pixel_bytes;
Doug Zongker16f97c32014-03-06 16:16:05 -0800154 }
155 }
156 src_p += src_row_bytes;
157 dst_p += dst_row_bytes;
158 }
159}
160
Ethan Yonker39662b22017-05-23 08:34:02 -0500161#ifdef TW_NO_MINUI_CUSTOM_FONTS
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700162void gr_text(int x, int y, const char *s, bool bold)
Doug Zongker16f97c32014-03-06 16:16:05 -0800163{
Elliott Hughes01a4d082015-03-24 15:21:48 -0700164 GRFont* font = gr_font;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800165
Elliott Hughes01a4d082015-03-24 15:21:48 -0700166 if (!font->texture || gr_current_a == 0) return;
Doug Zongker55a36ac2013-03-04 15:49:02 -0800167
Ethan Yonker84d61ce2017-05-10 16:11:35 -0500168 bold = bold && (font->texture->height != font->char_height);
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800169
Doug Zongkerc560a672012-12-18 16:31:27 -0800170 x += overscan_offset_x;
171 y += overscan_offset_y;
172
Elliott Hughes01a4d082015-03-24 15:21:48 -0700173 unsigned char ch;
174 while ((ch = *s++)) {
Ethan Yonker84d61ce2017-05-10 16:11:35 -0500175 if (outside(x, y) || outside(x+font->char_width-1, y+font->char_height-1)) break;
Doug Zongker16f97c32014-03-06 16:16:05 -0800176
Elliott Hughes01a4d082015-03-24 15:21:48 -0700177 if (ch < ' ' || ch > '~') {
178 ch = '?';
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800179 }
Elliott Hughes01a4d082015-03-24 15:21:48 -0700180
Ethan Yonker84d61ce2017-05-10 16:11:35 -0500181 unsigned char* src_p = font->texture->data + ((ch - ' ') * font->char_width) +
182 (bold ? font->char_height * font->texture->row_bytes : 0);
Elliott Hughes01a4d082015-03-24 15:21:48 -0700183 unsigned char* dst_p = gr_draw->data + y*gr_draw->row_bytes + x*gr_draw->pixel_bytes;
184
185 text_blend(src_p, font->texture->row_bytes,
186 dst_p, gr_draw->row_bytes,
Ethan Yonker84d61ce2017-05-10 16:11:35 -0500187 font->char_width, font->char_height);
Elliott Hughes01a4d082015-03-24 15:21:48 -0700188
Ethan Yonker84d61ce2017-05-10 16:11:35 -0500189 x += font->char_width;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800190 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800191}
Ethan Yonker39662b22017-05-23 08:34:02 -0500192#else //TW_NO_MINUI_CUSTOM_FONTS
Damien Bargiacchi35fff612016-08-11 15:57:03 -0700193void gr_text(const GRFont* font, int x, int y, const char *s, bool bold)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800194{
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800195 if (!font->texture || gr_current_a == 0) return;
196
Damien Bargiacchi35fff612016-08-11 15:57:03 -0700197 bold = bold && (font->texture->height != font->char_height);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800198
199 x += overscan_offset_x;
200 y += overscan_offset_y;
201
202 unsigned char ch;
203 while ((ch = *s++)) {
Damien Bargiacchi35fff612016-08-11 15:57:03 -0700204 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 -0800205
206 if (ch < ' ' || ch > '~') {
207 ch = '?';
208 }
209
Damien Bargiacchi35fff612016-08-11 15:57:03 -0700210 unsigned char* src_p = font->texture->data + ((ch - ' ') * font->char_width) +
211 (bold ? font->char_height * font->texture->row_bytes : 0);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800212 unsigned char* dst_p = gr_draw->data + y*gr_draw->row_bytes + x*gr_draw->pixel_bytes;
213
214 text_blend(src_p, font->texture->row_bytes,
215 dst_p, gr_draw->row_bytes,
Damien Bargiacchi35fff612016-08-11 15:57:03 -0700216 font->char_width, font->char_height);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800217
Damien Bargiacchi35fff612016-08-11 15:57:03 -0700218 x += font->char_width;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800219 }
220}
Ethan Yonker39662b22017-05-23 08:34:02 -0500221#endif //TW_NO_MINUI_CUSTOM_FONTS
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800222
Doug Zongker16f97c32014-03-06 16:16:05 -0800223void gr_texticon(int x, int y, GRSurface* icon) {
224 if (icon == NULL) return;
225
226 if (icon->pixel_bytes != 1) {
227 printf("gr_texticon: source has wrong format\n");
Doug Zongker52eeea4f2012-09-04 14:28:25 -0700228 return;
229 }
Doug Zongker02ec6b82012-08-22 17:26:40 -0700230
Doug Zongkerc560a672012-12-18 16:31:27 -0800231 x += overscan_offset_x;
232 y += overscan_offset_y;
233
Doug Zongker16f97c32014-03-06 16:16:05 -0800234 if (outside(x, y) || outside(x+icon->width-1, y+icon->height-1)) return;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700235
Doug Zongker16f97c32014-03-06 16:16:05 -0800236 unsigned char* src_p = icon->data;
237 unsigned char* dst_p = gr_draw->data + y*gr_draw->row_bytes + x*gr_draw->pixel_bytes;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700238
Doug Zongker16f97c32014-03-06 16:16:05 -0800239 text_blend(src_p, icon->row_bytes,
240 dst_p, gr_draw->row_bytes,
241 icon->width, icon->height);
242}
243
Ethan Yonkere96182e2015-10-13 19:32:03 -0500244void gr_convert_rgb_555()
245{
246 gr_current_r5 = (((gr_current_r & 0xFF) * 0x1F) + 0x7F) / 0xFF;
247 gr_current_g5 = (((gr_current_g & 0xFF) * 0x3F) + 0x7F) / 0xFF;
248 gr_current_b5 = (((gr_current_b & 0xFF) * 0x1F) + 0x7F) / 0xFF;
249
250 rgb_555[0] = (gr_current_g5 << 5) + (gr_current_b5);
251 rgb_555[1] = (gr_current_r5 << 3) + (gr_current_g5 >> 3);
252}
253
Doug Zongker16f97c32014-03-06 16:16:05 -0800254void gr_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
255{
Tony Kuofd778e32015-02-05 21:25:56 +0800256#if defined(RECOVERY_ABGR) || defined(RECOVERY_BGRA)
257 gr_current_r = b;
258 gr_current_g = g;
259 gr_current_b = r;
260 gr_current_a = a;
261#else
Doug Zongker16f97c32014-03-06 16:16:05 -0800262 gr_current_r = r;
263 gr_current_g = g;
264 gr_current_b = b;
265 gr_current_a = a;
Tony Kuofd778e32015-02-05 21:25:56 +0800266#endif
Ethan Yonkere96182e2015-10-13 19:32:03 -0500267 if (gr_draw->pixel_bytes == 2) {
268 gr_convert_rgb_555();
269 }
Doug Zongker16f97c32014-03-06 16:16:05 -0800270}
271
272void gr_clear()
273{
Ethan Yonkere96182e2015-10-13 19:32:03 -0500274 if (gr_draw->pixel_bytes == 2) {
275 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
276 return;
277 }
278
279 // This code only works on 32bpp devices
Elliott Hughes01a4d082015-03-24 15:21:48 -0700280 if (gr_current_r == gr_current_g && gr_current_r == gr_current_b) {
Doug Zongker16f97c32014-03-06 16:16:05 -0800281 memset(gr_draw->data, gr_current_r, gr_draw->height * gr_draw->row_bytes);
282 } else {
Doug Zongker16f97c32014-03-06 16:16:05 -0800283 unsigned char* px = gr_draw->data;
Elliott Hughes01a4d082015-03-24 15:21:48 -0700284 for (int y = 0; y < gr_draw->height; ++y) {
285 for (int x = 0; x < gr_draw->width; ++x) {
Doug Zongker16f97c32014-03-06 16:16:05 -0800286 *px++ = gr_current_r;
287 *px++ = gr_current_g;
288 *px++ = gr_current_b;
289 px++;
290 }
291 px += gr_draw->row_bytes - (gr_draw->width * gr_draw->pixel_bytes);
292 }
293 }
Doug Zongker02ec6b82012-08-22 17:26:40 -0700294}
295
Doug Zongkerc560a672012-12-18 16:31:27 -0800296void gr_fill(int x1, int y1, int x2, int y2)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800297{
Doug Zongkerc560a672012-12-18 16:31:27 -0800298 x1 += overscan_offset_x;
299 y1 += overscan_offset_y;
300
301 x2 += overscan_offset_x;
302 y2 += overscan_offset_y;
303
Doug Zongker16f97c32014-03-06 16:16:05 -0800304 if (outside(x1, y1) || outside(x2-1, y2-1)) return;
305
306 unsigned char* p = gr_draw->data + y1 * gr_draw->row_bytes + x1 * gr_draw->pixel_bytes;
307 if (gr_current_a == 255) {
308 int x, y;
309 for (y = y1; y < y2; ++y) {
310 unsigned char* px = p;
311 for (x = x1; x < x2; ++x) {
Ethan Yonkere96182e2015-10-13 19:32:03 -0500312 if (gr_draw->pixel_bytes == 2) {
313 *px++ = rgb_555[0];
314 *px++ = rgb_555[1];
315 } else {
316 *px++ = gr_current_r;
317 *px++ = gr_current_g;
318 *px++ = gr_current_b;
319 px++;
320 }
Doug Zongker16f97c32014-03-06 16:16:05 -0800321 }
322 p += gr_draw->row_bytes;
323 }
324 } else if (gr_current_a > 0) {
325 int x, y;
326 for (y = y1; y < y2; ++y) {
327 unsigned char* px = p;
328 for (x = x1; x < x2; ++x) {
Ethan Yonkere96182e2015-10-13 19:32:03 -0500329 if (gr_draw->pixel_bytes == 2) {
330 blend_16bpp(px, gr_current_r5, gr_current_g5, gr_current_b5, gr_current_a);
331 px += gr_draw->row_bytes;
332 } else {
333 *px = (*px * (255-gr_current_a) + gr_current_r * gr_current_a) / 255;
334 ++px;
335 *px = (*px * (255-gr_current_a) + gr_current_g * gr_current_a) / 255;
336 ++px;
337 *px = (*px * (255-gr_current_a) + gr_current_b * gr_current_a) / 255;
338 ++px;
339 ++px;
340 }
Doug Zongker16f97c32014-03-06 16:16:05 -0800341 }
342 p += gr_draw->row_bytes;
343 }
344 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800345}
346
Ethan Yonkere96182e2015-10-13 19:32:03 -0500347void gr_blit_32to16(GRSurface* source, int sx, int sy, int w, int h, int dx, int dy) {
348 dx += overscan_offset_x;
349 dy += overscan_offset_y;
350
351 if (outside(dx, dy) || outside(dx+w-1, dy+h-1)) return;
352
353 unsigned char* src_p = source->data + sy*source->row_bytes + sx*source->pixel_bytes;
354 unsigned char* dst_p = gr_draw->data + dy*gr_draw->row_bytes + dx*gr_draw->pixel_bytes;
355
356 int i, j;
357 for (i = 0; i < h; ++i) {
358 unsigned char* spx = src_p;
359 unsigned char* dpx = dst_p;
360
361 for (j = 0; j < w; ++j) {
362 unsigned a = spx[3];
363
364 if (a == 0) {
365 spx += source->pixel_bytes;
366 dpx += gr_draw->pixel_bytes;
367 } else {
368 unsigned r5 = (((*spx++ & 0xFF) * 0x1F) + 0x7F) / 0xFF;
369 unsigned g5 = (((*spx++ & 0xFF) * 0x3F) + 0x7F) / 0xFF;
370 unsigned b5 = (((*spx++ & 0xFF) * 0x1F) + 0x7F) / 0xFF;
371 spx++;
372 if (a == 255) {
373 *dpx++ = (g5 << 5) + (b5);
374 *dpx++ = (r5 << 3) + (g5 >> 3);
375 } else {
376 blend_16bpp(dpx, r5, g5, b5, a);
377 spx += source->pixel_bytes;
378 }
379 }
380 }
381 src_p += source->row_bytes;
382 dst_p += gr_draw->row_bytes;
383 }
384}
385
Doug Zongker16f97c32014-03-06 16:16:05 -0800386void gr_blit(GRSurface* source, int sx, int sy, int w, int h, int dx, int dy) {
387 if (source == NULL) return;
388
389 if (gr_draw->pixel_bytes != source->pixel_bytes) {
Ethan Yonkere96182e2015-10-13 19:32:03 -0500390 if (gr_draw->pixel_bytes == 2 && source->pixel_bytes == 4) {
391 gr_blit_32to16(source, sx, sy, w, h, dx, dy);
392 return;
393 } else {
394 printf("gr_blit: source has wrong format\n");
395 return;
396 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800397 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800398
Doug Zongkerc560a672012-12-18 16:31:27 -0800399 dx += overscan_offset_x;
400 dy += overscan_offset_y;
401
Doug Zongker16f97c32014-03-06 16:16:05 -0800402 if (outside(dx, dy) || outside(dx+w-1, dy+h-1)) return;
403
404 unsigned char* src_p = source->data + sy*source->row_bytes + sx*source->pixel_bytes;
405 unsigned char* dst_p = gr_draw->data + dy*gr_draw->row_bytes + dx*gr_draw->pixel_bytes;
406
407 int i;
408 for (i = 0; i < h; ++i) {
409 memcpy(dst_p, src_p, w * source->pixel_bytes);
410 src_p += source->row_bytes;
411 dst_p += gr_draw->row_bytes;
412 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800413}
414
Doug Zongker16f97c32014-03-06 16:16:05 -0800415unsigned int gr_get_width(GRSurface* surface) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800416 if (surface == NULL) {
417 return 0;
418 }
Doug Zongker16f97c32014-03-06 16:16:05 -0800419 return surface->width;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800420}
421
Doug Zongker16f97c32014-03-06 16:16:05 -0800422unsigned int gr_get_height(GRSurface* surface) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800423 if (surface == NULL) {
424 return 0;
425 }
Doug Zongker16f97c32014-03-06 16:16:05 -0800426 return surface->height;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800427}
428
Ethan Yonker39662b22017-05-23 08:34:02 -0500429#ifdef TW_NO_MINUI_CUSTOM_FONTS
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800430static void gr_init_font(void)
431{
Elliott Hughes07cfb8f2015-04-10 13:12:05 -0700432 gr_font = reinterpret_cast<GRFont*>(calloc(sizeof(*gr_font), 1));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800433
Doug Zongkera418aa72014-03-17 12:10:02 -0700434 int res = res_create_alpha_surface("font", &(gr_font->texture));
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800435 if (res == 0) {
436 // The font image should be a 96x2 array of character images. The
437 // columns are the printable ASCII characters 0x20 - 0x7f. The
438 // top row is regular text; the bottom row is bold.
Ethan Yonker84d61ce2017-05-10 16:11:35 -0500439 gr_font->char_width = gr_font->texture->width / 96;
440 gr_font->char_height = gr_font->texture->height / 2;
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800441 } else {
Doug Zongker55a36ac2013-03-04 15:49:02 -0800442 printf("failed to read font: res=%d\n", res);
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800443
444 // fall back to the compiled-in font.
Elliott Hughes07cfb8f2015-04-10 13:12:05 -0700445 gr_font->texture = reinterpret_cast<GRSurface*>(malloc(sizeof(*gr_font->texture)));
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800446 gr_font->texture->width = font.width;
447 gr_font->texture->height = font.height;
Doug Zongker16f97c32014-03-06 16:16:05 -0800448 gr_font->texture->row_bytes = font.width;
449 gr_font->texture->pixel_bytes = 1;
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800450
Elliott Hughes07cfb8f2015-04-10 13:12:05 -0700451 unsigned char* bits = reinterpret_cast<unsigned char*>(malloc(font.width * font.height));
452 gr_font->texture->data = reinterpret_cast<unsigned char*>(bits);
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800453
454 unsigned char data;
455 unsigned char* in = font.rundata;
456 while((data = *in++)) {
457 memset(bits, (data & 0x80) ? 255 : 0, data & 0x7f);
458 bits += (data & 0x7f);
459 }
460
Ethan Yonker84d61ce2017-05-10 16:11:35 -0500461 gr_font->char_width = font.char_width;
462 gr_font->char_height = font.char_height;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800463 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800464}
465
Ethan Yonker84d61ce2017-05-10 16:11:35 -0500466void gr_set_font(__attribute__ ((unused))const char* name) {
467 //this cm function is made to change font. Don't care, just init the font:
468 gr_init_font();
469 return;
470}
Ethan Yonker39662b22017-05-23 08:34:02 -0500471#else // TW_NO_MINUI_CUSTOM_FONTS
Damien Bargiacchid00f5eb2016-09-09 07:14:08 -0700472int gr_init_font(const char* name, GRFont** dest) {
473 GRFont* font = reinterpret_cast<GRFont*>(calloc(1, sizeof(*gr_font)));
474 if (font == nullptr) {
475 return -1;
476 }
477
478 int res = res_create_alpha_surface(name, &(font->texture));
Damien Bargiacchi35fff612016-08-11 15:57:03 -0700479 if (res < 0) {
Damien Bargiacchid00f5eb2016-09-09 07:14:08 -0700480 free(font);
Damien Bargiacchi35fff612016-08-11 15:57:03 -0700481 return res;
482 }
483
484 // The font image should be a 96x2 array of character images. The
485 // columns are the printable ASCII characters 0x20 - 0x7f. The
486 // top row is regular text; the bottom row is bold.
Damien Bargiacchid00f5eb2016-09-09 07:14:08 -0700487 font->char_width = font->texture->width / 96;
488 font->char_height = font->texture->height / 2;
489
490 *dest = font;
Damien Bargiacchi35fff612016-08-11 15:57:03 -0700491
492 return 0;
493}
494
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800495static void gr_init_font(void)
496{
Damien Bargiacchid00f5eb2016-09-09 07:14:08 -0700497 int res = gr_init_font("font", &gr_font);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800498 if (res == 0) {
Damien Bargiacchi35fff612016-08-11 15:57:03 -0700499 return;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800500 }
Damien Bargiacchi35fff612016-08-11 15:57:03 -0700501
502 printf("failed to read font: res=%d\n", res);
503
Damien Bargiacchid00f5eb2016-09-09 07:14:08 -0700504
Damien Bargiacchi35fff612016-08-11 15:57:03 -0700505 // fall back to the compiled-in font.
Damien Bargiacchid00f5eb2016-09-09 07:14:08 -0700506 gr_font = reinterpret_cast<GRFont*>(calloc(1, sizeof(*gr_font)));
Damien Bargiacchi35fff612016-08-11 15:57:03 -0700507 gr_font->texture = reinterpret_cast<GRSurface*>(malloc(sizeof(*gr_font->texture)));
508 gr_font->texture->width = font.width;
509 gr_font->texture->height = font.height;
510 gr_font->texture->row_bytes = font.width;
511 gr_font->texture->pixel_bytes = 1;
512
513 unsigned char* bits = reinterpret_cast<unsigned char*>(malloc(font.width * font.height));
514 gr_font->texture->data = reinterpret_cast<unsigned char*>(bits);
515
516 unsigned char data;
517 unsigned char* in = font.rundata;
518 while((data = *in++)) {
519 memset(bits, (data & 0x80) ? 255 : 0, data & 0x7f);
520 bits += (data & 0x7f);
521 }
522
523 gr_font->char_width = font.char_width;
524 gr_font->char_height = font.char_height;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800525}
Ethan Yonker39662b22017-05-23 08:34:02 -0500526#endif // TW_NO_MINUI_CUSTOM_FONTS
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800527
Doug Zongker16f97c32014-03-06 16:16:05 -0800528#if 0
529// Exercises many of the gr_*() functions; useful for testing.
530static void gr_test() {
531 GRSurface** images;
532 int frames;
533 int result = res_create_multi_surface("icon_installing", &frames, &images);
534 if (result < 0) {
535 printf("create surface %d\n", result);
536 gr_exit();
537 return;
538 }
539
540 time_t start = time(NULL);
541 int x;
542 for (x = 0; x <= 1200; ++x) {
543 if (x < 400) {
544 gr_color(0, 0, 0, 255);
545 } else {
546 gr_color(0, (x-400)%128, 0, 255);
547 }
548 gr_clear();
549
550 gr_color(255, 0, 0, 255);
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700551 GRSurface* frame = images[x%frames];
Doug Zongker16f97c32014-03-06 16:16:05 -0800552 gr_blit(frame, 0, 0, frame->width, frame->height, x, 0);
553
554 gr_color(255, 0, 0, 128);
555 gr_fill(400, 150, 600, 350);
556
557 gr_color(255, 255, 255, 255);
558 gr_text(500, 225, "hello, world!", 0);
559 gr_color(255, 255, 0, 128);
560 gr_text(300+x, 275, "pack my box with five dozen liquor jugs", 1);
561
562 gr_color(0, 0, 255, 128);
563 gr_fill(gr_draw->width - 200 - x, 300, gr_draw->width - x, 500);
564
Doug Zongker5290f202014-03-11 13:22:04 -0700565 gr_draw = gr_backend->flip(gr_backend);
Doug Zongker16f97c32014-03-06 16:16:05 -0800566 }
567 printf("getting end time\n");
568 time_t end = time(NULL);
569 printf("got end time\n");
570 printf("start %ld end %ld\n", (long)start, (long)end);
571 if (end > start) {
572 printf("%.2f fps\n", ((double)x) / (end-start));
573 }
574}
575#endif
576
Doug Zongker5290f202014-03-11 13:22:04 -0700577void gr_flip() {
578 gr_draw = gr_backend->flip(gr_backend);
579}
580
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800581int gr_init(void)
582{
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800583 gr_init_font();
Ethan Yonkera59da092015-10-13 19:35:05 -0500584 gr_draw = NULL;
Doug Zongker16f97c32014-03-06 16:16:05 -0800585
Ethan Yonkerb386f712017-01-20 14:30:28 -0600586#ifdef MSM_BSP
Ethan Yonkera59da092015-10-13 19:35:05 -0500587 gr_backend = open_overlay();
Greg Hackmann41909dd2014-04-25 10:39:50 -0700588 if (gr_backend) {
589 gr_draw = gr_backend->init(gr_backend);
590 if (!gr_draw) {
591 gr_backend->exit(gr_backend);
Ethan Yonkera59da092015-10-13 19:35:05 -0500592 } else
593 printf("Using overlay graphics.\n");
594 }
Ethan Yonkerb386f712017-01-20 14:30:28 -0600595#endif
Ethan Yonkera59da092015-10-13 19:35:05 -0500596
597#ifndef MSM_BSP
Ethan Yonkerb386f712017-01-20 14:30:28 -0600598 if (!gr_backend || !gr_draw) {
Ethan Yonkera59da092015-10-13 19:35:05 -0500599 gr_backend = open_adf();
600 if (gr_backend) {
601 gr_draw = gr_backend->init(gr_backend);
602 if (!gr_draw) {
603 gr_backend->exit(gr_backend);
604 } else
605 printf("Using adf graphics.\n");
Greg Hackmann41909dd2014-04-25 10:39:50 -0700606 }
607 }
Ethan Yonkera59da092015-10-13 19:35:05 -0500608#else
609 printf("Skipping adf graphics because TW_TARGET_USES_QCOM_BSP := true\n");
610#endif
Greg Hackmann41909dd2014-04-25 10:39:50 -0700611
Ethan Yonkerb386f712017-01-20 14:30:28 -0600612 if (!gr_backend || !gr_draw) {
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700613 gr_backend = open_drm();
614 gr_draw = gr_backend->init(gr_backend);
Ethan Yonkera59da092015-10-13 19:35:05 -0500615 if (gr_draw)
616 printf("Using drm graphics.\n");
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700617 }
618
Ethan Yonkerb386f712017-01-20 14:30:28 -0600619 if (!gr_backend || !gr_draw) {
Greg Hackmann41909dd2014-04-25 10:39:50 -0700620 gr_backend = open_fbdev();
621 gr_draw = gr_backend->init(gr_backend);
622 if (gr_draw == NULL) {
623 return -1;
Ethan Yonkera59da092015-10-13 19:35:05 -0500624 } else
625 printf("Using fbdev graphics.\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800626 }
627
Doug Zongker5290f202014-03-11 13:22:04 -0700628 overscan_offset_x = gr_draw->width * overscan_percent / 100;
629 overscan_offset_y = gr_draw->height * overscan_percent / 100;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800630
Doug Zongker16f97c32014-03-06 16:16:05 -0800631 gr_flip();
632 gr_flip();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800633
634 return 0;
635}
636
637void gr_exit(void)
638{
Doug Zongker5290f202014-03-11 13:22:04 -0700639 gr_backend->exit(gr_backend);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800640}
641
642int gr_fb_width(void)
643{
Doug Zongker16f97c32014-03-06 16:16:05 -0800644 return gr_draw->width - 2*overscan_offset_x;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800645}
646
647int gr_fb_height(void)
648{
Doug Zongker16f97c32014-03-06 16:16:05 -0800649 return gr_draw->height - 2*overscan_offset_y;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800650}
Dima Zavin4daf48a2011-08-30 11:59:20 -0700651
652void gr_fb_blank(bool blank)
653{
Doug Zongker5290f202014-03-11 13:22:04 -0700654 gr_backend->blank(gr_backend, blank);
Dima Zavin4daf48a2011-08-30 11:59:20 -0700655}