1 /*
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
17 #include "graphics.h"
18
19 #include <stdint.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23
24 #include <memory>
25
26 #include <android-base/properties.h>
27
28 #include "graphics_drm.h"
29 #include "graphics_fbdev.h"
30 #include "minui/minui.h"
31
32 static GRFont* gr_font = nullptr;
33 static MinuiBackend* gr_backend = nullptr;
34
35 static int overscan_offset_x = 0;
36 static int overscan_offset_y = 0;
37
38 static uint32_t gr_current = ~0;
39 static constexpr uint32_t alpha_mask = 0xff000000;
40
41 // gr_draw is owned by backends.
42 static GRSurface* gr_draw = nullptr;
43 static GRRotation rotation = GRRotation::NONE;
44 static PixelFormat pixel_format = PixelFormat::UNKNOWN;
45
outside(int x,int y)46 static bool outside(int x, int y) {
47 auto swapped = (rotation == GRRotation::LEFT || rotation == GRRotation::RIGHT);
48 return x < 0 || x >= (swapped ? gr_draw->height : gr_draw->width) || y < 0 ||
49 y >= (swapped ? gr_draw->width : gr_draw->height);
50 }
51
gr_sys_font()52 const GRFont* gr_sys_font() {
53 return gr_font;
54 }
55
gr_pixel_format()56 PixelFormat gr_pixel_format() {
57 return pixel_format;
58 }
59
gr_measure(const GRFont * font,const char * s)60 int gr_measure(const GRFont* font, const char* s) {
61 if (font == nullptr) {
62 return -1;
63 }
64
65 return font->char_width * strlen(s);
66 }
67
gr_font_size(const GRFont * font,int * x,int * y)68 int gr_font_size(const GRFont* font, int* x, int* y) {
69 if (font == nullptr) {
70 return -1;
71 }
72
73 *x = font->char_width;
74 *y = font->char_height;
75 return 0;
76 }
77
78 // Blends gr_current onto pix value, assumes alpha as most significant byte.
pixel_blend(uint8_t alpha,uint32_t pix)79 static inline uint32_t pixel_blend(uint8_t alpha, uint32_t pix) {
80 if (alpha == 255) return gr_current;
81 if (alpha == 0) return pix;
82 uint32_t pix_r = pix & 0xff;
83 uint32_t pix_g = pix & 0xff00;
84 uint32_t pix_b = pix & 0xff0000;
85 uint32_t cur_r = gr_current & 0xff;
86 uint32_t cur_g = gr_current & 0xff00;
87 uint32_t cur_b = gr_current & 0xff0000;
88
89 uint32_t out_r = (pix_r * (255 - alpha) + cur_r * alpha) / 255;
90 uint32_t out_g = (pix_g * (255 - alpha) + cur_g * alpha) / 255;
91 uint32_t out_b = (pix_b * (255 - alpha) + cur_b * alpha) / 255;
92
93 return (out_r & 0xff) | (out_g & 0xff00) | (out_b & 0xff0000) | (gr_current & 0xff000000);
94 }
95
96 // Increments pixel pointer right, with current rotation.
incr_x(uint32_t ** p,int row_pixels)97 static void incr_x(uint32_t** p, int row_pixels) {
98 if (rotation == GRRotation::LEFT) {
99 *p = *p - row_pixels;
100 } else if (rotation == GRRotation::RIGHT) {
101 *p = *p + row_pixels;
102 } else if (rotation == GRRotation::DOWN) {
103 *p = *p - 1;
104 } else { // GRRotation::NONE
105 *p = *p + 1;
106 }
107 }
108
109 // Increments pixel pointer down, with current rotation.
incr_y(uint32_t ** p,int row_pixels)110 static void incr_y(uint32_t** p, int row_pixels) {
111 if (rotation == GRRotation::LEFT) {
112 *p = *p + 1;
113 } else if (rotation == GRRotation::RIGHT) {
114 *p = *p - 1;
115 } else if (rotation == GRRotation::DOWN) {
116 *p = *p - row_pixels;
117 } else { // GRRotation::NONE
118 *p = *p + row_pixels;
119 }
120 }
121
122 // Returns pixel pointer at given coordinates with rotation adjustment.
PixelAt(GRSurface * surface,int x,int y,int row_pixels)123 static uint32_t* PixelAt(GRSurface* surface, int x, int y, int row_pixels) {
124 switch (rotation) {
125 case GRRotation::NONE:
126 return reinterpret_cast<uint32_t*>(surface->data()) + y * row_pixels + x;
127 case GRRotation::RIGHT:
128 return reinterpret_cast<uint32_t*>(surface->data()) + x * row_pixels + (surface->width - y);
129 case GRRotation::DOWN:
130 return reinterpret_cast<uint32_t*>(surface->data()) + (surface->height - 1 - y) * row_pixels +
131 (surface->width - 1 - x);
132 case GRRotation::LEFT:
133 return reinterpret_cast<uint32_t*>(surface->data()) + (surface->height - 1 - x) * row_pixels +
134 y;
135 default:
136 printf("invalid rotation %d", static_cast<int>(rotation));
137 }
138 return nullptr;
139 }
140
TextBlend(const uint8_t * src_p,int src_row_bytes,uint32_t * dst_p,int dst_row_pixels,int width,int height)141 static void TextBlend(const uint8_t* src_p, int src_row_bytes, uint32_t* dst_p, int dst_row_pixels,
142 int width, int height) {
143 uint8_t alpha_current = static_cast<uint8_t>((alpha_mask & gr_current) >> 24);
144 for (int j = 0; j < height; ++j) {
145 const uint8_t* sx = src_p;
146 uint32_t* px = dst_p;
147 for (int i = 0; i < width; ++i, incr_x(&px, dst_row_pixels)) {
148 uint8_t a = *sx++;
149 if (alpha_current < 255) a = (static_cast<uint32_t>(a) * alpha_current) / 255;
150 *px = pixel_blend(a, *px);
151 }
152 src_p += src_row_bytes;
153 incr_y(&dst_p, dst_row_pixels);
154 }
155 }
156
gr_text(const GRFont * font,int x,int y,const char * s,bool bold)157 void gr_text(const GRFont* font, int x, int y, const char* s, bool bold) {
158 if (!font || !font->texture || (gr_current & alpha_mask) == 0) return;
159
160 if (font->texture->pixel_bytes != 1) {
161 printf("gr_text: font has wrong format\n");
162 return;
163 }
164
165 bold = bold && (font->texture->height != font->char_height);
166
167 x += overscan_offset_x;
168 y += overscan_offset_y;
169
170 unsigned char ch;
171 while ((ch = *s++)) {
172 if (outside(x, y) || outside(x + font->char_width - 1, y + font->char_height - 1)) break;
173
174 if (ch < ' ' || ch > '~') {
175 ch = '?';
176 }
177
178 int row_pixels = gr_draw->row_bytes / gr_draw->pixel_bytes;
179 const uint8_t* src_p = font->texture->data() + ((ch - ' ') * font->char_width) +
180 (bold ? font->char_height * font->texture->row_bytes : 0);
181 uint32_t* dst_p = PixelAt(gr_draw, x, y, row_pixels);
182
183 TextBlend(src_p, font->texture->row_bytes, dst_p, row_pixels, font->char_width,
184 font->char_height);
185
186 x += font->char_width;
187 }
188 }
189
gr_texticon(int x,int y,const GRSurface * icon)190 void gr_texticon(int x, int y, const GRSurface* icon) {
191 if (icon == nullptr) return;
192
193 if (icon->pixel_bytes != 1) {
194 printf("gr_texticon: source has wrong format\n");
195 return;
196 }
197
198 x += overscan_offset_x;
199 y += overscan_offset_y;
200
201 if (outside(x, y) || outside(x + icon->width - 1, y + icon->height - 1)) return;
202
203 int row_pixels = gr_draw->row_bytes / gr_draw->pixel_bytes;
204 const uint8_t* src_p = icon->data();
205 uint32_t* dst_p = PixelAt(gr_draw, x, y, row_pixels);
206 TextBlend(src_p, icon->row_bytes, dst_p, row_pixels, icon->width, icon->height);
207 }
208
gr_color(unsigned char r,unsigned char g,unsigned char b,unsigned char a)209 void gr_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a) {
210 uint32_t r32 = r, g32 = g, b32 = b, a32 = a;
211 if (pixel_format == PixelFormat::ARGB || pixel_format == PixelFormat::BGRA) {
212 gr_current = (a32 << 24) | (r32 << 16) | (g32 << 8) | b32;
213 } else {
214 gr_current = (a32 << 24) | (b32 << 16) | (g32 << 8) | r32;
215 }
216 }
217
gr_clear()218 void gr_clear() {
219 if ((gr_current & 0xff) == ((gr_current >> 8) & 0xff) &&
220 (gr_current & 0xff) == ((gr_current >> 16) & 0xff) &&
221 (gr_current & 0xff) == ((gr_current >> 24) & 0xff) &&
222 gr_draw->row_bytes == gr_draw->width * gr_draw->pixel_bytes) {
223 memset(gr_draw->data(), gr_current & 0xff, gr_draw->height * gr_draw->row_bytes);
224 } else {
225 uint32_t* px = reinterpret_cast<uint32_t*>(gr_draw->data());
226 int row_diff = gr_draw->row_bytes / gr_draw->pixel_bytes - gr_draw->width;
227 for (int y = 0; y < gr_draw->height; ++y) {
228 for (int x = 0; x < gr_draw->width; ++x) {
229 *px++ = gr_current;
230 }
231 px += row_diff;
232 }
233 }
234 }
235
gr_fill(int x1,int y1,int x2,int y2)236 void gr_fill(int x1, int y1, int x2, int y2) {
237 x1 += overscan_offset_x;
238 y1 += overscan_offset_y;
239
240 x2 += overscan_offset_x;
241 y2 += overscan_offset_y;
242
243 if (outside(x1, y1) || outside(x2 - 1, y2 - 1)) return;
244
245 int row_pixels = gr_draw->row_bytes / gr_draw->pixel_bytes;
246 uint32_t* p = PixelAt(gr_draw, x1, y1, row_pixels);
247 uint8_t alpha = static_cast<uint8_t>(((gr_current & alpha_mask) >> 24));
248 if (alpha > 0) {
249 for (int y = y1; y < y2; ++y) {
250 uint32_t* px = p;
251 for (int x = x1; x < x2; ++x) {
252 *px = pixel_blend(alpha, *px);
253 incr_x(&px, row_pixels);
254 }
255 incr_y(&p, row_pixels);
256 }
257 }
258 }
259
gr_blit(const GRSurface * source,int sx,int sy,int w,int h,int dx,int dy)260 void gr_blit(const GRSurface* source, int sx, int sy, int w, int h, int dx, int dy) {
261 if (source == nullptr) return;
262
263 if (gr_draw->pixel_bytes != source->pixel_bytes) {
264 printf("gr_blit: source has wrong format\n");
265 return;
266 }
267
268 dx += overscan_offset_x;
269 dy += overscan_offset_y;
270
271 if (outside(dx, dy) || outside(dx + w - 1, dy + h - 1)) return;
272
273 if (rotation != GRRotation::NONE) {
274 int src_row_pixels = source->row_bytes / source->pixel_bytes;
275 int row_pixels = gr_draw->row_bytes / gr_draw->pixel_bytes;
276 const uint32_t* src_py =
277 reinterpret_cast<const uint32_t*>(source->data()) + sy * source->row_bytes / 4 + sx;
278 uint32_t* dst_py = PixelAt(gr_draw, dx, dy, row_pixels);
279
280 for (int y = 0; y < h; y += 1) {
281 const uint32_t* src_px = src_py;
282 uint32_t* dst_px = dst_py;
283 for (int x = 0; x < w; x += 1) {
284 *dst_px = *src_px++;
285 incr_x(&dst_px, row_pixels);
286 }
287 src_py += src_row_pixels;
288 incr_y(&dst_py, row_pixels);
289 }
290 } else {
291 const uint8_t* src_p = source->data() + sy * source->row_bytes + sx * source->pixel_bytes;
292 uint8_t* dst_p = gr_draw->data() + dy * gr_draw->row_bytes + dx * gr_draw->pixel_bytes;
293
294 for (int i = 0; i < h; ++i) {
295 memcpy(dst_p, src_p, w * source->pixel_bytes);
296 src_p += source->row_bytes;
297 dst_p += gr_draw->row_bytes;
298 }
299 }
300 }
301
gr_get_width(const GRSurface * surface)302 unsigned int gr_get_width(const GRSurface* surface) {
303 if (surface == nullptr) {
304 return 0;
305 }
306 return surface->width;
307 }
308
gr_get_height(const GRSurface * surface)309 unsigned int gr_get_height(const GRSurface* surface) {
310 if (surface == nullptr) {
311 return 0;
312 }
313 return surface->height;
314 }
315
gr_init_font(const char * name,GRFont ** dest)316 int gr_init_font(const char* name, GRFont** dest) {
317 GRFont* font = static_cast<GRFont*>(calloc(1, sizeof(*gr_font)));
318 if (font == nullptr) {
319 return -1;
320 }
321
322 int res = res_create_alpha_surface(name, &(font->texture));
323 if (res < 0) {
324 free(font);
325 return res;
326 }
327
328 // The font image should be a 96x2 array of character images. The
329 // columns are the printable ASCII characters 0x20 - 0x7f. The
330 // top row is regular text; the bottom row is bold.
331 font->char_width = font->texture->width / 96;
332 font->char_height = font->texture->height / 2;
333
334 *dest = font;
335
336 return 0;
337 }
338
gr_flip()339 void gr_flip() {
340 gr_draw = gr_backend->Flip();
341 }
342
gr_init()343 int gr_init() {
344 // pixel_format needs to be set before loading any resources or initializing backends.
345 std::string format = android::base::GetProperty("ro.minui.pixel_format", "");
346 if (format == "ABGR_8888") {
347 pixel_format = PixelFormat::ABGR;
348 } else if (format == "RGBX_8888") {
349 pixel_format = PixelFormat::RGBX;
350 } else if (format == "ARGB_8888") {
351 pixel_format = PixelFormat::ARGB;
352 } else if (format == "BGRA_8888") {
353 pixel_format = PixelFormat::BGRA;
354 } else {
355 pixel_format = PixelFormat::UNKNOWN;
356 }
357
358 int ret = gr_init_font("font", &gr_font);
359 if (ret != 0) {
360 printf("Failed to init font: %d, continuing graphic backend initialization without font file\n",
361 ret);
362 }
363
364 auto backend = std::unique_ptr<MinuiBackend>{ std::make_unique<MinuiBackendDrm>() };
365 gr_draw = backend->Init();
366
367 if (!gr_draw) {
368 backend = std::make_unique<MinuiBackendFbdev>();
369 gr_draw = backend->Init();
370 }
371
372 if (!gr_draw) {
373 return -1;
374 }
375
376 gr_backend = backend.release();
377
378 int overscan_percent = android::base::GetIntProperty("ro.minui.overscan_percent", 0);
379 overscan_offset_x = gr_draw->width * overscan_percent / 100;
380 overscan_offset_y = gr_draw->height * overscan_percent / 100;
381
382 gr_flip();
383 gr_flip();
384 if (!gr_draw) {
385 printf("gr_init: gr_draw becomes nullptr after gr_flip\n");
386 return -1;
387 }
388
389 std::string rotation_str =
390 android::base::GetProperty("ro.minui.default_rotation", "ROTATION_NONE");
391 if (rotation_str == "ROTATION_RIGHT") {
392 gr_rotate(GRRotation::RIGHT);
393 } else if (rotation_str == "ROTATION_DOWN") {
394 gr_rotate(GRRotation::DOWN);
395 } else if (rotation_str == "ROTATION_LEFT") {
396 gr_rotate(GRRotation::LEFT);
397 } else { // "ROTATION_NONE" or unknown string
398 gr_rotate(GRRotation::NONE);
399 }
400
401 if (gr_draw->pixel_bytes != 4) {
402 printf("gr_init: Only 4-byte pixel formats supported\n");
403 }
404
405 return 0;
406 }
407
gr_exit()408 void gr_exit() {
409 delete gr_backend;
410 gr_backend = nullptr;
411
412 delete gr_font;
413 gr_font = nullptr;
414 }
415
gr_fb_width()416 int gr_fb_width() {
417 return (rotation == GRRotation::LEFT || rotation == GRRotation::RIGHT)
418 ? gr_draw->height - 2 * overscan_offset_y
419 : gr_draw->width - 2 * overscan_offset_x;
420 }
421
gr_fb_height()422 int gr_fb_height() {
423 return (rotation == GRRotation::LEFT || rotation == GRRotation::RIGHT)
424 ? gr_draw->width - 2 * overscan_offset_x
425 : gr_draw->height - 2 * overscan_offset_y;
426 }
427
gr_fb_blank(bool blank)428 void gr_fb_blank(bool blank) {
429 gr_backend->Blank(blank);
430 }
431
gr_rotate(GRRotation rot)432 void gr_rotate(GRRotation rot) {
433 rotation = rot;
434 }
435