blob: 39f83c76fc8370ee5eea72609afdb8ef62de4eb3 [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
30#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
Doug Zongker19faefa2009-03-27 17:06:24 -070042// 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) {
Edwin Vaneedc5d172012-07-27 11:32:23 -040047 return x * y;
Doug Zongker19faefa2009-03-27 17:06:24 -070048}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080049
50int res_create_surface(const char* name, gr_surface* pSurface) {
51 char resPath[256];
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080052 GGLSurface* surface = NULL;
53 int result = 0;
Doug Zongker19faefa2009-03-27 17:06:24 -070054 unsigned char header[8];
55 png_structp png_ptr = NULL;
56 png_infop info_ptr = NULL;
57
Doug Zongker6809c512011-03-01 14:04:34 -080058 *pSurface = NULL;
59
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
79 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
85 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
91 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
96 png_init_io(png_ptr, fp);
97 png_set_sig_bytes(png_ptr, sizeof(header));
98 png_read_info(png_ptr, info_ptr);
99
Dees Troy62b75ab2014-05-02 13:20:33 +0000100 int color_type = info_ptr->color_type;
101 int bit_depth = info_ptr->bit_depth;
102 int channels = info_ptr->channels;
Doug Zongkerd93a2542009-10-08 16:32:58 -0700103 if (!(bit_depth == 8 &&
104 ((channels == 3 && color_type == PNG_COLOR_TYPE_RGB) ||
105 (channels == 4 && color_type == PNG_COLOR_TYPE_RGBA) ||
Doug Zongker55a36ac2013-03-04 15:49:02 -0800106 (channels == 1 && (color_type == PNG_COLOR_TYPE_PALETTE ||
107 color_type == PNG_COLOR_TYPE_GRAY))))) {
Doug Zongker19faefa2009-03-27 17:06:24 -0700108 return -7;
109 goto exit;
110 }
111
Dees Troy62b75ab2014-05-02 13:20:33 +0000112 size_t width = info_ptr->width;
113 size_t height = info_ptr->height;
Doug Zongker55a36ac2013-03-04 15:49:02 -0800114 size_t stride = (color_type == PNG_COLOR_TYPE_GRAY ? 1 : 4) * width;
115 size_t pixelSize = stride * height;
116
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800117 surface = malloc(sizeof(GGLSurface) + pixelSize);
118 if (surface == NULL) {
Doug Zongker19faefa2009-03-27 17:06:24 -0700119 result = -8;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800120 goto exit;
121 }
122 unsigned char* pData = (unsigned char*) (surface + 1);
123 surface->version = sizeof(GGLSurface);
124 surface->width = width;
125 surface->height = height;
126 surface->stride = width; /* Yes, pixels, not bytes */
127 surface->data = pData;
Doug Zongker55a36ac2013-03-04 15:49:02 -0800128 surface->format = (channels == 3) ? GGL_PIXEL_FORMAT_RGBX_8888 :
129 ((color_type == PNG_COLOR_TYPE_PALETTE ? GGL_PIXEL_FORMAT_RGBA_8888 : GGL_PIXEL_FORMAT_L_8));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800130
Doug Zongker68189f22011-03-04 16:28:48 -0800131 int alpha = 0;
Doug Zongkerd93a2542009-10-08 16:32:58 -0700132 if (color_type == PNG_COLOR_TYPE_PALETTE) {
Doug Zongker68189f22011-03-04 16:28:48 -0800133 png_set_palette_to_rgb(png_ptr);
134 }
135 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
136 png_set_tRNS_to_alpha(png_ptr);
137 alpha = 1;
Doug Zongkerd93a2542009-10-08 16:32:58 -0700138 }
Doug Zongker55a36ac2013-03-04 15:49:02 -0800139 if (color_type == PNG_COLOR_TYPE_GRAY) {
140 alpha = 1;
141 }
Doug Zongkerd93a2542009-10-08 16:32:58 -0700142
Edwin Vaneedc5d172012-07-27 11:32:23 -0400143 unsigned int y;
Doug Zongker68189f22011-03-04 16:28:48 -0800144 if (channels == 3 || (channels == 1 && !alpha)) {
Doug Zongker19faefa2009-03-27 17:06:24 -0700145 for (y = 0; y < height; ++y) {
146 unsigned char* pRow = pData + y * stride;
147 png_read_row(png_ptr, pRow, NULL);
148
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800149 int x;
150 for(x = width - 1; x >= 0; x--) {
151 int sx = x * 3;
152 int dx = x * 4;
Doug Zongker19faefa2009-03-27 17:06:24 -0700153 unsigned char r = pRow[sx];
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800154 unsigned char g = pRow[sx + 1];
Doug Zongker19faefa2009-03-27 17:06:24 -0700155 unsigned char b = pRow[sx + 2];
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800156 unsigned char a = 0xff;
157 pRow[dx ] = r; // r
158 pRow[dx + 1] = g; // g
Doug Zongker19faefa2009-03-27 17:06:24 -0700159 pRow[dx + 2] = b; // b
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800160 pRow[dx + 3] = a;
161 }
162 }
Doug Zongker19faefa2009-03-27 17:06:24 -0700163 } else {
164 for (y = 0; y < height; ++y) {
165 unsigned char* pRow = pData + y * stride;
166 png_read_row(png_ptr, pRow, NULL);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800167 }
168 }
Doug Zongker19faefa2009-03-27 17:06:24 -0700169
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800170 *pSurface = (gr_surface) surface;
171
172exit:
Doug Zongker19faefa2009-03-27 17:06:24 -0700173 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
174
175 if (fp != NULL) {
176 fclose(fp);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800177 }
178 if (result < 0) {
179 if (surface) {
180 free(surface);
181 }
182 }
183 return result;
184}
185
Doug Zongker02ec6b82012-08-22 17:26:40 -0700186static int matches_locale(const char* loc) {
187 if (locale == NULL) return 0;
188
Doug Zongker02ec6b82012-08-22 17:26:40 -0700189 if (strcmp(loc, locale) == 0) return 1;
190
191 // if loc does *not* have an underscore, and it matches the start
192 // of locale, and the next character in locale *is* an underscore,
193 // that's a match. For instance, loc == "en" matches locale ==
194 // "en_US".
195
196 int i;
197 for (i = 0; loc[i] != 0 && loc[i] != '_'; ++i);
198 if (loc[i] == '_') return 0;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700199
200 return (strncmp(locale, loc, i) == 0 && locale[i] == '_');
201}
202
203int res_create_localized_surface(const char* name, gr_surface* pSurface) {
204 char resPath[256];
205 GGLSurface* surface = NULL;
206 int result = 0;
207 unsigned char header[8];
208 png_structp png_ptr = NULL;
209 png_infop info_ptr = NULL;
210
211 *pSurface = NULL;
212
213 snprintf(resPath, sizeof(resPath)-1, "/res/images/%s.png", name);
214 resPath[sizeof(resPath)-1] = '\0';
215 FILE* fp = fopen(resPath, "rb");
216 if (fp == NULL) {
217 result = -1;
218 goto exit;
219 }
220
221 size_t bytesRead = fread(header, 1, sizeof(header), fp);
222 if (bytesRead != sizeof(header)) {
223 result = -2;
224 goto exit;
225 }
226
227 if (png_sig_cmp(header, 0, sizeof(header))) {
228 result = -3;
229 goto exit;
230 }
231
232 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
233 if (!png_ptr) {
234 result = -4;
235 goto exit;
236 }
237
238 info_ptr = png_create_info_struct(png_ptr);
239 if (!info_ptr) {
240 result = -5;
241 goto exit;
242 }
243
244 if (setjmp(png_jmpbuf(png_ptr))) {
245 result = -6;
246 goto exit;
247 }
248
249 png_init_io(png_ptr, fp);
250 png_set_sig_bytes(png_ptr, sizeof(header));
251 png_read_info(png_ptr, info_ptr);
252
Dees Troy62b75ab2014-05-02 13:20:33 +0000253 size_t width = info_ptr->width;
254 size_t height = info_ptr->height;
255 size_t stride = 4 * width;
256
257 int color_type = info_ptr->color_type;
258 int bit_depth = info_ptr->bit_depth;
259 int channels = info_ptr->channels;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700260
261 if (!(bit_depth == 8 &&
262 (channels == 1 && color_type == PNG_COLOR_TYPE_GRAY))) {
263 return -7;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700264 goto exit;
265 }
266
267 unsigned char* row = malloc(width);
268 int y;
269 for (y = 0; y < height; ++y) {
270 png_read_row(png_ptr, row, NULL);
271 int w = (row[1] << 8) | row[0];
272 int h = (row[3] << 8) | row[2];
273 int len = row[4];
274 char* loc = row+5;
275
Doug Zongker02ec6b82012-08-22 17:26:40 -0700276 if (y+1+h >= height || matches_locale(loc)) {
Doug Zongker52eeea4f2012-09-04 14:28:25 -0700277 printf(" %20s: %s (%d x %d @ %d)\n", name, loc, w, h, y);
278
Doug Zongker02ec6b82012-08-22 17:26:40 -0700279 surface = malloc(sizeof(GGLSurface));
280 if (surface == NULL) {
281 result = -8;
282 goto exit;
283 }
284 unsigned char* pData = malloc(w*h);
285
286 surface->version = sizeof(GGLSurface);
287 surface->width = w;
288 surface->height = h;
289 surface->stride = w; /* Yes, pixels, not bytes */
290 surface->data = pData;
291 surface->format = GGL_PIXEL_FORMAT_A_8;
292
293 int i;
294 for (i = 0; i < h; ++i, ++y) {
295 png_read_row(png_ptr, row, NULL);
296 memcpy(pData + i*w, row, w);
297 }
298
299 *pSurface = (gr_surface) surface;
300 break;
301 } else {
Doug Zongker02ec6b82012-08-22 17:26:40 -0700302 int i;
303 for (i = 0; i < h; ++i, ++y) {
304 png_read_row(png_ptr, row, NULL);
305 }
306 }
307 }
308
309exit:
310 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
311
312 if (fp != NULL) {
313 fclose(fp);
314 }
315 if (result < 0) {
316 if (surface) {
317 free(surface);
318 }
319 }
320 return result;
321}
322
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800323void res_free_surface(gr_surface surface) {
324 GGLSurface* pSurface = (GGLSurface*) surface;
325 if (pSurface) {
326 free(pSurface);
327 }
328}