blob: 41a36611200bdae751bc9b3cdc54119e069da5f7 [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;
Tao Bao557fa1f2017-02-07 12:51:00 -080033static MinuiBackend* gr_backend = nullptr;
Doug Zongker16f97c32014-03-06 16:16:05 -080034
Doug Zongkerc560a672012-12-18 16:31:27 -080035static int overscan_offset_x = 0;
36static int overscan_offset_y = 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080037
Luke Song846012f2017-09-13 15:56:16 -070038static uint32_t gr_current = ~0;
Doug Zongker16f97c32014-03-06 16:16:05 -080039
Tao Bao9f426332018-06-13 10:39:44 -070040// gr_draw is owned by backends.
Tao Bao92bdb5a2018-10-21 12:12:37 -070041static GRSurface* gr_draw = nullptr;
Tao Bao44478df2018-07-31 22:01:03 -070042static GRRotation rotation = GRRotation::NONE;
Tao Baoed876a72018-07-31 21:32:50 -070043static PixelFormat pixel_format = PixelFormat::UNKNOWN;
Chihhang Chuang6f7362a2021-09-24 00:11:51 +080044// The graphics backend list that provides fallback options for the default backend selection.
45// For example, it will fist try DRM, then try FBDEV if DRM is unavailable.
46constexpr auto default_backends = { GraphicsBackend::DRM, GraphicsBackend::FBDEV };
Doug Zongker16f97c32014-03-06 16:16:05 -080047
Luke Song846012f2017-09-13 15:56:16 -070048static bool outside(int x, int y) {
Tao Bao44478df2018-07-31 22:01:03 -070049 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 Zongker16f97c32014-03-06 16:16:05 -080052}
53
Luke Song846012f2017-09-13 15:56:16 -070054const GRFont* gr_sys_font() {
55 return gr_font;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080056}
57
Tao Baoed876a72018-07-31 21:32:50 -070058PixelFormat gr_pixel_format() {
59 return pixel_format;
60}
61
Luke Song846012f2017-09-13 15:56:16 -070062int gr_measure(const GRFont* font, const char* s) {
Tianjie Xu842f2a32018-05-31 18:16:28 -070063 if (font == nullptr) {
64 return -1;
65 }
66
Luke Song846012f2017-09-13 15:56:16 -070067 return font->char_width * strlen(s);
Damien Bargiacchi35fff612016-08-11 15:57:03 -070068}
69
Tianjie Xu842f2a32018-05-31 18:16:28 -070070int gr_font_size(const GRFont* font, int* x, int* y) {
71 if (font == nullptr) {
72 return -1;
73 }
74
Luke Song846012f2017-09-13 15:56:16 -070075 *x = font->char_width;
76 *y = font->char_height;
Tianjie Xu842f2a32018-05-31 18:16:28 -070077 return 0;
Dima Zavin3c7f00e2011-08-30 11:58:24 -070078}
79
Luke Song846012f2017-09-13 15:56:16 -070080// Blends gr_current onto pix value, assumes alpha as most significant byte.
Patrik Torstensson51433b92021-01-30 18:16:25 -080081static inline uint32_t pixel_blend_argb(uint8_t alpha, uint32_t pix) {
Luke Song846012f2017-09-13 15:56:16 -070082 if (alpha == 255) return gr_current;
83 if (alpha == 0) return pix;
84 uint32_t pix_r = pix & 0xff;
85 uint32_t pix_g = pix & 0xff00;
86 uint32_t pix_b = pix & 0xff0000;
87 uint32_t cur_r = gr_current & 0xff;
88 uint32_t cur_g = gr_current & 0xff00;
89 uint32_t cur_b = gr_current & 0xff0000;
90
91 uint32_t out_r = (pix_r * (255 - alpha) + cur_r * alpha) / 255;
92 uint32_t out_g = (pix_g * (255 - alpha) + cur_g * alpha) / 255;
93 uint32_t out_b = (pix_b * (255 - alpha) + cur_b * alpha) / 255;
94
95 return (out_r & 0xff) | (out_g & 0xff00) | (out_b & 0xff0000) | (gr_current & 0xff000000);
96}
97
Patrik Torstensson51433b92021-01-30 18:16:25 -080098static inline uint32_t pixel_blend_rgba(uint8_t alpha, uint32_t pix) {
99 if (alpha == 255) return gr_current;
100 if (alpha == 0) return pix;
101 uint32_t pix_r = pix & 0xff00;
102 uint32_t pix_g = pix & 0xff0000;
103 uint32_t pix_b = pix & 0xff000000;
104 uint32_t cur_r = gr_current & 0xff00;
105 uint32_t cur_g = gr_current & 0xff0000;
106 uint32_t cur_b = gr_current & 0xff000000;
107
108 uint32_t out_r = (pix_r * (255 - alpha) + cur_r * alpha) / 255;
109 uint32_t out_g = (pix_g * (255 - alpha) + cur_g * alpha) / 255;
110 uint32_t out_b = (pix_b * (255 - alpha) + cur_b * alpha) / 255;
111
112 return (gr_current & 0xff) | (out_r & 0xff00) | (out_g & 0xff0000) | (out_b & 0xff000000);
113}
114
115static inline uint32_t pixel_blend(uint8_t alpha, uint32_t pix) {
116 if (pixel_format == PixelFormat::RGBA) {
117 return pixel_blend_rgba(alpha, pix);
118 }
119 return pixel_blend_argb(alpha, pix);
120}
121
122static inline uint32_t get_alphamask() {
123 if (pixel_format == PixelFormat::RGBA) {
124 return 0x000000ff;
125 }
126 return 0xff000000;
127}
128
129static inline uint8_t get_alpha_shift() {
130 if (pixel_format == PixelFormat::RGBA) {
131 return 0;
132 }
133 return 24;
134}
135
136static inline uint8_t get_alpha(uint32_t pix) {
137 return static_cast<uint8_t>((pix & (gr_current & get_alphamask())) >> get_alpha_shift());
138}
139
Tao Bao9f426332018-06-13 10:39:44 -0700140// Increments pixel pointer right, with current rotation.
Luke Song846012f2017-09-13 15:56:16 -0700141static void incr_x(uint32_t** p, int row_pixels) {
Tao Bao44478df2018-07-31 22:01:03 -0700142 if (rotation == GRRotation::LEFT) {
143 *p = *p - row_pixels;
144 } else if (rotation == GRRotation::RIGHT) {
145 *p = *p + row_pixels;
146 } else if (rotation == GRRotation::DOWN) {
147 *p = *p - 1;
148 } else { // GRRotation::NONE
149 *p = *p + 1;
Luke Song846012f2017-09-13 15:56:16 -0700150 }
151}
152
Tao Bao9f426332018-06-13 10:39:44 -0700153// Increments pixel pointer down, with current rotation.
Luke Song846012f2017-09-13 15:56:16 -0700154static void incr_y(uint32_t** p, int row_pixels) {
Tao Bao44478df2018-07-31 22:01:03 -0700155 if (rotation == GRRotation::LEFT) {
156 *p = *p + 1;
157 } else if (rotation == GRRotation::RIGHT) {
158 *p = *p - 1;
159 } else if (rotation == GRRotation::DOWN) {
160 *p = *p - row_pixels;
161 } else { // GRRotation::NONE
162 *p = *p + row_pixels;
Luke Song846012f2017-09-13 15:56:16 -0700163 }
164}
165
Tao Bao9f426332018-06-13 10:39:44 -0700166// Returns pixel pointer at given coordinates with rotation adjustment.
Tao Bao92bdb5a2018-10-21 12:12:37 -0700167static uint32_t* PixelAt(GRSurface* surface, int x, int y, int row_pixels) {
Luke Song846012f2017-09-13 15:56:16 -0700168 switch (rotation) {
Tao Bao44478df2018-07-31 22:01:03 -0700169 case GRRotation::NONE:
Tao Bao92bdb5a2018-10-21 12:12:37 -0700170 return reinterpret_cast<uint32_t*>(surface->data()) + y * row_pixels + x;
Tao Bao44478df2018-07-31 22:01:03 -0700171 case GRRotation::RIGHT:
Tao Bao92bdb5a2018-10-21 12:12:37 -0700172 return reinterpret_cast<uint32_t*>(surface->data()) + x * row_pixels + (surface->width - y);
Tao Bao44478df2018-07-31 22:01:03 -0700173 case GRRotation::DOWN:
Tao Bao92bdb5a2018-10-21 12:12:37 -0700174 return reinterpret_cast<uint32_t*>(surface->data()) + (surface->height - 1 - y) * row_pixels +
175 (surface->width - 1 - x);
Tao Bao44478df2018-07-31 22:01:03 -0700176 case GRRotation::LEFT:
Tao Bao92bdb5a2018-10-21 12:12:37 -0700177 return reinterpret_cast<uint32_t*>(surface->data()) + (surface->height - 1 - x) * row_pixels +
178 y;
Luke Song846012f2017-09-13 15:56:16 -0700179 default:
Tao Bao44478df2018-07-31 22:01:03 -0700180 printf("invalid rotation %d", static_cast<int>(rotation));
Luke Song846012f2017-09-13 15:56:16 -0700181 }
182 return nullptr;
183}
184
Tao Bao92bdb5a2018-10-21 12:12:37 -0700185static void TextBlend(const uint8_t* src_p, int src_row_bytes, uint32_t* dst_p, int dst_row_pixels,
186 int width, int height) {
Patrik Torstensson51433b92021-01-30 18:16:25 -0800187 uint8_t alpha_current = get_alpha(gr_current);
Luke Song846012f2017-09-13 15:56:16 -0700188 for (int j = 0; j < height; ++j) {
Tao Bao92bdb5a2018-10-21 12:12:37 -0700189 const uint8_t* sx = src_p;
Luke Song846012f2017-09-13 15:56:16 -0700190 uint32_t* px = dst_p;
191 for (int i = 0; i < width; ++i, incr_x(&px, dst_row_pixels)) {
192 uint8_t a = *sx++;
193 if (alpha_current < 255) a = (static_cast<uint32_t>(a) * alpha_current) / 255;
194 *px = pixel_blend(a, *px);
Doug Zongker16f97c32014-03-06 16:16:05 -0800195 }
Luke Song846012f2017-09-13 15:56:16 -0700196 src_p += src_row_bytes;
197 incr_y(&dst_p, dst_row_pixels);
198 }
Doug Zongker16f97c32014-03-06 16:16:05 -0800199}
200
Luke Song846012f2017-09-13 15:56:16 -0700201void gr_text(const GRFont* font, int x, int y, const char* s, bool bold) {
Patrik Torstensson51433b92021-01-30 18:16:25 -0800202 if (!font || !font->texture || (gr_current & get_alphamask()) == 0) return;
Doug Zongker55a36ac2013-03-04 15:49:02 -0800203
Luke Song846012f2017-09-13 15:56:16 -0700204 if (font->texture->pixel_bytes != 1) {
205 printf("gr_text: font has wrong format\n");
206 return;
207 }
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800208
Luke Song846012f2017-09-13 15:56:16 -0700209 bold = bold && (font->texture->height != font->char_height);
Doug Zongkerc560a672012-12-18 16:31:27 -0800210
Luke Song846012f2017-09-13 15:56:16 -0700211 x += overscan_offset_x;
212 y += overscan_offset_y;
Doug Zongker16f97c32014-03-06 16:16:05 -0800213
Luke Song846012f2017-09-13 15:56:16 -0700214 unsigned char ch;
215 while ((ch = *s++)) {
216 if (outside(x, y) || outside(x + font->char_width - 1, y + font->char_height - 1)) break;
Elliott Hughes01a4d082015-03-24 15:21:48 -0700217
Luke Song846012f2017-09-13 15:56:16 -0700218 if (ch < ' ' || ch > '~') {
219 ch = '?';
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800220 }
Luke Song846012f2017-09-13 15:56:16 -0700221
222 int row_pixels = gr_draw->row_bytes / gr_draw->pixel_bytes;
Tao Bao92bdb5a2018-10-21 12:12:37 -0700223 const uint8_t* src_p = font->texture->data() + ((ch - ' ') * font->char_width) +
224 (bold ? font->char_height * font->texture->row_bytes : 0);
225 uint32_t* dst_p = PixelAt(gr_draw, x, y, row_pixels);
Luke Song846012f2017-09-13 15:56:16 -0700226
Tao Bao92bdb5a2018-10-21 12:12:37 -0700227 TextBlend(src_p, font->texture->row_bytes, dst_p, row_pixels, font->char_width,
228 font->char_height);
Luke Song846012f2017-09-13 15:56:16 -0700229
230 x += font->char_width;
231 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800232}
233
Tao Bao92bdb5a2018-10-21 12:12:37 -0700234void gr_texticon(int x, int y, const GRSurface* icon) {
Tao Bao9f426332018-06-13 10:39:44 -0700235 if (icon == nullptr) return;
Doug Zongker16f97c32014-03-06 16:16:05 -0800236
Luke Song846012f2017-09-13 15:56:16 -0700237 if (icon->pixel_bytes != 1) {
238 printf("gr_texticon: source has wrong format\n");
239 return;
240 }
Doug Zongker02ec6b82012-08-22 17:26:40 -0700241
Luke Song846012f2017-09-13 15:56:16 -0700242 x += overscan_offset_x;
243 y += overscan_offset_y;
Doug Zongkerc560a672012-12-18 16:31:27 -0800244
Luke Song846012f2017-09-13 15:56:16 -0700245 if (outside(x, y) || outside(x + icon->width - 1, y + icon->height - 1)) return;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700246
Luke Song846012f2017-09-13 15:56:16 -0700247 int row_pixels = gr_draw->row_bytes / gr_draw->pixel_bytes;
Tao Bao92bdb5a2018-10-21 12:12:37 -0700248 const uint8_t* src_p = icon->data();
249 uint32_t* dst_p = PixelAt(gr_draw, x, y, row_pixels);
250 TextBlend(src_p, icon->row_bytes, dst_p, row_pixels, icon->width, icon->height);
Doug Zongker16f97c32014-03-06 16:16:05 -0800251}
252
Luke Song846012f2017-09-13 15:56:16 -0700253void gr_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a) {
254 uint32_t r32 = r, g32 = g, b32 = b, a32 = a;
Adrian Salidocc7b7eb2019-12-12 20:18:09 -0800255 if (pixel_format == PixelFormat::ARGB || pixel_format == PixelFormat::BGRA) {
Tao Baoed876a72018-07-31 21:32:50 -0700256 gr_current = (a32 << 24) | (r32 << 16) | (g32 << 8) | b32;
Patrik Torstensson51433b92021-01-30 18:16:25 -0800257 } else if (pixel_format == PixelFormat::RGBA) {
258 gr_current = (b32 << 24) | (g32 << 16) | (r32 << 8) | a32;
Tao Baoed876a72018-07-31 21:32:50 -0700259 } else {
260 gr_current = (a32 << 24) | (b32 << 16) | (g32 << 8) | r32;
261 }
Doug Zongker16f97c32014-03-06 16:16:05 -0800262}
263
Luke Song846012f2017-09-13 15:56:16 -0700264void gr_clear() {
265 if ((gr_current & 0xff) == ((gr_current >> 8) & 0xff) &&
266 (gr_current & 0xff) == ((gr_current >> 16) & 0xff) &&
267 (gr_current & 0xff) == ((gr_current >> 24) & 0xff) &&
268 gr_draw->row_bytes == gr_draw->width * gr_draw->pixel_bytes) {
Tao Bao92bdb5a2018-10-21 12:12:37 -0700269 memset(gr_draw->data(), gr_current & 0xff, gr_draw->height * gr_draw->row_bytes);
Luke Song846012f2017-09-13 15:56:16 -0700270 } else {
Tao Bao92bdb5a2018-10-21 12:12:37 -0700271 uint32_t* px = reinterpret_cast<uint32_t*>(gr_draw->data());
Luke Song846012f2017-09-13 15:56:16 -0700272 int row_diff = gr_draw->row_bytes / gr_draw->pixel_bytes - gr_draw->width;
273 for (int y = 0; y < gr_draw->height; ++y) {
274 for (int x = 0; x < gr_draw->width; ++x) {
275 *px++ = gr_current;
276 }
277 px += row_diff;
Doug Zongker16f97c32014-03-06 16:16:05 -0800278 }
Luke Song846012f2017-09-13 15:56:16 -0700279 }
Doug Zongker02ec6b82012-08-22 17:26:40 -0700280}
281
Luke Song846012f2017-09-13 15:56:16 -0700282void gr_fill(int x1, int y1, int x2, int y2) {
283 x1 += overscan_offset_x;
284 y1 += overscan_offset_y;
Doug Zongkerc560a672012-12-18 16:31:27 -0800285
Luke Song846012f2017-09-13 15:56:16 -0700286 x2 += overscan_offset_x;
287 y2 += overscan_offset_y;
Doug Zongkerc560a672012-12-18 16:31:27 -0800288
Luke Song846012f2017-09-13 15:56:16 -0700289 if (outside(x1, y1) || outside(x2 - 1, y2 - 1)) return;
Doug Zongker16f97c32014-03-06 16:16:05 -0800290
Luke Song846012f2017-09-13 15:56:16 -0700291 int row_pixels = gr_draw->row_bytes / gr_draw->pixel_bytes;
Tao Bao92bdb5a2018-10-21 12:12:37 -0700292 uint32_t* p = PixelAt(gr_draw, x1, y1, row_pixels);
Patrik Torstensson51433b92021-01-30 18:16:25 -0800293 uint8_t alpha = get_alpha(gr_current);
Luke Song846012f2017-09-13 15:56:16 -0700294 if (alpha > 0) {
295 for (int y = y1; y < y2; ++y) {
296 uint32_t* px = p;
297 for (int x = x1; x < x2; ++x) {
298 *px = pixel_blend(alpha, *px);
299 incr_x(&px, row_pixels);
300 }
301 incr_y(&p, row_pixels);
Doug Zongker16f97c32014-03-06 16:16:05 -0800302 }
Luke Song846012f2017-09-13 15:56:16 -0700303 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800304}
305
Tao Bao92bdb5a2018-10-21 12:12:37 -0700306void gr_blit(const GRSurface* source, int sx, int sy, int w, int h, int dx, int dy) {
Tao Bao9f426332018-06-13 10:39:44 -0700307 if (source == nullptr) return;
Doug Zongker16f97c32014-03-06 16:16:05 -0800308
Luke Song846012f2017-09-13 15:56:16 -0700309 if (gr_draw->pixel_bytes != source->pixel_bytes) {
310 printf("gr_blit: source has wrong format\n");
311 return;
312 }
313
314 dx += overscan_offset_x;
315 dy += overscan_offset_y;
316
317 if (outside(dx, dy) || outside(dx + w - 1, dy + h - 1)) return;
318
Tao Bao44478df2018-07-31 22:01:03 -0700319 if (rotation != GRRotation::NONE) {
Luke Song846012f2017-09-13 15:56:16 -0700320 int src_row_pixels = source->row_bytes / source->pixel_bytes;
321 int row_pixels = gr_draw->row_bytes / gr_draw->pixel_bytes;
Tao Bao92bdb5a2018-10-21 12:12:37 -0700322 const uint32_t* src_py =
323 reinterpret_cast<const uint32_t*>(source->data()) + sy * source->row_bytes / 4 + sx;
324 uint32_t* dst_py = PixelAt(gr_draw, dx, dy, row_pixels);
Luke Song846012f2017-09-13 15:56:16 -0700325
326 for (int y = 0; y < h; y += 1) {
Tao Bao92bdb5a2018-10-21 12:12:37 -0700327 const uint32_t* src_px = src_py;
Luke Song846012f2017-09-13 15:56:16 -0700328 uint32_t* dst_px = dst_py;
329 for (int x = 0; x < w; x += 1) {
330 *dst_px = *src_px++;
331 incr_x(&dst_px, row_pixels);
332 }
333 src_py += src_row_pixels;
334 incr_y(&dst_py, row_pixels);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800335 }
Luke Song846012f2017-09-13 15:56:16 -0700336 } else {
Tao Bao92bdb5a2018-10-21 12:12:37 -0700337 const uint8_t* src_p = source->data() + sy * source->row_bytes + sx * source->pixel_bytes;
338 uint8_t* dst_p = gr_draw->data() + dy * gr_draw->row_bytes + dx * gr_draw->pixel_bytes;
Doug Zongker16f97c32014-03-06 16:16:05 -0800339
Tao Bao9f426332018-06-13 10:39:44 -0700340 for (int i = 0; i < h; ++i) {
Luke Song846012f2017-09-13 15:56:16 -0700341 memcpy(dst_p, src_p, w * source->pixel_bytes);
342 src_p += source->row_bytes;
343 dst_p += gr_draw->row_bytes;
Doug Zongker16f97c32014-03-06 16:16:05 -0800344 }
Luke Song846012f2017-09-13 15:56:16 -0700345 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800346}
347
Tao Bao9f426332018-06-13 10:39:44 -0700348unsigned int gr_get_width(const GRSurface* surface) {
349 if (surface == nullptr) {
Luke Song846012f2017-09-13 15:56:16 -0700350 return 0;
351 }
352 return surface->width;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800353}
354
Tao Bao9f426332018-06-13 10:39:44 -0700355unsigned int gr_get_height(const GRSurface* surface) {
356 if (surface == nullptr) {
Luke Song846012f2017-09-13 15:56:16 -0700357 return 0;
358 }
359 return surface->height;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800360}
361
Damien Bargiacchid00f5eb2016-09-09 07:14:08 -0700362int gr_init_font(const char* name, GRFont** dest) {
Luke Song846012f2017-09-13 15:56:16 -0700363 GRFont* font = static_cast<GRFont*>(calloc(1, sizeof(*gr_font)));
364 if (font == nullptr) {
365 return -1;
366 }
Damien Bargiacchid00f5eb2016-09-09 07:14:08 -0700367
Luke Song846012f2017-09-13 15:56:16 -0700368 int res = res_create_alpha_surface(name, &(font->texture));
369 if (res < 0) {
370 free(font);
371 return res;
372 }
Damien Bargiacchi35fff612016-08-11 15:57:03 -0700373
Luke Song846012f2017-09-13 15:56:16 -0700374 // The font image should be a 96x2 array of character images. The
375 // columns are the printable ASCII characters 0x20 - 0x7f. The
376 // top row is regular text; the bottom row is bold.
377 font->char_width = font->texture->width / 96;
378 font->char_height = font->texture->height / 2;
Damien Bargiacchid00f5eb2016-09-09 07:14:08 -0700379
Luke Song846012f2017-09-13 15:56:16 -0700380 *dest = font;
Damien Bargiacchi35fff612016-08-11 15:57:03 -0700381
Luke Song846012f2017-09-13 15:56:16 -0700382 return 0;
Damien Bargiacchi35fff612016-08-11 15:57:03 -0700383}
384
Doug Zongker5290f202014-03-11 13:22:04 -0700385void gr_flip() {
Tao Bao557fa1f2017-02-07 12:51:00 -0800386 gr_draw = gr_backend->Flip();
Doug Zongker5290f202014-03-11 13:22:04 -0700387}
388
Chihhang Chuang6f7362a2021-09-24 00:11:51 +0800389std::unique_ptr<MinuiBackend> create_backend(GraphicsBackend backend) {
390 switch (backend) {
391 case GraphicsBackend::DRM:
392 return std::make_unique<MinuiBackendDrm>();
393 case GraphicsBackend::FBDEV:
394 return std::make_unique<MinuiBackendFbdev>();
395 default:
396 return nullptr;
397 }
398}
399
Tao Bao557fa1f2017-02-07 12:51:00 -0800400int gr_init() {
Chihhang Chuang6f7362a2021-09-24 00:11:51 +0800401 return gr_init(default_backends);
402}
403
404int gr_init(std::initializer_list<GraphicsBackend> backends) {
Tao Baoed876a72018-07-31 21:32:50 -0700405 // pixel_format needs to be set before loading any resources or initializing backends.
Tao Bao050feb02018-09-05 21:45:42 -0700406 std::string format = android::base::GetProperty("ro.minui.pixel_format", "");
Tao Baoed876a72018-07-31 21:32:50 -0700407 if (format == "ABGR_8888") {
408 pixel_format = PixelFormat::ABGR;
409 } else if (format == "RGBX_8888") {
410 pixel_format = PixelFormat::RGBX;
Adrian Salidocc7b7eb2019-12-12 20:18:09 -0800411 } else if (format == "ARGB_8888") {
412 pixel_format = PixelFormat::ARGB;
Tao Baoed876a72018-07-31 21:32:50 -0700413 } else if (format == "BGRA_8888") {
414 pixel_format = PixelFormat::BGRA;
Patrik Torstensson51433b92021-01-30 18:16:25 -0800415 } else if (format == "RGBA_8888") {
416 pixel_format = PixelFormat::RGBA;
Tao Baoed876a72018-07-31 21:32:50 -0700417 } else {
418 pixel_format = PixelFormat::UNKNOWN;
419 }
420
Tianjie Xu55a2c4e2018-03-29 11:07:50 -0700421 int ret = gr_init_font("font", &gr_font);
422 if (ret != 0) {
Tianjie Xu842f2a32018-05-31 18:16:28 -0700423 printf("Failed to init font: %d, continuing graphic backend initialization without font file\n",
424 ret);
Tianjie Xu55a2c4e2018-03-29 11:07:50 -0700425 }
Doug Zongker16f97c32014-03-06 16:16:05 -0800426
Chihhang Chuang6f7362a2021-09-24 00:11:51 +0800427 std::unique_ptr<MinuiBackend> minui_backend;
428 for (GraphicsBackend backend : backends) {
429 minui_backend = create_backend(backend);
430 if (!minui_backend) {
431 printf("gr_init: minui_backend %d is a nullptr\n", backend);
432 continue;
433 }
434 gr_draw = minui_backend->Init();
435 if (gr_draw) break;
Tao Bao557fa1f2017-02-07 12:51:00 -0800436 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800437
Tao Bao557fa1f2017-02-07 12:51:00 -0800438 if (!gr_draw) {
439 return -1;
440 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800441
Chihhang Chuang6f7362a2021-09-24 00:11:51 +0800442 gr_backend = minui_backend.release();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800443
Tao Bao050feb02018-09-05 21:45:42 -0700444 int overscan_percent = android::base::GetIntProperty("ro.minui.overscan_percent", 0);
Tao Bao557fa1f2017-02-07 12:51:00 -0800445 overscan_offset_x = gr_draw->width * overscan_percent / 100;
446 overscan_offset_y = gr_draw->height * overscan_percent / 100;
447
448 gr_flip();
449 gr_flip();
Tianjie Xuccf00a22018-06-05 17:10:23 -0700450 if (!gr_draw) {
451 printf("gr_init: gr_draw becomes nullptr after gr_flip\n");
452 return -1;
453 }
Tao Bao557fa1f2017-02-07 12:51:00 -0800454
Tao Baoed876a72018-07-31 21:32:50 -0700455 std::string rotation_str =
Tao Bao050feb02018-09-05 21:45:42 -0700456 android::base::GetProperty("ro.minui.default_rotation", "ROTATION_NONE");
Tao Bao44478df2018-07-31 22:01:03 -0700457 if (rotation_str == "ROTATION_RIGHT") {
458 gr_rotate(GRRotation::RIGHT);
459 } else if (rotation_str == "ROTATION_DOWN") {
460 gr_rotate(GRRotation::DOWN);
461 } else if (rotation_str == "ROTATION_LEFT") {
462 gr_rotate(GRRotation::LEFT);
Tao Baoed876a72018-07-31 21:32:50 -0700463 } else { // "ROTATION_NONE" or unknown string
Tao Bao44478df2018-07-31 22:01:03 -0700464 gr_rotate(GRRotation::NONE);
465 }
Luke Song846012f2017-09-13 15:56:16 -0700466
467 if (gr_draw->pixel_bytes != 4) {
468 printf("gr_init: Only 4-byte pixel formats supported\n");
469 }
470
Tao Bao557fa1f2017-02-07 12:51:00 -0800471 return 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800472}
473
Tao Bao557fa1f2017-02-07 12:51:00 -0800474void gr_exit() {
475 delete gr_backend;
Tao Bao9f426332018-06-13 10:39:44 -0700476 gr_backend = nullptr;
477
478 delete gr_font;
479 gr_font = nullptr;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800480}
481
Tao Bao557fa1f2017-02-07 12:51:00 -0800482int gr_fb_width() {
Tao Bao44478df2018-07-31 22:01:03 -0700483 return (rotation == GRRotation::LEFT || rotation == GRRotation::RIGHT)
484 ? gr_draw->height - 2 * overscan_offset_y
485 : gr_draw->width - 2 * overscan_offset_x;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800486}
487
Tao Bao557fa1f2017-02-07 12:51:00 -0800488int gr_fb_height() {
Tao Bao44478df2018-07-31 22:01:03 -0700489 return (rotation == GRRotation::LEFT || rotation == GRRotation::RIGHT)
490 ? gr_draw->width - 2 * overscan_offset_x
491 : gr_draw->height - 2 * overscan_offset_y;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800492}
Dima Zavin4daf48a2011-08-30 11:59:20 -0700493
Tao Bao557fa1f2017-02-07 12:51:00 -0800494void gr_fb_blank(bool blank) {
495 gr_backend->Blank(blank);
Dima Zavin4daf48a2011-08-30 11:59:20 -0700496}
Luke Song846012f2017-09-13 15:56:16 -0700497
Weizhung Dingf4dfa1a2021-09-27 11:30:26 +0800498void gr_fb_blank(bool blank, int index) {
499 gr_backend->Blank(blank, static_cast<MinuiBackend::DrmConnector>(index));
500}
501
Luke Song846012f2017-09-13 15:56:16 -0700502void gr_rotate(GRRotation rot) {
503 rotation = rot;
504}
Weizhung Dinge43c5032022-07-19 16:34:43 +0800505
506bool gr_has_multiple_connectors() {
507 return gr_backend->HasMultipleConnectors();
508}