swapinterval.cpp revision 0928e31cc7a9ec7367a68796fcaa9c52959216a5
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#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>
26#include <ui/FramebufferNativeWindow.h>
27#include <ui/EGLUtils.h>
28
29using namespace android;
30
31int main(int argc, char** argv)
32{
33    EGLint configAttribs[] = {
34            EGL_SURFACE_TYPE,   EGL_WINDOW_BIT,
35            EGL_NONE
36    };
37
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;
46
47    dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
48    eglInitialize(dpy, 0 ,0) ;//&majorVersion, &minorVersion);
49    eglGetConfigs(dpy, NULL, 0, &numConfigs);
50    printf("# configs = %d\n", numConfigs);
51
52    EGLNativeWindowType window = android_createDisplaySurface();
53
54    status_t err = EGLUtils::selectConfigForNativeWindow(
55            dpy, configAttribs, window, &config);
56    if (err) {
57        fprintf(stderr, "couldn't find an EGLConfig matching the screen format\n");
58        return 0;
59    }
60
61    EGLint r,g,b,a;
62    eglGetConfigAttrib(dpy, config, EGL_RED_SIZE,   &r);
63    eglGetConfigAttrib(dpy, config, EGL_GREEN_SIZE, &g);
64    eglGetConfigAttrib(dpy, config, EGL_BLUE_SIZE,  &b);
65    eglGetConfigAttrib(dpy, config, EGL_ALPHA_SIZE, &a);
66
67    surface = eglCreateWindowSurface(dpy, config, window, NULL);
68    if (surface == EGL_NO_SURFACE) {
69        EGLint err = eglGetError();
70        fprintf(stderr, "%s, config=%p, format = %d-%d-%d-%d\n",
71                EGLUtils::strerror(err), config, r,g,b,a);
72        return 0;
73    } else {
74        printf("config=%p, format = %d-%d-%d-%d\n", config, r,g,b,a);
75    }
76
77    context = eglCreateContext(dpy, config, NULL, NULL);
78    eglMakeCurrent(dpy, surface, surface, context);
79    eglQuerySurface(dpy, surface, EGL_WIDTH, &w);
80    eglQuerySurface(dpy, surface, EGL_HEIGHT, &h);
81
82    printf("w=%d, h=%d\n", w, h);
83
84    glDisable(GL_DITHER);
85    glEnable(GL_BLEND);
86
87    glViewport(0, 0, w, h);
88    glOrthof(0, w, 0, h, 0, 1);
89
90    eglSwapInterval(dpy, 1);
91
92    glClearColor(1,0,0,0);
93    glClear(GL_COLOR_BUFFER_BIT);
94    eglSwapBuffers(dpy, surface);
95
96
97    int time = 10;
98    printf("screen should flash red/green quickly for %d s...\n", time);
99
100    int c = 0;
101    nsecs_t start = systemTime();
102    nsecs_t t;
103    do {
104        glClearColor(1,0,0,0);
105        glClear(GL_COLOR_BUFFER_BIT);
106        eglSwapBuffers(dpy, surface);
107        glClearColor(0,1,0,0);
108        glClear(GL_COLOR_BUFFER_BIT);
109        eglSwapBuffers(dpy, surface);
110        t = systemTime() - start;
111        c += 2;
112    } while (int(ns2s(t))<=time);
113
114    double p =  (double(t) / c) / 1000000000.0;
115    printf("refresh-rate is %f fps (%f ms)\n", 1.0f/p, p*1000.0);
116
117    eglTerminate(dpy);
118
119    return 0;
120}
121