blob: 5ad9b1d05204cf43ecd4bcd23f90290d32b4ccaa [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
Doug Zongker02ec6b82012-08-22 17:26:40 -070036extern char* locale;
37
Doug Zongker19faefa2009-03-27 17:06:24 -070038// libpng gives "undefined reference to 'pow'" errors, and I have no
39// idea how to convince the build system to link with -lm. We don't
40// need this functionality (it's used for gamma adjustment) so provide
41// a dummy implementation to satisfy the linker.
42double pow(double x, double y) {
Edwin Vaneedc5d172012-07-27 11:32:23 -040043 return x * y;
Doug Zongker19faefa2009-03-27 17:06:24 -070044}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080045
46int res_create_surface(const char* name, gr_surface* pSurface) {
47 char resPath[256];
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080048 GGLSurface* surface = NULL;
49 int result = 0;
Doug Zongker19faefa2009-03-27 17:06:24 -070050 unsigned char header[8];
51 png_structp png_ptr = NULL;
52 png_infop info_ptr = NULL;
53
Doug Zongker6809c512011-03-01 14:04:34 -080054 *pSurface = NULL;
55
Doug Zongker19faefa2009-03-27 17:06:24 -070056 snprintf(resPath, sizeof(resPath)-1, "/res/images/%s.png", name);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080057 resPath[sizeof(resPath)-1] = '\0';
Doug Zongker19faefa2009-03-27 17:06:24 -070058 FILE* fp = fopen(resPath, "rb");
59 if (fp == NULL) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080060 result = -1;
61 goto exit;
62 }
Doug Zongker19faefa2009-03-27 17:06:24 -070063
64 size_t bytesRead = fread(header, 1, sizeof(header), fp);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080065 if (bytesRead != sizeof(header)) {
66 result = -2;
67 goto exit;
68 }
Doug Zongker19faefa2009-03-27 17:06:24 -070069
70 if (png_sig_cmp(header, 0, sizeof(header))) {
71 result = -3;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080072 goto exit;
73 }
Doug Zongker19faefa2009-03-27 17:06:24 -070074
75 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
76 if (!png_ptr) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080077 result = -4;
78 goto exit;
79 }
Doug Zongker19faefa2009-03-27 17:06:24 -070080
81 info_ptr = png_create_info_struct(png_ptr);
82 if (!info_ptr) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080083 result = -5;
84 goto exit;
85 }
Doug Zongker19faefa2009-03-27 17:06:24 -070086
87 if (setjmp(png_jmpbuf(png_ptr))) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080088 result = -6;
89 goto exit;
90 }
Doug Zongker19faefa2009-03-27 17:06:24 -070091
92 png_init_io(png_ptr, fp);
93 png_set_sig_bytes(png_ptr, sizeof(header));
94 png_read_info(png_ptr, info_ptr);
95
John Reck94fd07b2013-08-26 16:45:33 -070096 int color_type, bit_depth;
97 size_t width, height;
98 png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth,
99 &color_type, NULL, NULL, NULL);
100
101 int channels = png_get_channels(png_ptr, info_ptr);
102
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
Doug Zongker55a36ac2013-03-04 15:49:02 -0800112 size_t stride = (color_type == PNG_COLOR_TYPE_GRAY ? 1 : 4) * width;
113 size_t pixelSize = stride * height;
114
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800115 surface = malloc(sizeof(GGLSurface) + pixelSize);
116 if (surface == NULL) {
Doug Zongker19faefa2009-03-27 17:06:24 -0700117 result = -8;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800118 goto exit;
119 }
120 unsigned char* pData = (unsigned char*) (surface + 1);
121 surface->version = sizeof(GGLSurface);
122 surface->width = width;
123 surface->height = height;
124 surface->stride = width; /* Yes, pixels, not bytes */
125 surface->data = pData;
Doug Zongker55a36ac2013-03-04 15:49:02 -0800126 surface->format = (channels == 3) ? GGL_PIXEL_FORMAT_RGBX_8888 :
127 ((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 -0800128
Doug Zongker68189f22011-03-04 16:28:48 -0800129 int alpha = 0;
Doug Zongkerd93a2542009-10-08 16:32:58 -0700130 if (color_type == PNG_COLOR_TYPE_PALETTE) {
Doug Zongker68189f22011-03-04 16:28:48 -0800131 png_set_palette_to_rgb(png_ptr);
132 }
133 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
134 png_set_tRNS_to_alpha(png_ptr);
135 alpha = 1;
Doug Zongkerd93a2542009-10-08 16:32:58 -0700136 }
Doug Zongker55a36ac2013-03-04 15:49:02 -0800137 if (color_type == PNG_COLOR_TYPE_GRAY) {
138 alpha = 1;
139 }
Doug Zongkerd93a2542009-10-08 16:32:58 -0700140
Edwin Vaneedc5d172012-07-27 11:32:23 -0400141 unsigned int y;
Doug Zongker68189f22011-03-04 16:28:48 -0800142 if (channels == 3 || (channels == 1 && !alpha)) {
Doug Zongker19faefa2009-03-27 17:06:24 -0700143 for (y = 0; y < height; ++y) {
144 unsigned char* pRow = pData + y * stride;
145 png_read_row(png_ptr, pRow, NULL);
146
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800147 int x;
148 for(x = width - 1; x >= 0; x--) {
149 int sx = x * 3;
150 int dx = x * 4;
Doug Zongker19faefa2009-03-27 17:06:24 -0700151 unsigned char r = pRow[sx];
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800152 unsigned char g = pRow[sx + 1];
Doug Zongker19faefa2009-03-27 17:06:24 -0700153 unsigned char b = pRow[sx + 2];
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800154 unsigned char a = 0xff;
155 pRow[dx ] = r; // r
156 pRow[dx + 1] = g; // g
Doug Zongker19faefa2009-03-27 17:06:24 -0700157 pRow[dx + 2] = b; // b
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800158 pRow[dx + 3] = a;
159 }
160 }
Doug Zongker19faefa2009-03-27 17:06:24 -0700161 } else {
162 for (y = 0; y < height; ++y) {
163 unsigned char* pRow = pData + y * stride;
164 png_read_row(png_ptr, pRow, NULL);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800165 }
166 }
Doug Zongker19faefa2009-03-27 17:06:24 -0700167
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800168 *pSurface = (gr_surface) surface;
169
170exit:
Doug Zongker19faefa2009-03-27 17:06:24 -0700171 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
172
173 if (fp != NULL) {
174 fclose(fp);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800175 }
176 if (result < 0) {
177 if (surface) {
178 free(surface);
179 }
180 }
181 return result;
182}
183
Doug Zongker469954f2014-03-07 09:21:25 -0800184int res_create_multi_surface(const char* name, int* frames, gr_surface** pSurface) {
185 char resPath[256];
186 int result = 0;
187 unsigned char header[8];
188 png_structp png_ptr = NULL;
189 png_infop info_ptr = NULL;
190 int i;
191 GGLSurface** surface = NULL;
192
193 *pSurface = NULL;
194 *frames = -1;
195
196 snprintf(resPath, sizeof(resPath)-1, "/res/images/%s.png", name);
197 resPath[sizeof(resPath)-1] = '\0';
198 FILE* fp = fopen(resPath, "rb");
199 if (fp == NULL) {
200 result = -1;
201 goto exit;
202 }
203
204 size_t bytesRead = fread(header, 1, sizeof(header), fp);
205 if (bytesRead != sizeof(header)) {
206 result = -2;
207 goto exit;
208 }
209
210 if (png_sig_cmp(header, 0, sizeof(header))) {
211 result = -3;
212 goto exit;
213 }
214
215 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
216 if (!png_ptr) {
217 result = -4;
218 goto exit;
219 }
220
221 info_ptr = png_create_info_struct(png_ptr);
222 if (!info_ptr) {
223 result = -5;
224 goto exit;
225 }
226
227 if (setjmp(png_jmpbuf(png_ptr))) {
228 result = -6;
229 goto exit;
230 }
231
232 png_init_io(png_ptr, fp);
233 png_set_sig_bytes(png_ptr, sizeof(header));
234 png_read_info(png_ptr, info_ptr);
235
236 int color_type, bit_depth;
237 png_uint_32 width, height;
238 png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth,
239 &color_type, NULL, NULL, NULL);
240
241 int channels = png_get_channels(png_ptr, info_ptr);
242
243 if (!(bit_depth == 8 &&
244 ((channels == 3 && color_type == PNG_COLOR_TYPE_RGB) ||
245 (channels == 4 && color_type == PNG_COLOR_TYPE_RGBA) ||
246 (channels == 1 && (color_type == PNG_COLOR_TYPE_PALETTE ||
247 color_type == PNG_COLOR_TYPE_GRAY))))) {
248 return -7;
249 goto exit;
250 }
251
252 *frames = 1;
253 png_textp text;
254 int num_text;
255 if (png_get_text(png_ptr, info_ptr, &text, &num_text)) {
256 for (i = 0; i < num_text; ++i) {
257 if (text[i].key && strcmp(text[i].key, "Frames") == 0 && text[i].text) {
258 *frames = atoi(text[i].text);
259 break;
260 }
261 }
262 printf(" found frames = %d\n", *frames);
263 }
264
265 if (height % *frames != 0) {
266 printf("bad height (%d) for frame count (%d)\n", height, *frames);
267 result = -9;
268 goto exit;
269 }
270
271 size_t stride = (color_type == PNG_COLOR_TYPE_GRAY ? 1 : 4) * width;
272 size_t pixelSize = stride * height / *frames;
273
274 surface = malloc(*frames * sizeof(GGLSurface*));
275 if (surface == NULL) {
276 result = -8;
277 goto exit;
278 }
279 for (i = 0; i < *frames; ++i) {
280 surface[i] = malloc(sizeof(GGLSurface) + pixelSize);
281 surface[i]->version = sizeof(GGLSurface);
282 surface[i]->width = width;
283 surface[i]->height = height / *frames;
284 surface[i]->stride = width; /* Yes, pixels, not bytes */
285 surface[i]->data = (unsigned char*) (surface[i] + 1);
286
287 if (channels == 3) {
288 surface[i]->format = GGL_PIXEL_FORMAT_RGBX_8888;
289 } else if (color_type == PNG_COLOR_TYPE_PALETTE) {
290 surface[i]->format = GGL_PIXEL_FORMAT_RGBA_8888;
291 } else if (channels == 1) {
292 surface[i]->format = GGL_PIXEL_FORMAT_L_8;
293 } else {
294 surface[i]->format = GGL_PIXEL_FORMAT_RGBA_8888;
295 }
296 }
297
298 int alpha = (channels == 4);
299 if (color_type == PNG_COLOR_TYPE_PALETTE) {
300 png_set_palette_to_rgb(png_ptr);
301 }
302 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
303 png_set_tRNS_to_alpha(png_ptr);
304 alpha = 1;
305 }
306 if (color_type == PNG_COLOR_TYPE_GRAY) {
307 alpha = 1;
308 }
309
310 png_uint_32 y;
311 if (channels == 3 || (channels == 1 && !alpha)) {
312 for (y = 0; y < height; ++y) {
313 int fy = y / *frames;
314 int fr = y % *frames;
315 unsigned char* pRow = surface[fr]->data + fy * stride;
316 png_read_row(png_ptr, pRow, NULL);
317
318 int x;
319 for(x = width - 1; x >= 0; x--) {
320 int sx = x * 3;
321 int dx = x * 4;
322 unsigned char r = pRow[sx];
323 unsigned char g = pRow[sx + 1];
324 unsigned char b = pRow[sx + 2];
325 unsigned char a = 0xff;
326 pRow[dx ] = r; // r
327 pRow[dx + 1] = g; // g
328 pRow[dx + 2] = b; // b
329 pRow[dx + 3] = a;
330 }
331 }
332 } else {
333 for (y = 0; y < height; ++y) {
334 int fy = y / *frames;
335 int fr = y % *frames;
336 unsigned char* pRow = surface[fr]->data + fy * stride;
337 png_read_row(png_ptr, pRow, NULL);
338 }
339 }
340
341 *pSurface = (gr_surface*) surface;
342
343exit:
344 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
345
346 if (fp != NULL) {
347 fclose(fp);
348 }
349 if (result < 0) {
350 if (surface) {
351 for (i = 0; i < *frames; ++i) {
352 if (surface[i]) free(surface[i]);
353 }
354 free(surface);
355 }
356 }
357 return result;
358}
359
Doug Zongker02ec6b82012-08-22 17:26:40 -0700360static int matches_locale(const char* loc) {
361 if (locale == NULL) return 0;
362
Doug Zongker02ec6b82012-08-22 17:26:40 -0700363 if (strcmp(loc, locale) == 0) return 1;
364
365 // if loc does *not* have an underscore, and it matches the start
366 // of locale, and the next character in locale *is* an underscore,
367 // that's a match. For instance, loc == "en" matches locale ==
368 // "en_US".
369
370 int i;
371 for (i = 0; loc[i] != 0 && loc[i] != '_'; ++i);
372 if (loc[i] == '_') return 0;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700373
374 return (strncmp(locale, loc, i) == 0 && locale[i] == '_');
375}
376
377int res_create_localized_surface(const char* name, gr_surface* pSurface) {
378 char resPath[256];
379 GGLSurface* surface = NULL;
380 int result = 0;
381 unsigned char header[8];
382 png_structp png_ptr = NULL;
383 png_infop info_ptr = NULL;
384
385 *pSurface = NULL;
386
387 snprintf(resPath, sizeof(resPath)-1, "/res/images/%s.png", name);
388 resPath[sizeof(resPath)-1] = '\0';
389 FILE* fp = fopen(resPath, "rb");
390 if (fp == NULL) {
391 result = -1;
392 goto exit;
393 }
394
395 size_t bytesRead = fread(header, 1, sizeof(header), fp);
396 if (bytesRead != sizeof(header)) {
397 result = -2;
398 goto exit;
399 }
400
401 if (png_sig_cmp(header, 0, sizeof(header))) {
402 result = -3;
403 goto exit;
404 }
405
406 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
407 if (!png_ptr) {
408 result = -4;
409 goto exit;
410 }
411
412 info_ptr = png_create_info_struct(png_ptr);
413 if (!info_ptr) {
414 result = -5;
415 goto exit;
416 }
417
418 if (setjmp(png_jmpbuf(png_ptr))) {
419 result = -6;
420 goto exit;
421 }
422
423 png_init_io(png_ptr, fp);
424 png_set_sig_bytes(png_ptr, sizeof(header));
425 png_read_info(png_ptr, info_ptr);
426
John Reck94fd07b2013-08-26 16:45:33 -0700427 int color_type, bit_depth;
Doug Zongker469954f2014-03-07 09:21:25 -0800428 png_uint_32 width, height;
John Reck94fd07b2013-08-26 16:45:33 -0700429 png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth,
430 &color_type, NULL, NULL, NULL);
431 int channels = png_get_channels(png_ptr, info_ptr);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700432
433 if (!(bit_depth == 8 &&
434 (channels == 1 && color_type == PNG_COLOR_TYPE_GRAY))) {
435 return -7;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700436 goto exit;
437 }
438
439 unsigned char* row = malloc(width);
Doug Zongker469954f2014-03-07 09:21:25 -0800440 png_uint_32 y;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700441 for (y = 0; y < height; ++y) {
442 png_read_row(png_ptr, row, NULL);
443 int w = (row[1] << 8) | row[0];
444 int h = (row[3] << 8) | row[2];
445 int len = row[4];
Doug Zongker469954f2014-03-07 09:21:25 -0800446 char* loc = (char*)row+5;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700447
Doug Zongker02ec6b82012-08-22 17:26:40 -0700448 if (y+1+h >= height || matches_locale(loc)) {
Doug Zongker52eeea4f2012-09-04 14:28:25 -0700449 printf(" %20s: %s (%d x %d @ %d)\n", name, loc, w, h, y);
450
Doug Zongker02ec6b82012-08-22 17:26:40 -0700451 surface = malloc(sizeof(GGLSurface));
452 if (surface == NULL) {
453 result = -8;
454 goto exit;
455 }
456 unsigned char* pData = malloc(w*h);
457
458 surface->version = sizeof(GGLSurface);
459 surface->width = w;
460 surface->height = h;
461 surface->stride = w; /* Yes, pixels, not bytes */
462 surface->data = pData;
463 surface->format = GGL_PIXEL_FORMAT_A_8;
464
465 int i;
466 for (i = 0; i < h; ++i, ++y) {
467 png_read_row(png_ptr, row, NULL);
468 memcpy(pData + i*w, row, w);
469 }
470
471 *pSurface = (gr_surface) surface;
472 break;
473 } else {
Doug Zongker02ec6b82012-08-22 17:26:40 -0700474 int i;
475 for (i = 0; i < h; ++i, ++y) {
476 png_read_row(png_ptr, row, NULL);
477 }
478 }
479 }
480
481exit:
482 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
483
484 if (fp != NULL) {
485 fclose(fp);
486 }
487 if (result < 0) {
488 if (surface) {
489 free(surface);
490 }
491 }
492 return result;
493}
494
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800495void res_free_surface(gr_surface surface) {
496 GGLSurface* pSurface = (GGLSurface*) surface;
497 if (pSurface) {
498 free(pSurface);
499 }
500}