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