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 | |
maxwen | 39624d4 | 2015-12-01 22:02:07 +0100 | [diff] [blame] | 34 | #ifdef BOARD_USE_CUSTOM_RECOVERY_FONT |
| 35 | #include BOARD_USE_CUSTOM_RECOVERY_FONT |
| 36 | #else |
Doug Zongker | 6fd59ac | 2013-03-06 15:01:11 -0800 | [diff] [blame] | 37 | #include "font_10x18.h" |
maxwen | 39624d4 | 2015-12-01 22:02:07 +0100 | [diff] [blame] | 38 | #endif |
| 39 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 40 | #include "minui.h" |
Doug Zongker | 5290f20 | 2014-03-11 13:22:04 -0700 | [diff] [blame] | 41 | #include "graphics.h" |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 42 | |
Elliott Hughes | 07cfb8f | 2015-04-10 13:12:05 -0700 | [diff] [blame] | 43 | struct GRFont { |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 44 | GRSurface* texture; |
| 45 | int cwidth; |
| 46 | int cheight; |
Elliott Hughes | 07cfb8f | 2015-04-10 13:12:05 -0700 | [diff] [blame] | 47 | }; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 48 | |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 49 | static GRFont* gr_font = NULL; |
Doug Zongker | 5290f20 | 2014-03-11 13:22:04 -0700 | [diff] [blame] | 50 | static minui_backend* gr_backend = NULL; |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 51 | |
Doug Zongker | c560a67 | 2012-12-18 16:31:27 -0800 | [diff] [blame] | 52 | static int overscan_percent = OVERSCAN_PERCENT; |
| 53 | static int overscan_offset_x = 0; |
| 54 | static int overscan_offset_y = 0; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 55 | |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 56 | static unsigned char gr_current_r = 255; |
| 57 | static unsigned char gr_current_g = 255; |
| 58 | static unsigned char gr_current_b = 255; |
| 59 | static unsigned char gr_current_a = 255; |
Ethan Yonker | e96182e | 2015-10-13 19:32:03 -0500 | [diff] [blame] | 60 | static unsigned char rgb_555[2]; |
| 61 | static unsigned char gr_current_r5 = 31; |
| 62 | static unsigned char gr_current_g5 = 63; |
| 63 | static unsigned char gr_current_b5 = 31; |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 64 | |
Doug Zongker | 5290f20 | 2014-03-11 13:22:04 -0700 | [diff] [blame] | 65 | static GRSurface* gr_draw = NULL; |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 66 | |
| 67 | static bool outside(int x, int y) |
| 68 | { |
| 69 | return x < 0 || x >= gr_draw->width || y < 0 || y >= gr_draw->height; |
| 70 | } |
| 71 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 72 | int gr_measure(const char *s) |
| 73 | { |
| 74 | return gr_font->cwidth * strlen(s); |
| 75 | } |
| 76 | |
Dima Zavin | 3c7f00e | 2011-08-30 11:58:24 -0700 | [diff] [blame] | 77 | void gr_font_size(int *x, int *y) |
| 78 | { |
| 79 | *x = gr_font->cwidth; |
| 80 | *y = gr_font->cheight; |
| 81 | } |
| 82 | |
Ethan Yonker | e96182e | 2015-10-13 19:32:03 -0500 | [diff] [blame] | 83 | void blend_16bpp(unsigned char* px, unsigned r5, unsigned g5, unsigned b5, unsigned char a) |
| 84 | { |
| 85 | unsigned char orig[2]; |
| 86 | orig[0] = px[0]; |
| 87 | orig[1] = px[1]; |
| 88 | |
| 89 | /* This code is a little easier to read |
| 90 | unsigned oldred = (orig[1] >> 3); |
| 91 | unsigned oldgreen = (((orig[0] >> 5) << 3) + (orig[1] & 0x7)); |
| 92 | unsigned oldblue = (orig[0] & 0x1F); |
| 93 | |
| 94 | unsigned newred = (oldred * (255-a) + r5 * a) / 255; |
| 95 | unsigned newgreen = (oldgreen * (255-a) + g5 * a) / 255; |
| 96 | unsigned newblue = (oldblue * (255-a) + b5 * a) / 255; |
| 97 | */ |
| 98 | |
| 99 | unsigned newred = ((orig[1] >> 3) * (255-a) + r5 * a) / 255; |
| 100 | unsigned newgreen = ((((orig[0] >> 5) << 3) + (orig[1] & 0x7)) * (255-a) + g5 * a) / 255; |
| 101 | unsigned newblue = ((orig[0] & 0x1F) * (255-a) + b5 * a) / 255; |
| 102 | |
| 103 | *px++ = (newgreen << 5) + (newblue); |
| 104 | *px++ = (newred << 3) + (newgreen >> 3); |
| 105 | } |
| 106 | |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 107 | static void text_blend(unsigned char* src_p, int src_row_bytes, |
| 108 | unsigned char* dst_p, int dst_row_bytes, |
| 109 | int width, int height) |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 110 | { |
Elliott Hughes | 01a4d08 | 2015-03-24 15:21:48 -0700 | [diff] [blame] | 111 | for (int j = 0; j < height; ++j) { |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 112 | unsigned char* sx = src_p; |
| 113 | unsigned char* px = dst_p; |
Elliott Hughes | 01a4d08 | 2015-03-24 15:21:48 -0700 | [diff] [blame] | 114 | for (int i = 0; i < width; ++i) { |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 115 | unsigned char a = *sx++; |
| 116 | if (gr_current_a < 255) a = ((int)a * gr_current_a) / 255; |
| 117 | if (a == 255) { |
Ethan Yonker | e96182e | 2015-10-13 19:32:03 -0500 | [diff] [blame] | 118 | if (gr_draw->pixel_bytes == 2) { |
| 119 | *px++ = rgb_555[0]; |
| 120 | *px++ = rgb_555[1]; |
| 121 | } else { |
| 122 | *px++ = gr_current_r; |
| 123 | *px++ = gr_current_g; |
| 124 | *px++ = gr_current_b; |
| 125 | px++; |
| 126 | } |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 127 | } else if (a > 0) { |
Ethan Yonker | e96182e | 2015-10-13 19:32:03 -0500 | [diff] [blame] | 128 | if (gr_draw->pixel_bytes == 2) { |
| 129 | blend_16bpp(px, gr_current_r5, gr_current_g5, gr_current_b5, a); |
| 130 | px += gr_draw->pixel_bytes; |
| 131 | } else { |
| 132 | *px = (*px * (255-a) + gr_current_r * a) / 255; |
| 133 | ++px; |
| 134 | *px = (*px * (255-a) + gr_current_g * a) / 255; |
| 135 | ++px; |
| 136 | *px = (*px * (255-a) + gr_current_b * a) / 255; |
| 137 | ++px; |
| 138 | ++px; |
| 139 | } |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 140 | } else { |
Ethan Yonker | e96182e | 2015-10-13 19:32:03 -0500 | [diff] [blame] | 141 | px += gr_draw->pixel_bytes; |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | src_p += src_row_bytes; |
| 145 | dst_p += dst_row_bytes; |
| 146 | } |
| 147 | } |
| 148 | |
Elliott Hughes | 8fd86d7 | 2015-04-13 14:36:02 -0700 | [diff] [blame] | 149 | void gr_text(int x, int y, const char *s, bool bold) |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 150 | { |
Elliott Hughes | 01a4d08 | 2015-03-24 15:21:48 -0700 | [diff] [blame] | 151 | GRFont* font = gr_font; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 152 | |
Elliott Hughes | 01a4d08 | 2015-03-24 15:21:48 -0700 | [diff] [blame] | 153 | if (!font->texture || gr_current_a == 0) return; |
Doug Zongker | 55a36ac | 2013-03-04 15:49:02 -0800 | [diff] [blame] | 154 | |
Doug Zongker | 6fd59ac | 2013-03-06 15:01:11 -0800 | [diff] [blame] | 155 | bold = bold && (font->texture->height != font->cheight); |
| 156 | |
Doug Zongker | c560a67 | 2012-12-18 16:31:27 -0800 | [diff] [blame] | 157 | x += overscan_offset_x; |
| 158 | y += overscan_offset_y; |
| 159 | |
Elliott Hughes | 01a4d08 | 2015-03-24 15:21:48 -0700 | [diff] [blame] | 160 | unsigned char ch; |
| 161 | while ((ch = *s++)) { |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 162 | if (outside(x, y) || outside(x+font->cwidth-1, y+font->cheight-1)) break; |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 163 | |
Elliott Hughes | 01a4d08 | 2015-03-24 15:21:48 -0700 | [diff] [blame] | 164 | if (ch < ' ' || ch > '~') { |
| 165 | ch = '?'; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 166 | } |
Elliott Hughes | 01a4d08 | 2015-03-24 15:21:48 -0700 | [diff] [blame] | 167 | |
| 168 | unsigned char* src_p = font->texture->data + ((ch - ' ') * font->cwidth) + |
| 169 | (bold ? font->cheight * font->texture->row_bytes : 0); |
| 170 | unsigned char* dst_p = gr_draw->data + y*gr_draw->row_bytes + x*gr_draw->pixel_bytes; |
| 171 | |
| 172 | text_blend(src_p, font->texture->row_bytes, |
| 173 | dst_p, gr_draw->row_bytes, |
| 174 | font->cwidth, font->cheight); |
| 175 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 176 | x += font->cwidth; |
| 177 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 178 | } |
| 179 | |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 180 | void gr_texticon(int x, int y, GRSurface* icon) { |
| 181 | if (icon == NULL) return; |
| 182 | |
| 183 | if (icon->pixel_bytes != 1) { |
| 184 | printf("gr_texticon: source has wrong format\n"); |
Doug Zongker | 52eeea4f | 2012-09-04 14:28:25 -0700 | [diff] [blame] | 185 | return; |
| 186 | } |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 187 | |
Doug Zongker | c560a67 | 2012-12-18 16:31:27 -0800 | [diff] [blame] | 188 | x += overscan_offset_x; |
| 189 | y += overscan_offset_y; |
| 190 | |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 191 | 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] | 192 | |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 193 | unsigned char* src_p = icon->data; |
| 194 | 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] | 195 | |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 196 | text_blend(src_p, icon->row_bytes, |
| 197 | dst_p, gr_draw->row_bytes, |
| 198 | icon->width, icon->height); |
| 199 | } |
| 200 | |
Ethan Yonker | e96182e | 2015-10-13 19:32:03 -0500 | [diff] [blame] | 201 | void gr_convert_rgb_555() |
| 202 | { |
| 203 | gr_current_r5 = (((gr_current_r & 0xFF) * 0x1F) + 0x7F) / 0xFF; |
| 204 | gr_current_g5 = (((gr_current_g & 0xFF) * 0x3F) + 0x7F) / 0xFF; |
| 205 | gr_current_b5 = (((gr_current_b & 0xFF) * 0x1F) + 0x7F) / 0xFF; |
| 206 | |
| 207 | rgb_555[0] = (gr_current_g5 << 5) + (gr_current_b5); |
| 208 | rgb_555[1] = (gr_current_r5 << 3) + (gr_current_g5 >> 3); |
| 209 | } |
| 210 | |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 211 | void gr_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a) |
| 212 | { |
Tony Kuo | fd778e3 | 2015-02-05 21:25:56 +0800 | [diff] [blame] | 213 | #if defined(RECOVERY_ABGR) || defined(RECOVERY_BGRA) |
| 214 | gr_current_r = b; |
| 215 | gr_current_g = g; |
| 216 | gr_current_b = r; |
| 217 | gr_current_a = a; |
| 218 | #else |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 219 | gr_current_r = r; |
| 220 | gr_current_g = g; |
| 221 | gr_current_b = b; |
| 222 | gr_current_a = a; |
Tony Kuo | fd778e3 | 2015-02-05 21:25:56 +0800 | [diff] [blame] | 223 | #endif |
Ethan Yonker | e96182e | 2015-10-13 19:32:03 -0500 | [diff] [blame] | 224 | if (gr_draw->pixel_bytes == 2) { |
| 225 | gr_convert_rgb_555(); |
| 226 | } |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | void gr_clear() |
| 230 | { |
Ethan Yonker | e96182e | 2015-10-13 19:32:03 -0500 | [diff] [blame] | 231 | if (gr_draw->pixel_bytes == 2) { |
| 232 | gr_fill(0, 0, gr_fb_width(), gr_fb_height()); |
| 233 | return; |
| 234 | } |
| 235 | |
| 236 | // This code only works on 32bpp devices |
Elliott Hughes | 01a4d08 | 2015-03-24 15:21:48 -0700 | [diff] [blame] | 237 | 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] | 238 | memset(gr_draw->data, gr_current_r, gr_draw->height * gr_draw->row_bytes); |
| 239 | } else { |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 240 | unsigned char* px = gr_draw->data; |
Elliott Hughes | 01a4d08 | 2015-03-24 15:21:48 -0700 | [diff] [blame] | 241 | for (int y = 0; y < gr_draw->height; ++y) { |
| 242 | for (int x = 0; x < gr_draw->width; ++x) { |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 243 | *px++ = gr_current_r; |
| 244 | *px++ = gr_current_g; |
| 245 | *px++ = gr_current_b; |
| 246 | px++; |
| 247 | } |
| 248 | px += gr_draw->row_bytes - (gr_draw->width * gr_draw->pixel_bytes); |
| 249 | } |
| 250 | } |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 251 | } |
| 252 | |
Doug Zongker | c560a67 | 2012-12-18 16:31:27 -0800 | [diff] [blame] | 253 | 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] | 254 | { |
Doug Zongker | c560a67 | 2012-12-18 16:31:27 -0800 | [diff] [blame] | 255 | x1 += overscan_offset_x; |
| 256 | y1 += overscan_offset_y; |
| 257 | |
| 258 | x2 += overscan_offset_x; |
| 259 | y2 += overscan_offset_y; |
| 260 | |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 261 | if (outside(x1, y1) || outside(x2-1, y2-1)) return; |
| 262 | |
| 263 | unsigned char* p = gr_draw->data + y1 * gr_draw->row_bytes + x1 * gr_draw->pixel_bytes; |
| 264 | if (gr_current_a == 255) { |
| 265 | int x, y; |
| 266 | for (y = y1; y < y2; ++y) { |
| 267 | unsigned char* px = p; |
| 268 | for (x = x1; x < x2; ++x) { |
Ethan Yonker | e96182e | 2015-10-13 19:32:03 -0500 | [diff] [blame] | 269 | if (gr_draw->pixel_bytes == 2) { |
| 270 | *px++ = rgb_555[0]; |
| 271 | *px++ = rgb_555[1]; |
| 272 | } else { |
| 273 | *px++ = gr_current_r; |
| 274 | *px++ = gr_current_g; |
| 275 | *px++ = gr_current_b; |
| 276 | px++; |
| 277 | } |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 278 | } |
| 279 | p += gr_draw->row_bytes; |
| 280 | } |
| 281 | } else if (gr_current_a > 0) { |
| 282 | int x, y; |
| 283 | for (y = y1; y < y2; ++y) { |
| 284 | unsigned char* px = p; |
| 285 | for (x = x1; x < x2; ++x) { |
Ethan Yonker | e96182e | 2015-10-13 19:32:03 -0500 | [diff] [blame] | 286 | if (gr_draw->pixel_bytes == 2) { |
| 287 | blend_16bpp(px, gr_current_r5, gr_current_g5, gr_current_b5, gr_current_a); |
| 288 | px += gr_draw->row_bytes; |
| 289 | } else { |
| 290 | *px = (*px * (255-gr_current_a) + gr_current_r * gr_current_a) / 255; |
| 291 | ++px; |
| 292 | *px = (*px * (255-gr_current_a) + gr_current_g * gr_current_a) / 255; |
| 293 | ++px; |
| 294 | *px = (*px * (255-gr_current_a) + gr_current_b * gr_current_a) / 255; |
| 295 | ++px; |
| 296 | ++px; |
| 297 | } |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 298 | } |
| 299 | p += gr_draw->row_bytes; |
| 300 | } |
| 301 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 302 | } |
| 303 | |
Ethan Yonker | e96182e | 2015-10-13 19:32:03 -0500 | [diff] [blame] | 304 | void gr_blit_32to16(GRSurface* source, int sx, int sy, int w, int h, int dx, int dy) { |
| 305 | dx += overscan_offset_x; |
| 306 | dy += overscan_offset_y; |
| 307 | |
| 308 | if (outside(dx, dy) || outside(dx+w-1, dy+h-1)) return; |
| 309 | |
| 310 | unsigned char* src_p = source->data + sy*source->row_bytes + sx*source->pixel_bytes; |
| 311 | unsigned char* dst_p = gr_draw->data + dy*gr_draw->row_bytes + dx*gr_draw->pixel_bytes; |
| 312 | |
| 313 | int i, j; |
| 314 | for (i = 0; i < h; ++i) { |
| 315 | unsigned char* spx = src_p; |
| 316 | unsigned char* dpx = dst_p; |
| 317 | |
| 318 | for (j = 0; j < w; ++j) { |
| 319 | unsigned a = spx[3]; |
| 320 | |
| 321 | if (a == 0) { |
| 322 | spx += source->pixel_bytes; |
| 323 | dpx += gr_draw->pixel_bytes; |
| 324 | } else { |
| 325 | unsigned r5 = (((*spx++ & 0xFF) * 0x1F) + 0x7F) / 0xFF; |
| 326 | unsigned g5 = (((*spx++ & 0xFF) * 0x3F) + 0x7F) / 0xFF; |
| 327 | unsigned b5 = (((*spx++ & 0xFF) * 0x1F) + 0x7F) / 0xFF; |
| 328 | spx++; |
| 329 | if (a == 255) { |
| 330 | *dpx++ = (g5 << 5) + (b5); |
| 331 | *dpx++ = (r5 << 3) + (g5 >> 3); |
| 332 | } else { |
| 333 | blend_16bpp(dpx, r5, g5, b5, a); |
| 334 | spx += source->pixel_bytes; |
| 335 | } |
| 336 | } |
| 337 | } |
| 338 | src_p += source->row_bytes; |
| 339 | dst_p += gr_draw->row_bytes; |
| 340 | } |
| 341 | } |
| 342 | |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 343 | void gr_blit(GRSurface* source, int sx, int sy, int w, int h, int dx, int dy) { |
| 344 | if (source == NULL) return; |
| 345 | |
| 346 | if (gr_draw->pixel_bytes != source->pixel_bytes) { |
Ethan Yonker | e96182e | 2015-10-13 19:32:03 -0500 | [diff] [blame] | 347 | if (gr_draw->pixel_bytes == 2 && source->pixel_bytes == 4) { |
| 348 | gr_blit_32to16(source, sx, sy, w, h, dx, dy); |
| 349 | return; |
| 350 | } else { |
| 351 | printf("gr_blit: source has wrong format\n"); |
| 352 | return; |
| 353 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 354 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 355 | |
Doug Zongker | c560a67 | 2012-12-18 16:31:27 -0800 | [diff] [blame] | 356 | dx += overscan_offset_x; |
| 357 | dy += overscan_offset_y; |
| 358 | |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 359 | if (outside(dx, dy) || outside(dx+w-1, dy+h-1)) return; |
| 360 | |
| 361 | unsigned char* src_p = source->data + sy*source->row_bytes + sx*source->pixel_bytes; |
| 362 | unsigned char* dst_p = gr_draw->data + dy*gr_draw->row_bytes + dx*gr_draw->pixel_bytes; |
| 363 | |
| 364 | int i; |
| 365 | for (i = 0; i < h; ++i) { |
| 366 | memcpy(dst_p, src_p, w * source->pixel_bytes); |
| 367 | src_p += source->row_bytes; |
| 368 | dst_p += gr_draw->row_bytes; |
| 369 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 370 | } |
| 371 | |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 372 | unsigned int gr_get_width(GRSurface* surface) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 373 | if (surface == NULL) { |
| 374 | return 0; |
| 375 | } |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 376 | return surface->width; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 377 | } |
| 378 | |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 379 | unsigned int gr_get_height(GRSurface* surface) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 380 | if (surface == NULL) { |
| 381 | return 0; |
| 382 | } |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 383 | return surface->height; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | static void gr_init_font(void) |
| 387 | { |
Elliott Hughes | 07cfb8f | 2015-04-10 13:12:05 -0700 | [diff] [blame] | 388 | gr_font = reinterpret_cast<GRFont*>(calloc(sizeof(*gr_font), 1)); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 389 | |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 390 | int res = res_create_alpha_surface("font", &(gr_font->texture)); |
Doug Zongker | 6fd59ac | 2013-03-06 15:01:11 -0800 | [diff] [blame] | 391 | if (res == 0) { |
| 392 | // The font image should be a 96x2 array of character images. The |
| 393 | // columns are the printable ASCII characters 0x20 - 0x7f. The |
| 394 | // top row is regular text; the bottom row is bold. |
| 395 | gr_font->cwidth = gr_font->texture->width / 96; |
| 396 | gr_font->cheight = gr_font->texture->height / 2; |
| 397 | } else { |
Doug Zongker | 55a36ac | 2013-03-04 15:49:02 -0800 | [diff] [blame] | 398 | printf("failed to read font: res=%d\n", res); |
Doug Zongker | 6fd59ac | 2013-03-06 15:01:11 -0800 | [diff] [blame] | 399 | |
| 400 | // fall back to the compiled-in font. |
Elliott Hughes | 07cfb8f | 2015-04-10 13:12:05 -0700 | [diff] [blame] | 401 | gr_font->texture = reinterpret_cast<GRSurface*>(malloc(sizeof(*gr_font->texture))); |
Doug Zongker | 6fd59ac | 2013-03-06 15:01:11 -0800 | [diff] [blame] | 402 | gr_font->texture->width = font.width; |
| 403 | gr_font->texture->height = font.height; |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 404 | gr_font->texture->row_bytes = font.width; |
| 405 | gr_font->texture->pixel_bytes = 1; |
Doug Zongker | 6fd59ac | 2013-03-06 15:01:11 -0800 | [diff] [blame] | 406 | |
Elliott Hughes | 07cfb8f | 2015-04-10 13:12:05 -0700 | [diff] [blame] | 407 | unsigned char* bits = reinterpret_cast<unsigned char*>(malloc(font.width * font.height)); |
| 408 | gr_font->texture->data = reinterpret_cast<unsigned char*>(bits); |
Doug Zongker | 6fd59ac | 2013-03-06 15:01:11 -0800 | [diff] [blame] | 409 | |
| 410 | unsigned char data; |
| 411 | unsigned char* in = font.rundata; |
| 412 | while((data = *in++)) { |
| 413 | memset(bits, (data & 0x80) ? 255 : 0, data & 0x7f); |
| 414 | bits += (data & 0x7f); |
| 415 | } |
| 416 | |
| 417 | gr_font->cwidth = font.cwidth; |
| 418 | gr_font->cheight = font.cheight; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 419 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 420 | } |
| 421 | |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 422 | #if 0 |
| 423 | // Exercises many of the gr_*() functions; useful for testing. |
| 424 | static void gr_test() { |
| 425 | GRSurface** images; |
| 426 | int frames; |
| 427 | int result = res_create_multi_surface("icon_installing", &frames, &images); |
| 428 | if (result < 0) { |
| 429 | printf("create surface %d\n", result); |
| 430 | gr_exit(); |
| 431 | return; |
| 432 | } |
| 433 | |
| 434 | time_t start = time(NULL); |
| 435 | int x; |
| 436 | for (x = 0; x <= 1200; ++x) { |
| 437 | if (x < 400) { |
| 438 | gr_color(0, 0, 0, 255); |
| 439 | } else { |
| 440 | gr_color(0, (x-400)%128, 0, 255); |
| 441 | } |
| 442 | gr_clear(); |
| 443 | |
| 444 | gr_color(255, 0, 0, 255); |
Elliott Hughes | 0a5cb0c | 2015-04-15 10:58:56 -0700 | [diff] [blame] | 445 | GRSurface* frame = images[x%frames]; |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 446 | gr_blit(frame, 0, 0, frame->width, frame->height, x, 0); |
| 447 | |
| 448 | gr_color(255, 0, 0, 128); |
| 449 | gr_fill(400, 150, 600, 350); |
| 450 | |
| 451 | gr_color(255, 255, 255, 255); |
| 452 | gr_text(500, 225, "hello, world!", 0); |
| 453 | gr_color(255, 255, 0, 128); |
| 454 | gr_text(300+x, 275, "pack my box with five dozen liquor jugs", 1); |
| 455 | |
| 456 | gr_color(0, 0, 255, 128); |
| 457 | gr_fill(gr_draw->width - 200 - x, 300, gr_draw->width - x, 500); |
| 458 | |
Doug Zongker | 5290f20 | 2014-03-11 13:22:04 -0700 | [diff] [blame] | 459 | gr_draw = gr_backend->flip(gr_backend); |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 460 | } |
| 461 | printf("getting end time\n"); |
| 462 | time_t end = time(NULL); |
| 463 | printf("got end time\n"); |
| 464 | printf("start %ld end %ld\n", (long)start, (long)end); |
| 465 | if (end > start) { |
| 466 | printf("%.2f fps\n", ((double)x) / (end-start)); |
| 467 | } |
| 468 | } |
| 469 | #endif |
| 470 | |
Doug Zongker | 5290f20 | 2014-03-11 13:22:04 -0700 | [diff] [blame] | 471 | void gr_flip() { |
| 472 | gr_draw = gr_backend->flip(gr_backend); |
| 473 | } |
| 474 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 475 | int gr_init(void) |
| 476 | { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 477 | gr_init_font(); |
Ethan Yonker | a59da09 | 2015-10-13 19:35:05 -0500 | [diff] [blame] | 478 | gr_draw = NULL; |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 479 | |
Ethan Yonker | a59da09 | 2015-10-13 19:35:05 -0500 | [diff] [blame] | 480 | gr_backend = open_overlay(); |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 481 | if (gr_backend) { |
| 482 | gr_draw = gr_backend->init(gr_backend); |
| 483 | if (!gr_draw) { |
| 484 | gr_backend->exit(gr_backend); |
Ethan Yonker | a59da09 | 2015-10-13 19:35:05 -0500 | [diff] [blame] | 485 | } else |
| 486 | printf("Using overlay graphics.\n"); |
| 487 | } |
| 488 | |
| 489 | #ifndef MSM_BSP |
| 490 | if (!gr_draw) { |
| 491 | gr_backend = open_adf(); |
| 492 | if (gr_backend) { |
| 493 | gr_draw = gr_backend->init(gr_backend); |
| 494 | if (!gr_draw) { |
| 495 | gr_backend->exit(gr_backend); |
| 496 | } else |
| 497 | printf("Using adf graphics.\n"); |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 498 | } |
| 499 | } |
Ethan Yonker | a59da09 | 2015-10-13 19:35:05 -0500 | [diff] [blame] | 500 | #else |
| 501 | printf("Skipping adf graphics because TW_TARGET_USES_QCOM_BSP := true\n"); |
| 502 | #endif |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 503 | |
| 504 | if (!gr_draw) { |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 505 | gr_backend = open_drm(); |
| 506 | gr_draw = gr_backend->init(gr_backend); |
Ethan Yonker | a59da09 | 2015-10-13 19:35:05 -0500 | [diff] [blame] | 507 | if (gr_draw) |
| 508 | printf("Using drm graphics.\n"); |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | if (!gr_draw) { |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 512 | gr_backend = open_fbdev(); |
| 513 | gr_draw = gr_backend->init(gr_backend); |
| 514 | if (gr_draw == NULL) { |
| 515 | return -1; |
Ethan Yonker | a59da09 | 2015-10-13 19:35:05 -0500 | [diff] [blame] | 516 | } else |
| 517 | printf("Using fbdev graphics.\n"); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 518 | } |
| 519 | |
Doug Zongker | 5290f20 | 2014-03-11 13:22:04 -0700 | [diff] [blame] | 520 | overscan_offset_x = gr_draw->width * overscan_percent / 100; |
| 521 | overscan_offset_y = gr_draw->height * overscan_percent / 100; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 522 | |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 523 | gr_flip(); |
| 524 | gr_flip(); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 525 | |
| 526 | return 0; |
| 527 | } |
| 528 | |
| 529 | void gr_exit(void) |
| 530 | { |
Doug Zongker | 5290f20 | 2014-03-11 13:22:04 -0700 | [diff] [blame] | 531 | gr_backend->exit(gr_backend); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | int gr_fb_width(void) |
| 535 | { |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 536 | return gr_draw->width - 2*overscan_offset_x; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | int gr_fb_height(void) |
| 540 | { |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 541 | return gr_draw->height - 2*overscan_offset_y; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 542 | } |
Dima Zavin | 4daf48a | 2011-08-30 11:59:20 -0700 | [diff] [blame] | 543 | |
| 544 | void gr_fb_blank(bool blank) |
| 545 | { |
Doug Zongker | 5290f20 | 2014-03-11 13:22:04 -0700 | [diff] [blame] | 546 | gr_backend->blank(gr_backend, blank); |
Dima Zavin | 4daf48a | 2011-08-30 11:59:20 -0700 | [diff] [blame] | 547 | } |
nailyk | c45b2d5 | 2016-08-19 13:27:12 +0200 | [diff] [blame] | 548 | |
| 549 | void gr_set_font(__attribute__ ((unused))const char* name) { |
| 550 | //this cm function is made to change font. Don't care, just init the font: |
| 551 | gr_init_font(); |
| 552 | return; |
| 553 | } |