blob: 064325edf344c99374c1b294399636cba8cede59 [file] [log] [blame]
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001/*
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 Yonkera33161b2014-11-06 15:11:20 -060030#include <pixelflinger/pixelflinger.h>
31
Doug Zongker19faefa2009-03-27 17:06:24 -070032#include <png.h>
33
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080034#include "minui.h"
35
Dees Troy62b75ab2014-05-02 13:20:33 +000036#ifdef FASTMMI_FEATURE
37char *locale = NULL;
38#else
Doug Zongker02ec6b82012-08-22 17:26:40 -070039extern char* locale;
Dees Troy62b75ab2014-05-02 13:20:33 +000040#endif
Doug Zongker02ec6b82012-08-22 17:26:40 -070041
Ethan Yonkera33161b2014-11-06 15:11:20 -060042// 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.
46double pow(double x, double y) {
47 return x * y;
Doug Zongker19faefa2009-03-27 17:06:24 -070048}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080049
Ethan Yonkera33161b2014-11-06 15:11:20 -060050int res_create_surface(const char* name, gr_surface* pSurface) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080051 char resPath[256];
Ethan Yonkera33161b2014-11-06 15:11:20 -060052 GGLSurface* surface = NULL;
Doug Zongkera418aa72014-03-17 12:10:02 -070053 int result = 0;
Ethan Yonkera33161b2014-11-06 15:11:20 -060054 unsigned char header[8];
55 png_structp png_ptr = NULL;
56 png_infop info_ptr = NULL;
57
58 *pSurface = NULL;
Doug Zongker6809c512011-03-01 14:04:34 -080059
Doug Zongker19faefa2009-03-27 17:06:24 -070060 snprintf(resPath, sizeof(resPath)-1, "/res/images/%s.png", name);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080061 resPath[sizeof(resPath)-1] = '\0';
Doug Zongker19faefa2009-03-27 17:06:24 -070062 FILE* fp = fopen(resPath, "rb");
63 if (fp == NULL) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080064 result = -1;
65 goto exit;
66 }
Doug Zongker19faefa2009-03-27 17:06:24 -070067
68 size_t bytesRead = fread(header, 1, sizeof(header), fp);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080069 if (bytesRead != sizeof(header)) {
70 result = -2;
71 goto exit;
72 }
Doug Zongker19faefa2009-03-27 17:06:24 -070073
74 if (png_sig_cmp(header, 0, sizeof(header))) {
75 result = -3;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080076 goto exit;
77 }
Doug Zongker19faefa2009-03-27 17:06:24 -070078
Ethan Yonkera33161b2014-11-06 15:11:20 -060079 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
80 if (!png_ptr) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080081 result = -4;
82 goto exit;
83 }
Doug Zongker19faefa2009-03-27 17:06:24 -070084
Ethan Yonkera33161b2014-11-06 15:11:20 -060085 info_ptr = png_create_info_struct(png_ptr);
86 if (!info_ptr) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080087 result = -5;
88 goto exit;
89 }
Doug Zongker19faefa2009-03-27 17:06:24 -070090
Ethan Yonkera33161b2014-11-06 15:11:20 -060091 if (setjmp(png_jmpbuf(png_ptr))) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080092 result = -6;
93 goto exit;
94 }
Doug Zongker19faefa2009-03-27 17:06:24 -070095
Ethan Yonkera33161b2014-11-06 15:11:20 -060096 png_init_io(png_ptr, fp);
97 png_set_sig_bytes(png_ptr, sizeof(header));
98 png_read_info(png_ptr, info_ptr);
Doug Zongker19faefa2009-03-27 17:06:24 -070099
John Reck94fd07b2013-08-26 16:45:33 -0700100 int color_type, bit_depth;
Ethan Yonkera33161b2014-11-06 15:11:20 -0600101 size_t width, height;
102
103 png_get_IHDR(png_ptr, info_ptr, width, height, &bit_depth,
John Reck94fd07b2013-08-26 16:45:33 -0700104 &color_type, NULL, NULL, NULL);
105
Ethan Yonkera33161b2014-11-06 15:11:20 -0600106 png_byte* channels = png_get_channels(png_ptr, info_ptr);
John Reck94fd07b2013-08-26 16:45:33 -0700107
Ethan Yonkera33161b2014-11-06 15:11:20 -0600108 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 Zongker19faefa2009-03-27 17:06:24 -0700114 goto exit;
115 }
116
Ethan Yonkera33161b2014-11-06 15:11:20 -0600117 size_t stride = (color_type == PNG_COLOR_TYPE_GRAY ? 1 : 4) * width;
118 size_t pixelSize = stride * height;
Doug Zongker55a36ac2013-03-04 15:49:02 -0800119
Ethan Yonkera33161b2014-11-06 15:11:20 -0600120 surface = malloc(sizeof(GGLSurface) + pixelSize);
121 if (surface == NULL) {
122 result = -8;
123 goto exit;
Doug Zongkera418aa72014-03-17 12:10:02 -0700124 }
Ethan Yonkera33161b2014-11-06 15:11:20 -0600125 unsigned char* pData = (unsigned char*) (surface + 1);
126 surface->version = sizeof(GGLSurface);
Doug Zongkera418aa72014-03-17 12:10:02 -0700127 surface->width = width;
128 surface->height = height;
Ethan Yonkera33161b2014-11-06 15:11:20 -0600129 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 Zongkera418aa72014-03-17 12:10:02 -0700133
Ethan Yonkera33161b2014-11-06 15:11:20 -0600134 int alpha = 0;
135 if (color_type == PNG_COLOR_TYPE_PALETTE) {
136 png_set_palette_to_rgb(png_ptr);
Doug Zongkera418aa72014-03-17 12:10:02 -0700137 }
Ethan Yonkera33161b2014-11-06 15:11:20 -0600138 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 Projectc24a8e62009-03-03 19:28:42 -0800144 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800145
Edwin Vaneedc5d172012-07-27 11:32:23 -0400146 unsigned int y;
Ethan Yonkera33161b2014-11-06 15:11:20 -0600147 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 Zongker19faefa2009-03-27 17:06:24 -0700151
Ethan Yonkera33161b2014-11-06 15:11:20 -0600152 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 Projectc24a8e62009-03-03 19:28:42 -0800164 }
165 }
Ethan Yonkera33161b2014-11-06 15:11:20 -0600166 } 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 Projectc24a8e62009-03-03 19:28:42 -0800170 }
171 }
Doug Zongker19faefa2009-03-27 17:06:24 -0700172
Ethan Yonkera33161b2014-11-06 15:11:20 -0600173 *pSurface = (gr_surface) surface;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800174
175exit:
Doug Zongker19faefa2009-03-27 17:06:24 -0700176 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
177
Ethan Yonkera33161b2014-11-06 15:11:20 -0600178 if (fp != NULL) {
179 fclose(fp);
180 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800181 if (result < 0) {
182 if (surface) {
183 free(surface);
184 }
185 }
186 return result;
187}
188
Ethan Yonkera33161b2014-11-06 15:11:20 -0600189static int matches_locale(const char* loc) {
Doug Zongker02ec6b82012-08-22 17:26:40 -0700190 if (locale == NULL) return 0;
191
Doug Zongker02ec6b82012-08-22 17:26:40 -0700192 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 Zongker02ec6b82012-08-22 17:26:40 -0700202
203 return (strncmp(locale, loc, i) == 0 && locale[i] == '_');
204}
205
Ethan Yonkera33161b2014-11-06 15:11:20 -0600206int res_create_localized_surface(const char* name, gr_surface* pSurface) {
207 char resPath[256];
208 GGLSurface* surface = NULL;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700209 int result = 0;
Ethan Yonkera33161b2014-11-06 15:11:20 -0600210 unsigned char header[8];
Doug Zongker02ec6b82012-08-22 17:26:40 -0700211 png_structp png_ptr = NULL;
212 png_infop info_ptr = NULL;
213
214 *pSurface = NULL;
215
Ethan Yonkera33161b2014-11-06 15:11:20 -0600216 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 Zongker02ec6b82012-08-22 17:26:40 -0700221 goto exit;
222 }
223
Ethan Yonkera33161b2014-11-06 15:11:20 -0600224 size_t bytesRead = fread(header, 1, sizeof(header), fp);
225 if (bytesRead != sizeof(header)) {
226 result = -2;
227 goto exit;
228 }
Doug Zongker02ec6b82012-08-22 17:26:40 -0700229
Ethan Yonkera33161b2014-11-06 15:11:20 -0600230 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 Zongker02ec6b82012-08-22 17:26:40 -0700268 goto exit;
269 }
270
271 unsigned char* row = malloc(width);
Ethan Yonkera33161b2014-11-06 15:11:20 -0600272 int y;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700273 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 Yonkera33161b2014-11-06 15:11:20 -0600278 char* loc = row+5;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700279
Ethan Yonkera33161b2014-11-06 15:11:20 -0600280 if (y+1+h >= height || matches_locale(loc)) {
Doug Zongker52eeea4f2012-09-04 14:28:25 -0700281 printf(" %20s: %s (%d x %d @ %d)\n", name, loc, w, h, y);
282
Ethan Yonkera33161b2014-11-06 15:11:20 -0600283 surface = malloc(sizeof(GGLSurface));
Doug Zongker02ec6b82012-08-22 17:26:40 -0700284 if (surface == NULL) {
285 result = -8;
286 goto exit;
287 }
Ethan Yonkera33161b2014-11-06 15:11:20 -0600288 unsigned char* pData = malloc(w*h);
289
290 surface->version = sizeof(GGLSurface);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700291 surface->width = w;
292 surface->height = h;
Ethan Yonkera33161b2014-11-06 15:11:20 -0600293 surface->stride = w; /* Yes, pixels, not bytes */
294 surface->data = pData;
295 surface->format = GGL_PIXEL_FORMAT_A_8;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700296
297 int i;
298 for (i = 0; i < h; ++i, ++y) {
299 png_read_row(png_ptr, row, NULL);
Ethan Yonkera33161b2014-11-06 15:11:20 -0600300 memcpy(pData + i*w, row, w);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700301 }
302
303 *pSurface = (gr_surface) surface;
304 break;
305 } else {
Doug Zongker02ec6b82012-08-22 17:26:40 -0700306 int i;
307 for (i = 0; i < h; ++i, ++y) {
308 png_read_row(png_ptr, row, NULL);
309 }
310 }
311 }
312
313exit:
314 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
Ethan Yonkera33161b2014-11-06 15:11:20 -0600315
316 if (fp != NULL) {
317 fclose(fp);
318 }
319 if (result < 0) {
320 if (surface) {
321 free(surface);
322 }
323 }
Doug Zongker02ec6b82012-08-22 17:26:40 -0700324 return result;
325}
326
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800327void res_free_surface(gr_surface surface) {
Ethan Yonkera33161b2014-11-06 15:11:20 -0600328 GGLSurface* pSurface = (GGLSurface*) surface;
329 if (pSurface) {
330 free(pSurface);
331 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800332}