blob: 4d1f9b2d2c3eac5bcf0f33b99674264b3d106101 [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_adf.h"
29#include "graphics_drm.h"
30#include "graphics_fbdev.h"
Tao Bao0ecbd762017-01-16 21:16:58 -080031#include "minui/minui.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080032
Tao Bao9f426332018-06-13 10:39:44 -070033static GRFont* gr_font = 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;
40static constexpr uint32_t alpha_mask = 0xff000000;
Doug Zongker16f97c32014-03-06 16:16:05 -080041
Tao Bao9f426332018-06-13 10:39:44 -070042// gr_draw is owned by backends.
Tao Bao92bdb5a2018-10-21 12:12:37 -070043static GRSurface* gr_draw = nullptr;
Tao Bao44478df2018-07-31 22:01:03 -070044static GRRotation rotation = GRRotation::NONE;
Tao Baoed876a72018-07-31 21:32:50 -070045static PixelFormat pixel_format = PixelFormat::UNKNOWN;
Doug Zongker16f97c32014-03-06 16:16:05 -080046
Luke Song846012f2017-09-13 15:56:16 -070047static bool outside(int x, int y) {
Tao Bao44478df2018-07-31 22:01:03 -070048 auto swapped = (rotation == GRRotation::LEFT || rotation == GRRotation::RIGHT);
49 return x < 0 || x >= (swapped ? gr_draw->height : gr_draw->width) || y < 0 ||
50 y >= (swapped ? gr_draw->width : gr_draw->height);
Doug Zongker16f97c32014-03-06 16:16:05 -080051}
52
Luke Song846012f2017-09-13 15:56:16 -070053const GRFont* gr_sys_font() {
54 return gr_font;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080055}
56
Tao Baoed876a72018-07-31 21:32:50 -070057PixelFormat gr_pixel_format() {
58 return pixel_format;
59}
60
Luke Song846012f2017-09-13 15:56:16 -070061int gr_measure(const GRFont* font, const char* s) {
Tianjie Xu842f2a32018-05-31 18:16:28 -070062 if (font == nullptr) {
63 return -1;
64 }
65
Luke Song846012f2017-09-13 15:56:16 -070066 return font->char_width * strlen(s);
Damien Bargiacchi35fff612016-08-11 15:57:03 -070067}
68
Tianjie Xu842f2a32018-05-31 18:16:28 -070069int gr_font_size(const GRFont* font, int* x, int* y) {
70 if (font == nullptr) {
71 return -1;
72 }
73
Luke Song846012f2017-09-13 15:56:16 -070074 *x = font->char_width;
75 *y = font->char_height;
Tianjie Xu842f2a32018-05-31 18:16:28 -070076 return 0;
Dima Zavin3c7f00e2011-08-30 11:58:24 -070077}
78
Luke Song846012f2017-09-13 15:56:16 -070079// Blends gr_current onto pix value, assumes alpha as most significant byte.
80static inline uint32_t pixel_blend(uint8_t alpha, uint32_t pix) {
81 if (alpha == 255) return gr_current;
82 if (alpha == 0) return pix;
83 uint32_t pix_r = pix & 0xff;
84 uint32_t pix_g = pix & 0xff00;
85 uint32_t pix_b = pix & 0xff0000;
86 uint32_t cur_r = gr_current & 0xff;
87 uint32_t cur_g = gr_current & 0xff00;
88 uint32_t cur_b = gr_current & 0xff0000;
89
90 uint32_t out_r = (pix_r * (255 - alpha) + cur_r * alpha) / 255;
91 uint32_t out_g = (pix_g * (255 - alpha) + cur_g * alpha) / 255;
92 uint32_t out_b = (pix_b * (255 - alpha) + cur_b * alpha) / 255;
93
94 return (out_r & 0xff) | (out_g & 0xff00) | (out_b & 0xff0000) | (gr_current & 0xff000000);
95}
96
Tao Bao9f426332018-06-13 10:39:44 -070097// Increments pixel pointer right, with current rotation.
Luke Song846012f2017-09-13 15:56:16 -070098static void incr_x(uint32_t** p, int row_pixels) {
Tao Bao44478df2018-07-31 22:01:03 -070099 if (rotation == GRRotation::LEFT) {
100 *p = *p - row_pixels;
101 } else if (rotation == GRRotation::RIGHT) {
102 *p = *p + row_pixels;
103 } else if (rotation == GRRotation::DOWN) {
104 *p = *p - 1;
105 } else { // GRRotation::NONE
106 *p = *p + 1;
Luke Song846012f2017-09-13 15:56:16 -0700107 }
108}
109
Tao Bao9f426332018-06-13 10:39:44 -0700110// Increments pixel pointer down, with current rotation.
Luke Song846012f2017-09-13 15:56:16 -0700111static void incr_y(uint32_t** p, int row_pixels) {
Tao Bao44478df2018-07-31 22:01:03 -0700112 if (rotation == GRRotation::LEFT) {
113 *p = *p + 1;
114 } else if (rotation == GRRotation::RIGHT) {
115 *p = *p - 1;
116 } else if (rotation == GRRotation::DOWN) {
117 *p = *p - row_pixels;
118 } else { // GRRotation::NONE
119 *p = *p + row_pixels;
Luke Song846012f2017-09-13 15:56:16 -0700120 }
121}
122
Tao Bao9f426332018-06-13 10:39:44 -0700123// Returns pixel pointer at given coordinates with rotation adjustment.
Tao Bao92bdb5a2018-10-21 12:12:37 -0700124static uint32_t* PixelAt(GRSurface* surface, int x, int y, int row_pixels) {
Luke Song846012f2017-09-13 15:56:16 -0700125 switch (rotation) {
Tao Bao44478df2018-07-31 22:01:03 -0700126 case GRRotation::NONE:
Tao Bao92bdb5a2018-10-21 12:12:37 -0700127 return reinterpret_cast<uint32_t*>(surface->data()) + y * row_pixels + x;
Tao Bao44478df2018-07-31 22:01:03 -0700128 case GRRotation::RIGHT:
Tao Bao92bdb5a2018-10-21 12:12:37 -0700129 return reinterpret_cast<uint32_t*>(surface->data()) + x * row_pixels + (surface->width - y);
Tao Bao44478df2018-07-31 22:01:03 -0700130 case GRRotation::DOWN:
Tao Bao92bdb5a2018-10-21 12:12:37 -0700131 return reinterpret_cast<uint32_t*>(surface->data()) + (surface->height - 1 - y) * row_pixels +
132 (surface->width - 1 - x);
Tao Bao44478df2018-07-31 22:01:03 -0700133 case GRRotation::LEFT:
Tao Bao92bdb5a2018-10-21 12:12:37 -0700134 return reinterpret_cast<uint32_t*>(surface->data()) + (surface->height - 1 - x) * row_pixels +
135 y;
Luke Song846012f2017-09-13 15:56:16 -0700136 default:
Tao Bao44478df2018-07-31 22:01:03 -0700137 printf("invalid rotation %d", static_cast<int>(rotation));
Luke Song846012f2017-09-13 15:56:16 -0700138 }
139 return nullptr;
140}
141
Tao Bao92bdb5a2018-10-21 12:12:37 -0700142static void TextBlend(const uint8_t* src_p, int src_row_bytes, uint32_t* dst_p, int dst_row_pixels,
143 int width, int height) {
Luke Song846012f2017-09-13 15:56:16 -0700144 uint8_t alpha_current = static_cast<uint8_t>((alpha_mask & gr_current) >> 24);
145 for (int j = 0; j < height; ++j) {
Tao Bao92bdb5a2018-10-21 12:12:37 -0700146 const uint8_t* sx = src_p;
Luke Song846012f2017-09-13 15:56:16 -0700147 uint32_t* px = dst_p;
148 for (int i = 0; i < width; ++i, incr_x(&px, dst_row_pixels)) {
149 uint8_t a = *sx++;
150 if (alpha_current < 255) a = (static_cast<uint32_t>(a) * alpha_current) / 255;
151 *px = pixel_blend(a, *px);
Doug Zongker16f97c32014-03-06 16:16:05 -0800152 }
Luke Song846012f2017-09-13 15:56:16 -0700153 src_p += src_row_bytes;
154 incr_y(&dst_p, dst_row_pixels);
155 }
Doug Zongker16f97c32014-03-06 16:16:05 -0800156}
157
Luke Song846012f2017-09-13 15:56:16 -0700158void gr_text(const GRFont* font, int x, int y, const char* s, bool bold) {
159 if (!font || !font->texture || (gr_current & alpha_mask) == 0) return;
Doug Zongker55a36ac2013-03-04 15:49:02 -0800160
Luke Song846012f2017-09-13 15:56:16 -0700161 if (font->texture->pixel_bytes != 1) {
162 printf("gr_text: font has wrong format\n");
163 return;
164 }
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800165
Luke Song846012f2017-09-13 15:56:16 -0700166 bold = bold && (font->texture->height != font->char_height);
Doug Zongkerc560a672012-12-18 16:31:27 -0800167
Luke Song846012f2017-09-13 15:56:16 -0700168 x += overscan_offset_x;
169 y += overscan_offset_y;
Doug Zongker16f97c32014-03-06 16:16:05 -0800170
Luke Song846012f2017-09-13 15:56:16 -0700171 unsigned char ch;
172 while ((ch = *s++)) {
173 if (outside(x, y) || outside(x + font->char_width - 1, y + font->char_height - 1)) break;
Elliott Hughes01a4d082015-03-24 15:21:48 -0700174
Luke Song846012f2017-09-13 15:56:16 -0700175 if (ch < ' ' || ch > '~') {
176 ch = '?';
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800177 }
Luke Song846012f2017-09-13 15:56:16 -0700178
179 int row_pixels = gr_draw->row_bytes / gr_draw->pixel_bytes;
Tao Bao92bdb5a2018-10-21 12:12:37 -0700180 const uint8_t* src_p = font->texture->data() + ((ch - ' ') * font->char_width) +
181 (bold ? font->char_height * font->texture->row_bytes : 0);
182 uint32_t* dst_p = PixelAt(gr_draw, x, y, row_pixels);
Luke Song846012f2017-09-13 15:56:16 -0700183
Tao Bao92bdb5a2018-10-21 12:12:37 -0700184 TextBlend(src_p, font->texture->row_bytes, dst_p, row_pixels, font->char_width,
185 font->char_height);
Luke Song846012f2017-09-13 15:56:16 -0700186
187 x += font->char_width;
188 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800189}
190
Tao Bao92bdb5a2018-10-21 12:12:37 -0700191void gr_texticon(int x, int y, const GRSurface* icon) {
Tao Bao9f426332018-06-13 10:39:44 -0700192 if (icon == nullptr) return;
Doug Zongker16f97c32014-03-06 16:16:05 -0800193
Luke Song846012f2017-09-13 15:56:16 -0700194 if (icon->pixel_bytes != 1) {
195 printf("gr_texticon: source has wrong format\n");
196 return;
197 }
Doug Zongker02ec6b82012-08-22 17:26:40 -0700198
Luke Song846012f2017-09-13 15:56:16 -0700199 x += overscan_offset_x;
200 y += overscan_offset_y;
Doug Zongkerc560a672012-12-18 16:31:27 -0800201
Luke Song846012f2017-09-13 15:56:16 -0700202 if (outside(x, y) || outside(x + icon->width - 1, y + icon->height - 1)) return;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700203
Luke Song846012f2017-09-13 15:56:16 -0700204 int row_pixels = gr_draw->row_bytes / gr_draw->pixel_bytes;
Tao Bao92bdb5a2018-10-21 12:12:37 -0700205 const uint8_t* src_p = icon->data();
206 uint32_t* dst_p = PixelAt(gr_draw, x, y, row_pixels);
207 TextBlend(src_p, icon->row_bytes, dst_p, row_pixels, icon->width, icon->height);
Doug Zongker16f97c32014-03-06 16:16:05 -0800208}
209
Luke Song846012f2017-09-13 15:56:16 -0700210void gr_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a) {
211 uint32_t r32 = r, g32 = g, b32 = b, a32 = a;
Tao Baoed876a72018-07-31 21:32:50 -0700212 if (pixel_format == PixelFormat::ABGR || pixel_format == PixelFormat::BGRA) {
213 gr_current = (a32 << 24) | (r32 << 16) | (g32 << 8) | b32;
214 } else {
215 gr_current = (a32 << 24) | (b32 << 16) | (g32 << 8) | r32;
216 }
Doug Zongker16f97c32014-03-06 16:16:05 -0800217}
218
Luke Song846012f2017-09-13 15:56:16 -0700219void gr_clear() {
220 if ((gr_current & 0xff) == ((gr_current >> 8) & 0xff) &&
221 (gr_current & 0xff) == ((gr_current >> 16) & 0xff) &&
222 (gr_current & 0xff) == ((gr_current >> 24) & 0xff) &&
223 gr_draw->row_bytes == gr_draw->width * gr_draw->pixel_bytes) {
Tao Bao92bdb5a2018-10-21 12:12:37 -0700224 memset(gr_draw->data(), gr_current & 0xff, gr_draw->height * gr_draw->row_bytes);
Luke Song846012f2017-09-13 15:56:16 -0700225 } else {
Tao Bao92bdb5a2018-10-21 12:12:37 -0700226 uint32_t* px = reinterpret_cast<uint32_t*>(gr_draw->data());
Luke Song846012f2017-09-13 15:56:16 -0700227 int row_diff = gr_draw->row_bytes / gr_draw->pixel_bytes - gr_draw->width;
228 for (int y = 0; y < gr_draw->height; ++y) {
229 for (int x = 0; x < gr_draw->width; ++x) {
230 *px++ = gr_current;
231 }
232 px += row_diff;
Doug Zongker16f97c32014-03-06 16:16:05 -0800233 }
Luke Song846012f2017-09-13 15:56:16 -0700234 }
Doug Zongker02ec6b82012-08-22 17:26:40 -0700235}
236
Luke Song846012f2017-09-13 15:56:16 -0700237void gr_fill(int x1, int y1, int x2, int y2) {
238 x1 += overscan_offset_x;
239 y1 += overscan_offset_y;
Doug Zongkerc560a672012-12-18 16:31:27 -0800240
Luke Song846012f2017-09-13 15:56:16 -0700241 x2 += overscan_offset_x;
242 y2 += overscan_offset_y;
Doug Zongkerc560a672012-12-18 16:31:27 -0800243
Luke Song846012f2017-09-13 15:56:16 -0700244 if (outside(x1, y1) || outside(x2 - 1, y2 - 1)) return;
Doug Zongker16f97c32014-03-06 16:16:05 -0800245
Luke Song846012f2017-09-13 15:56:16 -0700246 int row_pixels = gr_draw->row_bytes / gr_draw->pixel_bytes;
Tao Bao92bdb5a2018-10-21 12:12:37 -0700247 uint32_t* p = PixelAt(gr_draw, x1, y1, row_pixels);
Luke Song846012f2017-09-13 15:56:16 -0700248 uint8_t alpha = static_cast<uint8_t>(((gr_current & alpha_mask) >> 24));
249 if (alpha > 0) {
250 for (int y = y1; y < y2; ++y) {
251 uint32_t* px = p;
252 for (int x = x1; x < x2; ++x) {
253 *px = pixel_blend(alpha, *px);
254 incr_x(&px, row_pixels);
255 }
256 incr_y(&p, row_pixels);
Doug Zongker16f97c32014-03-06 16:16:05 -0800257 }
Luke Song846012f2017-09-13 15:56:16 -0700258 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800259}
260
Tao Bao92bdb5a2018-10-21 12:12:37 -0700261void gr_blit(const GRSurface* source, int sx, int sy, int w, int h, int dx, int dy) {
Tao Bao9f426332018-06-13 10:39:44 -0700262 if (source == nullptr) return;
Doug Zongker16f97c32014-03-06 16:16:05 -0800263
Luke Song846012f2017-09-13 15:56:16 -0700264 if (gr_draw->pixel_bytes != source->pixel_bytes) {
265 printf("gr_blit: source has wrong format\n");
266 return;
267 }
268
269 dx += overscan_offset_x;
270 dy += overscan_offset_y;
271
272 if (outside(dx, dy) || outside(dx + w - 1, dy + h - 1)) return;
273
Tao Bao44478df2018-07-31 22:01:03 -0700274 if (rotation != GRRotation::NONE) {
Luke Song846012f2017-09-13 15:56:16 -0700275 int src_row_pixels = source->row_bytes / source->pixel_bytes;
276 int row_pixels = gr_draw->row_bytes / gr_draw->pixel_bytes;
Tao Bao92bdb5a2018-10-21 12:12:37 -0700277 const uint32_t* src_py =
278 reinterpret_cast<const uint32_t*>(source->data()) + sy * source->row_bytes / 4 + sx;
279 uint32_t* dst_py = PixelAt(gr_draw, dx, dy, row_pixels);
Luke Song846012f2017-09-13 15:56:16 -0700280
281 for (int y = 0; y < h; y += 1) {
Tao Bao92bdb5a2018-10-21 12:12:37 -0700282 const uint32_t* src_px = src_py;
Luke Song846012f2017-09-13 15:56:16 -0700283 uint32_t* dst_px = dst_py;
284 for (int x = 0; x < w; x += 1) {
285 *dst_px = *src_px++;
286 incr_x(&dst_px, row_pixels);
287 }
288 src_py += src_row_pixels;
289 incr_y(&dst_py, row_pixels);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800290 }
Luke Song846012f2017-09-13 15:56:16 -0700291 } else {
Tao Bao92bdb5a2018-10-21 12:12:37 -0700292 const uint8_t* src_p = source->data() + sy * source->row_bytes + sx * source->pixel_bytes;
293 uint8_t* dst_p = gr_draw->data() + dy * gr_draw->row_bytes + dx * gr_draw->pixel_bytes;
Doug Zongker16f97c32014-03-06 16:16:05 -0800294
Tao Bao9f426332018-06-13 10:39:44 -0700295 for (int i = 0; i < h; ++i) {
Luke Song846012f2017-09-13 15:56:16 -0700296 memcpy(dst_p, src_p, w * source->pixel_bytes);
297 src_p += source->row_bytes;
298 dst_p += gr_draw->row_bytes;
Doug Zongker16f97c32014-03-06 16:16:05 -0800299 }
Luke Song846012f2017-09-13 15:56:16 -0700300 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800301}
302
Tao Bao9f426332018-06-13 10:39:44 -0700303unsigned int gr_get_width(const GRSurface* surface) {
304 if (surface == nullptr) {
Luke Song846012f2017-09-13 15:56:16 -0700305 return 0;
306 }
307 return surface->width;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800308}
309
Tao Bao9f426332018-06-13 10:39:44 -0700310unsigned int gr_get_height(const GRSurface* surface) {
311 if (surface == nullptr) {
Luke Song846012f2017-09-13 15:56:16 -0700312 return 0;
313 }
314 return surface->height;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800315}
316
Damien Bargiacchid00f5eb2016-09-09 07:14:08 -0700317int gr_init_font(const char* name, GRFont** dest) {
Luke Song846012f2017-09-13 15:56:16 -0700318 GRFont* font = static_cast<GRFont*>(calloc(1, sizeof(*gr_font)));
319 if (font == nullptr) {
320 return -1;
321 }
Damien Bargiacchid00f5eb2016-09-09 07:14:08 -0700322
Luke Song846012f2017-09-13 15:56:16 -0700323 int res = res_create_alpha_surface(name, &(font->texture));
324 if (res < 0) {
325 free(font);
326 return res;
327 }
Damien Bargiacchi35fff612016-08-11 15:57:03 -0700328
Luke Song846012f2017-09-13 15:56:16 -0700329 // The font image should be a 96x2 array of character images. The
330 // columns are the printable ASCII characters 0x20 - 0x7f. The
331 // top row is regular text; the bottom row is bold.
332 font->char_width = font->texture->width / 96;
333 font->char_height = font->texture->height / 2;
Damien Bargiacchid00f5eb2016-09-09 07:14:08 -0700334
Luke Song846012f2017-09-13 15:56:16 -0700335 *dest = font;
Damien Bargiacchi35fff612016-08-11 15:57:03 -0700336
Luke Song846012f2017-09-13 15:56:16 -0700337 return 0;
Damien Bargiacchi35fff612016-08-11 15:57:03 -0700338}
339
Doug Zongker5290f202014-03-11 13:22:04 -0700340void gr_flip() {
Tao Bao557fa1f2017-02-07 12:51:00 -0800341 gr_draw = gr_backend->Flip();
Doug Zongker5290f202014-03-11 13:22:04 -0700342}
343
Tao Bao557fa1f2017-02-07 12:51:00 -0800344int gr_init() {
Tao Baoed876a72018-07-31 21:32:50 -0700345 // pixel_format needs to be set before loading any resources or initializing backends.
Tao Bao050feb02018-09-05 21:45:42 -0700346 std::string format = android::base::GetProperty("ro.minui.pixel_format", "");
Tao Baoed876a72018-07-31 21:32:50 -0700347 if (format == "ABGR_8888") {
348 pixel_format = PixelFormat::ABGR;
349 } else if (format == "RGBX_8888") {
350 pixel_format = PixelFormat::RGBX;
351 } else if (format == "BGRA_8888") {
352 pixel_format = PixelFormat::BGRA;
353 } else {
354 pixel_format = PixelFormat::UNKNOWN;
355 }
356
Tianjie Xu55a2c4e2018-03-29 11:07:50 -0700357 int ret = gr_init_font("font", &gr_font);
358 if (ret != 0) {
Tianjie Xu842f2a32018-05-31 18:16:28 -0700359 printf("Failed to init font: %d, continuing graphic backend initialization without font file\n",
360 ret);
Tianjie Xu55a2c4e2018-03-29 11:07:50 -0700361 }
Doug Zongker16f97c32014-03-06 16:16:05 -0800362
Tao Bao557fa1f2017-02-07 12:51:00 -0800363 auto backend = std::unique_ptr<MinuiBackend>{ std::make_unique<MinuiBackendAdf>() };
364 gr_draw = backend->Init();
Greg Hackmann41909dd2014-04-25 10:39:50 -0700365
Tao Bao557fa1f2017-02-07 12:51:00 -0800366 if (!gr_draw) {
367 backend = std::make_unique<MinuiBackendDrm>();
368 gr_draw = backend->Init();
369 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700370
Tao Bao557fa1f2017-02-07 12:51:00 -0800371 if (!gr_draw) {
372 backend = std::make_unique<MinuiBackendFbdev>();
373 gr_draw = backend->Init();
374 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800375
Tao Bao557fa1f2017-02-07 12:51:00 -0800376 if (!gr_draw) {
377 return -1;
378 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800379
Tao Bao557fa1f2017-02-07 12:51:00 -0800380 gr_backend = backend.release();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800381
Tao Bao050feb02018-09-05 21:45:42 -0700382 int overscan_percent = android::base::GetIntProperty("ro.minui.overscan_percent", 0);
Tao Bao557fa1f2017-02-07 12:51:00 -0800383 overscan_offset_x = gr_draw->width * overscan_percent / 100;
384 overscan_offset_y = gr_draw->height * overscan_percent / 100;
385
386 gr_flip();
387 gr_flip();
Tianjie Xuccf00a22018-06-05 17:10:23 -0700388 if (!gr_draw) {
389 printf("gr_init: gr_draw becomes nullptr after gr_flip\n");
390 return -1;
391 }
Tao Bao557fa1f2017-02-07 12:51:00 -0800392
Tao Baoed876a72018-07-31 21:32:50 -0700393 std::string rotation_str =
Tao Bao050feb02018-09-05 21:45:42 -0700394 android::base::GetProperty("ro.minui.default_rotation", "ROTATION_NONE");
Tao Bao44478df2018-07-31 22:01:03 -0700395 if (rotation_str == "ROTATION_RIGHT") {
396 gr_rotate(GRRotation::RIGHT);
397 } else if (rotation_str == "ROTATION_DOWN") {
398 gr_rotate(GRRotation::DOWN);
399 } else if (rotation_str == "ROTATION_LEFT") {
400 gr_rotate(GRRotation::LEFT);
Tao Baoed876a72018-07-31 21:32:50 -0700401 } else { // "ROTATION_NONE" or unknown string
Tao Bao44478df2018-07-31 22:01:03 -0700402 gr_rotate(GRRotation::NONE);
403 }
Luke Song846012f2017-09-13 15:56:16 -0700404
405 if (gr_draw->pixel_bytes != 4) {
406 printf("gr_init: Only 4-byte pixel formats supported\n");
407 }
408
Tao Bao557fa1f2017-02-07 12:51:00 -0800409 return 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800410}
411
Tao Bao557fa1f2017-02-07 12:51:00 -0800412void gr_exit() {
413 delete gr_backend;
Tao Bao9f426332018-06-13 10:39:44 -0700414 gr_backend = nullptr;
415
416 delete gr_font;
417 gr_font = nullptr;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800418}
419
Tao Bao557fa1f2017-02-07 12:51:00 -0800420int gr_fb_width() {
Tao Bao44478df2018-07-31 22:01:03 -0700421 return (rotation == GRRotation::LEFT || rotation == GRRotation::RIGHT)
422 ? gr_draw->height - 2 * overscan_offset_y
423 : gr_draw->width - 2 * overscan_offset_x;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800424}
425
Tao Bao557fa1f2017-02-07 12:51:00 -0800426int gr_fb_height() {
Tao Bao44478df2018-07-31 22:01:03 -0700427 return (rotation == GRRotation::LEFT || rotation == GRRotation::RIGHT)
428 ? gr_draw->width - 2 * overscan_offset_x
429 : gr_draw->height - 2 * overscan_offset_y;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800430}
Dima Zavin4daf48a2011-08-30 11:59:20 -0700431
Tao Bao557fa1f2017-02-07 12:51:00 -0800432void gr_fb_blank(bool blank) {
433 gr_backend->Blank(blank);
Dima Zavin4daf48a2011-08-30 11:59:20 -0700434}
Luke Song846012f2017-09-13 15:56:16 -0700435
436void gr_rotate(GRRotation rot) {
437 rotation = rot;
438}