swapinterval.cpp revision 6cf50a770dabd13cf5b72bb0a6fb9dd002c88db6
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 18#define LOG_TAG "fillrate" 19 20#include <stdlib.h> 21#include <stdio.h> 22 23#include <EGL/egl.h> 24#include <GLES/gl.h> 25#include <GLES/glext.h> 26 27#include <utils/StopWatch.h> 28#include <ui/FramebufferNativeWindow.h> 29#include <ui/EGLUtils.h> 30 31using namespace android; 32 33int main(int argc, char** argv) 34{ 35 EGLint configAttribs[] = { 36 EGL_DEPTH_SIZE, 0, 37 EGL_NONE 38 }; 39 40 EGLint majorVersion; 41 EGLint minorVersion; 42 EGLContext context; 43 EGLConfig config; 44 EGLSurface surface; 45 EGLint w, h; 46 EGLDisplay dpy; 47 48 dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY); 49 eglInitialize(dpy, &majorVersion, &minorVersion); 50 51 EGLNativeWindowType window = android_createDisplaySurface(); 52 53 status_t err = EGLUtils::selectConfigForNativeWindow( 54 dpy, configAttribs, window, &config); 55 if (err) { 56 fprintf(stderr, "couldn't find an EGLConfig matching the screen format\n"); 57 return 0; 58 } 59 60 surface = eglCreateWindowSurface(dpy, config, window, NULL); 61 context = eglCreateContext(dpy, config, NULL, NULL); 62 eglMakeCurrent(dpy, surface, surface, context); 63 eglQuerySurface(dpy, surface, EGL_WIDTH, &w); 64 eglQuerySurface(dpy, surface, EGL_HEIGHT, &h); 65 66 printf("w=%d, h=%d\n", w, h); 67 68 glDisable(GL_DITHER); 69 glEnable(GL_BLEND); 70 71 glViewport(0, 0, w, h); 72 glOrthof(0, w, 0, h, 0, 1); 73 74 eglSwapInterval(dpy, 1); 75 76 glClearColor(1,0,0,0); 77 glClear(GL_COLOR_BUFFER_BIT); 78 eglSwapBuffers(dpy, surface); 79 80 81 int time = 10; 82 printf("screen should flash red/green quickly for %d s...\n", time); 83 84 int c = 0; 85 nsecs_t start = systemTime(); 86 nsecs_t t; 87 do { 88 glClearColor(1,0,0,0); 89 glClear(GL_COLOR_BUFFER_BIT); 90 eglSwapBuffers(dpy, surface); 91 glClearColor(0,1,0,0); 92 glClear(GL_COLOR_BUFFER_BIT); 93 eglSwapBuffers(dpy, surface); 94 t = systemTime() - start; 95 c += 2; 96 } while (int(ns2s(t))<=time); 97 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 101 eglTerminate(dpy); 102 103 return 0; 104} 105