Merge commit 'origin/gallium-master-merge'
[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 double tt = current_time();
134 double dt = tt - ct;
135 ct = tt;
136
137 draw();
138
139 eglSwapBuffers(dpy, surf);
140
141 frames++;
142 }
143
144 GLfloat seconds = ct - st;
145 GLfloat fps = frames / seconds;
146 printf("%d frames in %3.1f seconds = %6.3f FPS\n", frames, seconds, fps);
147 }
148
149 int main(int argc, char *argv[])
150 {
151 int maj, min;
152 EGLContext ctx;
153 EGLSurface screen_surf;
154 EGLConfig configs[MAX_CONFIGS];
155 EGLint numConfigs, i;
156 EGLBoolean b;
157 EGLDisplay d;
158 EGLint screenAttribs[10];
159 EGLModeMESA mode[MAX_MODES];
160 EGLScreenMESA screen;
161 EGLint count, chosenMode = 0;
162 GLboolean printInfo = GL_FALSE;
163 EGLint width = 0, height = 0;
164
165 /* parse cmd line args */
166 for (i = 1; i < argc; i++)
167 {
168 if (strcmp(argv[i], "-info") == 0)
169 {
170 printInfo = GL_TRUE;
171 }
172 else
173 printf("Warning: unknown parameter: %s\n", argv[i]);
174 }
175
176 /* DBR : Create EGL context/surface etc */
177 d = eglGetDisplay((EGLNativeDisplayType)"!EGL_i915");
178 assert(d);
179
180 if (!eglInitialize(d, &maj, &min)) {
181 printf("egltri: eglInitialize failed\n");
182 exit(1);
183 }
184
185 printf("egltri: EGL version = %d.%d\n", maj, min);
186 printf("egltri: EGL_VENDOR = %s\n", eglQueryString(d, EGL_VENDOR));
187
188 /* XXX use ChooseConfig */
189 eglGetConfigs(d, configs, MAX_CONFIGS, &numConfigs);
190 eglGetScreensMESA(d, &screen, 1, &count);
191
192 if (!eglGetModesMESA(d, screen, mode, MAX_MODES, &count) || count == 0) {
193 printf("egltri: eglGetModesMESA failed!\n");
194 return 0;
195 }
196
197 /* Print list of modes, and find the one to use */
198 printf("egltri: Found %d modes:\n", count);
199 for (i = 0; i < count; i++) {
200 EGLint w, h;
201 eglGetModeAttribMESA(d, mode[i], EGL_WIDTH, &w);
202 eglGetModeAttribMESA(d, mode[i], EGL_HEIGHT, &h);
203 printf("%3d: %d x %d\n", i, w, h);
204 if (w > width && h > height) {
205 width = w;
206 height = h;
207 chosenMode = i;
208 }
209 }
210 printf("egltri: Using screen mode/size %d: %d x %d\n", chosenMode, width, height);
211
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 }