gl2_perf.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright (C) 2007 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <stdlib.h>
  17. #include <stdio.h>
  18. #include <time.h>
  19. #include <sched.h>
  20. #include <sys/resource.h>
  21. #include <EGL/egl.h>
  22. #include <GLES2/gl2.h>
  23. #include <GLES2/gl2ext.h>
  24. #include <utils/Timers.h>
  25. #include <WindowSurface.h>
  26. #include <EGLUtils.h>
  27. using namespace android;
  28. static void checkEglError(const char* op, EGLBoolean returnVal = EGL_TRUE) {
  29. if (returnVal != EGL_TRUE) {
  30. fprintf(stderr, "%s() returned %d\n", op, returnVal);
  31. }
  32. for (EGLint error = eglGetError(); error != EGL_SUCCESS; error
  33. = eglGetError()) {
  34. fprintf(stderr, "after %s() eglError %s (0x%x)\n", op, EGLUtils::strerror(error),
  35. error);
  36. }
  37. }
  38. bool doTest(uint32_t w, uint32_t h);
  39. static EGLDisplay dpy;
  40. static EGLSurface surface;
  41. int main(int /*argc*/, char** /*argv*/) {
  42. EGLBoolean returnValue;
  43. EGLConfig myConfig = {0};
  44. EGLint context_attribs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE };
  45. EGLint s_configAttribs[] = {
  46. EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
  47. EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
  48. EGL_NONE };
  49. EGLint majorVersion;
  50. EGLint minorVersion;
  51. EGLContext context;
  52. EGLint w, h;
  53. checkEglError("<init>");
  54. dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
  55. checkEglError("eglGetDisplay");
  56. if (dpy == EGL_NO_DISPLAY) {
  57. printf("eglGetDisplay returned EGL_NO_DISPLAY.\n");
  58. return 0;
  59. }
  60. returnValue = eglInitialize(dpy, &majorVersion, &minorVersion);
  61. checkEglError("eglInitialize", returnValue);
  62. if (returnValue != EGL_TRUE) {
  63. printf("eglInitialize failed\n");
  64. return 0;
  65. }
  66. WindowSurface windowSurface;
  67. EGLNativeWindowType window = windowSurface.getSurface();
  68. returnValue = EGLUtils::selectConfigForNativeWindow(dpy, s_configAttribs, window, &myConfig);
  69. if (returnValue) {
  70. printf("EGLUtils::selectConfigForNativeWindow() returned %d", returnValue);
  71. return 0;
  72. }
  73. checkEglError("EGLUtils::selectConfigForNativeWindow");
  74. surface = eglCreateWindowSurface(dpy, myConfig, window, NULL);
  75. checkEglError("eglCreateWindowSurface");
  76. if (surface == EGL_NO_SURFACE) {
  77. printf("gelCreateWindowSurface failed.\n");
  78. return 0;
  79. }
  80. context = eglCreateContext(dpy, myConfig, EGL_NO_CONTEXT, context_attribs);
  81. checkEglError("eglCreateContext");
  82. if (context == EGL_NO_CONTEXT) {
  83. printf("eglCreateContext failed\n");
  84. return 0;
  85. }
  86. returnValue = eglMakeCurrent(dpy, surface, surface, context);
  87. checkEglError("eglMakeCurrent", returnValue);
  88. if (returnValue != EGL_TRUE) {
  89. return 0;
  90. }
  91. eglQuerySurface(dpy, surface, EGL_WIDTH, &w);
  92. checkEglError("eglQuerySurface");
  93. eglQuerySurface(dpy, surface, EGL_HEIGHT, &h);
  94. checkEglError("eglQuerySurface");
  95. glViewport(0, 0, w, h);
  96. for (;;) {
  97. doTest(w, h);
  98. eglSwapBuffers(dpy, surface);
  99. checkEglError("eglSwapBuffers");
  100. }
  101. return 0;
  102. }
  103. void ptSwap() {
  104. eglSwapBuffers(dpy, surface);
  105. }