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