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 | |
Romain Guy | 8a3957d | 2011-09-07 17:55:15 -0700 | [diff] [blame] | 94 | // TODO: This implementation is flawed and can generate T-junctions |
| 95 | // in the mesh, which will in turn produce cracks when the |
| 96 | // mesh is rotated/skewed. The easiest way to fix this would |
| 97 | // be, for each row, to add new vertices shared with the previous |
| 98 | // row when the two rows share an edge. |
| 99 | // In practice, T-junctions do not appear often so this has yet |
| 100 | // to be fixed. |
Romain Guy | f219da5 | 2011-01-16 12:54:25 -0800 | [diff] [blame] | 101 | void LayerRenderer::generateMesh() { |
| 102 | #if RENDER_LAYERS_AS_REGIONS |
| 103 | if (mLayer->region.isRect() || mLayer->region.isEmpty()) { |
| 104 | if (mLayer->mesh) { |
| 105 | delete mLayer->mesh; |
| 106 | delete mLayer->meshIndices; |
| 107 | |
| 108 | mLayer->mesh = NULL; |
| 109 | mLayer->meshIndices = NULL; |
| 110 | mLayer->meshElementCount = 0; |
| 111 | } |
Romain Guy | 4066767 | 2011-03-18 14:34:03 -0700 | [diff] [blame] | 112 | |
Romain Guy | 9fc2781 | 2011-04-27 14:21:41 -0700 | [diff] [blame] | 113 | mLayer->setRegionAsRect(); |
Romain Guy | f219da5 | 2011-01-16 12:54:25 -0800 | [diff] [blame] | 114 | return; |
| 115 | } |
| 116 | |
| 117 | size_t count; |
| 118 | const android::Rect* rects = mLayer->region.getArray(&count); |
| 119 | |
| 120 | GLsizei elementCount = count * 6; |
| 121 | |
| 122 | if (mLayer->mesh && mLayer->meshElementCount < elementCount) { |
| 123 | delete mLayer->mesh; |
| 124 | delete mLayer->meshIndices; |
| 125 | |
| 126 | mLayer->mesh = NULL; |
| 127 | mLayer->meshIndices = NULL; |
| 128 | } |
| 129 | |
Romain Guy | 4f09f54 | 2011-01-26 22:41:43 -0800 | [diff] [blame] | 130 | bool rebuildIndices = false; |
Romain Guy | f219da5 | 2011-01-16 12:54:25 -0800 | [diff] [blame] | 131 | if (!mLayer->mesh) { |
| 132 | mLayer->mesh = new TextureVertex[count * 4]; |
| 133 | mLayer->meshIndices = new uint16_t[elementCount]; |
Romain Guy | 4f09f54 | 2011-01-26 22:41:43 -0800 | [diff] [blame] | 134 | rebuildIndices = true; |
Romain Guy | f219da5 | 2011-01-16 12:54:25 -0800 | [diff] [blame] | 135 | } |
Romain Guy | 4f09f54 | 2011-01-26 22:41:43 -0800 | [diff] [blame] | 136 | mLayer->meshElementCount = elementCount; |
Romain Guy | f219da5 | 2011-01-16 12:54:25 -0800 | [diff] [blame] | 137 | |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 138 | const float texX = 1.0f / float(mLayer->getWidth()); |
| 139 | const float texY = 1.0f / float(mLayer->getHeight()); |
Romain Guy | f219da5 | 2011-01-16 12:54:25 -0800 | [diff] [blame] | 140 | const float height = mLayer->layer.getHeight(); |
| 141 | |
| 142 | TextureVertex* mesh = mLayer->mesh; |
| 143 | uint16_t* indices = mLayer->meshIndices; |
| 144 | |
| 145 | for (size_t i = 0; i < count; i++) { |
| 146 | const android::Rect* r = &rects[i]; |
| 147 | |
| 148 | const float u1 = r->left * texX; |
| 149 | const float v1 = (height - r->top) * texY; |
| 150 | const float u2 = r->right * texX; |
| 151 | const float v2 = (height - r->bottom) * texY; |
| 152 | |
| 153 | TextureVertex::set(mesh++, r->left, r->top, u1, v1); |
| 154 | TextureVertex::set(mesh++, r->right, r->top, u2, v1); |
| 155 | TextureVertex::set(mesh++, r->left, r->bottom, u1, v2); |
| 156 | TextureVertex::set(mesh++, r->right, r->bottom, u2, v2); |
| 157 | |
Romain Guy | 4f09f54 | 2011-01-26 22:41:43 -0800 | [diff] [blame] | 158 | if (rebuildIndices) { |
| 159 | uint16_t quad = i * 4; |
| 160 | int index = i * 6; |
| 161 | indices[index ] = quad; // top-left |
| 162 | indices[index + 1] = quad + 1; // top-right |
| 163 | indices[index + 2] = quad + 2; // bottom-left |
| 164 | indices[index + 3] = quad + 2; // bottom-left |
| 165 | indices[index + 4] = quad + 1; // top-right |
| 166 | indices[index + 5] = quad + 3; // bottom-right |
| 167 | } |
Romain Guy | f219da5 | 2011-01-16 12:54:25 -0800 | [diff] [blame] | 168 | } |
| 169 | #endif |
| 170 | } |
| 171 | |
| 172 | /////////////////////////////////////////////////////////////////////////////// |
| 173 | // Layers management |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 174 | /////////////////////////////////////////////////////////////////////////////// |
| 175 | |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 176 | Layer* LayerRenderer::createLayer(uint32_t width, uint32_t height, bool isOpaque) { |
Romain Guy | eea6069 | 2011-07-26 20:35:55 -0700 | [diff] [blame] | 177 | LAYER_RENDERER_LOGD("Requesting new render layer %dx%d", width, height); |
Romain Guy | 1fc883b | 2011-01-12 14:30:59 -0800 | [diff] [blame] | 178 | |
Romain Guy | 09b7c91 | 2011-02-02 20:28:09 -0800 | [diff] [blame] | 179 | GLuint fbo = Caches::getInstance().fboCache.get(); |
| 180 | if (!fbo) { |
| 181 | LOGW("Could not obtain an FBO"); |
| 182 | return NULL; |
| 183 | } |
| 184 | |
| 185 | glActiveTexture(GL_TEXTURE0); |
| 186 | Layer* layer = Caches::getInstance().layerCache.get(width, height); |
| 187 | if (!layer) { |
| 188 | LOGW("Could not obtain a layer"); |
| 189 | return NULL; |
| 190 | } |
| 191 | |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 192 | layer->setFbo(fbo); |
Romain Guy | 09b7c91 | 2011-02-02 20:28:09 -0800 | [diff] [blame] | 193 | layer->layer.set(0.0f, 0.0f, width, height); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 194 | layer->texCoords.set(0.0f, height / float(layer->getHeight()), |
| 195 | width / float(layer->getWidth()), 0.0f); |
| 196 | layer->setAlpha(255, SkXfermode::kSrcOver_Mode); |
| 197 | layer->setBlend(!isOpaque); |
| 198 | layer->setColorFilter(NULL); |
Romain Guy | 09b7c91 | 2011-02-02 20:28:09 -0800 | [diff] [blame] | 199 | layer->region.clear(); |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 200 | |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 201 | GLuint previousFbo; |
| 202 | glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint*) &previousFbo); |
| 203 | |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 204 | glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo()); |
| 205 | layer->bindTexture(); |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 206 | |
Romain Guy | 09b7c91 | 2011-02-02 20:28:09 -0800 | [diff] [blame] | 207 | // Initialize the texture if needed |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 208 | if (layer->isEmpty()) { |
| 209 | layer->setEmpty(false); |
| 210 | layer->allocateTexture(GL_RGBA, GL_UNSIGNED_BYTE); |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 211 | |
Romain Guy | 09b7c91 | 2011-02-02 20:28:09 -0800 | [diff] [blame] | 212 | if (glGetError() != GL_NO_ERROR) { |
| 213 | LOGD("Could not allocate texture"); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 214 | |
Romain Guy | 09b7c91 | 2011-02-02 20:28:09 -0800 | [diff] [blame] | 215 | glBindFramebuffer(GL_FRAMEBUFFER, previousFbo); |
Romain Guy | 09b7c91 | 2011-02-02 20:28:09 -0800 | [diff] [blame] | 216 | Caches::getInstance().fboCache.put(fbo); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 217 | |
| 218 | layer->deleteTexture(); |
Romain Guy | 09b7c91 | 2011-02-02 20:28:09 -0800 | [diff] [blame] | 219 | delete layer; |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 220 | |
Romain Guy | 09b7c91 | 2011-02-02 20:28:09 -0800 | [diff] [blame] | 221 | return NULL; |
| 222 | } |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 226 | layer->getTexture(), 0); |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 227 | |
Romain Guy | 40a787f | 2011-03-02 15:15:42 -0800 | [diff] [blame] | 228 | glDisable(GL_SCISSOR_TEST); |
| 229 | glClearColor(0.0f, 0.0f, 0.0f, 0.0f); |
| 230 | glClear(GL_COLOR_BUFFER_BIT); |
| 231 | glEnable(GL_SCISSOR_TEST); |
| 232 | |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 233 | glBindFramebuffer(GL_FRAMEBUFFER, previousFbo); |
| 234 | |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 235 | return layer; |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 236 | } |
| 237 | |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 238 | bool LayerRenderer::resizeLayer(Layer* layer, uint32_t width, uint32_t height) { |
| 239 | if (layer) { |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 240 | 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] | 241 | |
Romain Guy | 09b7c91 | 2011-02-02 20:28:09 -0800 | [diff] [blame] | 242 | if (Caches::getInstance().layerCache.resize(layer, width, height)) { |
| 243 | layer->layer.set(0.0f, 0.0f, width, height); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 244 | layer->texCoords.set(0.0f, height / float(layer->getHeight()), |
| 245 | width / float(layer->getWidth()), 0.0f); |
Romain Guy | 09b7c91 | 2011-02-02 20:28:09 -0800 | [diff] [blame] | 246 | } else { |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 247 | layer->deleteTexture(); |
Romain Guy | 09b7c91 | 2011-02-02 20:28:09 -0800 | [diff] [blame] | 248 | delete layer; |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 249 | return false; |
| 250 | } |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 251 | } |
Romain Guy | 09b7c91 | 2011-02-02 20:28:09 -0800 | [diff] [blame] | 252 | |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 253 | return true; |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 254 | } |
| 255 | |
Romain Guy | 4a5a715 | 2011-06-24 17:53:53 -0700 | [diff] [blame] | 256 | Layer* LayerRenderer::createTextureLayer(bool isOpaque) { |
| 257 | LAYER_RENDERER_LOGD("Creating new texture layer"); |
| 258 | |
| 259 | Layer* layer = new Layer(0, 0); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 260 | layer->setCacheable(false); |
| 261 | layer->setTextureLayer(true); |
| 262 | layer->setBlend(!isOpaque); |
| 263 | layer->setEmpty(true); |
| 264 | layer->setFbo(0); |
| 265 | layer->setAlpha(255, SkXfermode::kSrcOver_Mode); |
Romain Guy | 4a5a715 | 2011-06-24 17:53:53 -0700 | [diff] [blame] | 266 | layer->layer.set(0.0f, 0.0f, 0.0f, 0.0f); |
Romain Guy | a9dc86b | 2011-10-11 14:06:21 -0700 | [diff] [blame^] | 267 | layer->texCoords.set(0.0f, 1.0f, 1.0f, 0.0f); |
Romain Guy | 4a5a715 | 2011-06-24 17:53:53 -0700 | [diff] [blame] | 268 | layer->region.clear(); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 269 | layer->setRenderTarget(GL_NONE); // see ::updateTextureLayer() |
Romain Guy | 4a5a715 | 2011-06-24 17:53:53 -0700 | [diff] [blame] | 270 | |
| 271 | glActiveTexture(GL_TEXTURE0); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 272 | layer->generateTexture(); |
Romain Guy | 4a5a715 | 2011-06-24 17:53:53 -0700 | [diff] [blame] | 273 | |
| 274 | return layer; |
| 275 | } |
| 276 | |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 277 | void LayerRenderer::updateTextureLayer(Layer* layer, uint32_t width, uint32_t height, |
Romain Guy | a948927 | 2011-06-22 20:58:11 -0700 | [diff] [blame] | 278 | bool isOpaque, GLenum renderTarget, float* transform) { |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 279 | if (layer) { |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 280 | layer->setBlend(!isOpaque); |
| 281 | layer->setSize(width, height); |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 282 | layer->layer.set(0.0f, 0.0f, width, height); |
| 283 | layer->region.set(width, height); |
| 284 | layer->regionRect.set(0.0f, 0.0f, width, height); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 285 | layer->getTexTransform().load(transform); |
Romain Guy | 8f0095c | 2011-05-02 17:24:22 -0700 | [diff] [blame] | 286 | |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 287 | if (renderTarget != layer->getRenderTarget()) { |
| 288 | layer->setRenderTarget(renderTarget); |
| 289 | layer->bindTexture(); |
| 290 | layer->setFilter(GL_NEAREST, GL_NEAREST, false, true); |
| 291 | layer->setWrap(GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, false, true); |
Romain Guy | 4a5a715 | 2011-06-24 17:53:53 -0700 | [diff] [blame] | 292 | } |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 296 | void LayerRenderer::destroyLayer(Layer* layer) { |
| 297 | if (layer) { |
Romain Guy | eea6069 | 2011-07-26 20:35:55 -0700 | [diff] [blame] | 298 | LAYER_RENDERER_LOGD("Recycling layer, %dx%d fbo = %d", |
| 299 | layer->getWidth(), layer->getHeight(), layer->getFbo()); |
Romain Guy | 1fc883b | 2011-01-12 14:30:59 -0800 | [diff] [blame] | 300 | |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 301 | if (layer->getFbo()) { |
| 302 | Caches::getInstance().fboCache.put(layer->getFbo()); |
Romain Guy | 09b7c91 | 2011-02-02 20:28:09 -0800 | [diff] [blame] | 303 | } |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 304 | |
Romain Guy | 09b7c91 | 2011-02-02 20:28:09 -0800 | [diff] [blame] | 305 | if (!Caches::getInstance().layerCache.put(layer)) { |
Romain Guy | eea6069 | 2011-07-26 20:35:55 -0700 | [diff] [blame] | 306 | LAYER_RENDERER_LOGD(" Destroyed!"); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 307 | layer->deleteTexture(); |
Romain Guy | 09b7c91 | 2011-02-02 20:28:09 -0800 | [diff] [blame] | 308 | delete layer; |
| 309 | } else { |
Romain Guy | eea6069 | 2011-07-26 20:35:55 -0700 | [diff] [blame] | 310 | LAYER_RENDERER_LOGD(" Cached!"); |
Romain Guy | 65b345f | 2011-07-27 18:51:50 -0700 | [diff] [blame] | 311 | #if DEBUG_LAYER_RENDERER |
Romain Guy | eea6069 | 2011-07-26 20:35:55 -0700 | [diff] [blame] | 312 | Caches::getInstance().layerCache.dump(); |
| 313 | #endif |
Romain Guy | 09b7c91 | 2011-02-02 20:28:09 -0800 | [diff] [blame] | 314 | layer->region.clear(); |
| 315 | } |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 316 | } |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 317 | } |
| 318 | |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 319 | void LayerRenderer::destroyLayerDeferred(Layer* layer) { |
| 320 | if (layer) { |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 321 | LAYER_RENDERER_LOGD("Deferring layer destruction, fbo = %d", layer->getFbo()); |
Romain Guy | 1fc883b | 2011-01-12 14:30:59 -0800 | [diff] [blame] | 322 | |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 323 | Caches::getInstance().deleteLayerDeferred(layer); |
| 324 | } |
Romain Guy | 57066eb | 2011-01-12 12:53:32 -0800 | [diff] [blame] | 325 | } |
| 326 | |
Romain Guy | 77a8116 | 2011-06-14 16:45:55 -0700 | [diff] [blame] | 327 | bool LayerRenderer::copyLayer(Layer* layer, SkBitmap* bitmap) { |
| 328 | Caches& caches = Caches::getInstance(); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 329 | if (layer && layer->isTextureLayer() && bitmap->width() <= caches.maxTextureSize && |
Romain Guy | 77a8116 | 2011-06-14 16:45:55 -0700 | [diff] [blame] | 330 | bitmap->height() <= caches.maxTextureSize) { |
| 331 | |
| 332 | GLuint fbo = caches.fboCache.get(); |
| 333 | if (!fbo) { |
| 334 | LOGW("Could not obtain an FBO"); |
| 335 | return false; |
| 336 | } |
| 337 | |
Romain Guy | d6b2a00 | 2011-06-17 17:45:59 -0700 | [diff] [blame] | 338 | SkAutoLockPixels alp(*bitmap); |
| 339 | |
Romain Guy | 77a8116 | 2011-06-14 16:45:55 -0700 | [diff] [blame] | 340 | GLuint texture; |
| 341 | GLuint previousFbo; |
| 342 | |
| 343 | GLenum format; |
| 344 | GLenum type; |
| 345 | |
Romain Guy | d6b2a00 | 2011-06-17 17:45:59 -0700 | [diff] [blame] | 346 | GLenum error = GL_NO_ERROR; |
| 347 | bool status = false; |
| 348 | |
Romain Guy | 77a8116 | 2011-06-14 16:45:55 -0700 | [diff] [blame] | 349 | switch (bitmap->config()) { |
| 350 | case SkBitmap::kA8_Config: |
| 351 | format = GL_ALPHA; |
| 352 | type = GL_UNSIGNED_BYTE; |
| 353 | break; |
| 354 | case SkBitmap::kRGB_565_Config: |
| 355 | format = GL_RGB; |
| 356 | type = GL_UNSIGNED_SHORT_5_6_5; |
| 357 | break; |
| 358 | case SkBitmap::kARGB_4444_Config: |
| 359 | format = GL_RGBA; |
| 360 | type = GL_UNSIGNED_SHORT_4_4_4_4; |
| 361 | break; |
| 362 | case SkBitmap::kARGB_8888_Config: |
| 363 | default: |
| 364 | format = GL_RGBA; |
| 365 | type = GL_UNSIGNED_BYTE; |
| 366 | break; |
| 367 | } |
| 368 | |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 369 | float alpha = layer->getAlpha(); |
| 370 | SkXfermode::Mode mode = layer->getMode(); |
Romain Guy | d6b2a00 | 2011-06-17 17:45:59 -0700 | [diff] [blame] | 371 | |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 372 | layer->setAlpha(255, SkXfermode::kSrc_Mode); |
| 373 | layer->setFbo(fbo); |
Romain Guy | d6b2a00 | 2011-06-17 17:45:59 -0700 | [diff] [blame] | 374 | |
Romain Guy | 77a8116 | 2011-06-14 16:45:55 -0700 | [diff] [blame] | 375 | glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint*) &previousFbo); |
| 376 | glBindFramebuffer(GL_FRAMEBUFFER, fbo); |
| 377 | |
| 378 | glGenTextures(1, &texture); |
Romain Guy | d6b2a00 | 2011-06-17 17:45:59 -0700 | [diff] [blame] | 379 | if ((error = glGetError()) != GL_NO_ERROR) goto error; |
Romain Guy | 77a8116 | 2011-06-14 16:45:55 -0700 | [diff] [blame] | 380 | |
| 381 | glActiveTexture(GL_TEXTURE0); |
| 382 | glBindTexture(GL_TEXTURE_2D, texture); |
| 383 | |
Romain Guy | ec19b4a | 2011-07-07 21:05:04 -0700 | [diff] [blame] | 384 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 385 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
Romain Guy | 77a8116 | 2011-06-14 16:45:55 -0700 | [diff] [blame] | 386 | |
| 387 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 388 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 389 | |
| 390 | glTexImage2D(GL_TEXTURE_2D, 0, format, bitmap->width(), bitmap->height(), |
| 391 | 0, format, type, NULL); |
Romain Guy | d6b2a00 | 2011-06-17 17:45:59 -0700 | [diff] [blame] | 392 | if ((error = glGetError()) != GL_NO_ERROR) goto error; |
| 393 | |
Romain Guy | 77a8116 | 2011-06-14 16:45:55 -0700 | [diff] [blame] | 394 | glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, |
| 395 | GL_TEXTURE_2D, texture, 0); |
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 | LayerRenderer renderer(layer); |
| 400 | renderer.setViewport(bitmap->width(), bitmap->height()); |
| 401 | renderer.OpenGLRenderer::prepareDirty(0.0f, 0.0f, |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 402 | bitmap->width(), bitmap->height(), !layer->isBlend()); |
Romain Guy | a9dc86b | 2011-10-11 14:06:21 -0700 | [diff] [blame^] | 403 | |
| 404 | glDisable(GL_SCISSOR_TEST); |
| 405 | renderer.translate(0.0f, bitmap->height()); |
| 406 | renderer.scale(1.0f, -1.0f); |
| 407 | |
| 408 | mat4 texTransform(layer->getTexTransform()); |
| 409 | |
| 410 | mat4 invert; |
| 411 | invert.translate(0.0f, 1.0f, 0.0f); |
| 412 | invert.scale(1.0f, -1.0f, 1.0f); |
| 413 | layer->getTexTransform().multiply(invert); |
| 414 | |
Romain Guy | d6b2a00 | 2011-06-17 17:45:59 -0700 | [diff] [blame] | 415 | if ((error = glGetError()) != GL_NO_ERROR) goto error; |
Romain Guy | 77a8116 | 2011-06-14 16:45:55 -0700 | [diff] [blame] | 416 | |
Romain Guy | d6b2a00 | 2011-06-17 17:45:59 -0700 | [diff] [blame] | 417 | { |
| 418 | Rect bounds; |
| 419 | bounds.set(0.0f, 0.0f, bitmap->width(), bitmap->height()); |
| 420 | renderer.drawTextureLayer(layer, bounds); |
Romain Guy | 77a8116 | 2011-06-14 16:45:55 -0700 | [diff] [blame] | 421 | |
Romain Guy | d6b2a00 | 2011-06-17 17:45:59 -0700 | [diff] [blame] | 422 | glReadPixels(0, 0, bitmap->width(), bitmap->height(), format, |
| 423 | type, bitmap->getPixels()); |
Romain Guy | 77a8116 | 2011-06-14 16:45:55 -0700 | [diff] [blame] | 424 | |
Romain Guy | d6b2a00 | 2011-06-17 17:45:59 -0700 | [diff] [blame] | 425 | if ((error = glGetError()) != GL_NO_ERROR) goto error; |
| 426 | } |
Romain Guy | 77a8116 | 2011-06-14 16:45:55 -0700 | [diff] [blame] | 427 | |
Romain Guy | a9dc86b | 2011-10-11 14:06:21 -0700 | [diff] [blame^] | 428 | layer->getTexTransform().load(texTransform); |
Romain Guy | d6b2a00 | 2011-06-17 17:45:59 -0700 | [diff] [blame] | 429 | status = true; |
| 430 | } |
Romain Guy | 77a8116 | 2011-06-14 16:45:55 -0700 | [diff] [blame] | 431 | |
Romain Guy | d6b2a00 | 2011-06-17 17:45:59 -0700 | [diff] [blame] | 432 | error: |
| 433 | #if DEBUG_OPENGL |
| 434 | if (error != GL_NO_ERROR) { |
| 435 | LOGD("GL error while copying layer into bitmap = 0x%x", error); |
| 436 | } |
| 437 | #endif |
Romain Guy | 77a8116 | 2011-06-14 16:45:55 -0700 | [diff] [blame] | 438 | |
| 439 | glBindFramebuffer(GL_FRAMEBUFFER, previousFbo); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 440 | layer->setAlpha(alpha, mode); |
| 441 | layer->setFbo(0); |
Romain Guy | 77a8116 | 2011-06-14 16:45:55 -0700 | [diff] [blame] | 442 | glDeleteTextures(1, &texture); |
| 443 | caches.fboCache.put(fbo); |
| 444 | |
Romain Guy | d6b2a00 | 2011-06-17 17:45:59 -0700 | [diff] [blame] | 445 | return status; |
Romain Guy | 77a8116 | 2011-06-14 16:45:55 -0700 | [diff] [blame] | 446 | } |
| 447 | return false; |
| 448 | } |
| 449 | |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 450 | }; // namespace uirenderer |
| 451 | }; // namespace android |