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