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