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 | { |
| 82 | LOGI("vi.xres: %d\n", vi.xres); |
| 83 | LOGI("vi.yres: %d\n", vi.yres); |
| 84 | LOGI("vi.xres_virtual: %d\n", vi.xres_virtual); |
| 85 | LOGI("vi.yres_virtual: %d\n", vi.yres_virtual); |
| 86 | LOGI("vi.xoffset: %d\n", vi.xoffset); |
| 87 | LOGI("vi.yoffset: %d\n", vi.yoffset); |
| 88 | LOGI("vi.bits_per_pixel: %d\n", vi.bits_per_pixel); |
| 89 | LOGI("vi.grayscale: %d\n", vi.grayscale); |
| 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 |
| 194 | LOGI("setting JANKY BACKBUFFER\n"); |
| 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; |
| 262 | for (i = 1; i < (vi.xres * vi.yres); i++) { |
| 263 | unsigned short tmp = gr_mem_surface.data[i]; |
| 264 | gr_mem_surface.data[i] = gr_mem_surface.data[(vi.xres * vi.yres * 2) - i]; |
| 265 | gr_mem_surface.data[(vi.xres * vi.yres * 2) - i] = tmp; |
| 266 | } |
| 267 | #endif |
| 268 | |
| 269 | /* copy data from the in-memory surface to the buffer we're about |
| 270 | * to make active. */ |
| 271 | memcpy(gr_framebuffer[gr_active_fb].data, gr_mem_surface.data, |
| 272 | vi.xres_virtual * vi.yres * PIXEL_SIZE); |
| 273 | |
| 274 | /* inform the display driver */ |
| 275 | set_active_framebuffer(gr_active_fb); |
| 276 | } |
| 277 | |
| 278 | void gr_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a) |
| 279 | { |
| 280 | GGLContext *gl = gr_context; |
| 281 | GGLint color[4]; |
| 282 | color[0] = ((r << 8) | r) + 1; |
| 283 | color[1] = ((g << 8) | g) + 1; |
| 284 | color[2] = ((b << 8) | b) + 1; |
| 285 | color[3] = ((a << 8) | a) + 1; |
| 286 | gl->color4xv(gl, color); |
| 287 | } |
| 288 | |
| 289 | int gr_measureEx(const char *s, void* font) |
| 290 | { |
| 291 | GRFont* fnt = (GRFont*) font; |
| 292 | int total = 0; |
| 293 | unsigned pos; |
| 294 | unsigned off; |
| 295 | |
| 296 | if (!fnt) fnt = gr_font; |
| 297 | |
| 298 | while ((off = *s++)) |
| 299 | { |
| 300 | off -= 32; |
| 301 | if (off < 96) |
| 302 | total += (fnt->offset[off+1] - fnt->offset[off]); |
| 303 | } |
| 304 | return total; |
| 305 | } |
| 306 | |
| 307 | unsigned character_width(const char *s, void* pFont) |
| 308 | { |
| 309 | GRFont *font = (GRFont*) pFont; |
| 310 | unsigned off; |
| 311 | |
| 312 | /* Handle default font */ |
| 313 | if (!font) font = gr_font; |
| 314 | |
| 315 | off = *s - 32; |
| 316 | if (off == 0) |
| 317 | return 0; |
| 318 | |
| 319 | return font->offset[off+1] - font->offset[off]; |
| 320 | } |
| 321 | |
| 322 | int gr_textEx(int x, int y, const char *s, void* pFont) |
| 323 | { |
| 324 | GGLContext *gl = gr_context; |
| 325 | GRFont *font = (GRFont*) pFont; |
| 326 | unsigned off; |
| 327 | unsigned cwidth; |
| 328 | |
| 329 | /* Handle default font */ |
| 330 | if (!font) font = gr_font; |
| 331 | |
| 332 | gl->bindTexture(gl, &font->texture); |
| 333 | gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE); |
| 334 | gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE); |
| 335 | gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE); |
| 336 | gl->enable(gl, GGL_TEXTURE_2D); |
| 337 | |
| 338 | while((off = *s++)) { |
| 339 | off -= 32; |
| 340 | cwidth = 0; |
| 341 | if (off < 96) { |
| 342 | cwidth = font->offset[off+1] - font->offset[off]; |
| 343 | gl->texCoord2i(gl, (font->offset[off]) - x, 0 - y); |
| 344 | gl->recti(gl, x, y, x + cwidth, y + font->cheight); |
| 345 | x += cwidth; |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | return x; |
| 350 | } |
| 351 | |
| 352 | int gr_textExW(int x, int y, const char *s, void* pFont, int max_width) |
| 353 | { |
| 354 | GGLContext *gl = gr_context; |
| 355 | GRFont *font = (GRFont*) pFont; |
| 356 | unsigned off; |
| 357 | unsigned cwidth; |
| 358 | |
| 359 | /* Handle default font */ |
| 360 | if (!font) font = gr_font; |
| 361 | |
| 362 | gl->bindTexture(gl, &font->texture); |
| 363 | gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE); |
| 364 | gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE); |
| 365 | gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE); |
| 366 | gl->enable(gl, GGL_TEXTURE_2D); |
| 367 | |
| 368 | while((off = *s++)) { |
| 369 | off -= 32; |
| 370 | cwidth = 0; |
| 371 | if (off < 96) { |
| 372 | cwidth = font->offset[off+1] - font->offset[off]; |
| 373 | if ((x + (int)cwidth) < max_width) { |
| 374 | gl->texCoord2i(gl, (font->offset[off]) - x, 0 - y); |
| 375 | gl->recti(gl, x, y, x + cwidth, y + font->cheight); |
| 376 | x += cwidth; |
| 377 | } else { |
| 378 | gl->texCoord2i(gl, (font->offset[off]) - x, 0 - y); |
| 379 | gl->recti(gl, x, y, max_width, y + font->cheight); |
| 380 | x = max_width; |
| 381 | return x; |
| 382 | } |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | return x; |
| 387 | } |
| 388 | |
| 389 | int gr_textExWH(int x, int y, const char *s, void* pFont, int max_width, int max_height) |
| 390 | { |
| 391 | GGLContext *gl = gr_context; |
| 392 | GRFont *font = (GRFont*) pFont; |
| 393 | unsigned off; |
| 394 | unsigned cwidth; |
| 395 | int rect_x, rect_y; |
| 396 | |
| 397 | /* Handle default font */ |
| 398 | if (!font) font = gr_font; |
| 399 | |
| 400 | gl->bindTexture(gl, &font->texture); |
| 401 | gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE); |
| 402 | gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE); |
| 403 | gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE); |
| 404 | gl->enable(gl, GGL_TEXTURE_2D); |
| 405 | |
| 406 | while((off = *s++)) { |
| 407 | off -= 32; |
| 408 | cwidth = 0; |
| 409 | if (off < 96) { |
| 410 | cwidth = font->offset[off+1] - font->offset[off]; |
| 411 | if ((x + (int)cwidth) < max_width) |
| 412 | rect_x = x + cwidth; |
| 413 | else |
| 414 | rect_x = max_width; |
Dees_Troy | f759675 | 2012-09-28 13:21:36 -0400 | [diff] [blame] | 415 | if (y + font->cheight < (unsigned int)(max_height)) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 416 | rect_y = y + font->cheight; |
| 417 | else |
| 418 | rect_y = max_height; |
| 419 | |
| 420 | gl->texCoord2i(gl, (font->offset[off]) - x, 0 - y); |
| 421 | gl->recti(gl, x, y, rect_x, rect_y); |
| 422 | x += cwidth; |
| 423 | if (x > max_width) |
| 424 | return x; |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | return x; |
| 429 | } |
| 430 | |
| 431 | int twgr_text(int x, int y, const char *s) |
| 432 | { |
| 433 | GGLContext *gl = gr_context; |
| 434 | GRFont *font = gr_font; |
| 435 | unsigned off; |
Dees_Troy | f759675 | 2012-09-28 13:21:36 -0400 | [diff] [blame] | 436 | unsigned cwidth = 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 437 | |
| 438 | y -= font->ascent; |
| 439 | |
| 440 | gl->bindTexture(gl, &font->texture); |
| 441 | gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE); |
| 442 | gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE); |
| 443 | gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE); |
| 444 | gl->enable(gl, GGL_TEXTURE_2D); |
| 445 | |
| 446 | while((off = *s++)) { |
| 447 | off -= 32; |
| 448 | if (off < 96) { |
| 449 | cwidth = font->offset[off+1] - font->offset[off]; |
| 450 | gl->texCoord2i(gl, (off * cwidth) - x, 0 - y); |
| 451 | gl->recti(gl, x, y, x + cwidth, y + font->cheight); |
| 452 | } |
| 453 | x += cwidth; |
| 454 | } |
| 455 | |
| 456 | return x; |
| 457 | } |
| 458 | |
| 459 | void gr_fill(int x, int y, int w, int h) |
| 460 | { |
| 461 | GGLContext *gl = gr_context; |
| 462 | gl->disable(gl, GGL_TEXTURE_2D); |
| 463 | gl->recti(gl, x, y, x + w, y + h); |
| 464 | } |
| 465 | |
| 466 | void gr_blit(gr_surface source, int sx, int sy, int w, int h, int dx, int dy) { |
| 467 | if (gr_context == NULL) { |
| 468 | return; |
| 469 | } |
| 470 | |
| 471 | GGLContext *gl = gr_context; |
| 472 | gl->bindTexture(gl, (GGLSurface*) source); |
| 473 | gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE); |
| 474 | gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE); |
| 475 | gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE); |
| 476 | gl->enable(gl, GGL_TEXTURE_2D); |
| 477 | gl->texCoord2i(gl, sx - dx, sy - dy); |
| 478 | gl->recti(gl, dx, dy, dx + w, dy + h); |
| 479 | } |
| 480 | |
| 481 | unsigned int gr_get_width(gr_surface surface) { |
| 482 | if (surface == NULL) { |
| 483 | return 0; |
| 484 | } |
| 485 | return ((GGLSurface*) surface)->width; |
| 486 | } |
| 487 | |
| 488 | unsigned int gr_get_height(gr_surface surface) { |
| 489 | if (surface == NULL) { |
| 490 | return 0; |
| 491 | } |
| 492 | return ((GGLSurface*) surface)->height; |
| 493 | } |
| 494 | |
| 495 | void* gr_loadFont(const char* fontName) |
| 496 | { |
| 497 | int fd; |
| 498 | GRFont *font = 0; |
| 499 | GGLSurface *ftex; |
| 500 | unsigned char *bits, *rle; |
| 501 | unsigned char *in, data; |
| 502 | unsigned width, height; |
| 503 | unsigned element; |
| 504 | |
| 505 | fd = open(fontName, O_RDONLY); |
| 506 | if (fd == -1) |
| 507 | { |
| 508 | char tmp[128]; |
| 509 | |
| 510 | sprintf(tmp, "/res/fonts/%s.dat", fontName); |
| 511 | fd = open(tmp, O_RDONLY); |
| 512 | if (fd == -1) |
| 513 | return NULL; |
| 514 | } |
| 515 | |
| 516 | font = calloc(sizeof(*font), 1); |
| 517 | ftex = &font->texture; |
| 518 | |
| 519 | read(fd, &width, sizeof(unsigned)); |
| 520 | read(fd, &height, sizeof(unsigned)); |
| 521 | read(fd, font->offset, sizeof(unsigned) * 96); |
| 522 | font->offset[96] = width; |
| 523 | |
| 524 | bits = malloc(width * height); |
| 525 | memset(bits, 0, width * height); |
| 526 | |
| 527 | unsigned pos = 0; |
| 528 | while (pos < width * height) |
| 529 | { |
| 530 | int bit; |
| 531 | |
| 532 | read(fd, &data, 1); |
| 533 | for (bit = 0; bit < 8; bit++) |
| 534 | { |
| 535 | if (data & (1 << (7-bit))) bits[pos++] = 255; |
| 536 | else bits[pos++] = 0; |
| 537 | |
| 538 | if (pos == width * height) break; |
| 539 | } |
| 540 | } |
| 541 | close(fd); |
| 542 | |
| 543 | ftex->version = sizeof(*ftex); |
| 544 | ftex->width = width; |
| 545 | ftex->height = height; |
| 546 | ftex->stride = width; |
| 547 | ftex->data = (void*) bits; |
| 548 | ftex->format = GGL_PIXEL_FORMAT_A_8; |
| 549 | font->cheight = height; |
| 550 | font->ascent = height - 2; |
| 551 | return (void*) font; |
| 552 | } |
| 553 | |
| 554 | int gr_getFontDetails(void* font, unsigned* cheight, unsigned* maxwidth) |
| 555 | { |
| 556 | GRFont *fnt = (GRFont*) font; |
| 557 | |
| 558 | if (!fnt) fnt = gr_font; |
| 559 | if (!fnt) return -1; |
| 560 | |
| 561 | if (cheight) *cheight = fnt->cheight; |
| 562 | if (maxwidth) |
| 563 | { |
| 564 | int pos; |
| 565 | *maxwidth = 0; |
| 566 | for (pos = 0; pos < 96; pos++) |
| 567 | { |
| 568 | unsigned int width = fnt->offset[pos+1] - fnt->offset[pos]; |
| 569 | if (width > *maxwidth) |
| 570 | { |
| 571 | *maxwidth = width; |
| 572 | } |
| 573 | } |
| 574 | } |
| 575 | return 0; |
| 576 | } |
| 577 | |
| 578 | static void gr_init_font(void) |
| 579 | { |
| 580 | int fontRes; |
| 581 | GGLSurface *ftex; |
| 582 | unsigned char *bits, *rle; |
| 583 | unsigned char *in, data; |
| 584 | unsigned width, height; |
| 585 | unsigned element; |
| 586 | |
| 587 | gr_font = calloc(sizeof(*gr_font), 1); |
| 588 | ftex = &gr_font->texture; |
| 589 | |
| 590 | width = font.width; |
| 591 | height = font.height; |
| 592 | |
| 593 | bits = malloc(width * height); |
| 594 | rle = bits; |
| 595 | |
| 596 | in = font.rundata; |
| 597 | while((data = *in++)) |
| 598 | { |
| 599 | memset(rle, (data & 0x80) ? 255 : 0, data & 0x7f); |
| 600 | rle += (data & 0x7f); |
| 601 | } |
| 602 | for (element = 0; element < 97; element++) |
| 603 | { |
| 604 | gr_font->offset[element] = (element * font.cwidth); |
| 605 | } |
| 606 | |
| 607 | ftex->version = sizeof(*ftex); |
| 608 | ftex->width = width; |
| 609 | ftex->height = height; |
| 610 | ftex->stride = width; |
| 611 | ftex->data = (void*) bits; |
| 612 | ftex->format = GGL_PIXEL_FORMAT_A_8; |
| 613 | gr_font->cheight = height; |
| 614 | gr_font->ascent = height - 2; |
| 615 | return; |
| 616 | } |
| 617 | |
| 618 | int gr_init(void) |
| 619 | { |
| 620 | gglInit(&gr_context); |
| 621 | GGLContext *gl = gr_context; |
| 622 | |
| 623 | gr_init_font(); |
| 624 | gr_vt_fd = open("/dev/tty0", O_RDWR | O_SYNC); |
| 625 | if (gr_vt_fd < 0) { |
| 626 | // This is non-fatal; post-Cupcake kernels don't have tty0. |
| 627 | } else if (ioctl(gr_vt_fd, KDSETMODE, (void*) KD_GRAPHICS)) { |
| 628 | // However, if we do open tty0, we expect the ioctl to work. |
| 629 | perror("failed KDSETMODE to KD_GRAPHICS on tty0"); |
| 630 | gr_exit(); |
| 631 | return -1; |
| 632 | } |
| 633 | |
| 634 | gr_fb_fd = get_framebuffer(gr_framebuffer); |
| 635 | if (gr_fb_fd < 0) { |
| 636 | perror("Unable to get framebuffer.\n"); |
| 637 | gr_exit(); |
| 638 | return -1; |
| 639 | } |
| 640 | |
| 641 | get_memory_surface(&gr_mem_surface); |
| 642 | |
| 643 | fprintf(stderr, "framebuffer: fd %d (%d x %d)\n", |
| 644 | gr_fb_fd, gr_framebuffer[0].width, gr_framebuffer[0].height); |
| 645 | |
| 646 | /* start with 0 as front (displayed) and 1 as back (drawing) */ |
| 647 | gr_active_fb = 0; |
| 648 | set_active_framebuffer(0); |
| 649 | gl->colorBuffer(gl, &gr_mem_surface); |
| 650 | |
| 651 | gl->activeTexture(gl, 0); |
| 652 | gl->enable(gl, GGL_BLEND); |
| 653 | gl->blendFunc(gl, GGL_SRC_ALPHA, GGL_ONE_MINUS_SRC_ALPHA); |
| 654 | |
| 655 | // gr_fb_blank(true); |
| 656 | // gr_fb_blank(false); |
| 657 | |
| 658 | return 0; |
| 659 | } |
| 660 | |
| 661 | void gr_exit(void) |
| 662 | { |
| 663 | close(gr_fb_fd); |
| 664 | gr_fb_fd = -1; |
| 665 | |
| 666 | free(gr_mem_surface.data); |
| 667 | |
| 668 | ioctl(gr_vt_fd, KDSETMODE, (void*) KD_TEXT); |
| 669 | close(gr_vt_fd); |
| 670 | gr_vt_fd = -1; |
| 671 | } |
| 672 | |
| 673 | int gr_fb_width(void) |
| 674 | { |
| 675 | return gr_framebuffer[0].width; |
| 676 | } |
| 677 | |
| 678 | int gr_fb_height(void) |
| 679 | { |
| 680 | return gr_framebuffer[0].height; |
| 681 | } |
| 682 | |
| 683 | gr_pixel *gr_fb_data(void) |
| 684 | { |
| 685 | return (unsigned short *) gr_mem_surface.data; |
| 686 | } |
| 687 | |
| 688 | void gr_fb_blank(int blank) |
| 689 | { |
| 690 | int ret; |
| 691 | |
| 692 | ret = ioctl(gr_fb_fd, FBIOBLANK, blank ? FB_BLANK_POWERDOWN : FB_BLANK_UNBLANK); |
| 693 | if (ret < 0) |
| 694 | perror("ioctl(): blank"); |
| 695 | } |
| 696 | |
| 697 | int gr_get_surface(gr_surface* surface) |
| 698 | { |
| 699 | GGLSurface* ms = malloc(sizeof(GGLSurface)); |
| 700 | if (!ms) return -1; |
| 701 | |
| 702 | // Allocate the data |
| 703 | get_memory_surface(ms); |
| 704 | |
| 705 | // Now, copy the data |
| 706 | memcpy(ms->data, gr_mem_surface.data, vi.xres * vi.yres * vi.bits_per_pixel / 8); |
| 707 | |
| 708 | *surface = (gr_surface*) ms; |
| 709 | return 0; |
| 710 | } |
| 711 | |
| 712 | int gr_free_surface(gr_surface surface) |
| 713 | { |
| 714 | if (!surface) |
| 715 | return -1; |
| 716 | |
| 717 | GGLSurface* ms = (GGLSurface*) surface; |
| 718 | free(ms->data); |
| 719 | free(ms); |
| 720 | return 0; |
| 721 | } |
| 722 | |
| 723 | void gr_write_frame_to_file(int fd) |
| 724 | { |
| 725 | write(fd, gr_mem_surface.data, vi.xres * vi.yres * vi.bits_per_pixel / 8); |
| 726 | } |