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