blob: 04c41fb54388f0d355b4889ffad2e1baa68cbed8 [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{
Ethan Yonker4f6a9762015-03-09 13:58:54 -0500166 int fd, index = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400167 void *bits;
168
169 fd = open("/dev/graphics/fb0", O_RDWR);
jenkins79699132015-05-08 06:04:44 -0400170
171 while (fd < 0 && index < 30) {
Ethan Yonker4f6a9762015-03-09 13:58:54 -0500172 usleep(1000);
173 fd = open("/dev/graphics/fb0", O_RDWR);
174 index++;
175 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400176 if (fd < 0) {
Ethan Yonker4f6a9762015-03-09 13:58:54 -0500177 perror("cannot open fb0\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400178 return -1;
179 }
180
181 if (ioctl(fd, FBIOGET_VSCREENINFO, &vi) < 0) {
182 perror("failed to get fb0 info");
183 close(fd);
184 return -1;
185 }
186
187 fprintf(stderr, "Pixel format: %dx%d @ %dbpp\n", vi.xres, vi.yres, vi.bits_per_pixel);
188
189 vi.bits_per_pixel = PIXEL_SIZE * 8;
190 if (PIXEL_FORMAT == GGL_PIXEL_FORMAT_BGRA_8888) {
191 fprintf(stderr, "Pixel format: BGRA_8888\n");
192 if (PIXEL_SIZE != 4) fprintf(stderr, "E: Pixel Size mismatch!\n");
193 vi.red.offset = 8;
194 vi.red.length = 8;
195 vi.green.offset = 16;
196 vi.green.length = 8;
197 vi.blue.offset = 24;
198 vi.blue.length = 8;
199 vi.transp.offset = 0;
200 vi.transp.length = 8;
201 } else if (PIXEL_FORMAT == GGL_PIXEL_FORMAT_RGBX_8888) {
202 fprintf(stderr, "Pixel format: RGBX_8888\n");
203 if (PIXEL_SIZE != 4) fprintf(stderr, "E: Pixel Size mismatch!\n");
204 vi.red.offset = 24;
205 vi.red.length = 8;
206 vi.green.offset = 16;
207 vi.green.length = 8;
208 vi.blue.offset = 8;
209 vi.blue.length = 8;
210 vi.transp.offset = 0;
211 vi.transp.length = 8;
212 } else if (PIXEL_FORMAT == GGL_PIXEL_FORMAT_RGB_565) {
213#ifdef RECOVERY_RGB_565
214 fprintf(stderr, "Pixel format: RGB_565\n");
215 vi.blue.offset = 0;
216 vi.green.offset = 5;
217 vi.red.offset = 11;
218#else
219 fprintf(stderr, "Pixel format: BGR_565\n");
220 vi.blue.offset = 11;
221 vi.green.offset = 5;
222 vi.red.offset = 0;
223#endif
224 if (PIXEL_SIZE != 2) fprintf(stderr, "E: Pixel Size mismatch!\n");
225 vi.blue.length = 5;
226 vi.green.length = 6;
227 vi.red.length = 5;
228 vi.blue.msb_right = 0;
229 vi.green.msb_right = 0;
230 vi.red.msb_right = 0;
231 vi.transp.offset = 0;
232 vi.transp.length = 0;
233 }
234 else
235 {
236 perror("unknown pixel format");
237 close(fd);
238 return -1;
239 }
240
241 vi.vmode = FB_VMODE_NONINTERLACED;
242 vi.activate = FB_ACTIVATE_NOW | FB_ACTIVATE_FORCE;
243
244 if (ioctl(fd, FBIOPUT_VSCREENINFO, &vi) < 0) {
245 perror("failed to put fb0 info");
246 close(fd);
247 return -1;
248 }
249
250 if (ioctl(fd, FBIOGET_FSCREENINFO, &fi) < 0) {
251 perror("failed to get fb0 info");
252 close(fd);
253 return -1;
254 }
255
Dees Troyb0425382014-04-03 00:10:03 +0000256#ifdef MSM_BSP
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600257 has_overlay = target_has_overlay(fi.id);
258
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600259 if (isTargetMdp5())
260 setDisplaySplit();
Dees Troyb0425382014-04-03 00:10:03 +0000261#else
262 has_overlay = false;
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600263#endif
264
265 if (!has_overlay) {
266 printf("Not using qualcomm overlay, '%s'\n", fi.id);
267 bits = mmap(0, fi.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
268 if (bits == MAP_FAILED) {
269 perror("failed to mmap framebuffer");
270 close(fd);
271 return -1;
272 }
273 } else {
274 printf("Using qualcomm overlay\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400275 }
276
277#ifdef RECOVERY_GRAPHICS_USE_LINELENGTH
278 vi.xres_virtual = fi.line_length / PIXEL_SIZE;
279#endif
280
281 fb->version = sizeof(*fb);
282 fb->width = vi.xres;
283 fb->height = vi.yres;
284#ifdef BOARD_HAS_JANKY_BACKBUFFER
Dees_Troy2673cec2013-04-02 20:22:16 +0000285 printf("setting JANKY BACKBUFFER\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400286 fb->stride = fi.line_length/2;
287#else
288 fb->stride = vi.xres_virtual;
289#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400290 fb->format = PIXEL_FORMAT;
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600291 if (!has_overlay) {
292 fb->data = bits;
293 memset(fb->data, 0, vi.yres * fb->stride * PIXEL_SIZE);
294 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400295
296 fb++;
297
Kra1o5c9556cc2015-06-24 12:19:09 +0200298#ifndef TW_DISABLE_DOUBLE_BUFFERING
Hiemanshu Sharma94f6c882012-11-21 11:25:22 -0600299 /* check if we can use double buffering */
300 if (vi.yres * fi.line_length * 2 > fi.smem_len)
Kra1o5c9556cc2015-06-24 12:19:09 +0200301#else
302 printf("TW_DISABLE_DOUBLE_BUFFERING := true\n");
303#endif
Hiemanshu Sharma94f6c882012-11-21 11:25:22 -0600304 return fd;
305
306 double_buffering = 1;
307
Dees_Troy51a0e822012-09-05 15:24:24 -0400308 fb->version = sizeof(*fb);
309 fb->width = vi.xres;
310 fb->height = vi.yres;
311#ifdef BOARD_HAS_JANKY_BACKBUFFER
312 fb->stride = fi.line_length/2;
Ethan Yonkerbcc502c2014-11-10 11:22:10 -0600313 fb->data = (GGLubyte*) (((unsigned long) bits) + vi.yres * fi.line_length);
Dees_Troy51a0e822012-09-05 15:24:24 -0400314#else
315 fb->stride = vi.xres_virtual;
Ethan Yonkerbcc502c2014-11-10 11:22:10 -0600316 fb->data = (GGLubyte*) (((unsigned long) bits) + vi.yres * fb->stride * PIXEL_SIZE);
Dees_Troy51a0e822012-09-05 15:24:24 -0400317#endif
318 fb->format = PIXEL_FORMAT;
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600319 if (!has_overlay) {
320 memset(fb->data, 0, vi.yres * fb->stride * PIXEL_SIZE);
321 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400322
323#ifdef PRINT_SCREENINFO
324 print_fb_var_screeninfo();
325#endif
326
327 return fd;
328}
329
330static void get_memory_surface(GGLSurface* ms) {
331 ms->version = sizeof(*ms);
332 ms->width = vi.xres;
333 ms->height = vi.yres;
334 ms->stride = vi.xres_virtual;
335 ms->data = malloc(vi.xres_virtual * vi.yres * PIXEL_SIZE);
336 ms->format = PIXEL_FORMAT;
337}
338
339static void set_active_framebuffer(unsigned n)
340{
Hiemanshu Sharma94f6c882012-11-21 11:25:22 -0600341 if (n > 1 || !double_buffering) return;
Hiemanshu Sharmaacf6a9b2012-11-21 11:28:36 -0600342 vi.yres_virtual = vi.yres * NUM_BUFFERS;
Dees_Troy51a0e822012-09-05 15:24:24 -0400343 vi.yoffset = n * vi.yres;
344// vi.bits_per_pixel = PIXEL_SIZE * 8;
345 if (ioctl(gr_fb_fd, FBIOPUT_VSCREENINFO, &vi) < 0) {
346 perror("active fb swap failed");
347 }
348}
349
350void gr_flip(void)
351{
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600352 if (-EINVAL == overlay_display_frame(gr_fb_fd, gr_mem_surface.data,
353 (fi.line_length * vi.yres))) {
354 GGLContext *gl = gr_context;
Dees_Troy51a0e822012-09-05 15:24:24 -0400355
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600356 /* swap front and back buffers */
357 if (double_buffering)
358 gr_active_fb = (gr_active_fb + 1) & 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400359
360#ifdef BOARD_HAS_FLIPPED_SCREEN
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600361 /* flip buffer 180 degrees for devices with physicaly inverted screens */
362 unsigned int i;
363 unsigned int j;
Bogdan Seniucd1372f72015-01-23 08:22:39 -0800364 for (i = 0; i < vi.yres; i++) {
365 for (j = 0; j < vi.xres; j++) {
366 memcpy(gr_framebuffer[gr_active_fb].data + (i * vi.xres_virtual + j) * PIXEL_SIZE,
367 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 -0600368 }
369 }
Bogdan Seniucd1372f72015-01-23 08:22:39 -0800370#else
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600371 /* copy data from the in-memory surface to the buffer we're about
372 * to make active. */
373 memcpy(gr_framebuffer[gr_active_fb].data, gr_mem_surface.data,
374 vi.xres_virtual * vi.yres * PIXEL_SIZE);
Bogdan Seniucd1372f72015-01-23 08:22:39 -0800375#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400376
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600377 /* inform the display driver */
378 set_active_framebuffer(gr_active_fb);
379 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400380}
381
382void gr_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
383{
384 GGLContext *gl = gr_context;
385 GGLint color[4];
386 color[0] = ((r << 8) | r) + 1;
387 color[1] = ((g << 8) | g) + 1;
388 color[2] = ((b << 8) | b) + 1;
389 color[3] = ((a << 8) | a) + 1;
390 gl->color4xv(gl, color);
Vojtech Bocek1a4744a2014-02-06 19:52:28 +0100391
392 gr_is_curr_clr_opaque = (a == 255);
Dees_Troy51a0e822012-09-05 15:24:24 -0400393}
394
395int gr_measureEx(const char *s, void* font)
396{
397 GRFont* fnt = (GRFont*) font;
398 int total = 0;
399 unsigned pos;
400 unsigned off;
401
402 if (!fnt) fnt = gr_font;
403
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200404#ifndef TW_DISABLE_TTF
405 if(fnt->type == FONT_TYPE_TTF)
406 return gr_ttf_measureEx(s, font);
407#endif
408
Dees_Troy51a0e822012-09-05 15:24:24 -0400409 while ((off = *s++))
410 {
411 off -= 32;
412 if (off < 96)
413 total += (fnt->offset[off+1] - fnt->offset[off]);
414 }
415 return total;
416}
417
Dees Troy31218ec2014-02-25 20:35:56 +0000418int gr_maxExW(const char *s, void* font, int max_width)
419{
420 GRFont* fnt = (GRFont*) font;
421 int total = 0;
422 unsigned pos;
423 unsigned off;
424
425 if (!fnt) fnt = gr_font;
426
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200427#ifndef TW_DISABLE_TTF
428 if(fnt->type == FONT_TYPE_TTF)
429 return gr_ttf_maxExW(s, font, max_width);
430#endif
431
Dees Troy31218ec2014-02-25 20:35:56 +0000432 while ((off = *s++))
433 {
434 off -= 32;
435 if (off < 96) {
436 max_width -= (fnt->offset[off+1] - fnt->offset[off]);
437 if (max_width > 0) {
438 total++;
439 } else {
440 return total;
441 }
442 }
443 }
444 return total;
445}
446
Dees_Troy51a0e822012-09-05 15:24:24 -0400447int gr_textEx(int x, int y, const char *s, void* pFont)
448{
449 GGLContext *gl = gr_context;
450 GRFont *font = (GRFont*) pFont;
451 unsigned off;
452 unsigned cwidth;
453
454 /* Handle default font */
455 if (!font) font = gr_font;
456
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200457#ifndef TW_DISABLE_TTF
458 if(font->type == FONT_TYPE_TTF)
459 return gr_ttf_textExWH(gl, x, y, s, pFont, -1, -1);
460#endif
461
Dees_Troy51a0e822012-09-05 15:24:24 -0400462 gl->bindTexture(gl, &font->texture);
463 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
464 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
465 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
466 gl->enable(gl, GGL_TEXTURE_2D);
467
468 while((off = *s++)) {
469 off -= 32;
470 cwidth = 0;
471 if (off < 96) {
472 cwidth = font->offset[off+1] - font->offset[off];
473 gl->texCoord2i(gl, (font->offset[off]) - x, 0 - y);
474 gl->recti(gl, x, y, x + cwidth, y + font->cheight);
475 x += cwidth;
476 }
477 }
478
Vojtech Bocek3041c882015-03-06 00:28:21 +0100479 gl->disable(gl, GGL_TEXTURE_2D);
480
Dees_Troy51a0e822012-09-05 15:24:24 -0400481 return x;
482}
483
484int gr_textExW(int x, int y, const char *s, void* pFont, int max_width)
485{
486 GGLContext *gl = gr_context;
487 GRFont *font = (GRFont*) pFont;
488 unsigned off;
489 unsigned cwidth;
490
491 /* Handle default font */
492 if (!font) font = gr_font;
493
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200494#ifndef TW_DISABLE_TTF
495 if(font->type == FONT_TYPE_TTF)
496 return gr_ttf_textExWH(gl, x, y, s, pFont, max_width, -1);
497#endif
498
Dees_Troy51a0e822012-09-05 15:24:24 -0400499 gl->bindTexture(gl, &font->texture);
500 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
501 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
502 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
503 gl->enable(gl, GGL_TEXTURE_2D);
504
505 while((off = *s++)) {
506 off -= 32;
507 cwidth = 0;
508 if (off < 96) {
509 cwidth = font->offset[off+1] - font->offset[off];
510 if ((x + (int)cwidth) < max_width) {
511 gl->texCoord2i(gl, (font->offset[off]) - x, 0 - y);
512 gl->recti(gl, x, y, x + cwidth, y + font->cheight);
513 x += cwidth;
514 } else {
515 gl->texCoord2i(gl, (font->offset[off]) - x, 0 - y);
516 gl->recti(gl, x, y, max_width, y + font->cheight);
517 x = max_width;
518 return x;
519 }
520 }
521 }
522
Vojtech Bocek3041c882015-03-06 00:28:21 +0100523 gl->disable(gl, GGL_TEXTURE_2D);
524
Dees_Troy51a0e822012-09-05 15:24:24 -0400525 return x;
526}
527
528int gr_textExWH(int x, int y, const char *s, void* pFont, int max_width, int max_height)
529{
530 GGLContext *gl = gr_context;
531 GRFont *font = (GRFont*) pFont;
532 unsigned off;
533 unsigned cwidth;
534 int rect_x, rect_y;
535
536 /* Handle default font */
537 if (!font) font = gr_font;
538
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200539#ifndef TW_DISABLE_TTF
540 if(font->type == FONT_TYPE_TTF)
541 return gr_ttf_textExWH(gl, x, y, s, pFont, max_width, max_height);
542#endif
543
Dees_Troy51a0e822012-09-05 15:24:24 -0400544 gl->bindTexture(gl, &font->texture);
545 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
546 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
547 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
548 gl->enable(gl, GGL_TEXTURE_2D);
549
550 while((off = *s++)) {
551 off -= 32;
552 cwidth = 0;
553 if (off < 96) {
554 cwidth = font->offset[off+1] - font->offset[off];
555 if ((x + (int)cwidth) < max_width)
556 rect_x = x + cwidth;
557 else
558 rect_x = max_width;
Dees_Troyf7596752012-09-28 13:21:36 -0400559 if (y + font->cheight < (unsigned int)(max_height))
Dees_Troy51a0e822012-09-05 15:24:24 -0400560 rect_y = y + font->cheight;
561 else
562 rect_y = max_height;
563
564 gl->texCoord2i(gl, (font->offset[off]) - x, 0 - y);
565 gl->recti(gl, x, y, rect_x, rect_y);
566 x += cwidth;
567 if (x > max_width)
568 return x;
569 }
570 }
571
Vojtech Bocek3041c882015-03-06 00:28:21 +0100572 gl->disable(gl, GGL_TEXTURE_2D);
573
Dees_Troy51a0e822012-09-05 15:24:24 -0400574 return x;
575}
576
that9876ac32015-02-15 21:40:59 +0100577void gr_clip(int x, int y, int w, int h)
578{
579 GGLContext *gl = gr_context;
580 gl->scissor(gl, x, y, w, h);
581 gl->enable(gl, GGL_SCISSOR_TEST);
582}
583
584void gr_noclip()
585{
586 GGLContext *gl = gr_context;
587 gl->scissor(gl, 0, 0, gr_fb_width(), gr_fb_height());
588 gl->disable(gl, GGL_SCISSOR_TEST);
589}
590
Dees_Troy51a0e822012-09-05 15:24:24 -0400591void gr_fill(int x, int y, int w, int h)
592{
593 GGLContext *gl = gr_context;
Vojtech Bocek1a4744a2014-02-06 19:52:28 +0100594
595 if(gr_is_curr_clr_opaque)
596 gl->disable(gl, GGL_BLEND);
597
Dees_Troy51a0e822012-09-05 15:24:24 -0400598 gl->recti(gl, x, y, x + w, y + h);
Vojtech Bocek1a4744a2014-02-06 19:52:28 +0100599
600 if(gr_is_curr_clr_opaque)
601 gl->enable(gl, GGL_BLEND);
Dees_Troy51a0e822012-09-05 15:24:24 -0400602}
603
Vojtech Bocekdb378b62015-03-05 20:02:57 +0100604void gr_line(int x0, int y0, int x1, int y1, int width)
605{
606 GGLContext *gl = gr_context;
607
608 if(gr_is_curr_clr_opaque)
609 gl->disable(gl, GGL_BLEND);
610
611 const int coords0[2] = { x0 << 4, y0 << 4 };
612 const int coords1[2] = { x1 << 4, y1 << 4 };
613 gl->linex(gl, coords0, coords1, width << 4);
614
615 if(gr_is_curr_clr_opaque)
616 gl->enable(gl, GGL_BLEND);
617}
618
619gr_surface gr_render_circle(int radius, unsigned char r, unsigned char g, unsigned char b, unsigned char a)
620{
621 int rx, ry;
622 GGLSurface *surface;
623 const int diameter = radius*2 + 1;
624 const int radius_check = radius*radius + radius*0.8;
625 const uint32_t px = (a << 24) | (b << 16) | (g << 8) | r;
626 uint32_t *data;
627
628 surface = malloc(sizeof(GGLSurface));
629 memset(surface, 0, sizeof(GGLSurface));
630
631 data = malloc(diameter * diameter * 4);
632 memset(data, 0, diameter * diameter * 4);
633
634 surface->version = sizeof(surface);
635 surface->width = diameter;
636 surface->height = diameter;
637 surface->stride = diameter;
638 surface->data = (GGLubyte*)data;
639 surface->format = GGL_PIXEL_FORMAT_RGBA_8888;
640
641 for(ry = -radius; ry <= radius; ++ry)
642 for(rx = -radius; rx <= radius; ++rx)
643 if(rx*rx+ry*ry <= radius_check)
644 *(data + diameter*(radius + ry) + (radius+rx)) = px;
645
646 return surface;
647}
648
Dees_Troy51a0e822012-09-05 15:24:24 -0400649void gr_blit(gr_surface source, int sx, int sy, int w, int h, int dx, int dy) {
Dees Troyf171e102014-11-06 22:19:58 +0100650 if (gr_context == NULL) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400651 return;
652 }
653
654 GGLContext *gl = gr_context;
Vojtech Bocek6c694b62014-02-06 20:36:25 +0100655 GGLSurface *surface = (GGLSurface*)source;
656
657 if(surface->format == GGL_PIXEL_FORMAT_RGBX_8888)
658 gl->disable(gl, GGL_BLEND);
659
660 gl->bindTexture(gl, surface);
Dees_Troy51a0e822012-09-05 15:24:24 -0400661 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
662 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
663 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
664 gl->enable(gl, GGL_TEXTURE_2D);
665 gl->texCoord2i(gl, sx - dx, sy - dy);
666 gl->recti(gl, dx, dy, dx + w, dy + h);
Vojtech Bocek3041c882015-03-06 00:28:21 +0100667 gl->disable(gl, GGL_TEXTURE_2D);
Vojtech Bocek6c694b62014-02-06 20:36:25 +0100668
669 if(surface->format == GGL_PIXEL_FORMAT_RGBX_8888)
Dees Troyf171e102014-11-06 22:19:58 +0100670 gl->enable(gl, GGL_BLEND);
Dees_Troy51a0e822012-09-05 15:24:24 -0400671}
672
673unsigned int gr_get_width(gr_surface surface) {
674 if (surface == NULL) {
675 return 0;
676 }
677 return ((GGLSurface*) surface)->width;
678}
679
680unsigned int gr_get_height(gr_surface surface) {
681 if (surface == NULL) {
682 return 0;
683 }
684 return ((GGLSurface*) surface)->height;
685}
686
687void* gr_loadFont(const char* fontName)
688{
689 int fd;
690 GRFont *font = 0;
691 GGLSurface *ftex;
692 unsigned char *bits, *rle;
693 unsigned char *in, data;
694 unsigned width, height;
695 unsigned element;
696
697 fd = open(fontName, O_RDONLY);
698 if (fd == -1)
699 {
700 char tmp[128];
701
Dees Troy3454ade2015-01-20 19:21:04 +0000702 sprintf(tmp, TWRES "fonts/%s.dat", fontName);
Dees_Troy51a0e822012-09-05 15:24:24 -0400703 fd = open(tmp, O_RDONLY);
704 if (fd == -1)
705 return NULL;
706 }
707
708 font = calloc(sizeof(*font), 1);
709 ftex = &font->texture;
710
711 read(fd, &width, sizeof(unsigned));
712 read(fd, &height, sizeof(unsigned));
713 read(fd, font->offset, sizeof(unsigned) * 96);
714 font->offset[96] = width;
715
716 bits = malloc(width * height);
717 memset(bits, 0, width * height);
718
719 unsigned pos = 0;
720 while (pos < width * height)
721 {
722 int bit;
723
724 read(fd, &data, 1);
725 for (bit = 0; bit < 8; bit++)
726 {
727 if (data & (1 << (7-bit))) bits[pos++] = 255;
728 else bits[pos++] = 0;
729
730 if (pos == width * height) break;
731 }
732 }
733 close(fd);
734
735 ftex->version = sizeof(*ftex);
736 ftex->width = width;
737 ftex->height = height;
738 ftex->stride = width;
739 ftex->data = (void*) bits;
740 ftex->format = GGL_PIXEL_FORMAT_A_8;
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200741 font->type = FONT_TYPE_TWRP;
Dees_Troy51a0e822012-09-05 15:24:24 -0400742 font->cheight = height;
743 font->ascent = height - 2;
744 return (void*) font;
745}
746
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200747void gr_freeFont(void *font)
748{
749 GRFont *f = font;
750 free(f->texture.data);
751 free(f);
752}
753
754int gr_getMaxFontHeight(void *font)
Dees_Troy51a0e822012-09-05 15:24:24 -0400755{
756 GRFont *fnt = (GRFont*) font;
757
758 if (!fnt) fnt = gr_font;
759 if (!fnt) return -1;
760
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200761#ifndef TW_DISABLE_TTF
762 if(fnt->type == FONT_TYPE_TTF)
763 return gr_ttf_getMaxFontHeight(font);
764#endif
765
766 return fnt->cheight;
Dees_Troy51a0e822012-09-05 15:24:24 -0400767}
768
769static void gr_init_font(void)
770{
771 int fontRes;
772 GGLSurface *ftex;
773 unsigned char *bits, *rle;
774 unsigned char *in, data;
775 unsigned width, height;
776 unsigned element;
777
778 gr_font = calloc(sizeof(*gr_font), 1);
779 ftex = &gr_font->texture;
780
781 width = font.width;
782 height = font.height;
783
784 bits = malloc(width * height);
785 rle = bits;
786
787 in = font.rundata;
788 while((data = *in++))
789 {
790 memset(rle, (data & 0x80) ? 255 : 0, data & 0x7f);
791 rle += (data & 0x7f);
792 }
793 for (element = 0; element < 97; element++)
794 {
795 gr_font->offset[element] = (element * font.cwidth);
796 }
797
798 ftex->version = sizeof(*ftex);
799 ftex->width = width;
800 ftex->height = height;
801 ftex->stride = width;
802 ftex->data = (void*) bits;
803 ftex->format = GGL_PIXEL_FORMAT_A_8;
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200804 gr_font->type = FONT_TYPE_TWRP;
Dees_Troy51a0e822012-09-05 15:24:24 -0400805 gr_font->cheight = height;
806 gr_font->ascent = height - 2;
807 return;
808}
809
810int gr_init(void)
811{
812 gglInit(&gr_context);
813 GGLContext *gl = gr_context;
814
815 gr_init_font();
816 gr_vt_fd = open("/dev/tty0", O_RDWR | O_SYNC);
817 if (gr_vt_fd < 0) {
818 // This is non-fatal; post-Cupcake kernels don't have tty0.
819 } else if (ioctl(gr_vt_fd, KDSETMODE, (void*) KD_GRAPHICS)) {
820 // However, if we do open tty0, we expect the ioctl to work.
821 perror("failed KDSETMODE to KD_GRAPHICS on tty0");
822 gr_exit();
823 return -1;
824 }
825
826 gr_fb_fd = get_framebuffer(gr_framebuffer);
827 if (gr_fb_fd < 0) {
828 perror("Unable to get framebuffer.\n");
829 gr_exit();
830 return -1;
831 }
832
833 get_memory_surface(&gr_mem_surface);
834
835 fprintf(stderr, "framebuffer: fd %d (%d x %d)\n",
836 gr_fb_fd, gr_framebuffer[0].width, gr_framebuffer[0].height);
837
838 /* start with 0 as front (displayed) and 1 as back (drawing) */
839 gr_active_fb = 0;
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600840 if (!has_overlay)
841 set_active_framebuffer(0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400842 gl->colorBuffer(gl, &gr_mem_surface);
843
844 gl->activeTexture(gl, 0);
845 gl->enable(gl, GGL_BLEND);
846 gl->blendFunc(gl, GGL_SRC_ALPHA, GGL_ONE_MINUS_SRC_ALPHA);
847
xNUTxcd56f8c2014-07-23 01:30:20 +0200848#ifdef TW_SCREEN_BLANK_ON_BOOT
849 printf("TW_SCREEN_BLANK_ON_BOOT := true\n");
850 gr_fb_blank(true);
851 gr_fb_blank(false);
852#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400853
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600854 if (!alloc_ion_mem(fi.line_length * vi.yres))
855 allocate_overlay(gr_fb_fd, gr_framebuffer);
856
Dees_Troy51a0e822012-09-05 15:24:24 -0400857 return 0;
858}
859
860void gr_exit(void)
861{
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600862 free_overlay(gr_fb_fd);
863 free_ion_mem();
864
Dees_Troy51a0e822012-09-05 15:24:24 -0400865 close(gr_fb_fd);
866 gr_fb_fd = -1;
867
868 free(gr_mem_surface.data);
869
870 ioctl(gr_vt_fd, KDSETMODE, (void*) KD_TEXT);
871 close(gr_vt_fd);
872 gr_vt_fd = -1;
873}
874
875int gr_fb_width(void)
876{
877 return gr_framebuffer[0].width;
878}
879
880int gr_fb_height(void)
881{
882 return gr_framebuffer[0].height;
883}
884
885gr_pixel *gr_fb_data(void)
886{
887 return (unsigned short *) gr_mem_surface.data;
888}
889
Dees_Troy70237dc2013-02-28 21:31:48 +0000890int gr_fb_blank(int blank)
Dees_Troy51a0e822012-09-05 15:24:24 -0400891{
892 int ret;
Dees Troy05d18c92014-08-04 18:17:07 +0000893 //if (blank)
894 //free_overlay(gr_fb_fd);
Dees_Troy51a0e822012-09-05 15:24:24 -0400895
896 ret = ioctl(gr_fb_fd, FBIOBLANK, blank ? FB_BLANK_POWERDOWN : FB_BLANK_UNBLANK);
897 if (ret < 0)
898 perror("ioctl(): blank");
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600899
Dees Troy05d18c92014-08-04 18:17:07 +0000900 //if (!blank)
901 //allocate_overlay(gr_fb_fd, gr_framebuffer);
902 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -0400903}
904
905int gr_get_surface(gr_surface* surface)
906{
907 GGLSurface* ms = malloc(sizeof(GGLSurface));
908 if (!ms) return -1;
909
910 // Allocate the data
911 get_memory_surface(ms);
912
913 // Now, copy the data
914 memcpy(ms->data, gr_mem_surface.data, vi.xres * vi.yres * vi.bits_per_pixel / 8);
915
916 *surface = (gr_surface*) ms;
917 return 0;
918}
919
920int gr_free_surface(gr_surface surface)
921{
922 if (!surface)
923 return -1;
924
925 GGLSurface* ms = (GGLSurface*) surface;
926 free(ms->data);
927 free(ms);
928 return 0;
929}
930
931void gr_write_frame_to_file(int fd)
932{
933 write(fd, gr_mem_surface.data, vi.xres * vi.yres * vi.bits_per_pixel / 8);
934}