blob: cc02e9e827920df728a689c2b5bf31b01724b88c [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 Bao557fa1f2017-02-07 12:51:00 -080026#include "graphics_adf.h"
27#include "graphics_drm.h"
28#include "graphics_fbdev.h"
Tao Bao0ecbd762017-01-16 21:16:58 -080029#include "minui/minui.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080030
Tao Bao9f426332018-06-13 10:39:44 -070031static GRFont* gr_font = nullptr;
Tao Bao557fa1f2017-02-07 12:51:00 -080032static MinuiBackend* gr_backend = nullptr;
Doug Zongker16f97c32014-03-06 16:16:05 -080033
Doug Zongkerc560a672012-12-18 16:31:27 -080034static int overscan_percent = OVERSCAN_PERCENT;
35static 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.
42static const GRSurface* gr_draw = nullptr;
Luke Song846012f2017-09-13 15:56:16 -070043static GRRotation rotation = ROTATION_NONE;
Doug Zongker16f97c32014-03-06 16:16:05 -080044
Luke Song846012f2017-09-13 15:56:16 -070045static bool outside(int x, int y) {
46 return x < 0 || x >= (rotation % 2 ? gr_draw->height : gr_draw->width) || y < 0 ||
47 y >= (rotation % 2 ? gr_draw->width : gr_draw->height);
Doug Zongker16f97c32014-03-06 16:16:05 -080048}
49
Luke Song846012f2017-09-13 15:56:16 -070050const GRFont* gr_sys_font() {
51 return gr_font;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080052}
53
Luke Song846012f2017-09-13 15:56:16 -070054int gr_measure(const GRFont* font, const char* s) {
Tianjie Xu842f2a32018-05-31 18:16:28 -070055 if (font == nullptr) {
56 return -1;
57 }
58
Luke Song846012f2017-09-13 15:56:16 -070059 return font->char_width * strlen(s);
Damien Bargiacchi35fff612016-08-11 15:57:03 -070060}
61
Tianjie Xu842f2a32018-05-31 18:16:28 -070062int gr_font_size(const GRFont* font, int* x, int* y) {
63 if (font == nullptr) {
64 return -1;
65 }
66
Luke Song846012f2017-09-13 15:56:16 -070067 *x = font->char_width;
68 *y = font->char_height;
Tianjie Xu842f2a32018-05-31 18:16:28 -070069 return 0;
Dima Zavin3c7f00e2011-08-30 11:58:24 -070070}
71
Luke Song846012f2017-09-13 15:56:16 -070072// Blends gr_current onto pix value, assumes alpha as most significant byte.
73static inline uint32_t pixel_blend(uint8_t alpha, uint32_t pix) {
74 if (alpha == 255) return gr_current;
75 if (alpha == 0) return pix;
76 uint32_t pix_r = pix & 0xff;
77 uint32_t pix_g = pix & 0xff00;
78 uint32_t pix_b = pix & 0xff0000;
79 uint32_t cur_r = gr_current & 0xff;
80 uint32_t cur_g = gr_current & 0xff00;
81 uint32_t cur_b = gr_current & 0xff0000;
82
83 uint32_t out_r = (pix_r * (255 - alpha) + cur_r * alpha) / 255;
84 uint32_t out_g = (pix_g * (255 - alpha) + cur_g * alpha) / 255;
85 uint32_t out_b = (pix_b * (255 - alpha) + cur_b * alpha) / 255;
86
87 return (out_r & 0xff) | (out_g & 0xff00) | (out_b & 0xff0000) | (gr_current & 0xff000000);
88}
89
Tao Bao9f426332018-06-13 10:39:44 -070090// Increments pixel pointer right, with current rotation.
Luke Song846012f2017-09-13 15:56:16 -070091static void incr_x(uint32_t** p, int row_pixels) {
92 if (rotation % 2) {
93 *p = *p + (rotation == 1 ? 1 : -1) * row_pixels;
94 } else {
95 *p = *p + (rotation ? -1 : 1);
96 }
97}
98
Tao Bao9f426332018-06-13 10:39:44 -070099// Increments pixel pointer down, with current rotation.
Luke Song846012f2017-09-13 15:56:16 -0700100static void incr_y(uint32_t** p, int row_pixels) {
101 if (rotation % 2) {
102 *p = *p + (rotation == 1 ? -1 : 1);
103 } else {
104 *p = *p + (rotation ? -1 : 1) * row_pixels;
105 }
106}
107
Tao Bao9f426332018-06-13 10:39:44 -0700108// Returns pixel pointer at given coordinates with rotation adjustment.
109static uint32_t* pixel_at(const GRSurface* surf, int x, int y, int row_pixels) {
Luke Song846012f2017-09-13 15:56:16 -0700110 switch (rotation) {
111 case ROTATION_NONE:
112 return reinterpret_cast<uint32_t*>(surf->data) + y * row_pixels + x;
113 case ROTATION_RIGHT:
114 return reinterpret_cast<uint32_t*>(surf->data) + x * row_pixels + (surf->width - y);
115 case ROTATION_DOWN:
116 return reinterpret_cast<uint32_t*>(surf->data) + (surf->height - 1 - y) * row_pixels +
117 (surf->width - 1 - x);
118 case ROTATION_LEFT:
119 return reinterpret_cast<uint32_t*>(surf->data) + (surf->height - 1 - x) * row_pixels + y;
120 default:
121 printf("invalid rotation %d", rotation);
122 }
123 return nullptr;
124}
125
126static void text_blend(uint8_t* src_p, int src_row_bytes, uint32_t* dst_p, int dst_row_pixels,
127 int width, int height) {
128 uint8_t alpha_current = static_cast<uint8_t>((alpha_mask & gr_current) >> 24);
129 for (int j = 0; j < height; ++j) {
130 uint8_t* sx = src_p;
131 uint32_t* px = dst_p;
132 for (int i = 0; i < width; ++i, incr_x(&px, dst_row_pixels)) {
133 uint8_t a = *sx++;
134 if (alpha_current < 255) a = (static_cast<uint32_t>(a) * alpha_current) / 255;
135 *px = pixel_blend(a, *px);
Doug Zongker16f97c32014-03-06 16:16:05 -0800136 }
Luke Song846012f2017-09-13 15:56:16 -0700137 src_p += src_row_bytes;
138 incr_y(&dst_p, dst_row_pixels);
139 }
Doug Zongker16f97c32014-03-06 16:16:05 -0800140}
141
Luke Song846012f2017-09-13 15:56:16 -0700142void gr_text(const GRFont* font, int x, int y, const char* s, bool bold) {
143 if (!font || !font->texture || (gr_current & alpha_mask) == 0) return;
Doug Zongker55a36ac2013-03-04 15:49:02 -0800144
Luke Song846012f2017-09-13 15:56:16 -0700145 if (font->texture->pixel_bytes != 1) {
146 printf("gr_text: font has wrong format\n");
147 return;
148 }
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800149
Luke Song846012f2017-09-13 15:56:16 -0700150 bold = bold && (font->texture->height != font->char_height);
Doug Zongkerc560a672012-12-18 16:31:27 -0800151
Luke Song846012f2017-09-13 15:56:16 -0700152 x += overscan_offset_x;
153 y += overscan_offset_y;
Doug Zongker16f97c32014-03-06 16:16:05 -0800154
Luke Song846012f2017-09-13 15:56:16 -0700155 unsigned char ch;
156 while ((ch = *s++)) {
157 if (outside(x, y) || outside(x + font->char_width - 1, y + font->char_height - 1)) break;
Elliott Hughes01a4d082015-03-24 15:21:48 -0700158
Luke Song846012f2017-09-13 15:56:16 -0700159 if (ch < ' ' || ch > '~') {
160 ch = '?';
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800161 }
Luke Song846012f2017-09-13 15:56:16 -0700162
163 int row_pixels = gr_draw->row_bytes / gr_draw->pixel_bytes;
164 uint8_t* src_p = font->texture->data + ((ch - ' ') * font->char_width) +
165 (bold ? font->char_height * font->texture->row_bytes : 0);
166 uint32_t* dst_p = pixel_at(gr_draw, x, y, row_pixels);
167
168 text_blend(src_p, font->texture->row_bytes, dst_p, row_pixels, font->char_width,
169 font->char_height);
170
171 x += font->char_width;
172 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800173}
174
Doug Zongker16f97c32014-03-06 16:16:05 -0800175void gr_texticon(int x, int y, GRSurface* icon) {
Tao Bao9f426332018-06-13 10:39:44 -0700176 if (icon == nullptr) return;
Doug Zongker16f97c32014-03-06 16:16:05 -0800177
Luke Song846012f2017-09-13 15:56:16 -0700178 if (icon->pixel_bytes != 1) {
179 printf("gr_texticon: source has wrong format\n");
180 return;
181 }
Doug Zongker02ec6b82012-08-22 17:26:40 -0700182
Luke Song846012f2017-09-13 15:56:16 -0700183 x += overscan_offset_x;
184 y += overscan_offset_y;
Doug Zongkerc560a672012-12-18 16:31:27 -0800185
Luke Song846012f2017-09-13 15:56:16 -0700186 if (outside(x, y) || outside(x + icon->width - 1, y + icon->height - 1)) return;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700187
Luke Song846012f2017-09-13 15:56:16 -0700188 int row_pixels = gr_draw->row_bytes / gr_draw->pixel_bytes;
189 uint8_t* src_p = icon->data;
190 uint32_t* dst_p = pixel_at(gr_draw, x, y, row_pixels);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700191
Luke Song846012f2017-09-13 15:56:16 -0700192 text_blend(src_p, icon->row_bytes, dst_p, row_pixels, icon->width, icon->height);
Doug Zongker16f97c32014-03-06 16:16:05 -0800193}
194
Luke Song846012f2017-09-13 15:56:16 -0700195void gr_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a) {
196 uint32_t r32 = r, g32 = g, b32 = b, a32 = a;
Tony Kuofd778e32015-02-05 21:25:56 +0800197#if defined(RECOVERY_ABGR) || defined(RECOVERY_BGRA)
Luke Song846012f2017-09-13 15:56:16 -0700198 gr_current = (a32 << 24) | (r32 << 16) | (g32 << 8) | b32;
Tony Kuofd778e32015-02-05 21:25:56 +0800199#else
Luke Song846012f2017-09-13 15:56:16 -0700200 gr_current = (a32 << 24) | (b32 << 16) | (g32 << 8) | r32;
Tony Kuofd778e32015-02-05 21:25:56 +0800201#endif
Doug Zongker16f97c32014-03-06 16:16:05 -0800202}
203
Luke Song846012f2017-09-13 15:56:16 -0700204void gr_clear() {
205 if ((gr_current & 0xff) == ((gr_current >> 8) & 0xff) &&
206 (gr_current & 0xff) == ((gr_current >> 16) & 0xff) &&
207 (gr_current & 0xff) == ((gr_current >> 24) & 0xff) &&
208 gr_draw->row_bytes == gr_draw->width * gr_draw->pixel_bytes) {
209 memset(gr_draw->data, gr_current & 0xff, gr_draw->height * gr_draw->row_bytes);
210 } else {
211 uint32_t* px = reinterpret_cast<uint32_t*>(gr_draw->data);
212 int row_diff = gr_draw->row_bytes / gr_draw->pixel_bytes - gr_draw->width;
213 for (int y = 0; y < gr_draw->height; ++y) {
214 for (int x = 0; x < gr_draw->width; ++x) {
215 *px++ = gr_current;
216 }
217 px += row_diff;
Doug Zongker16f97c32014-03-06 16:16:05 -0800218 }
Luke Song846012f2017-09-13 15:56:16 -0700219 }
Doug Zongker02ec6b82012-08-22 17:26:40 -0700220}
221
Luke Song846012f2017-09-13 15:56:16 -0700222void gr_fill(int x1, int y1, int x2, int y2) {
223 x1 += overscan_offset_x;
224 y1 += overscan_offset_y;
Doug Zongkerc560a672012-12-18 16:31:27 -0800225
Luke Song846012f2017-09-13 15:56:16 -0700226 x2 += overscan_offset_x;
227 y2 += overscan_offset_y;
Doug Zongkerc560a672012-12-18 16:31:27 -0800228
Luke Song846012f2017-09-13 15:56:16 -0700229 if (outside(x1, y1) || outside(x2 - 1, y2 - 1)) return;
Doug Zongker16f97c32014-03-06 16:16:05 -0800230
Luke Song846012f2017-09-13 15:56:16 -0700231 int row_pixels = gr_draw->row_bytes / gr_draw->pixel_bytes;
232 uint32_t* p = pixel_at(gr_draw, x1, y1, row_pixels);
233 uint8_t alpha = static_cast<uint8_t>(((gr_current & alpha_mask) >> 24));
234 if (alpha > 0) {
235 for (int y = y1; y < y2; ++y) {
236 uint32_t* px = p;
237 for (int x = x1; x < x2; ++x) {
238 *px = pixel_blend(alpha, *px);
239 incr_x(&px, row_pixels);
240 }
241 incr_y(&p, row_pixels);
Doug Zongker16f97c32014-03-06 16:16:05 -0800242 }
Luke Song846012f2017-09-13 15:56:16 -0700243 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800244}
245
Doug Zongker16f97c32014-03-06 16:16:05 -0800246void gr_blit(GRSurface* source, int sx, int sy, int w, int h, int dx, int dy) {
Tao Bao9f426332018-06-13 10:39:44 -0700247 if (source == nullptr) return;
Doug Zongker16f97c32014-03-06 16:16:05 -0800248
Luke Song846012f2017-09-13 15:56:16 -0700249 if (gr_draw->pixel_bytes != source->pixel_bytes) {
250 printf("gr_blit: source has wrong format\n");
251 return;
252 }
253
254 dx += overscan_offset_x;
255 dy += overscan_offset_y;
256
257 if (outside(dx, dy) || outside(dx + w - 1, dy + h - 1)) return;
258
259 if (rotation) {
260 int src_row_pixels = source->row_bytes / source->pixel_bytes;
261 int row_pixels = gr_draw->row_bytes / gr_draw->pixel_bytes;
262 uint32_t* src_py = reinterpret_cast<uint32_t*>(source->data) + sy * source->row_bytes / 4 + sx;
263 uint32_t* dst_py = pixel_at(gr_draw, dx, dy, row_pixels);
264
265 for (int y = 0; y < h; y += 1) {
266 uint32_t* src_px = src_py;
267 uint32_t* dst_px = dst_py;
268 for (int x = 0; x < w; x += 1) {
269 *dst_px = *src_px++;
270 incr_x(&dst_px, row_pixels);
271 }
272 src_py += src_row_pixels;
273 incr_y(&dst_py, row_pixels);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800274 }
Luke Song846012f2017-09-13 15:56:16 -0700275 } else {
276 unsigned char* src_p = source->data + sy * source->row_bytes + sx * source->pixel_bytes;
277 unsigned char* dst_p = gr_draw->data + dy * gr_draw->row_bytes + dx * gr_draw->pixel_bytes;
Doug Zongker16f97c32014-03-06 16:16:05 -0800278
Tao Bao9f426332018-06-13 10:39:44 -0700279 for (int i = 0; i < h; ++i) {
Luke Song846012f2017-09-13 15:56:16 -0700280 memcpy(dst_p, src_p, w * source->pixel_bytes);
281 src_p += source->row_bytes;
282 dst_p += gr_draw->row_bytes;
Doug Zongker16f97c32014-03-06 16:16:05 -0800283 }
Luke Song846012f2017-09-13 15:56:16 -0700284 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800285}
286
Tao Bao9f426332018-06-13 10:39:44 -0700287unsigned int gr_get_width(const GRSurface* surface) {
288 if (surface == nullptr) {
Luke Song846012f2017-09-13 15:56:16 -0700289 return 0;
290 }
291 return surface->width;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800292}
293
Tao Bao9f426332018-06-13 10:39:44 -0700294unsigned int gr_get_height(const GRSurface* surface) {
295 if (surface == nullptr) {
Luke Song846012f2017-09-13 15:56:16 -0700296 return 0;
297 }
298 return surface->height;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800299}
300
Damien Bargiacchid00f5eb2016-09-09 07:14:08 -0700301int gr_init_font(const char* name, GRFont** dest) {
Luke Song846012f2017-09-13 15:56:16 -0700302 GRFont* font = static_cast<GRFont*>(calloc(1, sizeof(*gr_font)));
303 if (font == nullptr) {
304 return -1;
305 }
Damien Bargiacchid00f5eb2016-09-09 07:14:08 -0700306
Luke Song846012f2017-09-13 15:56:16 -0700307 int res = res_create_alpha_surface(name, &(font->texture));
308 if (res < 0) {
309 free(font);
310 return res;
311 }
Damien Bargiacchi35fff612016-08-11 15:57:03 -0700312
Luke Song846012f2017-09-13 15:56:16 -0700313 // The font image should be a 96x2 array of character images. The
314 // columns are the printable ASCII characters 0x20 - 0x7f. The
315 // top row is regular text; the bottom row is bold.
316 font->char_width = font->texture->width / 96;
317 font->char_height = font->texture->height / 2;
Damien Bargiacchid00f5eb2016-09-09 07:14:08 -0700318
Luke Song846012f2017-09-13 15:56:16 -0700319 *dest = font;
Damien Bargiacchi35fff612016-08-11 15:57:03 -0700320
Luke Song846012f2017-09-13 15:56:16 -0700321 return 0;
Damien Bargiacchi35fff612016-08-11 15:57:03 -0700322}
323
Doug Zongker5290f202014-03-11 13:22:04 -0700324void gr_flip() {
Tao Bao557fa1f2017-02-07 12:51:00 -0800325 gr_draw = gr_backend->Flip();
Doug Zongker5290f202014-03-11 13:22:04 -0700326}
327
Tao Bao557fa1f2017-02-07 12:51:00 -0800328int gr_init() {
Tianjie Xu55a2c4e2018-03-29 11:07:50 -0700329 int ret = gr_init_font("font", &gr_font);
330 if (ret != 0) {
Tianjie Xu842f2a32018-05-31 18:16:28 -0700331 printf("Failed to init font: %d, continuing graphic backend initialization without font file\n",
332 ret);
Tianjie Xu55a2c4e2018-03-29 11:07:50 -0700333 }
Doug Zongker16f97c32014-03-06 16:16:05 -0800334
Tao Bao557fa1f2017-02-07 12:51:00 -0800335 auto backend = std::unique_ptr<MinuiBackend>{ std::make_unique<MinuiBackendAdf>() };
336 gr_draw = backend->Init();
Greg Hackmann41909dd2014-04-25 10:39:50 -0700337
Tao Bao557fa1f2017-02-07 12:51:00 -0800338 if (!gr_draw) {
339 backend = std::make_unique<MinuiBackendDrm>();
340 gr_draw = backend->Init();
341 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700342
Tao Bao557fa1f2017-02-07 12:51:00 -0800343 if (!gr_draw) {
344 backend = std::make_unique<MinuiBackendFbdev>();
345 gr_draw = backend->Init();
346 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800347
Tao Bao557fa1f2017-02-07 12:51:00 -0800348 if (!gr_draw) {
349 return -1;
350 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800351
Tao Bao557fa1f2017-02-07 12:51:00 -0800352 gr_backend = backend.release();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800353
Tao Bao557fa1f2017-02-07 12:51:00 -0800354 overscan_offset_x = gr_draw->width * overscan_percent / 100;
355 overscan_offset_y = gr_draw->height * overscan_percent / 100;
356
357 gr_flip();
358 gr_flip();
Tianjie Xuccf00a22018-06-05 17:10:23 -0700359 if (!gr_draw) {
360 printf("gr_init: gr_draw becomes nullptr after gr_flip\n");
361 return -1;
362 }
Tao Bao557fa1f2017-02-07 12:51:00 -0800363
Luke Song846012f2017-09-13 15:56:16 -0700364 gr_rotate(DEFAULT_ROTATION);
365
366 if (gr_draw->pixel_bytes != 4) {
367 printf("gr_init: Only 4-byte pixel formats supported\n");
368 }
369
Tao Bao557fa1f2017-02-07 12:51:00 -0800370 return 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800371}
372
Tao Bao557fa1f2017-02-07 12:51:00 -0800373void gr_exit() {
374 delete gr_backend;
Tao Bao9f426332018-06-13 10:39:44 -0700375 gr_backend = nullptr;
376
377 delete gr_font;
378 gr_font = nullptr;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800379}
380
Tao Bao557fa1f2017-02-07 12:51:00 -0800381int gr_fb_width() {
Luke Song846012f2017-09-13 15:56:16 -0700382 return rotation % 2 ? gr_draw->height - 2 * overscan_offset_y
383 : gr_draw->width - 2 * overscan_offset_x;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800384}
385
Tao Bao557fa1f2017-02-07 12:51:00 -0800386int gr_fb_height() {
Luke Song846012f2017-09-13 15:56:16 -0700387 return rotation % 2 ? gr_draw->width - 2 * overscan_offset_x
388 : gr_draw->height - 2 * overscan_offset_y;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800389}
Dima Zavin4daf48a2011-08-30 11:59:20 -0700390
Tao Bao557fa1f2017-02-07 12:51:00 -0800391void gr_fb_blank(bool blank) {
392 gr_backend->Blank(blank);
Dima Zavin4daf48a2011-08-30 11:59:20 -0700393}
Luke Song846012f2017-09-13 15:56:16 -0700394
395void gr_rotate(GRRotation rot) {
396 rotation = rot;
397}