blob: c591e5398fed5d8a531859d2ffb4bdab40ba0393 [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>
Vladimir Olteand32b7eb2018-07-03 00:04:03 +030022#include <string.h>
Vojtech Bocek03fd6c52014-03-13 18:46:34 +010023
24#include "minui.h"
25
26struct fb_var_screeninfo vi;
Ethan Yonkerfbb43532015-12-28 21:54:50 +010027extern GGLSurface gr_mem_surface;
28extern GRSurface* gr_draw;
Vojtech Bocek03fd6c52014-03-13 18:46:34 +010029
30int gr_save_screenshot(const char *dest)
31{
32 uint32_t y, stride_bytes;
xiaolu26ffa862015-05-23 12:11:09 +080033 volatile int res = -1;
Vojtech Bocek03fd6c52014-03-13 18:46:34 +010034 GGLContext *gl = NULL;
35 GGLSurface surface;
36 uint8_t * volatile img_data = NULL;
37 uint8_t *ptr;
xiaolu26ffa862015-05-23 12:11:09 +080038 FILE * volatile fp = NULL;
Vojtech Bocek03fd6c52014-03-13 18:46:34 +010039 png_structp png_ptr = NULL;
40 png_infop info_ptr = NULL;
41
42 fp = fopen(dest, "wb");
43 if(!fp)
44 goto exit;
45
Ethan Yonkerfbb43532015-12-28 21:54:50 +010046 img_data = (uint8_t *)malloc(gr_mem_surface.stride * gr_mem_surface.height * gr_draw->pixel_bytes);
47 if (!img_data) {
48 printf("gr_save_screenshot failed to malloc img_data\n");
49 goto exit;
50 }
Vojtech Bocek03fd6c52014-03-13 18:46:34 +010051 surface.version = sizeof(surface);
52 surface.width = gr_mem_surface.width;
53 surface.height = gr_mem_surface.height;
xiaolu26ffa862015-05-23 12:11:09 +080054 surface.stride = gr_mem_surface.stride;
Vojtech Bocek03fd6c52014-03-13 18:46:34 +010055 surface.data = img_data;
notsyncing8e4e8ec2018-06-09 10:40:50 +080056
57#if defined(RECOVERY_BGRA)
58 surface.format = GGL_PIXEL_FORMAT_BGRA_8888;
59#else
xiaolu26ffa862015-05-23 12:11:09 +080060 surface.format = GGL_PIXEL_FORMAT_RGBA_8888;
notsyncing8e4e8ec2018-06-09 10:40:50 +080061#endif
Vojtech Bocek03fd6c52014-03-13 18:46:34 +010062
63 gglInit(&gl);
64 gl->colorBuffer(gl, &surface);
65 gl->activeTexture(gl, 0);
66
xiaolu26ffa862015-05-23 12:11:09 +080067 if(gr_mem_surface.format == GGL_PIXEL_FORMAT_RGBX_8888)
68 gl->disable(gl, GGL_BLEND);
69
Vojtech Bocek03fd6c52014-03-13 18:46:34 +010070 gl->bindTexture(gl, &gr_mem_surface);
71 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
72 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
73 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
74 gl->enable(gl, GGL_TEXTURE_2D);
75 gl->texCoord2i(gl, 0, 0);
76 gl->recti(gl, 0, 0, gr_mem_surface.width, gr_mem_surface.height);
77
78 gglUninit(gl);
79 gl = NULL;
80
81 png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
82 if (!png_ptr)
83 goto exit;
84
85 info_ptr = png_create_info_struct(png_ptr);
86 if (info_ptr == NULL)
87 goto exit;
88
89 if (setjmp(png_jmpbuf(png_ptr)))
90 goto exit;
91
92 png_init_io(png_ptr, fp);
93 png_set_IHDR(png_ptr, info_ptr, surface.width, surface.height,
94 8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
95 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
96 png_write_info(png_ptr, info_ptr);
97
xiaolu26ffa862015-05-23 12:11:09 +080098 // To remove the alpha channel for PNG_COLOR_TYPE_RGB format,
99 png_set_filler(png_ptr, 0, PNG_FILLER_AFTER);
100
Vojtech Bocek03fd6c52014-03-13 18:46:34 +0100101 ptr = img_data;
xiaolu26ffa862015-05-23 12:11:09 +0800102 stride_bytes = surface.stride*4;
Vojtech Bocek03fd6c52014-03-13 18:46:34 +0100103 for(y = 0; y < surface.height; ++y)
104 {
105 png_write_row(png_ptr, ptr);
106 ptr += stride_bytes;
107 }
108
109 png_write_end(png_ptr, NULL);
110
111 res = 0;
112exit:
113 if(info_ptr)
114 png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
115 if(png_ptr)
116 png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
117 if(gl)
118 gglUninit(gl);
119 if(img_data)
120 free(img_data);
121 if(fp)
122 fclose(fp);
123 return res;
124}
Vladimir Olteand32b7eb2018-07-03 00:04:03 +0300125
126#define MATRIX_ELEMENT(matrix, row, col, row_size, elem_size) \
127 (((uint8_t*) (matrix)) + (((row) * (elem_size)) * (row_size)) + ((col) * (elem_size)))
128
129#define DO_MATRIX_ROTATION(bits_per_pixel, bytes_per_pixel) \
130{ \
131 for (size_t y = 0; y < src->height; y++) { \
132 for (size_t x = 0; x < src->width; x++) { \
133 /* output pointer in dst->data */ \
134 uint##bits_per_pixel##_t *op; \
135 /* input pointer from src->data */ \
136 const uint##bits_per_pixel##_t *ip; \
137 /* Display coordinates (in dst) corresponding to (x, y) in src */ \
138 size_t x_disp = ROTATION_X_DISP(x, y, dst); \
139 size_t y_disp = ROTATION_Y_DISP(x, y, dst); \
140 \
141 ip = (const uint##bits_per_pixel##_t*) \
142 MATRIX_ELEMENT(src->data, y, x, \
143 src->stride, bytes_per_pixel); \
144 op = (uint##bits_per_pixel##_t*) \
145 MATRIX_ELEMENT(dst->data, y_disp, x_disp, \
146 dst->stride, bytes_per_pixel); \
147 *op = *ip; \
148 } \
149 } \
150}
151
152void surface_ROTATION_transform(gr_surface dst_ptr, const gr_surface src_ptr,
153 size_t num_bytes_per_pixel)
154{
155 GGLSurface *dst = (GGLSurface*) dst_ptr;
156 const GGLSurface *src = (GGLSurface*) src_ptr;
157
158 /* Handle duplicated code via a macro.
159 * This is currently used for rotating surfaces of graphical resources
160 * (32-bit pixel format) and of font glyphs (8-bit pixel format).
161 * If you need to add handling of other pixel formats feel free to do so.
162 */
163 if (num_bytes_per_pixel == 4) {
164 DO_MATRIX_ROTATION(32, 4);
165 } else if (num_bytes_per_pixel == 1) {
166 DO_MATRIX_ROTATION(8, 1);
167 }
168}