Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

demo2.c 4.8KB

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