The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 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 | |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 17 | #include <fcntl.h> |
| 18 | #include <linux/fb.h> |
| 19 | #include <linux/kd.h> |
| 20 | #include <stdio.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 21 | #include <stdlib.h> |
Elliott Hughes | cd3c55a | 2015-01-29 20:50:08 -0800 | [diff] [blame] | 22 | #include <string.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 23 | #include <sys/ioctl.h> |
| 24 | #include <sys/mman.h> |
| 25 | #include <sys/types.h> |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 26 | #include <unistd.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 27 | |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 28 | #include <memory> |
Tianjie Xu | 0a59956 | 2017-03-28 20:11:15 +0000 | [diff] [blame] | 29 | #include <regex> |
| 30 | #include <string> |
Yabin Cui | 4425c1d | 2016-02-10 13:47:32 -0800 | [diff] [blame] | 31 | #include <vector> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 32 | |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 33 | //#include <android-base/stringprintf.h> // does not exist in 6.0 |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 34 | //#include <android-base/strings.h> // does not exist in 6.0 |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 35 | #include <png.h> |
| 36 | |
Tao Bao | 0ecbd76 | 2017-01-16 21:16:58 -0800 | [diff] [blame] | 37 | #include "minui/minui.h" |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 38 | |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 39 | #define SURFACE_DATA_ALIGNMENT 8 |
| 40 | |
Elliott Hughes | 0a5cb0c | 2015-04-15 10:58:56 -0700 | [diff] [blame] | 41 | static GRSurface* malloc_surface(size_t data_size) { |
Elliott Hughes | 07cfb8f | 2015-04-10 13:12:05 -0700 | [diff] [blame] | 42 | size_t size = sizeof(GRSurface) + data_size + SURFACE_DATA_ALIGNMENT; |
Rahul Chaudhry | b29f23f | 2016-11-09 13:17:01 -0800 | [diff] [blame] | 43 | unsigned char* temp = static_cast<unsigned char*>(malloc(size)); |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 44 | if (temp == NULL) return NULL; |
Elliott Hughes | 0a5cb0c | 2015-04-15 10:58:56 -0700 | [diff] [blame] | 45 | GRSurface* surface = reinterpret_cast<GRSurface*>(temp); |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 46 | surface->data = temp + sizeof(GRSurface) + |
| 47 | (SURFACE_DATA_ALIGNMENT - (sizeof(GRSurface) % SURFACE_DATA_ALIGNMENT)); |
| 48 | return surface; |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 49 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 50 | |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 51 | // This class handles the png file parsing. It also holds the ownership of the png pointer and the |
| 52 | // opened file pointer. Both will be destroyed/closed when this object goes out of scope. |
| 53 | class PngHandler { |
| 54 | public: |
| 55 | PngHandler(const std::string& name); |
Doug Zongker | 6809c51 | 2011-03-01 14:04:34 -0800 | [diff] [blame] | 56 | |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 57 | ~PngHandler(); |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 58 | |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 59 | png_uint_32 width() const { |
| 60 | return width_; |
| 61 | } |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 62 | |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 63 | png_uint_32 height() const { |
| 64 | return height_; |
| 65 | } |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 66 | |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 67 | png_byte channels() const { |
| 68 | return channels_; |
| 69 | } |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 70 | |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 71 | png_structp png_ptr() const { |
| 72 | return png_ptr_; |
| 73 | } |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 74 | |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 75 | png_infop info_ptr() const { |
| 76 | return info_ptr_; |
| 77 | } |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 78 | |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 79 | int error_code() const { |
| 80 | return error_code_; |
| 81 | }; |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 82 | |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 83 | operator bool() const { |
| 84 | return error_code_ == 0; |
| 85 | } |
John Reck | 94fd07b | 2013-08-26 16:45:33 -0700 | [diff] [blame] | 86 | |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 87 | private: |
| 88 | png_structp png_ptr_{ nullptr }; |
| 89 | png_infop info_ptr_{ nullptr }; |
| 90 | png_uint_32 width_; |
| 91 | png_uint_32 height_; |
| 92 | png_byte channels_; |
John Reck | 94fd07b | 2013-08-26 16:45:33 -0700 | [diff] [blame] | 93 | |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 94 | // The |error_code_| is set to a negative value if an error occurs when opening the png file. |
| 95 | int error_code_; |
| 96 | // After initialization, we'll keep the file pointer open before destruction of PngHandler. |
| 97 | std::unique_ptr<FILE, decltype(&fclose)> png_fp_; |
| 98 | }; |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 99 | |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 100 | PngHandler::PngHandler(const std::string& name) : error_code_(0), png_fp_(nullptr, fclose) { |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 101 | char res_path[PATH_MAX]; |
| 102 | sprintf(res_path, "/res/images/%s.png", name.c_str()); |
| 103 | //std::string res_path = sprintf("/res/images/%s.png", name.c_str()); |
| 104 | png_fp_.reset(fopen(res_path, "rbe")); |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 105 | if (!png_fp_) { |
| 106 | error_code_ = -1; |
| 107 | return; |
| 108 | } |
Doug Zongker | 55a36ac | 2013-03-04 15:49:02 -0800 | [diff] [blame] | 109 | |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 110 | unsigned char header[8]; |
| 111 | size_t bytesRead = fread(header, 1, sizeof(header), png_fp_.get()); |
| 112 | if (bytesRead != sizeof(header)) { |
| 113 | error_code_ = -2; |
| 114 | return; |
| 115 | } |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 116 | |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 117 | if (png_sig_cmp(header, 0, sizeof(header))) { |
| 118 | error_code_ = -3; |
| 119 | return; |
| 120 | } |
| 121 | |
| 122 | png_ptr_ = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr); |
| 123 | if (!png_ptr_) { |
| 124 | error_code_ = -4; |
| 125 | return; |
| 126 | } |
| 127 | |
| 128 | info_ptr_ = png_create_info_struct(png_ptr_); |
| 129 | if (!info_ptr_) { |
| 130 | error_code_ = -5; |
| 131 | return; |
| 132 | } |
| 133 | |
| 134 | if (setjmp(png_jmpbuf(png_ptr_))) { |
| 135 | error_code_ = -6; |
| 136 | return; |
| 137 | } |
| 138 | |
| 139 | png_init_io(png_ptr_, png_fp_.get()); |
| 140 | png_set_sig_bytes(png_ptr_, sizeof(header)); |
| 141 | png_read_info(png_ptr_, info_ptr_); |
| 142 | |
| 143 | int color_type; |
| 144 | int bit_depth; |
| 145 | png_get_IHDR(png_ptr_, info_ptr_, &width_, &height_, &bit_depth, &color_type, nullptr, nullptr, |
| 146 | nullptr); |
| 147 | |
| 148 | channels_ = png_get_channels(png_ptr_, info_ptr_); |
| 149 | |
| 150 | if (bit_depth == 8 && channels_ == 3 && color_type == PNG_COLOR_TYPE_RGB) { |
| 151 | // 8-bit RGB images: great, nothing to do. |
| 152 | } else if (bit_depth <= 8 && channels_ == 1 && color_type == PNG_COLOR_TYPE_GRAY) { |
| 153 | // 1-, 2-, 4-, or 8-bit gray images: expand to 8-bit gray. |
| 154 | png_set_expand_gray_1_2_4_to_8(png_ptr_); |
| 155 | } else if (bit_depth <= 8 && channels_ == 1 && color_type == PNG_COLOR_TYPE_PALETTE) { |
| 156 | // paletted images: expand to 8-bit RGB. Note that we DON'T |
| 157 | // currently expand the tRNS chunk (if any) to an alpha |
| 158 | // channel, because minui doesn't support alpha channels in |
| 159 | // general. |
| 160 | png_set_palette_to_rgb(png_ptr_); |
| 161 | channels_ = 3; |
| 162 | } else { |
| 163 | fprintf(stderr, "minui doesn't support PNG depth %d channels %d color_type %d\n", bit_depth, |
| 164 | channels_, color_type); |
| 165 | error_code_ = -7; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | PngHandler::~PngHandler() { |
| 170 | if (png_ptr_) { |
| 171 | png_destroy_read_struct(&png_ptr_, &info_ptr_, nullptr); |
| 172 | } |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | // "display" surfaces are transformed into the framebuffer's required |
| 176 | // pixel format (currently only RGBX is supported) at load time, so |
| 177 | // gr_blit() can be nothing more than a memcpy() for each row. The |
| 178 | // next two functions are the only ones that know anything about the |
| 179 | // framebuffer pixel format; they need to be modified if the |
| 180 | // framebuffer format changes (but nothing else should). |
| 181 | |
Elliott Hughes | 0a5cb0c | 2015-04-15 10:58:56 -0700 | [diff] [blame] | 182 | // Allocate and return a GRSurface* sufficient for storing an image of |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 183 | // the indicated size in the framebuffer pixel format. |
Elliott Hughes | 0a5cb0c | 2015-04-15 10:58:56 -0700 | [diff] [blame] | 184 | static GRSurface* init_display_surface(png_uint_32 width, png_uint_32 height) { |
| 185 | GRSurface* surface = malloc_surface(width * height * 4); |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 186 | if (surface == NULL) return NULL; |
| 187 | |
| 188 | surface->width = width; |
| 189 | surface->height = height; |
| 190 | surface->row_bytes = width * 4; |
| 191 | surface->pixel_bytes = 4; |
| 192 | |
| 193 | return surface; |
| 194 | } |
| 195 | |
| 196 | // Copy 'input_row' to 'output_row', transforming it to the |
| 197 | // framebuffer pixel format. The input format depends on the value of |
| 198 | // 'channels': |
| 199 | // |
| 200 | // 1 - input is 8-bit grayscale |
| 201 | // 3 - input is 24-bit RGB |
| 202 | // 4 - input is 32-bit RGBA/RGBX |
| 203 | // |
| 204 | // 'width' is the number of pixels in the row. |
| 205 | static void transform_rgb_to_draw(unsigned char* input_row, |
| 206 | unsigned char* output_row, |
| 207 | int channels, int width) { |
| 208 | int x; |
| 209 | unsigned char* ip = input_row; |
| 210 | unsigned char* op = output_row; |
| 211 | |
| 212 | switch (channels) { |
| 213 | case 1: |
| 214 | // expand gray level to RGBX |
| 215 | for (x = 0; x < width; ++x) { |
| 216 | *op++ = *ip; |
| 217 | *op++ = *ip; |
| 218 | *op++ = *ip; |
| 219 | *op++ = 0xff; |
| 220 | ip++; |
| 221 | } |
| 222 | break; |
| 223 | |
| 224 | case 3: |
| 225 | // expand RGBA to RGBX |
| 226 | for (x = 0; x < width; ++x) { |
| 227 | *op++ = *ip++; |
| 228 | *op++ = *ip++; |
| 229 | *op++ = *ip++; |
| 230 | *op++ = 0xff; |
| 231 | } |
| 232 | break; |
| 233 | |
| 234 | case 4: |
| 235 | // copy RGBA to RGBX |
| 236 | memcpy(output_row, input_row, width*4); |
| 237 | break; |
| 238 | } |
| 239 | } |
| 240 | |
Elliott Hughes | 0a5cb0c | 2015-04-15 10:58:56 -0700 | [diff] [blame] | 241 | int res_create_display_surface(const char* name, GRSurface** pSurface) { |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 242 | *pSurface = nullptr; |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 243 | |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 244 | PngHandler png_handler(name); |
| 245 | if (!png_handler) return png_handler.error_code(); |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 246 | |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 247 | png_structp png_ptr = png_handler.png_ptr(); |
| 248 | png_uint_32 width = png_handler.width(); |
| 249 | png_uint_32 height = png_handler.height(); |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 250 | |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 251 | GRSurface* surface = init_display_surface(width, height); |
| 252 | if (!surface) { |
| 253 | return -8; |
| 254 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 255 | |
Tony Kuo | fd778e3 | 2015-02-05 21:25:56 +0800 | [diff] [blame] | 256 | #if defined(RECOVERY_ABGR) || defined(RECOVERY_BGRA) |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 257 | png_set_bgr(png_ptr); |
Tony Kuo | fd778e3 | 2015-02-05 21:25:56 +0800 | [diff] [blame] | 258 | #endif |
| 259 | |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 260 | for (png_uint_32 y = 0; y < height; ++y) { |
| 261 | std::vector<unsigned char> p_row(width * 4); |
| 262 | png_read_row(png_ptr, p_row.data(), nullptr); |
| 263 | transform_rgb_to_draw(p_row.data(), surface->data + y * surface->row_bytes, |
| 264 | png_handler.channels(), width); |
| 265 | } |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 266 | |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 267 | *pSurface = surface; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 268 | |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 269 | return 0; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 270 | } |
| 271 | |
Tao Bao | b723f4f | 2015-12-11 15:18:51 -0800 | [diff] [blame] | 272 | int res_create_multi_display_surface(const char* name, int* frames, int* fps, |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 273 | GRSurface*** pSurface) { |
| 274 | *pSurface = nullptr; |
| 275 | *frames = -1; |
Doug Zongker | 469954f | 2014-03-07 09:21:25 -0800 | [diff] [blame] | 276 | |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 277 | PngHandler png_handler(name); |
| 278 | if (!png_handler) return png_handler.error_code(); |
Doug Zongker | 469954f | 2014-03-07 09:21:25 -0800 | [diff] [blame] | 279 | |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 280 | png_structp png_ptr = png_handler.png_ptr(); |
| 281 | png_uint_32 width = png_handler.width(); |
| 282 | png_uint_32 height = png_handler.height(); |
Doug Zongker | 469954f | 2014-03-07 09:21:25 -0800 | [diff] [blame] | 283 | |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 284 | *frames = 1; |
| 285 | *fps = 20; |
| 286 | png_textp text; |
| 287 | int num_text; |
| 288 | if (png_get_text(png_ptr, png_handler.info_ptr(), &text, &num_text)) { |
| 289 | for (int i = 0; i < num_text; ++i) { |
| 290 | if (text[i].key && strcmp(text[i].key, "Frames") == 0 && text[i].text) { |
| 291 | *frames = atoi(text[i].text); |
| 292 | } else if (text[i].key && strcmp(text[i].key, "FPS") == 0 && text[i].text) { |
| 293 | *fps = atoi(text[i].text); |
| 294 | } |
Tao Bao | b723f4f | 2015-12-11 15:18:51 -0800 | [diff] [blame] | 295 | } |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 296 | printf(" found frames = %d\n", *frames); |
| 297 | printf(" found fps = %d\n", *fps); |
| 298 | } |
Tao Bao | b723f4f | 2015-12-11 15:18:51 -0800 | [diff] [blame] | 299 | |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 300 | int result = 0; |
| 301 | GRSurface** surface = nullptr; |
| 302 | if (*frames <= 0 || *fps <= 0) { |
| 303 | printf("bad number of frames (%d) and/or FPS (%d)\n", *frames, *fps); |
| 304 | result = -10; |
| 305 | goto exit; |
| 306 | } |
Doug Zongker | 469954f | 2014-03-07 09:21:25 -0800 | [diff] [blame] | 307 | |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 308 | if (height % *frames != 0) { |
| 309 | printf("bad height (%d) for frame count (%d)\n", height, *frames); |
| 310 | result = -9; |
| 311 | goto exit; |
| 312 | } |
Doug Zongker | 469954f | 2014-03-07 09:21:25 -0800 | [diff] [blame] | 313 | |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 314 | surface = static_cast<GRSurface**>(calloc(*frames, sizeof(GRSurface*))); |
| 315 | if (!surface) { |
| 316 | result = -8; |
| 317 | goto exit; |
| 318 | } |
| 319 | for (int i = 0; i < *frames; ++i) { |
| 320 | surface[i] = init_display_surface(width, height / *frames); |
| 321 | if (!surface[i]) { |
| 322 | result = -8; |
| 323 | goto exit; |
Doug Zongker | 469954f | 2014-03-07 09:21:25 -0800 | [diff] [blame] | 324 | } |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 325 | } |
Doug Zongker | 469954f | 2014-03-07 09:21:25 -0800 | [diff] [blame] | 326 | |
Tony Kuo | fd778e3 | 2015-02-05 21:25:56 +0800 | [diff] [blame] | 327 | #if defined(RECOVERY_ABGR) || defined(RECOVERY_BGRA) |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 328 | png_set_bgr(png_ptr); |
Tony Kuo | fd778e3 | 2015-02-05 21:25:56 +0800 | [diff] [blame] | 329 | #endif |
| 330 | |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 331 | for (png_uint_32 y = 0; y < height; ++y) { |
| 332 | std::vector<unsigned char> p_row(width * 4); |
| 333 | png_read_row(png_ptr, p_row.data(), nullptr); |
| 334 | int frame = y % *frames; |
| 335 | unsigned char* out_row = surface[frame]->data + (y / *frames) * surface[frame]->row_bytes; |
| 336 | transform_rgb_to_draw(p_row.data(), out_row, png_handler.channels(), width); |
| 337 | } |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 338 | |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 339 | *pSurface = surface; |
Doug Zongker | 469954f | 2014-03-07 09:21:25 -0800 | [diff] [blame] | 340 | |
| 341 | exit: |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 342 | if (result < 0) { |
| 343 | if (surface) { |
| 344 | for (int i = 0; i < *frames; ++i) { |
| 345 | free(surface[i]); |
| 346 | } |
| 347 | free(surface); |
Doug Zongker | 469954f | 2014-03-07 09:21:25 -0800 | [diff] [blame] | 348 | } |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 349 | } |
| 350 | return result; |
Doug Zongker | 469954f | 2014-03-07 09:21:25 -0800 | [diff] [blame] | 351 | } |
| 352 | |
Ethan Yonker | 534d4e0 | 2016-08-26 10:05:03 -0500 | [diff] [blame] | 353 | int res_create_multi_display_surface(const char* name, int* frames, |
| 354 | GRSurface*** pSurface) { |
| 355 | int fps = 0; |
| 356 | return res_create_multi_display_surface(name, frames, &fps, pSurface); |
| 357 | } |
| 358 | |
Elliott Hughes | 0a5cb0c | 2015-04-15 10:58:56 -0700 | [diff] [blame] | 359 | int res_create_alpha_surface(const char* name, GRSurface** pSurface) { |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 360 | *pSurface = nullptr; |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 361 | |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 362 | PngHandler png_handler(name); |
| 363 | if (!png_handler) return png_handler.error_code(); |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 364 | |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 365 | if (png_handler.channels() != 1) { |
| 366 | return -7; |
| 367 | } |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 368 | |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 369 | png_structp png_ptr = png_handler.png_ptr(); |
| 370 | png_uint_32 width = png_handler.width(); |
| 371 | png_uint_32 height = png_handler.height(); |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 372 | |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 373 | GRSurface* surface = malloc_surface(width * height); |
| 374 | if (!surface) { |
| 375 | return -8; |
| 376 | } |
| 377 | surface->width = width; |
| 378 | surface->height = height; |
| 379 | surface->row_bytes = width; |
| 380 | surface->pixel_bytes = 1; |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 381 | |
Tony Kuo | fd778e3 | 2015-02-05 21:25:56 +0800 | [diff] [blame] | 382 | #if defined(RECOVERY_ABGR) || defined(RECOVERY_BGRA) |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 383 | png_set_bgr(png_ptr); |
Tony Kuo | fd778e3 | 2015-02-05 21:25:56 +0800 | [diff] [blame] | 384 | #endif |
| 385 | |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 386 | for (png_uint_32 y = 0; y < height; ++y) { |
| 387 | unsigned char* p_row = surface->data + y * surface->row_bytes; |
| 388 | png_read_row(png_ptr, p_row, nullptr); |
| 389 | } |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 390 | |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 391 | *pSurface = surface; |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 392 | |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 393 | return 0; |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 394 | } |
| 395 | |
Tianjie Xu | 2430e29 | 2016-04-19 15:02:41 -0700 | [diff] [blame] | 396 | // This function tests if a locale string stored in PNG (prefix) matches |
| 397 | // the locale string provided by the system (locale). |
Tianjie Xu | 0a59956 | 2017-03-28 20:11:15 +0000 | [diff] [blame] | 398 | bool matches_locale(const std::string& prefix, const std::string& locale) { |
| 399 | // According to the BCP 47 format, A locale string may consists of: |
| 400 | // language-{extlang}-{script}-{region}-{variant} |
| 401 | // The locale headers in PNG mostly consist of language-{region} except for sr-Latn, and some |
| 402 | // android's system locale can have the format language-{script}-{region}. |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 403 | |
Tianjie Xu | 0a59956 | 2017-03-28 20:11:15 +0000 | [diff] [blame] | 404 | // Return true if the whole string of prefix matches the top part of locale. Otherwise try to |
| 405 | // match the locale string without the {script} section. |
| 406 | // For instance, prefix == "en" matches locale == "en-US", prefix == "sr-Latn" matches locale |
| 407 | // == "sr-Latn-BA", and prefix == "zh-CN" matches locale == "zh-Hans-CN". |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 408 | //if (android::base::StartsWith(locale, prefix)) { // does not exist in 6.0 |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 409 | if (strncmp(prefix.c_str(), locale.c_str(), prefix.length()) == 0) { |
Tianjie Xu | 0a59956 | 2017-03-28 20:11:15 +0000 | [diff] [blame] | 410 | return true; |
| 411 | } |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 412 | |
Tianjie Xu | 0a59956 | 2017-03-28 20:11:15 +0000 | [diff] [blame] | 413 | size_t separator = prefix.find('-'); |
| 414 | if (separator == std::string::npos) { |
| 415 | return false; |
| 416 | } |
| 417 | std::regex loc_regex(prefix.substr(0, separator) + "-[A-Za-z]*" + prefix.substr(separator)); |
| 418 | return std::regex_match(locale, loc_regex); |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 419 | } |
| 420 | |
Tianjie Xu | 29d5575 | 2017-09-20 17:53:46 -0700 | [diff] [blame] | 421 | std::vector<std::string> get_locales_in_png(const std::string& png_name) { |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 422 | PngHandler png_handler(png_name); |
| 423 | if (!png_handler) { |
| 424 | printf("Failed to open %s, error: %d\n", png_name.c_str(), png_handler.error_code()); |
Tianjie Xu | 29d5575 | 2017-09-20 17:53:46 -0700 | [diff] [blame] | 425 | return {}; |
| 426 | } |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 427 | if (png_handler.channels() != 1) { |
| 428 | printf("Expect input png to have 1 data channel, this file has %d\n", png_handler.channels()); |
Tianjie Xu | 29d5575 | 2017-09-20 17:53:46 -0700 | [diff] [blame] | 429 | return {}; |
| 430 | } |
| 431 | |
| 432 | std::vector<std::string> result; |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 433 | std::vector<unsigned char> row(png_handler.width()); |
| 434 | for (png_uint_32 y = 0; y < png_handler.height(); ++y) { |
| 435 | png_read_row(png_handler.png_ptr(), row.data(), nullptr); |
Tianjie Xu | 29d5575 | 2017-09-20 17:53:46 -0700 | [diff] [blame] | 436 | int h = (row[3] << 8) | row[2]; |
| 437 | std::string loc(reinterpret_cast<char*>(&row[5])); |
| 438 | if (!loc.empty()) { |
| 439 | result.push_back(loc); |
| 440 | } |
| 441 | for (int i = 0; i < h; ++i, ++y) { |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 442 | png_read_row(png_handler.png_ptr(), row.data(), nullptr); |
Tianjie Xu | 29d5575 | 2017-09-20 17:53:46 -0700 | [diff] [blame] | 443 | } |
| 444 | } |
| 445 | |
Tianjie Xu | 29d5575 | 2017-09-20 17:53:46 -0700 | [diff] [blame] | 446 | return result; |
| 447 | } |
| 448 | |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 449 | int res_create_localized_alpha_surface(const char* name, |
| 450 | const char* locale, |
Elliott Hughes | 0a5cb0c | 2015-04-15 10:58:56 -0700 | [diff] [blame] | 451 | GRSurface** pSurface) { |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 452 | *pSurface = nullptr; |
| 453 | if (locale == nullptr) { |
| 454 | return 0; |
| 455 | } |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 456 | |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 457 | PngHandler png_handler(name); |
| 458 | if (!png_handler) return png_handler.error_code(); |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 459 | |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 460 | if (png_handler.channels() != 1) { |
| 461 | return -7; |
| 462 | } |
| 463 | |
| 464 | png_structp png_ptr = png_handler.png_ptr(); |
| 465 | png_uint_32 width = png_handler.width(); |
| 466 | png_uint_32 height = png_handler.height(); |
| 467 | |
| 468 | for (png_uint_32 y = 0; y < height; ++y) { |
| 469 | std::vector<unsigned char> row(width); |
| 470 | png_read_row(png_ptr, row.data(), nullptr); |
| 471 | int w = (row[1] << 8) | row[0]; |
| 472 | int h = (row[3] << 8) | row[2]; |
| 473 | __unused int len = row[4]; |
| 474 | char* loc = reinterpret_cast<char*>(&row[5]); |
| 475 | |
| 476 | if (y + 1 + h >= height || matches_locale(loc, locale)) { |
| 477 | printf(" %20s: %s (%d x %d @ %d)\n", name, loc, w, h, y); |
| 478 | |
| 479 | GRSurface* surface = malloc_surface(w * h); |
| 480 | if (!surface) { |
| 481 | return -8; |
| 482 | } |
| 483 | surface->width = w; |
| 484 | surface->height = h; |
| 485 | surface->row_bytes = w; |
| 486 | surface->pixel_bytes = 1; |
| 487 | |
| 488 | for (int i = 0; i < h; ++i, ++y) { |
| 489 | png_read_row(png_ptr, row.data(), nullptr); |
| 490 | memcpy(surface->data + i * w, row.data(), w); |
| 491 | } |
| 492 | |
| 493 | *pSurface = surface; |
| 494 | break; |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 495 | } |
| 496 | |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 497 | for (int i = 0; i < h; ++i, ++y) { |
| 498 | png_read_row(png_ptr, row.data(), nullptr); |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 499 | } |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 500 | } |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 501 | |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 502 | return 0; |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 503 | } |
| 504 | |
Elliott Hughes | 0a5cb0c | 2015-04-15 10:58:56 -0700 | [diff] [blame] | 505 | void res_free_surface(GRSurface* surface) { |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 506 | free(surface); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 507 | } |