blob: 10d74a7a715355ee108832623ac11b22d3c18600 [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;
Bogdan Seniucd1372f72015-01-23 08:22:39 -0800354 for (i = 0; i < vi.yres; i++) {
355 for (j = 0; j < vi.xres; j++) {
356 memcpy(gr_framebuffer[gr_active_fb].data + (i * vi.xres_virtual + j) * PIXEL_SIZE,
357 gr_mem_surface.data + ((vi.yres - i - 1) * vi.xres_virtual + vi.xres - j - 1) * PIXEL_SIZE, PIXEL_SIZE);
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600358 }
359 }
Bogdan Seniucd1372f72015-01-23 08:22:39 -0800360#else
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);
Bogdan Seniucd1372f72015-01-23 08:22:39 -0800365#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400366
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600367 /* inform the display driver */
368 set_active_framebuffer(gr_active_fb);
369 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400370}
371
372void gr_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
373{
374 GGLContext *gl = gr_context;
375 GGLint color[4];
376 color[0] = ((r << 8) | r) + 1;
377 color[1] = ((g << 8) | g) + 1;
378 color[2] = ((b << 8) | b) + 1;
379 color[3] = ((a << 8) | a) + 1;
380 gl->color4xv(gl, color);
Vojtech Bocek1a4744a2014-02-06 19:52:28 +0100381
382 gr_is_curr_clr_opaque = (a == 255);
Dees_Troy51a0e822012-09-05 15:24:24 -0400383}
384
385int gr_measureEx(const char *s, void* font)
386{
387 GRFont* fnt = (GRFont*) font;
388 int total = 0;
389 unsigned pos;
390 unsigned off;
391
392 if (!fnt) fnt = gr_font;
393
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200394#ifndef TW_DISABLE_TTF
395 if(fnt->type == FONT_TYPE_TTF)
396 return gr_ttf_measureEx(s, font);
397#endif
398
Dees_Troy51a0e822012-09-05 15:24:24 -0400399 while ((off = *s++))
400 {
401 off -= 32;
402 if (off < 96)
403 total += (fnt->offset[off+1] - fnt->offset[off]);
404 }
405 return total;
406}
407
Dees Troy31218ec2014-02-25 20:35:56 +0000408int gr_maxExW(const char *s, void* font, int max_width)
409{
410 GRFont* fnt = (GRFont*) font;
411 int total = 0;
412 unsigned pos;
413 unsigned off;
414
415 if (!fnt) fnt = gr_font;
416
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200417#ifndef TW_DISABLE_TTF
418 if(fnt->type == FONT_TYPE_TTF)
419 return gr_ttf_maxExW(s, font, max_width);
420#endif
421
Dees Troy31218ec2014-02-25 20:35:56 +0000422 while ((off = *s++))
423 {
424 off -= 32;
425 if (off < 96) {
426 max_width -= (fnt->offset[off+1] - fnt->offset[off]);
427 if (max_width > 0) {
428 total++;
429 } else {
430 return total;
431 }
432 }
433 }
434 return total;
435}
436
Dees_Troy51a0e822012-09-05 15:24:24 -0400437int gr_textEx(int x, int y, const char *s, void* pFont)
438{
439 GGLContext *gl = gr_context;
440 GRFont *font = (GRFont*) pFont;
441 unsigned off;
442 unsigned cwidth;
443
444 /* Handle default font */
445 if (!font) font = gr_font;
446
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200447#ifndef TW_DISABLE_TTF
448 if(font->type == FONT_TYPE_TTF)
449 return gr_ttf_textExWH(gl, x, y, s, pFont, -1, -1);
450#endif
451
Dees_Troy51a0e822012-09-05 15:24:24 -0400452 gl->bindTexture(gl, &font->texture);
453 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
454 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
455 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
456 gl->enable(gl, GGL_TEXTURE_2D);
457
458 while((off = *s++)) {
459 off -= 32;
460 cwidth = 0;
461 if (off < 96) {
462 cwidth = font->offset[off+1] - font->offset[off];
463 gl->texCoord2i(gl, (font->offset[off]) - x, 0 - y);
464 gl->recti(gl, x, y, x + cwidth, y + font->cheight);
465 x += cwidth;
466 }
467 }
468
Vojtech Bocek3041c882015-03-06 00:28:21 +0100469 gl->disable(gl, GGL_TEXTURE_2D);
470
Dees_Troy51a0e822012-09-05 15:24:24 -0400471 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
Vojtech Bocek3041c882015-03-06 00:28:21 +0100513 gl->disable(gl, GGL_TEXTURE_2D);
514
Dees_Troy51a0e822012-09-05 15:24:24 -0400515 return x;
516}
517
518int gr_textExWH(int x, int y, const char *s, void* pFont, int max_width, int max_height)
519{
520 GGLContext *gl = gr_context;
521 GRFont *font = (GRFont*) pFont;
522 unsigned off;
523 unsigned cwidth;
524 int rect_x, rect_y;
525
526 /* Handle default font */
527 if (!font) font = gr_font;
528
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200529#ifndef TW_DISABLE_TTF
530 if(font->type == FONT_TYPE_TTF)
531 return gr_ttf_textExWH(gl, x, y, s, pFont, max_width, max_height);
532#endif
533
Dees_Troy51a0e822012-09-05 15:24:24 -0400534 gl->bindTexture(gl, &font->texture);
535 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
536 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
537 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
538 gl->enable(gl, GGL_TEXTURE_2D);
539
540 while((off = *s++)) {
541 off -= 32;
542 cwidth = 0;
543 if (off < 96) {
544 cwidth = font->offset[off+1] - font->offset[off];
545 if ((x + (int)cwidth) < max_width)
546 rect_x = x + cwidth;
547 else
548 rect_x = max_width;
Dees_Troyf7596752012-09-28 13:21:36 -0400549 if (y + font->cheight < (unsigned int)(max_height))
Dees_Troy51a0e822012-09-05 15:24:24 -0400550 rect_y = y + font->cheight;
551 else
552 rect_y = max_height;
553
554 gl->texCoord2i(gl, (font->offset[off]) - x, 0 - y);
555 gl->recti(gl, x, y, rect_x, rect_y);
556 x += cwidth;
557 if (x > max_width)
558 return x;
559 }
560 }
561
Vojtech Bocek3041c882015-03-06 00:28:21 +0100562 gl->disable(gl, GGL_TEXTURE_2D);
563
Dees_Troy51a0e822012-09-05 15:24:24 -0400564 return x;
565}
566
that9876ac32015-02-15 21:40:59 +0100567void gr_clip(int x, int y, int w, int h)
568{
569 GGLContext *gl = gr_context;
570 gl->scissor(gl, x, y, w, h);
571 gl->enable(gl, GGL_SCISSOR_TEST);
572}
573
574void gr_noclip()
575{
576 GGLContext *gl = gr_context;
577 gl->scissor(gl, 0, 0, gr_fb_width(), gr_fb_height());
578 gl->disable(gl, GGL_SCISSOR_TEST);
579}
580
Dees_Troy51a0e822012-09-05 15:24:24 -0400581void gr_fill(int x, int y, int w, int h)
582{
583 GGLContext *gl = gr_context;
Vojtech Bocek1a4744a2014-02-06 19:52:28 +0100584
585 if(gr_is_curr_clr_opaque)
586 gl->disable(gl, GGL_BLEND);
587
Dees_Troy51a0e822012-09-05 15:24:24 -0400588 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
Vojtech Bocekdb378b62015-03-05 20:02:57 +0100594void gr_line(int x0, int y0, int x1, int y1, int width)
595{
596 GGLContext *gl = gr_context;
597
598 if(gr_is_curr_clr_opaque)
599 gl->disable(gl, GGL_BLEND);
600
601 const int coords0[2] = { x0 << 4, y0 << 4 };
602 const int coords1[2] = { x1 << 4, y1 << 4 };
603 gl->linex(gl, coords0, coords1, width << 4);
604
605 if(gr_is_curr_clr_opaque)
606 gl->enable(gl, GGL_BLEND);
607}
608
609gr_surface gr_render_circle(int radius, unsigned char r, unsigned char g, unsigned char b, unsigned char a)
610{
611 int rx, ry;
612 GGLSurface *surface;
613 const int diameter = radius*2 + 1;
614 const int radius_check = radius*radius + radius*0.8;
615 const uint32_t px = (a << 24) | (b << 16) | (g << 8) | r;
616 uint32_t *data;
617
618 surface = malloc(sizeof(GGLSurface));
619 memset(surface, 0, sizeof(GGLSurface));
620
621 data = malloc(diameter * diameter * 4);
622 memset(data, 0, diameter * diameter * 4);
623
624 surface->version = sizeof(surface);
625 surface->width = diameter;
626 surface->height = diameter;
627 surface->stride = diameter;
628 surface->data = (GGLubyte*)data;
629 surface->format = GGL_PIXEL_FORMAT_RGBA_8888;
630
631 for(ry = -radius; ry <= radius; ++ry)
632 for(rx = -radius; rx <= radius; ++rx)
633 if(rx*rx+ry*ry <= radius_check)
634 *(data + diameter*(radius + ry) + (radius+rx)) = px;
635
636 return surface;
637}
638
Dees_Troy51a0e822012-09-05 15:24:24 -0400639void gr_blit(gr_surface source, int sx, int sy, int w, int h, int dx, int dy) {
Dees Troyf171e102014-11-06 22:19:58 +0100640 if (gr_context == NULL) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400641 return;
642 }
643
644 GGLContext *gl = gr_context;
Vojtech Bocek6c694b62014-02-06 20:36:25 +0100645 GGLSurface *surface = (GGLSurface*)source;
646
647 if(surface->format == GGL_PIXEL_FORMAT_RGBX_8888)
648 gl->disable(gl, GGL_BLEND);
649
650 gl->bindTexture(gl, surface);
Dees_Troy51a0e822012-09-05 15:24:24 -0400651 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
652 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
653 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
654 gl->enable(gl, GGL_TEXTURE_2D);
655 gl->texCoord2i(gl, sx - dx, sy - dy);
656 gl->recti(gl, dx, dy, dx + w, dy + h);
Vojtech Bocek3041c882015-03-06 00:28:21 +0100657 gl->disable(gl, GGL_TEXTURE_2D);
Vojtech Bocek6c694b62014-02-06 20:36:25 +0100658
659 if(surface->format == GGL_PIXEL_FORMAT_RGBX_8888)
Dees Troyf171e102014-11-06 22:19:58 +0100660 gl->enable(gl, GGL_BLEND);
Dees_Troy51a0e822012-09-05 15:24:24 -0400661}
662
663unsigned int gr_get_width(gr_surface surface) {
664 if (surface == NULL) {
665 return 0;
666 }
667 return ((GGLSurface*) surface)->width;
668}
669
670unsigned int gr_get_height(gr_surface surface) {
671 if (surface == NULL) {
672 return 0;
673 }
674 return ((GGLSurface*) surface)->height;
675}
676
677void* gr_loadFont(const char* fontName)
678{
679 int fd;
680 GRFont *font = 0;
681 GGLSurface *ftex;
682 unsigned char *bits, *rle;
683 unsigned char *in, data;
684 unsigned width, height;
685 unsigned element;
686
687 fd = open(fontName, O_RDONLY);
688 if (fd == -1)
689 {
690 char tmp[128];
691
Dees Troy3454ade2015-01-20 19:21:04 +0000692 sprintf(tmp, TWRES "fonts/%s.dat", fontName);
Dees_Troy51a0e822012-09-05 15:24:24 -0400693 fd = open(tmp, O_RDONLY);
694 if (fd == -1)
695 return NULL;
696 }
697
698 font = calloc(sizeof(*font), 1);
699 ftex = &font->texture;
700
701 read(fd, &width, sizeof(unsigned));
702 read(fd, &height, sizeof(unsigned));
703 read(fd, font->offset, sizeof(unsigned) * 96);
704 font->offset[96] = width;
705
706 bits = malloc(width * height);
707 memset(bits, 0, width * height);
708
709 unsigned pos = 0;
710 while (pos < width * height)
711 {
712 int bit;
713
714 read(fd, &data, 1);
715 for (bit = 0; bit < 8; bit++)
716 {
717 if (data & (1 << (7-bit))) bits[pos++] = 255;
718 else bits[pos++] = 0;
719
720 if (pos == width * height) break;
721 }
722 }
723 close(fd);
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 font->type = FONT_TYPE_TWRP;
Dees_Troy51a0e822012-09-05 15:24:24 -0400732 font->cheight = height;
733 font->ascent = height - 2;
734 return (void*) font;
735}
736
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200737void gr_freeFont(void *font)
738{
739 GRFont *f = font;
740 free(f->texture.data);
741 free(f);
742}
743
744int gr_getMaxFontHeight(void *font)
Dees_Troy51a0e822012-09-05 15:24:24 -0400745{
746 GRFont *fnt = (GRFont*) font;
747
748 if (!fnt) fnt = gr_font;
749 if (!fnt) return -1;
750
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200751#ifndef TW_DISABLE_TTF
752 if(fnt->type == FONT_TYPE_TTF)
753 return gr_ttf_getMaxFontHeight(font);
754#endif
755
756 return fnt->cheight;
Dees_Troy51a0e822012-09-05 15:24:24 -0400757}
758
759static void gr_init_font(void)
760{
761 int fontRes;
762 GGLSurface *ftex;
763 unsigned char *bits, *rle;
764 unsigned char *in, data;
765 unsigned width, height;
766 unsigned element;
767
768 gr_font = calloc(sizeof(*gr_font), 1);
769 ftex = &gr_font->texture;
770
771 width = font.width;
772 height = font.height;
773
774 bits = malloc(width * height);
775 rle = bits;
776
777 in = font.rundata;
778 while((data = *in++))
779 {
780 memset(rle, (data & 0x80) ? 255 : 0, data & 0x7f);
781 rle += (data & 0x7f);
782 }
783 for (element = 0; element < 97; element++)
784 {
785 gr_font->offset[element] = (element * font.cwidth);
786 }
787
788 ftex->version = sizeof(*ftex);
789 ftex->width = width;
790 ftex->height = height;
791 ftex->stride = width;
792 ftex->data = (void*) bits;
793 ftex->format = GGL_PIXEL_FORMAT_A_8;
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200794 gr_font->type = FONT_TYPE_TWRP;
Dees_Troy51a0e822012-09-05 15:24:24 -0400795 gr_font->cheight = height;
796 gr_font->ascent = height - 2;
797 return;
798}
799
800int gr_init(void)
801{
802 gglInit(&gr_context);
803 GGLContext *gl = gr_context;
804
805 gr_init_font();
806 gr_vt_fd = open("/dev/tty0", O_RDWR | O_SYNC);
807 if (gr_vt_fd < 0) {
808 // This is non-fatal; post-Cupcake kernels don't have tty0.
809 } else if (ioctl(gr_vt_fd, KDSETMODE, (void*) KD_GRAPHICS)) {
810 // However, if we do open tty0, we expect the ioctl to work.
811 perror("failed KDSETMODE to KD_GRAPHICS on tty0");
812 gr_exit();
813 return -1;
814 }
815
816 gr_fb_fd = get_framebuffer(gr_framebuffer);
817 if (gr_fb_fd < 0) {
818 perror("Unable to get framebuffer.\n");
819 gr_exit();
820 return -1;
821 }
822
823 get_memory_surface(&gr_mem_surface);
824
825 fprintf(stderr, "framebuffer: fd %d (%d x %d)\n",
826 gr_fb_fd, gr_framebuffer[0].width, gr_framebuffer[0].height);
827
828 /* start with 0 as front (displayed) and 1 as back (drawing) */
829 gr_active_fb = 0;
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600830 if (!has_overlay)
831 set_active_framebuffer(0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400832 gl->colorBuffer(gl, &gr_mem_surface);
833
834 gl->activeTexture(gl, 0);
835 gl->enable(gl, GGL_BLEND);
836 gl->blendFunc(gl, GGL_SRC_ALPHA, GGL_ONE_MINUS_SRC_ALPHA);
837
xNUTxcd56f8c2014-07-23 01:30:20 +0200838#ifdef TW_SCREEN_BLANK_ON_BOOT
839 printf("TW_SCREEN_BLANK_ON_BOOT := true\n");
840 gr_fb_blank(true);
841 gr_fb_blank(false);
842#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400843
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600844 if (!alloc_ion_mem(fi.line_length * vi.yres))
845 allocate_overlay(gr_fb_fd, gr_framebuffer);
846
Dees_Troy51a0e822012-09-05 15:24:24 -0400847 return 0;
848}
849
850void gr_exit(void)
851{
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600852 free_overlay(gr_fb_fd);
853 free_ion_mem();
854
Dees_Troy51a0e822012-09-05 15:24:24 -0400855 close(gr_fb_fd);
856 gr_fb_fd = -1;
857
858 free(gr_mem_surface.data);
859
860 ioctl(gr_vt_fd, KDSETMODE, (void*) KD_TEXT);
861 close(gr_vt_fd);
862 gr_vt_fd = -1;
863}
864
865int gr_fb_width(void)
866{
867 return gr_framebuffer[0].width;
868}
869
870int gr_fb_height(void)
871{
872 return gr_framebuffer[0].height;
873}
874
875gr_pixel *gr_fb_data(void)
876{
877 return (unsigned short *) gr_mem_surface.data;
878}
879
Dees_Troy70237dc2013-02-28 21:31:48 +0000880int gr_fb_blank(int blank)
Dees_Troy51a0e822012-09-05 15:24:24 -0400881{
882 int ret;
Dees Troy05d18c92014-08-04 18:17:07 +0000883 //if (blank)
884 //free_overlay(gr_fb_fd);
Dees_Troy51a0e822012-09-05 15:24:24 -0400885
886 ret = ioctl(gr_fb_fd, FBIOBLANK, blank ? FB_BLANK_POWERDOWN : FB_BLANK_UNBLANK);
887 if (ret < 0)
888 perror("ioctl(): blank");
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600889
Dees Troy05d18c92014-08-04 18:17:07 +0000890 //if (!blank)
891 //allocate_overlay(gr_fb_fd, gr_framebuffer);
892 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -0400893}
894
895int gr_get_surface(gr_surface* surface)
896{
897 GGLSurface* ms = malloc(sizeof(GGLSurface));
898 if (!ms) return -1;
899
900 // Allocate the data
901 get_memory_surface(ms);
902
903 // Now, copy the data
904 memcpy(ms->data, gr_mem_surface.data, vi.xres * vi.yres * vi.bits_per_pixel / 8);
905
906 *surface = (gr_surface*) ms;
907 return 0;
908}
909
910int gr_free_surface(gr_surface surface)
911{
912 if (!surface)
913 return -1;
914
915 GGLSurface* ms = (GGLSurface*) surface;
916 free(ms->data);
917 free(ms);
918 return 0;
919}
920
921void gr_write_frame_to_file(int fd)
922{
923 write(fd, gr_mem_surface.data, vi.xres * vi.yres * vi.bits_per_pixel / 8);
924}