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