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