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