blob: fe1e99926faeca16811569b411e40f91f067e890 [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
Ethan Yonker5a95c3f2014-11-07 13:32:55 -0600103 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 Yonker5a95c3f2014-11-07 13:32:55 -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 Yonker304f32f2014-11-07 10:14:05 -0600189static int open_png(const char* name, png_structp* png_ptr, png_infop* info_ptr,
190 png_uint_32* width, png_uint_32* height, png_byte* channels) {
191 char resPath[256];
192 unsigned char header[8];
193 int result = 0;
194
195 snprintf(resPath, sizeof(resPath)-1, "/res/images/%s.png", name);
196 resPath[sizeof(resPath)-1] = '\0';
197 FILE* fp = fopen(resPath, "rb");
198 if (fp == NULL) {
199 result = -1;
200 goto exit;
201 }
202
203 size_t bytesRead = fread(header, 1, sizeof(header), fp);
204 if (bytesRead != sizeof(header)) {
205 result = -2;
206 goto exit;
207 }
208
209 if (png_sig_cmp(header, 0, sizeof(header))) {
210 result = -3;
211 goto exit;
212 }
213
214 *png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
215 if (!*png_ptr) {
216 result = -4;
217 goto exit;
218 }
219
220 *info_ptr = png_create_info_struct(*png_ptr);
221 if (!*info_ptr) {
222 result = -5;
223 goto exit;
224 }
225
226 if (setjmp(png_jmpbuf(*png_ptr))) {
227 result = -6;
228 goto exit;
229 }
230
231 png_init_io(*png_ptr, fp);
232 png_set_sig_bytes(*png_ptr, sizeof(header));
233 png_read_info(*png_ptr, *info_ptr);
234
235 int color_type, bit_depth;
236 png_get_IHDR(*png_ptr, *info_ptr, width, height, &bit_depth,
237 &color_type, NULL, NULL, NULL);
238
239 *channels = png_get_channels(*png_ptr, *info_ptr);
240
241 if (bit_depth == 8 && *channels == 3 && color_type == PNG_COLOR_TYPE_RGB) {
242 // 8-bit RGB images: great, nothing to do.
243 } else if (bit_depth <= 8 && *channels == 1 && color_type == PNG_COLOR_TYPE_GRAY) {
244 // 1-, 2-, 4-, or 8-bit gray images: expand to 8-bit gray.
245 png_set_expand_gray_1_2_4_to_8(*png_ptr);
246 } else if (bit_depth <= 8 && *channels == 1 && color_type == PNG_COLOR_TYPE_PALETTE) {
247 // paletted images: expand to 8-bit RGB. Note that we DON'T
248 // currently expand the tRNS chunk (if any) to an alpha
249 // channel, because minui doesn't support alpha channels in
250 // general.
251 png_set_palette_to_rgb(*png_ptr);
252 *channels = 3;
253 } else {
254 fprintf(stderr, "minui doesn't support PNG depth %d channels %d color_type %d\n",
255 bit_depth, *channels, color_type);
256 result = -7;
257 goto exit;
258 }
259
260 return result;
261
262 exit:
263 if (result < 0) {
264 png_destroy_read_struct(png_ptr, info_ptr, NULL);
265 }
266 if (fp != NULL) {
267 fclose(fp);
268 }
269
270 return result;
271}
272
273int res_create_multi_display_surface(const char* name, int* frames, gr_surface** pSurface) {
274 gr_surface* surface = NULL;
275 int result = 0;
276 png_structp png_ptr = NULL;
277 png_infop info_ptr = NULL;
278 png_uint_32 width, height;
279 png_byte channels;
280 int i;
281
282 *pSurface = NULL;
283 *frames = -1;
284
285 result = open_png(name, &png_ptr, &info_ptr, &width, &height, &channels);
286 if (result < 0) return result;
287
288 *frames = 1;
289 png_textp text;
290 int num_text;
291 if (png_get_text(png_ptr, info_ptr, &text, &num_text)) {
292 for (i = 0; i < num_text; ++i) {
293 if (text[i].key && strcmp(text[i].key, "Frames") == 0 && text[i].text) {
294 *frames = atoi(text[i].text);
295 break;
296 }
297 }
298 printf(" found frames = %d\n", *frames);
299 }
300
301 if (height % *frames != 0) {
302 printf("bad height (%d) for frame count (%d)\n", height, *frames);
303 result = -9;
304 goto exit;
305 }
306
307 surface = malloc(*frames * sizeof(gr_surface));
308 if (surface == NULL) {
309 result = -8;
310 goto exit;
311 }
312 for (i = 0; i < *frames; ++i) {
313 surface[i] = NULL;//init_display_surface(width, height / *frames);
314 if (surface[i] == NULL) {
315 result = -8;
316 goto exit;
317 }
318 }
319
320 /*unsigned char* p_row = malloc(width * 4);
321 unsigned int y;
322 for (y = 0; y < height; ++y) {
323 png_read_row(png_ptr, p_row, NULL);
324 int frame = y % *frames;
325 unsigned char* out_row = surface[frame]->data +
326 (y / *frames) * surface[frame]->row_bytes;
327 transform_rgb_to_draw(p_row, out_row, channels, width);
328 }
329 free(p_row); this will need to be brought in line with the older resources and graphics code */
330
331 *pSurface = (gr_surface*) surface;
332
333exit:
334 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
335
336 if (result < 0) {
337 if (surface) {
338 for (i = 0; i < *frames; ++i) {
339 if (surface[i]) free(surface[i]);
340 }
341 free(surface);
342 }
343 }
344 return result;
345}
346
Ethan Yonkera33161b2014-11-06 15:11:20 -0600347static int matches_locale(const char* loc) {
Doug Zongker02ec6b82012-08-22 17:26:40 -0700348 if (locale == NULL) return 0;
349
Doug Zongker02ec6b82012-08-22 17:26:40 -0700350 if (strcmp(loc, locale) == 0) return 1;
351
352 // if loc does *not* have an underscore, and it matches the start
353 // of locale, and the next character in locale *is* an underscore,
354 // that's a match. For instance, loc == "en" matches locale ==
355 // "en_US".
356
357 int i;
358 for (i = 0; loc[i] != 0 && loc[i] != '_'; ++i);
359 if (loc[i] == '_') return 0;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700360
361 return (strncmp(locale, loc, i) == 0 && locale[i] == '_');
362}
363
Ethan Yonkera33161b2014-11-06 15:11:20 -0600364int res_create_localized_surface(const char* name, gr_surface* pSurface) {
365 char resPath[256];
366 GGLSurface* surface = NULL;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700367 int result = 0;
Ethan Yonkera33161b2014-11-06 15:11:20 -0600368 unsigned char header[8];
Doug Zongker02ec6b82012-08-22 17:26:40 -0700369 png_structp png_ptr = NULL;
370 png_infop info_ptr = NULL;
371
372 *pSurface = NULL;
373
Ethan Yonkera33161b2014-11-06 15:11:20 -0600374 snprintf(resPath, sizeof(resPath)-1, "/res/images/%s.png", name);
375 resPath[sizeof(resPath)-1] = '\0';
376 FILE* fp = fopen(resPath, "rb");
377 if (fp == NULL) {
378 result = -1;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700379 goto exit;
380 }
381
Ethan Yonkera33161b2014-11-06 15:11:20 -0600382 size_t bytesRead = fread(header, 1, sizeof(header), fp);
383 if (bytesRead != sizeof(header)) {
384 result = -2;
385 goto exit;
386 }
Doug Zongker02ec6b82012-08-22 17:26:40 -0700387
Ethan Yonkera33161b2014-11-06 15:11:20 -0600388 if (png_sig_cmp(header, 0, sizeof(header))) {
389 result = -3;
390 goto exit;
391 }
392
393 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
394 if (!png_ptr) {
395 result = -4;
396 goto exit;
397 }
398
399 info_ptr = png_create_info_struct(png_ptr);
400 if (!info_ptr) {
401 result = -5;
402 goto exit;
403 }
404
405 if (setjmp(png_jmpbuf(png_ptr))) {
406 result = -6;
407 goto exit;
408 }
409
410 png_init_io(png_ptr, fp);
411 png_set_sig_bytes(png_ptr, sizeof(header));
412 png_read_info(png_ptr, info_ptr);
413
414 int color_type, bit_depth;
415 size_t width, height;
416
417 png_get_IHDR(png_ptr, info_ptr, width, height, &bit_depth,
418 &color_type, NULL, NULL, NULL);
419
420 png_byte* channels = png_get_channels(png_ptr, info_ptr);
421 size_t stride = 4 * width;
422
423 if (!(bit_depth == 8 &&
424 (channels == 1 && color_type == PNG_COLOR_TYPE_GRAY))) {
425 return -7;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700426 goto exit;
427 }
428
429 unsigned char* row = malloc(width);
Ethan Yonkera33161b2014-11-06 15:11:20 -0600430 int y;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700431 for (y = 0; y < height; ++y) {
432 png_read_row(png_ptr, row, NULL);
433 int w = (row[1] << 8) | row[0];
434 int h = (row[3] << 8) | row[2];
435 int len = row[4];
Ethan Yonkera33161b2014-11-06 15:11:20 -0600436 char* loc = row+5;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700437
Ethan Yonkera33161b2014-11-06 15:11:20 -0600438 if (y+1+h >= height || matches_locale(loc)) {
Doug Zongker52eeea4f2012-09-04 14:28:25 -0700439 printf(" %20s: %s (%d x %d @ %d)\n", name, loc, w, h, y);
440
Ethan Yonkera33161b2014-11-06 15:11:20 -0600441 surface = malloc(sizeof(GGLSurface));
Doug Zongker02ec6b82012-08-22 17:26:40 -0700442 if (surface == NULL) {
443 result = -8;
444 goto exit;
445 }
Ethan Yonkera33161b2014-11-06 15:11:20 -0600446 unsigned char* pData = malloc(w*h);
447
448 surface->version = sizeof(GGLSurface);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700449 surface->width = w;
450 surface->height = h;
Ethan Yonkera33161b2014-11-06 15:11:20 -0600451 surface->stride = w; /* Yes, pixels, not bytes */
452 surface->data = pData;
453 surface->format = GGL_PIXEL_FORMAT_A_8;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700454
455 int i;
456 for (i = 0; i < h; ++i, ++y) {
457 png_read_row(png_ptr, row, NULL);
Ethan Yonkera33161b2014-11-06 15:11:20 -0600458 memcpy(pData + i*w, row, w);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700459 }
460
461 *pSurface = (gr_surface) surface;
462 break;
463 } else {
Doug Zongker02ec6b82012-08-22 17:26:40 -0700464 int i;
465 for (i = 0; i < h; ++i, ++y) {
466 png_read_row(png_ptr, row, NULL);
467 }
468 }
469 }
470
471exit:
472 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
Ethan Yonkera33161b2014-11-06 15:11:20 -0600473
474 if (fp != NULL) {
475 fclose(fp);
476 }
477 if (result < 0) {
478 if (surface) {
479 free(surface);
480 }
481 }
Doug Zongker02ec6b82012-08-22 17:26:40 -0700482 return result;
483}
484
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800485void res_free_surface(gr_surface surface) {
Ethan Yonkera33161b2014-11-06 15:11:20 -0600486 GGLSurface* pSurface = (GGLSurface*) surface;
487 if (pSurface) {
488 free(pSurface);
489 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800490}