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