Vojtech Bocek | 03fd6c5 | 2014-03-13 18:46:34 +0100 | [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 <stdio.h> |
Matt Mower | 2b18a53 | 2015-02-20 16:58:05 -0600 | [diff] [blame] | 18 | #include <stdlib.h> |
Vojtech Bocek | 03fd6c5 | 2014-03-13 18:46:34 +0100 | [diff] [blame] | 19 | #include <png.h> |
| 20 | #include <pixelflinger/pixelflinger.h> |
| 21 | #include <linux/fb.h> |
Vladimir Oltean | d32b7eb | 2018-07-03 00:04:03 +0300 | [diff] [blame^] | 22 | #include <string.h> |
Vojtech Bocek | 03fd6c5 | 2014-03-13 18:46:34 +0100 | [diff] [blame] | 23 | |
| 24 | #include "minui.h" |
| 25 | |
| 26 | struct fb_var_screeninfo vi; |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 27 | extern GGLSurface gr_mem_surface; |
| 28 | extern GRSurface* gr_draw; |
Vojtech Bocek | 03fd6c5 | 2014-03-13 18:46:34 +0100 | [diff] [blame] | 29 | |
| 30 | int gr_save_screenshot(const char *dest) |
| 31 | { |
| 32 | uint32_t y, stride_bytes; |
xiaolu | 26ffa86 | 2015-05-23 12:11:09 +0800 | [diff] [blame] | 33 | volatile int res = -1; |
Vojtech Bocek | 03fd6c5 | 2014-03-13 18:46:34 +0100 | [diff] [blame] | 34 | GGLContext *gl = NULL; |
| 35 | GGLSurface surface; |
| 36 | uint8_t * volatile img_data = NULL; |
| 37 | uint8_t *ptr; |
xiaolu | 26ffa86 | 2015-05-23 12:11:09 +0800 | [diff] [blame] | 38 | FILE * volatile fp = NULL; |
Vojtech Bocek | 03fd6c5 | 2014-03-13 18:46:34 +0100 | [diff] [blame] | 39 | png_structp png_ptr = NULL; |
| 40 | png_infop info_ptr = NULL; |
| 41 | |
| 42 | fp = fopen(dest, "wb"); |
| 43 | if(!fp) |
| 44 | goto exit; |
| 45 | |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 46 | img_data = (uint8_t *)malloc(gr_mem_surface.stride * gr_mem_surface.height * gr_draw->pixel_bytes); |
| 47 | if (!img_data) { |
| 48 | printf("gr_save_screenshot failed to malloc img_data\n"); |
| 49 | goto exit; |
| 50 | } |
Vojtech Bocek | 03fd6c5 | 2014-03-13 18:46:34 +0100 | [diff] [blame] | 51 | surface.version = sizeof(surface); |
| 52 | surface.width = gr_mem_surface.width; |
| 53 | surface.height = gr_mem_surface.height; |
xiaolu | 26ffa86 | 2015-05-23 12:11:09 +0800 | [diff] [blame] | 54 | surface.stride = gr_mem_surface.stride; |
Vojtech Bocek | 03fd6c5 | 2014-03-13 18:46:34 +0100 | [diff] [blame] | 55 | surface.data = img_data; |
notsyncing | 8e4e8ec | 2018-06-09 10:40:50 +0800 | [diff] [blame] | 56 | |
| 57 | #if defined(RECOVERY_BGRA) |
| 58 | surface.format = GGL_PIXEL_FORMAT_BGRA_8888; |
| 59 | #else |
xiaolu | 26ffa86 | 2015-05-23 12:11:09 +0800 | [diff] [blame] | 60 | surface.format = GGL_PIXEL_FORMAT_RGBA_8888; |
notsyncing | 8e4e8ec | 2018-06-09 10:40:50 +0800 | [diff] [blame] | 61 | #endif |
Vojtech Bocek | 03fd6c5 | 2014-03-13 18:46:34 +0100 | [diff] [blame] | 62 | |
| 63 | gglInit(&gl); |
| 64 | gl->colorBuffer(gl, &surface); |
| 65 | gl->activeTexture(gl, 0); |
| 66 | |
xiaolu | 26ffa86 | 2015-05-23 12:11:09 +0800 | [diff] [blame] | 67 | if(gr_mem_surface.format == GGL_PIXEL_FORMAT_RGBX_8888) |
| 68 | gl->disable(gl, GGL_BLEND); |
| 69 | |
Vojtech Bocek | 03fd6c5 | 2014-03-13 18:46:34 +0100 | [diff] [blame] | 70 | gl->bindTexture(gl, &gr_mem_surface); |
| 71 | gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE); |
| 72 | gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE); |
| 73 | gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE); |
| 74 | gl->enable(gl, GGL_TEXTURE_2D); |
| 75 | gl->texCoord2i(gl, 0, 0); |
| 76 | gl->recti(gl, 0, 0, gr_mem_surface.width, gr_mem_surface.height); |
| 77 | |
| 78 | gglUninit(gl); |
| 79 | gl = NULL; |
| 80 | |
| 81 | png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); |
| 82 | if (!png_ptr) |
| 83 | goto exit; |
| 84 | |
| 85 | info_ptr = png_create_info_struct(png_ptr); |
| 86 | if (info_ptr == NULL) |
| 87 | goto exit; |
| 88 | |
| 89 | if (setjmp(png_jmpbuf(png_ptr))) |
| 90 | goto exit; |
| 91 | |
| 92 | png_init_io(png_ptr, fp); |
| 93 | png_set_IHDR(png_ptr, info_ptr, surface.width, surface.height, |
| 94 | 8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE, |
| 95 | PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE); |
| 96 | png_write_info(png_ptr, info_ptr); |
| 97 | |
xiaolu | 26ffa86 | 2015-05-23 12:11:09 +0800 | [diff] [blame] | 98 | // To remove the alpha channel for PNG_COLOR_TYPE_RGB format, |
| 99 | png_set_filler(png_ptr, 0, PNG_FILLER_AFTER); |
| 100 | |
Vojtech Bocek | 03fd6c5 | 2014-03-13 18:46:34 +0100 | [diff] [blame] | 101 | ptr = img_data; |
xiaolu | 26ffa86 | 2015-05-23 12:11:09 +0800 | [diff] [blame] | 102 | stride_bytes = surface.stride*4; |
Vojtech Bocek | 03fd6c5 | 2014-03-13 18:46:34 +0100 | [diff] [blame] | 103 | for(y = 0; y < surface.height; ++y) |
| 104 | { |
| 105 | png_write_row(png_ptr, ptr); |
| 106 | ptr += stride_bytes; |
| 107 | } |
| 108 | |
| 109 | png_write_end(png_ptr, NULL); |
| 110 | |
| 111 | res = 0; |
| 112 | exit: |
| 113 | if(info_ptr) |
| 114 | png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1); |
| 115 | if(png_ptr) |
| 116 | png_destroy_write_struct(&png_ptr, (png_infopp)NULL); |
| 117 | if(gl) |
| 118 | gglUninit(gl); |
| 119 | if(img_data) |
| 120 | free(img_data); |
| 121 | if(fp) |
| 122 | fclose(fp); |
| 123 | return res; |
| 124 | } |
Vladimir Oltean | d32b7eb | 2018-07-03 00:04:03 +0300 | [diff] [blame^] | 125 | |
| 126 | #define MATRIX_ELEMENT(matrix, row, col, row_size, elem_size) \ |
| 127 | (((uint8_t*) (matrix)) + (((row) * (elem_size)) * (row_size)) + ((col) * (elem_size))) |
| 128 | |
| 129 | #define DO_MATRIX_ROTATION(bits_per_pixel, bytes_per_pixel) \ |
| 130 | { \ |
| 131 | for (size_t y = 0; y < src->height; y++) { \ |
| 132 | for (size_t x = 0; x < src->width; x++) { \ |
| 133 | /* output pointer in dst->data */ \ |
| 134 | uint##bits_per_pixel##_t *op; \ |
| 135 | /* input pointer from src->data */ \ |
| 136 | const uint##bits_per_pixel##_t *ip; \ |
| 137 | /* Display coordinates (in dst) corresponding to (x, y) in src */ \ |
| 138 | size_t x_disp = ROTATION_X_DISP(x, y, dst); \ |
| 139 | size_t y_disp = ROTATION_Y_DISP(x, y, dst); \ |
| 140 | \ |
| 141 | ip = (const uint##bits_per_pixel##_t*) \ |
| 142 | MATRIX_ELEMENT(src->data, y, x, \ |
| 143 | src->stride, bytes_per_pixel); \ |
| 144 | op = (uint##bits_per_pixel##_t*) \ |
| 145 | MATRIX_ELEMENT(dst->data, y_disp, x_disp, \ |
| 146 | dst->stride, bytes_per_pixel); \ |
| 147 | *op = *ip; \ |
| 148 | } \ |
| 149 | } \ |
| 150 | } |
| 151 | |
| 152 | void surface_ROTATION_transform(gr_surface dst_ptr, const gr_surface src_ptr, |
| 153 | size_t num_bytes_per_pixel) |
| 154 | { |
| 155 | GGLSurface *dst = (GGLSurface*) dst_ptr; |
| 156 | const GGLSurface *src = (GGLSurface*) src_ptr; |
| 157 | |
| 158 | /* Handle duplicated code via a macro. |
| 159 | * This is currently used for rotating surfaces of graphical resources |
| 160 | * (32-bit pixel format) and of font glyphs (8-bit pixel format). |
| 161 | * If you need to add handling of other pixel formats feel free to do so. |
| 162 | */ |
| 163 | if (num_bytes_per_pixel == 4) { |
| 164 | DO_MATRIX_ROTATION(32, 4); |
| 165 | } else if (num_bytes_per_pixel == 1) { |
| 166 | DO_MATRIX_ROTATION(8, 1); |
| 167 | } |
| 168 | } |