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