1/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <stdlib.h>
18#include <stdio.h>
19#include <time.h>
20#include <sched.h>
21#include <sys/resource.h>
22
23#include <EGL/egl.h>
24#include <GLES2/gl2.h>
25#include <GLES2/gl2ext.h>
26
27#include <utils/Timers.h>
28
29#include <WindowSurface.h>
30#include <EGLUtils.h>
31
32using namespace android;
33
34
35static void checkEglError(const char* op, EGLBoolean returnVal = EGL_TRUE) {
36    if (returnVal != EGL_TRUE) {
37        fprintf(stderr, "%s() returned %d\n", op, returnVal);
38    }
39
40    for (EGLint error = eglGetError(); error != EGL_SUCCESS; error
41            = eglGetError()) {
42        fprintf(stderr, "after %s() eglError %s (0x%x)\n", op, EGLUtils::strerror(error),
43                error);
44    }
45}
46
47static void checkGlError(const char* op) {
48    for (GLint error = glGetError(); error; error
49            = glGetError()) {
50        fprintf(stderr, "after %s() glError (0x%x)\n", op, error);
51    }
52}
53
54bool doTest(uint32_t w, uint32_t h);
55
56static EGLDisplay dpy;
57static EGLSurface surface;
58
59int main(int argc, char** argv) {
60    EGLBoolean returnValue;
61    EGLConfig myConfig = {0};
62
63    EGLint context_attribs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE };
64    EGLint s_configAttribs[] = {
65            EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
66            EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
67            EGL_NONE };
68    EGLint majorVersion;
69    EGLint minorVersion;
70    EGLContext context;
71    EGLint w, h;
72
73
74    checkEglError("<init>");
75    dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
76    checkEglError("eglGetDisplay");
77    if (dpy == EGL_NO_DISPLAY) {
78        printf("eglGetDisplay returned EGL_NO_DISPLAY.\n");
79        return 0;
80    }
81
82    returnValue = eglInitialize(dpy, &majorVersion, &minorVersion);
83    checkEglError("eglInitialize", returnValue);
84    if (returnValue != EGL_TRUE) {
85        printf("eglInitialize failed\n");
86        return 0;
87    }
88
89    WindowSurface windowSurface;
90    EGLNativeWindowType window = windowSurface.getSurface();
91    returnValue = EGLUtils::selectConfigForNativeWindow(dpy, s_configAttribs, window, &myConfig);
92    if (returnValue) {
93        printf("EGLUtils::selectConfigForNativeWindow() returned %d", returnValue);
94        return 0;
95    }
96
97    checkEglError("EGLUtils::selectConfigForNativeWindow");
98
99    surface = eglCreateWindowSurface(dpy, myConfig, window, NULL);
100    checkEglError("eglCreateWindowSurface");
101    if (surface == EGL_NO_SURFACE) {
102        printf("gelCreateWindowSurface failed.\n");
103        return 0;
104    }
105
106    context = eglCreateContext(dpy, myConfig, EGL_NO_CONTEXT, context_attribs);
107    checkEglError("eglCreateContext");
108    if (context == EGL_NO_CONTEXT) {
109        printf("eglCreateContext failed\n");
110        return 0;
111    }
112    returnValue = eglMakeCurrent(dpy, surface, surface, context);
113    checkEglError("eglMakeCurrent", returnValue);
114    if (returnValue != EGL_TRUE) {
115        return 0;
116    }
117    eglQuerySurface(dpy, surface, EGL_WIDTH, &w);
118    checkEglError("eglQuerySurface");
119    eglQuerySurface(dpy, surface, EGL_HEIGHT, &h);
120    checkEglError("eglQuerySurface");
121    GLint dim = w < h ? w : h;
122
123    glViewport(0, 0, w, h);
124
125    for (;;) {
126        doTest(w, h);
127        eglSwapBuffers(dpy, surface);
128        checkEglError("eglSwapBuffers");
129    }
130
131    return 0;
132}
133
134void ptSwap() {
135    eglSwapBuffers(dpy, surface);
136}
137
138