blob: 5470457e72b61783f3b991bb4e638c8876c68709 [file] [log] [blame]
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001/*
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 Bao92bdb5a2018-10-21 12:12:37 -070017#pragma once
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080018
Tao Bao92bdb5a2018-10-21 12:12:37 -070019#include <stdint.h>
Tao Bao9cf163e2018-11-07 10:15:50 -080020#include <stdlib.h>
Doug Zongker830b3e32014-03-11 13:22:04 -070021#include <sys/types.h>
22
Elliott Hughes07cfb8f2015-04-10 13:12:05 -070023#include <functional>
Tao Bao92bdb5a2018-10-21 12:12:37 -070024#include <memory>
Tianjie Xu2078b222017-03-22 12:27:26 -070025#include <string>
Tianjie Xu29d55752017-09-20 17:53:46 -070026#include <vector>
Doug Zongker28ce47c2011-10-28 10:33:05 -070027
Tao Bao710bc532018-10-24 07:59:49 -070028#include <android-base/macros.h>
Tao Bao835bf092019-03-11 14:13:21 -070029#include <android-base/unique_fd.h>
Tao Bao710bc532018-10-24 07:59:49 -070030
Elliott Hughes07138192015-04-10 09:40:53 -070031//
32// Graphics.
33//
34
Tao Bao92bdb5a2018-10-21 12:12:37 -070035class GRSurface {
36 public:
Tao Baodd789822018-11-26 16:28:07 -080037 static constexpr size_t kSurfaceDataAlignment = 8;
38
Tao Bao9cf163e2018-11-07 10:15:50 -080039 virtual ~GRSurface() = default;
Tao Bao92bdb5a2018-10-21 12:12:37 -070040
Tao Bao44820ac2018-10-30 23:34:50 -070041 // Creates and returns a GRSurface instance that's sufficient for storing an image of the given
Tao Baodd789822018-11-26 16:28:07 -080042 // 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);
Tao Bao92bdb5a2018-10-21 12:12:37 -070047
Tao Bao63b59dc2018-10-31 15:23:04 -070048 // Clones the current GRSurface instance (i.e. an image).
49 std::unique_ptr<GRSurface> Clone() const;
50
Tao Bao92bdb5a2018-10-21 12:12:37 -070051 virtual uint8_t* data() {
Tao Bao9cf163e2018-11-07 10:15:50 -080052 return data_.get();
Tao Bao92bdb5a2018-10-21 12:12:37 -070053 }
54
55 const uint8_t* data() const {
56 return const_cast<const uint8_t*>(const_cast<GRSurface*>(this)->data());
57 }
58
Tao Baodd789822018-11-26 16:28:07 -080059 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;
Tao Bao92bdb5a2018-10-21 12:12:37 -070067
Tao Bao44820ac2018-10-30 23:34:50 -070068 protected:
Tao Baodd789822018-11-26 16:28:07 -080069 GRSurface(size_t width, size_t height, size_t row_bytes, size_t pixel_bytes)
Tao Bao44820ac2018-10-30 23:34:50 -070070 : width(width), height(height), row_bytes(row_bytes), pixel_bytes(pixel_bytes) {}
71
Tao Bao92bdb5a2018-10-21 12:12:37 -070072 private:
Tao Bao9cf163e2018-11-07 10:15:50 -080073 // The deleter for data_, whose data is allocated via aligned_alloc(3).
74 struct DataDeleter {
75 void operator()(uint8_t* data) {
76 free(data);
77 }
78 };
79
80 std::unique_ptr<uint8_t, DataDeleter> data_;
Tao Bao63b59dc2018-10-31 15:23:04 -070081 size_t data_size_;
Tao Bao710bc532018-10-24 07:59:49 -070082
83 DISALLOW_COPY_AND_ASSIGN(GRSurface);
Elliott Hughes07cfb8f2015-04-10 13:12:05 -070084};
Doug Zongker39cf4172014-03-06 16:16:05 -080085
Damien Bargiacchi35fff612016-08-11 15:57:03 -070086struct GRFont {
Luke Song846012f2017-09-13 15:56:16 -070087 GRSurface* texture;
88 int char_width;
89 int char_height;
90};
91
Tao Bao44478df2018-07-31 22:01:03 -070092enum class GRRotation : int {
93 NONE = 0,
94 RIGHT = 1,
95 DOWN = 2,
96 LEFT = 3,
Damien Bargiacchi35fff612016-08-11 15:57:03 -070097};
98
Tao Baoed876a72018-07-31 21:32:50 -070099enum class PixelFormat : int {
100 UNKNOWN = 0,
101 ABGR = 1,
102 RGBX = 2,
103 BGRA = 3,
Adrian Salidocc7b7eb2019-12-12 20:18:09 -0800104 ARGB = 4,
Tao Baoed876a72018-07-31 21:32:50 -0700105};
106
Chihhang Chuang6f7362a2021-09-24 00:11:51 +0800107enum class GraphicsBackend : int {
108 UNKNOWN = 0,
109 DRM = 1,
110 FBDEV = 2,
111};
112
113// Initializes the default graphics backend and loads font file. Returns 0 on success, or -1 on
114// error. Note that the font initialization failure would be non-fatal, as caller may not need to
115// draw any text at all. Caller can check the font initialization result via gr_sys_font() as
116// needed.
Elliott Hughes07cfb8f2015-04-10 13:12:05 -0700117int gr_init();
Chihhang Chuang6f7362a2021-09-24 00:11:51 +0800118// Supports backend selection for minui client.
119int gr_init(std::initializer_list<GraphicsBackend> backends);
Tao Bao9f426332018-06-13 10:39:44 -0700120
121// Frees the allocated resources. The function is idempotent, and safe to be called if gr_init()
122// didn't finish successfully.
Elliott Hughes07cfb8f2015-04-10 13:12:05 -0700123void gr_exit();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800124
Elliott Hughes07cfb8f2015-04-10 13:12:05 -0700125int gr_fb_width();
126int gr_fb_height();
Doug Zongker39cf4172014-03-06 16:16:05 -0800127
Elliott Hughes07cfb8f2015-04-10 13:12:05 -0700128void gr_flip();
Dima Zavin4daf48a2011-08-30 11:59:20 -0700129void gr_fb_blank(bool blank);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800130
Tao Bao9f426332018-06-13 10:39:44 -0700131// Clears entire surface to current color.
132void gr_clear();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800133void gr_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a);
Doug Zongkerc560a672012-12-18 16:31:27 -0800134void gr_fill(int x1, int y1, int x2, int y2);
Damien Bargiacchi35fff612016-08-11 15:57:03 -0700135
Tao Bao92bdb5a2018-10-21 12:12:37 -0700136void gr_texticon(int x, int y, const GRSurface* icon);
Damien Bargiacchi35fff612016-08-11 15:57:03 -0700137
138const GRFont* gr_sys_font();
Damien Bargiacchid00f5eb2016-09-09 07:14:08 -0700139int gr_init_font(const char* name, GRFont** dest);
Luke Song846012f2017-09-13 15:56:16 -0700140void gr_text(const GRFont* font, int x, int y, const char* s, bool bold);
Tao Bao9f426332018-06-13 10:39:44 -0700141// Returns -1 if font is nullptr.
Luke Song846012f2017-09-13 15:56:16 -0700142int gr_measure(const GRFont* font, const char* s);
Tao Bao9f426332018-06-13 10:39:44 -0700143// Returns -1 if font is nullptr.
Tianjie Xu842f2a32018-05-31 18:16:28 -0700144int gr_font_size(const GRFont* font, int* x, int* y);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800145
Tao Bao92bdb5a2018-10-21 12:12:37 -0700146void gr_blit(const GRSurface* source, int sx, int sy, int w, int h, int dx, int dy);
Tao Bao9f426332018-06-13 10:39:44 -0700147unsigned int gr_get_width(const GRSurface* surface);
148unsigned int gr_get_height(const GRSurface* surface);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800149
Tao Bao9f426332018-06-13 10:39:44 -0700150// Sets rotation, flips gr_fb_width/height if 90 degree rotation difference
Luke Song846012f2017-09-13 15:56:16 -0700151void gr_rotate(GRRotation rotation);
152
Tao Baoed876a72018-07-31 21:32:50 -0700153// Returns the current PixelFormat being used.
154PixelFormat gr_pixel_format();
155
Elliott Hughes07138192015-04-10 09:40:53 -0700156//
157// Input events.
158//
159
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800160struct input_event;
161
Tao Bao0b1118d2017-01-14 07:46:10 -0800162using ev_callback = std::function<int(int fd, uint32_t epevents)>;
163using ev_set_key_callback = std::function<int(int code, int value)>;
Dima Zavinbc290632011-08-30 11:59:45 -0700164
Tao Bao5f8dd992017-07-28 00:05:40 -0700165int ev_init(ev_callback input_cb, bool allow_touch_inputs = false);
Elliott Hughes07cfb8f2015-04-10 13:12:05 -0700166void ev_exit();
Tao Bao835bf092019-03-11 14:13:21 -0700167int ev_add_fd(android::base::unique_fd&& fd, ev_callback cb);
Chih-Hung Hsieh23abfd32016-07-27 10:19:47 -0700168void ev_iterate_available_keys(const std::function<void(int)>& f);
Tao Bao5f8dd992017-07-28 00:05:40 -0700169void ev_iterate_touch_inputs(const std::function<void(int)>& action);
Tao Bao0b1118d2017-01-14 07:46:10 -0800170int ev_sync_key_state(const ev_set_key_callback& set_key_cb);
Dima Zavinbc290632011-08-30 11:59:45 -0700171
Elliott Hughes07138192015-04-10 09:40:53 -0700172// 'timeout' has the same semantics as poll(2).
173// 0 : don't block
174// < 0 : block forever
175// > 0 : block for 'timeout' milliseconds
Dima Zavinbc290632011-08-30 11:59:45 -0700176int ev_wait(int timeout);
177
Elliott Hughes07cfb8f2015-04-10 13:12:05 -0700178int ev_get_input(int fd, uint32_t epevents, input_event* ev);
179void ev_dispatch();
180int ev_get_epollfd();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800181
Elliott Hughes07138192015-04-10 09:40:53 -0700182//
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800183// Resources
Elliott Hughes07138192015-04-10 09:40:53 -0700184//
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800185
Tianjie Xu2078b222017-03-22 12:27:26 -0700186bool matches_locale(const std::string& prefix, const std::string& locale);
Tianjie Xu2430e292016-04-19 15:02:41 -0700187
Doug Zongkera418aa72014-03-17 12:10:02 -0700188// res_create_*_surface() functions return 0 if no error, else
189// negative.
190//
191// A "display" surface is one that is intended to be drawn to the
192// screen with gr_blit(). An "alpha" surface is a grayscale image
193// interpreted as an alpha mask used to render text in the current
194// color (with gr_text() or gr_texticon()).
195//
196// All these functions load PNG images from "/res/images/${name}.png".
197
198// Load a single display surface from a PNG image.
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700199int res_create_display_surface(const char* name, GRSurface** pSurface);
Doug Zongkera418aa72014-03-17 12:10:02 -0700200
201// Load an array of display surfaces from a single PNG image. The PNG
202// should have a 'Frames' text chunk whose value is the number of
203// frames this image represents. The pixel data itself is interlaced
204// by row.
Luke Song846012f2017-09-13 15:56:16 -0700205int res_create_multi_display_surface(const char* name, int* frames, int* fps,
206 GRSurface*** pSurface);
Doug Zongkera418aa72014-03-17 12:10:02 -0700207
208// Load a single alpha surface from a grayscale PNG image.
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700209int res_create_alpha_surface(const char* name, GRSurface** pSurface);
Doug Zongkera418aa72014-03-17 12:10:02 -0700210
211// Load part of a grayscale PNG image that is the first match for the
212// given locale. The image is expected to be a composite of multiple
213// translations of the same text, with special added rows that encode
214// the subimages' size and intended locale in the pixel data. See
Tianjie Xu9a259772016-07-28 14:15:00 -0700215// bootable/recovery/tools/recovery_l10n for an app that will generate
216// these specialized images from Android resources.
Doug Zongkera418aa72014-03-17 12:10:02 -0700217int res_create_localized_alpha_surface(const char* name, const char* locale,
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700218 GRSurface** pSurface);
Doug Zongkera418aa72014-03-17 12:10:02 -0700219
Tianjie Xu29d55752017-09-20 17:53:46 -0700220// Return a list of locale strings embedded in |png_name|. Return a empty list in case of failure.
221std::vector<std::string> get_locales_in_png(const std::string& png_name);
222
Doug Zongkera418aa72014-03-17 12:10:02 -0700223// Free a surface allocated by any of the res_create_*_surface()
224// functions.
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700225void res_free_surface(GRSurface* surface);