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" |
| 36 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 37 | #ifdef RECOVERY_BGRA |
| 38 | #define PIXEL_FORMAT GGL_PIXEL_FORMAT_BGRA_8888 |
| 39 | #define PIXEL_SIZE 4 |
| 40 | #endif |
Kra1o5 | 7756859 | 2015-10-14 18:09:54 +0200 | [diff] [blame] | 41 | #ifdef RECOVERY_RGBA |
| 42 | #define PIXEL_FORMAT GGL_PIXEL_FORMAT_RGBA_8888 |
| 43 | #define PIXEL_SIZE 4 |
| 44 | #endif |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 45 | #ifdef RECOVERY_RGBX |
| 46 | #define PIXEL_FORMAT GGL_PIXEL_FORMAT_RGBX_8888 |
| 47 | #define PIXEL_SIZE 4 |
| 48 | #endif |
| 49 | #ifndef PIXEL_FORMAT |
| 50 | #define PIXEL_FORMAT GGL_PIXEL_FORMAT_RGB_565 |
| 51 | #define PIXEL_SIZE 2 |
| 52 | #endif |
| 53 | |
Hiemanshu Sharma | acf6a9b | 2012-11-21 11:28:36 -0600 | [diff] [blame] | 54 | #define NUM_BUFFERS 2 |
Ethan Yonker | 4ee5ad7 | 2014-02-18 18:41:17 -0600 | [diff] [blame] | 55 | #define MAX_DISPLAY_DIM 2048 |
Hiemanshu Sharma | acf6a9b | 2012-11-21 11:28:36 -0600 | [diff] [blame] | 56 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 57 | // #define PRINT_SCREENINFO 1 // Enables printing of screen info to log |
| 58 | |
| 59 | typedef struct { |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 60 | int type; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 61 | GGLSurface texture; |
| 62 | unsigned offset[97]; |
| 63 | unsigned cheight; |
| 64 | unsigned ascent; |
| 65 | } GRFont; |
| 66 | |
| 67 | static GRFont *gr_font = 0; |
| 68 | static GGLContext *gr_context = 0; |
| 69 | static GGLSurface gr_font_texture; |
Hiemanshu Sharma | acf6a9b | 2012-11-21 11:28:36 -0600 | [diff] [blame] | 70 | static GGLSurface gr_framebuffer[NUM_BUFFERS]; |
Vojtech Bocek | 03fd6c5 | 2014-03-13 18:46:34 +0100 | [diff] [blame] | 71 | GGLSurface gr_mem_surface; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 72 | static unsigned gr_active_fb = 0; |
Hiemanshu Sharma | 94f6c88 | 2012-11-21 11:25:22 -0600 | [diff] [blame] | 73 | static unsigned double_buffering = 0; |
Vojtech Bocek | 1a4744a | 2014-02-06 19:52:28 +0100 | [diff] [blame] | 74 | static int gr_is_curr_clr_opaque = 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 75 | |
| 76 | static int gr_fb_fd = -1; |
| 77 | static int gr_vt_fd = -1; |
| 78 | |
Vojtech Bocek | 03fd6c5 | 2014-03-13 18:46:34 +0100 | [diff] [blame] | 79 | struct fb_var_screeninfo vi; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 80 | static struct fb_fix_screeninfo fi; |
| 81 | |
Ethan Yonker | 4ee5ad7 | 2014-02-18 18:41:17 -0600 | [diff] [blame] | 82 | static bool has_overlay = false; |
| 83 | static int leftSplit = 0; |
| 84 | static int rightSplit = 0; |
| 85 | |
| 86 | bool target_has_overlay(char *version); |
| 87 | int free_ion_mem(void); |
| 88 | int alloc_ion_mem(unsigned int size); |
| 89 | int allocate_overlay(int fd, GGLSurface gr_fb[]); |
| 90 | int free_overlay(int fd); |
| 91 | int overlay_display_frame(int fd, GGLubyte* data, size_t size); |
| 92 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 93 | #ifdef PRINT_SCREENINFO |
| 94 | static void print_fb_var_screeninfo() |
| 95 | { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 96 | printf("vi.xres: %d\n", vi.xres); |
| 97 | printf("vi.yres: %d\n", vi.yres); |
| 98 | printf("vi.xres_virtual: %d\n", vi.xres_virtual); |
| 99 | printf("vi.yres_virtual: %d\n", vi.yres_virtual); |
| 100 | printf("vi.xoffset: %d\n", vi.xoffset); |
| 101 | printf("vi.yoffset: %d\n", vi.yoffset); |
| 102 | printf("vi.bits_per_pixel: %d\n", vi.bits_per_pixel); |
| 103 | printf("vi.grayscale: %d\n", vi.grayscale); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 104 | } |
| 105 | #endif |
| 106 | |
Ethan Yonker | 4ee5ad7 | 2014-02-18 18:41:17 -0600 | [diff] [blame] | 107 | #ifdef MSM_BSP |
| 108 | int getLeftSplit(void) { |
| 109 | //Default even split for all displays with high res |
| 110 | int lSplit = vi.xres / 2; |
| 111 | |
| 112 | //Override if split published by driver |
| 113 | if (leftSplit) |
| 114 | lSplit = leftSplit; |
| 115 | |
| 116 | return lSplit; |
| 117 | } |
| 118 | |
| 119 | int getRightSplit(void) { |
| 120 | return rightSplit; |
| 121 | } |
| 122 | |
| 123 | |
| 124 | void setDisplaySplit(void) { |
| 125 | char split[64] = {0}; |
| 126 | FILE* fp = fopen("/sys/class/graphics/fb0/msm_fb_split", "r"); |
| 127 | if (fp) { |
| 128 | //Format "left right" space as delimiter |
| 129 | if(fread(split, sizeof(char), 64, fp)) { |
| 130 | leftSplit = atoi(split); |
| 131 | printf("Left Split=%d\n",leftSplit); |
| 132 | char *rght = strpbrk(split, " "); |
| 133 | if (rght) |
| 134 | rightSplit = atoi(rght + 1); |
| 135 | printf("Right Split=%d\n", rightSplit); |
| 136 | } |
| 137 | } else { |
| 138 | printf("Failed to open mdss_fb_split node\n"); |
| 139 | } |
| 140 | if (fp) |
| 141 | fclose(fp); |
| 142 | } |
| 143 | |
| 144 | bool isDisplaySplit(void) { |
| 145 | if (vi.xres > MAX_DISPLAY_DIM) |
| 146 | return true; |
| 147 | //check if right split is set by driver |
| 148 | if (getRightSplit()) |
| 149 | return true; |
| 150 | |
| 151 | return false; |
| 152 | } |
| 153 | |
| 154 | int getFbXres(void) { |
| 155 | return vi.xres; |
| 156 | } |
| 157 | |
| 158 | int getFbYres (void) { |
| 159 | return vi.yres; |
| 160 | } |
| 161 | #endif // MSM_BSP |
| 162 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 163 | static int get_framebuffer(GGLSurface *fb) |
| 164 | { |
Ethan Yonker | 4f6a976 | 2015-03-09 13:58:54 -0500 | [diff] [blame] | 165 | int fd, index = 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 166 | void *bits; |
| 167 | |
| 168 | fd = open("/dev/graphics/fb0", O_RDWR); |
jenkins | 7969913 | 2015-05-08 06:04:44 -0400 | [diff] [blame] | 169 | |
| 170 | while (fd < 0 && index < 30) { |
Ethan Yonker | 4f6a976 | 2015-03-09 13:58:54 -0500 | [diff] [blame] | 171 | usleep(1000); |
| 172 | fd = open("/dev/graphics/fb0", O_RDWR); |
| 173 | index++; |
| 174 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 175 | if (fd < 0) { |
Ethan Yonker | 4f6a976 | 2015-03-09 13:58:54 -0500 | [diff] [blame] | 176 | perror("cannot open fb0\n"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 177 | return -1; |
| 178 | } |
| 179 | |
| 180 | if (ioctl(fd, FBIOGET_VSCREENINFO, &vi) < 0) { |
| 181 | perror("failed to get fb0 info"); |
| 182 | close(fd); |
| 183 | return -1; |
| 184 | } |
| 185 | |
| 186 | fprintf(stderr, "Pixel format: %dx%d @ %dbpp\n", vi.xres, vi.yres, vi.bits_per_pixel); |
| 187 | |
| 188 | vi.bits_per_pixel = PIXEL_SIZE * 8; |
| 189 | if (PIXEL_FORMAT == GGL_PIXEL_FORMAT_BGRA_8888) { |
| 190 | fprintf(stderr, "Pixel format: BGRA_8888\n"); |
| 191 | if (PIXEL_SIZE != 4) fprintf(stderr, "E: Pixel Size mismatch!\n"); |
| 192 | vi.red.offset = 8; |
| 193 | vi.red.length = 8; |
| 194 | vi.green.offset = 16; |
| 195 | vi.green.length = 8; |
| 196 | vi.blue.offset = 24; |
| 197 | vi.blue.length = 8; |
| 198 | vi.transp.offset = 0; |
| 199 | vi.transp.length = 8; |
Kra1o5 | 7756859 | 2015-10-14 18:09:54 +0200 | [diff] [blame] | 200 | } else if (PIXEL_FORMAT == GGL_PIXEL_FORMAT_RGBA_8888) { |
| 201 | fprintf(stderr, "Pixel format: RGBA_8888\n"); |
| 202 | if (PIXEL_SIZE != 4) fprintf(stderr, "E: Pixel Size mismatch!\n"); |
| 203 | vi.red.offset = 0; |
| 204 | vi.red.length = 8; |
| 205 | vi.green.offset = 8; |
| 206 | vi.green.length = 8; |
| 207 | vi.blue.offset = 16; |
| 208 | vi.blue.length = 8; |
| 209 | vi.transp.offset = 24; |
| 210 | vi.transp.length = 8; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 211 | } else if (PIXEL_FORMAT == GGL_PIXEL_FORMAT_RGBX_8888) { |
| 212 | fprintf(stderr, "Pixel format: RGBX_8888\n"); |
| 213 | if (PIXEL_SIZE != 4) fprintf(stderr, "E: Pixel Size mismatch!\n"); |
| 214 | vi.red.offset = 24; |
| 215 | vi.red.length = 8; |
| 216 | vi.green.offset = 16; |
| 217 | vi.green.length = 8; |
| 218 | vi.blue.offset = 8; |
| 219 | vi.blue.length = 8; |
| 220 | vi.transp.offset = 0; |
| 221 | vi.transp.length = 8; |
| 222 | } else if (PIXEL_FORMAT == GGL_PIXEL_FORMAT_RGB_565) { |
| 223 | #ifdef RECOVERY_RGB_565 |
| 224 | fprintf(stderr, "Pixel format: RGB_565\n"); |
| 225 | vi.blue.offset = 0; |
| 226 | vi.green.offset = 5; |
| 227 | vi.red.offset = 11; |
| 228 | #else |
| 229 | fprintf(stderr, "Pixel format: BGR_565\n"); |
| 230 | vi.blue.offset = 11; |
| 231 | vi.green.offset = 5; |
| 232 | vi.red.offset = 0; |
| 233 | #endif |
| 234 | if (PIXEL_SIZE != 2) fprintf(stderr, "E: Pixel Size mismatch!\n"); |
| 235 | vi.blue.length = 5; |
| 236 | vi.green.length = 6; |
| 237 | vi.red.length = 5; |
| 238 | vi.blue.msb_right = 0; |
| 239 | vi.green.msb_right = 0; |
| 240 | vi.red.msb_right = 0; |
| 241 | vi.transp.offset = 0; |
| 242 | vi.transp.length = 0; |
| 243 | } |
| 244 | else |
| 245 | { |
| 246 | perror("unknown pixel format"); |
| 247 | close(fd); |
| 248 | return -1; |
| 249 | } |
| 250 | |
| 251 | vi.vmode = FB_VMODE_NONINTERLACED; |
| 252 | vi.activate = FB_ACTIVATE_NOW | FB_ACTIVATE_FORCE; |
| 253 | |
| 254 | if (ioctl(fd, FBIOPUT_VSCREENINFO, &vi) < 0) { |
| 255 | perror("failed to put fb0 info"); |
| 256 | close(fd); |
| 257 | return -1; |
| 258 | } |
| 259 | |
| 260 | if (ioctl(fd, FBIOGET_FSCREENINFO, &fi) < 0) { |
| 261 | perror("failed to get fb0 info"); |
| 262 | close(fd); |
| 263 | return -1; |
| 264 | } |
| 265 | |
Dees Troy | b042538 | 2014-04-03 00:10:03 +0000 | [diff] [blame] | 266 | #ifdef MSM_BSP |
Ethan Yonker | 4ee5ad7 | 2014-02-18 18:41:17 -0600 | [diff] [blame] | 267 | has_overlay = target_has_overlay(fi.id); |
| 268 | |
Ethan Yonker | 4ee5ad7 | 2014-02-18 18:41:17 -0600 | [diff] [blame] | 269 | if (isTargetMdp5()) |
| 270 | setDisplaySplit(); |
Dees Troy | b042538 | 2014-04-03 00:10:03 +0000 | [diff] [blame] | 271 | #else |
| 272 | has_overlay = false; |
Ethan Yonker | 4ee5ad7 | 2014-02-18 18:41:17 -0600 | [diff] [blame] | 273 | #endif |
| 274 | |
| 275 | if (!has_overlay) { |
| 276 | printf("Not using qualcomm overlay, '%s'\n", fi.id); |
| 277 | bits = mmap(0, fi.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); |
| 278 | if (bits == MAP_FAILED) { |
| 279 | perror("failed to mmap framebuffer"); |
| 280 | close(fd); |
| 281 | return -1; |
| 282 | } |
| 283 | } else { |
| 284 | printf("Using qualcomm overlay\n"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | #ifdef RECOVERY_GRAPHICS_USE_LINELENGTH |
| 288 | vi.xres_virtual = fi.line_length / PIXEL_SIZE; |
| 289 | #endif |
| 290 | |
| 291 | fb->version = sizeof(*fb); |
| 292 | fb->width = vi.xres; |
| 293 | fb->height = vi.yres; |
| 294 | #ifdef BOARD_HAS_JANKY_BACKBUFFER |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 295 | printf("setting JANKY BACKBUFFER\n"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 296 | fb->stride = fi.line_length/2; |
| 297 | #else |
| 298 | fb->stride = vi.xres_virtual; |
| 299 | #endif |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 300 | fb->format = PIXEL_FORMAT; |
Ethan Yonker | 4ee5ad7 | 2014-02-18 18:41:17 -0600 | [diff] [blame] | 301 | if (!has_overlay) { |
| 302 | fb->data = bits; |
| 303 | memset(fb->data, 0, vi.yres * fb->stride * PIXEL_SIZE); |
| 304 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 305 | |
| 306 | fb++; |
| 307 | |
Kra1o5 | c9556cc | 2015-06-24 12:19:09 +0200 | [diff] [blame] | 308 | #ifndef TW_DISABLE_DOUBLE_BUFFERING |
Hiemanshu Sharma | 94f6c88 | 2012-11-21 11:25:22 -0600 | [diff] [blame] | 309 | /* check if we can use double buffering */ |
| 310 | if (vi.yres * fi.line_length * 2 > fi.smem_len) |
Kra1o5 | c9556cc | 2015-06-24 12:19:09 +0200 | [diff] [blame] | 311 | #else |
| 312 | printf("TW_DISABLE_DOUBLE_BUFFERING := true\n"); |
| 313 | #endif |
Hiemanshu Sharma | 94f6c88 | 2012-11-21 11:25:22 -0600 | [diff] [blame] | 314 | return fd; |
| 315 | |
| 316 | double_buffering = 1; |
| 317 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 318 | fb->version = sizeof(*fb); |
| 319 | fb->width = vi.xres; |
| 320 | fb->height = vi.yres; |
| 321 | #ifdef BOARD_HAS_JANKY_BACKBUFFER |
| 322 | fb->stride = fi.line_length/2; |
Ethan Yonker | bcc502c | 2014-11-10 11:22:10 -0600 | [diff] [blame] | 323 | fb->data = (GGLubyte*) (((unsigned long) bits) + vi.yres * fi.line_length); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 324 | #else |
| 325 | fb->stride = vi.xres_virtual; |
Ethan Yonker | bcc502c | 2014-11-10 11:22:10 -0600 | [diff] [blame] | 326 | fb->data = (GGLubyte*) (((unsigned long) bits) + vi.yres * fb->stride * PIXEL_SIZE); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 327 | #endif |
| 328 | fb->format = PIXEL_FORMAT; |
Ethan Yonker | 4ee5ad7 | 2014-02-18 18:41:17 -0600 | [diff] [blame] | 329 | if (!has_overlay) { |
| 330 | memset(fb->data, 0, vi.yres * fb->stride * PIXEL_SIZE); |
| 331 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 332 | |
| 333 | #ifdef PRINT_SCREENINFO |
| 334 | print_fb_var_screeninfo(); |
| 335 | #endif |
| 336 | |
| 337 | return fd; |
| 338 | } |
| 339 | |
| 340 | static void get_memory_surface(GGLSurface* ms) { |
| 341 | ms->version = sizeof(*ms); |
| 342 | ms->width = vi.xres; |
| 343 | ms->height = vi.yres; |
| 344 | ms->stride = vi.xres_virtual; |
| 345 | ms->data = malloc(vi.xres_virtual * vi.yres * PIXEL_SIZE); |
| 346 | ms->format = PIXEL_FORMAT; |
| 347 | } |
| 348 | |
| 349 | static void set_active_framebuffer(unsigned n) |
| 350 | { |
Hiemanshu Sharma | 94f6c88 | 2012-11-21 11:25:22 -0600 | [diff] [blame] | 351 | if (n > 1 || !double_buffering) return; |
Hiemanshu Sharma | acf6a9b | 2012-11-21 11:28:36 -0600 | [diff] [blame] | 352 | vi.yres_virtual = vi.yres * NUM_BUFFERS; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 353 | vi.yoffset = n * vi.yres; |
| 354 | // vi.bits_per_pixel = PIXEL_SIZE * 8; |
| 355 | if (ioctl(gr_fb_fd, FBIOPUT_VSCREENINFO, &vi) < 0) { |
| 356 | perror("active fb swap failed"); |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | void gr_flip(void) |
| 361 | { |
Ethan Yonker | 4ee5ad7 | 2014-02-18 18:41:17 -0600 | [diff] [blame] | 362 | if (-EINVAL == overlay_display_frame(gr_fb_fd, gr_mem_surface.data, |
| 363 | (fi.line_length * vi.yres))) { |
| 364 | GGLContext *gl = gr_context; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 365 | |
Ethan Yonker | 4ee5ad7 | 2014-02-18 18:41:17 -0600 | [diff] [blame] | 366 | /* swap front and back buffers */ |
| 367 | if (double_buffering) |
| 368 | gr_active_fb = (gr_active_fb + 1) & 1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 369 | |
| 370 | #ifdef BOARD_HAS_FLIPPED_SCREEN |
Ethan Yonker | 4ee5ad7 | 2014-02-18 18:41:17 -0600 | [diff] [blame] | 371 | /* flip buffer 180 degrees for devices with physicaly inverted screens */ |
| 372 | unsigned int i; |
| 373 | unsigned int j; |
Bogdan Seniuc | d1372f7 | 2015-01-23 08:22:39 -0800 | [diff] [blame] | 374 | for (i = 0; i < vi.yres; i++) { |
| 375 | for (j = 0; j < vi.xres; j++) { |
| 376 | memcpy(gr_framebuffer[gr_active_fb].data + (i * vi.xres_virtual + j) * PIXEL_SIZE, |
| 377 | 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] | 378 | } |
| 379 | } |
Bogdan Seniuc | d1372f7 | 2015-01-23 08:22:39 -0800 | [diff] [blame] | 380 | #else |
Ethan Yonker | 4ee5ad7 | 2014-02-18 18:41:17 -0600 | [diff] [blame] | 381 | /* copy data from the in-memory surface to the buffer we're about |
| 382 | * to make active. */ |
| 383 | memcpy(gr_framebuffer[gr_active_fb].data, gr_mem_surface.data, |
| 384 | vi.xres_virtual * vi.yres * PIXEL_SIZE); |
Bogdan Seniuc | d1372f7 | 2015-01-23 08:22:39 -0800 | [diff] [blame] | 385 | #endif |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 386 | |
Ethan Yonker | 4ee5ad7 | 2014-02-18 18:41:17 -0600 | [diff] [blame] | 387 | /* inform the display driver */ |
| 388 | set_active_framebuffer(gr_active_fb); |
| 389 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | void gr_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a) |
| 393 | { |
| 394 | GGLContext *gl = gr_context; |
| 395 | GGLint color[4]; |
| 396 | color[0] = ((r << 8) | r) + 1; |
| 397 | color[1] = ((g << 8) | g) + 1; |
| 398 | color[2] = ((b << 8) | b) + 1; |
| 399 | color[3] = ((a << 8) | a) + 1; |
| 400 | gl->color4xv(gl, color); |
Vojtech Bocek | 1a4744a | 2014-02-06 19:52:28 +0100 | [diff] [blame] | 401 | |
| 402 | gr_is_curr_clr_opaque = (a == 255); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | int gr_measureEx(const char *s, void* font) |
| 406 | { |
| 407 | GRFont* fnt = (GRFont*) font; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 408 | |
Ethan Yonker | 88037f4 | 2015-10-04 22:09:08 -0500 | [diff] [blame] | 409 | if (!fnt) |
| 410 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 411 | |
Ethan Yonker | 88037f4 | 2015-10-04 22:09:08 -0500 | [diff] [blame] | 412 | return gr_ttf_measureEx(s, font); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 413 | } |
| 414 | |
Dees Troy | 31218ec | 2014-02-25 20:35:56 +0000 | [diff] [blame] | 415 | int gr_maxExW(const char *s, void* font, int max_width) |
| 416 | { |
| 417 | GRFont* fnt = (GRFont*) font; |
Dees Troy | 31218ec | 2014-02-25 20:35:56 +0000 | [diff] [blame] | 418 | |
Ethan Yonker | 88037f4 | 2015-10-04 22:09:08 -0500 | [diff] [blame] | 419 | if (!fnt) |
| 420 | return 0; |
Dees Troy | 31218ec | 2014-02-25 20:35:56 +0000 | [diff] [blame] | 421 | |
Ethan Yonker | 88037f4 | 2015-10-04 22:09:08 -0500 | [diff] [blame] | 422 | return gr_ttf_maxExW(s, font, max_width); |
Dees Troy | 31218ec | 2014-02-25 20:35:56 +0000 | [diff] [blame] | 423 | } |
| 424 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 425 | int gr_textEx(int x, int y, const char *s, void* pFont) |
| 426 | { |
| 427 | GGLContext *gl = gr_context; |
| 428 | GRFont *font = (GRFont*) pFont; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 429 | |
Ethan Yonker | 88037f4 | 2015-10-04 22:09:08 -0500 | [diff] [blame] | 430 | if (!font) |
| 431 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 432 | |
Ethan Yonker | 88037f4 | 2015-10-04 22:09:08 -0500 | [diff] [blame] | 433 | return gr_ttf_textExWH(gl, x, y, s, pFont, -1, -1); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | int gr_textExW(int x, int y, const char *s, void* pFont, int max_width) |
| 437 | { |
| 438 | GGLContext *gl = gr_context; |
| 439 | GRFont *font = (GRFont*) pFont; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 440 | |
Ethan Yonker | 88037f4 | 2015-10-04 22:09:08 -0500 | [diff] [blame] | 441 | if (!font) |
| 442 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 443 | |
Ethan Yonker | 88037f4 | 2015-10-04 22:09:08 -0500 | [diff] [blame] | 444 | return gr_ttf_textExWH(gl, x, y, s, pFont, max_width, -1); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | int gr_textExWH(int x, int y, const char *s, void* pFont, int max_width, int max_height) |
| 448 | { |
| 449 | GGLContext *gl = gr_context; |
| 450 | GRFont *font = (GRFont*) pFont; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 451 | |
Ethan Yonker | 88037f4 | 2015-10-04 22:09:08 -0500 | [diff] [blame] | 452 | if (!font) |
| 453 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 454 | |
Ethan Yonker | 88037f4 | 2015-10-04 22:09:08 -0500 | [diff] [blame] | 455 | 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] | 456 | } |
| 457 | |
that | 9876ac3 | 2015-02-15 21:40:59 +0100 | [diff] [blame] | 458 | void gr_clip(int x, int y, int w, int h) |
| 459 | { |
| 460 | GGLContext *gl = gr_context; |
| 461 | gl->scissor(gl, x, y, w, h); |
| 462 | gl->enable(gl, GGL_SCISSOR_TEST); |
| 463 | } |
| 464 | |
| 465 | void gr_noclip() |
| 466 | { |
| 467 | GGLContext *gl = gr_context; |
| 468 | gl->scissor(gl, 0, 0, gr_fb_width(), gr_fb_height()); |
| 469 | gl->disable(gl, GGL_SCISSOR_TEST); |
| 470 | } |
| 471 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 472 | void gr_fill(int x, int y, int w, int h) |
| 473 | { |
| 474 | GGLContext *gl = gr_context; |
Vojtech Bocek | 1a4744a | 2014-02-06 19:52:28 +0100 | [diff] [blame] | 475 | |
| 476 | if(gr_is_curr_clr_opaque) |
| 477 | gl->disable(gl, GGL_BLEND); |
| 478 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 479 | gl->recti(gl, x, y, x + w, y + h); |
Vojtech Bocek | 1a4744a | 2014-02-06 19:52:28 +0100 | [diff] [blame] | 480 | |
| 481 | if(gr_is_curr_clr_opaque) |
| 482 | gl->enable(gl, GGL_BLEND); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 483 | } |
| 484 | |
Vojtech Bocek | db378b6 | 2015-03-05 20:02:57 +0100 | [diff] [blame] | 485 | void gr_line(int x0, int y0, int x1, int y1, int width) |
| 486 | { |
| 487 | GGLContext *gl = gr_context; |
| 488 | |
| 489 | if(gr_is_curr_clr_opaque) |
| 490 | gl->disable(gl, GGL_BLEND); |
| 491 | |
| 492 | const int coords0[2] = { x0 << 4, y0 << 4 }; |
| 493 | const int coords1[2] = { x1 << 4, y1 << 4 }; |
| 494 | gl->linex(gl, coords0, coords1, width << 4); |
| 495 | |
| 496 | if(gr_is_curr_clr_opaque) |
| 497 | gl->enable(gl, GGL_BLEND); |
| 498 | } |
| 499 | |
| 500 | gr_surface gr_render_circle(int radius, unsigned char r, unsigned char g, unsigned char b, unsigned char a) |
| 501 | { |
| 502 | int rx, ry; |
| 503 | GGLSurface *surface; |
| 504 | const int diameter = radius*2 + 1; |
| 505 | const int radius_check = radius*radius + radius*0.8; |
| 506 | const uint32_t px = (a << 24) | (b << 16) | (g << 8) | r; |
| 507 | uint32_t *data; |
| 508 | |
| 509 | surface = malloc(sizeof(GGLSurface)); |
| 510 | memset(surface, 0, sizeof(GGLSurface)); |
| 511 | |
| 512 | data = malloc(diameter * diameter * 4); |
| 513 | memset(data, 0, diameter * diameter * 4); |
| 514 | |
| 515 | surface->version = sizeof(surface); |
| 516 | surface->width = diameter; |
| 517 | surface->height = diameter; |
| 518 | surface->stride = diameter; |
| 519 | surface->data = (GGLubyte*)data; |
| 520 | surface->format = GGL_PIXEL_FORMAT_RGBA_8888; |
| 521 | |
| 522 | for(ry = -radius; ry <= radius; ++ry) |
| 523 | for(rx = -radius; rx <= radius; ++rx) |
| 524 | if(rx*rx+ry*ry <= radius_check) |
| 525 | *(data + diameter*(radius + ry) + (radius+rx)) = px; |
| 526 | |
| 527 | return surface; |
| 528 | } |
| 529 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 530 | 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] | 531 | if (gr_context == NULL) { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 532 | return; |
| 533 | } |
| 534 | |
| 535 | GGLContext *gl = gr_context; |
Vojtech Bocek | 6c694b6 | 2014-02-06 20:36:25 +0100 | [diff] [blame] | 536 | GGLSurface *surface = (GGLSurface*)source; |
| 537 | |
| 538 | if(surface->format == GGL_PIXEL_FORMAT_RGBX_8888) |
| 539 | gl->disable(gl, GGL_BLEND); |
| 540 | |
| 541 | gl->bindTexture(gl, surface); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 542 | gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE); |
| 543 | gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE); |
| 544 | gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE); |
| 545 | gl->enable(gl, GGL_TEXTURE_2D); |
| 546 | gl->texCoord2i(gl, sx - dx, sy - dy); |
| 547 | gl->recti(gl, dx, dy, dx + w, dy + h); |
Vojtech Bocek | 3041c88 | 2015-03-06 00:28:21 +0100 | [diff] [blame] | 548 | gl->disable(gl, GGL_TEXTURE_2D); |
Vojtech Bocek | 6c694b6 | 2014-02-06 20:36:25 +0100 | [diff] [blame] | 549 | |
| 550 | if(surface->format == GGL_PIXEL_FORMAT_RGBX_8888) |
Dees Troy | f171e10 | 2014-11-06 22:19:58 +0100 | [diff] [blame] | 551 | gl->enable(gl, GGL_BLEND); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | unsigned int gr_get_width(gr_surface surface) { |
| 555 | if (surface == NULL) { |
| 556 | return 0; |
| 557 | } |
| 558 | return ((GGLSurface*) surface)->width; |
| 559 | } |
| 560 | |
| 561 | unsigned int gr_get_height(gr_surface surface) { |
| 562 | if (surface == NULL) { |
| 563 | return 0; |
| 564 | } |
| 565 | return ((GGLSurface*) surface)->height; |
| 566 | } |
| 567 | |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 568 | int gr_getMaxFontHeight(void *font) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 569 | { |
| 570 | GRFont *fnt = (GRFont*) font; |
| 571 | |
Ethan Yonker | 88037f4 | 2015-10-04 22:09:08 -0500 | [diff] [blame] | 572 | if (!fnt) |
| 573 | return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 574 | |
Ethan Yonker | 88037f4 | 2015-10-04 22:09:08 -0500 | [diff] [blame] | 575 | return gr_ttf_getMaxFontHeight(font); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | int gr_init(void) |
| 579 | { |
| 580 | gglInit(&gr_context); |
| 581 | GGLContext *gl = gr_context; |
| 582 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 583 | gr_vt_fd = open("/dev/tty0", O_RDWR | O_SYNC); |
| 584 | if (gr_vt_fd < 0) { |
| 585 | // This is non-fatal; post-Cupcake kernels don't have tty0. |
| 586 | } else if (ioctl(gr_vt_fd, KDSETMODE, (void*) KD_GRAPHICS)) { |
| 587 | // However, if we do open tty0, we expect the ioctl to work. |
| 588 | perror("failed KDSETMODE to KD_GRAPHICS on tty0"); |
| 589 | gr_exit(); |
| 590 | return -1; |
| 591 | } |
| 592 | |
| 593 | gr_fb_fd = get_framebuffer(gr_framebuffer); |
| 594 | if (gr_fb_fd < 0) { |
| 595 | perror("Unable to get framebuffer.\n"); |
| 596 | gr_exit(); |
| 597 | return -1; |
| 598 | } |
| 599 | |
| 600 | get_memory_surface(&gr_mem_surface); |
| 601 | |
| 602 | fprintf(stderr, "framebuffer: fd %d (%d x %d)\n", |
| 603 | gr_fb_fd, gr_framebuffer[0].width, gr_framebuffer[0].height); |
| 604 | |
| 605 | /* start with 0 as front (displayed) and 1 as back (drawing) */ |
| 606 | gr_active_fb = 0; |
Ethan Yonker | 4ee5ad7 | 2014-02-18 18:41:17 -0600 | [diff] [blame] | 607 | if (!has_overlay) |
| 608 | set_active_framebuffer(0); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 609 | gl->colorBuffer(gl, &gr_mem_surface); |
| 610 | |
| 611 | gl->activeTexture(gl, 0); |
| 612 | gl->enable(gl, GGL_BLEND); |
| 613 | gl->blendFunc(gl, GGL_SRC_ALPHA, GGL_ONE_MINUS_SRC_ALPHA); |
| 614 | |
xNUTx | cd56f8c | 2014-07-23 01:30:20 +0200 | [diff] [blame] | 615 | #ifdef TW_SCREEN_BLANK_ON_BOOT |
| 616 | printf("TW_SCREEN_BLANK_ON_BOOT := true\n"); |
| 617 | gr_fb_blank(true); |
| 618 | gr_fb_blank(false); |
| 619 | #endif |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 620 | |
Ethan Yonker | 4ee5ad7 | 2014-02-18 18:41:17 -0600 | [diff] [blame] | 621 | if (!alloc_ion_mem(fi.line_length * vi.yres)) |
| 622 | allocate_overlay(gr_fb_fd, gr_framebuffer); |
| 623 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 624 | return 0; |
| 625 | } |
| 626 | |
| 627 | void gr_exit(void) |
| 628 | { |
Ethan Yonker | 4ee5ad7 | 2014-02-18 18:41:17 -0600 | [diff] [blame] | 629 | free_overlay(gr_fb_fd); |
| 630 | free_ion_mem(); |
| 631 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 632 | close(gr_fb_fd); |
| 633 | gr_fb_fd = -1; |
| 634 | |
| 635 | free(gr_mem_surface.data); |
| 636 | |
| 637 | ioctl(gr_vt_fd, KDSETMODE, (void*) KD_TEXT); |
| 638 | close(gr_vt_fd); |
| 639 | gr_vt_fd = -1; |
| 640 | } |
| 641 | |
| 642 | int gr_fb_width(void) |
| 643 | { |
| 644 | return gr_framebuffer[0].width; |
| 645 | } |
| 646 | |
| 647 | int gr_fb_height(void) |
| 648 | { |
| 649 | return gr_framebuffer[0].height; |
| 650 | } |
| 651 | |
| 652 | gr_pixel *gr_fb_data(void) |
| 653 | { |
| 654 | return (unsigned short *) gr_mem_surface.data; |
| 655 | } |
| 656 | |
Dees_Troy | 70237dc | 2013-02-28 21:31:48 +0000 | [diff] [blame] | 657 | int gr_fb_blank(int blank) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 658 | { |
| 659 | int ret; |
Dees Troy | 05d18c9 | 2014-08-04 18:17:07 +0000 | [diff] [blame] | 660 | //if (blank) |
| 661 | //free_overlay(gr_fb_fd); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 662 | |
| 663 | ret = ioctl(gr_fb_fd, FBIOBLANK, blank ? FB_BLANK_POWERDOWN : FB_BLANK_UNBLANK); |
| 664 | if (ret < 0) |
| 665 | perror("ioctl(): blank"); |
Ethan Yonker | 4ee5ad7 | 2014-02-18 18:41:17 -0600 | [diff] [blame] | 666 | |
Dees Troy | 05d18c9 | 2014-08-04 18:17:07 +0000 | [diff] [blame] | 667 | //if (!blank) |
| 668 | //allocate_overlay(gr_fb_fd, gr_framebuffer); |
| 669 | return ret; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | int gr_get_surface(gr_surface* surface) |
| 673 | { |
| 674 | GGLSurface* ms = malloc(sizeof(GGLSurface)); |
| 675 | if (!ms) return -1; |
| 676 | |
| 677 | // Allocate the data |
| 678 | get_memory_surface(ms); |
| 679 | |
| 680 | // Now, copy the data |
| 681 | memcpy(ms->data, gr_mem_surface.data, vi.xres * vi.yres * vi.bits_per_pixel / 8); |
| 682 | |
| 683 | *surface = (gr_surface*) ms; |
| 684 | return 0; |
| 685 | } |
| 686 | |
| 687 | int gr_free_surface(gr_surface surface) |
| 688 | { |
| 689 | if (!surface) |
| 690 | return -1; |
| 691 | |
| 692 | GGLSurface* ms = (GGLSurface*) surface; |
| 693 | free(ms->data); |
| 694 | free(ms); |
| 695 | return 0; |
| 696 | } |
| 697 | |
| 698 | void gr_write_frame_to_file(int fd) |
| 699 | { |
| 700 | write(fd, gr_mem_surface.data, vi.xres * vi.yres * vi.bits_per_pixel / 8); |
| 701 | } |