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