The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [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> |
| 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 | |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 32 | #include <png.h> |
| 33 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 34 | #include "minui.h" |
| 35 | |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 36 | // libpng gives "undefined reference to 'pow'" errors, and I have no |
| 37 | // idea how to convince the build system to link with -lm. We don't |
| 38 | // need this functionality (it's used for gamma adjustment) so provide |
| 39 | // a dummy implementation to satisfy the linker. |
| 40 | double pow(double x, double y) { |
| 41 | return x; |
| 42 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 43 | |
| 44 | int res_create_surface(const char* name, gr_surface* pSurface) { |
| 45 | char resPath[256]; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 46 | GGLSurface* surface = NULL; |
| 47 | int result = 0; |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 48 | unsigned char header[8]; |
| 49 | png_structp png_ptr = NULL; |
| 50 | png_infop info_ptr = NULL; |
| 51 | |
| 52 | snprintf(resPath, sizeof(resPath)-1, "/res/images/%s.png", name); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 53 | resPath[sizeof(resPath)-1] = '\0'; |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 54 | FILE* fp = fopen(resPath, "rb"); |
| 55 | if (fp == NULL) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 56 | result = -1; |
| 57 | goto exit; |
| 58 | } |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 59 | |
| 60 | size_t bytesRead = fread(header, 1, sizeof(header), fp); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 61 | if (bytesRead != sizeof(header)) { |
| 62 | result = -2; |
| 63 | goto exit; |
| 64 | } |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 65 | |
| 66 | if (png_sig_cmp(header, 0, sizeof(header))) { |
| 67 | result = -3; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 68 | goto exit; |
| 69 | } |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 70 | |
| 71 | png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); |
| 72 | if (!png_ptr) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 73 | result = -4; |
| 74 | goto exit; |
| 75 | } |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 76 | |
| 77 | info_ptr = png_create_info_struct(png_ptr); |
| 78 | if (!info_ptr) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 79 | result = -5; |
| 80 | goto exit; |
| 81 | } |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 82 | |
| 83 | if (setjmp(png_jmpbuf(png_ptr))) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 84 | result = -6; |
| 85 | goto exit; |
| 86 | } |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 87 | |
| 88 | png_init_io(png_ptr, fp); |
| 89 | png_set_sig_bytes(png_ptr, sizeof(header)); |
| 90 | png_read_info(png_ptr, info_ptr); |
| 91 | |
| 92 | size_t width = info_ptr->width; |
| 93 | size_t height = info_ptr->height; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 94 | size_t stride = 4 * width; |
| 95 | size_t pixelSize = stride * height; |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 96 | |
| 97 | int color_type = info_ptr->color_type; |
| 98 | int bit_depth = info_ptr->bit_depth; |
| 99 | int channels = info_ptr->channels; |
| 100 | if (bit_depth != 8 || (channels != 3 && channels != 4) || |
| 101 | (color_type != PNG_COLOR_TYPE_RGB && |
| 102 | color_type != PNG_COLOR_TYPE_RGBA)) { |
| 103 | return -7; |
| 104 | goto exit; |
| 105 | } |
| 106 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 107 | surface = malloc(sizeof(GGLSurface) + pixelSize); |
| 108 | if (surface == NULL) { |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 109 | result = -8; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 110 | goto exit; |
| 111 | } |
| 112 | unsigned char* pData = (unsigned char*) (surface + 1); |
| 113 | surface->version = sizeof(GGLSurface); |
| 114 | surface->width = width; |
| 115 | surface->height = height; |
| 116 | surface->stride = width; /* Yes, pixels, not bytes */ |
| 117 | surface->data = pData; |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 118 | surface->format = (channels == 3) ? |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 119 | GGL_PIXEL_FORMAT_RGBX_8888 : GGL_PIXEL_FORMAT_RGBA_8888; |
| 120 | |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 121 | int y; |
| 122 | if (channels == 3) { |
| 123 | for (y = 0; y < height; ++y) { |
| 124 | unsigned char* pRow = pData + y * stride; |
| 125 | png_read_row(png_ptr, pRow, NULL); |
| 126 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 127 | int x; |
| 128 | for(x = width - 1; x >= 0; x--) { |
| 129 | int sx = x * 3; |
| 130 | int dx = x * 4; |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 131 | unsigned char r = pRow[sx]; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 132 | unsigned char g = pRow[sx + 1]; |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 133 | unsigned char b = pRow[sx + 2]; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 134 | unsigned char a = 0xff; |
| 135 | pRow[dx ] = r; // r |
| 136 | pRow[dx + 1] = g; // g |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 137 | pRow[dx + 2] = b; // b |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 138 | pRow[dx + 3] = a; |
| 139 | } |
| 140 | } |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 141 | } else { |
| 142 | for (y = 0; y < height; ++y) { |
| 143 | unsigned char* pRow = pData + y * stride; |
| 144 | png_read_row(png_ptr, pRow, NULL); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 145 | } |
| 146 | } |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 147 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 148 | *pSurface = (gr_surface) surface; |
| 149 | |
| 150 | exit: |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 151 | png_destroy_read_struct(&png_ptr, &info_ptr, NULL); |
| 152 | |
| 153 | if (fp != NULL) { |
| 154 | fclose(fp); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 155 | } |
| 156 | if (result < 0) { |
| 157 | if (surface) { |
| 158 | free(surface); |
| 159 | } |
| 160 | } |
| 161 | return result; |
| 162 | } |
| 163 | |
| 164 | void res_free_surface(gr_surface surface) { |
| 165 | GGLSurface* pSurface = (GGLSurface*) surface; |
| 166 | if (pSurface) { |
| 167 | free(pSurface); |
| 168 | } |
| 169 | } |