progs/egl: Replace EGL_i915 by EGL_DEFAULT_DISPLAY.
[mesa.git] / progs / egl / egltri.c
1 /*
2 * Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
3 * Copyright (C) 2008 Brian Paul All Rights Reserved.
4 * Copyright (C) 2008 Jakob Bornecrantz All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
20 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24 /*
25 * This program is based on eglgears and xegl_tri.
26 * Remixed by Jakob Bornecrantz
27 *
28 * No command line options.
29 * Program runs for 5 seconds then exits, outputing framerate to console
30 */
31
32 #define EGL_EGLEXT_PROTOTYPES
33
34 #include <assert.h>
35 #include <math.h>
36 #include <stdlib.h>
37 #include <stdio.h>
38 #include <string.h>
39 #include <GL/gl.h>
40 #include <EGL/egl.h>
41 #include <EGL/eglext.h>
42
43 #define MAX_CONFIGS 10
44 #define MAX_MODES 100
45
46
47 /* XXX this probably isn't very portable */
48
49 #include <sys/time.h>
50 #include <unistd.h>
51
52 /* return current time (in seconds) */
53 static double
54 current_time(void)
55 {
56 struct timeval tv;
57 #ifdef __VMS
58 (void) gettimeofday(&tv, NULL );
59 #else
60 struct timezone tz;
61 (void) gettimeofday(&tv, &tz);
62 #endif
63 return (double) tv.tv_sec + tv.tv_usec / 1000000.0;
64 }
65
66
67 static GLfloat view_rotx = 0.0, view_roty = 0.0, view_rotz = 0.0;
68
69 static void draw()
70 {
71 static const GLfloat verts[3][2] = {
72 { -1, -1 },
73 { 1, -1 },
74 { 0, 1 }
75 };
76 static const GLfloat colors[3][3] = {
77 { 1, 0, 0 },
78 { 0, 1, 0 },
79 { 0, 0, 1 }
80 };
81
82 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
83
84 glPushMatrix();
85 glRotatef(view_rotx, 1, 0, 0);
86 glRotatef(view_roty, 0, 1, 0);
87 glRotatef(view_rotz, 0, 0, 1);
88
89 {
90 glVertexPointer(2, GL_FLOAT, 0, verts);
91 glColorPointer(3, GL_FLOAT, 0, colors);
92 glEnableClientState(GL_VERTEX_ARRAY);
93 glEnableClientState(GL_COLOR_ARRAY);
94
95 glDrawArrays(GL_TRIANGLES, 0, 3);
96
97 glDisableClientState(GL_VERTEX_ARRAY);
98 glDisableClientState(GL_COLOR_ARRAY);
99 }
100
101 glPopMatrix();
102 }
103
104 static void init()
105 {
106 glClearColor(0.4, 0.4, 0.4, 0.0);
107 }
108
109 /* new window size or exposure */
110 static void reshape(int width, int height)
111 {
112 GLfloat ar = (GLfloat) width / (GLfloat) height;
113
114 glViewport(0, 0, (GLint) width, (GLint) height);
115
116 glMatrixMode(GL_PROJECTION);
117 glLoadIdentity();
118 glFrustum(-ar, ar, -1, 1, 5.0, 60.0);
119
120 glMatrixMode(GL_MODELVIEW);
121 glLoadIdentity();
122 glTranslatef(0.0, 0.0, -10.0);
123 }
124
125 static void run(EGLDisplay dpy, EGLSurface surf, int ttr)
126 {
127 double st = current_time();
128 double ct = st;
129 int frames = 0;
130
131 while (ct - st < ttr)
132 {
133 ct = current_time();
134
135 draw();
136
137 eglSwapBuffers(dpy, surf);
138
139 frames++;
140 }
141
142 GLfloat seconds = ct - st;
143 GLfloat fps = frames / seconds;
144 printf("%d frames in %3.1f seconds = %6.3f FPS\n", frames, seconds, fps);
145 }
146
147 int main(int argc, char *argv[])
148 {
149 int maj, min;
150 EGLContext ctx;
151 EGLSurface screen_surf;
152 EGLConfig configs[MAX_CONFIGS];
153 EGLint numConfigs, i;
154 EGLBoolean b;
155 EGLDisplay d;
156 EGLint screenAttribs[10];
157 EGLModeMESA mode[MAX_MODES];
158 EGLScreenMESA screen;
159 EGLint count, chosenMode = 0;
160 GLboolean printInfo = GL_FALSE;
161 EGLint width = 0, height = 0;
162
163 /* parse cmd line args */
164 for (i = 1; i < argc; i++)
165 {
166 if (strcmp(argv[i], "-info") == 0)
167 {
168 printInfo = GL_TRUE;
169 }
170 else
171 printf("Warning: unknown parameter: %s\n", argv[i]);
172 }
173
174 /* DBR : Create EGL context/surface etc */
175 d = eglGetDisplay(EGL_DEFAULT_DISPLAY);
176 assert(d);
177
178 if (!eglInitialize(d, &maj, &min)) {
179 printf("egltri: eglInitialize failed\n");
180 exit(1);
181 }
182
183 printf("egltri: EGL version = %d.%d\n", maj, min);
184 printf("egltri: EGL_VENDOR = %s\n", eglQueryString(d, EGL_VENDOR));
185
186 /* XXX use ChooseConfig */
187 eglGetConfigs(d, configs, MAX_CONFIGS, &numConfigs);
188 eglGetScreensMESA(d, &screen, 1, &count);
189
190 if (!eglGetModesMESA(d, screen, mode, MAX_MODES, &count) || count == 0) {
191 printf("egltri: eglGetModesMESA failed!\n");
192 return 0;
193 }
194
195 /* Print list of modes, and find the one to use */
196 printf("egltri: Found %d modes:\n", count);
197 for (i = 0; i < count; i++) {
198 EGLint w, h;
199 eglGetModeAttribMESA(d, mode[i], EGL_WIDTH, &w);
200 eglGetModeAttribMESA(d, mode[i], EGL_HEIGHT, &h);
201 printf("%3d: %d x %d\n", i, w, h);
202 if (w > width && h > height) {
203 width = w;
204 height = h;
205 chosenMode = i;
206 }
207 }
208 printf("egltri: Using screen mode/size %d: %d x %d\n", chosenMode, width, height);
209
210 ctx = eglCreateContext(d, configs[0], EGL_NO_CONTEXT, NULL);
211 if (ctx == EGL_NO_CONTEXT) {
212 printf("egltri: failed to create context\n");
213 return 0;
214 }
215
216 /* build up screenAttribs array */
217 i = 0;
218 screenAttribs[i++] = EGL_WIDTH;
219 screenAttribs[i++] = width;
220 screenAttribs[i++] = EGL_HEIGHT;
221 screenAttribs[i++] = height;
222 screenAttribs[i++] = EGL_NONE;
223
224 screen_surf = eglCreateScreenSurfaceMESA(d, configs[0], screenAttribs);
225 if (screen_surf == EGL_NO_SURFACE) {
226 printf("egltri: failed to create screen surface\n");
227 return 0;
228 }
229
230 b = eglShowScreenSurfaceMESA(d, screen, screen_surf, mode[chosenMode]);
231 if (!b) {
232 printf("egltri: show surface failed\n");
233 return 0;
234 }
235
236 b = eglMakeCurrent(d, screen_surf, screen_surf, ctx);
237 if (!b) {
238 printf("egltri: make current failed\n");
239 return 0;
240 }
241
242 if (printInfo)
243 {
244 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
245 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
246 printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
247 printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS));
248 }
249
250 init();
251 reshape(width, height);
252
253 glDrawBuffer( GL_BACK );
254
255 run(d, screen_surf, 5.0);
256
257 eglDestroySurface(d, screen_surf);
258 eglDestroyContext(d, ctx);
259 eglTerminate(d);
260
261 return 0;
262 }