blob: fb86c1450108d1609be2f3ada7ebc52b55ab7925 [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>
Ethan Yonkerc798c9c2015-10-09 11:15:26 -050024#include <string.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040025
26#include <sys/ioctl.h>
27#include <sys/mman.h>
28#include <sys/types.h>
29
30#include <linux/fb.h>
31#include <linux/kd.h>
32
33#include <pixelflinger/pixelflinger.h>
34
35#include "minui.h"
Ethan Yonkerb7a54a32015-10-05 10:16:27 -050036#include "../gui/placement.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040037
Dees_Troy51a0e822012-09-05 15:24:24 -040038#ifdef RECOVERY_BGRA
39#define PIXEL_FORMAT GGL_PIXEL_FORMAT_BGRA_8888
40#define PIXEL_SIZE 4
41#endif
Kra1o577568592015-10-14 18:09:54 +020042#ifdef RECOVERY_RGBA
43#define PIXEL_FORMAT GGL_PIXEL_FORMAT_RGBA_8888
44#define PIXEL_SIZE 4
45#endif
Dees_Troy51a0e822012-09-05 15:24:24 -040046#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{
Ethan Yonkerb7a54a32015-10-05 10:16:27 -050097 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;
Kra1o577568592015-10-14 18:09:54 +0200201 } else if (PIXEL_FORMAT == GGL_PIXEL_FORMAT_RGBA_8888) {
202 fprintf(stderr, "Pixel format: RGBA_8888\n");
203 if (PIXEL_SIZE != 4) fprintf(stderr, "E: Pixel Size mismatch!\n");
204 vi.red.offset = 0;
205 vi.red.length = 8;
206 vi.green.offset = 8;
207 vi.green.length = 8;
208 vi.blue.offset = 16;
209 vi.blue.length = 8;
210 vi.transp.offset = 24;
211 vi.transp.length = 8;
Dees_Troy51a0e822012-09-05 15:24:24 -0400212 } else if (PIXEL_FORMAT == GGL_PIXEL_FORMAT_RGBX_8888) {
213 fprintf(stderr, "Pixel format: RGBX_8888\n");
214 if (PIXEL_SIZE != 4) fprintf(stderr, "E: Pixel Size mismatch!\n");
215 vi.red.offset = 24;
216 vi.red.length = 8;
217 vi.green.offset = 16;
218 vi.green.length = 8;
219 vi.blue.offset = 8;
220 vi.blue.length = 8;
221 vi.transp.offset = 0;
222 vi.transp.length = 8;
223 } else if (PIXEL_FORMAT == GGL_PIXEL_FORMAT_RGB_565) {
224#ifdef RECOVERY_RGB_565
Ethan Yonkerb7a54a32015-10-05 10:16:27 -0500225 fprintf(stderr, "Pixel format: RGB_565\n");
226 vi.blue.offset = 0;
227 vi.green.offset = 5;
228 vi.red.offset = 11;
Dees_Troy51a0e822012-09-05 15:24:24 -0400229#else
230 fprintf(stderr, "Pixel format: BGR_565\n");
Ethan Yonkerb7a54a32015-10-05 10:16:27 -0500231 vi.blue.offset = 11;
232 vi.green.offset = 5;
233 vi.red.offset = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400234#endif
Ethan Yonkerb7a54a32015-10-05 10:16:27 -0500235 if (PIXEL_SIZE != 2) fprintf(stderr, "E: Pixel Size mismatch!\n");
236 vi.blue.length = 5;
237 vi.green.length = 6;
238 vi.red.length = 5;
Dees_Troy51a0e822012-09-05 15:24:24 -0400239 vi.blue.msb_right = 0;
240 vi.green.msb_right = 0;
241 vi.red.msb_right = 0;
242 vi.transp.offset = 0;
243 vi.transp.length = 0;
244 }
245 else
246 {
247 perror("unknown pixel format");
248 close(fd);
249 return -1;
250 }
251
252 vi.vmode = FB_VMODE_NONINTERLACED;
253 vi.activate = FB_ACTIVATE_NOW | FB_ACTIVATE_FORCE;
254
255 if (ioctl(fd, FBIOPUT_VSCREENINFO, &vi) < 0) {
256 perror("failed to put fb0 info");
257 close(fd);
258 return -1;
259 }
260
261 if (ioctl(fd, FBIOGET_FSCREENINFO, &fi) < 0) {
262 perror("failed to get fb0 info");
263 close(fd);
264 return -1;
265 }
266
Dees Troyb0425382014-04-03 00:10:03 +0000267#ifdef MSM_BSP
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600268 has_overlay = target_has_overlay(fi.id);
269
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600270 if (isTargetMdp5())
271 setDisplaySplit();
Dees Troyb0425382014-04-03 00:10:03 +0000272#else
273 has_overlay = false;
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600274#endif
275
276 if (!has_overlay) {
277 printf("Not using qualcomm overlay, '%s'\n", fi.id);
278 bits = mmap(0, fi.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
279 if (bits == MAP_FAILED) {
280 perror("failed to mmap framebuffer");
281 close(fd);
282 return -1;
283 }
284 } else {
285 printf("Using qualcomm overlay\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400286 }
287
288#ifdef RECOVERY_GRAPHICS_USE_LINELENGTH
289 vi.xres_virtual = fi.line_length / PIXEL_SIZE;
290#endif
291
292 fb->version = sizeof(*fb);
293 fb->width = vi.xres;
294 fb->height = vi.yres;
295#ifdef BOARD_HAS_JANKY_BACKBUFFER
Dees_Troy2673cec2013-04-02 20:22:16 +0000296 printf("setting JANKY BACKBUFFER\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400297 fb->stride = fi.line_length/2;
298#else
299 fb->stride = vi.xres_virtual;
300#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400301 fb->format = PIXEL_FORMAT;
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600302 if (!has_overlay) {
303 fb->data = bits;
304 memset(fb->data, 0, vi.yres * fb->stride * PIXEL_SIZE);
305 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400306
307 fb++;
308
Kra1o5c9556cc2015-06-24 12:19:09 +0200309#ifndef TW_DISABLE_DOUBLE_BUFFERING
Hiemanshu Sharma94f6c882012-11-21 11:25:22 -0600310 /* check if we can use double buffering */
311 if (vi.yres * fi.line_length * 2 > fi.smem_len)
Kra1o5c9556cc2015-06-24 12:19:09 +0200312#else
313 printf("TW_DISABLE_DOUBLE_BUFFERING := true\n");
314#endif
Hiemanshu Sharma94f6c882012-11-21 11:25:22 -0600315 return fd;
316
317 double_buffering = 1;
318
Dees_Troy51a0e822012-09-05 15:24:24 -0400319 fb->version = sizeof(*fb);
320 fb->width = vi.xres;
321 fb->height = vi.yres;
322#ifdef BOARD_HAS_JANKY_BACKBUFFER
323 fb->stride = fi.line_length/2;
Ethan Yonkerbcc502c2014-11-10 11:22:10 -0600324 fb->data = (GGLubyte*) (((unsigned long) bits) + vi.yres * fi.line_length);
Dees_Troy51a0e822012-09-05 15:24:24 -0400325#else
326 fb->stride = vi.xres_virtual;
Ethan Yonkerbcc502c2014-11-10 11:22:10 -0600327 fb->data = (GGLubyte*) (((unsigned long) bits) + vi.yres * fb->stride * PIXEL_SIZE);
Dees_Troy51a0e822012-09-05 15:24:24 -0400328#endif
329 fb->format = PIXEL_FORMAT;
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600330 if (!has_overlay) {
331 memset(fb->data, 0, vi.yres * fb->stride * PIXEL_SIZE);
332 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400333
334#ifdef PRINT_SCREENINFO
Ethan Yonkerb7a54a32015-10-05 10:16:27 -0500335 print_fb_var_screeninfo();
Dees_Troy51a0e822012-09-05 15:24:24 -0400336#endif
337
338 return fd;
339}
340
341static void get_memory_surface(GGLSurface* ms) {
342 ms->version = sizeof(*ms);
343 ms->width = vi.xres;
344 ms->height = vi.yres;
345 ms->stride = vi.xres_virtual;
346 ms->data = malloc(vi.xres_virtual * vi.yres * PIXEL_SIZE);
347 ms->format = PIXEL_FORMAT;
348}
349
350static void set_active_framebuffer(unsigned n)
351{
Hiemanshu Sharma94f6c882012-11-21 11:25:22 -0600352 if (n > 1 || !double_buffering) return;
Hiemanshu Sharmaacf6a9b2012-11-21 11:28:36 -0600353 vi.yres_virtual = vi.yres * NUM_BUFFERS;
Dees_Troy51a0e822012-09-05 15:24:24 -0400354 vi.yoffset = n * vi.yres;
355// vi.bits_per_pixel = PIXEL_SIZE * 8;
356 if (ioctl(gr_fb_fd, FBIOPUT_VSCREENINFO, &vi) < 0) {
357 perror("active fb swap failed");
358 }
359}
360
361void gr_flip(void)
362{
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600363 if (-EINVAL == overlay_display_frame(gr_fb_fd, gr_mem_surface.data,
364 (fi.line_length * vi.yres))) {
365 GGLContext *gl = gr_context;
Dees_Troy51a0e822012-09-05 15:24:24 -0400366
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600367 /* swap front and back buffers */
368 if (double_buffering)
369 gr_active_fb = (gr_active_fb + 1) & 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400370
371#ifdef BOARD_HAS_FLIPPED_SCREEN
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600372 /* flip buffer 180 degrees for devices with physicaly inverted screens */
373 unsigned int i;
374 unsigned int j;
Bogdan Seniucd1372f72015-01-23 08:22:39 -0800375 for (i = 0; i < vi.yres; i++) {
376 for (j = 0; j < vi.xres; j++) {
377 memcpy(gr_framebuffer[gr_active_fb].data + (i * vi.xres_virtual + j) * PIXEL_SIZE,
378 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 -0600379 }
380 }
Bogdan Seniucd1372f72015-01-23 08:22:39 -0800381#else
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600382 /* copy data from the in-memory surface to the buffer we're about
383 * to make active. */
384 memcpy(gr_framebuffer[gr_active_fb].data, gr_mem_surface.data,
385 vi.xres_virtual * vi.yres * PIXEL_SIZE);
Bogdan Seniucd1372f72015-01-23 08:22:39 -0800386#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400387
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600388 /* inform the display driver */
389 set_active_framebuffer(gr_active_fb);
390 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400391}
392
393void gr_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
394{
395 GGLContext *gl = gr_context;
396 GGLint color[4];
397 color[0] = ((r << 8) | r) + 1;
398 color[1] = ((g << 8) | g) + 1;
399 color[2] = ((b << 8) | b) + 1;
400 color[3] = ((a << 8) | a) + 1;
401 gl->color4xv(gl, color);
Vojtech Bocek1a4744a2014-02-06 19:52:28 +0100402
403 gr_is_curr_clr_opaque = (a == 255);
Dees_Troy51a0e822012-09-05 15:24:24 -0400404}
405
406int gr_measureEx(const char *s, void* font)
407{
408 GRFont* fnt = (GRFont*) font;
Dees_Troy51a0e822012-09-05 15:24:24 -0400409
Ethan Yonker88037f42015-10-04 22:09:08 -0500410 if (!fnt)
411 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400412
Ethan Yonker88037f42015-10-04 22:09:08 -0500413 return gr_ttf_measureEx(s, font);
Dees_Troy51a0e822012-09-05 15:24:24 -0400414}
415
Dees Troy31218ec2014-02-25 20:35:56 +0000416int gr_maxExW(const char *s, void* font, int max_width)
417{
418 GRFont* fnt = (GRFont*) font;
Dees Troy31218ec2014-02-25 20:35:56 +0000419
Ethan Yonker88037f42015-10-04 22:09:08 -0500420 if (!fnt)
421 return 0;
Dees Troy31218ec2014-02-25 20:35:56 +0000422
Ethan Yonker88037f42015-10-04 22:09:08 -0500423 return gr_ttf_maxExW(s, font, max_width);
Dees Troy31218ec2014-02-25 20:35:56 +0000424}
425
Dees_Troy51a0e822012-09-05 15:24:24 -0400426int gr_textEx(int x, int y, const char *s, void* pFont)
427{
428 GGLContext *gl = gr_context;
429 GRFont *font = (GRFont*) pFont;
Dees_Troy51a0e822012-09-05 15:24:24 -0400430
Ethan Yonker88037f42015-10-04 22:09:08 -0500431 if (!font)
432 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400433
Ethan Yonker88037f42015-10-04 22:09:08 -0500434 return gr_ttf_textExWH(gl, x, y, s, pFont, -1, -1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400435}
436
Ethan Yonkerb7a54a32015-10-05 10:16:27 -0500437int gr_textEx_scaleW(int x, int y, const char *s, void* pFont, int max_width, int placement, int scale)
438{
439 GGLContext *gl = gr_context;
440 void* vfont = pFont;
441 GRFont *font = (GRFont*) pFont;
442 unsigned off;
443 unsigned cwidth;
444 int y_scale = 0, measured_width, measured_height, ret, new_height;
445
446 if (!s || strlen(s) == 0 || !font)
447 return 0;
448
449 measured_height = gr_ttf_getMaxFontHeight(font);
450
451 if (scale) {
452 measured_width = gr_ttf_measureEx(s, vfont);
453 if (measured_width > max_width) {
454 // Adjust font size down until the text fits
455 void *new_font = gr_ttf_scaleFont(vfont, max_width, measured_width);
456 if (!new_font) {
457 printf("gr_textEx_scaleW new_font is NULL\n");
458 return 0;
459 }
460 measured_width = gr_ttf_measureEx(s, new_font);
461 // These next 2 lines adjust the y point based on the new font's height
462 new_height = gr_ttf_getMaxFontHeight(new_font);
463 y_scale = (measured_height - new_height) / 2;
464 vfont = new_font;
465 }
466 } else
467 measured_width = gr_ttf_measureEx(s, vfont);
468
469 int x_adj = measured_width;
470 if (measured_width > max_width)
471 x_adj = max_width;
472
473 if (placement != TOP_LEFT && placement != BOTTOM_LEFT && placement != TEXT_ONLY_RIGHT) {
474 if (placement == CENTER || placement == CENTER_X_ONLY)
475 x -= (x_adj / 2);
476 else
477 x -= x_adj;
478 }
479
480 if (placement != TOP_LEFT && placement != TOP_RIGHT) {
481 if (placement == CENTER || placement == TEXT_ONLY_RIGHT)
482 y -= (measured_height / 2);
483 else if (placement == BOTTOM_LEFT || placement == BOTTOM_RIGHT)
484 y -= measured_height;
485 }
486 return gr_ttf_textExWH(gl, x, y + y_scale, s, vfont, measured_width + x, -1);
487}
488
Dees_Troy51a0e822012-09-05 15:24:24 -0400489int gr_textExW(int x, int y, const char *s, void* pFont, int max_width)
490{
491 GGLContext *gl = gr_context;
492 GRFont *font = (GRFont*) pFont;
Dees_Troy51a0e822012-09-05 15:24:24 -0400493
Ethan Yonker88037f42015-10-04 22:09:08 -0500494 if (!font)
495 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400496
Ethan Yonker88037f42015-10-04 22:09:08 -0500497 return gr_ttf_textExWH(gl, x, y, s, pFont, max_width, -1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400498}
499
500int gr_textExWH(int x, int y, const char *s, void* pFont, int max_width, int max_height)
501{
502 GGLContext *gl = gr_context;
503 GRFont *font = (GRFont*) pFont;
Dees_Troy51a0e822012-09-05 15:24:24 -0400504
Ethan Yonker88037f42015-10-04 22:09:08 -0500505 if (!font)
506 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400507
Ethan Yonker88037f42015-10-04 22:09:08 -0500508 return gr_ttf_textExWH(gl, x, y, s, pFont, max_width, max_height);
Dees_Troy51a0e822012-09-05 15:24:24 -0400509}
510
that9876ac32015-02-15 21:40:59 +0100511void gr_clip(int x, int y, int w, int h)
512{
513 GGLContext *gl = gr_context;
514 gl->scissor(gl, x, y, w, h);
515 gl->enable(gl, GGL_SCISSOR_TEST);
516}
517
518void gr_noclip()
519{
520 GGLContext *gl = gr_context;
521 gl->scissor(gl, 0, 0, gr_fb_width(), gr_fb_height());
522 gl->disable(gl, GGL_SCISSOR_TEST);
523}
524
Dees_Troy51a0e822012-09-05 15:24:24 -0400525void gr_fill(int x, int y, int w, int h)
526{
527 GGLContext *gl = gr_context;
Vojtech Bocek1a4744a2014-02-06 19:52:28 +0100528
529 if(gr_is_curr_clr_opaque)
530 gl->disable(gl, GGL_BLEND);
531
Dees_Troy51a0e822012-09-05 15:24:24 -0400532 gl->recti(gl, x, y, x + w, y + h);
Vojtech Bocek1a4744a2014-02-06 19:52:28 +0100533
534 if(gr_is_curr_clr_opaque)
535 gl->enable(gl, GGL_BLEND);
Dees_Troy51a0e822012-09-05 15:24:24 -0400536}
537
Vojtech Bocekdb378b62015-03-05 20:02:57 +0100538void gr_line(int x0, int y0, int x1, int y1, int width)
539{
540 GGLContext *gl = gr_context;
541
542 if(gr_is_curr_clr_opaque)
543 gl->disable(gl, GGL_BLEND);
544
545 const int coords0[2] = { x0 << 4, y0 << 4 };
546 const int coords1[2] = { x1 << 4, y1 << 4 };
547 gl->linex(gl, coords0, coords1, width << 4);
548
549 if(gr_is_curr_clr_opaque)
550 gl->enable(gl, GGL_BLEND);
551}
552
553gr_surface gr_render_circle(int radius, unsigned char r, unsigned char g, unsigned char b, unsigned char a)
554{
555 int rx, ry;
556 GGLSurface *surface;
557 const int diameter = radius*2 + 1;
558 const int radius_check = radius*radius + radius*0.8;
559 const uint32_t px = (a << 24) | (b << 16) | (g << 8) | r;
560 uint32_t *data;
561
562 surface = malloc(sizeof(GGLSurface));
563 memset(surface, 0, sizeof(GGLSurface));
564
565 data = malloc(diameter * diameter * 4);
566 memset(data, 0, diameter * diameter * 4);
567
568 surface->version = sizeof(surface);
569 surface->width = diameter;
570 surface->height = diameter;
571 surface->stride = diameter;
572 surface->data = (GGLubyte*)data;
573 surface->format = GGL_PIXEL_FORMAT_RGBA_8888;
574
575 for(ry = -radius; ry <= radius; ++ry)
576 for(rx = -radius; rx <= radius; ++rx)
577 if(rx*rx+ry*ry <= radius_check)
578 *(data + diameter*(radius + ry) + (radius+rx)) = px;
579
580 return surface;
581}
582
Dees_Troy51a0e822012-09-05 15:24:24 -0400583void gr_blit(gr_surface source, int sx, int sy, int w, int h, int dx, int dy) {
Dees Troyf171e102014-11-06 22:19:58 +0100584 if (gr_context == NULL) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400585 return;
586 }
587
588 GGLContext *gl = gr_context;
Vojtech Bocek6c694b62014-02-06 20:36:25 +0100589 GGLSurface *surface = (GGLSurface*)source;
590
591 if(surface->format == GGL_PIXEL_FORMAT_RGBX_8888)
592 gl->disable(gl, GGL_BLEND);
593
594 gl->bindTexture(gl, surface);
Dees_Troy51a0e822012-09-05 15:24:24 -0400595 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
596 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
597 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
598 gl->enable(gl, GGL_TEXTURE_2D);
599 gl->texCoord2i(gl, sx - dx, sy - dy);
600 gl->recti(gl, dx, dy, dx + w, dy + h);
Vojtech Bocek3041c882015-03-06 00:28:21 +0100601 gl->disable(gl, GGL_TEXTURE_2D);
Vojtech Bocek6c694b62014-02-06 20:36:25 +0100602
603 if(surface->format == GGL_PIXEL_FORMAT_RGBX_8888)
Dees Troyf171e102014-11-06 22:19:58 +0100604 gl->enable(gl, GGL_BLEND);
Dees_Troy51a0e822012-09-05 15:24:24 -0400605}
606
607unsigned int gr_get_width(gr_surface surface) {
608 if (surface == NULL) {
609 return 0;
610 }
611 return ((GGLSurface*) surface)->width;
612}
613
614unsigned int gr_get_height(gr_surface surface) {
615 if (surface == NULL) {
616 return 0;
617 }
618 return ((GGLSurface*) surface)->height;
619}
620
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200621int gr_getMaxFontHeight(void *font)
Dees_Troy51a0e822012-09-05 15:24:24 -0400622{
623 GRFont *fnt = (GRFont*) font;
624
Ethan Yonker88037f42015-10-04 22:09:08 -0500625 if (!fnt)
626 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400627
Ethan Yonker88037f42015-10-04 22:09:08 -0500628 return gr_ttf_getMaxFontHeight(font);
Dees_Troy51a0e822012-09-05 15:24:24 -0400629}
630
631int gr_init(void)
632{
633 gglInit(&gr_context);
634 GGLContext *gl = gr_context;
635
Dees_Troy51a0e822012-09-05 15:24:24 -0400636 gr_vt_fd = open("/dev/tty0", O_RDWR | O_SYNC);
637 if (gr_vt_fd < 0) {
638 // This is non-fatal; post-Cupcake kernels don't have tty0.
639 } else if (ioctl(gr_vt_fd, KDSETMODE, (void*) KD_GRAPHICS)) {
640 // However, if we do open tty0, we expect the ioctl to work.
641 perror("failed KDSETMODE to KD_GRAPHICS on tty0");
642 gr_exit();
643 return -1;
644 }
645
646 gr_fb_fd = get_framebuffer(gr_framebuffer);
647 if (gr_fb_fd < 0) {
648 perror("Unable to get framebuffer.\n");
649 gr_exit();
650 return -1;
651 }
652
653 get_memory_surface(&gr_mem_surface);
654
655 fprintf(stderr, "framebuffer: fd %d (%d x %d)\n",
656 gr_fb_fd, gr_framebuffer[0].width, gr_framebuffer[0].height);
657
658 /* start with 0 as front (displayed) and 1 as back (drawing) */
659 gr_active_fb = 0;
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600660 if (!has_overlay)
661 set_active_framebuffer(0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400662 gl->colorBuffer(gl, &gr_mem_surface);
663
664 gl->activeTexture(gl, 0);
665 gl->enable(gl, GGL_BLEND);
666 gl->blendFunc(gl, GGL_SRC_ALPHA, GGL_ONE_MINUS_SRC_ALPHA);
667
xNUTxcd56f8c2014-07-23 01:30:20 +0200668#ifdef TW_SCREEN_BLANK_ON_BOOT
669 printf("TW_SCREEN_BLANK_ON_BOOT := true\n");
670 gr_fb_blank(true);
671 gr_fb_blank(false);
672#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400673
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600674 if (!alloc_ion_mem(fi.line_length * vi.yres))
675 allocate_overlay(gr_fb_fd, gr_framebuffer);
676
Dees_Troy51a0e822012-09-05 15:24:24 -0400677 return 0;
678}
679
680void gr_exit(void)
681{
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600682 free_overlay(gr_fb_fd);
683 free_ion_mem();
684
Dees_Troy51a0e822012-09-05 15:24:24 -0400685 close(gr_fb_fd);
686 gr_fb_fd = -1;
687
688 free(gr_mem_surface.data);
689
690 ioctl(gr_vt_fd, KDSETMODE, (void*) KD_TEXT);
691 close(gr_vt_fd);
692 gr_vt_fd = -1;
693}
694
695int gr_fb_width(void)
696{
697 return gr_framebuffer[0].width;
698}
699
700int gr_fb_height(void)
701{
702 return gr_framebuffer[0].height;
703}
704
705gr_pixel *gr_fb_data(void)
706{
707 return (unsigned short *) gr_mem_surface.data;
708}
709
Dees_Troy70237dc2013-02-28 21:31:48 +0000710int gr_fb_blank(int blank)
Dees_Troy51a0e822012-09-05 15:24:24 -0400711{
712 int ret;
Dees Troy05d18c92014-08-04 18:17:07 +0000713 //if (blank)
714 //free_overlay(gr_fb_fd);
Dees_Troy51a0e822012-09-05 15:24:24 -0400715
716 ret = ioctl(gr_fb_fd, FBIOBLANK, blank ? FB_BLANK_POWERDOWN : FB_BLANK_UNBLANK);
717 if (ret < 0)
718 perror("ioctl(): blank");
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600719
Dees Troy05d18c92014-08-04 18:17:07 +0000720 //if (!blank)
721 //allocate_overlay(gr_fb_fd, gr_framebuffer);
722 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -0400723}
724
725int gr_get_surface(gr_surface* surface)
726{
727 GGLSurface* ms = malloc(sizeof(GGLSurface));
728 if (!ms) return -1;
729
730 // Allocate the data
731 get_memory_surface(ms);
732
733 // Now, copy the data
734 memcpy(ms->data, gr_mem_surface.data, vi.xres * vi.yres * vi.bits_per_pixel / 8);
735
736 *surface = (gr_surface*) ms;
737 return 0;
738}
739
740int gr_free_surface(gr_surface surface)
741{
742 if (!surface)
743 return -1;
744
745 GGLSurface* ms = (GGLSurface*) surface;
746 free(ms->data);
747 free(ms);
748 return 0;
749}
750
751void gr_write_frame_to_file(int fd)
752{
753 write(fd, gr_mem_surface.data, vi.xres * vi.yres * vi.bits_per_pixel / 8);
754}