blob: ec39433b841597149795b7da2ba571f5b4ddf681 [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{
Doug Zongker16f97c32014-03-06 16:16:05 -080080 int i, j;
81 for (j = 0; j < height; ++j) {
82 unsigned char* sx = src_p;
83 unsigned char* px = dst_p;
84 for (i = 0; i < width; ++i) {
85 unsigned char a = *sx++;
86 if (gr_current_a < 255) a = ((int)a * gr_current_a) / 255;
87 if (a == 255) {
88 *px++ = gr_current_r;
89 *px++ = gr_current_g;
90 *px++ = gr_current_b;
91 px++;
92 } else if (a > 0) {
93 *px = (*px * (255-a) + gr_current_r * a) / 255;
94 ++px;
95 *px = (*px * (255-a) + gr_current_g * a) / 255;
96 ++px;
97 *px = (*px * (255-a) + gr_current_b * a) / 255;
98 ++px;
99 ++px;
100 } else {
101 px += 4;
102 }
103 }
104 src_p += src_row_bytes;
105 dst_p += dst_row_bytes;
106 }
107}
108
109
110void gr_text(int x, int y, const char *s, int bold)
111{
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800112 GRFont *font = gr_font;
113 unsigned off;
114
Doug Zongker16f97c32014-03-06 16:16:05 -0800115 if (!font->texture) return;
116 if (gr_current_a == 0) return;
Doug Zongker55a36ac2013-03-04 15:49:02 -0800117
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800118 bold = bold && (font->texture->height != font->cheight);
119
Doug Zongkerc560a672012-12-18 16:31:27 -0800120 x += overscan_offset_x;
121 y += overscan_offset_y;
122
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800123 while((off = *s++)) {
124 off -= 32;
Doug Zongker16f97c32014-03-06 16:16:05 -0800125 if (outside(x, y) || outside(x+font->cwidth-1, y+font->cheight-1)) break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800126 if (off < 96) {
Doug Zongker16f97c32014-03-06 16:16:05 -0800127
128 unsigned char* src_p = font->texture->data + (off * font->cwidth) +
129 (bold ? font->cheight * font->texture->row_bytes : 0);
130 unsigned char* dst_p = gr_draw->data + y*gr_draw->row_bytes + x*gr_draw->pixel_bytes;
131
132 text_blend(src_p, font->texture->row_bytes,
133 dst_p, gr_draw->row_bytes,
134 font->cwidth, font->cheight);
135
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800136 }
137 x += font->cwidth;
138 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800139}
140
Doug Zongker16f97c32014-03-06 16:16:05 -0800141void gr_texticon(int x, int y, GRSurface* icon) {
142 if (icon == NULL) return;
143
144 if (icon->pixel_bytes != 1) {
145 printf("gr_texticon: source has wrong format\n");
Doug Zongker52eeea4f2012-09-04 14:28:25 -0700146 return;
147 }
Doug Zongker02ec6b82012-08-22 17:26:40 -0700148
Doug Zongkerc560a672012-12-18 16:31:27 -0800149 x += overscan_offset_x;
150 y += overscan_offset_y;
151
Doug Zongker16f97c32014-03-06 16:16:05 -0800152 if (outside(x, y) || outside(x+icon->width-1, y+icon->height-1)) return;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700153
Doug Zongker16f97c32014-03-06 16:16:05 -0800154 unsigned char* src_p = icon->data;
155 unsigned char* dst_p = gr_draw->data + y*gr_draw->row_bytes + x*gr_draw->pixel_bytes;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700156
Doug Zongker16f97c32014-03-06 16:16:05 -0800157 text_blend(src_p, icon->row_bytes,
158 dst_p, gr_draw->row_bytes,
159 icon->width, icon->height);
160}
161
162void gr_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
163{
164 gr_current_r = r;
165 gr_current_g = g;
166 gr_current_b = b;
167 gr_current_a = a;
168}
169
170void gr_clear()
171{
172 if (gr_current_r == gr_current_g &&
173 gr_current_r == gr_current_b) {
174 memset(gr_draw->data, gr_current_r, gr_draw->height * gr_draw->row_bytes);
175 } else {
176 int x, y;
177 unsigned char* px = gr_draw->data;
178 for (y = 0; y < gr_draw->height; ++y) {
179 for (x = 0; x < gr_draw->width; ++x) {
180 *px++ = gr_current_r;
181 *px++ = gr_current_g;
182 *px++ = gr_current_b;
183 px++;
184 }
185 px += gr_draw->row_bytes - (gr_draw->width * gr_draw->pixel_bytes);
186 }
187 }
Doug Zongker02ec6b82012-08-22 17:26:40 -0700188}
189
Doug Zongkerc560a672012-12-18 16:31:27 -0800190void gr_fill(int x1, int y1, int x2, int y2)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800191{
Doug Zongkerc560a672012-12-18 16:31:27 -0800192 x1 += overscan_offset_x;
193 y1 += overscan_offset_y;
194
195 x2 += overscan_offset_x;
196 y2 += overscan_offset_y;
197
Doug Zongker16f97c32014-03-06 16:16:05 -0800198 if (outside(x1, y1) || outside(x2-1, y2-1)) return;
199
200 unsigned char* p = gr_draw->data + y1 * gr_draw->row_bytes + x1 * gr_draw->pixel_bytes;
201 if (gr_current_a == 255) {
202 int x, y;
203 for (y = y1; y < y2; ++y) {
204 unsigned char* px = p;
205 for (x = x1; x < x2; ++x) {
206 *px++ = gr_current_r;
207 *px++ = gr_current_g;
208 *px++ = gr_current_b;
209 px++;
210 }
211 p += gr_draw->row_bytes;
212 }
213 } else if (gr_current_a > 0) {
214 int x, y;
215 for (y = y1; y < y2; ++y) {
216 unsigned char* px = p;
217 for (x = x1; x < x2; ++x) {
218 *px = (*px * (255-gr_current_a) + gr_current_r * gr_current_a) / 255;
219 ++px;
220 *px = (*px * (255-gr_current_a) + gr_current_g * gr_current_a) / 255;
221 ++px;
222 *px = (*px * (255-gr_current_a) + gr_current_b * gr_current_a) / 255;
223 ++px;
224 ++px;
225 }
226 p += gr_draw->row_bytes;
227 }
228 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800229}
230
Doug Zongker16f97c32014-03-06 16:16:05 -0800231void gr_blit(GRSurface* source, int sx, int sy, int w, int h, int dx, int dy) {
232 if (source == NULL) return;
233
234 if (gr_draw->pixel_bytes != source->pixel_bytes) {
235 printf("gr_blit: source has wrong format\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800236 return;
237 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800238
Doug Zongkerc560a672012-12-18 16:31:27 -0800239 dx += overscan_offset_x;
240 dy += overscan_offset_y;
241
Doug Zongker16f97c32014-03-06 16:16:05 -0800242 if (outside(dx, dy) || outside(dx+w-1, dy+h-1)) return;
243
244 unsigned char* src_p = source->data + sy*source->row_bytes + sx*source->pixel_bytes;
245 unsigned char* dst_p = gr_draw->data + dy*gr_draw->row_bytes + dx*gr_draw->pixel_bytes;
246
247 int i;
248 for (i = 0; i < h; ++i) {
249 memcpy(dst_p, src_p, w * source->pixel_bytes);
250 src_p += source->row_bytes;
251 dst_p += gr_draw->row_bytes;
252 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800253}
254
Doug Zongker16f97c32014-03-06 16:16:05 -0800255unsigned int gr_get_width(GRSurface* surface) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800256 if (surface == NULL) {
257 return 0;
258 }
Doug Zongker16f97c32014-03-06 16:16:05 -0800259 return surface->width;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800260}
261
Doug Zongker16f97c32014-03-06 16:16:05 -0800262unsigned int gr_get_height(GRSurface* surface) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800263 if (surface == NULL) {
264 return 0;
265 }
Doug Zongker16f97c32014-03-06 16:16:05 -0800266 return surface->height;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800267}
268
269static void gr_init_font(void)
270{
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800271 gr_font = calloc(sizeof(*gr_font), 1);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800272
Doug Zongkera418aa72014-03-17 12:10:02 -0700273 int res = res_create_alpha_surface("font", &(gr_font->texture));
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800274 if (res == 0) {
275 // The font image should be a 96x2 array of character images. The
276 // columns are the printable ASCII characters 0x20 - 0x7f. The
277 // top row is regular text; the bottom row is bold.
278 gr_font->cwidth = gr_font->texture->width / 96;
279 gr_font->cheight = gr_font->texture->height / 2;
280 } else {
Doug Zongker55a36ac2013-03-04 15:49:02 -0800281 printf("failed to read font: res=%d\n", res);
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800282
283 // fall back to the compiled-in font.
284 gr_font->texture = malloc(sizeof(*gr_font->texture));
285 gr_font->texture->width = font.width;
286 gr_font->texture->height = font.height;
Doug Zongker16f97c32014-03-06 16:16:05 -0800287 gr_font->texture->row_bytes = font.width;
288 gr_font->texture->pixel_bytes = 1;
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800289
290 unsigned char* bits = malloc(font.width * font.height);
291 gr_font->texture->data = (void*) bits;
292
293 unsigned char data;
294 unsigned char* in = font.rundata;
295 while((data = *in++)) {
296 memset(bits, (data & 0x80) ? 255 : 0, data & 0x7f);
297 bits += (data & 0x7f);
298 }
299
300 gr_font->cwidth = font.cwidth;
301 gr_font->cheight = font.cheight;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800302 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800303}
304
Doug Zongker16f97c32014-03-06 16:16:05 -0800305#if 0
306// Exercises many of the gr_*() functions; useful for testing.
307static void gr_test() {
308 GRSurface** images;
309 int frames;
310 int result = res_create_multi_surface("icon_installing", &frames, &images);
311 if (result < 0) {
312 printf("create surface %d\n", result);
313 gr_exit();
314 return;
315 }
316
317 time_t start = time(NULL);
318 int x;
319 for (x = 0; x <= 1200; ++x) {
320 if (x < 400) {
321 gr_color(0, 0, 0, 255);
322 } else {
323 gr_color(0, (x-400)%128, 0, 255);
324 }
325 gr_clear();
326
327 gr_color(255, 0, 0, 255);
328 gr_surface frame = images[x%frames];
329 gr_blit(frame, 0, 0, frame->width, frame->height, x, 0);
330
331 gr_color(255, 0, 0, 128);
332 gr_fill(400, 150, 600, 350);
333
334 gr_color(255, 255, 255, 255);
335 gr_text(500, 225, "hello, world!", 0);
336 gr_color(255, 255, 0, 128);
337 gr_text(300+x, 275, "pack my box with five dozen liquor jugs", 1);
338
339 gr_color(0, 0, 255, 128);
340 gr_fill(gr_draw->width - 200 - x, 300, gr_draw->width - x, 500);
341
Doug Zongker5290f202014-03-11 13:22:04 -0700342 gr_draw = gr_backend->flip(gr_backend);
Doug Zongker16f97c32014-03-06 16:16:05 -0800343 }
344 printf("getting end time\n");
345 time_t end = time(NULL);
346 printf("got end time\n");
347 printf("start %ld end %ld\n", (long)start, (long)end);
348 if (end > start) {
349 printf("%.2f fps\n", ((double)x) / (end-start));
350 }
351}
352#endif
353
Doug Zongker5290f202014-03-11 13:22:04 -0700354void gr_flip() {
355 gr_draw = gr_backend->flip(gr_backend);
356}
357
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800358int gr_init(void)
359{
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800360 gr_init_font();
Doug Zongker16f97c32014-03-06 16:16:05 -0800361
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800362 gr_vt_fd = open("/dev/tty0", O_RDWR | O_SYNC);
363 if (gr_vt_fd < 0) {
364 // This is non-fatal; post-Cupcake kernels don't have tty0.
365 perror("can't open /dev/tty0");
366 } else if (ioctl(gr_vt_fd, KDSETMODE, (void*) KD_GRAPHICS)) {
367 // However, if we do open tty0, we expect the ioctl to work.
368 perror("failed KDSETMODE to KD_GRAPHICS on tty0");
369 gr_exit();
370 return -1;
371 }
372
Greg Hackmann41909dd2014-04-25 10:39:50 -0700373 gr_backend = open_adf();
374 if (gr_backend) {
375 gr_draw = gr_backend->init(gr_backend);
376 if (!gr_draw) {
377 gr_backend->exit(gr_backend);
378 }
379 }
380
381 if (!gr_draw) {
382 gr_backend = open_fbdev();
383 gr_draw = gr_backend->init(gr_backend);
384 if (gr_draw == NULL) {
385 return -1;
386 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800387 }
388
Doug Zongker5290f202014-03-11 13:22:04 -0700389 overscan_offset_x = gr_draw->width * overscan_percent / 100;
390 overscan_offset_y = gr_draw->height * overscan_percent / 100;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800391
Doug Zongker16f97c32014-03-06 16:16:05 -0800392 gr_flip();
393 gr_flip();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800394
395 return 0;
396}
397
398void gr_exit(void)
399{
Doug Zongker5290f202014-03-11 13:22:04 -0700400 gr_backend->exit(gr_backend);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800401
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800402 ioctl(gr_vt_fd, KDSETMODE, (void*) KD_TEXT);
403 close(gr_vt_fd);
404 gr_vt_fd = -1;
405}
406
407int gr_fb_width(void)
408{
Doug Zongker16f97c32014-03-06 16:16:05 -0800409 return gr_draw->width - 2*overscan_offset_x;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800410}
411
412int gr_fb_height(void)
413{
Doug Zongker16f97c32014-03-06 16:16:05 -0800414 return gr_draw->height - 2*overscan_offset_y;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800415}
Dima Zavin4daf48a2011-08-30 11:59:20 -0700416
417void gr_fb_blank(bool blank)
418{
Doug Zongker5290f202014-03-11 13:22:04 -0700419 gr_backend->blank(gr_backend, blank);
Dima Zavin4daf48a2011-08-30 11:59:20 -0700420}