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