blob: a1825c5bc4c1a4ef9ebcb89b3f867c1ccfa63267 [file] [log] [blame]
Chris Craikc3566d02013-02-04 16:16:33 -08001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Romain Guyc46d07a2013-03-15 19:06:39 -070017#include <SkCanvas.h>
18
Chris Craikc3566d02013-02-04 16:16:33 -080019#include <utils/Trace.h>
Chris Craik28ce94a2013-05-31 11:38:03 -070020#include <ui/Rect.h>
21#include <ui/Region.h>
Chris Craikc3566d02013-02-04 16:16:33 -080022
Romain Guycf51a412013-04-08 19:40:31 -070023#include "Caches.h"
Chris Craikc3566d02013-02-04 16:16:33 -080024#include "Debug.h"
Chris Craik527a3aa2013-03-04 10:19:31 -080025#include "DeferredDisplayList.h"
Chris Craikc3566d02013-02-04 16:16:33 -080026#include "DisplayListOp.h"
27#include "OpenGLRenderer.h"
Chris Craik2507c342015-05-04 14:36:49 -070028#include "Properties.h"
Chris Craikdeeda3d2014-05-05 19:09:33 -070029#include "utils/MathUtils.h"
Chris Craikc3566d02013-02-04 16:16:33 -080030
31#if DEBUG_DEFER
32 #define DEFER_LOGD(...) ALOGD(__VA_ARGS__)
33#else
34 #define DEFER_LOGD(...)
35#endif
36
37namespace android {
38namespace uirenderer {
39
Chris Craik1ed30c92013-04-03 12:37:35 -070040// Depth of the save stack at the beginning of batch playback at flush time
41#define FLUSH_SAVE_STACK_DEPTH 2
42
Chris Craik527a3aa2013-03-04 10:19:31 -080043#define DEBUG_COLOR_BARRIER 0x1f000000
44#define DEBUG_COLOR_MERGEDBATCH 0x5f7f7fff
45#define DEBUG_COLOR_MERGEDBATCH_SOLO 0x5f7fff7f
46
Chris Craikb45c6aa2015-09-28 15:41:27 -070047static bool avoidOverdraw() {
48 // Don't avoid overdraw when visualizing it, since that makes it harder to
49 // debug where it's coming from, and when the problem occurs.
50 return !Properties::debugOverdraw;
51};
52
Chris Craikff785832013-03-08 13:12:16 -080053/////////////////////////////////////////////////////////////////////////////////
54// Operation Batches
55/////////////////////////////////////////////////////////////////////////////////
56
Chris Craik527a3aa2013-03-04 10:19:31 -080057class Batch {
Chris Craikc3566d02013-02-04 16:16:33 -080058public:
Tom Hudson107843d2014-09-08 11:26:26 -040059 virtual void replay(OpenGLRenderer& renderer, Rect& dirty, int index) = 0;
Chris Craik527a3aa2013-03-04 10:19:31 -080060 virtual ~Batch() {}
Chris Craik28ce94a2013-05-31 11:38:03 -070061 virtual bool purelyDrawBatch() { return false; }
Andreas Gampe64bb4132014-11-22 00:35:09 +000062 virtual bool coversBounds(const Rect& bounds) { return false; }
Chris Craik527a3aa2013-03-04 10:19:31 -080063};
Chris Craikc3566d02013-02-04 16:16:33 -080064
Chris Craik527a3aa2013-03-04 10:19:31 -080065class DrawBatch : public Batch {
66public:
Chris Craik28ce94a2013-05-31 11:38:03 -070067 DrawBatch(const DeferInfo& deferInfo) : mAllOpsOpaque(true),
68 mBatchId(deferInfo.batchId), mMergeId(deferInfo.mergeId) {
Chris Craik527a3aa2013-03-04 10:19:31 -080069 mOps.clear();
70 }
71
72 virtual ~DrawBatch() { mOps.clear(); }
Chris Craikc3566d02013-02-04 16:16:33 -080073
Chris Craikc1c5f082013-09-11 16:23:37 -070074 virtual void add(DrawOp* op, const DeferredDisplayState* state, bool opaqueOverBounds) {
Chris Craikc3566d02013-02-04 16:16:33 -080075 // NOTE: ignore empty bounds special case, since we don't merge across those ops
Chris Craikc1c5f082013-09-11 16:23:37 -070076 mBounds.unionWith(state->mBounds);
Chris Craik28ce94a2013-05-31 11:38:03 -070077 mAllOpsOpaque &= opaqueOverBounds;
John Reck272a6852015-07-29 16:48:58 -070078 mOps.push_back(OpStatePair(op, state));
Chris Craikc3566d02013-02-04 16:16:33 -080079 }
80
Chris Craikc1c5f082013-09-11 16:23:37 -070081 bool intersects(const Rect& rect) {
Chris Craikc3566d02013-02-04 16:16:33 -080082 if (!rect.intersects(mBounds)) return false;
Chris Craikff785832013-03-08 13:12:16 -080083
Chris Craikc3566d02013-02-04 16:16:33 -080084 for (unsigned int i = 0; i < mOps.size(); i++) {
Chris Craikc1c5f082013-09-11 16:23:37 -070085 if (rect.intersects(mOps[i].state->mBounds)) {
Chris Craikc3566d02013-02-04 16:16:33 -080086#if DEBUG_DEFER
Chris Craikc1c5f082013-09-11 16:23:37 -070087 DEFER_LOGD("op intersects with op %p with bounds %f %f %f %f:", mOps[i].op,
88 mOps[i].state->mBounds.left, mOps[i].state->mBounds.top,
89 mOps[i].state->mBounds.right, mOps[i].state->mBounds.bottom);
90 mOps[i].op->output(2);
Chris Craikc3566d02013-02-04 16:16:33 -080091#endif
92 return true;
93 }
94 }
95 return false;
96 }
97
Chris Craikd41c4d82015-01-05 15:51:13 -080098 virtual void replay(OpenGLRenderer& renderer, Rect& dirty, int index) override {
Chris Craik41541822013-05-03 16:35:54 -070099 DEFER_LOGD("%d replaying DrawBatch %p, with %d ops (batch id %x, merge id %p)",
100 index, this, mOps.size(), getBatchId(), getMergeId());
Chris Craikff785832013-03-08 13:12:16 -0800101
Chris Craikff785832013-03-08 13:12:16 -0800102 for (unsigned int i = 0; i < mOps.size(); i++) {
Chris Craikc1c5f082013-09-11 16:23:37 -0700103 DrawOp* op = mOps[i].op;
104 const DeferredDisplayState* state = mOps[i].state;
105 renderer.restoreDisplayState(*state);
Chris Craikff785832013-03-08 13:12:16 -0800106
107#if DEBUG_DISPLAY_LIST_OPS_AS_EVENTS
Chris Craikd90144d2013-03-19 15:03:48 -0700108 renderer.eventMark(op->name());
Chris Craikff785832013-03-08 13:12:16 -0800109#endif
Tom Hudson107843d2014-09-08 11:26:26 -0400110 op->applyDraw(renderer, dirty);
Chris Craik527a3aa2013-03-04 10:19:31 -0800111
112#if DEBUG_MERGE_BEHAVIOR
Chris Craikc1c5f082013-09-11 16:23:37 -0700113 const Rect& bounds = state->mBounds;
Chris Craik527a3aa2013-03-04 10:19:31 -0800114 int batchColor = 0x1f000000;
115 if (getBatchId() & 0x1) batchColor |= 0x0000ff;
116 if (getBatchId() & 0x2) batchColor |= 0x00ff00;
117 if (getBatchId() & 0x4) batchColor |= 0xff0000;
118 renderer.drawScreenSpaceColorRect(bounds.left, bounds.top, bounds.right, bounds.bottom,
119 batchColor);
120#endif
Chris Craikff785832013-03-08 13:12:16 -0800121 }
Chris Craikff785832013-03-08 13:12:16 -0800122 }
123
Chris Craikd41c4d82015-01-05 15:51:13 -0800124 virtual bool purelyDrawBatch() override { return true; }
Chris Craik28ce94a2013-05-31 11:38:03 -0700125
Chris Craikd41c4d82015-01-05 15:51:13 -0800126 virtual bool coversBounds(const Rect& bounds) override {
Chris Craik28ce94a2013-05-31 11:38:03 -0700127 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 Craikc1c5f082013-09-11 16:23:37 -0700131 const Rect &r = mOps[i].state->mBounds;
Chris Craik28ce94a2013-05-31 11:38:03 -0700132 uncovered.subtractSelf(android::Rect(r.left, r.top, r.right, r.bottom));
133 }
134 return uncovered.isEmpty();
135 }
136
Chris Craik527a3aa2013-03-04 10:19:31 -0800137 inline int getBatchId() const { return mBatchId; }
138 inline mergeid_t getMergeId() const { return mMergeId; }
Chris Craikff785832013-03-08 13:12:16 -0800139 inline int count() const { return mOps.size(); }
Chris Craik527a3aa2013-03-04 10:19:31 -0800140
141protected:
John Reck272a6852015-07-29 16:48:58 -0700142 std::vector<OpStatePair> mOps;
Chris Craik28ce94a2013-05-31 11:38:03 -0700143 Rect mBounds; // union of bounds of contained ops
Chris Craik527a3aa2013-03-04 10:19:31 -0800144private:
Chris Craik28ce94a2013-05-31 11:38:03 -0700145 bool mAllOpsOpaque;
Chris Craik527a3aa2013-03-04 10:19:31 -0800146 int mBatchId;
147 mergeid_t mMergeId;
Chris Craikc3566d02013-02-04 16:16:33 -0800148};
149
Chris Craik527a3aa2013-03-04 10:19:31 -0800150class MergingDrawBatch : public DrawBatch {
151public:
Chris Craik0e87f002013-06-19 16:54:59 -0700152 MergingDrawBatch(DeferInfo& deferInfo, int width, int height) :
153 DrawBatch(deferInfo), mClipRect(width, height),
154 mClipSideFlags(kClipSide_None) {}
Chris Craika02c4ed2013-06-14 13:43:58 -0700155
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 Craik527a3aa2013-03-04 10:19:31 -0800173
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 Craika02c4ed2013-06-14 13:43:58 -0700179 * op tries to merge at all, and b) if the op can merge with another set of ops
Chris Craik527a3aa2013-03-04 10:19:31 -0800180 *
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 Craikc1c5f082013-09-11 16:23:37 -0700184 bool canMergeWith(const DrawOp* op, const DeferredDisplayState* state) {
Chris Craik527a3aa2013-03-04 10:19:31 -0800185 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 Sollenbergerc29a0a42014-03-31 13:52:39 -0400190 if (!isTextBatch || op->hasTextShadow()) {
Chris Craikc1c5f082013-09-11 16:23:37 -0700191 if (intersects(state->mBounds)) return false;
Chris Craik527a3aa2013-03-04 10:19:31 -0800192 }
Chris Craikc1c5f082013-09-11 16:23:37 -0700193 const DeferredDisplayState* lhs = state;
194 const DeferredDisplayState* rhs = mOps[0].state;
Chris Craik527a3aa2013-03-04 10:19:31 -0800195
Chris Craikdeeda3d2014-05-05 19:09:33 -0700196 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 Craikfca52b752015-04-28 11:45:59 -0700201 if (lhs->mProjectionPathMask != rhs->mProjectionPathMask) return false;
Chris Craik527a3aa2013-03-04 10:19:31 -0800202
Chris Craika02c4ed2013-06-14 13:43:58 -0700203 /* Clipping compatibility check
204 *
205 * Exploits the fact that if a op or batch is clipped on a side, its bounds will equal its
206 * clip for that side.
207 */
208 const int currentFlags = mClipSideFlags;
Chris Craikc1c5f082013-09-11 16:23:37 -0700209 const int newFlags = state->mClipSideFlags;
Chris Craika02c4ed2013-06-14 13:43:58 -0700210 if (currentFlags != kClipSide_None || newFlags != kClipSide_None) {
Chris Craikc1c5f082013-09-11 16:23:37 -0700211 const Rect& opBounds = state->mBounds;
Chris Craika02c4ed2013-06-14 13:43:58 -0700212 float boundsDelta = mBounds.left - opBounds.left;
213 if (!checkSide(currentFlags, newFlags, kClipSide_Left, boundsDelta)) return false;
214 boundsDelta = mBounds.top - opBounds.top;
215 if (!checkSide(currentFlags, newFlags, kClipSide_Top, boundsDelta)) return false;
216
217 // right and bottom delta calculation reversed to account for direction
218 boundsDelta = opBounds.right - mBounds.right;
219 if (!checkSide(currentFlags, newFlags, kClipSide_Right, boundsDelta)) return false;
220 boundsDelta = opBounds.bottom - mBounds.bottom;
221 if (!checkSide(currentFlags, newFlags, kClipSide_Bottom, boundsDelta)) return false;
Chris Craik28ce94a2013-05-31 11:38:03 -0700222 }
Chris Craik28ce94a2013-05-31 11:38:03 -0700223
Chris Craik527a3aa2013-03-04 10:19:31 -0800224 // if paints are equal, then modifiers + paint attribs don't need to be compared
Chris Craikc1c5f082013-09-11 16:23:37 -0700225 if (op->mPaint == mOps[0].op->mPaint) return true;
Chris Craik527a3aa2013-03-04 10:19:31 -0800226
Chris Craikbf6f0f22015-10-01 12:36:07 -0700227 if (PaintUtils::getAlphaDirect(op->mPaint)
228 != PaintUtils::getAlphaDirect(mOps[0].op->mPaint)) {
229 return false;
230 }
Chris Craik527a3aa2013-03-04 10:19:31 -0800231
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500232 if (op->mPaint && mOps[0].op->mPaint &&
233 op->mPaint->getColorFilter() != mOps[0].op->mPaint->getColorFilter()) {
234 return false;
235 }
236
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400237 if (op->mPaint && mOps[0].op->mPaint &&
238 op->mPaint->getShader() != mOps[0].op->mPaint->getShader()) {
239 return false;
240 }
241
Chris Craik527a3aa2013-03-04 10:19:31 -0800242 return true;
243 }
244
Chris Craikd41c4d82015-01-05 15:51:13 -0800245 virtual void add(DrawOp* op, const DeferredDisplayState* state,
246 bool opaqueOverBounds) override {
Chris Craikc1c5f082013-09-11 16:23:37 -0700247 DrawBatch::add(op, state, opaqueOverBounds);
Chris Craik28ce94a2013-05-31 11:38:03 -0700248
Chris Craikc1c5f082013-09-11 16:23:37 -0700249 const int newClipSideFlags = state->mClipSideFlags;
Chris Craik28ce94a2013-05-31 11:38:03 -0700250 mClipSideFlags |= newClipSideFlags;
Chris Craikc1c5f082013-09-11 16:23:37 -0700251 if (newClipSideFlags & kClipSide_Left) mClipRect.left = state->mClip.left;
252 if (newClipSideFlags & kClipSide_Top) mClipRect.top = state->mClip.top;
253 if (newClipSideFlags & kClipSide_Right) mClipRect.right = state->mClip.right;
254 if (newClipSideFlags & kClipSide_Bottom) mClipRect.bottom = state->mClip.bottom;
Chris Craik28ce94a2013-05-31 11:38:03 -0700255 }
256
Chris Craikd41c4d82015-01-05 15:51:13 -0800257 virtual void replay(OpenGLRenderer& renderer, Rect& dirty, int index) override {
Chris Craik28ce94a2013-05-31 11:38:03 -0700258 DEFER_LOGD("%d replaying MergingDrawBatch %p, with %d ops,"
259 " clip flags %x (batch id %x, merge id %p)",
260 index, this, mOps.size(), mClipSideFlags, getBatchId(), getMergeId());
Chris Craik527a3aa2013-03-04 10:19:31 -0800261 if (mOps.size() == 1) {
Tom Hudson107843d2014-09-08 11:26:26 -0400262 DrawBatch::replay(renderer, dirty, -1);
263 return;
Chris Craik527a3aa2013-03-04 10:19:31 -0800264 }
265
Chris Craik28ce94a2013-05-31 11:38:03 -0700266 // clipping in the merged case is done ahead of time since all ops share the clip (if any)
Chris Craikd41c4d82015-01-05 15:51:13 -0800267 renderer.setupMergedMultiDraw(mClipSideFlags ? &mClipRect : nullptr);
Chris Craik28ce94a2013-05-31 11:38:03 -0700268
Chris Craikc1c5f082013-09-11 16:23:37 -0700269 DrawOp* op = mOps[0].op;
Chris Craikd965bc52013-09-16 14:47:13 -0700270#if DEBUG_DISPLAY_LIST_OPS_AS_EVENTS
271 renderer.eventMark("multiDraw");
272 renderer.eventMark(op->name());
273#endif
Tom Hudson107843d2014-09-08 11:26:26 -0400274 op->multiDraw(renderer, dirty, mOps, mBounds);
Chris Craik527a3aa2013-03-04 10:19:31 -0800275
276#if DEBUG_MERGE_BEHAVIOR
277 renderer.drawScreenSpaceColorRect(mBounds.left, mBounds.top, mBounds.right, mBounds.bottom,
278 DEBUG_COLOR_MERGEDBATCH);
279#endif
Chris Craik527a3aa2013-03-04 10:19:31 -0800280 }
Chris Craik28ce94a2013-05-31 11:38:03 -0700281
282private:
283 /*
284 * Contains the effective clip rect shared by all merged ops. Initialized to the layer viewport,
285 * it will shrink if an op must be clipped on a certain side. The clipped sides are reflected in
286 * mClipSideFlags.
287 */
288 Rect mClipRect;
289 int mClipSideFlags;
Chris Craik527a3aa2013-03-04 10:19:31 -0800290};
291
292class StateOpBatch : public Batch {
Chris Craikff785832013-03-08 13:12:16 -0800293public:
294 // creates a single operation batch
Chris Craikc1c5f082013-09-11 16:23:37 -0700295 StateOpBatch(const StateOp* op, const DeferredDisplayState* state) : mOp(op), mState(state) {}
Chris Craikff785832013-03-08 13:12:16 -0800296
Chris Craikd41c4d82015-01-05 15:51:13 -0800297 virtual void replay(OpenGLRenderer& renderer, Rect& dirty, int index) override {
Chris Craikff785832013-03-08 13:12:16 -0800298 DEFER_LOGD("replaying state op batch %p", this);
Chris Craikc1c5f082013-09-11 16:23:37 -0700299 renderer.restoreDisplayState(*mState);
Chris Craikff785832013-03-08 13:12:16 -0800300
301 // use invalid save count because it won't be used at flush time - RestoreToCountOp is the
302 // only one to use it, and we don't use that class at flush time, instead calling
303 // renderer.restoreToCount directly
304 int saveCount = -1;
305 mOp->applyState(renderer, saveCount);
Chris Craikff785832013-03-08 13:12:16 -0800306 }
307
308private:
Chris Craik7273daa2013-03-28 11:25:24 -0700309 const StateOp* mOp;
Chris Craikc1c5f082013-09-11 16:23:37 -0700310 const DeferredDisplayState* mState;
Chris Craikff785832013-03-08 13:12:16 -0800311};
312
Chris Craik527a3aa2013-03-04 10:19:31 -0800313class RestoreToCountBatch : public Batch {
Chris Craikff785832013-03-08 13:12:16 -0800314public:
Andreas Gampe64bb4132014-11-22 00:35:09 +0000315 RestoreToCountBatch(const StateOp* op, const DeferredDisplayState* state, int restoreCount) :
316 mState(state), mRestoreCount(restoreCount) {}
Chris Craikff785832013-03-08 13:12:16 -0800317
Chris Craikd41c4d82015-01-05 15:51:13 -0800318 virtual void replay(OpenGLRenderer& renderer, Rect& dirty, int index) override {
Chris Craikff785832013-03-08 13:12:16 -0800319 DEFER_LOGD("batch %p restoring to count %d", this, mRestoreCount);
Chris Craik7273daa2013-03-28 11:25:24 -0700320
Chris Craikc1c5f082013-09-11 16:23:37 -0700321 renderer.restoreDisplayState(*mState);
Chris Craikff785832013-03-08 13:12:16 -0800322 renderer.restoreToCount(mRestoreCount);
Chris Craikff785832013-03-08 13:12:16 -0800323 }
324
325private:
Chris Craik7273daa2013-03-28 11:25:24 -0700326 // we use the state storage for the RestoreToCountOp, but don't replay the op itself
Chris Craikc1c5f082013-09-11 16:23:37 -0700327 const DeferredDisplayState* mState;
328
Chris Craikff785832013-03-08 13:12:16 -0800329 /*
330 * The count used here represents the flush() time saveCount. This is as opposed to the
331 * DisplayList record time, or defer() time values (which are RestoreToCountOp's mCount, and
332 * (saveCount + mCount) respectively). Since the count is different from the original
333 * RestoreToCountOp, we don't store a pointer to the op, as elsewhere.
334 */
335 const int mRestoreCount;
336};
337
Chris Craik527a3aa2013-03-04 10:19:31 -0800338#if DEBUG_MERGE_BEHAVIOR
339class BarrierDebugBatch : public Batch {
Tom Hudson107843d2014-09-08 11:26:26 -0400340 virtual void replay(OpenGLRenderer& renderer, Rect& dirty, int index) {
Chris Craik527a3aa2013-03-04 10:19:31 -0800341 renderer.drawScreenSpaceColorRect(0, 0, 10000, 10000, DEBUG_COLOR_BARRIER);
Chris Craik527a3aa2013-03-04 10:19:31 -0800342 }
343};
344#endif
345
Chris Craikff785832013-03-08 13:12:16 -0800346/////////////////////////////////////////////////////////////////////////////////
347// DeferredDisplayList
348/////////////////////////////////////////////////////////////////////////////////
349
350void DeferredDisplayList::resetBatchingState() {
Chris Craikc3566d02013-02-04 16:16:33 -0800351 for (int i = 0; i < kOpBatch_Count; i++) {
Chris Craikd41c4d82015-01-05 15:51:13 -0800352 mBatchLookup[i] = nullptr;
Chris Craik527a3aa2013-03-04 10:19:31 -0800353 mMergingBatches[i].clear();
Chris Craikc3566d02013-02-04 16:16:33 -0800354 }
Chris Craik527a3aa2013-03-04 10:19:31 -0800355#if DEBUG_MERGE_BEHAVIOR
356 if (mBatches.size() != 0) {
357 mBatches.add(new BarrierDebugBatch());
358 }
359#endif
360 mEarliestBatchIndex = mBatches.size();
Chris Craikff785832013-03-08 13:12:16 -0800361}
362
363void DeferredDisplayList::clear() {
364 resetBatchingState();
365 mComplexClipStackStart = -1;
366
Chris Craikc3566d02013-02-04 16:16:33 -0800367 for (unsigned int i = 0; i < mBatches.size(); i++) {
368 delete mBatches[i];
369 }
370 mBatches.clear();
Chris Craikff785832013-03-08 13:12:16 -0800371 mSaveStack.clear();
Chris Craik527a3aa2013-03-04 10:19:31 -0800372 mEarliestBatchIndex = 0;
Chris Craik28ce94a2013-05-31 11:38:03 -0700373 mEarliestUnclearedIndex = 0;
Chris Craikc3566d02013-02-04 16:16:33 -0800374}
375
Chris Craikff785832013-03-08 13:12:16 -0800376/////////////////////////////////////////////////////////////////////////////////
377// Operation adding
378/////////////////////////////////////////////////////////////////////////////////
379
380int DeferredDisplayList::getStateOpDeferFlags() const {
381 // For both clipOp and save(Layer)Op, we don't want to save drawing info, and only want to save
382 // the clip if we aren't recording a complex clip (and can thus trust it to be a rect)
383 return recordingComplexClip() ? 0 : kStateDeferFlag_Clip;
384}
385
386int DeferredDisplayList::getDrawOpDeferFlags() const {
387 return kStateDeferFlag_Draw | getStateOpDeferFlags();
388}
389
390/**
391 * When an clipping operation occurs that could cause a complex clip, record the operation and all
392 * subsequent clipOps, save/restores (if the clip flag is set). During a flush, instead of loading
393 * the clip from deferred state, we play back all of the relevant state operations that generated
394 * the complex clip.
395 *
396 * Note that we don't need to record the associated restore operation, since operations at defer
397 * time record whether they should store the renderer's current clip
398 */
399void DeferredDisplayList::addClip(OpenGLRenderer& renderer, ClipOp* op) {
400 if (recordingComplexClip() || op->canCauseComplexClip() || !renderer.hasRectToRectTransform()) {
401 DEFER_LOGD("%p Received complex clip operation %p", this, op);
402
403 // NOTE: defer clip op before setting mComplexClipStackStart so previous clip is recorded
404 storeStateOpBarrier(renderer, op);
405
406 if (!recordingComplexClip()) {
407 mComplexClipStackStart = renderer.getSaveCount() - 1;
408 DEFER_LOGD(" Starting complex clip region, start is %d", mComplexClipStackStart);
Chris Craikc3566d02013-02-04 16:16:33 -0800409 }
Chris Craikff785832013-03-08 13:12:16 -0800410 }
411}
412
413/**
414 * For now, we record save layer operations as barriers in the batch list, preventing drawing
415 * operations from reordering around the saveLayer and it's associated restore()
416 *
417 * In the future, we should send saveLayer commands (if they can be played out of order) and their
418 * contained drawing operations to a seperate list of batches, so that they may draw at the
419 * beginning of the frame. This would avoid targetting and removing an FBO in the middle of a frame.
420 *
421 * saveLayer operations should be pulled to the beginning of the frame if the canvas doesn't have a
422 * complex clip, and if the flags (kClip_SaveFlag & kClipToLayer_SaveFlag) are set.
423 */
424void DeferredDisplayList::addSaveLayer(OpenGLRenderer& renderer,
425 SaveLayerOp* op, int newSaveCount) {
426 DEFER_LOGD("%p adding saveLayerOp %p, flags %x, new count %d",
427 this, op, op->getFlags(), newSaveCount);
428
429 storeStateOpBarrier(renderer, op);
John Reck272a6852015-07-29 16:48:58 -0700430 mSaveStack.push_back(newSaveCount);
Chris Craikff785832013-03-08 13:12:16 -0800431}
432
433/**
434 * Takes save op and it's return value - the new save count - and stores it into the stream as a
435 * barrier if it's needed to properly modify a complex clip
436 */
437void DeferredDisplayList::addSave(OpenGLRenderer& renderer, SaveOp* op, int newSaveCount) {
438 int saveFlags = op->getFlags();
439 DEFER_LOGD("%p adding saveOp %p, flags %x, new count %d", this, op, saveFlags, newSaveCount);
440
441 if (recordingComplexClip() && (saveFlags & SkCanvas::kClip_SaveFlag)) {
442 // store and replay the save operation, as it may be needed to correctly playback the clip
443 DEFER_LOGD(" adding save barrier with new save count %d", newSaveCount);
444 storeStateOpBarrier(renderer, op);
John Reck272a6852015-07-29 16:48:58 -0700445 mSaveStack.push_back(newSaveCount);
Chris Craikff785832013-03-08 13:12:16 -0800446 }
447}
448
449/**
450 * saveLayer() commands must be associated with a restoreToCount batch that will clean up and draw
451 * the layer in the deferred list
452 *
453 * other save() commands which occur as children of a snapshot with complex clip will be deferred,
454 * and must be restored
455 *
456 * Either will act as a barrier to draw operation reordering, as we want to play back layer
457 * save/restore and complex canvas modifications (including save/restore) in order.
458 */
Chris Craik7273daa2013-03-28 11:25:24 -0700459void DeferredDisplayList::addRestoreToCount(OpenGLRenderer& renderer, StateOp* op,
460 int newSaveCount) {
Chris Craikff785832013-03-08 13:12:16 -0800461 DEFER_LOGD("%p addRestoreToCount %d", this, newSaveCount);
462
463 if (recordingComplexClip() && newSaveCount <= mComplexClipStackStart) {
464 mComplexClipStackStart = -1;
465 resetBatchingState();
466 }
467
John Reck272a6852015-07-29 16:48:58 -0700468 if (mSaveStack.empty() || newSaveCount > mSaveStack.back()) {
Chris Craikff785832013-03-08 13:12:16 -0800469 return;
470 }
471
John Reck272a6852015-07-29 16:48:58 -0700472 while (!mSaveStack.empty() && mSaveStack.back() >= newSaveCount) mSaveStack.pop_back();
Chris Craikff785832013-03-08 13:12:16 -0800473
Chris Craik1ed30c92013-04-03 12:37:35 -0700474 storeRestoreToCountBarrier(renderer, op, mSaveStack.size() + FLUSH_SAVE_STACK_DEPTH);
Chris Craikff785832013-03-08 13:12:16 -0800475}
476
477void DeferredDisplayList::addDrawOp(OpenGLRenderer& renderer, DrawOp* op) {
Chris Craikc1c5f082013-09-11 16:23:37 -0700478 /* 1: op calculates local bounds */
479 DeferredDisplayState* const state = createState();
John Reck3b202512014-06-23 13:13:08 -0700480 if (op->getLocalBounds(state->mBounds)) {
Chris Craikc1c5f082013-09-11 16:23:37 -0700481 if (state->mBounds.isEmpty()) {
482 // valid empty bounds, don't bother deferring
483 tryRecycleState(state);
484 return;
485 }
486 } else {
487 state->mBounds.setEmpty();
488 }
489
490 /* 2: renderer calculates global bounds + stores state */
491 if (renderer.storeDisplayState(*state, getDrawOpDeferFlags())) {
492 tryRecycleState(state);
Chris Craikff785832013-03-08 13:12:16 -0800493 return; // quick rejected
494 }
495
Chris Craikc1c5f082013-09-11 16:23:37 -0700496 /* 3: ask op for defer info, given renderer state */
Chris Craik28ce94a2013-05-31 11:38:03 -0700497 DeferInfo deferInfo;
Chris Craikc1c5f082013-09-11 16:23:37 -0700498 op->onDefer(renderer, deferInfo, *state);
Chris Craik527a3aa2013-03-04 10:19:31 -0800499
500 // complex clip has a complex set of expectations on the renderer state - for now, avoid taking
501 // the merge path in those cases
Chris Craik28ce94a2013-05-31 11:38:03 -0700502 deferInfo.mergeable &= !recordingComplexClip();
Chris Craikb1f990d2015-06-12 11:28:52 -0700503 deferInfo.opaqueOverBounds &= !recordingComplexClip()
John Reck272a6852015-07-29 16:48:58 -0700504 && mSaveStack.empty()
Chris Craikb1f990d2015-06-12 11:28:52 -0700505 && !state->mRoundRectClipState;
Chris Craik28ce94a2013-05-31 11:38:03 -0700506
Chris Craikb45c6aa2015-09-28 15:41:27 -0700507 if (CC_LIKELY(avoidOverdraw()) && mBatches.size() &&
Chris Craikc1c5f082013-09-11 16:23:37 -0700508 state->mClipSideFlags != kClipSide_ConservativeFull &&
509 deferInfo.opaqueOverBounds && state->mBounds.contains(mBounds)) {
Chris Craikf70119c2013-06-13 11:21:22 -0700510 // avoid overdraw by resetting drawing state + discarding drawing ops
Chris Craik28ce94a2013-05-31 11:38:03 -0700511 discardDrawingBatches(mBatches.size() - 1);
Chris Craikf70119c2013-06-13 11:21:22 -0700512 resetBatchingState();
Chris Craik28ce94a2013-05-31 11:38:03 -0700513 }
Chris Craikff785832013-03-08 13:12:16 -0800514
Chris Craik2507c342015-05-04 14:36:49 -0700515 if (CC_UNLIKELY(Properties::drawReorderDisabled)) {
Chris Craikff785832013-03-08 13:12:16 -0800516 // TODO: elegant way to reuse batches?
Chris Craik28ce94a2013-05-31 11:38:03 -0700517 DrawBatch* b = new DrawBatch(deferInfo);
Chris Craikc1c5f082013-09-11 16:23:37 -0700518 b->add(op, state, deferInfo.opaqueOverBounds);
John Reck272a6852015-07-29 16:48:58 -0700519 mBatches.push_back(b);
Chris Craikc3566d02013-02-04 16:16:33 -0800520 return;
521 }
522
Chris Craik527a3aa2013-03-04 10:19:31 -0800523 // find the latest batch of the new op's type, and try to merge the new op into it
Chris Craikd41c4d82015-01-05 15:51:13 -0800524 DrawBatch* targetBatch = nullptr;
Chris Craikc3566d02013-02-04 16:16:33 -0800525
Chris Craik527a3aa2013-03-04 10:19:31 -0800526 // insertion point of a new batch, will hopefully be immediately after similar batch
527 // (eventually, should be similar shader)
528 int insertBatchIndex = mBatches.size();
John Reck272a6852015-07-29 16:48:58 -0700529 if (!mBatches.empty()) {
Chris Craikc1c5f082013-09-11 16:23:37 -0700530 if (state->mBounds.isEmpty()) {
Chris Craikb565df12015-10-05 13:00:52 -0700531 // don't know the bounds for op, so create new batch and start from scratch on next op
Chris Craik28ce94a2013-05-31 11:38:03 -0700532 DrawBatch* b = new DrawBatch(deferInfo);
Chris Craikc1c5f082013-09-11 16:23:37 -0700533 b->add(op, state, deferInfo.opaqueOverBounds);
John Reck272a6852015-07-29 16:48:58 -0700534 mBatches.push_back(b);
Chris Craik527a3aa2013-03-04 10:19:31 -0800535 resetBatchingState();
Chris Craikc3566d02013-02-04 16:16:33 -0800536#if DEBUG_DEFER
537 DEFER_LOGD("Warning: Encountered op with empty bounds, resetting batches");
538 op->output(2);
539#endif
540 return;
541 }
542
Chris Craik28ce94a2013-05-31 11:38:03 -0700543 if (deferInfo.mergeable) {
Chris Craik527a3aa2013-03-04 10:19:31 -0800544 // Try to merge with any existing batch with same mergeId.
Sene Gales1673035f2015-09-30 14:41:29 +0100545 std::unordered_map<mergeid_t, DrawBatch*>& mergingBatch
546 = mMergingBatches[deferInfo.batchId];
547 auto getResult = mergingBatch.find(deferInfo.mergeId);
548 if (getResult != mergingBatch.end()) {
549 targetBatch = getResult->second;
Chris Craikc1c5f082013-09-11 16:23:37 -0700550 if (!((MergingDrawBatch*) targetBatch)->canMergeWith(op, state)) {
Chris Craikd41c4d82015-01-05 15:51:13 -0800551 targetBatch = nullptr;
Chris Craik527a3aa2013-03-04 10:19:31 -0800552 }
553 }
554 } else {
555 // join with similar, non-merging batch
Chris Craik28ce94a2013-05-31 11:38:03 -0700556 targetBatch = (DrawBatch*)mBatchLookup[deferInfo.batchId];
Chris Craik527a3aa2013-03-04 10:19:31 -0800557 }
558
Chris Craik28ce94a2013-05-31 11:38:03 -0700559 if (targetBatch || deferInfo.mergeable) {
Chris Craikc3566d02013-02-04 16:16:33 -0800560 // iterate back toward target to see if anything drawn since should overlap the new op
Chris Craik527a3aa2013-03-04 10:19:31 -0800561 // if no target, merging ops still interate to find similar batch to insert after
562 for (int i = mBatches.size() - 1; i >= mEarliestBatchIndex; i--) {
563 DrawBatch* overBatch = (DrawBatch*)mBatches[i];
564
565 if (overBatch == targetBatch) break;
566
567 // TODO: also consider shader shared between batch types
Chris Craik28ce94a2013-05-31 11:38:03 -0700568 if (deferInfo.batchId == overBatch->getBatchId()) {
Chris Craik527a3aa2013-03-04 10:19:31 -0800569 insertBatchIndex = i + 1;
570 if (!targetBatch) break; // found insert position, quit
571 }
572
Chris Craikc1c5f082013-09-11 16:23:37 -0700573 if (overBatch->intersects(state->mBounds)) {
Chris Craik527a3aa2013-03-04 10:19:31 -0800574 // NOTE: it may be possible to optimize for special cases where two operations
575 // of the same batch/paint could swap order, such as with a non-mergeable
576 // (clipped) and a mergeable text operation
Chris Craikd41c4d82015-01-05 15:51:13 -0800577 targetBatch = nullptr;
Chris Craikc3566d02013-02-04 16:16:33 -0800578#if DEBUG_DEFER
Chris Craikc1c5f082013-09-11 16:23:37 -0700579 DEFER_LOGD("op couldn't join batch %p, was intersected by batch %d",
580 targetBatch, i);
Chris Craikc3566d02013-02-04 16:16:33 -0800581 op->output(2);
582#endif
583 break;
584 }
585 }
586 }
587 }
Chris Craik527a3aa2013-03-04 10:19:31 -0800588
Chris Craikc3566d02013-02-04 16:16:33 -0800589 if (!targetBatch) {
Chris Craik28ce94a2013-05-31 11:38:03 -0700590 if (deferInfo.mergeable) {
Chris Craik0e87f002013-06-19 16:54:59 -0700591 targetBatch = new MergingDrawBatch(deferInfo,
592 renderer.getViewportWidth(), renderer.getViewportHeight());
Sene Gales1673035f2015-09-30 14:41:29 +0100593 mMergingBatches[deferInfo.batchId].insert(
594 std::make_pair(deferInfo.mergeId, targetBatch));
Chris Craik527a3aa2013-03-04 10:19:31 -0800595 } else {
Chris Craik28ce94a2013-05-31 11:38:03 -0700596 targetBatch = new DrawBatch(deferInfo);
597 mBatchLookup[deferInfo.batchId] = targetBatch;
Chris Craikc3566d02013-02-04 16:16:33 -0800598 }
Chris Craik527a3aa2013-03-04 10:19:31 -0800599
Chris Craikf70119c2013-06-13 11:21:22 -0700600 DEFER_LOGD("creating %singBatch %p, bid %x, at %d",
601 deferInfo.mergeable ? "Merg" : "Draw",
602 targetBatch, deferInfo.batchId, insertBatchIndex);
John Reck272a6852015-07-29 16:48:58 -0700603 mBatches.insert(mBatches.begin() + insertBatchIndex, targetBatch);
Chris Craikc3566d02013-02-04 16:16:33 -0800604 }
Chris Craik527a3aa2013-03-04 10:19:31 -0800605
Chris Craikc1c5f082013-09-11 16:23:37 -0700606 targetBatch->add(op, state, deferInfo.opaqueOverBounds);
Chris Craikc3566d02013-02-04 16:16:33 -0800607}
608
Chris Craikff785832013-03-08 13:12:16 -0800609void DeferredDisplayList::storeStateOpBarrier(OpenGLRenderer& renderer, StateOp* op) {
610 DEFER_LOGD("%p adding state op barrier at pos %d", this, mBatches.size());
611
Chris Craikc1c5f082013-09-11 16:23:37 -0700612 DeferredDisplayState* state = createState();
613 renderer.storeDisplayState(*state, getStateOpDeferFlags());
John Reck272a6852015-07-29 16:48:58 -0700614 mBatches.push_back(new StateOpBatch(op, state));
Chris Craikff785832013-03-08 13:12:16 -0800615 resetBatchingState();
616}
617
Chris Craik7273daa2013-03-28 11:25:24 -0700618void DeferredDisplayList::storeRestoreToCountBarrier(OpenGLRenderer& renderer, StateOp* op,
619 int newSaveCount) {
Chris Craikff785832013-03-08 13:12:16 -0800620 DEFER_LOGD("%p adding restore to count %d barrier, pos %d",
621 this, newSaveCount, mBatches.size());
622
Chris Craik7273daa2013-03-28 11:25:24 -0700623 // store displayState for the restore operation, as it may be associated with a saveLayer that
624 // doesn't have kClip_SaveFlag set
Chris Craikc1c5f082013-09-11 16:23:37 -0700625 DeferredDisplayState* state = createState();
626 renderer.storeDisplayState(*state, getStateOpDeferFlags());
John Reck272a6852015-07-29 16:48:58 -0700627 mBatches.push_back(new RestoreToCountBatch(op, state, newSaveCount));
Chris Craikff785832013-03-08 13:12:16 -0800628 resetBatchingState();
629}
630
631/////////////////////////////////////////////////////////////////////////////////
632// Replay / flush
633/////////////////////////////////////////////////////////////////////////////////
634
John Reck272a6852015-07-29 16:48:58 -0700635static void replayBatchList(const std::vector<Batch*>& batchList,
Chris Craikff785832013-03-08 13:12:16 -0800636 OpenGLRenderer& renderer, Rect& dirty) {
Chris Craikff785832013-03-08 13:12:16 -0800637
Chris Craikff785832013-03-08 13:12:16 -0800638 for (unsigned int i = 0; i < batchList.size(); i++) {
Chris Craik28ce94a2013-05-31 11:38:03 -0700639 if (batchList[i]) {
Tom Hudson107843d2014-09-08 11:26:26 -0400640 batchList[i]->replay(renderer, dirty, i);
Chris Craik28ce94a2013-05-31 11:38:03 -0700641 }
Chris Craikff785832013-03-08 13:12:16 -0800642 }
Chris Craik527a3aa2013-03-04 10:19:31 -0800643 DEFER_LOGD("--flushed, drew %d batches", batchList.size());
Chris Craikff785832013-03-08 13:12:16 -0800644}
645
Tom Hudson107843d2014-09-08 11:26:26 -0400646void DeferredDisplayList::flush(OpenGLRenderer& renderer, Rect& dirty) {
Chris Craikff785832013-03-08 13:12:16 -0800647 ATRACE_NAME("flush drawing commands");
Chris Craikc08820f2015-09-22 14:22:29 -0700648 Caches::getInstance().fontRenderer.endPrecaching();
Romain Guycf51a412013-04-08 19:40:31 -0700649
Tom Hudson107843d2014-09-08 11:26:26 -0400650 if (isEmpty()) return; // nothing to flush
Chris Craika4e16c52013-03-22 10:00:48 -0700651 renderer.restoreToCount(1);
Chris Craikc3566d02013-02-04 16:16:33 -0800652
653 DEFER_LOGD("--flushing");
Romain Guy0f6675332013-03-01 14:31:04 -0800654 renderer.eventMark("Flush");
655
Chris Craik8df5ffa2015-04-28 17:47:20 -0700656 // save and restore so that reordering doesn't affect final state
Chris Craika4e16c52013-03-22 10:00:48 -0700657 renderer.save(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
658
Chris Craikb45c6aa2015-09-28 15:41:27 -0700659 if (CC_LIKELY(avoidOverdraw())) {
Chris Craikef8d6f22014-12-17 11:10:28 -0800660 for (unsigned int i = 1; i < mBatches.size(); i++) {
661 if (mBatches[i] && mBatches[i]->coversBounds(mBounds)) {
662 discardDrawingBatches(i - 1);
663 }
Chris Craik28ce94a2013-05-31 11:38:03 -0700664 }
665 }
Chris Craik1ed30c92013-04-03 12:37:35 -0700666 // NOTE: depth of the save stack at this point, before playback, should be reflected in
667 // FLUSH_SAVE_STACK_DEPTH, so that save/restores match up correctly
Tom Hudson107843d2014-09-08 11:26:26 -0400668 replayBatchList(mBatches, renderer, dirty);
Chris Craika4e16c52013-03-22 10:00:48 -0700669
670 renderer.restoreToCount(1);
Chris Craikc3566d02013-02-04 16:16:33 -0800671
Chris Craikff785832013-03-08 13:12:16 -0800672 DEFER_LOGD("--flush complete, returning %x", status);
Chris Craikc3566d02013-02-04 16:16:33 -0800673 clear();
Chris Craikc3566d02013-02-04 16:16:33 -0800674}
675
Chris Craikf70119c2013-06-13 11:21:22 -0700676void DeferredDisplayList::discardDrawingBatches(const unsigned int maxIndex) {
Chris Craik28ce94a2013-05-31 11:38:03 -0700677 for (unsigned int i = mEarliestUnclearedIndex; i <= maxIndex; i++) {
Chris Craikf70119c2013-06-13 11:21:22 -0700678 // leave deferred state ops alone for simplicity (empty save restore pairs may now exist)
Chris Craik28ce94a2013-05-31 11:38:03 -0700679 if (mBatches[i] && mBatches[i]->purelyDrawBatch()) {
Chris Craik28ce94a2013-05-31 11:38:03 -0700680 delete mBatches[i];
John Reck272a6852015-07-29 16:48:58 -0700681 mBatches[i] = nullptr;
Chris Craik28ce94a2013-05-31 11:38:03 -0700682 }
683 }
684 mEarliestUnclearedIndex = maxIndex + 1;
685}
686
Chris Craikc3566d02013-02-04 16:16:33 -0800687}; // namespace uirenderer
688}; // namespace android