Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1 | /* |
| 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 Yonker | 4ee5ad7 | 2014-02-18 18:41:17 -0600 | [diff] [blame] | 21 | #include <errno.h> |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 22 | #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 Sharma | acf6a9b | 2012-11-21 11:28:36 -0600 | [diff] [blame] | 55 | #define NUM_BUFFERS 2 |
Ethan Yonker | 4ee5ad7 | 2014-02-18 18:41:17 -0600 | [diff] [blame] | 56 | #define MAX_DISPLAY_DIM 2048 |
Hiemanshu Sharma | acf6a9b | 2012-11-21 11:28:36 -0600 | [diff] [blame] | 57 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 58 | // #define PRINT_SCREENINFO 1 // Enables printing of screen info to log |
| 59 | |
| 60 | typedef struct { |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 61 | int type; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 62 | GGLSurface texture; |
| 63 | unsigned offset[97]; |
| 64 | unsigned cheight; |
| 65 | unsigned ascent; |
| 66 | } GRFont; |
| 67 | |
| 68 | static GRFont *gr_font = 0; |
| 69 | static GGLContext *gr_context = 0; |
| 70 | static GGLSurface gr_font_texture; |
Hiemanshu Sharma | acf6a9b | 2012-11-21 11:28:36 -0600 | [diff] [blame] | 71 | static GGLSurface gr_framebuffer[NUM_BUFFERS]; |
Vojtech Bocek | 03fd6c5 | 2014-03-13 18:46:34 +0100 | [diff] [blame] | 72 | GGLSurface gr_mem_surface; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 73 | static unsigned gr_active_fb = 0; |
Hiemanshu Sharma | 94f6c88 | 2012-11-21 11:25:22 -0600 | [diff] [blame] | 74 | static unsigned double_buffering = 0; |
Vojtech Bocek | 1a4744a | 2014-02-06 19:52:28 +0100 | [diff] [blame] | 75 | static int gr_is_curr_clr_opaque = 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 76 | |
| 77 | static int gr_fb_fd = -1; |
| 78 | static int gr_vt_fd = -1; |
| 79 | |
Vojtech Bocek | 03fd6c5 | 2014-03-13 18:46:34 +0100 | [diff] [blame] | 80 | struct fb_var_screeninfo vi; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 81 | static struct fb_fix_screeninfo fi; |
| 82 | |
Ethan Yonker | 4ee5ad7 | 2014-02-18 18:41:17 -0600 | [diff] [blame] | 83 | static bool has_overlay = false; |
| 84 | static int leftSplit = 0; |
| 85 | static int rightSplit = 0; |
| 86 | |
| 87 | bool target_has_overlay(char *version); |
| 88 | int free_ion_mem(void); |
| 89 | int alloc_ion_mem(unsigned int size); |
| 90 | int allocate_overlay(int fd, GGLSurface gr_fb[]); |
| 91 | int free_overlay(int fd); |
| 92 | int overlay_display_frame(int fd, GGLubyte* data, size_t size); |
| 93 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 94 | #ifdef PRINT_SCREENINFO |
| 95 | static void print_fb_var_screeninfo() |
| 96 | { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 97 | 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_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 105 | } |
| 106 | #endif |
| 107 | |
Ethan Yonker | 4ee5ad7 | 2014-02-18 18:41:17 -0600 | [diff] [blame] | 108 | #ifdef MSM_BSP |
| 109 | int 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 | |
| 120 | int getRightSplit(void) { |
| 121 | return rightSplit; |
| 122 | } |
| 123 | |
| 124 | |
| 125 | void 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 | |
| 145 | bool 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 | |
| 155 | int getFbXres(void) { |
| 156 | return vi.xres; |
| 157 | } |
| 158 | |
| 159 | int getFbYres (void) { |
| 160 | return vi.yres; |
| 161 | } |
| 162 | #endif // MSM_BSP |
| 163 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 164 | static int get_framebuffer(GGLSurface *fb) |
| 165 | { |
Ethan Yonker | 4f6a976 | 2015-03-09 13:58:54 -0500 | [diff] [blame] | 166 | int fd, index = 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 167 | void *bits; |
| 168 | |
| 169 | fd = open("/dev/graphics/fb0", O_RDWR); |
jenkins | 7969913 | 2015-05-08 06:04:44 -0400 | [diff] [blame] | 170 | |
| 171 | while (fd < 0 && index < 30) { |
Ethan Yonker | 4f6a976 | 2015-03-09 13:58:54 -0500 | [diff] [blame] | 172 | usleep(1000); |
| 173 | fd = open("/dev/graphics/fb0", O_RDWR); |
| 174 | index++; |
| 175 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 176 | if (fd < 0) { |
Ethan Yonker | 4f6a976 | 2015-03-09 13:58:54 -0500 | [diff] [blame] | 177 | perror("cannot open fb0\n"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 178 | return -1; |
| 179 | } |
| 180 | |
| 181 | if (ioctl(fd, FBIOGET_VSCREENINFO, &vi) < 0) { |
| 182 | perror("failed to get fb0 info"); |
| 183 | close(fd); |
| 184 | return -1; |
| 185 | } |
| 186 | |
| 187 | fprintf(stderr, "Pixel format: %dx%d @ %dbpp\n", vi.xres, vi.yres, vi.bits_per_pixel); |
| 188 | |
| 189 | vi.bits_per_pixel = PIXEL_SIZE * 8; |
| 190 | if (PIXEL_FORMAT == GGL_PIXEL_FORMAT_BGRA_8888) { |
| 191 | fprintf(stderr, "Pixel format: BGRA_8888\n"); |
| 192 | if (PIXEL_SIZE != 4) fprintf(stderr, "E: Pixel Size mismatch!\n"); |
| 193 | vi.red.offset = 8; |
| 194 | vi.red.length = 8; |
| 195 | vi.green.offset = 16; |
| 196 | vi.green.length = 8; |
| 197 | vi.blue.offset = 24; |
| 198 | vi.blue.length = 8; |
| 199 | vi.transp.offset = 0; |
| 200 | vi.transp.length = 8; |
| 201 | } else if (PIXEL_FORMAT == GGL_PIXEL_FORMAT_RGBX_8888) { |
| 202 | fprintf(stderr, "Pixel format: RGBX_8888\n"); |
| 203 | if (PIXEL_SIZE != 4) fprintf(stderr, "E: Pixel Size mismatch!\n"); |
| 204 | vi.red.offset = 24; |
| 205 | vi.red.length = 8; |
| 206 | vi.green.offset = 16; |
| 207 | vi.green.length = 8; |
| 208 | vi.blue.offset = 8; |
| 209 | vi.blue.length = 8; |
| 210 | vi.transp.offset = 0; |
| 211 | vi.transp.length = 8; |
| 212 | } else if (PIXEL_FORMAT == GGL_PIXEL_FORMAT_RGB_565) { |
| 213 | #ifdef RECOVERY_RGB_565 |
| 214 | fprintf(stderr, "Pixel format: RGB_565\n"); |
| 215 | vi.blue.offset = 0; |
| 216 | vi.green.offset = 5; |
| 217 | vi.red.offset = 11; |
| 218 | #else |
| 219 | fprintf(stderr, "Pixel format: BGR_565\n"); |
| 220 | vi.blue.offset = 11; |
| 221 | vi.green.offset = 5; |
| 222 | vi.red.offset = 0; |
| 223 | #endif |
| 224 | if (PIXEL_SIZE != 2) fprintf(stderr, "E: Pixel Size mismatch!\n"); |
| 225 | vi.blue.length = 5; |
| 226 | vi.green.length = 6; |
| 227 | vi.red.length = 5; |
| 228 | vi.blue.msb_right = 0; |
| 229 | vi.green.msb_right = 0; |
| 230 | vi.red.msb_right = 0; |
| 231 | vi.transp.offset = 0; |
| 232 | vi.transp.length = 0; |
| 233 | } |
| 234 | else |
| 235 | { |
| 236 | perror("unknown pixel format"); |
| 237 | close(fd); |
| 238 | return -1; |
| 239 | } |
| 240 | |
| 241 | vi.vmode = FB_VMODE_NONINTERLACED; |
| 242 | vi.activate = FB_ACTIVATE_NOW | FB_ACTIVATE_FORCE; |
| 243 | |
| 244 | if (ioctl(fd, FBIOPUT_VSCREENINFO, &vi) < 0) { |
| 245 | perror("failed to put fb0 info"); |
| 246 | close(fd); |
| 247 | return -1; |
| 248 | } |
| 249 | |
| 250 | if (ioctl(fd, FBIOGET_FSCREENINFO, &fi) < 0) { |
| 251 | perror("failed to get fb0 info"); |
| 252 | close(fd); |
| 253 | return -1; |
| 254 | } |
| 255 | |
Dees Troy | b042538 | 2014-04-03 00:10:03 +0000 | [diff] [blame] | 256 | #ifdef MSM_BSP |
Ethan Yonker | 4ee5ad7 | 2014-02-18 18:41:17 -0600 | [diff] [blame] | 257 | has_overlay = target_has_overlay(fi.id); |
| 258 | |
Ethan Yonker | 4ee5ad7 | 2014-02-18 18:41:17 -0600 | [diff] [blame] | 259 | if (isTargetMdp5()) |
| 260 | setDisplaySplit(); |
Dees Troy | b042538 | 2014-04-03 00:10:03 +0000 | [diff] [blame] | 261 | #else |
| 262 | has_overlay = false; |
Ethan Yonker | 4ee5ad7 | 2014-02-18 18:41:17 -0600 | [diff] [blame] | 263 | #endif |
| 264 | |
| 265 | if (!has_overlay) { |
| 266 | printf("Not using qualcomm overlay, '%s'\n", fi.id); |
| 267 | bits = mmap(0, fi.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); |
| 268 | if (bits == MAP_FAILED) { |
| 269 | perror("failed to mmap framebuffer"); |
| 270 | close(fd); |
| 271 | return -1; |
| 272 | } |
| 273 | } else { |
| 274 | printf("Using qualcomm overlay\n"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | #ifdef RECOVERY_GRAPHICS_USE_LINELENGTH |
| 278 | vi.xres_virtual = fi.line_length / PIXEL_SIZE; |
| 279 | #endif |
| 280 | |
| 281 | fb->version = sizeof(*fb); |
| 282 | fb->width = vi.xres; |
| 283 | fb->height = vi.yres; |
| 284 | #ifdef BOARD_HAS_JANKY_BACKBUFFER |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 285 | printf("setting JANKY BACKBUFFER\n"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 286 | fb->stride = fi.line_length/2; |
| 287 | #else |
| 288 | fb->stride = vi.xres_virtual; |
| 289 | #endif |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 290 | fb->format = PIXEL_FORMAT; |
Ethan Yonker | 4ee5ad7 | 2014-02-18 18:41:17 -0600 | [diff] [blame] | 291 | if (!has_overlay) { |
| 292 | fb->data = bits; |
| 293 | memset(fb->data, 0, vi.yres * fb->stride * PIXEL_SIZE); |
| 294 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 295 | |
| 296 | fb++; |
| 297 | |
Hiemanshu Sharma | 94f6c88 | 2012-11-21 11:25:22 -0600 | [diff] [blame] | 298 | /* check if we can use double buffering */ |
| 299 | if (vi.yres * fi.line_length * 2 > fi.smem_len) |
| 300 | return fd; |
| 301 | |
| 302 | double_buffering = 1; |
| 303 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 304 | fb->version = sizeof(*fb); |
| 305 | fb->width = vi.xres; |
| 306 | fb->height = vi.yres; |
| 307 | #ifdef BOARD_HAS_JANKY_BACKBUFFER |
| 308 | fb->stride = fi.line_length/2; |
Ethan Yonker | bcc502c | 2014-11-10 11:22:10 -0600 | [diff] [blame] | 309 | fb->data = (GGLubyte*) (((unsigned long) bits) + vi.yres * fi.line_length); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 310 | #else |
| 311 | fb->stride = vi.xres_virtual; |
Ethan Yonker | bcc502c | 2014-11-10 11:22:10 -0600 | [diff] [blame] | 312 | fb->data = (GGLubyte*) (((unsigned long) bits) + vi.yres * fb->stride * PIXEL_SIZE); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 313 | #endif |
| 314 | fb->format = PIXEL_FORMAT; |
Ethan Yonker | 4ee5ad7 | 2014-02-18 18:41:17 -0600 | [diff] [blame] | 315 | if (!has_overlay) { |
| 316 | memset(fb->data, 0, vi.yres * fb->stride * PIXEL_SIZE); |
| 317 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 318 | |
| 319 | #ifdef PRINT_SCREENINFO |
| 320 | print_fb_var_screeninfo(); |
| 321 | #endif |
| 322 | |
| 323 | return fd; |
| 324 | } |
| 325 | |
| 326 | static void get_memory_surface(GGLSurface* ms) { |
| 327 | ms->version = sizeof(*ms); |
| 328 | ms->width = vi.xres; |
| 329 | ms->height = vi.yres; |
| 330 | ms->stride = vi.xres_virtual; |
| 331 | ms->data = malloc(vi.xres_virtual * vi.yres * PIXEL_SIZE); |
| 332 | ms->format = PIXEL_FORMAT; |
| 333 | } |
| 334 | |
| 335 | static void set_active_framebuffer(unsigned n) |
| 336 | { |
Hiemanshu Sharma | 94f6c88 | 2012-11-21 11:25:22 -0600 | [diff] [blame] | 337 | if (n > 1 || !double_buffering) return; |
Hiemanshu Sharma | acf6a9b | 2012-11-21 11:28:36 -0600 | [diff] [blame] | 338 | vi.yres_virtual = vi.yres * NUM_BUFFERS; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 339 | vi.yoffset = n * vi.yres; |
| 340 | // vi.bits_per_pixel = PIXEL_SIZE * 8; |
| 341 | if (ioctl(gr_fb_fd, FBIOPUT_VSCREENINFO, &vi) < 0) { |
| 342 | perror("active fb swap failed"); |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | void gr_flip(void) |
| 347 | { |
Ethan Yonker | 4ee5ad7 | 2014-02-18 18:41:17 -0600 | [diff] [blame] | 348 | if (-EINVAL == overlay_display_frame(gr_fb_fd, gr_mem_surface.data, |
| 349 | (fi.line_length * vi.yres))) { |
| 350 | GGLContext *gl = gr_context; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 351 | |
Ethan Yonker | 4ee5ad7 | 2014-02-18 18:41:17 -0600 | [diff] [blame] | 352 | /* swap front and back buffers */ |
| 353 | if (double_buffering) |
| 354 | gr_active_fb = (gr_active_fb + 1) & 1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 355 | |
| 356 | #ifdef BOARD_HAS_FLIPPED_SCREEN |
Ethan Yonker | 4ee5ad7 | 2014-02-18 18:41:17 -0600 | [diff] [blame] | 357 | /* flip buffer 180 degrees for devices with physicaly inverted screens */ |
| 358 | unsigned int i; |
| 359 | unsigned int j; |
Bogdan Seniuc | d1372f7 | 2015-01-23 08:22:39 -0800 | [diff] [blame] | 360 | for (i = 0; i < vi.yres; i++) { |
| 361 | for (j = 0; j < vi.xres; j++) { |
| 362 | memcpy(gr_framebuffer[gr_active_fb].data + (i * vi.xres_virtual + j) * PIXEL_SIZE, |
| 363 | gr_mem_surface.data + ((vi.yres - i - 1) * vi.xres_virtual + vi.xres - j - 1) * PIXEL_SIZE, PIXEL_SIZE); |
Ethan Yonker | 4ee5ad7 | 2014-02-18 18:41:17 -0600 | [diff] [blame] | 364 | } |
| 365 | } |
Bogdan Seniuc | d1372f7 | 2015-01-23 08:22:39 -0800 | [diff] [blame] | 366 | #else |
Ethan Yonker | 4ee5ad7 | 2014-02-18 18:41:17 -0600 | [diff] [blame] | 367 | /* copy data from the in-memory surface to the buffer we're about |
| 368 | * to make active. */ |
| 369 | memcpy(gr_framebuffer[gr_active_fb].data, gr_mem_surface.data, |
| 370 | vi.xres_virtual * vi.yres * PIXEL_SIZE); |
Bogdan Seniuc | d1372f7 | 2015-01-23 08:22:39 -0800 | [diff] [blame] | 371 | #endif |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 372 | |
Ethan Yonker | 4ee5ad7 | 2014-02-18 18:41:17 -0600 | [diff] [blame] | 373 | /* inform the display driver */ |
| 374 | set_active_framebuffer(gr_active_fb); |
| 375 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | void gr_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a) |
| 379 | { |
| 380 | GGLContext *gl = gr_context; |
| 381 | GGLint color[4]; |
| 382 | color[0] = ((r << 8) | r) + 1; |
| 383 | color[1] = ((g << 8) | g) + 1; |
| 384 | color[2] = ((b << 8) | b) + 1; |
| 385 | color[3] = ((a << 8) | a) + 1; |
| 386 | gl->color4xv(gl, color); |
Vojtech Bocek | 1a4744a | 2014-02-06 19:52:28 +0100 | [diff] [blame] | 387 | |
| 388 | gr_is_curr_clr_opaque = (a == 255); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | int gr_measureEx(const char *s, void* font) |
| 392 | { |
| 393 | GRFont* fnt = (GRFont*) font; |
| 394 | int total = 0; |
| 395 | unsigned pos; |
| 396 | unsigned off; |
| 397 | |
| 398 | if (!fnt) fnt = gr_font; |
| 399 | |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 400 | #ifndef TW_DISABLE_TTF |
| 401 | if(fnt->type == FONT_TYPE_TTF) |
| 402 | return gr_ttf_measureEx(s, font); |
| 403 | #endif |
| 404 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 405 | while ((off = *s++)) |
| 406 | { |
| 407 | off -= 32; |
| 408 | if (off < 96) |
| 409 | total += (fnt->offset[off+1] - fnt->offset[off]); |
| 410 | } |
| 411 | return total; |
| 412 | } |
| 413 | |
Dees Troy | 31218ec | 2014-02-25 20:35:56 +0000 | [diff] [blame] | 414 | int gr_maxExW(const char *s, void* font, int max_width) |
| 415 | { |
| 416 | GRFont* fnt = (GRFont*) font; |
| 417 | int total = 0; |
| 418 | unsigned pos; |
| 419 | unsigned off; |
| 420 | |
| 421 | if (!fnt) fnt = gr_font; |
| 422 | |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 423 | #ifndef TW_DISABLE_TTF |
| 424 | if(fnt->type == FONT_TYPE_TTF) |
| 425 | return gr_ttf_maxExW(s, font, max_width); |
| 426 | #endif |
| 427 | |
Dees Troy | 31218ec | 2014-02-25 20:35:56 +0000 | [diff] [blame] | 428 | while ((off = *s++)) |
| 429 | { |
| 430 | off -= 32; |
| 431 | if (off < 96) { |
| 432 | max_width -= (fnt->offset[off+1] - fnt->offset[off]); |
| 433 | if (max_width > 0) { |
| 434 | total++; |
| 435 | } else { |
| 436 | return total; |
| 437 | } |
| 438 | } |
| 439 | } |
| 440 | return total; |
| 441 | } |
| 442 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 443 | int gr_textEx(int x, int y, const char *s, void* pFont) |
| 444 | { |
| 445 | GGLContext *gl = gr_context; |
| 446 | GRFont *font = (GRFont*) pFont; |
| 447 | unsigned off; |
| 448 | unsigned cwidth; |
| 449 | |
| 450 | /* Handle default font */ |
| 451 | if (!font) font = gr_font; |
| 452 | |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 453 | #ifndef TW_DISABLE_TTF |
| 454 | if(font->type == FONT_TYPE_TTF) |
| 455 | return gr_ttf_textExWH(gl, x, y, s, pFont, -1, -1); |
| 456 | #endif |
| 457 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 458 | gl->bindTexture(gl, &font->texture); |
| 459 | gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE); |
| 460 | gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE); |
| 461 | gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE); |
| 462 | gl->enable(gl, GGL_TEXTURE_2D); |
| 463 | |
| 464 | while((off = *s++)) { |
| 465 | off -= 32; |
| 466 | cwidth = 0; |
| 467 | if (off < 96) { |
| 468 | cwidth = font->offset[off+1] - font->offset[off]; |
| 469 | gl->texCoord2i(gl, (font->offset[off]) - x, 0 - y); |
| 470 | gl->recti(gl, x, y, x + cwidth, y + font->cheight); |
| 471 | x += cwidth; |
| 472 | } |
| 473 | } |
| 474 | |
Vojtech Bocek | 3041c88 | 2015-03-06 00:28:21 +0100 | [diff] [blame] | 475 | gl->disable(gl, GGL_TEXTURE_2D); |
| 476 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 477 | return x; |
| 478 | } |
| 479 | |
| 480 | int gr_textExW(int x, int y, const char *s, void* pFont, int max_width) |
| 481 | { |
| 482 | GGLContext *gl = gr_context; |
| 483 | GRFont *font = (GRFont*) pFont; |
| 484 | unsigned off; |
| 485 | unsigned cwidth; |
| 486 | |
| 487 | /* Handle default font */ |
| 488 | if (!font) font = gr_font; |
| 489 | |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 490 | #ifndef TW_DISABLE_TTF |
| 491 | if(font->type == FONT_TYPE_TTF) |
| 492 | return gr_ttf_textExWH(gl, x, y, s, pFont, max_width, -1); |
| 493 | #endif |
| 494 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 495 | gl->bindTexture(gl, &font->texture); |
| 496 | gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE); |
| 497 | gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE); |
| 498 | gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE); |
| 499 | gl->enable(gl, GGL_TEXTURE_2D); |
| 500 | |
| 501 | while((off = *s++)) { |
| 502 | off -= 32; |
| 503 | cwidth = 0; |
| 504 | if (off < 96) { |
| 505 | cwidth = font->offset[off+1] - font->offset[off]; |
| 506 | if ((x + (int)cwidth) < max_width) { |
| 507 | gl->texCoord2i(gl, (font->offset[off]) - x, 0 - y); |
| 508 | gl->recti(gl, x, y, x + cwidth, y + font->cheight); |
| 509 | x += cwidth; |
| 510 | } else { |
| 511 | gl->texCoord2i(gl, (font->offset[off]) - x, 0 - y); |
| 512 | gl->recti(gl, x, y, max_width, y + font->cheight); |
| 513 | x = max_width; |
| 514 | return x; |
| 515 | } |
| 516 | } |
| 517 | } |
| 518 | |
Vojtech Bocek | 3041c88 | 2015-03-06 00:28:21 +0100 | [diff] [blame] | 519 | gl->disable(gl, GGL_TEXTURE_2D); |
| 520 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 521 | return x; |
| 522 | } |
| 523 | |
| 524 | int gr_textExWH(int x, int y, const char *s, void* pFont, int max_width, int max_height) |
| 525 | { |
| 526 | GGLContext *gl = gr_context; |
| 527 | GRFont *font = (GRFont*) pFont; |
| 528 | unsigned off; |
| 529 | unsigned cwidth; |
| 530 | int rect_x, rect_y; |
| 531 | |
| 532 | /* Handle default font */ |
| 533 | if (!font) font = gr_font; |
| 534 | |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 535 | #ifndef TW_DISABLE_TTF |
| 536 | if(font->type == FONT_TYPE_TTF) |
| 537 | return gr_ttf_textExWH(gl, x, y, s, pFont, max_width, max_height); |
| 538 | #endif |
| 539 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 540 | gl->bindTexture(gl, &font->texture); |
| 541 | gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE); |
| 542 | gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE); |
| 543 | gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE); |
| 544 | gl->enable(gl, GGL_TEXTURE_2D); |
| 545 | |
| 546 | while((off = *s++)) { |
| 547 | off -= 32; |
| 548 | cwidth = 0; |
| 549 | if (off < 96) { |
| 550 | cwidth = font->offset[off+1] - font->offset[off]; |
| 551 | if ((x + (int)cwidth) < max_width) |
| 552 | rect_x = x + cwidth; |
| 553 | else |
| 554 | rect_x = max_width; |
Dees_Troy | f759675 | 2012-09-28 13:21:36 -0400 | [diff] [blame] | 555 | if (y + font->cheight < (unsigned int)(max_height)) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 556 | rect_y = y + font->cheight; |
| 557 | else |
| 558 | rect_y = max_height; |
| 559 | |
| 560 | gl->texCoord2i(gl, (font->offset[off]) - x, 0 - y); |
| 561 | gl->recti(gl, x, y, rect_x, rect_y); |
| 562 | x += cwidth; |
| 563 | if (x > max_width) |
| 564 | return x; |
| 565 | } |
| 566 | } |
| 567 | |
Vojtech Bocek | 3041c88 | 2015-03-06 00:28:21 +0100 | [diff] [blame] | 568 | gl->disable(gl, GGL_TEXTURE_2D); |
| 569 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 570 | return x; |
| 571 | } |
| 572 | |
that | 9876ac3 | 2015-02-15 21:40:59 +0100 | [diff] [blame] | 573 | void gr_clip(int x, int y, int w, int h) |
| 574 | { |
| 575 | GGLContext *gl = gr_context; |
| 576 | gl->scissor(gl, x, y, w, h); |
| 577 | gl->enable(gl, GGL_SCISSOR_TEST); |
| 578 | } |
| 579 | |
| 580 | void gr_noclip() |
| 581 | { |
| 582 | GGLContext *gl = gr_context; |
| 583 | gl->scissor(gl, 0, 0, gr_fb_width(), gr_fb_height()); |
| 584 | gl->disable(gl, GGL_SCISSOR_TEST); |
| 585 | } |
| 586 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 587 | void gr_fill(int x, int y, int w, int h) |
| 588 | { |
| 589 | GGLContext *gl = gr_context; |
Vojtech Bocek | 1a4744a | 2014-02-06 19:52:28 +0100 | [diff] [blame] | 590 | |
| 591 | if(gr_is_curr_clr_opaque) |
| 592 | gl->disable(gl, GGL_BLEND); |
| 593 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 594 | gl->recti(gl, x, y, x + w, y + h); |
Vojtech Bocek | 1a4744a | 2014-02-06 19:52:28 +0100 | [diff] [blame] | 595 | |
| 596 | if(gr_is_curr_clr_opaque) |
| 597 | gl->enable(gl, GGL_BLEND); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 598 | } |
| 599 | |
Vojtech Bocek | db378b6 | 2015-03-05 20:02:57 +0100 | [diff] [blame] | 600 | void gr_line(int x0, int y0, int x1, int y1, int width) |
| 601 | { |
| 602 | GGLContext *gl = gr_context; |
| 603 | |
| 604 | if(gr_is_curr_clr_opaque) |
| 605 | gl->disable(gl, GGL_BLEND); |
| 606 | |
| 607 | const int coords0[2] = { x0 << 4, y0 << 4 }; |
| 608 | const int coords1[2] = { x1 << 4, y1 << 4 }; |
| 609 | gl->linex(gl, coords0, coords1, width << 4); |
| 610 | |
| 611 | if(gr_is_curr_clr_opaque) |
| 612 | gl->enable(gl, GGL_BLEND); |
| 613 | } |
| 614 | |
| 615 | gr_surface gr_render_circle(int radius, unsigned char r, unsigned char g, unsigned char b, unsigned char a) |
| 616 | { |
| 617 | int rx, ry; |
| 618 | GGLSurface *surface; |
| 619 | const int diameter = radius*2 + 1; |
| 620 | const int radius_check = radius*radius + radius*0.8; |
| 621 | const uint32_t px = (a << 24) | (b << 16) | (g << 8) | r; |
| 622 | uint32_t *data; |
| 623 | |
| 624 | surface = malloc(sizeof(GGLSurface)); |
| 625 | memset(surface, 0, sizeof(GGLSurface)); |
| 626 | |
| 627 | data = malloc(diameter * diameter * 4); |
| 628 | memset(data, 0, diameter * diameter * 4); |
| 629 | |
| 630 | surface->version = sizeof(surface); |
| 631 | surface->width = diameter; |
| 632 | surface->height = diameter; |
| 633 | surface->stride = diameter; |
| 634 | surface->data = (GGLubyte*)data; |
| 635 | surface->format = GGL_PIXEL_FORMAT_RGBA_8888; |
| 636 | |
| 637 | for(ry = -radius; ry <= radius; ++ry) |
| 638 | for(rx = -radius; rx <= radius; ++rx) |
| 639 | if(rx*rx+ry*ry <= radius_check) |
| 640 | *(data + diameter*(radius + ry) + (radius+rx)) = px; |
| 641 | |
| 642 | return surface; |
| 643 | } |
| 644 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 645 | void gr_blit(gr_surface source, int sx, int sy, int w, int h, int dx, int dy) { |
Dees Troy | f171e10 | 2014-11-06 22:19:58 +0100 | [diff] [blame] | 646 | if (gr_context == NULL) { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 647 | return; |
| 648 | } |
| 649 | |
| 650 | GGLContext *gl = gr_context; |
Vojtech Bocek | 6c694b6 | 2014-02-06 20:36:25 +0100 | [diff] [blame] | 651 | GGLSurface *surface = (GGLSurface*)source; |
| 652 | |
| 653 | if(surface->format == GGL_PIXEL_FORMAT_RGBX_8888) |
| 654 | gl->disable(gl, GGL_BLEND); |
| 655 | |
| 656 | gl->bindTexture(gl, surface); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 657 | gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE); |
| 658 | gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE); |
| 659 | gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE); |
| 660 | gl->enable(gl, GGL_TEXTURE_2D); |
| 661 | gl->texCoord2i(gl, sx - dx, sy - dy); |
| 662 | gl->recti(gl, dx, dy, dx + w, dy + h); |
Vojtech Bocek | 3041c88 | 2015-03-06 00:28:21 +0100 | [diff] [blame] | 663 | gl->disable(gl, GGL_TEXTURE_2D); |
Vojtech Bocek | 6c694b6 | 2014-02-06 20:36:25 +0100 | [diff] [blame] | 664 | |
| 665 | if(surface->format == GGL_PIXEL_FORMAT_RGBX_8888) |
Dees Troy | f171e10 | 2014-11-06 22:19:58 +0100 | [diff] [blame] | 666 | gl->enable(gl, GGL_BLEND); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | unsigned int gr_get_width(gr_surface surface) { |
| 670 | if (surface == NULL) { |
| 671 | return 0; |
| 672 | } |
| 673 | return ((GGLSurface*) surface)->width; |
| 674 | } |
| 675 | |
| 676 | unsigned int gr_get_height(gr_surface surface) { |
| 677 | if (surface == NULL) { |
| 678 | return 0; |
| 679 | } |
| 680 | return ((GGLSurface*) surface)->height; |
| 681 | } |
| 682 | |
| 683 | void* gr_loadFont(const char* fontName) |
| 684 | { |
| 685 | int fd; |
| 686 | GRFont *font = 0; |
| 687 | GGLSurface *ftex; |
| 688 | unsigned char *bits, *rle; |
| 689 | unsigned char *in, data; |
| 690 | unsigned width, height; |
| 691 | unsigned element; |
| 692 | |
| 693 | fd = open(fontName, O_RDONLY); |
| 694 | if (fd == -1) |
| 695 | { |
| 696 | char tmp[128]; |
| 697 | |
Dees Troy | 3454ade | 2015-01-20 19:21:04 +0000 | [diff] [blame] | 698 | sprintf(tmp, TWRES "fonts/%s.dat", fontName); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 699 | fd = open(tmp, O_RDONLY); |
| 700 | if (fd == -1) |
| 701 | return NULL; |
| 702 | } |
| 703 | |
| 704 | font = calloc(sizeof(*font), 1); |
| 705 | ftex = &font->texture; |
| 706 | |
| 707 | read(fd, &width, sizeof(unsigned)); |
| 708 | read(fd, &height, sizeof(unsigned)); |
| 709 | read(fd, font->offset, sizeof(unsigned) * 96); |
| 710 | font->offset[96] = width; |
| 711 | |
| 712 | bits = malloc(width * height); |
| 713 | memset(bits, 0, width * height); |
| 714 | |
| 715 | unsigned pos = 0; |
| 716 | while (pos < width * height) |
| 717 | { |
| 718 | int bit; |
| 719 | |
| 720 | read(fd, &data, 1); |
| 721 | for (bit = 0; bit < 8; bit++) |
| 722 | { |
| 723 | if (data & (1 << (7-bit))) bits[pos++] = 255; |
| 724 | else bits[pos++] = 0; |
| 725 | |
| 726 | if (pos == width * height) break; |
| 727 | } |
| 728 | } |
| 729 | close(fd); |
| 730 | |
| 731 | ftex->version = sizeof(*ftex); |
| 732 | ftex->width = width; |
| 733 | ftex->height = height; |
| 734 | ftex->stride = width; |
| 735 | ftex->data = (void*) bits; |
| 736 | ftex->format = GGL_PIXEL_FORMAT_A_8; |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 737 | font->type = FONT_TYPE_TWRP; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 738 | font->cheight = height; |
| 739 | font->ascent = height - 2; |
| 740 | return (void*) font; |
| 741 | } |
| 742 | |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 743 | void gr_freeFont(void *font) |
| 744 | { |
| 745 | GRFont *f = font; |
| 746 | free(f->texture.data); |
| 747 | free(f); |
| 748 | } |
| 749 | |
| 750 | int gr_getMaxFontHeight(void *font) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 751 | { |
| 752 | GRFont *fnt = (GRFont*) font; |
| 753 | |
| 754 | if (!fnt) fnt = gr_font; |
| 755 | if (!fnt) return -1; |
| 756 | |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 757 | #ifndef TW_DISABLE_TTF |
| 758 | if(fnt->type == FONT_TYPE_TTF) |
| 759 | return gr_ttf_getMaxFontHeight(font); |
| 760 | #endif |
| 761 | |
| 762 | return fnt->cheight; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | static void gr_init_font(void) |
| 766 | { |
| 767 | int fontRes; |
| 768 | GGLSurface *ftex; |
| 769 | unsigned char *bits, *rle; |
| 770 | unsigned char *in, data; |
| 771 | unsigned width, height; |
| 772 | unsigned element; |
| 773 | |
| 774 | gr_font = calloc(sizeof(*gr_font), 1); |
| 775 | ftex = &gr_font->texture; |
| 776 | |
| 777 | width = font.width; |
| 778 | height = font.height; |
| 779 | |
| 780 | bits = malloc(width * height); |
| 781 | rle = bits; |
| 782 | |
| 783 | in = font.rundata; |
| 784 | while((data = *in++)) |
| 785 | { |
| 786 | memset(rle, (data & 0x80) ? 255 : 0, data & 0x7f); |
| 787 | rle += (data & 0x7f); |
| 788 | } |
| 789 | for (element = 0; element < 97; element++) |
| 790 | { |
| 791 | gr_font->offset[element] = (element * font.cwidth); |
| 792 | } |
| 793 | |
| 794 | ftex->version = sizeof(*ftex); |
| 795 | ftex->width = width; |
| 796 | ftex->height = height; |
| 797 | ftex->stride = width; |
| 798 | ftex->data = (void*) bits; |
| 799 | ftex->format = GGL_PIXEL_FORMAT_A_8; |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 800 | gr_font->type = FONT_TYPE_TWRP; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 801 | gr_font->cheight = height; |
| 802 | gr_font->ascent = height - 2; |
| 803 | return; |
| 804 | } |
| 805 | |
| 806 | int gr_init(void) |
| 807 | { |
| 808 | gglInit(&gr_context); |
| 809 | GGLContext *gl = gr_context; |
| 810 | |
| 811 | gr_init_font(); |
| 812 | gr_vt_fd = open("/dev/tty0", O_RDWR | O_SYNC); |
| 813 | if (gr_vt_fd < 0) { |
| 814 | // This is non-fatal; post-Cupcake kernels don't have tty0. |
| 815 | } else if (ioctl(gr_vt_fd, KDSETMODE, (void*) KD_GRAPHICS)) { |
| 816 | // However, if we do open tty0, we expect the ioctl to work. |
| 817 | perror("failed KDSETMODE to KD_GRAPHICS on tty0"); |
| 818 | gr_exit(); |
| 819 | return -1; |
| 820 | } |
| 821 | |
| 822 | gr_fb_fd = get_framebuffer(gr_framebuffer); |
| 823 | if (gr_fb_fd < 0) { |
| 824 | perror("Unable to get framebuffer.\n"); |
| 825 | gr_exit(); |
| 826 | return -1; |
| 827 | } |
| 828 | |
| 829 | get_memory_surface(&gr_mem_surface); |
| 830 | |
| 831 | fprintf(stderr, "framebuffer: fd %d (%d x %d)\n", |
| 832 | gr_fb_fd, gr_framebuffer[0].width, gr_framebuffer[0].height); |
| 833 | |
| 834 | /* start with 0 as front (displayed) and 1 as back (drawing) */ |
| 835 | gr_active_fb = 0; |
Ethan Yonker | 4ee5ad7 | 2014-02-18 18:41:17 -0600 | [diff] [blame] | 836 | if (!has_overlay) |
| 837 | set_active_framebuffer(0); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 838 | gl->colorBuffer(gl, &gr_mem_surface); |
| 839 | |
| 840 | gl->activeTexture(gl, 0); |
| 841 | gl->enable(gl, GGL_BLEND); |
| 842 | gl->blendFunc(gl, GGL_SRC_ALPHA, GGL_ONE_MINUS_SRC_ALPHA); |
| 843 | |
xNUTx | cd56f8c | 2014-07-23 01:30:20 +0200 | [diff] [blame] | 844 | #ifdef TW_SCREEN_BLANK_ON_BOOT |
| 845 | printf("TW_SCREEN_BLANK_ON_BOOT := true\n"); |
| 846 | gr_fb_blank(true); |
| 847 | gr_fb_blank(false); |
| 848 | #endif |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 849 | |
Ethan Yonker | 4ee5ad7 | 2014-02-18 18:41:17 -0600 | [diff] [blame] | 850 | if (!alloc_ion_mem(fi.line_length * vi.yres)) |
| 851 | allocate_overlay(gr_fb_fd, gr_framebuffer); |
| 852 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 853 | return 0; |
| 854 | } |
| 855 | |
| 856 | void gr_exit(void) |
| 857 | { |
Ethan Yonker | 4ee5ad7 | 2014-02-18 18:41:17 -0600 | [diff] [blame] | 858 | free_overlay(gr_fb_fd); |
| 859 | free_ion_mem(); |
| 860 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 861 | close(gr_fb_fd); |
| 862 | gr_fb_fd = -1; |
| 863 | |
| 864 | free(gr_mem_surface.data); |
| 865 | |
| 866 | ioctl(gr_vt_fd, KDSETMODE, (void*) KD_TEXT); |
| 867 | close(gr_vt_fd); |
| 868 | gr_vt_fd = -1; |
| 869 | } |
| 870 | |
| 871 | int gr_fb_width(void) |
| 872 | { |
| 873 | return gr_framebuffer[0].width; |
| 874 | } |
| 875 | |
| 876 | int gr_fb_height(void) |
| 877 | { |
| 878 | return gr_framebuffer[0].height; |
| 879 | } |
| 880 | |
| 881 | gr_pixel *gr_fb_data(void) |
| 882 | { |
| 883 | return (unsigned short *) gr_mem_surface.data; |
| 884 | } |
| 885 | |
Dees_Troy | 70237dc | 2013-02-28 21:31:48 +0000 | [diff] [blame] | 886 | int gr_fb_blank(int blank) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 887 | { |
| 888 | int ret; |
Dees Troy | 05d18c9 | 2014-08-04 18:17:07 +0000 | [diff] [blame] | 889 | //if (blank) |
| 890 | //free_overlay(gr_fb_fd); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 891 | |
| 892 | ret = ioctl(gr_fb_fd, FBIOBLANK, blank ? FB_BLANK_POWERDOWN : FB_BLANK_UNBLANK); |
| 893 | if (ret < 0) |
| 894 | perror("ioctl(): blank"); |
Ethan Yonker | 4ee5ad7 | 2014-02-18 18:41:17 -0600 | [diff] [blame] | 895 | |
Dees Troy | 05d18c9 | 2014-08-04 18:17:07 +0000 | [diff] [blame] | 896 | //if (!blank) |
| 897 | //allocate_overlay(gr_fb_fd, gr_framebuffer); |
| 898 | return ret; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 899 | } |
| 900 | |
| 901 | int gr_get_surface(gr_surface* surface) |
| 902 | { |
| 903 | GGLSurface* ms = malloc(sizeof(GGLSurface)); |
| 904 | if (!ms) return -1; |
| 905 | |
| 906 | // Allocate the data |
| 907 | get_memory_surface(ms); |
| 908 | |
| 909 | // Now, copy the data |
| 910 | memcpy(ms->data, gr_mem_surface.data, vi.xres * vi.yres * vi.bits_per_pixel / 8); |
| 911 | |
| 912 | *surface = (gr_surface*) ms; |
| 913 | return 0; |
| 914 | } |
| 915 | |
| 916 | int gr_free_surface(gr_surface surface) |
| 917 | { |
| 918 | if (!surface) |
| 919 | return -1; |
| 920 | |
| 921 | GGLSurface* ms = (GGLSurface*) surface; |
| 922 | free(ms->data); |
| 923 | free(ms); |
| 924 | return 0; |
| 925 | } |
| 926 | |
| 927 | void gr_write_frame_to_file(int fd) |
| 928 | { |
| 929 | write(fd, gr_mem_surface.data, vi.xres * vi.yres * vi.bits_per_pixel / 8); |
| 930 | } |