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