blob: bfae6222947f723b47588c79a6733012d0da77ff [file] [log] [blame]
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001/*
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 Bao0ecbd762017-01-16 21:16:58 -080017#include "graphics.h"
18
Luke Song846012f2017-09-13 15:56:16 -070019#include <stdint.h>
Tao Baoe8020f42017-02-03 09:30:07 -080020#include <stdio.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080021#include <stdlib.h>
Elliott Hughescd3c55a2015-01-29 20:50:08 -080022#include <string.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080023
Tao Bao557fa1f2017-02-07 12:51:00 -080024#include <memory>
25
Tao Baoed876a72018-07-31 21:32:50 -070026#include <android-base/properties.h>
27
Tao Bao557fa1f2017-02-07 12:51:00 -080028#include "graphics_drm.h"
29#include "graphics_fbdev.h"
Tao Bao0ecbd762017-01-16 21:16:58 -080030#include "minui/minui.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080031
Tao Bao9f426332018-06-13 10:39:44 -070032static GRFont* gr_font = nullptr;
Alessandro Astone607eccf2020-03-09 23:17:50 +010033static GRFont* gr_font_menu = nullptr;
Tao Bao557fa1f2017-02-07 12:51:00 -080034static MinuiBackend* gr_backend = nullptr;
Doug Zongker16f97c32014-03-06 16:16:05 -080035
Doug Zongkerc560a672012-12-18 16:31:27 -080036static int overscan_offset_x = 0;
37static int overscan_offset_y = 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080038
Luke Song846012f2017-09-13 15:56:16 -070039static uint32_t gr_current = ~0;
Doug Zongker16f97c32014-03-06 16:16:05 -080040
Tao Bao9f426332018-06-13 10:39:44 -070041// gr_draw is owned by backends.
Tao Bao92bdb5a2018-10-21 12:12:37 -070042static GRSurface* gr_draw = nullptr;
Tao Bao44478df2018-07-31 22:01:03 -070043static GRRotation rotation = GRRotation::NONE;
Tao Baoed876a72018-07-31 21:32:50 -070044static PixelFormat pixel_format = PixelFormat::UNKNOWN;
Chihhang Chuang6f7362a2021-09-24 00:11:51 +080045// The graphics backend list that provides fallback options for the default backend selection.
46// For example, it will fist try DRM, then try FBDEV if DRM is unavailable.
47constexpr auto default_backends = { GraphicsBackend::DRM, GraphicsBackend::FBDEV };
Doug Zongker16f97c32014-03-06 16:16:05 -080048
Luke Song846012f2017-09-13 15:56:16 -070049static bool outside(int x, int y) {
Tao Bao44478df2018-07-31 22:01:03 -070050 auto swapped = (rotation == GRRotation::LEFT || rotation == GRRotation::RIGHT);
51 return x < 0 || x >= (swapped ? gr_draw->height : gr_draw->width) || y < 0 ||
52 y >= (swapped ? gr_draw->width : gr_draw->height);
Doug Zongker16f97c32014-03-06 16:16:05 -080053}
54
Luke Song846012f2017-09-13 15:56:16 -070055const GRFont* gr_sys_font() {
56 return gr_font;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080057}
58
Alessandro Astone607eccf2020-03-09 23:17:50 +010059const GRFont* gr_menu_font() {
60 return gr_font_menu;
61}
62
Tao Baoed876a72018-07-31 21:32:50 -070063PixelFormat gr_pixel_format() {
64 return pixel_format;
65}
66
Luke Song846012f2017-09-13 15:56:16 -070067int gr_measure(const GRFont* font, const char* s) {
Tianjie Xu842f2a32018-05-31 18:16:28 -070068 if (font == nullptr) {
69 return -1;
70 }
71
Luke Song846012f2017-09-13 15:56:16 -070072 return font->char_width * strlen(s);
Damien Bargiacchi35fff612016-08-11 15:57:03 -070073}
74
Tianjie Xu842f2a32018-05-31 18:16:28 -070075int gr_font_size(const GRFont* font, int* x, int* y) {
76 if (font == nullptr) {
77 return -1;
78 }
79
Luke Song846012f2017-09-13 15:56:16 -070080 *x = font->char_width;
81 *y = font->char_height;
Tianjie Xu842f2a32018-05-31 18:16:28 -070082 return 0;
Dima Zavin3c7f00e2011-08-30 11:58:24 -070083}
84
Luke Song846012f2017-09-13 15:56:16 -070085// Blends gr_current onto pix value, assumes alpha as most significant byte.
Patrik Torstensson51433b92021-01-30 18:16:25 -080086static inline uint32_t pixel_blend_argb(uint8_t alpha, uint32_t pix) {
Luke Song846012f2017-09-13 15:56:16 -070087 if (alpha == 255) return gr_current;
88 if (alpha == 0) return pix;
89 uint32_t pix_r = pix & 0xff;
90 uint32_t pix_g = pix & 0xff00;
91 uint32_t pix_b = pix & 0xff0000;
92 uint32_t cur_r = gr_current & 0xff;
93 uint32_t cur_g = gr_current & 0xff00;
94 uint32_t cur_b = gr_current & 0xff0000;
95
96 uint32_t out_r = (pix_r * (255 - alpha) + cur_r * alpha) / 255;
97 uint32_t out_g = (pix_g * (255 - alpha) + cur_g * alpha) / 255;
98 uint32_t out_b = (pix_b * (255 - alpha) + cur_b * alpha) / 255;
99
100 return (out_r & 0xff) | (out_g & 0xff00) | (out_b & 0xff0000) | (gr_current & 0xff000000);
101}
102
Patrik Torstensson51433b92021-01-30 18:16:25 -0800103static inline uint32_t pixel_blend_rgba(uint8_t alpha, uint32_t pix) {
104 if (alpha == 255) return gr_current;
105 if (alpha == 0) return pix;
106 uint32_t pix_r = pix & 0xff00;
107 uint32_t pix_g = pix & 0xff0000;
108 uint32_t pix_b = pix & 0xff000000;
109 uint32_t cur_r = gr_current & 0xff00;
110 uint32_t cur_g = gr_current & 0xff0000;
111 uint32_t cur_b = gr_current & 0xff000000;
112
113 uint32_t out_r = (pix_r * (255 - alpha) + cur_r * alpha) / 255;
114 uint32_t out_g = (pix_g * (255 - alpha) + cur_g * alpha) / 255;
115 uint32_t out_b = (pix_b * (255 - alpha) + cur_b * alpha) / 255;
116
117 return (gr_current & 0xff) | (out_r & 0xff00) | (out_g & 0xff0000) | (out_b & 0xff000000);
118}
119
120static inline uint32_t pixel_blend(uint8_t alpha, uint32_t pix) {
121 if (pixel_format == PixelFormat::RGBA) {
122 return pixel_blend_rgba(alpha, pix);
123 }
124 return pixel_blend_argb(alpha, pix);
125}
126
127static inline uint32_t get_alphamask() {
128 if (pixel_format == PixelFormat::RGBA) {
129 return 0x000000ff;
130 }
131 return 0xff000000;
132}
133
134static inline uint8_t get_alpha_shift() {
135 if (pixel_format == PixelFormat::RGBA) {
136 return 0;
137 }
138 return 24;
139}
140
141static inline uint8_t get_alpha(uint32_t pix) {
142 return static_cast<uint8_t>((pix & (gr_current & get_alphamask())) >> get_alpha_shift());
143}
144
Tao Bao9f426332018-06-13 10:39:44 -0700145// Increments pixel pointer right, with current rotation.
Luke Song846012f2017-09-13 15:56:16 -0700146static void incr_x(uint32_t** p, int row_pixels) {
Tao Bao44478df2018-07-31 22:01:03 -0700147 if (rotation == GRRotation::LEFT) {
148 *p = *p - row_pixels;
149 } else if (rotation == GRRotation::RIGHT) {
150 *p = *p + row_pixels;
151 } else if (rotation == GRRotation::DOWN) {
152 *p = *p - 1;
153 } else { // GRRotation::NONE
154 *p = *p + 1;
Luke Song846012f2017-09-13 15:56:16 -0700155 }
156}
157
Tao Bao9f426332018-06-13 10:39:44 -0700158// Increments pixel pointer down, with current rotation.
Luke Song846012f2017-09-13 15:56:16 -0700159static void incr_y(uint32_t** p, int row_pixels) {
Tao Bao44478df2018-07-31 22:01:03 -0700160 if (rotation == GRRotation::LEFT) {
161 *p = *p + 1;
162 } else if (rotation == GRRotation::RIGHT) {
163 *p = *p - 1;
164 } else if (rotation == GRRotation::DOWN) {
165 *p = *p - row_pixels;
166 } else { // GRRotation::NONE
167 *p = *p + row_pixels;
Luke Song846012f2017-09-13 15:56:16 -0700168 }
169}
170
Tao Bao9f426332018-06-13 10:39:44 -0700171// Returns pixel pointer at given coordinates with rotation adjustment.
Tao Bao92bdb5a2018-10-21 12:12:37 -0700172static uint32_t* PixelAt(GRSurface* surface, int x, int y, int row_pixels) {
Luke Song846012f2017-09-13 15:56:16 -0700173 switch (rotation) {
Tao Bao44478df2018-07-31 22:01:03 -0700174 case GRRotation::NONE:
Tao Bao92bdb5a2018-10-21 12:12:37 -0700175 return reinterpret_cast<uint32_t*>(surface->data()) + y * row_pixels + x;
Tao Bao44478df2018-07-31 22:01:03 -0700176 case GRRotation::RIGHT:
Tao Bao92bdb5a2018-10-21 12:12:37 -0700177 return reinterpret_cast<uint32_t*>(surface->data()) + x * row_pixels + (surface->width - y);
Tao Bao44478df2018-07-31 22:01:03 -0700178 case GRRotation::DOWN:
Tao Bao92bdb5a2018-10-21 12:12:37 -0700179 return reinterpret_cast<uint32_t*>(surface->data()) + (surface->height - 1 - y) * row_pixels +
180 (surface->width - 1 - x);
Tao Bao44478df2018-07-31 22:01:03 -0700181 case GRRotation::LEFT:
Tao Bao92bdb5a2018-10-21 12:12:37 -0700182 return reinterpret_cast<uint32_t*>(surface->data()) + (surface->height - 1 - x) * row_pixels +
183 y;
Luke Song846012f2017-09-13 15:56:16 -0700184 default:
Tao Bao44478df2018-07-31 22:01:03 -0700185 printf("invalid rotation %d", static_cast<int>(rotation));
Luke Song846012f2017-09-13 15:56:16 -0700186 }
187 return nullptr;
188}
189
Tao Bao92bdb5a2018-10-21 12:12:37 -0700190static void TextBlend(const uint8_t* src_p, int src_row_bytes, uint32_t* dst_p, int dst_row_pixels,
191 int width, int height) {
Patrik Torstensson51433b92021-01-30 18:16:25 -0800192 uint8_t alpha_current = get_alpha(gr_current);
Luke Song846012f2017-09-13 15:56:16 -0700193 for (int j = 0; j < height; ++j) {
Tao Bao92bdb5a2018-10-21 12:12:37 -0700194 const uint8_t* sx = src_p;
Luke Song846012f2017-09-13 15:56:16 -0700195 uint32_t* px = dst_p;
196 for (int i = 0; i < width; ++i, incr_x(&px, dst_row_pixels)) {
197 uint8_t a = *sx++;
198 if (alpha_current < 255) a = (static_cast<uint32_t>(a) * alpha_current) / 255;
199 *px = pixel_blend(a, *px);
Doug Zongker16f97c32014-03-06 16:16:05 -0800200 }
Luke Song846012f2017-09-13 15:56:16 -0700201 src_p += src_row_bytes;
202 incr_y(&dst_p, dst_row_pixels);
203 }
Doug Zongker16f97c32014-03-06 16:16:05 -0800204}
205
Luke Song846012f2017-09-13 15:56:16 -0700206void gr_text(const GRFont* font, int x, int y, const char* s, bool bold) {
Patrik Torstensson51433b92021-01-30 18:16:25 -0800207 if (!font || !font->texture || (gr_current & get_alphamask()) == 0) return;
Doug Zongker55a36ac2013-03-04 15:49:02 -0800208
Luke Song846012f2017-09-13 15:56:16 -0700209 if (font->texture->pixel_bytes != 1) {
210 printf("gr_text: font has wrong format\n");
211 return;
212 }
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800213
Luke Song846012f2017-09-13 15:56:16 -0700214 bold = bold && (font->texture->height != font->char_height);
Doug Zongkerc560a672012-12-18 16:31:27 -0800215
Luke Song846012f2017-09-13 15:56:16 -0700216 x += overscan_offset_x;
217 y += overscan_offset_y;
Doug Zongker16f97c32014-03-06 16:16:05 -0800218
Luke Song846012f2017-09-13 15:56:16 -0700219 unsigned char ch;
220 while ((ch = *s++)) {
221 if (outside(x, y) || outside(x + font->char_width - 1, y + font->char_height - 1)) break;
Elliott Hughes01a4d082015-03-24 15:21:48 -0700222
Luke Song846012f2017-09-13 15:56:16 -0700223 if (ch < ' ' || ch > '~') {
224 ch = '?';
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800225 }
Luke Song846012f2017-09-13 15:56:16 -0700226
227 int row_pixels = gr_draw->row_bytes / gr_draw->pixel_bytes;
Tao Bao92bdb5a2018-10-21 12:12:37 -0700228 const uint8_t* src_p = font->texture->data() + ((ch - ' ') * font->char_width) +
229 (bold ? font->char_height * font->texture->row_bytes : 0);
230 uint32_t* dst_p = PixelAt(gr_draw, x, y, row_pixels);
Luke Song846012f2017-09-13 15:56:16 -0700231
Tao Bao92bdb5a2018-10-21 12:12:37 -0700232 TextBlend(src_p, font->texture->row_bytes, dst_p, row_pixels, font->char_width,
233 font->char_height);
Luke Song846012f2017-09-13 15:56:16 -0700234
235 x += font->char_width;
236 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800237}
238
Tao Bao92bdb5a2018-10-21 12:12:37 -0700239void gr_texticon(int x, int y, const GRSurface* icon) {
Tao Bao9f426332018-06-13 10:39:44 -0700240 if (icon == nullptr) return;
Doug Zongker16f97c32014-03-06 16:16:05 -0800241
Luke Song846012f2017-09-13 15:56:16 -0700242 if (icon->pixel_bytes != 1) {
243 printf("gr_texticon: source has wrong format\n");
244 return;
245 }
Doug Zongker02ec6b82012-08-22 17:26:40 -0700246
Luke Song846012f2017-09-13 15:56:16 -0700247 x += overscan_offset_x;
248 y += overscan_offset_y;
Doug Zongkerc560a672012-12-18 16:31:27 -0800249
Luke Song846012f2017-09-13 15:56:16 -0700250 if (outside(x, y) || outside(x + icon->width - 1, y + icon->height - 1)) return;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700251
Luke Song846012f2017-09-13 15:56:16 -0700252 int row_pixels = gr_draw->row_bytes / gr_draw->pixel_bytes;
Tao Bao92bdb5a2018-10-21 12:12:37 -0700253 const uint8_t* src_p = icon->data();
254 uint32_t* dst_p = PixelAt(gr_draw, x, y, row_pixels);
255 TextBlend(src_p, icon->row_bytes, dst_p, row_pixels, icon->width, icon->height);
Doug Zongker16f97c32014-03-06 16:16:05 -0800256}
257
Luke Song846012f2017-09-13 15:56:16 -0700258void gr_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a) {
259 uint32_t r32 = r, g32 = g, b32 = b, a32 = a;
Adrian Salidocc7b7eb2019-12-12 20:18:09 -0800260 if (pixel_format == PixelFormat::ARGB || pixel_format == PixelFormat::BGRA) {
Tao Baoed876a72018-07-31 21:32:50 -0700261 gr_current = (a32 << 24) | (r32 << 16) | (g32 << 8) | b32;
Patrik Torstensson51433b92021-01-30 18:16:25 -0800262 } else if (pixel_format == PixelFormat::RGBA) {
263 gr_current = (b32 << 24) | (g32 << 16) | (r32 << 8) | a32;
Tao Baoed876a72018-07-31 21:32:50 -0700264 } else {
265 gr_current = (a32 << 24) | (b32 << 16) | (g32 << 8) | r32;
266 }
Doug Zongker16f97c32014-03-06 16:16:05 -0800267}
268
Luke Song846012f2017-09-13 15:56:16 -0700269void gr_clear() {
270 if ((gr_current & 0xff) == ((gr_current >> 8) & 0xff) &&
271 (gr_current & 0xff) == ((gr_current >> 16) & 0xff) &&
272 (gr_current & 0xff) == ((gr_current >> 24) & 0xff) &&
273 gr_draw->row_bytes == gr_draw->width * gr_draw->pixel_bytes) {
Tao Bao92bdb5a2018-10-21 12:12:37 -0700274 memset(gr_draw->data(), gr_current & 0xff, gr_draw->height * gr_draw->row_bytes);
Luke Song846012f2017-09-13 15:56:16 -0700275 } else {
Tao Bao92bdb5a2018-10-21 12:12:37 -0700276 uint32_t* px = reinterpret_cast<uint32_t*>(gr_draw->data());
Luke Song846012f2017-09-13 15:56:16 -0700277 int row_diff = gr_draw->row_bytes / gr_draw->pixel_bytes - gr_draw->width;
278 for (int y = 0; y < gr_draw->height; ++y) {
279 for (int x = 0; x < gr_draw->width; ++x) {
280 *px++ = gr_current;
281 }
282 px += row_diff;
Doug Zongker16f97c32014-03-06 16:16:05 -0800283 }
Luke Song846012f2017-09-13 15:56:16 -0700284 }
Doug Zongker02ec6b82012-08-22 17:26:40 -0700285}
286
Luke Song846012f2017-09-13 15:56:16 -0700287void gr_fill(int x1, int y1, int x2, int y2) {
288 x1 += overscan_offset_x;
289 y1 += overscan_offset_y;
Doug Zongkerc560a672012-12-18 16:31:27 -0800290
Luke Song846012f2017-09-13 15:56:16 -0700291 x2 += overscan_offset_x;
292 y2 += overscan_offset_y;
Doug Zongkerc560a672012-12-18 16:31:27 -0800293
Luke Song846012f2017-09-13 15:56:16 -0700294 if (outside(x1, y1) || outside(x2 - 1, y2 - 1)) return;
Doug Zongker16f97c32014-03-06 16:16:05 -0800295
Luke Song846012f2017-09-13 15:56:16 -0700296 int row_pixels = gr_draw->row_bytes / gr_draw->pixel_bytes;
Tao Bao92bdb5a2018-10-21 12:12:37 -0700297 uint32_t* p = PixelAt(gr_draw, x1, y1, row_pixels);
Patrik Torstensson51433b92021-01-30 18:16:25 -0800298 uint8_t alpha = get_alpha(gr_current);
Luke Song846012f2017-09-13 15:56:16 -0700299 if (alpha > 0) {
300 for (int y = y1; y < y2; ++y) {
301 uint32_t* px = p;
302 for (int x = x1; x < x2; ++x) {
303 *px = pixel_blend(alpha, *px);
304 incr_x(&px, row_pixels);
305 }
306 incr_y(&p, row_pixels);
Doug Zongker16f97c32014-03-06 16:16:05 -0800307 }
Luke Song846012f2017-09-13 15:56:16 -0700308 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800309}
310
Tao Bao92bdb5a2018-10-21 12:12:37 -0700311void gr_blit(const GRSurface* source, int sx, int sy, int w, int h, int dx, int dy) {
Tao Bao9f426332018-06-13 10:39:44 -0700312 if (source == nullptr) return;
Doug Zongker16f97c32014-03-06 16:16:05 -0800313
Luke Song846012f2017-09-13 15:56:16 -0700314 if (gr_draw->pixel_bytes != source->pixel_bytes) {
315 printf("gr_blit: source has wrong format\n");
316 return;
317 }
318
319 dx += overscan_offset_x;
320 dy += overscan_offset_y;
321
322 if (outside(dx, dy) || outside(dx + w - 1, dy + h - 1)) return;
323
Tao Bao44478df2018-07-31 22:01:03 -0700324 if (rotation != GRRotation::NONE) {
Luke Song846012f2017-09-13 15:56:16 -0700325 int src_row_pixels = source->row_bytes / source->pixel_bytes;
326 int row_pixels = gr_draw->row_bytes / gr_draw->pixel_bytes;
Tao Bao92bdb5a2018-10-21 12:12:37 -0700327 const uint32_t* src_py =
328 reinterpret_cast<const uint32_t*>(source->data()) + sy * source->row_bytes / 4 + sx;
329 uint32_t* dst_py = PixelAt(gr_draw, dx, dy, row_pixels);
Luke Song846012f2017-09-13 15:56:16 -0700330
331 for (int y = 0; y < h; y += 1) {
Tao Bao92bdb5a2018-10-21 12:12:37 -0700332 const uint32_t* src_px = src_py;
Luke Song846012f2017-09-13 15:56:16 -0700333 uint32_t* dst_px = dst_py;
334 for (int x = 0; x < w; x += 1) {
335 *dst_px = *src_px++;
336 incr_x(&dst_px, row_pixels);
337 }
338 src_py += src_row_pixels;
339 incr_y(&dst_py, row_pixels);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800340 }
Luke Song846012f2017-09-13 15:56:16 -0700341 } else {
Tao Bao92bdb5a2018-10-21 12:12:37 -0700342 const uint8_t* src_p = source->data() + sy * source->row_bytes + sx * source->pixel_bytes;
343 uint8_t* dst_p = gr_draw->data() + dy * gr_draw->row_bytes + dx * gr_draw->pixel_bytes;
Doug Zongker16f97c32014-03-06 16:16:05 -0800344
Tao Bao9f426332018-06-13 10:39:44 -0700345 for (int i = 0; i < h; ++i) {
Luke Song846012f2017-09-13 15:56:16 -0700346 memcpy(dst_p, src_p, w * source->pixel_bytes);
347 src_p += source->row_bytes;
348 dst_p += gr_draw->row_bytes;
Doug Zongker16f97c32014-03-06 16:16:05 -0800349 }
Luke Song846012f2017-09-13 15:56:16 -0700350 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800351}
352
Tao Bao9f426332018-06-13 10:39:44 -0700353unsigned int gr_get_width(const GRSurface* surface) {
354 if (surface == nullptr) {
Luke Song846012f2017-09-13 15:56:16 -0700355 return 0;
356 }
357 return surface->width;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800358}
359
Tao Bao9f426332018-06-13 10:39:44 -0700360unsigned int gr_get_height(const GRSurface* surface) {
361 if (surface == nullptr) {
Luke Song846012f2017-09-13 15:56:16 -0700362 return 0;
363 }
364 return surface->height;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800365}
366
Damien Bargiacchid00f5eb2016-09-09 07:14:08 -0700367int gr_init_font(const char* name, GRFont** dest) {
Luke Song846012f2017-09-13 15:56:16 -0700368 GRFont* font = static_cast<GRFont*>(calloc(1, sizeof(*gr_font)));
369 if (font == nullptr) {
370 return -1;
371 }
Damien Bargiacchid00f5eb2016-09-09 07:14:08 -0700372
Luke Song846012f2017-09-13 15:56:16 -0700373 int res = res_create_alpha_surface(name, &(font->texture));
374 if (res < 0) {
375 free(font);
376 return res;
377 }
Damien Bargiacchi35fff612016-08-11 15:57:03 -0700378
Luke Song846012f2017-09-13 15:56:16 -0700379 // The font image should be a 96x2 array of character images. The
380 // columns are the printable ASCII characters 0x20 - 0x7f. The
381 // top row is regular text; the bottom row is bold.
382 font->char_width = font->texture->width / 96;
383 font->char_height = font->texture->height / 2;
Damien Bargiacchid00f5eb2016-09-09 07:14:08 -0700384
Luke Song846012f2017-09-13 15:56:16 -0700385 *dest = font;
Damien Bargiacchi35fff612016-08-11 15:57:03 -0700386
Luke Song846012f2017-09-13 15:56:16 -0700387 return 0;
Damien Bargiacchi35fff612016-08-11 15:57:03 -0700388}
389
Doug Zongker5290f202014-03-11 13:22:04 -0700390void gr_flip() {
Tao Bao557fa1f2017-02-07 12:51:00 -0800391 gr_draw = gr_backend->Flip();
Doug Zongker5290f202014-03-11 13:22:04 -0700392}
393
Chihhang Chuang6f7362a2021-09-24 00:11:51 +0800394std::unique_ptr<MinuiBackend> create_backend(GraphicsBackend backend) {
395 switch (backend) {
396 case GraphicsBackend::DRM:
397 return std::make_unique<MinuiBackendDrm>();
398 case GraphicsBackend::FBDEV:
399 return std::make_unique<MinuiBackendFbdev>();
400 default:
401 return nullptr;
402 }
403}
404
Tao Bao557fa1f2017-02-07 12:51:00 -0800405int gr_init() {
Chihhang Chuang6f7362a2021-09-24 00:11:51 +0800406 return gr_init(default_backends);
407}
408
409int gr_init(std::initializer_list<GraphicsBackend> backends) {
Tao Baoed876a72018-07-31 21:32:50 -0700410 // pixel_format needs to be set before loading any resources or initializing backends.
Tao Bao050feb02018-09-05 21:45:42 -0700411 std::string format = android::base::GetProperty("ro.minui.pixel_format", "");
Tao Baoed876a72018-07-31 21:32:50 -0700412 if (format == "ABGR_8888") {
413 pixel_format = PixelFormat::ABGR;
414 } else if (format == "RGBX_8888") {
415 pixel_format = PixelFormat::RGBX;
Adrian Salidocc7b7eb2019-12-12 20:18:09 -0800416 } else if (format == "ARGB_8888") {
417 pixel_format = PixelFormat::ARGB;
Tao Baoed876a72018-07-31 21:32:50 -0700418 } else if (format == "BGRA_8888") {
419 pixel_format = PixelFormat::BGRA;
Patrik Torstensson51433b92021-01-30 18:16:25 -0800420 } else if (format == "RGBA_8888") {
421 pixel_format = PixelFormat::RGBA;
Tao Baoed876a72018-07-31 21:32:50 -0700422 } else {
423 pixel_format = PixelFormat::UNKNOWN;
424 }
425
Tianjie Xu55a2c4e2018-03-29 11:07:50 -0700426 int ret = gr_init_font("font", &gr_font);
427 if (ret != 0) {
Tianjie Xu842f2a32018-05-31 18:16:28 -0700428 printf("Failed to init font: %d, continuing graphic backend initialization without font file\n",
429 ret);
Tianjie Xu55a2c4e2018-03-29 11:07:50 -0700430 }
Alessandro Astone607eccf2020-03-09 23:17:50 +0100431 ret = gr_init_font("font_menu", &gr_font_menu);
432 if (ret != 0) {
433 printf("Failed to init menu font: %d. Falling back to system font\n", ret);
434 gr_font_menu = gr_font;
435 }
Doug Zongker16f97c32014-03-06 16:16:05 -0800436
Chihhang Chuang6f7362a2021-09-24 00:11:51 +0800437 std::unique_ptr<MinuiBackend> minui_backend;
438 for (GraphicsBackend backend : backends) {
439 minui_backend = create_backend(backend);
440 if (!minui_backend) {
441 printf("gr_init: minui_backend %d is a nullptr\n", backend);
442 continue;
443 }
444 gr_draw = minui_backend->Init();
445 if (gr_draw) break;
Tao Bao557fa1f2017-02-07 12:51:00 -0800446 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800447
Tao Bao557fa1f2017-02-07 12:51:00 -0800448 if (!gr_draw) {
449 return -1;
450 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800451
Chihhang Chuang6f7362a2021-09-24 00:11:51 +0800452 gr_backend = minui_backend.release();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800453
Tao Bao050feb02018-09-05 21:45:42 -0700454 int overscan_percent = android::base::GetIntProperty("ro.minui.overscan_percent", 0);
Tao Bao557fa1f2017-02-07 12:51:00 -0800455 overscan_offset_x = gr_draw->width * overscan_percent / 100;
456 overscan_offset_y = gr_draw->height * overscan_percent / 100;
457
458 gr_flip();
459 gr_flip();
Tianjie Xuccf00a22018-06-05 17:10:23 -0700460 if (!gr_draw) {
461 printf("gr_init: gr_draw becomes nullptr after gr_flip\n");
462 return -1;
463 }
Tao Bao557fa1f2017-02-07 12:51:00 -0800464
Tao Baoed876a72018-07-31 21:32:50 -0700465 std::string rotation_str =
Tao Bao050feb02018-09-05 21:45:42 -0700466 android::base::GetProperty("ro.minui.default_rotation", "ROTATION_NONE");
Tao Bao44478df2018-07-31 22:01:03 -0700467 if (rotation_str == "ROTATION_RIGHT") {
468 gr_rotate(GRRotation::RIGHT);
469 } else if (rotation_str == "ROTATION_DOWN") {
470 gr_rotate(GRRotation::DOWN);
471 } else if (rotation_str == "ROTATION_LEFT") {
472 gr_rotate(GRRotation::LEFT);
Tao Baoed876a72018-07-31 21:32:50 -0700473 } else { // "ROTATION_NONE" or unknown string
Tao Bao44478df2018-07-31 22:01:03 -0700474 gr_rotate(GRRotation::NONE);
475 }
Luke Song846012f2017-09-13 15:56:16 -0700476
477 if (gr_draw->pixel_bytes != 4) {
478 printf("gr_init: Only 4-byte pixel formats supported\n");
479 }
480
Tao Bao557fa1f2017-02-07 12:51:00 -0800481 return 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800482}
483
Tao Bao557fa1f2017-02-07 12:51:00 -0800484void gr_exit() {
485 delete gr_backend;
Tao Bao9f426332018-06-13 10:39:44 -0700486 gr_backend = nullptr;
487
488 delete gr_font;
489 gr_font = nullptr;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800490}
491
Tao Bao557fa1f2017-02-07 12:51:00 -0800492int gr_fb_width() {
Tao Bao44478df2018-07-31 22:01:03 -0700493 return (rotation == GRRotation::LEFT || rotation == GRRotation::RIGHT)
494 ? gr_draw->height - 2 * overscan_offset_y
495 : gr_draw->width - 2 * overscan_offset_x;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800496}
497
Tao Bao557fa1f2017-02-07 12:51:00 -0800498int gr_fb_height() {
Tao Bao44478df2018-07-31 22:01:03 -0700499 return (rotation == GRRotation::LEFT || rotation == GRRotation::RIGHT)
500 ? gr_draw->width - 2 * overscan_offset_x
501 : gr_draw->height - 2 * overscan_offset_y;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800502}
Dima Zavin4daf48a2011-08-30 11:59:20 -0700503
Alessandro Astonea28f1ae2022-03-21 17:14:47 +0100504int gr_fb_width_real() {
505 return (rotation == GRRotation::LEFT || rotation == GRRotation::RIGHT) ? gr_draw->height
506 : gr_draw->width;
507}
508
509int gr_fb_height_real() {
510 return (rotation == GRRotation::LEFT || rotation == GRRotation::RIGHT) ? gr_draw->width
511 : gr_draw->height;
512}
513
514int gr_overscan_offset_x() {
515 return overscan_offset_x;
516}
517
518int gr_overscan_offset_y() {
519 return overscan_offset_y;
520}
521
Tao Bao557fa1f2017-02-07 12:51:00 -0800522void gr_fb_blank(bool blank) {
523 gr_backend->Blank(blank);
Dima Zavin4daf48a2011-08-30 11:59:20 -0700524}
Luke Song846012f2017-09-13 15:56:16 -0700525
Weizhung Dingf4dfa1a2021-09-27 11:30:26 +0800526void gr_fb_blank(bool blank, int index) {
527 gr_backend->Blank(blank, static_cast<MinuiBackend::DrmConnector>(index));
528}
529
Luke Song846012f2017-09-13 15:56:16 -0700530void gr_rotate(GRRotation rot) {
531 rotation = rot;
532}
Weizhung Dingafd0a1b2022-07-19 16:34:43 +0800533
Ludvig Hansson510589f2022-11-07 12:39:20 +0100534GRRotation gr_get_rotation() {
535 return rotation;
536}
537
Weizhung Dingafd0a1b2022-07-19 16:34:43 +0800538bool gr_has_multiple_connectors() {
539 return gr_backend->HasMultipleConnectors();
540}