DeferredDisplayList.cpp revision 09c2d4fe15fbac2faf8a97ba2cc59132ee12222a
1/* 2 * Copyright (C) 2013 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 LOG_TAG "OpenGLRenderer" 18#define ATRACE_TAG ATRACE_TAG_VIEW 19 20#include <SkCanvas.h> 21 22#include <utils/Trace.h> 23#include <ui/Rect.h> 24#include <ui/Region.h> 25 26#include "Caches.h" 27#include "Debug.h" 28#include "DeferredDisplayList.h" 29#include "DisplayListOp.h" 30#include "OpenGLRenderer.h" 31#include "utils/MathUtils.h" 32 33#if DEBUG_DEFER 34 #define DEFER_LOGD(...) ALOGD(__VA_ARGS__) 35#else 36 #define DEFER_LOGD(...) 37#endif 38 39namespace android { 40namespace uirenderer { 41 42// Depth of the save stack at the beginning of batch playback at flush time 43#define FLUSH_SAVE_STACK_DEPTH 2 44 45#define DEBUG_COLOR_BARRIER 0x1f000000 46#define DEBUG_COLOR_MERGEDBATCH 0x5f7f7fff 47#define DEBUG_COLOR_MERGEDBATCH_SOLO 0x5f7fff7f 48 49///////////////////////////////////////////////////////////////////////////////// 50// Operation Batches 51///////////////////////////////////////////////////////////////////////////////// 52 53class Batch { 54public: 55 virtual status_t replay(OpenGLRenderer& renderer, Rect& dirty, int index) = 0; 56 virtual ~Batch() {} 57 virtual bool purelyDrawBatch() { return false; } 58 virtual bool coversBounds(const Rect& bounds) { return false; } 59}; 60 61class DrawBatch : public Batch { 62public: 63 DrawBatch(const DeferInfo& deferInfo) : mAllOpsOpaque(true), 64 mBatchId(deferInfo.batchId), mMergeId(deferInfo.mergeId) { 65 mOps.clear(); 66 } 67 68 virtual ~DrawBatch() { mOps.clear(); } 69 70 virtual void add(DrawOp* op, const DeferredDisplayState* state, bool opaqueOverBounds) { 71 // NOTE: ignore empty bounds special case, since we don't merge across those ops 72 mBounds.unionWith(state->mBounds); 73 mAllOpsOpaque &= opaqueOverBounds; 74 mOps.add(OpStatePair(op, state)); 75 } 76 77 bool intersects(const Rect& rect) { 78 if (!rect.intersects(mBounds)) return false; 79 80 for (unsigned int i = 0; i < mOps.size(); i++) { 81 if (rect.intersects(mOps[i].state->mBounds)) { 82#if DEBUG_DEFER 83 DEFER_LOGD("op intersects with op %p with bounds %f %f %f %f:", mOps[i].op, 84 mOps[i].state->mBounds.left, mOps[i].state->mBounds.top, 85 mOps[i].state->mBounds.right, mOps[i].state->mBounds.bottom); 86 mOps[i].op->output(2); 87#endif 88 return true; 89 } 90 } 91 return false; 92 } 93 94 virtual status_t replay(OpenGLRenderer& renderer, Rect& dirty, int index) { 95 DEFER_LOGD("%d replaying DrawBatch %p, with %d ops (batch id %x, merge id %p)", 96 index, this, mOps.size(), getBatchId(), getMergeId()); 97 98 status_t status = DrawGlInfo::kStatusDone; 99 DisplayListLogBuffer& logBuffer = DisplayListLogBuffer::getInstance(); 100 for (unsigned int i = 0; i < mOps.size(); i++) { 101 DrawOp* op = mOps[i].op; 102 const DeferredDisplayState* state = mOps[i].state; 103 renderer.restoreDisplayState(*state); 104 105#if DEBUG_DISPLAY_LIST_OPS_AS_EVENTS 106 renderer.eventMark(op->name()); 107#endif 108 logBuffer.writeCommand(0, op->name()); 109 status |= op->applyDraw(renderer, dirty); 110 111#if DEBUG_MERGE_BEHAVIOR 112 const Rect& bounds = state->mBounds; 113 int batchColor = 0x1f000000; 114 if (getBatchId() & 0x1) batchColor |= 0x0000ff; 115 if (getBatchId() & 0x2) batchColor |= 0x00ff00; 116 if (getBatchId() & 0x4) batchColor |= 0xff0000; 117 renderer.drawScreenSpaceColorRect(bounds.left, bounds.top, bounds.right, bounds.bottom, 118 batchColor); 119#endif 120 } 121 return status; 122 } 123 124 virtual bool purelyDrawBatch() { return true; } 125 126 virtual bool coversBounds(const Rect& bounds) { 127 if (CC_LIKELY(!mAllOpsOpaque || !mBounds.contains(bounds) || count() == 1)) return false; 128 129 Region uncovered(android::Rect(bounds.left, bounds.top, bounds.right, bounds.bottom)); 130 for (unsigned int i = 0; i < mOps.size(); i++) { 131 const Rect &r = mOps[i].state->mBounds; 132 uncovered.subtractSelf(android::Rect(r.left, r.top, r.right, r.bottom)); 133 } 134 return uncovered.isEmpty(); 135 } 136 137 inline int getBatchId() const { return mBatchId; } 138 inline mergeid_t getMergeId() const { return mMergeId; } 139 inline int count() const { return mOps.size(); } 140 141protected: 142 Vector<OpStatePair> mOps; 143 Rect mBounds; // union of bounds of contained ops 144private: 145 bool mAllOpsOpaque; 146 int mBatchId; 147 mergeid_t mMergeId; 148}; 149 150class MergingDrawBatch : public DrawBatch { 151public: 152 MergingDrawBatch(DeferInfo& deferInfo, int width, int height) : 153 DrawBatch(deferInfo), mClipRect(width, height), 154 mClipSideFlags(kClipSide_None) {} 155 156 /* 157 * Helper for determining if a new op can merge with a MergingDrawBatch based on their bounds 158 * and clip side flags. Positive bounds delta means new bounds fit in old. 159 */ 160 static inline bool checkSide(const int currentFlags, const int newFlags, const int side, 161 float boundsDelta) { 162 bool currentClipExists = currentFlags & side; 163 bool newClipExists = newFlags & side; 164 165 // if current is clipped, we must be able to fit new bounds in current 166 if (boundsDelta > 0 && currentClipExists) return false; 167 168 // if new is clipped, we must be able to fit current bounds in new 169 if (boundsDelta < 0 && newClipExists) return false; 170 171 return true; 172 } 173 174 /* 175 * Checks if a (mergeable) op can be merged into this batch 176 * 177 * If true, the op's multiDraw must be guaranteed to handle both ops simultaneously, so it is 178 * important to consider all paint attributes used in the draw calls in deciding both a) if an 179 * op tries to merge at all, and b) if the op can merge with another set of ops 180 * 181 * False positives can lead to information from the paints of subsequent merged operations being 182 * dropped, so we make simplifying qualifications on the ops that can merge, per op type. 183 */ 184 bool canMergeWith(const DrawOp* op, const DeferredDisplayState* state) { 185 bool isTextBatch = getBatchId() == DeferredDisplayList::kOpBatch_Text || 186 getBatchId() == DeferredDisplayList::kOpBatch_ColorText; 187 188 // Overlapping other operations is only allowed for text without shadow. For other ops, 189 // multiDraw isn't guaranteed to overdraw correctly 190 if (!isTextBatch || op->hasTextShadow()) { 191 if (intersects(state->mBounds)) return false; 192 } 193 const DeferredDisplayState* lhs = state; 194 const DeferredDisplayState* rhs = mOps[0].state; 195 196 if (!MathUtils::areEqual(lhs->mAlpha, rhs->mAlpha)) return false; 197 198 // Identical round rect clip state means both ops will clip in the same way, or not at all. 199 // As the state objects are const, we can compare their pointers to determine mergeability 200 if (lhs->mRoundRectClipState != rhs->mRoundRectClipState) return false; 201 202 /* Clipping compatibility check 203 * 204 * Exploits the fact that if a op or batch is clipped on a side, its bounds will equal its 205 * clip for that side. 206 */ 207 const int currentFlags = mClipSideFlags; 208 const int newFlags = state->mClipSideFlags; 209 if (currentFlags != kClipSide_None || newFlags != kClipSide_None) { 210 const Rect& opBounds = state->mBounds; 211 float boundsDelta = mBounds.left - opBounds.left; 212 if (!checkSide(currentFlags, newFlags, kClipSide_Left, boundsDelta)) return false; 213 boundsDelta = mBounds.top - opBounds.top; 214 if (!checkSide(currentFlags, newFlags, kClipSide_Top, boundsDelta)) return false; 215 216 // right and bottom delta calculation reversed to account for direction 217 boundsDelta = opBounds.right - mBounds.right; 218 if (!checkSide(currentFlags, newFlags, kClipSide_Right, boundsDelta)) return false; 219 boundsDelta = opBounds.bottom - mBounds.bottom; 220 if (!checkSide(currentFlags, newFlags, kClipSide_Bottom, boundsDelta)) return false; 221 } 222 223 // if paints are equal, then modifiers + paint attribs don't need to be compared 224 if (op->mPaint == mOps[0].op->mPaint) return true; 225 226 if (op->getPaintAlpha() != mOps[0].op->getPaintAlpha()) return false; 227 228 if (op->mPaint && mOps[0].op->mPaint && 229 op->mPaint->getColorFilter() != mOps[0].op->mPaint->getColorFilter()) { 230 return false; 231 } 232 233 if (op->mPaint && mOps[0].op->mPaint && 234 op->mPaint->getShader() != mOps[0].op->mPaint->getShader()) { 235 return false; 236 } 237 238 return true; 239 } 240 241 virtual void add(DrawOp* op, const DeferredDisplayState* state, bool opaqueOverBounds) { 242 DrawBatch::add(op, state, opaqueOverBounds); 243 244 const int newClipSideFlags = state->mClipSideFlags; 245 mClipSideFlags |= newClipSideFlags; 246 if (newClipSideFlags & kClipSide_Left) mClipRect.left = state->mClip.left; 247 if (newClipSideFlags & kClipSide_Top) mClipRect.top = state->mClip.top; 248 if (newClipSideFlags & kClipSide_Right) mClipRect.right = state->mClip.right; 249 if (newClipSideFlags & kClipSide_Bottom) mClipRect.bottom = state->mClip.bottom; 250 } 251 252 virtual status_t replay(OpenGLRenderer& renderer, Rect& dirty, int index) { 253 DEFER_LOGD("%d replaying MergingDrawBatch %p, with %d ops," 254 " clip flags %x (batch id %x, merge id %p)", 255 index, this, mOps.size(), mClipSideFlags, getBatchId(), getMergeId()); 256 if (mOps.size() == 1) { 257 return DrawBatch::replay(renderer, dirty, -1); 258 } 259 260 // clipping in the merged case is done ahead of time since all ops share the clip (if any) 261 renderer.setupMergedMultiDraw(mClipSideFlags ? &mClipRect : NULL); 262 263 DrawOp* op = mOps[0].op; 264 DisplayListLogBuffer& buffer = DisplayListLogBuffer::getInstance(); 265 buffer.writeCommand(0, "multiDraw"); 266 buffer.writeCommand(1, op->name()); 267 268#if DEBUG_DISPLAY_LIST_OPS_AS_EVENTS 269 renderer.eventMark("multiDraw"); 270 renderer.eventMark(op->name()); 271#endif 272 status_t status = op->multiDraw(renderer, dirty, mOps, mBounds); 273 274#if DEBUG_MERGE_BEHAVIOR 275 renderer.drawScreenSpaceColorRect(mBounds.left, mBounds.top, mBounds.right, mBounds.bottom, 276 DEBUG_COLOR_MERGEDBATCH); 277#endif 278 return status; 279 } 280 281private: 282 /* 283 * Contains the effective clip rect shared by all merged ops. Initialized to the layer viewport, 284 * it will shrink if an op must be clipped on a certain side. The clipped sides are reflected in 285 * mClipSideFlags. 286 */ 287 Rect mClipRect; 288 int mClipSideFlags; 289}; 290 291class StateOpBatch : public Batch { 292public: 293 // creates a single operation batch 294 StateOpBatch(const StateOp* op, const DeferredDisplayState* state) : mOp(op), mState(state) {} 295 296 virtual status_t replay(OpenGLRenderer& renderer, Rect& dirty, int index) { 297 DEFER_LOGD("replaying state op batch %p", this); 298 renderer.restoreDisplayState(*mState); 299 300 // use invalid save count because it won't be used at flush time - RestoreToCountOp is the 301 // only one to use it, and we don't use that class at flush time, instead calling 302 // renderer.restoreToCount directly 303 int saveCount = -1; 304 mOp->applyState(renderer, saveCount); 305 return DrawGlInfo::kStatusDone; 306 } 307 308private: 309 const StateOp* mOp; 310 const DeferredDisplayState* mState; 311}; 312 313class RestoreToCountBatch : public Batch { 314public: 315 RestoreToCountBatch(const StateOp* op, const DeferredDisplayState* state, int restoreCount) : 316 mOp(op), mState(state), mRestoreCount(restoreCount) {} 317 318 virtual status_t replay(OpenGLRenderer& renderer, Rect& dirty, int index) { 319 DEFER_LOGD("batch %p restoring to count %d", this, mRestoreCount); 320 321 renderer.restoreDisplayState(*mState); 322 renderer.restoreToCount(mRestoreCount); 323 return DrawGlInfo::kStatusDone; 324 } 325 326private: 327 // we use the state storage for the RestoreToCountOp, but don't replay the op itself 328 const StateOp* mOp; 329 const DeferredDisplayState* mState; 330 331 /* 332 * The count used here represents the flush() time saveCount. This is as opposed to the 333 * DisplayList record time, or defer() time values (which are RestoreToCountOp's mCount, and 334 * (saveCount + mCount) respectively). Since the count is different from the original 335 * RestoreToCountOp, we don't store a pointer to the op, as elsewhere. 336 */ 337 const int mRestoreCount; 338}; 339 340#if DEBUG_MERGE_BEHAVIOR 341class BarrierDebugBatch : public Batch { 342 virtual status_t replay(OpenGLRenderer& renderer, Rect& dirty, int index) { 343 renderer.drawScreenSpaceColorRect(0, 0, 10000, 10000, DEBUG_COLOR_BARRIER); 344 return DrawGlInfo::kStatusDrew; 345 } 346}; 347#endif 348 349///////////////////////////////////////////////////////////////////////////////// 350// DeferredDisplayList 351///////////////////////////////////////////////////////////////////////////////// 352 353void DeferredDisplayList::resetBatchingState() { 354 for (int i = 0; i < kOpBatch_Count; i++) { 355 mBatchLookup[i] = NULL; 356 mMergingBatches[i].clear(); 357 } 358#if DEBUG_MERGE_BEHAVIOR 359 if (mBatches.size() != 0) { 360 mBatches.add(new BarrierDebugBatch()); 361 } 362#endif 363 mEarliestBatchIndex = mBatches.size(); 364} 365 366void DeferredDisplayList::clear() { 367 resetBatchingState(); 368 mComplexClipStackStart = -1; 369 370 for (unsigned int i = 0; i < mBatches.size(); i++) { 371 delete mBatches[i]; 372 } 373 mBatches.clear(); 374 mSaveStack.clear(); 375 mEarliestBatchIndex = 0; 376 mEarliestUnclearedIndex = 0; 377} 378 379///////////////////////////////////////////////////////////////////////////////// 380// Operation adding 381///////////////////////////////////////////////////////////////////////////////// 382 383int DeferredDisplayList::getStateOpDeferFlags() const { 384 // For both clipOp and save(Layer)Op, we don't want to save drawing info, and only want to save 385 // the clip if we aren't recording a complex clip (and can thus trust it to be a rect) 386 return recordingComplexClip() ? 0 : kStateDeferFlag_Clip; 387} 388 389int DeferredDisplayList::getDrawOpDeferFlags() const { 390 return kStateDeferFlag_Draw | getStateOpDeferFlags(); 391} 392 393/** 394 * When an clipping operation occurs that could cause a complex clip, record the operation and all 395 * subsequent clipOps, save/restores (if the clip flag is set). During a flush, instead of loading 396 * the clip from deferred state, we play back all of the relevant state operations that generated 397 * the complex clip. 398 * 399 * Note that we don't need to record the associated restore operation, since operations at defer 400 * time record whether they should store the renderer's current clip 401 */ 402void DeferredDisplayList::addClip(OpenGLRenderer& renderer, ClipOp* op) { 403 if (recordingComplexClip() || op->canCauseComplexClip() || !renderer.hasRectToRectTransform()) { 404 DEFER_LOGD("%p Received complex clip operation %p", this, op); 405 406 // NOTE: defer clip op before setting mComplexClipStackStart so previous clip is recorded 407 storeStateOpBarrier(renderer, op); 408 409 if (!recordingComplexClip()) { 410 mComplexClipStackStart = renderer.getSaveCount() - 1; 411 DEFER_LOGD(" Starting complex clip region, start is %d", mComplexClipStackStart); 412 } 413 } 414} 415 416/** 417 * For now, we record save layer operations as barriers in the batch list, preventing drawing 418 * operations from reordering around the saveLayer and it's associated restore() 419 * 420 * In the future, we should send saveLayer commands (if they can be played out of order) and their 421 * contained drawing operations to a seperate list of batches, so that they may draw at the 422 * beginning of the frame. This would avoid targetting and removing an FBO in the middle of a frame. 423 * 424 * saveLayer operations should be pulled to the beginning of the frame if the canvas doesn't have a 425 * complex clip, and if the flags (kClip_SaveFlag & kClipToLayer_SaveFlag) are set. 426 */ 427void DeferredDisplayList::addSaveLayer(OpenGLRenderer& renderer, 428 SaveLayerOp* op, int newSaveCount) { 429 DEFER_LOGD("%p adding saveLayerOp %p, flags %x, new count %d", 430 this, op, op->getFlags(), newSaveCount); 431 432 storeStateOpBarrier(renderer, op); 433 mSaveStack.push(newSaveCount); 434} 435 436/** 437 * Takes save op and it's return value - the new save count - and stores it into the stream as a 438 * barrier if it's needed to properly modify a complex clip 439 */ 440void DeferredDisplayList::addSave(OpenGLRenderer& renderer, SaveOp* op, int newSaveCount) { 441 int saveFlags = op->getFlags(); 442 DEFER_LOGD("%p adding saveOp %p, flags %x, new count %d", this, op, saveFlags, newSaveCount); 443 444 if (recordingComplexClip() && (saveFlags & SkCanvas::kClip_SaveFlag)) { 445 // store and replay the save operation, as it may be needed to correctly playback the clip 446 DEFER_LOGD(" adding save barrier with new save count %d", newSaveCount); 447 storeStateOpBarrier(renderer, op); 448 mSaveStack.push(newSaveCount); 449 } 450} 451 452/** 453 * saveLayer() commands must be associated with a restoreToCount batch that will clean up and draw 454 * the layer in the deferred list 455 * 456 * other save() commands which occur as children of a snapshot with complex clip will be deferred, 457 * and must be restored 458 * 459 * Either will act as a barrier to draw operation reordering, as we want to play back layer 460 * save/restore and complex canvas modifications (including save/restore) in order. 461 */ 462void DeferredDisplayList::addRestoreToCount(OpenGLRenderer& renderer, StateOp* op, 463 int newSaveCount) { 464 DEFER_LOGD("%p addRestoreToCount %d", this, newSaveCount); 465 466 if (recordingComplexClip() && newSaveCount <= mComplexClipStackStart) { 467 mComplexClipStackStart = -1; 468 resetBatchingState(); 469 } 470 471 if (mSaveStack.isEmpty() || newSaveCount > mSaveStack.top()) { 472 return; 473 } 474 475 while (!mSaveStack.isEmpty() && mSaveStack.top() >= newSaveCount) mSaveStack.pop(); 476 477 storeRestoreToCountBarrier(renderer, op, mSaveStack.size() + FLUSH_SAVE_STACK_DEPTH); 478} 479 480void DeferredDisplayList::addDrawOp(OpenGLRenderer& renderer, DrawOp* op) { 481 /* 1: op calculates local bounds */ 482 DeferredDisplayState* const state = createState(); 483 if (op->getLocalBounds(state->mBounds)) { 484 if (state->mBounds.isEmpty()) { 485 // valid empty bounds, don't bother deferring 486 tryRecycleState(state); 487 return; 488 } 489 } else { 490 state->mBounds.setEmpty(); 491 } 492 493 /* 2: renderer calculates global bounds + stores state */ 494 if (renderer.storeDisplayState(*state, getDrawOpDeferFlags())) { 495 tryRecycleState(state); 496 return; // quick rejected 497 } 498 499 /* 3: ask op for defer info, given renderer state */ 500 DeferInfo deferInfo; 501 op->onDefer(renderer, deferInfo, *state); 502 503 // complex clip has a complex set of expectations on the renderer state - for now, avoid taking 504 // the merge path in those cases 505 deferInfo.mergeable &= !recordingComplexClip(); 506 deferInfo.opaqueOverBounds &= !recordingComplexClip() && mSaveStack.isEmpty(); 507 508 if (CC_LIKELY(mAvoidOverdraw) && mBatches.size() && 509 state->mClipSideFlags != kClipSide_ConservativeFull && 510 deferInfo.opaqueOverBounds && state->mBounds.contains(mBounds)) { 511 // avoid overdraw by resetting drawing state + discarding drawing ops 512 discardDrawingBatches(mBatches.size() - 1); 513 resetBatchingState(); 514 } 515 516 if (CC_UNLIKELY(renderer.getCaches().drawReorderDisabled)) { 517 // TODO: elegant way to reuse batches? 518 DrawBatch* b = new DrawBatch(deferInfo); 519 b->add(op, state, deferInfo.opaqueOverBounds); 520 mBatches.add(b); 521 return; 522 } 523 524 // find the latest batch of the new op's type, and try to merge the new op into it 525 DrawBatch* targetBatch = NULL; 526 527 // insertion point of a new batch, will hopefully be immediately after similar batch 528 // (eventually, should be similar shader) 529 int insertBatchIndex = mBatches.size(); 530 if (!mBatches.isEmpty()) { 531 if (state->mBounds.isEmpty()) { 532 // don't know the bounds for op, so add to last batch and start from scratch on next op 533 DrawBatch* b = new DrawBatch(deferInfo); 534 b->add(op, state, deferInfo.opaqueOverBounds); 535 mBatches.add(b); 536 resetBatchingState(); 537#if DEBUG_DEFER 538 DEFER_LOGD("Warning: Encountered op with empty bounds, resetting batches"); 539 op->output(2); 540#endif 541 return; 542 } 543 544 if (deferInfo.mergeable) { 545 // Try to merge with any existing batch with same mergeId. 546 if (mMergingBatches[deferInfo.batchId].get(deferInfo.mergeId, targetBatch)) { 547 if (!((MergingDrawBatch*) targetBatch)->canMergeWith(op, state)) { 548 targetBatch = NULL; 549 } 550 } 551 } else { 552 // join with similar, non-merging batch 553 targetBatch = (DrawBatch*)mBatchLookup[deferInfo.batchId]; 554 } 555 556 if (targetBatch || deferInfo.mergeable) { 557 // iterate back toward target to see if anything drawn since should overlap the new op 558 // if no target, merging ops still interate to find similar batch to insert after 559 for (int i = mBatches.size() - 1; i >= mEarliestBatchIndex; i--) { 560 DrawBatch* overBatch = (DrawBatch*)mBatches[i]; 561 562 if (overBatch == targetBatch) break; 563 564 // TODO: also consider shader shared between batch types 565 if (deferInfo.batchId == overBatch->getBatchId()) { 566 insertBatchIndex = i + 1; 567 if (!targetBatch) break; // found insert position, quit 568 } 569 570 if (overBatch->intersects(state->mBounds)) { 571 // NOTE: it may be possible to optimize for special cases where two operations 572 // of the same batch/paint could swap order, such as with a non-mergeable 573 // (clipped) and a mergeable text operation 574 targetBatch = NULL; 575#if DEBUG_DEFER 576 DEFER_LOGD("op couldn't join batch %p, was intersected by batch %d", 577 targetBatch, i); 578 op->output(2); 579#endif 580 break; 581 } 582 } 583 } 584 } 585 586 if (!targetBatch) { 587 if (deferInfo.mergeable) { 588 targetBatch = new MergingDrawBatch(deferInfo, 589 renderer.getViewportWidth(), renderer.getViewportHeight()); 590 mMergingBatches[deferInfo.batchId].put(deferInfo.mergeId, targetBatch); 591 } else { 592 targetBatch = new DrawBatch(deferInfo); 593 mBatchLookup[deferInfo.batchId] = targetBatch; 594 } 595 596 DEFER_LOGD("creating %singBatch %p, bid %x, at %d", 597 deferInfo.mergeable ? "Merg" : "Draw", 598 targetBatch, deferInfo.batchId, insertBatchIndex); 599 mBatches.insertAt(targetBatch, insertBatchIndex); 600 } 601 602 targetBatch->add(op, state, deferInfo.opaqueOverBounds); 603} 604 605void DeferredDisplayList::storeStateOpBarrier(OpenGLRenderer& renderer, StateOp* op) { 606 DEFER_LOGD("%p adding state op barrier at pos %d", this, mBatches.size()); 607 608 DeferredDisplayState* state = createState(); 609 renderer.storeDisplayState(*state, getStateOpDeferFlags()); 610 mBatches.add(new StateOpBatch(op, state)); 611 resetBatchingState(); 612} 613 614void DeferredDisplayList::storeRestoreToCountBarrier(OpenGLRenderer& renderer, StateOp* op, 615 int newSaveCount) { 616 DEFER_LOGD("%p adding restore to count %d barrier, pos %d", 617 this, newSaveCount, mBatches.size()); 618 619 // store displayState for the restore operation, as it may be associated with a saveLayer that 620 // doesn't have kClip_SaveFlag set 621 DeferredDisplayState* state = createState(); 622 renderer.storeDisplayState(*state, getStateOpDeferFlags()); 623 mBatches.add(new RestoreToCountBatch(op, state, newSaveCount)); 624 resetBatchingState(); 625} 626 627///////////////////////////////////////////////////////////////////////////////// 628// Replay / flush 629///////////////////////////////////////////////////////////////////////////////// 630 631static status_t replayBatchList(const Vector<Batch*>& batchList, 632 OpenGLRenderer& renderer, Rect& dirty) { 633 status_t status = DrawGlInfo::kStatusDone; 634 635 for (unsigned int i = 0; i < batchList.size(); i++) { 636 if (batchList[i]) { 637 status |= batchList[i]->replay(renderer, dirty, i); 638 } 639 } 640 DEFER_LOGD("--flushed, drew %d batches", batchList.size()); 641 return status; 642} 643 644status_t DeferredDisplayList::flush(OpenGLRenderer& renderer, Rect& dirty) { 645 ATRACE_NAME("flush drawing commands"); 646 Caches::getInstance().fontRenderer->endPrecaching(); 647 648 status_t status = DrawGlInfo::kStatusDone; 649 650 if (isEmpty()) return status; // nothing to flush 651 renderer.restoreToCount(1); 652 653 DEFER_LOGD("--flushing"); 654 renderer.eventMark("Flush"); 655 656 // save and restore (with draw modifiers) so that reordering doesn't affect final state 657 DrawModifiers restoreDrawModifiers = renderer.getDrawModifiers(); 658 renderer.save(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag); 659 660 if (CC_LIKELY(mAvoidOverdraw)) { 661 for (unsigned int i = 1; i < mBatches.size(); i++) { 662 if (mBatches[i] && mBatches[i]->coversBounds(mBounds)) { 663 discardDrawingBatches(i - 1); 664 } 665 } 666 } 667 // NOTE: depth of the save stack at this point, before playback, should be reflected in 668 // FLUSH_SAVE_STACK_DEPTH, so that save/restores match up correctly 669 status |= replayBatchList(mBatches, renderer, dirty); 670 671 renderer.restoreToCount(1); 672 renderer.setDrawModifiers(restoreDrawModifiers); 673 674 DEFER_LOGD("--flush complete, returning %x", status); 675 clear(); 676 return status; 677} 678 679void DeferredDisplayList::discardDrawingBatches(const unsigned int maxIndex) { 680 for (unsigned int i = mEarliestUnclearedIndex; i <= maxIndex; i++) { 681 // leave deferred state ops alone for simplicity (empty save restore pairs may now exist) 682 if (mBatches[i] && mBatches[i]->purelyDrawBatch()) { 683 DrawBatch* b = (DrawBatch*) mBatches[i]; 684 delete mBatches[i]; 685 mBatches.replaceAt(NULL, i); 686 } 687 } 688 mEarliestUnclearedIndex = maxIndex + 1; 689} 690 691}; // namespace uirenderer 692}; // namespace android 693