Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 1 | /* |
Chris Craik | 5ea1724 | 2016-01-11 14:07:59 -0800 | [diff] [blame] | 2 | * Copyright (C) 2016 The Android Open Source Project |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 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 | |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 17 | #include "FrameBuilder.h" |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 18 | |
Chris Craik | 0b7e824 | 2015-10-28 16:50:44 -0700 | [diff] [blame] | 19 | #include "LayerUpdateQueue.h" |
Chris Craik | 161f54b | 2015-11-05 11:08:52 -0800 | [diff] [blame] | 20 | #include "RenderNode.h" |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 21 | #include "VectorDrawable.h" |
Chris Craik | 98787e6 | 2015-11-13 10:55:30 -0800 | [diff] [blame] | 22 | #include "renderstate/OffscreenBufferPool.h" |
sergeyv | dccca44 | 2016-03-21 15:38:21 -0700 | [diff] [blame] | 23 | #include "hwui/Canvas.h" |
Chris Craik | 161f54b | 2015-11-05 11:08:52 -0800 | [diff] [blame] | 24 | #include "utils/FatVector.h" |
| 25 | #include "utils/PaintUtils.h" |
Chris Craik | 8ecf41c | 2015-11-16 10:27:59 -0800 | [diff] [blame] | 26 | #include "utils/TraceUtils.h" |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 27 | |
Chris Craik | d3daa31 | 2015-11-06 10:59:56 -0800 | [diff] [blame] | 28 | #include <SkPathOps.h> |
Chris Craik | 161f54b | 2015-11-05 11:08:52 -0800 | [diff] [blame] | 29 | #include <utils/TypeHelpers.h> |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 30 | |
| 31 | namespace android { |
| 32 | namespace uirenderer { |
| 33 | |
Chris Craik | 9cd1bbe | 2016-04-14 16:08:25 -0700 | [diff] [blame] | 34 | FrameBuilder::FrameBuilder(const SkRect& clip, |
Chris Craik | 0b7e824 | 2015-10-28 16:50:44 -0700 | [diff] [blame] | 35 | uint32_t viewportWidth, uint32_t viewportHeight, |
Chris Craik | 9cd1bbe | 2016-04-14 16:08:25 -0700 | [diff] [blame] | 36 | const LightGeometry& lightGeometry, Caches& caches) |
| 37 | : mStdAllocator(mAllocator) |
| 38 | , mLayerBuilders(mStdAllocator) |
| 39 | , mLayerStack(mStdAllocator) |
| 40 | , mCanvasState(*this) |
Chris Craik | 6e068c01 | 2016-01-15 16:15:30 -0800 | [diff] [blame] | 41 | , mCaches(caches) |
Chris Craik | 6246d278 | 2016-03-29 15:01:41 -0700 | [diff] [blame] | 42 | , mLightRadius(lightGeometry.radius) |
Chris Craik | 9cd1bbe | 2016-04-14 16:08:25 -0700 | [diff] [blame] | 43 | , mDrawFbo0(true) { |
Chris Craik | 98787e6 | 2015-11-13 10:55:30 -0800 | [diff] [blame] | 44 | |
| 45 | // Prepare to defer Fbo0 |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 46 | auto fbo0 = mAllocator.create<LayerBuilder>(viewportWidth, viewportHeight, Rect(clip)); |
| 47 | mLayerBuilders.push_back(fbo0); |
Chris Craik | 98787e6 | 2015-11-13 10:55:30 -0800 | [diff] [blame] | 48 | mLayerStack.push_back(0); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 49 | mCanvasState.initializeSaveStack(viewportWidth, viewportHeight, |
Chris Craik | ddf2215 | 2015-10-14 17:42:47 -0700 | [diff] [blame] | 50 | clip.fLeft, clip.fTop, clip.fRight, clip.fBottom, |
Chris Craik | 6e068c01 | 2016-01-15 16:15:30 -0800 | [diff] [blame] | 51 | lightGeometry.center); |
Chris Craik | 9cd1bbe | 2016-04-14 16:08:25 -0700 | [diff] [blame] | 52 | } |
Chris Craik | 0b7e824 | 2015-10-28 16:50:44 -0700 | [diff] [blame] | 53 | |
Chris Craik | 9cd1bbe | 2016-04-14 16:08:25 -0700 | [diff] [blame] | 54 | FrameBuilder::FrameBuilder(const LayerUpdateQueue& layers, |
| 55 | const LightGeometry& lightGeometry, Caches& caches) |
| 56 | : mStdAllocator(mAllocator) |
| 57 | , mLayerBuilders(mStdAllocator) |
| 58 | , mLayerStack(mStdAllocator) |
| 59 | , mCanvasState(*this) |
| 60 | , mCaches(caches) |
| 61 | , mLightRadius(lightGeometry.radius) |
| 62 | , mDrawFbo0(false) { |
| 63 | // TODO: remove, with each layer on its own save stack |
| 64 | |
| 65 | // Prepare to defer Fbo0 (which will be empty) |
| 66 | auto fbo0 = mAllocator.create<LayerBuilder>(1, 1, Rect(1, 1)); |
| 67 | mLayerBuilders.push_back(fbo0); |
| 68 | mLayerStack.push_back(0); |
| 69 | mCanvasState.initializeSaveStack(1, 1, |
| 70 | 0, 0, 1, 1, |
| 71 | lightGeometry.center); |
| 72 | |
| 73 | deferLayers(layers); |
| 74 | } |
| 75 | |
| 76 | void FrameBuilder::deferLayers(const LayerUpdateQueue& layers) { |
Chris Craik | 0b7e824 | 2015-10-28 16:50:44 -0700 | [diff] [blame] | 77 | // Render all layers to be updated, in order. Defer in reverse order, so that they'll be |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 78 | // updated in the order they're passed in (mLayerBuilders are issued to Renderer in reverse) |
Chris Craik | 0b7e824 | 2015-10-28 16:50:44 -0700 | [diff] [blame] | 79 | for (int i = layers.entries().size() - 1; i >= 0; i--) { |
| 80 | RenderNode* layerNode = layers.entries()[i].renderNode; |
Chris Craik | e9c5fd8 | 2016-01-12 18:59:38 -0800 | [diff] [blame] | 81 | // only schedule repaint if node still on layer - possible it may have been |
| 82 | // removed during a dropped frame, but layers may still remain scheduled so |
| 83 | // as not to lose info on what portion is damaged |
Chris Craik | 3741328 | 2016-05-12 17:48:51 -0700 | [diff] [blame] | 84 | OffscreenBuffer* layer = layerNode->getLayer(); |
| 85 | if (CC_LIKELY(layer)) { |
Chris Craik | aff230f | 2016-05-04 16:27:28 -0700 | [diff] [blame] | 86 | ATRACE_FORMAT("Optimize HW Layer DisplayList %s %ux%u", |
| 87 | layerNode->getName(), layerNode->getWidth(), layerNode->getHeight()); |
| 88 | |
Chris Craik | d4fe4d3 | 2016-06-09 16:57:11 -0700 | [diff] [blame] | 89 | Rect layerDamage = layers.entries()[i].damage; |
| 90 | // TODO: ensure layer damage can't be larger than layer |
| 91 | layerDamage.doIntersect(0, 0, layer->viewportWidth, layer->viewportHeight); |
Chris Craik | e9c5fd8 | 2016-01-12 18:59:38 -0800 | [diff] [blame] | 92 | layerNode->computeOrdering(); |
Chris Craik | 0b7e824 | 2015-10-28 16:50:44 -0700 | [diff] [blame] | 93 | |
Chris Craik | e9c5fd8 | 2016-01-12 18:59:38 -0800 | [diff] [blame] | 94 | // map current light center into RenderNode's coordinate space |
| 95 | Vector3 lightCenter = mCanvasState.currentSnapshot()->getRelativeLightCenter(); |
Chris Craik | 3741328 | 2016-05-12 17:48:51 -0700 | [diff] [blame] | 96 | layer->inverseTransformInWindow.mapPoint3d(lightCenter); |
Chris Craik | 8ecf41c | 2015-11-16 10:27:59 -0800 | [diff] [blame] | 97 | |
Chris Craik | e9c5fd8 | 2016-01-12 18:59:38 -0800 | [diff] [blame] | 98 | saveForLayer(layerNode->getWidth(), layerNode->getHeight(), 0, 0, |
| 99 | layerDamage, lightCenter, nullptr, layerNode); |
Chris Craik | 0b7e824 | 2015-10-28 16:50:44 -0700 | [diff] [blame] | 100 | |
Chris Craik | e9c5fd8 | 2016-01-12 18:59:38 -0800 | [diff] [blame] | 101 | if (layerNode->getDisplayList()) { |
| 102 | deferNodeOps(*layerNode); |
| 103 | } |
| 104 | restoreForLayer(); |
Chris Craik | 0b7e824 | 2015-10-28 16:50:44 -0700 | [diff] [blame] | 105 | } |
Chris Craik | 0b7e824 | 2015-10-28 16:50:44 -0700 | [diff] [blame] | 106 | } |
Chris Craik | 9cd1bbe | 2016-04-14 16:08:25 -0700 | [diff] [blame] | 107 | } |
Chris Craik | 0b7e824 | 2015-10-28 16:50:44 -0700 | [diff] [blame] | 108 | |
Chris Craik | 9cd1bbe | 2016-04-14 16:08:25 -0700 | [diff] [blame] | 109 | void FrameBuilder::deferRenderNode(RenderNode& renderNode) { |
| 110 | renderNode.computeOrdering(); |
| 111 | |
| 112 | mCanvasState.save(SaveFlags::MatrixClip); |
| 113 | deferNodePropsAndOps(renderNode); |
| 114 | mCanvasState.restore(); |
| 115 | } |
| 116 | |
| 117 | void FrameBuilder::deferRenderNode(float tx, float ty, Rect clipRect, RenderNode& renderNode) { |
| 118 | renderNode.computeOrdering(); |
| 119 | |
| 120 | mCanvasState.save(SaveFlags::MatrixClip); |
| 121 | mCanvasState.translate(tx, ty); |
| 122 | mCanvasState.clipRect(clipRect.left, clipRect.top, clipRect.right, clipRect.bottom, |
Mike Reed | 6c67f1d | 2016-12-14 10:29:54 -0500 | [diff] [blame] | 123 | SkClipOp::kIntersect); |
Chris Craik | 9cd1bbe | 2016-04-14 16:08:25 -0700 | [diff] [blame] | 124 | deferNodePropsAndOps(renderNode); |
| 125 | mCanvasState.restore(); |
| 126 | } |
| 127 | |
| 128 | static Rect nodeBounds(RenderNode& node) { |
| 129 | auto& props = node.properties(); |
| 130 | return Rect(props.getLeft(), props.getTop(), |
| 131 | props.getRight(), props.getBottom()); |
| 132 | } |
| 133 | |
| 134 | void FrameBuilder::deferRenderNodeScene(const std::vector< sp<RenderNode> >& nodes, |
| 135 | const Rect& contentDrawBounds) { |
| 136 | if (nodes.size() < 1) return; |
| 137 | if (nodes.size() == 1) { |
| 138 | if (!nodes[0]->nothingToDraw()) { |
| 139 | deferRenderNode(*nodes[0]); |
| 140 | } |
| 141 | return; |
| 142 | } |
Chong Zhang | c3bd568 | 2016-01-25 12:01:12 -0800 | [diff] [blame] | 143 | // It there are multiple render nodes, they are laid out as follows: |
| 144 | // #0 - backdrop (content + caption) |
Chris Craik | 9cd1bbe | 2016-04-14 16:08:25 -0700 | [diff] [blame] | 145 | // #1 - content (local bounds are at (0,0), will be translated and clipped to backdrop) |
Chong Zhang | c3bd568 | 2016-01-25 12:01:12 -0800 | [diff] [blame] | 146 | // #2 - additional overlay nodes |
| 147 | // Usually the backdrop cannot be seen since it will be entirely covered by the content. While |
| 148 | // resizing however it might become partially visible. The following render loop will crop the |
| 149 | // backdrop against the content and draw the remaining part of it. It will then draw the content |
| 150 | // cropped to the backdrop (since that indicates a shrinking of the window). |
| 151 | // |
| 152 | // Additional nodes will be drawn on top with no particular clipping semantics. |
| 153 | |
Chong Zhang | c3bd568 | 2016-01-25 12:01:12 -0800 | [diff] [blame] | 154 | // Usually the contents bounds should be mContentDrawBounds - however - we will |
| 155 | // move it towards the fixed edge to give it a more stable appearance (for the moment). |
| 156 | // If there is no content bounds we ignore the layering as stated above and start with 2. |
Chong Zhang | c3bd568 | 2016-01-25 12:01:12 -0800 | [diff] [blame] | 157 | |
Chris Craik | 9cd1bbe | 2016-04-14 16:08:25 -0700 | [diff] [blame] | 158 | // Backdrop bounds in render target space |
| 159 | const Rect backdrop = nodeBounds(*nodes[0]); |
Chong Zhang | c3bd568 | 2016-01-25 12:01:12 -0800 | [diff] [blame] | 160 | |
Chris Craik | 9cd1bbe | 2016-04-14 16:08:25 -0700 | [diff] [blame] | 161 | // Bounds that content will fill in render target space (note content node bounds may be bigger) |
| 162 | Rect content(contentDrawBounds.getWidth(), contentDrawBounds.getHeight()); |
| 163 | content.translate(backdrop.left, backdrop.top); |
| 164 | if (!content.contains(backdrop) && !nodes[0]->nothingToDraw()) { |
| 165 | // Content doesn't entirely overlap backdrop, so fill around content (right/bottom) |
| 166 | |
| 167 | // Note: in the future, if content doesn't snap to backdrop's left/top, this may need to |
| 168 | // also fill left/top. Currently, both 2up and freeform position content at the top/left of |
| 169 | // the backdrop, so this isn't necessary. |
| 170 | if (content.right < backdrop.right) { |
| 171 | // draw backdrop to right side of content |
| 172 | deferRenderNode(0, 0, Rect(content.right, backdrop.top, |
| 173 | backdrop.right, backdrop.bottom), *nodes[0]); |
Chong Zhang | c3bd568 | 2016-01-25 12:01:12 -0800 | [diff] [blame] | 174 | } |
Chris Craik | 9cd1bbe | 2016-04-14 16:08:25 -0700 | [diff] [blame] | 175 | if (content.bottom < backdrop.bottom) { |
| 176 | // draw backdrop to bottom of content |
| 177 | // Note: bottom fill uses content left/right, to avoid overdrawing left/right fill |
| 178 | deferRenderNode(0, 0, Rect(content.left, content.bottom, |
| 179 | content.right, backdrop.bottom), *nodes[0]); |
| 180 | } |
| 181 | } |
Chong Zhang | c3bd568 | 2016-01-25 12:01:12 -0800 | [diff] [blame] | 182 | |
John Reck | 51c51df | 2017-01-23 10:24:27 -0800 | [diff] [blame^] | 183 | if (!nodes[1]->nothingToDraw()) { |
| 184 | if (!backdrop.isEmpty()) { |
| 185 | // content node translation to catch up with backdrop |
| 186 | float dx = contentDrawBounds.left - backdrop.left; |
| 187 | float dy = contentDrawBounds.top - backdrop.top; |
Chris Craik | 9cd1bbe | 2016-04-14 16:08:25 -0700 | [diff] [blame] | 188 | |
John Reck | 51c51df | 2017-01-23 10:24:27 -0800 | [diff] [blame^] | 189 | Rect contentLocalClip = backdrop; |
| 190 | contentLocalClip.translate(dx, dy); |
| 191 | deferRenderNode(-dx, -dy, contentLocalClip, *nodes[1]); |
| 192 | } else { |
| 193 | deferRenderNode(*nodes[1]); |
| 194 | } |
Chris Craik | 9cd1bbe | 2016-04-14 16:08:25 -0700 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | // remaining overlay nodes, simply defer |
| 198 | for (size_t index = 2; index < nodes.size(); index++) { |
| 199 | if (!nodes[index]->nothingToDraw()) { |
| 200 | deferRenderNode(*nodes[index]); |
| 201 | } |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 205 | void FrameBuilder::onViewportInitialized() {} |
Chris Craik | 818c9fb | 2015-10-23 14:33:42 -0700 | [diff] [blame] | 206 | |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 207 | void FrameBuilder::onSnapshotRestored(const Snapshot& removed, const Snapshot& restored) {} |
Chris Craik | 818c9fb | 2015-10-23 14:33:42 -0700 | [diff] [blame] | 208 | |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 209 | void FrameBuilder::deferNodePropsAndOps(RenderNode& node) { |
Chris Craik | 8ecf41c | 2015-11-16 10:27:59 -0800 | [diff] [blame] | 210 | const RenderProperties& properties = node.properties(); |
| 211 | const Outline& outline = properties.getOutline(); |
| 212 | if (properties.getAlpha() <= 0 |
| 213 | || (outline.getShouldClip() && outline.isEmpty()) |
| 214 | || properties.getScaleX() == 0 |
| 215 | || properties.getScaleY() == 0) { |
| 216 | return; // rejected |
| 217 | } |
| 218 | |
| 219 | if (properties.getLeft() != 0 || properties.getTop() != 0) { |
| 220 | mCanvasState.translate(properties.getLeft(), properties.getTop()); |
| 221 | } |
| 222 | if (properties.getStaticMatrix()) { |
| 223 | mCanvasState.concatMatrix(*properties.getStaticMatrix()); |
| 224 | } else if (properties.getAnimationMatrix()) { |
| 225 | mCanvasState.concatMatrix(*properties.getAnimationMatrix()); |
| 226 | } |
| 227 | if (properties.hasTransformMatrix()) { |
| 228 | if (properties.isTransformTranslateOnly()) { |
| 229 | mCanvasState.translate(properties.getTranslationX(), properties.getTranslationY()); |
| 230 | } else { |
| 231 | mCanvasState.concatMatrix(*properties.getTransformMatrix()); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | const int width = properties.getWidth(); |
| 236 | const int height = properties.getHeight(); |
| 237 | |
| 238 | Rect saveLayerBounds; // will be set to non-empty if saveLayer needed |
| 239 | const bool isLayer = properties.effectiveLayerType() != LayerType::None; |
| 240 | int clipFlags = properties.getClippingFlags(); |
| 241 | if (properties.getAlpha() < 1) { |
| 242 | if (isLayer) { |
| 243 | clipFlags &= ~CLIP_TO_BOUNDS; // bounds clipping done by layer |
| 244 | } |
| 245 | if (CC_LIKELY(isLayer || !properties.getHasOverlappingRendering())) { |
| 246 | // simply scale rendering content's alpha |
| 247 | mCanvasState.scaleAlpha(properties.getAlpha()); |
| 248 | } else { |
| 249 | // schedule saveLayer by initializing saveLayerBounds |
| 250 | saveLayerBounds.set(0, 0, width, height); |
| 251 | if (clipFlags) { |
| 252 | properties.getClippingRectForFlags(clipFlags, &saveLayerBounds); |
| 253 | clipFlags = 0; // all clipping done by savelayer |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | if (CC_UNLIKELY(ATRACE_ENABLED() && properties.promotedToLayer())) { |
| 258 | // pretend alpha always causes savelayer to warn about |
| 259 | // performance problem affecting old versions |
| 260 | ATRACE_FORMAT("%s alpha caused saveLayer %dx%d", node.getName(), width, height); |
| 261 | } |
| 262 | } |
| 263 | if (clipFlags) { |
| 264 | Rect clipRect; |
| 265 | properties.getClippingRectForFlags(clipFlags, &clipRect); |
| 266 | mCanvasState.clipRect(clipRect.left, clipRect.top, clipRect.right, clipRect.bottom, |
Mike Reed | 6c67f1d | 2016-12-14 10:29:54 -0500 | [diff] [blame] | 267 | SkClipOp::kIntersect); |
Chris Craik | 8ecf41c | 2015-11-16 10:27:59 -0800 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | if (properties.getRevealClip().willClip()) { |
| 271 | Rect bounds; |
| 272 | properties.getRevealClip().getBounds(&bounds); |
| 273 | mCanvasState.setClippingRoundRect(mAllocator, |
| 274 | bounds, properties.getRevealClip().getRadius()); |
| 275 | } else if (properties.getOutline().willClip()) { |
| 276 | mCanvasState.setClippingOutline(mAllocator, &(properties.getOutline())); |
| 277 | } |
| 278 | |
Chris Craik | 8913c89 | 2016-01-14 16:15:03 -0800 | [diff] [blame] | 279 | bool quickRejected = mCanvasState.currentSnapshot()->getRenderTargetClip().isEmpty() |
| 280 | || (properties.getClipToBounds() |
| 281 | && mCanvasState.quickRejectConservative(0, 0, width, height)); |
Chris Craik | 7fc1b03 | 2016-02-03 19:45:06 -0800 | [diff] [blame] | 282 | if (!quickRejected) { |
Chris Craik | 8ecf41c | 2015-11-16 10:27:59 -0800 | [diff] [blame] | 283 | // not rejected, so defer render as either Layer, or direct (possibly wrapped in saveLayer) |
Chris Craik | 0b7e824 | 2015-10-28 16:50:44 -0700 | [diff] [blame] | 284 | if (node.getLayer()) { |
| 285 | // HW layer |
John Reck | 7df9ff2 | 2016-02-10 16:08:08 -0800 | [diff] [blame] | 286 | LayerOp* drawLayerOp = mAllocator.create_trivial<LayerOp>(node); |
Chris Craik | 0b7e824 | 2015-10-28 16:50:44 -0700 | [diff] [blame] | 287 | BakedOpState* bakedOpState = tryBakeOpState(*drawLayerOp); |
| 288 | if (bakedOpState) { |
Chris Craik | 8ecf41c | 2015-11-16 10:27:59 -0800 | [diff] [blame] | 289 | // Node's layer already deferred, schedule it to render into parent layer |
Chris Craik | 0b7e824 | 2015-10-28 16:50:44 -0700 | [diff] [blame] | 290 | currentLayer().deferUnmergeableOp(mAllocator, bakedOpState, OpBatchType::Bitmap); |
| 291 | } |
Chris Craik | 8ecf41c | 2015-11-16 10:27:59 -0800 | [diff] [blame] | 292 | } else if (CC_UNLIKELY(!saveLayerBounds.isEmpty())) { |
| 293 | // draw DisplayList contents within temporary, since persisted layer could not be used. |
| 294 | // (temp layers are clipped to viewport, since they don't persist offscreen content) |
| 295 | SkPaint saveLayerPaint; |
| 296 | saveLayerPaint.setAlpha(properties.getAlpha()); |
John Reck | 7df9ff2 | 2016-02-10 16:08:08 -0800 | [diff] [blame] | 297 | deferBeginLayerOp(*mAllocator.create_trivial<BeginLayerOp>( |
Chris Craik | 8ecf41c | 2015-11-16 10:27:59 -0800 | [diff] [blame] | 298 | saveLayerBounds, |
| 299 | Matrix4::identity(), |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 300 | nullptr, // no record-time clip - need only respect defer-time one |
Chris Craik | 8ecf41c | 2015-11-16 10:27:59 -0800 | [diff] [blame] | 301 | &saveLayerPaint)); |
Chris Craik | 8d1f212 | 2015-11-24 16:40:09 -0800 | [diff] [blame] | 302 | deferNodeOps(node); |
John Reck | 7df9ff2 | 2016-02-10 16:08:08 -0800 | [diff] [blame] | 303 | deferEndLayerOp(*mAllocator.create_trivial<EndLayerOp>()); |
Chris Craik | 0b7e824 | 2015-10-28 16:50:44 -0700 | [diff] [blame] | 304 | } else { |
Chris Craik | 8d1f212 | 2015-11-24 16:40:09 -0800 | [diff] [blame] | 305 | deferNodeOps(node); |
Chris Craik | 0b7e824 | 2015-10-28 16:50:44 -0700 | [diff] [blame] | 306 | } |
| 307 | } |
| 308 | } |
| 309 | |
Chris Craik | 161f54b | 2015-11-05 11:08:52 -0800 | [diff] [blame] | 310 | typedef key_value_pair_t<float, const RenderNodeOp*> ZRenderNodeOpPair; |
| 311 | |
| 312 | template <typename V> |
| 313 | static void buildZSortedChildList(V* zTranslatedNodes, |
| 314 | const DisplayList& displayList, const DisplayList::Chunk& chunk) { |
| 315 | if (chunk.beginChildIndex == chunk.endChildIndex) return; |
| 316 | |
| 317 | for (size_t i = chunk.beginChildIndex; i < chunk.endChildIndex; i++) { |
| 318 | RenderNodeOp* childOp = displayList.getChildren()[i]; |
| 319 | RenderNode* child = childOp->renderNode; |
| 320 | float childZ = child->properties().getZ(); |
| 321 | |
| 322 | if (!MathUtils::isZero(childZ) && chunk.reorderChildren) { |
| 323 | zTranslatedNodes->push_back(ZRenderNodeOpPair(childZ, childOp)); |
| 324 | childOp->skipInOrderDraw = true; |
| 325 | } else if (!child->properties().getProjectBackwards()) { |
| 326 | // regular, in order drawing DisplayList |
| 327 | childOp->skipInOrderDraw = false; |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | // Z sort any 3d children (stable-ness makes z compare fall back to standard drawing order) |
| 332 | std::stable_sort(zTranslatedNodes->begin(), zTranslatedNodes->end()); |
| 333 | } |
| 334 | |
| 335 | template <typename V> |
| 336 | static size_t findNonNegativeIndex(const V& zTranslatedNodes) { |
| 337 | for (size_t i = 0; i < zTranslatedNodes.size(); i++) { |
| 338 | if (zTranslatedNodes[i].key >= 0.0f) return i; |
| 339 | } |
| 340 | return zTranslatedNodes.size(); |
| 341 | } |
| 342 | |
| 343 | template <typename V> |
Chris Craik | d645640 | 2016-04-11 12:24:23 -0700 | [diff] [blame] | 344 | void FrameBuilder::defer3dChildren(const ClipBase* reorderClip, ChildrenSelectMode mode, |
| 345 | const V& zTranslatedNodes) { |
Chris Craik | 161f54b | 2015-11-05 11:08:52 -0800 | [diff] [blame] | 346 | const int size = zTranslatedNodes.size(); |
| 347 | if (size == 0 |
| 348 | || (mode == ChildrenSelectMode::Negative&& zTranslatedNodes[0].key > 0.0f) |
| 349 | || (mode == ChildrenSelectMode::Positive && zTranslatedNodes[size - 1].key < 0.0f)) { |
| 350 | // no 3d children to draw |
| 351 | return; |
| 352 | } |
| 353 | |
| 354 | /** |
| 355 | * Draw shadows and (potential) casters mostly in order, but allow the shadows of casters |
| 356 | * with very similar Z heights to draw together. |
| 357 | * |
| 358 | * This way, if Views A & B have the same Z height and are both casting shadows, the shadows are |
| 359 | * underneath both, and neither's shadow is drawn on top of the other. |
| 360 | */ |
| 361 | const size_t nonNegativeIndex = findNonNegativeIndex(zTranslatedNodes); |
| 362 | size_t drawIndex, shadowIndex, endIndex; |
| 363 | if (mode == ChildrenSelectMode::Negative) { |
| 364 | drawIndex = 0; |
| 365 | endIndex = nonNegativeIndex; |
| 366 | shadowIndex = endIndex; // draw no shadows |
| 367 | } else { |
| 368 | drawIndex = nonNegativeIndex; |
| 369 | endIndex = size; |
| 370 | shadowIndex = drawIndex; // potentially draw shadow for each pos Z child |
| 371 | } |
| 372 | |
| 373 | float lastCasterZ = 0.0f; |
| 374 | while (shadowIndex < endIndex || drawIndex < endIndex) { |
| 375 | if (shadowIndex < endIndex) { |
| 376 | const RenderNodeOp* casterNodeOp = zTranslatedNodes[shadowIndex].value; |
| 377 | const float casterZ = zTranslatedNodes[shadowIndex].key; |
| 378 | // attempt to render the shadow if the caster about to be drawn is its caster, |
| 379 | // OR if its caster's Z value is similar to the previous potential caster |
| 380 | if (shadowIndex == drawIndex || casterZ - lastCasterZ < 0.1f) { |
Chris Craik | d645640 | 2016-04-11 12:24:23 -0700 | [diff] [blame] | 381 | deferShadow(reorderClip, *casterNodeOp); |
Chris Craik | 161f54b | 2015-11-05 11:08:52 -0800 | [diff] [blame] | 382 | |
| 383 | lastCasterZ = casterZ; // must do this even if current caster not casting a shadow |
| 384 | shadowIndex++; |
| 385 | continue; |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | const RenderNodeOp* childOp = zTranslatedNodes[drawIndex].value; |
Chris Craik | 268a9c0 | 2015-12-09 18:05:12 -0800 | [diff] [blame] | 390 | deferRenderNodeOpImpl(*childOp); |
Chris Craik | 161f54b | 2015-11-05 11:08:52 -0800 | [diff] [blame] | 391 | drawIndex++; |
| 392 | } |
| 393 | } |
| 394 | |
Chris Craik | d645640 | 2016-04-11 12:24:23 -0700 | [diff] [blame] | 395 | void FrameBuilder::deferShadow(const ClipBase* reorderClip, const RenderNodeOp& casterNodeOp) { |
Chris Craik | d3daa31 | 2015-11-06 10:59:56 -0800 | [diff] [blame] | 396 | auto& node = *casterNodeOp.renderNode; |
| 397 | auto& properties = node.properties(); |
| 398 | |
| 399 | if (properties.getAlpha() <= 0.0f |
| 400 | || properties.getOutline().getAlpha() <= 0.0f |
| 401 | || !properties.getOutline().getPath() |
| 402 | || properties.getScaleX() == 0 |
| 403 | || properties.getScaleY() == 0) { |
| 404 | // no shadow to draw |
| 405 | return; |
| 406 | } |
| 407 | |
| 408 | const SkPath* casterOutlinePath = properties.getOutline().getPath(); |
| 409 | const SkPath* revealClipPath = properties.getRevealClip().getPath(); |
| 410 | if (revealClipPath && revealClipPath->isEmpty()) return; |
| 411 | |
| 412 | float casterAlpha = properties.getAlpha() * properties.getOutline().getAlpha(); |
| 413 | |
| 414 | // holds temporary SkPath to store the result of intersections |
| 415 | SkPath* frameAllocatedPath = nullptr; |
| 416 | const SkPath* casterPath = casterOutlinePath; |
| 417 | |
| 418 | // intersect the shadow-casting path with the reveal, if present |
| 419 | if (revealClipPath) { |
| 420 | frameAllocatedPath = createFrameAllocatedPath(); |
| 421 | |
| 422 | Op(*casterPath, *revealClipPath, kIntersect_SkPathOp, frameAllocatedPath); |
| 423 | casterPath = frameAllocatedPath; |
| 424 | } |
| 425 | |
| 426 | // intersect the shadow-casting path with the clipBounds, if present |
| 427 | if (properties.getClippingFlags() & CLIP_TO_CLIP_BOUNDS) { |
| 428 | if (!frameAllocatedPath) { |
| 429 | frameAllocatedPath = createFrameAllocatedPath(); |
| 430 | } |
| 431 | Rect clipBounds; |
| 432 | properties.getClippingRectForFlags(CLIP_TO_CLIP_BOUNDS, &clipBounds); |
| 433 | SkPath clipBoundsPath; |
| 434 | clipBoundsPath.addRect(clipBounds.left, clipBounds.top, |
| 435 | clipBounds.right, clipBounds.bottom); |
| 436 | |
| 437 | Op(*casterPath, clipBoundsPath, kIntersect_SkPathOp, frameAllocatedPath); |
| 438 | casterPath = frameAllocatedPath; |
| 439 | } |
| 440 | |
Chris Craik | d645640 | 2016-04-11 12:24:23 -0700 | [diff] [blame] | 441 | // apply reorder clip to shadow, so it respects clip at beginning of reorderable chunk |
| 442 | int restoreTo = mCanvasState.save(SaveFlags::MatrixClip); |
| 443 | mCanvasState.writableSnapshot()->applyClip(reorderClip, |
| 444 | *mCanvasState.currentSnapshot()->transform); |
Chris Craik | 6e068c01 | 2016-01-15 16:15:30 -0800 | [diff] [blame] | 445 | if (CC_LIKELY(!mCanvasState.getRenderTargetClipBounds().isEmpty())) { |
| 446 | Matrix4 shadowMatrixXY(casterNodeOp.localMatrix); |
| 447 | Matrix4 shadowMatrixZ(casterNodeOp.localMatrix); |
| 448 | node.applyViewPropertyTransforms(shadowMatrixXY, false); |
| 449 | node.applyViewPropertyTransforms(shadowMatrixZ, true); |
| 450 | |
Chris Craik | 3a5811b | 2016-03-22 15:03:08 -0700 | [diff] [blame] | 451 | sp<TessellationCache::ShadowTask> task = mCaches.tessellationCache.getShadowTask( |
Chris Craik | 6e068c01 | 2016-01-15 16:15:30 -0800 | [diff] [blame] | 452 | mCanvasState.currentTransform(), |
| 453 | mCanvasState.getLocalClipBounds(), |
| 454 | casterAlpha >= 1.0f, |
| 455 | casterPath, |
| 456 | &shadowMatrixXY, &shadowMatrixZ, |
| 457 | mCanvasState.currentSnapshot()->getRelativeLightCenter(), |
| 458 | mLightRadius); |
| 459 | ShadowOp* shadowOp = mAllocator.create<ShadowOp>(task, casterAlpha); |
| 460 | BakedOpState* bakedOpState = BakedOpState::tryShadowOpConstruct( |
| 461 | mAllocator, *mCanvasState.writableSnapshot(), shadowOp); |
| 462 | if (CC_LIKELY(bakedOpState)) { |
| 463 | currentLayer().deferUnmergeableOp(mAllocator, bakedOpState, OpBatchType::Shadow); |
| 464 | } |
Chris Craik | d3daa31 | 2015-11-06 10:59:56 -0800 | [diff] [blame] | 465 | } |
Chris Craik | d645640 | 2016-04-11 12:24:23 -0700 | [diff] [blame] | 466 | mCanvasState.restoreToCount(restoreTo); |
Chris Craik | 161f54b | 2015-11-05 11:08:52 -0800 | [diff] [blame] | 467 | } |
Chris Craik | d3daa31 | 2015-11-06 10:59:56 -0800 | [diff] [blame] | 468 | |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 469 | void FrameBuilder::deferProjectedChildren(const RenderNode& renderNode) { |
Florin Malita | eecff56 | 2015-12-21 10:43:01 -0500 | [diff] [blame] | 470 | int count = mCanvasState.save(SaveFlags::MatrixClip); |
Chris Craik | 678ff81 | 2016-03-01 13:27:54 -0800 | [diff] [blame] | 471 | const SkPath* projectionReceiverOutline = renderNode.properties().getOutline().getPath(); |
Chris Craik | 8d1f212 | 2015-11-24 16:40:09 -0800 | [diff] [blame] | 472 | |
Chris Craik | 678ff81 | 2016-03-01 13:27:54 -0800 | [diff] [blame] | 473 | SkPath transformedMaskPath; // on stack, since BakedOpState makes a deep copy |
| 474 | if (projectionReceiverOutline) { |
| 475 | // transform the mask for this projector into render target space |
| 476 | // TODO: consider combining both transforms by stashing transform instead of applying |
| 477 | SkMatrix skCurrentTransform; |
| 478 | mCanvasState.currentTransform()->copyTo(skCurrentTransform); |
| 479 | projectionReceiverOutline->transform( |
| 480 | skCurrentTransform, |
| 481 | &transformedMaskPath); |
Chris Craik | 5e00c7c | 2016-07-06 16:10:09 -0700 | [diff] [blame] | 482 | mCanvasState.setProjectionPathMask(&transformedMaskPath); |
Chris Craik | 678ff81 | 2016-03-01 13:27:54 -0800 | [diff] [blame] | 483 | } |
Chris Craik | 8d1f212 | 2015-11-24 16:40:09 -0800 | [diff] [blame] | 484 | |
Chris Craik | 8d1f212 | 2015-11-24 16:40:09 -0800 | [diff] [blame] | 485 | for (size_t i = 0; i < renderNode.mProjectedNodes.size(); i++) { |
| 486 | RenderNodeOp* childOp = renderNode.mProjectedNodes[i]; |
Chris Craik | a748c08 | 2016-03-01 18:48:37 -0800 | [diff] [blame] | 487 | RenderNode& childNode = *childOp->renderNode; |
Chris Craik | 678ff81 | 2016-03-01 13:27:54 -0800 | [diff] [blame] | 488 | |
Chris Craik | a748c08 | 2016-03-01 18:48:37 -0800 | [diff] [blame] | 489 | // Draw child if it has content, but ignore state in childOp - matrix already applied to |
| 490 | // transformFromCompositingAncestor, and record-time clip is ignored when projecting |
| 491 | if (!childNode.nothingToDraw()) { |
| 492 | int restoreTo = mCanvasState.save(SaveFlags::MatrixClip); |
Chris Craik | 678ff81 | 2016-03-01 13:27:54 -0800 | [diff] [blame] | 493 | |
Chris Craik | a748c08 | 2016-03-01 18:48:37 -0800 | [diff] [blame] | 494 | // Apply transform between ancestor and projected descendant |
| 495 | mCanvasState.concatMatrix(childOp->transformFromCompositingAncestor); |
| 496 | |
| 497 | deferNodePropsAndOps(childNode); |
| 498 | |
| 499 | mCanvasState.restoreToCount(restoreTo); |
| 500 | } |
Chris Craik | 8d1f212 | 2015-11-24 16:40:09 -0800 | [diff] [blame] | 501 | } |
Chris Craik | 8d1f212 | 2015-11-24 16:40:09 -0800 | [diff] [blame] | 502 | mCanvasState.restoreToCount(count); |
| 503 | } |
| 504 | |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 505 | /** |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 506 | * Used to define a list of lambdas referencing private FrameBuilder::onXX::defer() methods. |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 507 | * |
Chris Craik | 8d1f212 | 2015-11-24 16:40:09 -0800 | [diff] [blame] | 508 | * This allows opIds embedded in the RecordedOps to be used for dispatching to these lambdas. |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 509 | * E.g. a BitmapOp op then would be dispatched to FrameBuilder::onBitmapOp(const BitmapOp&) |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 510 | */ |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 511 | #define OP_RECEIVER(Type) \ |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 512 | [](FrameBuilder& frameBuilder, const RecordedOp& op) { frameBuilder.defer##Type(static_cast<const Type&>(op)); }, |
| 513 | void FrameBuilder::deferNodeOps(const RenderNode& renderNode) { |
| 514 | typedef void (*OpDispatcher) (FrameBuilder& frameBuilder, const RecordedOp& op); |
Chris Craik | 7cbf63d | 2016-01-06 13:46:52 -0800 | [diff] [blame] | 515 | static OpDispatcher receivers[] = BUILD_DEFERRABLE_OP_LUT(OP_RECEIVER); |
Chris Craik | 8d1f212 | 2015-11-24 16:40:09 -0800 | [diff] [blame] | 516 | |
| 517 | // can't be null, since DL=null node rejection happens before deferNodePropsAndOps |
| 518 | const DisplayList& displayList = *(renderNode.getDisplayList()); |
Chris Craik | d645640 | 2016-04-11 12:24:23 -0700 | [diff] [blame] | 519 | for (auto& chunk : displayList.getChunks()) { |
Chris Craik | 161f54b | 2015-11-05 11:08:52 -0800 | [diff] [blame] | 520 | FatVector<ZRenderNodeOpPair, 16> zTranslatedNodes; |
| 521 | buildZSortedChildList(&zTranslatedNodes, displayList, chunk); |
| 522 | |
Chris Craik | d645640 | 2016-04-11 12:24:23 -0700 | [diff] [blame] | 523 | defer3dChildren(chunk.reorderClip, ChildrenSelectMode::Negative, zTranslatedNodes); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 524 | for (size_t opIndex = chunk.beginOpIndex; opIndex < chunk.endOpIndex; opIndex++) { |
Chris Craik | b36af87 | 2015-10-16 14:23:12 -0700 | [diff] [blame] | 525 | const RecordedOp* op = displayList.getOps()[opIndex]; |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 526 | receivers[op->opId](*this, *op); |
Chris Craik | 8d1f212 | 2015-11-24 16:40:09 -0800 | [diff] [blame] | 527 | |
| 528 | if (CC_UNLIKELY(!renderNode.mProjectedNodes.empty() |
| 529 | && displayList.projectionReceiveIndex >= 0 |
| 530 | && static_cast<int>(opIndex) == displayList.projectionReceiveIndex)) { |
| 531 | deferProjectedChildren(renderNode); |
| 532 | } |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 533 | } |
Chris Craik | d645640 | 2016-04-11 12:24:23 -0700 | [diff] [blame] | 534 | defer3dChildren(chunk.reorderClip, ChildrenSelectMode::Positive, zTranslatedNodes); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 535 | } |
| 536 | } |
| 537 | |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 538 | void FrameBuilder::deferRenderNodeOpImpl(const RenderNodeOp& op) { |
Chris Craik | 161f54b | 2015-11-05 11:08:52 -0800 | [diff] [blame] | 539 | if (op.renderNode->nothingToDraw()) return; |
Florin Malita | eecff56 | 2015-12-21 10:43:01 -0500 | [diff] [blame] | 540 | int count = mCanvasState.save(SaveFlags::MatrixClip); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 541 | |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 542 | // apply state from RecordedOp (clip first, since op's clip is transformed by current matrix) |
Chris Craik | 04d46eb | 2016-04-07 13:51:07 -0700 | [diff] [blame] | 543 | mCanvasState.writableSnapshot()->applyClip(op.localClip, |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 544 | *mCanvasState.currentSnapshot()->transform); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 545 | mCanvasState.concatMatrix(op.localMatrix); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 546 | |
Chris Craik | 0b7e824 | 2015-10-28 16:50:44 -0700 | [diff] [blame] | 547 | // then apply state from node properties, and defer ops |
| 548 | deferNodePropsAndOps(*op.renderNode); |
| 549 | |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 550 | mCanvasState.restoreToCount(count); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 551 | } |
| 552 | |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 553 | void FrameBuilder::deferRenderNodeOp(const RenderNodeOp& op) { |
Chris Craik | 161f54b | 2015-11-05 11:08:52 -0800 | [diff] [blame] | 554 | if (!op.skipInOrderDraw) { |
Chris Craik | 268a9c0 | 2015-12-09 18:05:12 -0800 | [diff] [blame] | 555 | deferRenderNodeOpImpl(op); |
Chris Craik | 161f54b | 2015-11-05 11:08:52 -0800 | [diff] [blame] | 556 | } |
| 557 | } |
| 558 | |
Chris Craik | 386aa03 | 2015-12-07 17:08:25 -0800 | [diff] [blame] | 559 | /** |
| 560 | * Defers an unmergeable, strokeable op, accounting correctly |
| 561 | * for paint's style on the bounds being computed. |
| 562 | */ |
Chris Craik | 80d2ade | 2016-03-28 12:54:07 -0700 | [diff] [blame] | 563 | BakedOpState* FrameBuilder::deferStrokeableOp(const RecordedOp& op, batchid_t batchId, |
Chris Craik | 386aa03 | 2015-12-07 17:08:25 -0800 | [diff] [blame] | 564 | BakedOpState::StrokeBehavior strokeBehavior) { |
| 565 | // Note: here we account for stroke when baking the op |
| 566 | BakedOpState* bakedState = BakedOpState::tryStrokeableOpConstruct( |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 567 | mAllocator, *mCanvasState.writableSnapshot(), op, strokeBehavior); |
Chris Craik | 3a5811b | 2016-03-22 15:03:08 -0700 | [diff] [blame] | 568 | if (!bakedState) return nullptr; // quick rejected |
Chris Craik | 80d2ade | 2016-03-28 12:54:07 -0700 | [diff] [blame] | 569 | |
| 570 | if (op.opId == RecordedOpId::RectOp && op.paint->getStyle() != SkPaint::kStroke_Style) { |
| 571 | bakedState->setupOpacity(op.paint); |
| 572 | } |
| 573 | |
Chris Craik | 386aa03 | 2015-12-07 17:08:25 -0800 | [diff] [blame] | 574 | currentLayer().deferUnmergeableOp(mAllocator, bakedState, batchId); |
Chris Craik | 3a5811b | 2016-03-22 15:03:08 -0700 | [diff] [blame] | 575 | return bakedState; |
Chris Craik | 386aa03 | 2015-12-07 17:08:25 -0800 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | /** |
| 579 | * Returns batch id for tessellatable shapes, based on paint. Checks to see if path effect/AA will |
| 580 | * be used, since they trigger significantly different rendering paths. |
| 581 | * |
| 582 | * Note: not used for lines/points, since they don't currently support path effects. |
| 583 | */ |
| 584 | static batchid_t tessBatchId(const RecordedOp& op) { |
| 585 | const SkPaint& paint = *(op.paint); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 586 | return paint.getPathEffect() |
| 587 | ? OpBatchType::AlphaMaskTexture |
| 588 | : (paint.isAntiAlias() ? OpBatchType::AlphaVertices : OpBatchType::Vertices); |
| 589 | } |
| 590 | |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 591 | void FrameBuilder::deferArcOp(const ArcOp& op) { |
Chris Craik | 268a9c0 | 2015-12-09 18:05:12 -0800 | [diff] [blame] | 592 | deferStrokeableOp(op, tessBatchId(op)); |
Chris Craik | 386aa03 | 2015-12-07 17:08:25 -0800 | [diff] [blame] | 593 | } |
| 594 | |
Chris Craik | b87eadd | 2016-01-06 09:16:05 -0800 | [diff] [blame] | 595 | static bool hasMergeableClip(const BakedOpState& state) { |
Chris Craik | f8f56cb | 2016-10-14 15:08:21 -0700 | [diff] [blame] | 596 | return !state.computedState.clipState |
Chris Craik | b87eadd | 2016-01-06 09:16:05 -0800 | [diff] [blame] | 597 | || state.computedState.clipState->mode == ClipMode::Rectangle; |
| 598 | } |
| 599 | |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 600 | void FrameBuilder::deferBitmapOp(const BitmapOp& op) { |
Chris Craik | 15c3f19 | 2015-12-03 12:16:56 -0800 | [diff] [blame] | 601 | BakedOpState* bakedState = tryBakeOpState(op); |
| 602 | if (!bakedState) return; // quick rejected |
sergeyv | a82ffc5 | 2016-04-04 17:12:04 -0700 | [diff] [blame] | 603 | |
| 604 | if (op.bitmap->isOpaque()) { |
| 605 | bakedState->setupOpacity(op.paint); |
| 606 | } |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 607 | |
Chris Craik | 15c3f19 | 2015-12-03 12:16:56 -0800 | [diff] [blame] | 608 | // Don't merge non-simply transformed or neg scale ops, SET_TEXTURE doesn't handle rotation |
| 609 | // Don't merge A8 bitmaps - the paint's color isn't compared by mergeId, or in |
| 610 | // MergingDrawBatch::canMergeWith() |
| 611 | if (bakedState->computedState.transform.isSimple() |
| 612 | && bakedState->computedState.transform.positiveScale() |
Mike Reed | 260ab72 | 2016-10-07 15:59:20 -0400 | [diff] [blame] | 613 | && PaintUtils::getBlendModeDirect(op.paint) == SkBlendMode::kSrcOver |
Chris Craik | b87eadd | 2016-01-06 09:16:05 -0800 | [diff] [blame] | 614 | && op.bitmap->colorType() != kAlpha_8_SkColorType |
| 615 | && hasMergeableClip(*bakedState)) { |
Chris Craik | b250a83 | 2016-01-11 19:28:17 -0800 | [diff] [blame] | 616 | mergeid_t mergeId = reinterpret_cast<mergeid_t>(op.bitmap->getGenerationID()); |
Chris Craik | 15c3f19 | 2015-12-03 12:16:56 -0800 | [diff] [blame] | 617 | currentLayer().deferMergeableOp(mAllocator, bakedState, OpBatchType::Bitmap, mergeId); |
| 618 | } else { |
| 619 | currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Bitmap); |
| 620 | } |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 621 | } |
| 622 | |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 623 | void FrameBuilder::deferBitmapMeshOp(const BitmapMeshOp& op) { |
Chris Craik | f09ff5a | 2015-12-08 17:21:58 -0800 | [diff] [blame] | 624 | BakedOpState* bakedState = tryBakeOpState(op); |
| 625 | if (!bakedState) return; // quick rejected |
| 626 | currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Bitmap); |
| 627 | } |
| 628 | |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 629 | void FrameBuilder::deferBitmapRectOp(const BitmapRectOp& op) { |
Chris Craik | f09ff5a | 2015-12-08 17:21:58 -0800 | [diff] [blame] | 630 | BakedOpState* bakedState = tryBakeOpState(op); |
| 631 | if (!bakedState) return; // quick rejected |
| 632 | currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Bitmap); |
| 633 | } |
| 634 | |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 635 | void FrameBuilder::deferVectorDrawableOp(const VectorDrawableOp& op) { |
sergeyv | ec4a4b1 | 2016-10-20 18:39:04 -0700 | [diff] [blame] | 636 | Bitmap& bitmap = op.vectorDrawable->getBitmapUpdateIfDirty(); |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 637 | SkPaint* paint = op.vectorDrawable->getPaint(); |
John Reck | 7df9ff2 | 2016-02-10 16:08:08 -0800 | [diff] [blame] | 638 | const BitmapRectOp* resolvedOp = mAllocator.create_trivial<BitmapRectOp>(op.unmappedBounds, |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 639 | op.localMatrix, |
| 640 | op.localClip, |
| 641 | paint, |
sergeyv | ec4a4b1 | 2016-10-20 18:39:04 -0700 | [diff] [blame] | 642 | &bitmap, |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 643 | Rect(bitmap.width(), bitmap.height())); |
| 644 | deferBitmapRectOp(*resolvedOp); |
| 645 | } |
| 646 | |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 647 | void FrameBuilder::deferCirclePropsOp(const CirclePropsOp& op) { |
Chris Craik | 268a9c0 | 2015-12-09 18:05:12 -0800 | [diff] [blame] | 648 | // allocate a temporary oval op (with mAllocator, so it persists until render), so the |
| 649 | // renderer doesn't have to handle the RoundRectPropsOp type, and so state baking is simple. |
| 650 | float x = *(op.x); |
| 651 | float y = *(op.y); |
| 652 | float radius = *(op.radius); |
| 653 | Rect unmappedBounds(x - radius, y - radius, x + radius, y + radius); |
John Reck | 7df9ff2 | 2016-02-10 16:08:08 -0800 | [diff] [blame] | 654 | const OvalOp* resolvedOp = mAllocator.create_trivial<OvalOp>( |
Chris Craik | 268a9c0 | 2015-12-09 18:05:12 -0800 | [diff] [blame] | 655 | unmappedBounds, |
| 656 | op.localMatrix, |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 657 | op.localClip, |
Chris Craik | 268a9c0 | 2015-12-09 18:05:12 -0800 | [diff] [blame] | 658 | op.paint); |
| 659 | deferOvalOp(*resolvedOp); |
| 660 | } |
| 661 | |
Chris Craik | a204848 | 2016-03-25 14:17:49 -0700 | [diff] [blame] | 662 | void FrameBuilder::deferColorOp(const ColorOp& op) { |
| 663 | BakedOpState* bakedState = tryBakeUnboundedOpState(op); |
| 664 | if (!bakedState) return; // quick rejected |
| 665 | currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Vertices); |
| 666 | } |
| 667 | |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 668 | void FrameBuilder::deferFunctorOp(const FunctorOp& op) { |
Chris Craik | 4c3980b | 2016-03-15 14:20:18 -0700 | [diff] [blame] | 669 | BakedOpState* bakedState = tryBakeUnboundedOpState(op); |
Chris Craik | e29ce6f | 2015-12-10 16:25:13 -0800 | [diff] [blame] | 670 | if (!bakedState) return; // quick rejected |
Chris Craik | d2dfd8f | 2015-12-16 14:27:20 -0800 | [diff] [blame] | 671 | currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Functor); |
Chris Craik | e29ce6f | 2015-12-10 16:25:13 -0800 | [diff] [blame] | 672 | } |
| 673 | |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 674 | void FrameBuilder::deferLinesOp(const LinesOp& op) { |
Chris Craik | 386aa03 | 2015-12-07 17:08:25 -0800 | [diff] [blame] | 675 | batchid_t batch = op.paint->isAntiAlias() ? OpBatchType::AlphaVertices : OpBatchType::Vertices; |
Chris Craik | 268a9c0 | 2015-12-09 18:05:12 -0800 | [diff] [blame] | 676 | deferStrokeableOp(op, batch, BakedOpState::StrokeBehavior::Forced); |
Chris Craik | 386aa03 | 2015-12-07 17:08:25 -0800 | [diff] [blame] | 677 | } |
| 678 | |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 679 | void FrameBuilder::deferOvalOp(const OvalOp& op) { |
Chris Craik | 268a9c0 | 2015-12-09 18:05:12 -0800 | [diff] [blame] | 680 | deferStrokeableOp(op, tessBatchId(op)); |
Chris Craik | 386aa03 | 2015-12-07 17:08:25 -0800 | [diff] [blame] | 681 | } |
| 682 | |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 683 | void FrameBuilder::deferPatchOp(const PatchOp& op) { |
Chris Craik | f09ff5a | 2015-12-08 17:21:58 -0800 | [diff] [blame] | 684 | BakedOpState* bakedState = tryBakeOpState(op); |
| 685 | if (!bakedState) return; // quick rejected |
| 686 | |
| 687 | if (bakedState->computedState.transform.isPureTranslate() |
Mike Reed | 260ab72 | 2016-10-07 15:59:20 -0400 | [diff] [blame] | 688 | && PaintUtils::getBlendModeDirect(op.paint) == SkBlendMode::kSrcOver |
Chris Craik | b87eadd | 2016-01-06 09:16:05 -0800 | [diff] [blame] | 689 | && hasMergeableClip(*bakedState)) { |
Chris Craik | b250a83 | 2016-01-11 19:28:17 -0800 | [diff] [blame] | 690 | mergeid_t mergeId = reinterpret_cast<mergeid_t>(op.bitmap->getGenerationID()); |
Chris Craik | f09ff5a | 2015-12-08 17:21:58 -0800 | [diff] [blame] | 691 | |
| 692 | // Only use the MergedPatch batchId when merged, so Bitmap+Patch don't try to merge together |
| 693 | currentLayer().deferMergeableOp(mAllocator, bakedState, OpBatchType::MergedPatch, mergeId); |
| 694 | } else { |
| 695 | // Use Bitmap batchId since Bitmap+Patch use same shader |
| 696 | currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Bitmap); |
| 697 | } |
| 698 | } |
| 699 | |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 700 | void FrameBuilder::deferPathOp(const PathOp& op) { |
Chris Craik | 3a5811b | 2016-03-22 15:03:08 -0700 | [diff] [blame] | 701 | auto state = deferStrokeableOp(op, OpBatchType::AlphaMaskTexture); |
| 702 | if (CC_LIKELY(state)) { |
| 703 | mCaches.pathCache.precache(op.path, op.paint); |
| 704 | } |
Chris Craik | 386aa03 | 2015-12-07 17:08:25 -0800 | [diff] [blame] | 705 | } |
| 706 | |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 707 | void FrameBuilder::deferPointsOp(const PointsOp& op) { |
Chris Craik | 386aa03 | 2015-12-07 17:08:25 -0800 | [diff] [blame] | 708 | batchid_t batch = op.paint->isAntiAlias() ? OpBatchType::AlphaVertices : OpBatchType::Vertices; |
Chris Craik | 268a9c0 | 2015-12-09 18:05:12 -0800 | [diff] [blame] | 709 | deferStrokeableOp(op, batch, BakedOpState::StrokeBehavior::Forced); |
Chris Craik | a171727 | 2015-11-19 13:02:43 -0800 | [diff] [blame] | 710 | } |
| 711 | |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 712 | void FrameBuilder::deferRectOp(const RectOp& op) { |
Chris Craik | 268a9c0 | 2015-12-09 18:05:12 -0800 | [diff] [blame] | 713 | deferStrokeableOp(op, tessBatchId(op)); |
Chris Craik | 386aa03 | 2015-12-07 17:08:25 -0800 | [diff] [blame] | 714 | } |
| 715 | |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 716 | void FrameBuilder::deferRoundRectOp(const RoundRectOp& op) { |
Chris Craik | 3a5811b | 2016-03-22 15:03:08 -0700 | [diff] [blame] | 717 | auto state = deferStrokeableOp(op, tessBatchId(op)); |
| 718 | if (CC_LIKELY(state && !op.paint->getPathEffect())) { |
| 719 | // TODO: consider storing tessellation task in BakedOpState |
| 720 | mCaches.tessellationCache.precacheRoundRect(state->computedState.transform, *(op.paint), |
| 721 | op.unmappedBounds.getWidth(), op.unmappedBounds.getHeight(), op.rx, op.ry); |
| 722 | } |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 723 | } |
| 724 | |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 725 | void FrameBuilder::deferRoundRectPropsOp(const RoundRectPropsOp& op) { |
Chris Craik | 268a9c0 | 2015-12-09 18:05:12 -0800 | [diff] [blame] | 726 | // allocate a temporary round rect op (with mAllocator, so it persists until render), so the |
| 727 | // renderer doesn't have to handle the RoundRectPropsOp type, and so state baking is simple. |
John Reck | 7df9ff2 | 2016-02-10 16:08:08 -0800 | [diff] [blame] | 728 | const RoundRectOp* resolvedOp = mAllocator.create_trivial<RoundRectOp>( |
Chris Craik | 268a9c0 | 2015-12-09 18:05:12 -0800 | [diff] [blame] | 729 | Rect(*(op.left), *(op.top), *(op.right), *(op.bottom)), |
| 730 | op.localMatrix, |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 731 | op.localClip, |
Chris Craik | 268a9c0 | 2015-12-09 18:05:12 -0800 | [diff] [blame] | 732 | op.paint, *op.rx, *op.ry); |
| 733 | deferRoundRectOp(*resolvedOp); |
| 734 | } |
| 735 | |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 736 | void FrameBuilder::deferSimpleRectsOp(const SimpleRectsOp& op) { |
Chris Craik | 15c3f19 | 2015-12-03 12:16:56 -0800 | [diff] [blame] | 737 | BakedOpState* bakedState = tryBakeOpState(op); |
| 738 | if (!bakedState) return; // quick rejected |
| 739 | currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Vertices); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 740 | } |
| 741 | |
Chris Craik | d7448e6 | 2015-12-15 10:34:36 -0800 | [diff] [blame] | 742 | static batchid_t textBatchId(const SkPaint& paint) { |
| 743 | // TODO: better handling of shader (since we won't care about color then) |
| 744 | return paint.getColor() == SK_ColorBLACK ? OpBatchType::Text : OpBatchType::ColorText; |
| 745 | } |
| 746 | |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 747 | void FrameBuilder::deferTextOp(const TextOp& op) { |
Chris Craik | 7c02cab | 2016-03-16 17:15:12 -0700 | [diff] [blame] | 748 | BakedOpState* bakedState = BakedOpState::tryStrokeableOpConstruct( |
| 749 | mAllocator, *mCanvasState.writableSnapshot(), op, |
| 750 | BakedOpState::StrokeBehavior::StyleDefined); |
Chris Craik | 15c3f19 | 2015-12-03 12:16:56 -0800 | [diff] [blame] | 751 | if (!bakedState) return; // quick rejected |
Chris Craik | a171727 | 2015-11-19 13:02:43 -0800 | [diff] [blame] | 752 | |
Chris Craik | d7448e6 | 2015-12-15 10:34:36 -0800 | [diff] [blame] | 753 | batchid_t batchId = textBatchId(*(op.paint)); |
Chris Craik | 15c3f19 | 2015-12-03 12:16:56 -0800 | [diff] [blame] | 754 | if (bakedState->computedState.transform.isPureTranslate() |
Mike Reed | 260ab72 | 2016-10-07 15:59:20 -0400 | [diff] [blame] | 755 | && PaintUtils::getBlendModeDirect(op.paint) == SkBlendMode::kSrcOver |
Chris Craik | b87eadd | 2016-01-06 09:16:05 -0800 | [diff] [blame] | 756 | && hasMergeableClip(*bakedState)) { |
Chris Craik | 15c3f19 | 2015-12-03 12:16:56 -0800 | [diff] [blame] | 757 | mergeid_t mergeId = reinterpret_cast<mergeid_t>(op.paint->getColor()); |
| 758 | currentLayer().deferMergeableOp(mAllocator, bakedState, batchId, mergeId); |
| 759 | } else { |
| 760 | currentLayer().deferUnmergeableOp(mAllocator, bakedState, batchId); |
| 761 | } |
Chris Craik | 3a5811b | 2016-03-22 15:03:08 -0700 | [diff] [blame] | 762 | |
| 763 | FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(); |
| 764 | auto& totalTransform = bakedState->computedState.transform; |
| 765 | if (totalTransform.isPureTranslate() || totalTransform.isPerspective()) { |
| 766 | fontRenderer.precache(op.paint, op.glyphs, op.glyphCount, SkMatrix::I()); |
| 767 | } else { |
| 768 | // Partial transform case, see BakedOpDispatcher::renderTextOp |
| 769 | float sx, sy; |
| 770 | totalTransform.decomposeScale(sx, sy); |
| 771 | fontRenderer.precache(op.paint, op.glyphs, op.glyphCount, SkMatrix::MakeScale( |
| 772 | roundf(std::max(1.0f, sx)), |
| 773 | roundf(std::max(1.0f, sy)))); |
| 774 | } |
Chris Craik | a171727 | 2015-11-19 13:02:43 -0800 | [diff] [blame] | 775 | } |
| 776 | |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 777 | void FrameBuilder::deferTextOnPathOp(const TextOnPathOp& op) { |
Chris Craik | 4c3980b | 2016-03-15 14:20:18 -0700 | [diff] [blame] | 778 | BakedOpState* bakedState = tryBakeUnboundedOpState(op); |
Chris Craik | d7448e6 | 2015-12-15 10:34:36 -0800 | [diff] [blame] | 779 | if (!bakedState) return; // quick rejected |
| 780 | currentLayer().deferUnmergeableOp(mAllocator, bakedState, textBatchId(*(op.paint))); |
Chris Craik | 3a5811b | 2016-03-22 15:03:08 -0700 | [diff] [blame] | 781 | |
| 782 | mCaches.fontRenderer.getFontRenderer().precache( |
| 783 | op.paint, op.glyphs, op.glyphCount, SkMatrix::I()); |
Chris Craik | d7448e6 | 2015-12-15 10:34:36 -0800 | [diff] [blame] | 784 | } |
| 785 | |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 786 | void FrameBuilder::deferTextureLayerOp(const TextureLayerOp& op) { |
John Reck | 417ed6d | 2016-03-22 16:01:08 -0700 | [diff] [blame] | 787 | if (CC_UNLIKELY(!op.layer->isRenderable())) return; |
Chris Craik | aafb01d | 2016-03-25 18:34:11 -0700 | [diff] [blame] | 788 | |
| 789 | const TextureLayerOp* textureLayerOp = &op; |
| 790 | // Now safe to access transform (which was potentially unready at record time) |
| 791 | if (!op.layer->getTransform().isIdentity()) { |
| 792 | // non-identity transform present, so 'inject it' into op by copying + replacing matrix |
| 793 | Matrix4 combinedMatrix(op.localMatrix); |
| 794 | combinedMatrix.multiply(op.layer->getTransform()); |
| 795 | textureLayerOp = mAllocator.create<TextureLayerOp>(op, combinedMatrix); |
| 796 | } |
| 797 | BakedOpState* bakedState = tryBakeOpState(*textureLayerOp); |
| 798 | |
Chris Craik | d2dfd8f | 2015-12-16 14:27:20 -0800 | [diff] [blame] | 799 | if (!bakedState) return; // quick rejected |
| 800 | currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::TextureLayer); |
| 801 | } |
| 802 | |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 803 | void FrameBuilder::saveForLayer(uint32_t layerWidth, uint32_t layerHeight, |
Chris Craik | 8ecf41c | 2015-11-16 10:27:59 -0800 | [diff] [blame] | 804 | float contentTranslateX, float contentTranslateY, |
| 805 | const Rect& repaintRect, |
| 806 | const Vector3& lightCenter, |
Chris Craik | 0b7e824 | 2015-10-28 16:50:44 -0700 | [diff] [blame] | 807 | const BeginLayerOp* beginLayerOp, RenderNode* renderNode) { |
Florin Malita | eecff56 | 2015-12-21 10:43:01 -0500 | [diff] [blame] | 808 | mCanvasState.save(SaveFlags::MatrixClip); |
Chris Craik | 818c9fb | 2015-10-23 14:33:42 -0700 | [diff] [blame] | 809 | mCanvasState.writableSnapshot()->initializeViewport(layerWidth, layerHeight); |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 810 | mCanvasState.writableSnapshot()->roundRectClipState = nullptr; |
Chris Craik | 98787e6 | 2015-11-13 10:55:30 -0800 | [diff] [blame] | 811 | mCanvasState.writableSnapshot()->setRelativeLightCenter(lightCenter); |
Chris Craik | 8ecf41c | 2015-11-16 10:27:59 -0800 | [diff] [blame] | 812 | mCanvasState.writableSnapshot()->transform->loadTranslate( |
| 813 | contentTranslateX, contentTranslateY, 0); |
| 814 | mCanvasState.writableSnapshot()->setClip( |
| 815 | repaintRect.left, repaintRect.top, repaintRect.right, repaintRect.bottom); |
Chris Craik | 98787e6 | 2015-11-13 10:55:30 -0800 | [diff] [blame] | 816 | |
Chris Craik | 8ecf41c | 2015-11-16 10:27:59 -0800 | [diff] [blame] | 817 | // create a new layer repaint, and push its index on the stack |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 818 | mLayerStack.push_back(mLayerBuilders.size()); |
| 819 | auto newFbo = mAllocator.create<LayerBuilder>(layerWidth, layerHeight, |
Chris Craik | 84ad614 | 2016-01-12 12:09:19 -0800 | [diff] [blame] | 820 | repaintRect, beginLayerOp, renderNode); |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 821 | mLayerBuilders.push_back(newFbo); |
Chris Craik | 0b7e824 | 2015-10-28 16:50:44 -0700 | [diff] [blame] | 822 | } |
| 823 | |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 824 | void FrameBuilder::restoreForLayer() { |
Chris Craik | 0b7e824 | 2015-10-28 16:50:44 -0700 | [diff] [blame] | 825 | // restore canvas, and pop finished layer off of the stack |
| 826 | mCanvasState.restore(); |
| 827 | mLayerStack.pop_back(); |
| 828 | } |
| 829 | |
Chris Craik | b87eadd | 2016-01-06 09:16:05 -0800 | [diff] [blame] | 830 | // TODO: defer time rejection (when bounds become empty) + tests |
| 831 | // Option - just skip layers with no bounds at playback + defer? |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 832 | void FrameBuilder::deferBeginLayerOp(const BeginLayerOp& op) { |
Chris Craik | 8ecf41c | 2015-11-16 10:27:59 -0800 | [diff] [blame] | 833 | uint32_t layerWidth = (uint32_t) op.unmappedBounds.getWidth(); |
| 834 | uint32_t layerHeight = (uint32_t) op.unmappedBounds.getHeight(); |
| 835 | |
| 836 | auto previous = mCanvasState.currentSnapshot(); |
| 837 | Vector3 lightCenter = previous->getRelativeLightCenter(); |
| 838 | |
| 839 | // Combine all transforms used to present saveLayer content: |
| 840 | // parent content transform * canvas transform * bounds offset |
Chris Craik | b87eadd | 2016-01-06 09:16:05 -0800 | [diff] [blame] | 841 | Matrix4 contentTransform(*(previous->transform)); |
Chris Craik | 8ecf41c | 2015-11-16 10:27:59 -0800 | [diff] [blame] | 842 | contentTransform.multiply(op.localMatrix); |
| 843 | contentTransform.translate(op.unmappedBounds.left, op.unmappedBounds.top); |
| 844 | |
| 845 | Matrix4 inverseContentTransform; |
| 846 | inverseContentTransform.loadInverse(contentTransform); |
| 847 | |
| 848 | // map the light center into layer-relative space |
| 849 | inverseContentTransform.mapPoint3d(lightCenter); |
| 850 | |
| 851 | // Clip bounds of temporary layer to parent's clip rect, so: |
| 852 | Rect saveLayerBounds(layerWidth, layerHeight); |
| 853 | // 1) transform Rect(width, height) into parent's space |
| 854 | // note: left/top offsets put in contentTransform above |
| 855 | contentTransform.mapRect(saveLayerBounds); |
| 856 | // 2) intersect with parent's clip |
| 857 | saveLayerBounds.doIntersect(previous->getRenderTargetClip()); |
| 858 | // 3) and transform back |
| 859 | inverseContentTransform.mapRect(saveLayerBounds); |
| 860 | saveLayerBounds.doIntersect(Rect(layerWidth, layerHeight)); |
| 861 | saveLayerBounds.roundOut(); |
| 862 | |
| 863 | // if bounds are reduced, will clip the layer's area by reducing required bounds... |
| 864 | layerWidth = saveLayerBounds.getWidth(); |
| 865 | layerHeight = saveLayerBounds.getHeight(); |
| 866 | // ...and shifting drawing content to account for left/top side clipping |
| 867 | float contentTranslateX = -saveLayerBounds.left; |
| 868 | float contentTranslateY = -saveLayerBounds.top; |
| 869 | |
| 870 | saveForLayer(layerWidth, layerHeight, |
| 871 | contentTranslateX, contentTranslateY, |
| 872 | Rect(layerWidth, layerHeight), |
| 873 | lightCenter, |
| 874 | &op, nullptr); |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 875 | } |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 876 | |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 877 | void FrameBuilder::deferEndLayerOp(const EndLayerOp& /* ignored */) { |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 878 | const BeginLayerOp& beginLayerOp = *currentLayer().beginLayerOp; |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 879 | int finishedLayerIndex = mLayerStack.back(); |
Chris Craik | 0b7e824 | 2015-10-28 16:50:44 -0700 | [diff] [blame] | 880 | |
| 881 | restoreForLayer(); |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 882 | |
John Reck | c9bb1a3 | 2016-05-24 15:06:01 -0700 | [diff] [blame] | 883 | // saveLayer will clip & translate the draw contents, so we need |
| 884 | // to translate the drawLayer by how much the contents was translated |
| 885 | // TODO: Unify this with beginLayerOp so we don't have to calculate this |
| 886 | // twice |
| 887 | uint32_t layerWidth = (uint32_t) beginLayerOp.unmappedBounds.getWidth(); |
| 888 | uint32_t layerHeight = (uint32_t) beginLayerOp.unmappedBounds.getHeight(); |
| 889 | |
| 890 | auto previous = mCanvasState.currentSnapshot(); |
| 891 | Vector3 lightCenter = previous->getRelativeLightCenter(); |
| 892 | |
| 893 | // Combine all transforms used to present saveLayer content: |
| 894 | // parent content transform * canvas transform * bounds offset |
| 895 | Matrix4 contentTransform(*(previous->transform)); |
| 896 | contentTransform.multiply(beginLayerOp.localMatrix); |
| 897 | contentTransform.translate(beginLayerOp.unmappedBounds.left, |
| 898 | beginLayerOp.unmappedBounds.top); |
| 899 | |
| 900 | Matrix4 inverseContentTransform; |
| 901 | inverseContentTransform.loadInverse(contentTransform); |
| 902 | |
| 903 | // map the light center into layer-relative space |
| 904 | inverseContentTransform.mapPoint3d(lightCenter); |
| 905 | |
| 906 | // Clip bounds of temporary layer to parent's clip rect, so: |
| 907 | Rect saveLayerBounds(layerWidth, layerHeight); |
| 908 | // 1) transform Rect(width, height) into parent's space |
| 909 | // note: left/top offsets put in contentTransform above |
| 910 | contentTransform.mapRect(saveLayerBounds); |
| 911 | // 2) intersect with parent's clip |
| 912 | saveLayerBounds.doIntersect(previous->getRenderTargetClip()); |
| 913 | // 3) and transform back |
| 914 | inverseContentTransform.mapRect(saveLayerBounds); |
| 915 | saveLayerBounds.doIntersect(Rect(layerWidth, layerHeight)); |
| 916 | saveLayerBounds.roundOut(); |
| 917 | |
| 918 | Matrix4 localMatrix(beginLayerOp.localMatrix); |
| 919 | localMatrix.translate(saveLayerBounds.left, saveLayerBounds.top); |
| 920 | |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 921 | // record the draw operation into the previous layer's list of draw commands |
| 922 | // uses state from the associated beginLayerOp, since it has all the state needed for drawing |
John Reck | 7df9ff2 | 2016-02-10 16:08:08 -0800 | [diff] [blame] | 923 | LayerOp* drawLayerOp = mAllocator.create_trivial<LayerOp>( |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 924 | beginLayerOp.unmappedBounds, |
John Reck | c9bb1a3 | 2016-05-24 15:06:01 -0700 | [diff] [blame] | 925 | localMatrix, |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 926 | beginLayerOp.localClip, |
Chris Craik | 818c9fb | 2015-10-23 14:33:42 -0700 | [diff] [blame] | 927 | beginLayerOp.paint, |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 928 | &(mLayerBuilders[finishedLayerIndex]->offscreenBuffer)); |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 929 | BakedOpState* bakedOpState = tryBakeOpState(*drawLayerOp); |
| 930 | |
| 931 | if (bakedOpState) { |
| 932 | // Layer will be drawn into parent layer (which is now current, since we popped mLayerStack) |
| 933 | currentLayer().deferUnmergeableOp(mAllocator, bakedOpState, OpBatchType::Bitmap); |
| 934 | } else { |
| 935 | // Layer won't be drawn - delete its drawing batches to prevent it from doing any work |
Chris Craik | b87eadd | 2016-01-06 09:16:05 -0800 | [diff] [blame] | 936 | // TODO: need to prevent any render work from being done |
| 937 | // - create layerop earlier for reject purposes? |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 938 | mLayerBuilders[finishedLayerIndex]->clear(); |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 939 | return; |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 940 | } |
| 941 | } |
| 942 | |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 943 | void FrameBuilder::deferBeginUnclippedLayerOp(const BeginUnclippedLayerOp& op) { |
Chris Craik | b87eadd | 2016-01-06 09:16:05 -0800 | [diff] [blame] | 944 | Matrix4 boundsTransform(*(mCanvasState.currentSnapshot()->transform)); |
| 945 | boundsTransform.multiply(op.localMatrix); |
| 946 | |
| 947 | Rect dstRect(op.unmappedBounds); |
| 948 | boundsTransform.mapRect(dstRect); |
Chris Craik | d5a9011 | 2016-06-24 13:53:37 -0700 | [diff] [blame] | 949 | dstRect.roundOut(); |
Chris Craik | b87eadd | 2016-01-06 09:16:05 -0800 | [diff] [blame] | 950 | dstRect.doIntersect(mCanvasState.currentSnapshot()->getRenderTargetClip()); |
| 951 | |
Chris Craik | 4876de1 | 2016-02-25 16:54:08 -0800 | [diff] [blame] | 952 | if (dstRect.isEmpty()) { |
| 953 | // Unclipped layer rejected - push a null op, so next EndUnclippedLayerOp is ignored |
| 954 | currentLayer().activeUnclippedSaveLayers.push_back(nullptr); |
| 955 | } else { |
| 956 | // Allocate a holding position for the layer object (copyTo will produce, copyFrom will consume) |
| 957 | OffscreenBuffer** layerHandle = mAllocator.create<OffscreenBuffer*>(nullptr); |
Chris Craik | b87eadd | 2016-01-06 09:16:05 -0800 | [diff] [blame] | 958 | |
Chris Craik | 4876de1 | 2016-02-25 16:54:08 -0800 | [diff] [blame] | 959 | /** |
| 960 | * First, defer an operation to copy out the content from the rendertarget into a layer. |
| 961 | */ |
| 962 | auto copyToOp = mAllocator.create_trivial<CopyToLayerOp>(op, layerHandle); |
| 963 | BakedOpState* bakedState = BakedOpState::directConstruct(mAllocator, |
| 964 | &(currentLayer().repaintClip), dstRect, *copyToOp); |
| 965 | currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::CopyToLayer); |
Chris Craik | b87eadd | 2016-01-06 09:16:05 -0800 | [diff] [blame] | 966 | |
Chris Craik | 4876de1 | 2016-02-25 16:54:08 -0800 | [diff] [blame] | 967 | /** |
| 968 | * Defer a clear rect, so that clears from multiple unclipped layers can be drawn |
| 969 | * both 1) simultaneously, and 2) as long after the copyToLayer executes as possible |
| 970 | */ |
| 971 | currentLayer().deferLayerClear(dstRect); |
Chris Craik | b87eadd | 2016-01-06 09:16:05 -0800 | [diff] [blame] | 972 | |
Chris Craik | 4876de1 | 2016-02-25 16:54:08 -0800 | [diff] [blame] | 973 | /** |
| 974 | * And stash an operation to copy that layer back under the rendertarget until |
| 975 | * a balanced EndUnclippedLayerOp is seen |
| 976 | */ |
| 977 | auto copyFromOp = mAllocator.create_trivial<CopyFromLayerOp>(op, layerHandle); |
| 978 | bakedState = BakedOpState::directConstruct(mAllocator, |
| 979 | &(currentLayer().repaintClip), dstRect, *copyFromOp); |
| 980 | currentLayer().activeUnclippedSaveLayers.push_back(bakedState); |
| 981 | } |
Chris Craik | b87eadd | 2016-01-06 09:16:05 -0800 | [diff] [blame] | 982 | } |
| 983 | |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 984 | void FrameBuilder::deferEndUnclippedLayerOp(const EndUnclippedLayerOp& /* ignored */) { |
Chris Craik | b87eadd | 2016-01-06 09:16:05 -0800 | [diff] [blame] | 985 | LOG_ALWAYS_FATAL_IF(currentLayer().activeUnclippedSaveLayers.empty(), "no layer to end!"); |
| 986 | |
| 987 | BakedOpState* copyFromLayerOp = currentLayer().activeUnclippedSaveLayers.back(); |
Chris Craik | b87eadd | 2016-01-06 09:16:05 -0800 | [diff] [blame] | 988 | currentLayer().activeUnclippedSaveLayers.pop_back(); |
Chris Craik | 4876de1 | 2016-02-25 16:54:08 -0800 | [diff] [blame] | 989 | if (copyFromLayerOp) { |
| 990 | currentLayer().deferUnmergeableOp(mAllocator, copyFromLayerOp, OpBatchType::CopyFromLayer); |
| 991 | } |
Chris Craik | b87eadd | 2016-01-06 09:16:05 -0800 | [diff] [blame] | 992 | } |
| 993 | |
Chris Craik | 3a5811b | 2016-03-22 15:03:08 -0700 | [diff] [blame] | 994 | void FrameBuilder::finishDefer() { |
| 995 | mCaches.fontRenderer.endPrecaching(); |
| 996 | } |
| 997 | |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 998 | } // namespace uirenderer |
| 999 | } // namespace android |