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