blob: 704389fdf049295eebdbcfd6b8c2c492ab9d01dd [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
Hiemanshu Sharma94f6c882012-11-21 11:25:22 -0600298 /* check if we can use double buffering */
299 if (vi.yres * fi.line_length * 2 > fi.smem_len)
300 return fd;
301
302 double_buffering = 1;
303
Dees_Troy51a0e822012-09-05 15:24:24 -0400304 fb->version = sizeof(*fb);
305 fb->width = vi.xres;
306 fb->height = vi.yres;
307#ifdef BOARD_HAS_JANKY_BACKBUFFER
308 fb->stride = fi.line_length/2;
Ethan Yonkerbcc502c2014-11-10 11:22:10 -0600309 fb->data = (GGLubyte*) (((unsigned long) bits) + vi.yres * fi.line_length);
Dees_Troy51a0e822012-09-05 15:24:24 -0400310#else
311 fb->stride = vi.xres_virtual;
Ethan Yonkerbcc502c2014-11-10 11:22:10 -0600312 fb->data = (GGLubyte*) (((unsigned long) bits) + vi.yres * fb->stride * PIXEL_SIZE);
Dees_Troy51a0e822012-09-05 15:24:24 -0400313#endif
314 fb->format = PIXEL_FORMAT;
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600315 if (!has_overlay) {
316 memset(fb->data, 0, vi.yres * fb->stride * PIXEL_SIZE);
317 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400318
319#ifdef PRINT_SCREENINFO
320 print_fb_var_screeninfo();
321#endif
322
323 return fd;
324}
325
326static void get_memory_surface(GGLSurface* ms) {
327 ms->version = sizeof(*ms);
328 ms->width = vi.xres;
329 ms->height = vi.yres;
330 ms->stride = vi.xres_virtual;
331 ms->data = malloc(vi.xres_virtual * vi.yres * PIXEL_SIZE);
332 ms->format = PIXEL_FORMAT;
333}
334
335static void set_active_framebuffer(unsigned n)
336{
Hiemanshu Sharma94f6c882012-11-21 11:25:22 -0600337 if (n > 1 || !double_buffering) return;
Hiemanshu Sharmaacf6a9b2012-11-21 11:28:36 -0600338 vi.yres_virtual = vi.yres * NUM_BUFFERS;
Dees_Troy51a0e822012-09-05 15:24:24 -0400339 vi.yoffset = n * vi.yres;
340// vi.bits_per_pixel = PIXEL_SIZE * 8;
341 if (ioctl(gr_fb_fd, FBIOPUT_VSCREENINFO, &vi) < 0) {
342 perror("active fb swap failed");
343 }
344}
345
346void gr_flip(void)
347{
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600348 if (-EINVAL == overlay_display_frame(gr_fb_fd, gr_mem_surface.data,
349 (fi.line_length * vi.yres))) {
350 GGLContext *gl = gr_context;
Dees_Troy51a0e822012-09-05 15:24:24 -0400351
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600352 /* swap front and back buffers */
353 if (double_buffering)
354 gr_active_fb = (gr_active_fb + 1) & 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400355
356#ifdef BOARD_HAS_FLIPPED_SCREEN
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600357 /* flip buffer 180 degrees for devices with physicaly inverted screens */
358 unsigned int i;
359 unsigned int j;
Bogdan Seniucd1372f72015-01-23 08:22:39 -0800360 for (i = 0; i < vi.yres; i++) {
361 for (j = 0; j < vi.xres; j++) {
362 memcpy(gr_framebuffer[gr_active_fb].data + (i * vi.xres_virtual + j) * PIXEL_SIZE,
363 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 -0600364 }
365 }
Bogdan Seniucd1372f72015-01-23 08:22:39 -0800366#else
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600367 /* copy data from the in-memory surface to the buffer we're about
368 * to make active. */
369 memcpy(gr_framebuffer[gr_active_fb].data, gr_mem_surface.data,
370 vi.xres_virtual * vi.yres * PIXEL_SIZE);
Bogdan Seniucd1372f72015-01-23 08:22:39 -0800371#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400372
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600373 /* inform the display driver */
374 set_active_framebuffer(gr_active_fb);
375 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400376}
377
378void gr_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
379{
380 GGLContext *gl = gr_context;
381 GGLint color[4];
382 color[0] = ((r << 8) | r) + 1;
383 color[1] = ((g << 8) | g) + 1;
384 color[2] = ((b << 8) | b) + 1;
385 color[3] = ((a << 8) | a) + 1;
386 gl->color4xv(gl, color);
Vojtech Bocek1a4744a2014-02-06 19:52:28 +0100387
388 gr_is_curr_clr_opaque = (a == 255);
Dees_Troy51a0e822012-09-05 15:24:24 -0400389}
390
391int gr_measureEx(const char *s, void* font)
392{
393 GRFont* fnt = (GRFont*) font;
394 int total = 0;
395 unsigned pos;
396 unsigned off;
397
398 if (!fnt) fnt = gr_font;
399
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200400#ifndef TW_DISABLE_TTF
401 if(fnt->type == FONT_TYPE_TTF)
402 return gr_ttf_measureEx(s, font);
403#endif
404
Dees_Troy51a0e822012-09-05 15:24:24 -0400405 while ((off = *s++))
406 {
407 off -= 32;
408 if (off < 96)
409 total += (fnt->offset[off+1] - fnt->offset[off]);
410 }
411 return total;
412}
413
Dees Troy31218ec2014-02-25 20:35:56 +0000414int gr_maxExW(const char *s, void* font, int max_width)
415{
416 GRFont* fnt = (GRFont*) font;
417 int total = 0;
418 unsigned pos;
419 unsigned off;
420
421 if (!fnt) fnt = gr_font;
422
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200423#ifndef TW_DISABLE_TTF
424 if(fnt->type == FONT_TYPE_TTF)
425 return gr_ttf_maxExW(s, font, max_width);
426#endif
427
Dees Troy31218ec2014-02-25 20:35:56 +0000428 while ((off = *s++))
429 {
430 off -= 32;
431 if (off < 96) {
432 max_width -= (fnt->offset[off+1] - fnt->offset[off]);
433 if (max_width > 0) {
434 total++;
435 } else {
436 return total;
437 }
438 }
439 }
440 return total;
441}
442
Dees_Troy51a0e822012-09-05 15:24:24 -0400443int gr_textEx(int x, int y, const char *s, void* pFont)
444{
445 GGLContext *gl = gr_context;
446 GRFont *font = (GRFont*) pFont;
447 unsigned off;
448 unsigned cwidth;
449
450 /* Handle default font */
451 if (!font) font = gr_font;
452
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200453#ifndef TW_DISABLE_TTF
454 if(font->type == FONT_TYPE_TTF)
455 return gr_ttf_textExWH(gl, x, y, s, pFont, -1, -1);
456#endif
457
Dees_Troy51a0e822012-09-05 15:24:24 -0400458 gl->bindTexture(gl, &font->texture);
459 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
460 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
461 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
462 gl->enable(gl, GGL_TEXTURE_2D);
463
464 while((off = *s++)) {
465 off -= 32;
466 cwidth = 0;
467 if (off < 96) {
468 cwidth = font->offset[off+1] - font->offset[off];
469 gl->texCoord2i(gl, (font->offset[off]) - x, 0 - y);
470 gl->recti(gl, x, y, x + cwidth, y + font->cheight);
471 x += cwidth;
472 }
473 }
474
Vojtech Bocek3041c882015-03-06 00:28:21 +0100475 gl->disable(gl, GGL_TEXTURE_2D);
476
Dees_Troy51a0e822012-09-05 15:24:24 -0400477 return x;
478}
479
480int gr_textExW(int x, int y, const char *s, void* pFont, int max_width)
481{
482 GGLContext *gl = gr_context;
483 GRFont *font = (GRFont*) pFont;
484 unsigned off;
485 unsigned cwidth;
486
487 /* Handle default font */
488 if (!font) font = gr_font;
489
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200490#ifndef TW_DISABLE_TTF
491 if(font->type == FONT_TYPE_TTF)
492 return gr_ttf_textExWH(gl, x, y, s, pFont, max_width, -1);
493#endif
494
Dees_Troy51a0e822012-09-05 15:24:24 -0400495 gl->bindTexture(gl, &font->texture);
496 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
497 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
498 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
499 gl->enable(gl, GGL_TEXTURE_2D);
500
501 while((off = *s++)) {
502 off -= 32;
503 cwidth = 0;
504 if (off < 96) {
505 cwidth = font->offset[off+1] - font->offset[off];
506 if ((x + (int)cwidth) < max_width) {
507 gl->texCoord2i(gl, (font->offset[off]) - x, 0 - y);
508 gl->recti(gl, x, y, x + cwidth, y + font->cheight);
509 x += cwidth;
510 } else {
511 gl->texCoord2i(gl, (font->offset[off]) - x, 0 - y);
512 gl->recti(gl, x, y, max_width, y + font->cheight);
513 x = max_width;
514 return x;
515 }
516 }
517 }
518
Vojtech Bocek3041c882015-03-06 00:28:21 +0100519 gl->disable(gl, GGL_TEXTURE_2D);
520
Dees_Troy51a0e822012-09-05 15:24:24 -0400521 return x;
522}
523
524int gr_textExWH(int x, int y, const char *s, void* pFont, int max_width, int max_height)
525{
526 GGLContext *gl = gr_context;
527 GRFont *font = (GRFont*) pFont;
528 unsigned off;
529 unsigned cwidth;
530 int rect_x, rect_y;
531
532 /* Handle default font */
533 if (!font) font = gr_font;
534
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200535#ifndef TW_DISABLE_TTF
536 if(font->type == FONT_TYPE_TTF)
537 return gr_ttf_textExWH(gl, x, y, s, pFont, max_width, max_height);
538#endif
539
Dees_Troy51a0e822012-09-05 15:24:24 -0400540 gl->bindTexture(gl, &font->texture);
541 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
542 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
543 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
544 gl->enable(gl, GGL_TEXTURE_2D);
545
546 while((off = *s++)) {
547 off -= 32;
548 cwidth = 0;
549 if (off < 96) {
550 cwidth = font->offset[off+1] - font->offset[off];
551 if ((x + (int)cwidth) < max_width)
552 rect_x = x + cwidth;
553 else
554 rect_x = max_width;
Dees_Troyf7596752012-09-28 13:21:36 -0400555 if (y + font->cheight < (unsigned int)(max_height))
Dees_Troy51a0e822012-09-05 15:24:24 -0400556 rect_y = y + font->cheight;
557 else
558 rect_y = max_height;
559
560 gl->texCoord2i(gl, (font->offset[off]) - x, 0 - y);
561 gl->recti(gl, x, y, rect_x, rect_y);
562 x += cwidth;
563 if (x > max_width)
564 return x;
565 }
566 }
567
Vojtech Bocek3041c882015-03-06 00:28:21 +0100568 gl->disable(gl, GGL_TEXTURE_2D);
569
Dees_Troy51a0e822012-09-05 15:24:24 -0400570 return x;
571}
572
that9876ac32015-02-15 21:40:59 +0100573void gr_clip(int x, int y, int w, int h)
574{
575 GGLContext *gl = gr_context;
576 gl->scissor(gl, x, y, w, h);
577 gl->enable(gl, GGL_SCISSOR_TEST);
578}
579
580void gr_noclip()
581{
582 GGLContext *gl = gr_context;
583 gl->scissor(gl, 0, 0, gr_fb_width(), gr_fb_height());
584 gl->disable(gl, GGL_SCISSOR_TEST);
585}
586
Dees_Troy51a0e822012-09-05 15:24:24 -0400587void gr_fill(int x, int y, int w, int h)
588{
589 GGLContext *gl = gr_context;
Vojtech Bocek1a4744a2014-02-06 19:52:28 +0100590
591 if(gr_is_curr_clr_opaque)
592 gl->disable(gl, GGL_BLEND);
593
Dees_Troy51a0e822012-09-05 15:24:24 -0400594 gl->recti(gl, x, y, x + w, y + h);
Vojtech Bocek1a4744a2014-02-06 19:52:28 +0100595
596 if(gr_is_curr_clr_opaque)
597 gl->enable(gl, GGL_BLEND);
Dees_Troy51a0e822012-09-05 15:24:24 -0400598}
599
Vojtech Bocekdb378b62015-03-05 20:02:57 +0100600void gr_line(int x0, int y0, int x1, int y1, int width)
601{
602 GGLContext *gl = gr_context;
603
604 if(gr_is_curr_clr_opaque)
605 gl->disable(gl, GGL_BLEND);
606
607 const int coords0[2] = { x0 << 4, y0 << 4 };
608 const int coords1[2] = { x1 << 4, y1 << 4 };
609 gl->linex(gl, coords0, coords1, width << 4);
610
611 if(gr_is_curr_clr_opaque)
612 gl->enable(gl, GGL_BLEND);
613}
614
615gr_surface gr_render_circle(int radius, unsigned char r, unsigned char g, unsigned char b, unsigned char a)
616{
617 int rx, ry;
618 GGLSurface *surface;
619 const int diameter = radius*2 + 1;
620 const int radius_check = radius*radius + radius*0.8;
621 const uint32_t px = (a << 24) | (b << 16) | (g << 8) | r;
622 uint32_t *data;
623
624 surface = malloc(sizeof(GGLSurface));
625 memset(surface, 0, sizeof(GGLSurface));
626
627 data = malloc(diameter * diameter * 4);
628 memset(data, 0, diameter * diameter * 4);
629
630 surface->version = sizeof(surface);
631 surface->width = diameter;
632 surface->height = diameter;
633 surface->stride = diameter;
634 surface->data = (GGLubyte*)data;
635 surface->format = GGL_PIXEL_FORMAT_RGBA_8888;
636
637 for(ry = -radius; ry <= radius; ++ry)
638 for(rx = -radius; rx <= radius; ++rx)
639 if(rx*rx+ry*ry <= radius_check)
640 *(data + diameter*(radius + ry) + (radius+rx)) = px;
641
642 return surface;
643}
644
Dees_Troy51a0e822012-09-05 15:24:24 -0400645void gr_blit(gr_surface source, int sx, int sy, int w, int h, int dx, int dy) {
Dees Troyf171e102014-11-06 22:19:58 +0100646 if (gr_context == NULL) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400647 return;
648 }
649
650 GGLContext *gl = gr_context;
Vojtech Bocek6c694b62014-02-06 20:36:25 +0100651 GGLSurface *surface = (GGLSurface*)source;
652
653 if(surface->format == GGL_PIXEL_FORMAT_RGBX_8888)
654 gl->disable(gl, GGL_BLEND);
655
656 gl->bindTexture(gl, surface);
Dees_Troy51a0e822012-09-05 15:24:24 -0400657 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
658 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
659 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
660 gl->enable(gl, GGL_TEXTURE_2D);
661 gl->texCoord2i(gl, sx - dx, sy - dy);
662 gl->recti(gl, dx, dy, dx + w, dy + h);
Vojtech Bocek3041c882015-03-06 00:28:21 +0100663 gl->disable(gl, GGL_TEXTURE_2D);
Vojtech Bocek6c694b62014-02-06 20:36:25 +0100664
665 if(surface->format == GGL_PIXEL_FORMAT_RGBX_8888)
Dees Troyf171e102014-11-06 22:19:58 +0100666 gl->enable(gl, GGL_BLEND);
Dees_Troy51a0e822012-09-05 15:24:24 -0400667}
668
669unsigned int gr_get_width(gr_surface surface) {
670 if (surface == NULL) {
671 return 0;
672 }
673 return ((GGLSurface*) surface)->width;
674}
675
676unsigned int gr_get_height(gr_surface surface) {
677 if (surface == NULL) {
678 return 0;
679 }
680 return ((GGLSurface*) surface)->height;
681}
682
683void* gr_loadFont(const char* fontName)
684{
685 int fd;
686 GRFont *font = 0;
687 GGLSurface *ftex;
688 unsigned char *bits, *rle;
689 unsigned char *in, data;
690 unsigned width, height;
691 unsigned element;
692
693 fd = open(fontName, O_RDONLY);
694 if (fd == -1)
695 {
696 char tmp[128];
697
Dees Troy3454ade2015-01-20 19:21:04 +0000698 sprintf(tmp, TWRES "fonts/%s.dat", fontName);
Dees_Troy51a0e822012-09-05 15:24:24 -0400699 fd = open(tmp, O_RDONLY);
700 if (fd == -1)
701 return NULL;
702 }
703
704 font = calloc(sizeof(*font), 1);
705 ftex = &font->texture;
706
707 read(fd, &width, sizeof(unsigned));
708 read(fd, &height, sizeof(unsigned));
709 read(fd, font->offset, sizeof(unsigned) * 96);
710 font->offset[96] = width;
711
712 bits = malloc(width * height);
713 memset(bits, 0, width * height);
714
715 unsigned pos = 0;
716 while (pos < width * height)
717 {
718 int bit;
719
720 read(fd, &data, 1);
721 for (bit = 0; bit < 8; bit++)
722 {
723 if (data & (1 << (7-bit))) bits[pos++] = 255;
724 else bits[pos++] = 0;
725
726 if (pos == width * height) break;
727 }
728 }
729 close(fd);
730
731 ftex->version = sizeof(*ftex);
732 ftex->width = width;
733 ftex->height = height;
734 ftex->stride = width;
735 ftex->data = (void*) bits;
736 ftex->format = GGL_PIXEL_FORMAT_A_8;
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200737 font->type = FONT_TYPE_TWRP;
Dees_Troy51a0e822012-09-05 15:24:24 -0400738 font->cheight = height;
739 font->ascent = height - 2;
740 return (void*) font;
741}
742
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200743void gr_freeFont(void *font)
744{
745 GRFont *f = font;
746 free(f->texture.data);
747 free(f);
748}
749
750int gr_getMaxFontHeight(void *font)
Dees_Troy51a0e822012-09-05 15:24:24 -0400751{
752 GRFont *fnt = (GRFont*) font;
753
754 if (!fnt) fnt = gr_font;
755 if (!fnt) return -1;
756
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200757#ifndef TW_DISABLE_TTF
758 if(fnt->type == FONT_TYPE_TTF)
759 return gr_ttf_getMaxFontHeight(font);
760#endif
761
762 return fnt->cheight;
Dees_Troy51a0e822012-09-05 15:24:24 -0400763}
764
765static void gr_init_font(void)
766{
767 int fontRes;
768 GGLSurface *ftex;
769 unsigned char *bits, *rle;
770 unsigned char *in, data;
771 unsigned width, height;
772 unsigned element;
773
774 gr_font = calloc(sizeof(*gr_font), 1);
775 ftex = &gr_font->texture;
776
777 width = font.width;
778 height = font.height;
779
780 bits = malloc(width * height);
781 rle = bits;
782
783 in = font.rundata;
784 while((data = *in++))
785 {
786 memset(rle, (data & 0x80) ? 255 : 0, data & 0x7f);
787 rle += (data & 0x7f);
788 }
789 for (element = 0; element < 97; element++)
790 {
791 gr_font->offset[element] = (element * font.cwidth);
792 }
793
794 ftex->version = sizeof(*ftex);
795 ftex->width = width;
796 ftex->height = height;
797 ftex->stride = width;
798 ftex->data = (void*) bits;
799 ftex->format = GGL_PIXEL_FORMAT_A_8;
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200800 gr_font->type = FONT_TYPE_TWRP;
Dees_Troy51a0e822012-09-05 15:24:24 -0400801 gr_font->cheight = height;
802 gr_font->ascent = height - 2;
803 return;
804}
805
806int gr_init(void)
807{
808 gglInit(&gr_context);
809 GGLContext *gl = gr_context;
810
811 gr_init_font();
812 gr_vt_fd = open("/dev/tty0", O_RDWR | O_SYNC);
813 if (gr_vt_fd < 0) {
814 // This is non-fatal; post-Cupcake kernels don't have tty0.
815 } else if (ioctl(gr_vt_fd, KDSETMODE, (void*) KD_GRAPHICS)) {
816 // However, if we do open tty0, we expect the ioctl to work.
817 perror("failed KDSETMODE to KD_GRAPHICS on tty0");
818 gr_exit();
819 return -1;
820 }
821
822 gr_fb_fd = get_framebuffer(gr_framebuffer);
823 if (gr_fb_fd < 0) {
824 perror("Unable to get framebuffer.\n");
825 gr_exit();
826 return -1;
827 }
828
829 get_memory_surface(&gr_mem_surface);
830
831 fprintf(stderr, "framebuffer: fd %d (%d x %d)\n",
832 gr_fb_fd, gr_framebuffer[0].width, gr_framebuffer[0].height);
833
834 /* start with 0 as front (displayed) and 1 as back (drawing) */
835 gr_active_fb = 0;
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600836 if (!has_overlay)
837 set_active_framebuffer(0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400838 gl->colorBuffer(gl, &gr_mem_surface);
839
840 gl->activeTexture(gl, 0);
841 gl->enable(gl, GGL_BLEND);
842 gl->blendFunc(gl, GGL_SRC_ALPHA, GGL_ONE_MINUS_SRC_ALPHA);
843
xNUTxcd56f8c2014-07-23 01:30:20 +0200844#ifdef TW_SCREEN_BLANK_ON_BOOT
845 printf("TW_SCREEN_BLANK_ON_BOOT := true\n");
846 gr_fb_blank(true);
847 gr_fb_blank(false);
848#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400849
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600850 if (!alloc_ion_mem(fi.line_length * vi.yres))
851 allocate_overlay(gr_fb_fd, gr_framebuffer);
852
Dees_Troy51a0e822012-09-05 15:24:24 -0400853 return 0;
854}
855
856void gr_exit(void)
857{
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600858 free_overlay(gr_fb_fd);
859 free_ion_mem();
860
Dees_Troy51a0e822012-09-05 15:24:24 -0400861 close(gr_fb_fd);
862 gr_fb_fd = -1;
863
864 free(gr_mem_surface.data);
865
866 ioctl(gr_vt_fd, KDSETMODE, (void*) KD_TEXT);
867 close(gr_vt_fd);
868 gr_vt_fd = -1;
869}
870
871int gr_fb_width(void)
872{
873 return gr_framebuffer[0].width;
874}
875
876int gr_fb_height(void)
877{
878 return gr_framebuffer[0].height;
879}
880
881gr_pixel *gr_fb_data(void)
882{
883 return (unsigned short *) gr_mem_surface.data;
884}
885
Dees_Troy70237dc2013-02-28 21:31:48 +0000886int gr_fb_blank(int blank)
Dees_Troy51a0e822012-09-05 15:24:24 -0400887{
888 int ret;
Dees Troy05d18c92014-08-04 18:17:07 +0000889 //if (blank)
890 //free_overlay(gr_fb_fd);
Dees_Troy51a0e822012-09-05 15:24:24 -0400891
892 ret = ioctl(gr_fb_fd, FBIOBLANK, blank ? FB_BLANK_POWERDOWN : FB_BLANK_UNBLANK);
893 if (ret < 0)
894 perror("ioctl(): blank");
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600895
Dees Troy05d18c92014-08-04 18:17:07 +0000896 //if (!blank)
897 //allocate_overlay(gr_fb_fd, gr_framebuffer);
898 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -0400899}
900
901int gr_get_surface(gr_surface* surface)
902{
903 GGLSurface* ms = malloc(sizeof(GGLSurface));
904 if (!ms) return -1;
905
906 // Allocate the data
907 get_memory_surface(ms);
908
909 // Now, copy the data
910 memcpy(ms->data, gr_mem_surface.data, vi.xres * vi.yres * vi.bits_per_pixel / 8);
911
912 *surface = (gr_surface*) ms;
913 return 0;
914}
915
916int gr_free_surface(gr_surface surface)
917{
918 if (!surface)
919 return -1;
920
921 GGLSurface* ms = (GGLSurface*) surface;
922 free(ms->data);
923 free(ms);
924 return 0;
925}
926
927void gr_write_frame_to_file(int fd)
928{
929 write(fd, gr_mem_surface.data, vi.xres * vi.yres * vi.bits_per_pixel / 8);
930}