blob: bd611b3804192a684b5d1ff1961ced48ec432a8e [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
Ethan Yonker4ee5ad72014-02-18 18:41:17 -060021#include <errno.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040022#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
32#include <pixelflinger/pixelflinger.h>
33
34#include "minui.h"
35
36#ifdef BOARD_USE_CUSTOM_RECOVERY_FONT
37#include BOARD_USE_CUSTOM_RECOVERY_FONT
38#else
39#include "font_10x18.h"
40#endif
41
42#ifdef RECOVERY_BGRA
43#define PIXEL_FORMAT GGL_PIXEL_FORMAT_BGRA_8888
44#define PIXEL_SIZE 4
45#endif
46#ifdef RECOVERY_RGBX
47#define PIXEL_FORMAT GGL_PIXEL_FORMAT_RGBX_8888
48#define PIXEL_SIZE 4
49#endif
50#ifndef PIXEL_FORMAT
51#define PIXEL_FORMAT GGL_PIXEL_FORMAT_RGB_565
52#define PIXEL_SIZE 2
53#endif
54
Hiemanshu Sharmaacf6a9b2012-11-21 11:28:36 -060055#define NUM_BUFFERS 2
Ethan Yonker4ee5ad72014-02-18 18:41:17 -060056#define MAX_DISPLAY_DIM 2048
Hiemanshu Sharmaacf6a9b2012-11-21 11:28:36 -060057
Dees_Troy51a0e822012-09-05 15:24:24 -040058// #define PRINT_SCREENINFO 1 // Enables printing of screen info to log
59
60typedef struct {
61 GGLSurface texture;
62 unsigned offset[97];
63 unsigned cheight;
64 unsigned ascent;
65} GRFont;
66
67static GRFont *gr_font = 0;
68static GGLContext *gr_context = 0;
69static GGLSurface gr_font_texture;
Hiemanshu Sharmaacf6a9b2012-11-21 11:28:36 -060070static GGLSurface gr_framebuffer[NUM_BUFFERS];
Dees_Troy51a0e822012-09-05 15:24:24 -040071static GGLSurface gr_mem_surface;
72static unsigned gr_active_fb = 0;
Hiemanshu Sharma94f6c882012-11-21 11:25:22 -060073static unsigned double_buffering = 0;
Vojtech Bocek1a4744a2014-02-06 19:52:28 +010074static int gr_is_curr_clr_opaque = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -040075
76static int gr_fb_fd = -1;
77static int gr_vt_fd = -1;
78
79static struct fb_var_screeninfo vi;
80static struct fb_fix_screeninfo fi;
81
Ethan Yonker4ee5ad72014-02-18 18:41:17 -060082static bool has_overlay = false;
83static int leftSplit = 0;
84static int rightSplit = 0;
85
86bool target_has_overlay(char *version);
87int free_ion_mem(void);
88int alloc_ion_mem(unsigned int size);
89int allocate_overlay(int fd, GGLSurface gr_fb[]);
90int free_overlay(int fd);
91int overlay_display_frame(int fd, GGLubyte* data, size_t size);
92
Dees_Troy51a0e822012-09-05 15:24:24 -040093#ifdef PRINT_SCREENINFO
94static void print_fb_var_screeninfo()
95{
Dees_Troy2673cec2013-04-02 20:22:16 +000096 printf("vi.xres: %d\n", vi.xres);
97 printf("vi.yres: %d\n", vi.yres);
98 printf("vi.xres_virtual: %d\n", vi.xres_virtual);
99 printf("vi.yres_virtual: %d\n", vi.yres_virtual);
100 printf("vi.xoffset: %d\n", vi.xoffset);
101 printf("vi.yoffset: %d\n", vi.yoffset);
102 printf("vi.bits_per_pixel: %d\n", vi.bits_per_pixel);
103 printf("vi.grayscale: %d\n", vi.grayscale);
Dees_Troy51a0e822012-09-05 15:24:24 -0400104}
105#endif
106
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600107#ifdef MSM_BSP
108int getLeftSplit(void) {
109 //Default even split for all displays with high res
110 int lSplit = vi.xres / 2;
111
112 //Override if split published by driver
113 if (leftSplit)
114 lSplit = leftSplit;
115
116 return lSplit;
117}
118
119int getRightSplit(void) {
120 return rightSplit;
121}
122
123
124void setDisplaySplit(void) {
125 char split[64] = {0};
126 FILE* fp = fopen("/sys/class/graphics/fb0/msm_fb_split", "r");
127 if (fp) {
128 //Format "left right" space as delimiter
129 if(fread(split, sizeof(char), 64, fp)) {
130 leftSplit = atoi(split);
131 printf("Left Split=%d\n",leftSplit);
132 char *rght = strpbrk(split, " ");
133 if (rght)
134 rightSplit = atoi(rght + 1);
135 printf("Right Split=%d\n", rightSplit);
136 }
137 } else {
138 printf("Failed to open mdss_fb_split node\n");
139 }
140 if (fp)
141 fclose(fp);
142}
143
144bool isDisplaySplit(void) {
145 if (vi.xres > MAX_DISPLAY_DIM)
146 return true;
147 //check if right split is set by driver
148 if (getRightSplit())
149 return true;
150
151 return false;
152}
153
154int getFbXres(void) {
155 return vi.xres;
156}
157
158int getFbYres (void) {
159 return vi.yres;
160}
161#endif // MSM_BSP
162
Dees_Troy51a0e822012-09-05 15:24:24 -0400163static int get_framebuffer(GGLSurface *fb)
164{
165 int fd;
166 void *bits;
167
168 fd = open("/dev/graphics/fb0", O_RDWR);
169 if (fd < 0) {
170 perror("cannot open fb0");
171 return -1;
172 }
173
174 if (ioctl(fd, FBIOGET_VSCREENINFO, &vi) < 0) {
175 perror("failed to get fb0 info");
176 close(fd);
177 return -1;
178 }
179
180 fprintf(stderr, "Pixel format: %dx%d @ %dbpp\n", vi.xres, vi.yres, vi.bits_per_pixel);
181
182 vi.bits_per_pixel = PIXEL_SIZE * 8;
183 if (PIXEL_FORMAT == GGL_PIXEL_FORMAT_BGRA_8888) {
184 fprintf(stderr, "Pixel format: BGRA_8888\n");
185 if (PIXEL_SIZE != 4) fprintf(stderr, "E: Pixel Size mismatch!\n");
186 vi.red.offset = 8;
187 vi.red.length = 8;
188 vi.green.offset = 16;
189 vi.green.length = 8;
190 vi.blue.offset = 24;
191 vi.blue.length = 8;
192 vi.transp.offset = 0;
193 vi.transp.length = 8;
194 } else if (PIXEL_FORMAT == GGL_PIXEL_FORMAT_RGBX_8888) {
195 fprintf(stderr, "Pixel format: RGBX_8888\n");
196 if (PIXEL_SIZE != 4) fprintf(stderr, "E: Pixel Size mismatch!\n");
197 vi.red.offset = 24;
198 vi.red.length = 8;
199 vi.green.offset = 16;
200 vi.green.length = 8;
201 vi.blue.offset = 8;
202 vi.blue.length = 8;
203 vi.transp.offset = 0;
204 vi.transp.length = 8;
205 } else if (PIXEL_FORMAT == GGL_PIXEL_FORMAT_RGB_565) {
206#ifdef RECOVERY_RGB_565
207 fprintf(stderr, "Pixel format: RGB_565\n");
208 vi.blue.offset = 0;
209 vi.green.offset = 5;
210 vi.red.offset = 11;
211#else
212 fprintf(stderr, "Pixel format: BGR_565\n");
213 vi.blue.offset = 11;
214 vi.green.offset = 5;
215 vi.red.offset = 0;
216#endif
217 if (PIXEL_SIZE != 2) fprintf(stderr, "E: Pixel Size mismatch!\n");
218 vi.blue.length = 5;
219 vi.green.length = 6;
220 vi.red.length = 5;
221 vi.blue.msb_right = 0;
222 vi.green.msb_right = 0;
223 vi.red.msb_right = 0;
224 vi.transp.offset = 0;
225 vi.transp.length = 0;
226 }
227 else
228 {
229 perror("unknown pixel format");
230 close(fd);
231 return -1;
232 }
233
234 vi.vmode = FB_VMODE_NONINTERLACED;
235 vi.activate = FB_ACTIVATE_NOW | FB_ACTIVATE_FORCE;
236
237 if (ioctl(fd, FBIOPUT_VSCREENINFO, &vi) < 0) {
238 perror("failed to put fb0 info");
239 close(fd);
240 return -1;
241 }
242
243 if (ioctl(fd, FBIOGET_FSCREENINFO, &fi) < 0) {
244 perror("failed to get fb0 info");
245 close(fd);
246 return -1;
247 }
248
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600249 has_overlay = target_has_overlay(fi.id);
250
251#ifdef MSM_BSP
252 if (isTargetMdp5())
253 setDisplaySplit();
254#endif
255
256 if (!has_overlay) {
257 printf("Not using qualcomm overlay, '%s'\n", fi.id);
258 bits = mmap(0, fi.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
259 if (bits == MAP_FAILED) {
260 perror("failed to mmap framebuffer");
261 close(fd);
262 return -1;
263 }
264 } else {
265 printf("Using qualcomm overlay\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400266 }
267
268#ifdef RECOVERY_GRAPHICS_USE_LINELENGTH
269 vi.xres_virtual = fi.line_length / PIXEL_SIZE;
270#endif
271
272 fb->version = sizeof(*fb);
273 fb->width = vi.xres;
274 fb->height = vi.yres;
275#ifdef BOARD_HAS_JANKY_BACKBUFFER
Dees_Troy2673cec2013-04-02 20:22:16 +0000276 printf("setting JANKY BACKBUFFER\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400277 fb->stride = fi.line_length/2;
278#else
279 fb->stride = vi.xres_virtual;
280#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400281 fb->format = PIXEL_FORMAT;
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600282 if (!has_overlay) {
283 fb->data = bits;
284 memset(fb->data, 0, vi.yres * fb->stride * PIXEL_SIZE);
285 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400286
287 fb++;
288
Hiemanshu Sharma94f6c882012-11-21 11:25:22 -0600289 /* check if we can use double buffering */
290 if (vi.yres * fi.line_length * 2 > fi.smem_len)
291 return fd;
292
293 double_buffering = 1;
294
Dees_Troy51a0e822012-09-05 15:24:24 -0400295 fb->version = sizeof(*fb);
296 fb->width = vi.xres;
297 fb->height = vi.yres;
298#ifdef BOARD_HAS_JANKY_BACKBUFFER
299 fb->stride = fi.line_length/2;
300 fb->data = (void*) (((unsigned) bits) + vi.yres * fi.line_length);
301#else
302 fb->stride = vi.xres_virtual;
303 fb->data = (void*) (((unsigned) bits) + vi.yres * fb->stride * PIXEL_SIZE);
304#endif
305 fb->format = PIXEL_FORMAT;
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600306 if (!has_overlay) {
307 memset(fb->data, 0, vi.yres * fb->stride * PIXEL_SIZE);
308 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400309
310#ifdef PRINT_SCREENINFO
311 print_fb_var_screeninfo();
312#endif
313
314 return fd;
315}
316
317static void get_memory_surface(GGLSurface* ms) {
318 ms->version = sizeof(*ms);
319 ms->width = vi.xres;
320 ms->height = vi.yres;
321 ms->stride = vi.xres_virtual;
322 ms->data = malloc(vi.xres_virtual * vi.yres * PIXEL_SIZE);
323 ms->format = PIXEL_FORMAT;
324}
325
326static void set_active_framebuffer(unsigned n)
327{
Hiemanshu Sharma94f6c882012-11-21 11:25:22 -0600328 if (n > 1 || !double_buffering) return;
Hiemanshu Sharmaacf6a9b2012-11-21 11:28:36 -0600329 vi.yres_virtual = vi.yres * NUM_BUFFERS;
Dees_Troy51a0e822012-09-05 15:24:24 -0400330 vi.yoffset = n * vi.yres;
331// vi.bits_per_pixel = PIXEL_SIZE * 8;
332 if (ioctl(gr_fb_fd, FBIOPUT_VSCREENINFO, &vi) < 0) {
333 perror("active fb swap failed");
334 }
335}
336
337void gr_flip(void)
338{
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600339 if (-EINVAL == overlay_display_frame(gr_fb_fd, gr_mem_surface.data,
340 (fi.line_length * vi.yres))) {
341 GGLContext *gl = gr_context;
Dees_Troy51a0e822012-09-05 15:24:24 -0400342
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600343 /* swap front and back buffers */
344 if (double_buffering)
345 gr_active_fb = (gr_active_fb + 1) & 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400346
347#ifdef BOARD_HAS_FLIPPED_SCREEN
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600348 /* flip buffer 180 degrees for devices with physicaly inverted screens */
349 unsigned int i;
350 unsigned int j;
351 uint8_t tmp;
352 for (i = 0; i < ((vi.xres_virtual * vi.yres)/2); i++) {
353 for (j = 0; j < PIXEL_SIZE; j++) {
354 tmp = gr_mem_surface.data[i * PIXEL_SIZE + j];
355 gr_mem_surface.data[i * PIXEL_SIZE + j] = gr_mem_surface.data[(vi.xres_virtual * vi.yres * PIXEL_SIZE) - ((i+1) * PIXEL_SIZE) + j];
356 gr_mem_surface.data[(vi.xres_virtual * vi.yres * PIXEL_SIZE) - ((i+1) * PIXEL_SIZE) + j] = tmp;
357 }
358 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400359#endif
360
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600361 /* copy data from the in-memory surface to the buffer we're about
362 * to make active. */
363 memcpy(gr_framebuffer[gr_active_fb].data, gr_mem_surface.data,
364 vi.xres_virtual * vi.yres * PIXEL_SIZE);
Dees_Troy51a0e822012-09-05 15:24:24 -0400365
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600366 /* inform the display driver */
367 set_active_framebuffer(gr_active_fb);
368 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400369}
370
371void gr_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
372{
373 GGLContext *gl = gr_context;
374 GGLint color[4];
375 color[0] = ((r << 8) | r) + 1;
376 color[1] = ((g << 8) | g) + 1;
377 color[2] = ((b << 8) | b) + 1;
378 color[3] = ((a << 8) | a) + 1;
379 gl->color4xv(gl, color);
Vojtech Bocek1a4744a2014-02-06 19:52:28 +0100380
381 gr_is_curr_clr_opaque = (a == 255);
Dees_Troy51a0e822012-09-05 15:24:24 -0400382}
383
384int gr_measureEx(const char *s, void* font)
385{
386 GRFont* fnt = (GRFont*) font;
387 int total = 0;
388 unsigned pos;
389 unsigned off;
390
391 if (!fnt) fnt = gr_font;
392
393 while ((off = *s++))
394 {
395 off -= 32;
396 if (off < 96)
397 total += (fnt->offset[off+1] - fnt->offset[off]);
398 }
399 return total;
400}
401
Dees Troy31218ec2014-02-25 20:35:56 +0000402int gr_maxExW(const char *s, void* font, int max_width)
403{
404 GRFont* fnt = (GRFont*) font;
405 int total = 0;
406 unsigned pos;
407 unsigned off;
408
409 if (!fnt) fnt = gr_font;
410
411 while ((off = *s++))
412 {
413 off -= 32;
414 if (off < 96) {
415 max_width -= (fnt->offset[off+1] - fnt->offset[off]);
416 if (max_width > 0) {
417 total++;
418 } else {
419 return total;
420 }
421 }
422 }
423 return total;
424}
425
Dees_Troy51a0e822012-09-05 15:24:24 -0400426unsigned character_width(const char *s, void* pFont)
427{
428 GRFont *font = (GRFont*) pFont;
429 unsigned off;
430
431 /* Handle default font */
432 if (!font) font = gr_font;
433
434 off = *s - 32;
435 if (off == 0)
436 return 0;
437
438 return font->offset[off+1] - font->offset[off];
439}
440
441int gr_textEx(int x, int y, const char *s, void* pFont)
442{
443 GGLContext *gl = gr_context;
444 GRFont *font = (GRFont*) pFont;
445 unsigned off;
446 unsigned cwidth;
447
448 /* Handle default font */
449 if (!font) font = gr_font;
450
451 gl->bindTexture(gl, &font->texture);
452 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
453 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
454 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
455 gl->enable(gl, GGL_TEXTURE_2D);
456
457 while((off = *s++)) {
458 off -= 32;
459 cwidth = 0;
460 if (off < 96) {
461 cwidth = font->offset[off+1] - font->offset[off];
462 gl->texCoord2i(gl, (font->offset[off]) - x, 0 - y);
463 gl->recti(gl, x, y, x + cwidth, y + font->cheight);
464 x += cwidth;
465 }
466 }
467
468 return x;
469}
470
471int gr_textExW(int x, int y, const char *s, void* pFont, int max_width)
472{
473 GGLContext *gl = gr_context;
474 GRFont *font = (GRFont*) pFont;
475 unsigned off;
476 unsigned cwidth;
477
478 /* Handle default font */
479 if (!font) font = gr_font;
480
481 gl->bindTexture(gl, &font->texture);
482 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
483 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
484 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
485 gl->enable(gl, GGL_TEXTURE_2D);
486
487 while((off = *s++)) {
488 off -= 32;
489 cwidth = 0;
490 if (off < 96) {
491 cwidth = font->offset[off+1] - font->offset[off];
492 if ((x + (int)cwidth) < max_width) {
493 gl->texCoord2i(gl, (font->offset[off]) - x, 0 - y);
494 gl->recti(gl, x, y, x + cwidth, y + font->cheight);
495 x += cwidth;
496 } else {
497 gl->texCoord2i(gl, (font->offset[off]) - x, 0 - y);
498 gl->recti(gl, x, y, max_width, y + font->cheight);
499 x = max_width;
500 return x;
501 }
502 }
503 }
504
505 return x;
506}
507
508int gr_textExWH(int x, int y, const char *s, void* pFont, int max_width, int max_height)
509{
510 GGLContext *gl = gr_context;
511 GRFont *font = (GRFont*) pFont;
512 unsigned off;
513 unsigned cwidth;
514 int rect_x, rect_y;
515
516 /* Handle default font */
517 if (!font) font = gr_font;
518
519 gl->bindTexture(gl, &font->texture);
520 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
521 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
522 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
523 gl->enable(gl, GGL_TEXTURE_2D);
524
525 while((off = *s++)) {
526 off -= 32;
527 cwidth = 0;
528 if (off < 96) {
529 cwidth = font->offset[off+1] - font->offset[off];
530 if ((x + (int)cwidth) < max_width)
531 rect_x = x + cwidth;
532 else
533 rect_x = max_width;
Dees_Troyf7596752012-09-28 13:21:36 -0400534 if (y + font->cheight < (unsigned int)(max_height))
Dees_Troy51a0e822012-09-05 15:24:24 -0400535 rect_y = y + font->cheight;
536 else
537 rect_y = max_height;
538
539 gl->texCoord2i(gl, (font->offset[off]) - x, 0 - y);
540 gl->recti(gl, x, y, rect_x, rect_y);
541 x += cwidth;
542 if (x > max_width)
543 return x;
544 }
545 }
546
547 return x;
548}
549
550int twgr_text(int x, int y, const char *s)
551{
552 GGLContext *gl = gr_context;
553 GRFont *font = gr_font;
554 unsigned off;
Dees_Troyf7596752012-09-28 13:21:36 -0400555 unsigned cwidth = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400556
557 y -= font->ascent;
558
559 gl->bindTexture(gl, &font->texture);
560 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
561 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
562 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
563 gl->enable(gl, GGL_TEXTURE_2D);
564
565 while((off = *s++)) {
566 off -= 32;
567 if (off < 96) {
568 cwidth = font->offset[off+1] - font->offset[off];
569 gl->texCoord2i(gl, (off * cwidth) - x, 0 - y);
570 gl->recti(gl, x, y, x + cwidth, y + font->cheight);
571 }
572 x += cwidth;
573 }
574
575 return x;
576}
577
578void gr_fill(int x, int y, int w, int h)
579{
580 GGLContext *gl = gr_context;
Vojtech Bocek1a4744a2014-02-06 19:52:28 +0100581
582 if(gr_is_curr_clr_opaque)
583 gl->disable(gl, GGL_BLEND);
584
Dees_Troy51a0e822012-09-05 15:24:24 -0400585 gl->disable(gl, GGL_TEXTURE_2D);
586 gl->recti(gl, x, y, x + w, y + h);
Vojtech Bocek1a4744a2014-02-06 19:52:28 +0100587
588 if(gr_is_curr_clr_opaque)
589 gl->enable(gl, GGL_BLEND);
Dees_Troy51a0e822012-09-05 15:24:24 -0400590}
591
592void gr_blit(gr_surface source, int sx, int sy, int w, int h, int dx, int dy) {
593 if (gr_context == NULL) {
594 return;
595 }
596
597 GGLContext *gl = gr_context;
Vojtech Bocek6c694b62014-02-06 20:36:25 +0100598 GGLSurface *surface = (GGLSurface*)source;
599
600 if(surface->format == GGL_PIXEL_FORMAT_RGBX_8888)
601 gl->disable(gl, GGL_BLEND);
602
603 gl->bindTexture(gl, surface);
Dees_Troy51a0e822012-09-05 15:24:24 -0400604 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
605 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
606 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
607 gl->enable(gl, GGL_TEXTURE_2D);
608 gl->texCoord2i(gl, sx - dx, sy - dy);
609 gl->recti(gl, dx, dy, dx + w, dy + h);
Vojtech Bocek6c694b62014-02-06 20:36:25 +0100610
611 if(surface->format == GGL_PIXEL_FORMAT_RGBX_8888)
612 gl->enable(gl, GGL_BLEND);
Dees_Troy51a0e822012-09-05 15:24:24 -0400613}
614
615unsigned int gr_get_width(gr_surface surface) {
616 if (surface == NULL) {
617 return 0;
618 }
619 return ((GGLSurface*) surface)->width;
620}
621
622unsigned int gr_get_height(gr_surface surface) {
623 if (surface == NULL) {
624 return 0;
625 }
626 return ((GGLSurface*) surface)->height;
627}
628
629void* gr_loadFont(const char* fontName)
630{
631 int fd;
632 GRFont *font = 0;
633 GGLSurface *ftex;
634 unsigned char *bits, *rle;
635 unsigned char *in, data;
636 unsigned width, height;
637 unsigned element;
638
639 fd = open(fontName, O_RDONLY);
640 if (fd == -1)
641 {
642 char tmp[128];
643
644 sprintf(tmp, "/res/fonts/%s.dat", fontName);
645 fd = open(tmp, O_RDONLY);
646 if (fd == -1)
647 return NULL;
648 }
649
650 font = calloc(sizeof(*font), 1);
651 ftex = &font->texture;
652
653 read(fd, &width, sizeof(unsigned));
654 read(fd, &height, sizeof(unsigned));
655 read(fd, font->offset, sizeof(unsigned) * 96);
656 font->offset[96] = width;
657
658 bits = malloc(width * height);
659 memset(bits, 0, width * height);
660
661 unsigned pos = 0;
662 while (pos < width * height)
663 {
664 int bit;
665
666 read(fd, &data, 1);
667 for (bit = 0; bit < 8; bit++)
668 {
669 if (data & (1 << (7-bit))) bits[pos++] = 255;
670 else bits[pos++] = 0;
671
672 if (pos == width * height) break;
673 }
674 }
675 close(fd);
676
677 ftex->version = sizeof(*ftex);
678 ftex->width = width;
679 ftex->height = height;
680 ftex->stride = width;
681 ftex->data = (void*) bits;
682 ftex->format = GGL_PIXEL_FORMAT_A_8;
683 font->cheight = height;
684 font->ascent = height - 2;
685 return (void*) font;
686}
687
688int gr_getFontDetails(void* font, unsigned* cheight, unsigned* maxwidth)
689{
690 GRFont *fnt = (GRFont*) font;
691
692 if (!fnt) fnt = gr_font;
693 if (!fnt) return -1;
694
695 if (cheight) *cheight = fnt->cheight;
696 if (maxwidth)
697 {
698 int pos;
699 *maxwidth = 0;
700 for (pos = 0; pos < 96; pos++)
701 {
702 unsigned int width = fnt->offset[pos+1] - fnt->offset[pos];
703 if (width > *maxwidth)
704 {
705 *maxwidth = width;
706 }
707 }
708 }
709 return 0;
710}
711
712static void gr_init_font(void)
713{
714 int fontRes;
715 GGLSurface *ftex;
716 unsigned char *bits, *rle;
717 unsigned char *in, data;
718 unsigned width, height;
719 unsigned element;
720
721 gr_font = calloc(sizeof(*gr_font), 1);
722 ftex = &gr_font->texture;
723
724 width = font.width;
725 height = font.height;
726
727 bits = malloc(width * height);
728 rle = bits;
729
730 in = font.rundata;
731 while((data = *in++))
732 {
733 memset(rle, (data & 0x80) ? 255 : 0, data & 0x7f);
734 rle += (data & 0x7f);
735 }
736 for (element = 0; element < 97; element++)
737 {
738 gr_font->offset[element] = (element * font.cwidth);
739 }
740
741 ftex->version = sizeof(*ftex);
742 ftex->width = width;
743 ftex->height = height;
744 ftex->stride = width;
745 ftex->data = (void*) bits;
746 ftex->format = GGL_PIXEL_FORMAT_A_8;
747 gr_font->cheight = height;
748 gr_font->ascent = height - 2;
749 return;
750}
751
752int gr_init(void)
753{
754 gglInit(&gr_context);
755 GGLContext *gl = gr_context;
756
757 gr_init_font();
758 gr_vt_fd = open("/dev/tty0", O_RDWR | O_SYNC);
759 if (gr_vt_fd < 0) {
760 // This is non-fatal; post-Cupcake kernels don't have tty0.
761 } else if (ioctl(gr_vt_fd, KDSETMODE, (void*) KD_GRAPHICS)) {
762 // However, if we do open tty0, we expect the ioctl to work.
763 perror("failed KDSETMODE to KD_GRAPHICS on tty0");
764 gr_exit();
765 return -1;
766 }
767
768 gr_fb_fd = get_framebuffer(gr_framebuffer);
769 if (gr_fb_fd < 0) {
770 perror("Unable to get framebuffer.\n");
771 gr_exit();
772 return -1;
773 }
774
775 get_memory_surface(&gr_mem_surface);
776
777 fprintf(stderr, "framebuffer: fd %d (%d x %d)\n",
778 gr_fb_fd, gr_framebuffer[0].width, gr_framebuffer[0].height);
779
780 /* start with 0 as front (displayed) and 1 as back (drawing) */
781 gr_active_fb = 0;
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600782 if (!has_overlay)
783 set_active_framebuffer(0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400784 gl->colorBuffer(gl, &gr_mem_surface);
785
786 gl->activeTexture(gl, 0);
787 gl->enable(gl, GGL_BLEND);
788 gl->blendFunc(gl, GGL_SRC_ALPHA, GGL_ONE_MINUS_SRC_ALPHA);
789
790// gr_fb_blank(true);
791// gr_fb_blank(false);
792
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600793 if (!alloc_ion_mem(fi.line_length * vi.yres))
794 allocate_overlay(gr_fb_fd, gr_framebuffer);
795
Dees_Troy51a0e822012-09-05 15:24:24 -0400796 return 0;
797}
798
799void gr_exit(void)
800{
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600801 free_overlay(gr_fb_fd);
802 free_ion_mem();
803
Dees_Troy51a0e822012-09-05 15:24:24 -0400804 close(gr_fb_fd);
805 gr_fb_fd = -1;
806
807 free(gr_mem_surface.data);
808
809 ioctl(gr_vt_fd, KDSETMODE, (void*) KD_TEXT);
810 close(gr_vt_fd);
811 gr_vt_fd = -1;
812}
813
814int gr_fb_width(void)
815{
816 return gr_framebuffer[0].width;
817}
818
819int gr_fb_height(void)
820{
821 return gr_framebuffer[0].height;
822}
823
824gr_pixel *gr_fb_data(void)
825{
826 return (unsigned short *) gr_mem_surface.data;
827}
828
Dees_Troy70237dc2013-02-28 21:31:48 +0000829int gr_fb_blank(int blank)
Dees_Troy51a0e822012-09-05 15:24:24 -0400830{
831 int ret;
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600832 if (blank)
833 free_overlay(gr_fb_fd);
Dees_Troy51a0e822012-09-05 15:24:24 -0400834
835 ret = ioctl(gr_fb_fd, FBIOBLANK, blank ? FB_BLANK_POWERDOWN : FB_BLANK_UNBLANK);
836 if (ret < 0)
837 perror("ioctl(): blank");
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600838
839 if (!blank)
840 allocate_overlay(gr_fb_fd, gr_framebuffer);
Dees_Troy70237dc2013-02-28 21:31:48 +0000841 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -0400842}
843
844int gr_get_surface(gr_surface* surface)
845{
846 GGLSurface* ms = malloc(sizeof(GGLSurface));
847 if (!ms) return -1;
848
849 // Allocate the data
850 get_memory_surface(ms);
851
852 // Now, copy the data
853 memcpy(ms->data, gr_mem_surface.data, vi.xres * vi.yres * vi.bits_per_pixel / 8);
854
855 *surface = (gr_surface*) ms;
856 return 0;
857}
858
859int gr_free_surface(gr_surface surface)
860{
861 if (!surface)
862 return -1;
863
864 GGLSurface* ms = (GGLSurface*) surface;
865 free(ms->data);
866 free(ms);
867 return 0;
868}
869
870void gr_write_frame_to_file(int fd)
871{
872 write(fd, gr_mem_surface.data, vi.xres * vi.yres * vi.bits_per_pixel / 8);
873}