blob: f25694ab574c627b78b38c7dd4a9aa08b7c7a315 [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;
39static constexpr uint32_t alpha_mask = 0xff000000;
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
Tao Baoed876a72018-07-31 21:32:50 -070059PixelFormat gr_pixel_format() {
60 return pixel_format;
61}
62
Luke Song846012f2017-09-13 15:56:16 -070063int gr_measure(const GRFont* font, const char* s) {
Tianjie Xu842f2a32018-05-31 18:16:28 -070064 if (font == nullptr) {
65 return -1;
66 }
67
Luke Song846012f2017-09-13 15:56:16 -070068 return font->char_width * strlen(s);
Damien Bargiacchi35fff612016-08-11 15:57:03 -070069}
70
Tianjie Xu842f2a32018-05-31 18:16:28 -070071int gr_font_size(const GRFont* font, int* x, int* y) {
72 if (font == nullptr) {
73 return -1;
74 }
75
Luke Song846012f2017-09-13 15:56:16 -070076 *x = font->char_width;
77 *y = font->char_height;
Tianjie Xu842f2a32018-05-31 18:16:28 -070078 return 0;
Dima Zavin3c7f00e2011-08-30 11:58:24 -070079}
80
Luke Song846012f2017-09-13 15:56:16 -070081// Blends gr_current onto pix value, assumes alpha as most significant byte.
82static inline uint32_t pixel_blend(uint8_t alpha, uint32_t pix) {
83 if (alpha == 255) return gr_current;
84 if (alpha == 0) return pix;
85 uint32_t pix_r = pix & 0xff;
86 uint32_t pix_g = pix & 0xff00;
87 uint32_t pix_b = pix & 0xff0000;
88 uint32_t cur_r = gr_current & 0xff;
89 uint32_t cur_g = gr_current & 0xff00;
90 uint32_t cur_b = gr_current & 0xff0000;
91
92 uint32_t out_r = (pix_r * (255 - alpha) + cur_r * alpha) / 255;
93 uint32_t out_g = (pix_g * (255 - alpha) + cur_g * alpha) / 255;
94 uint32_t out_b = (pix_b * (255 - alpha) + cur_b * alpha) / 255;
95
96 return (out_r & 0xff) | (out_g & 0xff00) | (out_b & 0xff0000) | (gr_current & 0xff000000);
97}
98
Tao Bao9f426332018-06-13 10:39:44 -070099// Increments pixel pointer right, with current rotation.
Luke Song846012f2017-09-13 15:56:16 -0700100static void incr_x(uint32_t** p, int row_pixels) {
Tao Bao44478df2018-07-31 22:01:03 -0700101 if (rotation == GRRotation::LEFT) {
102 *p = *p - row_pixels;
103 } else if (rotation == GRRotation::RIGHT) {
104 *p = *p + row_pixels;
105 } else if (rotation == GRRotation::DOWN) {
106 *p = *p - 1;
107 } else { // GRRotation::NONE
108 *p = *p + 1;
Luke Song846012f2017-09-13 15:56:16 -0700109 }
110}
111
Tao Bao9f426332018-06-13 10:39:44 -0700112// Increments pixel pointer down, with current rotation.
Luke Song846012f2017-09-13 15:56:16 -0700113static void incr_y(uint32_t** p, int row_pixels) {
Tao Bao44478df2018-07-31 22:01:03 -0700114 if (rotation == GRRotation::LEFT) {
115 *p = *p + 1;
116 } else if (rotation == GRRotation::RIGHT) {
117 *p = *p - 1;
118 } else if (rotation == GRRotation::DOWN) {
119 *p = *p - row_pixels;
120 } else { // GRRotation::NONE
121 *p = *p + row_pixels;
Luke Song846012f2017-09-13 15:56:16 -0700122 }
123}
124
Tao Bao9f426332018-06-13 10:39:44 -0700125// Returns pixel pointer at given coordinates with rotation adjustment.
Tao Bao92bdb5a2018-10-21 12:12:37 -0700126static uint32_t* PixelAt(GRSurface* surface, int x, int y, int row_pixels) {
Luke Song846012f2017-09-13 15:56:16 -0700127 switch (rotation) {
Tao Bao44478df2018-07-31 22:01:03 -0700128 case GRRotation::NONE:
Tao Bao92bdb5a2018-10-21 12:12:37 -0700129 return reinterpret_cast<uint32_t*>(surface->data()) + y * row_pixels + x;
Tao Bao44478df2018-07-31 22:01:03 -0700130 case GRRotation::RIGHT:
Tao Bao92bdb5a2018-10-21 12:12:37 -0700131 return reinterpret_cast<uint32_t*>(surface->data()) + x * row_pixels + (surface->width - y);
Tao Bao44478df2018-07-31 22:01:03 -0700132 case GRRotation::DOWN:
Tao Bao92bdb5a2018-10-21 12:12:37 -0700133 return reinterpret_cast<uint32_t*>(surface->data()) + (surface->height - 1 - y) * row_pixels +
134 (surface->width - 1 - x);
Tao Bao44478df2018-07-31 22:01:03 -0700135 case GRRotation::LEFT:
Tao Bao92bdb5a2018-10-21 12:12:37 -0700136 return reinterpret_cast<uint32_t*>(surface->data()) + (surface->height - 1 - x) * row_pixels +
137 y;
Luke Song846012f2017-09-13 15:56:16 -0700138 default:
Tao Bao44478df2018-07-31 22:01:03 -0700139 printf("invalid rotation %d", static_cast<int>(rotation));
Luke Song846012f2017-09-13 15:56:16 -0700140 }
141 return nullptr;
142}
143
Tao Bao92bdb5a2018-10-21 12:12:37 -0700144static void TextBlend(const uint8_t* src_p, int src_row_bytes, uint32_t* dst_p, int dst_row_pixels,
145 int width, int height) {
Luke Song846012f2017-09-13 15:56:16 -0700146 uint8_t alpha_current = static_cast<uint8_t>((alpha_mask & gr_current) >> 24);
147 for (int j = 0; j < height; ++j) {
Tao Bao92bdb5a2018-10-21 12:12:37 -0700148 const uint8_t* sx = src_p;
Luke Song846012f2017-09-13 15:56:16 -0700149 uint32_t* px = dst_p;
150 for (int i = 0; i < width; ++i, incr_x(&px, dst_row_pixels)) {
151 uint8_t a = *sx++;
152 if (alpha_current < 255) a = (static_cast<uint32_t>(a) * alpha_current) / 255;
153 *px = pixel_blend(a, *px);
Doug Zongker16f97c32014-03-06 16:16:05 -0800154 }
Luke Song846012f2017-09-13 15:56:16 -0700155 src_p += src_row_bytes;
156 incr_y(&dst_p, dst_row_pixels);
157 }
Doug Zongker16f97c32014-03-06 16:16:05 -0800158}
159
Luke Song846012f2017-09-13 15:56:16 -0700160void gr_text(const GRFont* font, int x, int y, const char* s, bool bold) {
161 if (!font || !font->texture || (gr_current & alpha_mask) == 0) return;
Doug Zongker55a36ac2013-03-04 15:49:02 -0800162
Luke Song846012f2017-09-13 15:56:16 -0700163 if (font->texture->pixel_bytes != 1) {
164 printf("gr_text: font has wrong format\n");
165 return;
166 }
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800167
Luke Song846012f2017-09-13 15:56:16 -0700168 bold = bold && (font->texture->height != font->char_height);
Doug Zongkerc560a672012-12-18 16:31:27 -0800169
Luke Song846012f2017-09-13 15:56:16 -0700170 x += overscan_offset_x;
171 y += overscan_offset_y;
Doug Zongker16f97c32014-03-06 16:16:05 -0800172
Luke Song846012f2017-09-13 15:56:16 -0700173 unsigned char ch;
174 while ((ch = *s++)) {
175 if (outside(x, y) || outside(x + font->char_width - 1, y + font->char_height - 1)) break;
Elliott Hughes01a4d082015-03-24 15:21:48 -0700176
Luke Song846012f2017-09-13 15:56:16 -0700177 if (ch < ' ' || ch > '~') {
178 ch = '?';
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800179 }
Luke Song846012f2017-09-13 15:56:16 -0700180
181 int row_pixels = gr_draw->row_bytes / gr_draw->pixel_bytes;
Tao Bao92bdb5a2018-10-21 12:12:37 -0700182 const uint8_t* src_p = font->texture->data() + ((ch - ' ') * font->char_width) +
183 (bold ? font->char_height * font->texture->row_bytes : 0);
184 uint32_t* dst_p = PixelAt(gr_draw, x, y, row_pixels);
Luke Song846012f2017-09-13 15:56:16 -0700185
Tao Bao92bdb5a2018-10-21 12:12:37 -0700186 TextBlend(src_p, font->texture->row_bytes, dst_p, row_pixels, font->char_width,
187 font->char_height);
Luke Song846012f2017-09-13 15:56:16 -0700188
189 x += font->char_width;
190 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800191}
192
Tao Bao92bdb5a2018-10-21 12:12:37 -0700193void gr_texticon(int x, int y, const GRSurface* icon) {
Tao Bao9f426332018-06-13 10:39:44 -0700194 if (icon == nullptr) return;
Doug Zongker16f97c32014-03-06 16:16:05 -0800195
Luke Song846012f2017-09-13 15:56:16 -0700196 if (icon->pixel_bytes != 1) {
197 printf("gr_texticon: source has wrong format\n");
198 return;
199 }
Doug Zongker02ec6b82012-08-22 17:26:40 -0700200
Luke Song846012f2017-09-13 15:56:16 -0700201 x += overscan_offset_x;
202 y += overscan_offset_y;
Doug Zongkerc560a672012-12-18 16:31:27 -0800203
Luke Song846012f2017-09-13 15:56:16 -0700204 if (outside(x, y) || outside(x + icon->width - 1, y + icon->height - 1)) return;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700205
Luke Song846012f2017-09-13 15:56:16 -0700206 int row_pixels = gr_draw->row_bytes / gr_draw->pixel_bytes;
Tao Bao92bdb5a2018-10-21 12:12:37 -0700207 const uint8_t* src_p = icon->data();
208 uint32_t* dst_p = PixelAt(gr_draw, x, y, row_pixels);
209 TextBlend(src_p, icon->row_bytes, dst_p, row_pixels, icon->width, icon->height);
Doug Zongker16f97c32014-03-06 16:16:05 -0800210}
211
Luke Song846012f2017-09-13 15:56:16 -0700212void gr_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a) {
213 uint32_t r32 = r, g32 = g, b32 = b, a32 = a;
Adrian Salidocc7b7eb2019-12-12 20:18:09 -0800214 if (pixel_format == PixelFormat::ARGB || pixel_format == PixelFormat::BGRA) {
Tao Baoed876a72018-07-31 21:32:50 -0700215 gr_current = (a32 << 24) | (r32 << 16) | (g32 << 8) | b32;
216 } else {
217 gr_current = (a32 << 24) | (b32 << 16) | (g32 << 8) | r32;
218 }
Doug Zongker16f97c32014-03-06 16:16:05 -0800219}
220
Luke Song846012f2017-09-13 15:56:16 -0700221void gr_clear() {
222 if ((gr_current & 0xff) == ((gr_current >> 8) & 0xff) &&
223 (gr_current & 0xff) == ((gr_current >> 16) & 0xff) &&
224 (gr_current & 0xff) == ((gr_current >> 24) & 0xff) &&
225 gr_draw->row_bytes == gr_draw->width * gr_draw->pixel_bytes) {
Tao Bao92bdb5a2018-10-21 12:12:37 -0700226 memset(gr_draw->data(), gr_current & 0xff, gr_draw->height * gr_draw->row_bytes);
Luke Song846012f2017-09-13 15:56:16 -0700227 } else {
Tao Bao92bdb5a2018-10-21 12:12:37 -0700228 uint32_t* px = reinterpret_cast<uint32_t*>(gr_draw->data());
Luke Song846012f2017-09-13 15:56:16 -0700229 int row_diff = gr_draw->row_bytes / gr_draw->pixel_bytes - gr_draw->width;
230 for (int y = 0; y < gr_draw->height; ++y) {
231 for (int x = 0; x < gr_draw->width; ++x) {
232 *px++ = gr_current;
233 }
234 px += row_diff;
Doug Zongker16f97c32014-03-06 16:16:05 -0800235 }
Luke Song846012f2017-09-13 15:56:16 -0700236 }
Doug Zongker02ec6b82012-08-22 17:26:40 -0700237}
238
Luke Song846012f2017-09-13 15:56:16 -0700239void gr_fill(int x1, int y1, int x2, int y2) {
240 x1 += overscan_offset_x;
241 y1 += overscan_offset_y;
Doug Zongkerc560a672012-12-18 16:31:27 -0800242
Luke Song846012f2017-09-13 15:56:16 -0700243 x2 += overscan_offset_x;
244 y2 += overscan_offset_y;
Doug Zongkerc560a672012-12-18 16:31:27 -0800245
Luke Song846012f2017-09-13 15:56:16 -0700246 if (outside(x1, y1) || outside(x2 - 1, y2 - 1)) return;
Doug Zongker16f97c32014-03-06 16:16:05 -0800247
Luke Song846012f2017-09-13 15:56:16 -0700248 int row_pixels = gr_draw->row_bytes / gr_draw->pixel_bytes;
Tao Bao92bdb5a2018-10-21 12:12:37 -0700249 uint32_t* p = PixelAt(gr_draw, x1, y1, row_pixels);
Luke Song846012f2017-09-13 15:56:16 -0700250 uint8_t alpha = static_cast<uint8_t>(((gr_current & alpha_mask) >> 24));
251 if (alpha > 0) {
252 for (int y = y1; y < y2; ++y) {
253 uint32_t* px = p;
254 for (int x = x1; x < x2; ++x) {
255 *px = pixel_blend(alpha, *px);
256 incr_x(&px, row_pixels);
257 }
258 incr_y(&p, row_pixels);
Doug Zongker16f97c32014-03-06 16:16:05 -0800259 }
Luke Song846012f2017-09-13 15:56:16 -0700260 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800261}
262
Tao Bao92bdb5a2018-10-21 12:12:37 -0700263void gr_blit(const GRSurface* source, int sx, int sy, int w, int h, int dx, int dy) {
Tao Bao9f426332018-06-13 10:39:44 -0700264 if (source == nullptr) return;
Doug Zongker16f97c32014-03-06 16:16:05 -0800265
Luke Song846012f2017-09-13 15:56:16 -0700266 if (gr_draw->pixel_bytes != source->pixel_bytes) {
267 printf("gr_blit: source has wrong format\n");
268 return;
269 }
270
271 dx += overscan_offset_x;
272 dy += overscan_offset_y;
273
274 if (outside(dx, dy) || outside(dx + w - 1, dy + h - 1)) return;
275
Tao Bao44478df2018-07-31 22:01:03 -0700276 if (rotation != GRRotation::NONE) {
Luke Song846012f2017-09-13 15:56:16 -0700277 int src_row_pixels = source->row_bytes / source->pixel_bytes;
278 int row_pixels = gr_draw->row_bytes / gr_draw->pixel_bytes;
Tao Bao92bdb5a2018-10-21 12:12:37 -0700279 const uint32_t* src_py =
280 reinterpret_cast<const uint32_t*>(source->data()) + sy * source->row_bytes / 4 + sx;
281 uint32_t* dst_py = PixelAt(gr_draw, dx, dy, row_pixels);
Luke Song846012f2017-09-13 15:56:16 -0700282
283 for (int y = 0; y < h; y += 1) {
Tao Bao92bdb5a2018-10-21 12:12:37 -0700284 const uint32_t* src_px = src_py;
Luke Song846012f2017-09-13 15:56:16 -0700285 uint32_t* dst_px = dst_py;
286 for (int x = 0; x < w; x += 1) {
287 *dst_px = *src_px++;
288 incr_x(&dst_px, row_pixels);
289 }
290 src_py += src_row_pixels;
291 incr_y(&dst_py, row_pixels);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800292 }
Luke Song846012f2017-09-13 15:56:16 -0700293 } else {
Tao Bao92bdb5a2018-10-21 12:12:37 -0700294 const uint8_t* src_p = source->data() + sy * source->row_bytes + sx * source->pixel_bytes;
295 uint8_t* dst_p = gr_draw->data() + dy * gr_draw->row_bytes + dx * gr_draw->pixel_bytes;
Doug Zongker16f97c32014-03-06 16:16:05 -0800296
Tao Bao9f426332018-06-13 10:39:44 -0700297 for (int i = 0; i < h; ++i) {
Luke Song846012f2017-09-13 15:56:16 -0700298 memcpy(dst_p, src_p, w * source->pixel_bytes);
299 src_p += source->row_bytes;
300 dst_p += gr_draw->row_bytes;
Doug Zongker16f97c32014-03-06 16:16:05 -0800301 }
Luke Song846012f2017-09-13 15:56:16 -0700302 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800303}
304
Tao Bao9f426332018-06-13 10:39:44 -0700305unsigned int gr_get_width(const GRSurface* surface) {
306 if (surface == nullptr) {
Luke Song846012f2017-09-13 15:56:16 -0700307 return 0;
308 }
309 return surface->width;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800310}
311
Tao Bao9f426332018-06-13 10:39:44 -0700312unsigned int gr_get_height(const GRSurface* surface) {
313 if (surface == nullptr) {
Luke Song846012f2017-09-13 15:56:16 -0700314 return 0;
315 }
316 return surface->height;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800317}
318
Damien Bargiacchid00f5eb2016-09-09 07:14:08 -0700319int gr_init_font(const char* name, GRFont** dest) {
Luke Song846012f2017-09-13 15:56:16 -0700320 GRFont* font = static_cast<GRFont*>(calloc(1, sizeof(*gr_font)));
321 if (font == nullptr) {
322 return -1;
323 }
Damien Bargiacchid00f5eb2016-09-09 07:14:08 -0700324
Luke Song846012f2017-09-13 15:56:16 -0700325 int res = res_create_alpha_surface(name, &(font->texture));
326 if (res < 0) {
327 free(font);
328 return res;
329 }
Damien Bargiacchi35fff612016-08-11 15:57:03 -0700330
Luke Song846012f2017-09-13 15:56:16 -0700331 // The font image should be a 96x2 array of character images. The
332 // columns are the printable ASCII characters 0x20 - 0x7f. The
333 // top row is regular text; the bottom row is bold.
334 font->char_width = font->texture->width / 96;
335 font->char_height = font->texture->height / 2;
Damien Bargiacchid00f5eb2016-09-09 07:14:08 -0700336
Luke Song846012f2017-09-13 15:56:16 -0700337 *dest = font;
Damien Bargiacchi35fff612016-08-11 15:57:03 -0700338
Luke Song846012f2017-09-13 15:56:16 -0700339 return 0;
Damien Bargiacchi35fff612016-08-11 15:57:03 -0700340}
341
Doug Zongker5290f202014-03-11 13:22:04 -0700342void gr_flip() {
Tao Bao557fa1f2017-02-07 12:51:00 -0800343 gr_draw = gr_backend->Flip();
Doug Zongker5290f202014-03-11 13:22:04 -0700344}
345
Chihhang Chuang6f7362a2021-09-24 00:11:51 +0800346std::unique_ptr<MinuiBackend> create_backend(GraphicsBackend backend) {
347 switch (backend) {
348 case GraphicsBackend::DRM:
349 return std::make_unique<MinuiBackendDrm>();
350 case GraphicsBackend::FBDEV:
351 return std::make_unique<MinuiBackendFbdev>();
352 default:
353 return nullptr;
354 }
355}
356
Tao Bao557fa1f2017-02-07 12:51:00 -0800357int gr_init() {
Chihhang Chuang6f7362a2021-09-24 00:11:51 +0800358 return gr_init(default_backends);
359}
360
361int gr_init(std::initializer_list<GraphicsBackend> backends) {
Tao Baoed876a72018-07-31 21:32:50 -0700362 // pixel_format needs to be set before loading any resources or initializing backends.
Tao Bao050feb02018-09-05 21:45:42 -0700363 std::string format = android::base::GetProperty("ro.minui.pixel_format", "");
Tao Baoed876a72018-07-31 21:32:50 -0700364 if (format == "ABGR_8888") {
365 pixel_format = PixelFormat::ABGR;
366 } else if (format == "RGBX_8888") {
367 pixel_format = PixelFormat::RGBX;
Adrian Salidocc7b7eb2019-12-12 20:18:09 -0800368 } else if (format == "ARGB_8888") {
369 pixel_format = PixelFormat::ARGB;
Tao Baoed876a72018-07-31 21:32:50 -0700370 } else if (format == "BGRA_8888") {
371 pixel_format = PixelFormat::BGRA;
372 } else {
373 pixel_format = PixelFormat::UNKNOWN;
374 }
375
Tianjie Xu55a2c4e2018-03-29 11:07:50 -0700376 int ret = gr_init_font("font", &gr_font);
377 if (ret != 0) {
Tianjie Xu842f2a32018-05-31 18:16:28 -0700378 printf("Failed to init font: %d, continuing graphic backend initialization without font file\n",
379 ret);
Tianjie Xu55a2c4e2018-03-29 11:07:50 -0700380 }
Doug Zongker16f97c32014-03-06 16:16:05 -0800381
Chihhang Chuang6f7362a2021-09-24 00:11:51 +0800382 std::unique_ptr<MinuiBackend> minui_backend;
383 for (GraphicsBackend backend : backends) {
384 minui_backend = create_backend(backend);
385 if (!minui_backend) {
386 printf("gr_init: minui_backend %d is a nullptr\n", backend);
387 continue;
388 }
389 gr_draw = minui_backend->Init();
390 if (gr_draw) break;
Tao Bao557fa1f2017-02-07 12:51:00 -0800391 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800392
Tao Bao557fa1f2017-02-07 12:51:00 -0800393 if (!gr_draw) {
394 return -1;
395 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800396
Chihhang Chuang6f7362a2021-09-24 00:11:51 +0800397 gr_backend = minui_backend.release();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800398
Tao Bao050feb02018-09-05 21:45:42 -0700399 int overscan_percent = android::base::GetIntProperty("ro.minui.overscan_percent", 0);
Tao Bao557fa1f2017-02-07 12:51:00 -0800400 overscan_offset_x = gr_draw->width * overscan_percent / 100;
401 overscan_offset_y = gr_draw->height * overscan_percent / 100;
402
403 gr_flip();
404 gr_flip();
Tianjie Xuccf00a22018-06-05 17:10:23 -0700405 if (!gr_draw) {
406 printf("gr_init: gr_draw becomes nullptr after gr_flip\n");
407 return -1;
408 }
Tao Bao557fa1f2017-02-07 12:51:00 -0800409
Tao Baoed876a72018-07-31 21:32:50 -0700410 std::string rotation_str =
Tao Bao050feb02018-09-05 21:45:42 -0700411 android::base::GetProperty("ro.minui.default_rotation", "ROTATION_NONE");
Tao Bao44478df2018-07-31 22:01:03 -0700412 if (rotation_str == "ROTATION_RIGHT") {
413 gr_rotate(GRRotation::RIGHT);
414 } else if (rotation_str == "ROTATION_DOWN") {
415 gr_rotate(GRRotation::DOWN);
416 } else if (rotation_str == "ROTATION_LEFT") {
417 gr_rotate(GRRotation::LEFT);
Tao Baoed876a72018-07-31 21:32:50 -0700418 } else { // "ROTATION_NONE" or unknown string
Tao Bao44478df2018-07-31 22:01:03 -0700419 gr_rotate(GRRotation::NONE);
420 }
Luke Song846012f2017-09-13 15:56:16 -0700421
422 if (gr_draw->pixel_bytes != 4) {
423 printf("gr_init: Only 4-byte pixel formats supported\n");
424 }
425
Tao Bao557fa1f2017-02-07 12:51:00 -0800426 return 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800427}
428
Tao Bao557fa1f2017-02-07 12:51:00 -0800429void gr_exit() {
430 delete gr_backend;
Tao Bao9f426332018-06-13 10:39:44 -0700431 gr_backend = nullptr;
432
433 delete gr_font;
434 gr_font = nullptr;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800435}
436
Tao Bao557fa1f2017-02-07 12:51:00 -0800437int gr_fb_width() {
Tao Bao44478df2018-07-31 22:01:03 -0700438 return (rotation == GRRotation::LEFT || rotation == GRRotation::RIGHT)
439 ? gr_draw->height - 2 * overscan_offset_y
440 : gr_draw->width - 2 * overscan_offset_x;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800441}
442
Tao Bao557fa1f2017-02-07 12:51:00 -0800443int gr_fb_height() {
Tao Bao44478df2018-07-31 22:01:03 -0700444 return (rotation == GRRotation::LEFT || rotation == GRRotation::RIGHT)
445 ? gr_draw->width - 2 * overscan_offset_x
446 : gr_draw->height - 2 * overscan_offset_y;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800447}
Dima Zavin4daf48a2011-08-30 11:59:20 -0700448
Tao Bao557fa1f2017-02-07 12:51:00 -0800449void gr_fb_blank(bool blank) {
450 gr_backend->Blank(blank);
Dima Zavin4daf48a2011-08-30 11:59:20 -0700451}
Luke Song846012f2017-09-13 15:56:16 -0700452
453void gr_rotate(GRRotation rot) {
454 rotation = rot;
455}