blob: 7355baaebbe64bab74228930d72f58b5425356c4 [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 Craikdeeda3d2014-05-05 19:09:33 -070031#include "utils/MathUtils.h"
Chris Craikc3566d02013-02-04 16:16:33 -080032
33#if DEBUG_DEFER
34 #define DEFER_LOGD(...) ALOGD(__VA_ARGS__)
35#else
36 #define DEFER_LOGD(...)
37#endif
38
39namespace android {
40namespace uirenderer {
41
Chris Craik1ed30c92013-04-03 12:37:35 -070042// Depth of the save stack at the beginning of batch playback at flush time
43#define FLUSH_SAVE_STACK_DEPTH 2
44
Chris Craik527a3aa2013-03-04 10:19:31 -080045#define DEBUG_COLOR_BARRIER 0x1f000000
46#define DEBUG_COLOR_MERGEDBATCH 0x5f7f7fff
47#define DEBUG_COLOR_MERGEDBATCH_SOLO 0x5f7fff7f
48
Chris Craikff785832013-03-08 13:12:16 -080049/////////////////////////////////////////////////////////////////////////////////
50// Operation Batches
51/////////////////////////////////////////////////////////////////////////////////
52
Chris Craik527a3aa2013-03-04 10:19:31 -080053class Batch {
Chris Craikc3566d02013-02-04 16:16:33 -080054public:
Tom Hudson107843d2014-09-08 11:26:26 -040055 virtual void replay(OpenGLRenderer& renderer, Rect& dirty, int index) = 0;
Chris Craik527a3aa2013-03-04 10:19:31 -080056 virtual ~Batch() {}
Chris Craik28ce94a2013-05-31 11:38:03 -070057 virtual bool purelyDrawBatch() { return false; }
Andreas Gampe64bb4132014-11-22 00:35:09 +000058 virtual bool coversBounds(const Rect& bounds) { return false; }
Chris Craik527a3aa2013-03-04 10:19:31 -080059};
Chris Craikc3566d02013-02-04 16:16:33 -080060
Chris Craik527a3aa2013-03-04 10:19:31 -080061class DrawBatch : public Batch {
62public:
Chris Craik28ce94a2013-05-31 11:38:03 -070063 DrawBatch(const DeferInfo& deferInfo) : mAllOpsOpaque(true),
64 mBatchId(deferInfo.batchId), mMergeId(deferInfo.mergeId) {
Chris Craik527a3aa2013-03-04 10:19:31 -080065 mOps.clear();
66 }
67
68 virtual ~DrawBatch() { mOps.clear(); }
Chris Craikc3566d02013-02-04 16:16:33 -080069
Chris Craikc1c5f082013-09-11 16:23:37 -070070 virtual void add(DrawOp* op, const DeferredDisplayState* state, bool opaqueOverBounds) {
Chris Craikc3566d02013-02-04 16:16:33 -080071 // NOTE: ignore empty bounds special case, since we don't merge across those ops
Chris Craikc1c5f082013-09-11 16:23:37 -070072 mBounds.unionWith(state->mBounds);
Chris Craik28ce94a2013-05-31 11:38:03 -070073 mAllOpsOpaque &= opaqueOverBounds;
Chris Craikc1c5f082013-09-11 16:23:37 -070074 mOps.add(OpStatePair(op, state));
Chris Craikc3566d02013-02-04 16:16:33 -080075 }
76
Chris Craikc1c5f082013-09-11 16:23:37 -070077 bool intersects(const Rect& rect) {
Chris Craikc3566d02013-02-04 16:16:33 -080078 if (!rect.intersects(mBounds)) return false;
Chris Craikff785832013-03-08 13:12:16 -080079
Chris Craikc3566d02013-02-04 16:16:33 -080080 for (unsigned int i = 0; i < mOps.size(); i++) {
Chris Craikc1c5f082013-09-11 16:23:37 -070081 if (rect.intersects(mOps[i].state->mBounds)) {
Chris Craikc3566d02013-02-04 16:16:33 -080082#if DEBUG_DEFER
Chris Craikc1c5f082013-09-11 16:23:37 -070083 DEFER_LOGD("op intersects with op %p with bounds %f %f %f %f:", mOps[i].op,
84 mOps[i].state->mBounds.left, mOps[i].state->mBounds.top,
85 mOps[i].state->mBounds.right, mOps[i].state->mBounds.bottom);
86 mOps[i].op->output(2);
Chris Craikc3566d02013-02-04 16:16:33 -080087#endif
88 return true;
89 }
90 }
91 return false;
92 }
93
Andreas Gampe64bb4132014-11-22 00:35:09 +000094 virtual void replay(OpenGLRenderer& renderer, Rect& dirty, int index) {
Chris Craik41541822013-05-03 16:35:54 -070095 DEFER_LOGD("%d replaying DrawBatch %p, with %d ops (batch id %x, merge id %p)",
96 index, this, mOps.size(), getBatchId(), getMergeId());
Chris Craikff785832013-03-08 13:12:16 -080097
Chris Craikff785832013-03-08 13:12:16 -080098 DisplayListLogBuffer& logBuffer = DisplayListLogBuffer::getInstance();
99 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
Chris Craikff785832013-03-08 13:12:16 -0800107 logBuffer.writeCommand(0, op->name());
Tom Hudson107843d2014-09-08 11:26:26 -0400108 op->applyDraw(renderer, dirty);
Chris Craik527a3aa2013-03-04 10:19:31 -0800109
110#if DEBUG_MERGE_BEHAVIOR
Chris Craikc1c5f082013-09-11 16:23:37 -0700111 const Rect& bounds = state->mBounds;
Chris Craik527a3aa2013-03-04 10:19:31 -0800112 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 Craikff785832013-03-08 13:12:16 -0800119 }
Chris Craikff785832013-03-08 13:12:16 -0800120 }
121
Chris Craik28ce94a2013-05-31 11:38:03 -0700122 virtual bool purelyDrawBatch() { return true; }
123
124 virtual bool coversBounds(const Rect& bounds) {
125 if (CC_LIKELY(!mAllOpsOpaque || !mBounds.contains(bounds) || count() == 1)) return false;
126
127 Region uncovered(android::Rect(bounds.left, bounds.top, bounds.right, bounds.bottom));
128 for (unsigned int i = 0; i < mOps.size(); i++) {
Chris Craikc1c5f082013-09-11 16:23:37 -0700129 const Rect &r = mOps[i].state->mBounds;
Chris Craik28ce94a2013-05-31 11:38:03 -0700130 uncovered.subtractSelf(android::Rect(r.left, r.top, r.right, r.bottom));
131 }
132 return uncovered.isEmpty();
133 }
134
Chris Craik527a3aa2013-03-04 10:19:31 -0800135 inline int getBatchId() const { return mBatchId; }
136 inline mergeid_t getMergeId() const { return mMergeId; }
Chris Craikff785832013-03-08 13:12:16 -0800137 inline int count() const { return mOps.size(); }
Chris Craik527a3aa2013-03-04 10:19:31 -0800138
139protected:
Chris Craikc1c5f082013-09-11 16:23:37 -0700140 Vector<OpStatePair> mOps;
Chris Craik28ce94a2013-05-31 11:38:03 -0700141 Rect mBounds; // union of bounds of contained ops
Chris Craik527a3aa2013-03-04 10:19:31 -0800142private:
Chris Craik28ce94a2013-05-31 11:38:03 -0700143 bool mAllOpsOpaque;
Chris Craik527a3aa2013-03-04 10:19:31 -0800144 int mBatchId;
145 mergeid_t mMergeId;
Chris Craikc3566d02013-02-04 16:16:33 -0800146};
147
Chris Craik527a3aa2013-03-04 10:19:31 -0800148class MergingDrawBatch : public DrawBatch {
149public:
Chris Craik0e87f002013-06-19 16:54:59 -0700150 MergingDrawBatch(DeferInfo& deferInfo, int width, int height) :
151 DrawBatch(deferInfo), mClipRect(width, height),
152 mClipSideFlags(kClipSide_None) {}
Chris Craika02c4ed2013-06-14 13:43:58 -0700153
154 /*
155 * Helper for determining if a new op can merge with a MergingDrawBatch based on their bounds
156 * and clip side flags. Positive bounds delta means new bounds fit in old.
157 */
158 static inline bool checkSide(const int currentFlags, const int newFlags, const int side,
159 float boundsDelta) {
160 bool currentClipExists = currentFlags & side;
161 bool newClipExists = newFlags & side;
162
163 // if current is clipped, we must be able to fit new bounds in current
164 if (boundsDelta > 0 && currentClipExists) return false;
165
166 // if new is clipped, we must be able to fit current bounds in new
167 if (boundsDelta < 0 && newClipExists) return false;
168
169 return true;
170 }
Chris Craik527a3aa2013-03-04 10:19:31 -0800171
172 /*
173 * Checks if a (mergeable) op can be merged into this batch
174 *
175 * If true, the op's multiDraw must be guaranteed to handle both ops simultaneously, so it is
176 * important to consider all paint attributes used in the draw calls in deciding both a) if an
Chris Craika02c4ed2013-06-14 13:43:58 -0700177 * 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 -0800178 *
179 * False positives can lead to information from the paints of subsequent merged operations being
180 * dropped, so we make simplifying qualifications on the ops that can merge, per op type.
181 */
Chris Craikc1c5f082013-09-11 16:23:37 -0700182 bool canMergeWith(const DrawOp* op, const DeferredDisplayState* state) {
Chris Craik527a3aa2013-03-04 10:19:31 -0800183 bool isTextBatch = getBatchId() == DeferredDisplayList::kOpBatch_Text ||
184 getBatchId() == DeferredDisplayList::kOpBatch_ColorText;
185
186 // Overlapping other operations is only allowed for text without shadow. For other ops,
187 // multiDraw isn't guaranteed to overdraw correctly
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -0400188 if (!isTextBatch || op->hasTextShadow()) {
Chris Craikc1c5f082013-09-11 16:23:37 -0700189 if (intersects(state->mBounds)) return false;
Chris Craik527a3aa2013-03-04 10:19:31 -0800190 }
Chris Craikc1c5f082013-09-11 16:23:37 -0700191 const DeferredDisplayState* lhs = state;
192 const DeferredDisplayState* rhs = mOps[0].state;
Chris Craik527a3aa2013-03-04 10:19:31 -0800193
Chris Craikdeeda3d2014-05-05 19:09:33 -0700194 if (!MathUtils::areEqual(lhs->mAlpha, rhs->mAlpha)) return false;
195
196 // Identical round rect clip state means both ops will clip in the same way, or not at all.
197 // As the state objects are const, we can compare their pointers to determine mergeability
198 if (lhs->mRoundRectClipState != rhs->mRoundRectClipState) return false;
Chris 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 Craik5af5fc52013-09-13 14:41:31 -0700239 virtual void add(DrawOp* op, const DeferredDisplayState* state, bool opaqueOverBounds) {
Chris Craikc1c5f082013-09-11 16:23:37 -0700240 DrawBatch::add(op, state, opaqueOverBounds);
Chris Craik28ce94a2013-05-31 11:38:03 -0700241
Chris Craikc1c5f082013-09-11 16:23:37 -0700242 const int newClipSideFlags = state->mClipSideFlags;
Chris Craik28ce94a2013-05-31 11:38:03 -0700243 mClipSideFlags |= newClipSideFlags;
Chris Craikc1c5f082013-09-11 16:23:37 -0700244 if (newClipSideFlags & kClipSide_Left) mClipRect.left = state->mClip.left;
245 if (newClipSideFlags & kClipSide_Top) mClipRect.top = state->mClip.top;
246 if (newClipSideFlags & kClipSide_Right) mClipRect.right = state->mClip.right;
247 if (newClipSideFlags & kClipSide_Bottom) mClipRect.bottom = state->mClip.bottom;
Chris Craik28ce94a2013-05-31 11:38:03 -0700248 }
249
Andreas Gampe64bb4132014-11-22 00:35:09 +0000250 virtual void replay(OpenGLRenderer& renderer, Rect& dirty, int index) {
Chris Craik28ce94a2013-05-31 11:38:03 -0700251 DEFER_LOGD("%d replaying MergingDrawBatch %p, with %d ops,"
252 " clip flags %x (batch id %x, merge id %p)",
253 index, this, mOps.size(), mClipSideFlags, getBatchId(), getMergeId());
Chris Craik527a3aa2013-03-04 10:19:31 -0800254 if (mOps.size() == 1) {
Tom Hudson107843d2014-09-08 11:26:26 -0400255 DrawBatch::replay(renderer, dirty, -1);
256 return;
Chris Craik527a3aa2013-03-04 10:19:31 -0800257 }
258
Chris Craik28ce94a2013-05-31 11:38:03 -0700259 // clipping in the merged case is done ahead of time since all ops share the clip (if any)
260 renderer.setupMergedMultiDraw(mClipSideFlags ? &mClipRect : NULL);
261
Chris Craikc1c5f082013-09-11 16:23:37 -0700262 DrawOp* op = mOps[0].op;
Chris Craik527a3aa2013-03-04 10:19:31 -0800263 DisplayListLogBuffer& buffer = DisplayListLogBuffer::getInstance();
264 buffer.writeCommand(0, "multiDraw");
265 buffer.writeCommand(1, op->name());
Chris Craikd965bc52013-09-16 14:47:13 -0700266
267#if DEBUG_DISPLAY_LIST_OPS_AS_EVENTS
268 renderer.eventMark("multiDraw");
269 renderer.eventMark(op->name());
270#endif
Tom Hudson107843d2014-09-08 11:26:26 -0400271 op->multiDraw(renderer, dirty, mOps, mBounds);
Chris Craik527a3aa2013-03-04 10:19:31 -0800272
273#if DEBUG_MERGE_BEHAVIOR
274 renderer.drawScreenSpaceColorRect(mBounds.left, mBounds.top, mBounds.right, mBounds.bottom,
275 DEBUG_COLOR_MERGEDBATCH);
276#endif
Chris Craik527a3aa2013-03-04 10:19:31 -0800277 }
Chris Craik28ce94a2013-05-31 11:38:03 -0700278
279private:
280 /*
281 * Contains the effective clip rect shared by all merged ops. Initialized to the layer viewport,
282 * it will shrink if an op must be clipped on a certain side. The clipped sides are reflected in
283 * mClipSideFlags.
284 */
285 Rect mClipRect;
286 int mClipSideFlags;
Chris Craik527a3aa2013-03-04 10:19:31 -0800287};
288
289class StateOpBatch : public Batch {
Chris Craikff785832013-03-08 13:12:16 -0800290public:
291 // creates a single operation batch
Chris Craikc1c5f082013-09-11 16:23:37 -0700292 StateOpBatch(const StateOp* op, const DeferredDisplayState* state) : mOp(op), mState(state) {}
Chris Craikff785832013-03-08 13:12:16 -0800293
Andreas Gampe64bb4132014-11-22 00:35:09 +0000294 virtual void replay(OpenGLRenderer& renderer, Rect& dirty, int index) {
Chris Craikff785832013-03-08 13:12:16 -0800295 DEFER_LOGD("replaying state op batch %p", this);
Chris Craikc1c5f082013-09-11 16:23:37 -0700296 renderer.restoreDisplayState(*mState);
Chris Craikff785832013-03-08 13:12:16 -0800297
298 // use invalid save count because it won't be used at flush time - RestoreToCountOp is the
299 // only one to use it, and we don't use that class at flush time, instead calling
300 // renderer.restoreToCount directly
301 int saveCount = -1;
302 mOp->applyState(renderer, saveCount);
Chris Craikff785832013-03-08 13:12:16 -0800303 }
304
305private:
Chris Craik7273daa2013-03-28 11:25:24 -0700306 const StateOp* mOp;
Chris Craikc1c5f082013-09-11 16:23:37 -0700307 const DeferredDisplayState* mState;
Chris Craikff785832013-03-08 13:12:16 -0800308};
309
Chris Craik527a3aa2013-03-04 10:19:31 -0800310class RestoreToCountBatch : public Batch {
Chris Craikff785832013-03-08 13:12:16 -0800311public:
Andreas Gampe64bb4132014-11-22 00:35:09 +0000312 RestoreToCountBatch(const StateOp* op, const DeferredDisplayState* state, int restoreCount) :
313 mState(state), mRestoreCount(restoreCount) {}
Chris Craikff785832013-03-08 13:12:16 -0800314
Andreas Gampe64bb4132014-11-22 00:35:09 +0000315 virtual void replay(OpenGLRenderer& renderer, Rect& dirty, int index) {
Chris Craikff785832013-03-08 13:12:16 -0800316 DEFER_LOGD("batch %p restoring to count %d", this, mRestoreCount);
Chris Craik7273daa2013-03-28 11:25:24 -0700317
Chris Craikc1c5f082013-09-11 16:23:37 -0700318 renderer.restoreDisplayState(*mState);
Chris Craikff785832013-03-08 13:12:16 -0800319 renderer.restoreToCount(mRestoreCount);
Chris Craikff785832013-03-08 13:12:16 -0800320 }
321
322private:
Chris Craik7273daa2013-03-28 11:25:24 -0700323 // we use the state storage for the RestoreToCountOp, but don't replay the op itself
Chris Craikc1c5f082013-09-11 16:23:37 -0700324 const DeferredDisplayState* mState;
325
Chris Craikff785832013-03-08 13:12:16 -0800326 /*
327 * The count used here represents the flush() time saveCount. This is as opposed to the
328 * DisplayList record time, or defer() time values (which are RestoreToCountOp's mCount, and
329 * (saveCount + mCount) respectively). Since the count is different from the original
330 * RestoreToCountOp, we don't store a pointer to the op, as elsewhere.
331 */
332 const int mRestoreCount;
333};
334
Chris Craik527a3aa2013-03-04 10:19:31 -0800335#if DEBUG_MERGE_BEHAVIOR
336class BarrierDebugBatch : public Batch {
Tom Hudson107843d2014-09-08 11:26:26 -0400337 virtual void replay(OpenGLRenderer& renderer, Rect& dirty, int index) {
Chris Craik527a3aa2013-03-04 10:19:31 -0800338 renderer.drawScreenSpaceColorRect(0, 0, 10000, 10000, DEBUG_COLOR_BARRIER);
Chris Craik527a3aa2013-03-04 10:19:31 -0800339 }
340};
341#endif
342
Chris Craikff785832013-03-08 13:12:16 -0800343/////////////////////////////////////////////////////////////////////////////////
344// DeferredDisplayList
345/////////////////////////////////////////////////////////////////////////////////
346
347void DeferredDisplayList::resetBatchingState() {
Chris Craikc3566d02013-02-04 16:16:33 -0800348 for (int i = 0; i < kOpBatch_Count; i++) {
Chris Craik527a3aa2013-03-04 10:19:31 -0800349 mBatchLookup[i] = NULL;
350 mMergingBatches[i].clear();
Chris Craikc3566d02013-02-04 16:16:33 -0800351 }
Chris Craik527a3aa2013-03-04 10:19:31 -0800352#if DEBUG_MERGE_BEHAVIOR
353 if (mBatches.size() != 0) {
354 mBatches.add(new BarrierDebugBatch());
355 }
356#endif
357 mEarliestBatchIndex = mBatches.size();
Chris Craikff785832013-03-08 13:12:16 -0800358}
359
360void DeferredDisplayList::clear() {
361 resetBatchingState();
362 mComplexClipStackStart = -1;
363
Chris Craikc3566d02013-02-04 16:16:33 -0800364 for (unsigned int i = 0; i < mBatches.size(); i++) {
365 delete mBatches[i];
366 }
367 mBatches.clear();
Chris Craikff785832013-03-08 13:12:16 -0800368 mSaveStack.clear();
Chris Craik527a3aa2013-03-04 10:19:31 -0800369 mEarliestBatchIndex = 0;
Chris Craik28ce94a2013-05-31 11:38:03 -0700370 mEarliestUnclearedIndex = 0;
Chris Craikc3566d02013-02-04 16:16:33 -0800371}
372
Chris Craikff785832013-03-08 13:12:16 -0800373/////////////////////////////////////////////////////////////////////////////////
374// Operation adding
375/////////////////////////////////////////////////////////////////////////////////
376
377int DeferredDisplayList::getStateOpDeferFlags() const {
378 // For both clipOp and save(Layer)Op, we don't want to save drawing info, and only want to save
379 // the clip if we aren't recording a complex clip (and can thus trust it to be a rect)
380 return recordingComplexClip() ? 0 : kStateDeferFlag_Clip;
381}
382
383int DeferredDisplayList::getDrawOpDeferFlags() const {
384 return kStateDeferFlag_Draw | getStateOpDeferFlags();
385}
386
387/**
388 * When an clipping operation occurs that could cause a complex clip, record the operation and all
389 * subsequent clipOps, save/restores (if the clip flag is set). During a flush, instead of loading
390 * the clip from deferred state, we play back all of the relevant state operations that generated
391 * the complex clip.
392 *
393 * Note that we don't need to record the associated restore operation, since operations at defer
394 * time record whether they should store the renderer's current clip
395 */
396void DeferredDisplayList::addClip(OpenGLRenderer& renderer, ClipOp* op) {
397 if (recordingComplexClip() || op->canCauseComplexClip() || !renderer.hasRectToRectTransform()) {
398 DEFER_LOGD("%p Received complex clip operation %p", this, op);
399
400 // NOTE: defer clip op before setting mComplexClipStackStart so previous clip is recorded
401 storeStateOpBarrier(renderer, op);
402
403 if (!recordingComplexClip()) {
404 mComplexClipStackStart = renderer.getSaveCount() - 1;
405 DEFER_LOGD(" Starting complex clip region, start is %d", mComplexClipStackStart);
Chris Craikc3566d02013-02-04 16:16:33 -0800406 }
Chris Craikff785832013-03-08 13:12:16 -0800407 }
408}
409
410/**
411 * For now, we record save layer operations as barriers in the batch list, preventing drawing
412 * operations from reordering around the saveLayer and it's associated restore()
413 *
414 * In the future, we should send saveLayer commands (if they can be played out of order) and their
415 * contained drawing operations to a seperate list of batches, so that they may draw at the
416 * beginning of the frame. This would avoid targetting and removing an FBO in the middle of a frame.
417 *
418 * saveLayer operations should be pulled to the beginning of the frame if the canvas doesn't have a
419 * complex clip, and if the flags (kClip_SaveFlag & kClipToLayer_SaveFlag) are set.
420 */
421void DeferredDisplayList::addSaveLayer(OpenGLRenderer& renderer,
422 SaveLayerOp* op, int newSaveCount) {
423 DEFER_LOGD("%p adding saveLayerOp %p, flags %x, new count %d",
424 this, op, op->getFlags(), newSaveCount);
425
426 storeStateOpBarrier(renderer, op);
427 mSaveStack.push(newSaveCount);
428}
429
430/**
431 * Takes save op and it's return value - the new save count - and stores it into the stream as a
432 * barrier if it's needed to properly modify a complex clip
433 */
434void DeferredDisplayList::addSave(OpenGLRenderer& renderer, SaveOp* op, int newSaveCount) {
435 int saveFlags = op->getFlags();
436 DEFER_LOGD("%p adding saveOp %p, flags %x, new count %d", this, op, saveFlags, newSaveCount);
437
438 if (recordingComplexClip() && (saveFlags & SkCanvas::kClip_SaveFlag)) {
439 // store and replay the save operation, as it may be needed to correctly playback the clip
440 DEFER_LOGD(" adding save barrier with new save count %d", newSaveCount);
441 storeStateOpBarrier(renderer, op);
442 mSaveStack.push(newSaveCount);
443 }
444}
445
446/**
447 * saveLayer() commands must be associated with a restoreToCount batch that will clean up and draw
448 * the layer in the deferred list
449 *
450 * other save() commands which occur as children of a snapshot with complex clip will be deferred,
451 * and must be restored
452 *
453 * Either will act as a barrier to draw operation reordering, as we want to play back layer
454 * save/restore and complex canvas modifications (including save/restore) in order.
455 */
Chris Craik7273daa2013-03-28 11:25:24 -0700456void DeferredDisplayList::addRestoreToCount(OpenGLRenderer& renderer, StateOp* op,
457 int newSaveCount) {
Chris Craikff785832013-03-08 13:12:16 -0800458 DEFER_LOGD("%p addRestoreToCount %d", this, newSaveCount);
459
460 if (recordingComplexClip() && newSaveCount <= mComplexClipStackStart) {
461 mComplexClipStackStart = -1;
462 resetBatchingState();
463 }
464
465 if (mSaveStack.isEmpty() || newSaveCount > mSaveStack.top()) {
466 return;
467 }
468
469 while (!mSaveStack.isEmpty() && mSaveStack.top() >= newSaveCount) mSaveStack.pop();
470
Chris Craik1ed30c92013-04-03 12:37:35 -0700471 storeRestoreToCountBarrier(renderer, op, mSaveStack.size() + FLUSH_SAVE_STACK_DEPTH);
Chris Craikff785832013-03-08 13:12:16 -0800472}
473
474void DeferredDisplayList::addDrawOp(OpenGLRenderer& renderer, DrawOp* op) {
Chris Craikc1c5f082013-09-11 16:23:37 -0700475 /* 1: op calculates local bounds */
476 DeferredDisplayState* const state = createState();
John Reck3b202512014-06-23 13:13:08 -0700477 if (op->getLocalBounds(state->mBounds)) {
Chris Craikc1c5f082013-09-11 16:23:37 -0700478 if (state->mBounds.isEmpty()) {
479 // valid empty bounds, don't bother deferring
480 tryRecycleState(state);
481 return;
482 }
483 } else {
484 state->mBounds.setEmpty();
485 }
486
487 /* 2: renderer calculates global bounds + stores state */
488 if (renderer.storeDisplayState(*state, getDrawOpDeferFlags())) {
489 tryRecycleState(state);
Chris Craikff785832013-03-08 13:12:16 -0800490 return; // quick rejected
491 }
492
Chris Craikc1c5f082013-09-11 16:23:37 -0700493 /* 3: ask op for defer info, given renderer state */
Chris Craik28ce94a2013-05-31 11:38:03 -0700494 DeferInfo deferInfo;
Chris Craikc1c5f082013-09-11 16:23:37 -0700495 op->onDefer(renderer, deferInfo, *state);
Chris Craik527a3aa2013-03-04 10:19:31 -0800496
497 // complex clip has a complex set of expectations on the renderer state - for now, avoid taking
498 // the merge path in those cases
Chris Craik28ce94a2013-05-31 11:38:03 -0700499 deferInfo.mergeable &= !recordingComplexClip();
Chris Craik0e87f002013-06-19 16:54:59 -0700500 deferInfo.opaqueOverBounds &= !recordingComplexClip() && mSaveStack.isEmpty();
Chris Craik28ce94a2013-05-31 11:38:03 -0700501
Chris Craikef8d6f22014-12-17 11:10:28 -0800502 if (CC_LIKELY(mAvoidOverdraw) && mBatches.size() &&
Chris Craikc1c5f082013-09-11 16:23:37 -0700503 state->mClipSideFlags != kClipSide_ConservativeFull &&
504 deferInfo.opaqueOverBounds && state->mBounds.contains(mBounds)) {
Chris Craikf70119c2013-06-13 11:21:22 -0700505 // avoid overdraw by resetting drawing state + discarding drawing ops
Chris Craik28ce94a2013-05-31 11:38:03 -0700506 discardDrawingBatches(mBatches.size() - 1);
Chris Craikf70119c2013-06-13 11:21:22 -0700507 resetBatchingState();
Chris Craik28ce94a2013-05-31 11:38:03 -0700508 }
Chris Craikff785832013-03-08 13:12:16 -0800509
510 if (CC_UNLIKELY(renderer.getCaches().drawReorderDisabled)) {
511 // TODO: elegant way to reuse batches?
Chris Craik28ce94a2013-05-31 11:38:03 -0700512 DrawBatch* b = new DrawBatch(deferInfo);
Chris Craikc1c5f082013-09-11 16:23:37 -0700513 b->add(op, state, deferInfo.opaqueOverBounds);
Chris Craikc3566d02013-02-04 16:16:33 -0800514 mBatches.add(b);
515 return;
516 }
517
Chris Craik527a3aa2013-03-04 10:19:31 -0800518 // find the latest batch of the new op's type, and try to merge the new op into it
519 DrawBatch* targetBatch = NULL;
Chris Craikc3566d02013-02-04 16:16:33 -0800520
Chris Craik527a3aa2013-03-04 10:19:31 -0800521 // insertion point of a new batch, will hopefully be immediately after similar batch
522 // (eventually, should be similar shader)
523 int insertBatchIndex = mBatches.size();
Chris Craikc3566d02013-02-04 16:16:33 -0800524 if (!mBatches.isEmpty()) {
Chris Craikc1c5f082013-09-11 16:23:37 -0700525 if (state->mBounds.isEmpty()) {
Chris Craikc3566d02013-02-04 16:16:33 -0800526 // 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 -0700527 DrawBatch* b = new DrawBatch(deferInfo);
Chris Craikc1c5f082013-09-11 16:23:37 -0700528 b->add(op, state, deferInfo.opaqueOverBounds);
Chris Craik527a3aa2013-03-04 10:19:31 -0800529 mBatches.add(b);
530 resetBatchingState();
Chris Craikc3566d02013-02-04 16:16:33 -0800531#if DEBUG_DEFER
532 DEFER_LOGD("Warning: Encountered op with empty bounds, resetting batches");
533 op->output(2);
534#endif
535 return;
536 }
537
Chris Craik28ce94a2013-05-31 11:38:03 -0700538 if (deferInfo.mergeable) {
Chris Craik527a3aa2013-03-04 10:19:31 -0800539 // Try to merge with any existing batch with same mergeId.
Chris Craik28ce94a2013-05-31 11:38:03 -0700540 if (mMergingBatches[deferInfo.batchId].get(deferInfo.mergeId, targetBatch)) {
Chris Craikc1c5f082013-09-11 16:23:37 -0700541 if (!((MergingDrawBatch*) targetBatch)->canMergeWith(op, state)) {
Chris Craik527a3aa2013-03-04 10:19:31 -0800542 targetBatch = NULL;
543 }
544 }
545 } else {
546 // join with similar, non-merging batch
Chris Craik28ce94a2013-05-31 11:38:03 -0700547 targetBatch = (DrawBatch*)mBatchLookup[deferInfo.batchId];
Chris Craik527a3aa2013-03-04 10:19:31 -0800548 }
549
Chris Craik28ce94a2013-05-31 11:38:03 -0700550 if (targetBatch || deferInfo.mergeable) {
Chris Craikc3566d02013-02-04 16:16:33 -0800551 // iterate back toward target to see if anything drawn since should overlap the new op
Chris Craik527a3aa2013-03-04 10:19:31 -0800552 // if no target, merging ops still interate to find similar batch to insert after
553 for (int i = mBatches.size() - 1; i >= mEarliestBatchIndex; i--) {
554 DrawBatch* overBatch = (DrawBatch*)mBatches[i];
555
556 if (overBatch == targetBatch) break;
557
558 // TODO: also consider shader shared between batch types
Chris Craik28ce94a2013-05-31 11:38:03 -0700559 if (deferInfo.batchId == overBatch->getBatchId()) {
Chris Craik527a3aa2013-03-04 10:19:31 -0800560 insertBatchIndex = i + 1;
561 if (!targetBatch) break; // found insert position, quit
562 }
563
Chris Craikc1c5f082013-09-11 16:23:37 -0700564 if (overBatch->intersects(state->mBounds)) {
Chris Craik527a3aa2013-03-04 10:19:31 -0800565 // NOTE: it may be possible to optimize for special cases where two operations
566 // of the same batch/paint could swap order, such as with a non-mergeable
567 // (clipped) and a mergeable text operation
Chris Craikc3566d02013-02-04 16:16:33 -0800568 targetBatch = NULL;
569#if DEBUG_DEFER
Chris Craikc1c5f082013-09-11 16:23:37 -0700570 DEFER_LOGD("op couldn't join batch %p, was intersected by batch %d",
571 targetBatch, i);
Chris Craikc3566d02013-02-04 16:16:33 -0800572 op->output(2);
573#endif
574 break;
575 }
576 }
577 }
578 }
Chris Craik527a3aa2013-03-04 10:19:31 -0800579
Chris Craikc3566d02013-02-04 16:16:33 -0800580 if (!targetBatch) {
Chris Craik28ce94a2013-05-31 11:38:03 -0700581 if (deferInfo.mergeable) {
Chris Craik0e87f002013-06-19 16:54:59 -0700582 targetBatch = new MergingDrawBatch(deferInfo,
583 renderer.getViewportWidth(), renderer.getViewportHeight());
Chris Craik28ce94a2013-05-31 11:38:03 -0700584 mMergingBatches[deferInfo.batchId].put(deferInfo.mergeId, targetBatch);
Chris Craik527a3aa2013-03-04 10:19:31 -0800585 } else {
Chris Craik28ce94a2013-05-31 11:38:03 -0700586 targetBatch = new DrawBatch(deferInfo);
587 mBatchLookup[deferInfo.batchId] = targetBatch;
Chris Craikc3566d02013-02-04 16:16:33 -0800588 }
Chris Craik527a3aa2013-03-04 10:19:31 -0800589
Chris Craikf70119c2013-06-13 11:21:22 -0700590 DEFER_LOGD("creating %singBatch %p, bid %x, at %d",
591 deferInfo.mergeable ? "Merg" : "Draw",
592 targetBatch, deferInfo.batchId, insertBatchIndex);
Chris Craik527a3aa2013-03-04 10:19:31 -0800593 mBatches.insertAt(targetBatch, insertBatchIndex);
Chris Craikc3566d02013-02-04 16:16:33 -0800594 }
Chris Craik527a3aa2013-03-04 10:19:31 -0800595
Chris Craikc1c5f082013-09-11 16:23:37 -0700596 targetBatch->add(op, state, deferInfo.opaqueOverBounds);
Chris Craikc3566d02013-02-04 16:16:33 -0800597}
598
Chris Craikff785832013-03-08 13:12:16 -0800599void DeferredDisplayList::storeStateOpBarrier(OpenGLRenderer& renderer, StateOp* op) {
600 DEFER_LOGD("%p adding state op barrier at pos %d", this, mBatches.size());
601
Chris Craikc1c5f082013-09-11 16:23:37 -0700602 DeferredDisplayState* state = createState();
603 renderer.storeDisplayState(*state, getStateOpDeferFlags());
604 mBatches.add(new StateOpBatch(op, state));
Chris Craikff785832013-03-08 13:12:16 -0800605 resetBatchingState();
606}
607
Chris Craik7273daa2013-03-28 11:25:24 -0700608void DeferredDisplayList::storeRestoreToCountBarrier(OpenGLRenderer& renderer, StateOp* op,
609 int newSaveCount) {
Chris Craikff785832013-03-08 13:12:16 -0800610 DEFER_LOGD("%p adding restore to count %d barrier, pos %d",
611 this, newSaveCount, mBatches.size());
612
Chris Craik7273daa2013-03-28 11:25:24 -0700613 // store displayState for the restore operation, as it may be associated with a saveLayer that
614 // doesn't have kClip_SaveFlag set
Chris Craikc1c5f082013-09-11 16:23:37 -0700615 DeferredDisplayState* state = createState();
616 renderer.storeDisplayState(*state, getStateOpDeferFlags());
617 mBatches.add(new RestoreToCountBatch(op, state, newSaveCount));
Chris Craikff785832013-03-08 13:12:16 -0800618 resetBatchingState();
619}
620
621/////////////////////////////////////////////////////////////////////////////////
622// Replay / flush
623/////////////////////////////////////////////////////////////////////////////////
624
Tom Hudson107843d2014-09-08 11:26:26 -0400625static void replayBatchList(const Vector<Batch*>& batchList,
Chris Craikff785832013-03-08 13:12:16 -0800626 OpenGLRenderer& renderer, Rect& dirty) {
Chris Craikff785832013-03-08 13:12:16 -0800627
Chris Craikff785832013-03-08 13:12:16 -0800628 for (unsigned int i = 0; i < batchList.size(); i++) {
Chris Craik28ce94a2013-05-31 11:38:03 -0700629 if (batchList[i]) {
Tom Hudson107843d2014-09-08 11:26:26 -0400630 batchList[i]->replay(renderer, dirty, i);
Chris Craik28ce94a2013-05-31 11:38:03 -0700631 }
Chris Craikff785832013-03-08 13:12:16 -0800632 }
Chris Craik527a3aa2013-03-04 10:19:31 -0800633 DEFER_LOGD("--flushed, drew %d batches", batchList.size());
Chris Craikff785832013-03-08 13:12:16 -0800634}
635
Tom Hudson107843d2014-09-08 11:26:26 -0400636void DeferredDisplayList::flush(OpenGLRenderer& renderer, Rect& dirty) {
Chris Craikff785832013-03-08 13:12:16 -0800637 ATRACE_NAME("flush drawing commands");
Romain Guycf51a412013-04-08 19:40:31 -0700638 Caches::getInstance().fontRenderer->endPrecaching();
639
Tom Hudson107843d2014-09-08 11:26:26 -0400640 if (isEmpty()) return; // nothing to flush
Chris Craika4e16c52013-03-22 10:00:48 -0700641 renderer.restoreToCount(1);
Chris Craikc3566d02013-02-04 16:16:33 -0800642
643 DEFER_LOGD("--flushing");
Romain Guy0f6675332013-03-01 14:31:04 -0800644 renderer.eventMark("Flush");
645
Chris Craika4e16c52013-03-22 10:00:48 -0700646 // save and restore (with draw modifiers) so that reordering doesn't affect final state
Chris Craikd90144d2013-03-19 15:03:48 -0700647 DrawModifiers restoreDrawModifiers = renderer.getDrawModifiers();
Chris Craika4e16c52013-03-22 10:00:48 -0700648 renderer.save(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
649
Chris Craikef8d6f22014-12-17 11:10:28 -0800650 if (CC_LIKELY(mAvoidOverdraw)) {
651 for (unsigned int i = 1; i < mBatches.size(); i++) {
652 if (mBatches[i] && mBatches[i]->coversBounds(mBounds)) {
653 discardDrawingBatches(i - 1);
654 }
Chris Craik28ce94a2013-05-31 11:38:03 -0700655 }
656 }
Chris Craik1ed30c92013-04-03 12:37:35 -0700657 // NOTE: depth of the save stack at this point, before playback, should be reflected in
658 // FLUSH_SAVE_STACK_DEPTH, so that save/restores match up correctly
Tom Hudson107843d2014-09-08 11:26:26 -0400659 replayBatchList(mBatches, renderer, dirty);
Chris Craika4e16c52013-03-22 10:00:48 -0700660
661 renderer.restoreToCount(1);
Chris Craikd90144d2013-03-19 15:03:48 -0700662 renderer.setDrawModifiers(restoreDrawModifiers);
Chris Craikc3566d02013-02-04 16:16:33 -0800663
Chris Craikff785832013-03-08 13:12:16 -0800664 DEFER_LOGD("--flush complete, returning %x", status);
Chris Craikc3566d02013-02-04 16:16:33 -0800665 clear();
Chris Craikc3566d02013-02-04 16:16:33 -0800666}
667
Chris Craikf70119c2013-06-13 11:21:22 -0700668void DeferredDisplayList::discardDrawingBatches(const unsigned int maxIndex) {
Chris Craik28ce94a2013-05-31 11:38:03 -0700669 for (unsigned int i = mEarliestUnclearedIndex; i <= maxIndex; i++) {
Chris Craikf70119c2013-06-13 11:21:22 -0700670 // leave deferred state ops alone for simplicity (empty save restore pairs may now exist)
Chris Craik28ce94a2013-05-31 11:38:03 -0700671 if (mBatches[i] && mBatches[i]->purelyDrawBatch()) {
Chris Craik28ce94a2013-05-31 11:38:03 -0700672 delete mBatches[i];
673 mBatches.replaceAt(NULL, i);
674 }
675 }
676 mEarliestUnclearedIndex = maxIndex + 1;
677}
678
Chris Craikc3566d02013-02-04 16:16:33 -0800679}; // namespace uirenderer
680}; // namespace android