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 | |
| 38 | GLES20RenderEngine::GLES20RenderEngine() { |
| 39 | |
| 40 | glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize); |
| 41 | glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims); |
| 42 | |
| 43 | glPixelStorei(GL_UNPACK_ALIGNMENT, 4); |
| 44 | glPixelStorei(GL_PACK_ALIGNMENT, 4); |
| 45 | |
| 46 | struct pack565 { |
| 47 | inline uint16_t operator() (int r, int g, int b) const { |
| 48 | return (r<<11)|(g<<5)|b; |
| 49 | } |
| 50 | } pack565; |
| 51 | |
| 52 | const uint16_t protTexData[] = { pack565(0x03, 0x03, 0x03) }; |
| 53 | glGenTextures(1, &mProtectedTexName); |
| 54 | glBindTexture(GL_TEXTURE_2D, mProtectedTexName); |
| 55 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 56 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 57 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); |
| 58 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); |
| 59 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0, |
| 60 | GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData); |
| 61 | } |
| 62 | |
| 63 | GLES20RenderEngine::~GLES20RenderEngine() { |
| 64 | } |
| 65 | |
| 66 | |
| 67 | size_t GLES20RenderEngine::getMaxTextureSize() const { |
| 68 | return mMaxTextureSize; |
| 69 | } |
| 70 | |
| 71 | size_t GLES20RenderEngine::getMaxViewportDims() const { |
| 72 | return |
| 73 | mMaxViewportDims[0] < mMaxViewportDims[1] ? |
| 74 | mMaxViewportDims[0] : mMaxViewportDims[1]; |
| 75 | } |
| 76 | |
| 77 | void GLES20RenderEngine::setViewportAndProjection( |
| 78 | 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] | 79 | mat4 m; |
| 80 | if (yswap) m = mat4::ortho(0, w, h, 0, 0, 1); |
| 81 | else m = mat4::ortho(0, w, 0, h, 0, 1); |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 82 | |
| 83 | glViewport(0, 0, vpw, vph); |
| 84 | mState.setProjectionMatrix(m); |
| 85 | } |
| 86 | |
| 87 | void GLES20RenderEngine::setupLayerBlending( |
| 88 | bool premultipliedAlpha, bool opaque, int alpha) { |
| 89 | |
| 90 | mState.setPremultipliedAlpha(premultipliedAlpha); |
| 91 | mState.setOpaque(opaque); |
| 92 | mState.setPlaneAlpha(alpha / 255.0f); |
| 93 | |
| 94 | if (alpha < 0xFF || !opaque) { |
| 95 | glEnable(GL_BLEND); |
| 96 | glBlendFunc(premultipliedAlpha ? GL_ONE : GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
| 97 | } else { |
| 98 | glDisable(GL_BLEND); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | void GLES20RenderEngine::setupDimLayerBlending(int alpha) { |
Mathias Agopian | bcab7b1 | 2013-08-23 16:12:34 -0700 | [diff] [blame] | 103 | mState.setPlaneAlpha(1.0f); |
| 104 | mState.setPremultipliedAlpha(true); |
| 105 | mState.setOpaque(false); |
| 106 | mState.setColor(0, 0, 0, alpha/255.0f); |
| 107 | mState.disableTexture(); |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 108 | |
| 109 | if (alpha == 0xFF) { |
| 110 | glDisable(GL_BLEND); |
| 111 | } else { |
| 112 | glEnable(GL_BLEND); |
| 113 | glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); |
| 114 | } |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Mathias Agopian | 49457ac | 2013-08-14 18:20:17 -0700 | [diff] [blame] | 117 | void GLES20RenderEngine::setupLayerTexturing(const Texture& texture) { |
| 118 | GLuint target = texture.getTextureTarget(); |
| 119 | glBindTexture(target, texture.getTextureName()); |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 120 | GLenum filter = GL_NEAREST; |
Mathias Agopian | 49457ac | 2013-08-14 18:20:17 -0700 | [diff] [blame] | 121 | if (texture.getFiltering()) { |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 122 | filter = GL_LINEAR; |
| 123 | } |
Mathias Agopian | 49457ac | 2013-08-14 18:20:17 -0700 | [diff] [blame] | 124 | glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 125 | glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 126 | glTexParameteri(target, GL_TEXTURE_MAG_FILTER, filter); |
| 127 | glTexParameteri(target, GL_TEXTURE_MIN_FILTER, filter); |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 128 | |
Mathias Agopian | 49457ac | 2013-08-14 18:20:17 -0700 | [diff] [blame] | 129 | mState.setTexture(texture); |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | void GLES20RenderEngine::setupLayerBlackedOut() { |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 133 | glBindTexture(GL_TEXTURE_2D, mProtectedTexName); |
Mathias Agopian | 49457ac | 2013-08-14 18:20:17 -0700 | [diff] [blame] | 134 | Texture texture(Texture::TEXTURE_2D, mProtectedTexName); |
| 135 | texture.setDimensions(1, 1); // FIXME: we should get that from somewhere |
| 136 | mState.setTexture(texture); |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | void GLES20RenderEngine::disableTexturing() { |
| 140 | mState.disableTexture(); |
| 141 | } |
| 142 | |
| 143 | void GLES20RenderEngine::disableBlending() { |
| 144 | glDisable(GL_BLEND); |
| 145 | } |
| 146 | |
Mathias Agopian | 458197d | 2013-08-15 14:56:51 -0700 | [diff] [blame] | 147 | |
| 148 | void GLES20RenderEngine::bindImageAsFramebuffer(EGLImageKHR image, |
| 149 | uint32_t* texName, uint32_t* fbName, uint32_t* status) { |
| 150 | GLuint tname, name; |
| 151 | // turn our EGLImage into a texture |
| 152 | glGenTextures(1, &tname); |
| 153 | glBindTexture(GL_TEXTURE_2D, tname); |
| 154 | glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, (GLeglImageOES)image); |
| 155 | |
| 156 | // create a Framebuffer Object to render into |
| 157 | glGenFramebuffers(1, &name); |
| 158 | glBindFramebuffer(GL_FRAMEBUFFER, name); |
| 159 | glFramebufferTexture2D(GL_FRAMEBUFFER, |
| 160 | GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tname, 0); |
| 161 | |
| 162 | *status = glCheckFramebufferStatus(GL_FRAMEBUFFER); |
| 163 | *texName = tname; |
| 164 | *fbName = name; |
| 165 | } |
| 166 | |
| 167 | void GLES20RenderEngine::unbindFramebuffer(uint32_t texName, uint32_t fbName) { |
| 168 | glBindFramebuffer(GL_FRAMEBUFFER, 0); |
| 169 | glDeleteFramebuffers(1, &fbName); |
| 170 | glDeleteTextures(1, &texName); |
| 171 | } |
| 172 | |
Mathias Agopian | 19733a3 | 2013-08-28 18:13:56 -0700 | [diff] [blame] | 173 | void GLES20RenderEngine::setupFillWithColor(float r, float g, float b, float a) { |
| 174 | mState.setPlaneAlpha(1.0f); |
| 175 | mState.setPremultipliedAlpha(true); |
| 176 | mState.setOpaque(false); |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 177 | mState.setColor(r, g, b, a); |
Mathias Agopian | 19733a3 | 2013-08-28 18:13:56 -0700 | [diff] [blame] | 178 | mState.disableTexture(); |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 179 | glDisable(GL_BLEND); |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | void GLES20RenderEngine::drawMesh(const Mesh& mesh) { |
| 183 | |
| 184 | ProgramCache::getInstance().useProgram(mState); |
| 185 | |
| 186 | if (mesh.getTexCoordsSize()) { |
| 187 | glEnableVertexAttribArray(Program::texCoords); |
| 188 | glVertexAttribPointer(Program::texCoords, |
| 189 | mesh.getTexCoordsSize(), |
| 190 | GL_FLOAT, GL_FALSE, |
| 191 | mesh.getByteStride(), |
| 192 | mesh.getTexCoords()); |
| 193 | } |
| 194 | |
| 195 | glVertexAttribPointer(Program::position, |
| 196 | mesh.getVertexSize(), |
| 197 | GL_FLOAT, GL_FALSE, |
| 198 | mesh.getByteStride(), |
Mathias Agopian | 5cdc899 | 2013-08-13 20:51:23 -0700 | [diff] [blame] | 199 | mesh.getPositions()); |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 200 | |
| 201 | glDrawArrays(mesh.getPrimitive(), 0, mesh.getVertexCount()); |
| 202 | |
| 203 | if (mesh.getTexCoordsSize()) { |
| 204 | glDisableVertexAttribArray(Program::texCoords); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | void GLES20RenderEngine::dump(String8& result) { |
Mathias Agopian | 458197d | 2013-08-15 14:56:51 -0700 | [diff] [blame] | 209 | RenderEngine::dump(result); |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | // --------------------------------------------------------------------------- |
| 213 | }; // namespace android |
| 214 | // --------------------------------------------------------------------------- |
Mathias Agopian | 458197d | 2013-08-15 14:56:51 -0700 | [diff] [blame] | 215 | |
| 216 | #if defined(__gl_h_) |
| 217 | #error "don't include gl/gl.h in this file" |
| 218 | #endif |