blob: 79b1e9aa550252108c45a8ea724166fd2b8dff67 [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];
Vojtech Bocek03fd6c52014-03-13 18:46:34 +010071GGLSurface gr_mem_surface;
Dees_Troy51a0e822012-09-05 15:24:24 -040072static 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
Vojtech Bocek03fd6c52014-03-13 18:46:34 +010079struct fb_var_screeninfo vi;
Dees_Troy51a0e822012-09-05 15:24:24 -040080static 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
Dees Troyb0425382014-04-03 00:10:03 +0000249#ifdef MSM_BSP
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600250 has_overlay = target_has_overlay(fi.id);
251
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600252 if (isTargetMdp5())
253 setDisplaySplit();
Dees Troyb0425382014-04-03 00:10:03 +0000254#else
255 has_overlay = false;
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600256#endif
257
258 if (!has_overlay) {
259 printf("Not using qualcomm overlay, '%s'\n", fi.id);
260 bits = mmap(0, fi.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
261 if (bits == MAP_FAILED) {
262 perror("failed to mmap framebuffer");
263 close(fd);
264 return -1;
265 }
266 } else {
267 printf("Using qualcomm overlay\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400268 }
269
270#ifdef RECOVERY_GRAPHICS_USE_LINELENGTH
271 vi.xres_virtual = fi.line_length / PIXEL_SIZE;
272#endif
273
274 fb->version = sizeof(*fb);
275 fb->width = vi.xres;
276 fb->height = vi.yres;
277#ifdef BOARD_HAS_JANKY_BACKBUFFER
Dees_Troy2673cec2013-04-02 20:22:16 +0000278 printf("setting JANKY BACKBUFFER\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400279 fb->stride = fi.line_length/2;
280#else
281 fb->stride = vi.xres_virtual;
282#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400283 fb->format = PIXEL_FORMAT;
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600284 if (!has_overlay) {
285 fb->data = bits;
286 memset(fb->data, 0, vi.yres * fb->stride * PIXEL_SIZE);
287 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400288
289 fb++;
290
Hiemanshu Sharma94f6c882012-11-21 11:25:22 -0600291 /* check if we can use double buffering */
292 if (vi.yres * fi.line_length * 2 > fi.smem_len)
293 return fd;
294
295 double_buffering = 1;
296
Dees_Troy51a0e822012-09-05 15:24:24 -0400297 fb->version = sizeof(*fb);
298 fb->width = vi.xres;
299 fb->height = vi.yres;
300#ifdef BOARD_HAS_JANKY_BACKBUFFER
301 fb->stride = fi.line_length/2;
302 fb->data = (void*) (((unsigned) bits) + vi.yres * fi.line_length);
303#else
304 fb->stride = vi.xres_virtual;
305 fb->data = (void*) (((unsigned) bits) + vi.yres * fb->stride * PIXEL_SIZE);
306#endif
307 fb->format = PIXEL_FORMAT;
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600308 if (!has_overlay) {
309 memset(fb->data, 0, vi.yres * fb->stride * PIXEL_SIZE);
310 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400311
312#ifdef PRINT_SCREENINFO
313 print_fb_var_screeninfo();
314#endif
315
316 return fd;
317}
318
319static void get_memory_surface(GGLSurface* ms) {
320 ms->version = sizeof(*ms);
321 ms->width = vi.xres;
322 ms->height = vi.yres;
323 ms->stride = vi.xres_virtual;
324 ms->data = malloc(vi.xres_virtual * vi.yres * PIXEL_SIZE);
325 ms->format = PIXEL_FORMAT;
326}
327
328static void set_active_framebuffer(unsigned n)
329{
Hiemanshu Sharma94f6c882012-11-21 11:25:22 -0600330 if (n > 1 || !double_buffering) return;
Hiemanshu Sharmaacf6a9b2012-11-21 11:28:36 -0600331 vi.yres_virtual = vi.yres * NUM_BUFFERS;
Dees_Troy51a0e822012-09-05 15:24:24 -0400332 vi.yoffset = n * vi.yres;
333// vi.bits_per_pixel = PIXEL_SIZE * 8;
334 if (ioctl(gr_fb_fd, FBIOPUT_VSCREENINFO, &vi) < 0) {
335 perror("active fb swap failed");
336 }
337}
338
339void gr_flip(void)
340{
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600341 if (-EINVAL == overlay_display_frame(gr_fb_fd, gr_mem_surface.data,
342 (fi.line_length * vi.yres))) {
343 GGLContext *gl = gr_context;
Dees_Troy51a0e822012-09-05 15:24:24 -0400344
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600345 /* swap front and back buffers */
346 if (double_buffering)
347 gr_active_fb = (gr_active_fb + 1) & 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400348
349#ifdef BOARD_HAS_FLIPPED_SCREEN
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600350 /* flip buffer 180 degrees for devices with physicaly inverted screens */
351 unsigned int i;
352 unsigned int j;
353 uint8_t tmp;
354 for (i = 0; i < ((vi.xres_virtual * vi.yres)/2); i++) {
355 for (j = 0; j < PIXEL_SIZE; j++) {
356 tmp = gr_mem_surface.data[i * PIXEL_SIZE + j];
357 gr_mem_surface.data[i * PIXEL_SIZE + j] = gr_mem_surface.data[(vi.xres_virtual * vi.yres * PIXEL_SIZE) - ((i+1) * PIXEL_SIZE) + j];
358 gr_mem_surface.data[(vi.xres_virtual * vi.yres * PIXEL_SIZE) - ((i+1) * PIXEL_SIZE) + j] = tmp;
359 }
360 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400361#endif
362
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600363 /* copy data from the in-memory surface to the buffer we're about
364 * to make active. */
365 memcpy(gr_framebuffer[gr_active_fb].data, gr_mem_surface.data,
366 vi.xres_virtual * vi.yres * PIXEL_SIZE);
Dees_Troy51a0e822012-09-05 15:24:24 -0400367
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600368 /* inform the display driver */
369 set_active_framebuffer(gr_active_fb);
370 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400371}
372
373void gr_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
374{
375 GGLContext *gl = gr_context;
376 GGLint color[4];
377 color[0] = ((r << 8) | r) + 1;
378 color[1] = ((g << 8) | g) + 1;
379 color[2] = ((b << 8) | b) + 1;
380 color[3] = ((a << 8) | a) + 1;
381 gl->color4xv(gl, color);
Vojtech Bocek1a4744a2014-02-06 19:52:28 +0100382
383 gr_is_curr_clr_opaque = (a == 255);
Dees_Troy51a0e822012-09-05 15:24:24 -0400384}
385
386int gr_measureEx(const char *s, void* font)
387{
388 GRFont* fnt = (GRFont*) font;
389 int total = 0;
390 unsigned pos;
391 unsigned off;
392
393 if (!fnt) fnt = gr_font;
394
395 while ((off = *s++))
396 {
397 off -= 32;
398 if (off < 96)
399 total += (fnt->offset[off+1] - fnt->offset[off]);
400 }
401 return total;
402}
403
Dees Troy31218ec2014-02-25 20:35:56 +0000404int gr_maxExW(const char *s, void* font, int max_width)
405{
406 GRFont* fnt = (GRFont*) font;
407 int total = 0;
408 unsigned pos;
409 unsigned off;
410
411 if (!fnt) fnt = gr_font;
412
413 while ((off = *s++))
414 {
415 off -= 32;
416 if (off < 96) {
417 max_width -= (fnt->offset[off+1] - fnt->offset[off]);
418 if (max_width > 0) {
419 total++;
420 } else {
421 return total;
422 }
423 }
424 }
425 return total;
426}
427
Dees_Troy51a0e822012-09-05 15:24:24 -0400428unsigned character_width(const char *s, void* pFont)
429{
430 GRFont *font = (GRFont*) pFont;
431 unsigned off;
432
433 /* Handle default font */
434 if (!font) font = gr_font;
435
436 off = *s - 32;
437 if (off == 0)
438 return 0;
439
440 return font->offset[off+1] - font->offset[off];
441}
442
443int gr_textEx(int x, int y, const char *s, void* pFont)
444{
445 GGLContext *gl = gr_context;
446 GRFont *font = (GRFont*) pFont;
447 unsigned off;
448 unsigned cwidth;
449
450 /* Handle default font */
451 if (!font) font = gr_font;
452
453 gl->bindTexture(gl, &font->texture);
454 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
455 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
456 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
457 gl->enable(gl, GGL_TEXTURE_2D);
458
459 while((off = *s++)) {
460 off -= 32;
461 cwidth = 0;
462 if (off < 96) {
463 cwidth = font->offset[off+1] - font->offset[off];
464 gl->texCoord2i(gl, (font->offset[off]) - x, 0 - y);
465 gl->recti(gl, x, y, x + cwidth, y + font->cheight);
466 x += cwidth;
467 }
468 }
469
470 return x;
471}
472
473int gr_textExW(int x, int y, const char *s, void* pFont, int max_width)
474{
475 GGLContext *gl = gr_context;
476 GRFont *font = (GRFont*) pFont;
477 unsigned off;
478 unsigned cwidth;
479
480 /* Handle default font */
481 if (!font) font = gr_font;
482
483 gl->bindTexture(gl, &font->texture);
484 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
485 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
486 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
487 gl->enable(gl, GGL_TEXTURE_2D);
488
489 while((off = *s++)) {
490 off -= 32;
491 cwidth = 0;
492 if (off < 96) {
493 cwidth = font->offset[off+1] - font->offset[off];
494 if ((x + (int)cwidth) < max_width) {
495 gl->texCoord2i(gl, (font->offset[off]) - x, 0 - y);
496 gl->recti(gl, x, y, x + cwidth, y + font->cheight);
497 x += cwidth;
498 } else {
499 gl->texCoord2i(gl, (font->offset[off]) - x, 0 - y);
500 gl->recti(gl, x, y, max_width, y + font->cheight);
501 x = max_width;
502 return x;
503 }
504 }
505 }
506
507 return x;
508}
509
510int gr_textExWH(int x, int y, const char *s, void* pFont, int max_width, int max_height)
511{
512 GGLContext *gl = gr_context;
513 GRFont *font = (GRFont*) pFont;
514 unsigned off;
515 unsigned cwidth;
516 int rect_x, rect_y;
517
518 /* Handle default font */
519 if (!font) font = gr_font;
520
521 gl->bindTexture(gl, &font->texture);
522 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
523 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
524 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
525 gl->enable(gl, GGL_TEXTURE_2D);
526
527 while((off = *s++)) {
528 off -= 32;
529 cwidth = 0;
530 if (off < 96) {
531 cwidth = font->offset[off+1] - font->offset[off];
532 if ((x + (int)cwidth) < max_width)
533 rect_x = x + cwidth;
534 else
535 rect_x = max_width;
Dees_Troyf7596752012-09-28 13:21:36 -0400536 if (y + font->cheight < (unsigned int)(max_height))
Dees_Troy51a0e822012-09-05 15:24:24 -0400537 rect_y = y + font->cheight;
538 else
539 rect_y = max_height;
540
541 gl->texCoord2i(gl, (font->offset[off]) - x, 0 - y);
542 gl->recti(gl, x, y, rect_x, rect_y);
543 x += cwidth;
544 if (x > max_width)
545 return x;
546 }
547 }
548
549 return x;
550}
551
552int twgr_text(int x, int y, const char *s)
553{
554 GGLContext *gl = gr_context;
555 GRFont *font = gr_font;
556 unsigned off;
Dees_Troyf7596752012-09-28 13:21:36 -0400557 unsigned cwidth = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400558
559 y -= font->ascent;
560
561 gl->bindTexture(gl, &font->texture);
562 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
563 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
564 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
565 gl->enable(gl, GGL_TEXTURE_2D);
566
567 while((off = *s++)) {
568 off -= 32;
569 if (off < 96) {
570 cwidth = font->offset[off+1] - font->offset[off];
571 gl->texCoord2i(gl, (off * cwidth) - x, 0 - y);
572 gl->recti(gl, x, y, x + cwidth, y + font->cheight);
573 }
574 x += cwidth;
575 }
576
577 return x;
578}
579
580void gr_fill(int x, int y, int w, int h)
581{
582 GGLContext *gl = gr_context;
Vojtech Bocek1a4744a2014-02-06 19:52:28 +0100583
584 if(gr_is_curr_clr_opaque)
585 gl->disable(gl, GGL_BLEND);
586
Dees_Troy51a0e822012-09-05 15:24:24 -0400587 gl->disable(gl, GGL_TEXTURE_2D);
588 gl->recti(gl, x, y, x + w, y + h);
Vojtech Bocek1a4744a2014-02-06 19:52:28 +0100589
590 if(gr_is_curr_clr_opaque)
591 gl->enable(gl, GGL_BLEND);
Dees_Troy51a0e822012-09-05 15:24:24 -0400592}
593
594void gr_blit(gr_surface source, int sx, int sy, int w, int h, int dx, int dy) {
595 if (gr_context == NULL) {
596 return;
597 }
598
599 GGLContext *gl = gr_context;
Vojtech Bocek6c694b62014-02-06 20:36:25 +0100600 GGLSurface *surface = (GGLSurface*)source;
601
602 if(surface->format == GGL_PIXEL_FORMAT_RGBX_8888)
603 gl->disable(gl, GGL_BLEND);
604
605 gl->bindTexture(gl, surface);
Dees_Troy51a0e822012-09-05 15:24:24 -0400606 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
607 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
608 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
609 gl->enable(gl, GGL_TEXTURE_2D);
610 gl->texCoord2i(gl, sx - dx, sy - dy);
611 gl->recti(gl, dx, dy, dx + w, dy + h);
Vojtech Bocek6c694b62014-02-06 20:36:25 +0100612
613 if(surface->format == GGL_PIXEL_FORMAT_RGBX_8888)
614 gl->enable(gl, GGL_BLEND);
Dees_Troy51a0e822012-09-05 15:24:24 -0400615}
616
617unsigned int gr_get_width(gr_surface surface) {
618 if (surface == NULL) {
619 return 0;
620 }
621 return ((GGLSurface*) surface)->width;
622}
623
624unsigned int gr_get_height(gr_surface surface) {
625 if (surface == NULL) {
626 return 0;
627 }
628 return ((GGLSurface*) surface)->height;
629}
630
631void* gr_loadFont(const char* fontName)
632{
633 int fd;
634 GRFont *font = 0;
635 GGLSurface *ftex;
636 unsigned char *bits, *rle;
637 unsigned char *in, data;
638 unsigned width, height;
639 unsigned element;
640
641 fd = open(fontName, O_RDONLY);
642 if (fd == -1)
643 {
644 char tmp[128];
645
646 sprintf(tmp, "/res/fonts/%s.dat", fontName);
647 fd = open(tmp, O_RDONLY);
648 if (fd == -1)
649 return NULL;
650 }
651
652 font = calloc(sizeof(*font), 1);
653 ftex = &font->texture;
654
655 read(fd, &width, sizeof(unsigned));
656 read(fd, &height, sizeof(unsigned));
657 read(fd, font->offset, sizeof(unsigned) * 96);
658 font->offset[96] = width;
659
660 bits = malloc(width * height);
661 memset(bits, 0, width * height);
662
663 unsigned pos = 0;
664 while (pos < width * height)
665 {
666 int bit;
667
668 read(fd, &data, 1);
669 for (bit = 0; bit < 8; bit++)
670 {
671 if (data & (1 << (7-bit))) bits[pos++] = 255;
672 else bits[pos++] = 0;
673
674 if (pos == width * height) break;
675 }
676 }
677 close(fd);
678
679 ftex->version = sizeof(*ftex);
680 ftex->width = width;
681 ftex->height = height;
682 ftex->stride = width;
683 ftex->data = (void*) bits;
684 ftex->format = GGL_PIXEL_FORMAT_A_8;
685 font->cheight = height;
686 font->ascent = height - 2;
687 return (void*) font;
688}
689
690int gr_getFontDetails(void* font, unsigned* cheight, unsigned* maxwidth)
691{
692 GRFont *fnt = (GRFont*) font;
693
694 if (!fnt) fnt = gr_font;
695 if (!fnt) return -1;
696
697 if (cheight) *cheight = fnt->cheight;
698 if (maxwidth)
699 {
700 int pos;
701 *maxwidth = 0;
702 for (pos = 0; pos < 96; pos++)
703 {
704 unsigned int width = fnt->offset[pos+1] - fnt->offset[pos];
705 if (width > *maxwidth)
706 {
707 *maxwidth = width;
708 }
709 }
710 }
711 return 0;
712}
713
714static void gr_init_font(void)
715{
716 int fontRes;
717 GGLSurface *ftex;
718 unsigned char *bits, *rle;
719 unsigned char *in, data;
720 unsigned width, height;
721 unsigned element;
722
723 gr_font = calloc(sizeof(*gr_font), 1);
724 ftex = &gr_font->texture;
725
726 width = font.width;
727 height = font.height;
728
729 bits = malloc(width * height);
730 rle = bits;
731
732 in = font.rundata;
733 while((data = *in++))
734 {
735 memset(rle, (data & 0x80) ? 255 : 0, data & 0x7f);
736 rle += (data & 0x7f);
737 }
738 for (element = 0; element < 97; element++)
739 {
740 gr_font->offset[element] = (element * font.cwidth);
741 }
742
743 ftex->version = sizeof(*ftex);
744 ftex->width = width;
745 ftex->height = height;
746 ftex->stride = width;
747 ftex->data = (void*) bits;
748 ftex->format = GGL_PIXEL_FORMAT_A_8;
749 gr_font->cheight = height;
750 gr_font->ascent = height - 2;
751 return;
752}
753
754int gr_init(void)
755{
756 gglInit(&gr_context);
757 GGLContext *gl = gr_context;
758
759 gr_init_font();
760 gr_vt_fd = open("/dev/tty0", O_RDWR | O_SYNC);
761 if (gr_vt_fd < 0) {
762 // This is non-fatal; post-Cupcake kernels don't have tty0.
763 } else if (ioctl(gr_vt_fd, KDSETMODE, (void*) KD_GRAPHICS)) {
764 // However, if we do open tty0, we expect the ioctl to work.
765 perror("failed KDSETMODE to KD_GRAPHICS on tty0");
766 gr_exit();
767 return -1;
768 }
769
770 gr_fb_fd = get_framebuffer(gr_framebuffer);
771 if (gr_fb_fd < 0) {
772 perror("Unable to get framebuffer.\n");
773 gr_exit();
774 return -1;
775 }
776
777 get_memory_surface(&gr_mem_surface);
778
779 fprintf(stderr, "framebuffer: fd %d (%d x %d)\n",
780 gr_fb_fd, gr_framebuffer[0].width, gr_framebuffer[0].height);
781
782 /* start with 0 as front (displayed) and 1 as back (drawing) */
783 gr_active_fb = 0;
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600784 if (!has_overlay)
785 set_active_framebuffer(0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400786 gl->colorBuffer(gl, &gr_mem_surface);
787
788 gl->activeTexture(gl, 0);
789 gl->enable(gl, GGL_BLEND);
790 gl->blendFunc(gl, GGL_SRC_ALPHA, GGL_ONE_MINUS_SRC_ALPHA);
791
xNUTxcd56f8c2014-07-23 01:30:20 +0200792#ifdef TW_SCREEN_BLANK_ON_BOOT
793 printf("TW_SCREEN_BLANK_ON_BOOT := true\n");
794 gr_fb_blank(true);
795 gr_fb_blank(false);
796#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400797
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600798 if (!alloc_ion_mem(fi.line_length * vi.yres))
799 allocate_overlay(gr_fb_fd, gr_framebuffer);
800
Dees_Troy51a0e822012-09-05 15:24:24 -0400801 return 0;
802}
803
804void gr_exit(void)
805{
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600806 free_overlay(gr_fb_fd);
807 free_ion_mem();
808
Dees_Troy51a0e822012-09-05 15:24:24 -0400809 close(gr_fb_fd);
810 gr_fb_fd = -1;
811
812 free(gr_mem_surface.data);
813
814 ioctl(gr_vt_fd, KDSETMODE, (void*) KD_TEXT);
815 close(gr_vt_fd);
816 gr_vt_fd = -1;
817}
818
819int gr_fb_width(void)
820{
821 return gr_framebuffer[0].width;
822}
823
824int gr_fb_height(void)
825{
826 return gr_framebuffer[0].height;
827}
828
829gr_pixel *gr_fb_data(void)
830{
831 return (unsigned short *) gr_mem_surface.data;
832}
833
Dees_Troy70237dc2013-02-28 21:31:48 +0000834int gr_fb_blank(int blank)
Dees_Troy51a0e822012-09-05 15:24:24 -0400835{
836 int ret;
Dees Troy05d18c92014-08-04 18:17:07 +0000837 //if (blank)
838 //free_overlay(gr_fb_fd);
Dees_Troy51a0e822012-09-05 15:24:24 -0400839
840 ret = ioctl(gr_fb_fd, FBIOBLANK, blank ? FB_BLANK_POWERDOWN : FB_BLANK_UNBLANK);
841 if (ret < 0)
842 perror("ioctl(): blank");
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600843
Dees Troy05d18c92014-08-04 18:17:07 +0000844 //if (!blank)
845 //allocate_overlay(gr_fb_fd, gr_framebuffer);
846 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -0400847}
848
849int gr_get_surface(gr_surface* surface)
850{
851 GGLSurface* ms = malloc(sizeof(GGLSurface));
852 if (!ms) return -1;
853
854 // Allocate the data
855 get_memory_surface(ms);
856
857 // Now, copy the data
858 memcpy(ms->data, gr_mem_surface.data, vi.xres * vi.yres * vi.bits_per_pixel / 8);
859
860 *surface = (gr_surface*) ms;
861 return 0;
862}
863
864int gr_free_surface(gr_surface surface)
865{
866 if (!surface)
867 return -1;
868
869 GGLSurface* ms = (GGLSurface*) surface;
870 free(ms->data);
871 free(ms);
872 return 0;
873}
874
875void gr_write_frame_to_file(int fd)
876{
877 write(fd, gr_mem_surface.data, vi.xres * vi.yres * vi.bits_per_pixel / 8);
878}