blob: 43ec83f08f95e429343dc8f6ac68dbf0a92ffde2 [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
Doug Zongker6fd59ac2013-03-06 15:01:11 -080034#include "font_10x18.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080035#include "minui.h"
Doug Zongker5290f202014-03-11 13:22:04 -070036#include "graphics.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080037
Doug Zongker16f97c32014-03-06 16:16:05 -080038static GRFont* gr_font = NULL;
Doug Zongker5290f202014-03-11 13:22:04 -070039static minui_backend* gr_backend = NULL;
Doug Zongker16f97c32014-03-06 16:16:05 -080040
Doug Zongkerc560a672012-12-18 16:31:27 -080041static int overscan_percent = OVERSCAN_PERCENT;
42static int overscan_offset_x = 0;
43static int overscan_offset_y = 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080044
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;
48static unsigned char gr_current_a = 255;
49
Doug Zongker5290f202014-03-11 13:22:04 -070050static GRSurface* gr_draw = NULL;
Doug Zongker16f97c32014-03-06 16:16:05 -080051
52static bool outside(int x, int y)
53{
54 return x < 0 || x >= gr_draw->width || y < 0 || y >= gr_draw->height;
55}
56
Damien Bargiacchi35fff612016-08-11 15:57:03 -070057const GRFont* gr_sys_font()
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080058{
Damien Bargiacchi35fff612016-08-11 15:57:03 -070059 return gr_font;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080060}
61
Damien Bargiacchi35fff612016-08-11 15:57:03 -070062int gr_measure(const GRFont* font, const char *s)
Dima Zavin3c7f00e2011-08-30 11:58:24 -070063{
Damien Bargiacchi35fff612016-08-11 15:57:03 -070064 return font->char_width * strlen(s);
65}
66
67void gr_font_size(const GRFont* font, int *x, int *y)
68{
69 *x = font->char_width;
70 *y = font->char_height;
Dima Zavin3c7f00e2011-08-30 11:58:24 -070071}
72
Doug Zongker16f97c32014-03-06 16:16:05 -080073static void text_blend(unsigned char* src_p, int src_row_bytes,
74 unsigned char* dst_p, int dst_row_bytes,
75 int width, int height)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080076{
Elliott Hughes01a4d082015-03-24 15:21:48 -070077 for (int j = 0; j < height; ++j) {
Doug Zongker16f97c32014-03-06 16:16:05 -080078 unsigned char* sx = src_p;
79 unsigned char* px = dst_p;
Elliott Hughes01a4d082015-03-24 15:21:48 -070080 for (int i = 0; i < width; ++i) {
Doug Zongker16f97c32014-03-06 16:16:05 -080081 unsigned char a = *sx++;
82 if (gr_current_a < 255) a = ((int)a * gr_current_a) / 255;
83 if (a == 255) {
84 *px++ = gr_current_r;
85 *px++ = gr_current_g;
86 *px++ = gr_current_b;
87 px++;
88 } else if (a > 0) {
89 *px = (*px * (255-a) + gr_current_r * a) / 255;
90 ++px;
91 *px = (*px * (255-a) + gr_current_g * a) / 255;
92 ++px;
93 *px = (*px * (255-a) + gr_current_b * a) / 255;
94 ++px;
95 ++px;
96 } else {
97 px += 4;
98 }
99 }
100 src_p += src_row_bytes;
101 dst_p += dst_row_bytes;
102 }
103}
104
Damien Bargiacchi35fff612016-08-11 15:57:03 -0700105void gr_text(const GRFont* font, int x, int y, const char *s, bool bold)
Doug Zongker16f97c32014-03-06 16:16:05 -0800106{
Elliott Hughes01a4d082015-03-24 15:21:48 -0700107 if (!font->texture || gr_current_a == 0) return;
Doug Zongker55a36ac2013-03-04 15:49:02 -0800108
Damien Bargiacchi35fff612016-08-11 15:57:03 -0700109 bold = bold && (font->texture->height != font->char_height);
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800110
Doug Zongkerc560a672012-12-18 16:31:27 -0800111 x += overscan_offset_x;
112 y += overscan_offset_y;
113
Elliott Hughes01a4d082015-03-24 15:21:48 -0700114 unsigned char ch;
115 while ((ch = *s++)) {
Damien Bargiacchi35fff612016-08-11 15:57:03 -0700116 if (outside(x, y) || outside(x+font->char_width-1, y+font->char_height-1)) break;
Doug Zongker16f97c32014-03-06 16:16:05 -0800117
Elliott Hughes01a4d082015-03-24 15:21:48 -0700118 if (ch < ' ' || ch > '~') {
119 ch = '?';
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800120 }
Elliott Hughes01a4d082015-03-24 15:21:48 -0700121
Damien Bargiacchi35fff612016-08-11 15:57:03 -0700122 unsigned char* src_p = font->texture->data + ((ch - ' ') * font->char_width) +
123 (bold ? font->char_height * font->texture->row_bytes : 0);
Elliott Hughes01a4d082015-03-24 15:21:48 -0700124 unsigned char* dst_p = gr_draw->data + y*gr_draw->row_bytes + x*gr_draw->pixel_bytes;
125
126 text_blend(src_p, font->texture->row_bytes,
127 dst_p, gr_draw->row_bytes,
Damien Bargiacchi35fff612016-08-11 15:57:03 -0700128 font->char_width, font->char_height);
Elliott Hughes01a4d082015-03-24 15:21:48 -0700129
Damien Bargiacchi35fff612016-08-11 15:57:03 -0700130 x += font->char_width;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800131 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800132}
133
Doug Zongker16f97c32014-03-06 16:16:05 -0800134void gr_texticon(int x, int y, GRSurface* icon) {
135 if (icon == NULL) return;
136
137 if (icon->pixel_bytes != 1) {
138 printf("gr_texticon: source has wrong format\n");
Doug Zongker52eeea4f2012-09-04 14:28:25 -0700139 return;
140 }
Doug Zongker02ec6b82012-08-22 17:26:40 -0700141
Doug Zongkerc560a672012-12-18 16:31:27 -0800142 x += overscan_offset_x;
143 y += overscan_offset_y;
144
Doug Zongker16f97c32014-03-06 16:16:05 -0800145 if (outside(x, y) || outside(x+icon->width-1, y+icon->height-1)) return;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700146
Doug Zongker16f97c32014-03-06 16:16:05 -0800147 unsigned char* src_p = icon->data;
148 unsigned char* dst_p = gr_draw->data + y*gr_draw->row_bytes + x*gr_draw->pixel_bytes;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700149
Doug Zongker16f97c32014-03-06 16:16:05 -0800150 text_blend(src_p, icon->row_bytes,
151 dst_p, gr_draw->row_bytes,
152 icon->width, icon->height);
153}
154
155void gr_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
156{
Tony Kuofd778e32015-02-05 21:25:56 +0800157#if defined(RECOVERY_ABGR) || defined(RECOVERY_BGRA)
158 gr_current_r = b;
159 gr_current_g = g;
160 gr_current_b = r;
161 gr_current_a = a;
162#else
Doug Zongker16f97c32014-03-06 16:16:05 -0800163 gr_current_r = r;
164 gr_current_g = g;
165 gr_current_b = b;
166 gr_current_a = a;
Tony Kuofd778e32015-02-05 21:25:56 +0800167#endif
Doug Zongker16f97c32014-03-06 16:16:05 -0800168}
169
170void gr_clear()
171{
Elliott Hughes01a4d082015-03-24 15:21:48 -0700172 if (gr_current_r == gr_current_g && gr_current_r == gr_current_b) {
Doug Zongker16f97c32014-03-06 16:16:05 -0800173 memset(gr_draw->data, gr_current_r, gr_draw->height * gr_draw->row_bytes);
174 } else {
Doug Zongker16f97c32014-03-06 16:16:05 -0800175 unsigned char* px = gr_draw->data;
Elliott Hughes01a4d082015-03-24 15:21:48 -0700176 for (int y = 0; y < gr_draw->height; ++y) {
177 for (int x = 0; x < gr_draw->width; ++x) {
Doug Zongker16f97c32014-03-06 16:16:05 -0800178 *px++ = gr_current_r;
179 *px++ = gr_current_g;
180 *px++ = gr_current_b;
181 px++;
182 }
183 px += gr_draw->row_bytes - (gr_draw->width * gr_draw->pixel_bytes);
184 }
185 }
Doug Zongker02ec6b82012-08-22 17:26:40 -0700186}
187
Doug Zongkerc560a672012-12-18 16:31:27 -0800188void gr_fill(int x1, int y1, int x2, int y2)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800189{
Doug Zongkerc560a672012-12-18 16:31:27 -0800190 x1 += overscan_offset_x;
191 y1 += overscan_offset_y;
192
193 x2 += overscan_offset_x;
194 y2 += overscan_offset_y;
195
Doug Zongker16f97c32014-03-06 16:16:05 -0800196 if (outside(x1, y1) || outside(x2-1, y2-1)) return;
197
198 unsigned char* p = gr_draw->data + y1 * gr_draw->row_bytes + x1 * gr_draw->pixel_bytes;
199 if (gr_current_a == 255) {
200 int x, y;
201 for (y = y1; y < y2; ++y) {
202 unsigned char* px = p;
203 for (x = x1; x < x2; ++x) {
204 *px++ = gr_current_r;
205 *px++ = gr_current_g;
206 *px++ = gr_current_b;
207 px++;
208 }
209 p += gr_draw->row_bytes;
210 }
211 } else if (gr_current_a > 0) {
212 int x, y;
213 for (y = y1; y < y2; ++y) {
214 unsigned char* px = p;
215 for (x = x1; x < x2; ++x) {
216 *px = (*px * (255-gr_current_a) + gr_current_r * gr_current_a) / 255;
217 ++px;
218 *px = (*px * (255-gr_current_a) + gr_current_g * gr_current_a) / 255;
219 ++px;
220 *px = (*px * (255-gr_current_a) + gr_current_b * gr_current_a) / 255;
221 ++px;
222 ++px;
223 }
224 p += gr_draw->row_bytes;
225 }
226 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800227}
228
Doug Zongker16f97c32014-03-06 16:16:05 -0800229void gr_blit(GRSurface* source, int sx, int sy, int w, int h, int dx, int dy) {
230 if (source == NULL) return;
231
232 if (gr_draw->pixel_bytes != source->pixel_bytes) {
233 printf("gr_blit: source has wrong format\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800234 return;
235 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800236
Doug Zongkerc560a672012-12-18 16:31:27 -0800237 dx += overscan_offset_x;
238 dy += overscan_offset_y;
239
Doug Zongker16f97c32014-03-06 16:16:05 -0800240 if (outside(dx, dy) || outside(dx+w-1, dy+h-1)) return;
241
242 unsigned char* src_p = source->data + sy*source->row_bytes + sx*source->pixel_bytes;
243 unsigned char* dst_p = gr_draw->data + dy*gr_draw->row_bytes + dx*gr_draw->pixel_bytes;
244
245 int i;
246 for (i = 0; i < h; ++i) {
247 memcpy(dst_p, src_p, w * source->pixel_bytes);
248 src_p += source->row_bytes;
249 dst_p += gr_draw->row_bytes;
250 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800251}
252
Doug Zongker16f97c32014-03-06 16:16:05 -0800253unsigned int gr_get_width(GRSurface* surface) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800254 if (surface == NULL) {
255 return 0;
256 }
Doug Zongker16f97c32014-03-06 16:16:05 -0800257 return surface->width;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800258}
259
Doug Zongker16f97c32014-03-06 16:16:05 -0800260unsigned int gr_get_height(GRSurface* surface) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800261 if (surface == NULL) {
262 return 0;
263 }
Doug Zongker16f97c32014-03-06 16:16:05 -0800264 return surface->height;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800265}
266
Damien Bargiacchi35fff612016-08-11 15:57:03 -0700267int gr_init_font(const char* name, GRFont* dest) {
268 int res = res_create_alpha_surface(name, &(dest->texture));
269 if (res < 0) {
270 return res;
271 }
272
273 // The font image should be a 96x2 array of character images. The
274 // columns are the printable ASCII characters 0x20 - 0x7f. The
275 // top row is regular text; the bottom row is bold.
276 dest->char_width = dest->texture->width / 96;
277 dest->char_height = dest->texture->height / 2;
278
279 return 0;
280}
281
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800282static void gr_init_font(void)
283{
Elliott Hughes07cfb8f2015-04-10 13:12:05 -0700284 gr_font = reinterpret_cast<GRFont*>(calloc(sizeof(*gr_font), 1));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800285
Damien Bargiacchi35fff612016-08-11 15:57:03 -0700286 int res = gr_init_font("font", gr_font);
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800287 if (res == 0) {
Damien Bargiacchi35fff612016-08-11 15:57:03 -0700288 return;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800289 }
Damien Bargiacchi35fff612016-08-11 15:57:03 -0700290
291 printf("failed to read font: res=%d\n", res);
292
293 // fall back to the compiled-in font.
294 gr_font->texture = reinterpret_cast<GRSurface*>(malloc(sizeof(*gr_font->texture)));
295 gr_font->texture->width = font.width;
296 gr_font->texture->height = font.height;
297 gr_font->texture->row_bytes = font.width;
298 gr_font->texture->pixel_bytes = 1;
299
300 unsigned char* bits = reinterpret_cast<unsigned char*>(malloc(font.width * font.height));
301 gr_font->texture->data = reinterpret_cast<unsigned char*>(bits);
302
303 unsigned char data;
304 unsigned char* in = font.rundata;
305 while((data = *in++)) {
306 memset(bits, (data & 0x80) ? 255 : 0, data & 0x7f);
307 bits += (data & 0x7f);
308 }
309
310 gr_font->char_width = font.char_width;
311 gr_font->char_height = font.char_height;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800312}
313
Doug Zongker16f97c32014-03-06 16:16:05 -0800314#if 0
315// Exercises many of the gr_*() functions; useful for testing.
316static void gr_test() {
317 GRSurface** images;
318 int frames;
319 int result = res_create_multi_surface("icon_installing", &frames, &images);
320 if (result < 0) {
321 printf("create surface %d\n", result);
322 gr_exit();
323 return;
324 }
325
326 time_t start = time(NULL);
327 int x;
328 for (x = 0; x <= 1200; ++x) {
329 if (x < 400) {
330 gr_color(0, 0, 0, 255);
331 } else {
332 gr_color(0, (x-400)%128, 0, 255);
333 }
334 gr_clear();
335
336 gr_color(255, 0, 0, 255);
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700337 GRSurface* frame = images[x%frames];
Doug Zongker16f97c32014-03-06 16:16:05 -0800338 gr_blit(frame, 0, 0, frame->width, frame->height, x, 0);
339
340 gr_color(255, 0, 0, 128);
341 gr_fill(400, 150, 600, 350);
342
343 gr_color(255, 255, 255, 255);
344 gr_text(500, 225, "hello, world!", 0);
345 gr_color(255, 255, 0, 128);
346 gr_text(300+x, 275, "pack my box with five dozen liquor jugs", 1);
347
348 gr_color(0, 0, 255, 128);
349 gr_fill(gr_draw->width - 200 - x, 300, gr_draw->width - x, 500);
350
Doug Zongker5290f202014-03-11 13:22:04 -0700351 gr_draw = gr_backend->flip(gr_backend);
Doug Zongker16f97c32014-03-06 16:16:05 -0800352 }
353 printf("getting end time\n");
354 time_t end = time(NULL);
355 printf("got end time\n");
356 printf("start %ld end %ld\n", (long)start, (long)end);
357 if (end > start) {
358 printf("%.2f fps\n", ((double)x) / (end-start));
359 }
360}
361#endif
362
Doug Zongker5290f202014-03-11 13:22:04 -0700363void gr_flip() {
364 gr_draw = gr_backend->flip(gr_backend);
365}
366
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800367int gr_init(void)
368{
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800369 gr_init_font();
Doug Zongker16f97c32014-03-06 16:16:05 -0800370
Greg Hackmann41909dd2014-04-25 10:39:50 -0700371 gr_backend = open_adf();
372 if (gr_backend) {
373 gr_draw = gr_backend->init(gr_backend);
374 if (!gr_draw) {
375 gr_backend->exit(gr_backend);
376 }
377 }
378
379 if (!gr_draw) {
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700380 gr_backend = open_drm();
381 gr_draw = gr_backend->init(gr_backend);
382 }
383
384 if (!gr_draw) {
Greg Hackmann41909dd2014-04-25 10:39:50 -0700385 gr_backend = open_fbdev();
386 gr_draw = gr_backend->init(gr_backend);
387 if (gr_draw == NULL) {
388 return -1;
389 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800390 }
391
Doug Zongker5290f202014-03-11 13:22:04 -0700392 overscan_offset_x = gr_draw->width * overscan_percent / 100;
393 overscan_offset_y = gr_draw->height * overscan_percent / 100;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800394
Doug Zongker16f97c32014-03-06 16:16:05 -0800395 gr_flip();
396 gr_flip();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800397
398 return 0;
399}
400
401void gr_exit(void)
402{
Doug Zongker5290f202014-03-11 13:22:04 -0700403 gr_backend->exit(gr_backend);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800404}
405
406int gr_fb_width(void)
407{
Doug Zongker16f97c32014-03-06 16:16:05 -0800408 return gr_draw->width - 2*overscan_offset_x;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800409}
410
411int gr_fb_height(void)
412{
Doug Zongker16f97c32014-03-06 16:16:05 -0800413 return gr_draw->height - 2*overscan_offset_y;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800414}
Dima Zavin4daf48a2011-08-30 11:59:20 -0700415
416void gr_fb_blank(bool blank)
417{
Doug Zongker5290f202014-03-11 13:22:04 -0700418 gr_backend->blank(gr_backend, blank);
Dima Zavin4daf48a2011-08-30 11:59:20 -0700419}