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 <ui/FramebufferNativeWindow.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    EGLNativeWindowType window = android_createDisplaySurface();
90    returnValue = EGLUtils::selectConfigForNativeWindow(dpy, s_configAttribs, window, &myConfig);
91    if (returnValue) {
92        printf("EGLUtils::selectConfigForNativeWindow() returned %d", returnValue);
93        return 0;
94    }
95
96    checkEglError("EGLUtils::selectConfigForNativeWindow");
97
98    surface = eglCreateWindowSurface(dpy, myConfig, window, NULL);
99    checkEglError("eglCreateWindowSurface");
100    if (surface == EGL_NO_SURFACE) {
101        printf("gelCreateWindowSurface failed.\n");
102        return 0;
103    }
104
105    context = eglCreateContext(dpy, myConfig, EGL_NO_CONTEXT, context_attribs);
106    checkEglError("eglCreateContext");
107    if (context == EGL_NO_CONTEXT) {
108        printf("eglCreateContext failed\n");
109        return 0;
110    }
111    returnValue = eglMakeCurrent(dpy, surface, surface, context);
112    checkEglError("eglMakeCurrent", returnValue);
113    if (returnValue != EGL_TRUE) {
114        return 0;
115    }
116    eglQuerySurface(dpy, surface, EGL_WIDTH, &w);
117    checkEglError("eglQuerySurface");
118    eglQuerySurface(dpy, surface, EGL_HEIGHT, &h);
119    checkEglError("eglQuerySurface");
120    GLint dim = w < h ? w : h;
121
122    glViewport(0, 0, w, h);
123
124    for (;;) {
125        doTest(w, h);
126        eglSwapBuffers(dpy, surface);
127        checkEglError("eglSwapBuffers");
128    }
129
130    return 0;
131}
132
133void ptSwap() {
134    eglSwapBuffers(dpy, surface);
135}
136
137