blob: e49c6ac97d2b3b529b85816b7bf934d0f9d81748 [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>
29
Elliott Hughes07138192015-04-10 09:40:53 -070030//
31// Graphics.
32//
33
Tao Bao92bdb5a2018-10-21 12:12:37 -070034class GRSurface {
35 public:
Tao Baodd789822018-11-26 16:28:07 -080036 static constexpr size_t kSurfaceDataAlignment = 8;
37
Tao Bao9cf163e2018-11-07 10:15:50 -080038 virtual ~GRSurface() = default;
Tao Bao92bdb5a2018-10-21 12:12:37 -070039
Tao Bao44820ac2018-10-30 23:34:50 -070040 // Creates and returns a GRSurface instance that's sufficient for storing an image of the given
Tao Baodd789822018-11-26 16:28:07 -080041 // 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 Bao92bdb5a2018-10-21 12:12:37 -070046
Tao Bao63b59dc2018-10-31 15:23:04 -070047 // Clones the current GRSurface instance (i.e. an image).
48 std::unique_ptr<GRSurface> Clone() const;
49
Tao Bao92bdb5a2018-10-21 12:12:37 -070050 virtual uint8_t* data() {
Tao Bao9cf163e2018-11-07 10:15:50 -080051 return data_.get();
Tao Bao92bdb5a2018-10-21 12:12:37 -070052 }
53
54 const uint8_t* data() const {
55 return const_cast<const uint8_t*>(const_cast<GRSurface*>(this)->data());
56 }
57
Tao Baodd789822018-11-26 16:28:07 -080058 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 Bao92bdb5a2018-10-21 12:12:37 -070066
Tao Bao44820ac2018-10-30 23:34:50 -070067 protected:
Tao Baodd789822018-11-26 16:28:07 -080068 GRSurface(size_t width, size_t height, size_t row_bytes, size_t pixel_bytes)
Tao Bao44820ac2018-10-30 23:34:50 -070069 : width(width), height(height), row_bytes(row_bytes), pixel_bytes(pixel_bytes) {}
70
Tao Bao92bdb5a2018-10-21 12:12:37 -070071 private:
Tao Bao9cf163e2018-11-07 10:15:50 -080072 // 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 Bao63b59dc2018-10-31 15:23:04 -070080 size_t data_size_;
Tao Bao710bc532018-10-24 07:59:49 -070081
82 DISALLOW_COPY_AND_ASSIGN(GRSurface);
Elliott Hughes07cfb8f2015-04-10 13:12:05 -070083};
Doug Zongker39cf4172014-03-06 16:16:05 -080084
Damien Bargiacchi35fff612016-08-11 15:57:03 -070085struct GRFont {
Luke Song846012f2017-09-13 15:56:16 -070086 GRSurface* texture;
87 int char_width;
88 int char_height;
89};
90
Tao Bao44478df2018-07-31 22:01:03 -070091enum class GRRotation : int {
92 NONE = 0,
93 RIGHT = 1,
94 DOWN = 2,
95 LEFT = 3,
Damien Bargiacchi35fff612016-08-11 15:57:03 -070096};
97
Tao Baoed876a72018-07-31 21:32:50 -070098enum class PixelFormat : int {
99 UNKNOWN = 0,
100 ABGR = 1,
101 RGBX = 2,
102 BGRA = 3,
103};
104
Tao Bao9f426332018-06-13 10:39:44 -0700105// 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 Hughes07cfb8f2015-04-10 13:12:05 -0700108int gr_init();
Tao Bao9f426332018-06-13 10:39:44 -0700109
110// Frees the allocated resources. The function is idempotent, and safe to be called if gr_init()
111// didn't finish successfully.
Elliott Hughes07cfb8f2015-04-10 13:12:05 -0700112void gr_exit();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800113
Elliott Hughes07cfb8f2015-04-10 13:12:05 -0700114int gr_fb_width();
115int gr_fb_height();
Doug Zongker39cf4172014-03-06 16:16:05 -0800116
Elliott Hughes07cfb8f2015-04-10 13:12:05 -0700117void gr_flip();
Dima Zavin4daf48a2011-08-30 11:59:20 -0700118void gr_fb_blank(bool blank);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800119
Tao Bao9f426332018-06-13 10:39:44 -0700120// Clears entire surface to current color.
121void gr_clear();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800122void gr_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a);
Doug Zongkerc560a672012-12-18 16:31:27 -0800123void gr_fill(int x1, int y1, int x2, int y2);
Damien Bargiacchi35fff612016-08-11 15:57:03 -0700124
Tao Bao92bdb5a2018-10-21 12:12:37 -0700125void gr_texticon(int x, int y, const GRSurface* icon);
Damien Bargiacchi35fff612016-08-11 15:57:03 -0700126
127const GRFont* gr_sys_font();
Damien Bargiacchid00f5eb2016-09-09 07:14:08 -0700128int gr_init_font(const char* name, GRFont** dest);
Luke Song846012f2017-09-13 15:56:16 -0700129void gr_text(const GRFont* font, int x, int y, const char* s, bool bold);
Tao Bao9f426332018-06-13 10:39:44 -0700130// Returns -1 if font is nullptr.
Luke Song846012f2017-09-13 15:56:16 -0700131int gr_measure(const GRFont* font, const char* s);
Tao Bao9f426332018-06-13 10:39:44 -0700132// Returns -1 if font is nullptr.
Tianjie Xu842f2a32018-05-31 18:16:28 -0700133int gr_font_size(const GRFont* font, int* x, int* y);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800134
Tao Bao92bdb5a2018-10-21 12:12:37 -0700135void gr_blit(const GRSurface* source, int sx, int sy, int w, int h, int dx, int dy);
Tao Bao9f426332018-06-13 10:39:44 -0700136unsigned int gr_get_width(const GRSurface* surface);
137unsigned int gr_get_height(const GRSurface* surface);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800138
Tao Bao9f426332018-06-13 10:39:44 -0700139// Sets rotation, flips gr_fb_width/height if 90 degree rotation difference
Luke Song846012f2017-09-13 15:56:16 -0700140void gr_rotate(GRRotation rotation);
141
Tao Baoed876a72018-07-31 21:32:50 -0700142// Returns the current PixelFormat being used.
143PixelFormat gr_pixel_format();
144
Elliott Hughes07138192015-04-10 09:40:53 -0700145//
146// Input events.
147//
148
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800149struct input_event;
150
Tao Bao0b1118d2017-01-14 07:46:10 -0800151using ev_callback = std::function<int(int fd, uint32_t epevents)>;
152using ev_set_key_callback = std::function<int(int code, int value)>;
Dima Zavinbc290632011-08-30 11:59:45 -0700153
Tao Bao5f8dd992017-07-28 00:05:40 -0700154int ev_init(ev_callback input_cb, bool allow_touch_inputs = false);
Elliott Hughes07cfb8f2015-04-10 13:12:05 -0700155void ev_exit();
Tao Bao0b1118d2017-01-14 07:46:10 -0800156int ev_add_fd(int fd, ev_callback cb);
Chih-Hung Hsieh23abfd32016-07-27 10:19:47 -0700157void ev_iterate_available_keys(const std::function<void(int)>& f);
Tao Bao5f8dd992017-07-28 00:05:40 -0700158void ev_iterate_touch_inputs(const std::function<void(int)>& action);
Tao Bao0b1118d2017-01-14 07:46:10 -0800159int ev_sync_key_state(const ev_set_key_callback& set_key_cb);
Dima Zavinbc290632011-08-30 11:59:45 -0700160
Elliott Hughes07138192015-04-10 09:40:53 -0700161// 'timeout' has the same semantics as poll(2).
162// 0 : don't block
163// < 0 : block forever
164// > 0 : block for 'timeout' milliseconds
Dima Zavinbc290632011-08-30 11:59:45 -0700165int ev_wait(int timeout);
166
Elliott Hughes07cfb8f2015-04-10 13:12:05 -0700167int ev_get_input(int fd, uint32_t epevents, input_event* ev);
168void ev_dispatch();
169int ev_get_epollfd();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800170
Elliott Hughes07138192015-04-10 09:40:53 -0700171//
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800172// Resources
Elliott Hughes07138192015-04-10 09:40:53 -0700173//
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800174
Tianjie Xu2078b222017-03-22 12:27:26 -0700175bool matches_locale(const std::string& prefix, const std::string& locale);
Tianjie Xu2430e292016-04-19 15:02:41 -0700176
Doug Zongkera418aa72014-03-17 12:10:02 -0700177// 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 Hughes0a5cb0c2015-04-15 10:58:56 -0700188int res_create_display_surface(const char* name, GRSurface** pSurface);
Doug Zongkera418aa72014-03-17 12:10:02 -0700189
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 Song846012f2017-09-13 15:56:16 -0700194int res_create_multi_display_surface(const char* name, int* frames, int* fps,
195 GRSurface*** pSurface);
Doug Zongkera418aa72014-03-17 12:10:02 -0700196
197// Load a single alpha surface from a grayscale PNG image.
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700198int res_create_alpha_surface(const char* name, GRSurface** pSurface);
Doug Zongkera418aa72014-03-17 12:10:02 -0700199
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 Xu9a259772016-07-28 14:15:00 -0700204// bootable/recovery/tools/recovery_l10n for an app that will generate
205// these specialized images from Android resources.
Doug Zongkera418aa72014-03-17 12:10:02 -0700206int res_create_localized_alpha_surface(const char* name, const char* locale,
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700207 GRSurface** pSurface);
Doug Zongkera418aa72014-03-17 12:10:02 -0700208
Tianjie Xu29d55752017-09-20 17:53:46 -0700209// Return a list of locale strings embedded in |png_name|. Return a empty list in case of failure.
210std::vector<std::string> get_locales_in_png(const std::string& png_name);
211
Doug Zongkera418aa72014-03-17 12:10:02 -0700212// Free a surface allocated by any of the res_create_*_surface()
213// functions.
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700214void res_free_surface(GRSurface* surface);