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> |
Ethan Yonker | c798c9c | 2015-10-09 11:15:26 -0500 | [diff] [blame] | 24 | #include <string.h> |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 25 | |
| 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 Yonker | b7a54a3 | 2015-10-05 10:16:27 -0500 | [diff] [blame] | 36 | #include "../gui/placement.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 37 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 38 | #ifdef RECOVERY_BGRA |
| 39 | #define PIXEL_FORMAT GGL_PIXEL_FORMAT_BGRA_8888 |
| 40 | #define PIXEL_SIZE 4 |
| 41 | #endif |
Kra1o5 | 7756859 | 2015-10-14 18:09:54 +0200 | [diff] [blame] | 42 | #ifdef RECOVERY_RGBA |
| 43 | #define PIXEL_FORMAT GGL_PIXEL_FORMAT_RGBA_8888 |
| 44 | #define PIXEL_SIZE 4 |
| 45 | #endif |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 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 | { |
Ethan Yonker | b7a54a3 | 2015-10-05 10:16:27 -0500 | [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; |
Kra1o5 | 7756859 | 2015-10-14 18:09:54 +0200 | [diff] [blame] | 201 | } 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_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 212 | } 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 Yonker | b7a54a3 | 2015-10-05 10:16:27 -0500 | [diff] [blame] | 225 | fprintf(stderr, "Pixel format: RGB_565\n"); |
| 226 | vi.blue.offset = 0; |
| 227 | vi.green.offset = 5; |
| 228 | vi.red.offset = 11; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 229 | #else |
| 230 | fprintf(stderr, "Pixel format: BGR_565\n"); |
Ethan Yonker | b7a54a3 | 2015-10-05 10:16:27 -0500 | [diff] [blame] | 231 | vi.blue.offset = 11; |
| 232 | vi.green.offset = 5; |
| 233 | vi.red.offset = 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 234 | #endif |
Ethan Yonker | b7a54a3 | 2015-10-05 10:16:27 -0500 | [diff] [blame] | 235 | 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_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 239 | 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 Troy | b042538 | 2014-04-03 00:10:03 +0000 | [diff] [blame] | 267 | #ifdef MSM_BSP |
Ethan Yonker | 4ee5ad7 | 2014-02-18 18:41:17 -0600 | [diff] [blame] | 268 | has_overlay = target_has_overlay(fi.id); |
| 269 | |
Ethan Yonker | 4ee5ad7 | 2014-02-18 18:41:17 -0600 | [diff] [blame] | 270 | if (isTargetMdp5()) |
| 271 | setDisplaySplit(); |
Dees Troy | b042538 | 2014-04-03 00:10:03 +0000 | [diff] [blame] | 272 | #else |
| 273 | has_overlay = false; |
Ethan Yonker | 4ee5ad7 | 2014-02-18 18:41:17 -0600 | [diff] [blame] | 274 | #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_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 286 | } |
| 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_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 296 | printf("setting JANKY BACKBUFFER\n"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 297 | fb->stride = fi.line_length/2; |
| 298 | #else |
| 299 | fb->stride = vi.xres_virtual; |
| 300 | #endif |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 301 | fb->format = PIXEL_FORMAT; |
Ethan Yonker | 4ee5ad7 | 2014-02-18 18:41:17 -0600 | [diff] [blame] | 302 | if (!has_overlay) { |
| 303 | fb->data = bits; |
| 304 | memset(fb->data, 0, vi.yres * fb->stride * PIXEL_SIZE); |
| 305 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 306 | |
| 307 | fb++; |
| 308 | |
Kra1o5 | c9556cc | 2015-06-24 12:19:09 +0200 | [diff] [blame] | 309 | #ifndef TW_DISABLE_DOUBLE_BUFFERING |
Hiemanshu Sharma | 94f6c88 | 2012-11-21 11:25:22 -0600 | [diff] [blame] | 310 | /* check if we can use double buffering */ |
| 311 | if (vi.yres * fi.line_length * 2 > fi.smem_len) |
Kra1o5 | c9556cc | 2015-06-24 12:19:09 +0200 | [diff] [blame] | 312 | #else |
| 313 | printf("TW_DISABLE_DOUBLE_BUFFERING := true\n"); |
| 314 | #endif |
Hiemanshu Sharma | 94f6c88 | 2012-11-21 11:25:22 -0600 | [diff] [blame] | 315 | return fd; |
| 316 | |
| 317 | double_buffering = 1; |
| 318 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 319 | 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 Yonker | bcc502c | 2014-11-10 11:22:10 -0600 | [diff] [blame] | 324 | fb->data = (GGLubyte*) (((unsigned long) bits) + vi.yres * fi.line_length); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 325 | #else |
| 326 | fb->stride = vi.xres_virtual; |
Ethan Yonker | bcc502c | 2014-11-10 11:22:10 -0600 | [diff] [blame] | 327 | fb->data = (GGLubyte*) (((unsigned long) bits) + vi.yres * fb->stride * PIXEL_SIZE); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 328 | #endif |
| 329 | fb->format = PIXEL_FORMAT; |
Ethan Yonker | 4ee5ad7 | 2014-02-18 18:41:17 -0600 | [diff] [blame] | 330 | if (!has_overlay) { |
| 331 | memset(fb->data, 0, vi.yres * fb->stride * PIXEL_SIZE); |
| 332 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 333 | |
| 334 | #ifdef PRINT_SCREENINFO |
Ethan Yonker | b7a54a3 | 2015-10-05 10:16:27 -0500 | [diff] [blame] | 335 | print_fb_var_screeninfo(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 336 | #endif |
| 337 | |
| 338 | return fd; |
| 339 | } |
| 340 | |
| 341 | static 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 | |
| 350 | static void set_active_framebuffer(unsigned n) |
| 351 | { |
Hiemanshu Sharma | 94f6c88 | 2012-11-21 11:25:22 -0600 | [diff] [blame] | 352 | if (n > 1 || !double_buffering) return; |
Hiemanshu Sharma | acf6a9b | 2012-11-21 11:28:36 -0600 | [diff] [blame] | 353 | vi.yres_virtual = vi.yres * NUM_BUFFERS; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 354 | 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 | |
| 361 | void gr_flip(void) |
| 362 | { |
Ethan Yonker | 4ee5ad7 | 2014-02-18 18:41:17 -0600 | [diff] [blame] | 363 | if (-EINVAL == overlay_display_frame(gr_fb_fd, gr_mem_surface.data, |
| 364 | (fi.line_length * vi.yres))) { |
| 365 | GGLContext *gl = gr_context; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 366 | |
Ethan Yonker | 4ee5ad7 | 2014-02-18 18:41:17 -0600 | [diff] [blame] | 367 | /* swap front and back buffers */ |
| 368 | if (double_buffering) |
| 369 | gr_active_fb = (gr_active_fb + 1) & 1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 370 | |
| 371 | #ifdef BOARD_HAS_FLIPPED_SCREEN |
Ethan Yonker | 4ee5ad7 | 2014-02-18 18:41:17 -0600 | [diff] [blame] | 372 | /* flip buffer 180 degrees for devices with physicaly inverted screens */ |
| 373 | unsigned int i; |
| 374 | unsigned int j; |
Bogdan Seniuc | d1372f7 | 2015-01-23 08:22:39 -0800 | [diff] [blame] | 375 | 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 Yonker | 4ee5ad7 | 2014-02-18 18:41:17 -0600 | [diff] [blame] | 379 | } |
| 380 | } |
Bogdan Seniuc | d1372f7 | 2015-01-23 08:22:39 -0800 | [diff] [blame] | 381 | #else |
Ethan Yonker | 4ee5ad7 | 2014-02-18 18:41:17 -0600 | [diff] [blame] | 382 | /* 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 Seniuc | d1372f7 | 2015-01-23 08:22:39 -0800 | [diff] [blame] | 386 | #endif |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 387 | |
Ethan Yonker | 4ee5ad7 | 2014-02-18 18:41:17 -0600 | [diff] [blame] | 388 | /* inform the display driver */ |
| 389 | set_active_framebuffer(gr_active_fb); |
| 390 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | void 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 Bocek | 1a4744a | 2014-02-06 19:52:28 +0100 | [diff] [blame] | 402 | |
| 403 | gr_is_curr_clr_opaque = (a == 255); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | int gr_measureEx(const char *s, void* font) |
| 407 | { |
| 408 | GRFont* fnt = (GRFont*) font; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 409 | |
Ethan Yonker | 88037f4 | 2015-10-04 22:09:08 -0500 | [diff] [blame] | 410 | if (!fnt) |
| 411 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 412 | |
Ethan Yonker | 88037f4 | 2015-10-04 22:09:08 -0500 | [diff] [blame] | 413 | return gr_ttf_measureEx(s, font); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 414 | } |
| 415 | |
Dees Troy | 31218ec | 2014-02-25 20:35:56 +0000 | [diff] [blame] | 416 | int gr_maxExW(const char *s, void* font, int max_width) |
| 417 | { |
| 418 | GRFont* fnt = (GRFont*) font; |
Dees Troy | 31218ec | 2014-02-25 20:35:56 +0000 | [diff] [blame] | 419 | |
Ethan Yonker | 88037f4 | 2015-10-04 22:09:08 -0500 | [diff] [blame] | 420 | if (!fnt) |
| 421 | return 0; |
Dees Troy | 31218ec | 2014-02-25 20:35:56 +0000 | [diff] [blame] | 422 | |
Ethan Yonker | 88037f4 | 2015-10-04 22:09:08 -0500 | [diff] [blame] | 423 | return gr_ttf_maxExW(s, font, max_width); |
Dees Troy | 31218ec | 2014-02-25 20:35:56 +0000 | [diff] [blame] | 424 | } |
| 425 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 426 | int gr_textEx(int x, int y, const char *s, void* pFont) |
| 427 | { |
| 428 | GGLContext *gl = gr_context; |
| 429 | GRFont *font = (GRFont*) pFont; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 430 | |
Ethan Yonker | 88037f4 | 2015-10-04 22:09:08 -0500 | [diff] [blame] | 431 | if (!font) |
| 432 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 433 | |
Ethan Yonker | 88037f4 | 2015-10-04 22:09:08 -0500 | [diff] [blame] | 434 | return gr_ttf_textExWH(gl, x, y, s, pFont, -1, -1); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 435 | } |
| 436 | |
Ethan Yonker | b7a54a3 | 2015-10-05 10:16:27 -0500 | [diff] [blame] | 437 | int 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_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 489 | int 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_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 493 | |
Ethan Yonker | 88037f4 | 2015-10-04 22:09:08 -0500 | [diff] [blame] | 494 | if (!font) |
| 495 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 496 | |
Ethan Yonker | 88037f4 | 2015-10-04 22:09:08 -0500 | [diff] [blame] | 497 | return gr_ttf_textExWH(gl, x, y, s, pFont, max_width, -1); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | int 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_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 504 | |
Ethan Yonker | 88037f4 | 2015-10-04 22:09:08 -0500 | [diff] [blame] | 505 | if (!font) |
| 506 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 507 | |
Ethan Yonker | 88037f4 | 2015-10-04 22:09:08 -0500 | [diff] [blame] | 508 | return gr_ttf_textExWH(gl, x, y, s, pFont, max_width, max_height); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 509 | } |
| 510 | |
that | 9876ac3 | 2015-02-15 21:40:59 +0100 | [diff] [blame] | 511 | void 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 | |
| 518 | void 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_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 525 | void gr_fill(int x, int y, int w, int h) |
| 526 | { |
| 527 | GGLContext *gl = gr_context; |
Vojtech Bocek | 1a4744a | 2014-02-06 19:52:28 +0100 | [diff] [blame] | 528 | |
| 529 | if(gr_is_curr_clr_opaque) |
| 530 | gl->disable(gl, GGL_BLEND); |
| 531 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 532 | gl->recti(gl, x, y, x + w, y + h); |
Vojtech Bocek | 1a4744a | 2014-02-06 19:52:28 +0100 | [diff] [blame] | 533 | |
| 534 | if(gr_is_curr_clr_opaque) |
| 535 | gl->enable(gl, GGL_BLEND); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 536 | } |
| 537 | |
Vojtech Bocek | db378b6 | 2015-03-05 20:02:57 +0100 | [diff] [blame] | 538 | void 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 | |
| 553 | gr_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_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 583 | 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] | 584 | if (gr_context == NULL) { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 585 | return; |
| 586 | } |
| 587 | |
| 588 | GGLContext *gl = gr_context; |
Vojtech Bocek | 6c694b6 | 2014-02-06 20:36:25 +0100 | [diff] [blame] | 589 | 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_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 595 | 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 Bocek | 3041c88 | 2015-03-06 00:28:21 +0100 | [diff] [blame] | 601 | gl->disable(gl, GGL_TEXTURE_2D); |
Vojtech Bocek | 6c694b6 | 2014-02-06 20:36:25 +0100 | [diff] [blame] | 602 | |
| 603 | if(surface->format == GGL_PIXEL_FORMAT_RGBX_8888) |
Dees Troy | f171e10 | 2014-11-06 22:19:58 +0100 | [diff] [blame] | 604 | gl->enable(gl, GGL_BLEND); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | unsigned int gr_get_width(gr_surface surface) { |
| 608 | if (surface == NULL) { |
| 609 | return 0; |
| 610 | } |
| 611 | return ((GGLSurface*) surface)->width; |
| 612 | } |
| 613 | |
| 614 | unsigned int gr_get_height(gr_surface surface) { |
| 615 | if (surface == NULL) { |
| 616 | return 0; |
| 617 | } |
| 618 | return ((GGLSurface*) surface)->height; |
| 619 | } |
| 620 | |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 621 | int gr_getMaxFontHeight(void *font) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 622 | { |
| 623 | GRFont *fnt = (GRFont*) font; |
| 624 | |
Ethan Yonker | 88037f4 | 2015-10-04 22:09:08 -0500 | [diff] [blame] | 625 | if (!fnt) |
| 626 | return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 627 | |
Ethan Yonker | 88037f4 | 2015-10-04 22:09:08 -0500 | [diff] [blame] | 628 | return gr_ttf_getMaxFontHeight(font); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | int gr_init(void) |
| 632 | { |
| 633 | gglInit(&gr_context); |
| 634 | GGLContext *gl = gr_context; |
| 635 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 636 | 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 Yonker | 4ee5ad7 | 2014-02-18 18:41:17 -0600 | [diff] [blame] | 660 | if (!has_overlay) |
| 661 | set_active_framebuffer(0); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 662 | 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 | |
xNUTx | cd56f8c | 2014-07-23 01:30:20 +0200 | [diff] [blame] | 668 | #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_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 673 | |
Ethan Yonker | 4ee5ad7 | 2014-02-18 18:41:17 -0600 | [diff] [blame] | 674 | if (!alloc_ion_mem(fi.line_length * vi.yres)) |
| 675 | allocate_overlay(gr_fb_fd, gr_framebuffer); |
| 676 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 677 | return 0; |
| 678 | } |
| 679 | |
| 680 | void gr_exit(void) |
| 681 | { |
Ethan Yonker | 4ee5ad7 | 2014-02-18 18:41:17 -0600 | [diff] [blame] | 682 | free_overlay(gr_fb_fd); |
| 683 | free_ion_mem(); |
| 684 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 685 | 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 | |
| 695 | int gr_fb_width(void) |
| 696 | { |
| 697 | return gr_framebuffer[0].width; |
| 698 | } |
| 699 | |
| 700 | int gr_fb_height(void) |
| 701 | { |
| 702 | return gr_framebuffer[0].height; |
| 703 | } |
| 704 | |
| 705 | gr_pixel *gr_fb_data(void) |
| 706 | { |
| 707 | return (unsigned short *) gr_mem_surface.data; |
| 708 | } |
| 709 | |
Dees_Troy | 70237dc | 2013-02-28 21:31:48 +0000 | [diff] [blame] | 710 | int gr_fb_blank(int blank) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 711 | { |
| 712 | int ret; |
Dees Troy | 05d18c9 | 2014-08-04 18:17:07 +0000 | [diff] [blame] | 713 | //if (blank) |
| 714 | //free_overlay(gr_fb_fd); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 715 | |
| 716 | ret = ioctl(gr_fb_fd, FBIOBLANK, blank ? FB_BLANK_POWERDOWN : FB_BLANK_UNBLANK); |
| 717 | if (ret < 0) |
| 718 | perror("ioctl(): blank"); |
Ethan Yonker | 4ee5ad7 | 2014-02-18 18:41:17 -0600 | [diff] [blame] | 719 | |
Dees Troy | 05d18c9 | 2014-08-04 18:17:07 +0000 | [diff] [blame] | 720 | //if (!blank) |
| 721 | //allocate_overlay(gr_fb_fd, gr_framebuffer); |
| 722 | return ret; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | int 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 | |
| 740 | int 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 | |
| 751 | void 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 | } |