egl: updated egl.h include paths
[mesa.git] / progs / egl / demo3.c
1 /*
2 * Exercise EGL API functions
3 */
4
5 #define EGL_EGLEXT_PROTOTYPES
6
7 #include <EGL/egl.h>
8 #include <EGL/eglext.h>
9 #include <GL/gl.h>
10 #include <assert.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14
15
16
17 #define PIXEL_CENTER(x) ((long)(x) + 0.5)
18
19 #define GAP 10
20 #define ROWS 3
21 #define COLS 4
22
23 #define OPENGL_WIDTH 48
24 #define OPENGL_HEIGHT 13
25
26
27 GLenum rgb, doubleBuffer, windType;
28 GLint windW, windH;
29
30 GLenum mode1, mode2;
31 GLint boxW, boxH;
32 GLubyte OpenGL_bits[] = {
33 0x00, 0x03, 0x00, 0x00, 0x00, 0x00,
34 0x7f, 0xfb, 0xff, 0xff, 0xff, 0x01,
35 0x7f, 0xfb, 0xff, 0xff, 0xff, 0x01,
36 0x00, 0x03, 0x00, 0x00, 0x00, 0x00,
37 0x3e, 0x8f, 0xb7, 0xf9, 0xfc, 0x01,
38 0x63, 0xdb, 0xb0, 0x8d, 0x0d, 0x00,
39 0x63, 0xdb, 0xb7, 0x8d, 0x0d, 0x00,
40 0x63, 0xdb, 0xb6, 0x8d, 0x0d, 0x00,
41 0x63, 0x8f, 0xf3, 0xcc, 0x0d, 0x00,
42 0x63, 0x00, 0x00, 0x0c, 0x4c, 0x0a,
43 0x63, 0x00, 0x00, 0x0c, 0x4c, 0x0e,
44 0x63, 0x00, 0x00, 0x8c, 0xed, 0x0e,
45 0x3e, 0x00, 0x00, 0xf8, 0x0c, 0x00,
46 };
47
48
49 static void Init(void)
50 {
51
52 mode1 = GL_TRUE;
53 mode2 = GL_TRUE;
54 }
55
56 static void Reshape(int width, int height)
57 {
58
59 windW = (GLint)width;
60 windH = (GLint)height;
61 }
62
63 #if 0
64 static void RotateColorMask(void)
65 {
66 static GLint rotation = 0;
67
68 rotation = (rotation + 1) & 0x3;
69 switch (rotation) {
70 case 0:
71 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
72 glIndexMask( 0xff );
73 break;
74 case 1:
75 glColorMask(GL_FALSE, GL_TRUE, GL_TRUE, GL_TRUE);
76 glIndexMask(0xFE);
77 break;
78 case 2:
79 glColorMask(GL_TRUE, GL_FALSE, GL_TRUE, GL_TRUE);
80 glIndexMask(0xFD);
81 break;
82 case 3:
83 glColorMask(GL_TRUE, GL_TRUE, GL_FALSE, GL_TRUE);
84 glIndexMask(0xFB);
85 break;
86 }
87 }
88 #endif
89
90 static void Viewport(GLint row, GLint column)
91 {
92 GLint x, y;
93
94 boxW = (windW - (COLS + 1) * GAP) / COLS;
95 boxH = (windH - (ROWS + 1) * GAP) / ROWS;
96
97 x = GAP + column * (boxW + GAP);
98 y = GAP + row * (boxH + GAP);
99
100 glViewport(x, y, boxW, boxH);
101
102 glMatrixMode(GL_PROJECTION);
103 glLoadIdentity();
104 glOrtho(-boxW/2, boxW/2, -boxH/2, boxH/2, 0.0, 1.0);
105 glMatrixMode(GL_MODELVIEW);
106
107 glEnable(GL_SCISSOR_TEST);
108 glScissor(x, y, boxW, boxH);
109 }
110
111 enum {
112 COLOR_BLACK = 0,
113 COLOR_RED,
114 COLOR_GREEN,
115 COLOR_YELLOW,
116 COLOR_BLUE,
117 COLOR_MAGENTA,
118 COLOR_CYAN,
119 COLOR_WHITE
120 };
121
122 static float RGBMap[9][3] = {
123 {0, 0, 0},
124 {1, 0, 0},
125 {0, 1, 0},
126 {1, 1, 0},
127 {0, 0, 1},
128 {1, 0, 1},
129 {0, 1, 1},
130 {1, 1, 1},
131 {0.5, 0.5, 0.5}
132 };
133
134 static void SetColor(int c)
135 {
136 glColor3fv(RGBMap[c]);
137 }
138
139 static void Point(void)
140 {
141 GLint i;
142
143 glBegin(GL_POINTS);
144 SetColor(COLOR_WHITE);
145 glVertex2i(0, 0);
146 for (i = 1; i < 8; i++) {
147 GLint j = i * 2;
148 SetColor(COLOR_BLACK+i);
149 glVertex2i(-j, -j);
150 glVertex2i(-j, 0);
151 glVertex2i(-j, j);
152 glVertex2i(0, j);
153 glVertex2i(j, j);
154 glVertex2i(j, 0);
155 glVertex2i(j, -j);
156 glVertex2i(0, -j);
157 }
158 glEnd();
159 }
160
161 static void Lines(void)
162 {
163 GLint i;
164
165 glPushMatrix();
166
167 glTranslatef(-12, 0, 0);
168 for (i = 1; i < 8; i++) {
169 SetColor(COLOR_BLACK+i);
170 glBegin(GL_LINES);
171 glVertex2i(-boxW/4, -boxH/4);
172 glVertex2i(boxW/4, boxH/4);
173 glEnd();
174 glTranslatef(4, 0, 0);
175 }
176
177 glPopMatrix();
178
179 glBegin(GL_LINES);
180 glVertex2i(0, 0);
181 glEnd();
182 }
183
184 static void LineStrip(void)
185 {
186
187 glBegin(GL_LINE_STRIP);
188 SetColor(COLOR_RED);
189 glVertex2f(PIXEL_CENTER(-boxW/4), PIXEL_CENTER(-boxH/4));
190 SetColor(COLOR_GREEN);
191 glVertex2f(PIXEL_CENTER(-boxW/4), PIXEL_CENTER(boxH/4));
192 SetColor(COLOR_BLUE);
193 glVertex2f(PIXEL_CENTER(boxW/4), PIXEL_CENTER(boxH/4));
194 SetColor(COLOR_WHITE);
195 glVertex2f(PIXEL_CENTER(boxW/4), PIXEL_CENTER(-boxH/4));
196 glEnd();
197
198 glBegin(GL_LINE_STRIP);
199 glVertex2i(0, 0);
200 glEnd();
201 }
202
203 static void LineLoop(void)
204 {
205
206 glBegin(GL_LINE_LOOP);
207 SetColor(COLOR_RED);
208 glVertex2f(PIXEL_CENTER(-boxW/4), PIXEL_CENTER(-boxH/4));
209 SetColor(COLOR_GREEN);
210 glVertex2f(PIXEL_CENTER(-boxW/4), PIXEL_CENTER(boxH/4));
211 SetColor(COLOR_BLUE);
212 glVertex2f(PIXEL_CENTER(boxW/4), PIXEL_CENTER(boxH/4));
213 SetColor(COLOR_WHITE);
214 glVertex2f(PIXEL_CENTER(boxW/4), PIXEL_CENTER(-boxH/4));
215 glEnd();
216
217 glEnable(GL_LOGIC_OP);
218 glLogicOp(GL_XOR);
219
220 glEnable(GL_BLEND);
221 glBlendFunc(GL_ONE, GL_ONE);
222
223 SetColor(COLOR_MAGENTA);
224 glBegin(GL_LINE_LOOP);
225 glVertex2f(PIXEL_CENTER(-boxW/8), PIXEL_CENTER(-boxH/8));
226 glVertex2f(PIXEL_CENTER(-boxW/8), PIXEL_CENTER(boxH/8));
227 glEnd();
228 glBegin(GL_LINE_LOOP);
229 glVertex2f(PIXEL_CENTER(-boxW/8), PIXEL_CENTER(boxH/8+5));
230 glVertex2f(PIXEL_CENTER(boxW/8), PIXEL_CENTER(boxH/8+5));
231 glEnd();
232 glDisable(GL_LOGIC_OP);
233 glDisable(GL_BLEND);
234
235 SetColor(COLOR_GREEN);
236 glBegin(GL_POINTS);
237 glVertex2i(0, 0);
238 glEnd();
239
240 glBegin(GL_LINE_LOOP);
241 glVertex2i(0, 0);
242 glEnd();
243 }
244
245 static void Bitmap(void)
246 {
247
248 glBegin(GL_LINES);
249 SetColor(COLOR_GREEN);
250 glVertex2i(-boxW/2, 0);
251 glVertex2i(boxW/2, 0);
252 glVertex2i(0, -boxH/2);
253 glVertex2i(0, boxH/2);
254 SetColor(COLOR_RED);
255 glVertex2i(0, -3);
256 glVertex2i(0, -3+OPENGL_HEIGHT);
257 SetColor(COLOR_BLUE);
258 glVertex2i(0, -3);
259 glVertex2i(OPENGL_WIDTH, -3);
260 glEnd();
261
262 SetColor(COLOR_GREEN);
263
264 glPixelStorei(GL_UNPACK_LSB_FIRST, GL_TRUE);
265 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
266
267 glRasterPos2i(0, 0);
268 glBitmap(OPENGL_WIDTH, OPENGL_HEIGHT, 0, 3, 0.0, 0.0, OpenGL_bits);
269 }
270
271 static void Triangles(void)
272 {
273
274 glBegin(GL_TRIANGLES);
275 SetColor(COLOR_GREEN);
276 glVertex2i(-boxW/4, -boxH/4);
277 SetColor(COLOR_RED);
278 glVertex2i(-boxW/8, -boxH/16);
279 SetColor(COLOR_BLUE);
280 glVertex2i(boxW/8, -boxH/16);
281
282 SetColor(COLOR_GREEN);
283 glVertex2i(-boxW/4, boxH/4);
284 SetColor(COLOR_RED);
285 glVertex2i(-boxW/8, boxH/16);
286 SetColor(COLOR_BLUE);
287 glVertex2i(boxW/8, boxH/16);
288 glEnd();
289
290 glBegin(GL_TRIANGLES);
291 glVertex2i(0, 0);
292 glVertex2i(-100, 100);
293 glEnd();
294 }
295
296 static void TriangleStrip(void)
297 {
298
299 glBegin(GL_TRIANGLE_STRIP);
300 SetColor(COLOR_GREEN);
301 glVertex2i(-boxW/4, -boxH/4);
302 SetColor(COLOR_RED);
303 glVertex2i(-boxW/4, boxH/4);
304 SetColor(COLOR_BLUE);
305 glVertex2i(0, -boxH/4);
306 SetColor(COLOR_WHITE);
307 glVertex2i(0, boxH/4);
308 SetColor(COLOR_CYAN);
309 glVertex2i(boxW/4, -boxH/4);
310 SetColor(COLOR_YELLOW);
311 glVertex2i(boxW/4, boxH/4);
312 glEnd();
313
314 glBegin(GL_TRIANGLE_STRIP);
315 glVertex2i(0, 0);
316 glVertex2i(-100, 100);
317 glEnd();
318 }
319
320 static void TriangleFan(void)
321 {
322 GLint vx[8][2];
323 GLint x0, y0, x1, y1, x2, y2, x3, y3;
324 GLint i;
325
326 y0 = -boxH/4;
327 y1 = y0 + boxH/2/3;
328 y2 = y1 + boxH/2/3;
329 y3 = boxH/4;
330 x0 = -boxW/4;
331 x1 = x0 + boxW/2/3;
332 x2 = x1 + boxW/2/3;
333 x3 = boxW/4;
334
335 vx[0][0] = x0; vx[0][1] = y1;
336 vx[1][0] = x0; vx[1][1] = y2;
337 vx[2][0] = x1; vx[2][1] = y3;
338 vx[3][0] = x2; vx[3][1] = y3;
339 vx[4][0] = x3; vx[4][1] = y2;
340 vx[5][0] = x3; vx[5][1] = y1;
341 vx[6][0] = x2; vx[6][1] = y0;
342 vx[7][0] = x1; vx[7][1] = y0;
343
344 glBegin(GL_TRIANGLE_FAN);
345 SetColor(COLOR_WHITE);
346 glVertex2i(0, 0);
347 for (i = 0; i < 8; i++) {
348 SetColor(COLOR_WHITE-i);
349 glVertex2iv(vx[i]);
350 }
351 glEnd();
352
353 glBegin(GL_TRIANGLE_FAN);
354 glVertex2i(0, 0);
355 glVertex2i(-100, 100);
356 glEnd();
357 }
358
359 static void Rect(void)
360 {
361
362 SetColor(COLOR_GREEN);
363 glRecti(-boxW/4, -boxH/4, boxW/4, boxH/4);
364 }
365
366 static void PolygonFunc(void)
367 {
368 GLint vx[8][2];
369 GLint x0, y0, x1, y1, x2, y2, x3, y3;
370 GLint i;
371
372 y0 = -boxH/4;
373 y1 = y0 + boxH/2/3;
374 y2 = y1 + boxH/2/3;
375 y3 = boxH/4;
376 x0 = -boxW/4;
377 x1 = x0 + boxW/2/3;
378 x2 = x1 + boxW/2/3;
379 x3 = boxW/4;
380
381 vx[0][0] = x0; vx[0][1] = y1;
382 vx[1][0] = x0; vx[1][1] = y2;
383 vx[2][0] = x1; vx[2][1] = y3;
384 vx[3][0] = x2; vx[3][1] = y3;
385 vx[4][0] = x3; vx[4][1] = y2;
386 vx[5][0] = x3; vx[5][1] = y1;
387 vx[6][0] = x2; vx[6][1] = y0;
388 vx[7][0] = x1; vx[7][1] = y0;
389
390 glBegin(GL_POLYGON);
391 for (i = 0; i < 8; i++) {
392 SetColor(COLOR_WHITE-i);
393 glVertex2iv(vx[i]);
394 }
395 glEnd();
396
397 glBegin(GL_POLYGON);
398 glVertex2i(0, 0);
399 glVertex2i(100, 100);
400 glEnd();
401 }
402
403 static void Quads(void)
404 {
405
406 glBegin(GL_QUADS);
407 SetColor(COLOR_GREEN);
408 glVertex2i(-boxW/4, -boxH/4);
409 SetColor(COLOR_RED);
410 glVertex2i(-boxW/8, -boxH/16);
411 SetColor(COLOR_BLUE);
412 glVertex2i(boxW/8, -boxH/16);
413 SetColor(COLOR_WHITE);
414 glVertex2i(boxW/4, -boxH/4);
415
416 SetColor(COLOR_GREEN);
417 glVertex2i(-boxW/4, boxH/4);
418 SetColor(COLOR_RED);
419 glVertex2i(-boxW/8, boxH/16);
420 SetColor(COLOR_BLUE);
421 glVertex2i(boxW/8, boxH/16);
422 SetColor(COLOR_WHITE);
423 glVertex2i(boxW/4, boxH/4);
424 glEnd();
425
426 glBegin(GL_QUADS);
427 glVertex2i(0, 0);
428 glVertex2i(100, 100);
429 glVertex2i(-100, 100);
430 glEnd();
431 }
432
433 static void QuadStrip(void)
434 {
435
436 glBegin(GL_QUAD_STRIP);
437 SetColor(COLOR_GREEN);
438 glVertex2i(-boxW/4, -boxH/4);
439 SetColor(COLOR_RED);
440 glVertex2i(-boxW/4, boxH/4);
441 SetColor(COLOR_BLUE);
442 glVertex2i(0, -boxH/4);
443 SetColor(COLOR_WHITE);
444 glVertex2i(0, boxH/4);
445 SetColor(COLOR_CYAN);
446 glVertex2i(boxW/4, -boxH/4);
447 SetColor(COLOR_YELLOW);
448 glVertex2i(boxW/4, boxH/4);
449 glEnd();
450
451 glBegin(GL_QUAD_STRIP);
452 glVertex2i(0, 0);
453 glVertex2i(100, 100);
454 glVertex2i(-100, 100);
455 glEnd();
456 }
457
458 static void Draw(EGLDisplay dpy, EGLSurface surf)
459 {
460
461 glViewport(0, 0, windW, windH);
462 glDisable(GL_SCISSOR_TEST);
463
464 glPushAttrib(GL_COLOR_BUFFER_BIT);
465
466 glColorMask(1, 1, 1, 1);
467 glIndexMask(~0);
468
469 glClearColor(0.0, 0.0, 0.0, 0.0);
470 glClear(GL_COLOR_BUFFER_BIT);
471
472 glPopAttrib();
473
474 if (mode1) {
475 glShadeModel(GL_SMOOTH);
476 } else {
477 glShadeModel(GL_FLAT);
478 }
479
480 if (mode2) {
481 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
482 } else {
483 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
484 }
485
486 Viewport(0, 0); Point();
487 Viewport(0, 1); Lines();
488 Viewport(0, 2); LineStrip();
489 Viewport(0, 3); LineLoop();
490
491 Viewport(1, 0); Bitmap();
492
493 Viewport(1, 1); TriangleFan();
494 Viewport(1, 2); Triangles();
495 Viewport(1, 3); TriangleStrip();
496
497 Viewport(2, 0); Rect();
498 Viewport(2, 1); PolygonFunc();
499 Viewport(2, 2); Quads();
500 Viewport(2, 3); QuadStrip();
501
502 glFlush();
503
504 if (doubleBuffer) {
505 eglSwapBuffers(dpy, surf);
506 }
507 }
508
509 static void
510 write_ppm(const char *filename, const GLubyte *buffer, int width, int height)
511 {
512 const int binary = 0;
513 FILE *f = fopen( filename, "w" );
514 if (f) {
515 int i, x, y;
516 const GLubyte *ptr = buffer;
517 if (binary) {
518 fprintf(f,"P6\n");
519 fprintf(f,"# ppm-file created by osdemo.c\n");
520 fprintf(f,"%i %i\n", width,height);
521 fprintf(f,"255\n");
522 fclose(f);
523 f = fopen( filename, "ab" ); /* reopen in binary append mode */
524 for (y=height-1; y>=0; y--) {
525 for (x=0; x<width; x++) {
526 i = (y*width + x) * 4;
527 fputc(ptr[i], f); /* write red */
528 fputc(ptr[i+1], f); /* write green */
529 fputc(ptr[i+2], f); /* write blue */
530 }
531 }
532 }
533 else {
534 /*ASCII*/
535 int counter = 0;
536 fprintf(f,"P3\n");
537 fprintf(f,"# ascii ppm file created by osdemo.c\n");
538 fprintf(f,"%i %i\n", width, height);
539 fprintf(f,"255\n");
540 for (y=height-1; y>=0; y--) {
541 for (x=0; x<width; x++) {
542 i = (y*width + x) * 4;
543 fprintf(f, " %3d %3d %3d", ptr[i], ptr[i+1], ptr[i+2]);
544 counter++;
545 if (counter % 5 == 0)
546 fprintf(f, "\n");
547 }
548 }
549 }
550 fclose(f);
551 }
552 }
553
554 #include "../src/egl/main/egldisplay.h"
555
556 typedef struct fb_display
557 {
558 _EGLDisplay Base; /* base class/object */
559 void *pFB;
560 } fbDisplay;
561
562
563 int
564 main(int argc, char *argv[])
565 {
566 int maj, min;
567 EGLContext ctx;
568 EGLSurface screen_surf;
569 EGLConfig configs[10];
570 EGLScreenMESA screen;
571 EGLModeMESA mode;
572 EGLint numConfigs, count;
573 EGLBoolean b;
574 const EGLint screenAttribs[] = {
575 EGL_WIDTH, 1024,
576 EGL_HEIGHT, 768,
577 EGL_NONE
578 };
579
580 /*
581 EGLDisplay d = eglGetDisplay(EGL_DEFAULT_DISPLAY);
582 */
583 EGLDisplay d = eglGetDisplay("!EGL_i915");
584 assert(d);
585
586 if (!eglInitialize(d, &maj, &min)) {
587 printf("demo: eglInitialize failed\n");
588 exit(1);
589 }
590
591 printf("EGL version = %d.%d\n", maj, min);
592 printf("EGL_VENDOR = %s\n", eglQueryString(d, EGL_VENDOR));
593
594 eglGetConfigs(d, configs, 10, &numConfigs);
595 eglGetScreensMESA(d, &screen, 1, &count);
596 eglGetModesMESA(d, screen, &mode, 1, &count);
597
598 ctx = eglCreateContext(d, configs[0], EGL_NO_CONTEXT, NULL);
599 if (ctx == EGL_NO_CONTEXT) {
600 printf("failed to create context\n");
601 return 0;
602 }
603
604 screen_surf = eglCreateScreenSurfaceMESA(d, configs[0], screenAttribs);
605 if (screen_surf == EGL_NO_SURFACE) {
606 printf("failed to create screen surface\n");
607 return 0;
608 }
609
610 eglShowScreenSurfaceMESA(d, screen, screen_surf, mode);
611
612 b = eglMakeCurrent(d, screen_surf, screen_surf, ctx);
613 if (!b) {
614 printf("make current failed\n");
615 return 0;
616 }
617 glViewport(0, 0, 1024, 768);
618
619
620 Init();
621 Reshape(1024, 768);
622
623 glDrawBuffer( GL_FRONT );
624 glClearColor( 0, 1.0, 0, 1);
625
626 glClear( GL_COLOR_BUFFER_BIT );
627
628 doubleBuffer = 1;
629 glDrawBuffer( GL_BACK );
630
631 Draw(d, screen_surf);
632
633 write_ppm("dump.ppm", ((struct fb_display *)_eglLookupDisplay(d))->pFB, 1024, 768);
634
635 eglDestroySurface(d, screen_surf);
636 eglDestroyContext(d, ctx);
637 eglTerminate(d);
638
639 return 0;
640 }