-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathegl-color-png.c
181 lines (144 loc) · 4.51 KB
/
egl-color-png.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <gbm.h>
#include <png.h>
#include <epoxy/gl.h>
#include <epoxy/egl.h>
extern EGLDisplay display;
extern EGLSurface surface;
#define TARGET_SIZE 256
struct gbm_device *gbm;
struct gbm_surface *gs;
void InitGLES(int width, int height);
void Render(void);
EGLConfig get_config(void)
{
EGLint egl_config_attribs[] = {
EGL_BUFFER_SIZE, 32,
EGL_DEPTH_SIZE, EGL_DONT_CARE,
EGL_STENCIL_SIZE, EGL_DONT_CARE,
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
EGL_NONE,
};
EGLint num_configs;
assert(eglGetConfigs(display, NULL, 0, &num_configs) == EGL_TRUE);
EGLConfig *configs = malloc(num_configs * sizeof(EGLConfig));
assert(eglChooseConfig(display, egl_config_attribs,
configs, num_configs, &num_configs) == EGL_TRUE);
assert(num_configs);
printf("num config %d\n", num_configs);
// Find a config whose native visual ID is the desired GBM format.
for (int i = 0; i < num_configs; ++i) {
EGLint gbm_format;
assert(eglGetConfigAttrib(display, configs[i],
EGL_NATIVE_VISUAL_ID, &gbm_format) == EGL_TRUE);
printf("gbm format %x\n", gbm_format);
if (gbm_format == GBM_FORMAT_ARGB8888) {
EGLConfig ret = configs[i];
free(configs);
return ret;
}
}
// Failed to find a config with matching GBM format.
abort();
}
void RenderTargetInit(void)
{
assert(epoxy_has_egl_extension(EGL_NO_DISPLAY, "EGL_MESA_platform_gbm"));
int fd = open("/dev/dri/renderD128", O_RDWR);
assert(fd >= 0);
gbm = gbm_create_device(fd);
assert(gbm != NULL);
assert((display = eglGetPlatformDisplayEXT(EGL_PLATFORM_GBM_MESA, gbm, NULL)) != EGL_NO_DISPLAY);
EGLint majorVersion;
EGLint minorVersion;
assert(eglInitialize(display, &majorVersion, &minorVersion) == EGL_TRUE);
assert(eglBindAPI(EGL_OPENGL_ES_API) == EGL_TRUE);
EGLConfig config = get_config();
gs = gbm_surface_create(
gbm, TARGET_SIZE, TARGET_SIZE, GBM_BO_FORMAT_ARGB8888,
GBM_BO_USE_LINEAR|GBM_BO_USE_SCANOUT|GBM_BO_USE_RENDERING);
assert(gs);
assert((surface = eglCreatePlatformWindowSurfaceEXT(display, config, gs, NULL)) != EGL_NO_SURFACE);
EGLContext context;
const EGLint contextAttribs[] = {
EGL_CONTEXT_CLIENT_VERSION, 2,
EGL_NONE
};
assert((context = eglCreateContext(display, config, EGL_NO_CONTEXT, contextAttribs)) != EGL_NO_CONTEXT);
assert(eglMakeCurrent(display, surface, surface, context) == EGL_TRUE);
}
int writeImage(char* filename, int width, int height, void *buffer, char* title)
{
int code = 0;
FILE *fp = NULL;
png_structp png_ptr = NULL;
png_infop info_ptr = NULL;
// Open file for writing (binary mode)
fp = fopen(filename, "wb");
if (fp == NULL) {
fprintf(stderr, "Could not open file %s for writing\n", filename);
code = 1;
goto finalise;
}
// Initialize write structure
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (png_ptr == NULL) {
fprintf(stderr, "Could not allocate write struct\n");
code = 1;
goto finalise;
}
// Initialize info structure
info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL) {
fprintf(stderr, "Could not allocate info struct\n");
code = 1;
goto finalise;
}
// Setup Exception handling
if (setjmp(png_jmpbuf(png_ptr))) {
fprintf(stderr, "Error during png creation\n");
code = 1;
goto finalise;
}
png_init_io(png_ptr, fp);
// Write header (8 bit colour depth)
png_set_IHDR(png_ptr, info_ptr, width, height,
8, PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
// Set title
if (title != NULL) {
png_text title_text;
title_text.compression = PNG_TEXT_COMPRESSION_NONE;
title_text.key = "Title";
title_text.text = title;
png_set_text(png_ptr, info_ptr, &title_text, 1);
}
png_write_info(png_ptr, info_ptr);
// Write image data
int i;
for (i = 0; i < height; i++)
png_write_row(png_ptr, (png_bytep)buffer + i * width * 4);
// End write
png_write_end(png_ptr, NULL);
finalise:
if (fp != NULL) fclose(fp);
if (info_ptr != NULL) png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
if (png_ptr != NULL) png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
return code;
}
int main(void)
{
RenderTargetInit();
InitGLES(TARGET_SIZE, TARGET_SIZE);
Render();
GLubyte result[TARGET_SIZE * TARGET_SIZE * 4] = {0};
glReadPixels(0, 0, TARGET_SIZE, TARGET_SIZE, GL_RGBA, GL_UNSIGNED_BYTE, result);
assert(glGetError() == GL_NO_ERROR);
assert(!writeImage("screenshot.png", TARGET_SIZE, TARGET_SIZE, result, "hello"));
return 0;
}