diff options
Diffstat (limited to 'libs/hwui/DeferredDisplayList.cpp')
| -rw-r--r-- | libs/hwui/DeferredDisplayList.cpp | 285 |
1 files changed, 212 insertions, 73 deletions
diff --git a/libs/hwui/DeferredDisplayList.cpp b/libs/hwui/DeferredDisplayList.cpp index 9323a3a..7eb7028 100644 --- a/libs/hwui/DeferredDisplayList.cpp +++ b/libs/hwui/DeferredDisplayList.cpp @@ -20,6 +20,8 @@ #include <SkCanvas.h> #include <utils/Trace.h> +#include <ui/Rect.h> +#include <ui/Region.h> #include "Caches.h" #include "Debug.h" @@ -51,32 +53,36 @@ class Batch { public: virtual status_t replay(OpenGLRenderer& renderer, Rect& dirty, int index) = 0; virtual ~Batch() {} + virtual bool purelyDrawBatch() { return false; } + virtual bool coversBounds(const Rect& bounds) { return false; } }; class DrawBatch : public Batch { public: - DrawBatch(int batchId, mergeid_t mergeId) : mBatchId(batchId), mMergeId(mergeId) { + DrawBatch(const DeferInfo& deferInfo) : mAllOpsOpaque(true), + mBatchId(deferInfo.batchId), mMergeId(deferInfo.mergeId) { mOps.clear(); } virtual ~DrawBatch() { mOps.clear(); } - void add(DrawOp* op) { + virtual void add(DrawOp* op, const DeferredDisplayState* state, bool opaqueOverBounds) { // NOTE: ignore empty bounds special case, since we don't merge across those ops - mBounds.unionWith(op->state.mBounds); - mOps.add(op); + mBounds.unionWith(state->mBounds); + mAllOpsOpaque &= opaqueOverBounds; + mOps.add(OpStatePair(op, state)); } - bool intersects(Rect& rect) { + bool intersects(const Rect& rect) { if (!rect.intersects(mBounds)) return false; for (unsigned int i = 0; i < mOps.size(); i++) { - if (rect.intersects(mOps[i]->state.mBounds)) { + if (rect.intersects(mOps[i].state->mBounds)) { #if DEBUG_DEFER - DEFER_LOGD("op intersects with op %p with bounds %f %f %f %f:", mOps[i], - mOps[i]->state.mBounds.left, mOps[i]->state.mBounds.top, - mOps[i]->state.mBounds.right, mOps[i]->state.mBounds.bottom); - mOps[i]->output(2); + DEFER_LOGD("op intersects with op %p with bounds %f %f %f %f:", mOps[i].op, + mOps[i].state->mBounds.left, mOps[i].state->mBounds.top, + mOps[i].state->mBounds.right, mOps[i].state->mBounds.bottom); + mOps[i].op->output(2); #endif return true; } @@ -85,15 +91,15 @@ public: } virtual status_t replay(OpenGLRenderer& renderer, Rect& dirty, int index) { - DEFER_LOGD("%d replaying DrawingBatch %p, with %d ops (batch id %x, merge id %p)", - index, this, mOps.size(), mOps[0]->getBatchId(), mOps[0]->getMergeId()); + DEFER_LOGD("%d replaying DrawBatch %p, with %d ops (batch id %x, merge id %p)", + index, this, mOps.size(), getBatchId(), getMergeId()); status_t status = DrawGlInfo::kStatusDone; DisplayListLogBuffer& logBuffer = DisplayListLogBuffer::getInstance(); for (unsigned int i = 0; i < mOps.size(); i++) { - DrawOp* op = mOps[i]; - - renderer.restoreDisplayState(op->state); + DrawOp* op = mOps[i].op; + const DeferredDisplayState* state = mOps[i].state; + renderer.restoreDisplayState(*state); #if DEBUG_DISPLAY_LIST_OPS_AS_EVENTS renderer.eventMark(op->name()); @@ -102,7 +108,7 @@ public: status |= op->applyDraw(renderer, dirty); #if DEBUG_MERGE_BEHAVIOR - Rect& bounds = mOps[i]->state.mBounds; + const Rect& bounds = state->mBounds; int batchColor = 0x1f000000; if (getBatchId() & 0x1) batchColor |= 0x0000ff; if (getBatchId() & 0x2) batchColor |= 0x00ff00; @@ -114,14 +120,28 @@ public: return status; } + virtual bool purelyDrawBatch() { return true; } + + virtual bool coversBounds(const Rect& bounds) { + if (CC_LIKELY(!mAllOpsOpaque || !mBounds.contains(bounds) || count() == 1)) return false; + + Region uncovered(android::Rect(bounds.left, bounds.top, bounds.right, bounds.bottom)); + for (unsigned int i = 0; i < mOps.size(); i++) { + const Rect &r = mOps[i].state->mBounds; + uncovered.subtractSelf(android::Rect(r.left, r.top, r.right, r.bottom)); + } + return uncovered.isEmpty(); + } + inline int getBatchId() const { return mBatchId; } inline mergeid_t getMergeId() const { return mMergeId; } inline int count() const { return mOps.size(); } protected: - Vector<DrawOp*> mOps; - Rect mBounds; + Vector<OpStatePair> mOps; + Rect mBounds; // union of bounds of contained ops private: + bool mAllOpsOpaque; int mBatchId; mergeid_t mMergeId; }; @@ -132,39 +152,77 @@ private: class MergingDrawBatch : public DrawBatch { public: - MergingDrawBatch(int batchId, mergeid_t mergeId) : DrawBatch(batchId, mergeId) {} + MergingDrawBatch(DeferInfo& deferInfo, int width, int height) : + DrawBatch(deferInfo), mClipRect(width, height), + mClipSideFlags(kClipSide_None) {} + + /* + * Helper for determining if a new op can merge with a MergingDrawBatch based on their bounds + * and clip side flags. Positive bounds delta means new bounds fit in old. + */ + static inline bool checkSide(const int currentFlags, const int newFlags, const int side, + float boundsDelta) { + bool currentClipExists = currentFlags & side; + bool newClipExists = newFlags & side; + + // if current is clipped, we must be able to fit new bounds in current + if (boundsDelta > 0 && currentClipExists) return false; + + // if new is clipped, we must be able to fit current bounds in new + if (boundsDelta < 0 && newClipExists) return false; + + return true; + } /* * Checks if a (mergeable) op can be merged into this batch * * If true, the op's multiDraw must be guaranteed to handle both ops simultaneously, so it is * important to consider all paint attributes used in the draw calls in deciding both a) if an - * op tries to merge at all, and b) if the op + * op tries to merge at all, and b) if the op can merge with another set of ops * * False positives can lead to information from the paints of subsequent merged operations being * dropped, so we make simplifying qualifications on the ops that can merge, per op type. */ - bool canMergeWith(DrawOp* op) { - if (!op->state.mMatrix.isPureTranslate()) return false; - + bool canMergeWith(const DrawOp* op, const DeferredDisplayState* state) { bool isTextBatch = getBatchId() == DeferredDisplayList::kOpBatch_Text || getBatchId() == DeferredDisplayList::kOpBatch_ColorText; // Overlapping other operations is only allowed for text without shadow. For other ops, // multiDraw isn't guaranteed to overdraw correctly - if (!isTextBatch || op->state.mDrawModifiers.mHasShadow) { - if (intersects(op->state.mBounds)) return false; + if (!isTextBatch || state->mDrawModifiers.mHasShadow) { + if (intersects(state->mBounds)) return false; } + const DeferredDisplayState* lhs = state; + const DeferredDisplayState* rhs = mOps[0].state; - const DeferredDisplayState& lhs = op->state; - const DeferredDisplayState& rhs = mOps[0]->state; + if (NEQ_FALPHA(lhs->mAlpha, rhs->mAlpha)) return false; - if (NEQ_FALPHA(lhs.mAlpha, rhs.mAlpha)) return false; + /* Clipping compatibility check + * + * Exploits the fact that if a op or batch is clipped on a side, its bounds will equal its + * clip for that side. + */ + const int currentFlags = mClipSideFlags; + const int newFlags = state->mClipSideFlags; + if (currentFlags != kClipSide_None || newFlags != kClipSide_None) { + const Rect& opBounds = state->mBounds; + float boundsDelta = mBounds.left - opBounds.left; + if (!checkSide(currentFlags, newFlags, kClipSide_Left, boundsDelta)) return false; + boundsDelta = mBounds.top - opBounds.top; + if (!checkSide(currentFlags, newFlags, kClipSide_Top, boundsDelta)) return false; + + // right and bottom delta calculation reversed to account for direction + boundsDelta = opBounds.right - mBounds.right; + if (!checkSide(currentFlags, newFlags, kClipSide_Right, boundsDelta)) return false; + boundsDelta = opBounds.bottom - mBounds.bottom; + if (!checkSide(currentFlags, newFlags, kClipSide_Bottom, boundsDelta)) return false; + } // if paints are equal, then modifiers + paint attribs don't need to be compared - if (op->mPaint == mOps[0]->mPaint) return true; + if (op->mPaint == mOps[0].op->mPaint) return true; - if (op->getPaintAlpha() != mOps[0]->getPaintAlpha()) return false; + if (op->getPaintAlpha() != mOps[0].op->getPaintAlpha()) return false; /* Draw Modifiers compatibility check * @@ -178,9 +236,8 @@ public: * * These ignore cases prevent us from simply memcmp'ing the drawModifiers */ - - const DrawModifiers& lhsMod = lhs.mDrawModifiers; - const DrawModifiers& rhsMod = rhs.mDrawModifiers; + const DrawModifiers& lhsMod = lhs->mDrawModifiers; + const DrawModifiers& rhsMod = rhs->mDrawModifiers; if (lhsMod.mShader != rhsMod.mShader) return false; if (lhsMod.mColorFilter != rhsMod.mColorFilter) return false; @@ -192,17 +249,37 @@ public: return true; } + virtual void add(DrawOp* op, const DeferredDisplayState* state, bool opaqueOverBounds) { + DrawBatch::add(op, state, opaqueOverBounds); + + const int newClipSideFlags = state->mClipSideFlags; + mClipSideFlags |= newClipSideFlags; + if (newClipSideFlags & kClipSide_Left) mClipRect.left = state->mClip.left; + if (newClipSideFlags & kClipSide_Top) mClipRect.top = state->mClip.top; + if (newClipSideFlags & kClipSide_Right) mClipRect.right = state->mClip.right; + if (newClipSideFlags & kClipSide_Bottom) mClipRect.bottom = state->mClip.bottom; + } + virtual status_t replay(OpenGLRenderer& renderer, Rect& dirty, int index) { - DEFER_LOGD("%d replaying DrawingBatch %p, with %d ops (batch id %x, merge id %p)", - index, this, mOps.size(), getBatchId(), getMergeId()); + DEFER_LOGD("%d replaying MergingDrawBatch %p, with %d ops," + " clip flags %x (batch id %x, merge id %p)", + index, this, mOps.size(), mClipSideFlags, getBatchId(), getMergeId()); if (mOps.size() == 1) { - return DrawBatch::replay(renderer, dirty, false); + return DrawBatch::replay(renderer, dirty, -1); } - DrawOp* op = mOps[0]; + // clipping in the merged case is done ahead of time since all ops share the clip (if any) + renderer.setupMergedMultiDraw(mClipSideFlags ? &mClipRect : NULL); + + DrawOp* op = mOps[0].op; DisplayListLogBuffer& buffer = DisplayListLogBuffer::getInstance(); buffer.writeCommand(0, "multiDraw"); buffer.writeCommand(1, op->name()); + +#if DEBUG_DISPLAY_LIST_OPS_AS_EVENTS + renderer.eventMark("multiDraw"); + renderer.eventMark(op->name()); +#endif status_t status = op->multiDraw(renderer, dirty, mOps, mBounds); #if DEBUG_MERGE_BEHAVIOR @@ -211,16 +288,25 @@ public: #endif return status; } + +private: + /* + * Contains the effective clip rect shared by all merged ops. Initialized to the layer viewport, + * it will shrink if an op must be clipped on a certain side. The clipped sides are reflected in + * mClipSideFlags. + */ + Rect mClipRect; + int mClipSideFlags; }; class StateOpBatch : public Batch { public: // creates a single operation batch - StateOpBatch(StateOp* op) : mOp(op) {} + StateOpBatch(const StateOp* op, const DeferredDisplayState* state) : mOp(op), mState(state) {} virtual status_t replay(OpenGLRenderer& renderer, Rect& dirty, int index) { DEFER_LOGD("replaying state op batch %p", this); - renderer.restoreDisplayState(mOp->state); + renderer.restoreDisplayState(*mState); // use invalid save count because it won't be used at flush time - RestoreToCountOp is the // only one to use it, and we don't use that class at flush time, instead calling @@ -232,16 +318,18 @@ public: private: const StateOp* mOp; + const DeferredDisplayState* mState; }; class RestoreToCountBatch : public Batch { public: - RestoreToCountBatch(StateOp* op, int restoreCount) : mOp(op), mRestoreCount(restoreCount) {} + RestoreToCountBatch(const StateOp* op, const DeferredDisplayState* state, int restoreCount) : + mOp(op), mState(state), mRestoreCount(restoreCount) {} virtual status_t replay(OpenGLRenderer& renderer, Rect& dirty, int index) { DEFER_LOGD("batch %p restoring to count %d", this, mRestoreCount); - renderer.restoreDisplayState(mOp->state); + renderer.restoreDisplayState(*mState); renderer.restoreToCount(mRestoreCount); return DrawGlInfo::kStatusDone; } @@ -249,6 +337,8 @@ public: private: // we use the state storage for the RestoreToCountOp, but don't replay the op itself const StateOp* mOp; + const DeferredDisplayState* mState; + /* * The count used here represents the flush() time saveCount. This is as opposed to the * DisplayList record time, or defer() time values (which are RestoreToCountOp's mCount, and @@ -294,6 +384,7 @@ void DeferredDisplayList::clear() { mBatches.clear(); mSaveStack.clear(); mEarliestBatchIndex = 0; + mEarliestUnclearedIndex = 0; } ///////////////////////////////////////////////////////////////////////////////// @@ -398,22 +489,45 @@ void DeferredDisplayList::addRestoreToCount(OpenGLRenderer& renderer, StateOp* o } void DeferredDisplayList::addDrawOp(OpenGLRenderer& renderer, DrawOp* op) { - if (renderer.storeDisplayState(op->state, getDrawOpDeferFlags())) { + /* 1: op calculates local bounds */ + DeferredDisplayState* const state = createState(); + if (op->getLocalBounds(renderer.getDrawModifiers(), state->mBounds)) { + if (state->mBounds.isEmpty()) { + // valid empty bounds, don't bother deferring + tryRecycleState(state); + return; + } + } else { + state->mBounds.setEmpty(); + } + + /* 2: renderer calculates global bounds + stores state */ + if (renderer.storeDisplayState(*state, getDrawOpDeferFlags())) { + tryRecycleState(state); return; // quick rejected } - int batchId = kOpBatch_None; - mergeid_t mergeId = (mergeid_t) -1; - bool mergeable = op->onDefer(renderer, &batchId, &mergeId); + /* 3: ask op for defer info, given renderer state */ + DeferInfo deferInfo; + op->onDefer(renderer, deferInfo, *state); // complex clip has a complex set of expectations on the renderer state - for now, avoid taking // the merge path in those cases - mergeable &= !recordingComplexClip(); + deferInfo.mergeable &= !recordingComplexClip(); + deferInfo.opaqueOverBounds &= !recordingComplexClip() && mSaveStack.isEmpty(); + + if (CC_LIKELY(mAvoidOverdraw) && mBatches.size() && + state->mClipSideFlags != kClipSide_ConservativeFull && + deferInfo.opaqueOverBounds && state->mBounds.contains(mBounds)) { + // avoid overdraw by resetting drawing state + discarding drawing ops + discardDrawingBatches(mBatches.size() - 1); + resetBatchingState(); + } if (CC_UNLIKELY(renderer.getCaches().drawReorderDisabled)) { // TODO: elegant way to reuse batches? - DrawBatch* b = new DrawBatch(batchId, mergeId); - b->add(op); + DrawBatch* b = new DrawBatch(deferInfo); + b->add(op, state, deferInfo.opaqueOverBounds); mBatches.add(b); return; } @@ -425,10 +539,10 @@ void DeferredDisplayList::addDrawOp(OpenGLRenderer& renderer, DrawOp* op) { // (eventually, should be similar shader) int insertBatchIndex = mBatches.size(); if (!mBatches.isEmpty()) { - if (op->state.mBounds.isEmpty()) { + if (state->mBounds.isEmpty()) { // don't know the bounds for op, so add to last batch and start from scratch on next op - DrawBatch* b = new DrawBatch(batchId, mergeId); - b->add(op); + DrawBatch* b = new DrawBatch(deferInfo); + b->add(op, state, deferInfo.opaqueOverBounds); mBatches.add(b); resetBatchingState(); #if DEBUG_DEFER @@ -438,19 +552,19 @@ void DeferredDisplayList::addDrawOp(OpenGLRenderer& renderer, DrawOp* op) { return; } - if (mergeable) { + if (deferInfo.mergeable) { // Try to merge with any existing batch with same mergeId. - if (mMergingBatches[batchId].get(mergeId, targetBatch)) { - if (!((MergingDrawBatch*) targetBatch)->canMergeWith(op)) { + if (mMergingBatches[deferInfo.batchId].get(deferInfo.mergeId, targetBatch)) { + if (!((MergingDrawBatch*) targetBatch)->canMergeWith(op, state)) { targetBatch = NULL; } } } else { // join with similar, non-merging batch - targetBatch = (DrawBatch*)mBatchLookup[batchId]; + targetBatch = (DrawBatch*)mBatchLookup[deferInfo.batchId]; } - if (targetBatch || mergeable) { + if (targetBatch || deferInfo.mergeable) { // iterate back toward target to see if anything drawn since should overlap the new op // if no target, merging ops still interate to find similar batch to insert after for (int i = mBatches.size() - 1; i >= mEarliestBatchIndex; i--) { @@ -459,19 +573,19 @@ void DeferredDisplayList::addDrawOp(OpenGLRenderer& renderer, DrawOp* op) { if (overBatch == targetBatch) break; // TODO: also consider shader shared between batch types - if (batchId == overBatch->getBatchId()) { + if (deferInfo.batchId == overBatch->getBatchId()) { insertBatchIndex = i + 1; if (!targetBatch) break; // found insert position, quit } - if (overBatch->intersects(op->state.mBounds)) { + if (overBatch->intersects(state->mBounds)) { // NOTE: it may be possible to optimize for special cases where two operations // of the same batch/paint could swap order, such as with a non-mergeable // (clipped) and a mergeable text operation targetBatch = NULL; #if DEBUG_DEFER - DEFER_LOGD("op couldn't join batch %d, was intersected by batch %d", - targetIndex, i); + DEFER_LOGD("op couldn't join batch %p, was intersected by batch %d", + targetBatch, i); op->output(2); #endif break; @@ -481,27 +595,30 @@ void DeferredDisplayList::addDrawOp(OpenGLRenderer& renderer, DrawOp* op) { } if (!targetBatch) { - if (mergeable) { - targetBatch = new MergingDrawBatch(batchId, mergeId); - mMergingBatches[batchId].put(mergeId, targetBatch); + if (deferInfo.mergeable) { + targetBatch = new MergingDrawBatch(deferInfo, + renderer.getViewportWidth(), renderer.getViewportHeight()); + mMergingBatches[deferInfo.batchId].put(deferInfo.mergeId, targetBatch); } else { - targetBatch = new DrawBatch(batchId, mergeId); - mBatchLookup[batchId] = targetBatch; - DEFER_LOGD("creating Batch %p, bid %x, at %d", - targetBatch, batchId, insertBatchIndex); + targetBatch = new DrawBatch(deferInfo); + mBatchLookup[deferInfo.batchId] = targetBatch; } + DEFER_LOGD("creating %singBatch %p, bid %x, at %d", + deferInfo.mergeable ? "Merg" : "Draw", + targetBatch, deferInfo.batchId, insertBatchIndex); mBatches.insertAt(targetBatch, insertBatchIndex); } - targetBatch->add(op); + targetBatch->add(op, state, deferInfo.opaqueOverBounds); } void DeferredDisplayList::storeStateOpBarrier(OpenGLRenderer& renderer, StateOp* op) { DEFER_LOGD("%p adding state op barrier at pos %d", this, mBatches.size()); - renderer.storeDisplayState(op->state, getStateOpDeferFlags()); - mBatches.add(new StateOpBatch(op)); + DeferredDisplayState* state = createState(); + renderer.storeDisplayState(*state, getStateOpDeferFlags()); + mBatches.add(new StateOpBatch(op, state)); resetBatchingState(); } @@ -512,8 +629,9 @@ void DeferredDisplayList::storeRestoreToCountBarrier(OpenGLRenderer& renderer, S // store displayState for the restore operation, as it may be associated with a saveLayer that // doesn't have kClip_SaveFlag set - renderer.storeDisplayState(op->state, getStateOpDeferFlags()); - mBatches.add(new RestoreToCountBatch(op, newSaveCount)); + DeferredDisplayState* state = createState(); + renderer.storeDisplayState(*state, getStateOpDeferFlags()); + mBatches.add(new RestoreToCountBatch(op, state, newSaveCount)); resetBatchingState(); } @@ -526,7 +644,9 @@ static status_t replayBatchList(const Vector<Batch*>& batchList, status_t status = DrawGlInfo::kStatusDone; for (unsigned int i = 0; i < batchList.size(); i++) { - status |= batchList[i]->replay(renderer, dirty, i); + if (batchList[i]) { + status |= batchList[i]->replay(renderer, dirty, i); + } } DEFER_LOGD("--flushed, drew %d batches", batchList.size()); return status; @@ -548,6 +668,13 @@ status_t DeferredDisplayList::flush(OpenGLRenderer& renderer, Rect& dirty) { DrawModifiers restoreDrawModifiers = renderer.getDrawModifiers(); renderer.save(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag); + if (CC_LIKELY(mAvoidOverdraw)) { + for (unsigned int i = 1; i < mBatches.size(); i++) { + if (mBatches[i] && mBatches[i]->coversBounds(mBounds)) { + discardDrawingBatches(i - 1); + } + } + } // NOTE: depth of the save stack at this point, before playback, should be reflected in // FLUSH_SAVE_STACK_DEPTH, so that save/restores match up correctly status |= replayBatchList(mBatches, renderer, dirty); @@ -560,5 +687,17 @@ status_t DeferredDisplayList::flush(OpenGLRenderer& renderer, Rect& dirty) { return status; } +void DeferredDisplayList::discardDrawingBatches(const unsigned int maxIndex) { + for (unsigned int i = mEarliestUnclearedIndex; i <= maxIndex; i++) { + // leave deferred state ops alone for simplicity (empty save restore pairs may now exist) + if (mBatches[i] && mBatches[i]->purelyDrawBatch()) { + DrawBatch* b = (DrawBatch*) mBatches[i]; + delete mBatches[i]; + mBatches.replaceAt(NULL, i); + } + } + mEarliestUnclearedIndex = maxIndex + 1; +} + }; // namespace uirenderer }; // namespace android |
