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