blob: 93123e40a10be202335ecc52c0bbea64afa98ad8 [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
Dees_Troy51a0e822012-09-05 15:24:24 -0400561void gr_fill(int x, int y, int w, int h)
562{
563 GGLContext *gl = gr_context;
Vojtech Bocek1a4744a2014-02-06 19:52:28 +0100564
565 if(gr_is_curr_clr_opaque)
566 gl->disable(gl, GGL_BLEND);
567
Dees_Troy51a0e822012-09-05 15:24:24 -0400568 gl->disable(gl, GGL_TEXTURE_2D);
569 gl->recti(gl, x, y, x + w, y + h);
Vojtech Bocek1a4744a2014-02-06 19:52:28 +0100570
571 if(gr_is_curr_clr_opaque)
572 gl->enable(gl, GGL_BLEND);
Dees_Troy51a0e822012-09-05 15:24:24 -0400573}
574
575void gr_blit(gr_surface source, int sx, int sy, int w, int h, int dx, int dy) {
Dees Troyf171e102014-11-06 22:19:58 +0100576 if (gr_context == NULL) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400577 return;
578 }
579
580 GGLContext *gl = gr_context;
Vojtech Bocek6c694b62014-02-06 20:36:25 +0100581 GGLSurface *surface = (GGLSurface*)source;
582
583 if(surface->format == GGL_PIXEL_FORMAT_RGBX_8888)
584 gl->disable(gl, GGL_BLEND);
585
586 gl->bindTexture(gl, surface);
Dees_Troy51a0e822012-09-05 15:24:24 -0400587 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
588 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
589 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
590 gl->enable(gl, GGL_TEXTURE_2D);
591 gl->texCoord2i(gl, sx - dx, sy - dy);
592 gl->recti(gl, dx, dy, dx + w, dy + h);
Vojtech Bocek6c694b62014-02-06 20:36:25 +0100593
594 if(surface->format == GGL_PIXEL_FORMAT_RGBX_8888)
Dees Troyf171e102014-11-06 22:19:58 +0100595 gl->enable(gl, GGL_BLEND);
Dees_Troy51a0e822012-09-05 15:24:24 -0400596}
597
598unsigned int gr_get_width(gr_surface surface) {
599 if (surface == NULL) {
600 return 0;
601 }
602 return ((GGLSurface*) surface)->width;
603}
604
605unsigned int gr_get_height(gr_surface surface) {
606 if (surface == NULL) {
607 return 0;
608 }
609 return ((GGLSurface*) surface)->height;
610}
611
612void* gr_loadFont(const char* fontName)
613{
614 int fd;
615 GRFont *font = 0;
616 GGLSurface *ftex;
617 unsigned char *bits, *rle;
618 unsigned char *in, data;
619 unsigned width, height;
620 unsigned element;
621
622 fd = open(fontName, O_RDONLY);
623 if (fd == -1)
624 {
625 char tmp[128];
626
Dees Troy3454ade2015-01-20 19:21:04 +0000627 sprintf(tmp, TWRES "fonts/%s.dat", fontName);
Dees_Troy51a0e822012-09-05 15:24:24 -0400628 fd = open(tmp, O_RDONLY);
629 if (fd == -1)
630 return NULL;
631 }
632
633 font = calloc(sizeof(*font), 1);
634 ftex = &font->texture;
635
636 read(fd, &width, sizeof(unsigned));
637 read(fd, &height, sizeof(unsigned));
638 read(fd, font->offset, sizeof(unsigned) * 96);
639 font->offset[96] = width;
640
641 bits = malloc(width * height);
642 memset(bits, 0, width * height);
643
644 unsigned pos = 0;
645 while (pos < width * height)
646 {
647 int bit;
648
649 read(fd, &data, 1);
650 for (bit = 0; bit < 8; bit++)
651 {
652 if (data & (1 << (7-bit))) bits[pos++] = 255;
653 else bits[pos++] = 0;
654
655 if (pos == width * height) break;
656 }
657 }
658 close(fd);
659
660 ftex->version = sizeof(*ftex);
661 ftex->width = width;
662 ftex->height = height;
663 ftex->stride = width;
664 ftex->data = (void*) bits;
665 ftex->format = GGL_PIXEL_FORMAT_A_8;
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200666 font->type = FONT_TYPE_TWRP;
Dees_Troy51a0e822012-09-05 15:24:24 -0400667 font->cheight = height;
668 font->ascent = height - 2;
669 return (void*) font;
670}
671
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200672void gr_freeFont(void *font)
673{
674 GRFont *f = font;
675 free(f->texture.data);
676 free(f);
677}
678
679int gr_getMaxFontHeight(void *font)
Dees_Troy51a0e822012-09-05 15:24:24 -0400680{
681 GRFont *fnt = (GRFont*) font;
682
683 if (!fnt) fnt = gr_font;
684 if (!fnt) return -1;
685
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200686#ifndef TW_DISABLE_TTF
687 if(fnt->type == FONT_TYPE_TTF)
688 return gr_ttf_getMaxFontHeight(font);
689#endif
690
691 return fnt->cheight;
Dees_Troy51a0e822012-09-05 15:24:24 -0400692}
693
694static void gr_init_font(void)
695{
696 int fontRes;
697 GGLSurface *ftex;
698 unsigned char *bits, *rle;
699 unsigned char *in, data;
700 unsigned width, height;
701 unsigned element;
702
703 gr_font = calloc(sizeof(*gr_font), 1);
704 ftex = &gr_font->texture;
705
706 width = font.width;
707 height = font.height;
708
709 bits = malloc(width * height);
710 rle = bits;
711
712 in = font.rundata;
713 while((data = *in++))
714 {
715 memset(rle, (data & 0x80) ? 255 : 0, data & 0x7f);
716 rle += (data & 0x7f);
717 }
718 for (element = 0; element < 97; element++)
719 {
720 gr_font->offset[element] = (element * font.cwidth);
721 }
722
723 ftex->version = sizeof(*ftex);
724 ftex->width = width;
725 ftex->height = height;
726 ftex->stride = width;
727 ftex->data = (void*) bits;
728 ftex->format = GGL_PIXEL_FORMAT_A_8;
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200729 gr_font->type = FONT_TYPE_TWRP;
Dees_Troy51a0e822012-09-05 15:24:24 -0400730 gr_font->cheight = height;
731 gr_font->ascent = height - 2;
732 return;
733}
734
735int gr_init(void)
736{
737 gglInit(&gr_context);
738 GGLContext *gl = gr_context;
739
740 gr_init_font();
741 gr_vt_fd = open("/dev/tty0", O_RDWR | O_SYNC);
742 if (gr_vt_fd < 0) {
743 // This is non-fatal; post-Cupcake kernels don't have tty0.
744 } else if (ioctl(gr_vt_fd, KDSETMODE, (void*) KD_GRAPHICS)) {
745 // However, if we do open tty0, we expect the ioctl to work.
746 perror("failed KDSETMODE to KD_GRAPHICS on tty0");
747 gr_exit();
748 return -1;
749 }
750
751 gr_fb_fd = get_framebuffer(gr_framebuffer);
752 if (gr_fb_fd < 0) {
753 perror("Unable to get framebuffer.\n");
754 gr_exit();
755 return -1;
756 }
757
758 get_memory_surface(&gr_mem_surface);
759
760 fprintf(stderr, "framebuffer: fd %d (%d x %d)\n",
761 gr_fb_fd, gr_framebuffer[0].width, gr_framebuffer[0].height);
762
763 /* start with 0 as front (displayed) and 1 as back (drawing) */
764 gr_active_fb = 0;
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600765 if (!has_overlay)
766 set_active_framebuffer(0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400767 gl->colorBuffer(gl, &gr_mem_surface);
768
769 gl->activeTexture(gl, 0);
770 gl->enable(gl, GGL_BLEND);
771 gl->blendFunc(gl, GGL_SRC_ALPHA, GGL_ONE_MINUS_SRC_ALPHA);
772
xNUTxcd56f8c2014-07-23 01:30:20 +0200773#ifdef TW_SCREEN_BLANK_ON_BOOT
774 printf("TW_SCREEN_BLANK_ON_BOOT := true\n");
775 gr_fb_blank(true);
776 gr_fb_blank(false);
777#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400778
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600779 if (!alloc_ion_mem(fi.line_length * vi.yres))
780 allocate_overlay(gr_fb_fd, gr_framebuffer);
781
Dees_Troy51a0e822012-09-05 15:24:24 -0400782 return 0;
783}
784
785void gr_exit(void)
786{
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600787 free_overlay(gr_fb_fd);
788 free_ion_mem();
789
Dees_Troy51a0e822012-09-05 15:24:24 -0400790 close(gr_fb_fd);
791 gr_fb_fd = -1;
792
793 free(gr_mem_surface.data);
794
795 ioctl(gr_vt_fd, KDSETMODE, (void*) KD_TEXT);
796 close(gr_vt_fd);
797 gr_vt_fd = -1;
798}
799
800int gr_fb_width(void)
801{
802 return gr_framebuffer[0].width;
803}
804
805int gr_fb_height(void)
806{
807 return gr_framebuffer[0].height;
808}
809
810gr_pixel *gr_fb_data(void)
811{
812 return (unsigned short *) gr_mem_surface.data;
813}
814
Dees_Troy70237dc2013-02-28 21:31:48 +0000815int gr_fb_blank(int blank)
Dees_Troy51a0e822012-09-05 15:24:24 -0400816{
817 int ret;
Dees Troy05d18c92014-08-04 18:17:07 +0000818 //if (blank)
819 //free_overlay(gr_fb_fd);
Dees_Troy51a0e822012-09-05 15:24:24 -0400820
821 ret = ioctl(gr_fb_fd, FBIOBLANK, blank ? FB_BLANK_POWERDOWN : FB_BLANK_UNBLANK);
822 if (ret < 0)
823 perror("ioctl(): blank");
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600824
Dees Troy05d18c92014-08-04 18:17:07 +0000825 //if (!blank)
826 //allocate_overlay(gr_fb_fd, gr_framebuffer);
827 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -0400828}
829
830int gr_get_surface(gr_surface* surface)
831{
832 GGLSurface* ms = malloc(sizeof(GGLSurface));
833 if (!ms) return -1;
834
835 // Allocate the data
836 get_memory_surface(ms);
837
838 // Now, copy the data
839 memcpy(ms->data, gr_mem_surface.data, vi.xres * vi.yres * vi.bits_per_pixel / 8);
840
841 *surface = (gr_surface*) ms;
842 return 0;
843}
844
845int gr_free_surface(gr_surface surface)
846{
847 if (!surface)
848 return -1;
849
850 GGLSurface* ms = (GGLSurface*) surface;
851 free(ms->data);
852 free(ms);
853 return 0;
854}
855
856void gr_write_frame_to_file(int fd)
857{
858 write(fd, gr_mem_surface.data, vi.xres * vi.yres * vi.bits_per_pixel / 8);
859}