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 <GLES/gl.h> |
| 18 | |
| 19 | #include <utils/String8.h> |
| 20 | #include <cutils/compiler.h> |
| 21 | |
| 22 | #include "GLES11RenderEngine.h" |
| 23 | #include "GLExtensions.h" |
| 24 | |
| 25 | // --------------------------------------------------------------------------- |
| 26 | namespace android { |
| 27 | // --------------------------------------------------------------------------- |
| 28 | |
| 29 | GLES11RenderEngine::GLES11RenderEngine() { |
| 30 | |
| 31 | glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize); |
| 32 | glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims); |
| 33 | |
| 34 | glPixelStorei(GL_UNPACK_ALIGNMENT, 4); |
| 35 | glPixelStorei(GL_PACK_ALIGNMENT, 4); |
| 36 | glEnableClientState(GL_VERTEX_ARRAY); |
| 37 | glShadeModel(GL_FLAT); |
| 38 | glDisable(GL_DITHER); |
| 39 | glDisable(GL_CULL_FACE); |
| 40 | |
| 41 | struct pack565 { |
| 42 | inline uint16_t operator() (int r, int g, int b) const { |
| 43 | return (r<<11)|(g<<5)|b; |
| 44 | } |
| 45 | } pack565; |
| 46 | |
| 47 | const uint16_t protTexData[] = { pack565(0x03, 0x03, 0x03) }; |
| 48 | glGenTextures(1, &mProtectedTexName); |
| 49 | glBindTexture(GL_TEXTURE_2D, mProtectedTexName); |
| 50 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 51 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 52 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); |
| 53 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); |
| 54 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0, |
| 55 | GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData); |
| 56 | } |
| 57 | |
| 58 | GLES11RenderEngine::~GLES11RenderEngine() { |
| 59 | } |
| 60 | |
| 61 | |
| 62 | size_t GLES11RenderEngine::getMaxTextureSize() const { |
| 63 | return mMaxTextureSize; |
| 64 | } |
| 65 | |
| 66 | size_t GLES11RenderEngine::getMaxViewportDims() const { |
| 67 | return |
| 68 | mMaxViewportDims[0] < mMaxViewportDims[1] ? |
| 69 | mMaxViewportDims[0] : mMaxViewportDims[1]; |
| 70 | } |
| 71 | |
| 72 | void GLES11RenderEngine::setViewportAndProjection(size_t w, size_t h) { |
| 73 | glViewport(0, 0, w, h); |
| 74 | glMatrixMode(GL_PROJECTION); |
| 75 | glLoadIdentity(); |
| 76 | // put the origin in the left-bottom corner |
| 77 | glOrthof(0, w, 0, h, 0, 1); // l=0, r=w ; b=0, t=h |
| 78 | glMatrixMode(GL_MODELVIEW); |
| 79 | } |
| 80 | |
| 81 | void GLES11RenderEngine::setupLayerBlending( |
| 82 | bool premultipliedAlpha, bool opaque, int alpha) { |
| 83 | GLenum combineRGB; |
| 84 | GLenum combineAlpha; |
| 85 | GLenum src0Alpha; |
| 86 | GLfloat envColor[4]; |
| 87 | |
| 88 | if (CC_UNLIKELY(alpha < 0xFF)) { |
| 89 | // Cv = premultiplied ? Cs*alpha : Cs |
| 90 | // Av = !opaque ? alpha*As : 1.0 |
| 91 | combineRGB = premultipliedAlpha ? GL_MODULATE : GL_REPLACE; |
| 92 | combineAlpha = !opaque ? GL_MODULATE : GL_REPLACE; |
| 93 | src0Alpha = GL_CONSTANT; |
| 94 | envColor[0] = alpha * (1.0f / 255.0f); |
| 95 | } else { |
| 96 | // Cv = Cs |
| 97 | // Av = opaque ? 1.0 : As |
| 98 | combineRGB = GL_REPLACE; |
| 99 | combineAlpha = GL_REPLACE; |
| 100 | src0Alpha = opaque ? GL_CONSTANT : GL_TEXTURE; |
| 101 | envColor[0] = 1.0f; |
| 102 | } |
| 103 | |
| 104 | glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE); |
| 105 | glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, combineRGB); |
| 106 | glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_RGB, GL_TEXTURE); |
| 107 | glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR); |
| 108 | if (combineRGB == GL_MODULATE) { |
| 109 | glTexEnvi(GL_TEXTURE_ENV, GL_SRC1_RGB, GL_CONSTANT); |
| 110 | glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_SRC_COLOR); |
| 111 | } |
| 112 | glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, combineAlpha); |
| 113 | glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_ALPHA, src0Alpha); |
| 114 | glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA, GL_SRC_ALPHA); |
| 115 | if (combineAlpha == GL_MODULATE) { |
| 116 | glTexEnvi(GL_TEXTURE_ENV, GL_SRC1_ALPHA, GL_TEXTURE); |
| 117 | glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA, GL_SRC_ALPHA); |
| 118 | } |
| 119 | if (combineRGB == GL_MODULATE || src0Alpha == GL_CONSTANT) { |
| 120 | envColor[1] = envColor[0]; |
| 121 | envColor[2] = envColor[0]; |
| 122 | envColor[3] = envColor[0]; |
| 123 | glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, envColor); |
| 124 | } |
| 125 | |
| 126 | if (alpha < 0xFF || !opaque) { |
| 127 | glEnable(GL_BLEND); |
| 128 | glBlendFunc(premultipliedAlpha ? GL_ONE : GL_SRC_ALPHA, |
| 129 | GL_ONE_MINUS_SRC_ALPHA); |
| 130 | } else { |
| 131 | glDisable(GL_BLEND); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | void GLES11RenderEngine::setupDimLayerBlending(int alpha) { |
| 136 | glDisable(GL_TEXTURE_EXTERNAL_OES); |
| 137 | glDisable(GL_TEXTURE_2D); |
| 138 | if (alpha == 0xFF) { |
| 139 | glDisable(GL_BLEND); |
| 140 | } else { |
| 141 | glEnable(GL_BLEND); |
| 142 | glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); |
| 143 | } |
| 144 | glColor4f(0, 0, 0, alpha/255.0f); |
| 145 | } |
| 146 | |
| 147 | void GLES11RenderEngine::setupLayerTexturing(size_t textureName, |
| 148 | bool useFiltering, const float* textureMatrix) { |
| 149 | glBindTexture(GL_TEXTURE_EXTERNAL_OES, textureName); |
| 150 | GLenum filter = GL_NEAREST; |
| 151 | if (useFiltering) { |
| 152 | filter = GL_LINEAR; |
| 153 | } |
| 154 | glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 155 | glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 156 | glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, filter); |
| 157 | glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, filter); |
| 158 | glMatrixMode(GL_TEXTURE); |
| 159 | glLoadMatrixf(textureMatrix); |
| 160 | glMatrixMode(GL_MODELVIEW); |
| 161 | glDisable(GL_TEXTURE_2D); |
| 162 | glEnable(GL_TEXTURE_EXTERNAL_OES); |
| 163 | } |
| 164 | |
| 165 | void GLES11RenderEngine::setupLayerBlackedOut() { |
| 166 | glBindTexture(GL_TEXTURE_2D, mProtectedTexName); |
| 167 | glMatrixMode(GL_TEXTURE); |
| 168 | glLoadIdentity(); |
| 169 | glMatrixMode(GL_MODELVIEW); |
| 170 | glDisable(GL_TEXTURE_EXTERNAL_OES); |
| 171 | glEnable(GL_TEXTURE_2D); |
| 172 | } |
| 173 | |
| 174 | void GLES11RenderEngine::disableTexturing() { |
| 175 | glDisable(GL_TEXTURE_EXTERNAL_OES); |
| 176 | glDisable(GL_TEXTURE_2D); |
| 177 | } |
| 178 | |
| 179 | void GLES11RenderEngine::disableBlending() { |
| 180 | glDisable(GL_BLEND); |
| 181 | } |
| 182 | |
| 183 | void GLES11RenderEngine::clearWithColor(const float vertices[][2] , size_t count, |
| 184 | float red, float green, float blue, float alpha) { |
| 185 | glColor4f(red, green, blue, alpha); |
| 186 | glDisable(GL_TEXTURE_EXTERNAL_OES); |
| 187 | glDisable(GL_TEXTURE_2D); |
| 188 | glDisable(GL_BLEND); |
| 189 | glVertexPointer(2, GL_FLOAT, 0, vertices); |
| 190 | glDrawArrays(GL_TRIANGLE_FAN, 0, count); |
| 191 | } |
| 192 | |
| 193 | void GLES11RenderEngine::drawMesh2D( |
| 194 | const float vertices[][2], const float texCoords[][2], size_t count) { |
| 195 | if (texCoords) { |
| 196 | glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
| 197 | glTexCoordPointer(2, GL_FLOAT, 0, texCoords); |
| 198 | } |
| 199 | glVertexPointer(2, GL_FLOAT, 0, vertices); |
| 200 | glDrawArrays(GL_TRIANGLE_FAN, 0, count); |
| 201 | if (texCoords) { |
| 202 | glDisableClientState(GL_TEXTURE_COORD_ARRAY); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | void GLES11RenderEngine::dump(String8& result) { |
| 207 | const GLExtensions& extensions(GLExtensions::getInstance()); |
| 208 | result.appendFormat("GLES: %s, %s, %s\n", |
| 209 | extensions.getVendor(), |
| 210 | extensions.getRenderer(), |
| 211 | extensions.getVersion()); |
| 212 | result.appendFormat("%s\n", extensions.getExtension()); |
| 213 | } |
| 214 | |
| 215 | // --------------------------------------------------------------------------- |
| 216 | }; // namespace android |
| 217 | // --------------------------------------------------------------------------- |