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