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