blob: 4b4e5c6796866205e9d564eb3dab0bed8b6da1e0 [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
Kra1o58499b082015-10-14 18:22:42 +020046#ifdef RECOVERY_RGBA
47#define PIXEL_FORMAT GGL_PIXEL_FORMAT_RGBA_8888
48#define PIXEL_SIZE 4
49#endif
Dees_Troy51a0e822012-09-05 15:24:24 -040050#ifdef RECOVERY_RGBX
51#define PIXEL_FORMAT GGL_PIXEL_FORMAT_RGBX_8888
52#define PIXEL_SIZE 4
53#endif
54#ifndef PIXEL_FORMAT
55#define PIXEL_FORMAT GGL_PIXEL_FORMAT_RGB_565
56#define PIXEL_SIZE 2
57#endif
58
Hiemanshu Sharmaacf6a9b2012-11-21 11:28:36 -060059#define NUM_BUFFERS 2
Ethan Yonker4ee5ad72014-02-18 18:41:17 -060060#define MAX_DISPLAY_DIM 2048
Hiemanshu Sharmaacf6a9b2012-11-21 11:28:36 -060061
Dees_Troy51a0e822012-09-05 15:24:24 -040062// #define PRINT_SCREENINFO 1 // Enables printing of screen info to log
63
64typedef struct {
Vojtech Bocek76ee9032014-09-07 15:01:56 +020065 int type;
Dees_Troy51a0e822012-09-05 15:24:24 -040066 GGLSurface texture;
67 unsigned offset[97];
68 unsigned cheight;
69 unsigned ascent;
70} GRFont;
71
72static GRFont *gr_font = 0;
73static GGLContext *gr_context = 0;
74static GGLSurface gr_font_texture;
Hiemanshu Sharmaacf6a9b2012-11-21 11:28:36 -060075static GGLSurface gr_framebuffer[NUM_BUFFERS];
Vojtech Bocek03fd6c52014-03-13 18:46:34 +010076GGLSurface gr_mem_surface;
Dees_Troy51a0e822012-09-05 15:24:24 -040077static unsigned gr_active_fb = 0;
Hiemanshu Sharma94f6c882012-11-21 11:25:22 -060078static unsigned double_buffering = 0;
Vojtech Bocek1a4744a2014-02-06 19:52:28 +010079static int gr_is_curr_clr_opaque = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -040080
81static int gr_fb_fd = -1;
82static int gr_vt_fd = -1;
83
Vojtech Bocek03fd6c52014-03-13 18:46:34 +010084struct fb_var_screeninfo vi;
Dees_Troy51a0e822012-09-05 15:24:24 -040085static struct fb_fix_screeninfo fi;
86
Ethan Yonker4ee5ad72014-02-18 18:41:17 -060087static bool has_overlay = false;
88static int leftSplit = 0;
89static int rightSplit = 0;
90
91bool target_has_overlay(char *version);
92int free_ion_mem(void);
93int alloc_ion_mem(unsigned int size);
94int allocate_overlay(int fd, GGLSurface gr_fb[]);
95int free_overlay(int fd);
96int overlay_display_frame(int fd, GGLubyte* data, size_t size);
97
Dees_Troy51a0e822012-09-05 15:24:24 -040098#ifdef PRINT_SCREENINFO
99static void print_fb_var_screeninfo()
100{
Dees_Troy2673cec2013-04-02 20:22:16 +0000101 printf("vi.xres: %d\n", vi.xres);
102 printf("vi.yres: %d\n", vi.yres);
103 printf("vi.xres_virtual: %d\n", vi.xres_virtual);
104 printf("vi.yres_virtual: %d\n", vi.yres_virtual);
105 printf("vi.xoffset: %d\n", vi.xoffset);
106 printf("vi.yoffset: %d\n", vi.yoffset);
107 printf("vi.bits_per_pixel: %d\n", vi.bits_per_pixel);
108 printf("vi.grayscale: %d\n", vi.grayscale);
Dees_Troy51a0e822012-09-05 15:24:24 -0400109}
110#endif
111
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600112#ifdef MSM_BSP
113int getLeftSplit(void) {
114 //Default even split for all displays with high res
115 int lSplit = vi.xres / 2;
116
117 //Override if split published by driver
118 if (leftSplit)
119 lSplit = leftSplit;
120
121 return lSplit;
122}
123
124int getRightSplit(void) {
125 return rightSplit;
126}
127
128
129void setDisplaySplit(void) {
130 char split[64] = {0};
131 FILE* fp = fopen("/sys/class/graphics/fb0/msm_fb_split", "r");
132 if (fp) {
133 //Format "left right" space as delimiter
134 if(fread(split, sizeof(char), 64, fp)) {
135 leftSplit = atoi(split);
136 printf("Left Split=%d\n",leftSplit);
137 char *rght = strpbrk(split, " ");
138 if (rght)
139 rightSplit = atoi(rght + 1);
140 printf("Right Split=%d\n", rightSplit);
141 }
142 } else {
143 printf("Failed to open mdss_fb_split node\n");
144 }
145 if (fp)
146 fclose(fp);
147}
148
149bool isDisplaySplit(void) {
150 if (vi.xres > MAX_DISPLAY_DIM)
151 return true;
152 //check if right split is set by driver
153 if (getRightSplit())
154 return true;
155
156 return false;
157}
158
159int getFbXres(void) {
160 return vi.xres;
161}
162
163int getFbYres (void) {
164 return vi.yres;
165}
166#endif // MSM_BSP
167
Dees_Troy51a0e822012-09-05 15:24:24 -0400168static int get_framebuffer(GGLSurface *fb)
169{
Ethan Yonker4f6a9762015-03-09 13:58:54 -0500170 int fd, index = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400171 void *bits;
172
173 fd = open("/dev/graphics/fb0", O_RDWR);
jenkins79699132015-05-08 06:04:44 -0400174
175 while (fd < 0 && index < 30) {
Ethan Yonker4f6a9762015-03-09 13:58:54 -0500176 usleep(1000);
177 fd = open("/dev/graphics/fb0", O_RDWR);
178 index++;
179 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400180 if (fd < 0) {
Ethan Yonker4f6a9762015-03-09 13:58:54 -0500181 perror("cannot open fb0\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400182 return -1;
183 }
184
185 if (ioctl(fd, FBIOGET_VSCREENINFO, &vi) < 0) {
186 perror("failed to get fb0 info");
187 close(fd);
188 return -1;
189 }
190
191 fprintf(stderr, "Pixel format: %dx%d @ %dbpp\n", vi.xres, vi.yres, vi.bits_per_pixel);
192
193 vi.bits_per_pixel = PIXEL_SIZE * 8;
194 if (PIXEL_FORMAT == GGL_PIXEL_FORMAT_BGRA_8888) {
195 fprintf(stderr, "Pixel format: BGRA_8888\n");
196 if (PIXEL_SIZE != 4) fprintf(stderr, "E: Pixel Size mismatch!\n");
197 vi.red.offset = 8;
198 vi.red.length = 8;
199 vi.green.offset = 16;
200 vi.green.length = 8;
201 vi.blue.offset = 24;
202 vi.blue.length = 8;
203 vi.transp.offset = 0;
204 vi.transp.length = 8;
Kra1o58499b082015-10-14 18:22:42 +0200205 } else if (PIXEL_FORMAT == GGL_PIXEL_FORMAT_RGBA_8888) {
206 fprintf(stderr, "Pixel format: RGBA_8888\n");
207 if (PIXEL_SIZE != 4) fprintf(stderr, "E: Pixel Size mismatch!\n");
208 vi.red.offset = 0;
209 vi.red.length = 8;
210 vi.green.offset = 8;
211 vi.green.length = 8;
212 vi.blue.offset = 16;
213 vi.blue.length = 8;
214 vi.transp.offset = 24;
215 vi.transp.length = 8;
Dees_Troy51a0e822012-09-05 15:24:24 -0400216 } else if (PIXEL_FORMAT == GGL_PIXEL_FORMAT_RGBX_8888) {
217 fprintf(stderr, "Pixel format: RGBX_8888\n");
218 if (PIXEL_SIZE != 4) fprintf(stderr, "E: Pixel Size mismatch!\n");
219 vi.red.offset = 24;
220 vi.red.length = 8;
221 vi.green.offset = 16;
222 vi.green.length = 8;
223 vi.blue.offset = 8;
224 vi.blue.length = 8;
225 vi.transp.offset = 0;
226 vi.transp.length = 8;
227 } else if (PIXEL_FORMAT == GGL_PIXEL_FORMAT_RGB_565) {
228#ifdef RECOVERY_RGB_565
229 fprintf(stderr, "Pixel format: RGB_565\n");
230 vi.blue.offset = 0;
231 vi.green.offset = 5;
232 vi.red.offset = 11;
233#else
234 fprintf(stderr, "Pixel format: BGR_565\n");
235 vi.blue.offset = 11;
236 vi.green.offset = 5;
237 vi.red.offset = 0;
238#endif
239 if (PIXEL_SIZE != 2) fprintf(stderr, "E: Pixel Size mismatch!\n");
240 vi.blue.length = 5;
241 vi.green.length = 6;
242 vi.red.length = 5;
243 vi.blue.msb_right = 0;
244 vi.green.msb_right = 0;
245 vi.red.msb_right = 0;
246 vi.transp.offset = 0;
247 vi.transp.length = 0;
248 }
249 else
250 {
251 perror("unknown pixel format");
252 close(fd);
253 return -1;
254 }
255
256 vi.vmode = FB_VMODE_NONINTERLACED;
257 vi.activate = FB_ACTIVATE_NOW | FB_ACTIVATE_FORCE;
258
259 if (ioctl(fd, FBIOPUT_VSCREENINFO, &vi) < 0) {
260 perror("failed to put fb0 info");
261 close(fd);
262 return -1;
263 }
264
265 if (ioctl(fd, FBIOGET_FSCREENINFO, &fi) < 0) {
266 perror("failed to get fb0 info");
267 close(fd);
268 return -1;
269 }
270
Dees Troyb0425382014-04-03 00:10:03 +0000271#ifdef MSM_BSP
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600272 has_overlay = target_has_overlay(fi.id);
273
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600274 if (isTargetMdp5())
275 setDisplaySplit();
Dees Troyb0425382014-04-03 00:10:03 +0000276#else
277 has_overlay = false;
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600278#endif
279
280 if (!has_overlay) {
281 printf("Not using qualcomm overlay, '%s'\n", fi.id);
282 bits = mmap(0, fi.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
283 if (bits == MAP_FAILED) {
284 perror("failed to mmap framebuffer");
285 close(fd);
286 return -1;
287 }
288 } else {
289 printf("Using qualcomm overlay\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400290 }
291
292#ifdef RECOVERY_GRAPHICS_USE_LINELENGTH
293 vi.xres_virtual = fi.line_length / PIXEL_SIZE;
294#endif
295
296 fb->version = sizeof(*fb);
297 fb->width = vi.xres;
298 fb->height = vi.yres;
299#ifdef BOARD_HAS_JANKY_BACKBUFFER
Dees_Troy2673cec2013-04-02 20:22:16 +0000300 printf("setting JANKY BACKBUFFER\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400301 fb->stride = fi.line_length/2;
302#else
303 fb->stride = vi.xres_virtual;
304#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400305 fb->format = PIXEL_FORMAT;
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600306 if (!has_overlay) {
307 fb->data = bits;
308 memset(fb->data, 0, vi.yres * fb->stride * PIXEL_SIZE);
309 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400310
311 fb++;
312
Kra1o5c9556cc2015-06-24 12:19:09 +0200313#ifndef TW_DISABLE_DOUBLE_BUFFERING
Hiemanshu Sharma94f6c882012-11-21 11:25:22 -0600314 /* check if we can use double buffering */
315 if (vi.yres * fi.line_length * 2 > fi.smem_len)
Kra1o5c9556cc2015-06-24 12:19:09 +0200316#else
317 printf("TW_DISABLE_DOUBLE_BUFFERING := true\n");
318#endif
Hiemanshu Sharma94f6c882012-11-21 11:25:22 -0600319 return fd;
320
321 double_buffering = 1;
322
Dees_Troy51a0e822012-09-05 15:24:24 -0400323 fb->version = sizeof(*fb);
324 fb->width = vi.xres;
325 fb->height = vi.yres;
326#ifdef BOARD_HAS_JANKY_BACKBUFFER
327 fb->stride = fi.line_length/2;
Ethan Yonkerbcc502c2014-11-10 11:22:10 -0600328 fb->data = (GGLubyte*) (((unsigned long) bits) + vi.yres * fi.line_length);
Dees_Troy51a0e822012-09-05 15:24:24 -0400329#else
330 fb->stride = vi.xres_virtual;
Ethan Yonkerbcc502c2014-11-10 11:22:10 -0600331 fb->data = (GGLubyte*) (((unsigned long) bits) + vi.yres * fb->stride * PIXEL_SIZE);
Dees_Troy51a0e822012-09-05 15:24:24 -0400332#endif
333 fb->format = PIXEL_FORMAT;
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600334 if (!has_overlay) {
335 memset(fb->data, 0, vi.yres * fb->stride * PIXEL_SIZE);
336 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400337
338#ifdef PRINT_SCREENINFO
339 print_fb_var_screeninfo();
340#endif
341
342 return fd;
343}
344
345static void get_memory_surface(GGLSurface* ms) {
346 ms->version = sizeof(*ms);
347 ms->width = vi.xres;
348 ms->height = vi.yres;
349 ms->stride = vi.xres_virtual;
350 ms->data = malloc(vi.xres_virtual * vi.yres * PIXEL_SIZE);
351 ms->format = PIXEL_FORMAT;
352}
353
354static void set_active_framebuffer(unsigned n)
355{
Hiemanshu Sharma94f6c882012-11-21 11:25:22 -0600356 if (n > 1 || !double_buffering) return;
Hiemanshu Sharmaacf6a9b2012-11-21 11:28:36 -0600357 vi.yres_virtual = vi.yres * NUM_BUFFERS;
Dees_Troy51a0e822012-09-05 15:24:24 -0400358 vi.yoffset = n * vi.yres;
359// vi.bits_per_pixel = PIXEL_SIZE * 8;
360 if (ioctl(gr_fb_fd, FBIOPUT_VSCREENINFO, &vi) < 0) {
361 perror("active fb swap failed");
362 }
363}
364
365void gr_flip(void)
366{
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600367 if (-EINVAL == overlay_display_frame(gr_fb_fd, gr_mem_surface.data,
368 (fi.line_length * vi.yres))) {
369 GGLContext *gl = gr_context;
Dees_Troy51a0e822012-09-05 15:24:24 -0400370
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600371 /* swap front and back buffers */
372 if (double_buffering)
373 gr_active_fb = (gr_active_fb + 1) & 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400374
375#ifdef BOARD_HAS_FLIPPED_SCREEN
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600376 /* flip buffer 180 degrees for devices with physicaly inverted screens */
377 unsigned int i;
378 unsigned int j;
Bogdan Seniucd1372f72015-01-23 08:22:39 -0800379 for (i = 0; i < vi.yres; i++) {
380 for (j = 0; j < vi.xres; j++) {
381 memcpy(gr_framebuffer[gr_active_fb].data + (i * vi.xres_virtual + j) * PIXEL_SIZE,
382 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 -0600383 }
384 }
Bogdan Seniucd1372f72015-01-23 08:22:39 -0800385#else
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600386 /* copy data from the in-memory surface to the buffer we're about
387 * to make active. */
388 memcpy(gr_framebuffer[gr_active_fb].data, gr_mem_surface.data,
389 vi.xres_virtual * vi.yres * PIXEL_SIZE);
Bogdan Seniucd1372f72015-01-23 08:22:39 -0800390#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400391
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600392 /* inform the display driver */
393 set_active_framebuffer(gr_active_fb);
394 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400395}
396
397void gr_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
398{
399 GGLContext *gl = gr_context;
400 GGLint color[4];
401 color[0] = ((r << 8) | r) + 1;
402 color[1] = ((g << 8) | g) + 1;
403 color[2] = ((b << 8) | b) + 1;
404 color[3] = ((a << 8) | a) + 1;
405 gl->color4xv(gl, color);
Vojtech Bocek1a4744a2014-02-06 19:52:28 +0100406
407 gr_is_curr_clr_opaque = (a == 255);
Dees_Troy51a0e822012-09-05 15:24:24 -0400408}
409
410int gr_measureEx(const char *s, void* font)
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_measureEx(s, font);
422#endif
423
Dees_Troy51a0e822012-09-05 15:24:24 -0400424 while ((off = *s++))
425 {
426 off -= 32;
427 if (off < 96)
428 total += (fnt->offset[off+1] - fnt->offset[off]);
429 }
430 return total;
431}
432
Dees Troy31218ec2014-02-25 20:35:56 +0000433int gr_maxExW(const char *s, void* font, int max_width)
434{
435 GRFont* fnt = (GRFont*) font;
436 int total = 0;
437 unsigned pos;
438 unsigned off;
439
440 if (!fnt) fnt = gr_font;
441
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200442#ifndef TW_DISABLE_TTF
443 if(fnt->type == FONT_TYPE_TTF)
444 return gr_ttf_maxExW(s, font, max_width);
445#endif
446
Dees Troy31218ec2014-02-25 20:35:56 +0000447 while ((off = *s++))
448 {
449 off -= 32;
450 if (off < 96) {
451 max_width -= (fnt->offset[off+1] - fnt->offset[off]);
452 if (max_width > 0) {
453 total++;
454 } else {
455 return total;
456 }
457 }
458 }
459 return total;
460}
461
Dees_Troy51a0e822012-09-05 15:24:24 -0400462int gr_textEx(int x, int y, const char *s, void* pFont)
463{
464 GGLContext *gl = gr_context;
465 GRFont *font = (GRFont*) pFont;
466 unsigned off;
467 unsigned cwidth;
468
469 /* Handle default font */
470 if (!font) font = gr_font;
471
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200472#ifndef TW_DISABLE_TTF
473 if(font->type == FONT_TYPE_TTF)
474 return gr_ttf_textExWH(gl, x, y, s, pFont, -1, -1);
475#endif
476
Dees_Troy51a0e822012-09-05 15:24:24 -0400477 gl->bindTexture(gl, &font->texture);
478 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
479 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
480 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
481 gl->enable(gl, GGL_TEXTURE_2D);
482
483 while((off = *s++)) {
484 off -= 32;
485 cwidth = 0;
486 if (off < 96) {
487 cwidth = font->offset[off+1] - font->offset[off];
488 gl->texCoord2i(gl, (font->offset[off]) - x, 0 - y);
489 gl->recti(gl, x, y, x + cwidth, y + font->cheight);
490 x += cwidth;
491 }
492 }
493
Vojtech Bocek3041c882015-03-06 00:28:21 +0100494 gl->disable(gl, GGL_TEXTURE_2D);
495
Dees_Troy51a0e822012-09-05 15:24:24 -0400496 return x;
497}
498
499int gr_textExW(int x, int y, const char *s, void* pFont, int max_width)
500{
501 GGLContext *gl = gr_context;
502 GRFont *font = (GRFont*) pFont;
503 unsigned off;
504 unsigned cwidth;
505
506 /* Handle default font */
507 if (!font) font = gr_font;
508
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200509#ifndef TW_DISABLE_TTF
510 if(font->type == FONT_TYPE_TTF)
511 return gr_ttf_textExWH(gl, x, y, s, pFont, max_width, -1);
512#endif
513
Dees_Troy51a0e822012-09-05 15:24:24 -0400514 gl->bindTexture(gl, &font->texture);
515 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
516 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
517 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
518 gl->enable(gl, GGL_TEXTURE_2D);
519
520 while((off = *s++)) {
521 off -= 32;
522 cwidth = 0;
523 if (off < 96) {
524 cwidth = font->offset[off+1] - font->offset[off];
525 if ((x + (int)cwidth) < max_width) {
526 gl->texCoord2i(gl, (font->offset[off]) - x, 0 - y);
527 gl->recti(gl, x, y, x + cwidth, y + font->cheight);
528 x += cwidth;
529 } else {
530 gl->texCoord2i(gl, (font->offset[off]) - x, 0 - y);
531 gl->recti(gl, x, y, max_width, y + font->cheight);
532 x = max_width;
533 return x;
534 }
535 }
536 }
537
Vojtech Bocek3041c882015-03-06 00:28:21 +0100538 gl->disable(gl, GGL_TEXTURE_2D);
539
Dees_Troy51a0e822012-09-05 15:24:24 -0400540 return x;
541}
542
543int gr_textExWH(int x, int y, const char *s, void* pFont, int max_width, int max_height)
544{
545 GGLContext *gl = gr_context;
546 GRFont *font = (GRFont*) pFont;
547 unsigned off;
548 unsigned cwidth;
549 int rect_x, rect_y;
550
551 /* Handle default font */
552 if (!font) font = gr_font;
553
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200554#ifndef TW_DISABLE_TTF
555 if(font->type == FONT_TYPE_TTF)
556 return gr_ttf_textExWH(gl, x, y, s, pFont, max_width, max_height);
557#endif
558
Dees_Troy51a0e822012-09-05 15:24:24 -0400559 gl->bindTexture(gl, &font->texture);
560 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
561 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
562 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
563 gl->enable(gl, GGL_TEXTURE_2D);
564
565 while((off = *s++)) {
566 off -= 32;
567 cwidth = 0;
568 if (off < 96) {
569 cwidth = font->offset[off+1] - font->offset[off];
570 if ((x + (int)cwidth) < max_width)
571 rect_x = x + cwidth;
572 else
573 rect_x = max_width;
Dees_Troyf7596752012-09-28 13:21:36 -0400574 if (y + font->cheight < (unsigned int)(max_height))
Dees_Troy51a0e822012-09-05 15:24:24 -0400575 rect_y = y + font->cheight;
576 else
577 rect_y = max_height;
578
579 gl->texCoord2i(gl, (font->offset[off]) - x, 0 - y);
580 gl->recti(gl, x, y, rect_x, rect_y);
581 x += cwidth;
582 if (x > max_width)
583 return x;
584 }
585 }
586
Vojtech Bocek3041c882015-03-06 00:28:21 +0100587 gl->disable(gl, GGL_TEXTURE_2D);
588
Dees_Troy51a0e822012-09-05 15:24:24 -0400589 return x;
590}
591
that9876ac32015-02-15 21:40:59 +0100592void gr_clip(int x, int y, int w, int h)
593{
594 GGLContext *gl = gr_context;
595 gl->scissor(gl, x, y, w, h);
596 gl->enable(gl, GGL_SCISSOR_TEST);
597}
598
599void gr_noclip()
600{
601 GGLContext *gl = gr_context;
602 gl->scissor(gl, 0, 0, gr_fb_width(), gr_fb_height());
603 gl->disable(gl, GGL_SCISSOR_TEST);
604}
605
Dees_Troy51a0e822012-09-05 15:24:24 -0400606void gr_fill(int x, int y, int w, int h)
607{
608 GGLContext *gl = gr_context;
Vojtech Bocek1a4744a2014-02-06 19:52:28 +0100609
610 if(gr_is_curr_clr_opaque)
611 gl->disable(gl, GGL_BLEND);
612
Dees_Troy51a0e822012-09-05 15:24:24 -0400613 gl->recti(gl, x, y, x + w, y + h);
Vojtech Bocek1a4744a2014-02-06 19:52:28 +0100614
615 if(gr_is_curr_clr_opaque)
616 gl->enable(gl, GGL_BLEND);
Dees_Troy51a0e822012-09-05 15:24:24 -0400617}
618
Vojtech Bocekdb378b62015-03-05 20:02:57 +0100619void gr_line(int x0, int y0, int x1, int y1, int width)
620{
621 GGLContext *gl = gr_context;
622
623 if(gr_is_curr_clr_opaque)
624 gl->disable(gl, GGL_BLEND);
625
626 const int coords0[2] = { x0 << 4, y0 << 4 };
627 const int coords1[2] = { x1 << 4, y1 << 4 };
628 gl->linex(gl, coords0, coords1, width << 4);
629
630 if(gr_is_curr_clr_opaque)
631 gl->enable(gl, GGL_BLEND);
632}
633
634gr_surface gr_render_circle(int radius, unsigned char r, unsigned char g, unsigned char b, unsigned char a)
635{
636 int rx, ry;
637 GGLSurface *surface;
638 const int diameter = radius*2 + 1;
639 const int radius_check = radius*radius + radius*0.8;
640 const uint32_t px = (a << 24) | (b << 16) | (g << 8) | r;
641 uint32_t *data;
642
643 surface = malloc(sizeof(GGLSurface));
644 memset(surface, 0, sizeof(GGLSurface));
645
646 data = malloc(diameter * diameter * 4);
647 memset(data, 0, diameter * diameter * 4);
648
649 surface->version = sizeof(surface);
650 surface->width = diameter;
651 surface->height = diameter;
652 surface->stride = diameter;
653 surface->data = (GGLubyte*)data;
654 surface->format = GGL_PIXEL_FORMAT_RGBA_8888;
655
656 for(ry = -radius; ry <= radius; ++ry)
657 for(rx = -radius; rx <= radius; ++rx)
658 if(rx*rx+ry*ry <= radius_check)
659 *(data + diameter*(radius + ry) + (radius+rx)) = px;
660
661 return surface;
662}
663
Dees_Troy51a0e822012-09-05 15:24:24 -0400664void gr_blit(gr_surface source, int sx, int sy, int w, int h, int dx, int dy) {
Dees Troyf171e102014-11-06 22:19:58 +0100665 if (gr_context == NULL) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400666 return;
667 }
668
669 GGLContext *gl = gr_context;
Vojtech Bocek6c694b62014-02-06 20:36:25 +0100670 GGLSurface *surface = (GGLSurface*)source;
671
672 if(surface->format == GGL_PIXEL_FORMAT_RGBX_8888)
673 gl->disable(gl, GGL_BLEND);
674
675 gl->bindTexture(gl, surface);
Dees_Troy51a0e822012-09-05 15:24:24 -0400676 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
677 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
678 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
679 gl->enable(gl, GGL_TEXTURE_2D);
680 gl->texCoord2i(gl, sx - dx, sy - dy);
681 gl->recti(gl, dx, dy, dx + w, dy + h);
Vojtech Bocek3041c882015-03-06 00:28:21 +0100682 gl->disable(gl, GGL_TEXTURE_2D);
Vojtech Bocek6c694b62014-02-06 20:36:25 +0100683
684 if(surface->format == GGL_PIXEL_FORMAT_RGBX_8888)
Dees Troyf171e102014-11-06 22:19:58 +0100685 gl->enable(gl, GGL_BLEND);
Dees_Troy51a0e822012-09-05 15:24:24 -0400686}
687
688unsigned int gr_get_width(gr_surface surface) {
689 if (surface == NULL) {
690 return 0;
691 }
692 return ((GGLSurface*) surface)->width;
693}
694
695unsigned int gr_get_height(gr_surface surface) {
696 if (surface == NULL) {
697 return 0;
698 }
699 return ((GGLSurface*) surface)->height;
700}
701
702void* gr_loadFont(const char* fontName)
703{
704 int fd;
705 GRFont *font = 0;
706 GGLSurface *ftex;
707 unsigned char *bits, *rle;
708 unsigned char *in, data;
709 unsigned width, height;
710 unsigned element;
711
712 fd = open(fontName, O_RDONLY);
713 if (fd == -1)
714 {
715 char tmp[128];
716
Dees Troy3454ade2015-01-20 19:21:04 +0000717 sprintf(tmp, TWRES "fonts/%s.dat", fontName);
Dees_Troy51a0e822012-09-05 15:24:24 -0400718 fd = open(tmp, O_RDONLY);
719 if (fd == -1)
720 return NULL;
721 }
722
723 font = calloc(sizeof(*font), 1);
724 ftex = &font->texture;
725
726 read(fd, &width, sizeof(unsigned));
727 read(fd, &height, sizeof(unsigned));
728 read(fd, font->offset, sizeof(unsigned) * 96);
729 font->offset[96] = width;
730
731 bits = malloc(width * height);
732 memset(bits, 0, width * height);
733
734 unsigned pos = 0;
735 while (pos < width * height)
736 {
737 int bit;
738
739 read(fd, &data, 1);
740 for (bit = 0; bit < 8; bit++)
741 {
742 if (data & (1 << (7-bit))) bits[pos++] = 255;
743 else bits[pos++] = 0;
744
745 if (pos == width * height) break;
746 }
747 }
748 close(fd);
749
750 ftex->version = sizeof(*ftex);
751 ftex->width = width;
752 ftex->height = height;
753 ftex->stride = width;
754 ftex->data = (void*) bits;
755 ftex->format = GGL_PIXEL_FORMAT_A_8;
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200756 font->type = FONT_TYPE_TWRP;
Dees_Troy51a0e822012-09-05 15:24:24 -0400757 font->cheight = height;
758 font->ascent = height - 2;
759 return (void*) font;
760}
761
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200762void gr_freeFont(void *font)
763{
764 GRFont *f = font;
765 free(f->texture.data);
766 free(f);
767}
768
769int gr_getMaxFontHeight(void *font)
Dees_Troy51a0e822012-09-05 15:24:24 -0400770{
771 GRFont *fnt = (GRFont*) font;
772
773 if (!fnt) fnt = gr_font;
774 if (!fnt) return -1;
775
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200776#ifndef TW_DISABLE_TTF
777 if(fnt->type == FONT_TYPE_TTF)
778 return gr_ttf_getMaxFontHeight(font);
779#endif
780
781 return fnt->cheight;
Dees_Troy51a0e822012-09-05 15:24:24 -0400782}
783
784static void gr_init_font(void)
785{
786 int fontRes;
787 GGLSurface *ftex;
788 unsigned char *bits, *rle;
789 unsigned char *in, data;
790 unsigned width, height;
791 unsigned element;
792
793 gr_font = calloc(sizeof(*gr_font), 1);
794 ftex = &gr_font->texture;
795
796 width = font.width;
797 height = font.height;
798
799 bits = malloc(width * height);
800 rle = bits;
801
802 in = font.rundata;
803 while((data = *in++))
804 {
805 memset(rle, (data & 0x80) ? 255 : 0, data & 0x7f);
806 rle += (data & 0x7f);
807 }
808 for (element = 0; element < 97; element++)
809 {
810 gr_font->offset[element] = (element * font.cwidth);
811 }
812
813 ftex->version = sizeof(*ftex);
814 ftex->width = width;
815 ftex->height = height;
816 ftex->stride = width;
817 ftex->data = (void*) bits;
818 ftex->format = GGL_PIXEL_FORMAT_A_8;
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200819 gr_font->type = FONT_TYPE_TWRP;
Dees_Troy51a0e822012-09-05 15:24:24 -0400820 gr_font->cheight = height;
821 gr_font->ascent = height - 2;
822 return;
823}
824
825int gr_init(void)
826{
827 gglInit(&gr_context);
828 GGLContext *gl = gr_context;
829
830 gr_init_font();
831 gr_vt_fd = open("/dev/tty0", O_RDWR | O_SYNC);
832 if (gr_vt_fd < 0) {
833 // This is non-fatal; post-Cupcake kernels don't have tty0.
834 } else if (ioctl(gr_vt_fd, KDSETMODE, (void*) KD_GRAPHICS)) {
835 // However, if we do open tty0, we expect the ioctl to work.
836 perror("failed KDSETMODE to KD_GRAPHICS on tty0");
837 gr_exit();
838 return -1;
839 }
840
841 gr_fb_fd = get_framebuffer(gr_framebuffer);
842 if (gr_fb_fd < 0) {
843 perror("Unable to get framebuffer.\n");
844 gr_exit();
845 return -1;
846 }
847
848 get_memory_surface(&gr_mem_surface);
849
850 fprintf(stderr, "framebuffer: fd %d (%d x %d)\n",
851 gr_fb_fd, gr_framebuffer[0].width, gr_framebuffer[0].height);
852
853 /* start with 0 as front (displayed) and 1 as back (drawing) */
854 gr_active_fb = 0;
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600855 if (!has_overlay)
856 set_active_framebuffer(0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400857 gl->colorBuffer(gl, &gr_mem_surface);
858
859 gl->activeTexture(gl, 0);
860 gl->enable(gl, GGL_BLEND);
861 gl->blendFunc(gl, GGL_SRC_ALPHA, GGL_ONE_MINUS_SRC_ALPHA);
862
xNUTxcd56f8c2014-07-23 01:30:20 +0200863#ifdef TW_SCREEN_BLANK_ON_BOOT
864 printf("TW_SCREEN_BLANK_ON_BOOT := true\n");
865 gr_fb_blank(true);
866 gr_fb_blank(false);
867#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400868
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600869 if (!alloc_ion_mem(fi.line_length * vi.yres))
870 allocate_overlay(gr_fb_fd, gr_framebuffer);
871
Dees_Troy51a0e822012-09-05 15:24:24 -0400872 return 0;
873}
874
875void gr_exit(void)
876{
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600877 free_overlay(gr_fb_fd);
878 free_ion_mem();
879
Dees_Troy51a0e822012-09-05 15:24:24 -0400880 close(gr_fb_fd);
881 gr_fb_fd = -1;
882
883 free(gr_mem_surface.data);
884
885 ioctl(gr_vt_fd, KDSETMODE, (void*) KD_TEXT);
886 close(gr_vt_fd);
887 gr_vt_fd = -1;
888}
889
890int gr_fb_width(void)
891{
892 return gr_framebuffer[0].width;
893}
894
895int gr_fb_height(void)
896{
897 return gr_framebuffer[0].height;
898}
899
900gr_pixel *gr_fb_data(void)
901{
902 return (unsigned short *) gr_mem_surface.data;
903}
904
Dees_Troy70237dc2013-02-28 21:31:48 +0000905int gr_fb_blank(int blank)
Dees_Troy51a0e822012-09-05 15:24:24 -0400906{
907 int ret;
Dees Troy05d18c92014-08-04 18:17:07 +0000908 //if (blank)
909 //free_overlay(gr_fb_fd);
Dees_Troy51a0e822012-09-05 15:24:24 -0400910
911 ret = ioctl(gr_fb_fd, FBIOBLANK, blank ? FB_BLANK_POWERDOWN : FB_BLANK_UNBLANK);
912 if (ret < 0)
913 perror("ioctl(): blank");
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600914
Dees Troy05d18c92014-08-04 18:17:07 +0000915 //if (!blank)
916 //allocate_overlay(gr_fb_fd, gr_framebuffer);
917 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -0400918}
919
920int gr_get_surface(gr_surface* surface)
921{
922 GGLSurface* ms = malloc(sizeof(GGLSurface));
923 if (!ms) return -1;
924
925 // Allocate the data
926 get_memory_surface(ms);
927
928 // Now, copy the data
929 memcpy(ms->data, gr_mem_surface.data, vi.xres * vi.yres * vi.bits_per_pixel / 8);
930
931 *surface = (gr_surface*) ms;
932 return 0;
933}
934
935int gr_free_surface(gr_surface surface)
936{
937 if (!surface)
938 return -1;
939
940 GGLSurface* ms = (GGLSurface*) surface;
941 free(ms->data);
942 free(ms);
943 return 0;
944}
945
946void gr_write_frame_to_file(int fd)
947{
948 write(fd, gr_mem_surface.data, vi.xres * vi.yres * vi.bits_per_pixel / 8);
949}