blob: 781df2b0637a4ce30e183510a49138202b2bf950 [file] [log] [blame]
Dees_Troy51a0e822012-09-05 15:24:24 -04001/*
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>
Ethan Yonkerfbb43532015-12-28 21:54:50 +010018#include <string.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040019#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
Dees_Troy51a0e822012-09-05 15:24:24 -040031#include <png.h>
Ethan Yonkerfbb43532015-12-28 21:54:50 +010032
33#include <pixelflinger/pixelflinger.h>
34extern "C" {
Dees_Troy930bf012013-08-10 22:19:03 +000035#include "jpeglib.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040036}
Ethan Yonkerfbb43532015-12-28 21:54:50 +010037#include "minui.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040038
Ethan Yonker448c8dc2014-12-08 15:34:34 -060039#define SURFACE_DATA_ALIGNMENT 8
40
41static GGLSurface* malloc_surface(size_t data_size) {
Ethan Yonkerfbb43532015-12-28 21:54:50 +010042 size_t size = sizeof(GGLSurface) + data_size + SURFACE_DATA_ALIGNMENT;
43 unsigned char* temp = reinterpret_cast<unsigned char*>(malloc(size));
Ethan Yonker448c8dc2014-12-08 15:34:34 -060044 if (temp == NULL) return NULL;
Ethan Yonkerfbb43532015-12-28 21:54:50 +010045 GGLSurface* surface = reinterpret_cast<GGLSurface*>(temp);
Ethan Yonker448c8dc2014-12-08 15:34:34 -060046 surface->data = temp + sizeof(GGLSurface) +
47 (SURFACE_DATA_ALIGNMENT - (sizeof(GGLSurface) % SURFACE_DATA_ALIGNMENT));
48 return surface;
49}
50
51static int open_png(const char* name, png_structp* png_ptr, png_infop* info_ptr,
thatc26ba0d2015-02-13 01:09:55 +010052 png_uint_32* width, png_uint_32* height, png_byte* channels, FILE** fpp) {
Ethan Yonker448c8dc2014-12-08 15:34:34 -060053 char resPath[256];
Dees_Troy51a0e822012-09-05 15:24:24 -040054 unsigned char header[8];
Ethan Yonker448c8dc2014-12-08 15:34:34 -060055 int result = 0;
Ethan Yonkerfbb43532015-12-28 21:54:50 +010056 int color_type, bit_depth;
57 size_t bytesRead;
Dees_Troy51a0e822012-09-05 15:24:24 -040058
Dees Troy3454ade2015-01-20 19:21:04 +000059 snprintf(resPath, sizeof(resPath)-1, TWRES "images/%s.png", name);
Ethan Yonker448c8dc2014-12-08 15:34:34 -060060 resPath[sizeof(resPath)-1] = '\0';
61 FILE* fp = fopen(resPath, "rb");
Dees_Troy51a0e822012-09-05 15:24:24 -040062 if (fp == NULL) {
Ethan Yonkerac21cb52014-12-11 18:05:59 -060063 fp = fopen(name, "rb");
64 if (fp == NULL) {
65 result = -1;
66 goto exit;
67 }
Dees_Troy51a0e822012-09-05 15:24:24 -040068 }
69
Ethan Yonkerfbb43532015-12-28 21:54:50 +010070 bytesRead = fread(header, 1, sizeof(header), fp);
Dees_Troy51a0e822012-09-05 15:24:24 -040071 if (bytesRead != sizeof(header)) {
72 result = -2;
73 goto exit;
74 }
75
76 if (png_sig_cmp(header, 0, sizeof(header))) {
77 result = -3;
78 goto exit;
79 }
80
Ethan Yonker448c8dc2014-12-08 15:34:34 -060081 *png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
82 if (!*png_ptr) {
Dees_Troy51a0e822012-09-05 15:24:24 -040083 result = -4;
84 goto exit;
85 }
86
Ethan Yonker448c8dc2014-12-08 15:34:34 -060087 *info_ptr = png_create_info_struct(*png_ptr);
88 if (!*info_ptr) {
Dees_Troy51a0e822012-09-05 15:24:24 -040089 result = -5;
90 goto exit;
91 }
92
Ethan Yonker448c8dc2014-12-08 15:34:34 -060093 if (setjmp(png_jmpbuf(*png_ptr))) {
Dees_Troy51a0e822012-09-05 15:24:24 -040094 result = -6;
95 goto exit;
96 }
97
Ethan Yonker448c8dc2014-12-08 15:34:34 -060098 png_init_io(*png_ptr, fp);
99 png_set_sig_bytes(*png_ptr, sizeof(header));
100 png_read_info(*png_ptr, *info_ptr);
Dees_Troy51a0e822012-09-05 15:24:24 -0400101
Ethan Yonker448c8dc2014-12-08 15:34:34 -0600102 png_get_IHDR(*png_ptr, *info_ptr, width, height, &bit_depth,
Ethan Yonker304f32f2014-11-07 10:14:05 -0600103 &color_type, NULL, NULL, NULL);
104
Ethan Yonker448c8dc2014-12-08 15:34:34 -0600105 *channels = png_get_channels(*png_ptr, *info_ptr);
Dees_Troy51a0e822012-09-05 15:24:24 -0400106
Ethan Yonkerfbb43532015-12-28 21:54:50 +0100107 if (bit_depth == 8 && *channels == 3 && color_type == PNG_COLOR_TYPE_RGB) {
Ethan Yonker448c8dc2014-12-08 15:34:34 -0600108 // 8-bit RGB images: great, nothing to do.
109 } else if (bit_depth <= 8 && *channels == 1 && color_type == PNG_COLOR_TYPE_GRAY) {
110 // 1-, 2-, 4-, or 8-bit gray images: expand to 8-bit gray.
111 png_set_expand_gray_1_2_4_to_8(*png_ptr);
112 } else if (bit_depth <= 8 && *channels == 1 && color_type == PNG_COLOR_TYPE_PALETTE) {
113 // paletted images: expand to 8-bit RGB. Note that we DON'T
114 // currently expand the tRNS chunk (if any) to an alpha
115 // channel, because minui doesn't support alpha channels in
116 // general.
117 png_set_palette_to_rgb(*png_ptr);
118 *channels = 3;
Ethan Yonkerfbb43532015-12-28 21:54:50 +0100119 } else if (color_type == PNG_COLOR_TYPE_PALETTE) {
120 png_set_palette_to_rgb(*png_ptr);
Dees_Troy51a0e822012-09-05 15:24:24 -0400121 }
122
thatc26ba0d2015-02-13 01:09:55 +0100123 *fpp = fp;
Ethan Yonker448c8dc2014-12-08 15:34:34 -0600124 return result;
Dees_Troy51a0e822012-09-05 15:24:24 -0400125
Ethan Yonker448c8dc2014-12-08 15:34:34 -0600126 exit:
127 if (result < 0) {
128 png_destroy_read_struct(png_ptr, info_ptr, NULL);
Dees_Troy51a0e822012-09-05 15:24:24 -0400129 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400130 if (fp != NULL) {
131 fclose(fp);
132 }
Ethan Yonker448c8dc2014-12-08 15:34:34 -0600133
134 return result;
135}
136
137// "display" surfaces are transformed into the framebuffer's required
138// pixel format (currently only RGBX is supported) at load time, so
139// gr_blit() can be nothing more than a memcpy() for each row. The
140// next two functions are the only ones that know anything about the
141// framebuffer pixel format; they need to be modified if the
142// framebuffer format changes (but nothing else should).
143
Ethan Yonkerfbb43532015-12-28 21:54:50 +0100144// Allocate and return a GRSurface* sufficient for storing an image of
Ethan Yonker448c8dc2014-12-08 15:34:34 -0600145// the indicated size in the framebuffer pixel format.
146static GGLSurface* init_display_surface(png_uint_32 width, png_uint_32 height) {
Ethan Yonkerfbb43532015-12-28 21:54:50 +0100147 GGLSurface* surface = malloc_surface(width * height * 4);
Ethan Yonker448c8dc2014-12-08 15:34:34 -0600148 if (surface == NULL) return NULL;
149
150 surface->version = sizeof(GGLSurface);
151 surface->width = width;
152 surface->height = height;
153 surface->stride = width;
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;
Dees_Troy51a0e822012-09-05 15:24:24 -0400200 }
Ethan Yonker448c8dc2014-12-08 15:34:34 -0600201}
202
203int res_create_surface_png(const char* name, gr_surface* pSurface) {
204 GGLSurface* 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;
thatc26ba0d2015-02-13 01:09:55 +0100210 FILE* fp;
Ethan Yonkerfbb43532015-12-28 21:54:50 +0100211 unsigned char* p_row;
212 unsigned int y;
Ethan Yonker448c8dc2014-12-08 15:34:34 -0600213
214 *pSurface = NULL;
215
thatc26ba0d2015-02-13 01:09:55 +0100216 result = open_png(name, &png_ptr, &info_ptr, &width, &height, &channels, &fp);
Ethan Yonker448c8dc2014-12-08 15:34:34 -0600217 if (result < 0) return result;
218
219 surface = init_display_surface(width, height);
220 if (surface == NULL) {
221 result = -8;
222 goto exit;
223 }
224
Ethan Yonkerfbb43532015-12-28 21:54:50 +0100225#if defined(RECOVERY_ABGR) || defined(RECOVERY_BGRA)
226 png_set_bgr(png_ptr);
227#endif
228
229 p_row = reinterpret_cast<unsigned char*>(malloc(width * 4));
230 if (p_row == NULL) {
231 result = -9;
232 goto exit;
233 }
Ethan Yonker448c8dc2014-12-08 15:34:34 -0600234 for (y = 0; y < height; ++y) {
235 png_read_row(png_ptr, p_row, NULL);
236 transform_rgb_to_draw(p_row, surface->data + y * width * 4, channels, width);
237 }
238 free(p_row);
239
240 if (channels == 3)
241 surface->format = GGL_PIXEL_FORMAT_RGBX_8888;
242 else
243 surface->format = GGL_PIXEL_FORMAT_RGBA_8888;
244
245 *pSurface = (gr_surface) surface;
246
247 exit:
thatc26ba0d2015-02-13 01:09:55 +0100248 fclose(fp);
Ethan Yonker448c8dc2014-12-08 15:34:34 -0600249 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
250 if (result < 0 && surface != NULL) free(surface);
Dees_Troy51a0e822012-09-05 15:24:24 -0400251 return result;
252}
253
254int res_create_surface_jpg(const char* name, gr_surface* pSurface) {
255 GGLSurface* surface = NULL;
Ethan Yonkerfbb43532015-12-28 21:54:50 +0100256 int result = 0, y;
Dees_Troy51a0e822012-09-05 15:24:24 -0400257 struct jpeg_decompress_struct cinfo;
258 struct jpeg_error_mgr jerr;
Ethan Yonkerfbb43532015-12-28 21:54:50 +0100259 unsigned char* pData;
260 size_t width, height, stride, pixelSize;
Dees_Troy51a0e822012-09-05 15:24:24 -0400261
262 FILE* fp = fopen(name, "rb");
263 if (fp == NULL) {
264 char resPath[256];
265
Dees Troy3454ade2015-01-20 19:21:04 +0000266 snprintf(resPath, sizeof(resPath)-1, TWRES "images/%s", name);
Dees_Troy51a0e822012-09-05 15:24:24 -0400267 resPath[sizeof(resPath)-1] = '\0';
268 fp = fopen(resPath, "rb");
269 if (fp == NULL) {
270 result = -1;
271 goto exit;
272 }
273 }
274
275 cinfo.err = jpeg_std_error(&jerr);
276 jpeg_create_decompress(&cinfo);
277
278 /* Specify data source for decompression */
279 jpeg_stdio_src(&cinfo, fp);
280
281 /* Read file header, set default decompression parameters */
282 if (jpeg_read_header(&cinfo, TRUE) != JPEG_HEADER_OK)
283 goto exit;
284
285 /* Start decompressor */
286 (void) jpeg_start_decompress(&cinfo);
287
Ethan Yonkerfbb43532015-12-28 21:54:50 +0100288 width = cinfo.image_width;
289 height = cinfo.image_height;
290 stride = 4 * width;
291 pixelSize = stride * height;
Dees_Troy51a0e822012-09-05 15:24:24 -0400292
Ethan Yonkerfbb43532015-12-28 21:54:50 +0100293 surface = reinterpret_cast<GGLSurface*>(malloc(sizeof(GGLSurface) + pixelSize));
294 //p_row = reinterpret_cast<unsigned char*>(malloc(width * 4));
Dees_Troy51a0e822012-09-05 15:24:24 -0400295 if (surface == NULL) {
296 result = -8;
297 goto exit;
298 }
299
Ethan Yonkerfbb43532015-12-28 21:54:50 +0100300 pData = (unsigned char*) (surface + 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400301 surface->version = sizeof(GGLSurface);
302 surface->width = width;
303 surface->height = height;
304 surface->stride = width; /* Yes, pixels, not bytes */
305 surface->data = pData;
306 surface->format = GGL_PIXEL_FORMAT_RGBX_8888;
307
Dees_Troy51a0e822012-09-05 15:24:24 -0400308 for (y = 0; y < (int) height; ++y) {
309 unsigned char* pRow = pData + y * stride;
310 jpeg_read_scanlines(&cinfo, &pRow, 1);
311
312 int x;
313 for(x = width - 1; x >= 0; x--) {
314 int sx = x * 3;
315 int dx = x * 4;
316 unsigned char r = pRow[sx];
317 unsigned char g = pRow[sx + 1];
318 unsigned char b = pRow[sx + 2];
319 unsigned char a = 0xff;
Ethan Yonkerfbb43532015-12-28 21:54:50 +0100320#if defined(RECOVERY_ABGR) || defined(RECOVERY_BGRA)
321 pRow[dx ] = b; // r
322 pRow[dx + 1] = g; // g
323 pRow[dx + 2] = r; // b
324 pRow[dx + 3] = a;
325#else
Dees_Troy51a0e822012-09-05 15:24:24 -0400326 pRow[dx ] = r; // r
327 pRow[dx + 1] = g; // g
328 pRow[dx + 2] = b; // b
329 pRow[dx + 3] = a;
Ethan Yonkerfbb43532015-12-28 21:54:50 +0100330#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400331 }
332 }
333 *pSurface = (gr_surface) surface;
334
335exit:
336 if (fp != NULL)
337 {
338 if (surface)
339 {
340 (void) jpeg_finish_decompress(&cinfo);
341 if (result < 0)
342 {
343 free(surface);
344 }
345 }
346 jpeg_destroy_decompress(&cinfo);
347 fclose(fp);
348 }
349 return result;
350}
351
352int res_create_surface(const char* name, gr_surface* pSurface) {
353 int ret;
354
355 if (!name) return -1;
356
357 if (strlen(name) > 4 && strcmp(name + strlen(name) - 4, ".jpg") == 0)
Ethan Yonker63e414f2015-02-06 15:44:39 -0600358 return res_create_surface_jpg(name,pSurface);
Dees_Troy51a0e822012-09-05 15:24:24 -0400359
360 ret = res_create_surface_png(name,pSurface);
361 if (ret < 0)
362 ret = res_create_surface_jpg(name,pSurface);
363
364 return ret;
365}
366
367void res_free_surface(gr_surface surface) {
368 GGLSurface* pSurface = (GGLSurface*) surface;
369 if (pSurface) {
370 free(pSurface);
371 }
372}
Ethan Yonker63e414f2015-02-06 15:44:39 -0600373
374// Scale image function
375int res_scale_surface(gr_surface source, gr_surface* destination, float scale_w, float scale_h) {
Ethan Yonker727aeb82015-02-10 18:07:44 -0600376 GGLContext *gl = NULL;
Ethan Yonker63e414f2015-02-06 15:44:39 -0600377 GGLSurface* sc_mem_surface = NULL;
378 *destination = NULL;
379 GGLSurface *surface = (GGLSurface*)source;
380 int w = gr_get_width(source), h = gr_get_height(source);
381 int sx = 0, sy = 0, dx = 0, dy = 0;
382 float dw = (float)w * scale_w;
383 float dh = (float)h * scale_h;
384
385 // Create a new surface that is the appropriate size
386 sc_mem_surface = init_display_surface((int)dw, (int)dh);
387 if (!sc_mem_surface) {
388 printf("gr_scale_surface failed to init_display_surface\n");
389 return -1;
390 }
391 sc_mem_surface->format = surface->format;
392
Ethan Yonker727aeb82015-02-10 18:07:44 -0600393 // Initialize the context
394 gglInit(&gl);
Ethan Yonker63e414f2015-02-06 15:44:39 -0600395 gl->colorBuffer(gl, sc_mem_surface);
396 gl->activeTexture(gl, 0);
397
398 // Enable or disable blending based on source surface format
399 if (surface->format == GGL_PIXEL_FORMAT_RGBX_8888) {
400 gl->disable(gl, GGL_BLEND);
401 } else {
402 gl->enable(gl, GGL_BLEND);
403 gl->blendFunc(gl, GGL_ONE, GGL_ZERO);
404 }
405
406 // Bind our source surface to the context
407 gl->bindTexture(gl, surface);
408
409 // Deal with the scaling
410 gl->texParameteri(gl, GGL_TEXTURE_2D, GGL_TEXTURE_MIN_FILTER, GGL_LINEAR);
411 gl->texParameteri(gl, GGL_TEXTURE_2D, GGL_TEXTURE_MAG_FILTER, GGL_LINEAR);
412 gl->texParameteri(gl, GGL_TEXTURE_2D, GGL_TEXTURE_WRAP_S, GGL_CLAMP);
413 gl->texParameteri(gl, GGL_TEXTURE_2D, GGL_TEXTURE_WRAP_T, GGL_CLAMP);
414 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
415 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_AUTOMATIC);
416 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_AUTOMATIC);
417 gl->enable(gl, GGL_TEXTURE_2D);
418
419 int32_t grad[8];
420 memset(grad, 0, sizeof(grad));
421 // s, dsdx, dsdy, scale, t, dtdx, dtdy, tscale <- this is wrong!
422 // This api uses block floating-point for S and T texture coordinates.
423 // All values are given in 16.16, scaled by 'scale'. In other words,
424 // set scale to 0, for 16.16 values.
425
426 // s, dsdx, dsdy, t, dtdx, dtdy, sscale, tscale
427 float dsdx = (float)w / dw;
428 float dtdy = (float)h / dh;
429 grad[0] = ((float)sx - (dsdx * dx)) * 65536;
430 grad[1] = dsdx * 65536;
431 grad[3] = ((float)sy - (dtdy * dy)) * 65536;
432 grad[5] = dtdy * 65536;
433// printf("blit: w=%d h=%d dx=%d dy=%d dw=%f dh=%f dsdx=%f dtdy=%f s0=%x dsdx=%x t0=%x dtdy=%x\n",
434// w, h, dx, dy, dw, dh, dsdx, dtdy, grad[0], grad[1], grad[3], grad[5]);
435 gl->texCoordGradScale8xv(gl, 0 /*tmu*/, grad);
436
437 // draw / scale the source surface to our target context
438 gl->recti(gl, dx, dy, dx + dw, dy + dh);
Ethan Yonker727aeb82015-02-10 18:07:44 -0600439 gglUninit(gl);
440 gl = NULL;
Ethan Yonker63e414f2015-02-06 15:44:39 -0600441 // put the scaled surface in our destination
442 *destination = (gr_surface*) sc_mem_surface;
443 // free memory used in the source
444 res_free_surface(source);
445 source = NULL;
446 return 0;
447}