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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
|
/*
* Copyright (C) 2009 Chia-I Wu <olv@0xlab.org>
*
* Based on eglgears by
* Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <sys/time.h>
#define EGL_EGLEXT_PROTOTYPES
#include <EGL/egl.h>
#include <EGL/eglext.h>
#include "winsys.h"
#define MAX_MODES 100
static struct {
EGLBoolean verbose;
EGLDisplay dpy;
EGLConfig conf;
EGLScreenMESA screen;
EGLModeMESA mode;
EGLint width, height;
EGLContext ctx;
EGLSurface surf;
} screen;
static EGLBoolean
init_screen(void)
{
EGLModeMESA modes[MAX_MODES];
EGLint num_screens, num_modes;
EGLint width, height, best_mode;
EGLint i;
if (!eglGetScreensMESA(screen.dpy, &screen.screen, 1, &num_screens) ||
!num_screens) {
printf("eglGetScreensMESA failed\n");
return EGL_FALSE;
}
if (!eglGetModesMESA(screen.dpy, screen.screen, modes, MAX_MODES,
&num_modes) ||
!num_modes) {
printf("eglGetModesMESA failed!\n");
return EGL_FALSE;
}
printf("Found %d modes:\n", num_modes);
best_mode = 0;
width = 0;
height = 0;
for (i = 0; i < num_modes; i++) {
EGLint w, h;
eglGetModeAttribMESA(screen.dpy, modes[i], EGL_WIDTH, &w);
eglGetModeAttribMESA(screen.dpy, modes[i], EGL_HEIGHT, &h);
printf("%3d: %d x %d\n", i, w, h);
if (w > width && h > height) {
width = w;
height = h;
best_mode = i;
}
}
screen.mode = modes[best_mode];
screen.width = width;
screen.height = height;
return EGL_TRUE;
}
static EGLBoolean
init_display(void)
{
EGLint maj, min;
const char *exts;
const EGLint attribs[] = {
EGL_SURFACE_TYPE, 0x0, /* should be EGL_SCREEN_BIT_MESA */
EGL_RENDERABLE_TYPE, 0x0, /* should be EGL_OPENGL_ES_BIT */
EGL_NONE
};
EGLint num_configs;
screen.dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
if (!screen.dpy) {
printf("eglGetDisplay failed\n");
return EGL_FALSE;
}
if (!eglInitialize(screen.dpy, &maj, &min)) {
printf("eglInitialize failed\n");
return EGL_FALSE;
}
printf("EGL_VERSION = %s\n", eglQueryString(screen.dpy, EGL_VERSION));
printf("EGL_VENDOR = %s\n", eglQueryString(screen.dpy, EGL_VENDOR));
exts = eglQueryString(screen.dpy, EGL_EXTENSIONS);
assert(exts);
if (!strstr(exts, "EGL_MESA_screen_surface")) {
printf("EGL_MESA_screen_surface is not supported\n");
return EGL_FALSE;
}
if (!eglChooseConfig(screen.dpy, attribs, &screen.conf, 1,
&num_configs) ||
!num_configs) {
printf("eglChooseConfig failed\n");
return EGL_FALSE;
}
return EGL_TRUE;
}
EGLBoolean
winsysInitScreen(void)
{
EGLint surf_attribs[20];
EGLint i;
EGLBoolean ok;
if (!init_display())
goto fail;
if (!init_screen())
goto fail;
/* create context */
screen.ctx = eglCreateContext(screen.dpy, screen.conf,
EGL_NO_CONTEXT, NULL);
if (screen.ctx == EGL_NO_CONTEXT) {
printf("eglCreateContext failed\n");
goto fail;
}
i = 0;
surf_attribs[i++] = EGL_WIDTH;
surf_attribs[i++] = screen.width;
surf_attribs[i++] = EGL_HEIGHT;
surf_attribs[i++] = screen.height;
surf_attribs[i++] = EGL_NONE;
/* create surface */
printf("Using screen size: %d x %d\n", screen.width, screen.height);
screen.surf = eglCreateScreenSurfaceMESA(screen.dpy, screen.conf,
surf_attribs);
if (screen.surf == EGL_NO_SURFACE) {
printf("eglCreateScreenSurfaceMESA failed\n");
goto fail;
}
ok = eglMakeCurrent(screen.dpy, screen.surf, screen.surf, screen.ctx);
if (!ok) {
printf("eglMakeCurrent failed\n");
goto fail;
}
ok = eglShowScreenSurfaceMESA(screen.dpy, screen.screen,
screen.surf, screen.mode);
if (!ok) {
printf("eglShowScreenSurfaceMESA failed\n");
goto fail;
}
return EGL_TRUE;
fail:
winsysFiniScreen();
return EGL_FALSE;
}
EGLBoolean
winsysQueryScreenSize(EGLint *width, EGLint *height)
{
if (!screen.dpy)
return EGL_FALSE;
if (width)
*width = screen.width;
if (height)
*height = screen.height;
return EGL_TRUE;
}
void
winsysFiniScreen(void)
{
if (screen.dpy) {
eglMakeCurrent(screen.dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
EGL_NO_CONTEXT);
if (screen.surf != EGL_NO_SURFACE)
eglDestroySurface(screen.dpy, screen.surf);
if (screen.ctx != EGL_NO_CONTEXT)
eglDestroyContext(screen.dpy, screen.ctx);
eglTerminate(screen.dpy);
memset(&screen, 0, sizeof(screen));
}
}
void
winsysSwapBuffers(void)
{
eglSwapBuffers(screen.dpy, screen.surf);
}
/* return current time (in seconds) */
double
winsysNow(void)
{
struct timeval tv;
gettimeofday(&tv, NULL);
return (double) tv.tv_sec + tv.tv_usec / 1000000.0;
}
void
winsysRun(double seconds, void (*draw_frame)(void *data), void *data)
{
double begin, end, last_frame, duration;
EGLint num_frames = 0;
begin = winsysNow();
end = begin + seconds;
last_frame = begin;
while (last_frame < end) {
draw_frame(data);
winsysSwapBuffers();
last_frame = winsysNow();
num_frames++;
}
duration = last_frame - begin;
printf("%d frames in %3.1f seconds = %6.3f FPS\n",
num_frames, duration, (double) num_frames / duration);
}
|