blob: 6dfbc23b52e160f1839a88daaa9566872dc6114b [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
47#ifdef RECOVERY_RGBX
48#define PIXEL_FORMAT GGL_PIXEL_FORMAT_RGBX_8888
49#define PIXEL_SIZE 4
50#endif
51#ifndef PIXEL_FORMAT
52#define PIXEL_FORMAT GGL_PIXEL_FORMAT_RGB_565
53#define PIXEL_SIZE 2
54#endif
55
Hiemanshu Sharmaacf6a9b2012-11-21 11:28:36 -060056#define NUM_BUFFERS 2
Ethan Yonker4ee5ad72014-02-18 18:41:17 -060057#define MAX_DISPLAY_DIM 2048
Hiemanshu Sharmaacf6a9b2012-11-21 11:28:36 -060058
Dees_Troy51a0e822012-09-05 15:24:24 -040059// #define PRINT_SCREENINFO 1 // Enables printing of screen info to log
60
61typedef struct {
Vojtech Bocek76ee9032014-09-07 15:01:56 +020062 int type;
Dees_Troy51a0e822012-09-05 15:24:24 -040063 GGLSurface texture;
64 unsigned offset[97];
65 unsigned cheight;
66 unsigned ascent;
67} GRFont;
68
69static GRFont *gr_font = 0;
70static GGLContext *gr_context = 0;
71static GGLSurface gr_font_texture;
Hiemanshu Sharmaacf6a9b2012-11-21 11:28:36 -060072static GGLSurface gr_framebuffer[NUM_BUFFERS];
Vojtech Bocek03fd6c52014-03-13 18:46:34 +010073GGLSurface gr_mem_surface;
Dees_Troy51a0e822012-09-05 15:24:24 -040074static unsigned gr_active_fb = 0;
Hiemanshu Sharma94f6c882012-11-21 11:25:22 -060075static unsigned double_buffering = 0;
Vojtech Bocek1a4744a2014-02-06 19:52:28 +010076static int gr_is_curr_clr_opaque = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -040077
78static int gr_fb_fd = -1;
79static int gr_vt_fd = -1;
80
Vojtech Bocek03fd6c52014-03-13 18:46:34 +010081struct fb_var_screeninfo vi;
Dees_Troy51a0e822012-09-05 15:24:24 -040082static struct fb_fix_screeninfo fi;
83
Ethan Yonker4ee5ad72014-02-18 18:41:17 -060084static bool has_overlay = false;
85static int leftSplit = 0;
86static int rightSplit = 0;
87
88bool target_has_overlay(char *version);
89int free_ion_mem(void);
90int alloc_ion_mem(unsigned int size);
91int allocate_overlay(int fd, GGLSurface gr_fb[]);
92int free_overlay(int fd);
93int overlay_display_frame(int fd, GGLubyte* data, size_t size);
94
Dees_Troy51a0e822012-09-05 15:24:24 -040095#ifdef PRINT_SCREENINFO
96static void print_fb_var_screeninfo()
97{
Dees_Troy2673cec2013-04-02 20:22:16 +000098 printf("vi.xres: %d\n", vi.xres);
99 printf("vi.yres: %d\n", vi.yres);
100 printf("vi.xres_virtual: %d\n", vi.xres_virtual);
101 printf("vi.yres_virtual: %d\n", vi.yres_virtual);
102 printf("vi.xoffset: %d\n", vi.xoffset);
103 printf("vi.yoffset: %d\n", vi.yoffset);
104 printf("vi.bits_per_pixel: %d\n", vi.bits_per_pixel);
105 printf("vi.grayscale: %d\n", vi.grayscale);
Dees_Troy51a0e822012-09-05 15:24:24 -0400106}
107#endif
108
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600109#ifdef MSM_BSP
110int getLeftSplit(void) {
111 //Default even split for all displays with high res
112 int lSplit = vi.xres / 2;
113
114 //Override if split published by driver
115 if (leftSplit)
116 lSplit = leftSplit;
117
118 return lSplit;
119}
120
121int getRightSplit(void) {
122 return rightSplit;
123}
124
125
126void setDisplaySplit(void) {
127 char split[64] = {0};
128 FILE* fp = fopen("/sys/class/graphics/fb0/msm_fb_split", "r");
129 if (fp) {
130 //Format "left right" space as delimiter
131 if(fread(split, sizeof(char), 64, fp)) {
132 leftSplit = atoi(split);
133 printf("Left Split=%d\n",leftSplit);
134 char *rght = strpbrk(split, " ");
135 if (rght)
136 rightSplit = atoi(rght + 1);
137 printf("Right Split=%d\n", rightSplit);
138 }
139 } else {
140 printf("Failed to open mdss_fb_split node\n");
141 }
142 if (fp)
143 fclose(fp);
144}
145
146bool isDisplaySplit(void) {
147 if (vi.xres > MAX_DISPLAY_DIM)
148 return true;
149 //check if right split is set by driver
150 if (getRightSplit())
151 return true;
152
153 return false;
154}
155
156int getFbXres(void) {
157 return vi.xres;
158}
159
160int getFbYres (void) {
161 return vi.yres;
162}
163#endif // MSM_BSP
164
Dees_Troy51a0e822012-09-05 15:24:24 -0400165static int get_framebuffer(GGLSurface *fb)
166{
Ethan Yonker4f6a9762015-03-09 13:58:54 -0500167 int fd, index = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400168 void *bits;
169
170 fd = open("/dev/graphics/fb0", O_RDWR);
jenkins79699132015-05-08 06:04:44 -0400171
172 while (fd < 0 && index < 30) {
Ethan Yonker4f6a9762015-03-09 13:58:54 -0500173 usleep(1000);
174 fd = open("/dev/graphics/fb0", O_RDWR);
175 index++;
176 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400177 if (fd < 0) {
Ethan Yonker4f6a9762015-03-09 13:58:54 -0500178 perror("cannot open fb0\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400179 return -1;
180 }
181
182 if (ioctl(fd, FBIOGET_VSCREENINFO, &vi) < 0) {
183 perror("failed to get fb0 info");
184 close(fd);
185 return -1;
186 }
187
188 fprintf(stderr, "Pixel format: %dx%d @ %dbpp\n", vi.xres, vi.yres, vi.bits_per_pixel);
189
190 vi.bits_per_pixel = PIXEL_SIZE * 8;
191 if (PIXEL_FORMAT == GGL_PIXEL_FORMAT_BGRA_8888) {
192 fprintf(stderr, "Pixel format: BGRA_8888\n");
193 if (PIXEL_SIZE != 4) fprintf(stderr, "E: Pixel Size mismatch!\n");
194 vi.red.offset = 8;
195 vi.red.length = 8;
196 vi.green.offset = 16;
197 vi.green.length = 8;
198 vi.blue.offset = 24;
199 vi.blue.length = 8;
200 vi.transp.offset = 0;
201 vi.transp.length = 8;
202 } else if (PIXEL_FORMAT == GGL_PIXEL_FORMAT_RGBX_8888) {
203 fprintf(stderr, "Pixel format: RGBX_8888\n");
204 if (PIXEL_SIZE != 4) fprintf(stderr, "E: Pixel Size mismatch!\n");
205 vi.red.offset = 24;
206 vi.red.length = 8;
207 vi.green.offset = 16;
208 vi.green.length = 8;
209 vi.blue.offset = 8;
210 vi.blue.length = 8;
211 vi.transp.offset = 0;
212 vi.transp.length = 8;
213 } else if (PIXEL_FORMAT == GGL_PIXEL_FORMAT_RGB_565) {
214#ifdef RECOVERY_RGB_565
215 fprintf(stderr, "Pixel format: RGB_565\n");
216 vi.blue.offset = 0;
217 vi.green.offset = 5;
218 vi.red.offset = 11;
219#else
220 fprintf(stderr, "Pixel format: BGR_565\n");
221 vi.blue.offset = 11;
222 vi.green.offset = 5;
223 vi.red.offset = 0;
224#endif
225 if (PIXEL_SIZE != 2) fprintf(stderr, "E: Pixel Size mismatch!\n");
226 vi.blue.length = 5;
227 vi.green.length = 6;
228 vi.red.length = 5;
229 vi.blue.msb_right = 0;
230 vi.green.msb_right = 0;
231 vi.red.msb_right = 0;
232 vi.transp.offset = 0;
233 vi.transp.length = 0;
234 }
235 else
236 {
237 perror("unknown pixel format");
238 close(fd);
239 return -1;
240 }
241
242 vi.vmode = FB_VMODE_NONINTERLACED;
243 vi.activate = FB_ACTIVATE_NOW | FB_ACTIVATE_FORCE;
244
245 if (ioctl(fd, FBIOPUT_VSCREENINFO, &vi) < 0) {
246 perror("failed to put fb0 info");
247 close(fd);
248 return -1;
249 }
250
251 if (ioctl(fd, FBIOGET_FSCREENINFO, &fi) < 0) {
252 perror("failed to get fb0 info");
253 close(fd);
254 return -1;
255 }
256
Dees Troyb0425382014-04-03 00:10:03 +0000257#ifdef MSM_BSP
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600258 has_overlay = target_has_overlay(fi.id);
259
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600260 if (isTargetMdp5())
261 setDisplaySplit();
Dees Troyb0425382014-04-03 00:10:03 +0000262#else
263 has_overlay = false;
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600264#endif
265
266 if (!has_overlay) {
267 printf("Not using qualcomm overlay, '%s'\n", fi.id);
268 bits = mmap(0, fi.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
269 if (bits == MAP_FAILED) {
270 perror("failed to mmap framebuffer");
271 close(fd);
272 return -1;
273 }
274 } else {
275 printf("Using qualcomm overlay\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400276 }
277
278#ifdef RECOVERY_GRAPHICS_USE_LINELENGTH
279 vi.xres_virtual = fi.line_length / PIXEL_SIZE;
280#endif
281
282 fb->version = sizeof(*fb);
283 fb->width = vi.xres;
284 fb->height = vi.yres;
285#ifdef BOARD_HAS_JANKY_BACKBUFFER
Dees_Troy2673cec2013-04-02 20:22:16 +0000286 printf("setting JANKY BACKBUFFER\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400287 fb->stride = fi.line_length/2;
288#else
289 fb->stride = vi.xres_virtual;
290#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400291 fb->format = PIXEL_FORMAT;
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600292 if (!has_overlay) {
293 fb->data = bits;
294 memset(fb->data, 0, vi.yres * fb->stride * PIXEL_SIZE);
295 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400296
297 fb++;
298
Kra1o5c9556cc2015-06-24 12:19:09 +0200299#ifndef TW_DISABLE_DOUBLE_BUFFERING
Hiemanshu Sharma94f6c882012-11-21 11:25:22 -0600300 /* check if we can use double buffering */
301 if (vi.yres * fi.line_length * 2 > fi.smem_len)
Kra1o5c9556cc2015-06-24 12:19:09 +0200302#else
303 printf("TW_DISABLE_DOUBLE_BUFFERING := true\n");
304#endif
Hiemanshu Sharma94f6c882012-11-21 11:25:22 -0600305 return fd;
306
307 double_buffering = 1;
308
Dees_Troy51a0e822012-09-05 15:24:24 -0400309 fb->version = sizeof(*fb);
310 fb->width = vi.xres;
311 fb->height = vi.yres;
312#ifdef BOARD_HAS_JANKY_BACKBUFFER
313 fb->stride = fi.line_length/2;
Ethan Yonkerbcc502c2014-11-10 11:22:10 -0600314 fb->data = (GGLubyte*) (((unsigned long) bits) + vi.yres * fi.line_length);
Dees_Troy51a0e822012-09-05 15:24:24 -0400315#else
316 fb->stride = vi.xres_virtual;
Ethan Yonkerbcc502c2014-11-10 11:22:10 -0600317 fb->data = (GGLubyte*) (((unsigned long) bits) + vi.yres * fb->stride * PIXEL_SIZE);
Dees_Troy51a0e822012-09-05 15:24:24 -0400318#endif
319 fb->format = PIXEL_FORMAT;
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600320 if (!has_overlay) {
321 memset(fb->data, 0, vi.yres * fb->stride * PIXEL_SIZE);
322 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400323
324#ifdef PRINT_SCREENINFO
325 print_fb_var_screeninfo();
326#endif
327
328 return fd;
329}
330
331static void get_memory_surface(GGLSurface* ms) {
332 ms->version = sizeof(*ms);
333 ms->width = vi.xres;
334 ms->height = vi.yres;
335 ms->stride = vi.xres_virtual;
336 ms->data = malloc(vi.xres_virtual * vi.yres * PIXEL_SIZE);
337 ms->format = PIXEL_FORMAT;
338}
339
340static void set_active_framebuffer(unsigned n)
341{
Hiemanshu Sharma94f6c882012-11-21 11:25:22 -0600342 if (n > 1 || !double_buffering) return;
Hiemanshu Sharmaacf6a9b2012-11-21 11:28:36 -0600343 vi.yres_virtual = vi.yres * NUM_BUFFERS;
Dees_Troy51a0e822012-09-05 15:24:24 -0400344 vi.yoffset = n * vi.yres;
345// vi.bits_per_pixel = PIXEL_SIZE * 8;
346 if (ioctl(gr_fb_fd, FBIOPUT_VSCREENINFO, &vi) < 0) {
347 perror("active fb swap failed");
348 }
349}
350
351void gr_flip(void)
352{
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600353 if (-EINVAL == overlay_display_frame(gr_fb_fd, gr_mem_surface.data,
354 (fi.line_length * vi.yres))) {
355 GGLContext *gl = gr_context;
Dees_Troy51a0e822012-09-05 15:24:24 -0400356
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600357 /* swap front and back buffers */
358 if (double_buffering)
359 gr_active_fb = (gr_active_fb + 1) & 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400360
361#ifdef BOARD_HAS_FLIPPED_SCREEN
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600362 /* flip buffer 180 degrees for devices with physicaly inverted screens */
363 unsigned int i;
364 unsigned int j;
Bogdan Seniucd1372f72015-01-23 08:22:39 -0800365 for (i = 0; i < vi.yres; i++) {
366 for (j = 0; j < vi.xres; j++) {
367 memcpy(gr_framebuffer[gr_active_fb].data + (i * vi.xres_virtual + j) * PIXEL_SIZE,
368 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 -0600369 }
370 }
Bogdan Seniucd1372f72015-01-23 08:22:39 -0800371#else
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600372 /* copy data from the in-memory surface to the buffer we're about
373 * to make active. */
374 memcpy(gr_framebuffer[gr_active_fb].data, gr_mem_surface.data,
375 vi.xres_virtual * vi.yres * PIXEL_SIZE);
Bogdan Seniucd1372f72015-01-23 08:22:39 -0800376#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400377
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600378 /* inform the display driver */
379 set_active_framebuffer(gr_active_fb);
380 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400381}
382
383void gr_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
384{
385 GGLContext *gl = gr_context;
386 GGLint color[4];
387 color[0] = ((r << 8) | r) + 1;
388 color[1] = ((g << 8) | g) + 1;
389 color[2] = ((b << 8) | b) + 1;
390 color[3] = ((a << 8) | a) + 1;
391 gl->color4xv(gl, color);
Vojtech Bocek1a4744a2014-02-06 19:52:28 +0100392
393 gr_is_curr_clr_opaque = (a == 255);
Dees_Troy51a0e822012-09-05 15:24:24 -0400394}
395
396int gr_measureEx(const char *s, void* font)
397{
398 GRFont* fnt = (GRFont*) font;
399 int total = 0;
400 unsigned pos;
401 unsigned off;
402
403 if (!fnt) fnt = gr_font;
404
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200405#ifndef TW_DISABLE_TTF
406 if(fnt->type == FONT_TYPE_TTF)
407 return gr_ttf_measureEx(s, font);
408#endif
409
Dees_Troy51a0e822012-09-05 15:24:24 -0400410 while ((off = *s++))
411 {
412 off -= 32;
413 if (off < 96)
414 total += (fnt->offset[off+1] - fnt->offset[off]);
415 }
416 return total;
417}
418
Dees Troy31218ec2014-02-25 20:35:56 +0000419int gr_maxExW(const char *s, void* font, int max_width)
420{
421 GRFont* fnt = (GRFont*) font;
422 int total = 0;
423 unsigned pos;
424 unsigned off;
425
426 if (!fnt) fnt = gr_font;
427
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200428#ifndef TW_DISABLE_TTF
429 if(fnt->type == FONT_TYPE_TTF)
430 return gr_ttf_maxExW(s, font, max_width);
431#endif
432
Dees Troy31218ec2014-02-25 20:35:56 +0000433 while ((off = *s++))
434 {
435 off -= 32;
436 if (off < 96) {
437 max_width -= (fnt->offset[off+1] - fnt->offset[off]);
438 if (max_width > 0) {
439 total++;
440 } else {
441 return total;
442 }
443 }
444 }
445 return total;
446}
447
Dees_Troy51a0e822012-09-05 15:24:24 -0400448int gr_textEx(int x, int y, const char *s, void* pFont)
449{
450 GGLContext *gl = gr_context;
451 GRFont *font = (GRFont*) pFont;
452 unsigned off;
453 unsigned cwidth;
454
455 /* Handle default font */
456 if (!font) font = gr_font;
457
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200458#ifndef TW_DISABLE_TTF
459 if(font->type == FONT_TYPE_TTF)
460 return gr_ttf_textExWH(gl, x, y, s, pFont, -1, -1);
461#endif
462
Dees_Troy51a0e822012-09-05 15:24:24 -0400463 gl->bindTexture(gl, &font->texture);
464 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
465 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
466 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
467 gl->enable(gl, GGL_TEXTURE_2D);
468
469 while((off = *s++)) {
470 off -= 32;
471 cwidth = 0;
472 if (off < 96) {
473 cwidth = font->offset[off+1] - font->offset[off];
474 gl->texCoord2i(gl, (font->offset[off]) - x, 0 - y);
475 gl->recti(gl, x, y, x + cwidth, y + font->cheight);
476 x += cwidth;
477 }
478 }
479
Vojtech Bocek3041c882015-03-06 00:28:21 +0100480 gl->disable(gl, GGL_TEXTURE_2D);
481
Dees_Troy51a0e822012-09-05 15:24:24 -0400482 return x;
483}
484
485int gr_textExW(int x, int y, const char *s, void* pFont, int max_width)
486{
487 GGLContext *gl = gr_context;
488 GRFont *font = (GRFont*) pFont;
489 unsigned off;
490 unsigned cwidth;
491
492 /* Handle default font */
493 if (!font) font = gr_font;
494
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200495#ifndef TW_DISABLE_TTF
496 if(font->type == FONT_TYPE_TTF)
497 return gr_ttf_textExWH(gl, x, y, s, pFont, max_width, -1);
498#endif
499
Dees_Troy51a0e822012-09-05 15:24:24 -0400500 gl->bindTexture(gl, &font->texture);
501 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
502 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
503 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
504 gl->enable(gl, GGL_TEXTURE_2D);
505
506 while((off = *s++)) {
507 off -= 32;
508 cwidth = 0;
509 if (off < 96) {
510 cwidth = font->offset[off+1] - font->offset[off];
511 if ((x + (int)cwidth) < max_width) {
512 gl->texCoord2i(gl, (font->offset[off]) - x, 0 - y);
513 gl->recti(gl, x, y, x + cwidth, y + font->cheight);
514 x += cwidth;
515 } else {
516 gl->texCoord2i(gl, (font->offset[off]) - x, 0 - y);
517 gl->recti(gl, x, y, max_width, y + font->cheight);
518 x = max_width;
519 return x;
520 }
521 }
522 }
523
Vojtech Bocek3041c882015-03-06 00:28:21 +0100524 gl->disable(gl, GGL_TEXTURE_2D);
525
Dees_Troy51a0e822012-09-05 15:24:24 -0400526 return x;
527}
528
529int gr_textExWH(int x, int y, const char *s, void* pFont, int max_width, int max_height)
530{
531 GGLContext *gl = gr_context;
532 GRFont *font = (GRFont*) pFont;
533 unsigned off;
534 unsigned cwidth;
535 int rect_x, rect_y;
536
537 /* Handle default font */
538 if (!font) font = gr_font;
539
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200540#ifndef TW_DISABLE_TTF
541 if(font->type == FONT_TYPE_TTF)
542 return gr_ttf_textExWH(gl, x, y, s, pFont, max_width, max_height);
543#endif
544
Dees_Troy51a0e822012-09-05 15:24:24 -0400545 gl->bindTexture(gl, &font->texture);
546 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
547 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
548 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
549 gl->enable(gl, GGL_TEXTURE_2D);
550
551 while((off = *s++)) {
552 off -= 32;
553 cwidth = 0;
554 if (off < 96) {
555 cwidth = font->offset[off+1] - font->offset[off];
556 if ((x + (int)cwidth) < max_width)
557 rect_x = x + cwidth;
558 else
559 rect_x = max_width;
Dees_Troyf7596752012-09-28 13:21:36 -0400560 if (y + font->cheight < (unsigned int)(max_height))
Dees_Troy51a0e822012-09-05 15:24:24 -0400561 rect_y = y + font->cheight;
562 else
563 rect_y = max_height;
564
565 gl->texCoord2i(gl, (font->offset[off]) - x, 0 - y);
566 gl->recti(gl, x, y, rect_x, rect_y);
567 x += cwidth;
568 if (x > max_width)
569 return x;
570 }
571 }
572
Vojtech Bocek3041c882015-03-06 00:28:21 +0100573 gl->disable(gl, GGL_TEXTURE_2D);
574
Dees_Troy51a0e822012-09-05 15:24:24 -0400575 return x;
576}
577
that9876ac32015-02-15 21:40:59 +0100578void gr_clip(int x, int y, int w, int h)
579{
580 GGLContext *gl = gr_context;
581 gl->scissor(gl, x, y, w, h);
582 gl->enable(gl, GGL_SCISSOR_TEST);
583}
584
585void gr_noclip()
586{
587 GGLContext *gl = gr_context;
588 gl->scissor(gl, 0, 0, gr_fb_width(), gr_fb_height());
589 gl->disable(gl, GGL_SCISSOR_TEST);
590}
591
Dees_Troy51a0e822012-09-05 15:24:24 -0400592void gr_fill(int x, int y, int w, int h)
593{
594 GGLContext *gl = gr_context;
Vojtech Bocek1a4744a2014-02-06 19:52:28 +0100595
596 if(gr_is_curr_clr_opaque)
597 gl->disable(gl, GGL_BLEND);
598
Dees_Troy51a0e822012-09-05 15:24:24 -0400599 gl->recti(gl, x, y, x + w, y + h);
Vojtech Bocek1a4744a2014-02-06 19:52:28 +0100600
601 if(gr_is_curr_clr_opaque)
602 gl->enable(gl, GGL_BLEND);
Dees_Troy51a0e822012-09-05 15:24:24 -0400603}
604
Vojtech Bocekdb378b62015-03-05 20:02:57 +0100605void gr_line(int x0, int y0, int x1, int y1, int width)
606{
607 GGLContext *gl = gr_context;
608
609 if(gr_is_curr_clr_opaque)
610 gl->disable(gl, GGL_BLEND);
611
612 const int coords0[2] = { x0 << 4, y0 << 4 };
613 const int coords1[2] = { x1 << 4, y1 << 4 };
614 gl->linex(gl, coords0, coords1, width << 4);
615
616 if(gr_is_curr_clr_opaque)
617 gl->enable(gl, GGL_BLEND);
618}
619
620gr_surface gr_render_circle(int radius, unsigned char r, unsigned char g, unsigned char b, unsigned char a)
621{
622 int rx, ry;
623 GGLSurface *surface;
624 const int diameter = radius*2 + 1;
625 const int radius_check = radius*radius + radius*0.8;
626 const uint32_t px = (a << 24) | (b << 16) | (g << 8) | r;
627 uint32_t *data;
628
629 surface = malloc(sizeof(GGLSurface));
630 memset(surface, 0, sizeof(GGLSurface));
631
632 data = malloc(diameter * diameter * 4);
633 memset(data, 0, diameter * diameter * 4);
634
635 surface->version = sizeof(surface);
636 surface->width = diameter;
637 surface->height = diameter;
638 surface->stride = diameter;
639 surface->data = (GGLubyte*)data;
640 surface->format = GGL_PIXEL_FORMAT_RGBA_8888;
641
642 for(ry = -radius; ry <= radius; ++ry)
643 for(rx = -radius; rx <= radius; ++rx)
644 if(rx*rx+ry*ry <= radius_check)
645 *(data + diameter*(radius + ry) + (radius+rx)) = px;
646
647 return surface;
648}
649
Dees_Troy51a0e822012-09-05 15:24:24 -0400650void gr_blit(gr_surface source, int sx, int sy, int w, int h, int dx, int dy) {
Dees Troyf171e102014-11-06 22:19:58 +0100651 if (gr_context == NULL) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400652 return;
653 }
654
655 GGLContext *gl = gr_context;
Vojtech Bocek6c694b62014-02-06 20:36:25 +0100656 GGLSurface *surface = (GGLSurface*)source;
657
658 if(surface->format == GGL_PIXEL_FORMAT_RGBX_8888)
659 gl->disable(gl, GGL_BLEND);
660
661 gl->bindTexture(gl, surface);
Dees_Troy51a0e822012-09-05 15:24:24 -0400662 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
663 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
664 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
665 gl->enable(gl, GGL_TEXTURE_2D);
666 gl->texCoord2i(gl, sx - dx, sy - dy);
667 gl->recti(gl, dx, dy, dx + w, dy + h);
Vojtech Bocek3041c882015-03-06 00:28:21 +0100668 gl->disable(gl, GGL_TEXTURE_2D);
Vojtech Bocek6c694b62014-02-06 20:36:25 +0100669
670 if(surface->format == GGL_PIXEL_FORMAT_RGBX_8888)
Dees Troyf171e102014-11-06 22:19:58 +0100671 gl->enable(gl, GGL_BLEND);
Dees_Troy51a0e822012-09-05 15:24:24 -0400672}
673
674unsigned int gr_get_width(gr_surface surface) {
675 if (surface == NULL) {
676 return 0;
677 }
678 return ((GGLSurface*) surface)->width;
679}
680
681unsigned int gr_get_height(gr_surface surface) {
682 if (surface == NULL) {
683 return 0;
684 }
685 return ((GGLSurface*) surface)->height;
686}
687
688void* gr_loadFont(const char* fontName)
689{
690 int fd;
691 GRFont *font = 0;
692 GGLSurface *ftex;
693 unsigned char *bits, *rle;
694 unsigned char *in, data;
695 unsigned width, height;
696 unsigned element;
697
698 fd = open(fontName, O_RDONLY);
699 if (fd == -1)
700 {
701 char tmp[128];
702
Dees Troy3454ade2015-01-20 19:21:04 +0000703 sprintf(tmp, TWRES "fonts/%s.dat", fontName);
Dees_Troy51a0e822012-09-05 15:24:24 -0400704 fd = open(tmp, O_RDONLY);
705 if (fd == -1)
706 return NULL;
707 }
708
709 font = calloc(sizeof(*font), 1);
710 ftex = &font->texture;
711
712 read(fd, &width, sizeof(unsigned));
713 read(fd, &height, sizeof(unsigned));
714 read(fd, font->offset, sizeof(unsigned) * 96);
715 font->offset[96] = width;
716
717 bits = malloc(width * height);
718 memset(bits, 0, width * height);
719
720 unsigned pos = 0;
721 while (pos < width * height)
722 {
723 int bit;
724
725 read(fd, &data, 1);
726 for (bit = 0; bit < 8; bit++)
727 {
728 if (data & (1 << (7-bit))) bits[pos++] = 255;
729 else bits[pos++] = 0;
730
731 if (pos == width * height) break;
732 }
733 }
734 close(fd);
735
736 ftex->version = sizeof(*ftex);
737 ftex->width = width;
738 ftex->height = height;
739 ftex->stride = width;
740 ftex->data = (void*) bits;
741 ftex->format = GGL_PIXEL_FORMAT_A_8;
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200742 font->type = FONT_TYPE_TWRP;
Dees_Troy51a0e822012-09-05 15:24:24 -0400743 font->cheight = height;
744 font->ascent = height - 2;
745 return (void*) font;
746}
747
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200748void gr_freeFont(void *font)
749{
750 GRFont *f = font;
751 free(f->texture.data);
752 free(f);
753}
754
755int gr_getMaxFontHeight(void *font)
Dees_Troy51a0e822012-09-05 15:24:24 -0400756{
757 GRFont *fnt = (GRFont*) font;
758
759 if (!fnt) fnt = gr_font;
760 if (!fnt) return -1;
761
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200762#ifndef TW_DISABLE_TTF
763 if(fnt->type == FONT_TYPE_TTF)
764 return gr_ttf_getMaxFontHeight(font);
765#endif
766
767 return fnt->cheight;
Dees_Troy51a0e822012-09-05 15:24:24 -0400768}
769
770static void gr_init_font(void)
771{
772 int fontRes;
773 GGLSurface *ftex;
774 unsigned char *bits, *rle;
775 unsigned char *in, data;
776 unsigned width, height;
777 unsigned element;
778
779 gr_font = calloc(sizeof(*gr_font), 1);
780 ftex = &gr_font->texture;
781
782 width = font.width;
783 height = font.height;
784
785 bits = malloc(width * height);
786 rle = bits;
787
788 in = font.rundata;
789 while((data = *in++))
790 {
791 memset(rle, (data & 0x80) ? 255 : 0, data & 0x7f);
792 rle += (data & 0x7f);
793 }
794 for (element = 0; element < 97; element++)
795 {
796 gr_font->offset[element] = (element * font.cwidth);
797 }
798
799 ftex->version = sizeof(*ftex);
800 ftex->width = width;
801 ftex->height = height;
802 ftex->stride = width;
803 ftex->data = (void*) bits;
804 ftex->format = GGL_PIXEL_FORMAT_A_8;
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200805 gr_font->type = FONT_TYPE_TWRP;
Dees_Troy51a0e822012-09-05 15:24:24 -0400806 gr_font->cheight = height;
807 gr_font->ascent = height - 2;
808 return;
809}
810
811int gr_init(void)
812{
813 gglInit(&gr_context);
814 GGLContext *gl = gr_context;
815
816 gr_init_font();
817 gr_vt_fd = open("/dev/tty0", O_RDWR | O_SYNC);
818 if (gr_vt_fd < 0) {
819 // This is non-fatal; post-Cupcake kernels don't have tty0.
820 } else if (ioctl(gr_vt_fd, KDSETMODE, (void*) KD_GRAPHICS)) {
821 // However, if we do open tty0, we expect the ioctl to work.
822 perror("failed KDSETMODE to KD_GRAPHICS on tty0");
823 gr_exit();
824 return -1;
825 }
826
827 gr_fb_fd = get_framebuffer(gr_framebuffer);
828 if (gr_fb_fd < 0) {
829 perror("Unable to get framebuffer.\n");
830 gr_exit();
831 return -1;
832 }
833
834 get_memory_surface(&gr_mem_surface);
835
836 fprintf(stderr, "framebuffer: fd %d (%d x %d)\n",
837 gr_fb_fd, gr_framebuffer[0].width, gr_framebuffer[0].height);
838
839 /* start with 0 as front (displayed) and 1 as back (drawing) */
840 gr_active_fb = 0;
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600841 if (!has_overlay)
842 set_active_framebuffer(0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400843 gl->colorBuffer(gl, &gr_mem_surface);
844
845 gl->activeTexture(gl, 0);
846 gl->enable(gl, GGL_BLEND);
847 gl->blendFunc(gl, GGL_SRC_ALPHA, GGL_ONE_MINUS_SRC_ALPHA);
848
xNUTxcd56f8c2014-07-23 01:30:20 +0200849#ifdef TW_SCREEN_BLANK_ON_BOOT
850 printf("TW_SCREEN_BLANK_ON_BOOT := true\n");
851 gr_fb_blank(true);
852 gr_fb_blank(false);
853#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400854
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600855 if (!alloc_ion_mem(fi.line_length * vi.yres))
856 allocate_overlay(gr_fb_fd, gr_framebuffer);
857
Dees_Troy51a0e822012-09-05 15:24:24 -0400858 return 0;
859}
860
861void gr_exit(void)
862{
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600863 free_overlay(gr_fb_fd);
864 free_ion_mem();
865
Dees_Troy51a0e822012-09-05 15:24:24 -0400866 close(gr_fb_fd);
867 gr_fb_fd = -1;
868
869 free(gr_mem_surface.data);
870
871 ioctl(gr_vt_fd, KDSETMODE, (void*) KD_TEXT);
872 close(gr_vt_fd);
873 gr_vt_fd = -1;
874}
875
876int gr_fb_width(void)
877{
878 return gr_framebuffer[0].width;
879}
880
881int gr_fb_height(void)
882{
883 return gr_framebuffer[0].height;
884}
885
886gr_pixel *gr_fb_data(void)
887{
888 return (unsigned short *) gr_mem_surface.data;
889}
890
Dees_Troy70237dc2013-02-28 21:31:48 +0000891int gr_fb_blank(int blank)
Dees_Troy51a0e822012-09-05 15:24:24 -0400892{
893 int ret;
Dees Troy05d18c92014-08-04 18:17:07 +0000894 //if (blank)
895 //free_overlay(gr_fb_fd);
Dees_Troy51a0e822012-09-05 15:24:24 -0400896
897 ret = ioctl(gr_fb_fd, FBIOBLANK, blank ? FB_BLANK_POWERDOWN : FB_BLANK_UNBLANK);
898 if (ret < 0)
899 perror("ioctl(): blank");
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600900
Dees Troy05d18c92014-08-04 18:17:07 +0000901 //if (!blank)
902 //allocate_overlay(gr_fb_fd, gr_framebuffer);
903 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -0400904}
905
906int gr_get_surface(gr_surface* surface)
907{
908 GGLSurface* ms = malloc(sizeof(GGLSurface));
909 if (!ms) return -1;
910
911 // Allocate the data
912 get_memory_surface(ms);
913
914 // Now, copy the data
915 memcpy(ms->data, gr_mem_surface.data, vi.xres * vi.yres * vi.bits_per_pixel / 8);
916
917 *surface = (gr_surface*) ms;
918 return 0;
919}
920
921int gr_free_surface(gr_surface surface)
922{
923 if (!surface)
924 return -1;
925
926 GGLSurface* ms = (GGLSurface*) surface;
927 free(ms->data);
928 free(ms);
929 return 0;
930}
931
932void gr_write_frame_to_file(int fd)
933{
934 write(fd, gr_mem_surface.data, vi.xres * vi.yres * vi.bits_per_pixel / 8);
935}