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