The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 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 | |
Tao Bao | 0ecbd76 | 2017-01-16 21:16:58 -0800 | [diff] [blame] | 17 | #include "graphics.h" |
| 18 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame^] | 19 | #include <stdint.h> |
Tao Bao | e8020f4 | 2017-02-03 09:30:07 -0800 | [diff] [blame] | 20 | #include <stdio.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 21 | #include <stdlib.h> |
Elliott Hughes | cd3c55a | 2015-01-29 20:50:08 -0800 | [diff] [blame] | 22 | #include <string.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 23 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 24 | #include <memory> |
| 25 | |
Doug Zongker | 6fd59ac | 2013-03-06 15:01:11 -0800 | [diff] [blame] | 26 | #include "font_10x18.h" |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 27 | #include "graphics_adf.h" |
| 28 | #include "graphics_drm.h" |
| 29 | #include "graphics_fbdev.h" |
Tao Bao | 0ecbd76 | 2017-01-16 21:16:58 -0800 | [diff] [blame] | 30 | #include "minui/minui.h" |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 31 | |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 32 | static GRFont* gr_font = NULL; |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 33 | static MinuiBackend* gr_backend = nullptr; |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 34 | |
Doug Zongker | c560a67 | 2012-12-18 16:31:27 -0800 | [diff] [blame] | 35 | static int overscan_percent = OVERSCAN_PERCENT; |
| 36 | static int overscan_offset_x = 0; |
| 37 | static int overscan_offset_y = 0; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 38 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame^] | 39 | static uint32_t gr_current = ~0; |
| 40 | static constexpr uint32_t alpha_mask = 0xff000000; |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 41 | |
Doug Zongker | 5290f20 | 2014-03-11 13:22:04 -0700 | [diff] [blame] | 42 | static GRSurface* gr_draw = NULL; |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame^] | 43 | static GRRotation rotation = ROTATION_NONE; |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 44 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame^] | 45 | static 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 Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 48 | } |
| 49 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame^] | 50 | const GRFont* gr_sys_font() { |
| 51 | return gr_font; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 52 | } |
| 53 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame^] | 54 | int gr_measure(const GRFont* font, const char* s) { |
| 55 | return font->char_width * strlen(s); |
Damien Bargiacchi | 35fff61 | 2016-08-11 15:57:03 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame^] | 58 | void gr_font_size(const GRFont* font, int* x, int* y) { |
| 59 | *x = font->char_width; |
| 60 | *y = font->char_height; |
Dima Zavin | 3c7f00e | 2011-08-30 11:58:24 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame^] | 63 | // Blends gr_current onto pix value, assumes alpha as most significant byte. |
| 64 | static inline uint32_t pixel_blend(uint8_t alpha, uint32_t pix) { |
| 65 | if (alpha == 255) return gr_current; |
| 66 | if (alpha == 0) return pix; |
| 67 | uint32_t pix_r = pix & 0xff; |
| 68 | uint32_t pix_g = pix & 0xff00; |
| 69 | uint32_t pix_b = pix & 0xff0000; |
| 70 | uint32_t cur_r = gr_current & 0xff; |
| 71 | uint32_t cur_g = gr_current & 0xff00; |
| 72 | uint32_t cur_b = gr_current & 0xff0000; |
| 73 | |
| 74 | uint32_t out_r = (pix_r * (255 - alpha) + cur_r * alpha) / 255; |
| 75 | uint32_t out_g = (pix_g * (255 - alpha) + cur_g * alpha) / 255; |
| 76 | uint32_t out_b = (pix_b * (255 - alpha) + cur_b * alpha) / 255; |
| 77 | |
| 78 | return (out_r & 0xff) | (out_g & 0xff00) | (out_b & 0xff0000) | (gr_current & 0xff000000); |
| 79 | } |
| 80 | |
| 81 | // increments pixel pointer right, with current rotation. |
| 82 | static void incr_x(uint32_t** p, int row_pixels) { |
| 83 | if (rotation % 2) { |
| 84 | *p = *p + (rotation == 1 ? 1 : -1) * row_pixels; |
| 85 | } else { |
| 86 | *p = *p + (rotation ? -1 : 1); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | // increments pixel pointer down, with current rotation. |
| 91 | static void incr_y(uint32_t** p, int row_pixels) { |
| 92 | if (rotation % 2) { |
| 93 | *p = *p + (rotation == 1 ? -1 : 1); |
| 94 | } else { |
| 95 | *p = *p + (rotation ? -1 : 1) * row_pixels; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | // returns pixel pointer at given coordinates with rotation adjustment. |
| 100 | static uint32_t* pixel_at(GRSurface* surf, int x, int y, int row_pixels) { |
| 101 | switch (rotation) { |
| 102 | case ROTATION_NONE: |
| 103 | return reinterpret_cast<uint32_t*>(surf->data) + y * row_pixels + x; |
| 104 | case ROTATION_RIGHT: |
| 105 | return reinterpret_cast<uint32_t*>(surf->data) + x * row_pixels + (surf->width - y); |
| 106 | case ROTATION_DOWN: |
| 107 | return reinterpret_cast<uint32_t*>(surf->data) + (surf->height - 1 - y) * row_pixels + |
| 108 | (surf->width - 1 - x); |
| 109 | case ROTATION_LEFT: |
| 110 | return reinterpret_cast<uint32_t*>(surf->data) + (surf->height - 1 - x) * row_pixels + y; |
| 111 | default: |
| 112 | printf("invalid rotation %d", rotation); |
| 113 | } |
| 114 | return nullptr; |
| 115 | } |
| 116 | |
| 117 | static void text_blend(uint8_t* src_p, int src_row_bytes, uint32_t* dst_p, int dst_row_pixels, |
| 118 | int width, int height) { |
| 119 | uint8_t alpha_current = static_cast<uint8_t>((alpha_mask & gr_current) >> 24); |
| 120 | for (int j = 0; j < height; ++j) { |
| 121 | uint8_t* sx = src_p; |
| 122 | uint32_t* px = dst_p; |
| 123 | for (int i = 0; i < width; ++i, incr_x(&px, dst_row_pixels)) { |
| 124 | uint8_t a = *sx++; |
| 125 | if (alpha_current < 255) a = (static_cast<uint32_t>(a) * alpha_current) / 255; |
| 126 | *px = pixel_blend(a, *px); |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 127 | } |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame^] | 128 | src_p += src_row_bytes; |
| 129 | incr_y(&dst_p, dst_row_pixels); |
| 130 | } |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 131 | } |
| 132 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame^] | 133 | void gr_text(const GRFont* font, int x, int y, const char* s, bool bold) { |
| 134 | if (!font || !font->texture || (gr_current & alpha_mask) == 0) return; |
Doug Zongker | 55a36ac | 2013-03-04 15:49:02 -0800 | [diff] [blame] | 135 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame^] | 136 | if (font->texture->pixel_bytes != 1) { |
| 137 | printf("gr_text: font has wrong format\n"); |
| 138 | return; |
| 139 | } |
Doug Zongker | 6fd59ac | 2013-03-06 15:01:11 -0800 | [diff] [blame] | 140 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame^] | 141 | bold = bold && (font->texture->height != font->char_height); |
Doug Zongker | c560a67 | 2012-12-18 16:31:27 -0800 | [diff] [blame] | 142 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame^] | 143 | x += overscan_offset_x; |
| 144 | y += overscan_offset_y; |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 145 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame^] | 146 | unsigned char ch; |
| 147 | while ((ch = *s++)) { |
| 148 | if (outside(x, y) || outside(x + font->char_width - 1, y + font->char_height - 1)) break; |
Elliott Hughes | 01a4d08 | 2015-03-24 15:21:48 -0700 | [diff] [blame] | 149 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame^] | 150 | if (ch < ' ' || ch > '~') { |
| 151 | ch = '?'; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 152 | } |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame^] | 153 | |
| 154 | int row_pixels = gr_draw->row_bytes / gr_draw->pixel_bytes; |
| 155 | uint8_t* src_p = font->texture->data + ((ch - ' ') * font->char_width) + |
| 156 | (bold ? font->char_height * font->texture->row_bytes : 0); |
| 157 | uint32_t* dst_p = pixel_at(gr_draw, x, y, row_pixels); |
| 158 | |
| 159 | text_blend(src_p, font->texture->row_bytes, dst_p, row_pixels, font->char_width, |
| 160 | font->char_height); |
| 161 | |
| 162 | x += font->char_width; |
| 163 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 164 | } |
| 165 | |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 166 | void gr_texticon(int x, int y, GRSurface* icon) { |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame^] | 167 | if (icon == NULL) return; |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 168 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame^] | 169 | if (icon->pixel_bytes != 1) { |
| 170 | printf("gr_texticon: source has wrong format\n"); |
| 171 | return; |
| 172 | } |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 173 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame^] | 174 | x += overscan_offset_x; |
| 175 | y += overscan_offset_y; |
Doug Zongker | c560a67 | 2012-12-18 16:31:27 -0800 | [diff] [blame] | 176 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame^] | 177 | if (outside(x, y) || outside(x + icon->width - 1, y + icon->height - 1)) return; |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 178 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame^] | 179 | int row_pixels = gr_draw->row_bytes / gr_draw->pixel_bytes; |
| 180 | uint8_t* src_p = icon->data; |
| 181 | uint32_t* dst_p = pixel_at(gr_draw, x, y, row_pixels); |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 182 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame^] | 183 | text_blend(src_p, icon->row_bytes, dst_p, row_pixels, icon->width, icon->height); |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 184 | } |
| 185 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame^] | 186 | void gr_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a) { |
| 187 | uint32_t r32 = r, g32 = g, b32 = b, a32 = a; |
Tony Kuo | fd778e3 | 2015-02-05 21:25:56 +0800 | [diff] [blame] | 188 | #if defined(RECOVERY_ABGR) || defined(RECOVERY_BGRA) |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame^] | 189 | gr_current = (a32 << 24) | (r32 << 16) | (g32 << 8) | b32; |
Tony Kuo | fd778e3 | 2015-02-05 21:25:56 +0800 | [diff] [blame] | 190 | #else |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame^] | 191 | gr_current = (a32 << 24) | (b32 << 16) | (g32 << 8) | r32; |
Tony Kuo | fd778e3 | 2015-02-05 21:25:56 +0800 | [diff] [blame] | 192 | #endif |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 193 | } |
| 194 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame^] | 195 | void gr_clear() { |
| 196 | if ((gr_current & 0xff) == ((gr_current >> 8) & 0xff) && |
| 197 | (gr_current & 0xff) == ((gr_current >> 16) & 0xff) && |
| 198 | (gr_current & 0xff) == ((gr_current >> 24) & 0xff) && |
| 199 | gr_draw->row_bytes == gr_draw->width * gr_draw->pixel_bytes) { |
| 200 | memset(gr_draw->data, gr_current & 0xff, gr_draw->height * gr_draw->row_bytes); |
| 201 | } else { |
| 202 | uint32_t* px = reinterpret_cast<uint32_t*>(gr_draw->data); |
| 203 | int row_diff = gr_draw->row_bytes / gr_draw->pixel_bytes - gr_draw->width; |
| 204 | for (int y = 0; y < gr_draw->height; ++y) { |
| 205 | for (int x = 0; x < gr_draw->width; ++x) { |
| 206 | *px++ = gr_current; |
| 207 | } |
| 208 | px += row_diff; |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 209 | } |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame^] | 210 | } |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 211 | } |
| 212 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame^] | 213 | void gr_fill(int x1, int y1, int x2, int y2) { |
| 214 | x1 += overscan_offset_x; |
| 215 | y1 += overscan_offset_y; |
Doug Zongker | c560a67 | 2012-12-18 16:31:27 -0800 | [diff] [blame] | 216 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame^] | 217 | x2 += overscan_offset_x; |
| 218 | y2 += overscan_offset_y; |
Doug Zongker | c560a67 | 2012-12-18 16:31:27 -0800 | [diff] [blame] | 219 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame^] | 220 | if (outside(x1, y1) || outside(x2 - 1, y2 - 1)) return; |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 221 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame^] | 222 | int row_pixels = gr_draw->row_bytes / gr_draw->pixel_bytes; |
| 223 | uint32_t* p = pixel_at(gr_draw, x1, y1, row_pixels); |
| 224 | uint8_t alpha = static_cast<uint8_t>(((gr_current & alpha_mask) >> 24)); |
| 225 | if (alpha > 0) { |
| 226 | for (int y = y1; y < y2; ++y) { |
| 227 | uint32_t* px = p; |
| 228 | for (int x = x1; x < x2; ++x) { |
| 229 | *px = pixel_blend(alpha, *px); |
| 230 | incr_x(&px, row_pixels); |
| 231 | } |
| 232 | incr_y(&p, row_pixels); |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 233 | } |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame^] | 234 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 235 | } |
| 236 | |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 237 | void gr_blit(GRSurface* source, int sx, int sy, int w, int h, int dx, int dy) { |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame^] | 238 | if (source == NULL) return; |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 239 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame^] | 240 | if (gr_draw->pixel_bytes != source->pixel_bytes) { |
| 241 | printf("gr_blit: source has wrong format\n"); |
| 242 | return; |
| 243 | } |
| 244 | |
| 245 | dx += overscan_offset_x; |
| 246 | dy += overscan_offset_y; |
| 247 | |
| 248 | if (outside(dx, dy) || outside(dx + w - 1, dy + h - 1)) return; |
| 249 | |
| 250 | if (rotation) { |
| 251 | int src_row_pixels = source->row_bytes / source->pixel_bytes; |
| 252 | int row_pixels = gr_draw->row_bytes / gr_draw->pixel_bytes; |
| 253 | uint32_t* src_py = reinterpret_cast<uint32_t*>(source->data) + sy * source->row_bytes / 4 + sx; |
| 254 | uint32_t* dst_py = pixel_at(gr_draw, dx, dy, row_pixels); |
| 255 | |
| 256 | for (int y = 0; y < h; y += 1) { |
| 257 | uint32_t* src_px = src_py; |
| 258 | uint32_t* dst_px = dst_py; |
| 259 | for (int x = 0; x < w; x += 1) { |
| 260 | *dst_px = *src_px++; |
| 261 | incr_x(&dst_px, row_pixels); |
| 262 | } |
| 263 | src_py += src_row_pixels; |
| 264 | incr_y(&dst_py, row_pixels); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 265 | } |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame^] | 266 | } else { |
| 267 | unsigned char* src_p = source->data + sy * source->row_bytes + sx * source->pixel_bytes; |
| 268 | unsigned char* dst_p = gr_draw->data + dy * gr_draw->row_bytes + dx * gr_draw->pixel_bytes; |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 269 | |
| 270 | int i; |
| 271 | for (i = 0; i < h; ++i) { |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame^] | 272 | memcpy(dst_p, src_p, w * source->pixel_bytes); |
| 273 | src_p += source->row_bytes; |
| 274 | dst_p += gr_draw->row_bytes; |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 275 | } |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame^] | 276 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 277 | } |
| 278 | |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 279 | unsigned int gr_get_width(GRSurface* surface) { |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame^] | 280 | if (surface == NULL) { |
| 281 | return 0; |
| 282 | } |
| 283 | return surface->width; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 284 | } |
| 285 | |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 286 | unsigned int gr_get_height(GRSurface* surface) { |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame^] | 287 | if (surface == NULL) { |
| 288 | return 0; |
| 289 | } |
| 290 | return surface->height; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 291 | } |
| 292 | |
Damien Bargiacchi | d00f5eb | 2016-09-09 07:14:08 -0700 | [diff] [blame] | 293 | int gr_init_font(const char* name, GRFont** dest) { |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame^] | 294 | GRFont* font = static_cast<GRFont*>(calloc(1, sizeof(*gr_font))); |
| 295 | if (font == nullptr) { |
| 296 | return -1; |
| 297 | } |
Damien Bargiacchi | d00f5eb | 2016-09-09 07:14:08 -0700 | [diff] [blame] | 298 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame^] | 299 | int res = res_create_alpha_surface(name, &(font->texture)); |
| 300 | if (res < 0) { |
| 301 | free(font); |
| 302 | return res; |
| 303 | } |
Damien Bargiacchi | 35fff61 | 2016-08-11 15:57:03 -0700 | [diff] [blame] | 304 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame^] | 305 | // The font image should be a 96x2 array of character images. The |
| 306 | // columns are the printable ASCII characters 0x20 - 0x7f. The |
| 307 | // top row is regular text; the bottom row is bold. |
| 308 | font->char_width = font->texture->width / 96; |
| 309 | font->char_height = font->texture->height / 2; |
Damien Bargiacchi | d00f5eb | 2016-09-09 07:14:08 -0700 | [diff] [blame] | 310 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame^] | 311 | *dest = font; |
Damien Bargiacchi | 35fff61 | 2016-08-11 15:57:03 -0700 | [diff] [blame] | 312 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame^] | 313 | return 0; |
Damien Bargiacchi | 35fff61 | 2016-08-11 15:57:03 -0700 | [diff] [blame] | 314 | } |
| 315 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame^] | 316 | static void gr_init_font(void) { |
| 317 | int res = gr_init_font("font", &gr_font); |
| 318 | if (res == 0) { |
| 319 | return; |
| 320 | } |
Damien Bargiacchi | 35fff61 | 2016-08-11 15:57:03 -0700 | [diff] [blame] | 321 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame^] | 322 | printf("failed to read font: res=%d\n", res); |
Damien Bargiacchi | 35fff61 | 2016-08-11 15:57:03 -0700 | [diff] [blame] | 323 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame^] | 324 | // fall back to the compiled-in font. |
| 325 | gr_font = static_cast<GRFont*>(calloc(1, sizeof(*gr_font))); |
| 326 | gr_font->texture = static_cast<GRSurface*>(malloc(sizeof(*gr_font->texture))); |
| 327 | gr_font->texture->width = font.width; |
| 328 | gr_font->texture->height = font.height; |
| 329 | gr_font->texture->row_bytes = font.width; |
| 330 | gr_font->texture->pixel_bytes = 1; |
Damien Bargiacchi | d00f5eb | 2016-09-09 07:14:08 -0700 | [diff] [blame] | 331 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame^] | 332 | unsigned char* bits = static_cast<unsigned char*>(malloc(font.width * font.height)); |
| 333 | gr_font->texture->data = bits; |
Damien Bargiacchi | 35fff61 | 2016-08-11 15:57:03 -0700 | [diff] [blame] | 334 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame^] | 335 | unsigned char data; |
| 336 | unsigned char* in = font.rundata; |
| 337 | while ((data = *in++)) { |
| 338 | memset(bits, (data & 0x80) ? 255 : 0, data & 0x7f); |
| 339 | bits += (data & 0x7f); |
| 340 | } |
Damien Bargiacchi | 35fff61 | 2016-08-11 15:57:03 -0700 | [diff] [blame] | 341 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame^] | 342 | gr_font->char_width = font.char_width; |
| 343 | gr_font->char_height = font.char_height; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 344 | } |
| 345 | |
Doug Zongker | 5290f20 | 2014-03-11 13:22:04 -0700 | [diff] [blame] | 346 | void gr_flip() { |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 347 | gr_draw = gr_backend->Flip(); |
Doug Zongker | 5290f20 | 2014-03-11 13:22:04 -0700 | [diff] [blame] | 348 | } |
| 349 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 350 | int gr_init() { |
| 351 | gr_init_font(); |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 352 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 353 | auto backend = std::unique_ptr<MinuiBackend>{ std::make_unique<MinuiBackendAdf>() }; |
| 354 | gr_draw = backend->Init(); |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 355 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 356 | if (!gr_draw) { |
| 357 | backend = std::make_unique<MinuiBackendDrm>(); |
| 358 | gr_draw = backend->Init(); |
| 359 | } |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 360 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 361 | if (!gr_draw) { |
| 362 | backend = std::make_unique<MinuiBackendFbdev>(); |
| 363 | gr_draw = backend->Init(); |
| 364 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 365 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 366 | if (!gr_draw) { |
| 367 | return -1; |
| 368 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 369 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 370 | gr_backend = backend.release(); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 371 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 372 | overscan_offset_x = gr_draw->width * overscan_percent / 100; |
| 373 | overscan_offset_y = gr_draw->height * overscan_percent / 100; |
| 374 | |
| 375 | gr_flip(); |
| 376 | gr_flip(); |
| 377 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame^] | 378 | gr_rotate(DEFAULT_ROTATION); |
| 379 | |
| 380 | if (gr_draw->pixel_bytes != 4) { |
| 381 | printf("gr_init: Only 4-byte pixel formats supported\n"); |
| 382 | } |
| 383 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 384 | return 0; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 385 | } |
| 386 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 387 | void gr_exit() { |
| 388 | delete gr_backend; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 389 | } |
| 390 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 391 | int gr_fb_width() { |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame^] | 392 | return rotation % 2 ? gr_draw->height - 2 * overscan_offset_y |
| 393 | : gr_draw->width - 2 * overscan_offset_x; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 394 | } |
| 395 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 396 | int gr_fb_height() { |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame^] | 397 | return rotation % 2 ? gr_draw->width - 2 * overscan_offset_x |
| 398 | : gr_draw->height - 2 * overscan_offset_y; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 399 | } |
Dima Zavin | 4daf48a | 2011-08-30 11:59:20 -0700 | [diff] [blame] | 400 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 401 | void gr_fb_blank(bool blank) { |
| 402 | gr_backend->Blank(blank); |
Dima Zavin | 4daf48a | 2011-08-30 11:59:20 -0700 | [diff] [blame] | 403 | } |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame^] | 404 | |
| 405 | void gr_rotate(GRRotation rot) { |
| 406 | rotation = rot; |
| 407 | } |