blob: d08e74303a85e51ac1e67a43a57e4b3463fb0541 [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 {
Vojtech Bocek76ee9032014-09-07 15:01:56 +020061 int type;
Dees_Troy51a0e822012-09-05 15:24:24 -040062 GGLSurface texture;
63 unsigned offset[97];
64 unsigned cheight;
65 unsigned ascent;
66} GRFont;
67
68static GRFont *gr_font = 0;
69static GGLContext *gr_context = 0;
70static GGLSurface gr_font_texture;
Hiemanshu Sharmaacf6a9b2012-11-21 11:28:36 -060071static GGLSurface gr_framebuffer[NUM_BUFFERS];
Vojtech Bocek03fd6c52014-03-13 18:46:34 +010072GGLSurface gr_mem_surface;
Dees_Troy51a0e822012-09-05 15:24:24 -040073static unsigned gr_active_fb = 0;
Hiemanshu Sharma94f6c882012-11-21 11:25:22 -060074static unsigned double_buffering = 0;
Vojtech Bocek1a4744a2014-02-06 19:52:28 +010075static int gr_is_curr_clr_opaque = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -040076
77static int gr_fb_fd = -1;
78static int gr_vt_fd = -1;
79
Vojtech Bocek03fd6c52014-03-13 18:46:34 +010080struct fb_var_screeninfo vi;
Dees_Troy51a0e822012-09-05 15:24:24 -040081static struct fb_fix_screeninfo fi;
82
Ethan Yonker4ee5ad72014-02-18 18:41:17 -060083static bool has_overlay = false;
84static int leftSplit = 0;
85static int rightSplit = 0;
86
87bool target_has_overlay(char *version);
88int free_ion_mem(void);
89int alloc_ion_mem(unsigned int size);
90int allocate_overlay(int fd, GGLSurface gr_fb[]);
91int free_overlay(int fd);
92int overlay_display_frame(int fd, GGLubyte* data, size_t size);
93
Dees_Troy51a0e822012-09-05 15:24:24 -040094#ifdef PRINT_SCREENINFO
95static void print_fb_var_screeninfo()
96{
Dees_Troy2673cec2013-04-02 20:22:16 +000097 printf("vi.xres: %d\n", vi.xres);
98 printf("vi.yres: %d\n", vi.yres);
99 printf("vi.xres_virtual: %d\n", vi.xres_virtual);
100 printf("vi.yres_virtual: %d\n", vi.yres_virtual);
101 printf("vi.xoffset: %d\n", vi.xoffset);
102 printf("vi.yoffset: %d\n", vi.yoffset);
103 printf("vi.bits_per_pixel: %d\n", vi.bits_per_pixel);
104 printf("vi.grayscale: %d\n", vi.grayscale);
Dees_Troy51a0e822012-09-05 15:24:24 -0400105}
106#endif
107
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600108#ifdef MSM_BSP
109int getLeftSplit(void) {
110 //Default even split for all displays with high res
111 int lSplit = vi.xres / 2;
112
113 //Override if split published by driver
114 if (leftSplit)
115 lSplit = leftSplit;
116
117 return lSplit;
118}
119
120int getRightSplit(void) {
121 return rightSplit;
122}
123
124
125void setDisplaySplit(void) {
126 char split[64] = {0};
127 FILE* fp = fopen("/sys/class/graphics/fb0/msm_fb_split", "r");
128 if (fp) {
129 //Format "left right" space as delimiter
130 if(fread(split, sizeof(char), 64, fp)) {
131 leftSplit = atoi(split);
132 printf("Left Split=%d\n",leftSplit);
133 char *rght = strpbrk(split, " ");
134 if (rght)
135 rightSplit = atoi(rght + 1);
136 printf("Right Split=%d\n", rightSplit);
137 }
138 } else {
139 printf("Failed to open mdss_fb_split node\n");
140 }
141 if (fp)
142 fclose(fp);
143}
144
145bool isDisplaySplit(void) {
146 if (vi.xres > MAX_DISPLAY_DIM)
147 return true;
148 //check if right split is set by driver
149 if (getRightSplit())
150 return true;
151
152 return false;
153}
154
155int getFbXres(void) {
156 return vi.xres;
157}
158
159int getFbYres (void) {
160 return vi.yres;
161}
162#endif // MSM_BSP
163
Dees_Troy51a0e822012-09-05 15:24:24 -0400164static int get_framebuffer(GGLSurface *fb)
165{
166 int fd;
167 void *bits;
168
169 fd = open("/dev/graphics/fb0", O_RDWR);
170 if (fd < 0) {
171 perror("cannot open fb0");
172 return -1;
173 }
174
175 if (ioctl(fd, FBIOGET_VSCREENINFO, &vi) < 0) {
176 perror("failed to get fb0 info");
177 close(fd);
178 return -1;
179 }
180
181 fprintf(stderr, "Pixel format: %dx%d @ %dbpp\n", vi.xres, vi.yres, vi.bits_per_pixel);
182
183 vi.bits_per_pixel = PIXEL_SIZE * 8;
184 if (PIXEL_FORMAT == GGL_PIXEL_FORMAT_BGRA_8888) {
185 fprintf(stderr, "Pixel format: BGRA_8888\n");
186 if (PIXEL_SIZE != 4) fprintf(stderr, "E: Pixel Size mismatch!\n");
187 vi.red.offset = 8;
188 vi.red.length = 8;
189 vi.green.offset = 16;
190 vi.green.length = 8;
191 vi.blue.offset = 24;
192 vi.blue.length = 8;
193 vi.transp.offset = 0;
194 vi.transp.length = 8;
195 } else if (PIXEL_FORMAT == GGL_PIXEL_FORMAT_RGBX_8888) {
196 fprintf(stderr, "Pixel format: RGBX_8888\n");
197 if (PIXEL_SIZE != 4) fprintf(stderr, "E: Pixel Size mismatch!\n");
198 vi.red.offset = 24;
199 vi.red.length = 8;
200 vi.green.offset = 16;
201 vi.green.length = 8;
202 vi.blue.offset = 8;
203 vi.blue.length = 8;
204 vi.transp.offset = 0;
205 vi.transp.length = 8;
206 } else if (PIXEL_FORMAT == GGL_PIXEL_FORMAT_RGB_565) {
207#ifdef RECOVERY_RGB_565
208 fprintf(stderr, "Pixel format: RGB_565\n");
209 vi.blue.offset = 0;
210 vi.green.offset = 5;
211 vi.red.offset = 11;
212#else
213 fprintf(stderr, "Pixel format: BGR_565\n");
214 vi.blue.offset = 11;
215 vi.green.offset = 5;
216 vi.red.offset = 0;
217#endif
218 if (PIXEL_SIZE != 2) fprintf(stderr, "E: Pixel Size mismatch!\n");
219 vi.blue.length = 5;
220 vi.green.length = 6;
221 vi.red.length = 5;
222 vi.blue.msb_right = 0;
223 vi.green.msb_right = 0;
224 vi.red.msb_right = 0;
225 vi.transp.offset = 0;
226 vi.transp.length = 0;
227 }
228 else
229 {
230 perror("unknown pixel format");
231 close(fd);
232 return -1;
233 }
234
235 vi.vmode = FB_VMODE_NONINTERLACED;
236 vi.activate = FB_ACTIVATE_NOW | FB_ACTIVATE_FORCE;
237
238 if (ioctl(fd, FBIOPUT_VSCREENINFO, &vi) < 0) {
239 perror("failed to put fb0 info");
240 close(fd);
241 return -1;
242 }
243
244 if (ioctl(fd, FBIOGET_FSCREENINFO, &fi) < 0) {
245 perror("failed to get fb0 info");
246 close(fd);
247 return -1;
248 }
249
Dees Troyb0425382014-04-03 00:10:03 +0000250#ifdef MSM_BSP
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600251 has_overlay = target_has_overlay(fi.id);
252
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600253 if (isTargetMdp5())
254 setDisplaySplit();
Dees Troyb0425382014-04-03 00:10:03 +0000255#else
256 has_overlay = false;
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600257#endif
258
259 if (!has_overlay) {
260 printf("Not using qualcomm overlay, '%s'\n", fi.id);
261 bits = mmap(0, fi.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
262 if (bits == MAP_FAILED) {
263 perror("failed to mmap framebuffer");
264 close(fd);
265 return -1;
266 }
267 } else {
268 printf("Using qualcomm overlay\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400269 }
270
271#ifdef RECOVERY_GRAPHICS_USE_LINELENGTH
272 vi.xres_virtual = fi.line_length / PIXEL_SIZE;
273#endif
274
275 fb->version = sizeof(*fb);
276 fb->width = vi.xres;
277 fb->height = vi.yres;
278#ifdef BOARD_HAS_JANKY_BACKBUFFER
Dees_Troy2673cec2013-04-02 20:22:16 +0000279 printf("setting JANKY BACKBUFFER\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400280 fb->stride = fi.line_length/2;
281#else
282 fb->stride = vi.xres_virtual;
283#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400284 fb->format = PIXEL_FORMAT;
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600285 if (!has_overlay) {
286 fb->data = bits;
287 memset(fb->data, 0, vi.yres * fb->stride * PIXEL_SIZE);
288 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400289
290 fb++;
291
Hiemanshu Sharma94f6c882012-11-21 11:25:22 -0600292 /* check if we can use double buffering */
293 if (vi.yres * fi.line_length * 2 > fi.smem_len)
294 return fd;
295
296 double_buffering = 1;
297
Dees_Troy51a0e822012-09-05 15:24:24 -0400298 fb->version = sizeof(*fb);
299 fb->width = vi.xres;
300 fb->height = vi.yres;
301#ifdef BOARD_HAS_JANKY_BACKBUFFER
302 fb->stride = fi.line_length/2;
Ethan Yonkerbcc502c2014-11-10 11:22:10 -0600303 fb->data = (GGLubyte*) (((unsigned long) bits) + vi.yres * fi.line_length);
Dees_Troy51a0e822012-09-05 15:24:24 -0400304#else
305 fb->stride = vi.xres_virtual;
Ethan Yonkerbcc502c2014-11-10 11:22:10 -0600306 fb->data = (GGLubyte*) (((unsigned long) bits) + vi.yres * fb->stride * PIXEL_SIZE);
Dees_Troy51a0e822012-09-05 15:24:24 -0400307#endif
308 fb->format = PIXEL_FORMAT;
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600309 if (!has_overlay) {
310 memset(fb->data, 0, vi.yres * fb->stride * PIXEL_SIZE);
311 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400312
313#ifdef PRINT_SCREENINFO
314 print_fb_var_screeninfo();
315#endif
316
317 return fd;
318}
319
320static void get_memory_surface(GGLSurface* ms) {
321 ms->version = sizeof(*ms);
322 ms->width = vi.xres;
323 ms->height = vi.yres;
324 ms->stride = vi.xres_virtual;
325 ms->data = malloc(vi.xres_virtual * vi.yres * PIXEL_SIZE);
326 ms->format = PIXEL_FORMAT;
327}
328
329static void set_active_framebuffer(unsigned n)
330{
Hiemanshu Sharma94f6c882012-11-21 11:25:22 -0600331 if (n > 1 || !double_buffering) return;
Hiemanshu Sharmaacf6a9b2012-11-21 11:28:36 -0600332 vi.yres_virtual = vi.yres * NUM_BUFFERS;
Dees_Troy51a0e822012-09-05 15:24:24 -0400333 vi.yoffset = n * vi.yres;
334// vi.bits_per_pixel = PIXEL_SIZE * 8;
335 if (ioctl(gr_fb_fd, FBIOPUT_VSCREENINFO, &vi) < 0) {
336 perror("active fb swap failed");
337 }
338}
339
340void gr_flip(void)
341{
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600342 if (-EINVAL == overlay_display_frame(gr_fb_fd, gr_mem_surface.data,
343 (fi.line_length * vi.yres))) {
344 GGLContext *gl = gr_context;
Dees_Troy51a0e822012-09-05 15:24:24 -0400345
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600346 /* swap front and back buffers */
347 if (double_buffering)
348 gr_active_fb = (gr_active_fb + 1) & 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400349
350#ifdef BOARD_HAS_FLIPPED_SCREEN
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600351 /* flip buffer 180 degrees for devices with physicaly inverted screens */
352 unsigned int i;
353 unsigned int j;
354 uint8_t tmp;
355 for (i = 0; i < ((vi.xres_virtual * vi.yres)/2); i++) {
356 for (j = 0; j < PIXEL_SIZE; j++) {
357 tmp = gr_mem_surface.data[i * PIXEL_SIZE + j];
358 gr_mem_surface.data[i * PIXEL_SIZE + j] = gr_mem_surface.data[(vi.xres_virtual * vi.yres * PIXEL_SIZE) - ((i+1) * PIXEL_SIZE) + j];
359 gr_mem_surface.data[(vi.xres_virtual * vi.yres * PIXEL_SIZE) - ((i+1) * PIXEL_SIZE) + j] = tmp;
360 }
361 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400362#endif
363
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600364 /* copy data from the in-memory surface to the buffer we're about
365 * to make active. */
366 memcpy(gr_framebuffer[gr_active_fb].data, gr_mem_surface.data,
367 vi.xres_virtual * vi.yres * PIXEL_SIZE);
Dees_Troy51a0e822012-09-05 15:24:24 -0400368
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600369 /* inform the display driver */
370 set_active_framebuffer(gr_active_fb);
371 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400372}
373
374void gr_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
375{
376 GGLContext *gl = gr_context;
377 GGLint color[4];
378 color[0] = ((r << 8) | r) + 1;
379 color[1] = ((g << 8) | g) + 1;
380 color[2] = ((b << 8) | b) + 1;
381 color[3] = ((a << 8) | a) + 1;
382 gl->color4xv(gl, color);
Vojtech Bocek1a4744a2014-02-06 19:52:28 +0100383
384 gr_is_curr_clr_opaque = (a == 255);
Dees_Troy51a0e822012-09-05 15:24:24 -0400385}
386
387int gr_measureEx(const char *s, void* font)
388{
389 GRFont* fnt = (GRFont*) font;
390 int total = 0;
391 unsigned pos;
392 unsigned off;
393
394 if (!fnt) fnt = gr_font;
395
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200396#ifndef TW_DISABLE_TTF
397 if(fnt->type == FONT_TYPE_TTF)
398 return gr_ttf_measureEx(s, font);
399#endif
400
Dees_Troy51a0e822012-09-05 15:24:24 -0400401 while ((off = *s++))
402 {
403 off -= 32;
404 if (off < 96)
405 total += (fnt->offset[off+1] - fnt->offset[off]);
406 }
407 return total;
408}
409
Dees Troy31218ec2014-02-25 20:35:56 +0000410int gr_maxExW(const char *s, void* font, int max_width)
411{
412 GRFont* fnt = (GRFont*) font;
413 int total = 0;
414 unsigned pos;
415 unsigned off;
416
417 if (!fnt) fnt = gr_font;
418
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200419#ifndef TW_DISABLE_TTF
420 if(fnt->type == FONT_TYPE_TTF)
421 return gr_ttf_maxExW(s, font, max_width);
422#endif
423
Dees Troy31218ec2014-02-25 20:35:56 +0000424 while ((off = *s++))
425 {
426 off -= 32;
427 if (off < 96) {
428 max_width -= (fnt->offset[off+1] - fnt->offset[off]);
429 if (max_width > 0) {
430 total++;
431 } else {
432 return total;
433 }
434 }
435 }
436 return total;
437}
438
Dees_Troy51a0e822012-09-05 15:24:24 -0400439int gr_textEx(int x, int y, const char *s, void* pFont)
440{
441 GGLContext *gl = gr_context;
442 GRFont *font = (GRFont*) pFont;
443 unsigned off;
444 unsigned cwidth;
445
446 /* Handle default font */
447 if (!font) font = gr_font;
448
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200449#ifndef TW_DISABLE_TTF
450 if(font->type == FONT_TYPE_TTF)
451 return gr_ttf_textExWH(gl, x, y, s, pFont, -1, -1);
452#endif
453
Dees_Troy51a0e822012-09-05 15:24:24 -0400454 gl->bindTexture(gl, &font->texture);
455 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
456 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
457 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
458 gl->enable(gl, GGL_TEXTURE_2D);
459
460 while((off = *s++)) {
461 off -= 32;
462 cwidth = 0;
463 if (off < 96) {
464 cwidth = font->offset[off+1] - font->offset[off];
465 gl->texCoord2i(gl, (font->offset[off]) - x, 0 - y);
466 gl->recti(gl, x, y, x + cwidth, y + font->cheight);
467 x += cwidth;
468 }
469 }
470
471 return x;
472}
473
474int gr_textExW(int x, int y, const char *s, void* pFont, int max_width)
475{
476 GGLContext *gl = gr_context;
477 GRFont *font = (GRFont*) pFont;
478 unsigned off;
479 unsigned cwidth;
480
481 /* Handle default font */
482 if (!font) font = gr_font;
483
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200484#ifndef TW_DISABLE_TTF
485 if(font->type == FONT_TYPE_TTF)
486 return gr_ttf_textExWH(gl, x, y, s, pFont, max_width, -1);
487#endif
488
Dees_Troy51a0e822012-09-05 15:24:24 -0400489 gl->bindTexture(gl, &font->texture);
490 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
491 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
492 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
493 gl->enable(gl, GGL_TEXTURE_2D);
494
495 while((off = *s++)) {
496 off -= 32;
497 cwidth = 0;
498 if (off < 96) {
499 cwidth = font->offset[off+1] - font->offset[off];
500 if ((x + (int)cwidth) < max_width) {
501 gl->texCoord2i(gl, (font->offset[off]) - x, 0 - y);
502 gl->recti(gl, x, y, x + cwidth, y + font->cheight);
503 x += cwidth;
504 } else {
505 gl->texCoord2i(gl, (font->offset[off]) - x, 0 - y);
506 gl->recti(gl, x, y, max_width, y + font->cheight);
507 x = max_width;
508 return x;
509 }
510 }
511 }
512
513 return x;
514}
515
516int gr_textExWH(int x, int y, const char *s, void* pFont, int max_width, int max_height)
517{
518 GGLContext *gl = gr_context;
519 GRFont *font = (GRFont*) pFont;
520 unsigned off;
521 unsigned cwidth;
522 int rect_x, rect_y;
523
524 /* Handle default font */
525 if (!font) font = gr_font;
526
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200527#ifndef TW_DISABLE_TTF
528 if(font->type == FONT_TYPE_TTF)
529 return gr_ttf_textExWH(gl, x, y, s, pFont, max_width, max_height);
530#endif
531
Dees_Troy51a0e822012-09-05 15:24:24 -0400532 gl->bindTexture(gl, &font->texture);
533 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
534 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
535 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
536 gl->enable(gl, GGL_TEXTURE_2D);
537
538 while((off = *s++)) {
539 off -= 32;
540 cwidth = 0;
541 if (off < 96) {
542 cwidth = font->offset[off+1] - font->offset[off];
543 if ((x + (int)cwidth) < max_width)
544 rect_x = x + cwidth;
545 else
546 rect_x = max_width;
Dees_Troyf7596752012-09-28 13:21:36 -0400547 if (y + font->cheight < (unsigned int)(max_height))
Dees_Troy51a0e822012-09-05 15:24:24 -0400548 rect_y = y + font->cheight;
549 else
550 rect_y = max_height;
551
552 gl->texCoord2i(gl, (font->offset[off]) - x, 0 - y);
553 gl->recti(gl, x, y, rect_x, rect_y);
554 x += cwidth;
555 if (x > max_width)
556 return x;
557 }
558 }
559
560 return x;
561}
562
Dees_Troy51a0e822012-09-05 15:24:24 -0400563void gr_fill(int x, int y, int w, int h)
564{
565 GGLContext *gl = gr_context;
Vojtech Bocek1a4744a2014-02-06 19:52:28 +0100566
567 if(gr_is_curr_clr_opaque)
568 gl->disable(gl, GGL_BLEND);
569
Dees_Troy51a0e822012-09-05 15:24:24 -0400570 gl->disable(gl, GGL_TEXTURE_2D);
571 gl->recti(gl, x, y, x + w, y + h);
Vojtech Bocek1a4744a2014-02-06 19:52:28 +0100572
573 if(gr_is_curr_clr_opaque)
574 gl->enable(gl, GGL_BLEND);
Dees_Troy51a0e822012-09-05 15:24:24 -0400575}
576
577void gr_blit(gr_surface source, int sx, int sy, int w, int h, int dx, int dy) {
Dees Troyf171e102014-11-06 22:19:58 +0100578 if (gr_context == NULL) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400579 return;
580 }
581
582 GGLContext *gl = gr_context;
Vojtech Bocek6c694b62014-02-06 20:36:25 +0100583 GGLSurface *surface = (GGLSurface*)source;
584
585 if(surface->format == GGL_PIXEL_FORMAT_RGBX_8888)
586 gl->disable(gl, GGL_BLEND);
587
588 gl->bindTexture(gl, surface);
Dees_Troy51a0e822012-09-05 15:24:24 -0400589 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
590 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
591 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
592 gl->enable(gl, GGL_TEXTURE_2D);
593 gl->texCoord2i(gl, sx - dx, sy - dy);
594 gl->recti(gl, dx, dy, dx + w, dy + h);
Vojtech Bocek6c694b62014-02-06 20:36:25 +0100595
596 if(surface->format == GGL_PIXEL_FORMAT_RGBX_8888)
Dees Troyf171e102014-11-06 22:19:58 +0100597 gl->enable(gl, GGL_BLEND);
Dees_Troy51a0e822012-09-05 15:24:24 -0400598}
599
600unsigned int gr_get_width(gr_surface surface) {
601 if (surface == NULL) {
602 return 0;
603 }
604 return ((GGLSurface*) surface)->width;
605}
606
607unsigned int gr_get_height(gr_surface surface) {
608 if (surface == NULL) {
609 return 0;
610 }
611 return ((GGLSurface*) surface)->height;
612}
613
614void* gr_loadFont(const char* fontName)
615{
616 int fd;
617 GRFont *font = 0;
618 GGLSurface *ftex;
619 unsigned char *bits, *rle;
620 unsigned char *in, data;
621 unsigned width, height;
622 unsigned element;
623
624 fd = open(fontName, O_RDONLY);
625 if (fd == -1)
626 {
627 char tmp[128];
628
629 sprintf(tmp, "/res/fonts/%s.dat", fontName);
630 fd = open(tmp, O_RDONLY);
631 if (fd == -1)
632 return NULL;
633 }
634
635 font = calloc(sizeof(*font), 1);
636 ftex = &font->texture;
637
638 read(fd, &width, sizeof(unsigned));
639 read(fd, &height, sizeof(unsigned));
640 read(fd, font->offset, sizeof(unsigned) * 96);
641 font->offset[96] = width;
642
643 bits = malloc(width * height);
644 memset(bits, 0, width * height);
645
646 unsigned pos = 0;
647 while (pos < width * height)
648 {
649 int bit;
650
651 read(fd, &data, 1);
652 for (bit = 0; bit < 8; bit++)
653 {
654 if (data & (1 << (7-bit))) bits[pos++] = 255;
655 else bits[pos++] = 0;
656
657 if (pos == width * height) break;
658 }
659 }
660 close(fd);
661
662 ftex->version = sizeof(*ftex);
663 ftex->width = width;
664 ftex->height = height;
665 ftex->stride = width;
666 ftex->data = (void*) bits;
667 ftex->format = GGL_PIXEL_FORMAT_A_8;
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200668 font->type = FONT_TYPE_TWRP;
Dees_Troy51a0e822012-09-05 15:24:24 -0400669 font->cheight = height;
670 font->ascent = height - 2;
671 return (void*) font;
672}
673
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200674void gr_freeFont(void *font)
675{
676 GRFont *f = font;
677 free(f->texture.data);
678 free(f);
679}
680
681int gr_getMaxFontHeight(void *font)
Dees_Troy51a0e822012-09-05 15:24:24 -0400682{
683 GRFont *fnt = (GRFont*) font;
684
685 if (!fnt) fnt = gr_font;
686 if (!fnt) return -1;
687
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200688#ifndef TW_DISABLE_TTF
689 if(fnt->type == FONT_TYPE_TTF)
690 return gr_ttf_getMaxFontHeight(font);
691#endif
692
693 return fnt->cheight;
Dees_Troy51a0e822012-09-05 15:24:24 -0400694}
695
696static void gr_init_font(void)
697{
698 int fontRes;
699 GGLSurface *ftex;
700 unsigned char *bits, *rle;
701 unsigned char *in, data;
702 unsigned width, height;
703 unsigned element;
704
705 gr_font = calloc(sizeof(*gr_font), 1);
706 ftex = &gr_font->texture;
707
708 width = font.width;
709 height = font.height;
710
711 bits = malloc(width * height);
712 rle = bits;
713
714 in = font.rundata;
715 while((data = *in++))
716 {
717 memset(rle, (data & 0x80) ? 255 : 0, data & 0x7f);
718 rle += (data & 0x7f);
719 }
720 for (element = 0; element < 97; element++)
721 {
722 gr_font->offset[element] = (element * font.cwidth);
723 }
724
725 ftex->version = sizeof(*ftex);
726 ftex->width = width;
727 ftex->height = height;
728 ftex->stride = width;
729 ftex->data = (void*) bits;
730 ftex->format = GGL_PIXEL_FORMAT_A_8;
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200731 gr_font->type = FONT_TYPE_TWRP;
Dees_Troy51a0e822012-09-05 15:24:24 -0400732 gr_font->cheight = height;
733 gr_font->ascent = height - 2;
734 return;
735}
736
737int gr_init(void)
738{
739 gglInit(&gr_context);
740 GGLContext *gl = gr_context;
741
742 gr_init_font();
743 gr_vt_fd = open("/dev/tty0", O_RDWR | O_SYNC);
744 if (gr_vt_fd < 0) {
745 // This is non-fatal; post-Cupcake kernels don't have tty0.
746 } else if (ioctl(gr_vt_fd, KDSETMODE, (void*) KD_GRAPHICS)) {
747 // However, if we do open tty0, we expect the ioctl to work.
748 perror("failed KDSETMODE to KD_GRAPHICS on tty0");
749 gr_exit();
750 return -1;
751 }
752
753 gr_fb_fd = get_framebuffer(gr_framebuffer);
754 if (gr_fb_fd < 0) {
755 perror("Unable to get framebuffer.\n");
756 gr_exit();
757 return -1;
758 }
759
760 get_memory_surface(&gr_mem_surface);
761
762 fprintf(stderr, "framebuffer: fd %d (%d x %d)\n",
763 gr_fb_fd, gr_framebuffer[0].width, gr_framebuffer[0].height);
764
765 /* start with 0 as front (displayed) and 1 as back (drawing) */
766 gr_active_fb = 0;
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600767 if (!has_overlay)
768 set_active_framebuffer(0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400769 gl->colorBuffer(gl, &gr_mem_surface);
770
771 gl->activeTexture(gl, 0);
772 gl->enable(gl, GGL_BLEND);
773 gl->blendFunc(gl, GGL_SRC_ALPHA, GGL_ONE_MINUS_SRC_ALPHA);
774
xNUTxcd56f8c2014-07-23 01:30:20 +0200775#ifdef TW_SCREEN_BLANK_ON_BOOT
776 printf("TW_SCREEN_BLANK_ON_BOOT := true\n");
777 gr_fb_blank(true);
778 gr_fb_blank(false);
779#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400780
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600781 if (!alloc_ion_mem(fi.line_length * vi.yres))
782 allocate_overlay(gr_fb_fd, gr_framebuffer);
783
Dees_Troy51a0e822012-09-05 15:24:24 -0400784 return 0;
785}
786
787void gr_exit(void)
788{
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600789 free_overlay(gr_fb_fd);
790 free_ion_mem();
791
Dees_Troy51a0e822012-09-05 15:24:24 -0400792 close(gr_fb_fd);
793 gr_fb_fd = -1;
794
795 free(gr_mem_surface.data);
796
797 ioctl(gr_vt_fd, KDSETMODE, (void*) KD_TEXT);
798 close(gr_vt_fd);
799 gr_vt_fd = -1;
800}
801
802int gr_fb_width(void)
803{
804 return gr_framebuffer[0].width;
805}
806
807int gr_fb_height(void)
808{
809 return gr_framebuffer[0].height;
810}
811
812gr_pixel *gr_fb_data(void)
813{
814 return (unsigned short *) gr_mem_surface.data;
815}
816
Dees_Troy70237dc2013-02-28 21:31:48 +0000817int gr_fb_blank(int blank)
Dees_Troy51a0e822012-09-05 15:24:24 -0400818{
819 int ret;
Dees Troy05d18c92014-08-04 18:17:07 +0000820 //if (blank)
821 //free_overlay(gr_fb_fd);
Dees_Troy51a0e822012-09-05 15:24:24 -0400822
823 ret = ioctl(gr_fb_fd, FBIOBLANK, blank ? FB_BLANK_POWERDOWN : FB_BLANK_UNBLANK);
824 if (ret < 0)
825 perror("ioctl(): blank");
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600826
Dees Troy05d18c92014-08-04 18:17:07 +0000827 //if (!blank)
828 //allocate_overlay(gr_fb_fd, gr_framebuffer);
829 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -0400830}
831
832int gr_get_surface(gr_surface* surface)
833{
834 GGLSurface* ms = malloc(sizeof(GGLSurface));
835 if (!ms) return -1;
836
837 // Allocate the data
838 get_memory_surface(ms);
839
840 // Now, copy the data
841 memcpy(ms->data, gr_mem_surface.data, vi.xres * vi.yres * vi.bits_per_pixel / 8);
842
843 *surface = (gr_surface*) ms;
844 return 0;
845}
846
847int gr_free_surface(gr_surface surface)
848{
849 if (!surface)
850 return -1;
851
852 GGLSurface* ms = (GGLSurface*) surface;
853 free(ms->data);
854 free(ms);
855 return 0;
856}
857
858void gr_write_frame_to_file(int fd)
859{
860 write(fd, gr_mem_surface.data, vi.xres * vi.yres * vi.bits_per_pixel / 8);
861}