blob: a3da6cbeadc37da82d0330f2dc6153fa390ffb1f [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>
Ethan Yonkerc798c9c2015-10-09 11:15:26 -050024#include <string.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040025
26#include <sys/ioctl.h>
27#include <sys/mman.h>
28#include <sys/types.h>
29
30#include <linux/fb.h>
31#include <linux/kd.h>
32
33#include <pixelflinger/pixelflinger.h>
34
35#include "minui.h"
36
37#ifdef BOARD_USE_CUSTOM_RECOVERY_FONT
38#include BOARD_USE_CUSTOM_RECOVERY_FONT
39#else
40#include "font_10x18.h"
41#endif
42
43#ifdef RECOVERY_BGRA
44#define PIXEL_FORMAT GGL_PIXEL_FORMAT_BGRA_8888
45#define PIXEL_SIZE 4
46#endif
Kra1o577568592015-10-14 18:09:54 +020047#ifdef RECOVERY_RGBA
48#define PIXEL_FORMAT GGL_PIXEL_FORMAT_RGBA_8888
49#define PIXEL_SIZE 4
50#endif
Dees_Troy51a0e822012-09-05 15:24:24 -040051#ifdef RECOVERY_RGBX
52#define PIXEL_FORMAT GGL_PIXEL_FORMAT_RGBX_8888
53#define PIXEL_SIZE 4
54#endif
55#ifndef PIXEL_FORMAT
56#define PIXEL_FORMAT GGL_PIXEL_FORMAT_RGB_565
57#define PIXEL_SIZE 2
58#endif
59
Hiemanshu Sharmaacf6a9b2012-11-21 11:28:36 -060060#define NUM_BUFFERS 2
Ethan Yonker4ee5ad72014-02-18 18:41:17 -060061#define MAX_DISPLAY_DIM 2048
Hiemanshu Sharmaacf6a9b2012-11-21 11:28:36 -060062
Dees_Troy51a0e822012-09-05 15:24:24 -040063// #define PRINT_SCREENINFO 1 // Enables printing of screen info to log
64
65typedef struct {
Vojtech Bocek76ee9032014-09-07 15:01:56 +020066 int type;
Dees_Troy51a0e822012-09-05 15:24:24 -040067 GGLSurface texture;
68 unsigned offset[97];
69 unsigned cheight;
70 unsigned ascent;
71} GRFont;
72
73static GRFont *gr_font = 0;
74static GGLContext *gr_context = 0;
75static GGLSurface gr_font_texture;
Hiemanshu Sharmaacf6a9b2012-11-21 11:28:36 -060076static GGLSurface gr_framebuffer[NUM_BUFFERS];
Vojtech Bocek03fd6c52014-03-13 18:46:34 +010077GGLSurface gr_mem_surface;
Dees_Troy51a0e822012-09-05 15:24:24 -040078static unsigned gr_active_fb = 0;
Hiemanshu Sharma94f6c882012-11-21 11:25:22 -060079static unsigned double_buffering = 0;
Vojtech Bocek1a4744a2014-02-06 19:52:28 +010080static int gr_is_curr_clr_opaque = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -040081
82static int gr_fb_fd = -1;
83static int gr_vt_fd = -1;
84
Vojtech Bocek03fd6c52014-03-13 18:46:34 +010085struct fb_var_screeninfo vi;
Dees_Troy51a0e822012-09-05 15:24:24 -040086static struct fb_fix_screeninfo fi;
87
Ethan Yonker4ee5ad72014-02-18 18:41:17 -060088static bool has_overlay = false;
89static int leftSplit = 0;
90static int rightSplit = 0;
91
92bool target_has_overlay(char *version);
93int free_ion_mem(void);
94int alloc_ion_mem(unsigned int size);
95int allocate_overlay(int fd, GGLSurface gr_fb[]);
96int free_overlay(int fd);
97int overlay_display_frame(int fd, GGLubyte* data, size_t size);
98
Dees_Troy51a0e822012-09-05 15:24:24 -040099#ifdef PRINT_SCREENINFO
100static void print_fb_var_screeninfo()
101{
Dees_Troy2673cec2013-04-02 20:22:16 +0000102 printf("vi.xres: %d\n", vi.xres);
103 printf("vi.yres: %d\n", vi.yres);
104 printf("vi.xres_virtual: %d\n", vi.xres_virtual);
105 printf("vi.yres_virtual: %d\n", vi.yres_virtual);
106 printf("vi.xoffset: %d\n", vi.xoffset);
107 printf("vi.yoffset: %d\n", vi.yoffset);
108 printf("vi.bits_per_pixel: %d\n", vi.bits_per_pixel);
109 printf("vi.grayscale: %d\n", vi.grayscale);
Dees_Troy51a0e822012-09-05 15:24:24 -0400110}
111#endif
112
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600113#ifdef MSM_BSP
114int getLeftSplit(void) {
115 //Default even split for all displays with high res
116 int lSplit = vi.xres / 2;
117
118 //Override if split published by driver
119 if (leftSplit)
120 lSplit = leftSplit;
121
122 return lSplit;
123}
124
125int getRightSplit(void) {
126 return rightSplit;
127}
128
129
130void setDisplaySplit(void) {
131 char split[64] = {0};
132 FILE* fp = fopen("/sys/class/graphics/fb0/msm_fb_split", "r");
133 if (fp) {
134 //Format "left right" space as delimiter
135 if(fread(split, sizeof(char), 64, fp)) {
136 leftSplit = atoi(split);
137 printf("Left Split=%d\n",leftSplit);
138 char *rght = strpbrk(split, " ");
139 if (rght)
140 rightSplit = atoi(rght + 1);
141 printf("Right Split=%d\n", rightSplit);
142 }
143 } else {
144 printf("Failed to open mdss_fb_split node\n");
145 }
146 if (fp)
147 fclose(fp);
148}
149
150bool isDisplaySplit(void) {
151 if (vi.xres > MAX_DISPLAY_DIM)
152 return true;
153 //check if right split is set by driver
154 if (getRightSplit())
155 return true;
156
157 return false;
158}
159
160int getFbXres(void) {
161 return vi.xres;
162}
163
164int getFbYres (void) {
165 return vi.yres;
166}
167#endif // MSM_BSP
168
Dees_Troy51a0e822012-09-05 15:24:24 -0400169static int get_framebuffer(GGLSurface *fb)
170{
Ethan Yonker4f6a9762015-03-09 13:58:54 -0500171 int fd, index = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400172 void *bits;
173
174 fd = open("/dev/graphics/fb0", O_RDWR);
jenkins79699132015-05-08 06:04:44 -0400175
176 while (fd < 0 && index < 30) {
Ethan Yonker4f6a9762015-03-09 13:58:54 -0500177 usleep(1000);
178 fd = open("/dev/graphics/fb0", O_RDWR);
179 index++;
180 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400181 if (fd < 0) {
Ethan Yonker4f6a9762015-03-09 13:58:54 -0500182 perror("cannot open fb0\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400183 return -1;
184 }
185
186 if (ioctl(fd, FBIOGET_VSCREENINFO, &vi) < 0) {
187 perror("failed to get fb0 info");
188 close(fd);
189 return -1;
190 }
191
192 fprintf(stderr, "Pixel format: %dx%d @ %dbpp\n", vi.xres, vi.yres, vi.bits_per_pixel);
193
194 vi.bits_per_pixel = PIXEL_SIZE * 8;
195 if (PIXEL_FORMAT == GGL_PIXEL_FORMAT_BGRA_8888) {
196 fprintf(stderr, "Pixel format: BGRA_8888\n");
197 if (PIXEL_SIZE != 4) fprintf(stderr, "E: Pixel Size mismatch!\n");
198 vi.red.offset = 8;
199 vi.red.length = 8;
200 vi.green.offset = 16;
201 vi.green.length = 8;
202 vi.blue.offset = 24;
203 vi.blue.length = 8;
204 vi.transp.offset = 0;
205 vi.transp.length = 8;
Kra1o577568592015-10-14 18:09:54 +0200206 } else if (PIXEL_FORMAT == GGL_PIXEL_FORMAT_RGBA_8888) {
207 fprintf(stderr, "Pixel format: RGBA_8888\n");
208 if (PIXEL_SIZE != 4) fprintf(stderr, "E: Pixel Size mismatch!\n");
209 vi.red.offset = 0;
210 vi.red.length = 8;
211 vi.green.offset = 8;
212 vi.green.length = 8;
213 vi.blue.offset = 16;
214 vi.blue.length = 8;
215 vi.transp.offset = 24;
216 vi.transp.length = 8;
Dees_Troy51a0e822012-09-05 15:24:24 -0400217 } else if (PIXEL_FORMAT == GGL_PIXEL_FORMAT_RGBX_8888) {
218 fprintf(stderr, "Pixel format: RGBX_8888\n");
219 if (PIXEL_SIZE != 4) fprintf(stderr, "E: Pixel Size mismatch!\n");
220 vi.red.offset = 24;
221 vi.red.length = 8;
222 vi.green.offset = 16;
223 vi.green.length = 8;
224 vi.blue.offset = 8;
225 vi.blue.length = 8;
226 vi.transp.offset = 0;
227 vi.transp.length = 8;
228 } else if (PIXEL_FORMAT == GGL_PIXEL_FORMAT_RGB_565) {
229#ifdef RECOVERY_RGB_565
230 fprintf(stderr, "Pixel format: RGB_565\n");
231 vi.blue.offset = 0;
232 vi.green.offset = 5;
233 vi.red.offset = 11;
234#else
235 fprintf(stderr, "Pixel format: BGR_565\n");
236 vi.blue.offset = 11;
237 vi.green.offset = 5;
238 vi.red.offset = 0;
239#endif
240 if (PIXEL_SIZE != 2) fprintf(stderr, "E: Pixel Size mismatch!\n");
241 vi.blue.length = 5;
242 vi.green.length = 6;
243 vi.red.length = 5;
244 vi.blue.msb_right = 0;
245 vi.green.msb_right = 0;
246 vi.red.msb_right = 0;
247 vi.transp.offset = 0;
248 vi.transp.length = 0;
249 }
250 else
251 {
252 perror("unknown pixel format");
253 close(fd);
254 return -1;
255 }
256
257 vi.vmode = FB_VMODE_NONINTERLACED;
258 vi.activate = FB_ACTIVATE_NOW | FB_ACTIVATE_FORCE;
259
260 if (ioctl(fd, FBIOPUT_VSCREENINFO, &vi) < 0) {
261 perror("failed to put fb0 info");
262 close(fd);
263 return -1;
264 }
265
266 if (ioctl(fd, FBIOGET_FSCREENINFO, &fi) < 0) {
267 perror("failed to get fb0 info");
268 close(fd);
269 return -1;
270 }
271
Dees Troyb0425382014-04-03 00:10:03 +0000272#ifdef MSM_BSP
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600273 has_overlay = target_has_overlay(fi.id);
274
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600275 if (isTargetMdp5())
276 setDisplaySplit();
Dees Troyb0425382014-04-03 00:10:03 +0000277#else
278 has_overlay = false;
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600279#endif
280
281 if (!has_overlay) {
282 printf("Not using qualcomm overlay, '%s'\n", fi.id);
283 bits = mmap(0, fi.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
284 if (bits == MAP_FAILED) {
285 perror("failed to mmap framebuffer");
286 close(fd);
287 return -1;
288 }
289 } else {
290 printf("Using qualcomm overlay\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400291 }
292
293#ifdef RECOVERY_GRAPHICS_USE_LINELENGTH
294 vi.xres_virtual = fi.line_length / PIXEL_SIZE;
295#endif
296
297 fb->version = sizeof(*fb);
298 fb->width = vi.xres;
299 fb->height = vi.yres;
300#ifdef BOARD_HAS_JANKY_BACKBUFFER
Dees_Troy2673cec2013-04-02 20:22:16 +0000301 printf("setting JANKY BACKBUFFER\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400302 fb->stride = fi.line_length/2;
303#else
304 fb->stride = vi.xres_virtual;
305#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400306 fb->format = PIXEL_FORMAT;
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600307 if (!has_overlay) {
308 fb->data = bits;
309 memset(fb->data, 0, vi.yres * fb->stride * PIXEL_SIZE);
310 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400311
312 fb++;
313
Kra1o5c9556cc2015-06-24 12:19:09 +0200314#ifndef TW_DISABLE_DOUBLE_BUFFERING
Hiemanshu Sharma94f6c882012-11-21 11:25:22 -0600315 /* check if we can use double buffering */
316 if (vi.yres * fi.line_length * 2 > fi.smem_len)
Kra1o5c9556cc2015-06-24 12:19:09 +0200317#else
318 printf("TW_DISABLE_DOUBLE_BUFFERING := true\n");
319#endif
Hiemanshu Sharma94f6c882012-11-21 11:25:22 -0600320 return fd;
321
322 double_buffering = 1;
323
Dees_Troy51a0e822012-09-05 15:24:24 -0400324 fb->version = sizeof(*fb);
325 fb->width = vi.xres;
326 fb->height = vi.yres;
327#ifdef BOARD_HAS_JANKY_BACKBUFFER
328 fb->stride = fi.line_length/2;
Ethan Yonkerbcc502c2014-11-10 11:22:10 -0600329 fb->data = (GGLubyte*) (((unsigned long) bits) + vi.yres * fi.line_length);
Dees_Troy51a0e822012-09-05 15:24:24 -0400330#else
331 fb->stride = vi.xres_virtual;
Ethan Yonkerbcc502c2014-11-10 11:22:10 -0600332 fb->data = (GGLubyte*) (((unsigned long) bits) + vi.yres * fb->stride * PIXEL_SIZE);
Dees_Troy51a0e822012-09-05 15:24:24 -0400333#endif
334 fb->format = PIXEL_FORMAT;
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600335 if (!has_overlay) {
336 memset(fb->data, 0, vi.yres * fb->stride * PIXEL_SIZE);
337 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400338
339#ifdef PRINT_SCREENINFO
340 print_fb_var_screeninfo();
341#endif
342
343 return fd;
344}
345
346static void get_memory_surface(GGLSurface* ms) {
347 ms->version = sizeof(*ms);
348 ms->width = vi.xres;
349 ms->height = vi.yres;
350 ms->stride = vi.xres_virtual;
351 ms->data = malloc(vi.xres_virtual * vi.yres * PIXEL_SIZE);
352 ms->format = PIXEL_FORMAT;
353}
354
355static void set_active_framebuffer(unsigned n)
356{
Hiemanshu Sharma94f6c882012-11-21 11:25:22 -0600357 if (n > 1 || !double_buffering) return;
Hiemanshu Sharmaacf6a9b2012-11-21 11:28:36 -0600358 vi.yres_virtual = vi.yres * NUM_BUFFERS;
Dees_Troy51a0e822012-09-05 15:24:24 -0400359 vi.yoffset = n * vi.yres;
360// vi.bits_per_pixel = PIXEL_SIZE * 8;
361 if (ioctl(gr_fb_fd, FBIOPUT_VSCREENINFO, &vi) < 0) {
362 perror("active fb swap failed");
363 }
364}
365
366void gr_flip(void)
367{
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600368 if (-EINVAL == overlay_display_frame(gr_fb_fd, gr_mem_surface.data,
369 (fi.line_length * vi.yres))) {
370 GGLContext *gl = gr_context;
Dees_Troy51a0e822012-09-05 15:24:24 -0400371
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600372 /* swap front and back buffers */
373 if (double_buffering)
374 gr_active_fb = (gr_active_fb + 1) & 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400375
376#ifdef BOARD_HAS_FLIPPED_SCREEN
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600377 /* flip buffer 180 degrees for devices with physicaly inverted screens */
378 unsigned int i;
379 unsigned int j;
Bogdan Seniucd1372f72015-01-23 08:22:39 -0800380 for (i = 0; i < vi.yres; i++) {
381 for (j = 0; j < vi.xres; j++) {
382 memcpy(gr_framebuffer[gr_active_fb].data + (i * vi.xres_virtual + j) * PIXEL_SIZE,
383 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 -0600384 }
385 }
Bogdan Seniucd1372f72015-01-23 08:22:39 -0800386#else
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600387 /* copy data from the in-memory surface to the buffer we're about
388 * to make active. */
389 memcpy(gr_framebuffer[gr_active_fb].data, gr_mem_surface.data,
390 vi.xres_virtual * vi.yres * PIXEL_SIZE);
Bogdan Seniucd1372f72015-01-23 08:22:39 -0800391#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400392
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600393 /* inform the display driver */
394 set_active_framebuffer(gr_active_fb);
395 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400396}
397
398void gr_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
399{
400 GGLContext *gl = gr_context;
401 GGLint color[4];
402 color[0] = ((r << 8) | r) + 1;
403 color[1] = ((g << 8) | g) + 1;
404 color[2] = ((b << 8) | b) + 1;
405 color[3] = ((a << 8) | a) + 1;
406 gl->color4xv(gl, color);
Vojtech Bocek1a4744a2014-02-06 19:52:28 +0100407
408 gr_is_curr_clr_opaque = (a == 255);
Dees_Troy51a0e822012-09-05 15:24:24 -0400409}
410
411int gr_measureEx(const char *s, void* font)
412{
413 GRFont* fnt = (GRFont*) font;
414 int total = 0;
415 unsigned pos;
416 unsigned off;
417
418 if (!fnt) fnt = gr_font;
419
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200420#ifndef TW_DISABLE_TTF
421 if(fnt->type == FONT_TYPE_TTF)
422 return gr_ttf_measureEx(s, font);
423#endif
424
Dees_Troy51a0e822012-09-05 15:24:24 -0400425 while ((off = *s++))
426 {
427 off -= 32;
428 if (off < 96)
429 total += (fnt->offset[off+1] - fnt->offset[off]);
430 }
431 return total;
432}
433
Dees Troy31218ec2014-02-25 20:35:56 +0000434int gr_maxExW(const char *s, void* font, int max_width)
435{
436 GRFont* fnt = (GRFont*) font;
437 int total = 0;
438 unsigned pos;
439 unsigned off;
440
441 if (!fnt) fnt = gr_font;
442
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200443#ifndef TW_DISABLE_TTF
444 if(fnt->type == FONT_TYPE_TTF)
445 return gr_ttf_maxExW(s, font, max_width);
446#endif
447
Dees Troy31218ec2014-02-25 20:35:56 +0000448 while ((off = *s++))
449 {
450 off -= 32;
451 if (off < 96) {
452 max_width -= (fnt->offset[off+1] - fnt->offset[off]);
453 if (max_width > 0) {
454 total++;
455 } else {
456 return total;
457 }
458 }
459 }
460 return total;
461}
462
Dees_Troy51a0e822012-09-05 15:24:24 -0400463int gr_textEx(int x, int y, const char *s, void* pFont)
464{
465 GGLContext *gl = gr_context;
466 GRFont *font = (GRFont*) pFont;
467 unsigned off;
468 unsigned cwidth;
469
470 /* Handle default font */
471 if (!font) font = gr_font;
472
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200473#ifndef TW_DISABLE_TTF
474 if(font->type == FONT_TYPE_TTF)
475 return gr_ttf_textExWH(gl, x, y, s, pFont, -1, -1);
476#endif
477
Dees_Troy51a0e822012-09-05 15:24:24 -0400478 gl->bindTexture(gl, &font->texture);
479 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
480 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
481 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
482 gl->enable(gl, GGL_TEXTURE_2D);
483
484 while((off = *s++)) {
485 off -= 32;
486 cwidth = 0;
487 if (off < 96) {
488 cwidth = font->offset[off+1] - font->offset[off];
489 gl->texCoord2i(gl, (font->offset[off]) - x, 0 - y);
490 gl->recti(gl, x, y, x + cwidth, y + font->cheight);
491 x += cwidth;
492 }
493 }
494
Vojtech Bocek3041c882015-03-06 00:28:21 +0100495 gl->disable(gl, GGL_TEXTURE_2D);
496
Dees_Troy51a0e822012-09-05 15:24:24 -0400497 return x;
498}
499
500int gr_textExW(int x, int y, const char *s, void* pFont, int max_width)
501{
502 GGLContext *gl = gr_context;
503 GRFont *font = (GRFont*) pFont;
504 unsigned off;
505 unsigned cwidth;
506
507 /* Handle default font */
508 if (!font) font = gr_font;
509
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200510#ifndef TW_DISABLE_TTF
511 if(font->type == FONT_TYPE_TTF)
512 return gr_ttf_textExWH(gl, x, y, s, pFont, max_width, -1);
513#endif
514
Dees_Troy51a0e822012-09-05 15:24:24 -0400515 gl->bindTexture(gl, &font->texture);
516 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
517 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
518 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
519 gl->enable(gl, GGL_TEXTURE_2D);
520
521 while((off = *s++)) {
522 off -= 32;
523 cwidth = 0;
524 if (off < 96) {
525 cwidth = font->offset[off+1] - font->offset[off];
526 if ((x + (int)cwidth) < max_width) {
527 gl->texCoord2i(gl, (font->offset[off]) - x, 0 - y);
528 gl->recti(gl, x, y, x + cwidth, y + font->cheight);
529 x += cwidth;
530 } else {
531 gl->texCoord2i(gl, (font->offset[off]) - x, 0 - y);
532 gl->recti(gl, x, y, max_width, y + font->cheight);
533 x = max_width;
534 return x;
535 }
536 }
537 }
538
Vojtech Bocek3041c882015-03-06 00:28:21 +0100539 gl->disable(gl, GGL_TEXTURE_2D);
540
Dees_Troy51a0e822012-09-05 15:24:24 -0400541 return x;
542}
543
544int gr_textExWH(int x, int y, const char *s, void* pFont, int max_width, int max_height)
545{
546 GGLContext *gl = gr_context;
547 GRFont *font = (GRFont*) pFont;
548 unsigned off;
549 unsigned cwidth;
550 int rect_x, rect_y;
551
552 /* Handle default font */
553 if (!font) font = gr_font;
554
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200555#ifndef TW_DISABLE_TTF
556 if(font->type == FONT_TYPE_TTF)
557 return gr_ttf_textExWH(gl, x, y, s, pFont, max_width, max_height);
558#endif
559
Dees_Troy51a0e822012-09-05 15:24:24 -0400560 gl->bindTexture(gl, &font->texture);
561 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
562 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
563 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
564 gl->enable(gl, GGL_TEXTURE_2D);
565
566 while((off = *s++)) {
567 off -= 32;
568 cwidth = 0;
569 if (off < 96) {
570 cwidth = font->offset[off+1] - font->offset[off];
571 if ((x + (int)cwidth) < max_width)
572 rect_x = x + cwidth;
573 else
574 rect_x = max_width;
Dees_Troyf7596752012-09-28 13:21:36 -0400575 if (y + font->cheight < (unsigned int)(max_height))
Dees_Troy51a0e822012-09-05 15:24:24 -0400576 rect_y = y + font->cheight;
577 else
578 rect_y = max_height;
579
580 gl->texCoord2i(gl, (font->offset[off]) - x, 0 - y);
581 gl->recti(gl, x, y, rect_x, rect_y);
582 x += cwidth;
583 if (x > max_width)
584 return x;
585 }
586 }
587
Vojtech Bocek3041c882015-03-06 00:28:21 +0100588 gl->disable(gl, GGL_TEXTURE_2D);
589
Dees_Troy51a0e822012-09-05 15:24:24 -0400590 return x;
591}
592
that9876ac32015-02-15 21:40:59 +0100593void gr_clip(int x, int y, int w, int h)
594{
595 GGLContext *gl = gr_context;
596 gl->scissor(gl, x, y, w, h);
597 gl->enable(gl, GGL_SCISSOR_TEST);
598}
599
600void gr_noclip()
601{
602 GGLContext *gl = gr_context;
603 gl->scissor(gl, 0, 0, gr_fb_width(), gr_fb_height());
604 gl->disable(gl, GGL_SCISSOR_TEST);
605}
606
Dees_Troy51a0e822012-09-05 15:24:24 -0400607void gr_fill(int x, int y, int w, int h)
608{
609 GGLContext *gl = gr_context;
Vojtech Bocek1a4744a2014-02-06 19:52:28 +0100610
611 if(gr_is_curr_clr_opaque)
612 gl->disable(gl, GGL_BLEND);
613
Dees_Troy51a0e822012-09-05 15:24:24 -0400614 gl->recti(gl, x, y, x + w, y + h);
Vojtech Bocek1a4744a2014-02-06 19:52:28 +0100615
616 if(gr_is_curr_clr_opaque)
617 gl->enable(gl, GGL_BLEND);
Dees_Troy51a0e822012-09-05 15:24:24 -0400618}
619
Vojtech Bocekdb378b62015-03-05 20:02:57 +0100620void gr_line(int x0, int y0, int x1, int y1, int width)
621{
622 GGLContext *gl = gr_context;
623
624 if(gr_is_curr_clr_opaque)
625 gl->disable(gl, GGL_BLEND);
626
627 const int coords0[2] = { x0 << 4, y0 << 4 };
628 const int coords1[2] = { x1 << 4, y1 << 4 };
629 gl->linex(gl, coords0, coords1, width << 4);
630
631 if(gr_is_curr_clr_opaque)
632 gl->enable(gl, GGL_BLEND);
633}
634
635gr_surface gr_render_circle(int radius, unsigned char r, unsigned char g, unsigned char b, unsigned char a)
636{
637 int rx, ry;
638 GGLSurface *surface;
639 const int diameter = radius*2 + 1;
640 const int radius_check = radius*radius + radius*0.8;
641 const uint32_t px = (a << 24) | (b << 16) | (g << 8) | r;
642 uint32_t *data;
643
644 surface = malloc(sizeof(GGLSurface));
645 memset(surface, 0, sizeof(GGLSurface));
646
647 data = malloc(diameter * diameter * 4);
648 memset(data, 0, diameter * diameter * 4);
649
650 surface->version = sizeof(surface);
651 surface->width = diameter;
652 surface->height = diameter;
653 surface->stride = diameter;
654 surface->data = (GGLubyte*)data;
655 surface->format = GGL_PIXEL_FORMAT_RGBA_8888;
656
657 for(ry = -radius; ry <= radius; ++ry)
658 for(rx = -radius; rx <= radius; ++rx)
659 if(rx*rx+ry*ry <= radius_check)
660 *(data + diameter*(radius + ry) + (radius+rx)) = px;
661
662 return surface;
663}
664
Dees_Troy51a0e822012-09-05 15:24:24 -0400665void gr_blit(gr_surface source, int sx, int sy, int w, int h, int dx, int dy) {
Dees Troyf171e102014-11-06 22:19:58 +0100666 if (gr_context == NULL) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400667 return;
668 }
669
670 GGLContext *gl = gr_context;
Vojtech Bocek6c694b62014-02-06 20:36:25 +0100671 GGLSurface *surface = (GGLSurface*)source;
672
673 if(surface->format == GGL_PIXEL_FORMAT_RGBX_8888)
674 gl->disable(gl, GGL_BLEND);
675
676 gl->bindTexture(gl, surface);
Dees_Troy51a0e822012-09-05 15:24:24 -0400677 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
678 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
679 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
680 gl->enable(gl, GGL_TEXTURE_2D);
681 gl->texCoord2i(gl, sx - dx, sy - dy);
682 gl->recti(gl, dx, dy, dx + w, dy + h);
Vojtech Bocek3041c882015-03-06 00:28:21 +0100683 gl->disable(gl, GGL_TEXTURE_2D);
Vojtech Bocek6c694b62014-02-06 20:36:25 +0100684
685 if(surface->format == GGL_PIXEL_FORMAT_RGBX_8888)
Dees Troyf171e102014-11-06 22:19:58 +0100686 gl->enable(gl, GGL_BLEND);
Dees_Troy51a0e822012-09-05 15:24:24 -0400687}
688
689unsigned int gr_get_width(gr_surface surface) {
690 if (surface == NULL) {
691 return 0;
692 }
693 return ((GGLSurface*) surface)->width;
694}
695
696unsigned int gr_get_height(gr_surface surface) {
697 if (surface == NULL) {
698 return 0;
699 }
700 return ((GGLSurface*) surface)->height;
701}
702
703void* gr_loadFont(const char* fontName)
704{
705 int fd;
706 GRFont *font = 0;
707 GGLSurface *ftex;
708 unsigned char *bits, *rle;
709 unsigned char *in, data;
710 unsigned width, height;
711 unsigned element;
712
713 fd = open(fontName, O_RDONLY);
714 if (fd == -1)
715 {
716 char tmp[128];
717
Dees Troy3454ade2015-01-20 19:21:04 +0000718 sprintf(tmp, TWRES "fonts/%s.dat", fontName);
Dees_Troy51a0e822012-09-05 15:24:24 -0400719 fd = open(tmp, O_RDONLY);
720 if (fd == -1)
721 return NULL;
722 }
723
724 font = calloc(sizeof(*font), 1);
725 ftex = &font->texture;
726
727 read(fd, &width, sizeof(unsigned));
728 read(fd, &height, sizeof(unsigned));
729 read(fd, font->offset, sizeof(unsigned) * 96);
730 font->offset[96] = width;
731
732 bits = malloc(width * height);
733 memset(bits, 0, width * height);
734
735 unsigned pos = 0;
736 while (pos < width * height)
737 {
738 int bit;
739
740 read(fd, &data, 1);
741 for (bit = 0; bit < 8; bit++)
742 {
743 if (data & (1 << (7-bit))) bits[pos++] = 255;
744 else bits[pos++] = 0;
745
746 if (pos == width * height) break;
747 }
748 }
749 close(fd);
750
751 ftex->version = sizeof(*ftex);
752 ftex->width = width;
753 ftex->height = height;
754 ftex->stride = width;
755 ftex->data = (void*) bits;
756 ftex->format = GGL_PIXEL_FORMAT_A_8;
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200757 font->type = FONT_TYPE_TWRP;
Dees_Troy51a0e822012-09-05 15:24:24 -0400758 font->cheight = height;
759 font->ascent = height - 2;
760 return (void*) font;
761}
762
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200763void gr_freeFont(void *font)
764{
765 GRFont *f = font;
766 free(f->texture.data);
767 free(f);
768}
769
770int gr_getMaxFontHeight(void *font)
Dees_Troy51a0e822012-09-05 15:24:24 -0400771{
772 GRFont *fnt = (GRFont*) font;
773
774 if (!fnt) fnt = gr_font;
775 if (!fnt) return -1;
776
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200777#ifndef TW_DISABLE_TTF
778 if(fnt->type == FONT_TYPE_TTF)
779 return gr_ttf_getMaxFontHeight(font);
780#endif
781
782 return fnt->cheight;
Dees_Troy51a0e822012-09-05 15:24:24 -0400783}
784
785static void gr_init_font(void)
786{
787 int fontRes;
788 GGLSurface *ftex;
789 unsigned char *bits, *rle;
790 unsigned char *in, data;
791 unsigned width, height;
792 unsigned element;
793
794 gr_font = calloc(sizeof(*gr_font), 1);
795 ftex = &gr_font->texture;
796
797 width = font.width;
798 height = font.height;
799
800 bits = malloc(width * height);
801 rle = bits;
802
803 in = font.rundata;
804 while((data = *in++))
805 {
806 memset(rle, (data & 0x80) ? 255 : 0, data & 0x7f);
807 rle += (data & 0x7f);
808 }
809 for (element = 0; element < 97; element++)
810 {
811 gr_font->offset[element] = (element * font.cwidth);
812 }
813
814 ftex->version = sizeof(*ftex);
815 ftex->width = width;
816 ftex->height = height;
817 ftex->stride = width;
818 ftex->data = (void*) bits;
819 ftex->format = GGL_PIXEL_FORMAT_A_8;
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200820 gr_font->type = FONT_TYPE_TWRP;
Dees_Troy51a0e822012-09-05 15:24:24 -0400821 gr_font->cheight = height;
822 gr_font->ascent = height - 2;
823 return;
824}
825
826int gr_init(void)
827{
828 gglInit(&gr_context);
829 GGLContext *gl = gr_context;
830
831 gr_init_font();
832 gr_vt_fd = open("/dev/tty0", O_RDWR | O_SYNC);
833 if (gr_vt_fd < 0) {
834 // This is non-fatal; post-Cupcake kernels don't have tty0.
835 } else if (ioctl(gr_vt_fd, KDSETMODE, (void*) KD_GRAPHICS)) {
836 // However, if we do open tty0, we expect the ioctl to work.
837 perror("failed KDSETMODE to KD_GRAPHICS on tty0");
838 gr_exit();
839 return -1;
840 }
841
842 gr_fb_fd = get_framebuffer(gr_framebuffer);
843 if (gr_fb_fd < 0) {
844 perror("Unable to get framebuffer.\n");
845 gr_exit();
846 return -1;
847 }
848
849 get_memory_surface(&gr_mem_surface);
850
851 fprintf(stderr, "framebuffer: fd %d (%d x %d)\n",
852 gr_fb_fd, gr_framebuffer[0].width, gr_framebuffer[0].height);
853
854 /* start with 0 as front (displayed) and 1 as back (drawing) */
855 gr_active_fb = 0;
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600856 if (!has_overlay)
857 set_active_framebuffer(0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400858 gl->colorBuffer(gl, &gr_mem_surface);
859
860 gl->activeTexture(gl, 0);
861 gl->enable(gl, GGL_BLEND);
862 gl->blendFunc(gl, GGL_SRC_ALPHA, GGL_ONE_MINUS_SRC_ALPHA);
863
xNUTxcd56f8c2014-07-23 01:30:20 +0200864#ifdef TW_SCREEN_BLANK_ON_BOOT
865 printf("TW_SCREEN_BLANK_ON_BOOT := true\n");
866 gr_fb_blank(true);
867 gr_fb_blank(false);
868#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400869
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600870 if (!alloc_ion_mem(fi.line_length * vi.yres))
871 allocate_overlay(gr_fb_fd, gr_framebuffer);
872
Dees_Troy51a0e822012-09-05 15:24:24 -0400873 return 0;
874}
875
876void gr_exit(void)
877{
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600878 free_overlay(gr_fb_fd);
879 free_ion_mem();
880
Dees_Troy51a0e822012-09-05 15:24:24 -0400881 close(gr_fb_fd);
882 gr_fb_fd = -1;
883
884 free(gr_mem_surface.data);
885
886 ioctl(gr_vt_fd, KDSETMODE, (void*) KD_TEXT);
887 close(gr_vt_fd);
888 gr_vt_fd = -1;
889}
890
891int gr_fb_width(void)
892{
893 return gr_framebuffer[0].width;
894}
895
896int gr_fb_height(void)
897{
898 return gr_framebuffer[0].height;
899}
900
901gr_pixel *gr_fb_data(void)
902{
903 return (unsigned short *) gr_mem_surface.data;
904}
905
Dees_Troy70237dc2013-02-28 21:31:48 +0000906int gr_fb_blank(int blank)
Dees_Troy51a0e822012-09-05 15:24:24 -0400907{
908 int ret;
Dees Troy05d18c92014-08-04 18:17:07 +0000909 //if (blank)
910 //free_overlay(gr_fb_fd);
Dees_Troy51a0e822012-09-05 15:24:24 -0400911
912 ret = ioctl(gr_fb_fd, FBIOBLANK, blank ? FB_BLANK_POWERDOWN : FB_BLANK_UNBLANK);
913 if (ret < 0)
914 perror("ioctl(): blank");
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600915
Dees Troy05d18c92014-08-04 18:17:07 +0000916 //if (!blank)
917 //allocate_overlay(gr_fb_fd, gr_framebuffer);
918 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -0400919}
920
921int gr_get_surface(gr_surface* surface)
922{
923 GGLSurface* ms = malloc(sizeof(GGLSurface));
924 if (!ms) return -1;
925
926 // Allocate the data
927 get_memory_surface(ms);
928
929 // Now, copy the data
930 memcpy(ms->data, gr_mem_surface.data, vi.xres * vi.yres * vi.bits_per_pixel / 8);
931
932 *surface = (gr_surface*) ms;
933 return 0;
934}
935
936int gr_free_surface(gr_surface surface)
937{
938 if (!surface)
939 return -1;
940
941 GGLSurface* ms = (GGLSurface*) surface;
942 free(ms->data);
943 free(ms);
944 return 0;
945}
946
947void gr_write_frame_to_file(int fd)
948{
949 write(fd, gr_mem_surface.data, vi.xres * vi.yres * vi.bits_per_pixel / 8);
950}