blob: ddff571feda299d7972704f83937966f09911c20 [file] [log] [blame]
Dees_Troy51a0e822012-09-05 15:24:24 -04001/*
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
17#include <stdbool.h>
18#include <stdlib.h>
19#include <unistd.h>
20
21#include <fcntl.h>
22#include <stdio.h>
23
24#include <sys/ioctl.h>
25#include <sys/mman.h>
26#include <sys/types.h>
27
28#include <linux/fb.h>
29#include <linux/kd.h>
30
31#include <pixelflinger/pixelflinger.h>
32
33#include "minui.h"
34
35#ifdef BOARD_USE_CUSTOM_RECOVERY_FONT
36#include BOARD_USE_CUSTOM_RECOVERY_FONT
37#else
38#include "font_10x18.h"
39#endif
40
41#ifdef RECOVERY_BGRA
42#define PIXEL_FORMAT GGL_PIXEL_FORMAT_BGRA_8888
43#define PIXEL_SIZE 4
44#endif
45#ifdef RECOVERY_RGBX
46#define PIXEL_FORMAT GGL_PIXEL_FORMAT_RGBX_8888
47#define PIXEL_SIZE 4
48#endif
49#ifndef PIXEL_FORMAT
50#define PIXEL_FORMAT GGL_PIXEL_FORMAT_RGB_565
51#define PIXEL_SIZE 2
52#endif
53
Hiemanshu Sharmaacf6a9b2012-11-21 11:28:36 -060054#define NUM_BUFFERS 2
55
Dees_Troy51a0e822012-09-05 15:24:24 -040056// #define PRINT_SCREENINFO 1 // Enables printing of screen info to log
57
58typedef struct {
59 GGLSurface texture;
60 unsigned offset[97];
61 unsigned cheight;
62 unsigned ascent;
63} GRFont;
64
65static GRFont *gr_font = 0;
66static GGLContext *gr_context = 0;
67static GGLSurface gr_font_texture;
Hiemanshu Sharmaacf6a9b2012-11-21 11:28:36 -060068static GGLSurface gr_framebuffer[NUM_BUFFERS];
Dees_Troy51a0e822012-09-05 15:24:24 -040069static GGLSurface gr_mem_surface;
70static unsigned gr_active_fb = 0;
Hiemanshu Sharma94f6c882012-11-21 11:25:22 -060071static unsigned double_buffering = 0;
Vojtech Bocek1a4744a2014-02-06 19:52:28 +010072static int gr_is_curr_clr_opaque = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -040073
74static int gr_fb_fd = -1;
75static int gr_vt_fd = -1;
76
77static struct fb_var_screeninfo vi;
78static struct fb_fix_screeninfo fi;
79
80#ifdef PRINT_SCREENINFO
81static void print_fb_var_screeninfo()
82{
Dees_Troy2673cec2013-04-02 20:22:16 +000083 printf("vi.xres: %d\n", vi.xres);
84 printf("vi.yres: %d\n", vi.yres);
85 printf("vi.xres_virtual: %d\n", vi.xres_virtual);
86 printf("vi.yres_virtual: %d\n", vi.yres_virtual);
87 printf("vi.xoffset: %d\n", vi.xoffset);
88 printf("vi.yoffset: %d\n", vi.yoffset);
89 printf("vi.bits_per_pixel: %d\n", vi.bits_per_pixel);
90 printf("vi.grayscale: %d\n", vi.grayscale);
Dees_Troy51a0e822012-09-05 15:24:24 -040091}
92#endif
93
94static int get_framebuffer(GGLSurface *fb)
95{
96 int fd;
97 void *bits;
98
99 fd = open("/dev/graphics/fb0", O_RDWR);
100 if (fd < 0) {
101 perror("cannot open fb0");
102 return -1;
103 }
104
105 if (ioctl(fd, FBIOGET_VSCREENINFO, &vi) < 0) {
106 perror("failed to get fb0 info");
107 close(fd);
108 return -1;
109 }
110
111 fprintf(stderr, "Pixel format: %dx%d @ %dbpp\n", vi.xres, vi.yres, vi.bits_per_pixel);
112
113 vi.bits_per_pixel = PIXEL_SIZE * 8;
114 if (PIXEL_FORMAT == GGL_PIXEL_FORMAT_BGRA_8888) {
115 fprintf(stderr, "Pixel format: BGRA_8888\n");
116 if (PIXEL_SIZE != 4) fprintf(stderr, "E: Pixel Size mismatch!\n");
117 vi.red.offset = 8;
118 vi.red.length = 8;
119 vi.green.offset = 16;
120 vi.green.length = 8;
121 vi.blue.offset = 24;
122 vi.blue.length = 8;
123 vi.transp.offset = 0;
124 vi.transp.length = 8;
125 } else if (PIXEL_FORMAT == GGL_PIXEL_FORMAT_RGBX_8888) {
126 fprintf(stderr, "Pixel format: RGBX_8888\n");
127 if (PIXEL_SIZE != 4) fprintf(stderr, "E: Pixel Size mismatch!\n");
128 vi.red.offset = 24;
129 vi.red.length = 8;
130 vi.green.offset = 16;
131 vi.green.length = 8;
132 vi.blue.offset = 8;
133 vi.blue.length = 8;
134 vi.transp.offset = 0;
135 vi.transp.length = 8;
136 } else if (PIXEL_FORMAT == GGL_PIXEL_FORMAT_RGB_565) {
137#ifdef RECOVERY_RGB_565
138 fprintf(stderr, "Pixel format: RGB_565\n");
139 vi.blue.offset = 0;
140 vi.green.offset = 5;
141 vi.red.offset = 11;
142#else
143 fprintf(stderr, "Pixel format: BGR_565\n");
144 vi.blue.offset = 11;
145 vi.green.offset = 5;
146 vi.red.offset = 0;
147#endif
148 if (PIXEL_SIZE != 2) fprintf(stderr, "E: Pixel Size mismatch!\n");
149 vi.blue.length = 5;
150 vi.green.length = 6;
151 vi.red.length = 5;
152 vi.blue.msb_right = 0;
153 vi.green.msb_right = 0;
154 vi.red.msb_right = 0;
155 vi.transp.offset = 0;
156 vi.transp.length = 0;
157 }
158 else
159 {
160 perror("unknown pixel format");
161 close(fd);
162 return -1;
163 }
164
165 vi.vmode = FB_VMODE_NONINTERLACED;
166 vi.activate = FB_ACTIVATE_NOW | FB_ACTIVATE_FORCE;
167
168 if (ioctl(fd, FBIOPUT_VSCREENINFO, &vi) < 0) {
169 perror("failed to put fb0 info");
170 close(fd);
171 return -1;
172 }
173
174 if (ioctl(fd, FBIOGET_FSCREENINFO, &fi) < 0) {
175 perror("failed to get fb0 info");
176 close(fd);
177 return -1;
178 }
179
180 bits = mmap(0, fi.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
181 if (bits == MAP_FAILED) {
182 perror("failed to mmap framebuffer");
183 close(fd);
184 return -1;
185 }
186
187#ifdef RECOVERY_GRAPHICS_USE_LINELENGTH
188 vi.xres_virtual = fi.line_length / PIXEL_SIZE;
189#endif
190
191 fb->version = sizeof(*fb);
192 fb->width = vi.xres;
193 fb->height = vi.yres;
194#ifdef BOARD_HAS_JANKY_BACKBUFFER
Dees_Troy2673cec2013-04-02 20:22:16 +0000195 printf("setting JANKY BACKBUFFER\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400196 fb->stride = fi.line_length/2;
197#else
198 fb->stride = vi.xres_virtual;
199#endif
200 fb->data = bits;
201 fb->format = PIXEL_FORMAT;
202 memset(fb->data, 0, vi.yres * fb->stride * PIXEL_SIZE);
203
204 fb++;
205
Hiemanshu Sharma94f6c882012-11-21 11:25:22 -0600206 /* check if we can use double buffering */
207 if (vi.yres * fi.line_length * 2 > fi.smem_len)
208 return fd;
209
210 double_buffering = 1;
211
Dees_Troy51a0e822012-09-05 15:24:24 -0400212 fb->version = sizeof(*fb);
213 fb->width = vi.xres;
214 fb->height = vi.yres;
215#ifdef BOARD_HAS_JANKY_BACKBUFFER
216 fb->stride = fi.line_length/2;
217 fb->data = (void*) (((unsigned) bits) + vi.yres * fi.line_length);
218#else
219 fb->stride = vi.xres_virtual;
220 fb->data = (void*) (((unsigned) bits) + vi.yres * fb->stride * PIXEL_SIZE);
221#endif
222 fb->format = PIXEL_FORMAT;
223 memset(fb->data, 0, vi.yres * fb->stride * PIXEL_SIZE);
224
225#ifdef PRINT_SCREENINFO
226 print_fb_var_screeninfo();
227#endif
228
229 return fd;
230}
231
232static void get_memory_surface(GGLSurface* ms) {
233 ms->version = sizeof(*ms);
234 ms->width = vi.xres;
235 ms->height = vi.yres;
236 ms->stride = vi.xres_virtual;
237 ms->data = malloc(vi.xres_virtual * vi.yres * PIXEL_SIZE);
238 ms->format = PIXEL_FORMAT;
239}
240
241static void set_active_framebuffer(unsigned n)
242{
Hiemanshu Sharma94f6c882012-11-21 11:25:22 -0600243 if (n > 1 || !double_buffering) return;
Hiemanshu Sharmaacf6a9b2012-11-21 11:28:36 -0600244 vi.yres_virtual = vi.yres * NUM_BUFFERS;
Dees_Troy51a0e822012-09-05 15:24:24 -0400245 vi.yoffset = n * vi.yres;
246// vi.bits_per_pixel = PIXEL_SIZE * 8;
247 if (ioctl(gr_fb_fd, FBIOPUT_VSCREENINFO, &vi) < 0) {
248 perror("active fb swap failed");
249 }
250}
251
252void gr_flip(void)
253{
254 GGLContext *gl = gr_context;
255
256 /* swap front and back buffers */
Hiemanshu Sharma94f6c882012-11-21 11:25:22 -0600257 if (double_buffering)
258 gr_active_fb = (gr_active_fb + 1) & 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400259
260#ifdef BOARD_HAS_FLIPPED_SCREEN
261 /* flip buffer 180 degrees for devices with physicaly inverted screens */
262 unsigned int i;
Hashcode232b9fd2013-07-03 11:19:17 -0700263 unsigned int j;
264 uint8_t tmp;
265 for (i = 0; i < ((vi.xres_virtual * vi.yres)/2); i++) {
266 for (j = 0; j < PIXEL_SIZE; j++) {
267 tmp = gr_mem_surface.data[i * PIXEL_SIZE + j];
268 gr_mem_surface.data[i * PIXEL_SIZE + j] = gr_mem_surface.data[(vi.xres_virtual * vi.yres * PIXEL_SIZE) - ((i+1) * PIXEL_SIZE) + j];
269 gr_mem_surface.data[(vi.xres_virtual * vi.yres * PIXEL_SIZE) - ((i+1) * PIXEL_SIZE) + j] = tmp;
270 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400271 }
272#endif
273
274 /* copy data from the in-memory surface to the buffer we're about
275 * to make active. */
276 memcpy(gr_framebuffer[gr_active_fb].data, gr_mem_surface.data,
277 vi.xres_virtual * vi.yres * PIXEL_SIZE);
278
279 /* inform the display driver */
280 set_active_framebuffer(gr_active_fb);
281}
282
283void gr_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
284{
285 GGLContext *gl = gr_context;
286 GGLint color[4];
287 color[0] = ((r << 8) | r) + 1;
288 color[1] = ((g << 8) | g) + 1;
289 color[2] = ((b << 8) | b) + 1;
290 color[3] = ((a << 8) | a) + 1;
291 gl->color4xv(gl, color);
Vojtech Bocek1a4744a2014-02-06 19:52:28 +0100292
293 gr_is_curr_clr_opaque = (a == 255);
Dees_Troy51a0e822012-09-05 15:24:24 -0400294}
295
296int gr_measureEx(const char *s, void* font)
297{
298 GRFont* fnt = (GRFont*) font;
299 int total = 0;
300 unsigned pos;
301 unsigned off;
302
303 if (!fnt) fnt = gr_font;
304
305 while ((off = *s++))
306 {
307 off -= 32;
308 if (off < 96)
309 total += (fnt->offset[off+1] - fnt->offset[off]);
310 }
311 return total;
312}
313
314unsigned character_width(const char *s, void* pFont)
315{
316 GRFont *font = (GRFont*) pFont;
317 unsigned off;
318
319 /* Handle default font */
320 if (!font) font = gr_font;
321
322 off = *s - 32;
323 if (off == 0)
324 return 0;
325
326 return font->offset[off+1] - font->offset[off];
327}
328
329int gr_textEx(int x, int y, const char *s, void* pFont)
330{
331 GGLContext *gl = gr_context;
332 GRFont *font = (GRFont*) pFont;
333 unsigned off;
334 unsigned cwidth;
335
336 /* Handle default font */
337 if (!font) font = gr_font;
338
339 gl->bindTexture(gl, &font->texture);
340 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
341 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
342 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
343 gl->enable(gl, GGL_TEXTURE_2D);
344
345 while((off = *s++)) {
346 off -= 32;
347 cwidth = 0;
348 if (off < 96) {
349 cwidth = font->offset[off+1] - font->offset[off];
350 gl->texCoord2i(gl, (font->offset[off]) - x, 0 - y);
351 gl->recti(gl, x, y, x + cwidth, y + font->cheight);
352 x += cwidth;
353 }
354 }
355
356 return x;
357}
358
359int gr_textExW(int x, int y, const char *s, void* pFont, int max_width)
360{
361 GGLContext *gl = gr_context;
362 GRFont *font = (GRFont*) pFont;
363 unsigned off;
364 unsigned cwidth;
365
366 /* Handle default font */
367 if (!font) font = gr_font;
368
369 gl->bindTexture(gl, &font->texture);
370 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
371 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
372 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
373 gl->enable(gl, GGL_TEXTURE_2D);
374
375 while((off = *s++)) {
376 off -= 32;
377 cwidth = 0;
378 if (off < 96) {
379 cwidth = font->offset[off+1] - font->offset[off];
380 if ((x + (int)cwidth) < max_width) {
381 gl->texCoord2i(gl, (font->offset[off]) - x, 0 - y);
382 gl->recti(gl, x, y, x + cwidth, y + font->cheight);
383 x += cwidth;
384 } else {
385 gl->texCoord2i(gl, (font->offset[off]) - x, 0 - y);
386 gl->recti(gl, x, y, max_width, y + font->cheight);
387 x = max_width;
388 return x;
389 }
390 }
391 }
392
393 return x;
394}
395
396int gr_textExWH(int x, int y, const char *s, void* pFont, int max_width, int max_height)
397{
398 GGLContext *gl = gr_context;
399 GRFont *font = (GRFont*) pFont;
400 unsigned off;
401 unsigned cwidth;
402 int rect_x, rect_y;
403
404 /* Handle default font */
405 if (!font) font = gr_font;
406
407 gl->bindTexture(gl, &font->texture);
408 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
409 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
410 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
411 gl->enable(gl, GGL_TEXTURE_2D);
412
413 while((off = *s++)) {
414 off -= 32;
415 cwidth = 0;
416 if (off < 96) {
417 cwidth = font->offset[off+1] - font->offset[off];
418 if ((x + (int)cwidth) < max_width)
419 rect_x = x + cwidth;
420 else
421 rect_x = max_width;
Dees_Troyf7596752012-09-28 13:21:36 -0400422 if (y + font->cheight < (unsigned int)(max_height))
Dees_Troy51a0e822012-09-05 15:24:24 -0400423 rect_y = y + font->cheight;
424 else
425 rect_y = max_height;
426
427 gl->texCoord2i(gl, (font->offset[off]) - x, 0 - y);
428 gl->recti(gl, x, y, rect_x, rect_y);
429 x += cwidth;
430 if (x > max_width)
431 return x;
432 }
433 }
434
435 return x;
436}
437
438int twgr_text(int x, int y, const char *s)
439{
440 GGLContext *gl = gr_context;
441 GRFont *font = gr_font;
442 unsigned off;
Dees_Troyf7596752012-09-28 13:21:36 -0400443 unsigned cwidth = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400444
445 y -= font->ascent;
446
447 gl->bindTexture(gl, &font->texture);
448 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
449 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
450 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
451 gl->enable(gl, GGL_TEXTURE_2D);
452
453 while((off = *s++)) {
454 off -= 32;
455 if (off < 96) {
456 cwidth = font->offset[off+1] - font->offset[off];
457 gl->texCoord2i(gl, (off * cwidth) - x, 0 - y);
458 gl->recti(gl, x, y, x + cwidth, y + font->cheight);
459 }
460 x += cwidth;
461 }
462
463 return x;
464}
465
466void gr_fill(int x, int y, int w, int h)
467{
468 GGLContext *gl = gr_context;
Vojtech Bocek1a4744a2014-02-06 19:52:28 +0100469
470 if(gr_is_curr_clr_opaque)
471 gl->disable(gl, GGL_BLEND);
472
Dees_Troy51a0e822012-09-05 15:24:24 -0400473 gl->disable(gl, GGL_TEXTURE_2D);
474 gl->recti(gl, x, y, x + w, y + h);
Vojtech Bocek1a4744a2014-02-06 19:52:28 +0100475
476 if(gr_is_curr_clr_opaque)
477 gl->enable(gl, GGL_BLEND);
Dees_Troy51a0e822012-09-05 15:24:24 -0400478}
479
480void gr_blit(gr_surface source, int sx, int sy, int w, int h, int dx, int dy) {
481 if (gr_context == NULL) {
482 return;
483 }
484
485 GGLContext *gl = gr_context;
Vojtech Bocek6c694b62014-02-06 20:36:25 +0100486 GGLSurface *surface = (GGLSurface*)source;
487
488 if(surface->format == GGL_PIXEL_FORMAT_RGBX_8888)
489 gl->disable(gl, GGL_BLEND);
490
491 gl->bindTexture(gl, surface);
Dees_Troy51a0e822012-09-05 15:24:24 -0400492 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
493 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
494 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
495 gl->enable(gl, GGL_TEXTURE_2D);
496 gl->texCoord2i(gl, sx - dx, sy - dy);
497 gl->recti(gl, dx, dy, dx + w, dy + h);
Vojtech Bocek6c694b62014-02-06 20:36:25 +0100498
499 if(surface->format == GGL_PIXEL_FORMAT_RGBX_8888)
500 gl->enable(gl, GGL_BLEND);
Dees_Troy51a0e822012-09-05 15:24:24 -0400501}
502
503unsigned int gr_get_width(gr_surface surface) {
504 if (surface == NULL) {
505 return 0;
506 }
507 return ((GGLSurface*) surface)->width;
508}
509
510unsigned int gr_get_height(gr_surface surface) {
511 if (surface == NULL) {
512 return 0;
513 }
514 return ((GGLSurface*) surface)->height;
515}
516
517void* gr_loadFont(const char* fontName)
518{
519 int fd;
520 GRFont *font = 0;
521 GGLSurface *ftex;
522 unsigned char *bits, *rle;
523 unsigned char *in, data;
524 unsigned width, height;
525 unsigned element;
526
527 fd = open(fontName, O_RDONLY);
528 if (fd == -1)
529 {
530 char tmp[128];
531
532 sprintf(tmp, "/res/fonts/%s.dat", fontName);
533 fd = open(tmp, O_RDONLY);
534 if (fd == -1)
535 return NULL;
536 }
537
538 font = calloc(sizeof(*font), 1);
539 ftex = &font->texture;
540
541 read(fd, &width, sizeof(unsigned));
542 read(fd, &height, sizeof(unsigned));
543 read(fd, font->offset, sizeof(unsigned) * 96);
544 font->offset[96] = width;
545
546 bits = malloc(width * height);
547 memset(bits, 0, width * height);
548
549 unsigned pos = 0;
550 while (pos < width * height)
551 {
552 int bit;
553
554 read(fd, &data, 1);
555 for (bit = 0; bit < 8; bit++)
556 {
557 if (data & (1 << (7-bit))) bits[pos++] = 255;
558 else bits[pos++] = 0;
559
560 if (pos == width * height) break;
561 }
562 }
563 close(fd);
564
565 ftex->version = sizeof(*ftex);
566 ftex->width = width;
567 ftex->height = height;
568 ftex->stride = width;
569 ftex->data = (void*) bits;
570 ftex->format = GGL_PIXEL_FORMAT_A_8;
571 font->cheight = height;
572 font->ascent = height - 2;
573 return (void*) font;
574}
575
576int gr_getFontDetails(void* font, unsigned* cheight, unsigned* maxwidth)
577{
578 GRFont *fnt = (GRFont*) font;
579
580 if (!fnt) fnt = gr_font;
581 if (!fnt) return -1;
582
583 if (cheight) *cheight = fnt->cheight;
584 if (maxwidth)
585 {
586 int pos;
587 *maxwidth = 0;
588 for (pos = 0; pos < 96; pos++)
589 {
590 unsigned int width = fnt->offset[pos+1] - fnt->offset[pos];
591 if (width > *maxwidth)
592 {
593 *maxwidth = width;
594 }
595 }
596 }
597 return 0;
598}
599
600static void gr_init_font(void)
601{
602 int fontRes;
603 GGLSurface *ftex;
604 unsigned char *bits, *rle;
605 unsigned char *in, data;
606 unsigned width, height;
607 unsigned element;
608
609 gr_font = calloc(sizeof(*gr_font), 1);
610 ftex = &gr_font->texture;
611
612 width = font.width;
613 height = font.height;
614
615 bits = malloc(width * height);
616 rle = bits;
617
618 in = font.rundata;
619 while((data = *in++))
620 {
621 memset(rle, (data & 0x80) ? 255 : 0, data & 0x7f);
622 rle += (data & 0x7f);
623 }
624 for (element = 0; element < 97; element++)
625 {
626 gr_font->offset[element] = (element * font.cwidth);
627 }
628
629 ftex->version = sizeof(*ftex);
630 ftex->width = width;
631 ftex->height = height;
632 ftex->stride = width;
633 ftex->data = (void*) bits;
634 ftex->format = GGL_PIXEL_FORMAT_A_8;
635 gr_font->cheight = height;
636 gr_font->ascent = height - 2;
637 return;
638}
639
640int gr_init(void)
641{
642 gglInit(&gr_context);
643 GGLContext *gl = gr_context;
644
645 gr_init_font();
646 gr_vt_fd = open("/dev/tty0", O_RDWR | O_SYNC);
647 if (gr_vt_fd < 0) {
648 // This is non-fatal; post-Cupcake kernels don't have tty0.
649 } else if (ioctl(gr_vt_fd, KDSETMODE, (void*) KD_GRAPHICS)) {
650 // However, if we do open tty0, we expect the ioctl to work.
651 perror("failed KDSETMODE to KD_GRAPHICS on tty0");
652 gr_exit();
653 return -1;
654 }
655
656 gr_fb_fd = get_framebuffer(gr_framebuffer);
657 if (gr_fb_fd < 0) {
658 perror("Unable to get framebuffer.\n");
659 gr_exit();
660 return -1;
661 }
662
663 get_memory_surface(&gr_mem_surface);
664
665 fprintf(stderr, "framebuffer: fd %d (%d x %d)\n",
666 gr_fb_fd, gr_framebuffer[0].width, gr_framebuffer[0].height);
667
668 /* start with 0 as front (displayed) and 1 as back (drawing) */
669 gr_active_fb = 0;
670 set_active_framebuffer(0);
671 gl->colorBuffer(gl, &gr_mem_surface);
672
673 gl->activeTexture(gl, 0);
674 gl->enable(gl, GGL_BLEND);
675 gl->blendFunc(gl, GGL_SRC_ALPHA, GGL_ONE_MINUS_SRC_ALPHA);
676
677// gr_fb_blank(true);
678// gr_fb_blank(false);
679
680 return 0;
681}
682
683void gr_exit(void)
684{
685 close(gr_fb_fd);
686 gr_fb_fd = -1;
687
688 free(gr_mem_surface.data);
689
690 ioctl(gr_vt_fd, KDSETMODE, (void*) KD_TEXT);
691 close(gr_vt_fd);
692 gr_vt_fd = -1;
693}
694
695int gr_fb_width(void)
696{
697 return gr_framebuffer[0].width;
698}
699
700int gr_fb_height(void)
701{
702 return gr_framebuffer[0].height;
703}
704
705gr_pixel *gr_fb_data(void)
706{
707 return (unsigned short *) gr_mem_surface.data;
708}
709
Dees_Troy70237dc2013-02-28 21:31:48 +0000710int gr_fb_blank(int blank)
Dees_Troy51a0e822012-09-05 15:24:24 -0400711{
712 int ret;
713
714 ret = ioctl(gr_fb_fd, FBIOBLANK, blank ? FB_BLANK_POWERDOWN : FB_BLANK_UNBLANK);
715 if (ret < 0)
716 perror("ioctl(): blank");
Dees_Troy70237dc2013-02-28 21:31:48 +0000717 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -0400718}
719
720int gr_get_surface(gr_surface* surface)
721{
722 GGLSurface* ms = malloc(sizeof(GGLSurface));
723 if (!ms) return -1;
724
725 // Allocate the data
726 get_memory_surface(ms);
727
728 // Now, copy the data
729 memcpy(ms->data, gr_mem_surface.data, vi.xres * vi.yres * vi.bits_per_pixel / 8);
730
731 *surface = (gr_surface*) ms;
732 return 0;
733}
734
735int gr_free_surface(gr_surface surface)
736{
737 if (!surface)
738 return -1;
739
740 GGLSurface* ms = (GGLSurface*) surface;
741 free(ms->data);
742 free(ms);
743 return 0;
744}
745
746void gr_write_frame_to_file(int fd)
747{
748 write(fd, gr_mem_surface.data, vi.xres * vi.yres * vi.bits_per_pixel / 8);
749}