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