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 | 92bdb5a | 2018-10-21 12:12:37 -0700 | [diff] [blame] | 17 | #pragma once |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 18 | |
Tao Bao | 92bdb5a | 2018-10-21 12:12:37 -0700 | [diff] [blame] | 19 | #include <stdint.h> |
Tao Bao | 9cf163e | 2018-11-07 10:15:50 -0800 | [diff] [blame] | 20 | #include <stdlib.h> |
Doug Zongker | 830b3e3 | 2014-03-11 13:22:04 -0700 | [diff] [blame] | 21 | #include <sys/types.h> |
| 22 | |
Elliott Hughes | 07cfb8f | 2015-04-10 13:12:05 -0700 | [diff] [blame] | 23 | #include <functional> |
Tao Bao | 92bdb5a | 2018-10-21 12:12:37 -0700 | [diff] [blame] | 24 | #include <memory> |
Tianjie Xu | 2078b22 | 2017-03-22 12:27:26 -0700 | [diff] [blame] | 25 | #include <string> |
Tianjie Xu | 29d5575 | 2017-09-20 17:53:46 -0700 | [diff] [blame] | 26 | #include <vector> |
Doug Zongker | 28ce47c | 2011-10-28 10:33:05 -0700 | [diff] [blame] | 27 | |
Tao Bao | 710bc53 | 2018-10-24 07:59:49 -0700 | [diff] [blame] | 28 | #include <android-base/macros.h> |
| 29 | |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 30 | // |
| 31 | // Graphics. |
| 32 | // |
| 33 | |
Tao Bao | 92bdb5a | 2018-10-21 12:12:37 -0700 | [diff] [blame] | 34 | class GRSurface { |
| 35 | public: |
Tao Bao | dd78982 | 2018-11-26 16:28:07 -0800 | [diff] [blame] | 36 | static constexpr size_t kSurfaceDataAlignment = 8; |
| 37 | |
Tao Bao | 9cf163e | 2018-11-07 10:15:50 -0800 | [diff] [blame] | 38 | virtual ~GRSurface() = default; |
Tao Bao | 92bdb5a | 2018-10-21 12:12:37 -0700 | [diff] [blame] | 39 | |
Tao Bao | 44820ac | 2018-10-30 23:34:50 -0700 | [diff] [blame] | 40 | // Creates and returns a GRSurface instance that's sufficient for storing an image of the given |
Tao Bao | dd78982 | 2018-11-26 16:28:07 -0800 | [diff] [blame] | 41 | // size (i.e. row_bytes * height). The starting address of the surface data is aligned to |
| 42 | // kSurfaceDataAlignment. Returns the created GRSurface instance (in std::unique_ptr), or nullptr |
| 43 | // on error. |
| 44 | static std::unique_ptr<GRSurface> Create(size_t width, size_t height, size_t row_bytes, |
| 45 | size_t pixel_bytes); |
Tao Bao | 92bdb5a | 2018-10-21 12:12:37 -0700 | [diff] [blame] | 46 | |
Tao Bao | 63b59dc | 2018-10-31 15:23:04 -0700 | [diff] [blame] | 47 | // Clones the current GRSurface instance (i.e. an image). |
| 48 | std::unique_ptr<GRSurface> Clone() const; |
| 49 | |
Tao Bao | 92bdb5a | 2018-10-21 12:12:37 -0700 | [diff] [blame] | 50 | virtual uint8_t* data() { |
Tao Bao | 9cf163e | 2018-11-07 10:15:50 -0800 | [diff] [blame] | 51 | return data_.get(); |
Tao Bao | 92bdb5a | 2018-10-21 12:12:37 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | const uint8_t* data() const { |
| 55 | return const_cast<const uint8_t*>(const_cast<GRSurface*>(this)->data()); |
| 56 | } |
| 57 | |
Tao Bao | dd78982 | 2018-11-26 16:28:07 -0800 | [diff] [blame] | 58 | size_t data_size() const { |
| 59 | return data_size_; |
| 60 | } |
| 61 | |
| 62 | size_t width; |
| 63 | size_t height; |
| 64 | size_t row_bytes; |
| 65 | size_t pixel_bytes; |
Tao Bao | 92bdb5a | 2018-10-21 12:12:37 -0700 | [diff] [blame] | 66 | |
Tao Bao | 44820ac | 2018-10-30 23:34:50 -0700 | [diff] [blame] | 67 | protected: |
Tao Bao | dd78982 | 2018-11-26 16:28:07 -0800 | [diff] [blame] | 68 | GRSurface(size_t width, size_t height, size_t row_bytes, size_t pixel_bytes) |
Tao Bao | 44820ac | 2018-10-30 23:34:50 -0700 | [diff] [blame] | 69 | : width(width), height(height), row_bytes(row_bytes), pixel_bytes(pixel_bytes) {} |
| 70 | |
Tao Bao | 92bdb5a | 2018-10-21 12:12:37 -0700 | [diff] [blame] | 71 | private: |
Tao Bao | 9cf163e | 2018-11-07 10:15:50 -0800 | [diff] [blame] | 72 | // The deleter for data_, whose data is allocated via aligned_alloc(3). |
| 73 | struct DataDeleter { |
| 74 | void operator()(uint8_t* data) { |
| 75 | free(data); |
| 76 | } |
| 77 | }; |
| 78 | |
| 79 | std::unique_ptr<uint8_t, DataDeleter> data_; |
Tao Bao | 63b59dc | 2018-10-31 15:23:04 -0700 | [diff] [blame] | 80 | size_t data_size_; |
Tao Bao | 710bc53 | 2018-10-24 07:59:49 -0700 | [diff] [blame] | 81 | |
| 82 | DISALLOW_COPY_AND_ASSIGN(GRSurface); |
Elliott Hughes | 07cfb8f | 2015-04-10 13:12:05 -0700 | [diff] [blame] | 83 | }; |
Doug Zongker | 39cf417 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 84 | |
Damien Bargiacchi | 35fff61 | 2016-08-11 15:57:03 -0700 | [diff] [blame] | 85 | struct GRFont { |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 86 | GRSurface* texture; |
| 87 | int char_width; |
| 88 | int char_height; |
| 89 | }; |
| 90 | |
Tao Bao | 44478df | 2018-07-31 22:01:03 -0700 | [diff] [blame] | 91 | enum class GRRotation : int { |
| 92 | NONE = 0, |
| 93 | RIGHT = 1, |
| 94 | DOWN = 2, |
| 95 | LEFT = 3, |
Damien Bargiacchi | 35fff61 | 2016-08-11 15:57:03 -0700 | [diff] [blame] | 96 | }; |
| 97 | |
Tao Bao | ed876a7 | 2018-07-31 21:32:50 -0700 | [diff] [blame] | 98 | enum class PixelFormat : int { |
| 99 | UNKNOWN = 0, |
| 100 | ABGR = 1, |
| 101 | RGBX = 2, |
| 102 | BGRA = 3, |
| 103 | }; |
| 104 | |
Tao Bao | 9f42633 | 2018-06-13 10:39:44 -0700 | [diff] [blame] | 105 | // Initializes the graphics backend and loads font file. Returns 0 on success, or -1 on error. Note |
| 106 | // that the font initialization failure would be non-fatal, as caller may not need to draw any text |
| 107 | // at all. Caller can check the font initialization result via gr_sys_font() as needed. |
Elliott Hughes | 07cfb8f | 2015-04-10 13:12:05 -0700 | [diff] [blame] | 108 | int gr_init(); |
Tao Bao | 9f42633 | 2018-06-13 10:39:44 -0700 | [diff] [blame] | 109 | |
| 110 | // Frees the allocated resources. The function is idempotent, and safe to be called if gr_init() |
| 111 | // didn't finish successfully. |
Elliott Hughes | 07cfb8f | 2015-04-10 13:12:05 -0700 | [diff] [blame] | 112 | void gr_exit(); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 113 | |
Elliott Hughes | 07cfb8f | 2015-04-10 13:12:05 -0700 | [diff] [blame] | 114 | int gr_fb_width(); |
| 115 | int gr_fb_height(); |
Doug Zongker | 39cf417 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 116 | |
Elliott Hughes | 07cfb8f | 2015-04-10 13:12:05 -0700 | [diff] [blame] | 117 | void gr_flip(); |
Dima Zavin | 4daf48a | 2011-08-30 11:59:20 -0700 | [diff] [blame] | 118 | void gr_fb_blank(bool blank); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 119 | |
Tao Bao | 9f42633 | 2018-06-13 10:39:44 -0700 | [diff] [blame] | 120 | // Clears entire surface to current color. |
| 121 | void gr_clear(); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 122 | void gr_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a); |
Doug Zongker | c560a67 | 2012-12-18 16:31:27 -0800 | [diff] [blame] | 123 | void gr_fill(int x1, int y1, int x2, int y2); |
Damien Bargiacchi | 35fff61 | 2016-08-11 15:57:03 -0700 | [diff] [blame] | 124 | |
Tao Bao | 92bdb5a | 2018-10-21 12:12:37 -0700 | [diff] [blame] | 125 | void gr_texticon(int x, int y, const GRSurface* icon); |
Damien Bargiacchi | 35fff61 | 2016-08-11 15:57:03 -0700 | [diff] [blame] | 126 | |
| 127 | const GRFont* gr_sys_font(); |
Damien Bargiacchi | d00f5eb | 2016-09-09 07:14:08 -0700 | [diff] [blame] | 128 | int gr_init_font(const char* name, GRFont** dest); |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 129 | void gr_text(const GRFont* font, int x, int y, const char* s, bool bold); |
Tao Bao | 9f42633 | 2018-06-13 10:39:44 -0700 | [diff] [blame] | 130 | // Returns -1 if font is nullptr. |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 131 | int gr_measure(const GRFont* font, const char* s); |
Tao Bao | 9f42633 | 2018-06-13 10:39:44 -0700 | [diff] [blame] | 132 | // Returns -1 if font is nullptr. |
Tianjie Xu | 842f2a3 | 2018-05-31 18:16:28 -0700 | [diff] [blame] | 133 | int gr_font_size(const GRFont* font, int* x, int* y); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 134 | |
Tao Bao | 92bdb5a | 2018-10-21 12:12:37 -0700 | [diff] [blame] | 135 | void gr_blit(const GRSurface* source, int sx, int sy, int w, int h, int dx, int dy); |
Tao Bao | 9f42633 | 2018-06-13 10:39:44 -0700 | [diff] [blame] | 136 | unsigned int gr_get_width(const GRSurface* surface); |
| 137 | unsigned int gr_get_height(const GRSurface* surface); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 138 | |
Tao Bao | 9f42633 | 2018-06-13 10:39:44 -0700 | [diff] [blame] | 139 | // Sets rotation, flips gr_fb_width/height if 90 degree rotation difference |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 140 | void gr_rotate(GRRotation rotation); |
| 141 | |
Tao Bao | ed876a7 | 2018-07-31 21:32:50 -0700 | [diff] [blame] | 142 | // Returns the current PixelFormat being used. |
| 143 | PixelFormat gr_pixel_format(); |
| 144 | |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 145 | // |
| 146 | // Input events. |
| 147 | // |
| 148 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 149 | struct input_event; |
| 150 | |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 151 | using ev_callback = std::function<int(int fd, uint32_t epevents)>; |
| 152 | using ev_set_key_callback = std::function<int(int code, int value)>; |
Dima Zavin | bc29063 | 2011-08-30 11:59:45 -0700 | [diff] [blame] | 153 | |
Tao Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 154 | int ev_init(ev_callback input_cb, bool allow_touch_inputs = false); |
Elliott Hughes | 07cfb8f | 2015-04-10 13:12:05 -0700 | [diff] [blame] | 155 | void ev_exit(); |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 156 | int ev_add_fd(int fd, ev_callback cb); |
Chih-Hung Hsieh | 23abfd3 | 2016-07-27 10:19:47 -0700 | [diff] [blame] | 157 | void ev_iterate_available_keys(const std::function<void(int)>& f); |
Tao Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 158 | void ev_iterate_touch_inputs(const std::function<void(int)>& action); |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 159 | int ev_sync_key_state(const ev_set_key_callback& set_key_cb); |
Dima Zavin | bc29063 | 2011-08-30 11:59:45 -0700 | [diff] [blame] | 160 | |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 161 | // 'timeout' has the same semantics as poll(2). |
| 162 | // 0 : don't block |
| 163 | // < 0 : block forever |
| 164 | // > 0 : block for 'timeout' milliseconds |
Dima Zavin | bc29063 | 2011-08-30 11:59:45 -0700 | [diff] [blame] | 165 | int ev_wait(int timeout); |
| 166 | |
Elliott Hughes | 07cfb8f | 2015-04-10 13:12:05 -0700 | [diff] [blame] | 167 | int ev_get_input(int fd, uint32_t epevents, input_event* ev); |
| 168 | void ev_dispatch(); |
| 169 | int ev_get_epollfd(); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 170 | |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 171 | // |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 172 | // Resources |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 173 | // |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 174 | |
Tianjie Xu | 2078b22 | 2017-03-22 12:27:26 -0700 | [diff] [blame] | 175 | bool matches_locale(const std::string& prefix, const std::string& locale); |
Tianjie Xu | 2430e29 | 2016-04-19 15:02:41 -0700 | [diff] [blame] | 176 | |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 177 | // res_create_*_surface() functions return 0 if no error, else |
| 178 | // negative. |
| 179 | // |
| 180 | // A "display" surface is one that is intended to be drawn to the |
| 181 | // screen with gr_blit(). An "alpha" surface is a grayscale image |
| 182 | // interpreted as an alpha mask used to render text in the current |
| 183 | // color (with gr_text() or gr_texticon()). |
| 184 | // |
| 185 | // All these functions load PNG images from "/res/images/${name}.png". |
| 186 | |
| 187 | // Load a single display surface from a PNG image. |
Elliott Hughes | 0a5cb0c | 2015-04-15 10:58:56 -0700 | [diff] [blame] | 188 | int res_create_display_surface(const char* name, GRSurface** pSurface); |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 189 | |
| 190 | // Load an array of display surfaces from a single PNG image. The PNG |
| 191 | // should have a 'Frames' text chunk whose value is the number of |
| 192 | // frames this image represents. The pixel data itself is interlaced |
| 193 | // by row. |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 194 | int res_create_multi_display_surface(const char* name, int* frames, int* fps, |
| 195 | GRSurface*** pSurface); |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 196 | |
| 197 | // Load a single alpha surface from a grayscale PNG image. |
Elliott Hughes | 0a5cb0c | 2015-04-15 10:58:56 -0700 | [diff] [blame] | 198 | int res_create_alpha_surface(const char* name, GRSurface** pSurface); |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 199 | |
| 200 | // Load part of a grayscale PNG image that is the first match for the |
| 201 | // given locale. The image is expected to be a composite of multiple |
| 202 | // translations of the same text, with special added rows that encode |
| 203 | // the subimages' size and intended locale in the pixel data. See |
Tianjie Xu | 9a25977 | 2016-07-28 14:15:00 -0700 | [diff] [blame] | 204 | // bootable/recovery/tools/recovery_l10n for an app that will generate |
| 205 | // these specialized images from Android resources. |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 206 | int res_create_localized_alpha_surface(const char* name, const char* locale, |
Elliott Hughes | 0a5cb0c | 2015-04-15 10:58:56 -0700 | [diff] [blame] | 207 | GRSurface** pSurface); |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 208 | |
Tianjie Xu | 29d5575 | 2017-09-20 17:53:46 -0700 | [diff] [blame] | 209 | // Return a list of locale strings embedded in |png_name|. Return a empty list in case of failure. |
| 210 | std::vector<std::string> get_locales_in_png(const std::string& png_name); |
| 211 | |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 212 | // Free a surface allocated by any of the res_create_*_surface() |
| 213 | // functions. |
Elliott Hughes | 0a5cb0c | 2015-04-15 10:58:56 -0700 | [diff] [blame] | 214 | void res_free_surface(GRSurface* surface); |