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