Advertisement
Guest User

Untitled

a guest
May 25th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.99 KB | None | 0 0
  1. /*
  2. * gltest1: GL Color Fliker simple.
  3. * based on Android's swapinterval.cpp simple.
  4. */
  5.  
  6. #include <stdlib.h>
  7. #include <stdio.h>
  8. #include <sys/time.h>
  9. #include <unistd.h>
  10.  
  11. #include <EGL/egl.h>
  12. #include <GLES/gl.h>
  13. #include <GLES/glext.h>
  14.  
  15. #if 0
  16. #define WINMGR_WAYLAND
  17. #endif
  18.  
  19. #ifdef WINMGR_WAYLAND
  20. #include <VlDisplayInfo.h>
  21. #include <wayland-egl-ext.h>
  22. #endif
  23.  
  24. typedef int64_t nsecs_t;
  25.  
  26. static inline nsecs_t ns2s(nsecs_t secs)
  27. {
  28. return secs/1000000000;
  29. }
  30.  
  31. nsecs_t systemTime()
  32. {
  33. struct timeval t;
  34. t.tv_sec = t.tv_usec = 0;
  35. gettimeofday(&t, NULL);
  36. return nsecs_t(t.tv_sec)*1000000000LL + nsecs_t(t.tv_usec)*1000LL;
  37. }
  38.  
  39. #define BAD_VALUE (-1)
  40. #define NAME_NOT_FOUND (-2)
  41. #define NO_ERROR (0)
  42.  
  43. int selectConfigForPixelFormat(
  44. EGLDisplay dpy,
  45. EGLint const* attrs,
  46. int32_t format,
  47. EGLConfig* outConfig)
  48. {
  49. EGLint numConfigs = -1, n=0;
  50.  
  51. if (!attrs)
  52. return BAD_VALUE;
  53.  
  54. if (outConfig == NULL)
  55. return BAD_VALUE;
  56.  
  57. // Get all the "potential match" configs...
  58. if (eglGetConfigs(dpy, NULL, 0, &numConfigs) == EGL_FALSE)
  59. return BAD_VALUE;
  60.  
  61. EGLConfig* const configs = (EGLConfig*)malloc(sizeof(EGLConfig)*numConfigs);
  62. if (eglChooseConfig(dpy, attrs, configs, numConfigs, &n) == EGL_FALSE) {
  63. free(configs);
  64. return BAD_VALUE;
  65. }
  66.  
  67. int i;
  68. EGLConfig config = NULL;
  69. for (i=0 ; i<n ; i++) {
  70. EGLint nativeVisualId = 0;
  71. eglGetConfigAttrib(dpy, configs[i], EGL_NATIVE_VISUAL_ID, &nativeVisualId);
  72. if (nativeVisualId>0 && format == nativeVisualId) {
  73. config = configs[i];
  74. break;
  75. }
  76. }
  77.  
  78. free(configs);
  79.  
  80. if (i<n) {
  81. *outConfig = config;
  82. return NO_ERROR;
  83. }
  84.  
  85. return NAME_NOT_FOUND;
  86. }
  87.  
  88. int main(int argc, char** argv)
  89. {
  90. EGLint format = 1;
  91. EGLint majorVersion;
  92. EGLint minorVersion;
  93. EGLContext context;
  94. EGLConfig config;
  95. EGLSurface surface;
  96. EGLint w, h;
  97. EGLDisplay dpy;
  98. EGLBoolean ret;
  99.  
  100. #ifdef WINMGR_WAYLAND
  101. struct wl_egl_window* window = NULL;
  102. displayInfo screenInfo;
  103. int screenId = 0;
  104.  
  105. initDisplayInfo(screenId, &screenInfo);
  106. #else
  107. void* window = NULL;
  108. #endif
  109.  
  110. EGLint configAttribs[] = {
  111. EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
  112. EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
  113. EGL_ALPHA_SIZE, 1,
  114. EGL_DEPTH_SIZE, 0,
  115. EGL_STENCIL_SIZE, 0,
  116. EGL_NONE
  117. };
  118.  
  119. dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
  120. if (!dpy) {
  121. fprintf(stderr, "couldn't Get a Display\n");
  122. return 0;
  123. }
  124.  
  125. ret = eglInitialize(dpy, &majorVersion, &minorVersion);
  126. if (ret == EGL_FALSE) {
  127. fprintf(stderr, "couldn't Initialize EGL\n");
  128. return 0;
  129. }
  130.  
  131. ret = selectConfigForPixelFormat(dpy, configAttribs, format, &config);
  132. if (ret != NO_ERROR) {
  133. fprintf(stderr, "couldn't find an EGLConfig matching the screen format\n");
  134. return 0;
  135. }
  136.  
  137. #ifdef WINMGR_WAYLAND
  138. window = wl_egl_window_create_extended(NULL, screenInfo.width,
  139. screenInfo.height, screenId);
  140. #endif
  141.  
  142. surface = eglCreateWindowSurface(dpy, config, (EGLNativeWindowType)window, NULL);
  143. context = eglCreateContext(dpy, config, NULL, NULL);
  144.  
  145. eglMakeCurrent(dpy, surface, surface, context);
  146. eglQuerySurface(dpy, surface, EGL_WIDTH, &w);
  147. eglQuerySurface(dpy, surface, EGL_HEIGHT, &h);
  148.  
  149. printf("w=%d, h=%d\n", w, h);
  150.  
  151. glDisable(GL_DITHER);
  152. glEnable(GL_BLEND);
  153. glViewport(0, 0, w, h);
  154. glOrthof(0, w, 0, h, 0, 1);
  155. eglSwapInterval(dpy, 1);
  156.  
  157. int time = 20;
  158. printf("screen should flash red/green quickly for %d s...\n", time);
  159.  
  160. int c = 0;
  161. nsecs_t start = systemTime();
  162. nsecs_t t;
  163. do {
  164. glClearColor(1,0,0,0);
  165. glClear(GL_COLOR_BUFFER_BIT);
  166. eglSwapBuffers(dpy, surface);
  167. glClearColor(0,1,0,0);
  168. sleep(1);
  169. glClear(GL_COLOR_BUFFER_BIT);
  170. eglSwapBuffers(dpy, surface);
  171. sleep(1);
  172. glClearColor(0,0,1,0);
  173. glClear(GL_COLOR_BUFFER_BIT);
  174. eglSwapBuffers(dpy, surface);
  175. t = systemTime() - start;
  176. c += 2;
  177. sleep(1);
  178. } while (int(ns2s(t))<=time);
  179.  
  180. double p = (double(t) / c) / 1000000000.0;
  181. printf("refresh-rate is %f fps (%f ms)\n", 1.0f/p, p*1000.0);
  182.  
  183. eglTerminate(dpy);
  184.  
  185. return 0;
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement