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 | |
| 17 | #include <stdlib.h> |
| 18 | #include <unistd.h> |
| 19 | |
| 20 | #include <fcntl.h> |
| 21 | #include <stdio.h> |
| 22 | |
| 23 | #include <sys/ioctl.h> |
| 24 | #include <sys/mman.h> |
| 25 | #include <sys/types.h> |
| 26 | |
| 27 | #include <linux/fb.h> |
| 28 | #include <linux/kd.h> |
| 29 | |
| 30 | #include <pixelflinger/pixelflinger.h> |
| 31 | |
| 32 | #include "font_10x18.h" |
| 33 | #include "minui.h" |
| 34 | |
Michael Ward | 9d1bcdf | 2011-06-22 14:30:34 -0700 | [diff] [blame] | 35 | #if defined(RECOVERY_BGRA) |
| 36 | #define PIXEL_FORMAT GGL_PIXEL_FORMAT_BGRA_8888 |
| 37 | #define PIXEL_SIZE 4 |
| 38 | #elif defined(RECOVERY_RGBX) |
Doug Zongker | be3e6f1 | 2011-01-13 16:43:44 -0800 | [diff] [blame] | 39 | #define PIXEL_FORMAT GGL_PIXEL_FORMAT_RGBX_8888 |
| 40 | #define PIXEL_SIZE 4 |
| 41 | #else |
| 42 | #define PIXEL_FORMAT GGL_PIXEL_FORMAT_RGB_565 |
| 43 | #define PIXEL_SIZE 2 |
| 44 | #endif |
| 45 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 46 | typedef struct { |
| 47 | GGLSurface texture; |
| 48 | unsigned cwidth; |
| 49 | unsigned cheight; |
| 50 | unsigned ascent; |
| 51 | } GRFont; |
| 52 | |
| 53 | static GRFont *gr_font = 0; |
| 54 | static GGLContext *gr_context = 0; |
| 55 | static GGLSurface gr_font_texture; |
| 56 | static GGLSurface gr_framebuffer[2]; |
| 57 | static GGLSurface gr_mem_surface; |
| 58 | static unsigned gr_active_fb = 0; |
| 59 | |
| 60 | static int gr_fb_fd = -1; |
| 61 | static int gr_vt_fd = -1; |
| 62 | |
| 63 | static struct fb_var_screeninfo vi; |
Michael Ward | 9d1bcdf | 2011-06-22 14:30:34 -0700 | [diff] [blame] | 64 | static struct fb_fix_screeninfo fi; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 65 | |
| 66 | static int get_framebuffer(GGLSurface *fb) |
| 67 | { |
| 68 | int fd; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 69 | void *bits; |
| 70 | |
| 71 | fd = open("/dev/graphics/fb0", O_RDWR); |
| 72 | if (fd < 0) { |
| 73 | perror("cannot open fb0"); |
| 74 | return -1; |
| 75 | } |
| 76 | |
| 77 | if (ioctl(fd, FBIOGET_FSCREENINFO, &fi) < 0) { |
| 78 | perror("failed to get fb0 info"); |
| 79 | close(fd); |
| 80 | return -1; |
| 81 | } |
| 82 | |
| 83 | if (ioctl(fd, FBIOGET_VSCREENINFO, &vi) < 0) { |
| 84 | perror("failed to get fb0 info"); |
| 85 | close(fd); |
| 86 | return -1; |
| 87 | } |
| 88 | |
| 89 | bits = mmap(0, fi.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); |
| 90 | if (bits == MAP_FAILED) { |
| 91 | perror("failed to mmap framebuffer"); |
| 92 | close(fd); |
| 93 | return -1; |
| 94 | } |
| 95 | |
| 96 | fb->version = sizeof(*fb); |
| 97 | fb->width = vi.xres; |
| 98 | fb->height = vi.yres; |
Michael Ward | 9d1bcdf | 2011-06-22 14:30:34 -0700 | [diff] [blame] | 99 | fb->stride = fi.line_length/PIXEL_SIZE; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 100 | fb->data = bits; |
Doug Zongker | be3e6f1 | 2011-01-13 16:43:44 -0800 | [diff] [blame] | 101 | fb->format = PIXEL_FORMAT; |
Michael Ward | 9d1bcdf | 2011-06-22 14:30:34 -0700 | [diff] [blame] | 102 | memset(fb->data, 0, vi.yres * fi.line_length); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 103 | |
| 104 | fb++; |
| 105 | |
| 106 | fb->version = sizeof(*fb); |
| 107 | fb->width = vi.xres; |
| 108 | fb->height = vi.yres; |
Michael Ward | 9d1bcdf | 2011-06-22 14:30:34 -0700 | [diff] [blame] | 109 | fb->stride = fi.line_length/PIXEL_SIZE; |
| 110 | fb->data = (void*) (((unsigned) bits) + vi.yres * fi.line_length); |
Doug Zongker | be3e6f1 | 2011-01-13 16:43:44 -0800 | [diff] [blame] | 111 | fb->format = PIXEL_FORMAT; |
Michael Ward | 9d1bcdf | 2011-06-22 14:30:34 -0700 | [diff] [blame] | 112 | memset(fb->data, 0, vi.yres * fi.line_length); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 113 | |
| 114 | return fd; |
| 115 | } |
| 116 | |
| 117 | static void get_memory_surface(GGLSurface* ms) { |
| 118 | ms->version = sizeof(*ms); |
| 119 | ms->width = vi.xres; |
| 120 | ms->height = vi.yres; |
Michael Ward | 9d1bcdf | 2011-06-22 14:30:34 -0700 | [diff] [blame] | 121 | ms->stride = fi.line_length/PIXEL_SIZE; |
| 122 | ms->data = malloc(fi.line_length * vi.yres); |
Doug Zongker | be3e6f1 | 2011-01-13 16:43:44 -0800 | [diff] [blame] | 123 | ms->format = PIXEL_FORMAT; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | static void set_active_framebuffer(unsigned n) |
| 127 | { |
| 128 | if (n > 1) return; |
Doug Zongker | be3e6f1 | 2011-01-13 16:43:44 -0800 | [diff] [blame] | 129 | vi.yres_virtual = vi.yres * PIXEL_SIZE; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 130 | vi.yoffset = n * vi.yres; |
Doug Zongker | be3e6f1 | 2011-01-13 16:43:44 -0800 | [diff] [blame] | 131 | vi.bits_per_pixel = PIXEL_SIZE * 8; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 132 | if (ioctl(gr_fb_fd, FBIOPUT_VSCREENINFO, &vi) < 0) { |
| 133 | perror("active fb swap failed"); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | void gr_flip(void) |
| 138 | { |
| 139 | GGLContext *gl = gr_context; |
| 140 | |
| 141 | /* swap front and back buffers */ |
| 142 | gr_active_fb = (gr_active_fb + 1) & 1; |
| 143 | |
| 144 | /* copy data from the in-memory surface to the buffer we're about |
| 145 | * to make active. */ |
| 146 | memcpy(gr_framebuffer[gr_active_fb].data, gr_mem_surface.data, |
Michael Ward | 9d1bcdf | 2011-06-22 14:30:34 -0700 | [diff] [blame] | 147 | fi.line_length * vi.yres); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 148 | |
| 149 | /* inform the display driver */ |
| 150 | set_active_framebuffer(gr_active_fb); |
| 151 | } |
| 152 | |
| 153 | void gr_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a) |
| 154 | { |
| 155 | GGLContext *gl = gr_context; |
| 156 | GGLint color[4]; |
| 157 | color[0] = ((r << 8) | r) + 1; |
| 158 | color[1] = ((g << 8) | g) + 1; |
| 159 | color[2] = ((b << 8) | b) + 1; |
| 160 | color[3] = ((a << 8) | a) + 1; |
| 161 | gl->color4xv(gl, color); |
| 162 | } |
| 163 | |
| 164 | int gr_measure(const char *s) |
| 165 | { |
| 166 | return gr_font->cwidth * strlen(s); |
| 167 | } |
| 168 | |
| 169 | int gr_text(int x, int y, const char *s) |
| 170 | { |
| 171 | GGLContext *gl = gr_context; |
| 172 | GRFont *font = gr_font; |
| 173 | unsigned off; |
| 174 | |
| 175 | y -= font->ascent; |
| 176 | |
| 177 | gl->bindTexture(gl, &font->texture); |
| 178 | gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE); |
| 179 | gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE); |
| 180 | gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE); |
| 181 | gl->enable(gl, GGL_TEXTURE_2D); |
| 182 | |
| 183 | while((off = *s++)) { |
| 184 | off -= 32; |
| 185 | if (off < 96) { |
| 186 | gl->texCoord2i(gl, (off * font->cwidth) - x, 0 - y); |
| 187 | gl->recti(gl, x, y, x + font->cwidth, y + font->cheight); |
| 188 | } |
| 189 | x += font->cwidth; |
| 190 | } |
| 191 | |
| 192 | return x; |
| 193 | } |
| 194 | |
| 195 | void gr_fill(int x, int y, int w, int h) |
| 196 | { |
| 197 | GGLContext *gl = gr_context; |
| 198 | gl->disable(gl, GGL_TEXTURE_2D); |
| 199 | gl->recti(gl, x, y, w, h); |
| 200 | } |
| 201 | |
| 202 | void gr_blit(gr_surface source, int sx, int sy, int w, int h, int dx, int dy) { |
| 203 | if (gr_context == NULL) { |
| 204 | return; |
| 205 | } |
| 206 | GGLContext *gl = gr_context; |
| 207 | |
| 208 | gl->bindTexture(gl, (GGLSurface*) source); |
| 209 | gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE); |
| 210 | gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE); |
| 211 | gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE); |
| 212 | gl->enable(gl, GGL_TEXTURE_2D); |
| 213 | gl->texCoord2i(gl, sx - dx, sy - dy); |
| 214 | gl->recti(gl, dx, dy, dx + w, dy + h); |
| 215 | } |
| 216 | |
| 217 | unsigned int gr_get_width(gr_surface surface) { |
| 218 | if (surface == NULL) { |
| 219 | return 0; |
| 220 | } |
| 221 | return ((GGLSurface*) surface)->width; |
| 222 | } |
| 223 | |
| 224 | unsigned int gr_get_height(gr_surface surface) { |
| 225 | if (surface == NULL) { |
| 226 | return 0; |
| 227 | } |
| 228 | return ((GGLSurface*) surface)->height; |
| 229 | } |
| 230 | |
| 231 | static void gr_init_font(void) |
| 232 | { |
| 233 | GGLSurface *ftex; |
| 234 | unsigned char *bits, *rle; |
| 235 | unsigned char *in, data; |
| 236 | |
| 237 | gr_font = calloc(sizeof(*gr_font), 1); |
| 238 | ftex = &gr_font->texture; |
| 239 | |
| 240 | bits = malloc(font.width * font.height); |
| 241 | |
| 242 | ftex->version = sizeof(*ftex); |
| 243 | ftex->width = font.width; |
| 244 | ftex->height = font.height; |
| 245 | ftex->stride = font.width; |
| 246 | ftex->data = (void*) bits; |
| 247 | ftex->format = GGL_PIXEL_FORMAT_A_8; |
| 248 | |
| 249 | in = font.rundata; |
| 250 | while((data = *in++)) { |
| 251 | memset(bits, (data & 0x80) ? 255 : 0, data & 0x7f); |
| 252 | bits += (data & 0x7f); |
| 253 | } |
| 254 | |
| 255 | gr_font->cwidth = font.cwidth; |
| 256 | gr_font->cheight = font.cheight; |
| 257 | gr_font->ascent = font.cheight - 2; |
| 258 | } |
| 259 | |
| 260 | int gr_init(void) |
| 261 | { |
| 262 | gglInit(&gr_context); |
| 263 | GGLContext *gl = gr_context; |
| 264 | |
| 265 | gr_init_font(); |
| 266 | gr_vt_fd = open("/dev/tty0", O_RDWR | O_SYNC); |
| 267 | if (gr_vt_fd < 0) { |
| 268 | // This is non-fatal; post-Cupcake kernels don't have tty0. |
| 269 | perror("can't open /dev/tty0"); |
| 270 | } else if (ioctl(gr_vt_fd, KDSETMODE, (void*) KD_GRAPHICS)) { |
| 271 | // However, if we do open tty0, we expect the ioctl to work. |
| 272 | perror("failed KDSETMODE to KD_GRAPHICS on tty0"); |
| 273 | gr_exit(); |
| 274 | return -1; |
| 275 | } |
| 276 | |
| 277 | gr_fb_fd = get_framebuffer(gr_framebuffer); |
| 278 | if (gr_fb_fd < 0) { |
| 279 | gr_exit(); |
| 280 | return -1; |
| 281 | } |
| 282 | |
| 283 | get_memory_surface(&gr_mem_surface); |
| 284 | |
| 285 | fprintf(stderr, "framebuffer: fd %d (%d x %d)\n", |
| 286 | gr_fb_fd, gr_framebuffer[0].width, gr_framebuffer[0].height); |
| 287 | |
| 288 | /* start with 0 as front (displayed) and 1 as back (drawing) */ |
| 289 | gr_active_fb = 0; |
| 290 | set_active_framebuffer(0); |
| 291 | gl->colorBuffer(gl, &gr_mem_surface); |
| 292 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 293 | gl->activeTexture(gl, 0); |
| 294 | gl->enable(gl, GGL_BLEND); |
| 295 | gl->blendFunc(gl, GGL_SRC_ALPHA, GGL_ONE_MINUS_SRC_ALPHA); |
| 296 | |
| 297 | return 0; |
| 298 | } |
| 299 | |
| 300 | void gr_exit(void) |
| 301 | { |
| 302 | close(gr_fb_fd); |
| 303 | gr_fb_fd = -1; |
| 304 | |
| 305 | free(gr_mem_surface.data); |
| 306 | |
| 307 | ioctl(gr_vt_fd, KDSETMODE, (void*) KD_TEXT); |
| 308 | close(gr_vt_fd); |
| 309 | gr_vt_fd = -1; |
| 310 | } |
| 311 | |
| 312 | int gr_fb_width(void) |
| 313 | { |
| 314 | return gr_framebuffer[0].width; |
| 315 | } |
| 316 | |
| 317 | int gr_fb_height(void) |
| 318 | { |
| 319 | return gr_framebuffer[0].height; |
| 320 | } |
| 321 | |
| 322 | gr_pixel *gr_fb_data(void) |
| 323 | { |
| 324 | return (unsigned short *) gr_mem_surface.data; |
| 325 | } |