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 
32 using namespace android;
33 
34 
checkEglError(const char * op,EGLBoolean returnVal=EGL_TRUE)35 static 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 
47 bool doTest(uint32_t w, uint32_t h);
48 
49 static EGLDisplay dpy;
50 static EGLSurface surface;
51 
main(int,char **)52 int main(int /*argc*/, char** /*argv*/) {
53     EGLBoolean returnValue;
54     EGLConfig myConfig = {0};
55 
56     EGLint context_attribs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE };
57     EGLint s_configAttribs[] = {
58             EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
59             EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
60             EGL_NONE };
61     EGLint majorVersion;
62     EGLint minorVersion;
63     EGLContext context;
64     EGLint w, h;
65 
66 
67     checkEglError("<init>");
68     dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
69     checkEglError("eglGetDisplay");
70     if (dpy == EGL_NO_DISPLAY) {
71         printf("eglGetDisplay returned EGL_NO_DISPLAY.\n");
72         return 0;
73     }
74 
75     returnValue = eglInitialize(dpy, &majorVersion, &minorVersion);
76     checkEglError("eglInitialize", returnValue);
77     if (returnValue != EGL_TRUE) {
78         printf("eglInitialize failed\n");
79         return 0;
80     }
81 
82     WindowSurface windowSurface;
83     EGLNativeWindowType window = windowSurface.getSurface();
84     returnValue = EGLUtils::selectConfigForNativeWindow(dpy, s_configAttribs, window, &myConfig);
85     if (returnValue) {
86         printf("EGLUtils::selectConfigForNativeWindow() returned %d", returnValue);
87         return 0;
88     }
89 
90     checkEglError("EGLUtils::selectConfigForNativeWindow");
91 
92     surface = eglCreateWindowSurface(dpy, myConfig, window, NULL);
93     checkEglError("eglCreateWindowSurface");
94     if (surface == EGL_NO_SURFACE) {
95         printf("gelCreateWindowSurface failed.\n");
96         return 0;
97     }
98 
99     context = eglCreateContext(dpy, myConfig, EGL_NO_CONTEXT, context_attribs);
100     checkEglError("eglCreateContext");
101     if (context == EGL_NO_CONTEXT) {
102         printf("eglCreateContext failed\n");
103         return 0;
104     }
105     returnValue = eglMakeCurrent(dpy, surface, surface, context);
106     checkEglError("eglMakeCurrent", returnValue);
107     if (returnValue != EGL_TRUE) {
108         return 0;
109     }
110     eglQuerySurface(dpy, surface, EGL_WIDTH, &w);
111     checkEglError("eglQuerySurface");
112     eglQuerySurface(dpy, surface, EGL_HEIGHT, &h);
113     checkEglError("eglQuerySurface");
114 
115     glViewport(0, 0, w, h);
116 
117     for (;;) {
118         doTest(w, h);
119         eglSwapBuffers(dpy, surface);
120         checkEglError("eglSwapBuffers");
121     }
122 
123     return 0;
124 }
125 
ptSwap()126 void ptSwap() {
127     eglSwapBuffers(dpy, surface);
128 }
129 
130