Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 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 <cutils/log.h> |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 18 | #include <ui/Rect.h> |
| 19 | #include <ui/Region.h> |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 20 | |
| 21 | #include "RenderEngine.h" |
| 22 | #include "GLES10RenderEngine.h" |
| 23 | #include "GLES11RenderEngine.h" |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 24 | #include "GLES20RenderEngine.h" |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 25 | #include "GLExtensions.h" |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 26 | #include "Mesh.h" |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 27 | |
| 28 | // --------------------------------------------------------------------------- |
| 29 | namespace android { |
| 30 | // --------------------------------------------------------------------------- |
| 31 | |
| 32 | RenderEngine* RenderEngine::create(EGLDisplay display, EGLConfig config) { |
| 33 | // Also create our EGLContext |
| 34 | EGLint contextAttributes[] = { |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 35 | EGL_CONTEXT_CLIENT_VERSION, 2, // MUST be first |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 36 | #ifdef EGL_IMG_context_priority |
| 37 | #ifdef HAS_CONTEXT_PRIORITY |
| 38 | #warning "using EGL_IMG_context_priority" |
| 39 | EGL_CONTEXT_PRIORITY_LEVEL_IMG, EGL_CONTEXT_PRIORITY_HIGH_IMG, |
| 40 | #endif |
| 41 | #endif |
| 42 | EGL_NONE, EGL_NONE |
| 43 | }; |
| 44 | |
Mathias Agopian | 458197d | 2013-08-15 14:56:51 -0700 | [diff] [blame] | 45 | EGLContext ctxt = eglCreateContext(display, config, NULL, contextAttributes); |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 46 | if (ctxt == EGL_NO_CONTEXT) { |
| 47 | // maybe ES 2.x is not supported |
| 48 | ALOGW("can't create an ES 2.x context, trying 1.x"); |
| 49 | ctxt = eglCreateContext(display, config, NULL, contextAttributes + 2); |
| 50 | } |
| 51 | |
| 52 | // if can't create a GL context, we can only abort. |
| 53 | LOG_ALWAYS_FATAL_IF(ctxt==EGL_NO_CONTEXT, "EGLContext creation failed"); |
| 54 | |
| 55 | |
| 56 | // now figure out what version of GL did we actually get |
| 57 | // NOTE: a dummy surface is not needed if KHR_create_context is supported |
| 58 | |
| 59 | EGLint attribs[] = { EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE, EGL_NONE }; |
| 60 | EGLSurface dummy = eglCreatePbufferSurface(display, config, attribs); |
| 61 | LOG_ALWAYS_FATAL_IF(dummy==EGL_NO_SURFACE, "can't create dummy pbuffer"); |
| 62 | EGLBoolean success = eglMakeCurrent(display, dummy, dummy, ctxt); |
| 63 | LOG_ALWAYS_FATAL_IF(!success, "can't make dummy pbuffer current"); |
| 64 | |
| 65 | GLExtensions& extensions(GLExtensions::getInstance()); |
| 66 | extensions.initWithGLStrings( |
| 67 | glGetString(GL_VENDOR), |
| 68 | glGetString(GL_RENDERER), |
| 69 | glGetString(GL_VERSION), |
| 70 | glGetString(GL_EXTENSIONS)); |
| 71 | |
| 72 | GlesVersion version = parseGlesVersion( extensions.getVersion() ); |
| 73 | |
| 74 | // initialize the renderer while GL is current |
| 75 | |
| 76 | RenderEngine* engine = NULL; |
| 77 | switch (version) { |
| 78 | case GLES_VERSION_1_0: |
| 79 | engine = new GLES10RenderEngine(); |
| 80 | break; |
| 81 | case GLES_VERSION_1_1: |
| 82 | engine = new GLES11RenderEngine(); |
| 83 | break; |
| 84 | case GLES_VERSION_2_0: |
| 85 | case GLES_VERSION_3_0: |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 86 | engine = new GLES20RenderEngine(); |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 87 | break; |
| 88 | } |
| 89 | engine->setEGLContext(ctxt); |
| 90 | |
| 91 | ALOGI("OpenGL ES informations:"); |
| 92 | ALOGI("vendor : %s", extensions.getVendor()); |
| 93 | ALOGI("renderer : %s", extensions.getRenderer()); |
| 94 | ALOGI("version : %s", extensions.getVersion()); |
| 95 | ALOGI("extensions: %s", extensions.getExtension()); |
| 96 | ALOGI("GL_MAX_TEXTURE_SIZE = %d", engine->getMaxTextureSize()); |
| 97 | ALOGI("GL_MAX_VIEWPORT_DIMS = %d", engine->getMaxViewportDims()); |
| 98 | |
| 99 | eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); |
| 100 | eglDestroySurface(display, dummy); |
| 101 | |
| 102 | return engine; |
| 103 | } |
| 104 | |
| 105 | RenderEngine::RenderEngine() : mEGLContext(EGL_NO_CONTEXT) { |
| 106 | } |
| 107 | |
| 108 | RenderEngine::~RenderEngine() { |
| 109 | } |
| 110 | |
| 111 | void RenderEngine::setEGLContext(EGLContext ctxt) { |
| 112 | mEGLContext = ctxt; |
| 113 | } |
| 114 | |
| 115 | EGLContext RenderEngine::getEGLContext() const { |
| 116 | return mEGLContext; |
| 117 | } |
| 118 | |
| 119 | void RenderEngine::checkErrors() const { |
| 120 | do { |
| 121 | // there could be more than one error flag |
| 122 | GLenum error = glGetError(); |
| 123 | if (error == GL_NO_ERROR) |
| 124 | break; |
| 125 | ALOGE("GL error 0x%04x", int(error)); |
| 126 | } while (true); |
| 127 | } |
| 128 | |
| 129 | RenderEngine::GlesVersion RenderEngine::parseGlesVersion(const char* str) { |
| 130 | int major, minor; |
| 131 | if (sscanf(str, "OpenGL ES-CM %d.%d", &major, &minor) != 2) { |
| 132 | if (sscanf(str, "OpenGL ES %d.%d", &major, &minor) != 2) { |
| 133 | ALOGW("Unable to parse GL_VERSION string: \"%s\"", str); |
| 134 | return GLES_VERSION_1_0; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | if (major == 1 && minor == 0) return GLES_VERSION_1_0; |
| 139 | if (major == 1 && minor >= 1) return GLES_VERSION_1_1; |
| 140 | if (major == 2 && minor >= 0) return GLES_VERSION_2_0; |
| 141 | if (major == 3 && minor >= 0) return GLES_VERSION_3_0; |
| 142 | |
| 143 | ALOGW("Unrecognized OpenGL ES version: %d.%d", major, minor); |
| 144 | return GLES_VERSION_1_0; |
| 145 | } |
| 146 | |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 147 | void RenderEngine::fillRegionWithColor(const Region& region, uint32_t height, |
| 148 | float red, float green, float blue, float alpha) { |
| 149 | size_t c; |
| 150 | Rect const* r = region.getArray(&c); |
| 151 | Mesh mesh(Mesh::TRIANGLES, c*6, 2); |
Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame^] | 152 | Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>()); |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 153 | for (size_t i=0 ; i<c ; i++, r++) { |
Mathias Agopian | 5cdc899 | 2013-08-13 20:51:23 -0700 | [diff] [blame] | 154 | position[i*6 + 0].x = r->left; |
| 155 | position[i*6 + 0].y = height - r->top; |
| 156 | position[i*6 + 1].x = r->left; |
| 157 | position[i*6 + 1].y = height - r->bottom; |
| 158 | position[i*6 + 2].x = r->right; |
| 159 | position[i*6 + 2].y = height - r->bottom; |
| 160 | position[i*6 + 3].x = r->left; |
| 161 | position[i*6 + 3].y = height - r->top; |
| 162 | position[i*6 + 4].x = r->right; |
| 163 | position[i*6 + 4].y = height - r->bottom; |
| 164 | position[i*6 + 5].x = r->right; |
| 165 | position[i*6 + 5].y = height - r->top; |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 166 | } |
Mathias Agopian | 19733a3 | 2013-08-28 18:13:56 -0700 | [diff] [blame] | 167 | setupFillWithColor(red, green, blue, alpha); |
| 168 | drawMesh(mesh); |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | void RenderEngine::clearWithColor(float red, float green, float blue, float alpha) { |
| 172 | glClearColor(red, green, blue, alpha); |
| 173 | glClear(GL_COLOR_BUFFER_BIT); |
| 174 | } |
| 175 | |
| 176 | void RenderEngine::setScissor( |
| 177 | uint32_t left, uint32_t bottom, uint32_t right, uint32_t top) { |
| 178 | glScissor(left, bottom, right, top); |
| 179 | glEnable(GL_SCISSOR_TEST); |
| 180 | } |
| 181 | |
| 182 | void RenderEngine::disableScissor() { |
| 183 | glDisable(GL_SCISSOR_TEST); |
| 184 | } |
| 185 | |
| 186 | void RenderEngine::genTextures(size_t count, uint32_t* names) { |
| 187 | glGenTextures(count, names); |
| 188 | } |
| 189 | |
| 190 | void RenderEngine::deleteTextures(size_t count, uint32_t const* names) { |
| 191 | glDeleteTextures(count, names); |
| 192 | } |
| 193 | |
Mathias Agopian | 458197d | 2013-08-15 14:56:51 -0700 | [diff] [blame] | 194 | void RenderEngine::dump(String8& result) { |
| 195 | const GLExtensions& extensions(GLExtensions::getInstance()); |
| 196 | result.appendFormat("GLES: %s, %s, %s\n", |
| 197 | extensions.getVendor(), |
| 198 | extensions.getRenderer(), |
| 199 | extensions.getVersion()); |
| 200 | result.appendFormat("%s\n", extensions.getExtension()); |
| 201 | } |
| 202 | |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 203 | // --------------------------------------------------------------------------- |
| 204 | |
| 205 | RenderEngine::BindImageAsFramebuffer::BindImageAsFramebuffer( |
| 206 | RenderEngine& engine, EGLImageKHR image) : mEngine(engine) |
| 207 | { |
Mathias Agopian | 458197d | 2013-08-15 14:56:51 -0700 | [diff] [blame] | 208 | mEngine.bindImageAsFramebuffer(image, &mTexName, &mFbName, &mStatus); |
| 209 | |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 210 | ALOGE_IF(mStatus != GL_FRAMEBUFFER_COMPLETE_OES, |
| 211 | "glCheckFramebufferStatusOES error %d", mStatus); |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | RenderEngine::BindImageAsFramebuffer::~BindImageAsFramebuffer() { |
| 215 | // back to main framebuffer |
Mathias Agopian | 458197d | 2013-08-15 14:56:51 -0700 | [diff] [blame] | 216 | mEngine.unbindFramebuffer(mTexName, mFbName); |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | status_t RenderEngine::BindImageAsFramebuffer::getStatus() const { |
| 220 | return mStatus == GL_FRAMEBUFFER_COMPLETE_OES ? NO_ERROR : BAD_VALUE; |
| 221 | } |
| 222 | |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 223 | // --------------------------------------------------------------------------- |
| 224 | }; // namespace android |
| 225 | // --------------------------------------------------------------------------- |