blob: 6083c7599d643937a0cdf1868e43e5ed9c90da72 [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;
31 int res = -1;
32 GGLContext *gl = NULL;
33 GGLSurface surface;
34 uint8_t * volatile img_data = NULL;
35 uint8_t *ptr;
36 FILE *fp = NULL;
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
44 img_data = malloc(vi.xres * vi.yres * 3);
45 surface.version = sizeof(surface);
46 surface.width = gr_mem_surface.width;
47 surface.height = gr_mem_surface.height;
48 surface.stride = gr_mem_surface.width;
49 surface.data = img_data;
50 surface.format = GGL_PIXEL_FORMAT_RGB_888;
51
52 gglInit(&gl);
53 gl->colorBuffer(gl, &surface);
54 gl->activeTexture(gl, 0);
55
56 gl->bindTexture(gl, &gr_mem_surface);
57 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
58 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
59 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
60 gl->enable(gl, GGL_TEXTURE_2D);
61 gl->texCoord2i(gl, 0, 0);
62 gl->recti(gl, 0, 0, gr_mem_surface.width, gr_mem_surface.height);
63
64 gglUninit(gl);
65 gl = NULL;
66
67 png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
68 if (!png_ptr)
69 goto exit;
70
71 info_ptr = png_create_info_struct(png_ptr);
72 if (info_ptr == NULL)
73 goto exit;
74
75 if (setjmp(png_jmpbuf(png_ptr)))
76 goto exit;
77
78 png_init_io(png_ptr, fp);
79 png_set_IHDR(png_ptr, info_ptr, surface.width, surface.height,
80 8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
81 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
82 png_write_info(png_ptr, info_ptr);
83
84 ptr = img_data;
85 stride_bytes = surface.width*3;
86 for(y = 0; y < surface.height; ++y)
87 {
88 png_write_row(png_ptr, ptr);
89 ptr += stride_bytes;
90 }
91
92 png_write_end(png_ptr, NULL);
93
94 res = 0;
95exit:
96 if(info_ptr)
97 png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
98 if(png_ptr)
99 png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
100 if(gl)
101 gglUninit(gl);
102 if(img_data)
103 free(img_data);
104 if(fp)
105 fclose(fp);
106 return res;
107}