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> |
Mathias Agopian | 458197d | 2013-08-15 14:56:51 -0700 | [diff] [blame] | 18 | #include <GLES/glext.h> |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 19 | |
| 20 | #include <utils/String8.h> |
| 21 | #include <cutils/compiler.h> |
| 22 | |
| 23 | #include "GLES11RenderEngine.h" |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 24 | #include "Mesh.h" |
Mathias Agopian | 49457ac | 2013-08-14 18:20:17 -0700 | [diff] [blame] | 25 | #include "Texture.h" |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 26 | |
| 27 | // --------------------------------------------------------------------------- |
| 28 | namespace android { |
| 29 | // --------------------------------------------------------------------------- |
| 30 | |
| 31 | GLES11RenderEngine::GLES11RenderEngine() { |
| 32 | |
| 33 | glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize); |
| 34 | glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims); |
| 35 | |
| 36 | glPixelStorei(GL_UNPACK_ALIGNMENT, 4); |
| 37 | glPixelStorei(GL_PACK_ALIGNMENT, 4); |
| 38 | glEnableClientState(GL_VERTEX_ARRAY); |
| 39 | glShadeModel(GL_FLAT); |
| 40 | glDisable(GL_DITHER); |
| 41 | glDisable(GL_CULL_FACE); |
| 42 | |
| 43 | struct pack565 { |
| 44 | inline uint16_t operator() (int r, int g, int b) const { |
| 45 | return (r<<11)|(g<<5)|b; |
| 46 | } |
| 47 | } pack565; |
| 48 | |
Andy McFadden | c6f2169 | 2013-10-11 11:16:03 -0700 | [diff] [blame^] | 49 | const uint16_t protTexData[] = { 0 }; |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 50 | glGenTextures(1, &mProtectedTexName); |
| 51 | glBindTexture(GL_TEXTURE_2D, mProtectedTexName); |
| 52 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 53 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 54 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); |
| 55 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); |
| 56 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0, |
| 57 | GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData); |
| 58 | } |
| 59 | |
| 60 | GLES11RenderEngine::~GLES11RenderEngine() { |
| 61 | } |
| 62 | |
| 63 | |
| 64 | size_t GLES11RenderEngine::getMaxTextureSize() const { |
| 65 | return mMaxTextureSize; |
| 66 | } |
| 67 | |
| 68 | size_t GLES11RenderEngine::getMaxViewportDims() const { |
| 69 | return |
| 70 | mMaxViewportDims[0] < mMaxViewportDims[1] ? |
| 71 | mMaxViewportDims[0] : mMaxViewportDims[1]; |
| 72 | } |
| 73 | |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 74 | void GLES11RenderEngine::setViewportAndProjection( |
| 75 | size_t vpw, size_t vph, size_t w, size_t h, bool yswap) { |
| 76 | glViewport(0, 0, vpw, vph); |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 77 | glMatrixMode(GL_PROJECTION); |
| 78 | glLoadIdentity(); |
| 79 | // put the origin in the left-bottom corner |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 80 | if (yswap) glOrthof(0, w, h, 0, 0, 1); |
| 81 | else glOrthof(0, w, 0, h, 0, 1); |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 82 | glMatrixMode(GL_MODELVIEW); |
| 83 | } |
| 84 | |
| 85 | void GLES11RenderEngine::setupLayerBlending( |
| 86 | bool premultipliedAlpha, bool opaque, int alpha) { |
| 87 | GLenum combineRGB; |
| 88 | GLenum combineAlpha; |
| 89 | GLenum src0Alpha; |
| 90 | GLfloat envColor[4]; |
| 91 | |
| 92 | if (CC_UNLIKELY(alpha < 0xFF)) { |
| 93 | // Cv = premultiplied ? Cs*alpha : Cs |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 94 | // Av = !opaque ? As*alpha : As |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 95 | combineRGB = premultipliedAlpha ? GL_MODULATE : GL_REPLACE; |
| 96 | combineAlpha = !opaque ? GL_MODULATE : GL_REPLACE; |
| 97 | src0Alpha = GL_CONSTANT; |
| 98 | envColor[0] = alpha * (1.0f / 255.0f); |
| 99 | } else { |
| 100 | // Cv = Cs |
| 101 | // Av = opaque ? 1.0 : As |
| 102 | combineRGB = GL_REPLACE; |
| 103 | combineAlpha = GL_REPLACE; |
| 104 | src0Alpha = opaque ? GL_CONSTANT : GL_TEXTURE; |
| 105 | envColor[0] = 1.0f; |
| 106 | } |
| 107 | |
| 108 | glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE); |
| 109 | glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, combineRGB); |
| 110 | glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_RGB, GL_TEXTURE); |
| 111 | glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR); |
| 112 | if (combineRGB == GL_MODULATE) { |
| 113 | glTexEnvi(GL_TEXTURE_ENV, GL_SRC1_RGB, GL_CONSTANT); |
| 114 | glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_SRC_COLOR); |
| 115 | } |
| 116 | glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, combineAlpha); |
| 117 | glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_ALPHA, src0Alpha); |
| 118 | glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA, GL_SRC_ALPHA); |
| 119 | if (combineAlpha == GL_MODULATE) { |
| 120 | glTexEnvi(GL_TEXTURE_ENV, GL_SRC1_ALPHA, GL_TEXTURE); |
| 121 | glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA, GL_SRC_ALPHA); |
| 122 | } |
| 123 | if (combineRGB == GL_MODULATE || src0Alpha == GL_CONSTANT) { |
| 124 | envColor[1] = envColor[0]; |
| 125 | envColor[2] = envColor[0]; |
| 126 | envColor[3] = envColor[0]; |
| 127 | glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, envColor); |
| 128 | } |
| 129 | |
| 130 | if (alpha < 0xFF || !opaque) { |
| 131 | glEnable(GL_BLEND); |
| 132 | glBlendFunc(premultipliedAlpha ? GL_ONE : GL_SRC_ALPHA, |
| 133 | GL_ONE_MINUS_SRC_ALPHA); |
| 134 | } else { |
| 135 | glDisable(GL_BLEND); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | void GLES11RenderEngine::setupDimLayerBlending(int alpha) { |
| 140 | glDisable(GL_TEXTURE_EXTERNAL_OES); |
| 141 | glDisable(GL_TEXTURE_2D); |
| 142 | if (alpha == 0xFF) { |
| 143 | glDisable(GL_BLEND); |
| 144 | } else { |
| 145 | glEnable(GL_BLEND); |
| 146 | glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); |
| 147 | } |
| 148 | glColor4f(0, 0, 0, alpha/255.0f); |
| 149 | } |
| 150 | |
Mathias Agopian | 49457ac | 2013-08-14 18:20:17 -0700 | [diff] [blame] | 151 | void GLES11RenderEngine::setupLayerTexturing(const Texture& texture) { |
| 152 | GLuint target = texture.getTextureTarget(); |
| 153 | glBindTexture(target, texture.getTextureName()); |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 154 | GLenum filter = GL_NEAREST; |
Mathias Agopian | 49457ac | 2013-08-14 18:20:17 -0700 | [diff] [blame] | 155 | if (texture.getFiltering()) { |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 156 | filter = GL_LINEAR; |
| 157 | } |
Mathias Agopian | 49457ac | 2013-08-14 18:20:17 -0700 | [diff] [blame] | 158 | glTexParameterx(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 159 | glTexParameterx(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 160 | glTexParameterx(target, GL_TEXTURE_MAG_FILTER, filter); |
| 161 | glTexParameterx(target, GL_TEXTURE_MIN_FILTER, filter); |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 162 | glMatrixMode(GL_TEXTURE); |
Mathias Agopian | a8c386f | 2013-08-26 20:42:07 -0700 | [diff] [blame] | 163 | glLoadMatrixf(texture.getMatrix().asArray()); |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 164 | glMatrixMode(GL_MODELVIEW); |
| 165 | glDisable(GL_TEXTURE_2D); |
| 166 | glEnable(GL_TEXTURE_EXTERNAL_OES); |
| 167 | } |
| 168 | |
| 169 | void GLES11RenderEngine::setupLayerBlackedOut() { |
| 170 | glBindTexture(GL_TEXTURE_2D, mProtectedTexName); |
| 171 | glMatrixMode(GL_TEXTURE); |
| 172 | glLoadIdentity(); |
| 173 | glMatrixMode(GL_MODELVIEW); |
| 174 | glDisable(GL_TEXTURE_EXTERNAL_OES); |
| 175 | glEnable(GL_TEXTURE_2D); |
| 176 | } |
| 177 | |
| 178 | void GLES11RenderEngine::disableTexturing() { |
| 179 | glDisable(GL_TEXTURE_EXTERNAL_OES); |
| 180 | glDisable(GL_TEXTURE_2D); |
| 181 | } |
| 182 | |
| 183 | void GLES11RenderEngine::disableBlending() { |
| 184 | glDisable(GL_BLEND); |
| 185 | } |
| 186 | |
Mathias Agopian | 458197d | 2013-08-15 14:56:51 -0700 | [diff] [blame] | 187 | void GLES11RenderEngine::bindImageAsFramebuffer(EGLImageKHR image, |
| 188 | uint32_t* texName, uint32_t* fbName, uint32_t* status) { |
| 189 | GLuint tname, name; |
| 190 | // turn our EGLImage into a texture |
| 191 | glGenTextures(1, &tname); |
| 192 | glBindTexture(GL_TEXTURE_2D, tname); |
| 193 | glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, (GLeglImageOES)image); |
| 194 | |
| 195 | // create a Framebuffer Object to render into |
| 196 | glGenFramebuffersOES(1, &name); |
| 197 | glBindFramebufferOES(GL_FRAMEBUFFER_OES, name); |
| 198 | glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES, |
| 199 | GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0); |
| 200 | |
| 201 | *status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES); |
| 202 | *texName = tname; |
| 203 | *fbName = name; |
| 204 | } |
| 205 | |
| 206 | void GLES11RenderEngine::unbindFramebuffer(uint32_t texName, uint32_t fbName) { |
| 207 | glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0); |
| 208 | glDeleteFramebuffersOES(1, &fbName); |
| 209 | glDeleteTextures(1, &texName); |
| 210 | } |
| 211 | |
Mathias Agopian | 19733a3 | 2013-08-28 18:13:56 -0700 | [diff] [blame] | 212 | void GLES11RenderEngine::setupFillWithColor(float r, float g, float b, float a) { |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 213 | glColor4f(r, g, b, a); |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 214 | glDisable(GL_TEXTURE_EXTERNAL_OES); |
| 215 | glDisable(GL_TEXTURE_2D); |
| 216 | glDisable(GL_BLEND); |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 217 | } |
| 218 | |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 219 | void GLES11RenderEngine::drawMesh(const Mesh& mesh) { |
| 220 | if (mesh.getTexCoordsSize()) { |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 221 | glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 222 | glTexCoordPointer(mesh.getTexCoordsSize(), |
| 223 | GL_FLOAT, |
| 224 | mesh.getByteStride(), |
| 225 | mesh.getTexCoords()); |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 226 | } |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 227 | |
| 228 | glVertexPointer(mesh.getVertexSize(), |
| 229 | GL_FLOAT, |
| 230 | mesh.getByteStride(), |
Mathias Agopian | 5cdc899 | 2013-08-13 20:51:23 -0700 | [diff] [blame] | 231 | mesh.getPositions()); |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 232 | |
| 233 | glDrawArrays(mesh.getPrimitive(), 0, mesh.getVertexCount()); |
| 234 | |
| 235 | if (mesh.getTexCoordsSize()) { |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 236 | glDisableClientState(GL_TEXTURE_COORD_ARRAY); |
| 237 | } |
| 238 | } |
| 239 | |
Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 240 | void GLES11RenderEngine::beginGroup(const mat4& colorTransform) { |
| 241 | // doesn't do anything in GLES 1.1 |
| 242 | } |
| 243 | |
| 244 | void GLES11RenderEngine::endGroup() { |
| 245 | // doesn't do anything in GLES 1.1 |
| 246 | } |
| 247 | |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 248 | void GLES11RenderEngine::dump(String8& result) { |
Mathias Agopian | 458197d | 2013-08-15 14:56:51 -0700 | [diff] [blame] | 249 | RenderEngine::dump(result); |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | // --------------------------------------------------------------------------- |
| 253 | }; // namespace android |
| 254 | // --------------------------------------------------------------------------- |
Mathias Agopian | 458197d | 2013-08-15 14:56:51 -0700 | [diff] [blame] | 255 | |
| 256 | #if defined(__gl2_h_) |
| 257 | #error "don't include gl2/gl2.h in this file" |
| 258 | #endif |