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