blob: 3e3375d11bd3d5bf8f5d3023c8b1128e3a6ef660 [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>
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
32#include <png.h>
Dees_Troy930bf012013-08-10 22:19:03 +000033#include "jpeglib.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040034
35#include "minui.h"
36
37// libpng gives "undefined reference to 'pow'" errors, and I have no
38// idea how to convince the build system to link with -lm. We don't
39// need this functionality (it's used for gamma adjustment) so provide
40// a dummy implementation to satisfy the linker.
41double pow(double x, double y) {
42 return x;
43}
44
Ethan Yonker448c8dc2014-12-08 15:34:34 -060045#define SURFACE_DATA_ALIGNMENT 8
46
47static GGLSurface* malloc_surface(size_t data_size) {
48 unsigned char* temp = malloc(sizeof(GGLSurface) + data_size + SURFACE_DATA_ALIGNMENT);
49 if (temp == NULL) return NULL;
50 GGLSurface* surface = (GGLSurface*) temp;
51 surface->data = temp + sizeof(GGLSurface) +
52 (SURFACE_DATA_ALIGNMENT - (sizeof(GGLSurface) % SURFACE_DATA_ALIGNMENT));
53 return surface;
54}
55
56static int open_png(const char* name, png_structp* png_ptr, png_infop* info_ptr,
thatc26ba0d2015-02-13 01:09:55 +010057 png_uint_32* width, png_uint_32* height, png_byte* channels, FILE** fpp) {
Ethan Yonker448c8dc2014-12-08 15:34:34 -060058 char resPath[256];
Dees_Troy51a0e822012-09-05 15:24:24 -040059 unsigned char header[8];
Ethan Yonker448c8dc2014-12-08 15:34:34 -060060 int result = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -040061
Dees Troy3454ade2015-01-20 19:21:04 +000062 snprintf(resPath, sizeof(resPath)-1, TWRES "images/%s.png", name);
63 printf("open_png %s\n", resPath);
Ethan Yonker448c8dc2014-12-08 15:34:34 -060064 resPath[sizeof(resPath)-1] = '\0';
65 FILE* fp = fopen(resPath, "rb");
Dees_Troy51a0e822012-09-05 15:24:24 -040066 if (fp == NULL) {
Ethan Yonkerac21cb52014-12-11 18:05:59 -060067 fp = fopen(name, "rb");
68 if (fp == NULL) {
69 result = -1;
70 goto exit;
71 }
Dees_Troy51a0e822012-09-05 15:24:24 -040072 }
73
74 size_t bytesRead = fread(header, 1, sizeof(header), fp);
75 if (bytesRead != sizeof(header)) {
76 result = -2;
77 goto exit;
78 }
79
80 if (png_sig_cmp(header, 0, sizeof(header))) {
81 result = -3;
82 goto exit;
83 }
84
Ethan Yonker448c8dc2014-12-08 15:34:34 -060085 *png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
86 if (!*png_ptr) {
Dees_Troy51a0e822012-09-05 15:24:24 -040087 result = -4;
88 goto exit;
89 }
90
Ethan Yonker448c8dc2014-12-08 15:34:34 -060091 *info_ptr = png_create_info_struct(*png_ptr);
92 if (!*info_ptr) {
Dees_Troy51a0e822012-09-05 15:24:24 -040093 result = -5;
94 goto exit;
95 }
96
Ethan Yonker448c8dc2014-12-08 15:34:34 -060097 if (setjmp(png_jmpbuf(*png_ptr))) {
Dees_Troy51a0e822012-09-05 15:24:24 -040098 result = -6;
99 goto exit;
100 }
101
Ethan Yonker448c8dc2014-12-08 15:34:34 -0600102 png_init_io(*png_ptr, fp);
103 png_set_sig_bytes(*png_ptr, sizeof(header));
104 png_read_info(*png_ptr, *info_ptr);
Dees_Troy51a0e822012-09-05 15:24:24 -0400105
Ethan Yonker304f32f2014-11-07 10:14:05 -0600106 int color_type, bit_depth;
Ethan Yonker448c8dc2014-12-08 15:34:34 -0600107 png_get_IHDR(*png_ptr, *info_ptr, width, height, &bit_depth,
Ethan Yonker304f32f2014-11-07 10:14:05 -0600108 &color_type, NULL, NULL, NULL);
109
Ethan Yonker448c8dc2014-12-08 15:34:34 -0600110 *channels = png_get_channels(*png_ptr, *info_ptr);
Dees_Troy51a0e822012-09-05 15:24:24 -0400111
Ethan Yonker448c8dc2014-12-08 15:34:34 -0600112 /*if (bit_depth == 8 && *channels == 3 && color_type == PNG_COLOR_TYPE_RGB) {
113 // 8-bit RGB images: great, nothing to do.
114 } else if (bit_depth <= 8 && *channels == 1 && color_type == PNG_COLOR_TYPE_GRAY) {
115 // 1-, 2-, 4-, or 8-bit gray images: expand to 8-bit gray.
116 png_set_expand_gray_1_2_4_to_8(*png_ptr);
117 } else if (bit_depth <= 8 && *channels == 1 && color_type == PNG_COLOR_TYPE_PALETTE) {
118 // paletted images: expand to 8-bit RGB. Note that we DON'T
119 // currently expand the tRNS chunk (if any) to an alpha
120 // channel, because minui doesn't support alpha channels in
121 // general.
122 png_set_palette_to_rgb(*png_ptr);
123 *channels = 3;
124 } else {
125 fprintf(stderr, "minui doesn't support PNG depth %d channels %d color_type %d\n",
126 bit_depth, *channels, color_type);
127 result = -7;
Dees_Troy51a0e822012-09-05 15:24:24 -0400128 goto exit;
Ethan Yonker448c8dc2014-12-08 15:34:34 -0600129 }*/
Dees_Troy51a0e822012-09-05 15:24:24 -0400130 if (color_type == PNG_COLOR_TYPE_PALETTE) {
131 png_set_palette_to_rgb(png_ptr);
132 }
133
thatc26ba0d2015-02-13 01:09:55 +0100134 *fpp = fp;
Ethan Yonker448c8dc2014-12-08 15:34:34 -0600135 return result;
Dees_Troy51a0e822012-09-05 15:24:24 -0400136
Ethan Yonker448c8dc2014-12-08 15:34:34 -0600137 exit:
138 if (result < 0) {
139 png_destroy_read_struct(png_ptr, info_ptr, NULL);
Dees_Troy51a0e822012-09-05 15:24:24 -0400140 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400141 if (fp != NULL) {
142 fclose(fp);
143 }
Ethan Yonker448c8dc2014-12-08 15:34:34 -0600144
145 return result;
146}
147
148// "display" surfaces are transformed into the framebuffer's required
149// pixel format (currently only RGBX is supported) at load time, so
150// gr_blit() can be nothing more than a memcpy() for each row. The
151// next two functions are the only ones that know anything about the
152// framebuffer pixel format; they need to be modified if the
153// framebuffer format changes (but nothing else should).
154
155// Allocate and return a gr_surface sufficient for storing an image of
156// the indicated size in the framebuffer pixel format.
157static GGLSurface* init_display_surface(png_uint_32 width, png_uint_32 height) {
158 GGLSurface* surface;
159
160 surface = (GGLSurface*) malloc_surface(width * height * 4);
161 if (surface == NULL) return NULL;
162
163 surface->version = sizeof(GGLSurface);
164 surface->width = width;
165 surface->height = height;
166 surface->stride = width;
167
168 return surface;
169}
170
171// Copy 'input_row' to 'output_row', transforming it to the
172// framebuffer pixel format. The input format depends on the value of
173// 'channels':
174//
175// 1 - input is 8-bit grayscale
176// 3 - input is 24-bit RGB
177// 4 - input is 32-bit RGBA/RGBX
178//
179// 'width' is the number of pixels in the row.
180static void transform_rgb_to_draw(unsigned char* input_row,
181 unsigned char* output_row,
182 int channels, int width) {
183 int x;
184 unsigned char* ip = input_row;
185 unsigned char* op = output_row;
186
187 switch (channels) {
188 case 1:
189 // expand gray level to RGBX
190 for (x = 0; x < width; ++x) {
191 *op++ = *ip;
192 *op++ = *ip;
193 *op++ = *ip;
194 *op++ = 0xff;
195 ip++;
196 }
197 break;
198
199 case 3:
200 // expand RGBA to RGBX
201 for (x = 0; x < width; ++x) {
202 *op++ = *ip++;
203 *op++ = *ip++;
204 *op++ = *ip++;
205 *op++ = 0xff;
206 }
207 break;
208
209 case 4:
210 // copy RGBA to RGBX
211 memcpy(output_row, input_row, width*4);
212 break;
Dees_Troy51a0e822012-09-05 15:24:24 -0400213 }
Ethan Yonker448c8dc2014-12-08 15:34:34 -0600214}
215
216int res_create_surface_png(const char* name, gr_surface* pSurface) {
217 GGLSurface* surface = NULL;
218 int result = 0;
219 png_structp png_ptr = NULL;
220 png_infop info_ptr = NULL;
221 png_uint_32 width, height;
222 png_byte channels;
thatc26ba0d2015-02-13 01:09:55 +0100223 FILE* fp;
Ethan Yonker448c8dc2014-12-08 15:34:34 -0600224
225 *pSurface = NULL;
226
thatc26ba0d2015-02-13 01:09:55 +0100227 result = open_png(name, &png_ptr, &info_ptr, &width, &height, &channels, &fp);
Ethan Yonker448c8dc2014-12-08 15:34:34 -0600228 if (result < 0) return result;
229
230 surface = init_display_surface(width, height);
231 if (surface == NULL) {
232 result = -8;
233 goto exit;
234 }
235
236 unsigned char* p_row = malloc(width * 4);
237 unsigned int y;
238 for (y = 0; y < height; ++y) {
239 png_read_row(png_ptr, p_row, NULL);
240 transform_rgb_to_draw(p_row, surface->data + y * width * 4, channels, width);
241 }
242 free(p_row);
243
244 if (channels == 3)
245 surface->format = GGL_PIXEL_FORMAT_RGBX_8888;
246 else
247 surface->format = GGL_PIXEL_FORMAT_RGBA_8888;
248
249 *pSurface = (gr_surface) surface;
250
251 exit:
thatc26ba0d2015-02-13 01:09:55 +0100252 fclose(fp);
Ethan Yonker448c8dc2014-12-08 15:34:34 -0600253 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
254 if (result < 0 && surface != NULL) free(surface);
Dees_Troy51a0e822012-09-05 15:24:24 -0400255 return result;
256}
257
258int res_create_surface_jpg(const char* name, gr_surface* pSurface) {
259 GGLSurface* surface = NULL;
260 int result = 0;
261 struct jpeg_decompress_struct cinfo;
262 struct jpeg_error_mgr jerr;
263
264 FILE* fp = fopen(name, "rb");
265 if (fp == NULL) {
266 char resPath[256];
267
Dees Troy3454ade2015-01-20 19:21:04 +0000268 snprintf(resPath, sizeof(resPath)-1, TWRES "images/%s", name);
Dees_Troy51a0e822012-09-05 15:24:24 -0400269 resPath[sizeof(resPath)-1] = '\0';
270 fp = fopen(resPath, "rb");
271 if (fp == NULL) {
272 result = -1;
273 goto exit;
274 }
275 }
276
277 cinfo.err = jpeg_std_error(&jerr);
278 jpeg_create_decompress(&cinfo);
279
280 /* Specify data source for decompression */
281 jpeg_stdio_src(&cinfo, fp);
282
283 /* Read file header, set default decompression parameters */
284 if (jpeg_read_header(&cinfo, TRUE) != JPEG_HEADER_OK)
285 goto exit;
286
287 /* Start decompressor */
288 (void) jpeg_start_decompress(&cinfo);
289
290 size_t width = cinfo.image_width;
291 size_t height = cinfo.image_height;
292 size_t stride = 4 * width;
293 size_t pixelSize = stride * height;
294
295 surface = malloc(sizeof(GGLSurface) + pixelSize);
296 if (surface == NULL) {
297 result = -8;
298 goto exit;
299 }
300
301 unsigned char* pData = (unsigned char*) (surface + 1);
302 surface->version = sizeof(GGLSurface);
303 surface->width = width;
304 surface->height = height;
305 surface->stride = width; /* Yes, pixels, not bytes */
306 surface->data = pData;
307 surface->format = GGL_PIXEL_FORMAT_RGBX_8888;
308
309 int y;
310 for (y = 0; y < (int) height; ++y) {
311 unsigned char* pRow = pData + y * stride;
312 jpeg_read_scanlines(&cinfo, &pRow, 1);
313
314 int x;
315 for(x = width - 1; x >= 0; x--) {
316 int sx = x * 3;
317 int dx = x * 4;
318 unsigned char r = pRow[sx];
319 unsigned char g = pRow[sx + 1];
320 unsigned char b = pRow[sx + 2];
321 unsigned char a = 0xff;
322 pRow[dx ] = r; // r
323 pRow[dx + 1] = g; // g
324 pRow[dx + 2] = b; // b
325 pRow[dx + 3] = a;
326 }
327 }
328 *pSurface = (gr_surface) surface;
329
330exit:
331 if (fp != NULL)
332 {
333 if (surface)
334 {
335 (void) jpeg_finish_decompress(&cinfo);
336 if (result < 0)
337 {
338 free(surface);
339 }
340 }
341 jpeg_destroy_decompress(&cinfo);
342 fclose(fp);
343 }
344 return result;
345}
346
347int res_create_surface(const char* name, gr_surface* pSurface) {
348 int ret;
349
350 if (!name) return -1;
351
352 if (strlen(name) > 4 && strcmp(name + strlen(name) - 4, ".jpg") == 0)
Ethan Yonker63e414f2015-02-06 15:44:39 -0600353 return res_create_surface_jpg(name,pSurface);
Dees_Troy51a0e822012-09-05 15:24:24 -0400354
355 ret = res_create_surface_png(name,pSurface);
356 if (ret < 0)
357 ret = res_create_surface_jpg(name,pSurface);
358
359 return ret;
360}
361
362void res_free_surface(gr_surface surface) {
363 GGLSurface* pSurface = (GGLSurface*) surface;
364 if (pSurface) {
365 free(pSurface);
366 }
367}
Ethan Yonker63e414f2015-02-06 15:44:39 -0600368
369// Scale image function
370int res_scale_surface(gr_surface source, gr_surface* destination, float scale_w, float scale_h) {
Ethan Yonker727aeb82015-02-10 18:07:44 -0600371 GGLContext *gl = NULL;
Ethan Yonker63e414f2015-02-06 15:44:39 -0600372 GGLSurface* sc_mem_surface = NULL;
373 *destination = NULL;
374 GGLSurface *surface = (GGLSurface*)source;
375 int w = gr_get_width(source), h = gr_get_height(source);
376 int sx = 0, sy = 0, dx = 0, dy = 0;
377 float dw = (float)w * scale_w;
378 float dh = (float)h * scale_h;
379
380 // Create a new surface that is the appropriate size
381 sc_mem_surface = init_display_surface((int)dw, (int)dh);
382 if (!sc_mem_surface) {
383 printf("gr_scale_surface failed to init_display_surface\n");
384 return -1;
385 }
386 sc_mem_surface->format = surface->format;
387
Ethan Yonker727aeb82015-02-10 18:07:44 -0600388 // Initialize the context
389 gglInit(&gl);
Ethan Yonker63e414f2015-02-06 15:44:39 -0600390 gl->colorBuffer(gl, sc_mem_surface);
391 gl->activeTexture(gl, 0);
392
393 // Enable or disable blending based on source surface format
394 if (surface->format == GGL_PIXEL_FORMAT_RGBX_8888) {
395 gl->disable(gl, GGL_BLEND);
396 } else {
397 gl->enable(gl, GGL_BLEND);
398 gl->blendFunc(gl, GGL_ONE, GGL_ZERO);
399 }
400
401 // Bind our source surface to the context
402 gl->bindTexture(gl, surface);
403
404 // Deal with the scaling
405 gl->texParameteri(gl, GGL_TEXTURE_2D, GGL_TEXTURE_MIN_FILTER, GGL_LINEAR);
406 gl->texParameteri(gl, GGL_TEXTURE_2D, GGL_TEXTURE_MAG_FILTER, GGL_LINEAR);
407 gl->texParameteri(gl, GGL_TEXTURE_2D, GGL_TEXTURE_WRAP_S, GGL_CLAMP);
408 gl->texParameteri(gl, GGL_TEXTURE_2D, GGL_TEXTURE_WRAP_T, GGL_CLAMP);
409 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
410 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_AUTOMATIC);
411 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_AUTOMATIC);
412 gl->enable(gl, GGL_TEXTURE_2D);
413
414 int32_t grad[8];
415 memset(grad, 0, sizeof(grad));
416 // s, dsdx, dsdy, scale, t, dtdx, dtdy, tscale <- this is wrong!
417 // This api uses block floating-point for S and T texture coordinates.
418 // All values are given in 16.16, scaled by 'scale'. In other words,
419 // set scale to 0, for 16.16 values.
420
421 // s, dsdx, dsdy, t, dtdx, dtdy, sscale, tscale
422 float dsdx = (float)w / dw;
423 float dtdy = (float)h / dh;
424 grad[0] = ((float)sx - (dsdx * dx)) * 65536;
425 grad[1] = dsdx * 65536;
426 grad[3] = ((float)sy - (dtdy * dy)) * 65536;
427 grad[5] = dtdy * 65536;
428// 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",
429// w, h, dx, dy, dw, dh, dsdx, dtdy, grad[0], grad[1], grad[3], grad[5]);
430 gl->texCoordGradScale8xv(gl, 0 /*tmu*/, grad);
431
432 // draw / scale the source surface to our target context
433 gl->recti(gl, dx, dy, dx + dw, dy + dh);
Ethan Yonker727aeb82015-02-10 18:07:44 -0600434 gglUninit(gl);
435 gl = NULL;
Ethan Yonker63e414f2015-02-06 15:44:39 -0600436 // put the scaled surface in our destination
437 *destination = (gr_surface*) sc_mem_surface;
438 // free memory used in the source
439 res_free_surface(source);
440 source = NULL;
441 return 0;
442}