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