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