1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #include "FrameBuilder.h" 18 19 #include "DeferredLayerUpdater.h" 20 #include "LayerUpdateQueue.h" 21 #include "RenderNode.h" 22 #include "VectorDrawable.h" 23 #include "hwui/Canvas.h" 24 #include "renderstate/OffscreenBufferPool.h" 25 #include "utils/FatVector.h" 26 #include "utils/PaintUtils.h" 27 #include "utils/TraceUtils.h" 28 29 #include <SkPathOps.h> 30 #include <utils/TypeHelpers.h> 31 32 namespace android { 33 namespace uirenderer { 34 35 FrameBuilder::FrameBuilder(const SkRect& clip, uint32_t viewportWidth, uint32_t viewportHeight, 36 const LightGeometry& lightGeometry, Caches& caches) 37 : mStdAllocator(mAllocator) 38 , mLayerBuilders(mStdAllocator) 39 , mLayerStack(mStdAllocator) 40 , mCanvasState(*this) 41 , mCaches(caches) 42 , mLightRadius(lightGeometry.radius) 43 , mDrawFbo0(true) { 44 // Prepare to defer Fbo0 45 auto fbo0 = mAllocator.create<LayerBuilder>(viewportWidth, viewportHeight, Rect(clip)); 46 mLayerBuilders.push_back(fbo0); 47 mLayerStack.push_back(0); 48 mCanvasState.initializeSaveStack(viewportWidth, viewportHeight, clip.fLeft, clip.fTop, 49 clip.fRight, clip.fBottom, lightGeometry.center); 50 } 51 52 FrameBuilder::FrameBuilder(const LayerUpdateQueue& layers, const LightGeometry& lightGeometry, 53 Caches& caches) 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); 67 mCanvasState.initializeSaveStack(1, 1, 0, 0, 1, 1, lightGeometry.center); 68 69 deferLayers(layers); 70 } 71 72 void FrameBuilder::deferLayers(const LayerUpdateQueue& layers) { 73 // Render all layers to be updated, in order. Defer in reverse order, so that they'll be 74 // updated in the order they're passed in (mLayerBuilders are issued to Renderer in reverse) 75 for (int i = layers.entries().size() - 1; i >= 0; i--) { 76 RenderNode* layerNode = layers.entries()[i].renderNode.get(); 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 80 OffscreenBuffer* layer = layerNode->getLayer(); 81 if (CC_LIKELY(layer)) { 82 ATRACE_FORMAT("Optimize HW Layer DisplayList %s %ux%u", layerNode->getName(), 83 layerNode->getWidth(), layerNode->getHeight()); 84 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); 88 layerNode->computeOrdering(); 89 90 // map current light center into RenderNode's coordinate space 91 Vector3 lightCenter = mCanvasState.currentSnapshot()->getRelativeLightCenter(); 92 layer->inverseTransformInWindow.mapPoint3d(lightCenter); 93 94 saveForLayer(layerNode->getWidth(), layerNode->getHeight(), 0, 0, layerDamage, 95 lightCenter, nullptr, layerNode); 96 97 if (layerNode->getDisplayList()) { 98 deferNodeOps(*layerNode); 99 } 100 restoreForLayer(); 101 } 102 } 103 } 104 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, 119 SkClipOp::kIntersect); 120 deferNodePropsAndOps(renderNode); 121 mCanvasState.restore(); 122 } 123 124 static Rect nodeBounds(RenderNode& node) { 125 auto& props = node.properties(); 126 return Rect(props.getLeft(), props.getTop(), props.getRight(), props.getBottom()); 127 } 128 129 void FrameBuilder::deferRenderNodeScene(const std::vector<sp<RenderNode> >& nodes, 130 const Rect& contentDrawBounds) { 131 if (nodes.size() < 1) return; 132 if (nodes.size() == 1) { 133 if (!nodes[0]->nothingToDraw()) { 134 deferRenderNode(*nodes[0]); 135 } 136 return; 137 } 138 // It there are multiple render nodes, they are laid out as follows: 139 // #0 - backdrop (content + caption) 140 // #1 - content (local bounds are at (0,0), will be translated and clipped to backdrop) 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 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. 152 153 // Backdrop bounds in render target space 154 const Rect backdrop = nodeBounds(*nodes[0]); 155 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 167 deferRenderNode(0, 0, 168 Rect(content.right, backdrop.top, backdrop.right, backdrop.bottom), 169 *nodes[0]); 170 } 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 174 deferRenderNode(0, 0, 175 Rect(content.left, content.bottom, content.right, backdrop.bottom), 176 *nodes[0]); 177 } 178 } 179 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; 185 186 Rect contentLocalClip = backdrop; 187 contentLocalClip.translate(dx, dy); 188 deferRenderNode(-dx, -dy, contentLocalClip, *nodes[1]); 189 } else { 190 deferRenderNode(*nodes[1]); 191 } 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 } 199 } 200 } 201 202 void FrameBuilder::onViewportInitialized() {} 203 204 void FrameBuilder::onSnapshotRestored(const Snapshot& removed, const Snapshot& restored) {} 205 206 void FrameBuilder::deferNodePropsAndOps(RenderNode& node) { 207 const RenderProperties& properties = node.properties(); 208 const Outline& outline = properties.getOutline(); 209 if (properties.getAlpha() <= 0 || (outline.getShouldClip() && outline.isEmpty()) || 210 properties.getScaleX() == 0 || properties.getScaleY() == 0) { 211 return; // rejected 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 233 Rect saveLayerBounds; // will be set to non-empty if saveLayer needed 234 const bool isLayer = properties.effectiveLayerType() != LayerType::None; 235 int clipFlags = properties.getClippingFlags(); 236 if (properties.getAlpha() < 1) { 237 if (isLayer) { 238 clipFlags &= ~CLIP_TO_BOUNDS; // bounds clipping done by layer 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); 248 clipFlags = 0; // all clipping done by savelayer 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, 262 SkClipOp::kIntersect); 263 } 264 265 if (properties.getRevealClip().willClip()) { 266 Rect bounds; 267 properties.getRevealClip().getBounds(&bounds); 268 mCanvasState.setClippingRoundRect(mAllocator, bounds, 269 properties.getRevealClip().getRadius()); 270 } else if (properties.getOutline().willClip()) { 271 mCanvasState.setClippingOutline(mAllocator, &(properties.getOutline())); 272 } 273 274 bool quickRejected = mCanvasState.currentSnapshot()->getRenderTargetClip().isEmpty() || 275 (properties.getClipToBounds() && 276 mCanvasState.quickRejectConservative(0, 0, width, height)); 277 if (!quickRejected) { 278 // not rejected, so defer render as either Layer, or direct (possibly wrapped in saveLayer) 279 if (node.getLayer()) { 280 // HW layer 281 LayerOp* drawLayerOp = mAllocator.create_trivial<LayerOp>(node); 282 BakedOpState* bakedOpState = tryBakeOpState(*drawLayerOp); 283 if (bakedOpState) { 284 // Node's layer already deferred, schedule it to render into parent layer 285 currentLayer().deferUnmergeableOp(mAllocator, bakedOpState, OpBatchType::Bitmap); 286 } 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()); 292 deferBeginLayerOp(*mAllocator.create_trivial<BeginLayerOp>( 293 saveLayerBounds, Matrix4::identity(), 294 nullptr, // no record-time clip - need only respect defer-time one 295 &saveLayerPaint)); 296 deferNodeOps(node); 297 deferEndLayerOp(*mAllocator.create_trivial<EndLayerOp>()); 298 } else { 299 deferNodeOps(node); 300 } 301 } 302 } 303 304 typedef key_value_pair_t<float, const RenderNodeOp*> ZRenderNodeOpPair; 305 306 template <typename V> 307 static void buildZSortedChildList(V* zTranslatedNodes, const DisplayList& displayList, 308 const DisplayList::Chunk& chunk) { 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> 338 void FrameBuilder::defer3dChildren(const ClipBase* reorderClip, ChildrenSelectMode mode, 339 const V& zTranslatedNodes) { 340 const int size = zTranslatedNodes.size(); 341 if (size == 0 || (mode == ChildrenSelectMode::Negative && zTranslatedNodes[0].key > 0.0f) || 342 (mode == ChildrenSelectMode::Positive && zTranslatedNodes[size - 1].key < 0.0f)) { 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; 359 shadowIndex = endIndex; // draw no shadows 360 } else { 361 drawIndex = nonNegativeIndex; 362 endIndex = size; 363 shadowIndex = drawIndex; // potentially draw shadow for each pos Z child 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) { 374 deferShadow(reorderClip, *casterNodeOp); 375 376 lastCasterZ = casterZ; // must do this even if current caster not casting a shadow 377 shadowIndex++; 378 continue; 379 } 380 } 381 382 const RenderNodeOp* childOp = zTranslatedNodes[drawIndex].value; 383 deferRenderNodeOpImpl(*childOp); 384 drawIndex++; 385 } 386 } 387 388 void FrameBuilder::deferShadow(const ClipBase* reorderClip, const RenderNodeOp& casterNodeOp) { 389 auto& node = *casterNodeOp.renderNode; 390 auto& properties = node.properties(); 391 392 if (properties.getAlpha() <= 0.0f || properties.getOutline().getAlpha() <= 0.0f || 393 !properties.getOutline().getPath() || properties.getScaleX() == 0 || 394 properties.getScaleY() == 0) { 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; 425 clipBoundsPath.addRect(clipBounds.left, clipBounds.top, clipBounds.right, 426 clipBounds.bottom); 427 428 Op(*casterPath, clipBoundsPath, kIntersect_SkPathOp, frameAllocatedPath); 429 casterPath = frameAllocatedPath; 430 } 431 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, 435 *mCanvasState.currentSnapshot()->transform); 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 442 sp<TessellationCache::ShadowTask> task = mCaches.tessellationCache.getShadowTask( 443 mCanvasState.currentTransform(), mCanvasState.getLocalClipBounds(), 444 casterAlpha >= 1.0f, casterPath, &shadowMatrixXY, &shadowMatrixZ, 445 mCanvasState.currentSnapshot()->getRelativeLightCenter(), mLightRadius); 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 } 452 } 453 mCanvasState.restoreToCount(restoreTo); 454 } 455 456 void FrameBuilder::deferProjectedChildren(const RenderNode& renderNode) { 457 int count = mCanvasState.save(SaveFlags::MatrixClip); 458 const SkPath* projectionReceiverOutline = renderNode.properties().getOutline().getPath(); 459 460 SkPath transformedMaskPath; // on stack, since BakedOpState makes a deep copy 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); 466 projectionReceiverOutline->transform(skCurrentTransform, &transformedMaskPath); 467 mCanvasState.setProjectionPathMask(&transformedMaskPath); 468 } 469 470 for (size_t i = 0; i < renderNode.mProjectedNodes.size(); i++) { 471 RenderNodeOp* childOp = renderNode.mProjectedNodes[i]; 472 RenderNode& childNode = *childOp->renderNode; 473 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); 478 479 // Apply transform between ancestor and projected descendant 480 mCanvasState.concatMatrix(childOp->transformFromCompositingAncestor); 481 482 deferNodePropsAndOps(childNode); 483 484 mCanvasState.restoreToCount(restoreTo); 485 } 486 } 487 mCanvasState.restoreToCount(count); 488 } 489 490 /** 491 * Used to define a list of lambdas referencing private FrameBuilder::onXX::defer() methods. 492 * 493 * This allows opIds embedded in the RecordedOps to be used for dispatching to these lambdas. 494 * E.g. a BitmapOp op then would be dispatched to FrameBuilder::onBitmapOp(const BitmapOp&) 495 */ 496 #define OP_RECEIVER(Type) \ 497 [](FrameBuilder& frameBuilder, const RecordedOp& op) { \ 498 frameBuilder.defer##Type(static_cast<const Type&>(op)); \ 499 }, 500 void FrameBuilder::deferNodeOps(const RenderNode& renderNode) { 501 typedef void (*OpDispatcher)(FrameBuilder & frameBuilder, const RecordedOp& op); 502 static OpDispatcher receivers[] = BUILD_DEFERRABLE_OP_LUT(OP_RECEIVER); 503 504 // can't be null, since DL=null node rejection happens before deferNodePropsAndOps 505 const DisplayList& displayList = *(renderNode.getDisplayList()); 506 for (auto& chunk : displayList.getChunks()) { 507 FatVector<ZRenderNodeOpPair, 16> zTranslatedNodes; 508 buildZSortedChildList(&zTranslatedNodes, displayList, chunk); 509 510 defer3dChildren(chunk.reorderClip, ChildrenSelectMode::Negative, zTranslatedNodes); 511 for (size_t opIndex = chunk.beginOpIndex; opIndex < chunk.endOpIndex; opIndex++) { 512 const RecordedOp* op = displayList.getOps()[opIndex]; 513 receivers[op->opId](*this, *op); 514 515 if (CC_UNLIKELY(!renderNode.mProjectedNodes.empty() && 516 displayList.projectionReceiveIndex >= 0 && 517 static_cast<int>(opIndex) == displayList.projectionReceiveIndex)) { 518 deferProjectedChildren(renderNode); 519 } 520 } 521 defer3dChildren(chunk.reorderClip, ChildrenSelectMode::Positive, zTranslatedNodes); 522 } 523 } 524 525 void FrameBuilder::deferRenderNodeOpImpl(const RenderNodeOp& op) { 526 if (op.renderNode->nothingToDraw()) return; 527 int count = mCanvasState.save(SaveFlags::MatrixClip); 528 529 // apply state from RecordedOp (clip first, since op's clip is transformed by current matrix) 530 mCanvasState.writableSnapshot()->applyClip(op.localClip, 531 *mCanvasState.currentSnapshot()->transform); 532 mCanvasState.concatMatrix(op.localMatrix); 533 534 // then apply state from node properties, and defer ops 535 deferNodePropsAndOps(*op.renderNode); 536 537 mCanvasState.restoreToCount(count); 538 } 539 540 void FrameBuilder::deferRenderNodeOp(const RenderNodeOp& op) { 541 if (!op.skipInOrderDraw) { 542 deferRenderNodeOpImpl(op); 543 } 544 } 545 546 /** 547 * Defers an unmergeable, strokeable op, accounting correctly 548 * for paint's style on the bounds being computed. 549 */ 550 BakedOpState* FrameBuilder::deferStrokeableOp(const RecordedOp& op, batchid_t batchId, 551 BakedOpState::StrokeBehavior strokeBehavior, 552 bool expandForPathTexture) { 553 // Note: here we account for stroke when baking the op 554 BakedOpState* bakedState = BakedOpState::tryStrokeableOpConstruct( 555 mAllocator, *mCanvasState.writableSnapshot(), op, strokeBehavior, expandForPathTexture); 556 if (!bakedState) return nullptr; // quick rejected 557 558 if (op.opId == RecordedOpId::RectOp && op.paint->getStyle() != SkPaint::kStroke_Style) { 559 bakedState->setupOpacity(op.paint); 560 } 561 562 currentLayer().deferUnmergeableOp(mAllocator, bakedState, batchId); 563 return bakedState; 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); 574 return paint.getPathEffect() 575 ? OpBatchType::AlphaMaskTexture 576 : (paint.isAntiAlias() ? OpBatchType::AlphaVertices : OpBatchType::Vertices); 577 } 578 579 void FrameBuilder::deferArcOp(const ArcOp& op) { 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); 584 } 585 586 static bool hasMergeableClip(const BakedOpState& state) { 587 return !state.computedState.clipState || 588 state.computedState.clipState->mode == ClipMode::Rectangle; 589 } 590 591 void FrameBuilder::deferBitmapOp(const BitmapOp& op) { 592 BakedOpState* bakedState = tryBakeOpState(op); 593 if (!bakedState) return; // quick rejected 594 595 if (op.bitmap->isOpaque()) { 596 bakedState->setupOpacity(op.paint); 597 } 598 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() 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)) { 606 mergeid_t mergeId = reinterpret_cast<mergeid_t>(op.bitmap->getGenerationID()); 607 currentLayer().deferMergeableOp(mAllocator, bakedState, OpBatchType::Bitmap, mergeId); 608 } else { 609 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Bitmap); 610 } 611 } 612 613 void FrameBuilder::deferBitmapMeshOp(const BitmapMeshOp& op) { 614 BakedOpState* bakedState = tryBakeOpState(op); 615 if (!bakedState) return; // quick rejected 616 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Bitmap); 617 } 618 619 void FrameBuilder::deferBitmapRectOp(const BitmapRectOp& op) { 620 BakedOpState* bakedState = tryBakeOpState(op); 621 if (!bakedState) return; // quick rejected 622 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Bitmap); 623 } 624 625 void FrameBuilder::deferVectorDrawableOp(const VectorDrawableOp& op) { 626 Bitmap& bitmap = op.vectorDrawable->getBitmapUpdateIfDirty(); 627 SkPaint* paint = op.vectorDrawable->getPaint(); 628 const BitmapRectOp* resolvedOp = mAllocator.create_trivial<BitmapRectOp>( 629 op.unmappedBounds, op.localMatrix, op.localClip, paint, &bitmap, 630 Rect(bitmap.width(), bitmap.height())); 631 deferBitmapRectOp(*resolvedOp); 632 } 633 634 void FrameBuilder::deferCirclePropsOp(const CirclePropsOp& op) { 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); 641 const OvalOp* resolvedOp = mAllocator.create_trivial<OvalOp>(unmappedBounds, op.localMatrix, 642 op.localClip, op.paint); 643 deferOvalOp(*resolvedOp); 644 } 645 646 void FrameBuilder::deferColorOp(const ColorOp& op) { 647 BakedOpState* bakedState = tryBakeUnboundedOpState(op); 648 if (!bakedState) return; // quick rejected 649 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Vertices); 650 } 651 652 void FrameBuilder::deferFunctorOp(const FunctorOp& op) { 653 BakedOpState* bakedState = tryBakeUnboundedOpState(op); 654 if (!bakedState) return; // quick rejected 655 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Functor); 656 } 657 658 void FrameBuilder::deferLinesOp(const LinesOp& op) { 659 batchid_t batch = op.paint->isAntiAlias() ? OpBatchType::AlphaVertices : OpBatchType::Vertices; 660 deferStrokeableOp(op, batch, BakedOpState::StrokeBehavior::Forced); 661 } 662 663 void FrameBuilder::deferOvalOp(const OvalOp& op) { 664 deferStrokeableOp(op, tessBatchId(op)); 665 } 666 667 void FrameBuilder::deferPatchOp(const PatchOp& op) { 668 BakedOpState* bakedState = tryBakeOpState(op); 669 if (!bakedState) return; // quick rejected 670 671 if (bakedState->computedState.transform.isPureTranslate() && 672 PaintUtils::getBlendModeDirect(op.paint) == SkBlendMode::kSrcOver && 673 hasMergeableClip(*bakedState)) { 674 mergeid_t mergeId = reinterpret_cast<mergeid_t>(op.bitmap->getGenerationID()); 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 684 void FrameBuilder::deferPathOp(const PathOp& op) { 685 auto state = deferStrokeableOp(op, OpBatchType::AlphaMaskTexture); 686 if (CC_LIKELY(state)) { 687 mCaches.pathCache.precache(op.path, op.paint); 688 } 689 } 690 691 void FrameBuilder::deferPointsOp(const PointsOp& op) { 692 batchid_t batch = op.paint->isAntiAlias() ? OpBatchType::AlphaVertices : OpBatchType::Vertices; 693 deferStrokeableOp(op, batch, BakedOpState::StrokeBehavior::Forced); 694 } 695 696 void FrameBuilder::deferRectOp(const RectOp& op) { 697 deferStrokeableOp(op, tessBatchId(op)); 698 } 699 700 void FrameBuilder::deferRoundRectOp(const RoundRectOp& op) { 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), 705 op.unmappedBounds.getWidth(), 706 op.unmappedBounds.getHeight(), op.rx, op.ry); 707 } 708 } 709 710 void FrameBuilder::deferRoundRectPropsOp(const RoundRectPropsOp& op) { 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. 713 const RoundRectOp* resolvedOp = mAllocator.create_trivial<RoundRectOp>( 714 Rect(*(op.left), *(op.top), *(op.right), *(op.bottom)), op.localMatrix, op.localClip, 715 op.paint, *op.rx, *op.ry); 716 deferRoundRectOp(*resolvedOp); 717 } 718 719 void FrameBuilder::deferSimpleRectsOp(const SimpleRectsOp& op) { 720 BakedOpState* bakedState = tryBakeOpState(op); 721 if (!bakedState) return; // quick rejected 722 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Vertices); 723 } 724 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 730 void FrameBuilder::deferTextOp(const TextOp& op) { 731 BakedOpState* bakedState = BakedOpState::tryStrokeableOpConstruct( 732 mAllocator, *mCanvasState.writableSnapshot(), op, 733 BakedOpState::StrokeBehavior::StyleDefined, false); 734 if (!bakedState) return; // quick rejected 735 736 batchid_t batchId = textBatchId(*(op.paint)); 737 if (bakedState->computedState.transform.isPureTranslate() && 738 PaintUtils::getBlendModeDirect(op.paint) == SkBlendMode::kSrcOver && 739 hasMergeableClip(*bakedState)) { 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 } 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); 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)))); 757 } 758 } 759 760 void FrameBuilder::deferTextOnPathOp(const TextOnPathOp& op) { 761 BakedOpState* bakedState = tryBakeUnboundedOpState(op); 762 if (!bakedState) return; // quick rejected 763 currentLayer().deferUnmergeableOp(mAllocator, bakedState, textBatchId(*(op.paint))); 764 765 mCaches.fontRenderer.getFontRenderer().precache(op.paint, op.glyphs, op.glyphCount, 766 SkMatrix::I()); 767 } 768 769 void FrameBuilder::deferTextureLayerOp(const TextureLayerOp& op) { 770 GlLayer* layer = static_cast<GlLayer*>(op.layerHandle->backingLayer()); 771 if (CC_UNLIKELY(!layer || !layer->isRenderable())) return; 772 773 const TextureLayerOp* textureLayerOp = &op; 774 // Now safe to access transform (which was potentially unready at record time) 775 if (!layer->getTransform().isIdentity()) { 776 // non-identity transform present, so 'inject it' into op by copying + replacing matrix 777 Matrix4 combinedMatrix(op.localMatrix); 778 combinedMatrix.multiply(layer->getTransform()); 779 textureLayerOp = mAllocator.create<TextureLayerOp>(op, combinedMatrix); 780 } 781 BakedOpState* bakedState = tryBakeOpState(*textureLayerOp); 782 783 if (!bakedState) return; // quick rejected 784 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::TextureLayer); 785 } 786 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) { 791 mCanvasState.save(SaveFlags::MatrixClip); 792 mCanvasState.writableSnapshot()->initializeViewport(layerWidth, layerHeight); 793 mCanvasState.writableSnapshot()->roundRectClipState = nullptr; 794 mCanvasState.writableSnapshot()->setRelativeLightCenter(lightCenter); 795 mCanvasState.writableSnapshot()->transform->loadTranslate(contentTranslateX, contentTranslateY, 796 0); 797 mCanvasState.writableSnapshot()->setClip(repaintRect.left, repaintRect.top, repaintRect.right, 798 repaintRect.bottom); 799 800 // create a new layer repaint, and push its index on the stack 801 mLayerStack.push_back(mLayerBuilders.size()); 802 auto newFbo = mAllocator.create<LayerBuilder>(layerWidth, layerHeight, repaintRect, 803 beginLayerOp, renderNode); 804 mLayerBuilders.push_back(newFbo); 805 } 806 807 void FrameBuilder::restoreForLayer() { 808 // restore canvas, and pop finished layer off of the stack 809 mCanvasState.restore(); 810 mLayerStack.pop_back(); 811 } 812 813 // TODO: defer time rejection (when bounds become empty) + tests 814 // Option - just skip layers with no bounds at playback + defer? 815 void FrameBuilder::deferBeginLayerOp(const BeginLayerOp& op) { 816 uint32_t layerWidth = (uint32_t)op.unmappedBounds.getWidth(); 817 uint32_t layerHeight = (uint32_t)op.unmappedBounds.getHeight(); 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 824 Matrix4 contentTransform(*(previous->transform)); 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 853 saveForLayer(layerWidth, layerHeight, contentTranslateX, contentTranslateY, 854 Rect(layerWidth, layerHeight), lightCenter, &op, nullptr); 855 } 856 857 void FrameBuilder::deferEndLayerOp(const EndLayerOp& /* ignored */) { 858 const BeginLayerOp& beginLayerOp = *currentLayer().beginLayerOp; 859 int finishedLayerIndex = mLayerStack.back(); 860 861 restoreForLayer(); 862 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 867 uint32_t layerWidth = (uint32_t)beginLayerOp.unmappedBounds.getWidth(); 868 uint32_t layerHeight = (uint32_t)beginLayerOp.unmappedBounds.getHeight(); 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); 877 contentTransform.translate(beginLayerOp.unmappedBounds.left, beginLayerOp.unmappedBounds.top); 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 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 902 LayerOp* drawLayerOp = mAllocator.create_trivial<LayerOp>( 903 beginLayerOp.unmappedBounds, localMatrix, beginLayerOp.localClip, beginLayerOp.paint, 904 &(mLayerBuilders[finishedLayerIndex]->offscreenBuffer)); 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 912 // TODO: need to prevent any render work from being done 913 // - create layerop earlier for reject purposes? 914 mLayerBuilders[finishedLayerIndex]->clear(); 915 return; 916 } 917 } 918 919 void FrameBuilder::deferBeginUnclippedLayerOp(const BeginUnclippedLayerOp& op) { 920 Matrix4 boundsTransform(*(mCanvasState.currentSnapshot()->transform)); 921 boundsTransform.multiply(op.localMatrix); 922 923 Rect dstRect(op.unmappedBounds); 924 boundsTransform.mapRect(dstRect); 925 dstRect.roundOut(); 926 dstRect.doIntersect(mCanvasState.currentSnapshot()->getRenderTargetClip()); 927 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 { 932 // Allocate a holding position for the layer object (copyTo will produce, copyFrom will 933 // consume) 934 OffscreenBuffer** layerHandle = mAllocator.create<OffscreenBuffer*>(nullptr); 935 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); 940 BakedOpState* bakedState = BakedOpState::directConstruct( 941 mAllocator, &(currentLayer().repaintClip), dstRect, *copyToOp); 942 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::CopyToLayer); 943 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); 949 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); 955 bakedState = BakedOpState::directConstruct(mAllocator, &(currentLayer().repaintClip), 956 dstRect, *copyFromOp); 957 currentLayer().activeUnclippedSaveLayers.push_back(bakedState); 958 } 959 } 960 961 void FrameBuilder::deferEndUnclippedLayerOp(const EndUnclippedLayerOp& /* ignored */) { 962 LOG_ALWAYS_FATAL_IF(currentLayer().activeUnclippedSaveLayers.empty(), "no layer to end!"); 963 964 BakedOpState* copyFromLayerOp = currentLayer().activeUnclippedSaveLayers.back(); 965 currentLayer().activeUnclippedSaveLayers.pop_back(); 966 if (copyFromLayerOp) { 967 currentLayer().deferUnmergeableOp(mAllocator, copyFromLayerOp, OpBatchType::CopyFromLayer); 968 } 969 } 970 971 void FrameBuilder::finishDefer() { 972 mCaches.fontRenderer.endPrecaching(); 973 } 974 975 } // namespace uirenderer 976 } // namespace android 977