RenderNode.cpp revision a447d29c65fb811cd184775a3476101a1cede929
1/* 2 * Copyright (C) 2014 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#define ATRACE_TAG ATRACE_TAG_VIEW 18#define LOG_TAG "RenderNode" 19 20#include "RenderNode.h" 21 22#include <algorithm> 23 24#include <SkCanvas.h> 25#include <algorithm> 26 27#include <utils/Trace.h> 28 29#include "DamageAccumulator.h" 30#include "Debug.h" 31#include "DisplayListOp.h" 32#include "DisplayListLogBuffer.h" 33#include "utils/MathUtils.h" 34 35namespace android { 36namespace uirenderer { 37 38void RenderNode::outputLogBuffer(int fd) { 39 DisplayListLogBuffer& logBuffer = DisplayListLogBuffer::getInstance(); 40 if (logBuffer.isEmpty()) { 41 return; 42 } 43 44 FILE *file = fdopen(fd, "a"); 45 46 fprintf(file, "\nRecent DisplayList operations\n"); 47 logBuffer.outputCommands(file); 48 49 String8 cachesLog; 50 Caches::getInstance().dumpMemoryUsage(cachesLog); 51 fprintf(file, "\nCaches:\n%s", cachesLog.string()); 52 fprintf(file, "\n"); 53 54 fflush(file); 55} 56 57RenderNode::RenderNode() 58 : mDirtyPropertyFields(0) 59 , mNeedsDisplayListDataSync(false) 60 , mDisplayListData(0) 61 , mStagingDisplayListData(0) 62 , mNeedsAnimatorsSync(false) { 63} 64 65RenderNode::~RenderNode() { 66 delete mDisplayListData; 67 delete mStagingDisplayListData; 68} 69 70void RenderNode::setStagingDisplayList(DisplayListData* data) { 71 mNeedsDisplayListDataSync = true; 72 delete mStagingDisplayListData; 73 mStagingDisplayListData = data; 74 if (mStagingDisplayListData) { 75 Caches::getInstance().registerFunctors(mStagingDisplayListData->functorCount); 76 } 77} 78 79/** 80 * This function is a simplified version of replay(), where we simply retrieve and log the 81 * display list. This function should remain in sync with the replay() function. 82 */ 83void RenderNode::output(uint32_t level) { 84 ALOGD("%*sStart display list (%p, %s, render=%d)", (level - 1) * 2, "", this, 85 getName(), isRenderable()); 86 ALOGD("%*s%s %d", level * 2, "", "Save", 87 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag); 88 89 properties().debugOutputProperties(level); 90 int flags = DisplayListOp::kOpLogFlag_Recurse; 91 for (unsigned int i = 0; i < mDisplayListData->displayListOps.size(); i++) { 92 mDisplayListData->displayListOps[i]->output(level, flags); 93 } 94 95 ALOGD("%*sDone (%p, %s)", (level - 1) * 2, "", this, getName()); 96} 97 98int RenderNode::getDebugSize() { 99 int size = sizeof(RenderNode); 100 if (mStagingDisplayListData) { 101 size += mStagingDisplayListData->allocator.usedSize(); 102 } 103 if (mDisplayListData && mDisplayListData != mStagingDisplayListData) { 104 size += mDisplayListData->allocator.usedSize(); 105 } 106 return size; 107} 108 109void RenderNode::prepareTree(TreeInfo& info) { 110 ATRACE_CALL(); 111 112 prepareTreeImpl(info); 113} 114 115void RenderNode::damageSelf(TreeInfo& info) { 116 if (isRenderable() && properties().getAlpha() > 0) { 117 if (properties().getClipToBounds()) { 118 info.damageAccumulator->dirty(0, 0, properties().getWidth(), properties().getHeight()); 119 } else { 120 // Hope this is big enough? 121 // TODO: Get this from the display list ops or something 122 info.damageAccumulator->dirty(INT_MIN, INT_MIN, INT_MAX, INT_MAX); 123 } 124 } 125} 126 127void RenderNode::prepareTreeImpl(TreeInfo& info) { 128 info.damageAccumulator->pushTransform(this); 129 if (info.mode == TreeInfo::MODE_FULL) { 130 pushStagingChanges(info); 131 evaluateAnimations(info); 132 } else if (info.mode == TreeInfo::MODE_MAYBE_DETACHING) { 133 pushStagingChanges(info); 134 } else if (info.mode == TreeInfo::MODE_RT_ONLY) { 135 evaluateAnimations(info); 136 } 137 prepareSubTree(info, mDisplayListData); 138 info.damageAccumulator->popTransform(); 139} 140 141class PushAnimatorsFunctor { 142public: 143 PushAnimatorsFunctor(RenderNode* target, TreeInfo& info) 144 : mTarget(target), mInfo(info) {} 145 146 bool operator() (const sp<BaseRenderNodeAnimator>& animator) { 147 animator->setupStartValueIfNecessary(mTarget, mInfo); 148 return animator->isFinished(); 149 } 150private: 151 RenderNode* mTarget; 152 TreeInfo& mInfo; 153}; 154 155void RenderNode::pushStagingChanges(TreeInfo& info) { 156 // Push the animators first so that setupStartValueIfNecessary() is called 157 // before properties() is trampled by stagingProperties(), as they are 158 // required by some animators. 159 if (mNeedsAnimatorsSync) { 160 mAnimators.resize(mStagingAnimators.size()); 161 std::vector< sp<BaseRenderNodeAnimator> >::iterator it; 162 PushAnimatorsFunctor functor(this, info); 163 // hint: this means copy_if_not() 164 it = std::remove_copy_if(mStagingAnimators.begin(), mStagingAnimators.end(), 165 mAnimators.begin(), functor); 166 mAnimators.resize(std::distance(mAnimators.begin(), it)); 167 } 168 if (mDirtyPropertyFields) { 169 mDirtyPropertyFields = 0; 170 damageSelf(info); 171 info.damageAccumulator->popTransform(); 172 mProperties = mStagingProperties; 173 // We could try to be clever and only re-damage if the matrix changed. 174 // However, we don't need to worry about that. The cost of over-damaging 175 // here is only going to be a single additional map rect of this node 176 // plus a rect join(). The parent's transform (and up) will only be 177 // performed once. 178 info.damageAccumulator->pushTransform(this); 179 damageSelf(info); 180 } 181 if (mNeedsDisplayListDataSync) { 182 mNeedsDisplayListDataSync = false; 183 // Do a push pass on the old tree to handle freeing DisplayListData 184 // that are no longer used 185 TreeInfo oldTreeInfo(TreeInfo::MODE_MAYBE_DETACHING); 186 oldTreeInfo.damageAccumulator = info.damageAccumulator; 187 prepareSubTree(oldTreeInfo, mDisplayListData); 188 delete mDisplayListData; 189 mDisplayListData = mStagingDisplayListData; 190 mStagingDisplayListData = 0; 191 damageSelf(info); 192 } 193} 194 195class AnimateFunctor { 196public: 197 AnimateFunctor(RenderNode* target, TreeInfo& info) 198 : mTarget(target), mInfo(info) {} 199 200 bool operator() (const sp<BaseRenderNodeAnimator>& animator) { 201 return animator->animate(mTarget, mInfo); 202 } 203private: 204 RenderNode* mTarget; 205 TreeInfo& mInfo; 206}; 207 208void RenderNode::evaluateAnimations(TreeInfo& info) { 209 if (!mAnimators.size()) return; 210 211 // TODO: Can we target this better? For now treat it like any other staging 212 // property push and just damage self before and after animators are run 213 214 damageSelf(info); 215 info.damageAccumulator->popTransform(); 216 217 AnimateFunctor functor(this, info); 218 std::vector< sp<BaseRenderNodeAnimator> >::iterator newEnd; 219 newEnd = std::remove_if(mAnimators.begin(), mAnimators.end(), functor); 220 mAnimators.erase(newEnd, mAnimators.end()); 221 mProperties.updateMatrix(); 222 info.out.hasAnimations |= mAnimators.size(); 223 224 info.damageAccumulator->pushTransform(this); 225 damageSelf(info); 226} 227 228void RenderNode::prepareSubTree(TreeInfo& info, DisplayListData* subtree) { 229 if (subtree) { 230 TextureCache& cache = Caches::getInstance().textureCache; 231 info.out.hasFunctors |= subtree->functorCount; 232 // TODO: Fix ownedBitmapResources to not require disabling prepareTextures 233 // and thus falling out of async drawing path. 234 if (subtree->ownedBitmapResources.size()) { 235 info.prepareTextures = false; 236 } 237 for (size_t i = 0; info.prepareTextures && i < subtree->bitmapResources.size(); i++) { 238 info.prepareTextures = cache.prefetchAndMarkInUse(subtree->bitmapResources[i]); 239 } 240 for (size_t i = 0; i < subtree->children().size(); i++) { 241 DrawDisplayListOp* op = subtree->children()[i]; 242 RenderNode* childNode = op->mDisplayList; 243 info.damageAccumulator->pushTransform(&op->mTransformFromParent); 244 childNode->prepareTreeImpl(info); 245 info.damageAccumulator->popTransform(); 246 } 247 } 248} 249 250/* 251 * For property operations, we pass a savecount of 0, since the operations aren't part of the 252 * displaylist, and thus don't have to compensate for the record-time/playback-time discrepancy in 253 * base saveCount (i.e., how RestoreToCount uses saveCount + properties().getCount()) 254 */ 255#define PROPERTY_SAVECOUNT 0 256 257template <class T> 258void RenderNode::setViewProperties(OpenGLRenderer& renderer, T& handler) { 259#if DEBUG_DISPLAY_LIST 260 properties().debugOutputProperties(handler.level() + 1); 261#endif 262 if (properties().getLeft() != 0 || properties().getTop() != 0) { 263 renderer.translate(properties().getLeft(), properties().getTop()); 264 } 265 if (properties().getStaticMatrix()) { 266 renderer.concatMatrix(*properties().getStaticMatrix()); 267 } else if (properties().getAnimationMatrix()) { 268 renderer.concatMatrix(*properties().getAnimationMatrix()); 269 } 270 if (properties().hasTransformMatrix()) { 271 if (properties().isTransformTranslateOnly()) { 272 renderer.translate(properties().getTranslationX(), properties().getTranslationY()); 273 } else { 274 renderer.concatMatrix(*properties().getTransformMatrix()); 275 } 276 } 277 bool clipToBoundsNeeded = properties().getCaching() ? false : properties().getClipToBounds(); 278 if (properties().getAlpha() < 1) { 279 if (properties().getCaching()) { 280 renderer.setOverrideLayerAlpha(properties().getAlpha()); 281 } else if (!properties().getHasOverlappingRendering()) { 282 renderer.scaleAlpha(properties().getAlpha()); 283 } else { 284 // TODO: should be able to store the size of a DL at record time and not 285 // have to pass it into this call. In fact, this information might be in the 286 // location/size info that we store with the new native transform data. 287 int saveFlags = SkCanvas::kHasAlphaLayer_SaveFlag; 288 if (clipToBoundsNeeded) { 289 saveFlags |= SkCanvas::kClipToLayer_SaveFlag; 290 clipToBoundsNeeded = false; // clipping done by saveLayer 291 } 292 293 SaveLayerOp* op = new (handler.allocator()) SaveLayerOp( 294 0, 0, properties().getWidth(), properties().getHeight(), 295 properties().getAlpha() * 255, saveFlags); 296 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds()); 297 } 298 } 299 if (clipToBoundsNeeded) { 300 ClipRectOp* op = new (handler.allocator()) ClipRectOp( 301 0, 0, properties().getWidth(), properties().getHeight(), SkRegion::kIntersect_Op); 302 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds()); 303 } 304 305 if (CC_UNLIKELY(properties().hasClippingPath())) { 306 ClipPathOp* op = new (handler.allocator()) ClipPathOp( 307 properties().getClippingPath(), properties().getClippingPathOp()); 308 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds()); 309 } 310} 311 312/** 313 * Apply property-based transformations to input matrix 314 * 315 * If true3dTransform is set to true, the transform applied to the input matrix will use true 4x4 316 * matrix computation instead of the Skia 3x3 matrix + camera hackery. 317 */ 318void RenderNode::applyViewPropertyTransforms(mat4& matrix, bool true3dTransform) { 319 if (properties().getLeft() != 0 || properties().getTop() != 0) { 320 matrix.translate(properties().getLeft(), properties().getTop()); 321 } 322 if (properties().getStaticMatrix()) { 323 mat4 stat(*properties().getStaticMatrix()); 324 matrix.multiply(stat); 325 } else if (properties().getAnimationMatrix()) { 326 mat4 anim(*properties().getAnimationMatrix()); 327 matrix.multiply(anim); 328 } 329 330 bool applyTranslationZ = true3dTransform && !MathUtils::isZero(properties().getZ()); 331 if (properties().hasTransformMatrix() || applyTranslationZ) { 332 if (properties().isTransformTranslateOnly()) { 333 matrix.translate(properties().getTranslationX(), properties().getTranslationY(), 334 true3dTransform ? properties().getZ() : 0.0f); 335 } else { 336 if (!true3dTransform) { 337 matrix.multiply(*properties().getTransformMatrix()); 338 } else { 339 mat4 true3dMat; 340 true3dMat.loadTranslate( 341 properties().getPivotX() + properties().getTranslationX(), 342 properties().getPivotY() + properties().getTranslationY(), 343 properties().getZ()); 344 true3dMat.rotate(properties().getRotationX(), 1, 0, 0); 345 true3dMat.rotate(properties().getRotationY(), 0, 1, 0); 346 true3dMat.rotate(properties().getRotation(), 0, 0, 1); 347 true3dMat.scale(properties().getScaleX(), properties().getScaleY(), 1); 348 true3dMat.translate(-properties().getPivotX(), -properties().getPivotY()); 349 350 matrix.multiply(true3dMat); 351 } 352 } 353 } 354} 355 356/** 357 * Organizes the DisplayList hierarchy to prepare for background projection reordering. 358 * 359 * This should be called before a call to defer() or drawDisplayList() 360 * 361 * Each DisplayList that serves as a 3d root builds its list of composited children, 362 * which are flagged to not draw in the standard draw loop. 363 */ 364void RenderNode::computeOrdering() { 365 ATRACE_CALL(); 366 mProjectedNodes.clear(); 367 368 // TODO: create temporary DDLOp and call computeOrderingImpl on top DisplayList so that 369 // transform properties are applied correctly to top level children 370 if (mDisplayListData == NULL) return; 371 for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) { 372 DrawDisplayListOp* childOp = mDisplayListData->children()[i]; 373 childOp->mDisplayList->computeOrderingImpl(childOp, 374 properties().getOutline().getPath(), &mProjectedNodes, &mat4::identity()); 375 } 376} 377 378void RenderNode::computeOrderingImpl( 379 DrawDisplayListOp* opState, 380 const SkPath* outlineOfProjectionSurface, 381 Vector<DrawDisplayListOp*>* compositedChildrenOfProjectionSurface, 382 const mat4* transformFromProjectionSurface) { 383 mProjectedNodes.clear(); 384 if (mDisplayListData == NULL || mDisplayListData->isEmpty()) return; 385 386 // TODO: should avoid this calculation in most cases 387 // TODO: just calculate single matrix, down to all leaf composited elements 388 Matrix4 localTransformFromProjectionSurface(*transformFromProjectionSurface); 389 localTransformFromProjectionSurface.multiply(opState->mTransformFromParent); 390 391 if (properties().getProjectBackwards()) { 392 // composited projectee, flag for out of order draw, save matrix, and store in proj surface 393 opState->mSkipInOrderDraw = true; 394 opState->mTransformFromCompositingAncestor.load(localTransformFromProjectionSurface); 395 compositedChildrenOfProjectionSurface->add(opState); 396 } else { 397 // standard in order draw 398 opState->mSkipInOrderDraw = false; 399 } 400 401 if (mDisplayListData->children().size() > 0) { 402 const bool isProjectionReceiver = mDisplayListData->projectionReceiveIndex >= 0; 403 bool haveAppliedPropertiesToProjection = false; 404 for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) { 405 DrawDisplayListOp* childOp = mDisplayListData->children()[i]; 406 RenderNode* child = childOp->mDisplayList; 407 408 const SkPath* projectionOutline = NULL; 409 Vector<DrawDisplayListOp*>* projectionChildren = NULL; 410 const mat4* projectionTransform = NULL; 411 if (isProjectionReceiver && !child->properties().getProjectBackwards()) { 412 // if receiving projections, collect projecting descendent 413 414 // Note that if a direct descendent is projecting backwards, we pass it's 415 // grandparent projection collection, since it shouldn't project onto it's 416 // parent, where it will already be drawing. 417 projectionOutline = properties().getOutline().getPath(); 418 projectionChildren = &mProjectedNodes; 419 projectionTransform = &mat4::identity(); 420 } else { 421 if (!haveAppliedPropertiesToProjection) { 422 applyViewPropertyTransforms(localTransformFromProjectionSurface); 423 haveAppliedPropertiesToProjection = true; 424 } 425 projectionOutline = outlineOfProjectionSurface; 426 projectionChildren = compositedChildrenOfProjectionSurface; 427 projectionTransform = &localTransformFromProjectionSurface; 428 } 429 child->computeOrderingImpl(childOp, 430 projectionOutline, projectionChildren, projectionTransform); 431 } 432 } 433} 434 435class DeferOperationHandler { 436public: 437 DeferOperationHandler(DeferStateStruct& deferStruct, int level) 438 : mDeferStruct(deferStruct), mLevel(level) {} 439 inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) { 440 operation->defer(mDeferStruct, saveCount, mLevel, clipToBounds); 441 } 442 inline LinearAllocator& allocator() { return *(mDeferStruct.mAllocator); } 443 inline void startMark(const char* name) {} // do nothing 444 inline void endMark() {} 445 inline int level() { return mLevel; } 446 inline int replayFlags() { return mDeferStruct.mReplayFlags; } 447 448private: 449 DeferStateStruct& mDeferStruct; 450 const int mLevel; 451}; 452 453void RenderNode::deferNodeTree(DeferStateStruct& deferStruct) { 454 DeferOperationHandler handler(deferStruct, 0); 455 if (MathUtils::isPositive(properties().getZ())) { 456 issueDrawShadowOperation(Matrix4::identity(), handler); 457 } 458 issueOperations<DeferOperationHandler>(deferStruct.mRenderer, handler); 459} 460 461void RenderNode::deferNodeInParent(DeferStateStruct& deferStruct, const int level) { 462 DeferOperationHandler handler(deferStruct, level); 463 issueOperations<DeferOperationHandler>(deferStruct.mRenderer, handler); 464} 465 466class ReplayOperationHandler { 467public: 468 ReplayOperationHandler(ReplayStateStruct& replayStruct, int level) 469 : mReplayStruct(replayStruct), mLevel(level) {} 470 inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) { 471#if DEBUG_DISPLAY_LIST_OPS_AS_EVENTS 472 mReplayStruct.mRenderer.eventMark(operation->name()); 473#endif 474 operation->replay(mReplayStruct, saveCount, mLevel, clipToBounds); 475 } 476 inline LinearAllocator& allocator() { return *(mReplayStruct.mAllocator); } 477 inline void startMark(const char* name) { 478 mReplayStruct.mRenderer.startMark(name); 479 } 480 inline void endMark() { 481 mReplayStruct.mRenderer.endMark(); 482 } 483 inline int level() { return mLevel; } 484 inline int replayFlags() { return mReplayStruct.mReplayFlags; } 485 486private: 487 ReplayStateStruct& mReplayStruct; 488 const int mLevel; 489}; 490 491void RenderNode::replayNodeTree(ReplayStateStruct& replayStruct) { 492 ReplayOperationHandler handler(replayStruct, 0); 493 if (MathUtils::isPositive(properties().getZ())) { 494 issueDrawShadowOperation(Matrix4::identity(), handler); 495 } 496 issueOperations<ReplayOperationHandler>(replayStruct.mRenderer, handler); 497} 498 499void RenderNode::replayNodeInParent(ReplayStateStruct& replayStruct, const int level) { 500 ReplayOperationHandler handler(replayStruct, level); 501 issueOperations<ReplayOperationHandler>(replayStruct.mRenderer, handler); 502} 503 504void RenderNode::buildZSortedChildList(Vector<ZDrawDisplayListOpPair>& zTranslatedNodes) { 505 if (mDisplayListData == NULL || mDisplayListData->children().size() == 0) return; 506 507 for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) { 508 DrawDisplayListOp* childOp = mDisplayListData->children()[i]; 509 RenderNode* child = childOp->mDisplayList; 510 float childZ = child->properties().getZ(); 511 512 if (!MathUtils::isZero(childZ)) { 513 zTranslatedNodes.add(ZDrawDisplayListOpPair(childZ, childOp)); 514 childOp->mSkipInOrderDraw = true; 515 } else if (!child->properties().getProjectBackwards()) { 516 // regular, in order drawing DisplayList 517 childOp->mSkipInOrderDraw = false; 518 } 519 } 520 521 // Z sort 3d children (stable-ness makes z compare fall back to standard drawing order) 522 std::stable_sort(zTranslatedNodes.begin(), zTranslatedNodes.end()); 523} 524 525template <class T> 526void RenderNode::issueDrawShadowOperation(const Matrix4& transformFromParent, T& handler) { 527 if (properties().getAlpha() <= 0.0f || properties().getOutline().isEmpty()) return; 528 529 mat4 shadowMatrixXY(transformFromParent); 530 applyViewPropertyTransforms(shadowMatrixXY); 531 532 // Z matrix needs actual 3d transformation, so mapped z values will be correct 533 mat4 shadowMatrixZ(transformFromParent); 534 applyViewPropertyTransforms(shadowMatrixZ, true); 535 536 const SkPath* outlinePath = properties().getOutline().getPath(); 537 const RevealClip& revealClip = properties().getRevealClip(); 538 const SkPath* revealClipPath = revealClip.hasConvexClip() 539 ? revealClip.getPath() : NULL; // only pass the reveal clip's path if it's convex 540 541 if (revealClipPath && revealClipPath->isEmpty()) return; 542 543 /** 544 * The drawing area of the caster is always the same as the its perimeter (which 545 * the shadow system uses) *except* in the inverse clip case. Inform the shadow 546 * system that the caster's drawing area (as opposed to its perimeter) has been 547 * clipped, so that it knows the caster can't be opaque. 548 */ 549 bool casterUnclipped = !revealClip.willClip() || revealClip.hasConvexClip(); 550 551 DisplayListOp* shadowOp = new (handler.allocator()) DrawShadowOp( 552 shadowMatrixXY, shadowMatrixZ, 553 properties().getAlpha(), casterUnclipped, 554 outlinePath, revealClipPath); 555 handler(shadowOp, PROPERTY_SAVECOUNT, properties().getClipToBounds()); 556} 557 558#define SHADOW_DELTA 0.1f 559 560template <class T> 561void RenderNode::issueOperationsOf3dChildren(const Vector<ZDrawDisplayListOpPair>& zTranslatedNodes, 562 ChildrenSelectMode mode, OpenGLRenderer& renderer, T& handler) { 563 const int size = zTranslatedNodes.size(); 564 if (size == 0 565 || (mode == kNegativeZChildren && zTranslatedNodes[0].key > 0.0f) 566 || (mode == kPositiveZChildren && zTranslatedNodes[size - 1].key < 0.0f)) { 567 // no 3d children to draw 568 return; 569 } 570 571 /** 572 * Draw shadows and (potential) casters mostly in order, but allow the shadows of casters 573 * with very similar Z heights to draw together. 574 * 575 * This way, if Views A & B have the same Z height and are both casting shadows, the shadows are 576 * underneath both, and neither's shadow is drawn on top of the other. 577 */ 578 const size_t nonNegativeIndex = findNonNegativeIndex(zTranslatedNodes); 579 size_t drawIndex, shadowIndex, endIndex; 580 if (mode == kNegativeZChildren) { 581 drawIndex = 0; 582 endIndex = nonNegativeIndex; 583 shadowIndex = endIndex; // draw no shadows 584 } else { 585 drawIndex = nonNegativeIndex; 586 endIndex = size; 587 shadowIndex = drawIndex; // potentially draw shadow for each pos Z child 588 } 589 590 DISPLAY_LIST_LOGD("%*s%d %s 3d children:", (handler.level() + 1) * 2, "", 591 endIndex - drawIndex, mode == kNegativeZChildren ? "negative" : "positive"); 592 593 float lastCasterZ = 0.0f; 594 while (shadowIndex < endIndex || drawIndex < endIndex) { 595 if (shadowIndex < endIndex) { 596 DrawDisplayListOp* casterOp = zTranslatedNodes[shadowIndex].value; 597 RenderNode* caster = casterOp->mDisplayList; 598 const float casterZ = zTranslatedNodes[shadowIndex].key; 599 // attempt to render the shadow if the caster about to be drawn is its caster, 600 // OR if its caster's Z value is similar to the previous potential caster 601 if (shadowIndex == drawIndex || casterZ - lastCasterZ < SHADOW_DELTA) { 602 caster->issueDrawShadowOperation(casterOp->mTransformFromParent, handler); 603 604 lastCasterZ = casterZ; // must do this even if current caster not casting a shadow 605 shadowIndex++; 606 continue; 607 } 608 } 609 610 // only the actual child DL draw needs to be in save/restore, 611 // since it modifies the renderer's matrix 612 int restoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag); 613 614 DrawDisplayListOp* childOp = zTranslatedNodes[drawIndex].value; 615 RenderNode* child = childOp->mDisplayList; 616 617 renderer.concatMatrix(childOp->mTransformFromParent); 618 childOp->mSkipInOrderDraw = false; // this is horrible, I'm so sorry everyone 619 handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds()); 620 childOp->mSkipInOrderDraw = true; 621 622 renderer.restoreToCount(restoreTo); 623 drawIndex++; 624 } 625} 626 627template <class T> 628void RenderNode::issueOperationsOfProjectedChildren(OpenGLRenderer& renderer, T& handler) { 629 DISPLAY_LIST_LOGD("%*s%d projected children:", (handler.level() + 1) * 2, "", mProjectedNodes.size()); 630 const SkPath* projectionReceiverOutline = properties().getOutline().getPath(); 631 bool maskProjecteesWithPath = projectionReceiverOutline != NULL 632 && !projectionReceiverOutline->isRect(NULL); 633 int restoreTo = renderer.getSaveCount(); 634 635 // If the projection reciever has an outline, we mask each of the projected rendernodes to it 636 // Either with clipRect, or special saveLayer masking 637 LinearAllocator& alloc = handler.allocator(); 638 if (projectionReceiverOutline != NULL) { 639 const SkRect& outlineBounds = projectionReceiverOutline->getBounds(); 640 if (projectionReceiverOutline->isRect(NULL)) { 641 // mask to the rect outline simply with clipRect 642 handler(new (alloc) SaveOp(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag), 643 PROPERTY_SAVECOUNT, properties().getClipToBounds()); 644 ClipRectOp* clipOp = new (alloc) ClipRectOp( 645 outlineBounds.left(), outlineBounds.top(), 646 outlineBounds.right(), outlineBounds.bottom(), SkRegion::kIntersect_Op); 647 handler(clipOp, PROPERTY_SAVECOUNT, properties().getClipToBounds()); 648 } else { 649 // wrap the projected RenderNodes with a SaveLayer that will mask to the outline 650 SaveLayerOp* op = new (alloc) SaveLayerOp( 651 outlineBounds.left(), outlineBounds.top(), 652 outlineBounds.right(), outlineBounds.bottom(), 653 255, SkCanvas::kARGB_ClipLayer_SaveFlag); 654 op->setMask(projectionReceiverOutline); 655 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds()); 656 657 /* TODO: add optimizations here to take advantage of placement/size of projected 658 * children (which may shrink saveLayer area significantly). This is dependent on 659 * passing actual drawing/dirtying bounds of projected content down to native. 660 */ 661 } 662 } 663 664 // draw projected nodes 665 for (size_t i = 0; i < mProjectedNodes.size(); i++) { 666 DrawDisplayListOp* childOp = mProjectedNodes[i]; 667 668 // matrix save, concat, and restore can be done safely without allocating operations 669 int restoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag); 670 renderer.concatMatrix(childOp->mTransformFromCompositingAncestor); 671 childOp->mSkipInOrderDraw = false; // this is horrible, I'm so sorry everyone 672 handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds()); 673 childOp->mSkipInOrderDraw = true; 674 renderer.restoreToCount(restoreTo); 675 } 676 677 if (projectionReceiverOutline != NULL) { 678 handler(new (alloc) RestoreToCountOp(restoreTo), 679 PROPERTY_SAVECOUNT, properties().getClipToBounds()); 680 } 681} 682 683/** 684 * This function serves both defer and replay modes, and will organize the displayList's component 685 * operations for a single frame: 686 * 687 * Every 'simple' state operation that affects just the matrix and alpha (or other factors of 688 * DeferredDisplayState) may be issued directly to the renderer, but complex operations (with custom 689 * defer logic) and operations in displayListOps are issued through the 'handler' which handles the 690 * defer vs replay logic, per operation 691 */ 692template <class T> 693void RenderNode::issueOperations(OpenGLRenderer& renderer, T& handler) { 694 const int level = handler.level(); 695 if (mDisplayListData->isEmpty() || properties().getAlpha() <= 0) { 696 DISPLAY_LIST_LOGD("%*sEmpty display list (%p, %s)", level * 2, "", this, getName()); 697 return; 698 } 699 700 handler.startMark(getName()); 701 702#if DEBUG_DISPLAY_LIST 703 const Rect& clipRect = renderer.getLocalClipBounds(); 704 DISPLAY_LIST_LOGD("%*sStart display list (%p, %s), localClipBounds: %.0f, %.0f, %.0f, %.0f", 705 level * 2, "", this, getName(), 706 clipRect.left, clipRect.top, clipRect.right, clipRect.bottom); 707#endif 708 709 LinearAllocator& alloc = handler.allocator(); 710 int restoreTo = renderer.getSaveCount(); 711 handler(new (alloc) SaveOp(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag), 712 PROPERTY_SAVECOUNT, properties().getClipToBounds()); 713 714 DISPLAY_LIST_LOGD("%*sSave %d %d", (level + 1) * 2, "", 715 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag, restoreTo); 716 717 setViewProperties<T>(renderer, handler); 718 719 bool quickRejected = properties().getClipToBounds() 720 && renderer.quickRejectConservative(0, 0, properties().getWidth(), properties().getHeight()); 721 if (!quickRejected) { 722 if (mProperties.getOutline().willClip()) { 723 renderer.setClippingOutline(alloc, &(mProperties.getOutline())); 724 } 725 726 Vector<ZDrawDisplayListOpPair> zTranslatedNodes; 727 buildZSortedChildList(zTranslatedNodes); 728 729 // for 3d root, draw children with negative z values 730 issueOperationsOf3dChildren(zTranslatedNodes, kNegativeZChildren, renderer, handler); 731 732 DisplayListLogBuffer& logBuffer = DisplayListLogBuffer::getInstance(); 733 const int saveCountOffset = renderer.getSaveCount() - 1; 734 const int projectionReceiveIndex = mDisplayListData->projectionReceiveIndex; 735 for (unsigned int i = 0; i < mDisplayListData->displayListOps.size(); i++) { 736 DisplayListOp *op = mDisplayListData->displayListOps[i]; 737 738#if DEBUG_DISPLAY_LIST 739 op->output(level + 1); 740#endif 741 logBuffer.writeCommand(level, op->name()); 742 handler(op, saveCountOffset, properties().getClipToBounds()); 743 744 if (CC_UNLIKELY(i == projectionReceiveIndex && mProjectedNodes.size() > 0)) { 745 issueOperationsOfProjectedChildren(renderer, handler); 746 } 747 } 748 749 // for 3d root, draw children with positive z values 750 issueOperationsOf3dChildren(zTranslatedNodes, kPositiveZChildren, renderer, handler); 751 } 752 753 DISPLAY_LIST_LOGD("%*sRestoreToCount %d", (level + 1) * 2, "", restoreTo); 754 handler(new (alloc) RestoreToCountOp(restoreTo), 755 PROPERTY_SAVECOUNT, properties().getClipToBounds()); 756 renderer.setOverrideLayerAlpha(1.0f); 757 758 DISPLAY_LIST_LOGD("%*sDone (%p, %s)", level * 2, "", this, getName()); 759 handler.endMark(); 760} 761 762} /* namespace uirenderer */ 763} /* namespace android */ 764