forked from shugaoye/virglrenderer
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathegltest.c
149 lines (133 loc) · 4.92 KB
/
egltest.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
// test based on https://github.com/matusnovak/rpi-opengl-without-x/blob/master/triangle.c
// clang egltest.c -lEGL build/vtest/libvirgl_test_server.so -o egltest
#include <EGL/egl.h>
#include <GLES2/gl2.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
static const EGLint configAttribs[] = {
EGL_SURFACE_TYPE, EGL_PBUFFER_BIT, EGL_BLUE_SIZE, 8, EGL_GREEN_SIZE, 8,
EGL_RED_SIZE, 8, EGL_DEPTH_SIZE, 8,
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES3_BIT, EGL_NONE};
// Width and height of the desired framebuffer
static const EGLint pbufferAttribs[] = {
EGL_WIDTH,
1280,
EGL_HEIGHT,
720,
EGL_NONE,
};
static const EGLint contextAttribs[] = {EGL_CONTEXT_CLIENT_VERSION, 3,
EGL_NONE};
static const char *eglGetErrorStr()
{
switch (eglGetError())
{
case EGL_SUCCESS:
return "The last function succeeded without error.";
case EGL_NOT_INITIALIZED:
return "EGL is not initialized, or could not be initialized, for the "
"specified EGL display connection.";
case EGL_BAD_ACCESS:
return "EGL cannot access a requested resource (for example a context "
"is bound in another thread).";
case EGL_BAD_ALLOC:
return "EGL failed to allocate resources for the requested operation.";
case EGL_BAD_ATTRIBUTE:
return "An unrecognized attribute or attribute value was passed in the "
"attribute list.";
case EGL_BAD_CONTEXT:
return "An EGLContext argument does not name a valid EGL rendering "
"context.";
case EGL_BAD_CONFIG:
return "An EGLConfig argument does not name a valid EGL frame buffer "
"configuration.";
case EGL_BAD_CURRENT_SURFACE:
return "The current surface of the calling thread is a window, pixel "
"buffer or pixmap that is no longer valid.";
case EGL_BAD_DISPLAY:
return "An EGLDisplay argument does not name a valid EGL display "
"connection.";
case EGL_BAD_SURFACE:
return "An EGLSurface argument does not name a valid surface (window, "
"pixel buffer or pixmap) configured for GL rendering.";
case EGL_BAD_MATCH:
return "Arguments are inconsistent (for example, a valid context "
"requires buffers not supplied by a valid surface).";
case EGL_BAD_PARAMETER:
return "One or more argument values are invalid.";
case EGL_BAD_NATIVE_PIXMAP:
return "A NativePixmapType argument does not refer to a valid native "
"pixmap.";
case EGL_BAD_NATIVE_WINDOW:
return "A NativeWindowType argument does not refer to a valid native "
"window.";
case EGL_CONTEXT_LOST:
return "A power management event has occurred. The application must "
"destroy all contexts and reinitialise OpenGL ES state and "
"objects to continue rendering.";
default:
break;
}
return "Unknown error!";
}
int vtest_main(int argc, char **argv);
int main(int argc, char **argv)
{
EGLDisplay display;
int major, minor;
int desiredWidth, desiredHeight;
GLuint program, vert, frag, vbo;
GLint posLoc, colorLoc, result;
if ((display = eglGetDisplay(EGL_DEFAULT_DISPLAY)) == EGL_NO_DISPLAY)
{
fprintf(stderr, "Failed to get EGL display! Error: %s\n",
eglGetErrorStr());
return EXIT_FAILURE;
}
if (eglInitialize(display, &major, &minor) == EGL_FALSE)
{
fprintf(stderr, "Failed to get EGL version! Error: %s\n",
eglGetErrorStr());
eglTerminate(display);
return EXIT_FAILURE;
}
printf("Initialized EGL version: %d.%d\n", major, minor);
EGLint numConfigs;
EGLConfig config;
if (!eglChooseConfig(display, configAttribs, &config, 1, &numConfigs))
{
fprintf(stderr, "Failed to get EGL config! Error: %s\n",
eglGetErrorStr());
eglTerminate(display);
return EXIT_FAILURE;
}
EGLSurface surface =
eglCreatePbufferSurface(display, config, pbufferAttribs);
if (surface == EGL_NO_SURFACE)
{
fprintf(stderr, "Failed to create EGL surface! Error: %s\n",
eglGetErrorStr());
eglTerminate(display);
return EXIT_FAILURE;
}
eglBindAPI(EGL_OPENGL_API);
EGLContext context =
eglCreateContext(display, config, EGL_NO_CONTEXT, contextAttribs);
if (context == EGL_NO_CONTEXT)
{
fprintf(stderr, "Failed to create EGL context! Error: %s\n",
eglGetErrorStr());
eglDestroySurface(display, surface);
eglTerminate(display);
return EXIT_FAILURE;
}
eglMakeCurrent(display, surface, surface, context);
// init vtest
vtest_main(argc, argv);
// Cleanup
eglDestroyContext(display, context);
eglDestroySurface(display, surface);
eglTerminate(display);
return EXIT_SUCCESS;
}