Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1 | /* |
| 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 Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame^] | 18 | #include <string.h> |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 19 | #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_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 31 | #include <png.h> |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame^] | 32 | |
| 33 | #include <pixelflinger/pixelflinger.h> |
| 34 | extern "C" { |
Dees_Troy | 930bf01 | 2013-08-10 22:19:03 +0000 | [diff] [blame] | 35 | #include "jpeglib.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 36 | } |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame^] | 37 | #include "minui.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 38 | |
Ethan Yonker | 448c8dc | 2014-12-08 15:34:34 -0600 | [diff] [blame] | 39 | #define SURFACE_DATA_ALIGNMENT 8 |
| 40 | |
| 41 | static GGLSurface* malloc_surface(size_t data_size) { |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame^] | 42 | size_t size = sizeof(GGLSurface) + data_size + SURFACE_DATA_ALIGNMENT; |
| 43 | unsigned char* temp = reinterpret_cast<unsigned char*>(malloc(size)); |
Ethan Yonker | 448c8dc | 2014-12-08 15:34:34 -0600 | [diff] [blame] | 44 | if (temp == NULL) return NULL; |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame^] | 45 | GGLSurface* surface = reinterpret_cast<GGLSurface*>(temp); |
Ethan Yonker | 448c8dc | 2014-12-08 15:34:34 -0600 | [diff] [blame] | 46 | surface->data = temp + sizeof(GGLSurface) + |
| 47 | (SURFACE_DATA_ALIGNMENT - (sizeof(GGLSurface) % SURFACE_DATA_ALIGNMENT)); |
| 48 | return surface; |
| 49 | } |
| 50 | |
| 51 | static int open_png(const char* name, png_structp* png_ptr, png_infop* info_ptr, |
that | c26ba0d | 2015-02-13 01:09:55 +0100 | [diff] [blame] | 52 | png_uint_32* width, png_uint_32* height, png_byte* channels, FILE** fpp) { |
Ethan Yonker | 448c8dc | 2014-12-08 15:34:34 -0600 | [diff] [blame] | 53 | char resPath[256]; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 54 | unsigned char header[8]; |
Ethan Yonker | 448c8dc | 2014-12-08 15:34:34 -0600 | [diff] [blame] | 55 | int result = 0; |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame^] | 56 | int color_type, bit_depth; |
| 57 | size_t bytesRead; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 58 | |
Dees Troy | 3454ade | 2015-01-20 19:21:04 +0000 | [diff] [blame] | 59 | snprintf(resPath, sizeof(resPath)-1, TWRES "images/%s.png", name); |
Ethan Yonker | 448c8dc | 2014-12-08 15:34:34 -0600 | [diff] [blame] | 60 | resPath[sizeof(resPath)-1] = '\0'; |
| 61 | FILE* fp = fopen(resPath, "rb"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 62 | if (fp == NULL) { |
Ethan Yonker | ac21cb5 | 2014-12-11 18:05:59 -0600 | [diff] [blame] | 63 | fp = fopen(name, "rb"); |
| 64 | if (fp == NULL) { |
| 65 | result = -1; |
| 66 | goto exit; |
| 67 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 68 | } |
| 69 | |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame^] | 70 | bytesRead = fread(header, 1, sizeof(header), fp); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 71 | 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 Yonker | 448c8dc | 2014-12-08 15:34:34 -0600 | [diff] [blame] | 81 | *png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); |
| 82 | if (!*png_ptr) { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 83 | result = -4; |
| 84 | goto exit; |
| 85 | } |
| 86 | |
Ethan Yonker | 448c8dc | 2014-12-08 15:34:34 -0600 | [diff] [blame] | 87 | *info_ptr = png_create_info_struct(*png_ptr); |
| 88 | if (!*info_ptr) { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 89 | result = -5; |
| 90 | goto exit; |
| 91 | } |
| 92 | |
Ethan Yonker | 448c8dc | 2014-12-08 15:34:34 -0600 | [diff] [blame] | 93 | if (setjmp(png_jmpbuf(*png_ptr))) { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 94 | result = -6; |
| 95 | goto exit; |
| 96 | } |
| 97 | |
Ethan Yonker | 448c8dc | 2014-12-08 15:34:34 -0600 | [diff] [blame] | 98 | png_init_io(*png_ptr, fp); |
| 99 | png_set_sig_bytes(*png_ptr, sizeof(header)); |
| 100 | png_read_info(*png_ptr, *info_ptr); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 101 | |
Ethan Yonker | 448c8dc | 2014-12-08 15:34:34 -0600 | [diff] [blame] | 102 | png_get_IHDR(*png_ptr, *info_ptr, width, height, &bit_depth, |
Ethan Yonker | 304f32f | 2014-11-07 10:14:05 -0600 | [diff] [blame] | 103 | &color_type, NULL, NULL, NULL); |
| 104 | |
Ethan Yonker | 448c8dc | 2014-12-08 15:34:34 -0600 | [diff] [blame] | 105 | *channels = png_get_channels(*png_ptr, *info_ptr); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 106 | |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame^] | 107 | if (bit_depth == 8 && *channels == 3 && color_type == PNG_COLOR_TYPE_RGB) { |
Ethan Yonker | 448c8dc | 2014-12-08 15:34:34 -0600 | [diff] [blame] | 108 | // 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 Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame^] | 119 | } else if (color_type == PNG_COLOR_TYPE_PALETTE) { |
| 120 | png_set_palette_to_rgb(*png_ptr); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 121 | } |
| 122 | |
that | c26ba0d | 2015-02-13 01:09:55 +0100 | [diff] [blame] | 123 | *fpp = fp; |
Ethan Yonker | 448c8dc | 2014-12-08 15:34:34 -0600 | [diff] [blame] | 124 | return result; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 125 | |
Ethan Yonker | 448c8dc | 2014-12-08 15:34:34 -0600 | [diff] [blame] | 126 | exit: |
| 127 | if (result < 0) { |
| 128 | png_destroy_read_struct(png_ptr, info_ptr, NULL); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 129 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 130 | if (fp != NULL) { |
| 131 | fclose(fp); |
| 132 | } |
Ethan Yonker | 448c8dc | 2014-12-08 15:34:34 -0600 | [diff] [blame] | 133 | |
| 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 Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame^] | 144 | // Allocate and return a GRSurface* sufficient for storing an image of |
Ethan Yonker | 448c8dc | 2014-12-08 15:34:34 -0600 | [diff] [blame] | 145 | // the indicated size in the framebuffer pixel format. |
| 146 | static GGLSurface* init_display_surface(png_uint_32 width, png_uint_32 height) { |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame^] | 147 | GGLSurface* surface = malloc_surface(width * height * 4); |
Ethan Yonker | 448c8dc | 2014-12-08 15:34:34 -0600 | [diff] [blame] | 148 | 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. |
| 167 | static 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_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 200 | } |
Ethan Yonker | 448c8dc | 2014-12-08 15:34:34 -0600 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | int 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; |
that | c26ba0d | 2015-02-13 01:09:55 +0100 | [diff] [blame] | 210 | FILE* fp; |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame^] | 211 | unsigned char* p_row; |
| 212 | unsigned int y; |
Ethan Yonker | 448c8dc | 2014-12-08 15:34:34 -0600 | [diff] [blame] | 213 | |
| 214 | *pSurface = NULL; |
| 215 | |
that | c26ba0d | 2015-02-13 01:09:55 +0100 | [diff] [blame] | 216 | result = open_png(name, &png_ptr, &info_ptr, &width, &height, &channels, &fp); |
Ethan Yonker | 448c8dc | 2014-12-08 15:34:34 -0600 | [diff] [blame] | 217 | 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 Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame^] | 225 | #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 Yonker | 448c8dc | 2014-12-08 15:34:34 -0600 | [diff] [blame] | 234 | 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: |
that | c26ba0d | 2015-02-13 01:09:55 +0100 | [diff] [blame] | 248 | fclose(fp); |
Ethan Yonker | 448c8dc | 2014-12-08 15:34:34 -0600 | [diff] [blame] | 249 | png_destroy_read_struct(&png_ptr, &info_ptr, NULL); |
| 250 | if (result < 0 && surface != NULL) free(surface); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 251 | return result; |
| 252 | } |
| 253 | |
| 254 | int res_create_surface_jpg(const char* name, gr_surface* pSurface) { |
| 255 | GGLSurface* surface = NULL; |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame^] | 256 | int result = 0, y; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 257 | struct jpeg_decompress_struct cinfo; |
| 258 | struct jpeg_error_mgr jerr; |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame^] | 259 | unsigned char* pData; |
| 260 | size_t width, height, stride, pixelSize; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 261 | |
| 262 | FILE* fp = fopen(name, "rb"); |
| 263 | if (fp == NULL) { |
| 264 | char resPath[256]; |
| 265 | |
Dees Troy | 3454ade | 2015-01-20 19:21:04 +0000 | [diff] [blame] | 266 | snprintf(resPath, sizeof(resPath)-1, TWRES "images/%s", name); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 267 | 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 Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame^] | 288 | width = cinfo.image_width; |
| 289 | height = cinfo.image_height; |
| 290 | stride = 4 * width; |
| 291 | pixelSize = stride * height; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 292 | |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame^] | 293 | surface = reinterpret_cast<GGLSurface*>(malloc(sizeof(GGLSurface) + pixelSize)); |
| 294 | //p_row = reinterpret_cast<unsigned char*>(malloc(width * 4)); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 295 | if (surface == NULL) { |
| 296 | result = -8; |
| 297 | goto exit; |
| 298 | } |
| 299 | |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame^] | 300 | pData = (unsigned char*) (surface + 1); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 301 | 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_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 308 | 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 Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame^] | 320 | #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_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 326 | pRow[dx ] = r; // r |
| 327 | pRow[dx + 1] = g; // g |
| 328 | pRow[dx + 2] = b; // b |
| 329 | pRow[dx + 3] = a; |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame^] | 330 | #endif |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 331 | } |
| 332 | } |
| 333 | *pSurface = (gr_surface) surface; |
| 334 | |
| 335 | exit: |
| 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 | |
| 352 | int 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 Yonker | 63e414f | 2015-02-06 15:44:39 -0600 | [diff] [blame] | 358 | return res_create_surface_jpg(name,pSurface); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 359 | |
| 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 | |
| 367 | void res_free_surface(gr_surface surface) { |
| 368 | GGLSurface* pSurface = (GGLSurface*) surface; |
| 369 | if (pSurface) { |
| 370 | free(pSurface); |
| 371 | } |
| 372 | } |
Ethan Yonker | 63e414f | 2015-02-06 15:44:39 -0600 | [diff] [blame] | 373 | |
| 374 | // Scale image function |
| 375 | int res_scale_surface(gr_surface source, gr_surface* destination, float scale_w, float scale_h) { |
Ethan Yonker | 727aeb8 | 2015-02-10 18:07:44 -0600 | [diff] [blame] | 376 | GGLContext *gl = NULL; |
Ethan Yonker | 63e414f | 2015-02-06 15:44:39 -0600 | [diff] [blame] | 377 | 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 Yonker | 727aeb8 | 2015-02-10 18:07:44 -0600 | [diff] [blame] | 393 | // Initialize the context |
| 394 | gglInit(&gl); |
Ethan Yonker | 63e414f | 2015-02-06 15:44:39 -0600 | [diff] [blame] | 395 | 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 Yonker | 727aeb8 | 2015-02-10 18:07:44 -0600 | [diff] [blame] | 439 | gglUninit(gl); |
| 440 | gl = NULL; |
Ethan Yonker | 63e414f | 2015-02-06 15:44:39 -0600 | [diff] [blame] | 441 | // 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 | } |