swapinterval.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. **
  3. ** Copyright 2006, The Android Open Source Project
  4. **
  5. ** Licensed under the Apache License, Version 2.0 (the "License");
  6. ** you may not use this file except in compliance with the License.
  7. ** You may obtain a copy of the License at
  8. **
  9. ** http://www.apache.org/licenses/LICENSE-2.0
  10. **
  11. ** Unless required by applicable law or agreed to in writing, software
  12. ** distributed under the License is distributed on an "AS IS" BASIS,
  13. ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. ** See the License for the specific language governing permissions and
  15. ** limitations under the License.
  16. */
  17. #include <stdlib.h>
  18. #include <stdio.h>
  19. #include <EGL/egl.h>
  20. #include <GLES/gl.h>
  21. #include <GLES/glext.h>
  22. #include <utils/StopWatch.h>
  23. #include <WindowSurface.h>
  24. #include <EGLUtils.h>
  25. using namespace android;
  26. int main(int argc, char** argv)
  27. {
  28. EGLint configAttribs[] = {
  29. EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
  30. EGL_NONE
  31. };
  32. EGLint majorVersion;
  33. EGLint minorVersion;
  34. EGLContext context;
  35. EGLConfig config;
  36. EGLint numConfigs=0;
  37. EGLSurface surface;
  38. EGLint w, h;
  39. EGLDisplay dpy;
  40. WindowSurface windowSurface;
  41. EGLNativeWindowType window = windowSurface.getSurface();
  42. dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
  43. eglInitialize(dpy, &majorVersion, &minorVersion);
  44. eglGetConfigs(dpy, NULL, 0, &numConfigs);
  45. printf("# configs = %d\n", numConfigs);
  46. status_t err = EGLUtils::selectConfigForNativeWindow(
  47. dpy, configAttribs, window, &config);
  48. if (err) {
  49. fprintf(stderr, "error: %s", EGLUtils::strerror(eglGetError()));
  50. eglTerminate(dpy);
  51. return 0;
  52. }
  53. EGLint r,g,b,a, vid;
  54. eglGetConfigAttrib(dpy, config, EGL_RED_SIZE, &r);
  55. eglGetConfigAttrib(dpy, config, EGL_GREEN_SIZE, &g);
  56. eglGetConfigAttrib(dpy, config, EGL_BLUE_SIZE, &b);
  57. eglGetConfigAttrib(dpy, config, EGL_ALPHA_SIZE, &a);
  58. eglGetConfigAttrib(dpy, config, EGL_NATIVE_VISUAL_ID, &vid);
  59. surface = eglCreateWindowSurface(dpy, config, window, NULL);
  60. if (surface == EGL_NO_SURFACE) {
  61. EGLint err = eglGetError();
  62. fprintf(stderr, "error: %s, config=%p, format = %d-%d-%d-%d, visual-id = %d\n",
  63. EGLUtils::strerror(err), config, r,g,b,a, vid);
  64. eglTerminate(dpy);
  65. return 0;
  66. } else {
  67. printf("config=%p, format = %d-%d-%d-%d, visual-id = %d\n",
  68. config, r,g,b,a, vid);
  69. }
  70. context = eglCreateContext(dpy, config, NULL, NULL);
  71. eglMakeCurrent(dpy, surface, surface, context);
  72. eglQuerySurface(dpy, surface, EGL_WIDTH, &w);
  73. eglQuerySurface(dpy, surface, EGL_HEIGHT, &h);
  74. printf("w=%d, h=%d\n", w, h);
  75. glDisable(GL_DITHER);
  76. glEnable(GL_BLEND);
  77. glViewport(0, 0, w, h);
  78. glOrthof(0, w, 0, h, 0, 1);
  79. eglSwapInterval(dpy, 1);
  80. glClearColor(1,0,0,0);
  81. glClear(GL_COLOR_BUFFER_BIT);
  82. eglSwapBuffers(dpy, surface);
  83. int time = 10;
  84. printf("screen should flash red/green quickly for %d s...\n", time);
  85. int c = 0;
  86. nsecs_t start = systemTime();
  87. nsecs_t t;
  88. do {
  89. glClearColor(1,0,0,0);
  90. glClear(GL_COLOR_BUFFER_BIT);
  91. eglSwapBuffers(dpy, surface);
  92. glClearColor(0,1,0,0);
  93. glClear(GL_COLOR_BUFFER_BIT);
  94. eglSwapBuffers(dpy, surface);
  95. t = systemTime() - start;
  96. c += 2;
  97. } while (int(ns2s(t))<=time);
  98. double p = (double(t) / c) / 1000000000.0;
  99. printf("refresh-rate is %f fps (%f ms)\n", 1.0f/p, p*1000.0);
  100. eglTerminate(dpy);
  101. return 0;
  102. }