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 | 3d0cd1d | 2018-04-13 13:41:58 -0700 | [diff] [blame] | 17 | #include "private/resources.h" |
| 18 | |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 19 | #include <fcntl.h> |
| 20 | #include <linux/fb.h> |
| 21 | #include <linux/kd.h> |
| 22 | #include <stdio.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 23 | #include <stdlib.h> |
Elliott Hughes | cd3c55a | 2015-01-29 20:50:08 -0800 | [diff] [blame] | 24 | #include <string.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 25 | #include <sys/ioctl.h> |
| 26 | #include <sys/mman.h> |
| 27 | #include <sys/types.h> |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 28 | #include <unistd.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 29 | |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 30 | #include <memory> |
Tianjie Xu | 2078b22 | 2017-03-22 12:27:26 -0700 | [diff] [blame] | 31 | #include <regex> |
| 32 | #include <string> |
Yabin Cui | 4425c1d | 2016-02-10 13:47:32 -0800 | [diff] [blame] | 33 | #include <vector> |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 34 | |
Tianjie Xu | 2078b22 | 2017-03-22 12:27:26 -0700 | [diff] [blame] | 35 | #include <android-base/strings.h> |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 36 | #include <png.h> |
| 37 | |
Tao Bao | 0ecbd76 | 2017-01-16 21:16:58 -0800 | [diff] [blame] | 38 | #include "minui/minui.h" |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 39 | |
Tao Bao | 6cd8168 | 2018-05-03 21:53:11 -0700 | [diff] [blame] | 40 | static std::string g_resource_dir{ "/res/images" }; |
| 41 | |
Tao Bao | 44820ac | 2018-10-30 23:34:50 -0700 | [diff] [blame] | 42 | std::unique_ptr<GRSurface> GRSurface::Create(int width, int height, int row_bytes, int pixel_bytes, |
| 43 | size_t data_size) { |
Tao Bao | 92bdb5a | 2018-10-21 12:12:37 -0700 | [diff] [blame] | 44 | static constexpr size_t kSurfaceDataAlignment = 8; |
Tao Bao | 44820ac | 2018-10-30 23:34:50 -0700 | [diff] [blame] | 45 | // Cannot use std::make_unique to access non-public ctor. |
| 46 | auto result = std::unique_ptr<GRSurface>(new GRSurface(width, height, row_bytes, pixel_bytes)); |
Tao Bao | 92bdb5a | 2018-10-21 12:12:37 -0700 | [diff] [blame] | 47 | size_t aligned_size = |
| 48 | (data_size + kSurfaceDataAlignment - 1) / kSurfaceDataAlignment * kSurfaceDataAlignment; |
| 49 | result->data_ = static_cast<uint8_t*>(aligned_alloc(kSurfaceDataAlignment, aligned_size)); |
| 50 | if (result->data_ == nullptr) return nullptr; |
| 51 | return result; |
| 52 | } |
| 53 | |
| 54 | GRSurface::~GRSurface() { |
| 55 | if (data_ != nullptr) { |
| 56 | free(data_); |
| 57 | data_ = nullptr; |
| 58 | } |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 59 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 60 | |
Tao Bao | 3d0cd1d | 2018-04-13 13:41:58 -0700 | [diff] [blame] | 61 | PngHandler::PngHandler(const std::string& name) { |
Tao Bao | 6cd8168 | 2018-05-03 21:53:11 -0700 | [diff] [blame] | 62 | std::string res_path = g_resource_dir + "/" + name + ".png"; |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 63 | png_fp_.reset(fopen(res_path.c_str(), "rbe")); |
Tao Bao | 3d0cd1d | 2018-04-13 13:41:58 -0700 | [diff] [blame] | 64 | // Try to read from |name| if the resource path does not work. |
| 65 | if (!png_fp_) { |
| 66 | png_fp_.reset(fopen(name.c_str(), "rbe")); |
| 67 | } |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 68 | if (!png_fp_) { |
| 69 | error_code_ = -1; |
| 70 | return; |
| 71 | } |
Doug Zongker | 55a36ac | 2013-03-04 15:49:02 -0800 | [diff] [blame] | 72 | |
Tao Bao | 44820ac | 2018-10-30 23:34:50 -0700 | [diff] [blame] | 73 | uint8_t header[8]; |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 74 | size_t bytesRead = fread(header, 1, sizeof(header), png_fp_.get()); |
| 75 | if (bytesRead != sizeof(header)) { |
| 76 | error_code_ = -2; |
| 77 | return; |
| 78 | } |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 79 | |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 80 | if (png_sig_cmp(header, 0, sizeof(header))) { |
| 81 | error_code_ = -3; |
| 82 | return; |
| 83 | } |
| 84 | |
| 85 | png_ptr_ = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr); |
| 86 | if (!png_ptr_) { |
| 87 | error_code_ = -4; |
| 88 | return; |
| 89 | } |
| 90 | |
| 91 | info_ptr_ = png_create_info_struct(png_ptr_); |
| 92 | if (!info_ptr_) { |
| 93 | error_code_ = -5; |
| 94 | return; |
| 95 | } |
| 96 | |
| 97 | if (setjmp(png_jmpbuf(png_ptr_))) { |
| 98 | error_code_ = -6; |
| 99 | return; |
| 100 | } |
| 101 | |
| 102 | png_init_io(png_ptr_, png_fp_.get()); |
| 103 | png_set_sig_bytes(png_ptr_, sizeof(header)); |
| 104 | png_read_info(png_ptr_, info_ptr_); |
| 105 | |
Tao Bao | 3d0cd1d | 2018-04-13 13:41:58 -0700 | [diff] [blame] | 106 | png_get_IHDR(png_ptr_, info_ptr_, &width_, &height_, &bit_depth_, &color_type_, nullptr, nullptr, |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 107 | nullptr); |
| 108 | |
| 109 | channels_ = png_get_channels(png_ptr_, info_ptr_); |
| 110 | |
Tao Bao | 3d0cd1d | 2018-04-13 13:41:58 -0700 | [diff] [blame] | 111 | if (bit_depth_ == 8 && channels_ == 3 && color_type_ == PNG_COLOR_TYPE_RGB) { |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 112 | // 8-bit RGB images: great, nothing to do. |
Tao Bao | 3d0cd1d | 2018-04-13 13:41:58 -0700 | [diff] [blame] | 113 | } else if (bit_depth_ <= 8 && channels_ == 1 && color_type_ == PNG_COLOR_TYPE_GRAY) { |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 114 | // 1-, 2-, 4-, or 8-bit gray images: expand to 8-bit gray. |
| 115 | png_set_expand_gray_1_2_4_to_8(png_ptr_); |
Tao Bao | 3d0cd1d | 2018-04-13 13:41:58 -0700 | [diff] [blame] | 116 | } else if (bit_depth_ <= 8 && channels_ == 1 && color_type_ == PNG_COLOR_TYPE_PALETTE) { |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 117 | // paletted images: expand to 8-bit RGB. Note that we DON'T |
| 118 | // currently expand the tRNS chunk (if any) to an alpha |
| 119 | // channel, because minui doesn't support alpha channels in |
| 120 | // general. |
| 121 | png_set_palette_to_rgb(png_ptr_); |
| 122 | channels_ = 3; |
| 123 | } else { |
Tao Bao | 3d0cd1d | 2018-04-13 13:41:58 -0700 | [diff] [blame] | 124 | fprintf(stderr, "minui doesn't support PNG depth %d channels %d color_type %d\n", bit_depth_, |
| 125 | channels_, color_type_); |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 126 | error_code_ = -7; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | PngHandler::~PngHandler() { |
| 131 | if (png_ptr_) { |
| 132 | png_destroy_read_struct(&png_ptr_, &info_ptr_, nullptr); |
| 133 | } |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Tao Bao | 44820ac | 2018-10-30 23:34:50 -0700 | [diff] [blame] | 136 | // "display" surfaces are transformed into the framebuffer's required pixel format (currently only |
| 137 | // RGBX is supported) at load time, so gr_blit() can be nothing more than a memcpy() for each row. |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 138 | |
Tao Bao | 44820ac | 2018-10-30 23:34:50 -0700 | [diff] [blame] | 139 | // Copies 'input_row' to 'output_row', transforming it to the framebuffer pixel format. The input |
| 140 | // format depends on the value of 'channels': |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 141 | // |
| 142 | // 1 - input is 8-bit grayscale |
| 143 | // 3 - input is 24-bit RGB |
| 144 | // 4 - input is 32-bit RGBA/RGBX |
| 145 | // |
| 146 | // 'width' is the number of pixels in the row. |
Tao Bao | 44820ac | 2018-10-30 23:34:50 -0700 | [diff] [blame] | 147 | static void TransformRgbToDraw(const uint8_t* input_row, uint8_t* output_row, int channels, |
| 148 | int width) { |
| 149 | const uint8_t* ip = input_row; |
| 150 | uint8_t* op = output_row; |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 151 | |
Tao Bao | 44820ac | 2018-10-30 23:34:50 -0700 | [diff] [blame] | 152 | switch (channels) { |
| 153 | case 1: |
| 154 | // expand gray level to RGBX |
| 155 | for (int x = 0; x < width; ++x) { |
| 156 | *op++ = *ip; |
| 157 | *op++ = *ip; |
| 158 | *op++ = *ip; |
| 159 | *op++ = 0xff; |
| 160 | ip++; |
| 161 | } |
| 162 | break; |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 163 | |
Tao Bao | 44820ac | 2018-10-30 23:34:50 -0700 | [diff] [blame] | 164 | case 3: |
| 165 | // expand RGBA to RGBX |
| 166 | for (int x = 0; x < width; ++x) { |
| 167 | *op++ = *ip++; |
| 168 | *op++ = *ip++; |
| 169 | *op++ = *ip++; |
| 170 | *op++ = 0xff; |
| 171 | } |
| 172 | break; |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 173 | |
Tao Bao | 44820ac | 2018-10-30 23:34:50 -0700 | [diff] [blame] | 174 | case 4: |
| 175 | // copy RGBA to RGBX |
| 176 | memcpy(output_row, input_row, width * 4); |
| 177 | break; |
| 178 | } |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 179 | } |
| 180 | |
Elliott Hughes | 0a5cb0c | 2015-04-15 10:58:56 -0700 | [diff] [blame] | 181 | int res_create_display_surface(const char* name, GRSurface** pSurface) { |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 182 | *pSurface = nullptr; |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 183 | |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 184 | PngHandler png_handler(name); |
| 185 | if (!png_handler) return png_handler.error_code(); |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 186 | |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 187 | png_structp png_ptr = png_handler.png_ptr(); |
| 188 | png_uint_32 width = png_handler.width(); |
| 189 | png_uint_32 height = png_handler.height(); |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 190 | |
Tao Bao | 44820ac | 2018-10-30 23:34:50 -0700 | [diff] [blame] | 191 | auto surface = GRSurface::Create(width, height, width * 4, 4, width * height * 4); |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 192 | if (!surface) { |
| 193 | return -8; |
| 194 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 195 | |
Tao Bao | ed876a7 | 2018-07-31 21:32:50 -0700 | [diff] [blame] | 196 | PixelFormat pixel_format = gr_pixel_format(); |
| 197 | if (pixel_format == PixelFormat::ABGR || pixel_format == PixelFormat::BGRA) { |
| 198 | png_set_bgr(png_ptr); |
| 199 | } |
Tony Kuo | fd778e3 | 2015-02-05 21:25:56 +0800 | [diff] [blame] | 200 | |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 201 | for (png_uint_32 y = 0; y < height; ++y) { |
Tao Bao | 44820ac | 2018-10-30 23:34:50 -0700 | [diff] [blame] | 202 | std::vector<uint8_t> p_row(width * 4); |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 203 | png_read_row(png_ptr, p_row.data(), nullptr); |
Tao Bao | 44820ac | 2018-10-30 23:34:50 -0700 | [diff] [blame] | 204 | TransformRgbToDraw(p_row.data(), surface->data() + y * surface->row_bytes, |
| 205 | png_handler.channels(), width); |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 206 | } |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 207 | |
Tao Bao | 92bdb5a | 2018-10-21 12:12:37 -0700 | [diff] [blame] | 208 | *pSurface = surface.release(); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 209 | |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 210 | return 0; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 211 | } |
| 212 | |
Tao Bao | b723f4f | 2015-12-11 15:18:51 -0800 | [diff] [blame] | 213 | 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] | 214 | GRSurface*** pSurface) { |
| 215 | *pSurface = nullptr; |
| 216 | *frames = -1; |
Doug Zongker | 469954f | 2014-03-07 09:21:25 -0800 | [diff] [blame] | 217 | |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 218 | PngHandler png_handler(name); |
| 219 | if (!png_handler) return png_handler.error_code(); |
Doug Zongker | 469954f | 2014-03-07 09:21:25 -0800 | [diff] [blame] | 220 | |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 221 | png_structp png_ptr = png_handler.png_ptr(); |
| 222 | png_uint_32 width = png_handler.width(); |
| 223 | png_uint_32 height = png_handler.height(); |
Doug Zongker | 469954f | 2014-03-07 09:21:25 -0800 | [diff] [blame] | 224 | |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 225 | *frames = 1; |
| 226 | *fps = 20; |
| 227 | png_textp text; |
| 228 | int num_text; |
| 229 | if (png_get_text(png_ptr, png_handler.info_ptr(), &text, &num_text)) { |
| 230 | for (int i = 0; i < num_text; ++i) { |
| 231 | if (text[i].key && strcmp(text[i].key, "Frames") == 0 && text[i].text) { |
| 232 | *frames = atoi(text[i].text); |
| 233 | } else if (text[i].key && strcmp(text[i].key, "FPS") == 0 && text[i].text) { |
| 234 | *fps = atoi(text[i].text); |
| 235 | } |
Tao Bao | b723f4f | 2015-12-11 15:18:51 -0800 | [diff] [blame] | 236 | } |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 237 | printf(" found frames = %d\n", *frames); |
| 238 | printf(" found fps = %d\n", *fps); |
| 239 | } |
Tao Bao | b723f4f | 2015-12-11 15:18:51 -0800 | [diff] [blame] | 240 | |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 241 | int result = 0; |
| 242 | GRSurface** surface = nullptr; |
| 243 | if (*frames <= 0 || *fps <= 0) { |
| 244 | printf("bad number of frames (%d) and/or FPS (%d)\n", *frames, *fps); |
| 245 | result = -10; |
| 246 | goto exit; |
| 247 | } |
Doug Zongker | 469954f | 2014-03-07 09:21:25 -0800 | [diff] [blame] | 248 | |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 249 | if (height % *frames != 0) { |
| 250 | printf("bad height (%d) for frame count (%d)\n", height, *frames); |
| 251 | result = -9; |
| 252 | goto exit; |
| 253 | } |
Doug Zongker | 469954f | 2014-03-07 09:21:25 -0800 | [diff] [blame] | 254 | |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 255 | surface = static_cast<GRSurface**>(calloc(*frames, sizeof(GRSurface*))); |
| 256 | if (!surface) { |
| 257 | result = -8; |
| 258 | goto exit; |
| 259 | } |
| 260 | for (int i = 0; i < *frames; ++i) { |
Tao Bao | 44820ac | 2018-10-30 23:34:50 -0700 | [diff] [blame] | 261 | auto height_per_frame = height / *frames; |
| 262 | auto created_surface = |
| 263 | GRSurface::Create(width, height_per_frame, width * 4, 4, width * height_per_frame); |
Tao Bao | 92bdb5a | 2018-10-21 12:12:37 -0700 | [diff] [blame] | 264 | if (!created_surface) { |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 265 | result = -8; |
| 266 | goto exit; |
Doug Zongker | 469954f | 2014-03-07 09:21:25 -0800 | [diff] [blame] | 267 | } |
Tao Bao | 92bdb5a | 2018-10-21 12:12:37 -0700 | [diff] [blame] | 268 | surface[i] = created_surface.release(); |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 269 | } |
Doug Zongker | 469954f | 2014-03-07 09:21:25 -0800 | [diff] [blame] | 270 | |
Tao Bao | ed876a7 | 2018-07-31 21:32:50 -0700 | [diff] [blame] | 271 | if (gr_pixel_format() == PixelFormat::ABGR || gr_pixel_format() == PixelFormat::BGRA) { |
| 272 | png_set_bgr(png_ptr); |
| 273 | } |
Tony Kuo | fd778e3 | 2015-02-05 21:25:56 +0800 | [diff] [blame] | 274 | |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 275 | for (png_uint_32 y = 0; y < height; ++y) { |
Tao Bao | 44820ac | 2018-10-30 23:34:50 -0700 | [diff] [blame] | 276 | std::vector<uint8_t> p_row(width * 4); |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 277 | png_read_row(png_ptr, p_row.data(), nullptr); |
| 278 | int frame = y % *frames; |
Tao Bao | 44820ac | 2018-10-30 23:34:50 -0700 | [diff] [blame] | 279 | uint8_t* out_row = surface[frame]->data() + (y / *frames) * surface[frame]->row_bytes; |
| 280 | TransformRgbToDraw(p_row.data(), out_row, png_handler.channels(), width); |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 281 | } |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 282 | |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 283 | *pSurface = surface; |
Doug Zongker | 469954f | 2014-03-07 09:21:25 -0800 | [diff] [blame] | 284 | |
| 285 | exit: |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 286 | if (result < 0) { |
| 287 | if (surface) { |
| 288 | for (int i = 0; i < *frames; ++i) { |
| 289 | free(surface[i]); |
| 290 | } |
| 291 | free(surface); |
Doug Zongker | 469954f | 2014-03-07 09:21:25 -0800 | [diff] [blame] | 292 | } |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 293 | } |
| 294 | return result; |
Doug Zongker | 469954f | 2014-03-07 09:21:25 -0800 | [diff] [blame] | 295 | } |
| 296 | |
Elliott Hughes | 0a5cb0c | 2015-04-15 10:58:56 -0700 | [diff] [blame] | 297 | int res_create_alpha_surface(const char* name, GRSurface** pSurface) { |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 298 | *pSurface = nullptr; |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 299 | |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 300 | PngHandler png_handler(name); |
| 301 | if (!png_handler) return png_handler.error_code(); |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 302 | |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 303 | if (png_handler.channels() != 1) { |
| 304 | return -7; |
| 305 | } |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 306 | |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 307 | png_structp png_ptr = png_handler.png_ptr(); |
| 308 | png_uint_32 width = png_handler.width(); |
| 309 | png_uint_32 height = png_handler.height(); |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 310 | |
Tao Bao | 44820ac | 2018-10-30 23:34:50 -0700 | [diff] [blame] | 311 | auto surface = GRSurface::Create(width, height, width, 1, width * height); |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 312 | if (!surface) { |
| 313 | return -8; |
| 314 | } |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 315 | |
Tao Bao | ed876a7 | 2018-07-31 21:32:50 -0700 | [diff] [blame] | 316 | PixelFormat pixel_format = gr_pixel_format(); |
| 317 | if (pixel_format == PixelFormat::ABGR || pixel_format == PixelFormat::BGRA) { |
| 318 | png_set_bgr(png_ptr); |
| 319 | } |
Tony Kuo | fd778e3 | 2015-02-05 21:25:56 +0800 | [diff] [blame] | 320 | |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 321 | for (png_uint_32 y = 0; y < height; ++y) { |
Tao Bao | 44820ac | 2018-10-30 23:34:50 -0700 | [diff] [blame] | 322 | uint8_t* p_row = surface->data() + y * surface->row_bytes; |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 323 | png_read_row(png_ptr, p_row, nullptr); |
| 324 | } |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 325 | |
Tao Bao | 92bdb5a | 2018-10-21 12:12:37 -0700 | [diff] [blame] | 326 | *pSurface = surface.release(); |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 327 | |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 328 | return 0; |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 329 | } |
| 330 | |
Tao Bao | 6cd8168 | 2018-05-03 21:53:11 -0700 | [diff] [blame] | 331 | void res_set_resource_dir(const std::string& dirname) { |
| 332 | g_resource_dir = dirname; |
| 333 | } |
| 334 | |
Tianjie Xu | 2430e29 | 2016-04-19 15:02:41 -0700 | [diff] [blame] | 335 | // This function tests if a locale string stored in PNG (prefix) matches |
| 336 | // the locale string provided by the system (locale). |
Tianjie Xu | 2078b22 | 2017-03-22 12:27:26 -0700 | [diff] [blame] | 337 | bool matches_locale(const std::string& prefix, const std::string& locale) { |
| 338 | // According to the BCP 47 format, A locale string may consists of: |
| 339 | // language-{extlang}-{script}-{region}-{variant} |
| 340 | // The locale headers in PNG mostly consist of language-{region} except for sr-Latn, and some |
| 341 | // android's system locale can have the format language-{script}-{region}. |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 342 | |
Tianjie Xu | 2078b22 | 2017-03-22 12:27:26 -0700 | [diff] [blame] | 343 | // Return true if the whole string of prefix matches the top part of locale. Otherwise try to |
| 344 | // match the locale string without the {script} section. |
| 345 | // For instance, prefix == "en" matches locale == "en-US", prefix == "sr-Latn" matches locale |
| 346 | // == "sr-Latn-BA", and prefix == "zh-CN" matches locale == "zh-Hans-CN". |
Elliott Hughes | 1d65c95 | 2017-12-20 12:36:31 -0800 | [diff] [blame] | 347 | if (android::base::StartsWith(locale, prefix)) { |
Tianjie Xu | 2078b22 | 2017-03-22 12:27:26 -0700 | [diff] [blame] | 348 | return true; |
| 349 | } |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 350 | |
Tianjie Xu | 2078b22 | 2017-03-22 12:27:26 -0700 | [diff] [blame] | 351 | size_t separator = prefix.find('-'); |
| 352 | if (separator == std::string::npos) { |
| 353 | return false; |
| 354 | } |
| 355 | std::regex loc_regex(prefix.substr(0, separator) + "-[A-Za-z]*" + prefix.substr(separator)); |
| 356 | return std::regex_match(locale, loc_regex); |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 357 | } |
| 358 | |
Tianjie Xu | 29d5575 | 2017-09-20 17:53:46 -0700 | [diff] [blame] | 359 | 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] | 360 | PngHandler png_handler(png_name); |
| 361 | if (!png_handler) { |
| 362 | 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] | 363 | return {}; |
| 364 | } |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 365 | if (png_handler.channels() != 1) { |
| 366 | 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] | 367 | return {}; |
| 368 | } |
| 369 | |
| 370 | std::vector<std::string> result; |
Tao Bao | 44820ac | 2018-10-30 23:34:50 -0700 | [diff] [blame] | 371 | std::vector<uint8_t> row(png_handler.width()); |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 372 | for (png_uint_32 y = 0; y < png_handler.height(); ++y) { |
| 373 | png_read_row(png_handler.png_ptr(), row.data(), nullptr); |
Tianjie Xu | 29d5575 | 2017-09-20 17:53:46 -0700 | [diff] [blame] | 374 | int h = (row[3] << 8) | row[2]; |
| 375 | std::string loc(reinterpret_cast<char*>(&row[5])); |
| 376 | if (!loc.empty()) { |
| 377 | result.push_back(loc); |
| 378 | } |
| 379 | for (int i = 0; i < h; ++i, ++y) { |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 380 | png_read_row(png_handler.png_ptr(), row.data(), nullptr); |
Tianjie Xu | 29d5575 | 2017-09-20 17:53:46 -0700 | [diff] [blame] | 381 | } |
| 382 | } |
| 383 | |
Tianjie Xu | 29d5575 | 2017-09-20 17:53:46 -0700 | [diff] [blame] | 384 | return result; |
| 385 | } |
| 386 | |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 387 | int res_create_localized_alpha_surface(const char* name, |
| 388 | const char* locale, |
Elliott Hughes | 0a5cb0c | 2015-04-15 10:58:56 -0700 | [diff] [blame] | 389 | GRSurface** pSurface) { |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 390 | *pSurface = nullptr; |
| 391 | if (locale == nullptr) { |
| 392 | return 0; |
| 393 | } |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 394 | |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 395 | PngHandler png_handler(name); |
| 396 | if (!png_handler) return png_handler.error_code(); |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 397 | |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 398 | if (png_handler.channels() != 1) { |
| 399 | return -7; |
| 400 | } |
| 401 | |
| 402 | png_structp png_ptr = png_handler.png_ptr(); |
| 403 | png_uint_32 width = png_handler.width(); |
| 404 | png_uint_32 height = png_handler.height(); |
| 405 | |
| 406 | for (png_uint_32 y = 0; y < height; ++y) { |
Tao Bao | 44820ac | 2018-10-30 23:34:50 -0700 | [diff] [blame] | 407 | std::vector<uint8_t> row(width); |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 408 | png_read_row(png_ptr, row.data(), nullptr); |
| 409 | int w = (row[1] << 8) | row[0]; |
| 410 | int h = (row[3] << 8) | row[2]; |
| 411 | __unused int len = row[4]; |
| 412 | char* loc = reinterpret_cast<char*>(&row[5]); |
| 413 | |
| 414 | if (y + 1 + h >= height || matches_locale(loc, locale)) { |
| 415 | printf(" %20s: %s (%d x %d @ %d)\n", name, loc, w, h, y); |
| 416 | |
Tao Bao | 44820ac | 2018-10-30 23:34:50 -0700 | [diff] [blame] | 417 | auto surface = GRSurface::Create(w, h, w, 1, w * h); |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 418 | if (!surface) { |
| 419 | return -8; |
| 420 | } |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 421 | |
| 422 | for (int i = 0; i < h; ++i, ++y) { |
| 423 | png_read_row(png_ptr, row.data(), nullptr); |
Tao Bao | 92bdb5a | 2018-10-21 12:12:37 -0700 | [diff] [blame] | 424 | memcpy(surface->data() + i * w, row.data(), w); |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 425 | } |
| 426 | |
Tao Bao | 92bdb5a | 2018-10-21 12:12:37 -0700 | [diff] [blame] | 427 | *pSurface = surface.release(); |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 428 | break; |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 429 | } |
| 430 | |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 431 | for (int i = 0; i < h; ++i, ++y) { |
| 432 | png_read_row(png_ptr, row.data(), nullptr); |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 433 | } |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 434 | } |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 435 | |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 436 | return 0; |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 437 | } |
| 438 | |
Elliott Hughes | 0a5cb0c | 2015-04-15 10:58:56 -0700 | [diff] [blame] | 439 | void res_free_surface(GRSurface* surface) { |
Tianjie Xu | acba38c | 2017-09-27 17:53:34 -0700 | [diff] [blame] | 440 | free(surface); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 441 | } |