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