blob: 886c3255d820bd15084363e578efb925765498b6 [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>
Elliott Hughescd3c55a2015-01-29 20:50:08 -080018#include <string.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080019#include <unistd.h>
20
21#include <fcntl.h>
22#include <stdio.h>
23
24#include <sys/ioctl.h>
25#include <sys/mman.h>
26#include <sys/types.h>
27
28#include <linux/fb.h>
29#include <linux/kd.h>
30
Doug Zongker19faefa2009-03-27 17:06:24 -070031#include <png.h>
32
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080033#include "minui.h"
34
Doug Zongker02ec6b82012-08-22 17:26:40 -070035extern char* locale;
36
Doug Zongker16f97c32014-03-06 16:16:05 -080037#define SURFACE_DATA_ALIGNMENT 8
38
39static gr_surface malloc_surface(size_t data_size) {
40 unsigned char* temp = malloc(sizeof(GRSurface) + data_size + SURFACE_DATA_ALIGNMENT);
41 if (temp == NULL) return NULL;
42 gr_surface surface = (gr_surface) temp;
43 surface->data = temp + sizeof(GRSurface) +
44 (SURFACE_DATA_ALIGNMENT - (sizeof(GRSurface) % SURFACE_DATA_ALIGNMENT));
45 return surface;
Doug Zongker19faefa2009-03-27 17:06:24 -070046}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080047
Doug Zongkera418aa72014-03-17 12:10:02 -070048static int open_png(const char* name, png_structp* png_ptr, png_infop* info_ptr,
49 png_uint_32* width, png_uint_32* height, png_byte* channels) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080050 char resPath[256];
Doug Zongker19faefa2009-03-27 17:06:24 -070051 unsigned char header[8];
Doug Zongkera418aa72014-03-17 12:10:02 -070052 int result = 0;
Doug Zongker6809c512011-03-01 14:04:34 -080053
Doug Zongker19faefa2009-03-27 17:06:24 -070054 snprintf(resPath, sizeof(resPath)-1, "/res/images/%s.png", name);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080055 resPath[sizeof(resPath)-1] = '\0';
Doug Zongker19faefa2009-03-27 17:06:24 -070056 FILE* fp = fopen(resPath, "rb");
57 if (fp == NULL) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080058 result = -1;
59 goto exit;
60 }
Doug Zongker19faefa2009-03-27 17:06:24 -070061
62 size_t bytesRead = fread(header, 1, sizeof(header), fp);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080063 if (bytesRead != sizeof(header)) {
64 result = -2;
65 goto exit;
66 }
Doug Zongker19faefa2009-03-27 17:06:24 -070067
68 if (png_sig_cmp(header, 0, sizeof(header))) {
69 result = -3;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080070 goto exit;
71 }
Doug Zongker19faefa2009-03-27 17:06:24 -070072
Doug Zongkera418aa72014-03-17 12:10:02 -070073 *png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
74 if (!*png_ptr) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080075 result = -4;
76 goto exit;
77 }
Doug Zongker19faefa2009-03-27 17:06:24 -070078
Doug Zongkera418aa72014-03-17 12:10:02 -070079 *info_ptr = png_create_info_struct(*png_ptr);
80 if (!*info_ptr) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080081 result = -5;
82 goto exit;
83 }
Doug Zongker19faefa2009-03-27 17:06:24 -070084
Doug Zongkera418aa72014-03-17 12:10:02 -070085 if (setjmp(png_jmpbuf(*png_ptr))) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080086 result = -6;
87 goto exit;
88 }
Doug Zongker19faefa2009-03-27 17:06:24 -070089
Doug Zongkera418aa72014-03-17 12:10:02 -070090 png_init_io(*png_ptr, fp);
91 png_set_sig_bytes(*png_ptr, sizeof(header));
92 png_read_info(*png_ptr, *info_ptr);
Doug Zongker19faefa2009-03-27 17:06:24 -070093
John Reck94fd07b2013-08-26 16:45:33 -070094 int color_type, bit_depth;
Doug Zongkera418aa72014-03-17 12:10:02 -070095 png_get_IHDR(*png_ptr, *info_ptr, width, height, &bit_depth,
John Reck94fd07b2013-08-26 16:45:33 -070096 &color_type, NULL, NULL, NULL);
97
Doug Zongkera418aa72014-03-17 12:10:02 -070098 *channels = png_get_channels(*png_ptr, *info_ptr);
John Reck94fd07b2013-08-26 16:45:33 -070099
Doug Zongkera418aa72014-03-17 12:10:02 -0700100 if (bit_depth == 8 && *channels == 3 && color_type == PNG_COLOR_TYPE_RGB) {
101 // 8-bit RGB images: great, nothing to do.
102 } else if (bit_depth <= 8 && *channels == 1 && color_type == PNG_COLOR_TYPE_GRAY) {
103 // 1-, 2-, 4-, or 8-bit gray images: expand to 8-bit gray.
104 png_set_expand_gray_1_2_4_to_8(*png_ptr);
Doug Zongker577a1302014-03-20 08:27:01 -0700105 } else if (bit_depth <= 8 && *channels == 1 && color_type == PNG_COLOR_TYPE_PALETTE) {
Doug Zongkera418aa72014-03-17 12:10:02 -0700106 // paletted images: expand to 8-bit RGB. Note that we DON'T
107 // currently expand the tRNS chunk (if any) to an alpha
108 // channel, because minui doesn't support alpha channels in
109 // general.
110 png_set_palette_to_rgb(*png_ptr);
111 *channels = 3;
112 } else {
113 fprintf(stderr, "minui doesn't support PNG depth %d channels %d color_type %d\n",
Doug Zongkera388a762014-03-17 16:51:47 -0700114 bit_depth, *channels, color_type);
Doug Zongkera418aa72014-03-17 12:10:02 -0700115 result = -7;
Doug Zongker19faefa2009-03-27 17:06:24 -0700116 goto exit;
117 }
118
Doug Zongkera418aa72014-03-17 12:10:02 -0700119 return result;
Doug Zongker55a36ac2013-03-04 15:49:02 -0800120
Doug Zongkera418aa72014-03-17 12:10:02 -0700121 exit:
122 if (result < 0) {
123 png_destroy_read_struct(png_ptr, info_ptr, NULL);
124 }
125 if (fp != NULL) {
126 fclose(fp);
127 }
128
129 return result;
130}
131
132// "display" surfaces are transformed into the framebuffer's required
133// pixel format (currently only RGBX is supported) at load time, so
134// gr_blit() can be nothing more than a memcpy() for each row. The
135// next two functions are the only ones that know anything about the
136// framebuffer pixel format; they need to be modified if the
137// framebuffer format changes (but nothing else should).
138
139// Allocate and return a gr_surface sufficient for storing an image of
140// the indicated size in the framebuffer pixel format.
141static gr_surface init_display_surface(png_uint_32 width, png_uint_32 height) {
142 gr_surface surface;
143
144 surface = malloc_surface(width * height * 4);
145 if (surface == NULL) return NULL;
146
147 surface->width = width;
148 surface->height = height;
149 surface->row_bytes = width * 4;
150 surface->pixel_bytes = 4;
151
152 return surface;
153}
154
155// Copy 'input_row' to 'output_row', transforming it to the
156// framebuffer pixel format. The input format depends on the value of
157// 'channels':
158//
159// 1 - input is 8-bit grayscale
160// 3 - input is 24-bit RGB
161// 4 - input is 32-bit RGBA/RGBX
162//
163// 'width' is the number of pixels in the row.
164static void transform_rgb_to_draw(unsigned char* input_row,
165 unsigned char* output_row,
166 int channels, int width) {
167 int x;
168 unsigned char* ip = input_row;
169 unsigned char* op = output_row;
170
171 switch (channels) {
172 case 1:
173 // expand gray level to RGBX
174 for (x = 0; x < width; ++x) {
175 *op++ = *ip;
176 *op++ = *ip;
177 *op++ = *ip;
178 *op++ = 0xff;
179 ip++;
180 }
181 break;
182
183 case 3:
184 // expand RGBA to RGBX
185 for (x = 0; x < width; ++x) {
186 *op++ = *ip++;
187 *op++ = *ip++;
188 *op++ = *ip++;
189 *op++ = 0xff;
190 }
191 break;
192
193 case 4:
194 // copy RGBA to RGBX
195 memcpy(output_row, input_row, width*4);
196 break;
197 }
198}
199
200int res_create_display_surface(const char* name, gr_surface* pSurface) {
201 gr_surface surface = NULL;
202 int result = 0;
203 png_structp png_ptr = NULL;
204 png_infop info_ptr = NULL;
205 png_uint_32 width, height;
206 png_byte channels;
207
208 *pSurface = NULL;
209
210 result = open_png(name, &png_ptr, &info_ptr, &width, &height, &channels);
211 if (result < 0) return result;
212
213 surface = init_display_surface(width, height);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800214 if (surface == NULL) {
Doug Zongker19faefa2009-03-27 17:06:24 -0700215 result = -8;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800216 goto exit;
217 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800218
Tony Kuofd778e32015-02-05 21:25:56 +0800219#if defined(RECOVERY_ABGR) || defined(RECOVERY_BGRA)
220 png_set_bgr(png_ptr);
221#endif
222
Doug Zongkera418aa72014-03-17 12:10:02 -0700223 unsigned char* p_row = malloc(width * 4);
Edwin Vaneedc5d172012-07-27 11:32:23 -0400224 unsigned int y;
Doug Zongkera418aa72014-03-17 12:10:02 -0700225 for (y = 0; y < height; ++y) {
226 png_read_row(png_ptr, p_row, NULL);
227 transform_rgb_to_draw(p_row, surface->data + y * surface->row_bytes, channels, width);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800228 }
Doug Zongkera418aa72014-03-17 12:10:02 -0700229 free(p_row);
Doug Zongker19faefa2009-03-27 17:06:24 -0700230
Doug Zongkera418aa72014-03-17 12:10:02 -0700231 *pSurface = surface;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800232
Doug Zongkera418aa72014-03-17 12:10:02 -0700233 exit:
Doug Zongker19faefa2009-03-27 17:06:24 -0700234 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
Doug Zongkera418aa72014-03-17 12:10:02 -0700235 if (result < 0 && surface != NULL) free(surface);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800236 return result;
237}
238
Doug Zongkera418aa72014-03-17 12:10:02 -0700239int res_create_multi_display_surface(const char* name, int* frames, gr_surface** pSurface) {
240 gr_surface* surface = NULL;
Doug Zongker469954f2014-03-07 09:21:25 -0800241 int result = 0;
Doug Zongker469954f2014-03-07 09:21:25 -0800242 png_structp png_ptr = NULL;
243 png_infop info_ptr = NULL;
Doug Zongkera418aa72014-03-17 12:10:02 -0700244 png_uint_32 width, height;
245 png_byte channels;
Doug Zongker469954f2014-03-07 09:21:25 -0800246 int i;
Doug Zongker469954f2014-03-07 09:21:25 -0800247
248 *pSurface = NULL;
249 *frames = -1;
250
Doug Zongkera418aa72014-03-17 12:10:02 -0700251 result = open_png(name, &png_ptr, &info_ptr, &width, &height, &channels);
252 if (result < 0) return result;
Doug Zongker469954f2014-03-07 09:21:25 -0800253
254 *frames = 1;
255 png_textp text;
256 int num_text;
257 if (png_get_text(png_ptr, info_ptr, &text, &num_text)) {
258 for (i = 0; i < num_text; ++i) {
259 if (text[i].key && strcmp(text[i].key, "Frames") == 0 && text[i].text) {
260 *frames = atoi(text[i].text);
261 break;
262 }
263 }
264 printf(" found frames = %d\n", *frames);
265 }
266
267 if (height % *frames != 0) {
268 printf("bad height (%d) for frame count (%d)\n", height, *frames);
269 result = -9;
270 goto exit;
271 }
272
Doug Zongker16f97c32014-03-06 16:16:05 -0800273 surface = malloc(*frames * sizeof(gr_surface));
Doug Zongker469954f2014-03-07 09:21:25 -0800274 if (surface == NULL) {
275 result = -8;
276 goto exit;
277 }
278 for (i = 0; i < *frames; ++i) {
Doug Zongkera418aa72014-03-17 12:10:02 -0700279 surface[i] = init_display_surface(width, height / *frames);
280 if (surface[i] == NULL) {
281 result = -8;
282 goto exit;
Doug Zongker469954f2014-03-07 09:21:25 -0800283 }
284 }
285
Tony Kuofd778e32015-02-05 21:25:56 +0800286#if defined(RECOVERY_ABGR) || defined(RECOVERY_BGRA)
287 png_set_bgr(png_ptr);
288#endif
289
Doug Zongkera418aa72014-03-17 12:10:02 -0700290 unsigned char* p_row = malloc(width * 4);
291 unsigned int y;
292 for (y = 0; y < height; ++y) {
293 png_read_row(png_ptr, p_row, NULL);
294 int frame = y % *frames;
295 unsigned char* out_row = surface[frame]->data +
296 (y / *frames) * surface[frame]->row_bytes;
297 transform_rgb_to_draw(p_row, out_row, channels, width);
298 }
299 free(p_row);
300
Doug Zongker469954f2014-03-07 09:21:25 -0800301 *pSurface = (gr_surface*) surface;
302
303exit:
304 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
305
Doug Zongker469954f2014-03-07 09:21:25 -0800306 if (result < 0) {
307 if (surface) {
308 for (i = 0; i < *frames; ++i) {
309 if (surface[i]) free(surface[i]);
310 }
311 free(surface);
312 }
313 }
314 return result;
315}
316
Doug Zongkera418aa72014-03-17 12:10:02 -0700317int res_create_alpha_surface(const char* name, gr_surface* pSurface) {
318 gr_surface surface = NULL;
319 int result = 0;
320 png_structp png_ptr = NULL;
321 png_infop info_ptr = NULL;
322 png_uint_32 width, height;
323 png_byte channels;
324
325 *pSurface = NULL;
326
327 result = open_png(name, &png_ptr, &info_ptr, &width, &height, &channels);
328 if (result < 0) return result;
329
330 if (channels != 1) {
331 result = -7;
332 goto exit;
333 }
334
335 surface = malloc_surface(width * height);
336 if (surface == NULL) {
337 result = -8;
338 goto exit;
339 }
340 surface->width = width;
341 surface->height = height;
342 surface->row_bytes = width;
343 surface->pixel_bytes = 1;
344
Tony Kuofd778e32015-02-05 21:25:56 +0800345#if defined(RECOVERY_ABGR) || defined(RECOVERY_BGRA)
346 png_set_bgr(png_ptr);
347#endif
348
Doug Zongkera418aa72014-03-17 12:10:02 -0700349 unsigned char* p_row;
350 unsigned int y;
351 for (y = 0; y < height; ++y) {
352 p_row = surface->data + y * surface->row_bytes;
353 png_read_row(png_ptr, p_row, NULL);
354 }
355
356 *pSurface = surface;
357
358 exit:
359 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
360 if (result < 0 && surface != NULL) free(surface);
361 return result;
362}
363
364static int matches_locale(const char* loc, const char* locale) {
Doug Zongker02ec6b82012-08-22 17:26:40 -0700365 if (locale == NULL) return 0;
366
Doug Zongker02ec6b82012-08-22 17:26:40 -0700367 if (strcmp(loc, locale) == 0) return 1;
368
369 // if loc does *not* have an underscore, and it matches the start
370 // of locale, and the next character in locale *is* an underscore,
371 // that's a match. For instance, loc == "en" matches locale ==
372 // "en_US".
373
374 int i;
375 for (i = 0; loc[i] != 0 && loc[i] != '_'; ++i);
376 if (loc[i] == '_') return 0;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700377
378 return (strncmp(locale, loc, i) == 0 && locale[i] == '_');
379}
380
Doug Zongkera418aa72014-03-17 12:10:02 -0700381int res_create_localized_alpha_surface(const char* name,
382 const char* locale,
383 gr_surface* pSurface) {
Doug Zongker16f97c32014-03-06 16:16:05 -0800384 gr_surface surface = NULL;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700385 int result = 0;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700386 png_structp png_ptr = NULL;
387 png_infop info_ptr = NULL;
Doug Zongkera418aa72014-03-17 12:10:02 -0700388 png_uint_32 width, height;
389 png_byte channels;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700390
391 *pSurface = NULL;
392
Doug Zongkera418aa72014-03-17 12:10:02 -0700393 if (locale == NULL) {
394 surface = malloc_surface(0);
395 surface->width = 0;
396 surface->height = 0;
397 surface->row_bytes = 0;
398 surface->pixel_bytes = 1;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700399 goto exit;
400 }
401
Doug Zongkera418aa72014-03-17 12:10:02 -0700402 result = open_png(name, &png_ptr, &info_ptr, &width, &height, &channels);
403 if (result < 0) return result;
404
405 if (channels != 1) {
406 result = -7;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700407 goto exit;
408 }
409
Doug Zongker02ec6b82012-08-22 17:26:40 -0700410 unsigned char* row = malloc(width);
Doug Zongker469954f2014-03-07 09:21:25 -0800411 png_uint_32 y;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700412 for (y = 0; y < height; ++y) {
413 png_read_row(png_ptr, row, NULL);
414 int w = (row[1] << 8) | row[0];
415 int h = (row[3] << 8) | row[2];
416 int len = row[4];
Doug Zongker469954f2014-03-07 09:21:25 -0800417 char* loc = (char*)row+5;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700418
Doug Zongkera418aa72014-03-17 12:10:02 -0700419 if (y+1+h >= height || matches_locale(loc, locale)) {
Doug Zongker52eeea4f2012-09-04 14:28:25 -0700420 printf(" %20s: %s (%d x %d @ %d)\n", name, loc, w, h, y);
421
Doug Zongker16f97c32014-03-06 16:16:05 -0800422 surface = malloc_surface(w*h);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700423 if (surface == NULL) {
424 result = -8;
425 goto exit;
426 }
Doug Zongker02ec6b82012-08-22 17:26:40 -0700427 surface->width = w;
428 surface->height = h;
Doug Zongker16f97c32014-03-06 16:16:05 -0800429 surface->row_bytes = w;
430 surface->pixel_bytes = 1;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700431
432 int i;
433 for (i = 0; i < h; ++i, ++y) {
434 png_read_row(png_ptr, row, NULL);
Doug Zongkera418aa72014-03-17 12:10:02 -0700435 memcpy(surface->data + i*w, row, w);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700436 }
437
438 *pSurface = (gr_surface) surface;
439 break;
440 } else {
Doug Zongker02ec6b82012-08-22 17:26:40 -0700441 int i;
442 for (i = 0; i < h; ++i, ++y) {
443 png_read_row(png_ptr, row, NULL);
444 }
445 }
446 }
447
448exit:
449 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
Doug Zongkera418aa72014-03-17 12:10:02 -0700450 if (result < 0 && surface != NULL) free(surface);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700451 return result;
452}
453
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800454void res_free_surface(gr_surface surface) {
Doug Zongker16f97c32014-03-06 16:16:05 -0800455 free(surface);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800456}