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