Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 LOG_TAG "OpenGLRenderer" |
| 18 | |
| 19 | #include "LayerRenderer.h" |
Romain Guy | 1fc883b | 2011-01-12 14:30:59 -0800 | [diff] [blame] | 20 | #include "Properties.h" |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 21 | |
| 22 | namespace android { |
| 23 | namespace uirenderer { |
| 24 | |
| 25 | /////////////////////////////////////////////////////////////////////////////// |
| 26 | // Rendering |
| 27 | /////////////////////////////////////////////////////////////////////////////// |
| 28 | |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 29 | void LayerRenderer::prepareDirty(float left, float top, float right, float bottom, bool opaque) { |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 30 | LAYER_RENDERER_LOGD("Rendering into layer, fbo = %d", mLayer->fbo); |
Romain Guy | 1fc883b | 2011-01-12 14:30:59 -0800 | [diff] [blame] | 31 | |
Romain Guy | c88e357 | 2011-01-22 00:32:12 -0800 | [diff] [blame] | 32 | #if RENDER_LAYERS_AS_REGIONS |
| 33 | mLayer->region.clear(); |
| 34 | #endif |
| 35 | |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 36 | glBindFramebuffer(GL_FRAMEBUFFER, mLayer->fbo); |
Romain Guy | 1fc883b | 2011-01-12 14:30:59 -0800 | [diff] [blame] | 37 | |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 38 | OpenGLRenderer::prepareDirty(0.0f, 0.0f, mLayer->width, mLayer->height, opaque); |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | void LayerRenderer::finish() { |
| 42 | OpenGLRenderer::finish(); |
Romain Guy | 1fc883b | 2011-01-12 14:30:59 -0800 | [diff] [blame] | 43 | |
Romain Guy | f219da5 | 2011-01-16 12:54:25 -0800 | [diff] [blame] | 44 | generateMesh(); |
| 45 | |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 46 | LAYER_RENDERER_LOGD("Finished rendering into layer, fbo = %d", mLayer->mFbo); |
Romain Guy | 42f3a4b | 2011-01-19 13:42:26 -0800 | [diff] [blame] | 47 | |
| 48 | // No need to unbind our FBO, this will be taken care of by the caller |
| 49 | // who will invoke OpenGLRenderer::resume() |
| 50 | } |
| 51 | |
| 52 | GLint LayerRenderer::getTargetFbo() { |
| 53 | return mLayer->fbo; |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | f219da5 | 2011-01-16 12:54:25 -0800 | [diff] [blame] | 57 | // Dirty region tracking |
| 58 | /////////////////////////////////////////////////////////////////////////////// |
| 59 | |
| 60 | bool LayerRenderer::hasLayer() { |
| 61 | return true; |
| 62 | } |
| 63 | |
| 64 | Region* LayerRenderer::getRegion() { |
| 65 | #if RENDER_LAYERS_AS_REGIONS |
| 66 | if (getSnapshot()->flags & Snapshot::kFlagFboTarget) { |
| 67 | return OpenGLRenderer::getRegion(); |
| 68 | } |
| 69 | return &mLayer->region; |
| 70 | #else |
| 71 | return OpenGLRenderer::getRegion(); |
| 72 | #endif |
| 73 | } |
| 74 | |
| 75 | void LayerRenderer::generateMesh() { |
| 76 | #if RENDER_LAYERS_AS_REGIONS |
| 77 | if (mLayer->region.isRect() || mLayer->region.isEmpty()) { |
| 78 | if (mLayer->mesh) { |
| 79 | delete mLayer->mesh; |
| 80 | delete mLayer->meshIndices; |
| 81 | |
| 82 | mLayer->mesh = NULL; |
| 83 | mLayer->meshIndices = NULL; |
| 84 | mLayer->meshElementCount = 0; |
| 85 | } |
| 86 | return; |
| 87 | } |
| 88 | |
| 89 | size_t count; |
| 90 | const android::Rect* rects = mLayer->region.getArray(&count); |
| 91 | |
| 92 | GLsizei elementCount = count * 6; |
| 93 | |
| 94 | if (mLayer->mesh && mLayer->meshElementCount < elementCount) { |
| 95 | delete mLayer->mesh; |
| 96 | delete mLayer->meshIndices; |
| 97 | |
| 98 | mLayer->mesh = NULL; |
| 99 | mLayer->meshIndices = NULL; |
| 100 | } |
| 101 | |
Romain Guy | 4f09f54 | 2011-01-26 22:41:43 -0800 | [diff] [blame^] | 102 | bool rebuildIndices = false; |
Romain Guy | f219da5 | 2011-01-16 12:54:25 -0800 | [diff] [blame] | 103 | if (!mLayer->mesh) { |
| 104 | mLayer->mesh = new TextureVertex[count * 4]; |
| 105 | mLayer->meshIndices = new uint16_t[elementCount]; |
Romain Guy | 4f09f54 | 2011-01-26 22:41:43 -0800 | [diff] [blame^] | 106 | rebuildIndices = true; |
Romain Guy | f219da5 | 2011-01-16 12:54:25 -0800 | [diff] [blame] | 107 | } |
Romain Guy | 4f09f54 | 2011-01-26 22:41:43 -0800 | [diff] [blame^] | 108 | mLayer->meshElementCount = elementCount; |
Romain Guy | f219da5 | 2011-01-16 12:54:25 -0800 | [diff] [blame] | 109 | |
| 110 | const float texX = 1.0f / float(mLayer->width); |
| 111 | const float texY = 1.0f / float(mLayer->height); |
| 112 | const float height = mLayer->layer.getHeight(); |
| 113 | |
| 114 | TextureVertex* mesh = mLayer->mesh; |
| 115 | uint16_t* indices = mLayer->meshIndices; |
| 116 | |
| 117 | for (size_t i = 0; i < count; i++) { |
| 118 | const android::Rect* r = &rects[i]; |
| 119 | |
| 120 | const float u1 = r->left * texX; |
| 121 | const float v1 = (height - r->top) * texY; |
| 122 | const float u2 = r->right * texX; |
| 123 | const float v2 = (height - r->bottom) * texY; |
| 124 | |
| 125 | TextureVertex::set(mesh++, r->left, r->top, u1, v1); |
| 126 | TextureVertex::set(mesh++, r->right, r->top, u2, v1); |
| 127 | TextureVertex::set(mesh++, r->left, r->bottom, u1, v2); |
| 128 | TextureVertex::set(mesh++, r->right, r->bottom, u2, v2); |
| 129 | |
Romain Guy | 4f09f54 | 2011-01-26 22:41:43 -0800 | [diff] [blame^] | 130 | if (rebuildIndices) { |
| 131 | uint16_t quad = i * 4; |
| 132 | int index = i * 6; |
| 133 | indices[index ] = quad; // top-left |
| 134 | indices[index + 1] = quad + 1; // top-right |
| 135 | indices[index + 2] = quad + 2; // bottom-left |
| 136 | indices[index + 3] = quad + 2; // bottom-left |
| 137 | indices[index + 4] = quad + 1; // top-right |
| 138 | indices[index + 5] = quad + 3; // bottom-right |
| 139 | } |
Romain Guy | f219da5 | 2011-01-16 12:54:25 -0800 | [diff] [blame] | 140 | } |
| 141 | #endif |
| 142 | } |
| 143 | |
| 144 | /////////////////////////////////////////////////////////////////////////////// |
| 145 | // Layers management |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 146 | /////////////////////////////////////////////////////////////////////////////// |
| 147 | |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 148 | Layer* LayerRenderer::createLayer(uint32_t width, uint32_t height, bool isOpaque) { |
Romain Guy | 1fc883b | 2011-01-12 14:30:59 -0800 | [diff] [blame] | 149 | LAYER_RENDERER_LOGD("Creating new layer %dx%d", width, height); |
| 150 | |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 151 | Layer* layer = new Layer(width, height); |
| 152 | |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 153 | GLuint previousFbo; |
| 154 | glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint*) &previousFbo); |
| 155 | |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 156 | glGenFramebuffers(1, &layer->fbo); |
| 157 | glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo); |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 158 | |
Romain Guy | 1fc883b | 2011-01-12 14:30:59 -0800 | [diff] [blame] | 159 | if (glGetError() != GL_NO_ERROR) { |
| 160 | glBindFramebuffer(GL_FRAMEBUFFER, previousFbo); |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 161 | glDeleteBuffers(1, &layer->fbo); |
Romain Guy | 1fc883b | 2011-01-12 14:30:59 -0800 | [diff] [blame] | 162 | return 0; |
| 163 | } |
| 164 | |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 165 | glActiveTexture(GL_TEXTURE0); |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 166 | glGenTextures(1, &layer->texture); |
| 167 | glBindTexture(GL_TEXTURE_2D, layer->texture); |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 168 | |
| 169 | glPixelStorei(GL_UNPACK_ALIGNMENT, 4); |
| 170 | |
| 171 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 172 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 173 | |
| 174 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 175 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 176 | |
| 177 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, |
| 178 | GL_RGBA, GL_UNSIGNED_BYTE, NULL); |
| 179 | |
| 180 | if (glGetError() != GL_NO_ERROR) { |
Romain Guy | 1fc883b | 2011-01-12 14:30:59 -0800 | [diff] [blame] | 181 | glBindFramebuffer(GL_FRAMEBUFFER, previousFbo); |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 182 | glDeleteBuffers(1, &layer->fbo); |
| 183 | glDeleteTextures(1, &layer->texture); |
| 184 | delete layer; |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 185 | return 0; |
| 186 | } |
| 187 | |
| 188 | glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 189 | layer->texture, 0); |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 190 | |
| 191 | if (glGetError() != GL_NO_ERROR) { |
Romain Guy | 1fc883b | 2011-01-12 14:30:59 -0800 | [diff] [blame] | 192 | glBindFramebuffer(GL_FRAMEBUFFER, previousFbo); |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 193 | glDeleteBuffers(1, &layer->fbo); |
| 194 | glDeleteTextures(1, &layer->texture); |
| 195 | delete layer; |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 196 | return 0; |
| 197 | } |
| 198 | |
| 199 | glBindFramebuffer(GL_FRAMEBUFFER, previousFbo); |
| 200 | |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 201 | layer->layer.set(0.0f, 0.0f, width, height); |
| 202 | layer->texCoords.set(0.0f, 1.0f, 1.0f, 0.0f); |
| 203 | layer->alpha = 255; |
| 204 | layer->mode = SkXfermode::kSrcOver_Mode; |
| 205 | layer->blend = !isOpaque; |
| 206 | layer->empty = false; |
| 207 | layer->colorFilter = NULL; |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 208 | |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 209 | return layer; |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 210 | } |
| 211 | |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 212 | bool LayerRenderer::resizeLayer(Layer* layer, uint32_t width, uint32_t height) { |
| 213 | if (layer) { |
| 214 | LAYER_RENDERER_LOGD("Resizing layer fbo = %d to %dx%d", layer->fbo, width, height); |
Romain Guy | 1fc883b | 2011-01-12 14:30:59 -0800 | [diff] [blame] | 215 | |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 216 | glActiveTexture(GL_TEXTURE0); |
| 217 | glBindTexture(GL_TEXTURE_2D, layer->texture); |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 218 | |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 219 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, |
| 220 | GL_RGBA, GL_UNSIGNED_BYTE, NULL); |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 221 | |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 222 | if (glGetError() != GL_NO_ERROR) { |
| 223 | glDeleteBuffers(1, &layer->fbo); |
| 224 | glDeleteTextures(1, &layer->texture); |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 225 | |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 226 | layer->width = 0; |
| 227 | layer->height = 0; |
| 228 | layer->fbo = 0; |
| 229 | layer->texture = 0; |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 230 | |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 231 | return false; |
| 232 | } |
| 233 | |
| 234 | layer->width = width; |
| 235 | layer->height = height; |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 236 | } |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 237 | return true; |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 238 | } |
| 239 | |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 240 | void LayerRenderer::destroyLayer(Layer* layer) { |
| 241 | if (layer) { |
| 242 | LAYER_RENDERER_LOGD("Destroying layer, fbo = %d", layer->fbo); |
Romain Guy | 1fc883b | 2011-01-12 14:30:59 -0800 | [diff] [blame] | 243 | |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 244 | if (layer->fbo) glDeleteFramebuffers(1, &layer->fbo); |
| 245 | if (layer->texture) glDeleteTextures(1, &layer->texture); |
| 246 | |
| 247 | delete layer; |
| 248 | } |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 249 | } |
| 250 | |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 251 | void LayerRenderer::destroyLayerDeferred(Layer* layer) { |
| 252 | if (layer) { |
| 253 | LAYER_RENDERER_LOGD("Deferring layer destruction, fbo = %d", layer->fbo); |
Romain Guy | 1fc883b | 2011-01-12 14:30:59 -0800 | [diff] [blame] | 254 | |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 255 | Caches::getInstance().deleteLayerDeferred(layer); |
| 256 | } |
Romain Guy | 57066eb | 2011-01-12 12:53:32 -0800 | [diff] [blame] | 257 | } |
| 258 | |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 259 | }; // namespace uirenderer |
| 260 | }; // namespace android |