blob: 99f5841833d23009e71bb7663d8605379a60023d [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;
486 gl->bindTexture(gl, (GGLSurface*) source);
487 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
488 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
489 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
490 gl->enable(gl, GGL_TEXTURE_2D);
491 gl->texCoord2i(gl, sx - dx, sy - dy);
492 gl->recti(gl, dx, dy, dx + w, dy + h);
493}
494
495unsigned int gr_get_width(gr_surface surface) {
496 if (surface == NULL) {
497 return 0;
498 }
499 return ((GGLSurface*) surface)->width;
500}
501
502unsigned int gr_get_height(gr_surface surface) {
503 if (surface == NULL) {
504 return 0;
505 }
506 return ((GGLSurface*) surface)->height;
507}
508
509void* gr_loadFont(const char* fontName)
510{
511 int fd;
512 GRFont *font = 0;
513 GGLSurface *ftex;
514 unsigned char *bits, *rle;
515 unsigned char *in, data;
516 unsigned width, height;
517 unsigned element;
518
519 fd = open(fontName, O_RDONLY);
520 if (fd == -1)
521 {
522 char tmp[128];
523
524 sprintf(tmp, "/res/fonts/%s.dat", fontName);
525 fd = open(tmp, O_RDONLY);
526 if (fd == -1)
527 return NULL;
528 }
529
530 font = calloc(sizeof(*font), 1);
531 ftex = &font->texture;
532
533 read(fd, &width, sizeof(unsigned));
534 read(fd, &height, sizeof(unsigned));
535 read(fd, font->offset, sizeof(unsigned) * 96);
536 font->offset[96] = width;
537
538 bits = malloc(width * height);
539 memset(bits, 0, width * height);
540
541 unsigned pos = 0;
542 while (pos < width * height)
543 {
544 int bit;
545
546 read(fd, &data, 1);
547 for (bit = 0; bit < 8; bit++)
548 {
549 if (data & (1 << (7-bit))) bits[pos++] = 255;
550 else bits[pos++] = 0;
551
552 if (pos == width * height) break;
553 }
554 }
555 close(fd);
556
557 ftex->version = sizeof(*ftex);
558 ftex->width = width;
559 ftex->height = height;
560 ftex->stride = width;
561 ftex->data = (void*) bits;
562 ftex->format = GGL_PIXEL_FORMAT_A_8;
563 font->cheight = height;
564 font->ascent = height - 2;
565 return (void*) font;
566}
567
568int gr_getFontDetails(void* font, unsigned* cheight, unsigned* maxwidth)
569{
570 GRFont *fnt = (GRFont*) font;
571
572 if (!fnt) fnt = gr_font;
573 if (!fnt) return -1;
574
575 if (cheight) *cheight = fnt->cheight;
576 if (maxwidth)
577 {
578 int pos;
579 *maxwidth = 0;
580 for (pos = 0; pos < 96; pos++)
581 {
582 unsigned int width = fnt->offset[pos+1] - fnt->offset[pos];
583 if (width > *maxwidth)
584 {
585 *maxwidth = width;
586 }
587 }
588 }
589 return 0;
590}
591
592static void gr_init_font(void)
593{
594 int fontRes;
595 GGLSurface *ftex;
596 unsigned char *bits, *rle;
597 unsigned char *in, data;
598 unsigned width, height;
599 unsigned element;
600
601 gr_font = calloc(sizeof(*gr_font), 1);
602 ftex = &gr_font->texture;
603
604 width = font.width;
605 height = font.height;
606
607 bits = malloc(width * height);
608 rle = bits;
609
610 in = font.rundata;
611 while((data = *in++))
612 {
613 memset(rle, (data & 0x80) ? 255 : 0, data & 0x7f);
614 rle += (data & 0x7f);
615 }
616 for (element = 0; element < 97; element++)
617 {
618 gr_font->offset[element] = (element * font.cwidth);
619 }
620
621 ftex->version = sizeof(*ftex);
622 ftex->width = width;
623 ftex->height = height;
624 ftex->stride = width;
625 ftex->data = (void*) bits;
626 ftex->format = GGL_PIXEL_FORMAT_A_8;
627 gr_font->cheight = height;
628 gr_font->ascent = height - 2;
629 return;
630}
631
632int gr_init(void)
633{
634 gglInit(&gr_context);
635 GGLContext *gl = gr_context;
636
637 gr_init_font();
638 gr_vt_fd = open("/dev/tty0", O_RDWR | O_SYNC);
639 if (gr_vt_fd < 0) {
640 // This is non-fatal; post-Cupcake kernels don't have tty0.
641 } else if (ioctl(gr_vt_fd, KDSETMODE, (void*) KD_GRAPHICS)) {
642 // However, if we do open tty0, we expect the ioctl to work.
643 perror("failed KDSETMODE to KD_GRAPHICS on tty0");
644 gr_exit();
645 return -1;
646 }
647
648 gr_fb_fd = get_framebuffer(gr_framebuffer);
649 if (gr_fb_fd < 0) {
650 perror("Unable to get framebuffer.\n");
651 gr_exit();
652 return -1;
653 }
654
655 get_memory_surface(&gr_mem_surface);
656
657 fprintf(stderr, "framebuffer: fd %d (%d x %d)\n",
658 gr_fb_fd, gr_framebuffer[0].width, gr_framebuffer[0].height);
659
660 /* start with 0 as front (displayed) and 1 as back (drawing) */
661 gr_active_fb = 0;
662 set_active_framebuffer(0);
663 gl->colorBuffer(gl, &gr_mem_surface);
664
665 gl->activeTexture(gl, 0);
666 gl->enable(gl, GGL_BLEND);
667 gl->blendFunc(gl, GGL_SRC_ALPHA, GGL_ONE_MINUS_SRC_ALPHA);
668
669// gr_fb_blank(true);
670// gr_fb_blank(false);
671
672 return 0;
673}
674
675void gr_exit(void)
676{
677 close(gr_fb_fd);
678 gr_fb_fd = -1;
679
680 free(gr_mem_surface.data);
681
682 ioctl(gr_vt_fd, KDSETMODE, (void*) KD_TEXT);
683 close(gr_vt_fd);
684 gr_vt_fd = -1;
685}
686
687int gr_fb_width(void)
688{
689 return gr_framebuffer[0].width;
690}
691
692int gr_fb_height(void)
693{
694 return gr_framebuffer[0].height;
695}
696
697gr_pixel *gr_fb_data(void)
698{
699 return (unsigned short *) gr_mem_surface.data;
700}
701
Dees_Troy70237dc2013-02-28 21:31:48 +0000702int gr_fb_blank(int blank)
Dees_Troy51a0e822012-09-05 15:24:24 -0400703{
704 int ret;
705
706 ret = ioctl(gr_fb_fd, FBIOBLANK, blank ? FB_BLANK_POWERDOWN : FB_BLANK_UNBLANK);
707 if (ret < 0)
708 perror("ioctl(): blank");
Dees_Troy70237dc2013-02-28 21:31:48 +0000709 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -0400710}
711
712int gr_get_surface(gr_surface* surface)
713{
714 GGLSurface* ms = malloc(sizeof(GGLSurface));
715 if (!ms) return -1;
716
717 // Allocate the data
718 get_memory_surface(ms);
719
720 // Now, copy the data
721 memcpy(ms->data, gr_mem_surface.data, vi.xres * vi.yres * vi.bits_per_pixel / 8);
722
723 *surface = (gr_surface*) ms;
724 return 0;
725}
726
727int gr_free_surface(gr_surface surface)
728{
729 if (!surface)
730 return -1;
731
732 GGLSurface* ms = (GGLSurface*) surface;
733 free(ms->data);
734 free(ms);
735 return 0;
736}
737
738void gr_write_frame_to_file(int fd)
739{
740 write(fd, gr_mem_surface.data, vi.xres * vi.yres * vi.bits_per_pixel / 8);
741}