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