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