Mathias Agopian | 1c3561e | 2009-08-05 17:38:49 -0700 | [diff] [blame] | 1 | /* |
Mathias Agopian | 0928e31 | 2009-08-07 16:38:10 -0700 | [diff] [blame] | 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 | */ |
Mathias Agopian | 1c3561e | 2009-08-05 17:38:49 -0700 | [diff] [blame] | 17 | |
| 18 | #include <stdlib.h> |
| 19 | #include <stdio.h> |
| 20 | |
| 21 | #include <EGL/egl.h> |
| 22 | #include <GLES/gl.h> |
| 23 | #include <GLES/glext.h> |
| 24 | |
| 25 | #include <utils/StopWatch.h> |
Andy McFadden | 6ef57d7 | 2014-03-05 15:06:53 -0800 | [diff] [blame] | 26 | #include <WindowSurface.h> |
| 27 | #include <EGLUtils.h> |
Mathias Agopian | 1c3561e | 2009-08-05 17:38:49 -0700 | [diff] [blame] | 28 | |
| 29 | using namespace android; |
| 30 | |
| 31 | int main(int argc, char** argv) |
| 32 | { |
| 33 | EGLint configAttribs[] = { |
Mathias Agopian | 0928e31 | 2009-08-07 16:38:10 -0700 | [diff] [blame] | 34 | EGL_SURFACE_TYPE, EGL_WINDOW_BIT, |
| 35 | EGL_NONE |
| 36 | }; |
Mathias Agopian | 1c3561e | 2009-08-05 17:38:49 -0700 | [diff] [blame] | 37 | |
Mathias Agopian | 0928e31 | 2009-08-07 16:38:10 -0700 | [diff] [blame] | 38 | EGLint majorVersion; |
| 39 | EGLint minorVersion; |
| 40 | EGLContext context; |
| 41 | EGLConfig config; |
| 42 | EGLint numConfigs=0; |
| 43 | EGLSurface surface; |
| 44 | EGLint w, h; |
| 45 | EGLDisplay dpy; |
Mathias Agopian | 6cf50a7 | 2009-08-06 16:05:39 -0700 | [diff] [blame] | 46 | |
Mathias Agopian | 1d3bcd6 | 2009-08-10 16:48:22 -0700 | [diff] [blame] | 47 | |
Andy McFadden | 6ef57d7 | 2014-03-05 15:06:53 -0800 | [diff] [blame] | 48 | WindowSurface windowSurface; |
| 49 | EGLNativeWindowType window = windowSurface.getSurface(); |
Mathias Agopian | 1d3bcd6 | 2009-08-10 16:48:22 -0700 | [diff] [blame] | 50 | |
Mathias Agopian | 0928e31 | 2009-08-07 16:38:10 -0700 | [diff] [blame] | 51 | dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY); |
Mathias Agopian | 7dddeac | 2011-08-15 14:56:38 -0700 | [diff] [blame] | 52 | eglInitialize(dpy, &majorVersion, &minorVersion); |
Mathias Agopian | 0928e31 | 2009-08-07 16:38:10 -0700 | [diff] [blame] | 53 | eglGetConfigs(dpy, NULL, 0, &numConfigs); |
| 54 | printf("# configs = %d\n", numConfigs); |
Mathias Agopian | 1c3561e | 2009-08-05 17:38:49 -0700 | [diff] [blame] | 55 | |
Mathias Agopian | 0928e31 | 2009-08-07 16:38:10 -0700 | [diff] [blame] | 56 | status_t err = EGLUtils::selectConfigForNativeWindow( |
| 57 | dpy, configAttribs, window, &config); |
| 58 | if (err) { |
Mathias Agopian | 7dddeac | 2011-08-15 14:56:38 -0700 | [diff] [blame] | 59 | fprintf(stderr, "error: %s", EGLUtils::strerror(eglGetError())); |
| 60 | eglTerminate(dpy); |
Mathias Agopian | 0928e31 | 2009-08-07 16:38:10 -0700 | [diff] [blame] | 61 | return 0; |
| 62 | } |
Mathias Agopian | 1c3561e | 2009-08-05 17:38:49 -0700 | [diff] [blame] | 63 | |
Mathias Agopian | 7dddeac | 2011-08-15 14:56:38 -0700 | [diff] [blame] | 64 | EGLint r,g,b,a, vid; |
Mathias Agopian | 0928e31 | 2009-08-07 16:38:10 -0700 | [diff] [blame] | 65 | eglGetConfigAttrib(dpy, config, EGL_RED_SIZE, &r); |
| 66 | eglGetConfigAttrib(dpy, config, EGL_GREEN_SIZE, &g); |
| 67 | eglGetConfigAttrib(dpy, config, EGL_BLUE_SIZE, &b); |
| 68 | eglGetConfigAttrib(dpy, config, EGL_ALPHA_SIZE, &a); |
Mathias Agopian | 7dddeac | 2011-08-15 14:56:38 -0700 | [diff] [blame] | 69 | eglGetConfigAttrib(dpy, config, EGL_NATIVE_VISUAL_ID, &vid); |
Mathias Agopian | 1c3561e | 2009-08-05 17:38:49 -0700 | [diff] [blame] | 70 | |
Mathias Agopian | 0928e31 | 2009-08-07 16:38:10 -0700 | [diff] [blame] | 71 | surface = eglCreateWindowSurface(dpy, config, window, NULL); |
| 72 | if (surface == EGL_NO_SURFACE) { |
| 73 | EGLint err = eglGetError(); |
Mathias Agopian | 7dddeac | 2011-08-15 14:56:38 -0700 | [diff] [blame] | 74 | fprintf(stderr, "error: %s, config=%p, format = %d-%d-%d-%d, visual-id = %d\n", |
| 75 | EGLUtils::strerror(err), config, r,g,b,a, vid); |
| 76 | eglTerminate(dpy); |
Mathias Agopian | 0928e31 | 2009-08-07 16:38:10 -0700 | [diff] [blame] | 77 | return 0; |
| 78 | } else { |
Mathias Agopian | 7dddeac | 2011-08-15 14:56:38 -0700 | [diff] [blame] | 79 | printf("config=%p, format = %d-%d-%d-%d, visual-id = %d\n", |
| 80 | config, r,g,b,a, vid); |
Mathias Agopian | 0928e31 | 2009-08-07 16:38:10 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | context = eglCreateContext(dpy, config, NULL, NULL); |
| 84 | eglMakeCurrent(dpy, surface, surface, context); |
| 85 | eglQuerySurface(dpy, surface, EGL_WIDTH, &w); |
| 86 | eglQuerySurface(dpy, surface, EGL_HEIGHT, &h); |
| 87 | |
| 88 | printf("w=%d, h=%d\n", w, h); |
| 89 | |
| 90 | glDisable(GL_DITHER); |
| 91 | glEnable(GL_BLEND); |
| 92 | |
| 93 | glViewport(0, 0, w, h); |
| 94 | glOrthof(0, w, 0, h, 0, 1); |
| 95 | |
| 96 | eglSwapInterval(dpy, 1); |
| 97 | |
| 98 | glClearColor(1,0,0,0); |
| 99 | glClear(GL_COLOR_BUFFER_BIT); |
| 100 | eglSwapBuffers(dpy, surface); |
Mathias Agopian | 1c3561e | 2009-08-05 17:38:49 -0700 | [diff] [blame] | 101 | |
| 102 | |
Mathias Agopian | 0928e31 | 2009-08-07 16:38:10 -0700 | [diff] [blame] | 103 | int time = 10; |
| 104 | printf("screen should flash red/green quickly for %d s...\n", time); |
| 105 | |
| 106 | int c = 0; |
| 107 | nsecs_t start = systemTime(); |
| 108 | nsecs_t t; |
| 109 | do { |
| 110 | glClearColor(1,0,0,0); |
| 111 | glClear(GL_COLOR_BUFFER_BIT); |
| 112 | eglSwapBuffers(dpy, surface); |
| 113 | glClearColor(0,1,0,0); |
| 114 | glClear(GL_COLOR_BUFFER_BIT); |
| 115 | eglSwapBuffers(dpy, surface); |
| 116 | t = systemTime() - start; |
| 117 | c += 2; |
| 118 | } while (int(ns2s(t))<=time); |
| 119 | |
| 120 | double p = (double(t) / c) / 1000000000.0; |
| 121 | printf("refresh-rate is %f fps (%f ms)\n", 1.0f/p, p*1000.0); |
| 122 | |
| 123 | eglTerminate(dpy); |
| 124 | |
| 125 | return 0; |
Mathias Agopian | 1c3561e | 2009-08-05 17:38:49 -0700 | [diff] [blame] | 126 | } |