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