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