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