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