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