blob: b45e22e064e88016d1b278b03e87ab60d7f5c5f6 [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
Tao Baoe8020f42017-02-03 09:30:07 -080019#include <stdio.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080020#include <stdlib.h>
Elliott Hughescd3c55a2015-01-29 20:50:08 -080021#include <string.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080022
Tao Bao557fa1f2017-02-07 12:51:00 -080023#include <memory>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080024
maxwen39624d42015-12-01 22:02:07 +010025#ifdef BOARD_USE_CUSTOM_RECOVERY_FONT
26#include BOARD_USE_CUSTOM_RECOVERY_FONT
27#else
Doug Zongker6fd59ac2013-03-06 15:01:11 -080028#include "font_10x18.h"
maxwen39624d42015-12-01 22:02:07 +010029#endif
30
Andreas Schneider58d68e52017-11-02 17:54:36 +010031#ifndef MSM_BSP
Tao Bao557fa1f2017-02-07 12:51:00 -080032#include "graphics_adf.h"
Andreas Schneider58d68e52017-11-02 17:54:36 +010033#endif
Tao Bao557fa1f2017-02-07 12:51:00 -080034#include "graphics_drm.h"
35#include "graphics_fbdev.h"
Ethan Yonker8373cfe2017-09-08 06:50:54 -050036#include "graphics_overlay.h"
Tao Bao0ecbd762017-01-16 21:16:58 -080037#include "minui/minui.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080038
Doug Zongker16f97c32014-03-06 16:16:05 -080039static GRFont* gr_font = NULL;
Tao Bao557fa1f2017-02-07 12:51:00 -080040static MinuiBackend* gr_backend = nullptr;
Doug Zongker16f97c32014-03-06 16:16:05 -080041
Doug Zongkerc560a672012-12-18 16:31:27 -080042static int overscan_percent = OVERSCAN_PERCENT;
43static int overscan_offset_x = 0;
44static int overscan_offset_y = 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080045
Doug Zongker16f97c32014-03-06 16:16:05 -080046static unsigned char gr_current_r = 255;
47static unsigned char gr_current_g = 255;
48static unsigned char gr_current_b = 255;
49static 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
Doug Zongker5290f202014-03-11 13:22:04 -070055static GRSurface* gr_draw = NULL;
Doug Zongker16f97c32014-03-06 16:16:05 -080056
57static bool outside(int x, int y)
58{
59 return x < 0 || x >= gr_draw->width || y < 0 || y >= gr_draw->height;
60}
Ethan Yonker39662b22017-05-23 08:34:02 -050061
62#ifdef TW_NO_MINUI_CUSTOM_FONTS
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080063int gr_measure(const char *s)
64{
Ethan Yonker84d61ce2017-05-10 16:11:35 -050065 return gr_font->char_width * strlen(s);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080066}
67
Dima Zavin3c7f00e2011-08-30 11:58:24 -070068void gr_font_size(int *x, int *y)
69{
Ethan Yonker84d61ce2017-05-10 16:11:35 -050070 *x = gr_font->char_width;
71 *y = gr_font->char_height;
Dima Zavin3c7f00e2011-08-30 11:58:24 -070072}
Ethan Yonker84d61ce2017-05-10 16:11:35 -050073#else // TW_USE_MINUI_CUSTOM_FONTS
Damien Bargiacchi35fff612016-08-11 15:57:03 -070074const GRFont* gr_sys_font()
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080075{
Damien Bargiacchi35fff612016-08-11 15:57:03 -070076 return gr_font;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080077}
78
Damien Bargiacchi35fff612016-08-11 15:57:03 -070079int gr_measure(const GRFont* font, const char *s)
Dima Zavin3c7f00e2011-08-30 11:58:24 -070080{
Damien Bargiacchi35fff612016-08-11 15:57:03 -070081 return font->char_width * strlen(s);
82}
83
84void gr_font_size(const GRFont* font, int *x, int *y)
85{
86 *x = font->char_width;
87 *y = font->char_height;
Dima Zavin3c7f00e2011-08-30 11:58:24 -070088}
Ethan Yonker39662b22017-05-23 08:34:02 -050089#endif // TW_NO_MINUI_CUSTOM_FONTS
Dima Zavin3c7f00e2011-08-30 11:58:24 -070090
Ethan Yonkere96182e2015-10-13 19:32:03 -050091void blend_16bpp(unsigned char* px, unsigned r5, unsigned g5, unsigned b5, unsigned char a)
92{
93 unsigned char orig[2];
94 orig[0] = px[0];
95 orig[1] = px[1];
96
97 /* This code is a little easier to read
98 unsigned oldred = (orig[1] >> 3);
99 unsigned oldgreen = (((orig[0] >> 5) << 3) + (orig[1] & 0x7));
100 unsigned oldblue = (orig[0] & 0x1F);
101
102 unsigned newred = (oldred * (255-a) + r5 * a) / 255;
103 unsigned newgreen = (oldgreen * (255-a) + g5 * a) / 255;
104 unsigned newblue = (oldblue * (255-a) + b5 * a) / 255;
105 */
106
107 unsigned newred = ((orig[1] >> 3) * (255-a) + r5 * a) / 255;
108 unsigned newgreen = ((((orig[0] >> 5) << 3) + (orig[1] & 0x7)) * (255-a) + g5 * a) / 255;
109 unsigned newblue = ((orig[0] & 0x1F) * (255-a) + b5 * a) / 255;
110
111 *px++ = (newgreen << 5) + (newblue);
112 *px++ = (newred << 3) + (newgreen >> 3);
113}
114
Doug Zongker16f97c32014-03-06 16:16:05 -0800115static void text_blend(unsigned char* src_p, int src_row_bytes,
116 unsigned char* dst_p, int dst_row_bytes,
117 int width, int height)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800118{
Elliott Hughes01a4d082015-03-24 15:21:48 -0700119 for (int j = 0; j < height; ++j) {
Doug Zongker16f97c32014-03-06 16:16:05 -0800120 unsigned char* sx = src_p;
121 unsigned char* px = dst_p;
Elliott Hughes01a4d082015-03-24 15:21:48 -0700122 for (int i = 0; i < width; ++i) {
Doug Zongker16f97c32014-03-06 16:16:05 -0800123 unsigned char a = *sx++;
124 if (gr_current_a < 255) a = ((int)a * gr_current_a) / 255;
125 if (a == 255) {
Ethan Yonkere96182e2015-10-13 19:32:03 -0500126 if (gr_draw->pixel_bytes == 2) {
127 *px++ = rgb_555[0];
128 *px++ = rgb_555[1];
129 } else {
130 *px++ = gr_current_r;
131 *px++ = gr_current_g;
132 *px++ = gr_current_b;
133 px++;
134 }
Doug Zongker16f97c32014-03-06 16:16:05 -0800135 } else if (a > 0) {
Ethan Yonkere96182e2015-10-13 19:32:03 -0500136 if (gr_draw->pixel_bytes == 2) {
137 blend_16bpp(px, gr_current_r5, gr_current_g5, gr_current_b5, a);
138 px += gr_draw->pixel_bytes;
139 } else {
140 *px = (*px * (255-a) + gr_current_r * a) / 255;
141 ++px;
142 *px = (*px * (255-a) + gr_current_g * a) / 255;
143 ++px;
144 *px = (*px * (255-a) + gr_current_b * a) / 255;
145 ++px;
146 ++px;
147 }
Doug Zongker16f97c32014-03-06 16:16:05 -0800148 } else {
Ethan Yonkere96182e2015-10-13 19:32:03 -0500149 px += gr_draw->pixel_bytes;
Doug Zongker16f97c32014-03-06 16:16:05 -0800150 }
151 }
152 src_p += src_row_bytes;
153 dst_p += dst_row_bytes;
154 }
155}
156
Ethan Yonker39662b22017-05-23 08:34:02 -0500157#ifdef TW_NO_MINUI_CUSTOM_FONTS
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700158void gr_text(int x, int y, const char *s, bool bold)
Doug Zongker16f97c32014-03-06 16:16:05 -0800159{
Elliott Hughes01a4d082015-03-24 15:21:48 -0700160 GRFont* font = gr_font;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800161
Elliott Hughes01a4d082015-03-24 15:21:48 -0700162 if (!font->texture || gr_current_a == 0) return;
Doug Zongker55a36ac2013-03-04 15:49:02 -0800163
Ethan Yonker84d61ce2017-05-10 16:11:35 -0500164 bold = bold && (font->texture->height != font->char_height);
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800165
Doug Zongkerc560a672012-12-18 16:31:27 -0800166 x += overscan_offset_x;
167 y += overscan_offset_y;
168
Elliott Hughes01a4d082015-03-24 15:21:48 -0700169 unsigned char ch;
170 while ((ch = *s++)) {
Ethan Yonker84d61ce2017-05-10 16:11:35 -0500171 if (outside(x, y) || outside(x+font->char_width-1, y+font->char_height-1)) break;
Doug Zongker16f97c32014-03-06 16:16:05 -0800172
Elliott Hughes01a4d082015-03-24 15:21:48 -0700173 if (ch < ' ' || ch > '~') {
174 ch = '?';
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800175 }
Elliott Hughes01a4d082015-03-24 15:21:48 -0700176
Ethan Yonker84d61ce2017-05-10 16:11:35 -0500177 unsigned char* src_p = font->texture->data + ((ch - ' ') * font->char_width) +
178 (bold ? font->char_height * font->texture->row_bytes : 0);
Elliott Hughes01a4d082015-03-24 15:21:48 -0700179 unsigned char* dst_p = gr_draw->data + y*gr_draw->row_bytes + x*gr_draw->pixel_bytes;
180
181 text_blend(src_p, font->texture->row_bytes,
182 dst_p, gr_draw->row_bytes,
Ethan Yonker84d61ce2017-05-10 16:11:35 -0500183 font->char_width, font->char_height);
Elliott Hughes01a4d082015-03-24 15:21:48 -0700184
Ethan Yonker84d61ce2017-05-10 16:11:35 -0500185 x += font->char_width;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800186 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800187}
Ethan Yonker39662b22017-05-23 08:34:02 -0500188#else //TW_NO_MINUI_CUSTOM_FONTS
Damien Bargiacchi35fff612016-08-11 15:57:03 -0700189void gr_text(const GRFont* font, int x, int y, const char *s, bool bold)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800190{
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800191 if (!font->texture || gr_current_a == 0) return;
192
Damien Bargiacchi35fff612016-08-11 15:57:03 -0700193 bold = bold && (font->texture->height != font->char_height);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800194
195 x += overscan_offset_x;
196 y += overscan_offset_y;
197
198 unsigned char ch;
199 while ((ch = *s++)) {
Damien Bargiacchi35fff612016-08-11 15:57:03 -0700200 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 -0800201
202 if (ch < ' ' || ch > '~') {
203 ch = '?';
204 }
205
Damien Bargiacchi35fff612016-08-11 15:57:03 -0700206 unsigned char* src_p = font->texture->data + ((ch - ' ') * font->char_width) +
207 (bold ? font->char_height * font->texture->row_bytes : 0);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800208 unsigned char* dst_p = gr_draw->data + y*gr_draw->row_bytes + x*gr_draw->pixel_bytes;
209
210 text_blend(src_p, font->texture->row_bytes,
211 dst_p, gr_draw->row_bytes,
Damien Bargiacchi35fff612016-08-11 15:57:03 -0700212 font->char_width, font->char_height);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800213
Damien Bargiacchi35fff612016-08-11 15:57:03 -0700214 x += font->char_width;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800215 }
216}
Ethan Yonker39662b22017-05-23 08:34:02 -0500217#endif //TW_NO_MINUI_CUSTOM_FONTS
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800218
Doug Zongker16f97c32014-03-06 16:16:05 -0800219void gr_texticon(int x, int y, GRSurface* icon) {
220 if (icon == NULL) return;
221
222 if (icon->pixel_bytes != 1) {
223 printf("gr_texticon: source has wrong format\n");
Doug Zongker52eeea4f2012-09-04 14:28:25 -0700224 return;
225 }
Doug Zongker02ec6b82012-08-22 17:26:40 -0700226
Doug Zongkerc560a672012-12-18 16:31:27 -0800227 x += overscan_offset_x;
228 y += overscan_offset_y;
229
Doug Zongker16f97c32014-03-06 16:16:05 -0800230 if (outside(x, y) || outside(x+icon->width-1, y+icon->height-1)) return;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700231
Doug Zongker16f97c32014-03-06 16:16:05 -0800232 unsigned char* src_p = icon->data;
233 unsigned char* dst_p = gr_draw->data + y*gr_draw->row_bytes + x*gr_draw->pixel_bytes;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700234
Doug Zongker16f97c32014-03-06 16:16:05 -0800235 text_blend(src_p, icon->row_bytes,
236 dst_p, gr_draw->row_bytes,
237 icon->width, icon->height);
238}
239
Ethan Yonkere96182e2015-10-13 19:32:03 -0500240void gr_convert_rgb_555()
241{
242 gr_current_r5 = (((gr_current_r & 0xFF) * 0x1F) + 0x7F) / 0xFF;
243 gr_current_g5 = (((gr_current_g & 0xFF) * 0x3F) + 0x7F) / 0xFF;
244 gr_current_b5 = (((gr_current_b & 0xFF) * 0x1F) + 0x7F) / 0xFF;
245
246 rgb_555[0] = (gr_current_g5 << 5) + (gr_current_b5);
247 rgb_555[1] = (gr_current_r5 << 3) + (gr_current_g5 >> 3);
248}
249
Doug Zongker16f97c32014-03-06 16:16:05 -0800250void gr_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
251{
Tony Kuofd778e32015-02-05 21:25:56 +0800252#if defined(RECOVERY_ABGR) || defined(RECOVERY_BGRA)
253 gr_current_r = b;
254 gr_current_g = g;
255 gr_current_b = r;
256 gr_current_a = a;
257#else
Doug Zongker16f97c32014-03-06 16:16:05 -0800258 gr_current_r = r;
259 gr_current_g = g;
260 gr_current_b = b;
261 gr_current_a = a;
Tony Kuofd778e32015-02-05 21:25:56 +0800262#endif
Ethan Yonkere96182e2015-10-13 19:32:03 -0500263 if (gr_draw->pixel_bytes == 2) {
264 gr_convert_rgb_555();
265 }
Doug Zongker16f97c32014-03-06 16:16:05 -0800266}
267
268void gr_clear()
269{
Ethan Yonkere96182e2015-10-13 19:32:03 -0500270 if (gr_draw->pixel_bytes == 2) {
271 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
272 return;
273 }
274
275 // This code only works on 32bpp devices
Elliott Hughes01a4d082015-03-24 15:21:48 -0700276 if (gr_current_r == gr_current_g && gr_current_r == gr_current_b) {
Doug Zongker16f97c32014-03-06 16:16:05 -0800277 memset(gr_draw->data, gr_current_r, gr_draw->height * gr_draw->row_bytes);
278 } else {
Doug Zongker16f97c32014-03-06 16:16:05 -0800279 unsigned char* px = gr_draw->data;
Elliott Hughes01a4d082015-03-24 15:21:48 -0700280 for (int y = 0; y < gr_draw->height; ++y) {
281 for (int x = 0; x < gr_draw->width; ++x) {
Doug Zongker16f97c32014-03-06 16:16:05 -0800282 *px++ = gr_current_r;
283 *px++ = gr_current_g;
284 *px++ = gr_current_b;
285 px++;
286 }
287 px += gr_draw->row_bytes - (gr_draw->width * gr_draw->pixel_bytes);
288 }
289 }
Doug Zongker02ec6b82012-08-22 17:26:40 -0700290}
291
Doug Zongkerc560a672012-12-18 16:31:27 -0800292void gr_fill(int x1, int y1, int x2, int y2)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800293{
Doug Zongkerc560a672012-12-18 16:31:27 -0800294 x1 += overscan_offset_x;
295 y1 += overscan_offset_y;
296
297 x2 += overscan_offset_x;
298 y2 += overscan_offset_y;
299
Doug Zongker16f97c32014-03-06 16:16:05 -0800300 if (outside(x1, y1) || outside(x2-1, y2-1)) return;
301
302 unsigned char* p = gr_draw->data + y1 * gr_draw->row_bytes + x1 * gr_draw->pixel_bytes;
303 if (gr_current_a == 255) {
304 int x, y;
305 for (y = y1; y < y2; ++y) {
306 unsigned char* px = p;
307 for (x = x1; x < x2; ++x) {
Ethan Yonkere96182e2015-10-13 19:32:03 -0500308 if (gr_draw->pixel_bytes == 2) {
309 *px++ = rgb_555[0];
310 *px++ = rgb_555[1];
311 } else {
312 *px++ = gr_current_r;
313 *px++ = gr_current_g;
314 *px++ = gr_current_b;
315 px++;
316 }
Doug Zongker16f97c32014-03-06 16:16:05 -0800317 }
318 p += gr_draw->row_bytes;
319 }
320 } else if (gr_current_a > 0) {
321 int x, y;
322 for (y = y1; y < y2; ++y) {
323 unsigned char* px = p;
324 for (x = x1; x < x2; ++x) {
Ethan Yonkere96182e2015-10-13 19:32:03 -0500325 if (gr_draw->pixel_bytes == 2) {
326 blend_16bpp(px, gr_current_r5, gr_current_g5, gr_current_b5, gr_current_a);
327 px += gr_draw->row_bytes;
328 } else {
329 *px = (*px * (255-gr_current_a) + gr_current_r * gr_current_a) / 255;
330 ++px;
331 *px = (*px * (255-gr_current_a) + gr_current_g * gr_current_a) / 255;
332 ++px;
333 *px = (*px * (255-gr_current_a) + gr_current_b * gr_current_a) / 255;
334 ++px;
335 ++px;
336 }
Doug Zongker16f97c32014-03-06 16:16:05 -0800337 }
338 p += gr_draw->row_bytes;
339 }
340 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800341}
342
Ethan Yonkere96182e2015-10-13 19:32:03 -0500343void gr_blit_32to16(GRSurface* source, int sx, int sy, int w, int h, int dx, int dy) {
344 dx += overscan_offset_x;
345 dy += overscan_offset_y;
346
347 if (outside(dx, dy) || outside(dx+w-1, dy+h-1)) return;
348
349 unsigned char* src_p = source->data + sy*source->row_bytes + sx*source->pixel_bytes;
350 unsigned char* dst_p = gr_draw->data + dy*gr_draw->row_bytes + dx*gr_draw->pixel_bytes;
351
352 int i, j;
353 for (i = 0; i < h; ++i) {
354 unsigned char* spx = src_p;
355 unsigned char* dpx = dst_p;
356
357 for (j = 0; j < w; ++j) {
358 unsigned a = spx[3];
359
360 if (a == 0) {
361 spx += source->pixel_bytes;
362 dpx += gr_draw->pixel_bytes;
363 } else {
364 unsigned r5 = (((*spx++ & 0xFF) * 0x1F) + 0x7F) / 0xFF;
365 unsigned g5 = (((*spx++ & 0xFF) * 0x3F) + 0x7F) / 0xFF;
366 unsigned b5 = (((*spx++ & 0xFF) * 0x1F) + 0x7F) / 0xFF;
367 spx++;
368 if (a == 255) {
369 *dpx++ = (g5 << 5) + (b5);
370 *dpx++ = (r5 << 3) + (g5 >> 3);
371 } else {
372 blend_16bpp(dpx, r5, g5, b5, a);
373 spx += source->pixel_bytes;
374 }
375 }
376 }
377 src_p += source->row_bytes;
378 dst_p += gr_draw->row_bytes;
379 }
380}
381
Doug Zongker16f97c32014-03-06 16:16:05 -0800382void gr_blit(GRSurface* source, int sx, int sy, int w, int h, int dx, int dy) {
383 if (source == NULL) return;
384
385 if (gr_draw->pixel_bytes != source->pixel_bytes) {
Ethan Yonkere96182e2015-10-13 19:32:03 -0500386 if (gr_draw->pixel_bytes == 2 && source->pixel_bytes == 4) {
387 gr_blit_32to16(source, sx, sy, w, h, dx, dy);
388 return;
389 } else {
390 printf("gr_blit: source has wrong format\n");
391 return;
392 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800393 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800394
Doug Zongkerc560a672012-12-18 16:31:27 -0800395 dx += overscan_offset_x;
396 dy += overscan_offset_y;
397
Doug Zongker16f97c32014-03-06 16:16:05 -0800398 if (outside(dx, dy) || outside(dx+w-1, dy+h-1)) return;
399
400 unsigned char* src_p = source->data + sy*source->row_bytes + sx*source->pixel_bytes;
401 unsigned char* dst_p = gr_draw->data + dy*gr_draw->row_bytes + dx*gr_draw->pixel_bytes;
402
403 int i;
404 for (i = 0; i < h; ++i) {
405 memcpy(dst_p, src_p, w * source->pixel_bytes);
406 src_p += source->row_bytes;
407 dst_p += gr_draw->row_bytes;
408 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800409}
410
Doug Zongker16f97c32014-03-06 16:16:05 -0800411unsigned int gr_get_width(GRSurface* surface) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800412 if (surface == NULL) {
413 return 0;
414 }
Doug Zongker16f97c32014-03-06 16:16:05 -0800415 return surface->width;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800416}
417
Doug Zongker16f97c32014-03-06 16:16:05 -0800418unsigned int gr_get_height(GRSurface* surface) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800419 if (surface == NULL) {
420 return 0;
421 }
Doug Zongker16f97c32014-03-06 16:16:05 -0800422 return surface->height;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800423}
424
Ethan Yonker39662b22017-05-23 08:34:02 -0500425#ifdef TW_NO_MINUI_CUSTOM_FONTS
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800426static void gr_init_font(void)
427{
Elliott Hughes07cfb8f2015-04-10 13:12:05 -0700428 gr_font = reinterpret_cast<GRFont*>(calloc(sizeof(*gr_font), 1));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800429
Doug Zongkera418aa72014-03-17 12:10:02 -0700430 int res = res_create_alpha_surface("font", &(gr_font->texture));
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800431 if (res == 0) {
432 // The font image should be a 96x2 array of character images. The
433 // columns are the printable ASCII characters 0x20 - 0x7f. The
434 // top row is regular text; the bottom row is bold.
Ethan Yonker84d61ce2017-05-10 16:11:35 -0500435 gr_font->char_width = gr_font->texture->width / 96;
436 gr_font->char_height = gr_font->texture->height / 2;
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800437 } else {
Doug Zongker55a36ac2013-03-04 15:49:02 -0800438 printf("failed to read font: res=%d\n", res);
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800439
440 // fall back to the compiled-in font.
Elliott Hughes07cfb8f2015-04-10 13:12:05 -0700441 gr_font->texture = reinterpret_cast<GRSurface*>(malloc(sizeof(*gr_font->texture)));
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800442 gr_font->texture->width = font.width;
443 gr_font->texture->height = font.height;
Doug Zongker16f97c32014-03-06 16:16:05 -0800444 gr_font->texture->row_bytes = font.width;
445 gr_font->texture->pixel_bytes = 1;
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800446
Elliott Hughes07cfb8f2015-04-10 13:12:05 -0700447 unsigned char* bits = reinterpret_cast<unsigned char*>(malloc(font.width * font.height));
448 gr_font->texture->data = reinterpret_cast<unsigned char*>(bits);
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800449
450 unsigned char data;
451 unsigned char* in = font.rundata;
452 while((data = *in++)) {
453 memset(bits, (data & 0x80) ? 255 : 0, data & 0x7f);
454 bits += (data & 0x7f);
455 }
456
Ethan Yonker84d61ce2017-05-10 16:11:35 -0500457 gr_font->char_width = font.char_width;
458 gr_font->char_height = font.char_height;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800459 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800460}
461
Ethan Yonker84d61ce2017-05-10 16:11:35 -0500462void gr_set_font(__attribute__ ((unused))const char* name) {
463 //this cm function is made to change font. Don't care, just init the font:
464 gr_init_font();
465 return;
466}
Ethan Yonker39662b22017-05-23 08:34:02 -0500467#else // TW_NO_MINUI_CUSTOM_FONTS
Damien Bargiacchid00f5eb2016-09-09 07:14:08 -0700468int gr_init_font(const char* name, GRFont** dest) {
Rahul Chaudhry4f7faac2016-11-08 16:06:13 -0800469 GRFont* font = static_cast<GRFont*>(calloc(1, sizeof(*gr_font)));
Damien Bargiacchid00f5eb2016-09-09 07:14:08 -0700470 if (font == nullptr) {
471 return -1;
472 }
473
474 int res = res_create_alpha_surface(name, &(font->texture));
Damien Bargiacchi35fff612016-08-11 15:57:03 -0700475 if (res < 0) {
Damien Bargiacchid00f5eb2016-09-09 07:14:08 -0700476 free(font);
Damien Bargiacchi35fff612016-08-11 15:57:03 -0700477 return res;
478 }
479
480 // The font image should be a 96x2 array of character images. The
481 // columns are the printable ASCII characters 0x20 - 0x7f. The
482 // top row is regular text; the bottom row is bold.
Damien Bargiacchid00f5eb2016-09-09 07:14:08 -0700483 font->char_width = font->texture->width / 96;
484 font->char_height = font->texture->height / 2;
485
486 *dest = font;
Damien Bargiacchi35fff612016-08-11 15:57:03 -0700487
488 return 0;
489}
490
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800491static void gr_init_font(void)
492{
Damien Bargiacchid00f5eb2016-09-09 07:14:08 -0700493 int res = gr_init_font("font", &gr_font);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800494 if (res == 0) {
Damien Bargiacchi35fff612016-08-11 15:57:03 -0700495 return;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800496 }
Damien Bargiacchi35fff612016-08-11 15:57:03 -0700497
498 printf("failed to read font: res=%d\n", res);
499
Damien Bargiacchid00f5eb2016-09-09 07:14:08 -0700500
Damien Bargiacchi35fff612016-08-11 15:57:03 -0700501 // fall back to the compiled-in font.
Rahul Chaudhry4f7faac2016-11-08 16:06:13 -0800502 gr_font = static_cast<GRFont*>(calloc(1, sizeof(*gr_font)));
503 gr_font->texture = static_cast<GRSurface*>(malloc(sizeof(*gr_font->texture)));
Damien Bargiacchi35fff612016-08-11 15:57:03 -0700504 gr_font->texture->width = font.width;
505 gr_font->texture->height = font.height;
506 gr_font->texture->row_bytes = font.width;
507 gr_font->texture->pixel_bytes = 1;
508
Rahul Chaudhry4f7faac2016-11-08 16:06:13 -0800509 unsigned char* bits = static_cast<unsigned char*>(malloc(font.width * font.height));
Rahul Chaudhry1cf93f52016-11-08 16:35:22 -0800510 gr_font->texture->data = bits;
Damien Bargiacchi35fff612016-08-11 15:57:03 -0700511
512 unsigned char data;
513 unsigned char* in = font.rundata;
514 while((data = *in++)) {
515 memset(bits, (data & 0x80) ? 255 : 0, data & 0x7f);
516 bits += (data & 0x7f);
517 }
518
519 gr_font->char_width = font.char_width;
520 gr_font->char_height = font.char_height;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800521}
Ethan Yonker39662b22017-05-23 08:34:02 -0500522#endif // TW_NO_MINUI_CUSTOM_FONTS
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800523
Doug Zongker5290f202014-03-11 13:22:04 -0700524void gr_flip() {
Tao Bao557fa1f2017-02-07 12:51:00 -0800525 gr_draw = gr_backend->Flip();
Doug Zongker5290f202014-03-11 13:22:04 -0700526}
527
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800528int gr_init(void)
529{
Tao Bao557fa1f2017-02-07 12:51:00 -0800530 gr_init_font();
Doug Zongker16f97c32014-03-06 16:16:05 -0800531
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500532 auto backend = std::unique_ptr<MinuiBackend>{ std::make_unique<MinuiBackendOverlay>() };
Tao Bao557fa1f2017-02-07 12:51:00 -0800533 gr_draw = backend->Init();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800534
Ethan Yonkerb386f712017-01-20 14:30:28 -0600535#ifdef MSM_BSP
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500536 if (gr_draw) {
537 printf("Using overlay graphics.\n");
Ethan Yonkera59da092015-10-13 19:35:05 -0500538 }
Ethan Yonkerb386f712017-01-20 14:30:28 -0600539#endif
Ethan Yonkera59da092015-10-13 19:35:05 -0500540
541#ifndef MSM_BSP
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500542 if (!gr_draw) {
543 backend = std::make_unique<MinuiBackendAdf>();
544 gr_draw = backend->Init();
545 if (gr_draw)
546 printf("Using adf graphics.\n");
Greg Hackmann41909dd2014-04-25 10:39:50 -0700547 }
Ethan Yonkera59da092015-10-13 19:35:05 -0500548#else
549 printf("Skipping adf graphics because TW_TARGET_USES_QCOM_BSP := true\n");
550#endif
Greg Hackmann41909dd2014-04-25 10:39:50 -0700551
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500552 if (!gr_draw) {
553 backend = std::make_unique<MinuiBackendDrm>();
554 gr_draw = backend->Init();
Ethan Yonkera59da092015-10-13 19:35:05 -0500555 if (gr_draw)
556 printf("Using drm graphics.\n");
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700557 }
558
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500559 if (!gr_draw) {
560 backend = std::make_unique<MinuiBackendFbdev>();
561 gr_draw = backend->Init();
562 if (gr_draw)
Ethan Yonkera59da092015-10-13 19:35:05 -0500563 printf("Using fbdev graphics.\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800564 }
565
Tao Bao557fa1f2017-02-07 12:51:00 -0800566 if (!gr_draw) {
567 return -1;
568 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800569
Tao Bao557fa1f2017-02-07 12:51:00 -0800570 gr_backend = backend.release();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800571
Tao Bao557fa1f2017-02-07 12:51:00 -0800572 overscan_offset_x = gr_draw->width * overscan_percent / 100;
573 overscan_offset_y = gr_draw->height * overscan_percent / 100;
574
575 gr_flip();
576 gr_flip();
577
578 return 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800579}
580
Tao Bao557fa1f2017-02-07 12:51:00 -0800581void gr_exit() {
582 delete gr_backend;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800583}
584
Tao Bao557fa1f2017-02-07 12:51:00 -0800585int gr_fb_width() {
586 return gr_draw->width - 2 * overscan_offset_x;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800587}
588
Tao Bao557fa1f2017-02-07 12:51:00 -0800589int gr_fb_height() {
590 return gr_draw->height - 2 * overscan_offset_y;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800591}
Dima Zavin4daf48a2011-08-30 11:59:20 -0700592
Tao Bao557fa1f2017-02-07 12:51:00 -0800593void gr_fb_blank(bool blank) {
594 gr_backend->Blank(blank);
Dima Zavin4daf48a2011-08-30 11:59:20 -0700595}