Merge branch 'mesa_7_7_branch'
[mesa.git] / progs / egl / demo2.c
1 /*
2 * Exercise EGL API functions
3 */
4
5 #define EGL_EGLEXT_PROTOTYPES
6
7 #include <assert.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <unistd.h>
12
13 #include <EGL/egl.h>
14 #include <EGL/eglext.h>
15 #include <GLES/gl.h>
16
17 /*#define FRONTBUFFER*/
18
19 static void _subset_Rectf(GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2,
20 GLfloat r, GLfloat g, GLfloat b)
21 {
22 GLfloat v[4][2], c[4][4];
23 int i;
24
25 v[0][0] = x1; v[0][1] = y1;
26 v[1][0] = x2; v[1][1] = y1;
27 v[2][0] = x2; v[2][1] = y2;
28 v[3][0] = x1; v[3][1] = y2;
29
30 for (i = 0; i < 4; i++) {
31 c[i][0] = r;
32 c[i][1] = g;
33 c[i][2] = b;
34 c[i][3] = 1.0;
35 }
36
37 glVertexPointer(2, GL_FLOAT, 0, v);
38 glColorPointer(4, GL_FLOAT, 0, v);
39 glEnableClientState(GL_VERTEX_ARRAY);
40 glEnableClientState(GL_COLOR_ARRAY);
41
42 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
43
44 glDisableClientState(GL_VERTEX_ARRAY);
45 glDisableClientState(GL_COLOR_ARRAY);
46 }
47
48
49 static void redraw(EGLDisplay dpy, EGLSurface surf, int rot)
50 {
51 GLfloat r, g, b;
52
53 printf("Redraw event\n");
54
55 glClearColor( rand()/(float)RAND_MAX,
56 rand()/(float)RAND_MAX,
57 rand()/(float)RAND_MAX,
58 1);
59
60 glClear( GL_COLOR_BUFFER_BIT );
61
62 r = rand()/(float)RAND_MAX;
63 g = rand()/(float)RAND_MAX;
64 b = rand()/(float)RAND_MAX;
65
66 glPushMatrix();
67 glRotatef(rot, 0, 0, 1);
68 glScalef(.5, .5, .5);
69 _subset_Rectf( -1, -1, 1, 1, r, g, b );
70 glPopMatrix();
71
72 #ifdef FRONTBUFFER
73 glFlush();
74 #else
75 eglSwapBuffers( dpy, surf );
76 #endif
77 glFinish();
78 }
79
80
81 /**
82 * Test EGL_MESA_screen_surface functions
83 */
84 static void
85 TestScreens(EGLDisplay dpy)
86 {
87 #define MAX 8
88 EGLScreenMESA screens[MAX];
89 EGLint numScreens;
90 EGLint i;
91
92 eglGetScreensMESA(dpy, screens, MAX, &numScreens);
93 printf("Found %d screens\n", numScreens);
94 for (i = 0; i < numScreens; i++) {
95 printf(" Screen %d handle: %d\n", i, (int) screens[i]);
96 }
97 }
98
99
100 int
101 main(int argc, char *argv[])
102 {
103 int maj, min;
104 EGLContext ctx;
105 EGLSurface pbuffer, screen_surf;
106 EGLConfig configs[10];
107 EGLint numConfigs, i;
108 EGLBoolean b;
109 const EGLint pbufAttribs[] = {
110 EGL_WIDTH, 500,
111 EGL_HEIGHT, 500,
112 EGL_NONE
113 };
114 EGLint screenAttribs[32];
115 EGLModeMESA mode;
116 EGLScreenMESA screen;
117 EGLint count;
118
119 EGLDisplay d = eglGetDisplay(EGL_DEFAULT_DISPLAY);
120 assert(d);
121
122 if (!eglInitialize(d, &maj, &min)) {
123 printf("demo: eglInitialize failed\n");
124 exit(1);
125 }
126
127 printf("EGL version = %d.%d\n", maj, min);
128 printf("EGL_VENDOR = %s\n", eglQueryString(d, EGL_VENDOR));
129 if (!strstr(eglQueryString(d, EGL_EXTENSIONS),
130 "EGL_MESA_screen_surface")) {
131 printf("EGL_MESA_screen_surface is not supported\n");
132 exit(1);
133 }
134
135 eglGetConfigs(d, configs, 10, &numConfigs);
136 printf("Got %d EGL configs:\n", numConfigs);
137 for (i = 0; i < numConfigs; i++) {
138 EGLint id, red, depth;
139 eglGetConfigAttrib(d, configs[i], EGL_CONFIG_ID, &id);
140 eglGetConfigAttrib(d, configs[i], EGL_RED_SIZE, &red);
141 eglGetConfigAttrib(d, configs[i], EGL_DEPTH_SIZE, &depth);
142 printf("%2d: Red Size = %d Depth Size = %d\n", id, red, depth);
143 }
144
145 eglGetScreensMESA(d, &screen, 1, &count);
146 eglGetModesMESA(d, screen, &mode, 1, &count);
147
148 eglBindAPI(EGL_OPENGL_API);
149 ctx = eglCreateContext(d, configs[0], EGL_NO_CONTEXT, NULL);
150 if (ctx == EGL_NO_CONTEXT) {
151 printf("failed to create context\n");
152 return 0;
153 }
154
155 pbuffer = eglCreatePbufferSurface(d, configs[0], pbufAttribs);
156 if (pbuffer == EGL_NO_SURFACE) {
157 printf("failed to create pbuffer\n");
158 return 0;
159 }
160
161 b = eglMakeCurrent(d, pbuffer, pbuffer, ctx);
162 if (!b) {
163 printf("make current failed\n");
164 return 0;
165 }
166
167 b = eglMakeCurrent(d, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
168
169 i = 0;
170 screenAttribs[i++] = EGL_WIDTH;
171 eglGetModeAttribMESA(d, mode, EGL_WIDTH, &screenAttribs[i++]);
172 screenAttribs[i++] = EGL_HEIGHT;
173 eglGetModeAttribMESA(d, mode, EGL_HEIGHT, &screenAttribs[i++]);
174 screenAttribs[i] = EGL_NONE;
175
176 screen_surf = eglCreateScreenSurfaceMESA(d, configs[0], screenAttribs);
177 if (screen_surf == EGL_NO_SURFACE) {
178 printf("failed to create screen surface\n");
179 return 0;
180 }
181
182 eglShowScreenSurfaceMESA(d, screen, screen_surf, mode);
183
184 b = eglMakeCurrent(d, screen_surf, screen_surf, ctx);
185 if (!b) {
186 printf("make current failed\n");
187 return 0;
188 }
189
190 glViewport(0, 0, 1024, 768);
191
192 glClearColor( 0,
193 1.0,
194 0,
195 1);
196
197 glClear( GL_COLOR_BUFFER_BIT );
198
199
200 TestScreens(d);
201
202 glShadeModel( GL_FLAT );
203
204 for (i = 0; i < 6; i++) {
205 redraw(d, screen_surf, i*10 );
206
207 printf("sleep(1)\n");
208 sleep(1);
209 }
210
211 eglDestroySurface(d, pbuffer);
212 eglDestroyContext(d, ctx);
213 eglTerminate(d);
214
215 return 0;
216 }