Chet Haase | d15ebf2 | 2012-09-05 11:40:29 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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 | |
| 19 | #include <utils/Log.h> |
| 20 | |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 21 | #include "Caches.h" |
Romain Guy | 96885eb | 2013-03-26 15:05:58 -0700 | [diff] [blame] | 22 | #include "DeferredDisplayList.h" |
Chet Haase | d15ebf2 | 2012-09-05 11:40:29 -0700 | [diff] [blame] | 23 | #include "Layer.h" |
Chet Haase | 98d3a64 | 2012-09-26 10:27:40 -0700 | [diff] [blame] | 24 | #include "LayerRenderer.h" |
Chet Haase | d15ebf2 | 2012-09-05 11:40:29 -0700 | [diff] [blame] | 25 | #include "OpenGLRenderer.h" |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 26 | #include "RenderNode.h" |
Chet Haase | d15ebf2 | 2012-09-05 11:40:29 -0700 | [diff] [blame] | 27 | |
| 28 | namespace android { |
| 29 | namespace uirenderer { |
| 30 | |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 31 | Layer::Layer(const uint32_t layerWidth, const uint32_t layerHeight): |
| 32 | caches(Caches::getInstance()), texture(caches) { |
Chet Haase | 603f6de | 2012-09-14 15:31:25 -0700 | [diff] [blame] | 33 | mesh = NULL; |
Chet Haase | 603f6de | 2012-09-14 15:31:25 -0700 | [diff] [blame] | 34 | meshElementCount = 0; |
| 35 | cacheable = true; |
Romain Guy | 7c25aab | 2012-10-18 15:05:02 -0700 | [diff] [blame] | 36 | dirty = false; |
Chet Haase | 603f6de | 2012-09-14 15:31:25 -0700 | [diff] [blame] | 37 | textureLayer = false; |
| 38 | renderTarget = GL_TEXTURE_2D; |
| 39 | texture.width = layerWidth; |
| 40 | texture.height = layerHeight; |
| 41 | colorFilter = NULL; |
| 42 | deferredUpdateScheduled = false; |
| 43 | renderer = NULL; |
| 44 | displayList = NULL; |
| 45 | fbo = 0; |
Romain Guy | 3bbacf2 | 2013-02-06 16:51:04 -0800 | [diff] [blame] | 46 | stencil = NULL; |
Romain Guy | 5bb3c73 | 2012-11-29 17:52:58 -0800 | [diff] [blame] | 47 | debugDrawUpdate = false; |
Chris Craik | 34416ea | 2013-04-15 16:08:28 -0700 | [diff] [blame] | 48 | hasDrawnSinceUpdate = false; |
Chris Craik | 9757ac0 | 2014-02-25 18:50:17 -0800 | [diff] [blame] | 49 | forceFilter = false; |
Romain Guy | 96885eb | 2013-03-26 15:05:58 -0700 | [diff] [blame] | 50 | deferredList = NULL; |
Chris Craik | 3f085429 | 2014-04-15 16:18:08 -0700 | [diff] [blame] | 51 | convexMask = NULL; |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 52 | caches.resourceCache.incrementRefcount(this); |
Chet Haase | 603f6de | 2012-09-14 15:31:25 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Chet Haase | d15ebf2 | 2012-09-05 11:40:29 -0700 | [diff] [blame] | 55 | Layer::~Layer() { |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 56 | SkSafeUnref(colorFilter); |
Chet Haase | 98d3a64 | 2012-09-26 10:27:40 -0700 | [diff] [blame] | 57 | removeFbo(); |
Romain Guy | 8a13749 | 2012-09-25 15:49:03 -0700 | [diff] [blame] | 58 | deleteTexture(); |
Romain Guy | 96885eb | 2013-03-26 15:05:58 -0700 | [diff] [blame] | 59 | |
| 60 | delete[] mesh; |
Romain Guy | 96885eb | 2013-03-26 15:05:58 -0700 | [diff] [blame] | 61 | delete deferredList; |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 62 | delete renderer; |
Romain Guy | 97dc917 | 2012-09-23 17:46:45 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Romain Guy | 2055aba | 2013-01-18 16:42:51 -0800 | [diff] [blame] | 65 | uint32_t Layer::computeIdealWidth(uint32_t layerWidth) { |
| 66 | return uint32_t(ceilf(layerWidth / float(LAYER_SIZE)) * LAYER_SIZE); |
| 67 | } |
| 68 | |
| 69 | uint32_t Layer::computeIdealHeight(uint32_t layerHeight) { |
| 70 | return uint32_t(ceilf(layerHeight / float(LAYER_SIZE)) * LAYER_SIZE); |
| 71 | } |
| 72 | |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 73 | void Layer::requireRenderer() { |
| 74 | if (!renderer) { |
| 75 | renderer = new LayerRenderer(this); |
| 76 | renderer->initProperties(); |
| 77 | } |
| 78 | } |
| 79 | |
Romain Guy | 2055aba | 2013-01-18 16:42:51 -0800 | [diff] [blame] | 80 | bool Layer::resize(const uint32_t width, const uint32_t height) { |
| 81 | uint32_t desiredWidth = computeIdealWidth(width); |
| 82 | uint32_t desiredHeight = computeIdealWidth(height); |
| 83 | |
| 84 | if (desiredWidth <= getWidth() && desiredHeight <= getHeight()) { |
| 85 | return true; |
| 86 | } |
| 87 | |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 88 | const uint32_t maxTextureSize = caches.maxTextureSize; |
Romain Guy | ce4a7df | 2013-03-28 11:32:33 -0700 | [diff] [blame] | 89 | if (desiredWidth > maxTextureSize || desiredHeight > maxTextureSize) { |
| 90 | ALOGW("Layer exceeds max. dimensions supported by the GPU (%dx%d, max=%dx%d)", |
| 91 | desiredWidth, desiredHeight, maxTextureSize, maxTextureSize); |
| 92 | return false; |
| 93 | } |
| 94 | |
Romain Guy | 2055aba | 2013-01-18 16:42:51 -0800 | [diff] [blame] | 95 | uint32_t oldWidth = getWidth(); |
| 96 | uint32_t oldHeight = getHeight(); |
| 97 | |
| 98 | setSize(desiredWidth, desiredHeight); |
| 99 | |
| 100 | if (fbo) { |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 101 | caches.activeTexture(0); |
Romain Guy | 2055aba | 2013-01-18 16:42:51 -0800 | [diff] [blame] | 102 | bindTexture(); |
Romain Guy | 0908764 | 2013-04-04 12:27:54 -0700 | [diff] [blame] | 103 | allocateTexture(); |
Romain Guy | 2055aba | 2013-01-18 16:42:51 -0800 | [diff] [blame] | 104 | |
| 105 | if (glGetError() != GL_NO_ERROR) { |
| 106 | setSize(oldWidth, oldHeight); |
| 107 | return false; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | if (stencil) { |
Romain Guy | 3bbacf2 | 2013-02-06 16:51:04 -0800 | [diff] [blame] | 112 | stencil->bind(); |
| 113 | stencil->resize(desiredWidth, desiredHeight); |
Romain Guy | 2055aba | 2013-01-18 16:42:51 -0800 | [diff] [blame] | 114 | |
| 115 | if (glGetError() != GL_NO_ERROR) { |
| 116 | setSize(oldWidth, oldHeight); |
| 117 | return false; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | return true; |
| 122 | } |
| 123 | |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 124 | void Layer::removeFbo(bool flush) { |
| 125 | if (stencil) { |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 126 | GLuint previousFbo; |
| 127 | glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint*) &previousFbo); |
| 128 | if (fbo != previousFbo) glBindFramebuffer(GL_FRAMEBUFFER, fbo); |
| 129 | glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, 0); |
| 130 | if (fbo != previousFbo) glBindFramebuffer(GL_FRAMEBUFFER, previousFbo); |
| 131 | |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 132 | caches.renderBufferCache.put(stencil); |
Romain Guy | 3bbacf2 | 2013-02-06 16:51:04 -0800 | [diff] [blame] | 133 | stencil = NULL; |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 134 | } |
| 135 | |
Chet Haase | 98d3a64 | 2012-09-26 10:27:40 -0700 | [diff] [blame] | 136 | if (fbo) { |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 137 | if (flush) LayerRenderer::flushLayer(this); |
| 138 | // If put fails the cache will delete the FBO |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 139 | caches.fboCache.put(fbo); |
Chet Haase | 98d3a64 | 2012-09-26 10:27:40 -0700 | [diff] [blame] | 140 | fbo = 0; |
Dave Burke | 56257af | 2012-09-25 20:30:09 -0700 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | |
John Reck | 087bc0c | 2014-04-04 16:20:08 -0700 | [diff] [blame] | 144 | void Layer::updateDeferred(RenderNode* displayList, |
| 145 | int left, int top, int right, int bottom) { |
| 146 | requireRenderer(); |
| 147 | this->displayList = displayList; |
| 148 | const Rect r(left, top, right, bottom); |
| 149 | dirtyRect.unionWith(r); |
| 150 | deferredUpdateScheduled = true; |
| 151 | } |
| 152 | |
Derek Sollenberger | 674554f | 2014-02-19 16:47:32 +0000 | [diff] [blame] | 153 | void Layer::setPaint(const SkPaint* paint) { |
Chet Haase | d15ebf2 | 2012-09-05 11:40:29 -0700 | [diff] [blame] | 154 | OpenGLRenderer::getAlphaAndModeDirect(paint, &alpha, &mode); |
Derek Sollenberger | 674554f | 2014-02-19 16:47:32 +0000 | [diff] [blame] | 155 | setColorFilter((paint) ? paint->getColorFilter() : NULL); |
Chet Haase | d15ebf2 | 2012-09-05 11:40:29 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 158 | void Layer::setColorFilter(SkColorFilter* filter) { |
| 159 | SkRefCnt_SafeAssign(colorFilter, filter); |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | void Layer::bindTexture() const { |
| 163 | if (texture.id) { |
| 164 | caches.bindTexture(renderTarget, texture.id); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | void Layer::bindStencilRenderBuffer() const { |
| 169 | if (stencil) { |
| 170 | stencil->bind(); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | void Layer::generateTexture() { |
| 175 | if (!texture.id) { |
| 176 | glGenTextures(1, &texture.id); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | void Layer::deleteTexture() { |
| 181 | if (texture.id) { |
Romain Guy | be1b127 | 2013-06-06 14:02:54 -0700 | [diff] [blame] | 182 | texture.deleteTexture(); |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 183 | texture.id = 0; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | void Layer::clearTexture() { |
| 188 | texture.id = 0; |
| 189 | } |
| 190 | |
| 191 | void Layer::allocateTexture() { |
| 192 | #if DEBUG_LAYERS |
| 193 | ALOGD(" Allocate layer: %dx%d", getWidth(), getHeight()); |
| 194 | #endif |
| 195 | if (texture.id) { |
| 196 | glPixelStorei(GL_UNPACK_ALIGNMENT, 4); |
| 197 | glTexImage2D(renderTarget, 0, GL_RGBA, getWidth(), getHeight(), 0, |
| 198 | GL_RGBA, GL_UNSIGNED_BYTE, NULL); |
Chet Haase | d15ebf2 | 2012-09-05 11:40:29 -0700 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | |
Romain Guy | 96885eb | 2013-03-26 15:05:58 -0700 | [diff] [blame] | 202 | void Layer::defer() { |
Romain Guy | 96885eb | 2013-03-26 15:05:58 -0700 | [diff] [blame] | 203 | const float width = layer.getWidth(); |
| 204 | const float height = layer.getHeight(); |
| 205 | |
| 206 | if (dirtyRect.isEmpty() || (dirtyRect.left <= 0 && dirtyRect.top <= 0 && |
| 207 | dirtyRect.right >= width && dirtyRect.bottom >= height)) { |
| 208 | dirtyRect.set(0, 0, width, height); |
| 209 | } |
| 210 | |
Chris Craik | f57776b | 2013-10-25 18:30:17 -0700 | [diff] [blame] | 211 | delete deferredList; |
| 212 | deferredList = new DeferredDisplayList(dirtyRect); |
| 213 | |
Chris Craik | 28ce94a | 2013-05-31 11:38:03 -0700 | [diff] [blame] | 214 | DeferStateStruct deferredState(*deferredList, *renderer, |
John Reck | e18264b | 2014-03-12 13:56:30 -0700 | [diff] [blame] | 215 | RenderNode::kReplayFlag_ClipChildren); |
Chris Craik | 28ce94a | 2013-05-31 11:38:03 -0700 | [diff] [blame] | 216 | |
Chris Craik | 797b95b | 2014-05-20 18:10:25 -0700 | [diff] [blame^] | 217 | renderer->setViewport(width, height); |
Romain Guy | 96885eb | 2013-03-26 15:05:58 -0700 | [diff] [blame] | 218 | renderer->setupFrameState(dirtyRect.left, dirtyRect.top, |
| 219 | dirtyRect.right, dirtyRect.bottom, !isBlend()); |
| 220 | |
Chris Craik | f57776b | 2013-10-25 18:30:17 -0700 | [diff] [blame] | 221 | displayList->computeOrdering(); |
Chris Craik | b265e2c | 2014-03-27 15:50:09 -0700 | [diff] [blame] | 222 | displayList->deferNodeTree(deferredState); |
Romain Guy | 02b49b7 | 2013-03-29 12:37:16 -0700 | [diff] [blame] | 223 | |
| 224 | deferredUpdateScheduled = false; |
Romain Guy | 96885eb | 2013-03-26 15:05:58 -0700 | [diff] [blame] | 225 | } |
| 226 | |
Romain Guy | e93482f | 2013-06-17 13:14:51 -0700 | [diff] [blame] | 227 | void Layer::cancelDefer() { |
Romain Guy | e93482f | 2013-06-17 13:14:51 -0700 | [diff] [blame] | 228 | displayList = NULL; |
| 229 | deferredUpdateScheduled = false; |
| 230 | if (deferredList) { |
| 231 | delete deferredList; |
| 232 | deferredList = NULL; |
| 233 | } |
| 234 | } |
| 235 | |
Romain Guy | 96885eb | 2013-03-26 15:05:58 -0700 | [diff] [blame] | 236 | void Layer::flush() { |
Chris Craik | 8c6e17c | 2013-06-17 13:02:12 -0700 | [diff] [blame] | 237 | // renderer is checked as layer may be destroyed/put in layer cache with flush scheduled |
| 238 | if (deferredList && renderer) { |
Romain Guy | 96885eb | 2013-03-26 15:05:58 -0700 | [diff] [blame] | 239 | renderer->setViewport(layer.getWidth(), layer.getHeight()); |
| 240 | renderer->prepareDirty(dirtyRect.left, dirtyRect.top, dirtyRect.right, dirtyRect.bottom, |
| 241 | !isBlend()); |
| 242 | |
| 243 | deferredList->flush(*renderer, dirtyRect); |
| 244 | |
| 245 | renderer->finish(); |
Romain Guy | 96885eb | 2013-03-26 15:05:58 -0700 | [diff] [blame] | 246 | |
| 247 | dirtyRect.setEmpty(); |
Chris Craik | 1206b9b | 2013-04-04 14:46:24 -0700 | [diff] [blame] | 248 | displayList = NULL; |
Romain Guy | 96885eb | 2013-03-26 15:05:58 -0700 | [diff] [blame] | 249 | } |
| 250 | } |
| 251 | |
Romain Guy | 02b49b7 | 2013-03-29 12:37:16 -0700 | [diff] [blame] | 252 | void Layer::render() { |
| 253 | renderer->setViewport(layer.getWidth(), layer.getHeight()); |
| 254 | renderer->prepareDirty(dirtyRect.left, dirtyRect.top, dirtyRect.right, dirtyRect.bottom, |
| 255 | !isBlend()); |
| 256 | |
John Reck | 087bc0c | 2014-04-04 16:20:08 -0700 | [diff] [blame] | 257 | renderer->drawDisplayList(displayList.get(), dirtyRect, RenderNode::kReplayFlag_ClipChildren); |
Romain Guy | 02b49b7 | 2013-03-29 12:37:16 -0700 | [diff] [blame] | 258 | |
| 259 | renderer->finish(); |
Romain Guy | 02b49b7 | 2013-03-29 12:37:16 -0700 | [diff] [blame] | 260 | |
| 261 | dirtyRect.setEmpty(); |
| 262 | |
| 263 | deferredUpdateScheduled = false; |
| 264 | displayList = NULL; |
| 265 | } |
| 266 | |
Chet Haase | d15ebf2 | 2012-09-05 11:40:29 -0700 | [diff] [blame] | 267 | }; // namespace uirenderer |
| 268 | }; // namespace android |