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