blob: 8e2f58120710efb09b9f67a2352428e63a5253ad [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 Astone09caacf2020-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;
LuK13377d2d5702023-06-25 01:53:57 +020044static GRRotation touch_rotation = GRRotation::NONE;
Tao Baoed876a72018-07-31 21:32:50 -070045static PixelFormat pixel_format = PixelFormat::UNKNOWN;
Chihhang Chuang6f7362a2021-09-24 00:11:51 +080046// The graphics backend list that provides fallback options for the default backend selection.
47// For example, it will fist try DRM, then try FBDEV if DRM is unavailable.
48constexpr auto default_backends = { GraphicsBackend::DRM, GraphicsBackend::FBDEV };
Doug Zongker16f97c32014-03-06 16:16:05 -080049
Luke Song846012f2017-09-13 15:56:16 -070050static bool outside(int x, int y) {
Tao Bao44478df2018-07-31 22:01:03 -070051 auto swapped = (rotation == GRRotation::LEFT || rotation == GRRotation::RIGHT);
52 return x < 0 || x >= (swapped ? gr_draw->height : gr_draw->width) || y < 0 ||
53 y >= (swapped ? gr_draw->width : gr_draw->height);
Doug Zongker16f97c32014-03-06 16:16:05 -080054}
55
Luke Song846012f2017-09-13 15:56:16 -070056const GRFont* gr_sys_font() {
57 return gr_font;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080058}
59
Alessandro Astone09caacf2020-03-09 23:17:50 +010060const GRFont* gr_menu_font() {
61 return gr_font_menu;
62}
63
Tao Baoed876a72018-07-31 21:32:50 -070064PixelFormat gr_pixel_format() {
65 return pixel_format;
66}
67
Luke Song846012f2017-09-13 15:56:16 -070068int gr_measure(const GRFont* font, const char* s) {
Tianjie Xu842f2a32018-05-31 18:16:28 -070069 if (font == nullptr) {
70 return -1;
71 }
72
Luke Song846012f2017-09-13 15:56:16 -070073 return font->char_width * strlen(s);
Damien Bargiacchi35fff612016-08-11 15:57:03 -070074}
75
Tianjie Xu842f2a32018-05-31 18:16:28 -070076int gr_font_size(const GRFont* font, int* x, int* y) {
77 if (font == nullptr) {
78 return -1;
79 }
80
Luke Song846012f2017-09-13 15:56:16 -070081 *x = font->char_width;
82 *y = font->char_height;
Tianjie Xu842f2a32018-05-31 18:16:28 -070083 return 0;
Dima Zavin3c7f00e2011-08-30 11:58:24 -070084}
85
Luke Song846012f2017-09-13 15:56:16 -070086// Blends gr_current onto pix value, assumes alpha as most significant byte.
Patrik Torstensson51433b92021-01-30 18:16:25 -080087static inline uint32_t pixel_blend_argb(uint8_t alpha, uint32_t pix) {
Luke Song846012f2017-09-13 15:56:16 -070088 if (alpha == 255) return gr_current;
89 if (alpha == 0) return pix;
90 uint32_t pix_r = pix & 0xff;
91 uint32_t pix_g = pix & 0xff00;
92 uint32_t pix_b = pix & 0xff0000;
93 uint32_t cur_r = gr_current & 0xff;
94 uint32_t cur_g = gr_current & 0xff00;
95 uint32_t cur_b = gr_current & 0xff0000;
96
97 uint32_t out_r = (pix_r * (255 - alpha) + cur_r * alpha) / 255;
98 uint32_t out_g = (pix_g * (255 - alpha) + cur_g * alpha) / 255;
99 uint32_t out_b = (pix_b * (255 - alpha) + cur_b * alpha) / 255;
100
101 return (out_r & 0xff) | (out_g & 0xff00) | (out_b & 0xff0000) | (gr_current & 0xff000000);
102}
103
Patrik Torstensson51433b92021-01-30 18:16:25 -0800104static inline uint32_t pixel_blend_rgba(uint8_t alpha, uint32_t pix) {
105 if (alpha == 255) return gr_current;
106 if (alpha == 0) return pix;
107 uint32_t pix_r = pix & 0xff00;
108 uint32_t pix_g = pix & 0xff0000;
109 uint32_t pix_b = pix & 0xff000000;
110 uint32_t cur_r = gr_current & 0xff00;
111 uint32_t cur_g = gr_current & 0xff0000;
112 uint32_t cur_b = gr_current & 0xff000000;
113
114 uint32_t out_r = (pix_r * (255 - alpha) + cur_r * alpha) / 255;
115 uint32_t out_g = (pix_g * (255 - alpha) + cur_g * alpha) / 255;
116 uint32_t out_b = (pix_b * (255 - alpha) + cur_b * alpha) / 255;
117
118 return (gr_current & 0xff) | (out_r & 0xff00) | (out_g & 0xff0000) | (out_b & 0xff000000);
119}
120
121static inline uint32_t pixel_blend(uint8_t alpha, uint32_t pix) {
122 if (pixel_format == PixelFormat::RGBA) {
123 return pixel_blend_rgba(alpha, pix);
124 }
125 return pixel_blend_argb(alpha, pix);
126}
127
128static inline uint32_t get_alphamask() {
129 if (pixel_format == PixelFormat::RGBA) {
130 return 0x000000ff;
131 }
132 return 0xff000000;
133}
134
135static inline uint8_t get_alpha_shift() {
136 if (pixel_format == PixelFormat::RGBA) {
137 return 0;
138 }
139 return 24;
140}
141
142static inline uint8_t get_alpha(uint32_t pix) {
143 return static_cast<uint8_t>((pix & (gr_current & get_alphamask())) >> get_alpha_shift());
144}
145
Tao Bao9f426332018-06-13 10:39:44 -0700146// Increments pixel pointer right, with current rotation.
Luke Song846012f2017-09-13 15:56:16 -0700147static void incr_x(uint32_t** p, int row_pixels) {
Tao Bao44478df2018-07-31 22:01:03 -0700148 if (rotation == GRRotation::LEFT) {
149 *p = *p - row_pixels;
150 } else if (rotation == GRRotation::RIGHT) {
151 *p = *p + row_pixels;
152 } else if (rotation == GRRotation::DOWN) {
153 *p = *p - 1;
154 } else { // GRRotation::NONE
155 *p = *p + 1;
Luke Song846012f2017-09-13 15:56:16 -0700156 }
157}
158
Tao Bao9f426332018-06-13 10:39:44 -0700159// Increments pixel pointer down, with current rotation.
Luke Song846012f2017-09-13 15:56:16 -0700160static void incr_y(uint32_t** p, int row_pixels) {
Tao Bao44478df2018-07-31 22:01:03 -0700161 if (rotation == GRRotation::LEFT) {
162 *p = *p + 1;
163 } else if (rotation == GRRotation::RIGHT) {
164 *p = *p - 1;
165 } else if (rotation == GRRotation::DOWN) {
166 *p = *p - row_pixels;
167 } else { // GRRotation::NONE
168 *p = *p + row_pixels;
Luke Song846012f2017-09-13 15:56:16 -0700169 }
170}
171
Tao Bao9f426332018-06-13 10:39:44 -0700172// Returns pixel pointer at given coordinates with rotation adjustment.
Tao Bao92bdb5a2018-10-21 12:12:37 -0700173static uint32_t* PixelAt(GRSurface* surface, int x, int y, int row_pixels) {
Luke Song846012f2017-09-13 15:56:16 -0700174 switch (rotation) {
Tao Bao44478df2018-07-31 22:01:03 -0700175 case GRRotation::NONE:
Tao Bao92bdb5a2018-10-21 12:12:37 -0700176 return reinterpret_cast<uint32_t*>(surface->data()) + y * row_pixels + x;
Tao Bao44478df2018-07-31 22:01:03 -0700177 case GRRotation::RIGHT:
Tao Bao92bdb5a2018-10-21 12:12:37 -0700178 return reinterpret_cast<uint32_t*>(surface->data()) + x * row_pixels + (surface->width - y);
Tao Bao44478df2018-07-31 22:01:03 -0700179 case GRRotation::DOWN:
Tao Bao92bdb5a2018-10-21 12:12:37 -0700180 return reinterpret_cast<uint32_t*>(surface->data()) + (surface->height - 1 - y) * row_pixels +
181 (surface->width - 1 - x);
Tao Bao44478df2018-07-31 22:01:03 -0700182 case GRRotation::LEFT:
Tao Bao92bdb5a2018-10-21 12:12:37 -0700183 return reinterpret_cast<uint32_t*>(surface->data()) + (surface->height - 1 - x) * row_pixels +
184 y;
Luke Song846012f2017-09-13 15:56:16 -0700185 default:
Tao Bao44478df2018-07-31 22:01:03 -0700186 printf("invalid rotation %d", static_cast<int>(rotation));
Luke Song846012f2017-09-13 15:56:16 -0700187 }
188 return nullptr;
189}
190
Tao Bao92bdb5a2018-10-21 12:12:37 -0700191static void TextBlend(const uint8_t* src_p, int src_row_bytes, uint32_t* dst_p, int dst_row_pixels,
192 int width, int height) {
Patrik Torstensson51433b92021-01-30 18:16:25 -0800193 uint8_t alpha_current = get_alpha(gr_current);
Luke Song846012f2017-09-13 15:56:16 -0700194 for (int j = 0; j < height; ++j) {
Tao Bao92bdb5a2018-10-21 12:12:37 -0700195 const uint8_t* sx = src_p;
Luke Song846012f2017-09-13 15:56:16 -0700196 uint32_t* px = dst_p;
197 for (int i = 0; i < width; ++i, incr_x(&px, dst_row_pixels)) {
198 uint8_t a = *sx++;
199 if (alpha_current < 255) a = (static_cast<uint32_t>(a) * alpha_current) / 255;
200 *px = pixel_blend(a, *px);
Doug Zongker16f97c32014-03-06 16:16:05 -0800201 }
Luke Song846012f2017-09-13 15:56:16 -0700202 src_p += src_row_bytes;
203 incr_y(&dst_p, dst_row_pixels);
204 }
Doug Zongker16f97c32014-03-06 16:16:05 -0800205}
206
Luke Song846012f2017-09-13 15:56:16 -0700207void gr_text(const GRFont* font, int x, int y, const char* s, bool bold) {
Patrik Torstensson51433b92021-01-30 18:16:25 -0800208 if (!font || !font->texture || (gr_current & get_alphamask()) == 0) return;
Doug Zongker55a36ac2013-03-04 15:49:02 -0800209
Luke Song846012f2017-09-13 15:56:16 -0700210 if (font->texture->pixel_bytes != 1) {
211 printf("gr_text: font has wrong format\n");
212 return;
213 }
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800214
Luke Song846012f2017-09-13 15:56:16 -0700215 bold = bold && (font->texture->height != font->char_height);
Doug Zongkerc560a672012-12-18 16:31:27 -0800216
Luke Song846012f2017-09-13 15:56:16 -0700217 x += overscan_offset_x;
218 y += overscan_offset_y;
Doug Zongker16f97c32014-03-06 16:16:05 -0800219
Luke Song846012f2017-09-13 15:56:16 -0700220 unsigned char ch;
221 while ((ch = *s++)) {
222 if (outside(x, y) || outside(x + font->char_width - 1, y + font->char_height - 1)) break;
Elliott Hughes01a4d082015-03-24 15:21:48 -0700223
Luke Song846012f2017-09-13 15:56:16 -0700224 if (ch < ' ' || ch > '~') {
225 ch = '?';
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800226 }
Luke Song846012f2017-09-13 15:56:16 -0700227
228 int row_pixels = gr_draw->row_bytes / gr_draw->pixel_bytes;
Tao Bao92bdb5a2018-10-21 12:12:37 -0700229 const uint8_t* src_p = font->texture->data() + ((ch - ' ') * font->char_width) +
230 (bold ? font->char_height * font->texture->row_bytes : 0);
231 uint32_t* dst_p = PixelAt(gr_draw, x, y, row_pixels);
Luke Song846012f2017-09-13 15:56:16 -0700232
Tao Bao92bdb5a2018-10-21 12:12:37 -0700233 TextBlend(src_p, font->texture->row_bytes, dst_p, row_pixels, font->char_width,
234 font->char_height);
Luke Song846012f2017-09-13 15:56:16 -0700235
236 x += font->char_width;
237 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800238}
239
Tao Bao92bdb5a2018-10-21 12:12:37 -0700240void gr_texticon(int x, int y, const GRSurface* icon) {
Tao Bao9f426332018-06-13 10:39:44 -0700241 if (icon == nullptr) return;
Doug Zongker16f97c32014-03-06 16:16:05 -0800242
Luke Song846012f2017-09-13 15:56:16 -0700243 if (icon->pixel_bytes != 1) {
244 printf("gr_texticon: source has wrong format\n");
245 return;
246 }
Doug Zongker02ec6b82012-08-22 17:26:40 -0700247
Luke Song846012f2017-09-13 15:56:16 -0700248 x += overscan_offset_x;
249 y += overscan_offset_y;
Doug Zongkerc560a672012-12-18 16:31:27 -0800250
Luke Song846012f2017-09-13 15:56:16 -0700251 if (outside(x, y) || outside(x + icon->width - 1, y + icon->height - 1)) return;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700252
Luke Song846012f2017-09-13 15:56:16 -0700253 int row_pixels = gr_draw->row_bytes / gr_draw->pixel_bytes;
Tao Bao92bdb5a2018-10-21 12:12:37 -0700254 const uint8_t* src_p = icon->data();
255 uint32_t* dst_p = PixelAt(gr_draw, x, y, row_pixels);
256 TextBlend(src_p, icon->row_bytes, dst_p, row_pixels, icon->width, icon->height);
Doug Zongker16f97c32014-03-06 16:16:05 -0800257}
258
Luke Song846012f2017-09-13 15:56:16 -0700259void gr_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a) {
260 uint32_t r32 = r, g32 = g, b32 = b, a32 = a;
Adrian Salidocc7b7eb2019-12-12 20:18:09 -0800261 if (pixel_format == PixelFormat::ARGB || pixel_format == PixelFormat::BGRA) {
Tao Baoed876a72018-07-31 21:32:50 -0700262 gr_current = (a32 << 24) | (r32 << 16) | (g32 << 8) | b32;
Patrik Torstensson51433b92021-01-30 18:16:25 -0800263 } else if (pixel_format == PixelFormat::RGBA) {
264 gr_current = (b32 << 24) | (g32 << 16) | (r32 << 8) | a32;
Tao Baoed876a72018-07-31 21:32:50 -0700265 } else {
266 gr_current = (a32 << 24) | (b32 << 16) | (g32 << 8) | r32;
267 }
Doug Zongker16f97c32014-03-06 16:16:05 -0800268}
269
Luke Song846012f2017-09-13 15:56:16 -0700270void gr_clear() {
271 if ((gr_current & 0xff) == ((gr_current >> 8) & 0xff) &&
272 (gr_current & 0xff) == ((gr_current >> 16) & 0xff) &&
273 (gr_current & 0xff) == ((gr_current >> 24) & 0xff) &&
274 gr_draw->row_bytes == gr_draw->width * gr_draw->pixel_bytes) {
Tao Bao92bdb5a2018-10-21 12:12:37 -0700275 memset(gr_draw->data(), gr_current & 0xff, gr_draw->height * gr_draw->row_bytes);
Luke Song846012f2017-09-13 15:56:16 -0700276 } else {
Tao Bao92bdb5a2018-10-21 12:12:37 -0700277 uint32_t* px = reinterpret_cast<uint32_t*>(gr_draw->data());
Luke Song846012f2017-09-13 15:56:16 -0700278 int row_diff = gr_draw->row_bytes / gr_draw->pixel_bytes - gr_draw->width;
279 for (int y = 0; y < gr_draw->height; ++y) {
280 for (int x = 0; x < gr_draw->width; ++x) {
281 *px++ = gr_current;
282 }
283 px += row_diff;
Doug Zongker16f97c32014-03-06 16:16:05 -0800284 }
Luke Song846012f2017-09-13 15:56:16 -0700285 }
Doug Zongker02ec6b82012-08-22 17:26:40 -0700286}
287
Luke Song846012f2017-09-13 15:56:16 -0700288void gr_fill(int x1, int y1, int x2, int y2) {
289 x1 += overscan_offset_x;
290 y1 += overscan_offset_y;
Doug Zongkerc560a672012-12-18 16:31:27 -0800291
Luke Song846012f2017-09-13 15:56:16 -0700292 x2 += overscan_offset_x;
293 y2 += overscan_offset_y;
Doug Zongkerc560a672012-12-18 16:31:27 -0800294
Luke Song846012f2017-09-13 15:56:16 -0700295 if (outside(x1, y1) || outside(x2 - 1, y2 - 1)) return;
Doug Zongker16f97c32014-03-06 16:16:05 -0800296
Luke Song846012f2017-09-13 15:56:16 -0700297 int row_pixels = gr_draw->row_bytes / gr_draw->pixel_bytes;
Tao Bao92bdb5a2018-10-21 12:12:37 -0700298 uint32_t* p = PixelAt(gr_draw, x1, y1, row_pixels);
Patrik Torstensson51433b92021-01-30 18:16:25 -0800299 uint8_t alpha = get_alpha(gr_current);
Luke Song846012f2017-09-13 15:56:16 -0700300 if (alpha > 0) {
301 for (int y = y1; y < y2; ++y) {
302 uint32_t* px = p;
303 for (int x = x1; x < x2; ++x) {
304 *px = pixel_blend(alpha, *px);
305 incr_x(&px, row_pixels);
306 }
307 incr_y(&p, row_pixels);
Doug Zongker16f97c32014-03-06 16:16:05 -0800308 }
Luke Song846012f2017-09-13 15:56:16 -0700309 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800310}
311
Tao Bao92bdb5a2018-10-21 12:12:37 -0700312void gr_blit(const GRSurface* source, int sx, int sy, int w, int h, int dx, int dy) {
Tao Bao9f426332018-06-13 10:39:44 -0700313 if (source == nullptr) return;
Doug Zongker16f97c32014-03-06 16:16:05 -0800314
Luke Song846012f2017-09-13 15:56:16 -0700315 if (gr_draw->pixel_bytes != source->pixel_bytes) {
316 printf("gr_blit: source has wrong format\n");
317 return;
318 }
319
320 dx += overscan_offset_x;
321 dy += overscan_offset_y;
322
323 if (outside(dx, dy) || outside(dx + w - 1, dy + h - 1)) return;
324
Tao Bao44478df2018-07-31 22:01:03 -0700325 if (rotation != GRRotation::NONE) {
Luke Song846012f2017-09-13 15:56:16 -0700326 int src_row_pixels = source->row_bytes / source->pixel_bytes;
327 int row_pixels = gr_draw->row_bytes / gr_draw->pixel_bytes;
Tao Bao92bdb5a2018-10-21 12:12:37 -0700328 const uint32_t* src_py =
329 reinterpret_cast<const uint32_t*>(source->data()) + sy * source->row_bytes / 4 + sx;
330 uint32_t* dst_py = PixelAt(gr_draw, dx, dy, row_pixels);
Luke Song846012f2017-09-13 15:56:16 -0700331
332 for (int y = 0; y < h; y += 1) {
Tao Bao92bdb5a2018-10-21 12:12:37 -0700333 const uint32_t* src_px = src_py;
Luke Song846012f2017-09-13 15:56:16 -0700334 uint32_t* dst_px = dst_py;
335 for (int x = 0; x < w; x += 1) {
336 *dst_px = *src_px++;
337 incr_x(&dst_px, row_pixels);
338 }
339 src_py += src_row_pixels;
340 incr_y(&dst_py, row_pixels);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800341 }
Luke Song846012f2017-09-13 15:56:16 -0700342 } else {
Tao Bao92bdb5a2018-10-21 12:12:37 -0700343 const uint8_t* src_p = source->data() + sy * source->row_bytes + sx * source->pixel_bytes;
344 uint8_t* dst_p = gr_draw->data() + dy * gr_draw->row_bytes + dx * gr_draw->pixel_bytes;
Doug Zongker16f97c32014-03-06 16:16:05 -0800345
Tao Bao9f426332018-06-13 10:39:44 -0700346 for (int i = 0; i < h; ++i) {
Luke Song846012f2017-09-13 15:56:16 -0700347 memcpy(dst_p, src_p, w * source->pixel_bytes);
348 src_p += source->row_bytes;
349 dst_p += gr_draw->row_bytes;
Doug Zongker16f97c32014-03-06 16:16:05 -0800350 }
Luke Song846012f2017-09-13 15:56:16 -0700351 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800352}
353
Tao Bao9f426332018-06-13 10:39:44 -0700354unsigned int gr_get_width(const GRSurface* surface) {
355 if (surface == nullptr) {
Luke Song846012f2017-09-13 15:56:16 -0700356 return 0;
357 }
358 return surface->width;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800359}
360
Tao Bao9f426332018-06-13 10:39:44 -0700361unsigned int gr_get_height(const GRSurface* surface) {
362 if (surface == nullptr) {
Luke Song846012f2017-09-13 15:56:16 -0700363 return 0;
364 }
365 return surface->height;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800366}
367
Damien Bargiacchid00f5eb2016-09-09 07:14:08 -0700368int gr_init_font(const char* name, GRFont** dest) {
Luke Song846012f2017-09-13 15:56:16 -0700369 GRFont* font = static_cast<GRFont*>(calloc(1, sizeof(*gr_font)));
370 if (font == nullptr) {
371 return -1;
372 }
Damien Bargiacchid00f5eb2016-09-09 07:14:08 -0700373
Luke Song846012f2017-09-13 15:56:16 -0700374 int res = res_create_alpha_surface(name, &(font->texture));
375 if (res < 0) {
376 free(font);
377 return res;
378 }
Damien Bargiacchi35fff612016-08-11 15:57:03 -0700379
Luke Song846012f2017-09-13 15:56:16 -0700380 // The font image should be a 96x2 array of character images. The
381 // columns are the printable ASCII characters 0x20 - 0x7f. The
382 // top row is regular text; the bottom row is bold.
383 font->char_width = font->texture->width / 96;
384 font->char_height = font->texture->height / 2;
Damien Bargiacchid00f5eb2016-09-09 07:14:08 -0700385
Luke Song846012f2017-09-13 15:56:16 -0700386 *dest = font;
Damien Bargiacchi35fff612016-08-11 15:57:03 -0700387
Luke Song846012f2017-09-13 15:56:16 -0700388 return 0;
Damien Bargiacchi35fff612016-08-11 15:57:03 -0700389}
390
Doug Zongker5290f202014-03-11 13:22:04 -0700391void gr_flip() {
Tao Bao557fa1f2017-02-07 12:51:00 -0800392 gr_draw = gr_backend->Flip();
Doug Zongker5290f202014-03-11 13:22:04 -0700393}
394
Chihhang Chuang6f7362a2021-09-24 00:11:51 +0800395std::unique_ptr<MinuiBackend> create_backend(GraphicsBackend backend) {
396 switch (backend) {
397 case GraphicsBackend::DRM:
398 return std::make_unique<MinuiBackendDrm>();
399 case GraphicsBackend::FBDEV:
400 return std::make_unique<MinuiBackendFbdev>();
401 default:
402 return nullptr;
403 }
404}
405
Tao Bao557fa1f2017-02-07 12:51:00 -0800406int gr_init() {
Chihhang Chuang6f7362a2021-09-24 00:11:51 +0800407 return gr_init(default_backends);
408}
409
410int gr_init(std::initializer_list<GraphicsBackend> backends) {
Tao Baoed876a72018-07-31 21:32:50 -0700411 // pixel_format needs to be set before loading any resources or initializing backends.
Tao Bao050feb02018-09-05 21:45:42 -0700412 std::string format = android::base::GetProperty("ro.minui.pixel_format", "");
Tao Baoed876a72018-07-31 21:32:50 -0700413 if (format == "ABGR_8888") {
414 pixel_format = PixelFormat::ABGR;
415 } else if (format == "RGBX_8888") {
416 pixel_format = PixelFormat::RGBX;
Adrian Salidocc7b7eb2019-12-12 20:18:09 -0800417 } else if (format == "ARGB_8888") {
418 pixel_format = PixelFormat::ARGB;
Tao Baoed876a72018-07-31 21:32:50 -0700419 } else if (format == "BGRA_8888") {
420 pixel_format = PixelFormat::BGRA;
Patrik Torstensson51433b92021-01-30 18:16:25 -0800421 } else if (format == "RGBA_8888") {
422 pixel_format = PixelFormat::RGBA;
Tao Baoed876a72018-07-31 21:32:50 -0700423 } else {
424 pixel_format = PixelFormat::UNKNOWN;
425 }
426
Tianjie Xu55a2c4e2018-03-29 11:07:50 -0700427 int ret = gr_init_font("font", &gr_font);
428 if (ret != 0) {
Tianjie Xu842f2a32018-05-31 18:16:28 -0700429 printf("Failed to init font: %d, continuing graphic backend initialization without font file\n",
430 ret);
Tianjie Xu55a2c4e2018-03-29 11:07:50 -0700431 }
Alessandro Astone09caacf2020-03-09 23:17:50 +0100432 ret = gr_init_font("font_menu", &gr_font_menu);
433 if (ret != 0) {
434 printf("Failed to init menu font: %d. Falling back to system font\n", ret);
435 gr_font_menu = gr_font;
436 }
Doug Zongker16f97c32014-03-06 16:16:05 -0800437
Chihhang Chuang6f7362a2021-09-24 00:11:51 +0800438 std::unique_ptr<MinuiBackend> minui_backend;
439 for (GraphicsBackend backend : backends) {
440 minui_backend = create_backend(backend);
441 if (!minui_backend) {
442 printf("gr_init: minui_backend %d is a nullptr\n", backend);
443 continue;
444 }
445 gr_draw = minui_backend->Init();
446 if (gr_draw) break;
Tao Bao557fa1f2017-02-07 12:51:00 -0800447 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800448
Tao Bao557fa1f2017-02-07 12:51:00 -0800449 if (!gr_draw) {
450 return -1;
451 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800452
Chihhang Chuang6f7362a2021-09-24 00:11:51 +0800453 gr_backend = minui_backend.release();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800454
Tao Bao050feb02018-09-05 21:45:42 -0700455 int overscan_percent = android::base::GetIntProperty("ro.minui.overscan_percent", 0);
Tao Bao557fa1f2017-02-07 12:51:00 -0800456 overscan_offset_x = gr_draw->width * overscan_percent / 100;
457 overscan_offset_y = gr_draw->height * overscan_percent / 100;
458
459 gr_flip();
460 gr_flip();
Tianjie Xuccf00a22018-06-05 17:10:23 -0700461 if (!gr_draw) {
462 printf("gr_init: gr_draw becomes nullptr after gr_flip\n");
463 return -1;
464 }
Tao Bao557fa1f2017-02-07 12:51:00 -0800465
Tao Baoed876a72018-07-31 21:32:50 -0700466 std::string rotation_str =
Tao Bao050feb02018-09-05 21:45:42 -0700467 android::base::GetProperty("ro.minui.default_rotation", "ROTATION_NONE");
Tao Bao44478df2018-07-31 22:01:03 -0700468 if (rotation_str == "ROTATION_RIGHT") {
469 gr_rotate(GRRotation::RIGHT);
470 } else if (rotation_str == "ROTATION_DOWN") {
471 gr_rotate(GRRotation::DOWN);
472 } else if (rotation_str == "ROTATION_LEFT") {
473 gr_rotate(GRRotation::LEFT);
Tao Baoed876a72018-07-31 21:32:50 -0700474 } else { // "ROTATION_NONE" or unknown string
Tao Bao44478df2018-07-31 22:01:03 -0700475 gr_rotate(GRRotation::NONE);
476 }
Luke Song846012f2017-09-13 15:56:16 -0700477
LuK13377d2d5702023-06-25 01:53:57 +0200478 std::string touch_rotation_str =
479 android::base::GetProperty("ro.minui.default_touch_rotation", "ROTATION_NONE");
480 if (touch_rotation_str == "ROTATION_RIGHT") {
481 gr_rotate_touch(GRRotation::RIGHT);
482 } else if (touch_rotation_str == "ROTATION_DOWN") {
483 gr_rotate_touch(GRRotation::DOWN);
484 } else if (touch_rotation_str == "ROTATION_LEFT") {
485 gr_rotate_touch(GRRotation::LEFT);
486 } else { // "ROTATION_NONE" or unknown string
487 gr_rotate_touch(GRRotation::NONE);
488 }
489
Luke Song846012f2017-09-13 15:56:16 -0700490 if (gr_draw->pixel_bytes != 4) {
491 printf("gr_init: Only 4-byte pixel formats supported\n");
492 }
493
Tao Bao557fa1f2017-02-07 12:51:00 -0800494 return 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800495}
496
Tao Bao557fa1f2017-02-07 12:51:00 -0800497void gr_exit() {
498 delete gr_backend;
Tao Bao9f426332018-06-13 10:39:44 -0700499 gr_backend = nullptr;
500
501 delete gr_font;
502 gr_font = nullptr;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800503}
504
Tao Bao557fa1f2017-02-07 12:51:00 -0800505int gr_fb_width() {
Tao Bao44478df2018-07-31 22:01:03 -0700506 return (rotation == GRRotation::LEFT || rotation == GRRotation::RIGHT)
507 ? gr_draw->height - 2 * overscan_offset_y
508 : gr_draw->width - 2 * overscan_offset_x;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800509}
510
Tao Bao557fa1f2017-02-07 12:51:00 -0800511int gr_fb_height() {
Tao Bao44478df2018-07-31 22:01:03 -0700512 return (rotation == GRRotation::LEFT || rotation == GRRotation::RIGHT)
513 ? gr_draw->width - 2 * overscan_offset_x
514 : gr_draw->height - 2 * overscan_offset_y;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800515}
Dima Zavin4daf48a2011-08-30 11:59:20 -0700516
Alessandro Astone069344d2022-03-21 17:14:47 +0100517int gr_fb_width_real() {
518 return (rotation == GRRotation::LEFT || rotation == GRRotation::RIGHT) ? gr_draw->height
519 : gr_draw->width;
520}
521
522int gr_fb_height_real() {
523 return (rotation == GRRotation::LEFT || rotation == GRRotation::RIGHT) ? gr_draw->width
524 : gr_draw->height;
525}
526
527int gr_overscan_offset_x() {
528 return overscan_offset_x;
529}
530
531int gr_overscan_offset_y() {
532 return overscan_offset_y;
533}
534
LuK13377d2d5702023-06-25 01:53:57 +0200535GRRotation gr_touch_rotation() {
536 return touch_rotation;
537}
538
Tao Bao557fa1f2017-02-07 12:51:00 -0800539void gr_fb_blank(bool blank) {
540 gr_backend->Blank(blank);
Dima Zavin4daf48a2011-08-30 11:59:20 -0700541}
Luke Song846012f2017-09-13 15:56:16 -0700542
Weizhung Dingf4dfa1a2021-09-27 11:30:26 +0800543void gr_fb_blank(bool blank, int index) {
544 gr_backend->Blank(blank, static_cast<MinuiBackend::DrmConnector>(index));
545}
546
Luke Song846012f2017-09-13 15:56:16 -0700547void gr_rotate(GRRotation rot) {
548 rotation = rot;
549}
Weizhung Dingafd0a1b2022-07-19 16:34:43 +0800550
Ludvig Hansson510589f2022-11-07 12:39:20 +0100551GRRotation gr_get_rotation() {
552 return rotation;
553}
554
LuK13377d2d5702023-06-25 01:53:57 +0200555void gr_rotate_touch(GRRotation rot) {
556 touch_rotation = rot;
557}
558
Weizhung Dingafd0a1b2022-07-19 16:34:43 +0800559bool gr_has_multiple_connectors() {
560 return gr_backend->HasMultipleConnectors();
561}