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> |
| 18 | #include <unistd.h> |
| 19 | |
| 20 | #include <fcntl.h> |
| 21 | #include <stdio.h> |
| 22 | |
| 23 | #include <sys/ioctl.h> |
| 24 | #include <sys/mman.h> |
| 25 | #include <sys/types.h> |
| 26 | |
| 27 | #include <linux/fb.h> |
| 28 | #include <linux/kd.h> |
| 29 | |
Ethan Yonker | a33161b | 2014-11-06 15:11:20 -0600 | [diff] [blame] | 30 | #include <pixelflinger/pixelflinger.h> |
| 31 | |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 32 | #include <png.h> |
| 33 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 34 | #include "minui.h" |
| 35 | |
Dees Troy | 62b75ab | 2014-05-02 13:20:33 +0000 | [diff] [blame] | 36 | #ifdef FASTMMI_FEATURE |
| 37 | char *locale = NULL; |
| 38 | #else |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 39 | extern char* locale; |
Dees Troy | 62b75ab | 2014-05-02 13:20:33 +0000 | [diff] [blame] | 40 | #endif |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 41 | |
Ethan Yonker | a33161b | 2014-11-06 15:11:20 -0600 | [diff] [blame] | 42 | // libpng gives "undefined reference to 'pow'" errors, and I have no |
| 43 | // idea how to convince the build system to link with -lm. We don't |
| 44 | // need this functionality (it's used for gamma adjustment) so provide |
| 45 | // a dummy implementation to satisfy the linker. |
| 46 | double pow(double x, double y) { |
| 47 | return x * y; |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 48 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 49 | |
Ethan Yonker | a33161b | 2014-11-06 15:11:20 -0600 | [diff] [blame] | 50 | int res_create_surface(const char* name, gr_surface* pSurface) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 51 | char resPath[256]; |
Ethan Yonker | a33161b | 2014-11-06 15:11:20 -0600 | [diff] [blame] | 52 | GGLSurface* surface = NULL; |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 53 | int result = 0; |
Ethan Yonker | a33161b | 2014-11-06 15:11:20 -0600 | [diff] [blame] | 54 | unsigned char header[8]; |
| 55 | png_structp png_ptr = NULL; |
| 56 | png_infop info_ptr = NULL; |
| 57 | |
| 58 | *pSurface = NULL; |
Doug Zongker | 6809c51 | 2011-03-01 14:04:34 -0800 | [diff] [blame] | 59 | |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 60 | 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] | 61 | resPath[sizeof(resPath)-1] = '\0'; |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 62 | FILE* fp = fopen(resPath, "rb"); |
| 63 | if (fp == NULL) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 64 | result = -1; |
| 65 | goto exit; |
| 66 | } |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 67 | |
| 68 | size_t bytesRead = fread(header, 1, sizeof(header), fp); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 69 | if (bytesRead != sizeof(header)) { |
| 70 | result = -2; |
| 71 | goto exit; |
| 72 | } |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 73 | |
| 74 | if (png_sig_cmp(header, 0, sizeof(header))) { |
| 75 | result = -3; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 76 | goto exit; |
| 77 | } |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 78 | |
Ethan Yonker | a33161b | 2014-11-06 15:11:20 -0600 | [diff] [blame] | 79 | png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); |
| 80 | if (!png_ptr) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 81 | result = -4; |
| 82 | goto exit; |
| 83 | } |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 84 | |
Ethan Yonker | a33161b | 2014-11-06 15:11:20 -0600 | [diff] [blame] | 85 | info_ptr = png_create_info_struct(png_ptr); |
| 86 | if (!info_ptr) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 87 | result = -5; |
| 88 | goto exit; |
| 89 | } |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 90 | |
Ethan Yonker | a33161b | 2014-11-06 15:11:20 -0600 | [diff] [blame] | 91 | if (setjmp(png_jmpbuf(png_ptr))) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 92 | result = -6; |
| 93 | goto exit; |
| 94 | } |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 95 | |
Ethan Yonker | a33161b | 2014-11-06 15:11:20 -0600 | [diff] [blame] | 96 | png_init_io(png_ptr, fp); |
| 97 | png_set_sig_bytes(png_ptr, sizeof(header)); |
| 98 | png_read_info(png_ptr, info_ptr); |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 99 | |
John Reck | 94fd07b | 2013-08-26 16:45:33 -0700 | [diff] [blame] | 100 | int color_type, bit_depth; |
Ethan Yonker | a33161b | 2014-11-06 15:11:20 -0600 | [diff] [blame] | 101 | size_t width, height; |
| 102 | |
| 103 | png_get_IHDR(png_ptr, info_ptr, width, height, &bit_depth, |
John Reck | 94fd07b | 2013-08-26 16:45:33 -0700 | [diff] [blame] | 104 | &color_type, NULL, NULL, NULL); |
| 105 | |
Ethan Yonker | a33161b | 2014-11-06 15:11:20 -0600 | [diff] [blame] | 106 | png_byte* channels = png_get_channels(png_ptr, info_ptr); |
John Reck | 94fd07b | 2013-08-26 16:45:33 -0700 | [diff] [blame] | 107 | |
Ethan Yonker | a33161b | 2014-11-06 15:11:20 -0600 | [diff] [blame] | 108 | if (!(bit_depth == 8 && |
| 109 | ((channels == 3 && color_type == PNG_COLOR_TYPE_RGB) || |
| 110 | (channels == 4 && color_type == PNG_COLOR_TYPE_RGBA) || |
| 111 | (channels == 1 && (color_type == PNG_COLOR_TYPE_PALETTE || |
| 112 | color_type == PNG_COLOR_TYPE_GRAY))))) { |
| 113 | return -7; |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 114 | goto exit; |
| 115 | } |
| 116 | |
Ethan Yonker | a33161b | 2014-11-06 15:11:20 -0600 | [diff] [blame] | 117 | size_t stride = (color_type == PNG_COLOR_TYPE_GRAY ? 1 : 4) * width; |
| 118 | size_t pixelSize = stride * height; |
Doug Zongker | 55a36ac | 2013-03-04 15:49:02 -0800 | [diff] [blame] | 119 | |
Ethan Yonker | a33161b | 2014-11-06 15:11:20 -0600 | [diff] [blame] | 120 | surface = malloc(sizeof(GGLSurface) + pixelSize); |
| 121 | if (surface == NULL) { |
| 122 | result = -8; |
| 123 | goto exit; |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 124 | } |
Ethan Yonker | a33161b | 2014-11-06 15:11:20 -0600 | [diff] [blame] | 125 | unsigned char* pData = (unsigned char*) (surface + 1); |
| 126 | surface->version = sizeof(GGLSurface); |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 127 | surface->width = width; |
| 128 | surface->height = height; |
Ethan Yonker | a33161b | 2014-11-06 15:11:20 -0600 | [diff] [blame] | 129 | surface->stride = width; /* Yes, pixels, not bytes */ |
| 130 | surface->data = pData; |
| 131 | surface->format = (channels == 3) ? GGL_PIXEL_FORMAT_RGBX_8888 : |
| 132 | ((color_type == PNG_COLOR_TYPE_PALETTE ? GGL_PIXEL_FORMAT_RGBA_8888 : GGL_PIXEL_FORMAT_L_8)); |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 133 | |
Ethan Yonker | a33161b | 2014-11-06 15:11:20 -0600 | [diff] [blame] | 134 | int alpha = 0; |
| 135 | if (color_type == PNG_COLOR_TYPE_PALETTE) { |
| 136 | png_set_palette_to_rgb(png_ptr); |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 137 | } |
Ethan Yonker | a33161b | 2014-11-06 15:11:20 -0600 | [diff] [blame] | 138 | if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) { |
| 139 | png_set_tRNS_to_alpha(png_ptr); |
| 140 | alpha = 1; |
| 141 | } |
| 142 | if (color_type == PNG_COLOR_TYPE_GRAY) { |
| 143 | alpha = 1; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 144 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 145 | |
Edwin Vane | edc5d17 | 2012-07-27 11:32:23 -0400 | [diff] [blame] | 146 | unsigned int y; |
Ethan Yonker | a33161b | 2014-11-06 15:11:20 -0600 | [diff] [blame] | 147 | if (channels == 3 || (channels == 1 && !alpha)) { |
| 148 | for (y = 0; y < height; ++y) { |
| 149 | unsigned char* pRow = pData + y * stride; |
| 150 | png_read_row(png_ptr, pRow, NULL); |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 151 | |
Ethan Yonker | a33161b | 2014-11-06 15:11:20 -0600 | [diff] [blame] | 152 | int x; |
| 153 | for(x = width - 1; x >= 0; x--) { |
| 154 | int sx = x * 3; |
| 155 | int dx = x * 4; |
| 156 | unsigned char r = pRow[sx]; |
| 157 | unsigned char g = pRow[sx + 1]; |
| 158 | unsigned char b = pRow[sx + 2]; |
| 159 | unsigned char a = 0xff; |
| 160 | pRow[dx ] = r; // r |
| 161 | pRow[dx + 1] = g; // g |
| 162 | pRow[dx + 2] = b; // b |
| 163 | pRow[dx + 3] = a; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 164 | } |
| 165 | } |
Ethan Yonker | a33161b | 2014-11-06 15:11:20 -0600 | [diff] [blame] | 166 | } else { |
| 167 | for (y = 0; y < height; ++y) { |
| 168 | unsigned char* pRow = pData + y * stride; |
| 169 | png_read_row(png_ptr, pRow, NULL); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 170 | } |
| 171 | } |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 172 | |
Ethan Yonker | a33161b | 2014-11-06 15:11:20 -0600 | [diff] [blame] | 173 | *pSurface = (gr_surface) surface; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 174 | |
| 175 | exit: |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 176 | png_destroy_read_struct(&png_ptr, &info_ptr, NULL); |
| 177 | |
Ethan Yonker | a33161b | 2014-11-06 15:11:20 -0600 | [diff] [blame] | 178 | if (fp != NULL) { |
| 179 | fclose(fp); |
| 180 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 181 | if (result < 0) { |
| 182 | if (surface) { |
| 183 | free(surface); |
| 184 | } |
| 185 | } |
| 186 | return result; |
| 187 | } |
| 188 | |
Ethan Yonker | a33161b | 2014-11-06 15:11:20 -0600 | [diff] [blame] | 189 | static int matches_locale(const char* loc) { |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 190 | if (locale == NULL) return 0; |
| 191 | |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 192 | if (strcmp(loc, locale) == 0) return 1; |
| 193 | |
| 194 | // if loc does *not* have an underscore, and it matches the start |
| 195 | // of locale, and the next character in locale *is* an underscore, |
| 196 | // that's a match. For instance, loc == "en" matches locale == |
| 197 | // "en_US". |
| 198 | |
| 199 | int i; |
| 200 | for (i = 0; loc[i] != 0 && loc[i] != '_'; ++i); |
| 201 | if (loc[i] == '_') return 0; |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 202 | |
| 203 | return (strncmp(locale, loc, i) == 0 && locale[i] == '_'); |
| 204 | } |
| 205 | |
Ethan Yonker | a33161b | 2014-11-06 15:11:20 -0600 | [diff] [blame] | 206 | int res_create_localized_surface(const char* name, gr_surface* pSurface) { |
| 207 | char resPath[256]; |
| 208 | GGLSurface* surface = NULL; |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 209 | int result = 0; |
Ethan Yonker | a33161b | 2014-11-06 15:11:20 -0600 | [diff] [blame] | 210 | unsigned char header[8]; |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 211 | png_structp png_ptr = NULL; |
| 212 | png_infop info_ptr = NULL; |
| 213 | |
| 214 | *pSurface = NULL; |
| 215 | |
Ethan Yonker | a33161b | 2014-11-06 15:11:20 -0600 | [diff] [blame] | 216 | snprintf(resPath, sizeof(resPath)-1, "/res/images/%s.png", name); |
| 217 | resPath[sizeof(resPath)-1] = '\0'; |
| 218 | FILE* fp = fopen(resPath, "rb"); |
| 219 | if (fp == NULL) { |
| 220 | result = -1; |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 221 | goto exit; |
| 222 | } |
| 223 | |
Ethan Yonker | a33161b | 2014-11-06 15:11:20 -0600 | [diff] [blame] | 224 | size_t bytesRead = fread(header, 1, sizeof(header), fp); |
| 225 | if (bytesRead != sizeof(header)) { |
| 226 | result = -2; |
| 227 | goto exit; |
| 228 | } |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 229 | |
Ethan Yonker | a33161b | 2014-11-06 15:11:20 -0600 | [diff] [blame] | 230 | if (png_sig_cmp(header, 0, sizeof(header))) { |
| 231 | result = -3; |
| 232 | goto exit; |
| 233 | } |
| 234 | |
| 235 | png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); |
| 236 | if (!png_ptr) { |
| 237 | result = -4; |
| 238 | goto exit; |
| 239 | } |
| 240 | |
| 241 | info_ptr = png_create_info_struct(png_ptr); |
| 242 | if (!info_ptr) { |
| 243 | result = -5; |
| 244 | goto exit; |
| 245 | } |
| 246 | |
| 247 | if (setjmp(png_jmpbuf(png_ptr))) { |
| 248 | result = -6; |
| 249 | goto exit; |
| 250 | } |
| 251 | |
| 252 | png_init_io(png_ptr, fp); |
| 253 | png_set_sig_bytes(png_ptr, sizeof(header)); |
| 254 | png_read_info(png_ptr, info_ptr); |
| 255 | |
| 256 | int color_type, bit_depth; |
| 257 | size_t width, height; |
| 258 | |
| 259 | png_get_IHDR(png_ptr, info_ptr, width, height, &bit_depth, |
| 260 | &color_type, NULL, NULL, NULL); |
| 261 | |
| 262 | png_byte* channels = png_get_channels(png_ptr, info_ptr); |
| 263 | size_t stride = 4 * width; |
| 264 | |
| 265 | if (!(bit_depth == 8 && |
| 266 | (channels == 1 && color_type == PNG_COLOR_TYPE_GRAY))) { |
| 267 | return -7; |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 268 | goto exit; |
| 269 | } |
| 270 | |
| 271 | unsigned char* row = malloc(width); |
Ethan Yonker | a33161b | 2014-11-06 15:11:20 -0600 | [diff] [blame] | 272 | int y; |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 273 | for (y = 0; y < height; ++y) { |
| 274 | png_read_row(png_ptr, row, NULL); |
| 275 | int w = (row[1] << 8) | row[0]; |
| 276 | int h = (row[3] << 8) | row[2]; |
| 277 | int len = row[4]; |
Ethan Yonker | a33161b | 2014-11-06 15:11:20 -0600 | [diff] [blame] | 278 | char* loc = row+5; |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 279 | |
Ethan Yonker | a33161b | 2014-11-06 15:11:20 -0600 | [diff] [blame] | 280 | if (y+1+h >= height || matches_locale(loc)) { |
Doug Zongker | 52eeea4f | 2012-09-04 14:28:25 -0700 | [diff] [blame] | 281 | printf(" %20s: %s (%d x %d @ %d)\n", name, loc, w, h, y); |
| 282 | |
Ethan Yonker | a33161b | 2014-11-06 15:11:20 -0600 | [diff] [blame] | 283 | surface = malloc(sizeof(GGLSurface)); |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 284 | if (surface == NULL) { |
| 285 | result = -8; |
| 286 | goto exit; |
| 287 | } |
Ethan Yonker | a33161b | 2014-11-06 15:11:20 -0600 | [diff] [blame] | 288 | unsigned char* pData = malloc(w*h); |
| 289 | |
| 290 | surface->version = sizeof(GGLSurface); |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 291 | surface->width = w; |
| 292 | surface->height = h; |
Ethan Yonker | a33161b | 2014-11-06 15:11:20 -0600 | [diff] [blame] | 293 | surface->stride = w; /* Yes, pixels, not bytes */ |
| 294 | surface->data = pData; |
| 295 | surface->format = GGL_PIXEL_FORMAT_A_8; |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 296 | |
| 297 | int i; |
| 298 | for (i = 0; i < h; ++i, ++y) { |
| 299 | png_read_row(png_ptr, row, NULL); |
Ethan Yonker | a33161b | 2014-11-06 15:11:20 -0600 | [diff] [blame] | 300 | memcpy(pData + i*w, row, w); |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | *pSurface = (gr_surface) surface; |
| 304 | break; |
| 305 | } else { |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 306 | int i; |
| 307 | for (i = 0; i < h; ++i, ++y) { |
| 308 | png_read_row(png_ptr, row, NULL); |
| 309 | } |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | exit: |
| 314 | png_destroy_read_struct(&png_ptr, &info_ptr, NULL); |
Ethan Yonker | a33161b | 2014-11-06 15:11:20 -0600 | [diff] [blame] | 315 | |
| 316 | if (fp != NULL) { |
| 317 | fclose(fp); |
| 318 | } |
| 319 | if (result < 0) { |
| 320 | if (surface) { |
| 321 | free(surface); |
| 322 | } |
| 323 | } |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 324 | return result; |
| 325 | } |
| 326 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 327 | void res_free_surface(gr_surface surface) { |
Ethan Yonker | a33161b | 2014-11-06 15:11:20 -0600 | [diff] [blame] | 328 | GGLSurface* pSurface = (GGLSurface*) surface; |
| 329 | if (pSurface) { |
| 330 | free(pSurface); |
| 331 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 332 | } |