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