gl2_basic.cpp revision 058777592dc784034cc19f6d358fe5ca4225d81d
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#if 0 // causes hangs 38 const char *v = (const char *)glGetString(s); 39 int error = glGetError(); 40 fprintf(stderr, "glGetError() = %d, result of glGetString = %x\n", error, 41 (unsigned int)v); 42 if ((v < (const char*) 0) || (v > (const char*) 0x10000)) 43 fprintf(stderr, "GL %s = %s\n", name, v); 44 else 45 fprintf(stderr, "GL %s = (null) 0x%08x\n", name, (unsigned int) v); 46#endif 47} 48 49static const char* eglErrorToString[] = { 50 "EGL_SUCCESS", // 0x3000 12288 51 "EGL_NOT_INITIALIZED", 52 "EGL_BAD_ACCESS", // 0x3002 12290 53 "EGL_BAD_ALLOC", 54 "EGL_BAD_ATTRIBUTE", 55 "EGL_BAD_CONFIG", 56 "EGL_BAD_CONTEXT", // 0x3006 12294 57 "EGL_BAD_CURRENT_SURFACE", 58 "EGL_BAD_DISPLAY", 59 "EGL_BAD_MATCH", 60 "EGL_BAD_NATIVE_PIXMAP", 61 "EGL_BAD_NATIVE_WINDOW", 62 "EGL_BAD_PARAMETER", // 0x300c 12300 63 "EGL_BAD_SURFACE" 64}; 65 66static void checkEglError(const char* op, EGLBoolean returnVal = EGL_TRUE) { 67 if (returnVal != EGL_TRUE) { 68 fprintf(stderr, "%s() returned %d\n", op, returnVal); 69 } 70 71 for(EGLint error = eglGetError(); 72 error != EGL_SUCCESS; 73 error = eglGetError()) { 74 const char* errorString = "unknown"; 75 if (error >= EGL_SUCCESS && error <= EGL_BAD_SURFACE) { 76 errorString = eglErrorToString[error - EGL_SUCCESS]; 77 } 78 fprintf(stderr, "after %s() eglError %s (0x%x)\n", op, 79 errorString, error); 80 } 81} 82 83int main(int argc, char** argv) 84{ 85 EGLBoolean returnValue; 86 EGLConfig configs[2]; 87 EGLint config_count; 88 89 EGLint context_attribs[] = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE}; 90 EGLint s_configAttribs[] = { 91 EGL_BUFFER_SIZE, EGL_DONT_CARE, 92 EGL_RED_SIZE, 5, 93 EGL_GREEN_SIZE, 6, 94 EGL_BLUE_SIZE, 5, 95 EGL_DEPTH_SIZE, 8, 96 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, 97 EGL_NONE 98 }; 99 100 EGLint majorVersion; 101 EGLint minorVersion; 102 EGLContext context; 103 EGLSurface surface; 104 EGLint w, h; 105 106 EGLDisplay dpy; 107 108 EGLNativeWindowType window = 0; 109 window = android_createDisplaySurface(); 110 111 checkEglError("<init>"); 112 dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY); 113 checkEglError("eglGetDisplay"); 114 if (dpy == EGL_NO_DISPLAY) { 115 printf("eglGetDisplay returned EGL_NO_DISPLAY.\n"); 116 return 0; 117 } 118 returnValue = eglInitialize(dpy, &majorVersion, &minorVersion); 119 checkEglError("eglInitialize", returnValue); 120 fprintf(stderr, "EGL version %d.%d\n", majorVersion, minorVersion); 121 122 returnValue = eglGetConfigs (dpy, configs, 2, &config_count); 123 checkEglError("eglGetConfigs", returnValue); 124 fprintf(stderr, "Config count: %d\n", config_count); 125 for(int i = 0; i < config_count; i++) { 126 fprintf(stderr, "%d: 0x%08x\n", i, (unsigned int) configs[i]); 127 } 128#if 0 129 EGLConfig config; 130 EGLUtils::selectConfigForNativeWindow(dpy, s_configAttribs, window, &config); 131 checkEglError("EGLUtils::selectConfigForNativeWindow"); 132#else 133 int chooseConfigResult = eglChooseConfig(dpy, s_configAttribs, configs, 2, &config_count); 134 checkEglError("eglChooseConfig", chooseConfigResult); 135 if (chooseConfigResult != EGL_TRUE ) 136 { 137 printf("eglChooseConfig failed\n"); 138 return 0; 139 } 140#endif 141 142 surface = eglCreateWindowSurface(dpy, configs[0], window, NULL); 143 checkEglError("eglCreateWindowSurface"); 144 if (surface == EGL_NO_SURFACE) 145 { 146 printf("gelCreateWindowSurface failed.\n"); 147 return 0; 148 } 149 EGLint gl2_0Attribs[] = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE}; 150 151 context = eglCreateContext(dpy, configs[0], EGL_NO_CONTEXT, context_attribs); 152 checkEglError("eglCreateContext"); 153 if (context == EGL_NO_CONTEXT) 154 { 155 printf("eglCreateContext failed\n"); 156 return 0; 157 } 158 eglMakeCurrent(dpy, surface, surface, context); 159 checkEglError("eglMakeCurrent"); 160 eglQuerySurface(dpy, surface, EGL_WIDTH, &w); 161 checkEglError("eglQuerySurface"); 162 eglQuerySurface(dpy, surface, EGL_HEIGHT, &h); 163 checkEglError("eglQuerySurface"); 164 GLint dim = w<h ? w : h; 165 166 fprintf(stderr, "Window dimensions: %d x %d\n", w, h); 167 168 printGLString("Version", GL_VERSION); 169 printGLString("Vendor", GL_VENDOR); 170 printGLString("Renderer", GL_RENDERER); 171 printGLString("Extensions", GL_EXTENSIONS); 172 173 return 0; 174} 175