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