2 * Copyright (C) 2009 Chia-I Wu <olv@0xlab.org>
5 * Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 #define EGL_EGLEXT_PROTOTYPES
34 #include <EGL/eglext.h>
58 EGLModeMESA modes
[MAX_MODES
];
59 EGLint num_screens
, num_modes
;
60 EGLint width
, height
, best_mode
;
63 if (!eglGetScreensMESA(screen
.dpy
, &screen
.screen
, 1, &num_screens
) ||
65 printf("eglGetScreensMESA failed\n");
69 if (!eglGetModesMESA(screen
.dpy
, screen
.screen
, modes
, MAX_MODES
,
72 printf("eglGetModesMESA failed!\n");
76 printf("Found %d modes:\n", num_modes
);
81 for (i
= 0; i
< num_modes
; i
++) {
83 eglGetModeAttribMESA(screen
.dpy
, modes
[i
], EGL_WIDTH
, &w
);
84 eglGetModeAttribMESA(screen
.dpy
, modes
[i
], EGL_HEIGHT
, &h
);
85 printf("%3d: %d x %d\n", i
, w
, h
);
86 if (w
> width
&& h
> height
) {
93 screen
.mode
= modes
[best_mode
];
95 screen
.height
= height
;
106 const EGLint attribs
[] = {
107 EGL_SURFACE_TYPE
, 0x0, /* should be EGL_SCREEN_BIT_MESA */
108 EGL_RENDERABLE_TYPE
, 0x0, /* should be EGL_OPENGL_ES_BIT */
113 screen
.dpy
= eglGetDisplay(EGL_DEFAULT_DISPLAY
);
115 printf("eglGetDisplay failed\n");
119 if (!eglInitialize(screen
.dpy
, &maj
, &min
)) {
120 printf("eglInitialize failed\n");
124 printf("EGL_VERSION = %s\n", eglQueryString(screen
.dpy
, EGL_VERSION
));
125 printf("EGL_VENDOR = %s\n", eglQueryString(screen
.dpy
, EGL_VENDOR
));
127 exts
= eglQueryString(screen
.dpy
, EGL_EXTENSIONS
);
130 if (!strstr(exts
, "EGL_MESA_screen_surface")) {
131 printf("EGL_MESA_screen_surface is not supported\n");
135 if (!eglChooseConfig(screen
.dpy
, attribs
, &screen
.conf
, 1,
138 printf("eglChooseConfig failed\n");
147 winsysInitScreen(void)
149 EGLint surf_attribs
[20];
159 screen
.ctx
= eglCreateContext(screen
.dpy
, screen
.conf
,
160 EGL_NO_CONTEXT
, NULL
);
161 if (screen
.ctx
== EGL_NO_CONTEXT
) {
162 printf("eglCreateContext failed\n");
167 surf_attribs
[i
++] = EGL_WIDTH
;
168 surf_attribs
[i
++] = screen
.width
;
169 surf_attribs
[i
++] = EGL_HEIGHT
;
170 surf_attribs
[i
++] = screen
.height
;
171 surf_attribs
[i
++] = EGL_NONE
;
174 printf("Using screen size: %d x %d\n", screen
.width
, screen
.height
);
175 screen
.surf
= eglCreateScreenSurfaceMESA(screen
.dpy
, screen
.conf
,
177 if (screen
.surf
== EGL_NO_SURFACE
) {
178 printf("eglCreateScreenSurfaceMESA failed\n");
182 ok
= eglMakeCurrent(screen
.dpy
, screen
.surf
, screen
.surf
, screen
.ctx
);
184 printf("eglMakeCurrent failed\n");
188 ok
= eglShowScreenSurfaceMESA(screen
.dpy
, screen
.screen
,
189 screen
.surf
, screen
.mode
);
191 printf("eglShowScreenSurfaceMESA failed\n");
204 winsysQueryScreenSize(EGLint
*width
, EGLint
*height
)
210 *width
= screen
.width
;
212 *height
= screen
.height
;
219 winsysFiniScreen(void)
222 eglMakeCurrent(screen
.dpy
, EGL_NO_SURFACE
, EGL_NO_SURFACE
,
224 if (screen
.surf
!= EGL_NO_SURFACE
)
225 eglDestroySurface(screen
.dpy
, screen
.surf
);
226 if (screen
.ctx
!= EGL_NO_CONTEXT
)
227 eglDestroyContext(screen
.dpy
, screen
.ctx
);
228 eglTerminate(screen
.dpy
);
230 memset(&screen
, 0, sizeof(screen
));
236 winsysSwapBuffers(void)
238 eglSwapBuffers(screen
.dpy
, screen
.surf
);
242 /* return current time (in seconds) */
247 gettimeofday(&tv
, NULL
);
248 return (double) tv
.tv_sec
+ tv
.tv_usec
/ 1000000.0;
253 winsysRun(double seconds
, void (*draw_frame
)(void *data
), void *data
)
255 double begin
, end
, last_frame
, duration
;
256 EGLint num_frames
= 0;
259 end
= begin
+ seconds
;
262 while (last_frame
< end
) {
265 last_frame
= winsysNow();
269 duration
= last_frame
- begin
;
270 printf("%d frames in %3.1f seconds = %6.3f FPS\n",
271 num_frames
, duration
, (double) num_frames
/ duration
);