RenderEngine.cpp revision 875d8e1323536e16dcfc90c9674d7ad32116a69a
1875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian/* 2875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian * Copyright 2013 The Android Open Source Project 3875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian * 4875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian * Licensed under the Apache License, Version 2.0 (the "License"); 5875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian * you may not use this file except in compliance with the License. 6875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian * You may obtain a copy of the License at 7875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian * 8875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian * http://www.apache.org/licenses/LICENSE-2.0 9875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian * 10875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian * Unless required by applicable law or agreed to in writing, software 11875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian * distributed under the License is distributed on an "AS IS" BASIS, 12875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian * See the License for the specific language governing permissions and 14875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian * limitations under the License. 15875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian */ 16875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian 17875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian#include <cutils/log.h> 18875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian 19875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian#include "RenderEngine.h" 20875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian#include "GLES10RenderEngine.h" 21875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian#include "GLES11RenderEngine.h" 22875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian#include "GLExtensions.h" 23875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian 24875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian// --------------------------------------------------------------------------- 25875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopiannamespace android { 26875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian// --------------------------------------------------------------------------- 27875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian 28875d8e1323536e16dcfc90c9674d7ad32116a69aMathias AgopianRenderEngine* RenderEngine::create(EGLDisplay display, EGLConfig config) { 29875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian // Also create our EGLContext 30875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian EGLint contextAttributes[] = { 31875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian// EGL_CONTEXT_CLIENT_VERSION, 2, 32875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian#ifdef EGL_IMG_context_priority 33875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian#ifdef HAS_CONTEXT_PRIORITY 34875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian#warning "using EGL_IMG_context_priority" 35875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian EGL_CONTEXT_PRIORITY_LEVEL_IMG, EGL_CONTEXT_PRIORITY_HIGH_IMG, 36875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian#endif 37875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian#endif 38875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian EGL_NONE, EGL_NONE 39875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian }; 40875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian 41875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian EGLContext ctxt = eglCreateContext(display, config, NULL, contextAttributes); 42875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian if (ctxt == EGL_NO_CONTEXT) { 43875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian // maybe ES 2.x is not supported 44875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian ALOGW("can't create an ES 2.x context, trying 1.x"); 45875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian ctxt = eglCreateContext(display, config, NULL, contextAttributes + 2); 46875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian } 47875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian 48875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian // if can't create a GL context, we can only abort. 49875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian LOG_ALWAYS_FATAL_IF(ctxt==EGL_NO_CONTEXT, "EGLContext creation failed"); 50875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian 51875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian 52875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian // now figure out what version of GL did we actually get 53875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian // NOTE: a dummy surface is not needed if KHR_create_context is supported 54875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian 55875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian EGLint attribs[] = { EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE, EGL_NONE }; 56875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian EGLSurface dummy = eglCreatePbufferSurface(display, config, attribs); 57875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian LOG_ALWAYS_FATAL_IF(dummy==EGL_NO_SURFACE, "can't create dummy pbuffer"); 58875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian EGLBoolean success = eglMakeCurrent(display, dummy, dummy, ctxt); 59875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian LOG_ALWAYS_FATAL_IF(!success, "can't make dummy pbuffer current"); 60875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian 61875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian GLExtensions& extensions(GLExtensions::getInstance()); 62875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian extensions.initWithGLStrings( 63875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian glGetString(GL_VENDOR), 64875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian glGetString(GL_RENDERER), 65875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian glGetString(GL_VERSION), 66875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian glGetString(GL_EXTENSIONS)); 67875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian 68875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian GlesVersion version = parseGlesVersion( extensions.getVersion() ); 69875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian 70875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian // initialize the renderer while GL is current 71875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian 72875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian RenderEngine* engine = NULL; 73875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian switch (version) { 74875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian case GLES_VERSION_1_0: 75875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian engine = new GLES10RenderEngine(); 76875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian break; 77875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian case GLES_VERSION_1_1: 78875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian engine = new GLES11RenderEngine(); 79875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian break; 80875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian case GLES_VERSION_2_0: 81875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian case GLES_VERSION_3_0: 82875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian //engine = new GLES20RenderEngine(); 83875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian break; 84875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian } 85875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian engine->setEGLContext(ctxt); 86875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian 87875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian ALOGI("OpenGL ES informations:"); 88875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian ALOGI("vendor : %s", extensions.getVendor()); 89875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian ALOGI("renderer : %s", extensions.getRenderer()); 90875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian ALOGI("version : %s", extensions.getVersion()); 91875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian ALOGI("extensions: %s", extensions.getExtension()); 92875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian ALOGI("GL_MAX_TEXTURE_SIZE = %d", engine->getMaxTextureSize()); 93875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian ALOGI("GL_MAX_VIEWPORT_DIMS = %d", engine->getMaxViewportDims()); 94875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian 95875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); 96875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian eglDestroySurface(display, dummy); 97875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian 98875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian return engine; 99875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian} 100875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian 101875d8e1323536e16dcfc90c9674d7ad32116a69aMathias AgopianRenderEngine::RenderEngine() : mEGLContext(EGL_NO_CONTEXT) { 102875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian} 103875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian 104875d8e1323536e16dcfc90c9674d7ad32116a69aMathias AgopianRenderEngine::~RenderEngine() { 105875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian} 106875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian 107875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopianvoid RenderEngine::setEGLContext(EGLContext ctxt) { 108875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian mEGLContext = ctxt; 109875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian} 110875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian 111875d8e1323536e16dcfc90c9674d7ad32116a69aMathias AgopianEGLContext RenderEngine::getEGLContext() const { 112875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian return mEGLContext; 113875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian} 114875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian 115875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopianvoid RenderEngine::checkErrors() const { 116875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian do { 117875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian // there could be more than one error flag 118875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian GLenum error = glGetError(); 119875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian if (error == GL_NO_ERROR) 120875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian break; 121875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian ALOGE("GL error 0x%04x", int(error)); 122875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian } while (true); 123875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian} 124875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian 125875d8e1323536e16dcfc90c9674d7ad32116a69aMathias AgopianRenderEngine::GlesVersion RenderEngine::parseGlesVersion(const char* str) { 126875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian int major, minor; 127875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian if (sscanf(str, "OpenGL ES-CM %d.%d", &major, &minor) != 2) { 128875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian if (sscanf(str, "OpenGL ES %d.%d", &major, &minor) != 2) { 129875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian ALOGW("Unable to parse GL_VERSION string: \"%s\"", str); 130875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian return GLES_VERSION_1_0; 131875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian } 132875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian } 133875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian 134875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian if (major == 1 && minor == 0) return GLES_VERSION_1_0; 135875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian if (major == 1 && minor >= 1) return GLES_VERSION_1_1; 136875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian if (major == 2 && minor >= 0) return GLES_VERSION_2_0; 137875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian if (major == 3 && minor >= 0) return GLES_VERSION_3_0; 138875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian 139875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian ALOGW("Unrecognized OpenGL ES version: %d.%d", major, minor); 140875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian return GLES_VERSION_1_0; 141875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian} 142875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian 143875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian// --------------------------------------------------------------------------- 144875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian}; // namespace android 145875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian// --------------------------------------------------------------------------- 146