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 | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 35 | LAYER_RENDERER_LOGD("Rendering into layer, fbo = %d", mLayer->getFbo()); |
Romain Guy | 1fc883b | 2011-01-12 14:30:59 -0800 | [diff] [blame] | 36 | |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 37 | glBindFramebuffer(GL_FRAMEBUFFER, mLayer->getFbo()); |
Romain Guy | 62687ec | 2011-02-02 15:44:19 -0800 | [diff] [blame] | 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 | |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 65 | LAYER_RENDERER_LOGD("Finished rendering into layer, fbo = %d", mLayer->getFbo()); |
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() { |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 72 | return mLayer->getFbo(); |
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 | |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 131 | const float texX = 1.0f / float(mLayer->getWidth()); |
| 132 | const float texY = 1.0f / float(mLayer->getHeight()); |
Romain Guy | f219da5 | 2011-01-16 12:54:25 -0800 | [diff] [blame] | 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 | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 169 | Layer* LayerRenderer::createLayer(uint32_t width, uint32_t height, bool isOpaque) { |
Romain Guy | eea6069 | 2011-07-26 20:35:55 -0700 | [diff] [blame] | 170 | LAYER_RENDERER_LOGD("Requesting new render layer %dx%d", width, height); |
Romain Guy | 1fc883b | 2011-01-12 14:30:59 -0800 | [diff] [blame] | 171 | |
Romain Guy | 09b7c91 | 2011-02-02 20:28:09 -0800 | [diff] [blame] | 172 | GLuint fbo = Caches::getInstance().fboCache.get(); |
| 173 | if (!fbo) { |
| 174 | LOGW("Could not obtain an FBO"); |
| 175 | return NULL; |
| 176 | } |
| 177 | |
| 178 | glActiveTexture(GL_TEXTURE0); |
| 179 | Layer* layer = Caches::getInstance().layerCache.get(width, height); |
| 180 | if (!layer) { |
| 181 | LOGW("Could not obtain a layer"); |
| 182 | return NULL; |
| 183 | } |
| 184 | |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 185 | layer->setFbo(fbo); |
Romain Guy | 09b7c91 | 2011-02-02 20:28:09 -0800 | [diff] [blame] | 186 | layer->layer.set(0.0f, 0.0f, width, height); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 187 | layer->texCoords.set(0.0f, height / float(layer->getHeight()), |
| 188 | width / float(layer->getWidth()), 0.0f); |
| 189 | layer->setAlpha(255, SkXfermode::kSrcOver_Mode); |
| 190 | layer->setBlend(!isOpaque); |
| 191 | layer->setColorFilter(NULL); |
Romain Guy | 09b7c91 | 2011-02-02 20:28:09 -0800 | [diff] [blame] | 192 | layer->region.clear(); |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 193 | |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 194 | GLuint previousFbo; |
| 195 | glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint*) &previousFbo); |
| 196 | |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 197 | glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo()); |
| 198 | layer->bindTexture(); |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 199 | |
Romain Guy | 09b7c91 | 2011-02-02 20:28:09 -0800 | [diff] [blame] | 200 | // Initialize the texture if needed |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 201 | if (layer->isEmpty()) { |
| 202 | layer->setEmpty(false); |
| 203 | layer->allocateTexture(GL_RGBA, GL_UNSIGNED_BYTE); |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 204 | |
Romain Guy | 09b7c91 | 2011-02-02 20:28:09 -0800 | [diff] [blame] | 205 | if (glGetError() != GL_NO_ERROR) { |
| 206 | LOGD("Could not allocate texture"); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 207 | |
Romain Guy | 09b7c91 | 2011-02-02 20:28:09 -0800 | [diff] [blame] | 208 | glBindFramebuffer(GL_FRAMEBUFFER, previousFbo); |
Romain Guy | 09b7c91 | 2011-02-02 20:28:09 -0800 | [diff] [blame] | 209 | Caches::getInstance().fboCache.put(fbo); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 210 | |
| 211 | layer->deleteTexture(); |
Romain Guy | 09b7c91 | 2011-02-02 20:28:09 -0800 | [diff] [blame] | 212 | delete layer; |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 213 | |
Romain Guy | 09b7c91 | 2011-02-02 20:28:09 -0800 | [diff] [blame] | 214 | return NULL; |
| 215 | } |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 219 | layer->getTexture(), 0); |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 220 | |
Romain Guy | 40a787f | 2011-03-02 15:15:42 -0800 | [diff] [blame] | 221 | glDisable(GL_SCISSOR_TEST); |
| 222 | glClearColor(0.0f, 0.0f, 0.0f, 0.0f); |
| 223 | glClear(GL_COLOR_BUFFER_BIT); |
| 224 | glEnable(GL_SCISSOR_TEST); |
| 225 | |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 226 | glBindFramebuffer(GL_FRAMEBUFFER, previousFbo); |
| 227 | |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 228 | return layer; |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 229 | } |
| 230 | |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 231 | bool LayerRenderer::resizeLayer(Layer* layer, uint32_t width, uint32_t height) { |
| 232 | if (layer) { |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 233 | LAYER_RENDERER_LOGD("Resizing layer fbo = %d to %dx%d", layer->getFbo(), width, height); |
Romain Guy | 1fc883b | 2011-01-12 14:30:59 -0800 | [diff] [blame] | 234 | |
Romain Guy | 09b7c91 | 2011-02-02 20:28:09 -0800 | [diff] [blame] | 235 | if (Caches::getInstance().layerCache.resize(layer, width, height)) { |
| 236 | layer->layer.set(0.0f, 0.0f, width, height); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 237 | layer->texCoords.set(0.0f, height / float(layer->getHeight()), |
| 238 | width / float(layer->getWidth()), 0.0f); |
Romain Guy | 09b7c91 | 2011-02-02 20:28:09 -0800 | [diff] [blame] | 239 | } else { |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 240 | layer->deleteTexture(); |
Romain Guy | 09b7c91 | 2011-02-02 20:28:09 -0800 | [diff] [blame] | 241 | delete layer; |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 242 | return false; |
| 243 | } |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 244 | } |
Romain Guy | 09b7c91 | 2011-02-02 20:28:09 -0800 | [diff] [blame] | 245 | |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 246 | return true; |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 247 | } |
| 248 | |
Romain Guy | 4a5a715 | 2011-06-24 17:53:53 -0700 | [diff] [blame] | 249 | Layer* LayerRenderer::createTextureLayer(bool isOpaque) { |
| 250 | LAYER_RENDERER_LOGD("Creating new texture layer"); |
| 251 | |
| 252 | Layer* layer = new Layer(0, 0); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 253 | layer->setCacheable(false); |
| 254 | layer->setTextureLayer(true); |
| 255 | layer->setBlend(!isOpaque); |
| 256 | layer->setEmpty(true); |
| 257 | layer->setFbo(0); |
| 258 | layer->setAlpha(255, SkXfermode::kSrcOver_Mode); |
Romain Guy | 4a5a715 | 2011-06-24 17:53:53 -0700 | [diff] [blame] | 259 | layer->layer.set(0.0f, 0.0f, 0.0f, 0.0f); |
| 260 | layer->texCoords.set(0.0f, 1.0f, 0.0f, 1.0f); |
Romain Guy | 4a5a715 | 2011-06-24 17:53:53 -0700 | [diff] [blame] | 261 | layer->region.clear(); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 262 | layer->setRenderTarget(GL_NONE); // see ::updateTextureLayer() |
Romain Guy | 4a5a715 | 2011-06-24 17:53:53 -0700 | [diff] [blame] | 263 | |
| 264 | glActiveTexture(GL_TEXTURE0); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 265 | layer->generateTexture(); |
Romain Guy | 4a5a715 | 2011-06-24 17:53:53 -0700 | [diff] [blame] | 266 | |
| 267 | return layer; |
| 268 | } |
| 269 | |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 270 | void LayerRenderer::updateTextureLayer(Layer* layer, uint32_t width, uint32_t height, |
Romain Guy | a948927 | 2011-06-22 20:58:11 -0700 | [diff] [blame] | 271 | bool isOpaque, GLenum renderTarget, float* transform) { |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 272 | if (layer) { |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 273 | layer->setBlend(!isOpaque); |
| 274 | layer->setSize(width, height); |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 275 | layer->layer.set(0.0f, 0.0f, width, height); |
| 276 | layer->region.set(width, height); |
| 277 | layer->regionRect.set(0.0f, 0.0f, width, height); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 278 | layer->getTexTransform().load(transform); |
Romain Guy | 8f0095c | 2011-05-02 17:24:22 -0700 | [diff] [blame] | 279 | |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 280 | if (renderTarget != layer->getRenderTarget()) { |
| 281 | layer->setRenderTarget(renderTarget); |
| 282 | layer->bindTexture(); |
| 283 | layer->setFilter(GL_NEAREST, GL_NEAREST, false, true); |
| 284 | layer->setWrap(GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, false, true); |
Romain Guy | 4a5a715 | 2011-06-24 17:53:53 -0700 | [diff] [blame] | 285 | } |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 286 | } |
| 287 | } |
| 288 | |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 289 | void LayerRenderer::destroyLayer(Layer* layer) { |
| 290 | if (layer) { |
Romain Guy | eea6069 | 2011-07-26 20:35:55 -0700 | [diff] [blame] | 291 | LAYER_RENDERER_LOGD("Recycling layer, %dx%d fbo = %d", |
| 292 | layer->getWidth(), layer->getHeight(), layer->getFbo()); |
Romain Guy | 1fc883b | 2011-01-12 14:30:59 -0800 | [diff] [blame] | 293 | |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 294 | if (layer->getFbo()) { |
| 295 | Caches::getInstance().fboCache.put(layer->getFbo()); |
Romain Guy | 09b7c91 | 2011-02-02 20:28:09 -0800 | [diff] [blame] | 296 | } |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 297 | |
Romain Guy | 09b7c91 | 2011-02-02 20:28:09 -0800 | [diff] [blame] | 298 | if (!Caches::getInstance().layerCache.put(layer)) { |
Romain Guy | eea6069 | 2011-07-26 20:35:55 -0700 | [diff] [blame] | 299 | LAYER_RENDERER_LOGD(" Destroyed!"); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 300 | layer->deleteTexture(); |
Romain Guy | 09b7c91 | 2011-02-02 20:28:09 -0800 | [diff] [blame] | 301 | delete layer; |
| 302 | } else { |
Romain Guy | eea6069 | 2011-07-26 20:35:55 -0700 | [diff] [blame] | 303 | LAYER_RENDERER_LOGD(" Cached!"); |
Romain Guy | 65b345f | 2011-07-27 18:51:50 -0700 | [diff] [blame^] | 304 | #if DEBUG_LAYER_RENDERER |
Romain Guy | eea6069 | 2011-07-26 20:35:55 -0700 | [diff] [blame] | 305 | Caches::getInstance().layerCache.dump(); |
| 306 | #endif |
Romain Guy | 09b7c91 | 2011-02-02 20:28:09 -0800 | [diff] [blame] | 307 | layer->region.clear(); |
| 308 | } |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 309 | } |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 310 | } |
| 311 | |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 312 | void LayerRenderer::destroyLayerDeferred(Layer* layer) { |
| 313 | if (layer) { |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 314 | LAYER_RENDERER_LOGD("Deferring layer destruction, fbo = %d", layer->getFbo()); |
Romain Guy | 1fc883b | 2011-01-12 14:30:59 -0800 | [diff] [blame] | 315 | |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 316 | Caches::getInstance().deleteLayerDeferred(layer); |
| 317 | } |
Romain Guy | 57066eb | 2011-01-12 12:53:32 -0800 | [diff] [blame] | 318 | } |
| 319 | |
Romain Guy | 77a8116 | 2011-06-14 16:45:55 -0700 | [diff] [blame] | 320 | bool LayerRenderer::copyLayer(Layer* layer, SkBitmap* bitmap) { |
| 321 | Caches& caches = Caches::getInstance(); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 322 | if (layer && layer->isTextureLayer() && bitmap->width() <= caches.maxTextureSize && |
Romain Guy | 77a8116 | 2011-06-14 16:45:55 -0700 | [diff] [blame] | 323 | bitmap->height() <= caches.maxTextureSize) { |
| 324 | |
| 325 | GLuint fbo = caches.fboCache.get(); |
| 326 | if (!fbo) { |
| 327 | LOGW("Could not obtain an FBO"); |
| 328 | return false; |
| 329 | } |
| 330 | |
Romain Guy | d6b2a00 | 2011-06-17 17:45:59 -0700 | [diff] [blame] | 331 | SkAutoLockPixels alp(*bitmap); |
| 332 | |
Romain Guy | 77a8116 | 2011-06-14 16:45:55 -0700 | [diff] [blame] | 333 | GLuint texture; |
| 334 | GLuint previousFbo; |
| 335 | |
| 336 | GLenum format; |
| 337 | GLenum type; |
| 338 | |
Romain Guy | d6b2a00 | 2011-06-17 17:45:59 -0700 | [diff] [blame] | 339 | GLenum error = GL_NO_ERROR; |
| 340 | bool status = false; |
| 341 | |
Romain Guy | 77a8116 | 2011-06-14 16:45:55 -0700 | [diff] [blame] | 342 | switch (bitmap->config()) { |
| 343 | case SkBitmap::kA8_Config: |
| 344 | format = GL_ALPHA; |
| 345 | type = GL_UNSIGNED_BYTE; |
| 346 | break; |
| 347 | case SkBitmap::kRGB_565_Config: |
| 348 | format = GL_RGB; |
| 349 | type = GL_UNSIGNED_SHORT_5_6_5; |
| 350 | break; |
| 351 | case SkBitmap::kARGB_4444_Config: |
| 352 | format = GL_RGBA; |
| 353 | type = GL_UNSIGNED_SHORT_4_4_4_4; |
| 354 | break; |
| 355 | case SkBitmap::kARGB_8888_Config: |
| 356 | default: |
| 357 | format = GL_RGBA; |
| 358 | type = GL_UNSIGNED_BYTE; |
| 359 | break; |
| 360 | } |
| 361 | |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 362 | float alpha = layer->getAlpha(); |
| 363 | SkXfermode::Mode mode = layer->getMode(); |
Romain Guy | d6b2a00 | 2011-06-17 17:45:59 -0700 | [diff] [blame] | 364 | |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 365 | layer->setAlpha(255, SkXfermode::kSrc_Mode); |
| 366 | layer->setFbo(fbo); |
Romain Guy | d6b2a00 | 2011-06-17 17:45:59 -0700 | [diff] [blame] | 367 | |
Romain Guy | 77a8116 | 2011-06-14 16:45:55 -0700 | [diff] [blame] | 368 | glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint*) &previousFbo); |
| 369 | glBindFramebuffer(GL_FRAMEBUFFER, fbo); |
| 370 | |
| 371 | glGenTextures(1, &texture); |
Romain Guy | d6b2a00 | 2011-06-17 17:45:59 -0700 | [diff] [blame] | 372 | if ((error = glGetError()) != GL_NO_ERROR) goto error; |
Romain Guy | 77a8116 | 2011-06-14 16:45:55 -0700 | [diff] [blame] | 373 | |
| 374 | glActiveTexture(GL_TEXTURE0); |
| 375 | glBindTexture(GL_TEXTURE_2D, texture); |
| 376 | |
Romain Guy | ec19b4a | 2011-07-07 21:05:04 -0700 | [diff] [blame] | 377 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 378 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
Romain Guy | 77a8116 | 2011-06-14 16:45:55 -0700 | [diff] [blame] | 379 | |
| 380 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 381 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 382 | |
| 383 | glTexImage2D(GL_TEXTURE_2D, 0, format, bitmap->width(), bitmap->height(), |
| 384 | 0, format, type, NULL); |
Romain Guy | d6b2a00 | 2011-06-17 17:45:59 -0700 | [diff] [blame] | 385 | if ((error = glGetError()) != GL_NO_ERROR) goto error; |
| 386 | |
Romain Guy | 77a8116 | 2011-06-14 16:45:55 -0700 | [diff] [blame] | 387 | glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, |
| 388 | GL_TEXTURE_2D, texture, 0); |
Romain Guy | d6b2a00 | 2011-06-17 17:45:59 -0700 | [diff] [blame] | 389 | if ((error = glGetError()) != GL_NO_ERROR) goto error; |
Romain Guy | 77a8116 | 2011-06-14 16:45:55 -0700 | [diff] [blame] | 390 | |
Romain Guy | d6b2a00 | 2011-06-17 17:45:59 -0700 | [diff] [blame] | 391 | { |
| 392 | LayerRenderer renderer(layer); |
| 393 | renderer.setViewport(bitmap->width(), bitmap->height()); |
| 394 | renderer.OpenGLRenderer::prepareDirty(0.0f, 0.0f, |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 395 | bitmap->width(), bitmap->height(), !layer->isBlend()); |
Romain Guy | d6b2a00 | 2011-06-17 17:45:59 -0700 | [diff] [blame] | 396 | if ((error = glGetError()) != GL_NO_ERROR) goto error; |
Romain Guy | 77a8116 | 2011-06-14 16:45:55 -0700 | [diff] [blame] | 397 | |
Romain Guy | d6b2a00 | 2011-06-17 17:45:59 -0700 | [diff] [blame] | 398 | { |
| 399 | Rect bounds; |
| 400 | bounds.set(0.0f, 0.0f, bitmap->width(), bitmap->height()); |
| 401 | renderer.drawTextureLayer(layer, bounds); |
Romain Guy | 77a8116 | 2011-06-14 16:45:55 -0700 | [diff] [blame] | 402 | |
Romain Guy | d6b2a00 | 2011-06-17 17:45:59 -0700 | [diff] [blame] | 403 | glReadPixels(0, 0, bitmap->width(), bitmap->height(), format, |
| 404 | type, bitmap->getPixels()); |
Romain Guy | 77a8116 | 2011-06-14 16:45:55 -0700 | [diff] [blame] | 405 | |
Romain Guy | d6b2a00 | 2011-06-17 17:45:59 -0700 | [diff] [blame] | 406 | if ((error = glGetError()) != GL_NO_ERROR) goto error; |
| 407 | } |
Romain Guy | 77a8116 | 2011-06-14 16:45:55 -0700 | [diff] [blame] | 408 | |
Romain Guy | d6b2a00 | 2011-06-17 17:45:59 -0700 | [diff] [blame] | 409 | status = true; |
| 410 | } |
Romain Guy | 77a8116 | 2011-06-14 16:45:55 -0700 | [diff] [blame] | 411 | |
Romain Guy | d6b2a00 | 2011-06-17 17:45:59 -0700 | [diff] [blame] | 412 | error: |
| 413 | #if DEBUG_OPENGL |
| 414 | if (error != GL_NO_ERROR) { |
| 415 | LOGD("GL error while copying layer into bitmap = 0x%x", error); |
| 416 | } |
| 417 | #endif |
Romain Guy | 77a8116 | 2011-06-14 16:45:55 -0700 | [diff] [blame] | 418 | |
| 419 | glBindFramebuffer(GL_FRAMEBUFFER, previousFbo); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 420 | layer->setAlpha(alpha, mode); |
| 421 | layer->setFbo(0); |
Romain Guy | 77a8116 | 2011-06-14 16:45:55 -0700 | [diff] [blame] | 422 | glDeleteTextures(1, &texture); |
| 423 | caches.fboCache.put(fbo); |
| 424 | |
Romain Guy | d6b2a00 | 2011-06-17 17:45:59 -0700 | [diff] [blame] | 425 | return status; |
Romain Guy | 77a8116 | 2011-06-14 16:45:55 -0700 | [diff] [blame] | 426 | } |
| 427 | return false; |
| 428 | } |
| 429 | |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 430 | }; // namespace uirenderer |
| 431 | }; // namespace android |