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