gl2_basic.cpp revision 13e68424fb7d68b4b9a9dd443eb0e87721312834
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 <GLES/gl.h>
25#include <GLES/glext.h>
26
27#include <utils/Timers.h>
28
29#include <ui/FramebufferNativeWindow.h>
30#include <ui/EGLUtils.h>
31
32using namespace android;
33
34static void printGLString(const char *name, GLenum s)
35{
36     fprintf(stderr, "printGLString %s, %d\n", name, s);
37     const char *v = (const char *)glGetString(s);
38     int error = glGetError();
39     fprintf(stderr, "glGetError() = %d, result of glGetString = %x\n", error,
40         (unsigned int)v);
41     if ((v < (const char*) 0) || (v > (const char*) 0x1000))
42         fprintf(stderr, "GL %s = %s\n", name, v);
43     else
44         fprintf(stderr, "GL %s = (null)\n", name);
45}
46
47static const char* eglErrorToString[] = {
48    "EGL_SUCCESS",      // 0x3000 12288
49    "EGL_NOT_INITIALIZED",
50    "EGL_BAD_ACCESS",   // 0x3002 12290
51    "EGL_BAD_ALLOC",
52    "EGL_BAD_ATTRIBUTE",
53    "EGL_BAD_CONFIG",
54    "EGL_BAD_CONTEXT",  // 0x3006 12294
55    "EGL_BAD_CURRENT_SURFACE",
56    "EGL_BAD_DISPLAY",
57    "EGL_BAD_MATCH",
58    "EGL_BAD_NATIVE_PIXMAP",
59    "EGL_BAD_NATIVE_WINDOW",
60    "EGL_BAD_PARAMETER",  // 0x300c 12300
61    "EGL_BAD_SURFACE"
62};
63
64static void checkEglError(const char* op) {
65    for(EGLint error = eglGetError();
66		error != EGL_SUCCESS;
67	error = eglGetError()) {
68        const char* errorString = "unknown";
69        if (error >= EGL_SUCCESS && error <= EGL_BAD_SURFACE) {
70            errorString = eglErrorToString[error - EGL_SUCCESS];
71        }
72        fprintf(stderr, "%s() returned eglError %s (0x%x)\n", op,
73            errorString, error);
74    }
75}
76
77int main(int argc, char** argv)
78{
79    EGLint s_configAttribs[] = {
80         EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
81         EGL_RED_SIZE,       5,
82         EGL_GREEN_SIZE,     6,
83         EGL_BLUE_SIZE,      5,
84         EGL_NONE
85     };
86
87     EGLint numConfigs = -1;
88     EGLint majorVersion;
89     EGLint minorVersion;
90     EGLConfig config;
91     EGLContext context;
92     EGLSurface surface;
93     EGLint w, h;
94
95     EGLDisplay dpy;
96
97     EGLNativeWindowType window = 0;
98     window = android_createDisplaySurface();
99
100     checkEglError("<init>");
101     dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
102     checkEglError("eglGetDisplay");
103     eglInitialize(dpy, &majorVersion, &minorVersion);
104     checkEglError("eglInitialize");
105     fprintf(stderr, "EGL version %d.%d\n", majorVersion, minorVersion);
106     EGLUtils::selectConfigForNativeWindow(dpy, s_configAttribs, window, &config);
107     fprintf(stderr, "Chosen config: 0x%08x\n", (unsigned long) config);
108
109     checkEglError("EGLUtils::selectConfigForNativeWindow");
110     surface = eglCreateWindowSurface(dpy, config, window, NULL);
111     checkEglError("eglCreateWindowSurface");
112
113     EGLint gl2_0Attribs[] = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE};
114
115     context = eglCreateContext(dpy, config, NULL, gl2_0Attribs);
116     checkEglError("eglCreateContext");
117     eglMakeCurrent(dpy, surface, surface, context);
118     checkEglError("eglMakeCurrent");
119     eglQuerySurface(dpy, surface, EGL_WIDTH, &w);
120     checkEglError("eglQuerySurface");
121     eglQuerySurface(dpy, surface, EGL_HEIGHT, &h);
122     checkEglError("eglQuerySurface");
123     GLint dim = w<h ? w : h;
124
125     fprintf(stderr, "Window dimensions: %d x %d\n", w, h);
126
127     printGLString("Version", GL_VERSION);
128     printGLString("Vendor", GL_VENDOR);
129     printGLString("Renderer", GL_RENDERER);
130     printGLString("Extensions", GL_EXTENSIONS);
131
132     return 0;
133}
134