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