Merge branch 'mesa_7_6_branch' of git+ssh://agd5f@git.freedesktop.org/git/mesa/mesa
[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 const EGLint screenAttribs[] = {
115 EGL_WIDTH, 1024,
116 EGL_HEIGHT, 768,
117 EGL_NONE
118 };
119 EGLModeMESA mode;
120 EGLScreenMESA screen;
121 EGLint count;
122
123 EGLDisplay d = eglGetDisplay(EGL_DEFAULT_DISPLAY);
124 assert(d);
125
126 if (!eglInitialize(d, &maj, &min)) {
127 printf("demo: eglInitialize failed\n");
128 exit(1);
129 }
130
131 printf("EGL version = %d.%d\n", maj, min);
132 printf("EGL_VENDOR = %s\n", eglQueryString(d, EGL_VENDOR));
133 if (!strstr(eglQueryString(d, EGL_EXTENSIONS),
134 "EGL_MESA_screen_surface")) {
135 printf("EGL_MESA_screen_surface is not supported\n");
136 exit(1);
137 }
138
139 eglGetConfigs(d, configs, 10, &numConfigs);
140 printf("Got %d EGL configs:\n", numConfigs);
141 for (i = 0; i < numConfigs; i++) {
142 EGLint id, red, depth;
143 eglGetConfigAttrib(d, configs[i], EGL_CONFIG_ID, &id);
144 eglGetConfigAttrib(d, configs[i], EGL_RED_SIZE, &red);
145 eglGetConfigAttrib(d, configs[i], EGL_DEPTH_SIZE, &depth);
146 printf("%2d: Red Size = %d Depth Size = %d\n", id, red, depth);
147 }
148
149 eglGetScreensMESA(d, &screen, 1, &count);
150 eglGetModesMESA(d, screen, &mode, 1, &count);
151
152 ctx = eglCreateContext(d, configs[0], EGL_NO_CONTEXT, NULL);
153 if (ctx == EGL_NO_CONTEXT) {
154 printf("failed to create context\n");
155 return 0;
156 }
157
158 pbuffer = eglCreatePbufferSurface(d, configs[0], pbufAttribs);
159 if (pbuffer == EGL_NO_SURFACE) {
160 printf("failed to create pbuffer\n");
161 return 0;
162 }
163
164 b = eglMakeCurrent(d, pbuffer, pbuffer, ctx);
165 if (!b) {
166 printf("make current failed\n");
167 return 0;
168 }
169
170 b = eglMakeCurrent(d, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
171
172 screen_surf = eglCreateScreenSurfaceMESA(d, configs[0], screenAttribs);
173 if (screen_surf == EGL_NO_SURFACE) {
174 printf("failed to create screen surface\n");
175 return 0;
176 }
177
178 eglShowScreenSurfaceMESA(d, screen, screen_surf, mode);
179
180 b = eglMakeCurrent(d, screen_surf, screen_surf, ctx);
181 if (!b) {
182 printf("make current failed\n");
183 return 0;
184 }
185
186 glViewport(0, 0, 1024, 768);
187
188 glClearColor( 0,
189 1.0,
190 0,
191 1);
192
193 glClear( GL_COLOR_BUFFER_BIT );
194
195
196 TestScreens(d);
197
198 glShadeModel( GL_FLAT );
199
200 for (i = 0; i < 6; i++) {
201 redraw(d, screen_surf, i*10 );
202
203 printf("sleep(1)\n");
204 sleep(1);
205 }
206
207 eglDestroySurface(d, pbuffer);
208 eglDestroyContext(d, ctx);
209 eglTerminate(d);
210
211 return 0;
212 }