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> |
| 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_drm.h" |
| 29 | #include "graphics_fbdev.h" |
Tao Bao | 0ecbd76 | 2017-01-16 21:16:58 -0800 | [diff] [blame] | 30 | #include "minui/minui.h" |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 31 | |
Tao Bao | 9f42633 | 2018-06-13 10:39:44 -0700 | [diff] [blame] | 32 | static GRFont* gr_font = nullptr; |
Alessandro Astone | e875475 | 2020-03-09 23:17:50 +0100 | [diff] [blame] | 33 | static GRFont* gr_font_menu = nullptr; |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 34 | static MinuiBackend* gr_backend = nullptr; |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 35 | |
Doug Zongker | c560a67 | 2012-12-18 16:31:27 -0800 | [diff] [blame] | 36 | static int overscan_offset_x = 0; |
| 37 | static int overscan_offset_y = 0; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 38 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 39 | static uint32_t gr_current = ~0; |
| 40 | static constexpr uint32_t alpha_mask = 0xff000000; |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 41 | |
Tao Bao | 9f42633 | 2018-06-13 10:39:44 -0700 | [diff] [blame] | 42 | // gr_draw is owned by backends. |
Tao Bao | 92bdb5a | 2018-10-21 12:12:37 -0700 | [diff] [blame] | 43 | static GRSurface* gr_draw = nullptr; |
Tao Bao | 44478df | 2018-07-31 22:01:03 -0700 | [diff] [blame] | 44 | static GRRotation rotation = GRRotation::NONE; |
Tao Bao | ed876a7 | 2018-07-31 21:32:50 -0700 | [diff] [blame] | 45 | static PixelFormat pixel_format = PixelFormat::UNKNOWN; |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 46 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 47 | static bool outside(int x, int y) { |
Tao Bao | 44478df | 2018-07-31 22:01:03 -0700 | [diff] [blame] | 48 | auto swapped = (rotation == GRRotation::LEFT || rotation == GRRotation::RIGHT); |
| 49 | return x < 0 || x >= (swapped ? gr_draw->height : gr_draw->width) || y < 0 || |
| 50 | y >= (swapped ? gr_draw->width : gr_draw->height); |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 51 | } |
| 52 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 53 | const GRFont* gr_sys_font() { |
| 54 | return gr_font; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 55 | } |
| 56 | |
Alessandro Astone | e875475 | 2020-03-09 23:17:50 +0100 | [diff] [blame] | 57 | const GRFont* gr_menu_font() { |
| 58 | return gr_font_menu; |
| 59 | } |
| 60 | |
Tao Bao | ed876a7 | 2018-07-31 21:32:50 -0700 | [diff] [blame] | 61 | PixelFormat gr_pixel_format() { |
| 62 | return pixel_format; |
| 63 | } |
| 64 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 65 | int gr_measure(const GRFont* font, const char* s) { |
Tianjie Xu | 842f2a3 | 2018-05-31 18:16:28 -0700 | [diff] [blame] | 66 | if (font == nullptr) { |
| 67 | return -1; |
| 68 | } |
| 69 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 70 | return font->char_width * strlen(s); |
Damien Bargiacchi | 35fff61 | 2016-08-11 15:57:03 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Tianjie Xu | 842f2a3 | 2018-05-31 18:16:28 -0700 | [diff] [blame] | 73 | int gr_font_size(const GRFont* font, int* x, int* y) { |
| 74 | if (font == nullptr) { |
| 75 | return -1; |
| 76 | } |
| 77 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 78 | *x = font->char_width; |
| 79 | *y = font->char_height; |
Tianjie Xu | 842f2a3 | 2018-05-31 18:16:28 -0700 | [diff] [blame] | 80 | return 0; |
Dima Zavin | 3c7f00e | 2011-08-30 11:58:24 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 83 | // Blends gr_current onto pix value, assumes alpha as most significant byte. |
| 84 | static inline uint32_t pixel_blend(uint8_t alpha, uint32_t pix) { |
| 85 | if (alpha == 255) return gr_current; |
| 86 | if (alpha == 0) return pix; |
| 87 | uint32_t pix_r = pix & 0xff; |
| 88 | uint32_t pix_g = pix & 0xff00; |
| 89 | uint32_t pix_b = pix & 0xff0000; |
| 90 | uint32_t cur_r = gr_current & 0xff; |
| 91 | uint32_t cur_g = gr_current & 0xff00; |
| 92 | uint32_t cur_b = gr_current & 0xff0000; |
| 93 | |
| 94 | uint32_t out_r = (pix_r * (255 - alpha) + cur_r * alpha) / 255; |
| 95 | uint32_t out_g = (pix_g * (255 - alpha) + cur_g * alpha) / 255; |
| 96 | uint32_t out_b = (pix_b * (255 - alpha) + cur_b * alpha) / 255; |
| 97 | |
| 98 | return (out_r & 0xff) | (out_g & 0xff00) | (out_b & 0xff0000) | (gr_current & 0xff000000); |
| 99 | } |
| 100 | |
Tao Bao | 9f42633 | 2018-06-13 10:39:44 -0700 | [diff] [blame] | 101 | // Increments pixel pointer right, with current rotation. |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 102 | static void incr_x(uint32_t** p, int row_pixels) { |
Tao Bao | 44478df | 2018-07-31 22:01:03 -0700 | [diff] [blame] | 103 | if (rotation == GRRotation::LEFT) { |
| 104 | *p = *p - row_pixels; |
| 105 | } else if (rotation == GRRotation::RIGHT) { |
| 106 | *p = *p + row_pixels; |
| 107 | } else if (rotation == GRRotation::DOWN) { |
| 108 | *p = *p - 1; |
| 109 | } else { // GRRotation::NONE |
| 110 | *p = *p + 1; |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | |
Tao Bao | 9f42633 | 2018-06-13 10:39:44 -0700 | [diff] [blame] | 114 | // Increments pixel pointer down, with current rotation. |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 115 | static void incr_y(uint32_t** p, int row_pixels) { |
Tao Bao | 44478df | 2018-07-31 22:01:03 -0700 | [diff] [blame] | 116 | if (rotation == GRRotation::LEFT) { |
| 117 | *p = *p + 1; |
| 118 | } else if (rotation == GRRotation::RIGHT) { |
| 119 | *p = *p - 1; |
| 120 | } else if (rotation == GRRotation::DOWN) { |
| 121 | *p = *p - row_pixels; |
| 122 | } else { // GRRotation::NONE |
| 123 | *p = *p + row_pixels; |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | |
Tao Bao | 9f42633 | 2018-06-13 10:39:44 -0700 | [diff] [blame] | 127 | // Returns pixel pointer at given coordinates with rotation adjustment. |
Tao Bao | 92bdb5a | 2018-10-21 12:12:37 -0700 | [diff] [blame] | 128 | 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] | 129 | switch (rotation) { |
Tao Bao | 44478df | 2018-07-31 22:01:03 -0700 | [diff] [blame] | 130 | case GRRotation::NONE: |
Tao Bao | 92bdb5a | 2018-10-21 12:12:37 -0700 | [diff] [blame] | 131 | return reinterpret_cast<uint32_t*>(surface->data()) + y * row_pixels + x; |
Tao Bao | 44478df | 2018-07-31 22:01:03 -0700 | [diff] [blame] | 132 | case GRRotation::RIGHT: |
Tao Bao | 92bdb5a | 2018-10-21 12:12:37 -0700 | [diff] [blame] | 133 | 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] | 134 | case GRRotation::DOWN: |
Tao Bao | 92bdb5a | 2018-10-21 12:12:37 -0700 | [diff] [blame] | 135 | return reinterpret_cast<uint32_t*>(surface->data()) + (surface->height - 1 - y) * row_pixels + |
| 136 | (surface->width - 1 - x); |
Tao Bao | 44478df | 2018-07-31 22:01:03 -0700 | [diff] [blame] | 137 | case GRRotation::LEFT: |
Tao Bao | 92bdb5a | 2018-10-21 12:12:37 -0700 | [diff] [blame] | 138 | return reinterpret_cast<uint32_t*>(surface->data()) + (surface->height - 1 - x) * row_pixels + |
| 139 | y; |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 140 | default: |
Tao Bao | 44478df | 2018-07-31 22:01:03 -0700 | [diff] [blame] | 141 | printf("invalid rotation %d", static_cast<int>(rotation)); |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 142 | } |
| 143 | return nullptr; |
| 144 | } |
| 145 | |
Tao Bao | 92bdb5a | 2018-10-21 12:12:37 -0700 | [diff] [blame] | 146 | static void TextBlend(const uint8_t* src_p, int src_row_bytes, uint32_t* dst_p, int dst_row_pixels, |
| 147 | int width, int height) { |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 148 | uint8_t alpha_current = static_cast<uint8_t>((alpha_mask & gr_current) >> 24); |
| 149 | for (int j = 0; j < height; ++j) { |
Tao Bao | 92bdb5a | 2018-10-21 12:12:37 -0700 | [diff] [blame] | 150 | const uint8_t* sx = src_p; |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 151 | uint32_t* px = dst_p; |
| 152 | for (int i = 0; i < width; ++i, incr_x(&px, dst_row_pixels)) { |
| 153 | uint8_t a = *sx++; |
| 154 | if (alpha_current < 255) a = (static_cast<uint32_t>(a) * alpha_current) / 255; |
| 155 | *px = pixel_blend(a, *px); |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 156 | } |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 157 | src_p += src_row_bytes; |
| 158 | incr_y(&dst_p, dst_row_pixels); |
| 159 | } |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 160 | } |
| 161 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 162 | void gr_text(const GRFont* font, int x, int y, const char* s, bool bold) { |
| 163 | if (!font || !font->texture || (gr_current & alpha_mask) == 0) return; |
Doug Zongker | 55a36ac | 2013-03-04 15:49:02 -0800 | [diff] [blame] | 164 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 165 | if (font->texture->pixel_bytes != 1) { |
| 166 | printf("gr_text: font has wrong format\n"); |
| 167 | return; |
| 168 | } |
Doug Zongker | 6fd59ac | 2013-03-06 15:01:11 -0800 | [diff] [blame] | 169 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 170 | bold = bold && (font->texture->height != font->char_height); |
Doug Zongker | c560a67 | 2012-12-18 16:31:27 -0800 | [diff] [blame] | 171 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 172 | x += overscan_offset_x; |
| 173 | y += overscan_offset_y; |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 174 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 175 | unsigned char ch; |
| 176 | while ((ch = *s++)) { |
| 177 | if (outside(x, y) || outside(x + font->char_width - 1, y + font->char_height - 1)) break; |
Elliott Hughes | 01a4d08 | 2015-03-24 15:21:48 -0700 | [diff] [blame] | 178 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 179 | if (ch < ' ' || ch > '~') { |
| 180 | ch = '?'; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 181 | } |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 182 | |
| 183 | int row_pixels = gr_draw->row_bytes / gr_draw->pixel_bytes; |
Tao Bao | 92bdb5a | 2018-10-21 12:12:37 -0700 | [diff] [blame] | 184 | const uint8_t* src_p = font->texture->data() + ((ch - ' ') * font->char_width) + |
| 185 | (bold ? font->char_height * font->texture->row_bytes : 0); |
| 186 | uint32_t* dst_p = PixelAt(gr_draw, x, y, row_pixels); |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 187 | |
Tao Bao | 92bdb5a | 2018-10-21 12:12:37 -0700 | [diff] [blame] | 188 | TextBlend(src_p, font->texture->row_bytes, dst_p, row_pixels, font->char_width, |
| 189 | font->char_height); |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 190 | |
| 191 | x += font->char_width; |
| 192 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 193 | } |
| 194 | |
Tao Bao | 92bdb5a | 2018-10-21 12:12:37 -0700 | [diff] [blame] | 195 | void gr_texticon(int x, int y, const GRSurface* icon) { |
Tao Bao | 9f42633 | 2018-06-13 10:39:44 -0700 | [diff] [blame] | 196 | if (icon == nullptr) return; |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 197 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 198 | if (icon->pixel_bytes != 1) { |
| 199 | printf("gr_texticon: source has wrong format\n"); |
| 200 | return; |
| 201 | } |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 202 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 203 | x += overscan_offset_x; |
| 204 | y += overscan_offset_y; |
Doug Zongker | c560a67 | 2012-12-18 16:31:27 -0800 | [diff] [blame] | 205 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 206 | 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] | 207 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 208 | int row_pixels = gr_draw->row_bytes / gr_draw->pixel_bytes; |
Tao Bao | 92bdb5a | 2018-10-21 12:12:37 -0700 | [diff] [blame] | 209 | const uint8_t* src_p = icon->data(); |
| 210 | uint32_t* dst_p = PixelAt(gr_draw, x, y, row_pixels); |
| 211 | 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] | 212 | } |
| 213 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 214 | void gr_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a) { |
| 215 | uint32_t r32 = r, g32 = g, b32 = b, a32 = a; |
Adrian Salido | cc7b7eb | 2019-12-12 20:18:09 -0800 | [diff] [blame] | 216 | if (pixel_format == PixelFormat::ARGB || pixel_format == PixelFormat::BGRA) { |
Tao Bao | ed876a7 | 2018-07-31 21:32:50 -0700 | [diff] [blame] | 217 | gr_current = (a32 << 24) | (r32 << 16) | (g32 << 8) | b32; |
| 218 | } else { |
| 219 | gr_current = (a32 << 24) | (b32 << 16) | (g32 << 8) | r32; |
| 220 | } |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 221 | } |
| 222 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 223 | void gr_clear() { |
| 224 | if ((gr_current & 0xff) == ((gr_current >> 8) & 0xff) && |
| 225 | (gr_current & 0xff) == ((gr_current >> 16) & 0xff) && |
| 226 | (gr_current & 0xff) == ((gr_current >> 24) & 0xff) && |
| 227 | gr_draw->row_bytes == gr_draw->width * gr_draw->pixel_bytes) { |
Tao Bao | 92bdb5a | 2018-10-21 12:12:37 -0700 | [diff] [blame] | 228 | 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] | 229 | } else { |
Tao Bao | 92bdb5a | 2018-10-21 12:12:37 -0700 | [diff] [blame] | 230 | uint32_t* px = reinterpret_cast<uint32_t*>(gr_draw->data()); |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 231 | int row_diff = gr_draw->row_bytes / gr_draw->pixel_bytes - gr_draw->width; |
| 232 | for (int y = 0; y < gr_draw->height; ++y) { |
| 233 | for (int x = 0; x < gr_draw->width; ++x) { |
| 234 | *px++ = gr_current; |
| 235 | } |
| 236 | px += row_diff; |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 237 | } |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 238 | } |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 239 | } |
| 240 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 241 | void gr_fill(int x1, int y1, int x2, int y2) { |
| 242 | x1 += overscan_offset_x; |
| 243 | y1 += overscan_offset_y; |
Doug Zongker | c560a67 | 2012-12-18 16:31:27 -0800 | [diff] [blame] | 244 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 245 | x2 += overscan_offset_x; |
| 246 | y2 += overscan_offset_y; |
Doug Zongker | c560a67 | 2012-12-18 16:31:27 -0800 | [diff] [blame] | 247 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 248 | if (outside(x1, y1) || outside(x2 - 1, y2 - 1)) return; |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 249 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 250 | int row_pixels = gr_draw->row_bytes / gr_draw->pixel_bytes; |
Tao Bao | 92bdb5a | 2018-10-21 12:12:37 -0700 | [diff] [blame] | 251 | uint32_t* p = PixelAt(gr_draw, x1, y1, row_pixels); |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 252 | uint8_t alpha = static_cast<uint8_t>(((gr_current & alpha_mask) >> 24)); |
| 253 | if (alpha > 0) { |
| 254 | for (int y = y1; y < y2; ++y) { |
| 255 | uint32_t* px = p; |
| 256 | for (int x = x1; x < x2; ++x) { |
| 257 | *px = pixel_blend(alpha, *px); |
| 258 | incr_x(&px, row_pixels); |
| 259 | } |
| 260 | incr_y(&p, row_pixels); |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 261 | } |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 262 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 263 | } |
| 264 | |
Tao Bao | 92bdb5a | 2018-10-21 12:12:37 -0700 | [diff] [blame] | 265 | 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] | 266 | if (source == nullptr) return; |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 267 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 268 | if (gr_draw->pixel_bytes != source->pixel_bytes) { |
| 269 | printf("gr_blit: source has wrong format\n"); |
| 270 | return; |
| 271 | } |
| 272 | |
| 273 | dx += overscan_offset_x; |
| 274 | dy += overscan_offset_y; |
| 275 | |
| 276 | if (outside(dx, dy) || outside(dx + w - 1, dy + h - 1)) return; |
| 277 | |
Tao Bao | 44478df | 2018-07-31 22:01:03 -0700 | [diff] [blame] | 278 | if (rotation != GRRotation::NONE) { |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 279 | int src_row_pixels = source->row_bytes / source->pixel_bytes; |
| 280 | int row_pixels = gr_draw->row_bytes / gr_draw->pixel_bytes; |
Tao Bao | 92bdb5a | 2018-10-21 12:12:37 -0700 | [diff] [blame] | 281 | const uint32_t* src_py = |
| 282 | reinterpret_cast<const uint32_t*>(source->data()) + sy * source->row_bytes / 4 + sx; |
| 283 | uint32_t* dst_py = PixelAt(gr_draw, dx, dy, row_pixels); |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 284 | |
| 285 | for (int y = 0; y < h; y += 1) { |
Tao Bao | 92bdb5a | 2018-10-21 12:12:37 -0700 | [diff] [blame] | 286 | const uint32_t* src_px = src_py; |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 287 | uint32_t* dst_px = dst_py; |
| 288 | for (int x = 0; x < w; x += 1) { |
| 289 | *dst_px = *src_px++; |
| 290 | incr_x(&dst_px, row_pixels); |
| 291 | } |
| 292 | src_py += src_row_pixels; |
| 293 | incr_y(&dst_py, row_pixels); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 294 | } |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 295 | } else { |
Tao Bao | 92bdb5a | 2018-10-21 12:12:37 -0700 | [diff] [blame] | 296 | const uint8_t* src_p = source->data() + sy * source->row_bytes + sx * source->pixel_bytes; |
| 297 | 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] | 298 | |
Tao Bao | 9f42633 | 2018-06-13 10:39:44 -0700 | [diff] [blame] | 299 | for (int i = 0; i < h; ++i) { |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 300 | memcpy(dst_p, src_p, w * source->pixel_bytes); |
| 301 | src_p += source->row_bytes; |
| 302 | dst_p += gr_draw->row_bytes; |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 303 | } |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 304 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 305 | } |
| 306 | |
Tao Bao | 9f42633 | 2018-06-13 10:39:44 -0700 | [diff] [blame] | 307 | unsigned int gr_get_width(const GRSurface* surface) { |
| 308 | if (surface == nullptr) { |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 309 | return 0; |
| 310 | } |
| 311 | return surface->width; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 312 | } |
| 313 | |
Tao Bao | 9f42633 | 2018-06-13 10:39:44 -0700 | [diff] [blame] | 314 | unsigned int gr_get_height(const GRSurface* surface) { |
| 315 | if (surface == nullptr) { |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 316 | return 0; |
| 317 | } |
| 318 | return surface->height; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 319 | } |
| 320 | |
Damien Bargiacchi | d00f5eb | 2016-09-09 07:14:08 -0700 | [diff] [blame] | 321 | int gr_init_font(const char* name, GRFont** dest) { |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 322 | GRFont* font = static_cast<GRFont*>(calloc(1, sizeof(*gr_font))); |
| 323 | if (font == nullptr) { |
| 324 | return -1; |
| 325 | } |
Damien Bargiacchi | d00f5eb | 2016-09-09 07:14:08 -0700 | [diff] [blame] | 326 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 327 | int res = res_create_alpha_surface(name, &(font->texture)); |
| 328 | if (res < 0) { |
| 329 | free(font); |
| 330 | return res; |
| 331 | } |
Damien Bargiacchi | 35fff61 | 2016-08-11 15:57:03 -0700 | [diff] [blame] | 332 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 333 | // The font image should be a 96x2 array of character images. The |
| 334 | // columns are the printable ASCII characters 0x20 - 0x7f. The |
| 335 | // top row is regular text; the bottom row is bold. |
| 336 | font->char_width = font->texture->width / 96; |
| 337 | font->char_height = font->texture->height / 2; |
Damien Bargiacchi | d00f5eb | 2016-09-09 07:14:08 -0700 | [diff] [blame] | 338 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 339 | *dest = font; |
Damien Bargiacchi | 35fff61 | 2016-08-11 15:57:03 -0700 | [diff] [blame] | 340 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 341 | return 0; |
Damien Bargiacchi | 35fff61 | 2016-08-11 15:57:03 -0700 | [diff] [blame] | 342 | } |
| 343 | |
Doug Zongker | 5290f20 | 2014-03-11 13:22:04 -0700 | [diff] [blame] | 344 | void gr_flip() { |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 345 | gr_draw = gr_backend->Flip(); |
Doug Zongker | 5290f20 | 2014-03-11 13:22:04 -0700 | [diff] [blame] | 346 | } |
| 347 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 348 | int gr_init() { |
Tao Bao | ed876a7 | 2018-07-31 21:32:50 -0700 | [diff] [blame] | 349 | // 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] | 350 | std::string format = android::base::GetProperty("ro.minui.pixel_format", ""); |
Tao Bao | ed876a7 | 2018-07-31 21:32:50 -0700 | [diff] [blame] | 351 | if (format == "ABGR_8888") { |
| 352 | pixel_format = PixelFormat::ABGR; |
| 353 | } else if (format == "RGBX_8888") { |
| 354 | pixel_format = PixelFormat::RGBX; |
Adrian Salido | cc7b7eb | 2019-12-12 20:18:09 -0800 | [diff] [blame] | 355 | } else if (format == "ARGB_8888") { |
| 356 | pixel_format = PixelFormat::ARGB; |
Tao Bao | ed876a7 | 2018-07-31 21:32:50 -0700 | [diff] [blame] | 357 | } else if (format == "BGRA_8888") { |
| 358 | pixel_format = PixelFormat::BGRA; |
| 359 | } else { |
| 360 | pixel_format = PixelFormat::UNKNOWN; |
| 361 | } |
| 362 | |
Tianjie Xu | 55a2c4e | 2018-03-29 11:07:50 -0700 | [diff] [blame] | 363 | int ret = gr_init_font("font", &gr_font); |
| 364 | if (ret != 0) { |
Tianjie Xu | 842f2a3 | 2018-05-31 18:16:28 -0700 | [diff] [blame] | 365 | printf("Failed to init font: %d, continuing graphic backend initialization without font file\n", |
| 366 | ret); |
Tianjie Xu | 55a2c4e | 2018-03-29 11:07:50 -0700 | [diff] [blame] | 367 | } |
Alessandro Astone | e875475 | 2020-03-09 23:17:50 +0100 | [diff] [blame] | 368 | ret = gr_init_font("font_menu", &gr_font_menu); |
| 369 | if (ret != 0) { |
| 370 | printf("Failed to init menu font: %d. Falling back to system font\n", ret); |
| 371 | gr_font_menu = gr_font; |
| 372 | } |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 373 | |
Marissa Wall | 025ce54 | 2020-02-28 11:22:46 -0800 | [diff] [blame] | 374 | auto backend = std::unique_ptr<MinuiBackend>{ std::make_unique<MinuiBackendDrm>() }; |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 375 | gr_draw = backend->Init(); |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 376 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 377 | if (!gr_draw) { |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 378 | backend = std::make_unique<MinuiBackendFbdev>(); |
| 379 | gr_draw = backend->Init(); |
| 380 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 381 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 382 | if (!gr_draw) { |
| 383 | return -1; |
| 384 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 385 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 386 | gr_backend = backend.release(); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 387 | |
Tao Bao | 050feb0 | 2018-09-05 21:45:42 -0700 | [diff] [blame] | 388 | int overscan_percent = android::base::GetIntProperty("ro.minui.overscan_percent", 0); |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 389 | overscan_offset_x = gr_draw->width * overscan_percent / 100; |
| 390 | overscan_offset_y = gr_draw->height * overscan_percent / 100; |
| 391 | |
| 392 | gr_flip(); |
| 393 | gr_flip(); |
Tianjie Xu | ccf00a2 | 2018-06-05 17:10:23 -0700 | [diff] [blame] | 394 | if (!gr_draw) { |
| 395 | printf("gr_init: gr_draw becomes nullptr after gr_flip\n"); |
| 396 | return -1; |
| 397 | } |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 398 | |
Tao Bao | ed876a7 | 2018-07-31 21:32:50 -0700 | [diff] [blame] | 399 | std::string rotation_str = |
Tao Bao | 050feb0 | 2018-09-05 21:45:42 -0700 | [diff] [blame] | 400 | android::base::GetProperty("ro.minui.default_rotation", "ROTATION_NONE"); |
Tao Bao | 44478df | 2018-07-31 22:01:03 -0700 | [diff] [blame] | 401 | if (rotation_str == "ROTATION_RIGHT") { |
| 402 | gr_rotate(GRRotation::RIGHT); |
| 403 | } else if (rotation_str == "ROTATION_DOWN") { |
| 404 | gr_rotate(GRRotation::DOWN); |
| 405 | } else if (rotation_str == "ROTATION_LEFT") { |
| 406 | gr_rotate(GRRotation::LEFT); |
Tao Bao | ed876a7 | 2018-07-31 21:32:50 -0700 | [diff] [blame] | 407 | } else { // "ROTATION_NONE" or unknown string |
Tao Bao | 44478df | 2018-07-31 22:01:03 -0700 | [diff] [blame] | 408 | gr_rotate(GRRotation::NONE); |
| 409 | } |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 410 | |
| 411 | if (gr_draw->pixel_bytes != 4) { |
| 412 | printf("gr_init: Only 4-byte pixel formats supported\n"); |
| 413 | } |
| 414 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 415 | return 0; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 416 | } |
| 417 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 418 | void gr_exit() { |
| 419 | delete gr_backend; |
Tao Bao | 9f42633 | 2018-06-13 10:39:44 -0700 | [diff] [blame] | 420 | gr_backend = nullptr; |
| 421 | |
| 422 | delete gr_font; |
| 423 | gr_font = nullptr; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 424 | } |
| 425 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 426 | int gr_fb_width() { |
Tao Bao | 44478df | 2018-07-31 22:01:03 -0700 | [diff] [blame] | 427 | return (rotation == GRRotation::LEFT || rotation == GRRotation::RIGHT) |
| 428 | ? gr_draw->height - 2 * overscan_offset_y |
| 429 | : gr_draw->width - 2 * overscan_offset_x; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 430 | } |
| 431 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 432 | int gr_fb_height() { |
Tao Bao | 44478df | 2018-07-31 22:01:03 -0700 | [diff] [blame] | 433 | return (rotation == GRRotation::LEFT || rotation == GRRotation::RIGHT) |
| 434 | ? gr_draw->width - 2 * overscan_offset_x |
| 435 | : gr_draw->height - 2 * overscan_offset_y; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 436 | } |
Dima Zavin | 4daf48a | 2011-08-30 11:59:20 -0700 | [diff] [blame] | 437 | |
Alessandro Astone | 4a00e64 | 2022-03-21 17:14:47 +0100 | [diff] [blame^] | 438 | int gr_fb_width_real() { |
| 439 | return (rotation == GRRotation::LEFT || rotation == GRRotation::RIGHT) ? gr_draw->height |
| 440 | : gr_draw->width; |
| 441 | } |
| 442 | |
| 443 | int gr_fb_height_real() { |
| 444 | return (rotation == GRRotation::LEFT || rotation == GRRotation::RIGHT) ? gr_draw->width |
| 445 | : gr_draw->height; |
| 446 | } |
| 447 | |
| 448 | int gr_overscan_offset_x() { |
| 449 | return overscan_offset_x; |
| 450 | } |
| 451 | |
| 452 | int gr_overscan_offset_y() { |
| 453 | return overscan_offset_y; |
| 454 | } |
| 455 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 456 | void gr_fb_blank(bool blank) { |
| 457 | gr_backend->Blank(blank); |
Dima Zavin | 4daf48a | 2011-08-30 11:59:20 -0700 | [diff] [blame] | 458 | } |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 459 | |
| 460 | void gr_rotate(GRRotation rot) { |
| 461 | rotation = rot; |
| 462 | } |