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> |
Mathias Agopian | 458197d | 2013-08-15 14:56:51 -0700 | [diff] [blame] | 20 | #include <GLES2/gl2ext.h> |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 21 | |
| 22 | #include <utils/String8.h> |
| 23 | #include <utils/Trace.h> |
| 24 | |
| 25 | #include <cutils/compiler.h> |
| 26 | |
| 27 | #include "GLES20RenderEngine.h" |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 28 | #include "Program.h" |
| 29 | #include "ProgramCache.h" |
| 30 | #include "Description.h" |
| 31 | #include "Mesh.h" |
Mathias Agopian | 49457ac | 2013-08-14 18:20:17 -0700 | [diff] [blame] | 32 | #include "Texture.h" |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 33 | |
| 34 | // --------------------------------------------------------------------------- |
| 35 | namespace android { |
| 36 | // --------------------------------------------------------------------------- |
| 37 | |
Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 38 | GLES20RenderEngine::GLES20RenderEngine() : |
| 39 | mVpWidth(0), mVpHeight(0) { |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 40 | |
| 41 | glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize); |
| 42 | glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims); |
| 43 | |
| 44 | glPixelStorei(GL_UNPACK_ALIGNMENT, 4); |
| 45 | glPixelStorei(GL_PACK_ALIGNMENT, 4); |
| 46 | |
| 47 | struct pack565 { |
| 48 | inline uint16_t operator() (int r, int g, int b) const { |
| 49 | return (r<<11)|(g<<5)|b; |
| 50 | } |
| 51 | } pack565; |
| 52 | |
Andy McFadden | c6f2169 | 2013-10-11 11:16:03 -0700 | [diff] [blame^] | 53 | const uint16_t protTexData[] = { 0 }; |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 54 | glGenTextures(1, &mProtectedTexName); |
| 55 | glBindTexture(GL_TEXTURE_2D, mProtectedTexName); |
| 56 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 57 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 58 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); |
| 59 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); |
| 60 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0, |
| 61 | GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData); |
Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 62 | |
| 63 | //mColorBlindnessCorrection = M; |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | GLES20RenderEngine::~GLES20RenderEngine() { |
| 67 | } |
| 68 | |
| 69 | |
| 70 | size_t GLES20RenderEngine::getMaxTextureSize() const { |
| 71 | return mMaxTextureSize; |
| 72 | } |
| 73 | |
| 74 | size_t GLES20RenderEngine::getMaxViewportDims() const { |
| 75 | return |
| 76 | mMaxViewportDims[0] < mMaxViewportDims[1] ? |
| 77 | mMaxViewportDims[0] : mMaxViewportDims[1]; |
| 78 | } |
| 79 | |
| 80 | void GLES20RenderEngine::setViewportAndProjection( |
| 81 | size_t vpw, size_t vph, size_t w, size_t h, bool yswap) { |
Mathias Agopian | a8c386f | 2013-08-26 20:42:07 -0700 | [diff] [blame] | 82 | mat4 m; |
| 83 | if (yswap) m = mat4::ortho(0, w, h, 0, 0, 1); |
| 84 | else m = mat4::ortho(0, w, 0, h, 0, 1); |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 85 | |
| 86 | glViewport(0, 0, vpw, vph); |
| 87 | mState.setProjectionMatrix(m); |
Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 88 | mVpWidth = vpw; |
| 89 | mVpHeight = vph; |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | void GLES20RenderEngine::setupLayerBlending( |
| 93 | bool premultipliedAlpha, bool opaque, int alpha) { |
| 94 | |
| 95 | mState.setPremultipliedAlpha(premultipliedAlpha); |
| 96 | mState.setOpaque(opaque); |
| 97 | mState.setPlaneAlpha(alpha / 255.0f); |
| 98 | |
| 99 | if (alpha < 0xFF || !opaque) { |
| 100 | glEnable(GL_BLEND); |
| 101 | glBlendFunc(premultipliedAlpha ? GL_ONE : GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
| 102 | } else { |
| 103 | glDisable(GL_BLEND); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | void GLES20RenderEngine::setupDimLayerBlending(int alpha) { |
Mathias Agopian | bcab7b1 | 2013-08-23 16:12:34 -0700 | [diff] [blame] | 108 | mState.setPlaneAlpha(1.0f); |
| 109 | mState.setPremultipliedAlpha(true); |
| 110 | mState.setOpaque(false); |
| 111 | mState.setColor(0, 0, 0, alpha/255.0f); |
| 112 | mState.disableTexture(); |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 113 | |
| 114 | if (alpha == 0xFF) { |
| 115 | glDisable(GL_BLEND); |
| 116 | } else { |
| 117 | glEnable(GL_BLEND); |
| 118 | glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); |
| 119 | } |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Mathias Agopian | 49457ac | 2013-08-14 18:20:17 -0700 | [diff] [blame] | 122 | void GLES20RenderEngine::setupLayerTexturing(const Texture& texture) { |
| 123 | GLuint target = texture.getTextureTarget(); |
| 124 | glBindTexture(target, texture.getTextureName()); |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 125 | GLenum filter = GL_NEAREST; |
Mathias Agopian | 49457ac | 2013-08-14 18:20:17 -0700 | [diff] [blame] | 126 | if (texture.getFiltering()) { |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 127 | filter = GL_LINEAR; |
| 128 | } |
Mathias Agopian | 49457ac | 2013-08-14 18:20:17 -0700 | [diff] [blame] | 129 | glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 130 | glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 131 | glTexParameteri(target, GL_TEXTURE_MAG_FILTER, filter); |
| 132 | glTexParameteri(target, GL_TEXTURE_MIN_FILTER, filter); |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 133 | |
Mathias Agopian | 49457ac | 2013-08-14 18:20:17 -0700 | [diff] [blame] | 134 | mState.setTexture(texture); |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | void GLES20RenderEngine::setupLayerBlackedOut() { |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 138 | glBindTexture(GL_TEXTURE_2D, mProtectedTexName); |
Mathias Agopian | 49457ac | 2013-08-14 18:20:17 -0700 | [diff] [blame] | 139 | Texture texture(Texture::TEXTURE_2D, mProtectedTexName); |
| 140 | texture.setDimensions(1, 1); // FIXME: we should get that from somewhere |
| 141 | mState.setTexture(texture); |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | void GLES20RenderEngine::disableTexturing() { |
| 145 | mState.disableTexture(); |
| 146 | } |
| 147 | |
| 148 | void GLES20RenderEngine::disableBlending() { |
| 149 | glDisable(GL_BLEND); |
| 150 | } |
| 151 | |
Mathias Agopian | 458197d | 2013-08-15 14:56:51 -0700 | [diff] [blame] | 152 | |
| 153 | void GLES20RenderEngine::bindImageAsFramebuffer(EGLImageKHR image, |
| 154 | uint32_t* texName, uint32_t* fbName, uint32_t* status) { |
| 155 | GLuint tname, name; |
| 156 | // turn our EGLImage into a texture |
| 157 | glGenTextures(1, &tname); |
| 158 | glBindTexture(GL_TEXTURE_2D, tname); |
| 159 | glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, (GLeglImageOES)image); |
| 160 | |
| 161 | // create a Framebuffer Object to render into |
| 162 | glGenFramebuffers(1, &name); |
| 163 | glBindFramebuffer(GL_FRAMEBUFFER, name); |
Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 164 | glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tname, 0); |
Mathias Agopian | 458197d | 2013-08-15 14:56:51 -0700 | [diff] [blame] | 165 | |
| 166 | *status = glCheckFramebufferStatus(GL_FRAMEBUFFER); |
| 167 | *texName = tname; |
| 168 | *fbName = name; |
| 169 | } |
| 170 | |
| 171 | void GLES20RenderEngine::unbindFramebuffer(uint32_t texName, uint32_t fbName) { |
| 172 | glBindFramebuffer(GL_FRAMEBUFFER, 0); |
| 173 | glDeleteFramebuffers(1, &fbName); |
| 174 | glDeleteTextures(1, &texName); |
| 175 | } |
| 176 | |
Mathias Agopian | 19733a3 | 2013-08-28 18:13:56 -0700 | [diff] [blame] | 177 | void GLES20RenderEngine::setupFillWithColor(float r, float g, float b, float a) { |
| 178 | mState.setPlaneAlpha(1.0f); |
| 179 | mState.setPremultipliedAlpha(true); |
| 180 | mState.setOpaque(false); |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 181 | mState.setColor(r, g, b, a); |
Mathias Agopian | 19733a3 | 2013-08-28 18:13:56 -0700 | [diff] [blame] | 182 | mState.disableTexture(); |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 183 | glDisable(GL_BLEND); |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | void GLES20RenderEngine::drawMesh(const Mesh& mesh) { |
| 187 | |
| 188 | ProgramCache::getInstance().useProgram(mState); |
| 189 | |
| 190 | if (mesh.getTexCoordsSize()) { |
| 191 | glEnableVertexAttribArray(Program::texCoords); |
| 192 | glVertexAttribPointer(Program::texCoords, |
| 193 | mesh.getTexCoordsSize(), |
| 194 | GL_FLOAT, GL_FALSE, |
| 195 | mesh.getByteStride(), |
| 196 | mesh.getTexCoords()); |
| 197 | } |
| 198 | |
| 199 | glVertexAttribPointer(Program::position, |
| 200 | mesh.getVertexSize(), |
| 201 | GL_FLOAT, GL_FALSE, |
| 202 | mesh.getByteStride(), |
Mathias Agopian | 5cdc899 | 2013-08-13 20:51:23 -0700 | [diff] [blame] | 203 | mesh.getPositions()); |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 204 | |
| 205 | glDrawArrays(mesh.getPrimitive(), 0, mesh.getVertexCount()); |
| 206 | |
| 207 | if (mesh.getTexCoordsSize()) { |
| 208 | glDisableVertexAttribArray(Program::texCoords); |
| 209 | } |
| 210 | } |
| 211 | |
Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 212 | void GLES20RenderEngine::beginGroup(const mat4& colorTransform) { |
| 213 | |
| 214 | GLuint tname, name; |
| 215 | // create the texture |
| 216 | glGenTextures(1, &tname); |
| 217 | glBindTexture(GL_TEXTURE_2D, tname); |
| 218 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 219 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 220 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 221 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 222 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, mVpWidth, mVpHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0); |
| 223 | |
| 224 | // create a Framebuffer Object to render into |
| 225 | glGenFramebuffers(1, &name); |
| 226 | glBindFramebuffer(GL_FRAMEBUFFER, name); |
| 227 | glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tname, 0); |
| 228 | |
| 229 | Group group; |
| 230 | group.texture = tname; |
| 231 | group.fbo = name; |
| 232 | group.width = mVpWidth; |
| 233 | group.height = mVpHeight; |
| 234 | group.colorTransform = colorTransform; |
| 235 | |
| 236 | mGroupStack.push(group); |
| 237 | } |
| 238 | |
| 239 | void GLES20RenderEngine::endGroup() { |
| 240 | |
| 241 | const Group group(mGroupStack.top()); |
| 242 | mGroupStack.pop(); |
| 243 | |
| 244 | // activate the previous render target |
| 245 | GLuint fbo = 0; |
| 246 | if (!mGroupStack.isEmpty()) { |
| 247 | fbo = mGroupStack.top().fbo; |
| 248 | } |
| 249 | glBindFramebuffer(GL_FRAMEBUFFER, fbo); |
| 250 | |
| 251 | // set our state |
| 252 | Texture texture(Texture::TEXTURE_2D, group.texture); |
| 253 | texture.setDimensions(group.width, group.height); |
| 254 | glBindTexture(GL_TEXTURE_2D, group.texture); |
| 255 | |
| 256 | mState.setPlaneAlpha(1.0f); |
| 257 | mState.setPremultipliedAlpha(true); |
| 258 | mState.setOpaque(false); |
| 259 | mState.setTexture(texture); |
| 260 | mState.setColorMatrix(group.colorTransform); |
| 261 | glDisable(GL_BLEND); |
| 262 | |
| 263 | Mesh mesh(Mesh::TRIANGLE_FAN, 4, 2, 2); |
| 264 | Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>()); |
| 265 | Mesh::VertexArray<vec2> texCoord(mesh.getTexCoordArray<vec2>()); |
| 266 | position[0] = vec2(0, 0); |
| 267 | position[1] = vec2(group.width, 0); |
| 268 | position[2] = vec2(group.width, group.height); |
| 269 | position[3] = vec2(0, group.height); |
| 270 | texCoord[0] = vec2(0, 0); |
| 271 | texCoord[1] = vec2(1, 0); |
| 272 | texCoord[2] = vec2(1, 1); |
| 273 | texCoord[3] = vec2(0, 1); |
| 274 | drawMesh(mesh); |
| 275 | |
| 276 | // reset color matrix |
| 277 | mState.setColorMatrix(mat4()); |
| 278 | |
| 279 | // free our fbo and texture |
| 280 | glDeleteFramebuffers(1, &group.fbo); |
| 281 | glDeleteTextures(1, &group.texture); |
| 282 | } |
| 283 | |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 284 | void GLES20RenderEngine::dump(String8& result) { |
Mathias Agopian | 458197d | 2013-08-15 14:56:51 -0700 | [diff] [blame] | 285 | RenderEngine::dump(result); |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | // --------------------------------------------------------------------------- |
| 289 | }; // namespace android |
| 290 | // --------------------------------------------------------------------------- |
Mathias Agopian | 458197d | 2013-08-15 14:56:51 -0700 | [diff] [blame] | 291 | |
| 292 | #if defined(__gl_h_) |
| 293 | #error "don't include gl/gl.h in this file" |
| 294 | #endif |