blob: 7160c7e2e058e085057aa8799fb3bd539217154e [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{
166 int fd;
167 void *bits;
168
169 fd = open("/dev/graphics/fb0", O_RDWR);
170 if (fd < 0) {
171 perror("cannot open fb0");
172 return -1;
173 }
174
175 if (ioctl(fd, FBIOGET_VSCREENINFO, &vi) < 0) {
176 perror("failed to get fb0 info");
177 close(fd);
178 return -1;
179 }
180
181 fprintf(stderr, "Pixel format: %dx%d @ %dbpp\n", vi.xres, vi.yres, vi.bits_per_pixel);
182
183 vi.bits_per_pixel = PIXEL_SIZE * 8;
184 if (PIXEL_FORMAT == GGL_PIXEL_FORMAT_BGRA_8888) {
185 fprintf(stderr, "Pixel format: BGRA_8888\n");
186 if (PIXEL_SIZE != 4) fprintf(stderr, "E: Pixel Size mismatch!\n");
187 vi.red.offset = 8;
188 vi.red.length = 8;
189 vi.green.offset = 16;
190 vi.green.length = 8;
191 vi.blue.offset = 24;
192 vi.blue.length = 8;
193 vi.transp.offset = 0;
194 vi.transp.length = 8;
195 } else if (PIXEL_FORMAT == GGL_PIXEL_FORMAT_RGBX_8888) {
196 fprintf(stderr, "Pixel format: RGBX_8888\n");
197 if (PIXEL_SIZE != 4) fprintf(stderr, "E: Pixel Size mismatch!\n");
198 vi.red.offset = 24;
199 vi.red.length = 8;
200 vi.green.offset = 16;
201 vi.green.length = 8;
202 vi.blue.offset = 8;
203 vi.blue.length = 8;
204 vi.transp.offset = 0;
205 vi.transp.length = 8;
206 } else if (PIXEL_FORMAT == GGL_PIXEL_FORMAT_RGB_565) {
207#ifdef RECOVERY_RGB_565
208 fprintf(stderr, "Pixel format: RGB_565\n");
209 vi.blue.offset = 0;
210 vi.green.offset = 5;
211 vi.red.offset = 11;
212#else
213 fprintf(stderr, "Pixel format: BGR_565\n");
214 vi.blue.offset = 11;
215 vi.green.offset = 5;
216 vi.red.offset = 0;
217#endif
218 if (PIXEL_SIZE != 2) fprintf(stderr, "E: Pixel Size mismatch!\n");
219 vi.blue.length = 5;
220 vi.green.length = 6;
221 vi.red.length = 5;
222 vi.blue.msb_right = 0;
223 vi.green.msb_right = 0;
224 vi.red.msb_right = 0;
225 vi.transp.offset = 0;
226 vi.transp.length = 0;
227 }
228 else
229 {
230 perror("unknown pixel format");
231 close(fd);
232 return -1;
233 }
234
235 vi.vmode = FB_VMODE_NONINTERLACED;
236 vi.activate = FB_ACTIVATE_NOW | FB_ACTIVATE_FORCE;
237
238 if (ioctl(fd, FBIOPUT_VSCREENINFO, &vi) < 0) {
239 perror("failed to put fb0 info");
240 close(fd);
241 return -1;
242 }
243
244 if (ioctl(fd, FBIOGET_FSCREENINFO, &fi) < 0) {
245 perror("failed to get fb0 info");
246 close(fd);
247 return -1;
248 }
249
Dees Troyb0425382014-04-03 00:10:03 +0000250#ifdef MSM_BSP
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600251 has_overlay = target_has_overlay(fi.id);
252
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600253 if (isTargetMdp5())
254 setDisplaySplit();
Dees Troyb0425382014-04-03 00:10:03 +0000255#else
256 has_overlay = false;
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600257#endif
258
259 if (!has_overlay) {
260 printf("Not using qualcomm overlay, '%s'\n", fi.id);
261 bits = mmap(0, fi.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
262 if (bits == MAP_FAILED) {
263 perror("failed to mmap framebuffer");
264 close(fd);
265 return -1;
266 }
267 } else {
268 printf("Using qualcomm overlay\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400269 }
270
271#ifdef RECOVERY_GRAPHICS_USE_LINELENGTH
272 vi.xres_virtual = fi.line_length / PIXEL_SIZE;
273#endif
274
275 fb->version = sizeof(*fb);
276 fb->width = vi.xres;
277 fb->height = vi.yres;
278#ifdef BOARD_HAS_JANKY_BACKBUFFER
Dees_Troy2673cec2013-04-02 20:22:16 +0000279 printf("setting JANKY BACKBUFFER\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400280 fb->stride = fi.line_length/2;
281#else
282 fb->stride = vi.xres_virtual;
283#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400284 fb->format = PIXEL_FORMAT;
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600285 if (!has_overlay) {
286 fb->data = bits;
287 memset(fb->data, 0, vi.yres * fb->stride * PIXEL_SIZE);
288 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400289
290 fb++;
291
Hiemanshu Sharma94f6c882012-11-21 11:25:22 -0600292 /* check if we can use double buffering */
293 if (vi.yres * fi.line_length * 2 > fi.smem_len)
294 return fd;
295
296 double_buffering = 1;
297
Dees_Troy51a0e822012-09-05 15:24:24 -0400298 fb->version = sizeof(*fb);
299 fb->width = vi.xres;
300 fb->height = vi.yres;
301#ifdef BOARD_HAS_JANKY_BACKBUFFER
302 fb->stride = fi.line_length/2;
Ethan Yonkerbcc502c2014-11-10 11:22:10 -0600303 fb->data = (GGLubyte*) (((unsigned long) bits) + vi.yres * fi.line_length);
Dees_Troy51a0e822012-09-05 15:24:24 -0400304#else
305 fb->stride = vi.xres_virtual;
Ethan Yonkerbcc502c2014-11-10 11:22:10 -0600306 fb->data = (GGLubyte*) (((unsigned long) bits) + vi.yres * fb->stride * PIXEL_SIZE);
Dees_Troy51a0e822012-09-05 15:24:24 -0400307#endif
308 fb->format = PIXEL_FORMAT;
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600309 if (!has_overlay) {
310 memset(fb->data, 0, vi.yres * fb->stride * PIXEL_SIZE);
311 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400312
313#ifdef PRINT_SCREENINFO
314 print_fb_var_screeninfo();
315#endif
316
317 return fd;
318}
319
320static void get_memory_surface(GGLSurface* ms) {
321 ms->version = sizeof(*ms);
322 ms->width = vi.xres;
323 ms->height = vi.yres;
324 ms->stride = vi.xres_virtual;
325 ms->data = malloc(vi.xres_virtual * vi.yres * PIXEL_SIZE);
326 ms->format = PIXEL_FORMAT;
327}
328
329static void set_active_framebuffer(unsigned n)
330{
Hiemanshu Sharma94f6c882012-11-21 11:25:22 -0600331 if (n > 1 || !double_buffering) return;
Hiemanshu Sharmaacf6a9b2012-11-21 11:28:36 -0600332 vi.yres_virtual = vi.yres * NUM_BUFFERS;
Dees_Troy51a0e822012-09-05 15:24:24 -0400333 vi.yoffset = n * vi.yres;
334// vi.bits_per_pixel = PIXEL_SIZE * 8;
335 if (ioctl(gr_fb_fd, FBIOPUT_VSCREENINFO, &vi) < 0) {
336 perror("active fb swap failed");
337 }
338}
339
340void gr_flip(void)
341{
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600342 if (-EINVAL == overlay_display_frame(gr_fb_fd, gr_mem_surface.data,
343 (fi.line_length * vi.yres))) {
344 GGLContext *gl = gr_context;
Dees_Troy51a0e822012-09-05 15:24:24 -0400345
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600346 /* swap front and back buffers */
347 if (double_buffering)
348 gr_active_fb = (gr_active_fb + 1) & 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400349
350#ifdef BOARD_HAS_FLIPPED_SCREEN
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600351 /* flip buffer 180 degrees for devices with physicaly inverted screens */
352 unsigned int i;
353 unsigned int j;
Bogdan Seniucd1372f72015-01-23 08:22:39 -0800354 for (i = 0; i < vi.yres; i++) {
355 for (j = 0; j < vi.xres; j++) {
356 memcpy(gr_framebuffer[gr_active_fb].data + (i * vi.xres_virtual + j) * PIXEL_SIZE,
357 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 -0600358 }
359 }
Bogdan Seniucd1372f72015-01-23 08:22:39 -0800360#else
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600361 /* copy data from the in-memory surface to the buffer we're about
362 * to make active. */
363 memcpy(gr_framebuffer[gr_active_fb].data, gr_mem_surface.data,
364 vi.xres_virtual * vi.yres * PIXEL_SIZE);
Bogdan Seniucd1372f72015-01-23 08:22:39 -0800365#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400366
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600367 /* inform the display driver */
368 set_active_framebuffer(gr_active_fb);
369 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400370}
371
372void gr_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
373{
374 GGLContext *gl = gr_context;
375 GGLint color[4];
376 color[0] = ((r << 8) | r) + 1;
377 color[1] = ((g << 8) | g) + 1;
378 color[2] = ((b << 8) | b) + 1;
379 color[3] = ((a << 8) | a) + 1;
380 gl->color4xv(gl, color);
Vojtech Bocek1a4744a2014-02-06 19:52:28 +0100381
382 gr_is_curr_clr_opaque = (a == 255);
Dees_Troy51a0e822012-09-05 15:24:24 -0400383}
384
385int gr_measureEx(const char *s, void* font)
386{
387 GRFont* fnt = (GRFont*) font;
388 int total = 0;
389 unsigned pos;
390 unsigned off;
391
392 if (!fnt) fnt = gr_font;
393
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200394#ifndef TW_DISABLE_TTF
395 if(fnt->type == FONT_TYPE_TTF)
396 return gr_ttf_measureEx(s, font);
397#endif
398
Dees_Troy51a0e822012-09-05 15:24:24 -0400399 while ((off = *s++))
400 {
401 off -= 32;
402 if (off < 96)
403 total += (fnt->offset[off+1] - fnt->offset[off]);
404 }
405 return total;
406}
407
Dees Troy31218ec2014-02-25 20:35:56 +0000408int gr_maxExW(const char *s, void* font, int max_width)
409{
410 GRFont* fnt = (GRFont*) font;
411 int total = 0;
412 unsigned pos;
413 unsigned off;
414
415 if (!fnt) fnt = gr_font;
416
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200417#ifndef TW_DISABLE_TTF
418 if(fnt->type == FONT_TYPE_TTF)
419 return gr_ttf_maxExW(s, font, max_width);
420#endif
421
Dees Troy31218ec2014-02-25 20:35:56 +0000422 while ((off = *s++))
423 {
424 off -= 32;
425 if (off < 96) {
426 max_width -= (fnt->offset[off+1] - fnt->offset[off]);
427 if (max_width > 0) {
428 total++;
429 } else {
430 return total;
431 }
432 }
433 }
434 return total;
435}
436
Dees_Troy51a0e822012-09-05 15:24:24 -0400437int gr_textEx(int x, int y, const char *s, void* pFont)
438{
439 GGLContext *gl = gr_context;
440 GRFont *font = (GRFont*) pFont;
441 unsigned off;
442 unsigned cwidth;
443
444 /* Handle default font */
445 if (!font) font = gr_font;
446
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200447#ifndef TW_DISABLE_TTF
448 if(font->type == FONT_TYPE_TTF)
449 return gr_ttf_textExWH(gl, x, y, s, pFont, -1, -1);
450#endif
451
Dees_Troy51a0e822012-09-05 15:24:24 -0400452 gl->bindTexture(gl, &font->texture);
453 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
454 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
455 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
456 gl->enable(gl, GGL_TEXTURE_2D);
457
458 while((off = *s++)) {
459 off -= 32;
460 cwidth = 0;
461 if (off < 96) {
462 cwidth = font->offset[off+1] - font->offset[off];
463 gl->texCoord2i(gl, (font->offset[off]) - x, 0 - y);
464 gl->recti(gl, x, y, x + cwidth, y + font->cheight);
465 x += cwidth;
466 }
467 }
468
469 return x;
470}
471
472int gr_textExW(int x, int y, const char *s, void* pFont, int max_width)
473{
474 GGLContext *gl = gr_context;
475 GRFont *font = (GRFont*) pFont;
476 unsigned off;
477 unsigned cwidth;
478
479 /* Handle default font */
480 if (!font) font = gr_font;
481
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200482#ifndef TW_DISABLE_TTF
483 if(font->type == FONT_TYPE_TTF)
484 return gr_ttf_textExWH(gl, x, y, s, pFont, max_width, -1);
485#endif
486
Dees_Troy51a0e822012-09-05 15:24:24 -0400487 gl->bindTexture(gl, &font->texture);
488 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
489 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
490 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
491 gl->enable(gl, GGL_TEXTURE_2D);
492
493 while((off = *s++)) {
494 off -= 32;
495 cwidth = 0;
496 if (off < 96) {
497 cwidth = font->offset[off+1] - font->offset[off];
498 if ((x + (int)cwidth) < max_width) {
499 gl->texCoord2i(gl, (font->offset[off]) - x, 0 - y);
500 gl->recti(gl, x, y, x + cwidth, y + font->cheight);
501 x += cwidth;
502 } else {
503 gl->texCoord2i(gl, (font->offset[off]) - x, 0 - y);
504 gl->recti(gl, x, y, max_width, y + font->cheight);
505 x = max_width;
506 return x;
507 }
508 }
509 }
510
511 return x;
512}
513
514int gr_textExWH(int x, int y, const char *s, void* pFont, int max_width, int max_height)
515{
516 GGLContext *gl = gr_context;
517 GRFont *font = (GRFont*) pFont;
518 unsigned off;
519 unsigned cwidth;
520 int rect_x, rect_y;
521
522 /* Handle default font */
523 if (!font) font = gr_font;
524
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200525#ifndef TW_DISABLE_TTF
526 if(font->type == FONT_TYPE_TTF)
527 return gr_ttf_textExWH(gl, x, y, s, pFont, max_width, max_height);
528#endif
529
Dees_Troy51a0e822012-09-05 15:24:24 -0400530 gl->bindTexture(gl, &font->texture);
531 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
532 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
533 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
534 gl->enable(gl, GGL_TEXTURE_2D);
535
536 while((off = *s++)) {
537 off -= 32;
538 cwidth = 0;
539 if (off < 96) {
540 cwidth = font->offset[off+1] - font->offset[off];
541 if ((x + (int)cwidth) < max_width)
542 rect_x = x + cwidth;
543 else
544 rect_x = max_width;
Dees_Troyf7596752012-09-28 13:21:36 -0400545 if (y + font->cheight < (unsigned int)(max_height))
Dees_Troy51a0e822012-09-05 15:24:24 -0400546 rect_y = y + font->cheight;
547 else
548 rect_y = max_height;
549
550 gl->texCoord2i(gl, (font->offset[off]) - x, 0 - y);
551 gl->recti(gl, x, y, rect_x, rect_y);
552 x += cwidth;
553 if (x > max_width)
554 return x;
555 }
556 }
557
558 return x;
559}
560
that9876ac32015-02-15 21:40:59 +0100561void gr_clip(int x, int y, int w, int h)
562{
563 GGLContext *gl = gr_context;
564 gl->scissor(gl, x, y, w, h);
565 gl->enable(gl, GGL_SCISSOR_TEST);
566}
567
568void gr_noclip()
569{
570 GGLContext *gl = gr_context;
571 gl->scissor(gl, 0, 0, gr_fb_width(), gr_fb_height());
572 gl->disable(gl, GGL_SCISSOR_TEST);
573}
574
Dees_Troy51a0e822012-09-05 15:24:24 -0400575void gr_fill(int x, int y, int w, int h)
576{
577 GGLContext *gl = gr_context;
Vojtech Bocek1a4744a2014-02-06 19:52:28 +0100578
579 if(gr_is_curr_clr_opaque)
580 gl->disable(gl, GGL_BLEND);
581
Dees_Troy51a0e822012-09-05 15:24:24 -0400582 gl->disable(gl, GGL_TEXTURE_2D);
583 gl->recti(gl, x, y, x + w, y + h);
Vojtech Bocek1a4744a2014-02-06 19:52:28 +0100584
585 if(gr_is_curr_clr_opaque)
586 gl->enable(gl, GGL_BLEND);
Dees_Troy51a0e822012-09-05 15:24:24 -0400587}
588
589void gr_blit(gr_surface source, int sx, int sy, int w, int h, int dx, int dy) {
Dees Troyf171e102014-11-06 22:19:58 +0100590 if (gr_context == NULL) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400591 return;
592 }
593
594 GGLContext *gl = gr_context;
Vojtech Bocek6c694b62014-02-06 20:36:25 +0100595 GGLSurface *surface = (GGLSurface*)source;
596
597 if(surface->format == GGL_PIXEL_FORMAT_RGBX_8888)
598 gl->disable(gl, GGL_BLEND);
599
600 gl->bindTexture(gl, surface);
Dees_Troy51a0e822012-09-05 15:24:24 -0400601 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
602 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
603 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
604 gl->enable(gl, GGL_TEXTURE_2D);
605 gl->texCoord2i(gl, sx - dx, sy - dy);
606 gl->recti(gl, dx, dy, dx + w, dy + h);
Vojtech Bocek6c694b62014-02-06 20:36:25 +0100607
608 if(surface->format == GGL_PIXEL_FORMAT_RGBX_8888)
Dees Troyf171e102014-11-06 22:19:58 +0100609 gl->enable(gl, GGL_BLEND);
Dees_Troy51a0e822012-09-05 15:24:24 -0400610}
611
612unsigned int gr_get_width(gr_surface surface) {
613 if (surface == NULL) {
614 return 0;
615 }
616 return ((GGLSurface*) surface)->width;
617}
618
619unsigned int gr_get_height(gr_surface surface) {
620 if (surface == NULL) {
621 return 0;
622 }
623 return ((GGLSurface*) surface)->height;
624}
625
626void* gr_loadFont(const char* fontName)
627{
628 int fd;
629 GRFont *font = 0;
630 GGLSurface *ftex;
631 unsigned char *bits, *rle;
632 unsigned char *in, data;
633 unsigned width, height;
634 unsigned element;
635
636 fd = open(fontName, O_RDONLY);
637 if (fd == -1)
638 {
639 char tmp[128];
640
Dees Troy3454ade2015-01-20 19:21:04 +0000641 sprintf(tmp, TWRES "fonts/%s.dat", fontName);
Dees_Troy51a0e822012-09-05 15:24:24 -0400642 fd = open(tmp, O_RDONLY);
643 if (fd == -1)
644 return NULL;
645 }
646
647 font = calloc(sizeof(*font), 1);
648 ftex = &font->texture;
649
650 read(fd, &width, sizeof(unsigned));
651 read(fd, &height, sizeof(unsigned));
652 read(fd, font->offset, sizeof(unsigned) * 96);
653 font->offset[96] = width;
654
655 bits = malloc(width * height);
656 memset(bits, 0, width * height);
657
658 unsigned pos = 0;
659 while (pos < width * height)
660 {
661 int bit;
662
663 read(fd, &data, 1);
664 for (bit = 0; bit < 8; bit++)
665 {
666 if (data & (1 << (7-bit))) bits[pos++] = 255;
667 else bits[pos++] = 0;
668
669 if (pos == width * height) break;
670 }
671 }
672 close(fd);
673
674 ftex->version = sizeof(*ftex);
675 ftex->width = width;
676 ftex->height = height;
677 ftex->stride = width;
678 ftex->data = (void*) bits;
679 ftex->format = GGL_PIXEL_FORMAT_A_8;
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200680 font->type = FONT_TYPE_TWRP;
Dees_Troy51a0e822012-09-05 15:24:24 -0400681 font->cheight = height;
682 font->ascent = height - 2;
683 return (void*) font;
684}
685
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200686void gr_freeFont(void *font)
687{
688 GRFont *f = font;
689 free(f->texture.data);
690 free(f);
691}
692
693int gr_getMaxFontHeight(void *font)
Dees_Troy51a0e822012-09-05 15:24:24 -0400694{
695 GRFont *fnt = (GRFont*) font;
696
697 if (!fnt) fnt = gr_font;
698 if (!fnt) return -1;
699
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200700#ifndef TW_DISABLE_TTF
701 if(fnt->type == FONT_TYPE_TTF)
702 return gr_ttf_getMaxFontHeight(font);
703#endif
704
705 return fnt->cheight;
Dees_Troy51a0e822012-09-05 15:24:24 -0400706}
707
708static void gr_init_font(void)
709{
710 int fontRes;
711 GGLSurface *ftex;
712 unsigned char *bits, *rle;
713 unsigned char *in, data;
714 unsigned width, height;
715 unsigned element;
716
717 gr_font = calloc(sizeof(*gr_font), 1);
718 ftex = &gr_font->texture;
719
720 width = font.width;
721 height = font.height;
722
723 bits = malloc(width * height);
724 rle = bits;
725
726 in = font.rundata;
727 while((data = *in++))
728 {
729 memset(rle, (data & 0x80) ? 255 : 0, data & 0x7f);
730 rle += (data & 0x7f);
731 }
732 for (element = 0; element < 97; element++)
733 {
734 gr_font->offset[element] = (element * font.cwidth);
735 }
736
737 ftex->version = sizeof(*ftex);
738 ftex->width = width;
739 ftex->height = height;
740 ftex->stride = width;
741 ftex->data = (void*) bits;
742 ftex->format = GGL_PIXEL_FORMAT_A_8;
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200743 gr_font->type = FONT_TYPE_TWRP;
Dees_Troy51a0e822012-09-05 15:24:24 -0400744 gr_font->cheight = height;
745 gr_font->ascent = height - 2;
746 return;
747}
748
749int gr_init(void)
750{
751 gglInit(&gr_context);
752 GGLContext *gl = gr_context;
753
754 gr_init_font();
755 gr_vt_fd = open("/dev/tty0", O_RDWR | O_SYNC);
756 if (gr_vt_fd < 0) {
757 // This is non-fatal; post-Cupcake kernels don't have tty0.
758 } else if (ioctl(gr_vt_fd, KDSETMODE, (void*) KD_GRAPHICS)) {
759 // However, if we do open tty0, we expect the ioctl to work.
760 perror("failed KDSETMODE to KD_GRAPHICS on tty0");
761 gr_exit();
762 return -1;
763 }
764
765 gr_fb_fd = get_framebuffer(gr_framebuffer);
766 if (gr_fb_fd < 0) {
767 perror("Unable to get framebuffer.\n");
768 gr_exit();
769 return -1;
770 }
771
772 get_memory_surface(&gr_mem_surface);
773
774 fprintf(stderr, "framebuffer: fd %d (%d x %d)\n",
775 gr_fb_fd, gr_framebuffer[0].width, gr_framebuffer[0].height);
776
777 /* start with 0 as front (displayed) and 1 as back (drawing) */
778 gr_active_fb = 0;
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600779 if (!has_overlay)
780 set_active_framebuffer(0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400781 gl->colorBuffer(gl, &gr_mem_surface);
782
783 gl->activeTexture(gl, 0);
784 gl->enable(gl, GGL_BLEND);
785 gl->blendFunc(gl, GGL_SRC_ALPHA, GGL_ONE_MINUS_SRC_ALPHA);
786
xNUTxcd56f8c2014-07-23 01:30:20 +0200787#ifdef TW_SCREEN_BLANK_ON_BOOT
788 printf("TW_SCREEN_BLANK_ON_BOOT := true\n");
789 gr_fb_blank(true);
790 gr_fb_blank(false);
791#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400792
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600793 if (!alloc_ion_mem(fi.line_length * vi.yres))
794 allocate_overlay(gr_fb_fd, gr_framebuffer);
795
Dees_Troy51a0e822012-09-05 15:24:24 -0400796 return 0;
797}
798
799void gr_exit(void)
800{
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600801 free_overlay(gr_fb_fd);
802 free_ion_mem();
803
Dees_Troy51a0e822012-09-05 15:24:24 -0400804 close(gr_fb_fd);
805 gr_fb_fd = -1;
806
807 free(gr_mem_surface.data);
808
809 ioctl(gr_vt_fd, KDSETMODE, (void*) KD_TEXT);
810 close(gr_vt_fd);
811 gr_vt_fd = -1;
812}
813
814int gr_fb_width(void)
815{
816 return gr_framebuffer[0].width;
817}
818
819int gr_fb_height(void)
820{
821 return gr_framebuffer[0].height;
822}
823
824gr_pixel *gr_fb_data(void)
825{
826 return (unsigned short *) gr_mem_surface.data;
827}
828
Dees_Troy70237dc2013-02-28 21:31:48 +0000829int gr_fb_blank(int blank)
Dees_Troy51a0e822012-09-05 15:24:24 -0400830{
831 int ret;
Dees Troy05d18c92014-08-04 18:17:07 +0000832 //if (blank)
833 //free_overlay(gr_fb_fd);
Dees_Troy51a0e822012-09-05 15:24:24 -0400834
835 ret = ioctl(gr_fb_fd, FBIOBLANK, blank ? FB_BLANK_POWERDOWN : FB_BLANK_UNBLANK);
836 if (ret < 0)
837 perror("ioctl(): blank");
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600838
Dees Troy05d18c92014-08-04 18:17:07 +0000839 //if (!blank)
840 //allocate_overlay(gr_fb_fd, gr_framebuffer);
841 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -0400842}
843
844int gr_get_surface(gr_surface* surface)
845{
846 GGLSurface* ms = malloc(sizeof(GGLSurface));
847 if (!ms) return -1;
848
849 // Allocate the data
850 get_memory_surface(ms);
851
852 // Now, copy the data
853 memcpy(ms->data, gr_mem_surface.data, vi.xres * vi.yres * vi.bits_per_pixel / 8);
854
855 *surface = (gr_surface*) ms;
856 return 0;
857}
858
859int gr_free_surface(gr_surface surface)
860{
861 if (!surface)
862 return -1;
863
864 GGLSurface* ms = (GGLSurface*) surface;
865 free(ms->data);
866 free(ms);
867 return 0;
868}
869
870void gr_write_frame_to_file(int fd)
871{
872 write(fd, gr_mem_surface.data, vi.xres * vi.yres * vi.bits_per_pixel / 8);
873}