blob: 470e735c98d7b6412dae5f34f934e2c0a6503626 [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>
19#include <unistd.h>
20
Dees Troy62b75ab2014-05-02 13:20:33 +000021#include <errno.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080022#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"
Devin Kim862d0262012-07-19 10:47:34 -070037
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080038typedef 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
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080060static struct fb_var_screeninfo vi;
Michael Ward9d1bcdf2011-06-22 14:30:34 -070061static struct fb_fix_screeninfo fi;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080062
Dees Troy62b75ab2014-05-02 13:20:33 +000063static bool has_overlay = false;
64
65bool target_has_overlay(char *version);
66int free_ion_mem(void);
67int alloc_ion_mem(unsigned int size);
68int allocate_overlay(int fd, GGLSurface gr_fb[]);
69int free_overlay(int fd);
70int overlay_display_frame(int fd, GGLubyte* data, size_t size);
71
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080072static int get_framebuffer(GGLSurface *fb)
73{
74 int fd;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080075 void *bits;
76
77 fd = open("/dev/graphics/fb0", O_RDWR);
78 if (fd < 0) {
79 perror("cannot open fb0");
80 return -1;
81 }
82
Michael Ward3dbe66b2011-06-23 19:28:53 -070083 if (ioctl(fd, FBIOGET_VSCREENINFO, &vi) < 0) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080084 perror("failed to get fb0 info");
85 close(fd);
86 return -1;
87 }
88
Michael Ward3dbe66b2011-06-23 19:28:53 -070089 vi.bits_per_pixel = PIXEL_SIZE * 8;
90 if (PIXEL_FORMAT == GGL_PIXEL_FORMAT_BGRA_8888) {
91 vi.red.offset = 8;
92 vi.red.length = 8;
93 vi.green.offset = 16;
94 vi.green.length = 8;
95 vi.blue.offset = 24;
96 vi.blue.length = 8;
97 vi.transp.offset = 0;
98 vi.transp.length = 8;
99 } else if (PIXEL_FORMAT == GGL_PIXEL_FORMAT_RGBX_8888) {
100 vi.red.offset = 24;
101 vi.red.length = 8;
102 vi.green.offset = 16;
103 vi.green.length = 8;
104 vi.blue.offset = 8;
105 vi.blue.length = 8;
106 vi.transp.offset = 0;
107 vi.transp.length = 8;
108 } else { /* RGB565*/
109 vi.red.offset = 11;
110 vi.red.length = 5;
111 vi.green.offset = 5;
112 vi.green.length = 6;
113 vi.blue.offset = 0;
114 vi.blue.length = 5;
115 vi.transp.offset = 0;
116 vi.transp.length = 0;
117 }
118 if (ioctl(fd, FBIOPUT_VSCREENINFO, &vi) < 0) {
119 perror("failed to put fb0 info");
120 close(fd);
121 return -1;
122 }
123
124 if (ioctl(fd, FBIOGET_FSCREENINFO, &fi) < 0) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800125 perror("failed to get fb0 info");
126 close(fd);
127 return -1;
128 }
129
Dees Troy62b75ab2014-05-02 13:20:33 +0000130 has_overlay = target_has_overlay(fi.id);
131
132 if (!has_overlay) {
133 bits = mmap(0, fi.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
134 if (bits == MAP_FAILED) {
135 perror("failed to mmap framebuffer");
136 close(fd);
137 return -1;
138 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800139 }
140
Doug Zongkerc560a672012-12-18 16:31:27 -0800141 overscan_offset_x = vi.xres * overscan_percent / 100;
142 overscan_offset_y = vi.yres * overscan_percent / 100;
143
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800144 fb->version = sizeof(*fb);
145 fb->width = vi.xres;
146 fb->height = vi.yres;
Michael Ward9d1bcdf2011-06-22 14:30:34 -0700147 fb->stride = fi.line_length/PIXEL_SIZE;
Doug Zongkerbe3e6f12011-01-13 16:43:44 -0800148 fb->format = PIXEL_FORMAT;
Dees Troy62b75ab2014-05-02 13:20:33 +0000149 if (!has_overlay) {
150 fb->data = bits;
151 memset(fb->data, 0, vi.yres * fi.line_length);
152 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800153
154 fb++;
155
Octavian Purdila0e348802011-07-01 17:57:45 +0300156 /* check if we can use double buffering */
157 if (vi.yres * fi.line_length * 2 > fi.smem_len)
158 return fd;
159
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800160 fb->version = sizeof(*fb);
161 fb->width = vi.xres;
162 fb->height = vi.yres;
Michael Ward9d1bcdf2011-06-22 14:30:34 -0700163 fb->stride = fi.line_length/PIXEL_SIZE;
Doug Zongkerbe3e6f12011-01-13 16:43:44 -0800164 fb->format = PIXEL_FORMAT;
Dees Troy62b75ab2014-05-02 13:20:33 +0000165 if (!has_overlay) {
166 fb->data = (void*) (((unsigned) bits) + vi.yres * fi.line_length);
167 memset(fb->data, 0, vi.yres * fi.line_length);
168 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800169
170 return fd;
171}
172
173static void get_memory_surface(GGLSurface* ms) {
174 ms->version = sizeof(*ms);
175 ms->width = vi.xres;
176 ms->height = vi.yres;
Michael Ward9d1bcdf2011-06-22 14:30:34 -0700177 ms->stride = fi.line_length/PIXEL_SIZE;
178 ms->data = malloc(fi.line_length * vi.yres);
Doug Zongkerbe3e6f12011-01-13 16:43:44 -0800179 ms->format = PIXEL_FORMAT;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800180}
181
182static void set_active_framebuffer(unsigned n)
183{
Octavian Purdila0e348802011-07-01 17:57:45 +0300184 if (n > 1 || !double_buffering) return;
Devin Kim862d0262012-07-19 10:47:34 -0700185 vi.yres_virtual = vi.yres * NUM_BUFFERS;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800186 vi.yoffset = n * vi.yres;
Doug Zongkerbe3e6f12011-01-13 16:43:44 -0800187 vi.bits_per_pixel = PIXEL_SIZE * 8;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800188 if (ioctl(gr_fb_fd, FBIOPUT_VSCREENINFO, &vi) < 0) {
189 perror("active fb swap failed");
190 }
191}
192
193void gr_flip(void)
194{
Dees Troy62b75ab2014-05-02 13:20:33 +0000195 if (-EINVAL == overlay_display_frame(gr_fb_fd, gr_mem_surface.data,
196 (fi.line_length * vi.yres))) {
197 GGLContext *gl = gr_context;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800198
Dees Troy62b75ab2014-05-02 13:20:33 +0000199 /* swap front and back buffers */
200 if (double_buffering)
201 gr_active_fb = (gr_active_fb + 1) & 1;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800202
Dees Troy62b75ab2014-05-02 13:20:33 +0000203 /* copy data from the in-memory surface to the buffer we're about
204 * to make active. */
205 memcpy(gr_framebuffer[gr_active_fb].data, gr_mem_surface.data,
206 fi.line_length * vi.yres);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800207
Dees Troy62b75ab2014-05-02 13:20:33 +0000208 /* inform the display driver */
209 set_active_framebuffer(gr_active_fb);
210 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800211}
212
213void gr_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
214{
215 GGLContext *gl = gr_context;
216 GGLint color[4];
217 color[0] = ((r << 8) | r) + 1;
218 color[1] = ((g << 8) | g) + 1;
219 color[2] = ((b << 8) | b) + 1;
220 color[3] = ((a << 8) | a) + 1;
221 gl->color4xv(gl, color);
222}
223
Doug Zongker16f97c32014-03-06 16:16:05 -0800224static bool outside(int x, int y)
225{
226 return x < 0 || x >= gr_draw->width || y < 0 || y >= gr_draw->height;
227}
228
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800229int gr_measure(const char *s)
230{
231 return gr_font->cwidth * strlen(s);
232}
233
Dima Zavin3c7f00e2011-08-30 11:58:24 -0700234void gr_font_size(int *x, int *y)
235{
236 *x = gr_font->cwidth;
237 *y = gr_font->cheight;
238}
239
Vojtech Bocek65fdcdd2013-09-06 21:32:22 +0200240int gr_text(int x, int y, const char *s, ...)
241{
242 return gr_text_impl(x, y, s, 0);
243}
244
Doug Zongker16f97c32014-03-06 16:16:05 -0800245static void text_blend(unsigned char* src_p, int src_row_bytes,
246 unsigned char* dst_p, int dst_row_bytes,
247 int width, int height)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800248{
Doug Zongker16f97c32014-03-06 16:16:05 -0800249 int i, j;
250 for (j = 0; j < height; ++j) {
251 unsigned char* sx = src_p;
252 unsigned char* px = dst_p;
253 for (i = 0; i < width; ++i) {
254 unsigned char a = *sx++;
255 if (gr_current_a < 255) a = ((int)a * gr_current_a) / 255;
256 if (a == 255) {
257 *px++ = gr_current_r;
258 *px++ = gr_current_g;
259 *px++ = gr_current_b;
260 px++;
261 } else if (a > 0) {
262 *px = (*px * (255-a) + gr_current_r * a) / 255;
263 ++px;
264 *px = (*px * (255-a) + gr_current_g * a) / 255;
265 ++px;
266 *px = (*px * (255-a) + gr_current_b * a) / 255;
267 ++px;
268 ++px;
269 } else {
270 px += 4;
271 }
272 }
273 src_p += src_row_bytes;
274 dst_p += dst_row_bytes;
275 }
276}
277
278
Vojtech Bocek65fdcdd2013-09-06 21:32:22 +0200279int gr_text_impl(int x, int y, const char *s, int bold)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800280{
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800281 GRFont *font = gr_font;
282 unsigned off;
283
Doug Zongker16f97c32014-03-06 16:16:05 -0800284 if (!font->texture) return;
285 if (gr_current_a == 0) return;
Doug Zongker55a36ac2013-03-04 15:49:02 -0800286
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800287 bold = bold && (font->texture->height != font->cheight);
288
Doug Zongkerc560a672012-12-18 16:31:27 -0800289 x += overscan_offset_x;
290 y += overscan_offset_y;
291
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800292 while((off = *s++)) {
293 off -= 32;
Doug Zongker16f97c32014-03-06 16:16:05 -0800294 if (outside(x, y) || outside(x+font->cwidth-1, y+font->cheight-1)) break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800295 if (off < 96) {
Doug Zongker16f97c32014-03-06 16:16:05 -0800296
297 unsigned char* src_p = font->texture->data + (off * font->cwidth) +
298 (bold ? font->cheight * font->texture->row_bytes : 0);
299 unsigned char* dst_p = gr_draw->data + y*gr_draw->row_bytes + x*gr_draw->pixel_bytes;
300
301 text_blend(src_p, font->texture->row_bytes,
302 dst_p, gr_draw->row_bytes,
303 font->cwidth, font->cheight);
304
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800305 }
306 x += font->cwidth;
307 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800308}
309
Doug Zongker16f97c32014-03-06 16:16:05 -0800310void gr_texticon(int x, int y, GRSurface* icon) {
311 if (icon == NULL) return;
312
313 if (icon->pixel_bytes != 1) {
314 printf("gr_texticon: source has wrong format\n");
Doug Zongker52eeea4f2012-09-04 14:28:25 -0700315 return;
316 }
Doug Zongker02ec6b82012-08-22 17:26:40 -0700317
Doug Zongkerc560a672012-12-18 16:31:27 -0800318 x += overscan_offset_x;
319 y += overscan_offset_y;
320
Doug Zongker16f97c32014-03-06 16:16:05 -0800321 if (outside(x, y) || outside(x+icon->width-1, y+icon->height-1)) return;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700322
Doug Zongker16f97c32014-03-06 16:16:05 -0800323 unsigned char* src_p = icon->data;
324 unsigned char* dst_p = gr_draw->data + y*gr_draw->row_bytes + x*gr_draw->pixel_bytes;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700325
Doug Zongker16f97c32014-03-06 16:16:05 -0800326 text_blend(src_p, icon->row_bytes,
327 dst_p, gr_draw->row_bytes,
328 icon->width, icon->height);
329}
330
331void gr_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
332{
333 gr_current_r = r;
334 gr_current_g = g;
335 gr_current_b = b;
336 gr_current_a = a;
337}
338
339void gr_clear()
340{
341 if (gr_current_r == gr_current_g &&
342 gr_current_r == gr_current_b) {
343 memset(gr_draw->data, gr_current_r, gr_draw->height * gr_draw->row_bytes);
344 } else {
345 int x, y;
346 unsigned char* px = gr_draw->data;
347 for (y = 0; y < gr_draw->height; ++y) {
348 for (x = 0; x < gr_draw->width; ++x) {
349 *px++ = gr_current_r;
350 *px++ = gr_current_g;
351 *px++ = gr_current_b;
352 px++;
353 }
354 px += gr_draw->row_bytes - (gr_draw->width * gr_draw->pixel_bytes);
355 }
356 }
Doug Zongker02ec6b82012-08-22 17:26:40 -0700357}
358
Doug Zongkerc560a672012-12-18 16:31:27 -0800359void gr_fill(int x1, int y1, int x2, int y2)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800360{
Doug Zongkerc560a672012-12-18 16:31:27 -0800361 x1 += overscan_offset_x;
362 y1 += overscan_offset_y;
363
364 x2 += overscan_offset_x;
365 y2 += overscan_offset_y;
366
Doug Zongker16f97c32014-03-06 16:16:05 -0800367 if (outside(x1, y1) || outside(x2-1, y2-1)) return;
368
369 unsigned char* p = gr_draw->data + y1 * gr_draw->row_bytes + x1 * gr_draw->pixel_bytes;
370 if (gr_current_a == 255) {
371 int x, y;
372 for (y = y1; y < y2; ++y) {
373 unsigned char* px = p;
374 for (x = x1; x < x2; ++x) {
375 *px++ = gr_current_r;
376 *px++ = gr_current_g;
377 *px++ = gr_current_b;
378 px++;
379 }
380 p += gr_draw->row_bytes;
381 }
382 } else if (gr_current_a > 0) {
383 int x, y;
384 for (y = y1; y < y2; ++y) {
385 unsigned char* px = p;
386 for (x = x1; x < x2; ++x) {
387 *px = (*px * (255-gr_current_a) + gr_current_r * gr_current_a) / 255;
388 ++px;
389 *px = (*px * (255-gr_current_a) + gr_current_g * gr_current_a) / 255;
390 ++px;
391 *px = (*px * (255-gr_current_a) + gr_current_b * gr_current_a) / 255;
392 ++px;
393 ++px;
394 }
395 p += gr_draw->row_bytes;
396 }
397 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800398}
399
Doug Zongker16f97c32014-03-06 16:16:05 -0800400void gr_blit(GRSurface* source, int sx, int sy, int w, int h, int dx, int dy) {
401 if (source == NULL) return;
402
403 if (gr_draw->pixel_bytes != source->pixel_bytes) {
404 printf("gr_blit: source has wrong format\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800405 return;
406 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800407
Doug Zongkerc560a672012-12-18 16:31:27 -0800408 dx += overscan_offset_x;
409 dy += overscan_offset_y;
410
Doug Zongker16f97c32014-03-06 16:16:05 -0800411 if (outside(dx, dy) || outside(dx+w-1, dy+h-1)) return;
412
413 unsigned char* src_p = source->data + sy*source->row_bytes + sx*source->pixel_bytes;
414 unsigned char* dst_p = gr_draw->data + dy*gr_draw->row_bytes + dx*gr_draw->pixel_bytes;
415
416 int i;
417 for (i = 0; i < h; ++i) {
418 memcpy(dst_p, src_p, w * source->pixel_bytes);
419 src_p += source->row_bytes;
420 dst_p += gr_draw->row_bytes;
421 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800422}
423
Doug Zongker16f97c32014-03-06 16:16:05 -0800424unsigned int gr_get_width(GRSurface* surface) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800425 if (surface == NULL) {
426 return 0;
427 }
Doug Zongker16f97c32014-03-06 16:16:05 -0800428 return surface->width;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800429}
430
Doug Zongker16f97c32014-03-06 16:16:05 -0800431unsigned int gr_get_height(GRSurface* surface) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800432 if (surface == NULL) {
433 return 0;
434 }
Doug Zongker16f97c32014-03-06 16:16:05 -0800435 return surface->height;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800436}
437
438static void gr_init_font(void)
439{
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800440 gr_font = calloc(sizeof(*gr_font), 1);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800441
Doug Zongkera418aa72014-03-17 12:10:02 -0700442 int res = res_create_alpha_surface("font", &(gr_font->texture));
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800443 if (res == 0) {
444 // The font image should be a 96x2 array of character images. The
445 // columns are the printable ASCII characters 0x20 - 0x7f. The
446 // top row is regular text; the bottom row is bold.
447 gr_font->cwidth = gr_font->texture->width / 96;
448 gr_font->cheight = gr_font->texture->height / 2;
449 } else {
Doug Zongker55a36ac2013-03-04 15:49:02 -0800450 printf("failed to read font: res=%d\n", res);
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800451
452 // fall back to the compiled-in font.
453 gr_font->texture = malloc(sizeof(*gr_font->texture));
454 gr_font->texture->width = font.width;
455 gr_font->texture->height = font.height;
Doug Zongker16f97c32014-03-06 16:16:05 -0800456 gr_font->texture->row_bytes = font.width;
457 gr_font->texture->pixel_bytes = 1;
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800458
459 unsigned char* bits = malloc(font.width * font.height);
460 gr_font->texture->data = (void*) bits;
461
462 unsigned char data;
463 unsigned char* in = font.rundata;
464 while((data = *in++)) {
465 memset(bits, (data & 0x80) ? 255 : 0, data & 0x7f);
466 bits += (data & 0x7f);
467 }
468
469 gr_font->cwidth = font.cwidth;
470 gr_font->cheight = font.cheight;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800471 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800472}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800473
Doug Zongker16f97c32014-03-06 16:16:05 -0800474#if 0
475// Exercises many of the gr_*() functions; useful for testing.
476static void gr_test() {
477 GRSurface** images;
478 int frames;
479 int result = res_create_multi_surface("icon_installing", &frames, &images);
480 if (result < 0) {
481 printf("create surface %d\n", result);
482 gr_exit();
483 return;
484 }
485
486 time_t start = time(NULL);
487 int x;
488 for (x = 0; x <= 1200; ++x) {
489 if (x < 400) {
490 gr_color(0, 0, 0, 255);
491 } else {
492 gr_color(0, (x-400)%128, 0, 255);
493 }
494 gr_clear();
495
496 gr_color(255, 0, 0, 255);
497 gr_surface frame = images[x%frames];
498 gr_blit(frame, 0, 0, frame->width, frame->height, x, 0);
499
500 gr_color(255, 0, 0, 128);
501 gr_fill(400, 150, 600, 350);
502
503 gr_color(255, 255, 255, 255);
504 gr_text(500, 225, "hello, world!", 0);
505 gr_color(255, 255, 0, 128);
506 gr_text(300+x, 275, "pack my box with five dozen liquor jugs", 1);
507
508 gr_color(0, 0, 255, 128);
509 gr_fill(gr_draw->width - 200 - x, 300, gr_draw->width - x, 500);
510
Doug Zongker5290f202014-03-11 13:22:04 -0700511 gr_draw = gr_backend->flip(gr_backend);
Doug Zongker16f97c32014-03-06 16:16:05 -0800512 }
513 printf("getting end time\n");
514 time_t end = time(NULL);
515 printf("got end time\n");
516 printf("start %ld end %ld\n", (long)start, (long)end);
517 if (end > start) {
518 printf("%.2f fps\n", ((double)x) / (end-start));
519 }
520}
521#endif
522
Doug Zongker5290f202014-03-11 13:22:04 -0700523void gr_flip() {
524 gr_draw = gr_backend->flip(gr_backend);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800525}
526
527int gr_init(void)
528{
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800529 gr_init_font();
Doug Zongker16f97c32014-03-06 16:16:05 -0800530
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800531 gr_vt_fd = open("/dev/tty0", O_RDWR | O_SYNC);
532 if (gr_vt_fd < 0) {
533 // This is non-fatal; post-Cupcake kernels don't have tty0.
534 perror("can't open /dev/tty0");
535 } else if (ioctl(gr_vt_fd, KDSETMODE, (void*) KD_GRAPHICS)) {
536 // However, if we do open tty0, we expect the ioctl to work.
537 perror("failed KDSETMODE to KD_GRAPHICS on tty0");
538 gr_exit();
539 return -1;
540 }
541
Greg Hackmann41909dd2014-04-25 10:39:50 -0700542 gr_backend = open_adf();
543 if (gr_backend) {
544 gr_draw = gr_backend->init(gr_backend);
545 if (!gr_draw) {
546 gr_backend->exit(gr_backend);
547 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800548 }
549
550 get_memory_surface(&gr_mem_surface);
551
Dees Troy62b75ab2014-05-02 13:20:33 +0000552 fprintf(stderr, "framebuffer: fd %d (%d x %d)\n",
553 gr_fb_fd, gr_framebuffer[0].width, gr_framebuffer[0].height);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800554
Dees Troy62b75ab2014-05-02 13:20:33 +0000555 /* start with 0 as front (displayed) and 1 as back (drawing) */
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800556 gr_active_fb = 0;
Dees Troy62b75ab2014-05-02 13:20:33 +0000557 if (!has_overlay)
558 set_active_framebuffer(0);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800559 gl->colorBuffer(gl, &gr_mem_surface);
560
Greg Hackmann41909dd2014-04-25 10:39:50 -0700561 if (!gr_draw) {
562 gr_backend = open_fbdev();
563 gr_draw = gr_backend->init(gr_backend);
564 if (gr_draw == NULL) {
565 return -1;
566 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800567 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800568
Doug Zongker5290f202014-03-11 13:22:04 -0700569 overscan_offset_x = gr_draw->width * overscan_percent / 100;
570 overscan_offset_y = gr_draw->height * overscan_percent / 100;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800571
Doug Zongker16f97c32014-03-06 16:16:05 -0800572 gr_flip();
573 gr_flip();
Doug Zongkerf6abd402011-09-27 13:09:48 -0700574
Dees Troy62b75ab2014-05-02 13:20:33 +0000575 if (!alloc_ion_mem(fi.line_length * vi.yres))
576 allocate_overlay(gr_fb_fd, gr_framebuffer);
577
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800578 return 0;
579}
580
581void gr_exit(void)
582{
Dees Troy62b75ab2014-05-02 13:20:33 +0000583 free_overlay(gr_fb_fd);
584 free_ion_mem();
585
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800586 close(gr_fb_fd);
587 gr_fb_fd = -1;
588
589 free(gr_mem_surface.data);
590
Doug Zongker5290f202014-03-11 13:22:04 -0700591 gr_backend->exit(gr_backend);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800592
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800593 ioctl(gr_vt_fd, KDSETMODE, (void*) KD_TEXT);
594 close(gr_vt_fd);
595 gr_vt_fd = -1;
596}
597
598int gr_fb_width(void)
599{
Doug Zongker16f97c32014-03-06 16:16:05 -0800600 return gr_draw->width - 2*overscan_offset_x;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800601}
602
603int gr_fb_height(void)
604{
Doug Zongker16f97c32014-03-06 16:16:05 -0800605 return gr_draw->height - 2*overscan_offset_y;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800606}
Dima Zavin4daf48a2011-08-30 11:59:20 -0700607
608void gr_fb_blank(bool blank)
609{
Matt Mower4a5db2d2014-01-20 16:14:25 -0600610#if defined(TW_NO_SCREEN_BLANK) && defined(TW_BRIGHTNESS_PATH) && defined(TW_MAX_BRIGHTNESS)
Ethan Chen0940e412013-10-22 13:48:50 -0700611 int fd;
Matt Mower4a5db2d2014-01-20 16:14:25 -0600612 char brightness[4];
613 snprintf(brightness, 4, "%03d", TW_MAX_BRIGHTNESS/2);
Ethan Chen0940e412013-10-22 13:48:50 -0700614
Matt Mower4a5db2d2014-01-20 16:14:25 -0600615 fd = open(TW_BRIGHTNESS_PATH, O_RDWR);
Ethan Chen0940e412013-10-22 13:48:50 -0700616 if (fd < 0) {
617 perror("cannot open LCD backlight");
618 return;
619 }
Matt Mower4a5db2d2014-01-20 16:14:25 -0600620 write(fd, blank ? "000" : brightness, 3);
Ethan Chen0940e412013-10-22 13:48:50 -0700621 close(fd);
622#else
Doug Zongker5290f202014-03-11 13:22:04 -0700623 gr_backend->blank(gr_backend, blank);
Ethan Yonkera1674162014-11-06 08:35:10 -0600624
Dees Troy62b75ab2014-05-02 13:20:33 +0000625 if (blank)
626 free_overlay(gr_fb_fd);
Dima Zavin4daf48a2011-08-30 11:59:20 -0700627
Dees Troy62b75ab2014-05-02 13:20:33 +0000628 if (!blank)
629 allocate_overlay(gr_fb_fd, gr_framebuffer);
Ethan Chen0940e412013-10-22 13:48:50 -0700630#endif
Dima Zavin4daf48a2011-08-30 11:59:20 -0700631}
Dees Troy62b75ab2014-05-02 13:20:33 +0000632
633void gr_get_memory_surface(gr_surface surface)
634{
635 get_memory_surface( (GGLSurface*) surface);
636}