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> 18 #include <string.h> 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 31 #include <png.h> 32 33 #include "minui.h" 34 35 #define SURFACE_DATA_ALIGNMENT 8 36 37 static GRSurface* malloc_surface(size_t data_size) { 38 size_t size = sizeof(GRSurface) + data_size + SURFACE_DATA_ALIGNMENT; 39 unsigned char* temp = reinterpret_cast<unsigned char*>(malloc(size)); 40 if (temp == NULL) return NULL; 41 GRSurface* surface = reinterpret_cast<GRSurface*>(temp); 42 surface->data = temp + sizeof(GRSurface) + 43 (SURFACE_DATA_ALIGNMENT - (sizeof(GRSurface) % SURFACE_DATA_ALIGNMENT)); 44 return surface; 45 } 46 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) { 49 char resPath[256]; 50 unsigned char header[8]; 51 int result = 0; 52 int color_type, bit_depth; 53 size_t bytesRead; 54 55 snprintf(resPath, sizeof(resPath)-1, "/res/images/%s.png", name); 56 resPath[sizeof(resPath)-1] = '\0'; 57 FILE* fp = fopen(resPath, "rb"); 58 if (fp == NULL) { 59 result = -1; 60 goto exit; 61 } 62 63 bytesRead = fread(header, 1, sizeof(header), fp); 64 if (bytesRead != sizeof(header)) { 65 result = -2; 66 goto exit; 67 } 68 69 if (png_sig_cmp(header, 0, sizeof(header))) { 70 result = -3; 71 goto exit; 72 } 73 74 *png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); 75 if (!*png_ptr) { 76 result = -4; 77 goto exit; 78 } 79 80 *info_ptr = png_create_info_struct(*png_ptr); 81 if (!*info_ptr) { 82 result = -5; 83 goto exit; 84 } 85 86 if (setjmp(png_jmpbuf(*png_ptr))) { 87 result = -6; 88 goto exit; 89 } 90 91 png_init_io(*png_ptr, fp); 92 png_set_sig_bytes(*png_ptr, sizeof(header)); 93 png_read_info(*png_ptr, *info_ptr); 94 95 png_get_IHDR(*png_ptr, *info_ptr, width, height, &bit_depth, 96 &color_type, NULL, NULL, NULL); 97 98 *channels = png_get_channels(*png_ptr, *info_ptr); 99 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); 105 } else if (bit_depth <= 8 && *channels == 1 && color_type == PNG_COLOR_TYPE_PALETTE) { 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", 114 bit_depth, *channels, color_type); 115 result = -7; 116 goto exit; 117 } 118 119 return result; 120 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 139 // Allocate and return a GRSurface* sufficient for storing an image of 140 // the indicated size in the framebuffer pixel format. 141 static GRSurface* init_display_surface(png_uint_32 width, png_uint_32 height) { 142 GRSurface* surface = malloc_surface(width * height * 4); 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 198 int res_create_display_surface(const char* name, GRSurface** pSurface) { 199 GRSurface* surface = NULL; 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; 205 unsigned char* p_row; 206 unsigned int y; 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); 214 if (surface == NULL) { 215 result = -8; 216 goto exit; 217 } 218 219 #if defined(RECOVERY_ABGR) || defined(RECOVERY_BGRA) 220 png_set_bgr(png_ptr); 221 #endif 222 223 p_row = reinterpret_cast<unsigned char*>(malloc(width * 4)); 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); 227 } 228 free(p_row); 229 230 *pSurface = surface; 231 232 exit: 233 png_destroy_read_struct(&png_ptr, &info_ptr, NULL); 234 if (result < 0 && surface != NULL) free(surface); 235 return result; 236 } 237 238 int res_create_multi_display_surface(const char* name, int* frames, int* fps, 239 GRSurface*** pSurface) { 240 GRSurface** surface = NULL; 241 int result = 0; 242 png_structp png_ptr = NULL; 243 png_infop info_ptr = NULL; 244 png_uint_32 width, height; 245 png_byte channels; 246 png_textp text; 247 int num_text; 248 unsigned char* p_row; 249 unsigned int y; 250 251 *pSurface = NULL; 252 *frames = -1; 253 254 result = open_png(name, &png_ptr, &info_ptr, &width, &height, &channels); 255 if (result < 0) return result; 256 257 *frames = 1; 258 *fps = 20; 259 if (png_get_text(png_ptr, info_ptr, &text, &num_text)) { 260 for (int i = 0; i < num_text; ++i) { 261 if (text[i].key && strcmp(text[i].key, "Frames") == 0 && text[i].text) { 262 *frames = atoi(text[i].text); 263 } else if (text[i].key && strcmp(text[i].key, "FPS") == 0 && text[i].text) { 264 *fps = atoi(text[i].text); 265 } 266 } 267 printf(" found frames = %d\n", *frames); 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; 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 283 surface = reinterpret_cast<GRSurface**>(malloc(*frames * sizeof(GRSurface*))); 284 if (surface == NULL) { 285 result = -8; 286 goto exit; 287 } 288 for (int i = 0; i < *frames; ++i) { 289 surface[i] = init_display_surface(width, height / *frames); 290 if (surface[i] == NULL) { 291 result = -8; 292 goto exit; 293 } 294 } 295 296 #if defined(RECOVERY_ABGR) || defined(RECOVERY_BGRA) 297 png_set_bgr(png_ptr); 298 #endif 299 300 p_row = reinterpret_cast<unsigned char*>(malloc(width * 4)); 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 310 *pSurface = reinterpret_cast<GRSurface**>(surface); 311 312 exit: 313 png_destroy_read_struct(&png_ptr, &info_ptr, NULL); 314 315 if (result < 0) { 316 if (surface) { 317 for (int i = 0; i < *frames; ++i) { 318 if (surface[i]) free(surface[i]); 319 } 320 free(surface); 321 } 322 } 323 return result; 324 } 325 326 int res_create_alpha_surface(const char* name, GRSurface** pSurface) { 327 GRSurface* surface = NULL; 328 int result = 0; 329 png_structp png_ptr = NULL; 330 png_infop info_ptr = NULL; 331 png_uint_32 width, height; 332 png_byte channels; 333 334 *pSurface = NULL; 335 336 result = open_png(name, &png_ptr, &info_ptr, &width, &height, &channels); 337 if (result < 0) return result; 338 339 if (channels != 1) { 340 result = -7; 341 goto exit; 342 } 343 344 surface = malloc_surface(width * height); 345 if (surface == NULL) { 346 result = -8; 347 goto exit; 348 } 349 surface->width = width; 350 surface->height = height; 351 surface->row_bytes = width; 352 surface->pixel_bytes = 1; 353 354 #if defined(RECOVERY_ABGR) || defined(RECOVERY_BGRA) 355 png_set_bgr(png_ptr); 356 #endif 357 358 unsigned char* p_row; 359 unsigned int y; 360 for (y = 0; y < height; ++y) { 361 p_row = surface->data + y * surface->row_bytes; 362 png_read_row(png_ptr, p_row, NULL); 363 } 364 365 *pSurface = surface; 366 367 exit: 368 png_destroy_read_struct(&png_ptr, &info_ptr, NULL); 369 if (result < 0 && surface != NULL) free(surface); 370 return result; 371 } 372 373 // This function tests if a locale string stored in PNG (prefix) matches 374 // the locale string provided by the system (locale). 375 bool matches_locale(const char* prefix, const char* locale) { 376 if (locale == NULL) return false; 377 378 // Return true if the whole string of prefix matches the top part of 379 // locale. For instance, prefix == "en" matches locale == "en_US"; 380 // and prefix == "zh_CN" matches locale == "zh_CN_#Hans". 381 382 return (strncmp(prefix, locale, strlen(prefix)) == 0); 383 } 384 385 int res_create_localized_alpha_surface(const char* name, 386 const char* locale, 387 GRSurface** pSurface) { 388 GRSurface* surface = NULL; 389 int result = 0; 390 png_structp png_ptr = NULL; 391 png_infop info_ptr = NULL; 392 png_uint_32 width, height; 393 png_byte channels; 394 unsigned char* row; 395 png_uint_32 y; 396 397 *pSurface = NULL; 398 399 if (locale == NULL) { 400 surface = malloc_surface(0); 401 surface->width = 0; 402 surface->height = 0; 403 surface->row_bytes = 0; 404 surface->pixel_bytes = 1; 405 goto exit; 406 } 407 408 result = open_png(name, &png_ptr, &info_ptr, &width, &height, &channels); 409 if (result < 0) return result; 410 411 if (channels != 1) { 412 result = -7; 413 goto exit; 414 } 415 416 row = reinterpret_cast<unsigned char*>(malloc(width)); 417 for (y = 0; y < height; ++y) { 418 png_read_row(png_ptr, row, NULL); 419 int w = (row[1] << 8) | row[0]; 420 int h = (row[3] << 8) | row[2]; 421 int len = row[4]; 422 char* loc = (char*)row+5; 423 424 if (y+1+h >= height || matches_locale(loc, locale)) { 425 printf(" %20s: %s (%d x %d @ %d)\n", name, loc, w, h, y); 426 427 surface = malloc_surface(w*h); 428 if (surface == NULL) { 429 result = -8; 430 goto exit; 431 } 432 surface->width = w; 433 surface->height = h; 434 surface->row_bytes = w; 435 surface->pixel_bytes = 1; 436 437 int i; 438 for (i = 0; i < h; ++i, ++y) { 439 png_read_row(png_ptr, row, NULL); 440 memcpy(surface->data + i*w, row, w); 441 } 442 443 *pSurface = reinterpret_cast<GRSurface*>(surface); 444 break; 445 } else { 446 int i; 447 for (i = 0; i < h; ++i, ++y) { 448 png_read_row(png_ptr, row, NULL); 449 } 450 } 451 } 452 453 exit: 454 png_destroy_read_struct(&png_ptr, &info_ptr, NULL); 455 if (result < 0 && surface != NULL) free(surface); 456 return result; 457 } 458 459 void res_free_surface(GRSurface* surface) { 460 free(surface); 461 } 462