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#include <GLES/glext.h> 19 20#include <utils/String8.h> 21#include <cutils/compiler.h> 22 23#include "GLES11RenderEngine.h" 24#include "Mesh.h" 25#include "Texture.h" 26 27// --------------------------------------------------------------------------- 28namespace android { 29// --------------------------------------------------------------------------- 30 31GLES11RenderEngine::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 49 const uint16_t protTexData[] = { 0 }; 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 60GLES11RenderEngine::~GLES11RenderEngine() { 61} 62 63 64size_t GLES11RenderEngine::getMaxTextureSize() const { 65 return mMaxTextureSize; 66} 67 68size_t GLES11RenderEngine::getMaxViewportDims() const { 69 return 70 mMaxViewportDims[0] < mMaxViewportDims[1] ? 71 mMaxViewportDims[0] : mMaxViewportDims[1]; 72} 73 74void GLES11RenderEngine::setViewportAndProjection( 75 size_t vpw, size_t vph, size_t w, size_t h, bool yswap) { 76 glViewport(0, 0, vpw, vph); 77 glMatrixMode(GL_PROJECTION); 78 glLoadIdentity(); 79 // put the origin in the left-bottom corner 80 if (yswap) glOrthof(0, w, h, 0, 0, 1); 81 else glOrthof(0, w, 0, h, 0, 1); 82 glMatrixMode(GL_MODELVIEW); 83} 84 85void 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 94 // Av = !opaque ? As*alpha : As 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 139void 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 151void GLES11RenderEngine::setupLayerTexturing(const Texture& texture) { 152 GLuint target = texture.getTextureTarget(); 153 glBindTexture(target, texture.getTextureName()); 154 GLenum filter = GL_NEAREST; 155 if (texture.getFiltering()) { 156 filter = GL_LINEAR; 157 } 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); 162 glMatrixMode(GL_TEXTURE); 163 glLoadMatrixf(texture.getMatrix().asArray()); 164 glMatrixMode(GL_MODELVIEW); 165 glDisable(GL_TEXTURE_2D); 166 glEnable(GL_TEXTURE_EXTERNAL_OES); 167} 168 169void 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 178void GLES11RenderEngine::disableTexturing() { 179 glDisable(GL_TEXTURE_EXTERNAL_OES); 180 glDisable(GL_TEXTURE_2D); 181} 182 183void GLES11RenderEngine::disableBlending() { 184 glDisable(GL_BLEND); 185} 186 187void 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 206void GLES11RenderEngine::unbindFramebuffer(uint32_t texName, uint32_t fbName) { 207 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0); 208 glDeleteFramebuffersOES(1, &fbName); 209 glDeleteTextures(1, &texName); 210} 211 212void GLES11RenderEngine::setupFillWithColor(float r, float g, float b, float a) { 213 glColor4f(r, g, b, a); 214 glDisable(GL_TEXTURE_EXTERNAL_OES); 215 glDisable(GL_TEXTURE_2D); 216 glDisable(GL_BLEND); 217} 218 219void GLES11RenderEngine::drawMesh(const Mesh& mesh) { 220 if (mesh.getTexCoordsSize()) { 221 glEnableClientState(GL_TEXTURE_COORD_ARRAY); 222 glTexCoordPointer(mesh.getTexCoordsSize(), 223 GL_FLOAT, 224 mesh.getByteStride(), 225 mesh.getTexCoords()); 226 } 227 228 glVertexPointer(mesh.getVertexSize(), 229 GL_FLOAT, 230 mesh.getByteStride(), 231 mesh.getPositions()); 232 233 glDrawArrays(mesh.getPrimitive(), 0, mesh.getVertexCount()); 234 235 if (mesh.getTexCoordsSize()) { 236 glDisableClientState(GL_TEXTURE_COORD_ARRAY); 237 } 238} 239 240void GLES11RenderEngine::beginGroup(const mat4& colorTransform) { 241 // doesn't do anything in GLES 1.1 242} 243 244void GLES11RenderEngine::endGroup() { 245 // doesn't do anything in GLES 1.1 246} 247 248void GLES11RenderEngine::dump(String8& result) { 249 RenderEngine::dump(result); 250} 251 252// --------------------------------------------------------------------------- 253}; // namespace android 254// --------------------------------------------------------------------------- 255 256#if defined(__gl2_h_) 257#error "don't include gl2/gl2.h in this file" 258#endif 259