blob: 1a9be6cb2872ddf8e39e1e3a5bc0825e0ce595d0 [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
38typedef struct {
Doug Zongker16f97c32014-03-06 16:16:05 -080039 GRSurface* texture;
40 int cwidth;
41 int cheight;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080042} GRFont;
43
Doug Zongker16f97c32014-03-06 16:16:05 -080044static GRFont* gr_font = NULL;
Doug Zongker5290f202014-03-11 13:22:04 -070045static minui_backend* gr_backend = NULL;
Doug Zongker16f97c32014-03-06 16:16:05 -080046
Doug Zongkerc560a672012-12-18 16:31:27 -080047static int overscan_percent = OVERSCAN_PERCENT;
48static int overscan_offset_x = 0;
49static int overscan_offset_y = 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080050
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080051static int gr_vt_fd = -1;
52
Doug Zongker16f97c32014-03-06 16:16:05 -080053static unsigned char gr_current_r = 255;
54static unsigned char gr_current_g = 255;
55static unsigned char gr_current_b = 255;
56static unsigned char gr_current_a = 255;
57
Doug Zongker5290f202014-03-11 13:22:04 -070058static GRSurface* gr_draw = NULL;
Doug Zongker16f97c32014-03-06 16:16:05 -080059
60static bool outside(int x, int y)
61{
62 return x < 0 || x >= gr_draw->width || y < 0 || y >= gr_draw->height;
63}
64
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080065int gr_measure(const char *s)
66{
67 return gr_font->cwidth * strlen(s);
68}
69
Dima Zavin3c7f00e2011-08-30 11:58:24 -070070void gr_font_size(int *x, int *y)
71{
72 *x = gr_font->cwidth;
73 *y = gr_font->cheight;
74}
75
Doug Zongker16f97c32014-03-06 16:16:05 -080076static void text_blend(unsigned char* src_p, int src_row_bytes,
77 unsigned char* dst_p, int dst_row_bytes,
78 int width, int height)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080079{
Elliott Hughes01a4d082015-03-24 15:21:48 -070080 for (int j = 0; j < height; ++j) {
Doug Zongker16f97c32014-03-06 16:16:05 -080081 unsigned char* sx = src_p;
82 unsigned char* px = dst_p;
Elliott Hughes01a4d082015-03-24 15:21:48 -070083 for (int i = 0; i < width; ++i) {
Doug Zongker16f97c32014-03-06 16:16:05 -080084 unsigned char a = *sx++;
85 if (gr_current_a < 255) a = ((int)a * gr_current_a) / 255;
86 if (a == 255) {
87 *px++ = gr_current_r;
88 *px++ = gr_current_g;
89 *px++ = gr_current_b;
90 px++;
91 } else if (a > 0) {
92 *px = (*px * (255-a) + gr_current_r * a) / 255;
93 ++px;
94 *px = (*px * (255-a) + gr_current_g * a) / 255;
95 ++px;
96 *px = (*px * (255-a) + gr_current_b * a) / 255;
97 ++px;
98 ++px;
99 } else {
100 px += 4;
101 }
102 }
103 src_p += src_row_bytes;
104 dst_p += dst_row_bytes;
105 }
106}
107
Doug Zongker16f97c32014-03-06 16:16:05 -0800108void gr_text(int x, int y, const char *s, int bold)
109{
Elliott Hughes01a4d082015-03-24 15:21:48 -0700110 GRFont* font = gr_font;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800111
Elliott Hughes01a4d082015-03-24 15:21:48 -0700112 if (!font->texture || gr_current_a == 0) return;
Doug Zongker55a36ac2013-03-04 15:49:02 -0800113
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800114 bold = bold && (font->texture->height != font->cheight);
115
Doug Zongkerc560a672012-12-18 16:31:27 -0800116 x += overscan_offset_x;
117 y += overscan_offset_y;
118
Elliott Hughes01a4d082015-03-24 15:21:48 -0700119 unsigned char ch;
120 while ((ch = *s++)) {
Doug Zongker16f97c32014-03-06 16:16:05 -0800121 if (outside(x, y) || outside(x+font->cwidth-1, y+font->cheight-1)) break;
Doug Zongker16f97c32014-03-06 16:16:05 -0800122
Elliott Hughes01a4d082015-03-24 15:21:48 -0700123 if (ch < ' ' || ch > '~') {
124 ch = '?';
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800125 }
Elliott Hughes01a4d082015-03-24 15:21:48 -0700126
127 unsigned char* src_p = font->texture->data + ((ch - ' ') * font->cwidth) +
128 (bold ? font->cheight * font->texture->row_bytes : 0);
129 unsigned char* dst_p = gr_draw->data + y*gr_draw->row_bytes + x*gr_draw->pixel_bytes;
130
131 text_blend(src_p, font->texture->row_bytes,
132 dst_p, gr_draw->row_bytes,
133 font->cwidth, font->cheight);
134
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800135 x += font->cwidth;
136 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800137}
138
Doug Zongker16f97c32014-03-06 16:16:05 -0800139void gr_texticon(int x, int y, GRSurface* icon) {
140 if (icon == NULL) return;
141
142 if (icon->pixel_bytes != 1) {
143 printf("gr_texticon: source has wrong format\n");
Doug Zongker52eeea4f2012-09-04 14:28:25 -0700144 return;
145 }
Doug Zongker02ec6b82012-08-22 17:26:40 -0700146
Doug Zongkerc560a672012-12-18 16:31:27 -0800147 x += overscan_offset_x;
148 y += overscan_offset_y;
149
Doug Zongker16f97c32014-03-06 16:16:05 -0800150 if (outside(x, y) || outside(x+icon->width-1, y+icon->height-1)) return;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700151
Doug Zongker16f97c32014-03-06 16:16:05 -0800152 unsigned char* src_p = icon->data;
153 unsigned char* dst_p = gr_draw->data + y*gr_draw->row_bytes + x*gr_draw->pixel_bytes;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700154
Doug Zongker16f97c32014-03-06 16:16:05 -0800155 text_blend(src_p, icon->row_bytes,
156 dst_p, gr_draw->row_bytes,
157 icon->width, icon->height);
158}
159
160void gr_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
161{
Tony Kuofd778e32015-02-05 21:25:56 +0800162#if defined(RECOVERY_ABGR) || defined(RECOVERY_BGRA)
163 gr_current_r = b;
164 gr_current_g = g;
165 gr_current_b = r;
166 gr_current_a = a;
167#else
Doug Zongker16f97c32014-03-06 16:16:05 -0800168 gr_current_r = r;
169 gr_current_g = g;
170 gr_current_b = b;
171 gr_current_a = a;
Tony Kuofd778e32015-02-05 21:25:56 +0800172#endif
Doug Zongker16f97c32014-03-06 16:16:05 -0800173}
174
175void gr_clear()
176{
Elliott Hughes01a4d082015-03-24 15:21:48 -0700177 if (gr_current_r == gr_current_g && gr_current_r == gr_current_b) {
Doug Zongker16f97c32014-03-06 16:16:05 -0800178 memset(gr_draw->data, gr_current_r, gr_draw->height * gr_draw->row_bytes);
179 } else {
Doug Zongker16f97c32014-03-06 16:16:05 -0800180 unsigned char* px = gr_draw->data;
Elliott Hughes01a4d082015-03-24 15:21:48 -0700181 for (int y = 0; y < gr_draw->height; ++y) {
182 for (int x = 0; x < gr_draw->width; ++x) {
Doug Zongker16f97c32014-03-06 16:16:05 -0800183 *px++ = gr_current_r;
184 *px++ = gr_current_g;
185 *px++ = gr_current_b;
186 px++;
187 }
188 px += gr_draw->row_bytes - (gr_draw->width * gr_draw->pixel_bytes);
189 }
190 }
Doug Zongker02ec6b82012-08-22 17:26:40 -0700191}
192
Doug Zongkerc560a672012-12-18 16:31:27 -0800193void gr_fill(int x1, int y1, int x2, int y2)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800194{
Doug Zongkerc560a672012-12-18 16:31:27 -0800195 x1 += overscan_offset_x;
196 y1 += overscan_offset_y;
197
198 x2 += overscan_offset_x;
199 y2 += overscan_offset_y;
200
Doug Zongker16f97c32014-03-06 16:16:05 -0800201 if (outside(x1, y1) || outside(x2-1, y2-1)) return;
202
203 unsigned char* p = gr_draw->data + y1 * gr_draw->row_bytes + x1 * gr_draw->pixel_bytes;
204 if (gr_current_a == 255) {
205 int x, y;
206 for (y = y1; y < y2; ++y) {
207 unsigned char* px = p;
208 for (x = x1; x < x2; ++x) {
209 *px++ = gr_current_r;
210 *px++ = gr_current_g;
211 *px++ = gr_current_b;
212 px++;
213 }
214 p += gr_draw->row_bytes;
215 }
216 } else if (gr_current_a > 0) {
217 int x, y;
218 for (y = y1; y < y2; ++y) {
219 unsigned char* px = p;
220 for (x = x1; x < x2; ++x) {
221 *px = (*px * (255-gr_current_a) + gr_current_r * gr_current_a) / 255;
222 ++px;
223 *px = (*px * (255-gr_current_a) + gr_current_g * gr_current_a) / 255;
224 ++px;
225 *px = (*px * (255-gr_current_a) + gr_current_b * gr_current_a) / 255;
226 ++px;
227 ++px;
228 }
229 p += gr_draw->row_bytes;
230 }
231 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800232}
233
Doug Zongker16f97c32014-03-06 16:16:05 -0800234void gr_blit(GRSurface* source, int sx, int sy, int w, int h, int dx, int dy) {
235 if (source == NULL) return;
236
237 if (gr_draw->pixel_bytes != source->pixel_bytes) {
238 printf("gr_blit: source has wrong format\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800239 return;
240 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800241
Doug Zongkerc560a672012-12-18 16:31:27 -0800242 dx += overscan_offset_x;
243 dy += overscan_offset_y;
244
Doug Zongker16f97c32014-03-06 16:16:05 -0800245 if (outside(dx, dy) || outside(dx+w-1, dy+h-1)) return;
246
247 unsigned char* src_p = source->data + sy*source->row_bytes + sx*source->pixel_bytes;
248 unsigned char* dst_p = gr_draw->data + dy*gr_draw->row_bytes + dx*gr_draw->pixel_bytes;
249
250 int i;
251 for (i = 0; i < h; ++i) {
252 memcpy(dst_p, src_p, w * source->pixel_bytes);
253 src_p += source->row_bytes;
254 dst_p += gr_draw->row_bytes;
255 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800256}
257
Doug Zongker16f97c32014-03-06 16:16:05 -0800258unsigned int gr_get_width(GRSurface* surface) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800259 if (surface == NULL) {
260 return 0;
261 }
Doug Zongker16f97c32014-03-06 16:16:05 -0800262 return surface->width;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800263}
264
Doug Zongker16f97c32014-03-06 16:16:05 -0800265unsigned int gr_get_height(GRSurface* surface) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800266 if (surface == NULL) {
267 return 0;
268 }
Doug Zongker16f97c32014-03-06 16:16:05 -0800269 return surface->height;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800270}
271
272static void gr_init_font(void)
273{
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800274 gr_font = calloc(sizeof(*gr_font), 1);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800275
Doug Zongkera418aa72014-03-17 12:10:02 -0700276 int res = res_create_alpha_surface("font", &(gr_font->texture));
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800277 if (res == 0) {
278 // The font image should be a 96x2 array of character images. The
279 // columns are the printable ASCII characters 0x20 - 0x7f. The
280 // top row is regular text; the bottom row is bold.
281 gr_font->cwidth = gr_font->texture->width / 96;
282 gr_font->cheight = gr_font->texture->height / 2;
283 } else {
Doug Zongker55a36ac2013-03-04 15:49:02 -0800284 printf("failed to read font: res=%d\n", res);
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800285
286 // fall back to the compiled-in font.
287 gr_font->texture = malloc(sizeof(*gr_font->texture));
288 gr_font->texture->width = font.width;
289 gr_font->texture->height = font.height;
Doug Zongker16f97c32014-03-06 16:16:05 -0800290 gr_font->texture->row_bytes = font.width;
291 gr_font->texture->pixel_bytes = 1;
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800292
293 unsigned char* bits = malloc(font.width * font.height);
294 gr_font->texture->data = (void*) bits;
295
296 unsigned char data;
297 unsigned char* in = font.rundata;
298 while((data = *in++)) {
299 memset(bits, (data & 0x80) ? 255 : 0, data & 0x7f);
300 bits += (data & 0x7f);
301 }
302
303 gr_font->cwidth = font.cwidth;
304 gr_font->cheight = font.cheight;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800305 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800306}
307
Doug Zongker16f97c32014-03-06 16:16:05 -0800308#if 0
309// Exercises many of the gr_*() functions; useful for testing.
310static void gr_test() {
311 GRSurface** images;
312 int frames;
313 int result = res_create_multi_surface("icon_installing", &frames, &images);
314 if (result < 0) {
315 printf("create surface %d\n", result);
316 gr_exit();
317 return;
318 }
319
320 time_t start = time(NULL);
321 int x;
322 for (x = 0; x <= 1200; ++x) {
323 if (x < 400) {
324 gr_color(0, 0, 0, 255);
325 } else {
326 gr_color(0, (x-400)%128, 0, 255);
327 }
328 gr_clear();
329
330 gr_color(255, 0, 0, 255);
331 gr_surface frame = images[x%frames];
332 gr_blit(frame, 0, 0, frame->width, frame->height, x, 0);
333
334 gr_color(255, 0, 0, 128);
335 gr_fill(400, 150, 600, 350);
336
337 gr_color(255, 255, 255, 255);
338 gr_text(500, 225, "hello, world!", 0);
339 gr_color(255, 255, 0, 128);
340 gr_text(300+x, 275, "pack my box with five dozen liquor jugs", 1);
341
342 gr_color(0, 0, 255, 128);
343 gr_fill(gr_draw->width - 200 - x, 300, gr_draw->width - x, 500);
344
Doug Zongker5290f202014-03-11 13:22:04 -0700345 gr_draw = gr_backend->flip(gr_backend);
Doug Zongker16f97c32014-03-06 16:16:05 -0800346 }
347 printf("getting end time\n");
348 time_t end = time(NULL);
349 printf("got end time\n");
350 printf("start %ld end %ld\n", (long)start, (long)end);
351 if (end > start) {
352 printf("%.2f fps\n", ((double)x) / (end-start));
353 }
354}
355#endif
356
Doug Zongker5290f202014-03-11 13:22:04 -0700357void gr_flip() {
358 gr_draw = gr_backend->flip(gr_backend);
359}
360
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800361int gr_init(void)
362{
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800363 gr_init_font();
Doug Zongker16f97c32014-03-06 16:16:05 -0800364
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800365 gr_vt_fd = open("/dev/tty0", O_RDWR | O_SYNC);
366 if (gr_vt_fd < 0) {
367 // This is non-fatal; post-Cupcake kernels don't have tty0.
368 perror("can't open /dev/tty0");
369 } else if (ioctl(gr_vt_fd, KDSETMODE, (void*) KD_GRAPHICS)) {
370 // However, if we do open tty0, we expect the ioctl to work.
371 perror("failed KDSETMODE to KD_GRAPHICS on tty0");
372 gr_exit();
373 return -1;
374 }
375
Greg Hackmann41909dd2014-04-25 10:39:50 -0700376 gr_backend = open_adf();
377 if (gr_backend) {
378 gr_draw = gr_backend->init(gr_backend);
379 if (!gr_draw) {
380 gr_backend->exit(gr_backend);
381 }
382 }
383
384 if (!gr_draw) {
385 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
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800405 ioctl(gr_vt_fd, KDSETMODE, (void*) KD_TEXT);
406 close(gr_vt_fd);
407 gr_vt_fd = -1;
408}
409
410int gr_fb_width(void)
411{
Doug Zongker16f97c32014-03-06 16:16:05 -0800412 return gr_draw->width - 2*overscan_offset_x;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800413}
414
415int gr_fb_height(void)
416{
Doug Zongker16f97c32014-03-06 16:16:05 -0800417 return gr_draw->height - 2*overscan_offset_y;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800418}
Dima Zavin4daf48a2011-08-30 11:59:20 -0700419
420void gr_fb_blank(bool blank)
421{
Doug Zongker5290f202014-03-11 13:22:04 -0700422 gr_backend->blank(gr_backend, blank);
Dima Zavin4daf48a2011-08-30 11:59:20 -0700423}