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