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