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> |
Elliott Hughes | cd3c55a | 2015-01-29 20:50:08 -0800 | [diff] [blame] | 19 | #include <string.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 20 | #include <unistd.h> |
| 21 | |
| 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" |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 37 | |
| 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 | |
| 60 | static bool outside(int x, int y) |
| 61 | { |
| 62 | return x < 0 || x >= gr_draw->width || y < 0 || y >= gr_draw->height; |
| 63 | } |
| 64 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 65 | int gr_measure(const char *s) |
| 66 | { |
| 67 | return gr_font->cwidth * strlen(s); |
| 68 | } |
| 69 | |
Dima Zavin | 3c7f00e | 2011-08-30 11:58:24 -0700 | [diff] [blame] | 70 | void gr_font_size(int *x, int *y) |
| 71 | { |
| 72 | *x = gr_font->cwidth; |
| 73 | *y = gr_font->cheight; |
| 74 | } |
| 75 | |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 76 | static void text_blend(unsigned char* src_p, int src_row_bytes, |
| 77 | unsigned char* dst_p, int dst_row_bytes, |
| 78 | int width, int height) |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 79 | { |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 80 | int i, j; |
| 81 | for (j = 0; j < height; ++j) { |
| 82 | unsigned char* sx = src_p; |
| 83 | unsigned char* px = dst_p; |
| 84 | for (i = 0; i < width; ++i) { |
| 85 | unsigned char a = *sx++; |
| 86 | if (gr_current_a < 255) a = ((int)a * gr_current_a) / 255; |
| 87 | if (a == 255) { |
| 88 | *px++ = gr_current_r; |
| 89 | *px++ = gr_current_g; |
| 90 | *px++ = gr_current_b; |
| 91 | px++; |
| 92 | } else if (a > 0) { |
| 93 | *px = (*px * (255-a) + gr_current_r * a) / 255; |
| 94 | ++px; |
| 95 | *px = (*px * (255-a) + gr_current_g * a) / 255; |
| 96 | ++px; |
| 97 | *px = (*px * (255-a) + gr_current_b * a) / 255; |
| 98 | ++px; |
| 99 | ++px; |
| 100 | } else { |
| 101 | px += 4; |
| 102 | } |
| 103 | } |
| 104 | src_p += src_row_bytes; |
| 105 | dst_p += dst_row_bytes; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | |
| 110 | void gr_text(int x, int y, const char *s, int bold) |
| 111 | { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 112 | GRFont *font = gr_font; |
| 113 | unsigned off; |
| 114 | |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 115 | if (!font->texture) return; |
| 116 | if (gr_current_a == 0) return; |
Doug Zongker | 55a36ac | 2013-03-04 15:49:02 -0800 | [diff] [blame] | 117 | |
Doug Zongker | 6fd59ac | 2013-03-06 15:01:11 -0800 | [diff] [blame] | 118 | bold = bold && (font->texture->height != font->cheight); |
| 119 | |
Doug Zongker | c560a67 | 2012-12-18 16:31:27 -0800 | [diff] [blame] | 120 | x += overscan_offset_x; |
| 121 | y += overscan_offset_y; |
| 122 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 123 | while((off = *s++)) { |
| 124 | off -= 32; |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 125 | 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] | 126 | if (off < 96) { |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 127 | |
| 128 | unsigned char* src_p = font->texture->data + (off * font->cwidth) + |
| 129 | (bold ? font->cheight * font->texture->row_bytes : 0); |
| 130 | unsigned char* dst_p = gr_draw->data + y*gr_draw->row_bytes + x*gr_draw->pixel_bytes; |
| 131 | |
| 132 | text_blend(src_p, font->texture->row_bytes, |
| 133 | dst_p, gr_draw->row_bytes, |
| 134 | font->cwidth, font->cheight); |
| 135 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 136 | } |
| 137 | x += font->cwidth; |
| 138 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 139 | } |
| 140 | |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 141 | void gr_texticon(int x, int y, GRSurface* icon) { |
| 142 | if (icon == NULL) return; |
| 143 | |
| 144 | if (icon->pixel_bytes != 1) { |
| 145 | printf("gr_texticon: source has wrong format\n"); |
Doug Zongker | 52eeea4f | 2012-09-04 14:28:25 -0700 | [diff] [blame] | 146 | return; |
| 147 | } |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 148 | |
Doug Zongker | c560a67 | 2012-12-18 16:31:27 -0800 | [diff] [blame] | 149 | x += overscan_offset_x; |
| 150 | y += overscan_offset_y; |
| 151 | |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 152 | 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] | 153 | |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 154 | unsigned char* src_p = icon->data; |
| 155 | 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] | 156 | |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 157 | text_blend(src_p, icon->row_bytes, |
| 158 | dst_p, gr_draw->row_bytes, |
| 159 | icon->width, icon->height); |
| 160 | } |
| 161 | |
| 162 | void gr_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a) |
| 163 | { |
Tony Kuo | fd778e3 | 2015-02-05 21:25:56 +0800 | [diff] [blame] | 164 | #if defined(RECOVERY_ABGR) || defined(RECOVERY_BGRA) |
| 165 | gr_current_r = b; |
| 166 | gr_current_g = g; |
| 167 | gr_current_b = r; |
| 168 | gr_current_a = a; |
| 169 | #else |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 170 | gr_current_r = r; |
| 171 | gr_current_g = g; |
| 172 | gr_current_b = b; |
| 173 | gr_current_a = a; |
Tony Kuo | fd778e3 | 2015-02-05 21:25:56 +0800 | [diff] [blame] | 174 | #endif |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | void gr_clear() |
| 178 | { |
| 179 | if (gr_current_r == gr_current_g && |
| 180 | gr_current_r == gr_current_b) { |
| 181 | memset(gr_draw->data, gr_current_r, gr_draw->height * gr_draw->row_bytes); |
| 182 | } else { |
| 183 | int x, y; |
| 184 | unsigned char* px = gr_draw->data; |
| 185 | for (y = 0; y < gr_draw->height; ++y) { |
| 186 | for (x = 0; x < gr_draw->width; ++x) { |
| 187 | *px++ = gr_current_r; |
| 188 | *px++ = gr_current_g; |
| 189 | *px++ = gr_current_b; |
| 190 | px++; |
| 191 | } |
| 192 | px += gr_draw->row_bytes - (gr_draw->width * gr_draw->pixel_bytes); |
| 193 | } |
| 194 | } |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Doug Zongker | c560a67 | 2012-12-18 16:31:27 -0800 | [diff] [blame] | 197 | 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] | 198 | { |
Doug Zongker | c560a67 | 2012-12-18 16:31:27 -0800 | [diff] [blame] | 199 | x1 += overscan_offset_x; |
| 200 | y1 += overscan_offset_y; |
| 201 | |
| 202 | x2 += overscan_offset_x; |
| 203 | y2 += overscan_offset_y; |
| 204 | |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 205 | if (outside(x1, y1) || outside(x2-1, y2-1)) return; |
| 206 | |
| 207 | unsigned char* p = gr_draw->data + y1 * gr_draw->row_bytes + x1 * gr_draw->pixel_bytes; |
| 208 | if (gr_current_a == 255) { |
| 209 | int x, y; |
| 210 | for (y = y1; y < y2; ++y) { |
| 211 | unsigned char* px = p; |
| 212 | for (x = x1; x < x2; ++x) { |
| 213 | *px++ = gr_current_r; |
| 214 | *px++ = gr_current_g; |
| 215 | *px++ = gr_current_b; |
| 216 | px++; |
| 217 | } |
| 218 | p += gr_draw->row_bytes; |
| 219 | } |
| 220 | } else if (gr_current_a > 0) { |
| 221 | int x, y; |
| 222 | for (y = y1; y < y2; ++y) { |
| 223 | unsigned char* px = p; |
| 224 | for (x = x1; x < x2; ++x) { |
| 225 | *px = (*px * (255-gr_current_a) + gr_current_r * gr_current_a) / 255; |
| 226 | ++px; |
| 227 | *px = (*px * (255-gr_current_a) + gr_current_g * gr_current_a) / 255; |
| 228 | ++px; |
| 229 | *px = (*px * (255-gr_current_a) + gr_current_b * gr_current_a) / 255; |
| 230 | ++px; |
| 231 | ++px; |
| 232 | } |
| 233 | p += gr_draw->row_bytes; |
| 234 | } |
| 235 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 236 | } |
| 237 | |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 238 | void gr_blit(GRSurface* source, int sx, int sy, int w, int h, int dx, int dy) { |
| 239 | if (source == NULL) return; |
| 240 | |
| 241 | if (gr_draw->pixel_bytes != source->pixel_bytes) { |
| 242 | printf("gr_blit: source has wrong format\n"); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 243 | return; |
| 244 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 245 | |
Doug Zongker | c560a67 | 2012-12-18 16:31:27 -0800 | [diff] [blame] | 246 | dx += overscan_offset_x; |
| 247 | dy += overscan_offset_y; |
| 248 | |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 249 | if (outside(dx, dy) || outside(dx+w-1, dy+h-1)) return; |
| 250 | |
| 251 | unsigned char* src_p = source->data + sy*source->row_bytes + sx*source->pixel_bytes; |
| 252 | unsigned char* dst_p = gr_draw->data + dy*gr_draw->row_bytes + dx*gr_draw->pixel_bytes; |
| 253 | |
| 254 | int i; |
| 255 | for (i = 0; i < h; ++i) { |
| 256 | memcpy(dst_p, src_p, w * source->pixel_bytes); |
| 257 | src_p += source->row_bytes; |
| 258 | dst_p += gr_draw->row_bytes; |
| 259 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 260 | } |
| 261 | |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 262 | unsigned int gr_get_width(GRSurface* surface) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 263 | if (surface == NULL) { |
| 264 | return 0; |
| 265 | } |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 266 | return surface->width; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 267 | } |
| 268 | |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 269 | unsigned int gr_get_height(GRSurface* surface) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 270 | if (surface == NULL) { |
| 271 | return 0; |
| 272 | } |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 273 | return surface->height; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | static void gr_init_font(void) |
| 277 | { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 278 | gr_font = calloc(sizeof(*gr_font), 1); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 279 | |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 280 | int res = res_create_alpha_surface("font", &(gr_font->texture)); |
Doug Zongker | 6fd59ac | 2013-03-06 15:01:11 -0800 | [diff] [blame] | 281 | if (res == 0) { |
| 282 | // The font image should be a 96x2 array of character images. The |
| 283 | // columns are the printable ASCII characters 0x20 - 0x7f. The |
| 284 | // top row is regular text; the bottom row is bold. |
| 285 | gr_font->cwidth = gr_font->texture->width / 96; |
| 286 | gr_font->cheight = gr_font->texture->height / 2; |
| 287 | } else { |
Doug Zongker | 55a36ac | 2013-03-04 15:49:02 -0800 | [diff] [blame] | 288 | printf("failed to read font: res=%d\n", res); |
Doug Zongker | 6fd59ac | 2013-03-06 15:01:11 -0800 | [diff] [blame] | 289 | |
| 290 | // fall back to the compiled-in font. |
| 291 | gr_font->texture = malloc(sizeof(*gr_font->texture)); |
| 292 | gr_font->texture->width = font.width; |
| 293 | gr_font->texture->height = font.height; |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 294 | gr_font->texture->row_bytes = font.width; |
| 295 | gr_font->texture->pixel_bytes = 1; |
Doug Zongker | 6fd59ac | 2013-03-06 15:01:11 -0800 | [diff] [blame] | 296 | |
| 297 | unsigned char* bits = malloc(font.width * font.height); |
| 298 | gr_font->texture->data = (void*) bits; |
| 299 | |
| 300 | unsigned char data; |
| 301 | unsigned char* in = font.rundata; |
| 302 | while((data = *in++)) { |
| 303 | memset(bits, (data & 0x80) ? 255 : 0, data & 0x7f); |
| 304 | bits += (data & 0x7f); |
| 305 | } |
| 306 | |
| 307 | gr_font->cwidth = font.cwidth; |
| 308 | gr_font->cheight = font.cheight; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 309 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 310 | } |
| 311 | |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 312 | #if 0 |
| 313 | // Exercises many of the gr_*() functions; useful for testing. |
| 314 | static void gr_test() { |
| 315 | GRSurface** images; |
| 316 | int frames; |
| 317 | int result = res_create_multi_surface("icon_installing", &frames, &images); |
| 318 | if (result < 0) { |
| 319 | printf("create surface %d\n", result); |
| 320 | gr_exit(); |
| 321 | return; |
| 322 | } |
| 323 | |
| 324 | time_t start = time(NULL); |
| 325 | int x; |
| 326 | for (x = 0; x <= 1200; ++x) { |
| 327 | if (x < 400) { |
| 328 | gr_color(0, 0, 0, 255); |
| 329 | } else { |
| 330 | gr_color(0, (x-400)%128, 0, 255); |
| 331 | } |
| 332 | gr_clear(); |
| 333 | |
| 334 | gr_color(255, 0, 0, 255); |
| 335 | gr_surface frame = images[x%frames]; |
| 336 | gr_blit(frame, 0, 0, frame->width, frame->height, x, 0); |
| 337 | |
| 338 | gr_color(255, 0, 0, 128); |
| 339 | gr_fill(400, 150, 600, 350); |
| 340 | |
| 341 | gr_color(255, 255, 255, 255); |
| 342 | gr_text(500, 225, "hello, world!", 0); |
| 343 | gr_color(255, 255, 0, 128); |
| 344 | gr_text(300+x, 275, "pack my box with five dozen liquor jugs", 1); |
| 345 | |
| 346 | gr_color(0, 0, 255, 128); |
| 347 | gr_fill(gr_draw->width - 200 - x, 300, gr_draw->width - x, 500); |
| 348 | |
Doug Zongker | 5290f20 | 2014-03-11 13:22:04 -0700 | [diff] [blame] | 349 | gr_draw = gr_backend->flip(gr_backend); |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 350 | } |
| 351 | printf("getting end time\n"); |
| 352 | time_t end = time(NULL); |
| 353 | printf("got end time\n"); |
| 354 | printf("start %ld end %ld\n", (long)start, (long)end); |
| 355 | if (end > start) { |
| 356 | printf("%.2f fps\n", ((double)x) / (end-start)); |
| 357 | } |
| 358 | } |
| 359 | #endif |
| 360 | |
Doug Zongker | 5290f20 | 2014-03-11 13:22:04 -0700 | [diff] [blame] | 361 | void gr_flip() { |
| 362 | gr_draw = gr_backend->flip(gr_backend); |
| 363 | } |
| 364 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 365 | int gr_init(void) |
| 366 | { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 367 | gr_init_font(); |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 368 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 369 | gr_vt_fd = open("/dev/tty0", O_RDWR | O_SYNC); |
| 370 | if (gr_vt_fd < 0) { |
| 371 | // This is non-fatal; post-Cupcake kernels don't have tty0. |
| 372 | perror("can't open /dev/tty0"); |
| 373 | } else if (ioctl(gr_vt_fd, KDSETMODE, (void*) KD_GRAPHICS)) { |
| 374 | // However, if we do open tty0, we expect the ioctl to work. |
| 375 | perror("failed KDSETMODE to KD_GRAPHICS on tty0"); |
| 376 | gr_exit(); |
| 377 | return -1; |
| 378 | } |
| 379 | |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 380 | gr_backend = open_adf(); |
| 381 | if (gr_backend) { |
| 382 | gr_draw = gr_backend->init(gr_backend); |
| 383 | if (!gr_draw) { |
| 384 | gr_backend->exit(gr_backend); |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | if (!gr_draw) { |
| 389 | gr_backend = open_fbdev(); |
| 390 | gr_draw = gr_backend->init(gr_backend); |
| 391 | if (gr_draw == NULL) { |
| 392 | return -1; |
| 393 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 394 | } |
| 395 | |
Doug Zongker | 5290f20 | 2014-03-11 13:22:04 -0700 | [diff] [blame] | 396 | overscan_offset_x = gr_draw->width * overscan_percent / 100; |
| 397 | overscan_offset_y = gr_draw->height * overscan_percent / 100; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 398 | |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 399 | gr_flip(); |
| 400 | gr_flip(); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 401 | |
| 402 | return 0; |
| 403 | } |
| 404 | |
| 405 | void gr_exit(void) |
| 406 | { |
Doug Zongker | 5290f20 | 2014-03-11 13:22:04 -0700 | [diff] [blame] | 407 | gr_backend->exit(gr_backend); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 408 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 409 | ioctl(gr_vt_fd, KDSETMODE, (void*) KD_TEXT); |
| 410 | close(gr_vt_fd); |
| 411 | gr_vt_fd = -1; |
| 412 | } |
| 413 | |
| 414 | int gr_fb_width(void) |
| 415 | { |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 416 | return gr_draw->width - 2*overscan_offset_x; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | int gr_fb_height(void) |
| 420 | { |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 421 | return gr_draw->height - 2*overscan_offset_y; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 422 | } |
Dima Zavin | 4daf48a | 2011-08-30 11:59:20 -0700 | [diff] [blame] | 423 | |
| 424 | void gr_fb_blank(bool blank) |
| 425 | { |
Doug Zongker | 5290f20 | 2014-03-11 13:22:04 -0700 | [diff] [blame] | 426 | gr_backend->blank(gr_backend, blank); |
Dima Zavin | 4daf48a | 2011-08-30 11:59:20 -0700 | [diff] [blame] | 427 | } |