blob: 1465ce99bd281f723b75ccab79f5beb341d4434d [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);
Ethan Yonker4f6a9762015-03-09 13:58:54 -0500170 while (fd < 0 && index < 10) {
171 usleep(1000);
172 fd = open("/dev/graphics/fb0", O_RDWR);
173 index++;
174 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400175 if (fd < 0) {
Ethan Yonker4f6a9762015-03-09 13:58:54 -0500176 perror("cannot open fb0\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400177 return -1;
178 }
179
180 if (ioctl(fd, FBIOGET_VSCREENINFO, &vi) < 0) {
181 perror("failed to get fb0 info");
182 close(fd);
183 return -1;
184 }
185
186 fprintf(stderr, "Pixel format: %dx%d @ %dbpp\n", vi.xres, vi.yres, vi.bits_per_pixel);
187
188 vi.bits_per_pixel = PIXEL_SIZE * 8;
189 if (PIXEL_FORMAT == GGL_PIXEL_FORMAT_BGRA_8888) {
190 fprintf(stderr, "Pixel format: BGRA_8888\n");
191 if (PIXEL_SIZE != 4) fprintf(stderr, "E: Pixel Size mismatch!\n");
192 vi.red.offset = 8;
193 vi.red.length = 8;
194 vi.green.offset = 16;
195 vi.green.length = 8;
196 vi.blue.offset = 24;
197 vi.blue.length = 8;
198 vi.transp.offset = 0;
199 vi.transp.length = 8;
200 } else if (PIXEL_FORMAT == GGL_PIXEL_FORMAT_RGBX_8888) {
201 fprintf(stderr, "Pixel format: RGBX_8888\n");
202 if (PIXEL_SIZE != 4) fprintf(stderr, "E: Pixel Size mismatch!\n");
203 vi.red.offset = 24;
204 vi.red.length = 8;
205 vi.green.offset = 16;
206 vi.green.length = 8;
207 vi.blue.offset = 8;
208 vi.blue.length = 8;
209 vi.transp.offset = 0;
210 vi.transp.length = 8;
211 } else if (PIXEL_FORMAT == GGL_PIXEL_FORMAT_RGB_565) {
212#ifdef RECOVERY_RGB_565
213 fprintf(stderr, "Pixel format: RGB_565\n");
214 vi.blue.offset = 0;
215 vi.green.offset = 5;
216 vi.red.offset = 11;
217#else
218 fprintf(stderr, "Pixel format: BGR_565\n");
219 vi.blue.offset = 11;
220 vi.green.offset = 5;
221 vi.red.offset = 0;
222#endif
223 if (PIXEL_SIZE != 2) fprintf(stderr, "E: Pixel Size mismatch!\n");
224 vi.blue.length = 5;
225 vi.green.length = 6;
226 vi.red.length = 5;
227 vi.blue.msb_right = 0;
228 vi.green.msb_right = 0;
229 vi.red.msb_right = 0;
230 vi.transp.offset = 0;
231 vi.transp.length = 0;
232 }
233 else
234 {
235 perror("unknown pixel format");
236 close(fd);
237 return -1;
238 }
239
240 vi.vmode = FB_VMODE_NONINTERLACED;
241 vi.activate = FB_ACTIVATE_NOW | FB_ACTIVATE_FORCE;
242
243 if (ioctl(fd, FBIOPUT_VSCREENINFO, &vi) < 0) {
244 perror("failed to put fb0 info");
245 close(fd);
246 return -1;
247 }
248
249 if (ioctl(fd, FBIOGET_FSCREENINFO, &fi) < 0) {
250 perror("failed to get fb0 info");
251 close(fd);
252 return -1;
253 }
254
Dees Troyb0425382014-04-03 00:10:03 +0000255#ifdef MSM_BSP
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600256 has_overlay = target_has_overlay(fi.id);
257
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600258 if (isTargetMdp5())
259 setDisplaySplit();
Dees Troyb0425382014-04-03 00:10:03 +0000260#else
261 has_overlay = false;
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600262#endif
263
264 if (!has_overlay) {
265 printf("Not using qualcomm overlay, '%s'\n", fi.id);
266 bits = mmap(0, fi.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
267 if (bits == MAP_FAILED) {
268 perror("failed to mmap framebuffer");
269 close(fd);
270 return -1;
271 }
272 } else {
273 printf("Using qualcomm overlay\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400274 }
275
276#ifdef RECOVERY_GRAPHICS_USE_LINELENGTH
277 vi.xres_virtual = fi.line_length / PIXEL_SIZE;
278#endif
279
280 fb->version = sizeof(*fb);
281 fb->width = vi.xres;
282 fb->height = vi.yres;
283#ifdef BOARD_HAS_JANKY_BACKBUFFER
Dees_Troy2673cec2013-04-02 20:22:16 +0000284 printf("setting JANKY BACKBUFFER\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400285 fb->stride = fi.line_length/2;
286#else
287 fb->stride = vi.xres_virtual;
288#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400289 fb->format = PIXEL_FORMAT;
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600290 if (!has_overlay) {
291 fb->data = bits;
292 memset(fb->data, 0, vi.yres * fb->stride * PIXEL_SIZE);
293 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400294
295 fb++;
296
Hiemanshu Sharma94f6c882012-11-21 11:25:22 -0600297 /* check if we can use double buffering */
298 if (vi.yres * fi.line_length * 2 > fi.smem_len)
299 return fd;
300
301 double_buffering = 1;
302
Dees_Troy51a0e822012-09-05 15:24:24 -0400303 fb->version = sizeof(*fb);
304 fb->width = vi.xres;
305 fb->height = vi.yres;
306#ifdef BOARD_HAS_JANKY_BACKBUFFER
307 fb->stride = fi.line_length/2;
Ethan Yonkerbcc502c2014-11-10 11:22:10 -0600308 fb->data = (GGLubyte*) (((unsigned long) bits) + vi.yres * fi.line_length);
Dees_Troy51a0e822012-09-05 15:24:24 -0400309#else
310 fb->stride = vi.xres_virtual;
Ethan Yonkerbcc502c2014-11-10 11:22:10 -0600311 fb->data = (GGLubyte*) (((unsigned long) bits) + vi.yres * fb->stride * PIXEL_SIZE);
Dees_Troy51a0e822012-09-05 15:24:24 -0400312#endif
313 fb->format = PIXEL_FORMAT;
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600314 if (!has_overlay) {
315 memset(fb->data, 0, vi.yres * fb->stride * PIXEL_SIZE);
316 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400317
318#ifdef PRINT_SCREENINFO
319 print_fb_var_screeninfo();
320#endif
321
322 return fd;
323}
324
325static void get_memory_surface(GGLSurface* ms) {
326 ms->version = sizeof(*ms);
327 ms->width = vi.xres;
328 ms->height = vi.yres;
329 ms->stride = vi.xres_virtual;
330 ms->data = malloc(vi.xres_virtual * vi.yres * PIXEL_SIZE);
331 ms->format = PIXEL_FORMAT;
332}
333
334static void set_active_framebuffer(unsigned n)
335{
Hiemanshu Sharma94f6c882012-11-21 11:25:22 -0600336 if (n > 1 || !double_buffering) return;
Hiemanshu Sharmaacf6a9b2012-11-21 11:28:36 -0600337 vi.yres_virtual = vi.yres * NUM_BUFFERS;
Dees_Troy51a0e822012-09-05 15:24:24 -0400338 vi.yoffset = n * vi.yres;
339// vi.bits_per_pixel = PIXEL_SIZE * 8;
340 if (ioctl(gr_fb_fd, FBIOPUT_VSCREENINFO, &vi) < 0) {
341 perror("active fb swap failed");
342 }
343}
344
345void gr_flip(void)
346{
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600347 if (-EINVAL == overlay_display_frame(gr_fb_fd, gr_mem_surface.data,
348 (fi.line_length * vi.yres))) {
349 GGLContext *gl = gr_context;
Dees_Troy51a0e822012-09-05 15:24:24 -0400350
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600351 /* swap front and back buffers */
352 if (double_buffering)
353 gr_active_fb = (gr_active_fb + 1) & 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400354
355#ifdef BOARD_HAS_FLIPPED_SCREEN
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600356 /* flip buffer 180 degrees for devices with physicaly inverted screens */
357 unsigned int i;
358 unsigned int j;
Bogdan Seniucd1372f72015-01-23 08:22:39 -0800359 for (i = 0; i < vi.yres; i++) {
360 for (j = 0; j < vi.xres; j++) {
361 memcpy(gr_framebuffer[gr_active_fb].data + (i * vi.xres_virtual + j) * PIXEL_SIZE,
362 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 -0600363 }
364 }
Bogdan Seniucd1372f72015-01-23 08:22:39 -0800365#else
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600366 /* copy data from the in-memory surface to the buffer we're about
367 * to make active. */
368 memcpy(gr_framebuffer[gr_active_fb].data, gr_mem_surface.data,
369 vi.xres_virtual * vi.yres * PIXEL_SIZE);
Bogdan Seniucd1372f72015-01-23 08:22:39 -0800370#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400371
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600372 /* inform the display driver */
373 set_active_framebuffer(gr_active_fb);
374 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400375}
376
377void gr_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
378{
379 GGLContext *gl = gr_context;
380 GGLint color[4];
381 color[0] = ((r << 8) | r) + 1;
382 color[1] = ((g << 8) | g) + 1;
383 color[2] = ((b << 8) | b) + 1;
384 color[3] = ((a << 8) | a) + 1;
385 gl->color4xv(gl, color);
Vojtech Bocek1a4744a2014-02-06 19:52:28 +0100386
387 gr_is_curr_clr_opaque = (a == 255);
Dees_Troy51a0e822012-09-05 15:24:24 -0400388}
389
390int gr_measureEx(const char *s, void* font)
391{
392 GRFont* fnt = (GRFont*) font;
393 int total = 0;
394 unsigned pos;
395 unsigned off;
396
397 if (!fnt) fnt = gr_font;
398
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200399#ifndef TW_DISABLE_TTF
400 if(fnt->type == FONT_TYPE_TTF)
401 return gr_ttf_measureEx(s, font);
402#endif
403
Dees_Troy51a0e822012-09-05 15:24:24 -0400404 while ((off = *s++))
405 {
406 off -= 32;
407 if (off < 96)
408 total += (fnt->offset[off+1] - fnt->offset[off]);
409 }
410 return total;
411}
412
Dees Troy31218ec2014-02-25 20:35:56 +0000413int gr_maxExW(const char *s, void* font, int max_width)
414{
415 GRFont* fnt = (GRFont*) font;
416 int total = 0;
417 unsigned pos;
418 unsigned off;
419
420 if (!fnt) fnt = gr_font;
421
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200422#ifndef TW_DISABLE_TTF
423 if(fnt->type == FONT_TYPE_TTF)
424 return gr_ttf_maxExW(s, font, max_width);
425#endif
426
Dees Troy31218ec2014-02-25 20:35:56 +0000427 while ((off = *s++))
428 {
429 off -= 32;
430 if (off < 96) {
431 max_width -= (fnt->offset[off+1] - fnt->offset[off]);
432 if (max_width > 0) {
433 total++;
434 } else {
435 return total;
436 }
437 }
438 }
439 return total;
440}
441
Dees_Troy51a0e822012-09-05 15:24:24 -0400442int gr_textEx(int x, int y, const char *s, void* pFont)
443{
444 GGLContext *gl = gr_context;
445 GRFont *font = (GRFont*) pFont;
446 unsigned off;
447 unsigned cwidth;
448
449 /* Handle default font */
450 if (!font) font = gr_font;
451
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200452#ifndef TW_DISABLE_TTF
453 if(font->type == FONT_TYPE_TTF)
454 return gr_ttf_textExWH(gl, x, y, s, pFont, -1, -1);
455#endif
456
Dees_Troy51a0e822012-09-05 15:24:24 -0400457 gl->bindTexture(gl, &font->texture);
458 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
459 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
460 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
461 gl->enable(gl, GGL_TEXTURE_2D);
462
463 while((off = *s++)) {
464 off -= 32;
465 cwidth = 0;
466 if (off < 96) {
467 cwidth = font->offset[off+1] - font->offset[off];
468 gl->texCoord2i(gl, (font->offset[off]) - x, 0 - y);
469 gl->recti(gl, x, y, x + cwidth, y + font->cheight);
470 x += cwidth;
471 }
472 }
473
Vojtech Bocek3041c882015-03-06 00:28:21 +0100474 gl->disable(gl, GGL_TEXTURE_2D);
475
Dees_Troy51a0e822012-09-05 15:24:24 -0400476 return x;
477}
478
479int gr_textExW(int x, int y, const char *s, void* pFont, int max_width)
480{
481 GGLContext *gl = gr_context;
482 GRFont *font = (GRFont*) pFont;
483 unsigned off;
484 unsigned cwidth;
485
486 /* Handle default font */
487 if (!font) font = gr_font;
488
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200489#ifndef TW_DISABLE_TTF
490 if(font->type == FONT_TYPE_TTF)
491 return gr_ttf_textExWH(gl, x, y, s, pFont, max_width, -1);
492#endif
493
Dees_Troy51a0e822012-09-05 15:24:24 -0400494 gl->bindTexture(gl, &font->texture);
495 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
496 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
497 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
498 gl->enable(gl, GGL_TEXTURE_2D);
499
500 while((off = *s++)) {
501 off -= 32;
502 cwidth = 0;
503 if (off < 96) {
504 cwidth = font->offset[off+1] - font->offset[off];
505 if ((x + (int)cwidth) < max_width) {
506 gl->texCoord2i(gl, (font->offset[off]) - x, 0 - y);
507 gl->recti(gl, x, y, x + cwidth, y + font->cheight);
508 x += cwidth;
509 } else {
510 gl->texCoord2i(gl, (font->offset[off]) - x, 0 - y);
511 gl->recti(gl, x, y, max_width, y + font->cheight);
512 x = max_width;
513 return x;
514 }
515 }
516 }
517
Vojtech Bocek3041c882015-03-06 00:28:21 +0100518 gl->disable(gl, GGL_TEXTURE_2D);
519
Dees_Troy51a0e822012-09-05 15:24:24 -0400520 return x;
521}
522
523int gr_textExWH(int x, int y, const char *s, void* pFont, int max_width, int max_height)
524{
525 GGLContext *gl = gr_context;
526 GRFont *font = (GRFont*) pFont;
527 unsigned off;
528 unsigned cwidth;
529 int rect_x, rect_y;
530
531 /* Handle default font */
532 if (!font) font = gr_font;
533
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200534#ifndef TW_DISABLE_TTF
535 if(font->type == FONT_TYPE_TTF)
536 return gr_ttf_textExWH(gl, x, y, s, pFont, max_width, max_height);
537#endif
538
Dees_Troy51a0e822012-09-05 15:24:24 -0400539 gl->bindTexture(gl, &font->texture);
540 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
541 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
542 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
543 gl->enable(gl, GGL_TEXTURE_2D);
544
545 while((off = *s++)) {
546 off -= 32;
547 cwidth = 0;
548 if (off < 96) {
549 cwidth = font->offset[off+1] - font->offset[off];
550 if ((x + (int)cwidth) < max_width)
551 rect_x = x + cwidth;
552 else
553 rect_x = max_width;
Dees_Troyf7596752012-09-28 13:21:36 -0400554 if (y + font->cheight < (unsigned int)(max_height))
Dees_Troy51a0e822012-09-05 15:24:24 -0400555 rect_y = y + font->cheight;
556 else
557 rect_y = max_height;
558
559 gl->texCoord2i(gl, (font->offset[off]) - x, 0 - y);
560 gl->recti(gl, x, y, rect_x, rect_y);
561 x += cwidth;
562 if (x > max_width)
563 return x;
564 }
565 }
566
Vojtech Bocek3041c882015-03-06 00:28:21 +0100567 gl->disable(gl, GGL_TEXTURE_2D);
568
Dees_Troy51a0e822012-09-05 15:24:24 -0400569 return x;
570}
571
that9876ac32015-02-15 21:40:59 +0100572void gr_clip(int x, int y, int w, int h)
573{
574 GGLContext *gl = gr_context;
575 gl->scissor(gl, x, y, w, h);
576 gl->enable(gl, GGL_SCISSOR_TEST);
577}
578
579void gr_noclip()
580{
581 GGLContext *gl = gr_context;
582 gl->scissor(gl, 0, 0, gr_fb_width(), gr_fb_height());
583 gl->disable(gl, GGL_SCISSOR_TEST);
584}
585
Dees_Troy51a0e822012-09-05 15:24:24 -0400586void gr_fill(int x, int y, int w, int h)
587{
588 GGLContext *gl = gr_context;
Vojtech Bocek1a4744a2014-02-06 19:52:28 +0100589
590 if(gr_is_curr_clr_opaque)
591 gl->disable(gl, GGL_BLEND);
592
Dees_Troy51a0e822012-09-05 15:24:24 -0400593 gl->recti(gl, x, y, x + w, y + h);
Vojtech Bocek1a4744a2014-02-06 19:52:28 +0100594
595 if(gr_is_curr_clr_opaque)
596 gl->enable(gl, GGL_BLEND);
Dees_Troy51a0e822012-09-05 15:24:24 -0400597}
598
Vojtech Bocekdb378b62015-03-05 20:02:57 +0100599void gr_line(int x0, int y0, int x1, int y1, int width)
600{
601 GGLContext *gl = gr_context;
602
603 if(gr_is_curr_clr_opaque)
604 gl->disable(gl, GGL_BLEND);
605
606 const int coords0[2] = { x0 << 4, y0 << 4 };
607 const int coords1[2] = { x1 << 4, y1 << 4 };
608 gl->linex(gl, coords0, coords1, width << 4);
609
610 if(gr_is_curr_clr_opaque)
611 gl->enable(gl, GGL_BLEND);
612}
613
614gr_surface gr_render_circle(int radius, unsigned char r, unsigned char g, unsigned char b, unsigned char a)
615{
616 int rx, ry;
617 GGLSurface *surface;
618 const int diameter = radius*2 + 1;
619 const int radius_check = radius*radius + radius*0.8;
620 const uint32_t px = (a << 24) | (b << 16) | (g << 8) | r;
621 uint32_t *data;
622
623 surface = malloc(sizeof(GGLSurface));
624 memset(surface, 0, sizeof(GGLSurface));
625
626 data = malloc(diameter * diameter * 4);
627 memset(data, 0, diameter * diameter * 4);
628
629 surface->version = sizeof(surface);
630 surface->width = diameter;
631 surface->height = diameter;
632 surface->stride = diameter;
633 surface->data = (GGLubyte*)data;
634 surface->format = GGL_PIXEL_FORMAT_RGBA_8888;
635
636 for(ry = -radius; ry <= radius; ++ry)
637 for(rx = -radius; rx <= radius; ++rx)
638 if(rx*rx+ry*ry <= radius_check)
639 *(data + diameter*(radius + ry) + (radius+rx)) = px;
640
641 return surface;
642}
643
Dees_Troy51a0e822012-09-05 15:24:24 -0400644void gr_blit(gr_surface source, int sx, int sy, int w, int h, int dx, int dy) {
Dees Troyf171e102014-11-06 22:19:58 +0100645 if (gr_context == NULL) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400646 return;
647 }
648
649 GGLContext *gl = gr_context;
Vojtech Bocek6c694b62014-02-06 20:36:25 +0100650 GGLSurface *surface = (GGLSurface*)source;
651
652 if(surface->format == GGL_PIXEL_FORMAT_RGBX_8888)
653 gl->disable(gl, GGL_BLEND);
654
655 gl->bindTexture(gl, surface);
Dees_Troy51a0e822012-09-05 15:24:24 -0400656 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
657 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
658 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
659 gl->enable(gl, GGL_TEXTURE_2D);
660 gl->texCoord2i(gl, sx - dx, sy - dy);
661 gl->recti(gl, dx, dy, dx + w, dy + h);
Vojtech Bocek3041c882015-03-06 00:28:21 +0100662 gl->disable(gl, GGL_TEXTURE_2D);
Vojtech Bocek6c694b62014-02-06 20:36:25 +0100663
664 if(surface->format == GGL_PIXEL_FORMAT_RGBX_8888)
Dees Troyf171e102014-11-06 22:19:58 +0100665 gl->enable(gl, GGL_BLEND);
Dees_Troy51a0e822012-09-05 15:24:24 -0400666}
667
668unsigned int gr_get_width(gr_surface surface) {
669 if (surface == NULL) {
670 return 0;
671 }
672 return ((GGLSurface*) surface)->width;
673}
674
675unsigned int gr_get_height(gr_surface surface) {
676 if (surface == NULL) {
677 return 0;
678 }
679 return ((GGLSurface*) surface)->height;
680}
681
682void* gr_loadFont(const char* fontName)
683{
684 int fd;
685 GRFont *font = 0;
686 GGLSurface *ftex;
687 unsigned char *bits, *rle;
688 unsigned char *in, data;
689 unsigned width, height;
690 unsigned element;
691
692 fd = open(fontName, O_RDONLY);
693 if (fd == -1)
694 {
695 char tmp[128];
696
Dees Troy3454ade2015-01-20 19:21:04 +0000697 sprintf(tmp, TWRES "fonts/%s.dat", fontName);
Dees_Troy51a0e822012-09-05 15:24:24 -0400698 fd = open(tmp, O_RDONLY);
699 if (fd == -1)
700 return NULL;
701 }
702
703 font = calloc(sizeof(*font), 1);
704 ftex = &font->texture;
705
706 read(fd, &width, sizeof(unsigned));
707 read(fd, &height, sizeof(unsigned));
708 read(fd, font->offset, sizeof(unsigned) * 96);
709 font->offset[96] = width;
710
711 bits = malloc(width * height);
712 memset(bits, 0, width * height);
713
714 unsigned pos = 0;
715 while (pos < width * height)
716 {
717 int bit;
718
719 read(fd, &data, 1);
720 for (bit = 0; bit < 8; bit++)
721 {
722 if (data & (1 << (7-bit))) bits[pos++] = 255;
723 else bits[pos++] = 0;
724
725 if (pos == width * height) break;
726 }
727 }
728 close(fd);
729
730 ftex->version = sizeof(*ftex);
731 ftex->width = width;
732 ftex->height = height;
733 ftex->stride = width;
734 ftex->data = (void*) bits;
735 ftex->format = GGL_PIXEL_FORMAT_A_8;
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200736 font->type = FONT_TYPE_TWRP;
Dees_Troy51a0e822012-09-05 15:24:24 -0400737 font->cheight = height;
738 font->ascent = height - 2;
739 return (void*) font;
740}
741
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200742void gr_freeFont(void *font)
743{
744 GRFont *f = font;
745 free(f->texture.data);
746 free(f);
747}
748
749int gr_getMaxFontHeight(void *font)
Dees_Troy51a0e822012-09-05 15:24:24 -0400750{
751 GRFont *fnt = (GRFont*) font;
752
753 if (!fnt) fnt = gr_font;
754 if (!fnt) return -1;
755
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200756#ifndef TW_DISABLE_TTF
757 if(fnt->type == FONT_TYPE_TTF)
758 return gr_ttf_getMaxFontHeight(font);
759#endif
760
761 return fnt->cheight;
Dees_Troy51a0e822012-09-05 15:24:24 -0400762}
763
764static void gr_init_font(void)
765{
766 int fontRes;
767 GGLSurface *ftex;
768 unsigned char *bits, *rle;
769 unsigned char *in, data;
770 unsigned width, height;
771 unsigned element;
772
773 gr_font = calloc(sizeof(*gr_font), 1);
774 ftex = &gr_font->texture;
775
776 width = font.width;
777 height = font.height;
778
779 bits = malloc(width * height);
780 rle = bits;
781
782 in = font.rundata;
783 while((data = *in++))
784 {
785 memset(rle, (data & 0x80) ? 255 : 0, data & 0x7f);
786 rle += (data & 0x7f);
787 }
788 for (element = 0; element < 97; element++)
789 {
790 gr_font->offset[element] = (element * font.cwidth);
791 }
792
793 ftex->version = sizeof(*ftex);
794 ftex->width = width;
795 ftex->height = height;
796 ftex->stride = width;
797 ftex->data = (void*) bits;
798 ftex->format = GGL_PIXEL_FORMAT_A_8;
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200799 gr_font->type = FONT_TYPE_TWRP;
Dees_Troy51a0e822012-09-05 15:24:24 -0400800 gr_font->cheight = height;
801 gr_font->ascent = height - 2;
802 return;
803}
804
805int gr_init(void)
806{
807 gglInit(&gr_context);
808 GGLContext *gl = gr_context;
809
810 gr_init_font();
811 gr_vt_fd = open("/dev/tty0", O_RDWR | O_SYNC);
812 if (gr_vt_fd < 0) {
813 // This is non-fatal; post-Cupcake kernels don't have tty0.
814 } else if (ioctl(gr_vt_fd, KDSETMODE, (void*) KD_GRAPHICS)) {
815 // However, if we do open tty0, we expect the ioctl to work.
816 perror("failed KDSETMODE to KD_GRAPHICS on tty0");
817 gr_exit();
818 return -1;
819 }
820
821 gr_fb_fd = get_framebuffer(gr_framebuffer);
822 if (gr_fb_fd < 0) {
823 perror("Unable to get framebuffer.\n");
824 gr_exit();
825 return -1;
826 }
827
828 get_memory_surface(&gr_mem_surface);
829
830 fprintf(stderr, "framebuffer: fd %d (%d x %d)\n",
831 gr_fb_fd, gr_framebuffer[0].width, gr_framebuffer[0].height);
832
833 /* start with 0 as front (displayed) and 1 as back (drawing) */
834 gr_active_fb = 0;
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600835 if (!has_overlay)
836 set_active_framebuffer(0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400837 gl->colorBuffer(gl, &gr_mem_surface);
838
839 gl->activeTexture(gl, 0);
840 gl->enable(gl, GGL_BLEND);
841 gl->blendFunc(gl, GGL_SRC_ALPHA, GGL_ONE_MINUS_SRC_ALPHA);
842
xNUTxcd56f8c2014-07-23 01:30:20 +0200843#ifdef TW_SCREEN_BLANK_ON_BOOT
844 printf("TW_SCREEN_BLANK_ON_BOOT := true\n");
845 gr_fb_blank(true);
846 gr_fb_blank(false);
847#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400848
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600849 if (!alloc_ion_mem(fi.line_length * vi.yres))
850 allocate_overlay(gr_fb_fd, gr_framebuffer);
851
Dees_Troy51a0e822012-09-05 15:24:24 -0400852 return 0;
853}
854
855void gr_exit(void)
856{
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600857 free_overlay(gr_fb_fd);
858 free_ion_mem();
859
Dees_Troy51a0e822012-09-05 15:24:24 -0400860 close(gr_fb_fd);
861 gr_fb_fd = -1;
862
863 free(gr_mem_surface.data);
864
865 ioctl(gr_vt_fd, KDSETMODE, (void*) KD_TEXT);
866 close(gr_vt_fd);
867 gr_vt_fd = -1;
868}
869
870int gr_fb_width(void)
871{
872 return gr_framebuffer[0].width;
873}
874
875int gr_fb_height(void)
876{
877 return gr_framebuffer[0].height;
878}
879
880gr_pixel *gr_fb_data(void)
881{
882 return (unsigned short *) gr_mem_surface.data;
883}
884
Dees_Troy70237dc2013-02-28 21:31:48 +0000885int gr_fb_blank(int blank)
Dees_Troy51a0e822012-09-05 15:24:24 -0400886{
887 int ret;
Dees Troy05d18c92014-08-04 18:17:07 +0000888 //if (blank)
889 //free_overlay(gr_fb_fd);
Dees_Troy51a0e822012-09-05 15:24:24 -0400890
891 ret = ioctl(gr_fb_fd, FBIOBLANK, blank ? FB_BLANK_POWERDOWN : FB_BLANK_UNBLANK);
892 if (ret < 0)
893 perror("ioctl(): blank");
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600894
Dees Troy05d18c92014-08-04 18:17:07 +0000895 //if (!blank)
896 //allocate_overlay(gr_fb_fd, gr_framebuffer);
897 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -0400898}
899
900int gr_get_surface(gr_surface* surface)
901{
902 GGLSurface* ms = malloc(sizeof(GGLSurface));
903 if (!ms) return -1;
904
905 // Allocate the data
906 get_memory_surface(ms);
907
908 // Now, copy the data
909 memcpy(ms->data, gr_mem_surface.data, vi.xres * vi.yres * vi.bits_per_pixel / 8);
910
911 *surface = (gr_surface*) ms;
912 return 0;
913}
914
915int gr_free_surface(gr_surface surface)
916{
917 if (!surface)
918 return -1;
919
920 GGLSurface* ms = (GGLSurface*) surface;
921 free(ms->data);
922 free(ms);
923 return 0;
924}
925
926void gr_write_frame_to_file(int fd)
927{
928 write(fd, gr_mem_surface.data, vi.xres * vi.yres * vi.bits_per_pixel / 8);
929}