blob: 0cdd7726c0d61a78388bae0408e40c7ae8259e9b [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
Ethan Yonkera27d02f2014-11-06 10:24:39 -060080static unsigned char gr_current_r = 255;
81static unsigned char gr_current_g = 255;
82static unsigned char gr_current_b = 255;
83static unsigned char gr_current_a = 255;
84
85static GRSurface* gr_draw = NULL;
86
Vojtech Bocek03fd6c52014-03-13 18:46:34 +010087struct fb_var_screeninfo vi;
Dees_Troy51a0e822012-09-05 15:24:24 -040088static struct fb_fix_screeninfo fi;
89
Ethan Yonker4ee5ad72014-02-18 18:41:17 -060090static bool has_overlay = false;
91static int leftSplit = 0;
92static int rightSplit = 0;
93
94bool target_has_overlay(char *version);
95int free_ion_mem(void);
96int alloc_ion_mem(unsigned int size);
97int allocate_overlay(int fd, GGLSurface gr_fb[]);
98int free_overlay(int fd);
99int overlay_display_frame(int fd, GGLubyte* data, size_t size);
100
Dees_Troy51a0e822012-09-05 15:24:24 -0400101#ifdef PRINT_SCREENINFO
102static void print_fb_var_screeninfo()
103{
Dees_Troy2673cec2013-04-02 20:22:16 +0000104 printf("vi.xres: %d\n", vi.xres);
105 printf("vi.yres: %d\n", vi.yres);
106 printf("vi.xres_virtual: %d\n", vi.xres_virtual);
107 printf("vi.yres_virtual: %d\n", vi.yres_virtual);
108 printf("vi.xoffset: %d\n", vi.xoffset);
109 printf("vi.yoffset: %d\n", vi.yoffset);
110 printf("vi.bits_per_pixel: %d\n", vi.bits_per_pixel);
111 printf("vi.grayscale: %d\n", vi.grayscale);
Dees_Troy51a0e822012-09-05 15:24:24 -0400112}
113#endif
114
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600115#ifdef MSM_BSP
116int getLeftSplit(void) {
117 //Default even split for all displays with high res
118 int lSplit = vi.xres / 2;
119
120 //Override if split published by driver
121 if (leftSplit)
122 lSplit = leftSplit;
123
124 return lSplit;
125}
126
127int getRightSplit(void) {
128 return rightSplit;
129}
130
131
132void setDisplaySplit(void) {
133 char split[64] = {0};
134 FILE* fp = fopen("/sys/class/graphics/fb0/msm_fb_split", "r");
135 if (fp) {
136 //Format "left right" space as delimiter
137 if(fread(split, sizeof(char), 64, fp)) {
138 leftSplit = atoi(split);
139 printf("Left Split=%d\n",leftSplit);
140 char *rght = strpbrk(split, " ");
141 if (rght)
142 rightSplit = atoi(rght + 1);
143 printf("Right Split=%d\n", rightSplit);
144 }
145 } else {
146 printf("Failed to open mdss_fb_split node\n");
147 }
148 if (fp)
149 fclose(fp);
150}
151
152bool isDisplaySplit(void) {
153 if (vi.xres > MAX_DISPLAY_DIM)
154 return true;
155 //check if right split is set by driver
156 if (getRightSplit())
157 return true;
158
159 return false;
160}
161
162int getFbXres(void) {
163 return vi.xres;
164}
165
166int getFbYres (void) {
167 return vi.yres;
168}
169#endif // MSM_BSP
170
Dees_Troy51a0e822012-09-05 15:24:24 -0400171static int get_framebuffer(GGLSurface *fb)
172{
173 int fd;
174 void *bits;
175
176 fd = open("/dev/graphics/fb0", O_RDWR);
177 if (fd < 0) {
178 perror("cannot open fb0");
179 return -1;
180 }
181
182 if (ioctl(fd, FBIOGET_VSCREENINFO, &vi) < 0) {
183 perror("failed to get fb0 info");
184 close(fd);
185 return -1;
186 }
187
188 fprintf(stderr, "Pixel format: %dx%d @ %dbpp\n", vi.xres, vi.yres, vi.bits_per_pixel);
189
190 vi.bits_per_pixel = PIXEL_SIZE * 8;
191 if (PIXEL_FORMAT == GGL_PIXEL_FORMAT_BGRA_8888) {
192 fprintf(stderr, "Pixel format: BGRA_8888\n");
193 if (PIXEL_SIZE != 4) fprintf(stderr, "E: Pixel Size mismatch!\n");
194 vi.red.offset = 8;
195 vi.red.length = 8;
196 vi.green.offset = 16;
197 vi.green.length = 8;
198 vi.blue.offset = 24;
199 vi.blue.length = 8;
200 vi.transp.offset = 0;
201 vi.transp.length = 8;
202 } else if (PIXEL_FORMAT == GGL_PIXEL_FORMAT_RGBX_8888) {
203 fprintf(stderr, "Pixel format: RGBX_8888\n");
204 if (PIXEL_SIZE != 4) fprintf(stderr, "E: Pixel Size mismatch!\n");
205 vi.red.offset = 24;
206 vi.red.length = 8;
207 vi.green.offset = 16;
208 vi.green.length = 8;
209 vi.blue.offset = 8;
210 vi.blue.length = 8;
211 vi.transp.offset = 0;
212 vi.transp.length = 8;
213 } else if (PIXEL_FORMAT == GGL_PIXEL_FORMAT_RGB_565) {
214#ifdef RECOVERY_RGB_565
215 fprintf(stderr, "Pixel format: RGB_565\n");
216 vi.blue.offset = 0;
217 vi.green.offset = 5;
218 vi.red.offset = 11;
219#else
220 fprintf(stderr, "Pixel format: BGR_565\n");
221 vi.blue.offset = 11;
222 vi.green.offset = 5;
223 vi.red.offset = 0;
224#endif
225 if (PIXEL_SIZE != 2) fprintf(stderr, "E: Pixel Size mismatch!\n");
226 vi.blue.length = 5;
227 vi.green.length = 6;
228 vi.red.length = 5;
229 vi.blue.msb_right = 0;
230 vi.green.msb_right = 0;
231 vi.red.msb_right = 0;
232 vi.transp.offset = 0;
233 vi.transp.length = 0;
234 }
235 else
236 {
237 perror("unknown pixel format");
238 close(fd);
239 return -1;
240 }
241
242 vi.vmode = FB_VMODE_NONINTERLACED;
243 vi.activate = FB_ACTIVATE_NOW | FB_ACTIVATE_FORCE;
244
245 if (ioctl(fd, FBIOPUT_VSCREENINFO, &vi) < 0) {
246 perror("failed to put fb0 info");
247 close(fd);
248 return -1;
249 }
250
251 if (ioctl(fd, FBIOGET_FSCREENINFO, &fi) < 0) {
252 perror("failed to get fb0 info");
253 close(fd);
254 return -1;
255 }
256
Dees Troyb0425382014-04-03 00:10:03 +0000257#ifdef MSM_BSP
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600258 has_overlay = target_has_overlay(fi.id);
259
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600260 if (isTargetMdp5())
261 setDisplaySplit();
Dees Troyb0425382014-04-03 00:10:03 +0000262#else
263 has_overlay = false;
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600264#endif
265
266 if (!has_overlay) {
267 printf("Not using qualcomm overlay, '%s'\n", fi.id);
268 bits = mmap(0, fi.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
269 if (bits == MAP_FAILED) {
270 perror("failed to mmap framebuffer");
271 close(fd);
272 return -1;
273 }
274 } else {
275 printf("Using qualcomm overlay\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400276 }
277
278#ifdef RECOVERY_GRAPHICS_USE_LINELENGTH
279 vi.xres_virtual = fi.line_length / PIXEL_SIZE;
280#endif
281
282 fb->version = sizeof(*fb);
283 fb->width = vi.xres;
284 fb->height = vi.yres;
285#ifdef BOARD_HAS_JANKY_BACKBUFFER
Dees_Troy2673cec2013-04-02 20:22:16 +0000286 printf("setting JANKY BACKBUFFER\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400287 fb->stride = fi.line_length/2;
288#else
289 fb->stride = vi.xres_virtual;
290#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400291 fb->format = PIXEL_FORMAT;
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600292 if (!has_overlay) {
293 fb->data = bits;
294 memset(fb->data, 0, vi.yres * fb->stride * PIXEL_SIZE);
295 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400296
297 fb++;
298
Hiemanshu Sharma94f6c882012-11-21 11:25:22 -0600299 /* check if we can use double buffering */
300 if (vi.yres * fi.line_length * 2 > fi.smem_len)
301 return fd;
302
303 double_buffering = 1;
304
Dees_Troy51a0e822012-09-05 15:24:24 -0400305 fb->version = sizeof(*fb);
306 fb->width = vi.xres;
307 fb->height = vi.yres;
308#ifdef BOARD_HAS_JANKY_BACKBUFFER
309 fb->stride = fi.line_length/2;
310 fb->data = (void*) (((unsigned) bits) + vi.yres * fi.line_length);
311#else
312 fb->stride = vi.xres_virtual;
313 fb->data = (void*) (((unsigned) bits) + vi.yres * fb->stride * PIXEL_SIZE);
314#endif
315 fb->format = PIXEL_FORMAT;
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600316 if (!has_overlay) {
317 memset(fb->data, 0, vi.yres * fb->stride * PIXEL_SIZE);
318 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400319
320#ifdef PRINT_SCREENINFO
321 print_fb_var_screeninfo();
322#endif
323
324 return fd;
325}
326
327static void get_memory_surface(GGLSurface* ms) {
328 ms->version = sizeof(*ms);
329 ms->width = vi.xres;
330 ms->height = vi.yres;
331 ms->stride = vi.xres_virtual;
332 ms->data = malloc(vi.xres_virtual * vi.yres * PIXEL_SIZE);
333 ms->format = PIXEL_FORMAT;
334}
335
336static void set_active_framebuffer(unsigned n)
337{
Hiemanshu Sharma94f6c882012-11-21 11:25:22 -0600338 if (n > 1 || !double_buffering) return;
Hiemanshu Sharmaacf6a9b2012-11-21 11:28:36 -0600339 vi.yres_virtual = vi.yres * NUM_BUFFERS;
Dees_Troy51a0e822012-09-05 15:24:24 -0400340 vi.yoffset = n * vi.yres;
341// vi.bits_per_pixel = PIXEL_SIZE * 8;
342 if (ioctl(gr_fb_fd, FBIOPUT_VSCREENINFO, &vi) < 0) {
343 perror("active fb swap failed");
344 }
345}
346
347void gr_flip(void)
348{
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600349 if (-EINVAL == overlay_display_frame(gr_fb_fd, gr_mem_surface.data,
350 (fi.line_length * vi.yres))) {
351 GGLContext *gl = gr_context;
Dees_Troy51a0e822012-09-05 15:24:24 -0400352
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600353 /* swap front and back buffers */
354 if (double_buffering)
355 gr_active_fb = (gr_active_fb + 1) & 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400356
357#ifdef BOARD_HAS_FLIPPED_SCREEN
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600358 /* flip buffer 180 degrees for devices with physicaly inverted screens */
359 unsigned int i;
360 unsigned int j;
361 uint8_t tmp;
362 for (i = 0; i < ((vi.xres_virtual * vi.yres)/2); i++) {
363 for (j = 0; j < PIXEL_SIZE; j++) {
364 tmp = gr_mem_surface.data[i * PIXEL_SIZE + j];
365 gr_mem_surface.data[i * PIXEL_SIZE + j] = gr_mem_surface.data[(vi.xres_virtual * vi.yres * PIXEL_SIZE) - ((i+1) * PIXEL_SIZE) + j];
366 gr_mem_surface.data[(vi.xres_virtual * vi.yres * PIXEL_SIZE) - ((i+1) * PIXEL_SIZE) + j] = tmp;
367 }
368 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400369#endif
370
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600371 /* copy data from the in-memory surface to the buffer we're about
372 * to make active. */
373 memcpy(gr_framebuffer[gr_active_fb].data, gr_mem_surface.data,
374 vi.xres_virtual * vi.yres * PIXEL_SIZE);
Dees_Troy51a0e822012-09-05 15:24:24 -0400375
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600376 /* inform the display driver */
377 set_active_framebuffer(gr_active_fb);
378 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400379}
380
381void gr_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
382{
383 GGLContext *gl = gr_context;
384 GGLint color[4];
385 color[0] = ((r << 8) | r) + 1;
386 color[1] = ((g << 8) | g) + 1;
387 color[2] = ((b << 8) | b) + 1;
388 color[3] = ((a << 8) | a) + 1;
389 gl->color4xv(gl, color);
Vojtech Bocek1a4744a2014-02-06 19:52:28 +0100390
391 gr_is_curr_clr_opaque = (a == 255);
Dees_Troy51a0e822012-09-05 15:24:24 -0400392}
393
394int gr_measureEx(const char *s, void* font)
395{
396 GRFont* fnt = (GRFont*) font;
397 int total = 0;
398 unsigned pos;
399 unsigned off;
400
401 if (!fnt) fnt = gr_font;
402
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200403#ifndef TW_DISABLE_TTF
404 if(fnt->type == FONT_TYPE_TTF)
405 return gr_ttf_measureEx(s, font);
406#endif
407
Dees_Troy51a0e822012-09-05 15:24:24 -0400408 while ((off = *s++))
409 {
410 off -= 32;
411 if (off < 96)
412 total += (fnt->offset[off+1] - fnt->offset[off]);
413 }
414 return total;
415}
416
Dees Troy31218ec2014-02-25 20:35:56 +0000417int gr_maxExW(const char *s, void* font, int max_width)
418{
419 GRFont* fnt = (GRFont*) font;
420 int total = 0;
421 unsigned pos;
422 unsigned off;
423
424 if (!fnt) fnt = gr_font;
425
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200426#ifndef TW_DISABLE_TTF
427 if(fnt->type == FONT_TYPE_TTF)
428 return gr_ttf_maxExW(s, font, max_width);
429#endif
430
Dees Troy31218ec2014-02-25 20:35:56 +0000431 while ((off = *s++))
432 {
433 off -= 32;
434 if (off < 96) {
435 max_width -= (fnt->offset[off+1] - fnt->offset[off]);
436 if (max_width > 0) {
437 total++;
438 } else {
439 return total;
440 }
441 }
442 }
443 return total;
444}
445
Dees_Troy51a0e822012-09-05 15:24:24 -0400446int gr_textEx(int x, int y, const char *s, void* pFont)
447{
448 GGLContext *gl = gr_context;
449 GRFont *font = (GRFont*) pFont;
450 unsigned off;
451 unsigned cwidth;
452
453 /* Handle default font */
454 if (!font) font = gr_font;
455
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200456#ifndef TW_DISABLE_TTF
457 if(font->type == FONT_TYPE_TTF)
458 return gr_ttf_textExWH(gl, x, y, s, pFont, -1, -1);
459#endif
460
Dees_Troy51a0e822012-09-05 15:24:24 -0400461 gl->bindTexture(gl, &font->texture);
462 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
463 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
464 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
465 gl->enable(gl, GGL_TEXTURE_2D);
466
467 while((off = *s++)) {
468 off -= 32;
469 cwidth = 0;
470 if (off < 96) {
471 cwidth = font->offset[off+1] - font->offset[off];
472 gl->texCoord2i(gl, (font->offset[off]) - x, 0 - y);
473 gl->recti(gl, x, y, x + cwidth, y + font->cheight);
474 x += cwidth;
475 }
476 }
477
478 return x;
479}
480
481int gr_textExW(int x, int y, const char *s, void* pFont, int max_width)
482{
483 GGLContext *gl = gr_context;
484 GRFont *font = (GRFont*) pFont;
485 unsigned off;
486 unsigned cwidth;
487
488 /* Handle default font */
489 if (!font) font = gr_font;
490
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200491#ifndef TW_DISABLE_TTF
492 if(font->type == FONT_TYPE_TTF)
493 return gr_ttf_textExWH(gl, x, y, s, pFont, max_width, -1);
494#endif
495
Dees_Troy51a0e822012-09-05 15:24:24 -0400496 gl->bindTexture(gl, &font->texture);
497 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
498 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
499 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
500 gl->enable(gl, GGL_TEXTURE_2D);
501
502 while((off = *s++)) {
503 off -= 32;
504 cwidth = 0;
505 if (off < 96) {
506 cwidth = font->offset[off+1] - font->offset[off];
507 if ((x + (int)cwidth) < max_width) {
508 gl->texCoord2i(gl, (font->offset[off]) - x, 0 - y);
509 gl->recti(gl, x, y, x + cwidth, y + font->cheight);
510 x += cwidth;
511 } else {
512 gl->texCoord2i(gl, (font->offset[off]) - x, 0 - y);
513 gl->recti(gl, x, y, max_width, y + font->cheight);
514 x = max_width;
515 return x;
516 }
517 }
518 }
519
520 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
567 return x;
568}
569
Dees_Troy51a0e822012-09-05 15:24:24 -0400570void gr_fill(int x, int y, int w, int h)
571{
572 GGLContext *gl = gr_context;
Vojtech Bocek1a4744a2014-02-06 19:52:28 +0100573
574 if(gr_is_curr_clr_opaque)
575 gl->disable(gl, GGL_BLEND);
576
Dees_Troy51a0e822012-09-05 15:24:24 -0400577 gl->disable(gl, GGL_TEXTURE_2D);
578 gl->recti(gl, x, y, x + w, y + h);
Vojtech Bocek1a4744a2014-02-06 19:52:28 +0100579
580 if(gr_is_curr_clr_opaque)
581 gl->enable(gl, GGL_BLEND);
Dees_Troy51a0e822012-09-05 15:24:24 -0400582}
583
584void gr_blit(gr_surface source, int sx, int sy, int w, int h, int dx, int dy) {
Ethan Yonkera27d02f2014-11-06 10:24:39 -0600585 // This is the old code, not sure if we still need it or not
586 /*if (gr_context == NULL) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400587 return;
588 }
589
590 GGLContext *gl = gr_context;
Vojtech Bocek6c694b62014-02-06 20:36:25 +0100591 GGLSurface *surface = (GGLSurface*)source;
592
593 if(surface->format == GGL_PIXEL_FORMAT_RGBX_8888)
594 gl->disable(gl, GGL_BLEND);
595
596 gl->bindTexture(gl, surface);
Dees_Troy51a0e822012-09-05 15:24:24 -0400597 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
598 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
599 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
600 gl->enable(gl, GGL_TEXTURE_2D);
601 gl->texCoord2i(gl, sx - dx, sy - dy);
602 gl->recti(gl, dx, dy, dx + w, dy + h);
Vojtech Bocek6c694b62014-02-06 20:36:25 +0100603
604 if(surface->format == GGL_PIXEL_FORMAT_RGBX_8888)
Ethan Yonkera27d02f2014-11-06 10:24:39 -0600605 gl->enable(gl, GGL_BLEND);*/
606 if (source == NULL) return;
607
608 // This code doesn't compile yet, but may very well be needed later
609 /*if (gr_draw->pixel_bytes != source->pixel_bytes) {
610 printf("gr_blit: source has wrong format\n");
611 return;
612 }
613
614 dx += overscan_offset_x;
615 dy += overscan_offset_y;
616
617 if (outside(dx, dy) || outside(dx+w-1, dy+h-1)) return;*/
618
619 unsigned char* src_p = source->data + sy*source->row_bytes + sx*source->pixel_bytes;
620 unsigned char* dst_p = gr_draw->data + dy*gr_draw->row_bytes + dx*gr_draw->pixel_bytes;
621
622 int i;
623 for (i = 0; i < h; ++i) {
624 memcpy(dst_p, src_p, w * source->pixel_bytes);
625 src_p += source->row_bytes;
626 dst_p += gr_draw->row_bytes;
627 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400628}
629
630unsigned int gr_get_width(gr_surface surface) {
631 if (surface == NULL) {
632 return 0;
633 }
634 return ((GGLSurface*) surface)->width;
635}
636
637unsigned int gr_get_height(gr_surface surface) {
638 if (surface == NULL) {
639 return 0;
640 }
641 return ((GGLSurface*) surface)->height;
642}
643
644void* gr_loadFont(const char* fontName)
645{
646 int fd;
647 GRFont *font = 0;
648 GGLSurface *ftex;
649 unsigned char *bits, *rle;
650 unsigned char *in, data;
651 unsigned width, height;
652 unsigned element;
653
654 fd = open(fontName, O_RDONLY);
655 if (fd == -1)
656 {
657 char tmp[128];
658
659 sprintf(tmp, "/res/fonts/%s.dat", fontName);
660 fd = open(tmp, O_RDONLY);
661 if (fd == -1)
662 return NULL;
663 }
664
665 font = calloc(sizeof(*font), 1);
666 ftex = &font->texture;
667
668 read(fd, &width, sizeof(unsigned));
669 read(fd, &height, sizeof(unsigned));
670 read(fd, font->offset, sizeof(unsigned) * 96);
671 font->offset[96] = width;
672
673 bits = malloc(width * height);
674 memset(bits, 0, width * height);
675
676 unsigned pos = 0;
677 while (pos < width * height)
678 {
679 int bit;
680
681 read(fd, &data, 1);
682 for (bit = 0; bit < 8; bit++)
683 {
684 if (data & (1 << (7-bit))) bits[pos++] = 255;
685 else bits[pos++] = 0;
686
687 if (pos == width * height) break;
688 }
689 }
690 close(fd);
691
692 ftex->version = sizeof(*ftex);
693 ftex->width = width;
694 ftex->height = height;
695 ftex->stride = width;
696 ftex->data = (void*) bits;
697 ftex->format = GGL_PIXEL_FORMAT_A_8;
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200698 font->type = FONT_TYPE_TWRP;
Dees_Troy51a0e822012-09-05 15:24:24 -0400699 font->cheight = height;
700 font->ascent = height - 2;
701 return (void*) font;
702}
703
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200704void gr_freeFont(void *font)
705{
706 GRFont *f = font;
707 free(f->texture.data);
708 free(f);
709}
710
711int gr_getMaxFontHeight(void *font)
Dees_Troy51a0e822012-09-05 15:24:24 -0400712{
713 GRFont *fnt = (GRFont*) font;
714
715 if (!fnt) fnt = gr_font;
716 if (!fnt) return -1;
717
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200718#ifndef TW_DISABLE_TTF
719 if(fnt->type == FONT_TYPE_TTF)
720 return gr_ttf_getMaxFontHeight(font);
721#endif
722
723 return fnt->cheight;
Dees_Troy51a0e822012-09-05 15:24:24 -0400724}
725
726static void gr_init_font(void)
727{
728 int fontRes;
729 GGLSurface *ftex;
730 unsigned char *bits, *rle;
731 unsigned char *in, data;
732 unsigned width, height;
733 unsigned element;
734
735 gr_font = calloc(sizeof(*gr_font), 1);
736 ftex = &gr_font->texture;
737
738 width = font.width;
739 height = font.height;
740
741 bits = malloc(width * height);
742 rle = bits;
743
744 in = font.rundata;
745 while((data = *in++))
746 {
747 memset(rle, (data & 0x80) ? 255 : 0, data & 0x7f);
748 rle += (data & 0x7f);
749 }
750 for (element = 0; element < 97; element++)
751 {
752 gr_font->offset[element] = (element * font.cwidth);
753 }
754
755 ftex->version = sizeof(*ftex);
756 ftex->width = width;
757 ftex->height = height;
758 ftex->stride = width;
759 ftex->data = (void*) bits;
760 ftex->format = GGL_PIXEL_FORMAT_A_8;
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200761 gr_font->type = FONT_TYPE_TWRP;
Dees_Troy51a0e822012-09-05 15:24:24 -0400762 gr_font->cheight = height;
763 gr_font->ascent = height - 2;
764 return;
765}
766
767int gr_init(void)
768{
769 gglInit(&gr_context);
770 GGLContext *gl = gr_context;
771
772 gr_init_font();
773 gr_vt_fd = open("/dev/tty0", O_RDWR | O_SYNC);
774 if (gr_vt_fd < 0) {
775 // This is non-fatal; post-Cupcake kernels don't have tty0.
776 } else if (ioctl(gr_vt_fd, KDSETMODE, (void*) KD_GRAPHICS)) {
777 // However, if we do open tty0, we expect the ioctl to work.
778 perror("failed KDSETMODE to KD_GRAPHICS on tty0");
779 gr_exit();
780 return -1;
781 }
782
783 gr_fb_fd = get_framebuffer(gr_framebuffer);
784 if (gr_fb_fd < 0) {
785 perror("Unable to get framebuffer.\n");
786 gr_exit();
787 return -1;
788 }
789
790 get_memory_surface(&gr_mem_surface);
791
792 fprintf(stderr, "framebuffer: fd %d (%d x %d)\n",
793 gr_fb_fd, gr_framebuffer[0].width, gr_framebuffer[0].height);
794
795 /* start with 0 as front (displayed) and 1 as back (drawing) */
796 gr_active_fb = 0;
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600797 if (!has_overlay)
798 set_active_framebuffer(0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400799 gl->colorBuffer(gl, &gr_mem_surface);
800
801 gl->activeTexture(gl, 0);
802 gl->enable(gl, GGL_BLEND);
803 gl->blendFunc(gl, GGL_SRC_ALPHA, GGL_ONE_MINUS_SRC_ALPHA);
804
xNUTxcd56f8c2014-07-23 01:30:20 +0200805#ifdef TW_SCREEN_BLANK_ON_BOOT
806 printf("TW_SCREEN_BLANK_ON_BOOT := true\n");
807 gr_fb_blank(true);
808 gr_fb_blank(false);
809#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400810
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600811 if (!alloc_ion_mem(fi.line_length * vi.yres))
812 allocate_overlay(gr_fb_fd, gr_framebuffer);
813
Dees_Troy51a0e822012-09-05 15:24:24 -0400814 return 0;
815}
816
817void gr_exit(void)
818{
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600819 free_overlay(gr_fb_fd);
820 free_ion_mem();
821
Dees_Troy51a0e822012-09-05 15:24:24 -0400822 close(gr_fb_fd);
823 gr_fb_fd = -1;
824
825 free(gr_mem_surface.data);
826
827 ioctl(gr_vt_fd, KDSETMODE, (void*) KD_TEXT);
828 close(gr_vt_fd);
829 gr_vt_fd = -1;
830}
831
832int gr_fb_width(void)
833{
834 return gr_framebuffer[0].width;
835}
836
837int gr_fb_height(void)
838{
839 return gr_framebuffer[0].height;
840}
841
842gr_pixel *gr_fb_data(void)
843{
844 return (unsigned short *) gr_mem_surface.data;
845}
846
Dees_Troy70237dc2013-02-28 21:31:48 +0000847int gr_fb_blank(int blank)
Dees_Troy51a0e822012-09-05 15:24:24 -0400848{
849 int ret;
Dees Troy05d18c92014-08-04 18:17:07 +0000850 //if (blank)
851 //free_overlay(gr_fb_fd);
Dees_Troy51a0e822012-09-05 15:24:24 -0400852
853 ret = ioctl(gr_fb_fd, FBIOBLANK, blank ? FB_BLANK_POWERDOWN : FB_BLANK_UNBLANK);
854 if (ret < 0)
855 perror("ioctl(): blank");
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600856
Dees Troy05d18c92014-08-04 18:17:07 +0000857 //if (!blank)
858 //allocate_overlay(gr_fb_fd, gr_framebuffer);
859 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -0400860}
861
862int gr_get_surface(gr_surface* surface)
863{
864 GGLSurface* ms = malloc(sizeof(GGLSurface));
865 if (!ms) return -1;
866
867 // Allocate the data
868 get_memory_surface(ms);
869
870 // Now, copy the data
871 memcpy(ms->data, gr_mem_surface.data, vi.xres * vi.yres * vi.bits_per_pixel / 8);
872
873 *surface = (gr_surface*) ms;
874 return 0;
875}
876
877int gr_free_surface(gr_surface surface)
878{
879 if (!surface)
880 return -1;
881
882 GGLSurface* ms = (GGLSurface*) surface;
883 free(ms->data);
884 free(ms);
885 return 0;
886}
887
888void gr_write_frame_to_file(int fd)
889{
890 write(fd, gr_mem_surface.data, vi.xres * vi.yres * vi.bits_per_pixel / 8);
891}