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