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> |
| 22 | |
| 23 | #include "minui.h" |
| 24 | |
| 25 | struct fb_var_screeninfo vi; |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 26 | extern GGLSurface gr_mem_surface; |
| 27 | extern GRSurface* gr_draw; |
Vojtech Bocek | 03fd6c5 | 2014-03-13 18:46:34 +0100 | [diff] [blame] | 28 | |
| 29 | int gr_save_screenshot(const char *dest) |
| 30 | { |
| 31 | uint32_t y, stride_bytes; |
xiaolu | 26ffa86 | 2015-05-23 12:11:09 +0800 | [diff] [blame] | 32 | volatile int res = -1; |
Vojtech Bocek | 03fd6c5 | 2014-03-13 18:46:34 +0100 | [diff] [blame] | 33 | GGLContext *gl = NULL; |
| 34 | GGLSurface surface; |
| 35 | uint8_t * volatile img_data = NULL; |
| 36 | uint8_t *ptr; |
xiaolu | 26ffa86 | 2015-05-23 12:11:09 +0800 | [diff] [blame] | 37 | FILE * volatile fp = NULL; |
Vojtech Bocek | 03fd6c5 | 2014-03-13 18:46:34 +0100 | [diff] [blame] | 38 | png_structp png_ptr = NULL; |
| 39 | png_infop info_ptr = NULL; |
| 40 | |
| 41 | fp = fopen(dest, "wb"); |
| 42 | if(!fp) |
| 43 | goto exit; |
| 44 | |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 45 | img_data = (uint8_t *)malloc(gr_mem_surface.stride * gr_mem_surface.height * gr_draw->pixel_bytes); |
| 46 | if (!img_data) { |
| 47 | printf("gr_save_screenshot failed to malloc img_data\n"); |
| 48 | goto exit; |
| 49 | } |
Vojtech Bocek | 03fd6c5 | 2014-03-13 18:46:34 +0100 | [diff] [blame] | 50 | surface.version = sizeof(surface); |
| 51 | surface.width = gr_mem_surface.width; |
| 52 | surface.height = gr_mem_surface.height; |
xiaolu | 26ffa86 | 2015-05-23 12:11:09 +0800 | [diff] [blame] | 53 | surface.stride = gr_mem_surface.stride; |
Vojtech Bocek | 03fd6c5 | 2014-03-13 18:46:34 +0100 | [diff] [blame] | 54 | surface.data = img_data; |
xiaolu | 26ffa86 | 2015-05-23 12:11:09 +0800 | [diff] [blame] | 55 | surface.format = GGL_PIXEL_FORMAT_RGBA_8888; |
Vojtech Bocek | 03fd6c5 | 2014-03-13 18:46:34 +0100 | [diff] [blame] | 56 | |
| 57 | gglInit(&gl); |
| 58 | gl->colorBuffer(gl, &surface); |
| 59 | gl->activeTexture(gl, 0); |
| 60 | |
xiaolu | 26ffa86 | 2015-05-23 12:11:09 +0800 | [diff] [blame] | 61 | if(gr_mem_surface.format == GGL_PIXEL_FORMAT_RGBX_8888) |
| 62 | gl->disable(gl, GGL_BLEND); |
| 63 | |
Vojtech Bocek | 03fd6c5 | 2014-03-13 18:46:34 +0100 | [diff] [blame] | 64 | gl->bindTexture(gl, &gr_mem_surface); |
| 65 | gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE); |
| 66 | gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE); |
| 67 | gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE); |
| 68 | gl->enable(gl, GGL_TEXTURE_2D); |
| 69 | gl->texCoord2i(gl, 0, 0); |
| 70 | gl->recti(gl, 0, 0, gr_mem_surface.width, gr_mem_surface.height); |
| 71 | |
| 72 | gglUninit(gl); |
| 73 | gl = NULL; |
| 74 | |
| 75 | png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); |
| 76 | if (!png_ptr) |
| 77 | goto exit; |
| 78 | |
| 79 | info_ptr = png_create_info_struct(png_ptr); |
| 80 | if (info_ptr == NULL) |
| 81 | goto exit; |
| 82 | |
| 83 | if (setjmp(png_jmpbuf(png_ptr))) |
| 84 | goto exit; |
| 85 | |
| 86 | png_init_io(png_ptr, fp); |
| 87 | png_set_IHDR(png_ptr, info_ptr, surface.width, surface.height, |
| 88 | 8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE, |
| 89 | PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE); |
| 90 | png_write_info(png_ptr, info_ptr); |
| 91 | |
xiaolu | 26ffa86 | 2015-05-23 12:11:09 +0800 | [diff] [blame] | 92 | // To remove the alpha channel for PNG_COLOR_TYPE_RGB format, |
| 93 | png_set_filler(png_ptr, 0, PNG_FILLER_AFTER); |
| 94 | |
Vojtech Bocek | 03fd6c5 | 2014-03-13 18:46:34 +0100 | [diff] [blame] | 95 | ptr = img_data; |
xiaolu | 26ffa86 | 2015-05-23 12:11:09 +0800 | [diff] [blame] | 96 | stride_bytes = surface.stride*4; |
Vojtech Bocek | 03fd6c5 | 2014-03-13 18:46:34 +0100 | [diff] [blame] | 97 | for(y = 0; y < surface.height; ++y) |
| 98 | { |
| 99 | png_write_row(png_ptr, ptr); |
| 100 | ptr += stride_bytes; |
| 101 | } |
| 102 | |
| 103 | png_write_end(png_ptr, NULL); |
| 104 | |
| 105 | res = 0; |
| 106 | exit: |
| 107 | if(info_ptr) |
| 108 | png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1); |
| 109 | if(png_ptr) |
| 110 | png_destroy_write_struct(&png_ptr, (png_infopp)NULL); |
| 111 | if(gl) |
| 112 | gglUninit(gl); |
| 113 | if(img_data) |
| 114 | free(img_data); |
| 115 | if(fp) |
| 116 | fclose(fp); |
| 117 | return res; |
| 118 | } |