blob: 6ce5704842c12e5a3e5beac4b83eb83aa75931cc [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;
Ethan Yonkerfbb43532015-12-28 21:54:50 +010026extern GGLSurface gr_mem_surface;
27extern GRSurface* gr_draw;
Vojtech Bocek03fd6c52014-03-13 18:46:34 +010028
29int gr_save_screenshot(const char *dest)
30{
31 uint32_t y, stride_bytes;
xiaolu26ffa862015-05-23 12:11:09 +080032 volatile int res = -1;
Vojtech Bocek03fd6c52014-03-13 18:46:34 +010033 GGLContext *gl = NULL;
34 GGLSurface surface;
35 uint8_t * volatile img_data = NULL;
36 uint8_t *ptr;
xiaolu26ffa862015-05-23 12:11:09 +080037 FILE * volatile fp = NULL;
Vojtech Bocek03fd6c52014-03-13 18:46:34 +010038 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 Yonkerfbb43532015-12-28 21:54:50 +010045 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 Bocek03fd6c52014-03-13 18:46:34 +010050 surface.version = sizeof(surface);
51 surface.width = gr_mem_surface.width;
52 surface.height = gr_mem_surface.height;
xiaolu26ffa862015-05-23 12:11:09 +080053 surface.stride = gr_mem_surface.stride;
Vojtech Bocek03fd6c52014-03-13 18:46:34 +010054 surface.data = img_data;
xiaolu26ffa862015-05-23 12:11:09 +080055 surface.format = GGL_PIXEL_FORMAT_RGBA_8888;
Vojtech Bocek03fd6c52014-03-13 18:46:34 +010056
57 gglInit(&gl);
58 gl->colorBuffer(gl, &surface);
59 gl->activeTexture(gl, 0);
60
xiaolu26ffa862015-05-23 12:11:09 +080061 if(gr_mem_surface.format == GGL_PIXEL_FORMAT_RGBX_8888)
62 gl->disable(gl, GGL_BLEND);
63
Vojtech Bocek03fd6c52014-03-13 18:46:34 +010064 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
xiaolu26ffa862015-05-23 12:11:09 +080092 // To remove the alpha channel for PNG_COLOR_TYPE_RGB format,
93 png_set_filler(png_ptr, 0, PNG_FILLER_AFTER);
94
Vojtech Bocek03fd6c52014-03-13 18:46:34 +010095 ptr = img_data;
xiaolu26ffa862015-05-23 12:11:09 +080096 stride_bytes = surface.stride*4;
Vojtech Bocek03fd6c52014-03-13 18:46:34 +010097 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;
106exit:
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}