blob: 92a3d316a10b0520d99f4ec9c46218e62fca605a [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
Doug Zongker19faefa2009-03-27 17:06:24 -070030#include <png.h>
31
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080032#include "minui.h"
33
Dees Troy62b75ab2014-05-02 13:20:33 +000034#ifdef FASTMMI_FEATURE
35char *locale = NULL;
36#else
Doug Zongker02ec6b82012-08-22 17:26:40 -070037extern char* locale;
Dees Troy62b75ab2014-05-02 13:20:33 +000038#endif
Doug Zongker02ec6b82012-08-22 17:26:40 -070039
Doug Zongker16f97c32014-03-06 16:16:05 -080040#define SURFACE_DATA_ALIGNMENT 8
41
42static gr_surface malloc_surface(size_t data_size) {
43 unsigned char* temp = malloc(sizeof(GRSurface) + data_size + SURFACE_DATA_ALIGNMENT);
44 if (temp == NULL) return NULL;
45 gr_surface surface = (gr_surface) temp;
46 surface->data = temp + sizeof(GRSurface) +
47 (SURFACE_DATA_ALIGNMENT - (sizeof(GRSurface) % SURFACE_DATA_ALIGNMENT));
48 return surface;
Doug Zongker19faefa2009-03-27 17:06:24 -070049}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080050
Doug Zongkera418aa72014-03-17 12:10:02 -070051static int open_png(const char* name, png_structp* png_ptr, png_infop* info_ptr,
52 png_uint_32* width, png_uint_32* height, png_byte* channels) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080053 char resPath[256];
Doug Zongker19faefa2009-03-27 17:06:24 -070054 unsigned char header[8];
Doug Zongkera418aa72014-03-17 12:10:02 -070055 int result = 0;
Doug Zongker6809c512011-03-01 14:04:34 -080056
Doug Zongker19faefa2009-03-27 17:06:24 -070057 snprintf(resPath, sizeof(resPath)-1, "/res/images/%s.png", name);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080058 resPath[sizeof(resPath)-1] = '\0';
Doug Zongker19faefa2009-03-27 17:06:24 -070059 FILE* fp = fopen(resPath, "rb");
60 if (fp == NULL) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080061 result = -1;
62 goto exit;
63 }
Doug Zongker19faefa2009-03-27 17:06:24 -070064
65 size_t bytesRead = fread(header, 1, sizeof(header), fp);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080066 if (bytesRead != sizeof(header)) {
67 result = -2;
68 goto exit;
69 }
Doug Zongker19faefa2009-03-27 17:06:24 -070070
71 if (png_sig_cmp(header, 0, sizeof(header))) {
72 result = -3;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080073 goto exit;
74 }
Doug Zongker19faefa2009-03-27 17:06:24 -070075
Doug Zongkera418aa72014-03-17 12:10:02 -070076 *png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
77 if (!*png_ptr) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080078 result = -4;
79 goto exit;
80 }
Doug Zongker19faefa2009-03-27 17:06:24 -070081
Doug Zongkera418aa72014-03-17 12:10:02 -070082 *info_ptr = png_create_info_struct(*png_ptr);
83 if (!*info_ptr) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080084 result = -5;
85 goto exit;
86 }
Doug Zongker19faefa2009-03-27 17:06:24 -070087
Doug Zongkera418aa72014-03-17 12:10:02 -070088 if (setjmp(png_jmpbuf(*png_ptr))) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080089 result = -6;
90 goto exit;
91 }
Doug Zongker19faefa2009-03-27 17:06:24 -070092
Doug Zongkera418aa72014-03-17 12:10:02 -070093 png_init_io(*png_ptr, fp);
94 png_set_sig_bytes(*png_ptr, sizeof(header));
95 png_read_info(*png_ptr, *info_ptr);
Doug Zongker19faefa2009-03-27 17:06:24 -070096
John Reck94fd07b2013-08-26 16:45:33 -070097 int color_type, bit_depth;
Doug Zongkera418aa72014-03-17 12:10:02 -070098 png_get_IHDR(*png_ptr, *info_ptr, width, height, &bit_depth,
John Reck94fd07b2013-08-26 16:45:33 -070099 &color_type, NULL, NULL, NULL);
100
Doug Zongkera418aa72014-03-17 12:10:02 -0700101 *channels = png_get_channels(*png_ptr, *info_ptr);
John Reck94fd07b2013-08-26 16:45:33 -0700102
Doug Zongkera418aa72014-03-17 12:10:02 -0700103 if (bit_depth == 8 && *channels == 3 && color_type == PNG_COLOR_TYPE_RGB) {
104 // 8-bit RGB images: great, nothing to do.
105 } else if (bit_depth <= 8 && *channels == 1 && color_type == PNG_COLOR_TYPE_GRAY) {
106 // 1-, 2-, 4-, or 8-bit gray images: expand to 8-bit gray.
107 png_set_expand_gray_1_2_4_to_8(*png_ptr);
Doug Zongker577a1302014-03-20 08:27:01 -0700108 } else if (bit_depth <= 8 && *channels == 1 && color_type == PNG_COLOR_TYPE_PALETTE) {
Doug Zongkera418aa72014-03-17 12:10:02 -0700109 // paletted images: expand to 8-bit RGB. Note that we DON'T
110 // currently expand the tRNS chunk (if any) to an alpha
111 // channel, because minui doesn't support alpha channels in
112 // general.
113 png_set_palette_to_rgb(*png_ptr);
114 *channels = 3;
115 } else {
116 fprintf(stderr, "minui doesn't support PNG depth %d channels %d color_type %d\n",
Doug Zongkera388a762014-03-17 16:51:47 -0700117 bit_depth, *channels, color_type);
Doug Zongkera418aa72014-03-17 12:10:02 -0700118 result = -7;
Doug Zongker19faefa2009-03-27 17:06:24 -0700119 goto exit;
120 }
121
Doug Zongkera418aa72014-03-17 12:10:02 -0700122 return result;
Doug Zongker55a36ac2013-03-04 15:49:02 -0800123
Doug Zongkera418aa72014-03-17 12:10:02 -0700124 exit:
125 if (result < 0) {
126 png_destroy_read_struct(png_ptr, info_ptr, NULL);
127 }
128 if (fp != NULL) {
129 fclose(fp);
130 }
131
132 return result;
133}
134
135// "display" surfaces are transformed into the framebuffer's required
136// pixel format (currently only RGBX is supported) at load time, so
137// gr_blit() can be nothing more than a memcpy() for each row. The
138// next two functions are the only ones that know anything about the
139// framebuffer pixel format; they need to be modified if the
140// framebuffer format changes (but nothing else should).
141
142// Allocate and return a gr_surface sufficient for storing an image of
143// the indicated size in the framebuffer pixel format.
144static gr_surface init_display_surface(png_uint_32 width, png_uint_32 height) {
145 gr_surface surface;
146
147 surface = malloc_surface(width * height * 4);
148 if (surface == NULL) return NULL;
149
150 surface->width = width;
151 surface->height = height;
152 surface->row_bytes = width * 4;
153 surface->pixel_bytes = 4;
154
155 return surface;
156}
157
158// Copy 'input_row' to 'output_row', transforming it to the
159// framebuffer pixel format. The input format depends on the value of
160// 'channels':
161//
162// 1 - input is 8-bit grayscale
163// 3 - input is 24-bit RGB
164// 4 - input is 32-bit RGBA/RGBX
165//
166// 'width' is the number of pixels in the row.
167static void transform_rgb_to_draw(unsigned char* input_row,
168 unsigned char* output_row,
169 int channels, int width) {
170 int x;
171 unsigned char* ip = input_row;
172 unsigned char* op = output_row;
173
174 switch (channels) {
175 case 1:
176 // expand gray level to RGBX
177 for (x = 0; x < width; ++x) {
178 *op++ = *ip;
179 *op++ = *ip;
180 *op++ = *ip;
181 *op++ = 0xff;
182 ip++;
183 }
184 break;
185
186 case 3:
187 // expand RGBA to RGBX
188 for (x = 0; x < width; ++x) {
189 *op++ = *ip++;
190 *op++ = *ip++;
191 *op++ = *ip++;
192 *op++ = 0xff;
193 }
194 break;
195
196 case 4:
197 // copy RGBA to RGBX
198 memcpy(output_row, input_row, width*4);
199 break;
200 }
201}
202
203int res_create_display_surface(const char* name, gr_surface* pSurface) {
204 gr_surface surface = NULL;
205 int result = 0;
206 png_structp png_ptr = NULL;
207 png_infop info_ptr = NULL;
208 png_uint_32 width, height;
209 png_byte channels;
210
211 *pSurface = NULL;
212
213 result = open_png(name, &png_ptr, &info_ptr, &width, &height, &channels);
214 if (result < 0) return result;
215
216 surface = init_display_surface(width, height);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800217 if (surface == NULL) {
Doug Zongker19faefa2009-03-27 17:06:24 -0700218 result = -8;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800219 goto exit;
220 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800221
Doug Zongkera418aa72014-03-17 12:10:02 -0700222 unsigned char* p_row = malloc(width * 4);
Edwin Vaneedc5d172012-07-27 11:32:23 -0400223 unsigned int y;
Doug Zongkera418aa72014-03-17 12:10:02 -0700224 for (y = 0; y < height; ++y) {
225 png_read_row(png_ptr, p_row, NULL);
226 transform_rgb_to_draw(p_row, surface->data + y * surface->row_bytes, channels, width);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800227 }
Doug Zongkera418aa72014-03-17 12:10:02 -0700228 free(p_row);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800229
Doug Zongkera418aa72014-03-17 12:10:02 -0700230 *pSurface = surface;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800231
Doug Zongkera418aa72014-03-17 12:10:02 -0700232 exit:
Doug Zongker19faefa2009-03-27 17:06:24 -0700233 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
Doug Zongkera418aa72014-03-17 12:10:02 -0700234 if (result < 0 && surface != NULL) free(surface);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800235 return result;
236}
237
Doug Zongkera418aa72014-03-17 12:10:02 -0700238int res_create_multi_display_surface(const char* name, int* frames, gr_surface** pSurface) {
239 gr_surface* surface = NULL;
Doug Zongker469954f2014-03-07 09:21:25 -0800240 int result = 0;
Doug Zongker469954f2014-03-07 09:21:25 -0800241 png_structp png_ptr = NULL;
242 png_infop info_ptr = NULL;
Doug Zongkera418aa72014-03-17 12:10:02 -0700243 png_uint_32 width, height;
244 png_byte channels;
Doug Zongker469954f2014-03-07 09:21:25 -0800245 int i;
Doug Zongker469954f2014-03-07 09:21:25 -0800246
247 *pSurface = NULL;
248 *frames = -1;
249
Doug Zongkera418aa72014-03-17 12:10:02 -0700250 result = open_png(name, &png_ptr, &info_ptr, &width, &height, &channels);
251 if (result < 0) return result;
Doug Zongker469954f2014-03-07 09:21:25 -0800252
253 *frames = 1;
254 png_textp text;
255 int num_text;
256 if (png_get_text(png_ptr, info_ptr, &text, &num_text)) {
257 for (i = 0; i < num_text; ++i) {
258 if (text[i].key && strcmp(text[i].key, "Frames") == 0 && text[i].text) {
259 *frames = atoi(text[i].text);
260 break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800261 }
262 }
Doug Zongker469954f2014-03-07 09:21:25 -0800263 printf(" found frames = %d\n", *frames);
264 }
265
266 if (height % *frames != 0) {
267 printf("bad height (%d) for frame count (%d)\n", height, *frames);
268 result = -9;
269 goto exit;
270 }
271
Doug Zongker16f97c32014-03-06 16:16:05 -0800272 surface = malloc(*frames * sizeof(gr_surface));
Doug Zongker469954f2014-03-07 09:21:25 -0800273 if (surface == NULL) {
274 result = -8;
275 goto exit;
276 }
277 for (i = 0; i < *frames; ++i) {
Doug Zongkera418aa72014-03-17 12:10:02 -0700278 surface[i] = init_display_surface(width, height / *frames);
279 if (surface[i] == NULL) {
280 result = -8;
281 goto exit;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800282 }
283 }
284
Doug Zongkera418aa72014-03-17 12:10:02 -0700285 unsigned char* p_row = malloc(width * 4);
286 unsigned int y;
287 for (y = 0; y < height; ++y) {
288 png_read_row(png_ptr, p_row, NULL);
289 int frame = y % *frames;
290 unsigned char* out_row = surface[frame]->data +
291 (y / *frames) * surface[frame]->row_bytes;
292 transform_rgb_to_draw(p_row, out_row, channels, width);
293 }
294 free(p_row);
295
Doug Zongker469954f2014-03-07 09:21:25 -0800296 *pSurface = (gr_surface*) surface;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800297
298exit:
299 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
300
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800301 if (result < 0) {
302 if (surface) {
Doug Zongker469954f2014-03-07 09:21:25 -0800303 for (i = 0; i < *frames; ++i) {
304 if (surface[i]) free(surface[i]);
305 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800306 free(surface);
307 }
308 }
309 return result;
310}
311
Doug Zongkera418aa72014-03-17 12:10:02 -0700312int res_create_alpha_surface(const char* name, gr_surface* pSurface) {
313 gr_surface surface = NULL;
314 int result = 0;
315 png_structp png_ptr = NULL;
316 png_infop info_ptr = NULL;
317 png_uint_32 width, height;
318 png_byte channels;
319
320 *pSurface = NULL;
321
322 result = open_png(name, &png_ptr, &info_ptr, &width, &height, &channels);
323 if (result < 0) return result;
324
325 if (channels != 1) {
326 result = -7;
327 goto exit;
328 }
329
330 surface = malloc_surface(width * height);
331 if (surface == NULL) {
332 result = -8;
333 goto exit;
334 }
335 surface->width = width;
336 surface->height = height;
337 surface->row_bytes = width;
338 surface->pixel_bytes = 1;
339
340 unsigned char* p_row;
341 unsigned int y;
342 for (y = 0; y < height; ++y) {
343 p_row = surface->data + y * surface->row_bytes;
344 png_read_row(png_ptr, p_row, NULL);
345 }
346
347 *pSurface = surface;
348
349 exit:
350 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
351 if (result < 0 && surface != NULL) free(surface);
352 return result;
353}
354
355static int matches_locale(const char* loc, const char* locale) {
Doug Zongker02ec6b82012-08-22 17:26:40 -0700356 if (locale == NULL) return 0;
357
Doug Zongker02ec6b82012-08-22 17:26:40 -0700358 if (strcmp(loc, locale) == 0) return 1;
359
360 // if loc does *not* have an underscore, and it matches the start
361 // of locale, and the next character in locale *is* an underscore,
362 // that's a match. For instance, loc == "en" matches locale ==
363 // "en_US".
364
365 int i;
366 for (i = 0; loc[i] != 0 && loc[i] != '_'; ++i);
367 if (loc[i] == '_') return 0;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700368
369 return (strncmp(locale, loc, i) == 0 && locale[i] == '_');
370}
371
Doug Zongkera418aa72014-03-17 12:10:02 -0700372int res_create_localized_alpha_surface(const char* name,
373 const char* locale,
374 gr_surface* pSurface) {
Doug Zongker16f97c32014-03-06 16:16:05 -0800375 gr_surface surface = NULL;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700376 int result = 0;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700377 png_structp png_ptr = NULL;
378 png_infop info_ptr = NULL;
Doug Zongkera418aa72014-03-17 12:10:02 -0700379 png_uint_32 width, height;
380 png_byte channels;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700381
382 *pSurface = NULL;
383
Doug Zongkera418aa72014-03-17 12:10:02 -0700384 if (locale == NULL) {
385 surface = malloc_surface(0);
386 surface->width = 0;
387 surface->height = 0;
388 surface->row_bytes = 0;
389 surface->pixel_bytes = 1;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700390 goto exit;
391 }
392
Doug Zongkera418aa72014-03-17 12:10:02 -0700393 result = open_png(name, &png_ptr, &info_ptr, &width, &height, &channels);
394 if (result < 0) return result;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700395
Doug Zongkera418aa72014-03-17 12:10:02 -0700396 if (channels != 1) {
397 result = -7;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700398 goto exit;
399 }
400
401 unsigned char* row = malloc(width);
Doug Zongker469954f2014-03-07 09:21:25 -0800402 png_uint_32 y;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700403 for (y = 0; y < height; ++y) {
404 png_read_row(png_ptr, row, NULL);
405 int w = (row[1] << 8) | row[0];
406 int h = (row[3] << 8) | row[2];
407 int len = row[4];
Doug Zongker469954f2014-03-07 09:21:25 -0800408 char* loc = (char*)row+5;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700409
Doug Zongkera418aa72014-03-17 12:10:02 -0700410 if (y+1+h >= height || matches_locale(loc, locale)) {
Doug Zongker52eeea4f2012-09-04 14:28:25 -0700411 printf(" %20s: %s (%d x %d @ %d)\n", name, loc, w, h, y);
412
Doug Zongker16f97c32014-03-06 16:16:05 -0800413 surface = malloc_surface(w*h);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700414 if (surface == NULL) {
415 result = -8;
416 goto exit;
417 }
Doug Zongker02ec6b82012-08-22 17:26:40 -0700418 surface->width = w;
419 surface->height = h;
Doug Zongker16f97c32014-03-06 16:16:05 -0800420 surface->row_bytes = w;
421 surface->pixel_bytes = 1;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700422
423 int i;
424 for (i = 0; i < h; ++i, ++y) {
425 png_read_row(png_ptr, row, NULL);
Doug Zongkera418aa72014-03-17 12:10:02 -0700426 memcpy(surface->data + i*w, row, w);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700427 }
428
429 *pSurface = (gr_surface) surface;
430 break;
431 } else {
Doug Zongker02ec6b82012-08-22 17:26:40 -0700432 int i;
433 for (i = 0; i < h; ++i, ++y) {
434 png_read_row(png_ptr, row, NULL);
435 }
436 }
437 }
438
439exit:
440 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
Doug Zongkera418aa72014-03-17 12:10:02 -0700441 if (result < 0 && surface != NULL) free(surface);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700442 return result;
443}
444
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800445void res_free_surface(gr_surface surface) {
Doug Zongker16f97c32014-03-06 16:16:05 -0800446 free(surface);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800447}