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 }; 105 106 // Initializes the graphics backend and loads font file. Returns 0 on success, or -1 on error. Note 107 // that the font initialization failure would be non-fatal, as caller may not need to draw any text 108 // at all. Caller can check the font initialization result via gr_sys_font() as needed. 109 int gr_init(); 110 111 // Frees the allocated resources. The function is idempotent, and safe to be called if gr_init() 112 // didn't finish successfully. 113 void gr_exit(); 114 115 int gr_fb_width(); 116 int gr_fb_height(); 117 118 void gr_flip(); 119 void gr_fb_blank(bool blank); 120 121 // Clears entire surface to current color. 122 void gr_clear(); 123 void gr_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a); 124 void gr_fill(int x1, int y1, int x2, int y2); 125 126 void gr_texticon(int x, int y, const GRSurface* icon); 127 128 const GRFont* gr_sys_font(); 129 int gr_init_font(const char* name, GRFont** dest); 130 void gr_text(const GRFont* font, int x, int y, const char* s, bool bold); 131 // Returns -1 if font is nullptr. 132 int gr_measure(const GRFont* font, const char* s); 133 // Returns -1 if font is nullptr. 134 int gr_font_size(const GRFont* font, int* x, int* y); 135 136 void gr_blit(const GRSurface* source, int sx, int sy, int w, int h, int dx, int dy); 137 unsigned int gr_get_width(const GRSurface* surface); 138 unsigned int gr_get_height(const GRSurface* surface); 139 140 // Sets rotation, flips gr_fb_width/height if 90 degree rotation difference 141 void gr_rotate(GRRotation rotation); 142 143 // Returns the current PixelFormat being used. 144 PixelFormat gr_pixel_format(); 145 146 // 147 // Input events. 148 // 149 150 struct input_event; 151 152 using ev_callback = std::function<int(int fd, uint32_t epevents)>; 153 using ev_set_key_callback = std::function<int(int code, int value)>; 154 155 int ev_init(ev_callback input_cb, bool allow_touch_inputs = false); 156 void ev_exit(); 157 int ev_add_fd(android::base::unique_fd&& fd, ev_callback cb); 158 void ev_iterate_available_keys(const std::function<void(int)>& f); 159 void ev_iterate_touch_inputs(const std::function<void(int)>& action); 160 int ev_sync_key_state(const ev_set_key_callback& set_key_cb); 161 162 // 'timeout' has the same semantics as poll(2). 163 // 0 : don't block 164 // < 0 : block forever 165 // > 0 : block for 'timeout' milliseconds 166 int ev_wait(int timeout); 167 168 int ev_get_input(int fd, uint32_t epevents, input_event* ev); 169 void ev_dispatch(); 170 int ev_get_epollfd(); 171 172 // 173 // Resources 174 // 175 176 bool matches_locale(const std::string& prefix, const std::string& locale); 177 178 // res_create_*_surface() functions return 0 if no error, else 179 // negative. 180 // 181 // A "display" surface is one that is intended to be drawn to the 182 // screen with gr_blit(). An "alpha" surface is a grayscale image 183 // interpreted as an alpha mask used to render text in the current 184 // color (with gr_text() or gr_texticon()). 185 // 186 // All these functions load PNG images from "/res/images/${name}.png". 187 188 // Load a single display surface from a PNG image. 189 int res_create_display_surface(const char* name, GRSurface** pSurface); 190 191 // Load an array of display surfaces from a single PNG image. The PNG 192 // should have a 'Frames' text chunk whose value is the number of 193 // frames this image represents. The pixel data itself is interlaced 194 // by row. 195 int res_create_multi_display_surface(const char* name, int* frames, int* fps, 196 GRSurface*** pSurface); 197 198 // Load a single alpha surface from a grayscale PNG image. 199 int res_create_alpha_surface(const char* name, GRSurface** pSurface); 200 201 // Load part of a grayscale PNG image that is the first match for the 202 // given locale. The image is expected to be a composite of multiple 203 // translations of the same text, with special added rows that encode 204 // the subimages' size and intended locale in the pixel data. See 205 // bootable/recovery/tools/recovery_l10n for an app that will generate 206 // these specialized images from Android resources. 207 int res_create_localized_alpha_surface(const char* name, const char* locale, 208 GRSurface** pSurface); 209 210 // Return a list of locale strings embedded in |png_name|. Return a empty list in case of failure. 211 std::vector<std::string> get_locales_in_png(const std::string& png_name); 212 213 // Free a surface allocated by any of the res_create_*_surface() 214 // functions. 215 void res_free_surface(GRSurface* surface); 216