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