blob: b077a850c5b93846e571b07abcaebd360789eaba [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
17#define LOG_TAG "OpenGLRenderer"
18#define ATRACE_TAG ATRACE_TAG_VIEW
19
Romain Guyc46d07a2013-03-15 19:06:39 -070020#include <SkCanvas.h>
21
Chris Craikc3566d02013-02-04 16:16:33 -080022#include <utils/Trace.h>
Chris Craik28ce94a2013-05-31 11:38:03 -070023#include <ui/Rect.h>
24#include <ui/Region.h>
Chris Craikc3566d02013-02-04 16:16:33 -080025
Romain Guycf51a412013-04-08 19:40:31 -070026#include "Caches.h"
Chris Craikc3566d02013-02-04 16:16:33 -080027#include "Debug.h"
Chris Craik527a3aa2013-03-04 10:19:31 -080028#include "DeferredDisplayList.h"
Chris Craikc3566d02013-02-04 16:16:33 -080029#include "DisplayListOp.h"
30#include "OpenGLRenderer.h"
Chris Craik2507c342015-05-04 14:36:49 -070031#include "Properties.h"
Chris Craikdeeda3d2014-05-05 19:09:33 -070032#include "utils/MathUtils.h"
Chris Craikc3566d02013-02-04 16:16:33 -080033
34#if DEBUG_DEFER
35 #define DEFER_LOGD(...) ALOGD(__VA_ARGS__)
36#else
37 #define DEFER_LOGD(...)
38#endif
39
40namespace android {
41namespace uirenderer {
42
Chris Craik1ed30c92013-04-03 12:37:35 -070043// Depth of the save stack at the beginning of batch playback at flush time
44#define FLUSH_SAVE_STACK_DEPTH 2
45
Chris Craik527a3aa2013-03-04 10:19:31 -080046#define DEBUG_COLOR_BARRIER 0x1f000000
47#define DEBUG_COLOR_MERGEDBATCH 0x5f7f7fff
48#define DEBUG_COLOR_MERGEDBATCH_SOLO 0x5f7fff7f
49
Chris Craikff785832013-03-08 13:12:16 -080050/////////////////////////////////////////////////////////////////////////////////
51// Operation Batches
52/////////////////////////////////////////////////////////////////////////////////
53
Chris Craik527a3aa2013-03-04 10:19:31 -080054class Batch {
Chris Craikc3566d02013-02-04 16:16:33 -080055public:
Tom Hudson107843d2014-09-08 11:26:26 -040056 virtual void replay(OpenGLRenderer& renderer, Rect& dirty, int index) = 0;
Chris Craik527a3aa2013-03-04 10:19:31 -080057 virtual ~Batch() {}
Chris Craik28ce94a2013-05-31 11:38:03 -070058 virtual bool purelyDrawBatch() { return false; }
Andreas Gampe64bb4132014-11-22 00:35:09 +000059 virtual bool coversBounds(const Rect& bounds) { return false; }
Chris Craik527a3aa2013-03-04 10:19:31 -080060};
Chris Craikc3566d02013-02-04 16:16:33 -080061
Chris Craik527a3aa2013-03-04 10:19:31 -080062class DrawBatch : public Batch {
63public:
Chris Craik28ce94a2013-05-31 11:38:03 -070064 DrawBatch(const DeferInfo& deferInfo) : mAllOpsOpaque(true),
65 mBatchId(deferInfo.batchId), mMergeId(deferInfo.mergeId) {
Chris Craik527a3aa2013-03-04 10:19:31 -080066 mOps.clear();
67 }
68
69 virtual ~DrawBatch() { mOps.clear(); }
Chris Craikc3566d02013-02-04 16:16:33 -080070
Chris Craikc1c5f082013-09-11 16:23:37 -070071 virtual void add(DrawOp* op, const DeferredDisplayState* state, bool opaqueOverBounds) {
Chris Craikc3566d02013-02-04 16:16:33 -080072 // NOTE: ignore empty bounds special case, since we don't merge across those ops
Chris Craikc1c5f082013-09-11 16:23:37 -070073 mBounds.unionWith(state->mBounds);
Chris Craik28ce94a2013-05-31 11:38:03 -070074 mAllOpsOpaque &= opaqueOverBounds;
Chris Craikc1c5f082013-09-11 16:23:37 -070075 mOps.add(OpStatePair(op, state));
Chris Craikc3566d02013-02-04 16:16:33 -080076 }
77
Chris Craikc1c5f082013-09-11 16:23:37 -070078 bool intersects(const Rect& rect) {
Chris Craikc3566d02013-02-04 16:16:33 -080079 if (!rect.intersects(mBounds)) return false;
Chris Craikff785832013-03-08 13:12:16 -080080
Chris Craikc3566d02013-02-04 16:16:33 -080081 for (unsigned int i = 0; i < mOps.size(); i++) {
Chris Craikc1c5f082013-09-11 16:23:37 -070082 if (rect.intersects(mOps[i].state->mBounds)) {
Chris Craikc3566d02013-02-04 16:16:33 -080083#if DEBUG_DEFER
Chris Craikc1c5f082013-09-11 16:23:37 -070084 DEFER_LOGD("op intersects with op %p with bounds %f %f %f %f:", mOps[i].op,
85 mOps[i].state->mBounds.left, mOps[i].state->mBounds.top,
86 mOps[i].state->mBounds.right, mOps[i].state->mBounds.bottom);
87 mOps[i].op->output(2);
Chris Craikc3566d02013-02-04 16:16:33 -080088#endif
89 return true;
90 }
91 }
92 return false;
93 }
94
Chris Craikd41c4d82015-01-05 15:51:13 -080095 virtual void replay(OpenGLRenderer& renderer, Rect& dirty, int index) override {
Chris Craik41541822013-05-03 16:35:54 -070096 DEFER_LOGD("%d replaying DrawBatch %p, with %d ops (batch id %x, merge id %p)",
97 index, this, mOps.size(), getBatchId(), getMergeId());
Chris Craikff785832013-03-08 13:12:16 -080098
Chris Craikff785832013-03-08 13:12:16 -080099 for (unsigned int i = 0; i < mOps.size(); i++) {
Chris Craikc1c5f082013-09-11 16:23:37 -0700100 DrawOp* op = mOps[i].op;
101 const DeferredDisplayState* state = mOps[i].state;
102 renderer.restoreDisplayState(*state);
Chris Craikff785832013-03-08 13:12:16 -0800103
104#if DEBUG_DISPLAY_LIST_OPS_AS_EVENTS
Chris Craikd90144d2013-03-19 15:03:48 -0700105 renderer.eventMark(op->name());
Chris Craikff785832013-03-08 13:12:16 -0800106#endif
Tom Hudson107843d2014-09-08 11:26:26 -0400107 op->applyDraw(renderer, dirty);
Chris Craik527a3aa2013-03-04 10:19:31 -0800108
109#if DEBUG_MERGE_BEHAVIOR
Chris Craikc1c5f082013-09-11 16:23:37 -0700110 const Rect& bounds = state->mBounds;
Chris Craik527a3aa2013-03-04 10:19:31 -0800111 int batchColor = 0x1f000000;
112 if (getBatchId() & 0x1) batchColor |= 0x0000ff;
113 if (getBatchId() & 0x2) batchColor |= 0x00ff00;
114 if (getBatchId() & 0x4) batchColor |= 0xff0000;
115 renderer.drawScreenSpaceColorRect(bounds.left, bounds.top, bounds.right, bounds.bottom,
116 batchColor);
117#endif
Chris Craikff785832013-03-08 13:12:16 -0800118 }
Chris Craikff785832013-03-08 13:12:16 -0800119 }
120
Chris Craikd41c4d82015-01-05 15:51:13 -0800121 virtual bool purelyDrawBatch() override { return true; }
Chris Craik28ce94a2013-05-31 11:38:03 -0700122
Chris Craikd41c4d82015-01-05 15:51:13 -0800123 virtual bool coversBounds(const Rect& bounds) override {
Chris Craik28ce94a2013-05-31 11:38:03 -0700124 if (CC_LIKELY(!mAllOpsOpaque || !mBounds.contains(bounds) || count() == 1)) return false;
125
126 Region uncovered(android::Rect(bounds.left, bounds.top, bounds.right, bounds.bottom));
127 for (unsigned int i = 0; i < mOps.size(); i++) {
Chris Craikc1c5f082013-09-11 16:23:37 -0700128 const Rect &r = mOps[i].state->mBounds;
Chris Craik28ce94a2013-05-31 11:38:03 -0700129 uncovered.subtractSelf(android::Rect(r.left, r.top, r.right, r.bottom));
130 }
131 return uncovered.isEmpty();
132 }
133
Chris Craik527a3aa2013-03-04 10:19:31 -0800134 inline int getBatchId() const { return mBatchId; }
135 inline mergeid_t getMergeId() const { return mMergeId; }
Chris Craikff785832013-03-08 13:12:16 -0800136 inline int count() const { return mOps.size(); }
Chris Craik527a3aa2013-03-04 10:19:31 -0800137
138protected:
Chris Craikc1c5f082013-09-11 16:23:37 -0700139 Vector<OpStatePair> mOps;
Chris Craik28ce94a2013-05-31 11:38:03 -0700140 Rect mBounds; // union of bounds of contained ops
Chris Craik527a3aa2013-03-04 10:19:31 -0800141private:
Chris Craik28ce94a2013-05-31 11:38:03 -0700142 bool mAllOpsOpaque;
Chris Craik527a3aa2013-03-04 10:19:31 -0800143 int mBatchId;
144 mergeid_t mMergeId;
Chris Craikc3566d02013-02-04 16:16:33 -0800145};
146
Chris Craik527a3aa2013-03-04 10:19:31 -0800147class MergingDrawBatch : public DrawBatch {
148public:
Chris Craik0e87f002013-06-19 16:54:59 -0700149 MergingDrawBatch(DeferInfo& deferInfo, int width, int height) :
150 DrawBatch(deferInfo), mClipRect(width, height),
151 mClipSideFlags(kClipSide_None) {}
Chris Craika02c4ed2013-06-14 13:43:58 -0700152
153 /*
154 * Helper for determining if a new op can merge with a MergingDrawBatch based on their bounds
155 * and clip side flags. Positive bounds delta means new bounds fit in old.
156 */
157 static inline bool checkSide(const int currentFlags, const int newFlags, const int side,
158 float boundsDelta) {
159 bool currentClipExists = currentFlags & side;
160 bool newClipExists = newFlags & side;
161
162 // if current is clipped, we must be able to fit new bounds in current
163 if (boundsDelta > 0 && currentClipExists) return false;
164
165 // if new is clipped, we must be able to fit current bounds in new
166 if (boundsDelta < 0 && newClipExists) return false;
167
168 return true;
169 }
Chris Craik527a3aa2013-03-04 10:19:31 -0800170
171 /*
172 * Checks if a (mergeable) op can be merged into this batch
173 *
174 * If true, the op's multiDraw must be guaranteed to handle both ops simultaneously, so it is
175 * important to consider all paint attributes used in the draw calls in deciding both a) if an
Chris Craika02c4ed2013-06-14 13:43:58 -0700176 * 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 -0800177 *
178 * False positives can lead to information from the paints of subsequent merged operations being
179 * dropped, so we make simplifying qualifications on the ops that can merge, per op type.
180 */
Chris Craikc1c5f082013-09-11 16:23:37 -0700181 bool canMergeWith(const DrawOp* op, const DeferredDisplayState* state) {
Chris Craik527a3aa2013-03-04 10:19:31 -0800182 bool isTextBatch = getBatchId() == DeferredDisplayList::kOpBatch_Text ||
183 getBatchId() == DeferredDisplayList::kOpBatch_ColorText;
184
185 // Overlapping other operations is only allowed for text without shadow. For other ops,
186 // multiDraw isn't guaranteed to overdraw correctly
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -0400187 if (!isTextBatch || op->hasTextShadow()) {
Chris Craikc1c5f082013-09-11 16:23:37 -0700188 if (intersects(state->mBounds)) return false;
Chris Craik527a3aa2013-03-04 10:19:31 -0800189 }
Chris Craikc1c5f082013-09-11 16:23:37 -0700190 const DeferredDisplayState* lhs = state;
191 const DeferredDisplayState* rhs = mOps[0].state;
Chris Craik527a3aa2013-03-04 10:19:31 -0800192
Chris Craikdeeda3d2014-05-05 19:09:33 -0700193 if (!MathUtils::areEqual(lhs->mAlpha, rhs->mAlpha)) return false;
194
195 // Identical round rect clip state means both ops will clip in the same way, or not at all.
196 // As the state objects are const, we can compare their pointers to determine mergeability
197 if (lhs->mRoundRectClipState != rhs->mRoundRectClipState) return false;
Chris Craikfca52b752015-04-28 11:45:59 -0700198 if (lhs->mProjectionPathMask != rhs->mProjectionPathMask) return false;
Chris Craik527a3aa2013-03-04 10:19:31 -0800199
Chris Craika02c4ed2013-06-14 13:43:58 -0700200 /* Clipping compatibility check
201 *
202 * Exploits the fact that if a op or batch is clipped on a side, its bounds will equal its
203 * clip for that side.
204 */
205 const int currentFlags = mClipSideFlags;
Chris Craikc1c5f082013-09-11 16:23:37 -0700206 const int newFlags = state->mClipSideFlags;
Chris Craika02c4ed2013-06-14 13:43:58 -0700207 if (currentFlags != kClipSide_None || newFlags != kClipSide_None) {
Chris Craikc1c5f082013-09-11 16:23:37 -0700208 const Rect& opBounds = state->mBounds;
Chris Craika02c4ed2013-06-14 13:43:58 -0700209 float boundsDelta = mBounds.left - opBounds.left;
210 if (!checkSide(currentFlags, newFlags, kClipSide_Left, boundsDelta)) return false;
211 boundsDelta = mBounds.top - opBounds.top;
212 if (!checkSide(currentFlags, newFlags, kClipSide_Top, boundsDelta)) return false;
213
214 // right and bottom delta calculation reversed to account for direction
215 boundsDelta = opBounds.right - mBounds.right;
216 if (!checkSide(currentFlags, newFlags, kClipSide_Right, boundsDelta)) return false;
217 boundsDelta = opBounds.bottom - mBounds.bottom;
218 if (!checkSide(currentFlags, newFlags, kClipSide_Bottom, boundsDelta)) return false;
Chris Craik28ce94a2013-05-31 11:38:03 -0700219 }
Chris Craik28ce94a2013-05-31 11:38:03 -0700220
Chris Craik527a3aa2013-03-04 10:19:31 -0800221 // if paints are equal, then modifiers + paint attribs don't need to be compared
Chris Craikc1c5f082013-09-11 16:23:37 -0700222 if (op->mPaint == mOps[0].op->mPaint) return true;
Chris Craik527a3aa2013-03-04 10:19:31 -0800223
Chris Craikc1c5f082013-09-11 16:23:37 -0700224 if (op->getPaintAlpha() != mOps[0].op->getPaintAlpha()) return false;
Chris Craik527a3aa2013-03-04 10:19:31 -0800225
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500226 if (op->mPaint && mOps[0].op->mPaint &&
227 op->mPaint->getColorFilter() != mOps[0].op->mPaint->getColorFilter()) {
228 return false;
229 }
230
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400231 if (op->mPaint && mOps[0].op->mPaint &&
232 op->mPaint->getShader() != mOps[0].op->mPaint->getShader()) {
233 return false;
234 }
235
Chris Craik527a3aa2013-03-04 10:19:31 -0800236 return true;
237 }
238
Chris Craikd41c4d82015-01-05 15:51:13 -0800239 virtual void add(DrawOp* op, const DeferredDisplayState* state,
240 bool opaqueOverBounds) override {
Chris Craikc1c5f082013-09-11 16:23:37 -0700241 DrawBatch::add(op, state, opaqueOverBounds);
Chris Craik28ce94a2013-05-31 11:38:03 -0700242
Chris Craikc1c5f082013-09-11 16:23:37 -0700243 const int newClipSideFlags = state->mClipSideFlags;
Chris Craik28ce94a2013-05-31 11:38:03 -0700244 mClipSideFlags |= newClipSideFlags;
Chris Craikc1c5f082013-09-11 16:23:37 -0700245 if (newClipSideFlags & kClipSide_Left) mClipRect.left = state->mClip.left;
246 if (newClipSideFlags & kClipSide_Top) mClipRect.top = state->mClip.top;
247 if (newClipSideFlags & kClipSide_Right) mClipRect.right = state->mClip.right;
248 if (newClipSideFlags & kClipSide_Bottom) mClipRect.bottom = state->mClip.bottom;
Chris Craik28ce94a2013-05-31 11:38:03 -0700249 }
250
Chris Craikd41c4d82015-01-05 15:51:13 -0800251 virtual void replay(OpenGLRenderer& renderer, Rect& dirty, int index) override {
Chris Craik28ce94a2013-05-31 11:38:03 -0700252 DEFER_LOGD("%d replaying MergingDrawBatch %p, with %d ops,"
253 " clip flags %x (batch id %x, merge id %p)",
254 index, this, mOps.size(), mClipSideFlags, getBatchId(), getMergeId());
Chris Craik527a3aa2013-03-04 10:19:31 -0800255 if (mOps.size() == 1) {
Tom Hudson107843d2014-09-08 11:26:26 -0400256 DrawBatch::replay(renderer, dirty, -1);
257 return;
Chris Craik527a3aa2013-03-04 10:19:31 -0800258 }
259
Chris Craik28ce94a2013-05-31 11:38:03 -0700260 // 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 -0800261 renderer.setupMergedMultiDraw(mClipSideFlags ? &mClipRect : nullptr);
Chris Craik28ce94a2013-05-31 11:38:03 -0700262
Chris Craikc1c5f082013-09-11 16:23:37 -0700263 DrawOp* op = mOps[0].op;
Chris Craikd965bc52013-09-16 14:47:13 -0700264#if DEBUG_DISPLAY_LIST_OPS_AS_EVENTS
265 renderer.eventMark("multiDraw");
266 renderer.eventMark(op->name());
267#endif
Tom Hudson107843d2014-09-08 11:26:26 -0400268 op->multiDraw(renderer, dirty, mOps, mBounds);
Chris Craik527a3aa2013-03-04 10:19:31 -0800269
270#if DEBUG_MERGE_BEHAVIOR
271 renderer.drawScreenSpaceColorRect(mBounds.left, mBounds.top, mBounds.right, mBounds.bottom,
272 DEBUG_COLOR_MERGEDBATCH);
273#endif
Chris Craik527a3aa2013-03-04 10:19:31 -0800274 }
Chris Craik28ce94a2013-05-31 11:38:03 -0700275
276private:
277 /*
278 * Contains the effective clip rect shared by all merged ops. Initialized to the layer viewport,
279 * it will shrink if an op must be clipped on a certain side. The clipped sides are reflected in
280 * mClipSideFlags.
281 */
282 Rect mClipRect;
283 int mClipSideFlags;
Chris Craik527a3aa2013-03-04 10:19:31 -0800284};
285
286class StateOpBatch : public Batch {
Chris Craikff785832013-03-08 13:12:16 -0800287public:
288 // creates a single operation batch
Chris Craikc1c5f082013-09-11 16:23:37 -0700289 StateOpBatch(const StateOp* op, const DeferredDisplayState* state) : mOp(op), mState(state) {}
Chris Craikff785832013-03-08 13:12:16 -0800290
Chris Craikd41c4d82015-01-05 15:51:13 -0800291 virtual void replay(OpenGLRenderer& renderer, Rect& dirty, int index) override {
Chris Craikff785832013-03-08 13:12:16 -0800292 DEFER_LOGD("replaying state op batch %p", this);
Chris Craikc1c5f082013-09-11 16:23:37 -0700293 renderer.restoreDisplayState(*mState);
Chris Craikff785832013-03-08 13:12:16 -0800294
295 // use invalid save count because it won't be used at flush time - RestoreToCountOp is the
296 // only one to use it, and we don't use that class at flush time, instead calling
297 // renderer.restoreToCount directly
298 int saveCount = -1;
299 mOp->applyState(renderer, saveCount);
Chris Craikff785832013-03-08 13:12:16 -0800300 }
301
302private:
Chris Craik7273daa2013-03-28 11:25:24 -0700303 const StateOp* mOp;
Chris Craikc1c5f082013-09-11 16:23:37 -0700304 const DeferredDisplayState* mState;
Chris Craikff785832013-03-08 13:12:16 -0800305};
306
Chris Craik527a3aa2013-03-04 10:19:31 -0800307class RestoreToCountBatch : public Batch {
Chris Craikff785832013-03-08 13:12:16 -0800308public:
Andreas Gampe64bb4132014-11-22 00:35:09 +0000309 RestoreToCountBatch(const StateOp* op, const DeferredDisplayState* state, int restoreCount) :
310 mState(state), mRestoreCount(restoreCount) {}
Chris Craikff785832013-03-08 13:12:16 -0800311
Chris Craikd41c4d82015-01-05 15:51:13 -0800312 virtual void replay(OpenGLRenderer& renderer, Rect& dirty, int index) override {
Chris Craikff785832013-03-08 13:12:16 -0800313 DEFER_LOGD("batch %p restoring to count %d", this, mRestoreCount);
Chris Craik7273daa2013-03-28 11:25:24 -0700314
Chris Craikc1c5f082013-09-11 16:23:37 -0700315 renderer.restoreDisplayState(*mState);
Chris Craikff785832013-03-08 13:12:16 -0800316 renderer.restoreToCount(mRestoreCount);
Chris Craikff785832013-03-08 13:12:16 -0800317 }
318
319private:
Chris Craik7273daa2013-03-28 11:25:24 -0700320 // we use the state storage for the RestoreToCountOp, but don't replay the op itself
Chris Craikc1c5f082013-09-11 16:23:37 -0700321 const DeferredDisplayState* mState;
322
Chris Craikff785832013-03-08 13:12:16 -0800323 /*
324 * The count used here represents the flush() time saveCount. This is as opposed to the
325 * DisplayList record time, or defer() time values (which are RestoreToCountOp's mCount, and
326 * (saveCount + mCount) respectively). Since the count is different from the original
327 * RestoreToCountOp, we don't store a pointer to the op, as elsewhere.
328 */
329 const int mRestoreCount;
330};
331
Chris Craik527a3aa2013-03-04 10:19:31 -0800332#if DEBUG_MERGE_BEHAVIOR
333class BarrierDebugBatch : public Batch {
Tom Hudson107843d2014-09-08 11:26:26 -0400334 virtual void replay(OpenGLRenderer& renderer, Rect& dirty, int index) {
Chris Craik527a3aa2013-03-04 10:19:31 -0800335 renderer.drawScreenSpaceColorRect(0, 0, 10000, 10000, DEBUG_COLOR_BARRIER);
Chris Craik527a3aa2013-03-04 10:19:31 -0800336 }
337};
338#endif
339
Chris Craikff785832013-03-08 13:12:16 -0800340/////////////////////////////////////////////////////////////////////////////////
341// DeferredDisplayList
342/////////////////////////////////////////////////////////////////////////////////
343
344void DeferredDisplayList::resetBatchingState() {
Chris Craikc3566d02013-02-04 16:16:33 -0800345 for (int i = 0; i < kOpBatch_Count; i++) {
Chris Craikd41c4d82015-01-05 15:51:13 -0800346 mBatchLookup[i] = nullptr;
Chris Craik527a3aa2013-03-04 10:19:31 -0800347 mMergingBatches[i].clear();
Chris Craikc3566d02013-02-04 16:16:33 -0800348 }
Chris Craik527a3aa2013-03-04 10:19:31 -0800349#if DEBUG_MERGE_BEHAVIOR
350 if (mBatches.size() != 0) {
351 mBatches.add(new BarrierDebugBatch());
352 }
353#endif
354 mEarliestBatchIndex = mBatches.size();
Chris Craikff785832013-03-08 13:12:16 -0800355}
356
357void DeferredDisplayList::clear() {
358 resetBatchingState();
359 mComplexClipStackStart = -1;
360
Chris Craikc3566d02013-02-04 16:16:33 -0800361 for (unsigned int i = 0; i < mBatches.size(); i++) {
362 delete mBatches[i];
363 }
364 mBatches.clear();
Chris Craikff785832013-03-08 13:12:16 -0800365 mSaveStack.clear();
Chris Craik527a3aa2013-03-04 10:19:31 -0800366 mEarliestBatchIndex = 0;
Chris Craik28ce94a2013-05-31 11:38:03 -0700367 mEarliestUnclearedIndex = 0;
Chris Craikc3566d02013-02-04 16:16:33 -0800368}
369
Chris Craikff785832013-03-08 13:12:16 -0800370/////////////////////////////////////////////////////////////////////////////////
371// Operation adding
372/////////////////////////////////////////////////////////////////////////////////
373
374int DeferredDisplayList::getStateOpDeferFlags() const {
375 // For both clipOp and save(Layer)Op, we don't want to save drawing info, and only want to save
376 // the clip if we aren't recording a complex clip (and can thus trust it to be a rect)
377 return recordingComplexClip() ? 0 : kStateDeferFlag_Clip;
378}
379
380int DeferredDisplayList::getDrawOpDeferFlags() const {
381 return kStateDeferFlag_Draw | getStateOpDeferFlags();
382}
383
384/**
385 * When an clipping operation occurs that could cause a complex clip, record the operation and all
386 * subsequent clipOps, save/restores (if the clip flag is set). During a flush, instead of loading
387 * the clip from deferred state, we play back all of the relevant state operations that generated
388 * the complex clip.
389 *
390 * Note that we don't need to record the associated restore operation, since operations at defer
391 * time record whether they should store the renderer's current clip
392 */
393void DeferredDisplayList::addClip(OpenGLRenderer& renderer, ClipOp* op) {
394 if (recordingComplexClip() || op->canCauseComplexClip() || !renderer.hasRectToRectTransform()) {
395 DEFER_LOGD("%p Received complex clip operation %p", this, op);
396
397 // NOTE: defer clip op before setting mComplexClipStackStart so previous clip is recorded
398 storeStateOpBarrier(renderer, op);
399
400 if (!recordingComplexClip()) {
401 mComplexClipStackStart = renderer.getSaveCount() - 1;
402 DEFER_LOGD(" Starting complex clip region, start is %d", mComplexClipStackStart);
Chris Craikc3566d02013-02-04 16:16:33 -0800403 }
Chris Craikff785832013-03-08 13:12:16 -0800404 }
405}
406
407/**
408 * For now, we record save layer operations as barriers in the batch list, preventing drawing
409 * operations from reordering around the saveLayer and it's associated restore()
410 *
411 * In the future, we should send saveLayer commands (if they can be played out of order) and their
412 * contained drawing operations to a seperate list of batches, so that they may draw at the
413 * beginning of the frame. This would avoid targetting and removing an FBO in the middle of a frame.
414 *
415 * saveLayer operations should be pulled to the beginning of the frame if the canvas doesn't have a
416 * complex clip, and if the flags (kClip_SaveFlag & kClipToLayer_SaveFlag) are set.
417 */
418void DeferredDisplayList::addSaveLayer(OpenGLRenderer& renderer,
419 SaveLayerOp* op, int newSaveCount) {
420 DEFER_LOGD("%p adding saveLayerOp %p, flags %x, new count %d",
421 this, op, op->getFlags(), newSaveCount);
422
423 storeStateOpBarrier(renderer, op);
424 mSaveStack.push(newSaveCount);
425}
426
427/**
428 * Takes save op and it's return value - the new save count - and stores it into the stream as a
429 * barrier if it's needed to properly modify a complex clip
430 */
431void DeferredDisplayList::addSave(OpenGLRenderer& renderer, SaveOp* op, int newSaveCount) {
432 int saveFlags = op->getFlags();
433 DEFER_LOGD("%p adding saveOp %p, flags %x, new count %d", this, op, saveFlags, newSaveCount);
434
435 if (recordingComplexClip() && (saveFlags & SkCanvas::kClip_SaveFlag)) {
436 // store and replay the save operation, as it may be needed to correctly playback the clip
437 DEFER_LOGD(" adding save barrier with new save count %d", newSaveCount);
438 storeStateOpBarrier(renderer, op);
439 mSaveStack.push(newSaveCount);
440 }
441}
442
443/**
444 * saveLayer() commands must be associated with a restoreToCount batch that will clean up and draw
445 * the layer in the deferred list
446 *
447 * other save() commands which occur as children of a snapshot with complex clip will be deferred,
448 * and must be restored
449 *
450 * Either will act as a barrier to draw operation reordering, as we want to play back layer
451 * save/restore and complex canvas modifications (including save/restore) in order.
452 */
Chris Craik7273daa2013-03-28 11:25:24 -0700453void DeferredDisplayList::addRestoreToCount(OpenGLRenderer& renderer, StateOp* op,
454 int newSaveCount) {
Chris Craikff785832013-03-08 13:12:16 -0800455 DEFER_LOGD("%p addRestoreToCount %d", this, newSaveCount);
456
457 if (recordingComplexClip() && newSaveCount <= mComplexClipStackStart) {
458 mComplexClipStackStart = -1;
459 resetBatchingState();
460 }
461
462 if (mSaveStack.isEmpty() || newSaveCount > mSaveStack.top()) {
463 return;
464 }
465
466 while (!mSaveStack.isEmpty() && mSaveStack.top() >= newSaveCount) mSaveStack.pop();
467
Chris Craik1ed30c92013-04-03 12:37:35 -0700468 storeRestoreToCountBarrier(renderer, op, mSaveStack.size() + FLUSH_SAVE_STACK_DEPTH);
Chris Craikff785832013-03-08 13:12:16 -0800469}
470
471void DeferredDisplayList::addDrawOp(OpenGLRenderer& renderer, DrawOp* op) {
Chris Craikc1c5f082013-09-11 16:23:37 -0700472 /* 1: op calculates local bounds */
473 DeferredDisplayState* const state = createState();
John Reck3b202512014-06-23 13:13:08 -0700474 if (op->getLocalBounds(state->mBounds)) {
Chris Craikc1c5f082013-09-11 16:23:37 -0700475 if (state->mBounds.isEmpty()) {
476 // valid empty bounds, don't bother deferring
477 tryRecycleState(state);
478 return;
479 }
480 } else {
481 state->mBounds.setEmpty();
482 }
483
484 /* 2: renderer calculates global bounds + stores state */
485 if (renderer.storeDisplayState(*state, getDrawOpDeferFlags())) {
486 tryRecycleState(state);
Chris Craikff785832013-03-08 13:12:16 -0800487 return; // quick rejected
488 }
489
Chris Craikc1c5f082013-09-11 16:23:37 -0700490 /* 3: ask op for defer info, given renderer state */
Chris Craik28ce94a2013-05-31 11:38:03 -0700491 DeferInfo deferInfo;
Chris Craikc1c5f082013-09-11 16:23:37 -0700492 op->onDefer(renderer, deferInfo, *state);
Chris Craik527a3aa2013-03-04 10:19:31 -0800493
494 // complex clip has a complex set of expectations on the renderer state - for now, avoid taking
495 // the merge path in those cases
Chris Craik28ce94a2013-05-31 11:38:03 -0700496 deferInfo.mergeable &= !recordingComplexClip();
Chris Craik0e87f002013-06-19 16:54:59 -0700497 deferInfo.opaqueOverBounds &= !recordingComplexClip() && mSaveStack.isEmpty();
Chris Craik28ce94a2013-05-31 11:38:03 -0700498
Chris Craikef8d6f22014-12-17 11:10:28 -0800499 if (CC_LIKELY(mAvoidOverdraw) && mBatches.size() &&
Chris Craikc1c5f082013-09-11 16:23:37 -0700500 state->mClipSideFlags != kClipSide_ConservativeFull &&
501 deferInfo.opaqueOverBounds && state->mBounds.contains(mBounds)) {
Chris Craikf70119c2013-06-13 11:21:22 -0700502 // avoid overdraw by resetting drawing state + discarding drawing ops
Chris Craik28ce94a2013-05-31 11:38:03 -0700503 discardDrawingBatches(mBatches.size() - 1);
Chris Craikf70119c2013-06-13 11:21:22 -0700504 resetBatchingState();
Chris Craik28ce94a2013-05-31 11:38:03 -0700505 }
Chris Craikff785832013-03-08 13:12:16 -0800506
Chris Craik2507c342015-05-04 14:36:49 -0700507 if (CC_UNLIKELY(Properties::drawReorderDisabled)) {
Chris Craikff785832013-03-08 13:12:16 -0800508 // TODO: elegant way to reuse batches?
Chris Craik28ce94a2013-05-31 11:38:03 -0700509 DrawBatch* b = new DrawBatch(deferInfo);
Chris Craikc1c5f082013-09-11 16:23:37 -0700510 b->add(op, state, deferInfo.opaqueOverBounds);
Chris Craikc3566d02013-02-04 16:16:33 -0800511 mBatches.add(b);
512 return;
513 }
514
Chris Craik527a3aa2013-03-04 10:19:31 -0800515 // 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 -0800516 DrawBatch* targetBatch = nullptr;
Chris Craikc3566d02013-02-04 16:16:33 -0800517
Chris Craik527a3aa2013-03-04 10:19:31 -0800518 // insertion point of a new batch, will hopefully be immediately after similar batch
519 // (eventually, should be similar shader)
520 int insertBatchIndex = mBatches.size();
Chris Craikc3566d02013-02-04 16:16:33 -0800521 if (!mBatches.isEmpty()) {
Chris Craikc1c5f082013-09-11 16:23:37 -0700522 if (state->mBounds.isEmpty()) {
Chris Craikc3566d02013-02-04 16:16:33 -0800523 // don't know the bounds for op, so add to last batch and start from scratch on next op
Chris Craik28ce94a2013-05-31 11:38:03 -0700524 DrawBatch* b = new DrawBatch(deferInfo);
Chris Craikc1c5f082013-09-11 16:23:37 -0700525 b->add(op, state, deferInfo.opaqueOverBounds);
Chris Craik527a3aa2013-03-04 10:19:31 -0800526 mBatches.add(b);
527 resetBatchingState();
Chris Craikc3566d02013-02-04 16:16:33 -0800528#if DEBUG_DEFER
529 DEFER_LOGD("Warning: Encountered op with empty bounds, resetting batches");
530 op->output(2);
531#endif
532 return;
533 }
534
Chris Craik28ce94a2013-05-31 11:38:03 -0700535 if (deferInfo.mergeable) {
Chris Craik527a3aa2013-03-04 10:19:31 -0800536 // Try to merge with any existing batch with same mergeId.
Chris Craik28ce94a2013-05-31 11:38:03 -0700537 if (mMergingBatches[deferInfo.batchId].get(deferInfo.mergeId, targetBatch)) {
Chris Craikc1c5f082013-09-11 16:23:37 -0700538 if (!((MergingDrawBatch*) targetBatch)->canMergeWith(op, state)) {
Chris Craikd41c4d82015-01-05 15:51:13 -0800539 targetBatch = nullptr;
Chris Craik527a3aa2013-03-04 10:19:31 -0800540 }
541 }
542 } else {
543 // join with similar, non-merging batch
Chris Craik28ce94a2013-05-31 11:38:03 -0700544 targetBatch = (DrawBatch*)mBatchLookup[deferInfo.batchId];
Chris Craik527a3aa2013-03-04 10:19:31 -0800545 }
546
Chris Craik28ce94a2013-05-31 11:38:03 -0700547 if (targetBatch || deferInfo.mergeable) {
Chris Craikc3566d02013-02-04 16:16:33 -0800548 // iterate back toward target to see if anything drawn since should overlap the new op
Chris Craik527a3aa2013-03-04 10:19:31 -0800549 // if no target, merging ops still interate to find similar batch to insert after
550 for (int i = mBatches.size() - 1; i >= mEarliestBatchIndex; i--) {
551 DrawBatch* overBatch = (DrawBatch*)mBatches[i];
552
553 if (overBatch == targetBatch) break;
554
555 // TODO: also consider shader shared between batch types
Chris Craik28ce94a2013-05-31 11:38:03 -0700556 if (deferInfo.batchId == overBatch->getBatchId()) {
Chris Craik527a3aa2013-03-04 10:19:31 -0800557 insertBatchIndex = i + 1;
558 if (!targetBatch) break; // found insert position, quit
559 }
560
Chris Craikc1c5f082013-09-11 16:23:37 -0700561 if (overBatch->intersects(state->mBounds)) {
Chris Craik527a3aa2013-03-04 10:19:31 -0800562 // NOTE: it may be possible to optimize for special cases where two operations
563 // of the same batch/paint could swap order, such as with a non-mergeable
564 // (clipped) and a mergeable text operation
Chris Craikd41c4d82015-01-05 15:51:13 -0800565 targetBatch = nullptr;
Chris Craikc3566d02013-02-04 16:16:33 -0800566#if DEBUG_DEFER
Chris Craikc1c5f082013-09-11 16:23:37 -0700567 DEFER_LOGD("op couldn't join batch %p, was intersected by batch %d",
568 targetBatch, i);
Chris Craikc3566d02013-02-04 16:16:33 -0800569 op->output(2);
570#endif
571 break;
572 }
573 }
574 }
575 }
Chris Craik527a3aa2013-03-04 10:19:31 -0800576
Chris Craikc3566d02013-02-04 16:16:33 -0800577 if (!targetBatch) {
Chris Craik28ce94a2013-05-31 11:38:03 -0700578 if (deferInfo.mergeable) {
Chris Craik0e87f002013-06-19 16:54:59 -0700579 targetBatch = new MergingDrawBatch(deferInfo,
580 renderer.getViewportWidth(), renderer.getViewportHeight());
Chris Craik28ce94a2013-05-31 11:38:03 -0700581 mMergingBatches[deferInfo.batchId].put(deferInfo.mergeId, targetBatch);
Chris Craik527a3aa2013-03-04 10:19:31 -0800582 } else {
Chris Craik28ce94a2013-05-31 11:38:03 -0700583 targetBatch = new DrawBatch(deferInfo);
584 mBatchLookup[deferInfo.batchId] = targetBatch;
Chris Craikc3566d02013-02-04 16:16:33 -0800585 }
Chris Craik527a3aa2013-03-04 10:19:31 -0800586
Chris Craikf70119c2013-06-13 11:21:22 -0700587 DEFER_LOGD("creating %singBatch %p, bid %x, at %d",
588 deferInfo.mergeable ? "Merg" : "Draw",
589 targetBatch, deferInfo.batchId, insertBatchIndex);
Chris Craik527a3aa2013-03-04 10:19:31 -0800590 mBatches.insertAt(targetBatch, insertBatchIndex);
Chris Craikc3566d02013-02-04 16:16:33 -0800591 }
Chris Craik527a3aa2013-03-04 10:19:31 -0800592
Chris Craikc1c5f082013-09-11 16:23:37 -0700593 targetBatch->add(op, state, deferInfo.opaqueOverBounds);
Chris Craikc3566d02013-02-04 16:16:33 -0800594}
595
Chris Craikff785832013-03-08 13:12:16 -0800596void DeferredDisplayList::storeStateOpBarrier(OpenGLRenderer& renderer, StateOp* op) {
597 DEFER_LOGD("%p adding state op barrier at pos %d", this, mBatches.size());
598
Chris Craikc1c5f082013-09-11 16:23:37 -0700599 DeferredDisplayState* state = createState();
600 renderer.storeDisplayState(*state, getStateOpDeferFlags());
601 mBatches.add(new StateOpBatch(op, state));
Chris Craikff785832013-03-08 13:12:16 -0800602 resetBatchingState();
603}
604
Chris Craik7273daa2013-03-28 11:25:24 -0700605void DeferredDisplayList::storeRestoreToCountBarrier(OpenGLRenderer& renderer, StateOp* op,
606 int newSaveCount) {
Chris Craikff785832013-03-08 13:12:16 -0800607 DEFER_LOGD("%p adding restore to count %d barrier, pos %d",
608 this, newSaveCount, mBatches.size());
609
Chris Craik7273daa2013-03-28 11:25:24 -0700610 // store displayState for the restore operation, as it may be associated with a saveLayer that
611 // doesn't have kClip_SaveFlag set
Chris Craikc1c5f082013-09-11 16:23:37 -0700612 DeferredDisplayState* state = createState();
613 renderer.storeDisplayState(*state, getStateOpDeferFlags());
614 mBatches.add(new RestoreToCountBatch(op, state, newSaveCount));
Chris Craikff785832013-03-08 13:12:16 -0800615 resetBatchingState();
616}
617
618/////////////////////////////////////////////////////////////////////////////////
619// Replay / flush
620/////////////////////////////////////////////////////////////////////////////////
621
Tom Hudson107843d2014-09-08 11:26:26 -0400622static void replayBatchList(const Vector<Batch*>& batchList,
Chris Craikff785832013-03-08 13:12:16 -0800623 OpenGLRenderer& renderer, Rect& dirty) {
Chris Craikff785832013-03-08 13:12:16 -0800624
Chris Craikff785832013-03-08 13:12:16 -0800625 for (unsigned int i = 0; i < batchList.size(); i++) {
Chris Craik28ce94a2013-05-31 11:38:03 -0700626 if (batchList[i]) {
Tom Hudson107843d2014-09-08 11:26:26 -0400627 batchList[i]->replay(renderer, dirty, i);
Chris Craik28ce94a2013-05-31 11:38:03 -0700628 }
Chris Craikff785832013-03-08 13:12:16 -0800629 }
Chris Craik527a3aa2013-03-04 10:19:31 -0800630 DEFER_LOGD("--flushed, drew %d batches", batchList.size());
Chris Craikff785832013-03-08 13:12:16 -0800631}
632
Tom Hudson107843d2014-09-08 11:26:26 -0400633void DeferredDisplayList::flush(OpenGLRenderer& renderer, Rect& dirty) {
Chris Craikff785832013-03-08 13:12:16 -0800634 ATRACE_NAME("flush drawing commands");
Romain Guycf51a412013-04-08 19:40:31 -0700635 Caches::getInstance().fontRenderer->endPrecaching();
636
Tom Hudson107843d2014-09-08 11:26:26 -0400637 if (isEmpty()) return; // nothing to flush
Chris Craika4e16c52013-03-22 10:00:48 -0700638 renderer.restoreToCount(1);
Chris Craikc3566d02013-02-04 16:16:33 -0800639
640 DEFER_LOGD("--flushing");
Romain Guy0f6675332013-03-01 14:31:04 -0800641 renderer.eventMark("Flush");
642
Chris Craik8df5ffa2015-04-28 17:47:20 -0700643 // save and restore so that reordering doesn't affect final state
Chris Craika4e16c52013-03-22 10:00:48 -0700644 renderer.save(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
645
Chris Craikef8d6f22014-12-17 11:10:28 -0800646 if (CC_LIKELY(mAvoidOverdraw)) {
647 for (unsigned int i = 1; i < mBatches.size(); i++) {
648 if (mBatches[i] && mBatches[i]->coversBounds(mBounds)) {
649 discardDrawingBatches(i - 1);
650 }
Chris Craik28ce94a2013-05-31 11:38:03 -0700651 }
652 }
Chris Craik1ed30c92013-04-03 12:37:35 -0700653 // NOTE: depth of the save stack at this point, before playback, should be reflected in
654 // FLUSH_SAVE_STACK_DEPTH, so that save/restores match up correctly
Tom Hudson107843d2014-09-08 11:26:26 -0400655 replayBatchList(mBatches, renderer, dirty);
Chris Craika4e16c52013-03-22 10:00:48 -0700656
657 renderer.restoreToCount(1);
Chris Craikc3566d02013-02-04 16:16:33 -0800658
Chris Craikff785832013-03-08 13:12:16 -0800659 DEFER_LOGD("--flush complete, returning %x", status);
Chris Craikc3566d02013-02-04 16:16:33 -0800660 clear();
Chris Craikc3566d02013-02-04 16:16:33 -0800661}
662
Chris Craikf70119c2013-06-13 11:21:22 -0700663void DeferredDisplayList::discardDrawingBatches(const unsigned int maxIndex) {
Chris Craik28ce94a2013-05-31 11:38:03 -0700664 for (unsigned int i = mEarliestUnclearedIndex; i <= maxIndex; i++) {
Chris Craikf70119c2013-06-13 11:21:22 -0700665 // leave deferred state ops alone for simplicity (empty save restore pairs may now exist)
Chris Craik28ce94a2013-05-31 11:38:03 -0700666 if (mBatches[i] && mBatches[i]->purelyDrawBatch()) {
Chris Craik28ce94a2013-05-31 11:38:03 -0700667 delete mBatches[i];
Chris Craikd41c4d82015-01-05 15:51:13 -0800668 mBatches.replaceAt(nullptr, i);
Chris Craik28ce94a2013-05-31 11:38:03 -0700669 }
670 }
671 mEarliestUnclearedIndex = maxIndex + 1;
672}
673
Chris Craikc3566d02013-02-04 16:16:33 -0800674}; // namespace uirenderer
675}; // namespace android