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