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