Merge branch 'mesa_7_7_branch'
[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 GLfloat seconds, fps;
131
132 while (ct - st < ttr)
133 {
134 ct = current_time();
135
136 draw();
137
138 eglSwapBuffers(dpy, surf);
139
140 frames++;
141 }
142
143 seconds = ct - st;
144 fps = frames / seconds;
145 printf("%d frames in %3.1f seconds = %6.3f FPS\n", frames, seconds, fps);
146 }
147
148 int main(int argc, char *argv[])
149 {
150 int maj, min;
151 EGLContext ctx;
152 EGLSurface screen_surf;
153 EGLConfig configs[MAX_CONFIGS];
154 EGLint numConfigs, i;
155 EGLBoolean b;
156 EGLDisplay d;
157 EGLint screenAttribs[10];
158 EGLModeMESA mode[MAX_MODES];
159 EGLScreenMESA screen;
160 EGLint count, chosenMode = 0;
161 GLboolean printInfo = GL_FALSE;
162 EGLint width = 0, height = 0;
163
164 /* parse cmd line args */
165 for (i = 1; i < argc; i++)
166 {
167 if (strcmp(argv[i], "-info") == 0)
168 {
169 printInfo = GL_TRUE;
170 }
171 else
172 printf("Warning: unknown parameter: %s\n", argv[i]);
173 }
174
175 /* DBR : Create EGL context/surface etc */
176 d = eglGetDisplay(EGL_DEFAULT_DISPLAY);
177 assert(d);
178
179 if (!eglInitialize(d, &maj, &min)) {
180 printf("egltri: eglInitialize failed\n");
181 exit(1);
182 }
183
184 printf("egltri: EGL version = %d.%d\n", maj, min);
185 printf("egltri: EGL_VENDOR = %s\n", eglQueryString(d, EGL_VENDOR));
186
187 /* XXX use ChooseConfig */
188 eglGetConfigs(d, configs, MAX_CONFIGS, &numConfigs);
189 eglGetScreensMESA(d, &screen, 1, &count);
190
191 if (!eglGetModesMESA(d, screen, mode, MAX_MODES, &count) || count == 0) {
192 printf("egltri: eglGetModesMESA failed!\n");
193 return 0;
194 }
195
196 /* Print list of modes, and find the one to use */
197 printf("egltri: Found %d modes:\n", count);
198 for (i = 0; i < count; i++) {
199 EGLint w, h;
200 eglGetModeAttribMESA(d, mode[i], EGL_WIDTH, &w);
201 eglGetModeAttribMESA(d, mode[i], EGL_HEIGHT, &h);
202 printf("%3d: %d x %d\n", i, w, h);
203 if (w > width && h > height) {
204 width = w;
205 height = h;
206 chosenMode = i;
207 }
208 }
209 printf("egltri: Using screen mode/size %d: %d x %d\n", chosenMode, width, height);
210
211 eglBindAPI(EGL_OPENGL_API);
212 ctx = eglCreateContext(d, configs[0], EGL_NO_CONTEXT, NULL);
213 if (ctx == EGL_NO_CONTEXT) {
214 printf("egltri: failed to create context\n");
215 return 0;
216 }
217
218 /* build up screenAttribs array */
219 i = 0;
220 screenAttribs[i++] = EGL_WIDTH;
221 screenAttribs[i++] = width;
222 screenAttribs[i++] = EGL_HEIGHT;
223 screenAttribs[i++] = height;
224 screenAttribs[i++] = EGL_NONE;
225
226 screen_surf = eglCreateScreenSurfaceMESA(d, configs[0], screenAttribs);
227 if (screen_surf == EGL_NO_SURFACE) {
228 printf("egltri: failed to create screen surface\n");
229 return 0;
230 }
231
232 b = eglShowScreenSurfaceMESA(d, screen, screen_surf, mode[chosenMode]);
233 if (!b) {
234 printf("egltri: show surface failed\n");
235 return 0;
236 }
237
238 b = eglMakeCurrent(d, screen_surf, screen_surf, ctx);
239 if (!b) {
240 printf("egltri: make current failed\n");
241 return 0;
242 }
243
244 if (printInfo)
245 {
246 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
247 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
248 printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
249 printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS));
250 }
251
252 init();
253 reshape(width, height);
254
255 glDrawBuffer( GL_BACK );
256
257 run(d, screen_surf, 5.0);
258
259 eglDestroySurface(d, screen_surf);
260 eglDestroyContext(d, ctx);
261 eglTerminate(d);
262
263 return 0;
264 }