blob: ea1182dd9929f685257c56a51c50ed2bd1126a72 [file] [log] [blame]
Vojtech Bocek03fd6c52014-03-13 18:46:34 +01001/*
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 Mower2b18a532015-02-20 16:58:05 -060018#include <stdlib.h>
Vojtech Bocek03fd6c52014-03-13 18:46:34 +010019#include <png.h>
20#include <pixelflinger/pixelflinger.h>
21#include <linux/fb.h>
22
23#include "minui.h"
24
25struct fb_var_screeninfo vi;
26GGLSurface gr_mem_surface;
27
28int gr_save_screenshot(const char *dest)
29{
30 uint32_t y, stride_bytes;
xiaolu26ffa862015-05-23 12:11:09 +080031 volatile int res = -1;
Vojtech Bocek03fd6c52014-03-13 18:46:34 +010032 GGLContext *gl = NULL;
33 GGLSurface surface;
34 uint8_t * volatile img_data = NULL;
35 uint8_t *ptr;
xiaolu26ffa862015-05-23 12:11:09 +080036 FILE * volatile fp = NULL;
Vojtech Bocek03fd6c52014-03-13 18:46:34 +010037 png_structp png_ptr = NULL;
38 png_infop info_ptr = NULL;
39
40 fp = fopen(dest, "wb");
41 if(!fp)
42 goto exit;
43
xiaolu26ffa862015-05-23 12:11:09 +080044 img_data = malloc(gr_mem_surface.stride * vi.yres * 4);
Vojtech Bocek03fd6c52014-03-13 18:46:34 +010045 surface.version = sizeof(surface);
46 surface.width = gr_mem_surface.width;
47 surface.height = gr_mem_surface.height;
xiaolu26ffa862015-05-23 12:11:09 +080048 surface.stride = gr_mem_surface.stride;
Vojtech Bocek03fd6c52014-03-13 18:46:34 +010049 surface.data = img_data;
xiaolu26ffa862015-05-23 12:11:09 +080050 surface.format = GGL_PIXEL_FORMAT_RGBA_8888;
Vojtech Bocek03fd6c52014-03-13 18:46:34 +010051
52 gglInit(&gl);
53 gl->colorBuffer(gl, &surface);
54 gl->activeTexture(gl, 0);
55
xiaolu26ffa862015-05-23 12:11:09 +080056 if(gr_mem_surface.format == GGL_PIXEL_FORMAT_RGBX_8888)
57 gl->disable(gl, GGL_BLEND);
58
Vojtech Bocek03fd6c52014-03-13 18:46:34 +010059 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
xiaolu26ffa862015-05-23 12:11:09 +080087 // To remove the alpha channel for PNG_COLOR_TYPE_RGB format,
88 png_set_filler(png_ptr, 0, PNG_FILLER_AFTER);
89
Vojtech Bocek03fd6c52014-03-13 18:46:34 +010090 ptr = img_data;
xiaolu26ffa862015-05-23 12:11:09 +080091 stride_bytes = surface.stride*4;
Vojtech Bocek03fd6c52014-03-13 18:46:34 +010092 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;
101exit:
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}