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