Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -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 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 18 | |
| 19 | #include <GLES2/gl2.h> |
Mathias Agopian | 458197d | 2013-08-15 14:56:51 -0700 | [diff] [blame^] | 20 | #include <GLES2/gl2ext.h> |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 21 | |
| 22 | #include <utils/String8.h> |
| 23 | #include <utils/Trace.h> |
| 24 | |
| 25 | #include <cutils/compiler.h> |
| 26 | |
| 27 | #include "GLES20RenderEngine.h" |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 28 | #include "Program.h" |
| 29 | #include "ProgramCache.h" |
| 30 | #include "Description.h" |
| 31 | #include "Mesh.h" |
| 32 | |
| 33 | // --------------------------------------------------------------------------- |
| 34 | namespace android { |
| 35 | // --------------------------------------------------------------------------- |
| 36 | |
| 37 | GLES20RenderEngine::GLES20RenderEngine() { |
| 38 | |
| 39 | glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize); |
| 40 | glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims); |
| 41 | |
| 42 | glPixelStorei(GL_UNPACK_ALIGNMENT, 4); |
| 43 | glPixelStorei(GL_PACK_ALIGNMENT, 4); |
| 44 | |
| 45 | struct pack565 { |
| 46 | inline uint16_t operator() (int r, int g, int b) const { |
| 47 | return (r<<11)|(g<<5)|b; |
| 48 | } |
| 49 | } pack565; |
| 50 | |
| 51 | const uint16_t protTexData[] = { pack565(0x03, 0x03, 0x03) }; |
| 52 | glGenTextures(1, &mProtectedTexName); |
| 53 | glBindTexture(GL_TEXTURE_2D, mProtectedTexName); |
| 54 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 55 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 56 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); |
| 57 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); |
| 58 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0, |
| 59 | GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData); |
| 60 | } |
| 61 | |
| 62 | GLES20RenderEngine::~GLES20RenderEngine() { |
| 63 | } |
| 64 | |
| 65 | |
| 66 | size_t GLES20RenderEngine::getMaxTextureSize() const { |
| 67 | return mMaxTextureSize; |
| 68 | } |
| 69 | |
| 70 | size_t GLES20RenderEngine::getMaxViewportDims() const { |
| 71 | return |
| 72 | mMaxViewportDims[0] < mMaxViewportDims[1] ? |
| 73 | mMaxViewportDims[0] : mMaxViewportDims[1]; |
| 74 | } |
| 75 | |
| 76 | void GLES20RenderEngine::setViewportAndProjection( |
| 77 | size_t vpw, size_t vph, size_t w, size_t h, bool yswap) { |
| 78 | |
| 79 | struct ortho { |
| 80 | inline void operator() (GLfloat *m, |
| 81 | GLfloat left, GLfloat right, GLfloat bottom, GLfloat top, |
| 82 | GLfloat near, GLfloat far) const { |
| 83 | memset(m, 0, 16*sizeof(GLfloat)); |
| 84 | m[ 0] = 2.0f / (right - left); |
| 85 | m[ 5] = 2.0f / (top - bottom); |
| 86 | m[10] =-2.0f / (far - near); |
| 87 | m[15] = 1.0f; |
| 88 | m[12] = -(right + left) / (right - left); |
| 89 | m[13] = -(top + bottom) / (top - bottom); |
| 90 | m[14] = -(far + near) / (far - near); |
| 91 | } |
| 92 | } ortho; |
| 93 | |
| 94 | GLfloat m[16]; |
| 95 | if (yswap) ortho(m, 0, w, h, 0, 0, 1); |
| 96 | else ortho(m, 0, w, 0, h, 0, 1); |
| 97 | |
| 98 | glViewport(0, 0, vpw, vph); |
| 99 | mState.setProjectionMatrix(m); |
| 100 | } |
| 101 | |
| 102 | void GLES20RenderEngine::setupLayerBlending( |
| 103 | bool premultipliedAlpha, bool opaque, int alpha) { |
| 104 | |
| 105 | mState.setPremultipliedAlpha(premultipliedAlpha); |
| 106 | mState.setOpaque(opaque); |
| 107 | mState.setPlaneAlpha(alpha / 255.0f); |
| 108 | |
| 109 | if (alpha < 0xFF || !opaque) { |
| 110 | glEnable(GL_BLEND); |
| 111 | glBlendFunc(premultipliedAlpha ? GL_ONE : GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
| 112 | } else { |
| 113 | glDisable(GL_BLEND); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | void GLES20RenderEngine::setupDimLayerBlending(int alpha) { |
| 118 | mState.setPlaneAlpha(alpha / 255.0f); |
| 119 | |
| 120 | if (alpha == 0xFF) { |
| 121 | glDisable(GL_BLEND); |
| 122 | } else { |
| 123 | glEnable(GL_BLEND); |
| 124 | glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); |
| 125 | } |
| 126 | disableTexturing(); |
| 127 | } |
| 128 | |
| 129 | void GLES20RenderEngine::setupLayerTexturing(size_t textureName, |
| 130 | bool useFiltering, const float* textureMatrix) { |
| 131 | glBindTexture(GL_TEXTURE_EXTERNAL_OES, textureName); |
| 132 | GLenum filter = GL_NEAREST; |
| 133 | if (useFiltering) { |
| 134 | filter = GL_LINEAR; |
| 135 | } |
| 136 | glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 137 | glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 138 | glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, filter); |
| 139 | glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, filter); |
| 140 | |
| 141 | mState.setTextureName(GL_TEXTURE_EXTERNAL_OES, textureName); |
| 142 | mState.setTextureMatrix(textureMatrix); |
| 143 | } |
| 144 | |
| 145 | void GLES20RenderEngine::setupLayerBlackedOut() { |
| 146 | const GLfloat m[16] = {1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1 }; |
| 147 | glBindTexture(GL_TEXTURE_2D, mProtectedTexName); |
| 148 | mState.setTextureName(GL_TEXTURE_2D, mProtectedTexName); |
| 149 | mState.setTextureMatrix(m); |
| 150 | } |
| 151 | |
| 152 | void GLES20RenderEngine::disableTexturing() { |
| 153 | mState.disableTexture(); |
| 154 | } |
| 155 | |
| 156 | void GLES20RenderEngine::disableBlending() { |
| 157 | glDisable(GL_BLEND); |
| 158 | } |
| 159 | |
Mathias Agopian | 458197d | 2013-08-15 14:56:51 -0700 | [diff] [blame^] | 160 | |
| 161 | void GLES20RenderEngine::bindImageAsFramebuffer(EGLImageKHR image, |
| 162 | uint32_t* texName, uint32_t* fbName, uint32_t* status) { |
| 163 | GLuint tname, name; |
| 164 | // turn our EGLImage into a texture |
| 165 | glGenTextures(1, &tname); |
| 166 | glBindTexture(GL_TEXTURE_2D, tname); |
| 167 | glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, (GLeglImageOES)image); |
| 168 | |
| 169 | // create a Framebuffer Object to render into |
| 170 | glGenFramebuffers(1, &name); |
| 171 | glBindFramebuffer(GL_FRAMEBUFFER, name); |
| 172 | glFramebufferTexture2D(GL_FRAMEBUFFER, |
| 173 | GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tname, 0); |
| 174 | |
| 175 | *status = glCheckFramebufferStatus(GL_FRAMEBUFFER); |
| 176 | *texName = tname; |
| 177 | *fbName = name; |
| 178 | } |
| 179 | |
| 180 | void GLES20RenderEngine::unbindFramebuffer(uint32_t texName, uint32_t fbName) { |
| 181 | glBindFramebuffer(GL_FRAMEBUFFER, 0); |
| 182 | glDeleteFramebuffers(1, &fbName); |
| 183 | glDeleteTextures(1, &texName); |
| 184 | } |
| 185 | |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 186 | void GLES20RenderEngine::fillWithColor(const Mesh& mesh, float r, float g, float b, float a) { |
| 187 | mState.setColor(r, g, b, a); |
| 188 | disableTexturing(); |
| 189 | glDisable(GL_BLEND); |
| 190 | |
| 191 | ProgramCache::getInstance().useProgram(mState); |
| 192 | |
| 193 | glVertexAttribPointer(Program::position, |
| 194 | mesh.getVertexSize(), |
| 195 | GL_FLOAT, GL_FALSE, |
| 196 | mesh.getByteStride(), |
Mathias Agopian | 5cdc899 | 2013-08-13 20:51:23 -0700 | [diff] [blame] | 197 | mesh.getPositions()); |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 198 | |
| 199 | glDrawArrays(mesh.getPrimitive(), 0, mesh.getVertexCount()); |
| 200 | } |
| 201 | |
| 202 | void GLES20RenderEngine::drawMesh(const Mesh& mesh) { |
| 203 | |
| 204 | ProgramCache::getInstance().useProgram(mState); |
| 205 | |
| 206 | if (mesh.getTexCoordsSize()) { |
| 207 | glEnableVertexAttribArray(Program::texCoords); |
| 208 | glVertexAttribPointer(Program::texCoords, |
| 209 | mesh.getTexCoordsSize(), |
| 210 | GL_FLOAT, GL_FALSE, |
| 211 | mesh.getByteStride(), |
| 212 | mesh.getTexCoords()); |
| 213 | } |
| 214 | |
| 215 | glVertexAttribPointer(Program::position, |
| 216 | mesh.getVertexSize(), |
| 217 | GL_FLOAT, GL_FALSE, |
| 218 | mesh.getByteStride(), |
Mathias Agopian | 5cdc899 | 2013-08-13 20:51:23 -0700 | [diff] [blame] | 219 | mesh.getPositions()); |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 220 | |
| 221 | glDrawArrays(mesh.getPrimitive(), 0, mesh.getVertexCount()); |
| 222 | |
| 223 | if (mesh.getTexCoordsSize()) { |
| 224 | glDisableVertexAttribArray(Program::texCoords); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | void GLES20RenderEngine::dump(String8& result) { |
Mathias Agopian | 458197d | 2013-08-15 14:56:51 -0700 | [diff] [blame^] | 229 | RenderEngine::dump(result); |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | // --------------------------------------------------------------------------- |
| 233 | }; // namespace android |
| 234 | // --------------------------------------------------------------------------- |
Mathias Agopian | 458197d | 2013-08-15 14:56:51 -0700 | [diff] [blame^] | 235 | |
| 236 | #if defined(__gl_h_) |
| 237 | #error "don't include gl/gl.h in this file" |
| 238 | #endif |