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 | |
Tao Bao | 0ecbd76 | 2017-01-16 21:16:58 -0800 | [diff] [blame] | 17 | #include "graphics.h" |
| 18 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 19 | #include <stdint.h> |
Tao Bao | e8020f4 | 2017-02-03 09:30:07 -0800 | [diff] [blame] | 20 | #include <stdio.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 21 | #include <stdlib.h> |
Elliott Hughes | cd3c55a | 2015-01-29 20:50:08 -0800 | [diff] [blame] | 22 | #include <string.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 23 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 24 | #include <memory> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 25 | |
maxwen | 39624d4 | 2015-12-01 22:02:07 +0100 | [diff] [blame] | 26 | #ifdef BOARD_USE_CUSTOM_RECOVERY_FONT |
| 27 | #include BOARD_USE_CUSTOM_RECOVERY_FONT |
| 28 | #else |
Doug Zongker | 6fd59ac | 2013-03-06 15:01:11 -0800 | [diff] [blame] | 29 | #include "font_10x18.h" |
maxwen | 39624d4 | 2015-12-01 22:02:07 +0100 | [diff] [blame] | 30 | #endif |
| 31 | |
Andreas Schneider | 58d68e5 | 2017-11-02 17:54:36 +0100 | [diff] [blame] | 32 | #ifndef MSM_BSP |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 33 | #include "graphics_adf.h" |
Andreas Schneider | 58d68e5 | 2017-11-02 17:54:36 +0100 | [diff] [blame] | 34 | #endif |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 35 | #include "graphics_drm.h" |
| 36 | #include "graphics_fbdev.h" |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 37 | #include "graphics_overlay.h" |
Tao Bao | 0ecbd76 | 2017-01-16 21:16:58 -0800 | [diff] [blame] | 38 | #include "minui/minui.h" |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 39 | |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 40 | static GRFont* gr_font = NULL; |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 41 | static MinuiBackend* gr_backend = nullptr; |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 42 | |
Doug Zongker | c560a67 | 2012-12-18 16:31:27 -0800 | [diff] [blame] | 43 | static int overscan_percent = OVERSCAN_PERCENT; |
| 44 | static int overscan_offset_x = 0; |
| 45 | static int overscan_offset_y = 0; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 46 | |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 47 | #ifdef TW_NO_MINUI_CUSTOM_FONTS |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 48 | static unsigned char gr_current_r = 255; |
| 49 | static unsigned char gr_current_g = 255; |
| 50 | static unsigned char gr_current_b = 255; |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 51 | #endif |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 52 | static unsigned char gr_current_a = 255; |
Ethan Yonker | e96182e | 2015-10-13 19:32:03 -0500 | [diff] [blame] | 53 | static unsigned char rgb_555[2]; |
| 54 | static unsigned char gr_current_r5 = 31; |
| 55 | static unsigned char gr_current_g5 = 63; |
| 56 | static unsigned char gr_current_b5 = 31; |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 57 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 58 | static uint32_t gr_current = ~0; |
| 59 | static constexpr uint32_t alpha_mask = 0xff000000; |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 60 | |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 61 | static GRSurface* gr_draw = NULL; |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 62 | static GRRotation rotation = ROTATION_NONE; |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 63 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 64 | static bool outside(int x, int y) { |
| 65 | return x < 0 || x >= (rotation % 2 ? gr_draw->height : gr_draw->width) || y < 0 || |
| 66 | y >= (rotation % 2 ? gr_draw->width : gr_draw->height); |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 67 | } |
Ethan Yonker | 39662b2 | 2017-05-23 08:34:02 -0500 | [diff] [blame] | 68 | |
| 69 | #ifdef TW_NO_MINUI_CUSTOM_FONTS |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 70 | int gr_measure(const char *s) |
| 71 | { |
Ethan Yonker | 84d61ce | 2017-05-10 16:11:35 -0500 | [diff] [blame] | 72 | return gr_font->char_width * strlen(s); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 73 | } |
| 74 | |
Dima Zavin | 3c7f00e | 2011-08-30 11:58:24 -0700 | [diff] [blame] | 75 | void gr_font_size(int *x, int *y) |
| 76 | { |
Ethan Yonker | 84d61ce | 2017-05-10 16:11:35 -0500 | [diff] [blame] | 77 | *x = gr_font->char_width; |
| 78 | *y = gr_font->char_height; |
Dima Zavin | 3c7f00e | 2011-08-30 11:58:24 -0700 | [diff] [blame] | 79 | } |
Ethan Yonker | 84d61ce | 2017-05-10 16:11:35 -0500 | [diff] [blame] | 80 | #else // TW_USE_MINUI_CUSTOM_FONTS |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 81 | const GRFont* gr_sys_font() { |
| 82 | return gr_font; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 83 | } |
| 84 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 85 | int gr_measure(const GRFont* font, const char* s) { |
| 86 | return font->char_width * strlen(s); |
Damien Bargiacchi | 35fff61 | 2016-08-11 15:57:03 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 89 | void gr_font_size(const GRFont* font, int* x, int* y) { |
| 90 | *x = font->char_width; |
| 91 | *y = font->char_height; |
Dima Zavin | 3c7f00e | 2011-08-30 11:58:24 -0700 | [diff] [blame] | 92 | } |
Ethan Yonker | 39662b2 | 2017-05-23 08:34:02 -0500 | [diff] [blame] | 93 | #endif // TW_NO_MINUI_CUSTOM_FONTS |
Dima Zavin | 3c7f00e | 2011-08-30 11:58:24 -0700 | [diff] [blame] | 94 | |
Ethan Yonker | e96182e | 2015-10-13 19:32:03 -0500 | [diff] [blame] | 95 | void blend_16bpp(unsigned char* px, unsigned r5, unsigned g5, unsigned b5, unsigned char a) |
| 96 | { |
| 97 | unsigned char orig[2]; |
| 98 | orig[0] = px[0]; |
| 99 | orig[1] = px[1]; |
| 100 | |
| 101 | /* This code is a little easier to read |
| 102 | unsigned oldred = (orig[1] >> 3); |
| 103 | unsigned oldgreen = (((orig[0] >> 5) << 3) + (orig[1] & 0x7)); |
| 104 | unsigned oldblue = (orig[0] & 0x1F); |
| 105 | |
| 106 | unsigned newred = (oldred * (255-a) + r5 * a) / 255; |
| 107 | unsigned newgreen = (oldgreen * (255-a) + g5 * a) / 255; |
| 108 | unsigned newblue = (oldblue * (255-a) + b5 * a) / 255; |
| 109 | */ |
| 110 | |
| 111 | unsigned newred = ((orig[1] >> 3) * (255-a) + r5 * a) / 255; |
| 112 | unsigned newgreen = ((((orig[0] >> 5) << 3) + (orig[1] & 0x7)) * (255-a) + g5 * a) / 255; |
| 113 | unsigned newblue = ((orig[0] & 0x1F) * (255-a) + b5 * a) / 255; |
| 114 | |
| 115 | *px++ = (newgreen << 5) + (newblue); |
| 116 | *px++ = (newred << 3) + (newgreen >> 3); |
| 117 | } |
| 118 | |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 119 | #ifdef TW_NO_MINUI_CUSTOM_FONTS |
| 120 | static void text_blend_old(unsigned char* src_p, int src_row_bytes, |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 121 | unsigned char* dst_p, int dst_row_bytes, |
| 122 | int width, int height) |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 123 | { |
Elliott Hughes | 01a4d08 | 2015-03-24 15:21:48 -0700 | [diff] [blame] | 124 | for (int j = 0; j < height; ++j) { |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 125 | unsigned char* sx = src_p; |
| 126 | unsigned char* px = dst_p; |
Elliott Hughes | 01a4d08 | 2015-03-24 15:21:48 -0700 | [diff] [blame] | 127 | for (int i = 0; i < width; ++i) { |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 128 | unsigned char a = *sx++; |
| 129 | if (gr_current_a < 255) a = ((int)a * gr_current_a) / 255; |
| 130 | if (a == 255) { |
Ethan Yonker | e96182e | 2015-10-13 19:32:03 -0500 | [diff] [blame] | 131 | if (gr_draw->pixel_bytes == 2) { |
| 132 | *px++ = rgb_555[0]; |
| 133 | *px++ = rgb_555[1]; |
| 134 | } else { |
| 135 | *px++ = gr_current_r; |
| 136 | *px++ = gr_current_g; |
| 137 | *px++ = gr_current_b; |
| 138 | px++; |
| 139 | } |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 140 | } else if (a > 0) { |
Ethan Yonker | e96182e | 2015-10-13 19:32:03 -0500 | [diff] [blame] | 141 | if (gr_draw->pixel_bytes == 2) { |
| 142 | blend_16bpp(px, gr_current_r5, gr_current_g5, gr_current_b5, a); |
| 143 | px += gr_draw->pixel_bytes; |
| 144 | } else { |
| 145 | *px = (*px * (255-a) + gr_current_r * a) / 255; |
| 146 | ++px; |
| 147 | *px = (*px * (255-a) + gr_current_g * a) / 255; |
| 148 | ++px; |
| 149 | *px = (*px * (255-a) + gr_current_b * a) / 255; |
| 150 | ++px; |
| 151 | ++px; |
| 152 | } |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 153 | } else { |
Ethan Yonker | e96182e | 2015-10-13 19:32:03 -0500 | [diff] [blame] | 154 | px += gr_draw->pixel_bytes; |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | src_p += src_row_bytes; |
| 158 | dst_p += dst_row_bytes; |
| 159 | } |
| 160 | } |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 161 | #endif // TW_NO_MINUI_CUSTOM_FONTS |
| 162 | |
| 163 | // Blends gr_current onto pix value, assumes alpha as most significant byte. |
| 164 | static inline uint16_t pixel_blend16(uint8_t a, uint16_t pix) { |
| 165 | unsigned char orig[2]; |
| 166 | orig[0] = (pix & 0xFF00) >> 8; |
| 167 | orig[1] = pix & 0x00FF; |
| 168 | |
| 169 | /* This code is a little easier to read |
| 170 | unsigned oldred = (orig[1] >> 3); |
| 171 | unsigned oldgreen = (((orig[0] >> 5) << 3) + (orig[1] & 0x7)); |
| 172 | unsigned oldblue = (orig[0] & 0x1F); |
| 173 | |
| 174 | unsigned newred = (oldred * (255-a) + r5 * a) / 255; |
| 175 | unsigned newgreen = (oldgreen * (255-a) + g5 * a) / 255; |
| 176 | unsigned newblue = (oldblue * (255-a) + b5 * a) / 255; |
| 177 | */ |
| 178 | |
| 179 | unsigned newred = ((orig[1] >> 3) * (255-a) + gr_current_r5 * a) / 255; |
| 180 | unsigned newgreen = ((((orig[0] >> 5) << 3) + (orig[1] & 0x7)) * (255-a) + gr_current_g5 * a) / 255; |
| 181 | unsigned newblue = ((orig[0] & 0x1F) * (255-a) + gr_current_b5 * a) / 255; |
| 182 | |
| 183 | uint16_t newpix = (newred << 10) + (newgreen << 5) + newblue; |
| 184 | return newpix; |
| 185 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 186 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 187 | // Blends gr_current onto pix value, assumes alpha as most significant byte. |
| 188 | static inline uint32_t pixel_blend(uint8_t alpha, uint32_t pix) { |
| 189 | if (alpha == 255) return gr_current; |
| 190 | if (alpha == 0) return pix; |
| 191 | uint32_t pix_r = pix & 0xff; |
| 192 | uint32_t pix_g = pix & 0xff00; |
| 193 | uint32_t pix_b = pix & 0xff0000; |
| 194 | uint32_t cur_r = gr_current & 0xff; |
| 195 | uint32_t cur_g = gr_current & 0xff00; |
| 196 | uint32_t cur_b = gr_current & 0xff0000; |
| 197 | |
| 198 | uint32_t out_r = (pix_r * (255 - alpha) + cur_r * alpha) / 255; |
| 199 | uint32_t out_g = (pix_g * (255 - alpha) + cur_g * alpha) / 255; |
| 200 | uint32_t out_b = (pix_b * (255 - alpha) + cur_b * alpha) / 255; |
| 201 | |
| 202 | return (out_r & 0xff) | (out_g & 0xff00) | (out_b & 0xff0000) | (gr_current & 0xff000000); |
| 203 | } |
| 204 | |
| 205 | // increments pixel pointer right, with current rotation. |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 206 | static void incr_x16(uint16_t** p, int row_pixels) { |
| 207 | if (rotation % 2) { |
| 208 | *p = *p + (rotation == 1 ? 1 : -1) * row_pixels; |
| 209 | } else { |
| 210 | *p = *p + (rotation ? -1 : 1); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | // increments pixel pointer down, with current rotation. |
| 215 | static void incr_y16(uint16_t** p, int row_pixels) { |
| 216 | if (rotation % 2) { |
| 217 | *p = *p + (rotation == 1 ? -1 : 1); |
| 218 | } else { |
| 219 | *p = *p + (rotation ? -1 : 1) * row_pixels; |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | // increments pixel pointer right, with current rotation. |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 224 | static void incr_x(uint32_t** p, int row_pixels) { |
| 225 | if (rotation % 2) { |
| 226 | *p = *p + (rotation == 1 ? 1 : -1) * row_pixels; |
| 227 | } else { |
| 228 | *p = *p + (rotation ? -1 : 1); |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | // increments pixel pointer down, with current rotation. |
| 233 | static void incr_y(uint32_t** p, int row_pixels) { |
| 234 | if (rotation % 2) { |
| 235 | *p = *p + (rotation == 1 ? -1 : 1); |
| 236 | } else { |
| 237 | *p = *p + (rotation ? -1 : 1) * row_pixels; |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | // returns pixel pointer at given coordinates with rotation adjustment. |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 242 | static uint16_t* pixel_at16(GRSurface* surf, int x, int y, int row_pixels) { |
| 243 | switch (rotation) { |
| 244 | case ROTATION_NONE: |
| 245 | return reinterpret_cast<uint16_t*>(surf->data) + y * row_pixels + x; |
| 246 | case ROTATION_RIGHT: |
| 247 | return reinterpret_cast<uint16_t*>(surf->data) + x * row_pixels + (surf->width - y); |
| 248 | case ROTATION_DOWN: |
| 249 | return reinterpret_cast<uint16_t*>(surf->data) + (surf->height - 1 - y) * row_pixels + |
| 250 | (surf->width - 1 - x); |
| 251 | case ROTATION_LEFT: |
| 252 | return reinterpret_cast<uint16_t*>(surf->data) + (surf->height - 1 - x) * row_pixels + y; |
| 253 | default: |
| 254 | printf("invalid rotation %d", rotation); |
| 255 | } |
| 256 | return nullptr; |
| 257 | } |
| 258 | |
| 259 | // returns pixel pointer at given coordinates with rotation adjustment. |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 260 | static uint32_t* pixel_at(GRSurface* surf, int x, int y, int row_pixels) { |
| 261 | switch (rotation) { |
| 262 | case ROTATION_NONE: |
| 263 | return reinterpret_cast<uint32_t*>(surf->data) + y * row_pixels + x; |
| 264 | case ROTATION_RIGHT: |
| 265 | return reinterpret_cast<uint32_t*>(surf->data) + x * row_pixels + (surf->width - y); |
| 266 | case ROTATION_DOWN: |
| 267 | return reinterpret_cast<uint32_t*>(surf->data) + (surf->height - 1 - y) * row_pixels + |
| 268 | (surf->width - 1 - x); |
| 269 | case ROTATION_LEFT: |
| 270 | return reinterpret_cast<uint32_t*>(surf->data) + (surf->height - 1 - x) * row_pixels + y; |
| 271 | default: |
| 272 | printf("invalid rotation %d", rotation); |
| 273 | } |
| 274 | return nullptr; |
| 275 | } |
| 276 | |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 277 | static void text_blend16(uint8_t* src_p, int src_row_bytes, uint16_t* dst_p, int dst_row_pixels, |
| 278 | int width, int height) { |
| 279 | uint8_t alpha_current = static_cast<uint8_t>((alpha_mask & gr_current) >> 24); |
| 280 | for (int j = 0; j < height; ++j) { |
| 281 | uint8_t* sx = src_p; |
| 282 | uint16_t* px = dst_p; |
| 283 | for (int i = 0; i < width; ++i, incr_x16(&px, dst_row_pixels)) { |
| 284 | uint8_t a = *sx++; |
| 285 | if (alpha_current < 255) a = (static_cast<uint32_t>(a) * alpha_current) / 255; |
| 286 | *px = pixel_blend16(a, *px); |
| 287 | } |
| 288 | src_p += src_row_bytes; |
| 289 | incr_y16(&dst_p, dst_row_pixels); |
| 290 | } |
| 291 | } |
| 292 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 293 | static void text_blend(uint8_t* src_p, int src_row_bytes, uint32_t* dst_p, int dst_row_pixels, |
| 294 | int width, int height) { |
| 295 | uint8_t alpha_current = static_cast<uint8_t>((alpha_mask & gr_current) >> 24); |
| 296 | for (int j = 0; j < height; ++j) { |
| 297 | uint8_t* sx = src_p; |
| 298 | uint32_t* px = dst_p; |
| 299 | for (int i = 0; i < width; ++i, incr_x(&px, dst_row_pixels)) { |
| 300 | uint8_t a = *sx++; |
| 301 | if (alpha_current < 255) a = (static_cast<uint32_t>(a) * alpha_current) / 255; |
| 302 | *px = pixel_blend(a, *px); |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 303 | } |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 304 | src_p += src_row_bytes; |
| 305 | incr_y(&dst_p, dst_row_pixels); |
| 306 | } |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 307 | } |
| 308 | |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 309 | |
Ethan Yonker | 39662b2 | 2017-05-23 08:34:02 -0500 | [diff] [blame] | 310 | #ifdef TW_NO_MINUI_CUSTOM_FONTS |
Elliott Hughes | 8fd86d7 | 2015-04-13 14:36:02 -0700 | [diff] [blame] | 311 | void gr_text(int x, int y, const char *s, bool bold) |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 312 | { |
Elliott Hughes | 01a4d08 | 2015-03-24 15:21:48 -0700 | [diff] [blame] | 313 | GRFont* font = gr_font; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 314 | |
Elliott Hughes | 01a4d08 | 2015-03-24 15:21:48 -0700 | [diff] [blame] | 315 | if (!font->texture || gr_current_a == 0) return; |
Doug Zongker | 55a36ac | 2013-03-04 15:49:02 -0800 | [diff] [blame] | 316 | |
Ethan Yonker | 84d61ce | 2017-05-10 16:11:35 -0500 | [diff] [blame] | 317 | bold = bold && (font->texture->height != font->char_height); |
Doug Zongker | 6fd59ac | 2013-03-06 15:01:11 -0800 | [diff] [blame] | 318 | |
Doug Zongker | c560a67 | 2012-12-18 16:31:27 -0800 | [diff] [blame] | 319 | x += overscan_offset_x; |
| 320 | y += overscan_offset_y; |
| 321 | |
Elliott Hughes | 01a4d08 | 2015-03-24 15:21:48 -0700 | [diff] [blame] | 322 | unsigned char ch; |
| 323 | while ((ch = *s++)) { |
Ethan Yonker | 84d61ce | 2017-05-10 16:11:35 -0500 | [diff] [blame] | 324 | 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] | 325 | |
Elliott Hughes | 01a4d08 | 2015-03-24 15:21:48 -0700 | [diff] [blame] | 326 | if (ch < ' ' || ch > '~') { |
| 327 | ch = '?'; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 328 | } |
Elliott Hughes | 01a4d08 | 2015-03-24 15:21:48 -0700 | [diff] [blame] | 329 | |
Ethan Yonker | 84d61ce | 2017-05-10 16:11:35 -0500 | [diff] [blame] | 330 | unsigned char* src_p = font->texture->data + ((ch - ' ') * font->char_width) + |
| 331 | (bold ? font->char_height * font->texture->row_bytes : 0); |
Elliott Hughes | 01a4d08 | 2015-03-24 15:21:48 -0700 | [diff] [blame] | 332 | unsigned char* dst_p = gr_draw->data + y*gr_draw->row_bytes + x*gr_draw->pixel_bytes; |
| 333 | |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 334 | text_blend_old(src_p, font->texture->row_bytes, |
| 335 | dst_p, gr_draw->row_bytes, |
| 336 | font->char_width, font->char_height); |
Elliott Hughes | 01a4d08 | 2015-03-24 15:21:48 -0700 | [diff] [blame] | 337 | |
Ethan Yonker | 84d61ce | 2017-05-10 16:11:35 -0500 | [diff] [blame] | 338 | x += font->char_width; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 339 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 340 | } |
Ethan Yonker | 39662b2 | 2017-05-23 08:34:02 -0500 | [diff] [blame] | 341 | #else //TW_NO_MINUI_CUSTOM_FONTS |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 342 | void gr_text(const GRFont* font, int x, int y, const char* s, bool bold) { |
| 343 | if (!font || !font->texture || (gr_current & alpha_mask) == 0) return; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 344 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 345 | if (font->texture->pixel_bytes != 1) { |
| 346 | printf("gr_text: font has wrong format\n"); |
| 347 | return; |
| 348 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 349 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 350 | bold = bold && (font->texture->height != font->char_height); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 351 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 352 | x += overscan_offset_x; |
| 353 | y += overscan_offset_y; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 354 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 355 | unsigned char ch; |
| 356 | while ((ch = *s++)) { |
| 357 | if (outside(x, y) || outside(x + font->char_width - 1, y + font->char_height - 1)) break; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 358 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 359 | if (ch < ' ' || ch > '~') { |
| 360 | ch = '?'; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 361 | } |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 362 | |
| 363 | int row_pixels = gr_draw->row_bytes / gr_draw->pixel_bytes; |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 364 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 365 | uint8_t* src_p = font->texture->data + ((ch - ' ') * font->char_width) + |
| 366 | (bold ? font->char_height * font->texture->row_bytes : 0); |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 367 | if (gr_draw->pixel_bytes == 2) { |
| 368 | uint16_t* dst_p = pixel_at16(gr_draw, x, y, row_pixels); |
| 369 | |
| 370 | text_blend16(src_p, font->texture->row_bytes, dst_p, row_pixels, font->char_width, |
| 371 | font->char_height); |
| 372 | } else { // not indenting AOSP original code |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 373 | uint32_t* dst_p = pixel_at(gr_draw, x, y, row_pixels); |
| 374 | |
| 375 | text_blend(src_p, font->texture->row_bytes, dst_p, row_pixels, font->char_width, |
| 376 | font->char_height); |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 377 | } |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 378 | x += font->char_width; |
| 379 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 380 | } |
Ethan Yonker | 39662b2 | 2017-05-23 08:34:02 -0500 | [diff] [blame] | 381 | #endif //TW_NO_MINUI_CUSTOM_FONTS |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 382 | |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 383 | void gr_texticon(int x, int y, GRSurface* icon) { |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 384 | if (icon == NULL) return; |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 385 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 386 | if (icon->pixel_bytes != 1) { |
| 387 | printf("gr_texticon: source has wrong format\n"); |
| 388 | return; |
| 389 | } |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 390 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 391 | x += overscan_offset_x; |
| 392 | y += overscan_offset_y; |
Doug Zongker | c560a67 | 2012-12-18 16:31:27 -0800 | [diff] [blame] | 393 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 394 | 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] | 395 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 396 | int row_pixels = gr_draw->row_bytes / gr_draw->pixel_bytes; |
| 397 | uint8_t* src_p = icon->data; |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 398 | if (gr_draw->pixel_bytes == 2) { |
| 399 | uint16_t* dst_p = pixel_at16(gr_draw, x, y, row_pixels); |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 400 | |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 401 | text_blend16(src_p, icon->row_bytes, dst_p, row_pixels, icon->width, icon->height); |
| 402 | return; |
| 403 | } |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 404 | uint32_t* dst_p = pixel_at(gr_draw, x, y, row_pixels); |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 405 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 406 | text_blend(src_p, icon->row_bytes, dst_p, row_pixels, icon->width, icon->height); |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 407 | } |
| 408 | |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 409 | void gr_convert_rgb_555(unsigned char r, unsigned char g, unsigned char b) |
Ethan Yonker | e96182e | 2015-10-13 19:32:03 -0500 | [diff] [blame] | 410 | { |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 411 | gr_current_r5 = (((r & 0xFF) * 0x1F) + 0x7F) / 0xFF; |
| 412 | gr_current_g5 = (((g & 0xFF) * 0x3F) + 0x7F) / 0xFF; |
| 413 | gr_current_b5 = (((b & 0xFF) * 0x1F) + 0x7F) / 0xFF; |
Ethan Yonker | e96182e | 2015-10-13 19:32:03 -0500 | [diff] [blame] | 414 | |
| 415 | rgb_555[0] = (gr_current_g5 << 5) + (gr_current_b5); |
| 416 | rgb_555[1] = (gr_current_r5 << 3) + (gr_current_g5 >> 3); |
| 417 | } |
| 418 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 419 | void gr_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a) { |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 420 | #ifdef TW_NO_MINUI_CUSTOM_FONTS |
Tony Kuo | fd778e3 | 2015-02-05 21:25:56 +0800 | [diff] [blame] | 421 | #if defined(RECOVERY_ABGR) || defined(RECOVERY_BGRA) |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 422 | gr_current_r = b; |
| 423 | gr_current_g = g; |
| 424 | gr_current_b = r; |
Tony Kuo | fd778e3 | 2015-02-05 21:25:56 +0800 | [diff] [blame] | 425 | #else |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 426 | gr_current_r = r; |
| 427 | gr_current_g = g; |
| 428 | gr_current_b = b; |
Tony Kuo | fd778e3 | 2015-02-05 21:25:56 +0800 | [diff] [blame] | 429 | #endif |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 430 | #endif |
| 431 | |
| 432 | if (gr_draw->pixel_bytes == 2) { |
| 433 | gr_current_a = a; |
| 434 | gr_convert_rgb_555(r, g, b); |
| 435 | return; |
| 436 | } |
| 437 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 438 | uint32_t r32 = r, g32 = g, b32 = b, a32 = a; |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 439 | #if defined(RECOVERY_ABGR) || defined(RECOVERY_BGRA) |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 440 | gr_current = (a32 << 24) | (r32 << 16) | (g32 << 8) | b32; |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 441 | #else |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 442 | gr_current = (a32 << 24) | (b32 << 16) | (g32 << 8) | r32; |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 443 | #endif |
| 444 | } |
| 445 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 446 | void gr_clear() { |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 447 | if (gr_draw->pixel_bytes == 2) { |
| 448 | gr_fill(0, 0, gr_fb_width(), gr_fb_height()); |
| 449 | return; |
| 450 | } |
Ethan Yonker | e96182e | 2015-10-13 19:32:03 -0500 | [diff] [blame] | 451 | |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 452 | // This code only works on 32bpp devices |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 453 | if ((gr_current & 0xff) == ((gr_current >> 8) & 0xff) && |
| 454 | (gr_current & 0xff) == ((gr_current >> 16) & 0xff) && |
| 455 | (gr_current & 0xff) == ((gr_current >> 24) & 0xff) && |
| 456 | gr_draw->row_bytes == gr_draw->width * gr_draw->pixel_bytes) { |
| 457 | memset(gr_draw->data, gr_current & 0xff, gr_draw->height * gr_draw->row_bytes); |
| 458 | } else { |
| 459 | uint32_t* px = reinterpret_cast<uint32_t*>(gr_draw->data); |
| 460 | int row_diff = gr_draw->row_bytes / gr_draw->pixel_bytes - gr_draw->width; |
| 461 | for (int y = 0; y < gr_draw->height; ++y) { |
| 462 | for (int x = 0; x < gr_draw->width; ++x) { |
| 463 | *px++ = gr_current; |
| 464 | } |
| 465 | px += row_diff; |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 466 | } |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 467 | } |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 468 | } |
| 469 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 470 | void gr_fill(int x1, int y1, int x2, int y2) { |
| 471 | x1 += overscan_offset_x; |
| 472 | y1 += overscan_offset_y; |
Doug Zongker | c560a67 | 2012-12-18 16:31:27 -0800 | [diff] [blame] | 473 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 474 | x2 += overscan_offset_x; |
| 475 | y2 += overscan_offset_y; |
Doug Zongker | c560a67 | 2012-12-18 16:31:27 -0800 | [diff] [blame] | 476 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 477 | if (outside(x1, y1) || outside(x2 - 1, y2 - 1)) return; |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 478 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 479 | int row_pixels = gr_draw->row_bytes / gr_draw->pixel_bytes; |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 480 | if (gr_draw->pixel_bytes == 2) { |
| 481 | uint16_t* p = pixel_at16(gr_draw, x1, y1, row_pixels); |
| 482 | uint8_t alpha = static_cast<uint8_t>(((gr_current & alpha_mask) >> 24)); |
| 483 | if (alpha > 0) { |
| 484 | for (int y = y1; y < y2; ++y) { |
| 485 | uint16_t* px = p; |
| 486 | for (int x = x1; x < x2; ++x) { |
| 487 | *px = pixel_blend16(alpha, *px); |
| 488 | incr_x16(&px, row_pixels); |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 489 | } |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 490 | incr_y16(&p, row_pixels); |
| 491 | } |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 492 | } |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 493 | return; |
| 494 | } |
| 495 | { // open brace to maintain separation between uint16_t p and uint32_t p |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 496 | uint32_t* p = pixel_at(gr_draw, x1, y1, row_pixels); |
| 497 | uint8_t alpha = static_cast<uint8_t>(((gr_current & alpha_mask) >> 24)); |
| 498 | if (alpha > 0) { |
| 499 | for (int y = y1; y < y2; ++y) { |
| 500 | uint32_t* px = p; |
| 501 | for (int x = x1; x < x2; ++x) { |
| 502 | *px = pixel_blend(alpha, *px); |
| 503 | incr_x(&px, row_pixels); |
| 504 | } |
| 505 | incr_y(&p, row_pixels); |
Doug Zongker | c560a67 | 2012-12-18 16:31:27 -0800 | [diff] [blame] | 506 | } |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 507 | } |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 508 | } // close brace to maintain separation between uint16_t p and uint32_t p |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 509 | } |
| 510 | |
Ethan Yonker | e96182e | 2015-10-13 19:32:03 -0500 | [diff] [blame] | 511 | void gr_blit_32to16(GRSurface* source, int sx, int sy, int w, int h, int dx, int dy) { |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 512 | if (rotation) |
| 513 | printf("gr_blit_32to16 does not support rotation!\n"); // but we'll draw something in the wrong spot anyway because, why not! |
Ethan Yonker | e96182e | 2015-10-13 19:32:03 -0500 | [diff] [blame] | 514 | |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 515 | dx += overscan_offset_x; |
| 516 | dy += overscan_offset_y; |
Ethan Yonker | e96182e | 2015-10-13 19:32:03 -0500 | [diff] [blame] | 517 | |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 518 | if (outside(dx, dy) || outside(dx+w-1, dy+h-1)) return; |
Ethan Yonker | e96182e | 2015-10-13 19:32:03 -0500 | [diff] [blame] | 519 | |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 520 | unsigned char* src_p = source->data + sy*source->row_bytes + sx*source->pixel_bytes; |
| 521 | unsigned char* dst_p = gr_draw->data + dy*gr_draw->row_bytes + dx*gr_draw->pixel_bytes; |
Ethan Yonker | e96182e | 2015-10-13 19:32:03 -0500 | [diff] [blame] | 522 | |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 523 | int i, j; |
| 524 | for (i = 0; i < h; ++i) { |
| 525 | unsigned char* spx = src_p; |
| 526 | unsigned char* dpx = dst_p; |
Ethan Yonker | e96182e | 2015-10-13 19:32:03 -0500 | [diff] [blame] | 527 | |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 528 | for (j = 0; j < w; ++j) { |
| 529 | unsigned a = spx[3]; |
| 530 | |
| 531 | if (a == 0) { |
| 532 | spx += source->pixel_bytes; |
| 533 | dpx += gr_draw->pixel_bytes; |
| 534 | } else { |
| 535 | unsigned r5 = (((*spx++ & 0xFF) * 0x1F) + 0x7F) / 0xFF; |
| 536 | unsigned g5 = (((*spx++ & 0xFF) * 0x3F) + 0x7F) / 0xFF; |
| 537 | unsigned b5 = (((*spx++ & 0xFF) * 0x1F) + 0x7F) / 0xFF; |
| 538 | spx++; |
| 539 | if (a == 255) { |
| 540 | *dpx++ = (g5 << 5) + (b5); |
| 541 | *dpx++ = (r5 << 3) + (g5 >> 3); |
| 542 | } else { |
| 543 | blend_16bpp(dpx, r5, g5, b5, a); |
| 544 | spx += source->pixel_bytes; |
Ethan Yonker | e96182e | 2015-10-13 19:32:03 -0500 | [diff] [blame] | 545 | } |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 546 | } |
Ethan Yonker | e96182e | 2015-10-13 19:32:03 -0500 | [diff] [blame] | 547 | } |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 548 | src_p += source->row_bytes; |
| 549 | dst_p += gr_draw->row_bytes; |
| 550 | } |
Ethan Yonker | e96182e | 2015-10-13 19:32:03 -0500 | [diff] [blame] | 551 | } |
| 552 | |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 553 | void gr_blit(GRSurface* source, int sx, int sy, int w, int h, int dx, int dy) { |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 554 | if (source == NULL) return; |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 555 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 556 | if (gr_draw->pixel_bytes != source->pixel_bytes) { |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 557 | if (gr_draw->pixel_bytes == 2 && source->pixel_bytes == 4) { |
| 558 | gr_blit_32to16(source, sx, sy, w, h, dx, dy); |
| 559 | return; |
| 560 | } else { |
| 561 | printf("gr_blit: source has wrong format\n"); |
| 562 | return; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 563 | } |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 564 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 565 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 566 | dx += overscan_offset_x; |
| 567 | dy += overscan_offset_y; |
Doug Zongker | c560a67 | 2012-12-18 16:31:27 -0800 | [diff] [blame] | 568 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 569 | if (outside(dx, dy) || outside(dx + w - 1, dy + h - 1)) return; |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 570 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 571 | if (rotation) { |
| 572 | int src_row_pixels = source->row_bytes / source->pixel_bytes; |
| 573 | int row_pixels = gr_draw->row_bytes / gr_draw->pixel_bytes; |
| 574 | uint32_t* src_py = reinterpret_cast<uint32_t*>(source->data) + sy * source->row_bytes / 4 + sx; |
| 575 | uint32_t* dst_py = pixel_at(gr_draw, dx, dy, row_pixels); |
| 576 | |
| 577 | for (int y = 0; y < h; y += 1) { |
| 578 | uint32_t* src_px = src_py; |
| 579 | uint32_t* dst_px = dst_py; |
| 580 | for (int x = 0; x < w; x += 1) { |
| 581 | *dst_px = *src_px++; |
| 582 | incr_x(&dst_px, row_pixels); |
| 583 | } |
| 584 | src_py += src_row_pixels; |
| 585 | incr_y(&dst_py, row_pixels); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 586 | } |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 587 | } else { |
| 588 | unsigned char* src_p = source->data + sy * source->row_bytes + sx * source->pixel_bytes; |
| 589 | unsigned char* dst_p = gr_draw->data + dy * gr_draw->row_bytes + dx * gr_draw->pixel_bytes; |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 590 | |
| 591 | int i; |
| 592 | for (i = 0; i < h; ++i) { |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 593 | memcpy(dst_p, src_p, w * source->pixel_bytes); |
| 594 | src_p += source->row_bytes; |
| 595 | dst_p += gr_draw->row_bytes; |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 596 | } |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 597 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 598 | } |
| 599 | |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 600 | unsigned int gr_get_width(GRSurface* surface) { |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 601 | if (surface == NULL) { |
| 602 | return 0; |
| 603 | } |
| 604 | return surface->width; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 605 | } |
| 606 | |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 607 | unsigned int gr_get_height(GRSurface* surface) { |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 608 | if (surface == NULL) { |
| 609 | return 0; |
| 610 | } |
| 611 | return surface->height; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 612 | } |
| 613 | |
Ethan Yonker | 39662b2 | 2017-05-23 08:34:02 -0500 | [diff] [blame] | 614 | #ifdef TW_NO_MINUI_CUSTOM_FONTS |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 615 | static void gr_init_font(void) |
| 616 | { |
Elliott Hughes | 07cfb8f | 2015-04-10 13:12:05 -0700 | [diff] [blame] | 617 | 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] | 618 | |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 619 | int res = res_create_alpha_surface("font", &(gr_font->texture)); |
Doug Zongker | 6fd59ac | 2013-03-06 15:01:11 -0800 | [diff] [blame] | 620 | if (res == 0) { |
| 621 | // The font image should be a 96x2 array of character images. The |
| 622 | // columns are the printable ASCII characters 0x20 - 0x7f. The |
| 623 | // top row is regular text; the bottom row is bold. |
Ethan Yonker | 84d61ce | 2017-05-10 16:11:35 -0500 | [diff] [blame] | 624 | gr_font->char_width = gr_font->texture->width / 96; |
| 625 | gr_font->char_height = gr_font->texture->height / 2; |
Doug Zongker | 6fd59ac | 2013-03-06 15:01:11 -0800 | [diff] [blame] | 626 | } else { |
Doug Zongker | 55a36ac | 2013-03-04 15:49:02 -0800 | [diff] [blame] | 627 | printf("failed to read font: res=%d\n", res); |
Doug Zongker | 6fd59ac | 2013-03-06 15:01:11 -0800 | [diff] [blame] | 628 | |
| 629 | // fall back to the compiled-in font. |
Elliott Hughes | 07cfb8f | 2015-04-10 13:12:05 -0700 | [diff] [blame] | 630 | gr_font->texture = reinterpret_cast<GRSurface*>(malloc(sizeof(*gr_font->texture))); |
Doug Zongker | 6fd59ac | 2013-03-06 15:01:11 -0800 | [diff] [blame] | 631 | gr_font->texture->width = font.width; |
| 632 | gr_font->texture->height = font.height; |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 633 | gr_font->texture->row_bytes = font.width; |
| 634 | gr_font->texture->pixel_bytes = 1; |
Doug Zongker | 6fd59ac | 2013-03-06 15:01:11 -0800 | [diff] [blame] | 635 | |
Elliott Hughes | 07cfb8f | 2015-04-10 13:12:05 -0700 | [diff] [blame] | 636 | unsigned char* bits = reinterpret_cast<unsigned char*>(malloc(font.width * font.height)); |
| 637 | gr_font->texture->data = reinterpret_cast<unsigned char*>(bits); |
Doug Zongker | 6fd59ac | 2013-03-06 15:01:11 -0800 | [diff] [blame] | 638 | |
| 639 | unsigned char data; |
| 640 | unsigned char* in = font.rundata; |
| 641 | while((data = *in++)) { |
| 642 | memset(bits, (data & 0x80) ? 255 : 0, data & 0x7f); |
| 643 | bits += (data & 0x7f); |
| 644 | } |
| 645 | |
Ethan Yonker | 84d61ce | 2017-05-10 16:11:35 -0500 | [diff] [blame] | 646 | gr_font->char_width = font.char_width; |
| 647 | gr_font->char_height = font.char_height; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 648 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 649 | } |
| 650 | |
Ethan Yonker | 84d61ce | 2017-05-10 16:11:35 -0500 | [diff] [blame] | 651 | void gr_set_font(__attribute__ ((unused))const char* name) { |
| 652 | //this cm function is made to change font. Don't care, just init the font: |
| 653 | gr_init_font(); |
| 654 | return; |
| 655 | } |
Ethan Yonker | 39662b2 | 2017-05-23 08:34:02 -0500 | [diff] [blame] | 656 | #else // TW_NO_MINUI_CUSTOM_FONTS |
Damien Bargiacchi | d00f5eb | 2016-09-09 07:14:08 -0700 | [diff] [blame] | 657 | int gr_init_font(const char* name, GRFont** dest) { |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 658 | GRFont* font = static_cast<GRFont*>(calloc(1, sizeof(*gr_font))); |
| 659 | if (font == nullptr) { |
| 660 | return -1; |
| 661 | } |
Damien Bargiacchi | d00f5eb | 2016-09-09 07:14:08 -0700 | [diff] [blame] | 662 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 663 | int res = res_create_alpha_surface(name, &(font->texture)); |
| 664 | if (res < 0) { |
| 665 | free(font); |
| 666 | return res; |
| 667 | } |
Damien Bargiacchi | 35fff61 | 2016-08-11 15:57:03 -0700 | [diff] [blame] | 668 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 669 | // The font image should be a 96x2 array of character images. The |
| 670 | // columns are the printable ASCII characters 0x20 - 0x7f. The |
| 671 | // top row is regular text; the bottom row is bold. |
| 672 | font->char_width = font->texture->width / 96; |
| 673 | font->char_height = font->texture->height / 2; |
Damien Bargiacchi | d00f5eb | 2016-09-09 07:14:08 -0700 | [diff] [blame] | 674 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 675 | *dest = font; |
Damien Bargiacchi | 35fff61 | 2016-08-11 15:57:03 -0700 | [diff] [blame] | 676 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 677 | return 0; |
Damien Bargiacchi | 35fff61 | 2016-08-11 15:57:03 -0700 | [diff] [blame] | 678 | } |
| 679 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 680 | static void gr_init_font(void) { |
| 681 | int res = gr_init_font("font", &gr_font); |
| 682 | if (res == 0) { |
| 683 | return; |
| 684 | } |
Damien Bargiacchi | 35fff61 | 2016-08-11 15:57:03 -0700 | [diff] [blame] | 685 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 686 | printf("failed to read font: res=%d\n", res); |
Damien Bargiacchi | 35fff61 | 2016-08-11 15:57:03 -0700 | [diff] [blame] | 687 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 688 | // fall back to the compiled-in font. |
| 689 | gr_font = static_cast<GRFont*>(calloc(1, sizeof(*gr_font))); |
| 690 | gr_font->texture = static_cast<GRSurface*>(malloc(sizeof(*gr_font->texture))); |
| 691 | gr_font->texture->width = font.width; |
| 692 | gr_font->texture->height = font.height; |
| 693 | gr_font->texture->row_bytes = font.width; |
| 694 | gr_font->texture->pixel_bytes = 1; |
Damien Bargiacchi | d00f5eb | 2016-09-09 07:14:08 -0700 | [diff] [blame] | 695 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 696 | unsigned char* bits = static_cast<unsigned char*>(malloc(font.width * font.height)); |
| 697 | gr_font->texture->data = bits; |
Damien Bargiacchi | 35fff61 | 2016-08-11 15:57:03 -0700 | [diff] [blame] | 698 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 699 | unsigned char data; |
| 700 | unsigned char* in = font.rundata; |
| 701 | while ((data = *in++)) { |
| 702 | memset(bits, (data & 0x80) ? 255 : 0, data & 0x7f); |
| 703 | bits += (data & 0x7f); |
| 704 | } |
Damien Bargiacchi | 35fff61 | 2016-08-11 15:57:03 -0700 | [diff] [blame] | 705 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 706 | gr_font->char_width = font.char_width; |
| 707 | gr_font->char_height = font.char_height; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 708 | } |
Ethan Yonker | 39662b2 | 2017-05-23 08:34:02 -0500 | [diff] [blame] | 709 | #endif // TW_NO_MINUI_CUSTOM_FONTS |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 710 | |
Doug Zongker | 5290f20 | 2014-03-11 13:22:04 -0700 | [diff] [blame] | 711 | void gr_flip() { |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 712 | gr_draw = gr_backend->Flip(); |
Doug Zongker | 5290f20 | 2014-03-11 13:22:04 -0700 | [diff] [blame] | 713 | } |
| 714 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 715 | int gr_init(void) |
| 716 | { |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 717 | gr_init_font(); |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 718 | |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 719 | auto backend = std::unique_ptr<MinuiBackend>{ std::make_unique<MinuiBackendOverlay>() }; |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 720 | gr_draw = backend->Init(); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 721 | |
Ethan Yonker | b386f71 | 2017-01-20 14:30:28 -0600 | [diff] [blame] | 722 | #ifdef MSM_BSP |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 723 | if (gr_draw) { |
| 724 | printf("Using overlay graphics.\n"); |
Ethan Yonker | a59da09 | 2015-10-13 19:35:05 -0500 | [diff] [blame] | 725 | } |
Ethan Yonker | b386f71 | 2017-01-20 14:30:28 -0600 | [diff] [blame] | 726 | #endif |
Ethan Yonker | a59da09 | 2015-10-13 19:35:05 -0500 | [diff] [blame] | 727 | |
| 728 | #ifndef MSM_BSP |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 729 | if (!gr_draw) { |
| 730 | backend = std::make_unique<MinuiBackendAdf>(); |
| 731 | gr_draw = backend->Init(); |
| 732 | if (gr_draw) |
| 733 | printf("Using adf graphics.\n"); |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 734 | } |
Ethan Yonker | a59da09 | 2015-10-13 19:35:05 -0500 | [diff] [blame] | 735 | #else |
| 736 | printf("Skipping adf graphics because TW_TARGET_USES_QCOM_BSP := true\n"); |
| 737 | #endif |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 738 | |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 739 | if (!gr_draw) { |
| 740 | backend = std::make_unique<MinuiBackendDrm>(); |
| 741 | gr_draw = backend->Init(); |
Ethan Yonker | a59da09 | 2015-10-13 19:35:05 -0500 | [diff] [blame] | 742 | if (gr_draw) |
| 743 | printf("Using drm graphics.\n"); |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 744 | } |
| 745 | |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 746 | if (!gr_draw) { |
| 747 | backend = std::make_unique<MinuiBackendFbdev>(); |
| 748 | gr_draw = backend->Init(); |
| 749 | if (gr_draw) |
Ethan Yonker | a59da09 | 2015-10-13 19:35:05 -0500 | [diff] [blame] | 750 | printf("Using fbdev graphics.\n"); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 751 | } |
| 752 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 753 | if (!gr_draw) { |
| 754 | return -1; |
| 755 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 756 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 757 | gr_backend = backend.release(); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 758 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 759 | overscan_offset_x = gr_draw->width * overscan_percent / 100; |
| 760 | overscan_offset_y = gr_draw->height * overscan_percent / 100; |
| 761 | |
| 762 | gr_flip(); |
| 763 | gr_flip(); |
| 764 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 765 | gr_rotate(DEFAULT_ROTATION); |
| 766 | |
| 767 | if (gr_draw->pixel_bytes != 4) { |
| 768 | printf("gr_init: Only 4-byte pixel formats supported\n"); |
| 769 | } |
| 770 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 771 | return 0; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 772 | } |
| 773 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 774 | void gr_exit() { |
| 775 | delete gr_backend; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 776 | } |
| 777 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 778 | int gr_fb_width() { |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 779 | return rotation % 2 ? gr_draw->height - 2 * overscan_offset_y |
| 780 | : gr_draw->width - 2 * overscan_offset_x; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 781 | } |
| 782 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 783 | int gr_fb_height() { |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 784 | return rotation % 2 ? gr_draw->width - 2 * overscan_offset_x |
| 785 | : gr_draw->height - 2 * overscan_offset_y; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 786 | } |
Dima Zavin | 4daf48a | 2011-08-30 11:59:20 -0700 | [diff] [blame] | 787 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 788 | void gr_fb_blank(bool blank) { |
| 789 | gr_backend->Blank(blank); |
Dima Zavin | 4daf48a | 2011-08-30 11:59:20 -0700 | [diff] [blame] | 790 | } |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 791 | |
| 792 | void gr_rotate(GRRotation rot) { |
| 793 | rotation = rot; |
| 794 | } |