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 | |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 32 | #include <time.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 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" |
Doug Zongker | 5290f20 | 2014-03-11 13:22:04 -0700 | [diff] [blame] | 36 | #include "graphics.h" |
Devin Kim | 862d026 | 2012-07-19 10:47:34 -0700 | [diff] [blame] | 37 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 38 | typedef struct { |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 39 | GRSurface* texture; |
| 40 | int cwidth; |
| 41 | int cheight; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 42 | } GRFont; |
| 43 | |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 44 | static GRFont* gr_font = NULL; |
Doug Zongker | 5290f20 | 2014-03-11 13:22:04 -0700 | [diff] [blame] | 45 | static minui_backend* gr_backend = NULL; |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 46 | |
Doug Zongker | c560a67 | 2012-12-18 16:31:27 -0800 | [diff] [blame] | 47 | static int overscan_percent = OVERSCAN_PERCENT; |
| 48 | static int overscan_offset_x = 0; |
| 49 | static int overscan_offset_y = 0; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 50 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 51 | static int gr_vt_fd = -1; |
| 52 | |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 53 | static unsigned char gr_current_r = 255; |
| 54 | static unsigned char gr_current_g = 255; |
| 55 | static unsigned char gr_current_b = 255; |
| 56 | static unsigned char gr_current_a = 255; |
| 57 | |
Doug Zongker | 5290f20 | 2014-03-11 13:22:04 -0700 | [diff] [blame] | 58 | static GRSurface* gr_draw = NULL; |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 59 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 60 | static struct fb_var_screeninfo vi; |
Michael Ward | 9d1bcdf | 2011-06-22 14:30:34 -0700 | [diff] [blame] | 61 | static struct fb_fix_screeninfo fi; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 62 | |
Dees Troy | 62b75ab | 2014-05-02 13:20:33 +0000 | [diff] [blame] | 63 | static bool has_overlay = false; |
| 64 | |
| 65 | bool target_has_overlay(char *version); |
| 66 | int free_ion_mem(void); |
| 67 | int alloc_ion_mem(unsigned int size); |
| 68 | int allocate_overlay(int fd, GGLSurface gr_fb[]); |
| 69 | int free_overlay(int fd); |
| 70 | int overlay_display_frame(int fd, GGLubyte* data, size_t size); |
| 71 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 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 | |
Dees Troy | 62b75ab | 2014-05-02 13:20:33 +0000 | [diff] [blame] | 130 | has_overlay = target_has_overlay(fi.id); |
| 131 | |
| 132 | if (!has_overlay) { |
| 133 | bits = mmap(0, fi.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); |
| 134 | if (bits == MAP_FAILED) { |
| 135 | perror("failed to mmap framebuffer"); |
| 136 | close(fd); |
| 137 | return -1; |
| 138 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 139 | } |
| 140 | |
Doug Zongker | c560a67 | 2012-12-18 16:31:27 -0800 | [diff] [blame] | 141 | overscan_offset_x = vi.xres * overscan_percent / 100; |
| 142 | overscan_offset_y = vi.yres * overscan_percent / 100; |
| 143 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 144 | fb->version = sizeof(*fb); |
| 145 | fb->width = vi.xres; |
| 146 | fb->height = vi.yres; |
Michael Ward | 9d1bcdf | 2011-06-22 14:30:34 -0700 | [diff] [blame] | 147 | fb->stride = fi.line_length/PIXEL_SIZE; |
Doug Zongker | be3e6f1 | 2011-01-13 16:43:44 -0800 | [diff] [blame] | 148 | fb->format = PIXEL_FORMAT; |
Dees Troy | 62b75ab | 2014-05-02 13:20:33 +0000 | [diff] [blame] | 149 | if (!has_overlay) { |
| 150 | fb->data = bits; |
| 151 | memset(fb->data, 0, vi.yres * fi.line_length); |
| 152 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 153 | |
| 154 | fb++; |
| 155 | |
Octavian Purdila | 0e34880 | 2011-07-01 17:57:45 +0300 | [diff] [blame] | 156 | /* check if we can use double buffering */ |
| 157 | if (vi.yres * fi.line_length * 2 > fi.smem_len) |
| 158 | return fd; |
| 159 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 160 | fb->version = sizeof(*fb); |
| 161 | fb->width = vi.xres; |
| 162 | fb->height = vi.yres; |
Michael Ward | 9d1bcdf | 2011-06-22 14:30:34 -0700 | [diff] [blame] | 163 | fb->stride = fi.line_length/PIXEL_SIZE; |
Doug Zongker | be3e6f1 | 2011-01-13 16:43:44 -0800 | [diff] [blame] | 164 | fb->format = PIXEL_FORMAT; |
Dees Troy | 62b75ab | 2014-05-02 13:20:33 +0000 | [diff] [blame] | 165 | if (!has_overlay) { |
| 166 | fb->data = (void*) (((unsigned) bits) + vi.yres * fi.line_length); |
| 167 | memset(fb->data, 0, vi.yres * fi.line_length); |
| 168 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 169 | |
| 170 | return fd; |
| 171 | } |
| 172 | |
| 173 | static void get_memory_surface(GGLSurface* ms) { |
| 174 | ms->version = sizeof(*ms); |
| 175 | ms->width = vi.xres; |
| 176 | ms->height = vi.yres; |
Michael Ward | 9d1bcdf | 2011-06-22 14:30:34 -0700 | [diff] [blame] | 177 | ms->stride = fi.line_length/PIXEL_SIZE; |
| 178 | ms->data = malloc(fi.line_length * vi.yres); |
Doug Zongker | be3e6f1 | 2011-01-13 16:43:44 -0800 | [diff] [blame] | 179 | ms->format = PIXEL_FORMAT; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | static void set_active_framebuffer(unsigned n) |
| 183 | { |
Octavian Purdila | 0e34880 | 2011-07-01 17:57:45 +0300 | [diff] [blame] | 184 | if (n > 1 || !double_buffering) return; |
Devin Kim | 862d026 | 2012-07-19 10:47:34 -0700 | [diff] [blame] | 185 | vi.yres_virtual = vi.yres * NUM_BUFFERS; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 186 | vi.yoffset = n * vi.yres; |
Doug Zongker | be3e6f1 | 2011-01-13 16:43:44 -0800 | [diff] [blame] | 187 | vi.bits_per_pixel = PIXEL_SIZE * 8; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 188 | if (ioctl(gr_fb_fd, FBIOPUT_VSCREENINFO, &vi) < 0) { |
| 189 | perror("active fb swap failed"); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | void gr_flip(void) |
| 194 | { |
Dees Troy | 62b75ab | 2014-05-02 13:20:33 +0000 | [diff] [blame] | 195 | if (-EINVAL == overlay_display_frame(gr_fb_fd, gr_mem_surface.data, |
| 196 | (fi.line_length * vi.yres))) { |
| 197 | GGLContext *gl = gr_context; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 198 | |
Dees Troy | 62b75ab | 2014-05-02 13:20:33 +0000 | [diff] [blame] | 199 | /* swap front and back buffers */ |
| 200 | if (double_buffering) |
| 201 | gr_active_fb = (gr_active_fb + 1) & 1; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 202 | |
Dees Troy | 62b75ab | 2014-05-02 13:20:33 +0000 | [diff] [blame] | 203 | /* copy data from the in-memory surface to the buffer we're about |
| 204 | * to make active. */ |
| 205 | memcpy(gr_framebuffer[gr_active_fb].data, gr_mem_surface.data, |
| 206 | fi.line_length * vi.yres); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 207 | |
Dees Troy | 62b75ab | 2014-05-02 13:20:33 +0000 | [diff] [blame] | 208 | /* inform the display driver */ |
| 209 | set_active_framebuffer(gr_active_fb); |
| 210 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | void gr_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a) |
| 214 | { |
| 215 | GGLContext *gl = gr_context; |
| 216 | GGLint color[4]; |
| 217 | color[0] = ((r << 8) | r) + 1; |
| 218 | color[1] = ((g << 8) | g) + 1; |
| 219 | color[2] = ((b << 8) | b) + 1; |
| 220 | color[3] = ((a << 8) | a) + 1; |
| 221 | gl->color4xv(gl, color); |
| 222 | } |
| 223 | |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 224 | static bool outside(int x, int y) |
| 225 | { |
| 226 | return x < 0 || x >= gr_draw->width || y < 0 || y >= gr_draw->height; |
| 227 | } |
| 228 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 229 | int gr_measure(const char *s) |
| 230 | { |
| 231 | return gr_font->cwidth * strlen(s); |
| 232 | } |
| 233 | |
Dima Zavin | 3c7f00e | 2011-08-30 11:58:24 -0700 | [diff] [blame] | 234 | void gr_font_size(int *x, int *y) |
| 235 | { |
| 236 | *x = gr_font->cwidth; |
| 237 | *y = gr_font->cheight; |
| 238 | } |
| 239 | |
Vojtech Bocek | 65fdcdd | 2013-09-06 21:32:22 +0200 | [diff] [blame] | 240 | int gr_text(int x, int y, const char *s, ...) |
| 241 | { |
| 242 | return gr_text_impl(x, y, s, 0); |
| 243 | } |
| 244 | |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 245 | static void text_blend(unsigned char* src_p, int src_row_bytes, |
| 246 | unsigned char* dst_p, int dst_row_bytes, |
| 247 | int width, int height) |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 248 | { |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 249 | int i, j; |
| 250 | for (j = 0; j < height; ++j) { |
| 251 | unsigned char* sx = src_p; |
| 252 | unsigned char* px = dst_p; |
| 253 | for (i = 0; i < width; ++i) { |
| 254 | unsigned char a = *sx++; |
| 255 | if (gr_current_a < 255) a = ((int)a * gr_current_a) / 255; |
| 256 | if (a == 255) { |
| 257 | *px++ = gr_current_r; |
| 258 | *px++ = gr_current_g; |
| 259 | *px++ = gr_current_b; |
| 260 | px++; |
| 261 | } else if (a > 0) { |
| 262 | *px = (*px * (255-a) + gr_current_r * a) / 255; |
| 263 | ++px; |
| 264 | *px = (*px * (255-a) + gr_current_g * a) / 255; |
| 265 | ++px; |
| 266 | *px = (*px * (255-a) + gr_current_b * a) / 255; |
| 267 | ++px; |
| 268 | ++px; |
| 269 | } else { |
| 270 | px += 4; |
| 271 | } |
| 272 | } |
| 273 | src_p += src_row_bytes; |
| 274 | dst_p += dst_row_bytes; |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | |
Vojtech Bocek | 65fdcdd | 2013-09-06 21:32:22 +0200 | [diff] [blame] | 279 | 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] | 280 | { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 281 | GRFont *font = gr_font; |
| 282 | unsigned off; |
| 283 | |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 284 | if (!font->texture) return; |
| 285 | if (gr_current_a == 0) return; |
Doug Zongker | 55a36ac | 2013-03-04 15:49:02 -0800 | [diff] [blame] | 286 | |
Doug Zongker | 6fd59ac | 2013-03-06 15:01:11 -0800 | [diff] [blame] | 287 | bold = bold && (font->texture->height != font->cheight); |
| 288 | |
Doug Zongker | c560a67 | 2012-12-18 16:31:27 -0800 | [diff] [blame] | 289 | x += overscan_offset_x; |
| 290 | y += overscan_offset_y; |
| 291 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 292 | while((off = *s++)) { |
| 293 | off -= 32; |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 294 | if (outside(x, y) || outside(x+font->cwidth-1, y+font->cheight-1)) break; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 295 | if (off < 96) { |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 296 | |
| 297 | unsigned char* src_p = font->texture->data + (off * font->cwidth) + |
| 298 | (bold ? font->cheight * font->texture->row_bytes : 0); |
| 299 | unsigned char* dst_p = gr_draw->data + y*gr_draw->row_bytes + x*gr_draw->pixel_bytes; |
| 300 | |
| 301 | text_blend(src_p, font->texture->row_bytes, |
| 302 | dst_p, gr_draw->row_bytes, |
| 303 | font->cwidth, font->cheight); |
| 304 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 305 | } |
| 306 | x += font->cwidth; |
| 307 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 308 | } |
| 309 | |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 310 | void gr_texticon(int x, int y, GRSurface* icon) { |
| 311 | if (icon == NULL) return; |
| 312 | |
| 313 | if (icon->pixel_bytes != 1) { |
| 314 | printf("gr_texticon: source has wrong format\n"); |
Doug Zongker | 52eeea4f | 2012-09-04 14:28:25 -0700 | [diff] [blame] | 315 | return; |
| 316 | } |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 317 | |
Doug Zongker | c560a67 | 2012-12-18 16:31:27 -0800 | [diff] [blame] | 318 | x += overscan_offset_x; |
| 319 | y += overscan_offset_y; |
| 320 | |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 321 | if (outside(x, y) || outside(x+icon->width-1, y+icon->height-1)) return; |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 322 | |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 323 | unsigned char* src_p = icon->data; |
| 324 | unsigned char* dst_p = gr_draw->data + y*gr_draw->row_bytes + x*gr_draw->pixel_bytes; |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 325 | |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 326 | text_blend(src_p, icon->row_bytes, |
| 327 | dst_p, gr_draw->row_bytes, |
| 328 | icon->width, icon->height); |
| 329 | } |
| 330 | |
| 331 | void gr_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a) |
| 332 | { |
| 333 | gr_current_r = r; |
| 334 | gr_current_g = g; |
| 335 | gr_current_b = b; |
| 336 | gr_current_a = a; |
| 337 | } |
| 338 | |
| 339 | void gr_clear() |
| 340 | { |
| 341 | if (gr_current_r == gr_current_g && |
| 342 | gr_current_r == gr_current_b) { |
| 343 | memset(gr_draw->data, gr_current_r, gr_draw->height * gr_draw->row_bytes); |
| 344 | } else { |
| 345 | int x, y; |
| 346 | unsigned char* px = gr_draw->data; |
| 347 | for (y = 0; y < gr_draw->height; ++y) { |
| 348 | for (x = 0; x < gr_draw->width; ++x) { |
| 349 | *px++ = gr_current_r; |
| 350 | *px++ = gr_current_g; |
| 351 | *px++ = gr_current_b; |
| 352 | px++; |
| 353 | } |
| 354 | px += gr_draw->row_bytes - (gr_draw->width * gr_draw->pixel_bytes); |
| 355 | } |
| 356 | } |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 357 | } |
| 358 | |
Doug Zongker | c560a67 | 2012-12-18 16:31:27 -0800 | [diff] [blame] | 359 | 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] | 360 | { |
Doug Zongker | c560a67 | 2012-12-18 16:31:27 -0800 | [diff] [blame] | 361 | x1 += overscan_offset_x; |
| 362 | y1 += overscan_offset_y; |
| 363 | |
| 364 | x2 += overscan_offset_x; |
| 365 | y2 += overscan_offset_y; |
| 366 | |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 367 | if (outside(x1, y1) || outside(x2-1, y2-1)) return; |
| 368 | |
| 369 | unsigned char* p = gr_draw->data + y1 * gr_draw->row_bytes + x1 * gr_draw->pixel_bytes; |
| 370 | if (gr_current_a == 255) { |
| 371 | int x, y; |
| 372 | for (y = y1; y < y2; ++y) { |
| 373 | unsigned char* px = p; |
| 374 | for (x = x1; x < x2; ++x) { |
| 375 | *px++ = gr_current_r; |
| 376 | *px++ = gr_current_g; |
| 377 | *px++ = gr_current_b; |
| 378 | px++; |
| 379 | } |
| 380 | p += gr_draw->row_bytes; |
| 381 | } |
| 382 | } else if (gr_current_a > 0) { |
| 383 | int x, y; |
| 384 | for (y = y1; y < y2; ++y) { |
| 385 | unsigned char* px = p; |
| 386 | for (x = x1; x < x2; ++x) { |
| 387 | *px = (*px * (255-gr_current_a) + gr_current_r * gr_current_a) / 255; |
| 388 | ++px; |
| 389 | *px = (*px * (255-gr_current_a) + gr_current_g * gr_current_a) / 255; |
| 390 | ++px; |
| 391 | *px = (*px * (255-gr_current_a) + gr_current_b * gr_current_a) / 255; |
| 392 | ++px; |
| 393 | ++px; |
| 394 | } |
| 395 | p += gr_draw->row_bytes; |
| 396 | } |
| 397 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 398 | } |
| 399 | |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 400 | void gr_blit(GRSurface* source, int sx, int sy, int w, int h, int dx, int dy) { |
| 401 | if (source == NULL) return; |
| 402 | |
| 403 | if (gr_draw->pixel_bytes != source->pixel_bytes) { |
| 404 | printf("gr_blit: source has wrong format\n"); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 405 | return; |
| 406 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 407 | |
Doug Zongker | c560a67 | 2012-12-18 16:31:27 -0800 | [diff] [blame] | 408 | dx += overscan_offset_x; |
| 409 | dy += overscan_offset_y; |
| 410 | |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 411 | if (outside(dx, dy) || outside(dx+w-1, dy+h-1)) return; |
| 412 | |
| 413 | unsigned char* src_p = source->data + sy*source->row_bytes + sx*source->pixel_bytes; |
| 414 | unsigned char* dst_p = gr_draw->data + dy*gr_draw->row_bytes + dx*gr_draw->pixel_bytes; |
| 415 | |
| 416 | int i; |
| 417 | for (i = 0; i < h; ++i) { |
| 418 | memcpy(dst_p, src_p, w * source->pixel_bytes); |
| 419 | src_p += source->row_bytes; |
| 420 | dst_p += gr_draw->row_bytes; |
| 421 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 422 | } |
| 423 | |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 424 | unsigned int gr_get_width(GRSurface* surface) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 425 | if (surface == NULL) { |
| 426 | return 0; |
| 427 | } |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 428 | return surface->width; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 429 | } |
| 430 | |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 431 | unsigned int gr_get_height(GRSurface* surface) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 432 | if (surface == NULL) { |
| 433 | return 0; |
| 434 | } |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 435 | return surface->height; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | static void gr_init_font(void) |
| 439 | { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 440 | gr_font = calloc(sizeof(*gr_font), 1); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 441 | |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 442 | int res = res_create_alpha_surface("font", &(gr_font->texture)); |
Doug Zongker | 6fd59ac | 2013-03-06 15:01:11 -0800 | [diff] [blame] | 443 | if (res == 0) { |
| 444 | // The font image should be a 96x2 array of character images. The |
| 445 | // columns are the printable ASCII characters 0x20 - 0x7f. The |
| 446 | // top row is regular text; the bottom row is bold. |
| 447 | gr_font->cwidth = gr_font->texture->width / 96; |
| 448 | gr_font->cheight = gr_font->texture->height / 2; |
| 449 | } else { |
Doug Zongker | 55a36ac | 2013-03-04 15:49:02 -0800 | [diff] [blame] | 450 | printf("failed to read font: res=%d\n", res); |
Doug Zongker | 6fd59ac | 2013-03-06 15:01:11 -0800 | [diff] [blame] | 451 | |
| 452 | // fall back to the compiled-in font. |
| 453 | gr_font->texture = malloc(sizeof(*gr_font->texture)); |
| 454 | gr_font->texture->width = font.width; |
| 455 | gr_font->texture->height = font.height; |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 456 | gr_font->texture->row_bytes = font.width; |
| 457 | gr_font->texture->pixel_bytes = 1; |
Doug Zongker | 6fd59ac | 2013-03-06 15:01:11 -0800 | [diff] [blame] | 458 | |
| 459 | unsigned char* bits = malloc(font.width * font.height); |
| 460 | gr_font->texture->data = (void*) bits; |
| 461 | |
| 462 | unsigned char data; |
| 463 | unsigned char* in = font.rundata; |
| 464 | while((data = *in++)) { |
| 465 | memset(bits, (data & 0x80) ? 255 : 0, data & 0x7f); |
| 466 | bits += (data & 0x7f); |
| 467 | } |
| 468 | |
| 469 | gr_font->cwidth = font.cwidth; |
| 470 | gr_font->cheight = font.cheight; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 471 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 472 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 473 | |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 474 | #if 0 |
| 475 | // Exercises many of the gr_*() functions; useful for testing. |
| 476 | static void gr_test() { |
| 477 | GRSurface** images; |
| 478 | int frames; |
| 479 | int result = res_create_multi_surface("icon_installing", &frames, &images); |
| 480 | if (result < 0) { |
| 481 | printf("create surface %d\n", result); |
| 482 | gr_exit(); |
| 483 | return; |
| 484 | } |
| 485 | |
| 486 | time_t start = time(NULL); |
| 487 | int x; |
| 488 | for (x = 0; x <= 1200; ++x) { |
| 489 | if (x < 400) { |
| 490 | gr_color(0, 0, 0, 255); |
| 491 | } else { |
| 492 | gr_color(0, (x-400)%128, 0, 255); |
| 493 | } |
| 494 | gr_clear(); |
| 495 | |
| 496 | gr_color(255, 0, 0, 255); |
| 497 | gr_surface frame = images[x%frames]; |
| 498 | gr_blit(frame, 0, 0, frame->width, frame->height, x, 0); |
| 499 | |
| 500 | gr_color(255, 0, 0, 128); |
| 501 | gr_fill(400, 150, 600, 350); |
| 502 | |
| 503 | gr_color(255, 255, 255, 255); |
| 504 | gr_text(500, 225, "hello, world!", 0); |
| 505 | gr_color(255, 255, 0, 128); |
| 506 | gr_text(300+x, 275, "pack my box with five dozen liquor jugs", 1); |
| 507 | |
| 508 | gr_color(0, 0, 255, 128); |
| 509 | gr_fill(gr_draw->width - 200 - x, 300, gr_draw->width - x, 500); |
| 510 | |
Doug Zongker | 5290f20 | 2014-03-11 13:22:04 -0700 | [diff] [blame] | 511 | gr_draw = gr_backend->flip(gr_backend); |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 512 | } |
| 513 | printf("getting end time\n"); |
| 514 | time_t end = time(NULL); |
| 515 | printf("got end time\n"); |
| 516 | printf("start %ld end %ld\n", (long)start, (long)end); |
| 517 | if (end > start) { |
| 518 | printf("%.2f fps\n", ((double)x) / (end-start)); |
| 519 | } |
| 520 | } |
| 521 | #endif |
| 522 | |
Doug Zongker | 5290f20 | 2014-03-11 13:22:04 -0700 | [diff] [blame] | 523 | void gr_flip() { |
| 524 | gr_draw = gr_backend->flip(gr_backend); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | int gr_init(void) |
| 528 | { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 529 | gr_init_font(); |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 530 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 531 | gr_vt_fd = open("/dev/tty0", O_RDWR | O_SYNC); |
| 532 | if (gr_vt_fd < 0) { |
| 533 | // This is non-fatal; post-Cupcake kernels don't have tty0. |
| 534 | perror("can't open /dev/tty0"); |
| 535 | } else if (ioctl(gr_vt_fd, KDSETMODE, (void*) KD_GRAPHICS)) { |
| 536 | // However, if we do open tty0, we expect the ioctl to work. |
| 537 | perror("failed KDSETMODE to KD_GRAPHICS on tty0"); |
| 538 | gr_exit(); |
| 539 | return -1; |
| 540 | } |
| 541 | |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 542 | gr_backend = open_adf(); |
| 543 | if (gr_backend) { |
| 544 | gr_draw = gr_backend->init(gr_backend); |
| 545 | if (!gr_draw) { |
| 546 | gr_backend->exit(gr_backend); |
| 547 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | get_memory_surface(&gr_mem_surface); |
| 551 | |
Dees Troy | 62b75ab | 2014-05-02 13:20:33 +0000 | [diff] [blame] | 552 | fprintf(stderr, "framebuffer: fd %d (%d x %d)\n", |
| 553 | 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] | 554 | |
Dees Troy | 62b75ab | 2014-05-02 13:20:33 +0000 | [diff] [blame] | 555 | /* 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] | 556 | gr_active_fb = 0; |
Dees Troy | 62b75ab | 2014-05-02 13:20:33 +0000 | [diff] [blame] | 557 | if (!has_overlay) |
| 558 | set_active_framebuffer(0); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 559 | gl->colorBuffer(gl, &gr_mem_surface); |
| 560 | |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 561 | if (!gr_draw) { |
| 562 | gr_backend = open_fbdev(); |
| 563 | gr_draw = gr_backend->init(gr_backend); |
| 564 | if (gr_draw == NULL) { |
| 565 | return -1; |
| 566 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 567 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 568 | |
Doug Zongker | 5290f20 | 2014-03-11 13:22:04 -0700 | [diff] [blame] | 569 | overscan_offset_x = gr_draw->width * overscan_percent / 100; |
| 570 | overscan_offset_y = gr_draw->height * overscan_percent / 100; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 571 | |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 572 | gr_flip(); |
| 573 | gr_flip(); |
Doug Zongker | f6abd40 | 2011-09-27 13:09:48 -0700 | [diff] [blame] | 574 | |
Dees Troy | 62b75ab | 2014-05-02 13:20:33 +0000 | [diff] [blame] | 575 | if (!alloc_ion_mem(fi.line_length * vi.yres)) |
| 576 | allocate_overlay(gr_fb_fd, gr_framebuffer); |
| 577 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 578 | return 0; |
| 579 | } |
| 580 | |
| 581 | void gr_exit(void) |
| 582 | { |
Dees Troy | 62b75ab | 2014-05-02 13:20:33 +0000 | [diff] [blame] | 583 | free_overlay(gr_fb_fd); |
| 584 | free_ion_mem(); |
| 585 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 586 | close(gr_fb_fd); |
| 587 | gr_fb_fd = -1; |
| 588 | |
| 589 | free(gr_mem_surface.data); |
| 590 | |
Doug Zongker | 5290f20 | 2014-03-11 13:22:04 -0700 | [diff] [blame] | 591 | gr_backend->exit(gr_backend); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 592 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 593 | ioctl(gr_vt_fd, KDSETMODE, (void*) KD_TEXT); |
| 594 | close(gr_vt_fd); |
| 595 | gr_vt_fd = -1; |
| 596 | } |
| 597 | |
| 598 | int gr_fb_width(void) |
| 599 | { |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 600 | return gr_draw->width - 2*overscan_offset_x; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | int gr_fb_height(void) |
| 604 | { |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 605 | return gr_draw->height - 2*overscan_offset_y; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 606 | } |
Dima Zavin | 4daf48a | 2011-08-30 11:59:20 -0700 | [diff] [blame] | 607 | |
| 608 | void gr_fb_blank(bool blank) |
| 609 | { |
Matt Mower | 4a5db2d | 2014-01-20 16:14:25 -0600 | [diff] [blame] | 610 | #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] | 611 | int fd; |
Matt Mower | 4a5db2d | 2014-01-20 16:14:25 -0600 | [diff] [blame] | 612 | char brightness[4]; |
| 613 | snprintf(brightness, 4, "%03d", TW_MAX_BRIGHTNESS/2); |
Ethan Chen | 0940e41 | 2013-10-22 13:48:50 -0700 | [diff] [blame] | 614 | |
Matt Mower | 4a5db2d | 2014-01-20 16:14:25 -0600 | [diff] [blame] | 615 | fd = open(TW_BRIGHTNESS_PATH, O_RDWR); |
Ethan Chen | 0940e41 | 2013-10-22 13:48:50 -0700 | [diff] [blame] | 616 | if (fd < 0) { |
| 617 | perror("cannot open LCD backlight"); |
| 618 | return; |
| 619 | } |
Matt Mower | 4a5db2d | 2014-01-20 16:14:25 -0600 | [diff] [blame] | 620 | write(fd, blank ? "000" : brightness, 3); |
Ethan Chen | 0940e41 | 2013-10-22 13:48:50 -0700 | [diff] [blame] | 621 | close(fd); |
| 622 | #else |
Doug Zongker | 5290f20 | 2014-03-11 13:22:04 -0700 | [diff] [blame] | 623 | gr_backend->blank(gr_backend, blank); |
Ethan Yonker | a167416 | 2014-11-06 08:35:10 -0600 | [diff] [blame] | 624 | |
Dees Troy | 62b75ab | 2014-05-02 13:20:33 +0000 | [diff] [blame] | 625 | if (blank) |
| 626 | free_overlay(gr_fb_fd); |
Dima Zavin | 4daf48a | 2011-08-30 11:59:20 -0700 | [diff] [blame] | 627 | |
Dees Troy | 62b75ab | 2014-05-02 13:20:33 +0000 | [diff] [blame] | 628 | if (!blank) |
| 629 | allocate_overlay(gr_fb_fd, gr_framebuffer); |
Ethan Chen | 0940e41 | 2013-10-22 13:48:50 -0700 | [diff] [blame] | 630 | #endif |
Dima Zavin | 4daf48a | 2011-08-30 11:59:20 -0700 | [diff] [blame] | 631 | } |
Dees Troy | 62b75ab | 2014-05-02 13:20:33 +0000 | [diff] [blame] | 632 | |
| 633 | void gr_get_memory_surface(gr_surface surface) |
| 634 | { |
| 635 | get_memory_surface( (GGLSurface*) surface); |
| 636 | } |