Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #define LOG_TAG "OpenGLRenderer" |
| 18 | #define ATRACE_TAG ATRACE_TAG_VIEW |
| 19 | |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 20 | #include <SkCanvas.h> |
| 21 | |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 22 | #include <utils/Trace.h> |
Chris Craik | 28ce94a | 2013-05-31 11:38:03 -0700 | [diff] [blame] | 23 | #include <ui/Rect.h> |
| 24 | #include <ui/Region.h> |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 25 | |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 26 | #include "Caches.h" |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 27 | #include "Debug.h" |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 28 | #include "DeferredDisplayList.h" |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 29 | #include "DisplayListOp.h" |
| 30 | #include "OpenGLRenderer.h" |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 31 | #include "utils/MathUtils.h" |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 32 | |
| 33 | #if DEBUG_DEFER |
| 34 | #define DEFER_LOGD(...) ALOGD(__VA_ARGS__) |
| 35 | #else |
| 36 | #define DEFER_LOGD(...) |
| 37 | #endif |
| 38 | |
| 39 | namespace android { |
| 40 | namespace uirenderer { |
| 41 | |
Chris Craik | 1ed30c9 | 2013-04-03 12:37:35 -0700 | [diff] [blame] | 42 | // Depth of the save stack at the beginning of batch playback at flush time |
| 43 | #define FLUSH_SAVE_STACK_DEPTH 2 |
| 44 | |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 45 | #define DEBUG_COLOR_BARRIER 0x1f000000 |
| 46 | #define DEBUG_COLOR_MERGEDBATCH 0x5f7f7fff |
| 47 | #define DEBUG_COLOR_MERGEDBATCH_SOLO 0x5f7fff7f |
| 48 | |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 49 | ///////////////////////////////////////////////////////////////////////////////// |
| 50 | // Operation Batches |
| 51 | ///////////////////////////////////////////////////////////////////////////////// |
| 52 | |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 53 | class Batch { |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 54 | public: |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 55 | virtual status_t replay(OpenGLRenderer& renderer, Rect& dirty, int index) = 0; |
| 56 | virtual ~Batch() {} |
Chris Craik | 28ce94a | 2013-05-31 11:38:03 -0700 | [diff] [blame] | 57 | virtual bool purelyDrawBatch() { return false; } |
John Reck | 8b59a52 | 2014-11-22 00:10:02 +0000 | [diff] [blame] | 58 | virtual bool coversBounds(const Rect& bounds) { return false; } |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 59 | }; |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 60 | |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 61 | class DrawBatch : public Batch { |
| 62 | public: |
Chris Craik | 28ce94a | 2013-05-31 11:38:03 -0700 | [diff] [blame] | 63 | DrawBatch(const DeferInfo& deferInfo) : mAllOpsOpaque(true), |
| 64 | mBatchId(deferInfo.batchId), mMergeId(deferInfo.mergeId) { |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 65 | mOps.clear(); |
| 66 | } |
| 67 | |
| 68 | virtual ~DrawBatch() { mOps.clear(); } |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 69 | |
Chris Craik | c1c5f08 | 2013-09-11 16:23:37 -0700 | [diff] [blame] | 70 | virtual void add(DrawOp* op, const DeferredDisplayState* state, bool opaqueOverBounds) { |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 71 | // NOTE: ignore empty bounds special case, since we don't merge across those ops |
Chris Craik | c1c5f08 | 2013-09-11 16:23:37 -0700 | [diff] [blame] | 72 | mBounds.unionWith(state->mBounds); |
Chris Craik | 28ce94a | 2013-05-31 11:38:03 -0700 | [diff] [blame] | 73 | mAllOpsOpaque &= opaqueOverBounds; |
Chris Craik | c1c5f08 | 2013-09-11 16:23:37 -0700 | [diff] [blame] | 74 | mOps.add(OpStatePair(op, state)); |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 75 | } |
| 76 | |
Chris Craik | c1c5f08 | 2013-09-11 16:23:37 -0700 | [diff] [blame] | 77 | bool intersects(const Rect& rect) { |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 78 | if (!rect.intersects(mBounds)) return false; |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 79 | |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 80 | for (unsigned int i = 0; i < mOps.size(); i++) { |
Chris Craik | c1c5f08 | 2013-09-11 16:23:37 -0700 | [diff] [blame] | 81 | if (rect.intersects(mOps[i].state->mBounds)) { |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 82 | #if DEBUG_DEFER |
Chris Craik | c1c5f08 | 2013-09-11 16:23:37 -0700 | [diff] [blame] | 83 | DEFER_LOGD("op intersects with op %p with bounds %f %f %f %f:", mOps[i].op, |
| 84 | mOps[i].state->mBounds.left, mOps[i].state->mBounds.top, |
| 85 | mOps[i].state->mBounds.right, mOps[i].state->mBounds.bottom); |
| 86 | mOps[i].op->output(2); |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 87 | #endif |
| 88 | return true; |
| 89 | } |
| 90 | } |
| 91 | return false; |
| 92 | } |
| 93 | |
John Reck | 8b59a52 | 2014-11-22 00:10:02 +0000 | [diff] [blame] | 94 | virtual status_t replay(OpenGLRenderer& renderer, Rect& dirty, int index) { |
Chris Craik | 4154182 | 2013-05-03 16:35:54 -0700 | [diff] [blame] | 95 | DEFER_LOGD("%d replaying DrawBatch %p, with %d ops (batch id %x, merge id %p)", |
| 96 | index, this, mOps.size(), getBatchId(), getMergeId()); |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 97 | |
| 98 | status_t status = DrawGlInfo::kStatusDone; |
| 99 | DisplayListLogBuffer& logBuffer = DisplayListLogBuffer::getInstance(); |
| 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 |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 108 | logBuffer.writeCommand(0, op->name()); |
Chris Craik | d4b43b3 | 2013-05-09 13:07:52 -0700 | [diff] [blame] | 109 | status |= op->applyDraw(renderer, dirty); |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 110 | |
| 111 | #if DEBUG_MERGE_BEHAVIOR |
Chris Craik | c1c5f08 | 2013-09-11 16:23:37 -0700 | [diff] [blame] | 112 | const Rect& bounds = state->mBounds; |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 113 | int batchColor = 0x1f000000; |
| 114 | if (getBatchId() & 0x1) batchColor |= 0x0000ff; |
| 115 | if (getBatchId() & 0x2) batchColor |= 0x00ff00; |
| 116 | if (getBatchId() & 0x4) batchColor |= 0xff0000; |
| 117 | renderer.drawScreenSpaceColorRect(bounds.left, bounds.top, bounds.right, bounds.bottom, |
| 118 | batchColor); |
| 119 | #endif |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 120 | } |
| 121 | return status; |
| 122 | } |
| 123 | |
Chris Craik | 28ce94a | 2013-05-31 11:38:03 -0700 | [diff] [blame] | 124 | virtual bool purelyDrawBatch() { return true; } |
| 125 | |
| 126 | virtual bool coversBounds(const Rect& bounds) { |
| 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: |
Chris Craik | c1c5f08 | 2013-09-11 16:23:37 -0700 | [diff] [blame] | 142 | 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 | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 201 | |
Chris Craik | a02c4ed | 2013-06-14 13:43:58 -0700 | [diff] [blame] | 202 | /* Clipping compatibility check |
| 203 | * |
| 204 | * Exploits the fact that if a op or batch is clipped on a side, its bounds will equal its |
| 205 | * clip for that side. |
| 206 | */ |
| 207 | const int currentFlags = mClipSideFlags; |
Chris Craik | c1c5f08 | 2013-09-11 16:23:37 -0700 | [diff] [blame] | 208 | const int newFlags = state->mClipSideFlags; |
Chris Craik | a02c4ed | 2013-06-14 13:43:58 -0700 | [diff] [blame] | 209 | if (currentFlags != kClipSide_None || newFlags != kClipSide_None) { |
Chris Craik | c1c5f08 | 2013-09-11 16:23:37 -0700 | [diff] [blame] | 210 | const Rect& opBounds = state->mBounds; |
Chris Craik | a02c4ed | 2013-06-14 13:43:58 -0700 | [diff] [blame] | 211 | float boundsDelta = mBounds.left - opBounds.left; |
| 212 | if (!checkSide(currentFlags, newFlags, kClipSide_Left, boundsDelta)) return false; |
| 213 | boundsDelta = mBounds.top - opBounds.top; |
| 214 | if (!checkSide(currentFlags, newFlags, kClipSide_Top, boundsDelta)) return false; |
| 215 | |
| 216 | // right and bottom delta calculation reversed to account for direction |
| 217 | boundsDelta = opBounds.right - mBounds.right; |
| 218 | if (!checkSide(currentFlags, newFlags, kClipSide_Right, boundsDelta)) return false; |
| 219 | boundsDelta = opBounds.bottom - mBounds.bottom; |
| 220 | if (!checkSide(currentFlags, newFlags, kClipSide_Bottom, boundsDelta)) return false; |
Chris Craik | 28ce94a | 2013-05-31 11:38:03 -0700 | [diff] [blame] | 221 | } |
Chris Craik | 28ce94a | 2013-05-31 11:38:03 -0700 | [diff] [blame] | 222 | |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 223 | // 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] | 224 | if (op->mPaint == mOps[0].op->mPaint) return true; |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 225 | |
Chris Craik | c1c5f08 | 2013-09-11 16:23:37 -0700 | [diff] [blame] | 226 | if (op->getPaintAlpha() != mOps[0].op->getPaintAlpha()) return false; |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 227 | |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 228 | if (op->mPaint && mOps[0].op->mPaint && |
| 229 | op->mPaint->getColorFilter() != mOps[0].op->mPaint->getColorFilter()) { |
| 230 | return false; |
| 231 | } |
| 232 | |
Leon Scroggins III | d1ad5e6 | 2014-05-05 12:50:38 -0400 | [diff] [blame] | 233 | if (op->mPaint && mOps[0].op->mPaint && |
| 234 | op->mPaint->getShader() != mOps[0].op->mPaint->getShader()) { |
| 235 | return false; |
| 236 | } |
| 237 | |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 238 | /* Draw Modifiers compatibility check |
| 239 | * |
| 240 | * Shadows are ignored, as only text uses them, and in that case they are drawn |
| 241 | * per-DrawTextOp, before the unified text draw. Because of this, it's always safe to merge |
| 242 | * text UNLESS a later draw's shadow should overlays a previous draw's text. This is covered |
| 243 | * above with the intersection check. |
| 244 | * |
| 245 | * OverrideLayerAlpha is also ignored, as it's only used for drawing layers, which are never |
| 246 | * merged. |
| 247 | * |
| 248 | * These ignore cases prevent us from simply memcmp'ing the drawModifiers |
| 249 | */ |
Chris Craik | c1c5f08 | 2013-09-11 16:23:37 -0700 | [diff] [blame] | 250 | const DrawModifiers& lhsMod = lhs->mDrawModifiers; |
| 251 | const DrawModifiers& rhsMod = rhs->mDrawModifiers; |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 252 | |
| 253 | // Draw filter testing expects bit fields to be clear if filter not set. |
| 254 | if (lhsMod.mHasDrawFilter != rhsMod.mHasDrawFilter) return false; |
| 255 | if (lhsMod.mPaintFilterClearBits != rhsMod.mPaintFilterClearBits) return false; |
| 256 | if (lhsMod.mPaintFilterSetBits != rhsMod.mPaintFilterSetBits) return false; |
| 257 | |
| 258 | return true; |
| 259 | } |
| 260 | |
Chris Craik | 5af5fc5 | 2013-09-13 14:41:31 -0700 | [diff] [blame] | 261 | virtual void add(DrawOp* op, const DeferredDisplayState* state, bool opaqueOverBounds) { |
Chris Craik | c1c5f08 | 2013-09-11 16:23:37 -0700 | [diff] [blame] | 262 | DrawBatch::add(op, state, opaqueOverBounds); |
Chris Craik | 28ce94a | 2013-05-31 11:38:03 -0700 | [diff] [blame] | 263 | |
Chris Craik | c1c5f08 | 2013-09-11 16:23:37 -0700 | [diff] [blame] | 264 | const int newClipSideFlags = state->mClipSideFlags; |
Chris Craik | 28ce94a | 2013-05-31 11:38:03 -0700 | [diff] [blame] | 265 | mClipSideFlags |= newClipSideFlags; |
Chris Craik | c1c5f08 | 2013-09-11 16:23:37 -0700 | [diff] [blame] | 266 | if (newClipSideFlags & kClipSide_Left) mClipRect.left = state->mClip.left; |
| 267 | if (newClipSideFlags & kClipSide_Top) mClipRect.top = state->mClip.top; |
| 268 | if (newClipSideFlags & kClipSide_Right) mClipRect.right = state->mClip.right; |
| 269 | if (newClipSideFlags & kClipSide_Bottom) mClipRect.bottom = state->mClip.bottom; |
Chris Craik | 28ce94a | 2013-05-31 11:38:03 -0700 | [diff] [blame] | 270 | } |
| 271 | |
John Reck | 8b59a52 | 2014-11-22 00:10:02 +0000 | [diff] [blame] | 272 | virtual status_t replay(OpenGLRenderer& renderer, Rect& dirty, int index) { |
Chris Craik | 28ce94a | 2013-05-31 11:38:03 -0700 | [diff] [blame] | 273 | DEFER_LOGD("%d replaying MergingDrawBatch %p, with %d ops," |
| 274 | " clip flags %x (batch id %x, merge id %p)", |
| 275 | index, this, mOps.size(), mClipSideFlags, getBatchId(), getMergeId()); |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 276 | if (mOps.size() == 1) { |
Chris Craik | 28ce94a | 2013-05-31 11:38:03 -0700 | [diff] [blame] | 277 | return DrawBatch::replay(renderer, dirty, -1); |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 278 | } |
| 279 | |
Chris Craik | 28ce94a | 2013-05-31 11:38:03 -0700 | [diff] [blame] | 280 | // clipping in the merged case is done ahead of time since all ops share the clip (if any) |
| 281 | renderer.setupMergedMultiDraw(mClipSideFlags ? &mClipRect : NULL); |
| 282 | |
Chris Craik | c1c5f08 | 2013-09-11 16:23:37 -0700 | [diff] [blame] | 283 | DrawOp* op = mOps[0].op; |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 284 | DisplayListLogBuffer& buffer = DisplayListLogBuffer::getInstance(); |
| 285 | buffer.writeCommand(0, "multiDraw"); |
| 286 | buffer.writeCommand(1, op->name()); |
Chris Craik | d965bc5 | 2013-09-16 14:47:13 -0700 | [diff] [blame] | 287 | |
| 288 | #if DEBUG_DISPLAY_LIST_OPS_AS_EVENTS |
| 289 | renderer.eventMark("multiDraw"); |
| 290 | renderer.eventMark(op->name()); |
| 291 | #endif |
Chris Craik | d4b43b3 | 2013-05-09 13:07:52 -0700 | [diff] [blame] | 292 | status_t status = op->multiDraw(renderer, dirty, mOps, mBounds); |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 293 | |
| 294 | #if DEBUG_MERGE_BEHAVIOR |
| 295 | renderer.drawScreenSpaceColorRect(mBounds.left, mBounds.top, mBounds.right, mBounds.bottom, |
| 296 | DEBUG_COLOR_MERGEDBATCH); |
| 297 | #endif |
| 298 | return status; |
| 299 | } |
Chris Craik | 28ce94a | 2013-05-31 11:38:03 -0700 | [diff] [blame] | 300 | |
| 301 | private: |
| 302 | /* |
| 303 | * Contains the effective clip rect shared by all merged ops. Initialized to the layer viewport, |
| 304 | * it will shrink if an op must be clipped on a certain side. The clipped sides are reflected in |
| 305 | * mClipSideFlags. |
| 306 | */ |
| 307 | Rect mClipRect; |
| 308 | int mClipSideFlags; |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 309 | }; |
| 310 | |
| 311 | class StateOpBatch : public Batch { |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 312 | public: |
| 313 | // creates a single operation batch |
Chris Craik | c1c5f08 | 2013-09-11 16:23:37 -0700 | [diff] [blame] | 314 | StateOpBatch(const StateOp* op, const DeferredDisplayState* state) : mOp(op), mState(state) {} |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 315 | |
John Reck | 8b59a52 | 2014-11-22 00:10:02 +0000 | [diff] [blame] | 316 | virtual status_t replay(OpenGLRenderer& renderer, Rect& dirty, int index) { |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 317 | DEFER_LOGD("replaying state op batch %p", this); |
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 | |
| 320 | // use invalid save count because it won't be used at flush time - RestoreToCountOp is the |
| 321 | // only one to use it, and we don't use that class at flush time, instead calling |
| 322 | // renderer.restoreToCount directly |
| 323 | int saveCount = -1; |
| 324 | mOp->applyState(renderer, saveCount); |
| 325 | return DrawGlInfo::kStatusDone; |
| 326 | } |
| 327 | |
| 328 | private: |
Chris Craik | 7273daa | 2013-03-28 11:25:24 -0700 | [diff] [blame] | 329 | const StateOp* mOp; |
Chris Craik | c1c5f08 | 2013-09-11 16:23:37 -0700 | [diff] [blame] | 330 | const DeferredDisplayState* mState; |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 331 | }; |
| 332 | |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 333 | class RestoreToCountBatch : public Batch { |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 334 | public: |
John Reck | 8b59a52 | 2014-11-22 00:10:02 +0000 | [diff] [blame] | 335 | RestoreToCountBatch(const StateOp* op, const DeferredDisplayState* state, int restoreCount) : |
| 336 | mState(state), mRestoreCount(restoreCount) {} |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 337 | |
John Reck | 8b59a52 | 2014-11-22 00:10:02 +0000 | [diff] [blame] | 338 | virtual status_t replay(OpenGLRenderer& renderer, Rect& dirty, int index) { |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 339 | DEFER_LOGD("batch %p restoring to count %d", this, mRestoreCount); |
Chris Craik | 7273daa | 2013-03-28 11:25:24 -0700 | [diff] [blame] | 340 | |
Chris Craik | c1c5f08 | 2013-09-11 16:23:37 -0700 | [diff] [blame] | 341 | renderer.restoreDisplayState(*mState); |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 342 | renderer.restoreToCount(mRestoreCount); |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 343 | return DrawGlInfo::kStatusDone; |
| 344 | } |
| 345 | |
| 346 | private: |
Chris Craik | 7273daa | 2013-03-28 11:25:24 -0700 | [diff] [blame] | 347 | // 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] | 348 | const DeferredDisplayState* mState; |
| 349 | |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 350 | /* |
| 351 | * The count used here represents the flush() time saveCount. This is as opposed to the |
| 352 | * DisplayList record time, or defer() time values (which are RestoreToCountOp's mCount, and |
| 353 | * (saveCount + mCount) respectively). Since the count is different from the original |
| 354 | * RestoreToCountOp, we don't store a pointer to the op, as elsewhere. |
| 355 | */ |
| 356 | const int mRestoreCount; |
| 357 | }; |
| 358 | |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 359 | #if DEBUG_MERGE_BEHAVIOR |
| 360 | class BarrierDebugBatch : public Batch { |
| 361 | virtual status_t replay(OpenGLRenderer& renderer, Rect& dirty, int index) { |
| 362 | renderer.drawScreenSpaceColorRect(0, 0, 10000, 10000, DEBUG_COLOR_BARRIER); |
| 363 | return DrawGlInfo::kStatusDrew; |
| 364 | } |
| 365 | }; |
| 366 | #endif |
| 367 | |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 368 | ///////////////////////////////////////////////////////////////////////////////// |
| 369 | // DeferredDisplayList |
| 370 | ///////////////////////////////////////////////////////////////////////////////// |
| 371 | |
| 372 | void DeferredDisplayList::resetBatchingState() { |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 373 | for (int i = 0; i < kOpBatch_Count; i++) { |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 374 | mBatchLookup[i] = NULL; |
| 375 | mMergingBatches[i].clear(); |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 376 | } |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 377 | #if DEBUG_MERGE_BEHAVIOR |
| 378 | if (mBatches.size() != 0) { |
| 379 | mBatches.add(new BarrierDebugBatch()); |
| 380 | } |
| 381 | #endif |
| 382 | mEarliestBatchIndex = mBatches.size(); |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | void DeferredDisplayList::clear() { |
| 386 | resetBatchingState(); |
| 387 | mComplexClipStackStart = -1; |
| 388 | |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 389 | for (unsigned int i = 0; i < mBatches.size(); i++) { |
| 390 | delete mBatches[i]; |
| 391 | } |
| 392 | mBatches.clear(); |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 393 | mSaveStack.clear(); |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 394 | mEarliestBatchIndex = 0; |
Chris Craik | 28ce94a | 2013-05-31 11:38:03 -0700 | [diff] [blame] | 395 | mEarliestUnclearedIndex = 0; |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 396 | } |
| 397 | |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 398 | ///////////////////////////////////////////////////////////////////////////////// |
| 399 | // Operation adding |
| 400 | ///////////////////////////////////////////////////////////////////////////////// |
| 401 | |
| 402 | int DeferredDisplayList::getStateOpDeferFlags() const { |
| 403 | // For both clipOp and save(Layer)Op, we don't want to save drawing info, and only want to save |
| 404 | // the clip if we aren't recording a complex clip (and can thus trust it to be a rect) |
| 405 | return recordingComplexClip() ? 0 : kStateDeferFlag_Clip; |
| 406 | } |
| 407 | |
| 408 | int DeferredDisplayList::getDrawOpDeferFlags() const { |
| 409 | return kStateDeferFlag_Draw | getStateOpDeferFlags(); |
| 410 | } |
| 411 | |
| 412 | /** |
| 413 | * When an clipping operation occurs that could cause a complex clip, record the operation and all |
| 414 | * subsequent clipOps, save/restores (if the clip flag is set). During a flush, instead of loading |
| 415 | * the clip from deferred state, we play back all of the relevant state operations that generated |
| 416 | * the complex clip. |
| 417 | * |
| 418 | * Note that we don't need to record the associated restore operation, since operations at defer |
| 419 | * time record whether they should store the renderer's current clip |
| 420 | */ |
| 421 | void DeferredDisplayList::addClip(OpenGLRenderer& renderer, ClipOp* op) { |
| 422 | if (recordingComplexClip() || op->canCauseComplexClip() || !renderer.hasRectToRectTransform()) { |
| 423 | DEFER_LOGD("%p Received complex clip operation %p", this, op); |
| 424 | |
| 425 | // NOTE: defer clip op before setting mComplexClipStackStart so previous clip is recorded |
| 426 | storeStateOpBarrier(renderer, op); |
| 427 | |
| 428 | if (!recordingComplexClip()) { |
| 429 | mComplexClipStackStart = renderer.getSaveCount() - 1; |
| 430 | DEFER_LOGD(" Starting complex clip region, start is %d", mComplexClipStackStart); |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 431 | } |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 432 | } |
| 433 | } |
| 434 | |
| 435 | /** |
| 436 | * For now, we record save layer operations as barriers in the batch list, preventing drawing |
| 437 | * operations from reordering around the saveLayer and it's associated restore() |
| 438 | * |
| 439 | * In the future, we should send saveLayer commands (if they can be played out of order) and their |
| 440 | * contained drawing operations to a seperate list of batches, so that they may draw at the |
| 441 | * beginning of the frame. This would avoid targetting and removing an FBO in the middle of a frame. |
| 442 | * |
| 443 | * saveLayer operations should be pulled to the beginning of the frame if the canvas doesn't have a |
| 444 | * complex clip, and if the flags (kClip_SaveFlag & kClipToLayer_SaveFlag) are set. |
| 445 | */ |
| 446 | void DeferredDisplayList::addSaveLayer(OpenGLRenderer& renderer, |
| 447 | SaveLayerOp* op, int newSaveCount) { |
| 448 | DEFER_LOGD("%p adding saveLayerOp %p, flags %x, new count %d", |
| 449 | this, op, op->getFlags(), newSaveCount); |
| 450 | |
| 451 | storeStateOpBarrier(renderer, op); |
| 452 | mSaveStack.push(newSaveCount); |
| 453 | } |
| 454 | |
| 455 | /** |
| 456 | * Takes save op and it's return value - the new save count - and stores it into the stream as a |
| 457 | * barrier if it's needed to properly modify a complex clip |
| 458 | */ |
| 459 | void DeferredDisplayList::addSave(OpenGLRenderer& renderer, SaveOp* op, int newSaveCount) { |
| 460 | int saveFlags = op->getFlags(); |
| 461 | DEFER_LOGD("%p adding saveOp %p, flags %x, new count %d", this, op, saveFlags, newSaveCount); |
| 462 | |
| 463 | if (recordingComplexClip() && (saveFlags & SkCanvas::kClip_SaveFlag)) { |
| 464 | // store and replay the save operation, as it may be needed to correctly playback the clip |
| 465 | DEFER_LOGD(" adding save barrier with new save count %d", newSaveCount); |
| 466 | storeStateOpBarrier(renderer, op); |
| 467 | mSaveStack.push(newSaveCount); |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | /** |
| 472 | * saveLayer() commands must be associated with a restoreToCount batch that will clean up and draw |
| 473 | * the layer in the deferred list |
| 474 | * |
| 475 | * other save() commands which occur as children of a snapshot with complex clip will be deferred, |
| 476 | * and must be restored |
| 477 | * |
| 478 | * Either will act as a barrier to draw operation reordering, as we want to play back layer |
| 479 | * save/restore and complex canvas modifications (including save/restore) in order. |
| 480 | */ |
Chris Craik | 7273daa | 2013-03-28 11:25:24 -0700 | [diff] [blame] | 481 | void DeferredDisplayList::addRestoreToCount(OpenGLRenderer& renderer, StateOp* op, |
| 482 | int newSaveCount) { |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 483 | DEFER_LOGD("%p addRestoreToCount %d", this, newSaveCount); |
| 484 | |
| 485 | if (recordingComplexClip() && newSaveCount <= mComplexClipStackStart) { |
| 486 | mComplexClipStackStart = -1; |
| 487 | resetBatchingState(); |
| 488 | } |
| 489 | |
| 490 | if (mSaveStack.isEmpty() || newSaveCount > mSaveStack.top()) { |
| 491 | return; |
| 492 | } |
| 493 | |
| 494 | while (!mSaveStack.isEmpty() && mSaveStack.top() >= newSaveCount) mSaveStack.pop(); |
| 495 | |
Chris Craik | 1ed30c9 | 2013-04-03 12:37:35 -0700 | [diff] [blame] | 496 | storeRestoreToCountBarrier(renderer, op, mSaveStack.size() + FLUSH_SAVE_STACK_DEPTH); |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | void DeferredDisplayList::addDrawOp(OpenGLRenderer& renderer, DrawOp* op) { |
Chris Craik | c1c5f08 | 2013-09-11 16:23:37 -0700 | [diff] [blame] | 500 | /* 1: op calculates local bounds */ |
| 501 | DeferredDisplayState* const state = createState(); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 502 | if (op->getLocalBounds(state->mBounds)) { |
Chris Craik | c1c5f08 | 2013-09-11 16:23:37 -0700 | [diff] [blame] | 503 | if (state->mBounds.isEmpty()) { |
| 504 | // valid empty bounds, don't bother deferring |
| 505 | tryRecycleState(state); |
| 506 | return; |
| 507 | } |
| 508 | } else { |
| 509 | state->mBounds.setEmpty(); |
| 510 | } |
| 511 | |
| 512 | /* 2: renderer calculates global bounds + stores state */ |
| 513 | if (renderer.storeDisplayState(*state, getDrawOpDeferFlags())) { |
| 514 | tryRecycleState(state); |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 515 | return; // quick rejected |
| 516 | } |
| 517 | |
Chris Craik | c1c5f08 | 2013-09-11 16:23:37 -0700 | [diff] [blame] | 518 | /* 3: ask op for defer info, given renderer state */ |
Chris Craik | 28ce94a | 2013-05-31 11:38:03 -0700 | [diff] [blame] | 519 | DeferInfo deferInfo; |
Chris Craik | c1c5f08 | 2013-09-11 16:23:37 -0700 | [diff] [blame] | 520 | op->onDefer(renderer, deferInfo, *state); |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 521 | |
| 522 | // complex clip has a complex set of expectations on the renderer state - for now, avoid taking |
| 523 | // the merge path in those cases |
Chris Craik | 28ce94a | 2013-05-31 11:38:03 -0700 | [diff] [blame] | 524 | deferInfo.mergeable &= !recordingComplexClip(); |
Chris Craik | 0e87f00 | 2013-06-19 16:54:59 -0700 | [diff] [blame] | 525 | deferInfo.opaqueOverBounds &= !recordingComplexClip() && mSaveStack.isEmpty(); |
Chris Craik | 28ce94a | 2013-05-31 11:38:03 -0700 | [diff] [blame] | 526 | |
| 527 | if (CC_LIKELY(mAvoidOverdraw) && mBatches.size() && |
Chris Craik | c1c5f08 | 2013-09-11 16:23:37 -0700 | [diff] [blame] | 528 | state->mClipSideFlags != kClipSide_ConservativeFull && |
| 529 | deferInfo.opaqueOverBounds && state->mBounds.contains(mBounds)) { |
Chris Craik | f70119c | 2013-06-13 11:21:22 -0700 | [diff] [blame] | 530 | // avoid overdraw by resetting drawing state + discarding drawing ops |
Chris Craik | 28ce94a | 2013-05-31 11:38:03 -0700 | [diff] [blame] | 531 | discardDrawingBatches(mBatches.size() - 1); |
Chris Craik | f70119c | 2013-06-13 11:21:22 -0700 | [diff] [blame] | 532 | resetBatchingState(); |
Chris Craik | 28ce94a | 2013-05-31 11:38:03 -0700 | [diff] [blame] | 533 | } |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 534 | |
| 535 | if (CC_UNLIKELY(renderer.getCaches().drawReorderDisabled)) { |
| 536 | // TODO: elegant way to reuse batches? |
Chris Craik | 28ce94a | 2013-05-31 11:38:03 -0700 | [diff] [blame] | 537 | DrawBatch* b = new DrawBatch(deferInfo); |
Chris Craik | c1c5f08 | 2013-09-11 16:23:37 -0700 | [diff] [blame] | 538 | b->add(op, state, deferInfo.opaqueOverBounds); |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 539 | mBatches.add(b); |
| 540 | return; |
| 541 | } |
| 542 | |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 543 | // find the latest batch of the new op's type, and try to merge the new op into it |
| 544 | DrawBatch* targetBatch = NULL; |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 545 | |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 546 | // insertion point of a new batch, will hopefully be immediately after similar batch |
| 547 | // (eventually, should be similar shader) |
| 548 | int insertBatchIndex = mBatches.size(); |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 549 | if (!mBatches.isEmpty()) { |
Chris Craik | c1c5f08 | 2013-09-11 16:23:37 -0700 | [diff] [blame] | 550 | if (state->mBounds.isEmpty()) { |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 551 | // 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] | 552 | DrawBatch* b = new DrawBatch(deferInfo); |
Chris Craik | c1c5f08 | 2013-09-11 16:23:37 -0700 | [diff] [blame] | 553 | b->add(op, state, deferInfo.opaqueOverBounds); |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 554 | mBatches.add(b); |
| 555 | resetBatchingState(); |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 556 | #if DEBUG_DEFER |
| 557 | DEFER_LOGD("Warning: Encountered op with empty bounds, resetting batches"); |
| 558 | op->output(2); |
| 559 | #endif |
| 560 | return; |
| 561 | } |
| 562 | |
Chris Craik | 28ce94a | 2013-05-31 11:38:03 -0700 | [diff] [blame] | 563 | if (deferInfo.mergeable) { |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 564 | // Try to merge with any existing batch with same mergeId. |
Chris Craik | 28ce94a | 2013-05-31 11:38:03 -0700 | [diff] [blame] | 565 | if (mMergingBatches[deferInfo.batchId].get(deferInfo.mergeId, targetBatch)) { |
Chris Craik | c1c5f08 | 2013-09-11 16:23:37 -0700 | [diff] [blame] | 566 | if (!((MergingDrawBatch*) targetBatch)->canMergeWith(op, state)) { |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 567 | targetBatch = NULL; |
| 568 | } |
| 569 | } |
| 570 | } else { |
| 571 | // join with similar, non-merging batch |
Chris Craik | 28ce94a | 2013-05-31 11:38:03 -0700 | [diff] [blame] | 572 | targetBatch = (DrawBatch*)mBatchLookup[deferInfo.batchId]; |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 573 | } |
| 574 | |
Chris Craik | 28ce94a | 2013-05-31 11:38:03 -0700 | [diff] [blame] | 575 | if (targetBatch || deferInfo.mergeable) { |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 576 | // 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] | 577 | // if no target, merging ops still interate to find similar batch to insert after |
| 578 | for (int i = mBatches.size() - 1; i >= mEarliestBatchIndex; i--) { |
| 579 | DrawBatch* overBatch = (DrawBatch*)mBatches[i]; |
| 580 | |
| 581 | if (overBatch == targetBatch) break; |
| 582 | |
| 583 | // TODO: also consider shader shared between batch types |
Chris Craik | 28ce94a | 2013-05-31 11:38:03 -0700 | [diff] [blame] | 584 | if (deferInfo.batchId == overBatch->getBatchId()) { |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 585 | insertBatchIndex = i + 1; |
| 586 | if (!targetBatch) break; // found insert position, quit |
| 587 | } |
| 588 | |
Chris Craik | c1c5f08 | 2013-09-11 16:23:37 -0700 | [diff] [blame] | 589 | if (overBatch->intersects(state->mBounds)) { |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 590 | // NOTE: it may be possible to optimize for special cases where two operations |
| 591 | // of the same batch/paint could swap order, such as with a non-mergeable |
| 592 | // (clipped) and a mergeable text operation |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 593 | targetBatch = NULL; |
| 594 | #if DEBUG_DEFER |
Chris Craik | c1c5f08 | 2013-09-11 16:23:37 -0700 | [diff] [blame] | 595 | DEFER_LOGD("op couldn't join batch %p, was intersected by batch %d", |
| 596 | targetBatch, i); |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 597 | op->output(2); |
| 598 | #endif |
| 599 | break; |
| 600 | } |
| 601 | } |
| 602 | } |
| 603 | } |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 604 | |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 605 | if (!targetBatch) { |
Chris Craik | 28ce94a | 2013-05-31 11:38:03 -0700 | [diff] [blame] | 606 | if (deferInfo.mergeable) { |
Chris Craik | 0e87f00 | 2013-06-19 16:54:59 -0700 | [diff] [blame] | 607 | targetBatch = new MergingDrawBatch(deferInfo, |
| 608 | renderer.getViewportWidth(), renderer.getViewportHeight()); |
Chris Craik | 28ce94a | 2013-05-31 11:38:03 -0700 | [diff] [blame] | 609 | mMergingBatches[deferInfo.batchId].put(deferInfo.mergeId, targetBatch); |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 610 | } else { |
Chris Craik | 28ce94a | 2013-05-31 11:38:03 -0700 | [diff] [blame] | 611 | targetBatch = new DrawBatch(deferInfo); |
| 612 | mBatchLookup[deferInfo.batchId] = targetBatch; |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 613 | } |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 614 | |
Chris Craik | f70119c | 2013-06-13 11:21:22 -0700 | [diff] [blame] | 615 | DEFER_LOGD("creating %singBatch %p, bid %x, at %d", |
| 616 | deferInfo.mergeable ? "Merg" : "Draw", |
| 617 | targetBatch, deferInfo.batchId, insertBatchIndex); |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 618 | mBatches.insertAt(targetBatch, insertBatchIndex); |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 619 | } |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 620 | |
Chris Craik | c1c5f08 | 2013-09-11 16:23:37 -0700 | [diff] [blame] | 621 | targetBatch->add(op, state, deferInfo.opaqueOverBounds); |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 622 | } |
| 623 | |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 624 | void DeferredDisplayList::storeStateOpBarrier(OpenGLRenderer& renderer, StateOp* op) { |
| 625 | DEFER_LOGD("%p adding state op barrier at pos %d", this, mBatches.size()); |
| 626 | |
Chris Craik | c1c5f08 | 2013-09-11 16:23:37 -0700 | [diff] [blame] | 627 | DeferredDisplayState* state = createState(); |
| 628 | renderer.storeDisplayState(*state, getStateOpDeferFlags()); |
| 629 | mBatches.add(new StateOpBatch(op, state)); |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 630 | resetBatchingState(); |
| 631 | } |
| 632 | |
Chris Craik | 7273daa | 2013-03-28 11:25:24 -0700 | [diff] [blame] | 633 | void DeferredDisplayList::storeRestoreToCountBarrier(OpenGLRenderer& renderer, StateOp* op, |
| 634 | int newSaveCount) { |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 635 | DEFER_LOGD("%p adding restore to count %d barrier, pos %d", |
| 636 | this, newSaveCount, mBatches.size()); |
| 637 | |
Chris Craik | 7273daa | 2013-03-28 11:25:24 -0700 | [diff] [blame] | 638 | // store displayState for the restore operation, as it may be associated with a saveLayer that |
| 639 | // doesn't have kClip_SaveFlag set |
Chris Craik | c1c5f08 | 2013-09-11 16:23:37 -0700 | [diff] [blame] | 640 | DeferredDisplayState* state = createState(); |
| 641 | renderer.storeDisplayState(*state, getStateOpDeferFlags()); |
| 642 | mBatches.add(new RestoreToCountBatch(op, state, newSaveCount)); |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 643 | resetBatchingState(); |
| 644 | } |
| 645 | |
| 646 | ///////////////////////////////////////////////////////////////////////////////// |
| 647 | // Replay / flush |
| 648 | ///////////////////////////////////////////////////////////////////////////////// |
| 649 | |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 650 | static status_t replayBatchList(const Vector<Batch*>& batchList, |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 651 | OpenGLRenderer& renderer, Rect& dirty) { |
| 652 | status_t status = DrawGlInfo::kStatusDone; |
| 653 | |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 654 | for (unsigned int i = 0; i < batchList.size(); i++) { |
Chris Craik | 28ce94a | 2013-05-31 11:38:03 -0700 | [diff] [blame] | 655 | if (batchList[i]) { |
| 656 | status |= batchList[i]->replay(renderer, dirty, i); |
| 657 | } |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 658 | } |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 659 | DEFER_LOGD("--flushed, drew %d batches", batchList.size()); |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 660 | return status; |
| 661 | } |
| 662 | |
| 663 | status_t DeferredDisplayList::flush(OpenGLRenderer& renderer, Rect& dirty) { |
| 664 | ATRACE_NAME("flush drawing commands"); |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 665 | Caches::getInstance().fontRenderer->endPrecaching(); |
| 666 | |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 667 | status_t status = DrawGlInfo::kStatusDone; |
| 668 | |
| 669 | if (isEmpty()) return status; // nothing to flush |
Chris Craik | a4e16c5 | 2013-03-22 10:00:48 -0700 | [diff] [blame] | 670 | renderer.restoreToCount(1); |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 671 | |
| 672 | DEFER_LOGD("--flushing"); |
Romain Guy | 0f66753 | 2013-03-01 14:31:04 -0800 | [diff] [blame] | 673 | renderer.eventMark("Flush"); |
| 674 | |
Chris Craik | a4e16c5 | 2013-03-22 10:00:48 -0700 | [diff] [blame] | 675 | // save and restore (with draw modifiers) so that reordering doesn't affect final state |
Chris Craik | d90144d | 2013-03-19 15:03:48 -0700 | [diff] [blame] | 676 | DrawModifiers restoreDrawModifiers = renderer.getDrawModifiers(); |
Chris Craik | a4e16c5 | 2013-03-22 10:00:48 -0700 | [diff] [blame] | 677 | renderer.save(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag); |
| 678 | |
Chris Craik | 28ce94a | 2013-05-31 11:38:03 -0700 | [diff] [blame] | 679 | if (CC_LIKELY(mAvoidOverdraw)) { |
| 680 | for (unsigned int i = 1; i < mBatches.size(); i++) { |
| 681 | if (mBatches[i] && mBatches[i]->coversBounds(mBounds)) { |
| 682 | discardDrawingBatches(i - 1); |
| 683 | } |
| 684 | } |
| 685 | } |
Chris Craik | 1ed30c9 | 2013-04-03 12:37:35 -0700 | [diff] [blame] | 686 | // NOTE: depth of the save stack at this point, before playback, should be reflected in |
| 687 | // FLUSH_SAVE_STACK_DEPTH, so that save/restores match up correctly |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 688 | status |= replayBatchList(mBatches, renderer, dirty); |
Chris Craik | a4e16c5 | 2013-03-22 10:00:48 -0700 | [diff] [blame] | 689 | |
| 690 | renderer.restoreToCount(1); |
Chris Craik | d90144d | 2013-03-19 15:03:48 -0700 | [diff] [blame] | 691 | renderer.setDrawModifiers(restoreDrawModifiers); |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 692 | |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 693 | DEFER_LOGD("--flush complete, returning %x", status); |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 694 | clear(); |
| 695 | return status; |
| 696 | } |
| 697 | |
Chris Craik | f70119c | 2013-06-13 11:21:22 -0700 | [diff] [blame] | 698 | void DeferredDisplayList::discardDrawingBatches(const unsigned int maxIndex) { |
Chris Craik | 28ce94a | 2013-05-31 11:38:03 -0700 | [diff] [blame] | 699 | for (unsigned int i = mEarliestUnclearedIndex; i <= maxIndex; i++) { |
Chris Craik | f70119c | 2013-06-13 11:21:22 -0700 | [diff] [blame] | 700 | // 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] | 701 | if (mBatches[i] && mBatches[i]->purelyDrawBatch()) { |
Chris Craik | 28ce94a | 2013-05-31 11:38:03 -0700 | [diff] [blame] | 702 | delete mBatches[i]; |
| 703 | mBatches.replaceAt(NULL, i); |
| 704 | } |
| 705 | } |
| 706 | mEarliestUnclearedIndex = maxIndex + 1; |
| 707 | } |
| 708 | |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 709 | }; // namespace uirenderer |
| 710 | }; // namespace android |