The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [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 | |
Dima Zavin | 4daf48a | 2011-08-30 11:59:20 -0700 | [diff] [blame] | 17 | #include <stdbool.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 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 "font_10x18.h" |
| 34 | #include "minui.h" |
| 35 | |
Michael Ward | 9d1bcdf | 2011-06-22 14:30:34 -0700 | [diff] [blame] | 36 | #if defined(RECOVERY_BGRA) |
| 37 | #define PIXEL_FORMAT GGL_PIXEL_FORMAT_BGRA_8888 |
| 38 | #define PIXEL_SIZE 4 |
| 39 | #elif defined(RECOVERY_RGBX) |
Doug Zongker | be3e6f1 | 2011-01-13 16:43:44 -0800 | [diff] [blame] | 40 | #define PIXEL_FORMAT GGL_PIXEL_FORMAT_RGBX_8888 |
| 41 | #define PIXEL_SIZE 4 |
| 42 | #else |
| 43 | #define PIXEL_FORMAT GGL_PIXEL_FORMAT_RGB_565 |
| 44 | #define PIXEL_SIZE 2 |
| 45 | #endif |
| 46 | |
Devin Kim | 862d026 | 2012-07-19 10:47:34 -0700 | [diff] [blame] | 47 | #define NUM_BUFFERS 2 |
| 48 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 49 | typedef struct { |
| 50 | GGLSurface texture; |
| 51 | unsigned cwidth; |
| 52 | unsigned cheight; |
| 53 | unsigned ascent; |
| 54 | } GRFont; |
| 55 | |
| 56 | static GRFont *gr_font = 0; |
| 57 | static GGLContext *gr_context = 0; |
| 58 | static GGLSurface gr_font_texture; |
Devin Kim | 862d026 | 2012-07-19 10:47:34 -0700 | [diff] [blame] | 59 | static GGLSurface gr_framebuffer[NUM_BUFFERS]; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 60 | static GGLSurface gr_mem_surface; |
| 61 | static unsigned gr_active_fb = 0; |
Octavian Purdila | 0e34880 | 2011-07-01 17:57:45 +0300 | [diff] [blame] | 62 | static unsigned double_buffering = 0; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 63 | |
| 64 | static int gr_fb_fd = -1; |
| 65 | static int gr_vt_fd = -1; |
| 66 | |
| 67 | static struct fb_var_screeninfo vi; |
Michael Ward | 9d1bcdf | 2011-06-22 14:30:34 -0700 | [diff] [blame] | 68 | static struct fb_fix_screeninfo fi; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 69 | |
| 70 | static int get_framebuffer(GGLSurface *fb) |
| 71 | { |
| 72 | int fd; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 73 | void *bits; |
| 74 | |
| 75 | fd = open("/dev/graphics/fb0", O_RDWR); |
| 76 | if (fd < 0) { |
| 77 | perror("cannot open fb0"); |
| 78 | return -1; |
| 79 | } |
| 80 | |
Michael Ward | 3dbe66b | 2011-06-23 19:28:53 -0700 | [diff] [blame] | 81 | if (ioctl(fd, FBIOGET_VSCREENINFO, &vi) < 0) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 82 | perror("failed to get fb0 info"); |
| 83 | close(fd); |
| 84 | return -1; |
| 85 | } |
| 86 | |
Michael Ward | 3dbe66b | 2011-06-23 19:28:53 -0700 | [diff] [blame] | 87 | vi.bits_per_pixel = PIXEL_SIZE * 8; |
| 88 | if (PIXEL_FORMAT == GGL_PIXEL_FORMAT_BGRA_8888) { |
| 89 | vi.red.offset = 8; |
| 90 | vi.red.length = 8; |
| 91 | vi.green.offset = 16; |
| 92 | vi.green.length = 8; |
| 93 | vi.blue.offset = 24; |
| 94 | vi.blue.length = 8; |
| 95 | vi.transp.offset = 0; |
| 96 | vi.transp.length = 8; |
| 97 | } else if (PIXEL_FORMAT == GGL_PIXEL_FORMAT_RGBX_8888) { |
| 98 | vi.red.offset = 24; |
| 99 | vi.red.length = 8; |
| 100 | vi.green.offset = 16; |
| 101 | vi.green.length = 8; |
| 102 | vi.blue.offset = 8; |
| 103 | vi.blue.length = 8; |
| 104 | vi.transp.offset = 0; |
| 105 | vi.transp.length = 8; |
| 106 | } else { /* RGB565*/ |
| 107 | vi.red.offset = 11; |
| 108 | vi.red.length = 5; |
| 109 | vi.green.offset = 5; |
| 110 | vi.green.length = 6; |
| 111 | vi.blue.offset = 0; |
| 112 | vi.blue.length = 5; |
| 113 | vi.transp.offset = 0; |
| 114 | vi.transp.length = 0; |
| 115 | } |
| 116 | if (ioctl(fd, FBIOPUT_VSCREENINFO, &vi) < 0) { |
| 117 | perror("failed to put fb0 info"); |
| 118 | close(fd); |
| 119 | return -1; |
| 120 | } |
| 121 | |
| 122 | if (ioctl(fd, FBIOGET_FSCREENINFO, &fi) < 0) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 123 | perror("failed to get fb0 info"); |
| 124 | close(fd); |
| 125 | return -1; |
| 126 | } |
| 127 | |
| 128 | bits = mmap(0, fi.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); |
| 129 | if (bits == MAP_FAILED) { |
| 130 | perror("failed to mmap framebuffer"); |
| 131 | close(fd); |
| 132 | return -1; |
| 133 | } |
| 134 | |
| 135 | fb->version = sizeof(*fb); |
| 136 | fb->width = vi.xres; |
| 137 | fb->height = vi.yres; |
Michael Ward | 9d1bcdf | 2011-06-22 14:30:34 -0700 | [diff] [blame] | 138 | fb->stride = fi.line_length/PIXEL_SIZE; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 139 | fb->data = bits; |
Doug Zongker | be3e6f1 | 2011-01-13 16:43:44 -0800 | [diff] [blame] | 140 | fb->format = PIXEL_FORMAT; |
Michael Ward | 9d1bcdf | 2011-06-22 14:30:34 -0700 | [diff] [blame] | 141 | memset(fb->data, 0, vi.yres * fi.line_length); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 142 | |
| 143 | fb++; |
| 144 | |
Octavian Purdila | 0e34880 | 2011-07-01 17:57:45 +0300 | [diff] [blame] | 145 | /* check if we can use double buffering */ |
| 146 | if (vi.yres * fi.line_length * 2 > fi.smem_len) |
| 147 | return fd; |
| 148 | |
| 149 | double_buffering = 1; |
| 150 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 151 | fb->version = sizeof(*fb); |
| 152 | fb->width = vi.xres; |
| 153 | fb->height = vi.yres; |
Michael Ward | 9d1bcdf | 2011-06-22 14:30:34 -0700 | [diff] [blame] | 154 | fb->stride = fi.line_length/PIXEL_SIZE; |
| 155 | fb->data = (void*) (((unsigned) bits) + vi.yres * fi.line_length); |
Doug Zongker | be3e6f1 | 2011-01-13 16:43:44 -0800 | [diff] [blame] | 156 | fb->format = PIXEL_FORMAT; |
Michael Ward | 9d1bcdf | 2011-06-22 14:30:34 -0700 | [diff] [blame] | 157 | memset(fb->data, 0, vi.yres * fi.line_length); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 158 | |
| 159 | return fd; |
| 160 | } |
| 161 | |
| 162 | static void get_memory_surface(GGLSurface* ms) { |
| 163 | ms->version = sizeof(*ms); |
| 164 | ms->width = vi.xres; |
| 165 | ms->height = vi.yres; |
Michael Ward | 9d1bcdf | 2011-06-22 14:30:34 -0700 | [diff] [blame] | 166 | ms->stride = fi.line_length/PIXEL_SIZE; |
| 167 | ms->data = malloc(fi.line_length * vi.yres); |
Doug Zongker | be3e6f1 | 2011-01-13 16:43:44 -0800 | [diff] [blame] | 168 | ms->format = PIXEL_FORMAT; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | static void set_active_framebuffer(unsigned n) |
| 172 | { |
Octavian Purdila | 0e34880 | 2011-07-01 17:57:45 +0300 | [diff] [blame] | 173 | if (n > 1 || !double_buffering) return; |
Devin Kim | 862d026 | 2012-07-19 10:47:34 -0700 | [diff] [blame] | 174 | vi.yres_virtual = vi.yres * NUM_BUFFERS; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 175 | vi.yoffset = n * vi.yres; |
Doug Zongker | be3e6f1 | 2011-01-13 16:43:44 -0800 | [diff] [blame] | 176 | vi.bits_per_pixel = PIXEL_SIZE * 8; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 177 | if (ioctl(gr_fb_fd, FBIOPUT_VSCREENINFO, &vi) < 0) { |
| 178 | perror("active fb swap failed"); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | void gr_flip(void) |
| 183 | { |
| 184 | GGLContext *gl = gr_context; |
| 185 | |
| 186 | /* swap front and back buffers */ |
Octavian Purdila | 0e34880 | 2011-07-01 17:57:45 +0300 | [diff] [blame] | 187 | if (double_buffering) |
| 188 | gr_active_fb = (gr_active_fb + 1) & 1; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 189 | |
| 190 | /* copy data from the in-memory surface to the buffer we're about |
| 191 | * to make active. */ |
| 192 | memcpy(gr_framebuffer[gr_active_fb].data, gr_mem_surface.data, |
Michael Ward | 9d1bcdf | 2011-06-22 14:30:34 -0700 | [diff] [blame] | 193 | fi.line_length * vi.yres); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 194 | |
| 195 | /* inform the display driver */ |
| 196 | set_active_framebuffer(gr_active_fb); |
| 197 | } |
| 198 | |
| 199 | void gr_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a) |
| 200 | { |
| 201 | GGLContext *gl = gr_context; |
| 202 | GGLint color[4]; |
| 203 | color[0] = ((r << 8) | r) + 1; |
| 204 | color[1] = ((g << 8) | g) + 1; |
| 205 | color[2] = ((b << 8) | b) + 1; |
| 206 | color[3] = ((a << 8) | a) + 1; |
| 207 | gl->color4xv(gl, color); |
| 208 | } |
| 209 | |
| 210 | int gr_measure(const char *s) |
| 211 | { |
| 212 | return gr_font->cwidth * strlen(s); |
| 213 | } |
| 214 | |
Dima Zavin | 3c7f00e | 2011-08-30 11:58:24 -0700 | [diff] [blame] | 215 | void gr_font_size(int *x, int *y) |
| 216 | { |
| 217 | *x = gr_font->cwidth; |
| 218 | *y = gr_font->cheight; |
| 219 | } |
| 220 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 221 | int gr_text(int x, int y, const char *s) |
| 222 | { |
| 223 | GGLContext *gl = gr_context; |
| 224 | GRFont *font = gr_font; |
| 225 | unsigned off; |
| 226 | |
| 227 | y -= font->ascent; |
| 228 | |
| 229 | gl->bindTexture(gl, &font->texture); |
| 230 | gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE); |
| 231 | gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE); |
| 232 | gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE); |
| 233 | gl->enable(gl, GGL_TEXTURE_2D); |
| 234 | |
| 235 | while((off = *s++)) { |
| 236 | off -= 32; |
| 237 | if (off < 96) { |
| 238 | gl->texCoord2i(gl, (off * font->cwidth) - x, 0 - y); |
| 239 | gl->recti(gl, x, y, x + font->cwidth, y + font->cheight); |
| 240 | } |
| 241 | x += font->cwidth; |
| 242 | } |
| 243 | |
| 244 | return x; |
| 245 | } |
| 246 | |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 247 | void gr_texticon(int x, int y, gr_surface icon) { |
| 248 | GGLContext* gl = gr_context; |
| 249 | |
| 250 | gl->bindTexture(gl, (GGLSurface*) icon); |
| 251 | gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE); |
| 252 | gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE); |
| 253 | gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE); |
| 254 | gl->enable(gl, GGL_TEXTURE_2D); |
| 255 | |
| 256 | int w = gr_get_width(icon); |
| 257 | int h = gr_get_height(icon); |
| 258 | |
| 259 | gl->texCoord2i(gl, -x, -y); |
| 260 | gl->recti(gl, x, y, x+gr_get_width(icon), y+gr_get_height(icon)); |
| 261 | } |
| 262 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 263 | void gr_fill(int x, int y, int w, int h) |
| 264 | { |
| 265 | GGLContext *gl = gr_context; |
| 266 | gl->disable(gl, GGL_TEXTURE_2D); |
| 267 | gl->recti(gl, x, y, w, h); |
| 268 | } |
| 269 | |
| 270 | void gr_blit(gr_surface source, int sx, int sy, int w, int h, int dx, int dy) { |
| 271 | if (gr_context == NULL) { |
| 272 | return; |
| 273 | } |
| 274 | GGLContext *gl = gr_context; |
| 275 | |
| 276 | gl->bindTexture(gl, (GGLSurface*) source); |
| 277 | gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE); |
| 278 | gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE); |
| 279 | gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE); |
| 280 | gl->enable(gl, GGL_TEXTURE_2D); |
| 281 | gl->texCoord2i(gl, sx - dx, sy - dy); |
| 282 | gl->recti(gl, dx, dy, dx + w, dy + h); |
| 283 | } |
| 284 | |
| 285 | unsigned int gr_get_width(gr_surface surface) { |
| 286 | if (surface == NULL) { |
| 287 | return 0; |
| 288 | } |
| 289 | return ((GGLSurface*) surface)->width; |
| 290 | } |
| 291 | |
| 292 | unsigned int gr_get_height(gr_surface surface) { |
| 293 | if (surface == NULL) { |
| 294 | return 0; |
| 295 | } |
| 296 | return ((GGLSurface*) surface)->height; |
| 297 | } |
| 298 | |
| 299 | static void gr_init_font(void) |
| 300 | { |
| 301 | GGLSurface *ftex; |
| 302 | unsigned char *bits, *rle; |
| 303 | unsigned char *in, data; |
| 304 | |
| 305 | gr_font = calloc(sizeof(*gr_font), 1); |
| 306 | ftex = &gr_font->texture; |
| 307 | |
| 308 | bits = malloc(font.width * font.height); |
| 309 | |
| 310 | ftex->version = sizeof(*ftex); |
| 311 | ftex->width = font.width; |
| 312 | ftex->height = font.height; |
| 313 | ftex->stride = font.width; |
| 314 | ftex->data = (void*) bits; |
| 315 | ftex->format = GGL_PIXEL_FORMAT_A_8; |
| 316 | |
| 317 | in = font.rundata; |
| 318 | while((data = *in++)) { |
| 319 | memset(bits, (data & 0x80) ? 255 : 0, data & 0x7f); |
| 320 | bits += (data & 0x7f); |
| 321 | } |
| 322 | |
| 323 | gr_font->cwidth = font.cwidth; |
| 324 | gr_font->cheight = font.cheight; |
| 325 | gr_font->ascent = font.cheight - 2; |
| 326 | } |
| 327 | |
| 328 | int gr_init(void) |
| 329 | { |
| 330 | gglInit(&gr_context); |
| 331 | GGLContext *gl = gr_context; |
| 332 | |
| 333 | gr_init_font(); |
| 334 | gr_vt_fd = open("/dev/tty0", O_RDWR | O_SYNC); |
| 335 | if (gr_vt_fd < 0) { |
| 336 | // This is non-fatal; post-Cupcake kernels don't have tty0. |
| 337 | perror("can't open /dev/tty0"); |
| 338 | } else if (ioctl(gr_vt_fd, KDSETMODE, (void*) KD_GRAPHICS)) { |
| 339 | // However, if we do open tty0, we expect the ioctl to work. |
| 340 | perror("failed KDSETMODE to KD_GRAPHICS on tty0"); |
| 341 | gr_exit(); |
| 342 | return -1; |
| 343 | } |
| 344 | |
| 345 | gr_fb_fd = get_framebuffer(gr_framebuffer); |
| 346 | if (gr_fb_fd < 0) { |
| 347 | gr_exit(); |
| 348 | return -1; |
| 349 | } |
| 350 | |
| 351 | get_memory_surface(&gr_mem_surface); |
| 352 | |
| 353 | fprintf(stderr, "framebuffer: fd %d (%d x %d)\n", |
| 354 | gr_fb_fd, gr_framebuffer[0].width, gr_framebuffer[0].height); |
| 355 | |
| 356 | /* start with 0 as front (displayed) and 1 as back (drawing) */ |
| 357 | gr_active_fb = 0; |
| 358 | set_active_framebuffer(0); |
| 359 | gl->colorBuffer(gl, &gr_mem_surface); |
| 360 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 361 | gl->activeTexture(gl, 0); |
| 362 | gl->enable(gl, GGL_BLEND); |
| 363 | gl->blendFunc(gl, GGL_SRC_ALPHA, GGL_ONE_MINUS_SRC_ALPHA); |
| 364 | |
Doug Zongker | f6abd40 | 2011-09-27 13:09:48 -0700 | [diff] [blame] | 365 | gr_fb_blank(true); |
| 366 | gr_fb_blank(false); |
| 367 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 368 | return 0; |
| 369 | } |
| 370 | |
| 371 | void gr_exit(void) |
| 372 | { |
| 373 | close(gr_fb_fd); |
| 374 | gr_fb_fd = -1; |
| 375 | |
| 376 | free(gr_mem_surface.data); |
| 377 | |
| 378 | ioctl(gr_vt_fd, KDSETMODE, (void*) KD_TEXT); |
| 379 | close(gr_vt_fd); |
| 380 | gr_vt_fd = -1; |
| 381 | } |
| 382 | |
| 383 | int gr_fb_width(void) |
| 384 | { |
| 385 | return gr_framebuffer[0].width; |
| 386 | } |
| 387 | |
| 388 | int gr_fb_height(void) |
| 389 | { |
| 390 | return gr_framebuffer[0].height; |
| 391 | } |
| 392 | |
| 393 | gr_pixel *gr_fb_data(void) |
| 394 | { |
| 395 | return (unsigned short *) gr_mem_surface.data; |
| 396 | } |
Dima Zavin | 4daf48a | 2011-08-30 11:59:20 -0700 | [diff] [blame] | 397 | |
| 398 | void gr_fb_blank(bool blank) |
| 399 | { |
| 400 | int ret; |
| 401 | |
| 402 | ret = ioctl(gr_fb_fd, FBIOBLANK, blank ? FB_BLANK_POWERDOWN : FB_BLANK_UNBLANK); |
| 403 | if (ret < 0) |
| 404 | perror("ioctl(): blank"); |
| 405 | } |