RenderNode.cpp revision 5bf11bb98f5dbe278c257355d24c181237abd68c
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 19#include "RenderNode.h" 20 21#include <SkCanvas.h> 22#include <algorithm> 23 24#include <utils/Trace.h> 25 26#include "Debug.h" 27#include "DisplayListOp.h" 28#include "DisplayListLogBuffer.h" 29 30namespace android { 31namespace uirenderer { 32 33void RenderNode::outputLogBuffer(int fd) { 34 DisplayListLogBuffer& logBuffer = DisplayListLogBuffer::getInstance(); 35 if (logBuffer.isEmpty()) { 36 return; 37 } 38 39 FILE *file = fdopen(fd, "a"); 40 41 fprintf(file, "\nRecent DisplayList operations\n"); 42 logBuffer.outputCommands(file); 43 44 String8 cachesLog; 45 Caches::getInstance().dumpMemoryUsage(cachesLog); 46 fprintf(file, "\nCaches:\n%s", cachesLog.string()); 47 fprintf(file, "\n"); 48 49 fflush(file); 50} 51 52RenderNode::RenderNode() : mDestroyed(false), mNeedsPropertiesSync(false), mDisplayListData(0) { 53} 54 55RenderNode::~RenderNode() { 56 LOG_ALWAYS_FATAL_IF(mDestroyed, "Double destroyed DisplayList %p", this); 57 58 mDestroyed = true; 59 delete mDisplayListData; 60} 61 62void RenderNode::destroyDisplayListDeferred(RenderNode* displayList) { 63 if (displayList) { 64 DISPLAY_LIST_LOGD("Deferring display list destruction"); 65 Caches::getInstance().deleteDisplayListDeferred(displayList); 66 } 67} 68 69void RenderNode::setData(DisplayListData* data) { 70 delete mDisplayListData; 71 mDisplayListData = data; 72 if (mDisplayListData) { 73 Caches::getInstance().registerFunctors(mDisplayListData->functorCount); 74 } 75} 76 77/** 78 * This function is a simplified version of replay(), where we simply retrieve and log the 79 * display list. This function should remain in sync with the replay() function. 80 */ 81void RenderNode::output(uint32_t level) { 82 ALOGD("%*sStart display list (%p, %s, render=%d)", (level - 1) * 2, "", this, 83 mName.string(), isRenderable()); 84 ALOGD("%*s%s %d", level * 2, "", "Save", 85 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag); 86 87 properties().debugOutputProperties(level); 88 int flags = DisplayListOp::kOpLogFlag_Recurse; 89 for (unsigned int i = 0; i < mDisplayListData->displayListOps.size(); i++) { 90 mDisplayListData->displayListOps[i]->output(level, flags); 91 } 92 93 ALOGD("%*sDone (%p, %s)", (level - 1) * 2, "", this, mName.string()); 94} 95 96void RenderNode::updateProperties() { 97 if (mNeedsPropertiesSync) { 98 mNeedsPropertiesSync = false; 99 mProperties = mStagingProperties; 100 } 101 102 if (mDisplayListData) { 103 for (size_t i = 0; i < mDisplayListData->children.size(); i++) { 104 RenderNode* childNode = mDisplayListData->children[i]->mDisplayList; 105 childNode->updateProperties(); 106 } 107 } 108} 109 110/* 111 * For property operations, we pass a savecount of 0, since the operations aren't part of the 112 * displaylist, and thus don't have to compensate for the record-time/playback-time discrepancy in 113 * base saveCount (i.e., how RestoreToCount uses saveCount + properties().getCount()) 114 */ 115#define PROPERTY_SAVECOUNT 0 116 117template <class T> 118void RenderNode::setViewProperties(OpenGLRenderer& renderer, T& handler, 119 const int level) { 120#if DEBUG_DISPLAY_LIST 121 properties().debugOutputProperties(level); 122#endif 123 if (properties().getLeft() != 0 || properties().getTop() != 0) { 124 renderer.translate(properties().getLeft(), properties().getTop()); 125 } 126 if (properties().getStaticMatrix()) { 127 renderer.concatMatrix(properties().getStaticMatrix()); 128 } else if (properties().getAnimationMatrix()) { 129 renderer.concatMatrix(properties().getAnimationMatrix()); 130 } 131 if (properties().getMatrixFlags() != 0) { 132 if (properties().getMatrixFlags() == TRANSLATION) { 133 renderer.translate(properties().getTranslationX(), properties().getTranslationY()); 134 } else { 135 renderer.concatMatrix(*properties().getTransformMatrix()); 136 } 137 } 138 bool clipToBoundsNeeded = properties().getCaching() ? false : properties().getClipToBounds(); 139 if (properties().getAlpha() < 1) { 140 if (properties().getCaching()) { 141 renderer.setOverrideLayerAlpha(properties().getAlpha()); 142 } else if (!properties().getHasOverlappingRendering()) { 143 renderer.scaleAlpha(properties().getAlpha()); 144 } else { 145 // TODO: should be able to store the size of a DL at record time and not 146 // have to pass it into this call. In fact, this information might be in the 147 // location/size info that we store with the new native transform data. 148 int saveFlags = SkCanvas::kHasAlphaLayer_SaveFlag; 149 if (clipToBoundsNeeded) { 150 saveFlags |= SkCanvas::kClipToLayer_SaveFlag; 151 clipToBoundsNeeded = false; // clipping done by saveLayer 152 } 153 154 SaveLayerOp* op = new (handler.allocator()) SaveLayerOp( 155 0, 0, properties().getWidth(), properties().getHeight(), properties().getAlpha() * 255, saveFlags); 156 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds()); 157 } 158 } 159 if (clipToBoundsNeeded) { 160 ClipRectOp* op = new (handler.allocator()) ClipRectOp(0, 0, 161 properties().getWidth(), properties().getHeight(), SkRegion::kIntersect_Op); 162 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds()); 163 } 164 if (CC_UNLIKELY(properties().getOutline().willClip())) { 165 ClipPathOp* op = new (handler.allocator()) ClipPathOp(properties().getOutline().getPath(), 166 SkRegion::kIntersect_Op); 167 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds()); 168 } 169} 170 171/** 172 * Apply property-based transformations to input matrix 173 * 174 * If true3dTransform is set to true, the transform applied to the input matrix will use true 4x4 175 * matrix computation instead of the Skia 3x3 matrix + camera hackery. 176 */ 177void RenderNode::applyViewPropertyTransforms(mat4& matrix, bool true3dTransform) { 178 if (properties().getLeft() != 0 || properties().getTop() != 0) { 179 matrix.translate(properties().getLeft(), properties().getTop()); 180 } 181 if (properties().getStaticMatrix()) { 182 mat4 stat(*properties().getStaticMatrix()); 183 matrix.multiply(stat); 184 } else if (properties().getAnimationMatrix()) { 185 mat4 anim(*properties().getAnimationMatrix()); 186 matrix.multiply(anim); 187 } 188 if (properties().getMatrixFlags() != 0) { 189 if (properties().getMatrixFlags() == TRANSLATION) { 190 matrix.translate(properties().getTranslationX(), properties().getTranslationY(), 191 true3dTransform ? properties().getTranslationZ() : 0.0f); 192 } else { 193 if (!true3dTransform) { 194 matrix.multiply(*properties().getTransformMatrix()); 195 } else { 196 mat4 true3dMat; 197 true3dMat.loadTranslate( 198 properties().getPivotX() + properties().getTranslationX(), 199 properties().getPivotY() + properties().getTranslationY(), 200 properties().getTranslationZ()); 201 true3dMat.rotate(properties().getRotationX(), 1, 0, 0); 202 true3dMat.rotate(properties().getRotationY(), 0, 1, 0); 203 true3dMat.rotate(properties().getRotation(), 0, 0, 1); 204 true3dMat.scale(properties().getScaleX(), properties().getScaleY(), 1); 205 true3dMat.translate(-properties().getPivotX(), -properties().getPivotY()); 206 207 matrix.multiply(true3dMat); 208 } 209 } 210 } 211} 212 213/** 214 * Organizes the DisplayList hierarchy to prepare for background projection reordering. 215 * 216 * This should be called before a call to defer() or drawDisplayList() 217 * 218 * Each DisplayList that serves as a 3d root builds its list of composited children, 219 * which are flagged to not draw in the standard draw loop. 220 */ 221void RenderNode::computeOrdering() { 222 ATRACE_CALL(); 223 mProjectedNodes.clear(); 224 225 // TODO: create temporary DDLOp and call computeOrderingImpl on top DisplayList so that 226 // transform properties are applied correctly to top level children 227 if (mDisplayListData == NULL) return; 228 for (unsigned int i = 0; i < mDisplayListData->children.size(); i++) { 229 DrawDisplayListOp* childOp = mDisplayListData->children[i]; 230 childOp->mDisplayList->computeOrderingImpl(childOp, 231 &mProjectedNodes, &mat4::identity()); 232 } 233} 234 235void RenderNode::computeOrderingImpl( 236 DrawDisplayListOp* opState, 237 Vector<DrawDisplayListOp*>* compositedChildrenOfProjectionSurface, 238 const mat4* transformFromProjectionSurface) { 239 mProjectedNodes.clear(); 240 if (mDisplayListData == NULL || mDisplayListData->isEmpty()) return; 241 242 // TODO: should avoid this calculation in most cases 243 // TODO: just calculate single matrix, down to all leaf composited elements 244 Matrix4 localTransformFromProjectionSurface(*transformFromProjectionSurface); 245 localTransformFromProjectionSurface.multiply(opState->mTransformFromParent); 246 247 if (properties().getProjectBackwards()) { 248 // composited projectee, flag for out of order draw, save matrix, and store in proj surface 249 opState->mSkipInOrderDraw = true; 250 opState->mTransformFromCompositingAncestor.load(localTransformFromProjectionSurface); 251 compositedChildrenOfProjectionSurface->add(opState); 252 } else { 253 // standard in order draw 254 opState->mSkipInOrderDraw = false; 255 } 256 257 if (mDisplayListData->children.size() > 0) { 258 const bool isProjectionReceiver = mDisplayListData->projectionReceiveIndex >= 0; 259 bool haveAppliedPropertiesToProjection = false; 260 for (unsigned int i = 0; i < mDisplayListData->children.size(); i++) { 261 DrawDisplayListOp* childOp = mDisplayListData->children[i]; 262 RenderNode* child = childOp->mDisplayList; 263 264 Vector<DrawDisplayListOp*>* projectionChildren = NULL; 265 const mat4* projectionTransform = NULL; 266 if (isProjectionReceiver && !child->properties().getProjectBackwards()) { 267 // if receiving projections, collect projecting descendent 268 269 // Note that if a direct descendent is projecting backwards, we pass it's 270 // grandparent projection collection, since it shouldn't project onto it's 271 // parent, where it will already be drawing. 272 projectionChildren = &mProjectedNodes; 273 projectionTransform = &mat4::identity(); 274 } else { 275 if (!haveAppliedPropertiesToProjection) { 276 applyViewPropertyTransforms(localTransformFromProjectionSurface); 277 haveAppliedPropertiesToProjection = true; 278 } 279 projectionChildren = compositedChildrenOfProjectionSurface; 280 projectionTransform = &localTransformFromProjectionSurface; 281 } 282 child->computeOrderingImpl(childOp, projectionChildren, projectionTransform); 283 } 284 } 285 286} 287 288class DeferOperationHandler { 289public: 290 DeferOperationHandler(DeferStateStruct& deferStruct, int level) 291 : mDeferStruct(deferStruct), mLevel(level) {} 292 inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) { 293 operation->defer(mDeferStruct, saveCount, mLevel, clipToBounds); 294 } 295 inline LinearAllocator& allocator() { return *(mDeferStruct.mAllocator); } 296 297private: 298 DeferStateStruct& mDeferStruct; 299 const int mLevel; 300}; 301 302void RenderNode::defer(DeferStateStruct& deferStruct, const int level) { 303 DeferOperationHandler handler(deferStruct, level); 304 iterate<DeferOperationHandler>(deferStruct.mRenderer, handler, level); 305} 306 307class ReplayOperationHandler { 308public: 309 ReplayOperationHandler(ReplayStateStruct& replayStruct, int level) 310 : mReplayStruct(replayStruct), mLevel(level) {} 311 inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) { 312#if DEBUG_DISPLAY_LIST_OPS_AS_EVENTS 313 properties().getReplayStruct().mRenderer.eventMark(operation->name()); 314#endif 315 operation->replay(mReplayStruct, saveCount, mLevel, clipToBounds); 316 } 317 inline LinearAllocator& allocator() { return *(mReplayStruct.mAllocator); } 318 319private: 320 ReplayStateStruct& mReplayStruct; 321 const int mLevel; 322}; 323 324void RenderNode::replay(ReplayStateStruct& replayStruct, const int level) { 325 ReplayOperationHandler handler(replayStruct, level); 326 327 replayStruct.mRenderer.startMark(mName.string()); 328 iterate<ReplayOperationHandler>(replayStruct.mRenderer, handler, level); 329 replayStruct.mRenderer.endMark(); 330 331 DISPLAY_LIST_LOGD("%*sDone (%p, %s), returning %d", level * 2, "", this, mName.string(), 332 replayStruct.mDrawGlStatus); 333} 334 335void RenderNode::buildZSortedChildList(Vector<ZDrawDisplayListOpPair>& zTranslatedNodes) { 336 if (mDisplayListData == NULL || mDisplayListData->children.size() == 0) return; 337 338 for (unsigned int i = 0; i < mDisplayListData->children.size(); i++) { 339 DrawDisplayListOp* childOp = mDisplayListData->children[i]; 340 RenderNode* child = childOp->mDisplayList; 341 float childZ = child->properties().getTranslationZ(); 342 343 if (childZ != 0.0f) { 344 zTranslatedNodes.add(ZDrawDisplayListOpPair(childZ, childOp)); 345 childOp->mSkipInOrderDraw = true; 346 } else if (!child->properties().getProjectBackwards()) { 347 // regular, in order drawing DisplayList 348 childOp->mSkipInOrderDraw = false; 349 } 350 } 351 352 // Z sort 3d children (stable-ness makes z compare fall back to standard drawing order) 353 std::stable_sort(zTranslatedNodes.begin(), zTranslatedNodes.end()); 354} 355 356#define SHADOW_DELTA 0.1f 357 358template <class T> 359void RenderNode::iterate3dChildren(const Vector<ZDrawDisplayListOpPair>& zTranslatedNodes, 360 ChildrenSelectMode mode, OpenGLRenderer& renderer, T& handler) { 361 const int size = zTranslatedNodes.size(); 362 if (size == 0 363 || (mode == kNegativeZChildren && zTranslatedNodes[0].key > 0.0f) 364 || (mode == kPositiveZChildren && zTranslatedNodes[size - 1].key < 0.0f)) { 365 // no 3d children to draw 366 return; 367 } 368 369 int rootRestoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag); 370 LinearAllocator& alloc = handler.allocator(); 371 ClipRectOp* clipOp = new (alloc) ClipRectOp(0, 0, properties().getWidth(), properties().getHeight(), 372 SkRegion::kIntersect_Op); // clip to 3d root bounds 373 handler(clipOp, PROPERTY_SAVECOUNT, properties().getClipToBounds()); 374 375 /** 376 * Draw shadows and (potential) casters mostly in order, but allow the shadows of casters 377 * with very similar Z heights to draw together. 378 * 379 * This way, if Views A & B have the same Z height and are both casting shadows, the shadows are 380 * underneath both, and neither's shadow is drawn on top of the other. 381 */ 382 const size_t nonNegativeIndex = findNonNegativeIndex(zTranslatedNodes); 383 size_t drawIndex, shadowIndex, endIndex; 384 if (mode == kNegativeZChildren) { 385 drawIndex = 0; 386 endIndex = nonNegativeIndex; 387 shadowIndex = endIndex; // draw no shadows 388 } else { 389 drawIndex = nonNegativeIndex; 390 endIndex = size; 391 shadowIndex = drawIndex; // potentially draw shadow for each pos Z child 392 } 393 float lastCasterZ = 0.0f; 394 while (shadowIndex < endIndex || drawIndex < endIndex) { 395 if (shadowIndex < endIndex) { 396 DrawDisplayListOp* casterOp = zTranslatedNodes[shadowIndex].value; 397 RenderNode* caster = casterOp->mDisplayList; 398 const float casterZ = zTranslatedNodes[shadowIndex].key; 399 // attempt to render the shadow if the caster about to be drawn is its caster, 400 // OR if its caster's Z value is similar to the previous potential caster 401 if (shadowIndex == drawIndex || casterZ - lastCasterZ < SHADOW_DELTA) { 402 403 if (caster->properties().getAlpha() > 0.0f) { 404 mat4 shadowMatrixXY(casterOp->mTransformFromParent); 405 caster->applyViewPropertyTransforms(shadowMatrixXY); 406 407 // Z matrix needs actual 3d transformation, so mapped z values will be correct 408 mat4 shadowMatrixZ(casterOp->mTransformFromParent); 409 caster->applyViewPropertyTransforms(shadowMatrixZ, true); 410 411 DisplayListOp* shadowOp = new (alloc) DrawShadowOp( 412 shadowMatrixXY, shadowMatrixZ, 413 caster->properties().getAlpha(), caster->properties().getOutline().getPath(), 414 caster->properties().getWidth(), caster->properties().getHeight()); 415 handler(shadowOp, PROPERTY_SAVECOUNT, properties().getClipToBounds()); 416 } 417 418 lastCasterZ = casterZ; // must do this even if current caster not casting a shadow 419 shadowIndex++; 420 continue; 421 } 422 } 423 424 // only the actual child DL draw needs to be in save/restore, 425 // since it modifies the renderer's matrix 426 int restoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag); 427 428 DrawDisplayListOp* childOp = zTranslatedNodes[drawIndex].value; 429 RenderNode* child = childOp->mDisplayList; 430 431 renderer.concatMatrix(childOp->mTransformFromParent); 432 childOp->mSkipInOrderDraw = false; // this is horrible, I'm so sorry everyone 433 handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds()); 434 childOp->mSkipInOrderDraw = true; 435 436 renderer.restoreToCount(restoreTo); 437 drawIndex++; 438 } 439 handler(new (alloc) RestoreToCountOp(rootRestoreTo), PROPERTY_SAVECOUNT, properties().getClipToBounds()); 440} 441 442template <class T> 443void RenderNode::iterateProjectedChildren(OpenGLRenderer& renderer, T& handler, const int level) { 444 int rootRestoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag); 445 LinearAllocator& alloc = handler.allocator(); 446 ClipRectOp* clipOp = new (alloc) ClipRectOp(0, 0, properties().getWidth(), properties().getHeight(), 447 SkRegion::kReplace_Op); // clip to projection surface root bounds 448 handler(clipOp, PROPERTY_SAVECOUNT, properties().getClipToBounds()); 449 450 for (size_t i = 0; i < mProjectedNodes.size(); i++) { 451 DrawDisplayListOp* childOp = mProjectedNodes[i]; 452 453 // matrix save, concat, and restore can be done safely without allocating operations 454 int restoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag); 455 renderer.concatMatrix(childOp->mTransformFromCompositingAncestor); 456 childOp->mSkipInOrderDraw = false; // this is horrible, I'm so sorry everyone 457 handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds()); 458 childOp->mSkipInOrderDraw = true; 459 renderer.restoreToCount(restoreTo); 460 } 461 handler(new (alloc) RestoreToCountOp(rootRestoreTo), PROPERTY_SAVECOUNT, properties().getClipToBounds()); 462} 463 464/** 465 * This function serves both defer and replay modes, and will organize the displayList's component 466 * operations for a single frame: 467 * 468 * Every 'simple' state operation that affects just the matrix and alpha (or other factors of 469 * DeferredDisplayState) may be issued directly to the renderer, but complex operations (with custom 470 * defer logic) and operations in displayListOps are issued through the 'handler' which handles the 471 * defer vs replay logic, per operation 472 */ 473template <class T> 474void RenderNode::iterate(OpenGLRenderer& renderer, T& handler, const int level) { 475 if (CC_UNLIKELY(mDestroyed)) { // temporary debug logging 476 ALOGW("Error: %s is drawing after destruction", mName.string()); 477 CRASH(); 478 } 479 if (mDisplayListData->isEmpty() || properties().getAlpha() <= 0) { 480 DISPLAY_LIST_LOGD("%*sEmpty display list (%p, %s)", level * 2, "", this, mName.string()); 481 return; 482 } 483 484#if DEBUG_DISPLAY_LIST 485 Rect* clipRect = renderer.getClipRect(); 486 DISPLAY_LIST_LOGD("%*sStart display list (%p, %s), clipRect: %.0f, %.0f, %.0f, %.0f", 487 level * 2, "", this, mName.string(), clipRect->left, clipRect->top, 488 clipRect->right, clipRect->bottom); 489#endif 490 491 LinearAllocator& alloc = handler.allocator(); 492 int restoreTo = renderer.getSaveCount(); 493 handler(new (alloc) SaveOp(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag), 494 PROPERTY_SAVECOUNT, properties().getClipToBounds()); 495 496 DISPLAY_LIST_LOGD("%*sSave %d %d", (level + 1) * 2, "", 497 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag, restoreTo); 498 499 setViewProperties<T>(renderer, handler, level + 1); 500 501 bool quickRejected = properties().getClipToBounds() && renderer.quickRejectConservative(0, 0, properties().getWidth(), properties().getHeight()); 502 if (!quickRejected) { 503 Vector<ZDrawDisplayListOpPair> zTranslatedNodes; 504 buildZSortedChildList(zTranslatedNodes); 505 506 // for 3d root, draw children with negative z values 507 iterate3dChildren(zTranslatedNodes, kNegativeZChildren, renderer, handler); 508 509 DisplayListLogBuffer& logBuffer = DisplayListLogBuffer::getInstance(); 510 const int saveCountOffset = renderer.getSaveCount() - 1; 511 const int projectionReceiveIndex = mDisplayListData->projectionReceiveIndex; 512 for (unsigned int i = 0; i < mDisplayListData->displayListOps.size(); i++) { 513 DisplayListOp *op = mDisplayListData->displayListOps[i]; 514 515#if DEBUG_DISPLAY_LIST 516 op->output(level + 1); 517#endif 518 519 logBuffer.writeCommand(level, op->name()); 520 handler(op, saveCountOffset, properties().getClipToBounds()); 521 522 if (CC_UNLIKELY(i == projectionReceiveIndex && mProjectedNodes.size() > 0)) { 523 iterateProjectedChildren(renderer, handler, level); 524 } 525 } 526 527 // for 3d root, draw children with positive z values 528 iterate3dChildren(zTranslatedNodes, kPositiveZChildren, renderer, handler); 529 } 530 531 DISPLAY_LIST_LOGD("%*sRestoreToCount %d", (level + 1) * 2, "", restoreTo); 532 handler(new (alloc) RestoreToCountOp(restoreTo), 533 PROPERTY_SAVECOUNT, properties().getClipToBounds()); 534 renderer.setOverrideLayerAlpha(1.0f); 535} 536 537} /* namespace uirenderer */ 538} /* namespace android */ 539