swapinterval.cpp revision 1c3561e8d862d8fd27f8e843a18f251b9d9500b4
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
30using namespace android;
31
32int main(int argc, char** argv)
33{
34    EGLint configAttribs[] = {
35         EGL_DEPTH_SIZE, 0,
36         EGL_NONE
37     };
38
39     EGLint numConfigs = -1, n=0;
40     EGLint majorVersion;
41     EGLint minorVersion;
42     EGLConfig config;
43     EGLContext context;
44     EGLSurface surface;
45     EGLint w, h;
46
47     EGLDisplay dpy;
48
49     dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
50     eglInitialize(dpy, &majorVersion, &minorVersion);
51
52     // Get all the "potential match" configs...
53     eglGetConfigs(dpy, NULL, 0, &numConfigs);
54     EGLConfig* const configs = (EGLConfig*)malloc(sizeof(EGLConfig)*numConfigs);
55     eglChooseConfig(dpy, configAttribs, configs, numConfigs, &n);
56     config = configs[0];
57     if (n > 1) {
58         // if there is more than one candidate, go through the list
59         // and pick one that matches our framebuffer format
60         int fbSzA = 0; // should not hardcode
61         int fbSzR = 5; // should not hardcode
62         int fbSzG = 6; // should not hardcode
63         int fbSzB = 5; // should not hardcode
64         int i;
65         for (i=0 ; i<n ; i++) {
66             EGLint r,g,b,a;
67             eglGetConfigAttrib(dpy, configs[i], EGL_RED_SIZE,   &r);
68             eglGetConfigAttrib(dpy, configs[i], EGL_GREEN_SIZE, &g);
69             eglGetConfigAttrib(dpy, configs[i], EGL_BLUE_SIZE,  &b);
70             eglGetConfigAttrib(dpy, configs[i], EGL_ALPHA_SIZE, &a);
71             if (fbSzA == a && fbSzR == r && fbSzG == g && fbSzB  == b) {
72                 config = configs[i];
73                 break;
74             }
75         }
76     }
77     free(configs);
78
79     surface = eglCreateWindowSurface(dpy, config,
80             android_createDisplaySurface(), NULL);
81     context = eglCreateContext(dpy, config, NULL, NULL);
82     eglMakeCurrent(dpy, surface, surface, context);
83     eglQuerySurface(dpy, surface, EGL_WIDTH, &w);
84     eglQuerySurface(dpy, surface, EGL_HEIGHT, &h);
85
86     printf("w=%d, h=%d\n", w, h);
87
88     glDisable(GL_DITHER);
89     glEnable(GL_BLEND);
90
91     glViewport(0, 0, w, h);
92     glOrthof(0, w, 0, h, 0, 1);
93
94     eglSwapInterval(dpy, 1);
95
96     glClearColor(1,0,0,0);
97     glClear(GL_COLOR_BUFFER_BIT);
98     eglSwapBuffers(dpy, surface);
99
100
101     int time = 10;
102     printf("screen should flash red/green quickly for %d s...\n", time);
103
104     int c = 0;
105     nsecs_t start = systemTime();
106     nsecs_t t;
107     do {
108         glClearColor(1,0,0,0);
109         glClear(GL_COLOR_BUFFER_BIT);
110         eglSwapBuffers(dpy, surface);
111         glClearColor(0,1,0,0);
112         glClear(GL_COLOR_BUFFER_BIT);
113         eglSwapBuffers(dpy, surface);
114         t = systemTime() - start;
115         c += 2;
116     } while (int(ns2s(t))<=time);
117
118     double p =  (double(t) / c) / 1000000000.0;
119     printf("refresh-rate is %f fps (%f ms)\n", 1.0f/p, p*1000.0);
120
121     eglTerminate(dpy);
122
123     return 0;
124}
125