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> |
| 20 | |
| 21 | #include <utils/String8.h> |
| 22 | #include <utils/Trace.h> |
| 23 | |
| 24 | #include <cutils/compiler.h> |
| 25 | |
| 26 | #include "GLES20RenderEngine.h" |
| 27 | #include "GLExtensions.h" |
| 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 | |
| 160 | void GLES20RenderEngine::fillWithColor(const Mesh& mesh, float r, float g, float b, float a) { |
| 161 | mState.setColor(r, g, b, a); |
| 162 | disableTexturing(); |
| 163 | glDisable(GL_BLEND); |
| 164 | |
| 165 | ProgramCache::getInstance().useProgram(mState); |
| 166 | |
| 167 | glVertexAttribPointer(Program::position, |
| 168 | mesh.getVertexSize(), |
| 169 | GL_FLOAT, GL_FALSE, |
| 170 | mesh.getByteStride(), |
| 171 | mesh.getVertices()); |
| 172 | |
| 173 | glDrawArrays(mesh.getPrimitive(), 0, mesh.getVertexCount()); |
| 174 | } |
| 175 | |
| 176 | void GLES20RenderEngine::drawMesh(const Mesh& mesh) { |
| 177 | |
| 178 | ProgramCache::getInstance().useProgram(mState); |
| 179 | |
| 180 | if (mesh.getTexCoordsSize()) { |
| 181 | glEnableVertexAttribArray(Program::texCoords); |
| 182 | glVertexAttribPointer(Program::texCoords, |
| 183 | mesh.getTexCoordsSize(), |
| 184 | GL_FLOAT, GL_FALSE, |
| 185 | mesh.getByteStride(), |
| 186 | mesh.getTexCoords()); |
| 187 | } |
| 188 | |
| 189 | glVertexAttribPointer(Program::position, |
| 190 | mesh.getVertexSize(), |
| 191 | GL_FLOAT, GL_FALSE, |
| 192 | mesh.getByteStride(), |
| 193 | mesh.getVertices()); |
| 194 | |
| 195 | glDrawArrays(mesh.getPrimitive(), 0, mesh.getVertexCount()); |
| 196 | |
| 197 | if (mesh.getTexCoordsSize()) { |
| 198 | glDisableVertexAttribArray(Program::texCoords); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | void GLES20RenderEngine::dump(String8& result) { |
| 203 | const GLExtensions& extensions(GLExtensions::getInstance()); |
| 204 | result.appendFormat("GLES: %s, %s, %s\n", |
| 205 | extensions.getVendor(), |
| 206 | extensions.getRenderer(), |
| 207 | extensions.getVersion()); |
| 208 | result.appendFormat("%s\n", extensions.getExtension()); |
| 209 | } |
| 210 | |
| 211 | // --------------------------------------------------------------------------- |
| 212 | }; // namespace android |
| 213 | // --------------------------------------------------------------------------- |