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