blob: 689179dd8fb43b02f0424fc50b730d7ef9ffd03e [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
Chris Craikc3566d02013-02-04 16:16:33 -080017#include <utils/Trace.h>
Chris Craik28ce94a2013-05-31 11:38:03 -070018#include <ui/Rect.h>
19#include <ui/Region.h>
Chris Craikc3566d02013-02-04 16:16:33 -080020
Romain Guycf51a412013-04-08 19:40:31 -070021#include "Caches.h"
Chris Craikc3566d02013-02-04 16:16:33 -080022#include "Debug.h"
Chris Craik527a3aa2013-03-04 10:19:31 -080023#include "DeferredDisplayList.h"
Chris Craikc3566d02013-02-04 16:16:33 -080024#include "DisplayListOp.h"
25#include "OpenGLRenderer.h"
Chris Craik2507c342015-05-04 14:36:49 -070026#include "Properties.h"
Chris Craikdeeda3d2014-05-05 19:09:33 -070027#include "utils/MathUtils.h"
Chris Craikc3566d02013-02-04 16:16:33 -080028
29#if DEBUG_DEFER
30 #define DEFER_LOGD(...) ALOGD(__VA_ARGS__)
31#else
32 #define DEFER_LOGD(...)
33#endif
34
35namespace android {
36namespace uirenderer {
37
Chris Craik1ed30c92013-04-03 12:37:35 -070038// Depth of the save stack at the beginning of batch playback at flush time
39#define FLUSH_SAVE_STACK_DEPTH 2
40
Chris Craik527a3aa2013-03-04 10:19:31 -080041#define DEBUG_COLOR_BARRIER 0x1f000000
42#define DEBUG_COLOR_MERGEDBATCH 0x5f7f7fff
43#define DEBUG_COLOR_MERGEDBATCH_SOLO 0x5f7fff7f
44
Chris Craikb45c6aa2015-09-28 15:41:27 -070045static bool avoidOverdraw() {
46 // Don't avoid overdraw when visualizing it, since that makes it harder to
47 // debug where it's coming from, and when the problem occurs.
48 return !Properties::debugOverdraw;
49};
50
Chris Craikff785832013-03-08 13:12:16 -080051/////////////////////////////////////////////////////////////////////////////////
52// Operation Batches
53/////////////////////////////////////////////////////////////////////////////////
54
Chris Craik527a3aa2013-03-04 10:19:31 -080055class Batch {
Chris Craikc3566d02013-02-04 16:16:33 -080056public:
Tom Hudson107843d2014-09-08 11:26:26 -040057 virtual void replay(OpenGLRenderer& renderer, Rect& dirty, int index) = 0;
Chris Craik527a3aa2013-03-04 10:19:31 -080058 virtual ~Batch() {}
Chris Craik28ce94a2013-05-31 11:38:03 -070059 virtual bool purelyDrawBatch() { return false; }
Andreas Gampe64bb4132014-11-22 00:35:09 +000060 virtual bool coversBounds(const Rect& bounds) { return false; }
Chris Craik527a3aa2013-03-04 10:19:31 -080061};
Chris Craikc3566d02013-02-04 16:16:33 -080062
Chris Craik527a3aa2013-03-04 10:19:31 -080063class DrawBatch : public Batch {
64public:
Chih-Hung Hsiehc6baf562016-04-27 11:29:23 -070065 explicit DrawBatch(const DeferInfo& deferInfo) : mAllOpsOpaque(true),
Chris Craik28ce94a2013-05-31 11:38:03 -070066 mBatchId(deferInfo.batchId), mMergeId(deferInfo.mergeId) {
Chris Craik527a3aa2013-03-04 10:19:31 -080067 mOps.clear();
68 }
69
70 virtual ~DrawBatch() { mOps.clear(); }
Chris Craikc3566d02013-02-04 16:16:33 -080071
Chris Craikc1c5f082013-09-11 16:23:37 -070072 virtual void add(DrawOp* op, const DeferredDisplayState* state, bool opaqueOverBounds) {
Chris Craikc3566d02013-02-04 16:16:33 -080073 // NOTE: ignore empty bounds special case, since we don't merge across those ops
Chris Craikc1c5f082013-09-11 16:23:37 -070074 mBounds.unionWith(state->mBounds);
Chris Craik28ce94a2013-05-31 11:38:03 -070075 mAllOpsOpaque &= opaqueOverBounds;
John Reck272a6852015-07-29 16:48:58 -070076 mOps.push_back(OpStatePair(op, state));
Chris Craikc3566d02013-02-04 16:16:33 -080077 }
78
Chris Craikc1c5f082013-09-11 16:23:37 -070079 bool intersects(const Rect& rect) {
Chris Craikc3566d02013-02-04 16:16:33 -080080 if (!rect.intersects(mBounds)) return false;
Chris Craikff785832013-03-08 13:12:16 -080081
Chris Craikc3566d02013-02-04 16:16:33 -080082 for (unsigned int i = 0; i < mOps.size(); i++) {
Chris Craikc1c5f082013-09-11 16:23:37 -070083 if (rect.intersects(mOps[i].state->mBounds)) {
Chris Craikc3566d02013-02-04 16:16:33 -080084#if DEBUG_DEFER
Chris Craikc1c5f082013-09-11 16:23:37 -070085 DEFER_LOGD("op intersects with op %p with bounds %f %f %f %f:", mOps[i].op,
86 mOps[i].state->mBounds.left, mOps[i].state->mBounds.top,
87 mOps[i].state->mBounds.right, mOps[i].state->mBounds.bottom);
88 mOps[i].op->output(2);
Chris Craikc3566d02013-02-04 16:16:33 -080089#endif
90 return true;
91 }
92 }
93 return false;
94 }
95
Chris Craikd41c4d82015-01-05 15:51:13 -080096 virtual void replay(OpenGLRenderer& renderer, Rect& dirty, int index) override {
Chris Craik41541822013-05-03 16:35:54 -070097 DEFER_LOGD("%d replaying DrawBatch %p, with %d ops (batch id %x, merge id %p)",
98 index, this, mOps.size(), getBatchId(), getMergeId());
Chris Craikff785832013-03-08 13:12:16 -080099
Chris Craikff785832013-03-08 13:12:16 -0800100 for (unsigned int i = 0; i < mOps.size(); i++) {
Chris Craikc1c5f082013-09-11 16:23:37 -0700101 DrawOp* op = mOps[i].op;
102 const DeferredDisplayState* state = mOps[i].state;
103 renderer.restoreDisplayState(*state);
Chris Craikff785832013-03-08 13:12:16 -0800104
105#if DEBUG_DISPLAY_LIST_OPS_AS_EVENTS
Chris Craikd90144d2013-03-19 15:03:48 -0700106 renderer.eventMark(op->name());
Chris Craikff785832013-03-08 13:12:16 -0800107#endif
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 Craikd41c4d82015-01-05 15:51:13 -0800122 virtual bool purelyDrawBatch() override { return true; }
Chris Craik28ce94a2013-05-31 11:38:03 -0700123
Chris Craikd41c4d82015-01-05 15:51:13 -0800124 virtual bool coversBounds(const Rect& bounds) override {
Chris Craik28ce94a2013-05-31 11:38:03 -0700125 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:
John Reck272a6852015-07-29 16:48:58 -0700140 std::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 Craikfca52b752015-04-28 11:45:59 -0700199 if (lhs->mProjectionPathMask != rhs->mProjectionPathMask) return false;
Chris Craik527a3aa2013-03-04 10:19:31 -0800200
Chris Craika02c4ed2013-06-14 13:43:58 -0700201 /* Clipping compatibility check
202 *
203 * Exploits the fact that if a op or batch is clipped on a side, its bounds will equal its
204 * clip for that side.
205 */
206 const int currentFlags = mClipSideFlags;
Chris Craikc1c5f082013-09-11 16:23:37 -0700207 const int newFlags = state->mClipSideFlags;
Chris Craika02c4ed2013-06-14 13:43:58 -0700208 if (currentFlags != kClipSide_None || newFlags != kClipSide_None) {
Chris Craikc1c5f082013-09-11 16:23:37 -0700209 const Rect& opBounds = state->mBounds;
Chris Craika02c4ed2013-06-14 13:43:58 -0700210 float boundsDelta = mBounds.left - opBounds.left;
211 if (!checkSide(currentFlags, newFlags, kClipSide_Left, boundsDelta)) return false;
212 boundsDelta = mBounds.top - opBounds.top;
213 if (!checkSide(currentFlags, newFlags, kClipSide_Top, boundsDelta)) return false;
214
215 // right and bottom delta calculation reversed to account for direction
216 boundsDelta = opBounds.right - mBounds.right;
217 if (!checkSide(currentFlags, newFlags, kClipSide_Right, boundsDelta)) return false;
218 boundsDelta = opBounds.bottom - mBounds.bottom;
219 if (!checkSide(currentFlags, newFlags, kClipSide_Bottom, boundsDelta)) return false;
Chris Craik28ce94a2013-05-31 11:38:03 -0700220 }
Chris Craik28ce94a2013-05-31 11:38:03 -0700221
Chris Craik527a3aa2013-03-04 10:19:31 -0800222 // if paints are equal, then modifiers + paint attribs don't need to be compared
Chris Craikc1c5f082013-09-11 16:23:37 -0700223 if (op->mPaint == mOps[0].op->mPaint) return true;
Chris Craik527a3aa2013-03-04 10:19:31 -0800224
Chris Craikbf6f0f22015-10-01 12:36:07 -0700225 if (PaintUtils::getAlphaDirect(op->mPaint)
226 != PaintUtils::getAlphaDirect(mOps[0].op->mPaint)) {
227 return false;
228 }
Chris Craik527a3aa2013-03-04 10:19:31 -0800229
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500230 if (op->mPaint && mOps[0].op->mPaint &&
231 op->mPaint->getColorFilter() != mOps[0].op->mPaint->getColorFilter()) {
232 return false;
233 }
234
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400235 if (op->mPaint && mOps[0].op->mPaint &&
236 op->mPaint->getShader() != mOps[0].op->mPaint->getShader()) {
237 return false;
238 }
239
Chris Craik527a3aa2013-03-04 10:19:31 -0800240 return true;
241 }
242
Chris Craikd41c4d82015-01-05 15:51:13 -0800243 virtual void add(DrawOp* op, const DeferredDisplayState* state,
244 bool opaqueOverBounds) override {
Chris Craikc1c5f082013-09-11 16:23:37 -0700245 DrawBatch::add(op, state, opaqueOverBounds);
Chris Craik28ce94a2013-05-31 11:38:03 -0700246
Chris Craikc1c5f082013-09-11 16:23:37 -0700247 const int newClipSideFlags = state->mClipSideFlags;
Chris Craik28ce94a2013-05-31 11:38:03 -0700248 mClipSideFlags |= newClipSideFlags;
Chris Craikc1c5f082013-09-11 16:23:37 -0700249 if (newClipSideFlags & kClipSide_Left) mClipRect.left = state->mClip.left;
250 if (newClipSideFlags & kClipSide_Top) mClipRect.top = state->mClip.top;
251 if (newClipSideFlags & kClipSide_Right) mClipRect.right = state->mClip.right;
252 if (newClipSideFlags & kClipSide_Bottom) mClipRect.bottom = state->mClip.bottom;
Chris Craik28ce94a2013-05-31 11:38:03 -0700253 }
254
Chris Craikd41c4d82015-01-05 15:51:13 -0800255 virtual void replay(OpenGLRenderer& renderer, Rect& dirty, int index) override {
Chris Craik28ce94a2013-05-31 11:38:03 -0700256 DEFER_LOGD("%d replaying MergingDrawBatch %p, with %d ops,"
257 " clip flags %x (batch id %x, merge id %p)",
258 index, this, mOps.size(), mClipSideFlags, getBatchId(), getMergeId());
Chris Craik527a3aa2013-03-04 10:19:31 -0800259 if (mOps.size() == 1) {
Tom Hudson107843d2014-09-08 11:26:26 -0400260 DrawBatch::replay(renderer, dirty, -1);
261 return;
Chris Craik527a3aa2013-03-04 10:19:31 -0800262 }
263
Chris Craik28ce94a2013-05-31 11:38:03 -0700264 // 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 -0800265 renderer.setupMergedMultiDraw(mClipSideFlags ? &mClipRect : nullptr);
Chris Craik28ce94a2013-05-31 11:38:03 -0700266
Chris Craikc1c5f082013-09-11 16:23:37 -0700267 DrawOp* op = mOps[0].op;
Chris Craikd965bc52013-09-16 14:47:13 -0700268#if DEBUG_DISPLAY_LIST_OPS_AS_EVENTS
269 renderer.eventMark("multiDraw");
270 renderer.eventMark(op->name());
271#endif
Tom Hudson107843d2014-09-08 11:26:26 -0400272 op->multiDraw(renderer, dirty, mOps, mBounds);
Chris Craik527a3aa2013-03-04 10:19:31 -0800273
274#if DEBUG_MERGE_BEHAVIOR
275 renderer.drawScreenSpaceColorRect(mBounds.left, mBounds.top, mBounds.right, mBounds.bottom,
276 DEBUG_COLOR_MERGEDBATCH);
277#endif
Chris Craik527a3aa2013-03-04 10:19:31 -0800278 }
Chris Craik28ce94a2013-05-31 11:38:03 -0700279
280private:
281 /*
282 * Contains the effective clip rect shared by all merged ops. Initialized to the layer viewport,
283 * it will shrink if an op must be clipped on a certain side. The clipped sides are reflected in
284 * mClipSideFlags.
285 */
286 Rect mClipRect;
287 int mClipSideFlags;
Chris Craik527a3aa2013-03-04 10:19:31 -0800288};
289
290class StateOpBatch : public Batch {
Chris Craikff785832013-03-08 13:12:16 -0800291public:
292 // creates a single operation batch
Chris Craikc1c5f082013-09-11 16:23:37 -0700293 StateOpBatch(const StateOp* op, const DeferredDisplayState* state) : mOp(op), mState(state) {}
Chris Craikff785832013-03-08 13:12:16 -0800294
Chris Craikd41c4d82015-01-05 15:51:13 -0800295 virtual void replay(OpenGLRenderer& renderer, Rect& dirty, int index) override {
Chris Craikff785832013-03-08 13:12:16 -0800296 DEFER_LOGD("replaying state op batch %p", this);
Chris Craikc1c5f082013-09-11 16:23:37 -0700297 renderer.restoreDisplayState(*mState);
Chris Craikff785832013-03-08 13:12:16 -0800298
299 // use invalid save count because it won't be used at flush time - RestoreToCountOp is the
300 // only one to use it, and we don't use that class at flush time, instead calling
301 // renderer.restoreToCount directly
302 int saveCount = -1;
303 mOp->applyState(renderer, saveCount);
Chris Craikff785832013-03-08 13:12:16 -0800304 }
305
306private:
Chris Craik7273daa2013-03-28 11:25:24 -0700307 const StateOp* mOp;
Chris Craikc1c5f082013-09-11 16:23:37 -0700308 const DeferredDisplayState* mState;
Chris Craikff785832013-03-08 13:12:16 -0800309};
310
Chris Craik527a3aa2013-03-04 10:19:31 -0800311class RestoreToCountBatch : public Batch {
Chris Craikff785832013-03-08 13:12:16 -0800312public:
Andreas Gampe64bb4132014-11-22 00:35:09 +0000313 RestoreToCountBatch(const StateOp* op, const DeferredDisplayState* state, int restoreCount) :
314 mState(state), mRestoreCount(restoreCount) {}
Chris Craikff785832013-03-08 13:12:16 -0800315
Chris Craikd41c4d82015-01-05 15:51:13 -0800316 virtual void replay(OpenGLRenderer& renderer, Rect& dirty, int index) override {
Chris Craikff785832013-03-08 13:12:16 -0800317 DEFER_LOGD("batch %p restoring to count %d", this, mRestoreCount);
Chris Craik7273daa2013-03-28 11:25:24 -0700318
Chris Craikc1c5f082013-09-11 16:23:37 -0700319 renderer.restoreDisplayState(*mState);
Chris Craikff785832013-03-08 13:12:16 -0800320 renderer.restoreToCount(mRestoreCount);
Chris Craikff785832013-03-08 13:12:16 -0800321 }
322
323private:
Chris Craik7273daa2013-03-28 11:25:24 -0700324 // we use the state storage for the RestoreToCountOp, but don't replay the op itself
Chris Craikc1c5f082013-09-11 16:23:37 -0700325 const DeferredDisplayState* mState;
326
Chris Craikff785832013-03-08 13:12:16 -0800327 /*
328 * The count used here represents the flush() time saveCount. This is as opposed to the
329 * DisplayList record time, or defer() time values (which are RestoreToCountOp's mCount, and
330 * (saveCount + mCount) respectively). Since the count is different from the original
331 * RestoreToCountOp, we don't store a pointer to the op, as elsewhere.
332 */
333 const int mRestoreCount;
334};
335
Chris Craik527a3aa2013-03-04 10:19:31 -0800336#if DEBUG_MERGE_BEHAVIOR
337class BarrierDebugBatch : public Batch {
Tom Hudson107843d2014-09-08 11:26:26 -0400338 virtual void replay(OpenGLRenderer& renderer, Rect& dirty, int index) {
Chris Craik527a3aa2013-03-04 10:19:31 -0800339 renderer.drawScreenSpaceColorRect(0, 0, 10000, 10000, DEBUG_COLOR_BARRIER);
Chris Craik527a3aa2013-03-04 10:19:31 -0800340 }
341};
342#endif
343
Chris Craikff785832013-03-08 13:12:16 -0800344/////////////////////////////////////////////////////////////////////////////////
345// DeferredDisplayList
346/////////////////////////////////////////////////////////////////////////////////
347
348void DeferredDisplayList::resetBatchingState() {
Chris Craikc3566d02013-02-04 16:16:33 -0800349 for (int i = 0; i < kOpBatch_Count; i++) {
Chris Craikd41c4d82015-01-05 15:51:13 -0800350 mBatchLookup[i] = nullptr;
Chris Craik527a3aa2013-03-04 10:19:31 -0800351 mMergingBatches[i].clear();
Chris Craikc3566d02013-02-04 16:16:33 -0800352 }
Chris Craik527a3aa2013-03-04 10:19:31 -0800353#if DEBUG_MERGE_BEHAVIOR
354 if (mBatches.size() != 0) {
355 mBatches.add(new BarrierDebugBatch());
356 }
357#endif
358 mEarliestBatchIndex = mBatches.size();
Chris Craikff785832013-03-08 13:12:16 -0800359}
360
361void DeferredDisplayList::clear() {
362 resetBatchingState();
363 mComplexClipStackStart = -1;
364
Chris Craikc3566d02013-02-04 16:16:33 -0800365 for (unsigned int i = 0; i < mBatches.size(); i++) {
366 delete mBatches[i];
367 }
368 mBatches.clear();
Chris Craikff785832013-03-08 13:12:16 -0800369 mSaveStack.clear();
Chris Craik527a3aa2013-03-04 10:19:31 -0800370 mEarliestBatchIndex = 0;
Chris Craik28ce94a2013-05-31 11:38:03 -0700371 mEarliestUnclearedIndex = 0;
Chris Craikc3566d02013-02-04 16:16:33 -0800372}
373
Chris Craikff785832013-03-08 13:12:16 -0800374/////////////////////////////////////////////////////////////////////////////////
375// Operation adding
376/////////////////////////////////////////////////////////////////////////////////
377
378int DeferredDisplayList::getStateOpDeferFlags() const {
379 // For both clipOp and save(Layer)Op, we don't want to save drawing info, and only want to save
380 // the clip if we aren't recording a complex clip (and can thus trust it to be a rect)
381 return recordingComplexClip() ? 0 : kStateDeferFlag_Clip;
382}
383
384int DeferredDisplayList::getDrawOpDeferFlags() const {
385 return kStateDeferFlag_Draw | getStateOpDeferFlags();
386}
387
388/**
389 * When an clipping operation occurs that could cause a complex clip, record the operation and all
390 * subsequent clipOps, save/restores (if the clip flag is set). During a flush, instead of loading
391 * the clip from deferred state, we play back all of the relevant state operations that generated
392 * the complex clip.
393 *
394 * Note that we don't need to record the associated restore operation, since operations at defer
395 * time record whether they should store the renderer's current clip
396 */
397void DeferredDisplayList::addClip(OpenGLRenderer& renderer, ClipOp* op) {
398 if (recordingComplexClip() || op->canCauseComplexClip() || !renderer.hasRectToRectTransform()) {
399 DEFER_LOGD("%p Received complex clip operation %p", this, op);
400
401 // NOTE: defer clip op before setting mComplexClipStackStart so previous clip is recorded
402 storeStateOpBarrier(renderer, op);
403
404 if (!recordingComplexClip()) {
405 mComplexClipStackStart = renderer.getSaveCount() - 1;
406 DEFER_LOGD(" Starting complex clip region, start is %d", mComplexClipStackStart);
Chris Craikc3566d02013-02-04 16:16:33 -0800407 }
Chris Craikff785832013-03-08 13:12:16 -0800408 }
409}
410
411/**
412 * For now, we record save layer operations as barriers in the batch list, preventing drawing
413 * operations from reordering around the saveLayer and it's associated restore()
414 *
415 * In the future, we should send saveLayer commands (if they can be played out of order) and their
416 * contained drawing operations to a seperate list of batches, so that they may draw at the
417 * beginning of the frame. This would avoid targetting and removing an FBO in the middle of a frame.
418 *
419 * saveLayer operations should be pulled to the beginning of the frame if the canvas doesn't have a
Florin Malitaeecff562015-12-21 10:43:01 -0500420 * complex clip, and if the flags (SaveFlags::Clip & SaveFlags::ClipToLayer) are set.
Chris Craikff785832013-03-08 13:12:16 -0800421 */
422void DeferredDisplayList::addSaveLayer(OpenGLRenderer& renderer,
423 SaveLayerOp* op, int newSaveCount) {
424 DEFER_LOGD("%p adding saveLayerOp %p, flags %x, new count %d",
425 this, op, op->getFlags(), newSaveCount);
426
427 storeStateOpBarrier(renderer, op);
John Reck272a6852015-07-29 16:48:58 -0700428 mSaveStack.push_back(newSaveCount);
Chris Craikff785832013-03-08 13:12:16 -0800429}
430
431/**
432 * Takes save op and it's return value - the new save count - and stores it into the stream as a
433 * barrier if it's needed to properly modify a complex clip
434 */
435void DeferredDisplayList::addSave(OpenGLRenderer& renderer, SaveOp* op, int newSaveCount) {
436 int saveFlags = op->getFlags();
437 DEFER_LOGD("%p adding saveOp %p, flags %x, new count %d", this, op, saveFlags, newSaveCount);
438
Florin Malitaeecff562015-12-21 10:43:01 -0500439 if (recordingComplexClip() && (saveFlags & SaveFlags::Clip)) {
Chris Craikff785832013-03-08 13:12:16 -0800440 // store and replay the save operation, as it may be needed to correctly playback the clip
441 DEFER_LOGD(" adding save barrier with new save count %d", newSaveCount);
442 storeStateOpBarrier(renderer, op);
John Reck272a6852015-07-29 16:48:58 -0700443 mSaveStack.push_back(newSaveCount);
Chris Craikff785832013-03-08 13:12:16 -0800444 }
445}
446
447/**
448 * saveLayer() commands must be associated with a restoreToCount batch that will clean up and draw
449 * the layer in the deferred list
450 *
451 * other save() commands which occur as children of a snapshot with complex clip will be deferred,
452 * and must be restored
453 *
454 * Either will act as a barrier to draw operation reordering, as we want to play back layer
455 * save/restore and complex canvas modifications (including save/restore) in order.
456 */
Chris Craik7273daa2013-03-28 11:25:24 -0700457void DeferredDisplayList::addRestoreToCount(OpenGLRenderer& renderer, StateOp* op,
458 int newSaveCount) {
Chris Craikff785832013-03-08 13:12:16 -0800459 DEFER_LOGD("%p addRestoreToCount %d", this, newSaveCount);
460
461 if (recordingComplexClip() && newSaveCount <= mComplexClipStackStart) {
462 mComplexClipStackStart = -1;
463 resetBatchingState();
464 }
465
John Reck272a6852015-07-29 16:48:58 -0700466 if (mSaveStack.empty() || newSaveCount > mSaveStack.back()) {
Chris Craikff785832013-03-08 13:12:16 -0800467 return;
468 }
469
John Reck272a6852015-07-29 16:48:58 -0700470 while (!mSaveStack.empty() && mSaveStack.back() >= newSaveCount) mSaveStack.pop_back();
Chris Craikff785832013-03-08 13:12:16 -0800471
Chris Craik1ed30c92013-04-03 12:37:35 -0700472 storeRestoreToCountBarrier(renderer, op, mSaveStack.size() + FLUSH_SAVE_STACK_DEPTH);
Chris Craikff785832013-03-08 13:12:16 -0800473}
474
475void DeferredDisplayList::addDrawOp(OpenGLRenderer& renderer, DrawOp* op) {
Chris Craikc1c5f082013-09-11 16:23:37 -0700476 /* 1: op calculates local bounds */
477 DeferredDisplayState* const state = createState();
John Reck3b202512014-06-23 13:13:08 -0700478 if (op->getLocalBounds(state->mBounds)) {
Chris Craikc1c5f082013-09-11 16:23:37 -0700479 if (state->mBounds.isEmpty()) {
480 // valid empty bounds, don't bother deferring
481 tryRecycleState(state);
482 return;
483 }
484 } else {
485 state->mBounds.setEmpty();
486 }
487
488 /* 2: renderer calculates global bounds + stores state */
489 if (renderer.storeDisplayState(*state, getDrawOpDeferFlags())) {
490 tryRecycleState(state);
Chris Craikff785832013-03-08 13:12:16 -0800491 return; // quick rejected
492 }
493
Chris Craikc1c5f082013-09-11 16:23:37 -0700494 /* 3: ask op for defer info, given renderer state */
Chris Craik28ce94a2013-05-31 11:38:03 -0700495 DeferInfo deferInfo;
Chris Craikc1c5f082013-09-11 16:23:37 -0700496 op->onDefer(renderer, deferInfo, *state);
Chris Craik527a3aa2013-03-04 10:19:31 -0800497
498 // complex clip has a complex set of expectations on the renderer state - for now, avoid taking
499 // the merge path in those cases
Chris Craik28ce94a2013-05-31 11:38:03 -0700500 deferInfo.mergeable &= !recordingComplexClip();
Chris Craikb1f990d2015-06-12 11:28:52 -0700501 deferInfo.opaqueOverBounds &= !recordingComplexClip()
John Reck272a6852015-07-29 16:48:58 -0700502 && mSaveStack.empty()
Chris Craikb1f990d2015-06-12 11:28:52 -0700503 && !state->mRoundRectClipState;
Chris Craik28ce94a2013-05-31 11:38:03 -0700504
Chris Craikb45c6aa2015-09-28 15:41:27 -0700505 if (CC_LIKELY(avoidOverdraw()) && mBatches.size() &&
Chris Craikc1c5f082013-09-11 16:23:37 -0700506 state->mClipSideFlags != kClipSide_ConservativeFull &&
507 deferInfo.opaqueOverBounds && state->mBounds.contains(mBounds)) {
Chris Craikf70119c2013-06-13 11:21:22 -0700508 // avoid overdraw by resetting drawing state + discarding drawing ops
Chris Craik28ce94a2013-05-31 11:38:03 -0700509 discardDrawingBatches(mBatches.size() - 1);
Chris Craikf70119c2013-06-13 11:21:22 -0700510 resetBatchingState();
Chris Craik28ce94a2013-05-31 11:38:03 -0700511 }
Chris Craikff785832013-03-08 13:12:16 -0800512
Chris Craik2507c342015-05-04 14:36:49 -0700513 if (CC_UNLIKELY(Properties::drawReorderDisabled)) {
Chris Craikff785832013-03-08 13:12:16 -0800514 // TODO: elegant way to reuse batches?
Chris Craik28ce94a2013-05-31 11:38:03 -0700515 DrawBatch* b = new DrawBatch(deferInfo);
Chris Craikc1c5f082013-09-11 16:23:37 -0700516 b->add(op, state, deferInfo.opaqueOverBounds);
John Reck272a6852015-07-29 16:48:58 -0700517 mBatches.push_back(b);
Chris Craikc3566d02013-02-04 16:16:33 -0800518 return;
519 }
520
Chris Craik527a3aa2013-03-04 10:19:31 -0800521 // 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 -0800522 DrawBatch* targetBatch = nullptr;
Chris Craikc3566d02013-02-04 16:16:33 -0800523
Chris Craik527a3aa2013-03-04 10:19:31 -0800524 // insertion point of a new batch, will hopefully be immediately after similar batch
525 // (eventually, should be similar shader)
526 int insertBatchIndex = mBatches.size();
John Reck272a6852015-07-29 16:48:58 -0700527 if (!mBatches.empty()) {
Chris Craikc1c5f082013-09-11 16:23:37 -0700528 if (state->mBounds.isEmpty()) {
Chris Craikb565df12015-10-05 13:00:52 -0700529 // 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 -0700530 DrawBatch* b = new DrawBatch(deferInfo);
Chris Craikc1c5f082013-09-11 16:23:37 -0700531 b->add(op, state, deferInfo.opaqueOverBounds);
John Reck272a6852015-07-29 16:48:58 -0700532 mBatches.push_back(b);
Chris Craik527a3aa2013-03-04 10:19:31 -0800533 resetBatchingState();
Chris Craikc3566d02013-02-04 16:16:33 -0800534#if DEBUG_DEFER
535 DEFER_LOGD("Warning: Encountered op with empty bounds, resetting batches");
536 op->output(2);
537#endif
538 return;
539 }
540
Chris Craik28ce94a2013-05-31 11:38:03 -0700541 if (deferInfo.mergeable) {
Chris Craik527a3aa2013-03-04 10:19:31 -0800542 // Try to merge with any existing batch with same mergeId.
Sene Gales16730352015-09-30 14:41:29 +0100543 std::unordered_map<mergeid_t, DrawBatch*>& mergingBatch
544 = mMergingBatches[deferInfo.batchId];
545 auto getResult = mergingBatch.find(deferInfo.mergeId);
546 if (getResult != mergingBatch.end()) {
547 targetBatch = getResult->second;
Chris Craikc1c5f082013-09-11 16:23:37 -0700548 if (!((MergingDrawBatch*) targetBatch)->canMergeWith(op, state)) {
Chris Craikd41c4d82015-01-05 15:51:13 -0800549 targetBatch = nullptr;
Chris Craik527a3aa2013-03-04 10:19:31 -0800550 }
551 }
552 } else {
553 // join with similar, non-merging batch
Chris Craik28ce94a2013-05-31 11:38:03 -0700554 targetBatch = (DrawBatch*)mBatchLookup[deferInfo.batchId];
Chris Craik527a3aa2013-03-04 10:19:31 -0800555 }
556
Chris Craik28ce94a2013-05-31 11:38:03 -0700557 if (targetBatch || deferInfo.mergeable) {
Chris Craikc3566d02013-02-04 16:16:33 -0800558 // iterate back toward target to see if anything drawn since should overlap the new op
Chris Craik527a3aa2013-03-04 10:19:31 -0800559 // if no target, merging ops still interate to find similar batch to insert after
560 for (int i = mBatches.size() - 1; i >= mEarliestBatchIndex; i--) {
561 DrawBatch* overBatch = (DrawBatch*)mBatches[i];
562
563 if (overBatch == targetBatch) break;
564
565 // TODO: also consider shader shared between batch types
Chris Craik28ce94a2013-05-31 11:38:03 -0700566 if (deferInfo.batchId == overBatch->getBatchId()) {
Chris Craik527a3aa2013-03-04 10:19:31 -0800567 insertBatchIndex = i + 1;
568 if (!targetBatch) break; // found insert position, quit
569 }
570
Chris Craikc1c5f082013-09-11 16:23:37 -0700571 if (overBatch->intersects(state->mBounds)) {
Chris Craik527a3aa2013-03-04 10:19:31 -0800572 // NOTE: it may be possible to optimize for special cases where two operations
573 // of the same batch/paint could swap order, such as with a non-mergeable
574 // (clipped) and a mergeable text operation
Chris Craikd41c4d82015-01-05 15:51:13 -0800575 targetBatch = nullptr;
Chris Craikc3566d02013-02-04 16:16:33 -0800576#if DEBUG_DEFER
Chris Craikc1c5f082013-09-11 16:23:37 -0700577 DEFER_LOGD("op couldn't join batch %p, was intersected by batch %d",
578 targetBatch, i);
Chris Craikc3566d02013-02-04 16:16:33 -0800579 op->output(2);
580#endif
581 break;
582 }
583 }
584 }
585 }
Chris Craik527a3aa2013-03-04 10:19:31 -0800586
Chris Craikc3566d02013-02-04 16:16:33 -0800587 if (!targetBatch) {
Chris Craik28ce94a2013-05-31 11:38:03 -0700588 if (deferInfo.mergeable) {
Chris Craik0e87f002013-06-19 16:54:59 -0700589 targetBatch = new MergingDrawBatch(deferInfo,
590 renderer.getViewportWidth(), renderer.getViewportHeight());
Sene Gales16730352015-09-30 14:41:29 +0100591 mMergingBatches[deferInfo.batchId].insert(
592 std::make_pair(deferInfo.mergeId, targetBatch));
Chris Craik527a3aa2013-03-04 10:19:31 -0800593 } else {
Chris Craik28ce94a2013-05-31 11:38:03 -0700594 targetBatch = new DrawBatch(deferInfo);
595 mBatchLookup[deferInfo.batchId] = targetBatch;
Chris Craikc3566d02013-02-04 16:16:33 -0800596 }
Chris Craik527a3aa2013-03-04 10:19:31 -0800597
Chris Craikf70119c2013-06-13 11:21:22 -0700598 DEFER_LOGD("creating %singBatch %p, bid %x, at %d",
599 deferInfo.mergeable ? "Merg" : "Draw",
600 targetBatch, deferInfo.batchId, insertBatchIndex);
John Reck272a6852015-07-29 16:48:58 -0700601 mBatches.insert(mBatches.begin() + insertBatchIndex, targetBatch);
Chris Craikc3566d02013-02-04 16:16:33 -0800602 }
Chris Craik527a3aa2013-03-04 10:19:31 -0800603
Chris Craikc1c5f082013-09-11 16:23:37 -0700604 targetBatch->add(op, state, deferInfo.opaqueOverBounds);
Chris Craikc3566d02013-02-04 16:16:33 -0800605}
606
Chris Craikff785832013-03-08 13:12:16 -0800607void DeferredDisplayList::storeStateOpBarrier(OpenGLRenderer& renderer, StateOp* op) {
608 DEFER_LOGD("%p adding state op barrier at pos %d", this, mBatches.size());
609
Chris Craikc1c5f082013-09-11 16:23:37 -0700610 DeferredDisplayState* state = createState();
611 renderer.storeDisplayState(*state, getStateOpDeferFlags());
John Reck272a6852015-07-29 16:48:58 -0700612 mBatches.push_back(new StateOpBatch(op, state));
Chris Craikff785832013-03-08 13:12:16 -0800613 resetBatchingState();
614}
615
Chris Craik7273daa2013-03-28 11:25:24 -0700616void DeferredDisplayList::storeRestoreToCountBarrier(OpenGLRenderer& renderer, StateOp* op,
617 int newSaveCount) {
Chris Craikff785832013-03-08 13:12:16 -0800618 DEFER_LOGD("%p adding restore to count %d barrier, pos %d",
619 this, newSaveCount, mBatches.size());
620
Chris Craik7273daa2013-03-28 11:25:24 -0700621 // store displayState for the restore operation, as it may be associated with a saveLayer that
Florin Malitaeecff562015-12-21 10:43:01 -0500622 // doesn't have SaveFlags::Clip set
Chris Craikc1c5f082013-09-11 16:23:37 -0700623 DeferredDisplayState* state = createState();
624 renderer.storeDisplayState(*state, getStateOpDeferFlags());
John Reck272a6852015-07-29 16:48:58 -0700625 mBatches.push_back(new RestoreToCountBatch(op, state, newSaveCount));
Chris Craikff785832013-03-08 13:12:16 -0800626 resetBatchingState();
627}
628
629/////////////////////////////////////////////////////////////////////////////////
630// Replay / flush
631/////////////////////////////////////////////////////////////////////////////////
632
John Reck272a6852015-07-29 16:48:58 -0700633static void replayBatchList(const std::vector<Batch*>& batchList,
Chris Craikff785832013-03-08 13:12:16 -0800634 OpenGLRenderer& renderer, Rect& dirty) {
Chris Craikff785832013-03-08 13:12:16 -0800635
Chris Craikff785832013-03-08 13:12:16 -0800636 for (unsigned int i = 0; i < batchList.size(); i++) {
Chris Craik28ce94a2013-05-31 11:38:03 -0700637 if (batchList[i]) {
Tom Hudson107843d2014-09-08 11:26:26 -0400638 batchList[i]->replay(renderer, dirty, i);
Chris Craik28ce94a2013-05-31 11:38:03 -0700639 }
Chris Craikff785832013-03-08 13:12:16 -0800640 }
Chris Craik527a3aa2013-03-04 10:19:31 -0800641 DEFER_LOGD("--flushed, drew %d batches", batchList.size());
Chris Craikff785832013-03-08 13:12:16 -0800642}
643
Tom Hudson107843d2014-09-08 11:26:26 -0400644void DeferredDisplayList::flush(OpenGLRenderer& renderer, Rect& dirty) {
Chris Craikff785832013-03-08 13:12:16 -0800645 ATRACE_NAME("flush drawing commands");
Chris Craikc08820f2015-09-22 14:22:29 -0700646 Caches::getInstance().fontRenderer.endPrecaching();
Romain Guycf51a412013-04-08 19:40:31 -0700647
Tom Hudson107843d2014-09-08 11:26:26 -0400648 if (isEmpty()) return; // nothing to flush
Chris Craika4e16c52013-03-22 10:00:48 -0700649 renderer.restoreToCount(1);
Chris Craikc3566d02013-02-04 16:16:33 -0800650
651 DEFER_LOGD("--flushing");
Romain Guy0f667532013-03-01 14:31:04 -0800652 renderer.eventMark("Flush");
653
Chris Craik8df5ffa2015-04-28 17:47:20 -0700654 // save and restore so that reordering doesn't affect final state
Florin Malitaeecff562015-12-21 10:43:01 -0500655 renderer.save(SaveFlags::MatrixClip);
Chris Craika4e16c52013-03-22 10:00:48 -0700656
Chris Craikb45c6aa2015-09-28 15:41:27 -0700657 if (CC_LIKELY(avoidOverdraw())) {
Chris Craikef8d6f22014-12-17 11:10:28 -0800658 for (unsigned int i = 1; i < mBatches.size(); i++) {
659 if (mBatches[i] && mBatches[i]->coversBounds(mBounds)) {
660 discardDrawingBatches(i - 1);
661 }
Chris Craik28ce94a2013-05-31 11:38:03 -0700662 }
663 }
Chris Craik1ed30c92013-04-03 12:37:35 -0700664 // NOTE: depth of the save stack at this point, before playback, should be reflected in
665 // FLUSH_SAVE_STACK_DEPTH, so that save/restores match up correctly
Tom Hudson107843d2014-09-08 11:26:26 -0400666 replayBatchList(mBatches, renderer, dirty);
Chris Craika4e16c52013-03-22 10:00:48 -0700667
668 renderer.restoreToCount(1);
Chris Craikc3566d02013-02-04 16:16:33 -0800669
Chris Craikff785832013-03-08 13:12:16 -0800670 DEFER_LOGD("--flush complete, returning %x", status);
Chris Craikc3566d02013-02-04 16:16:33 -0800671 clear();
Chris Craikc3566d02013-02-04 16:16:33 -0800672}
673
Chris Craikf70119c2013-06-13 11:21:22 -0700674void DeferredDisplayList::discardDrawingBatches(const unsigned int maxIndex) {
Chris Craik28ce94a2013-05-31 11:38:03 -0700675 for (unsigned int i = mEarliestUnclearedIndex; i <= maxIndex; i++) {
Chris Craikf70119c2013-06-13 11:21:22 -0700676 // leave deferred state ops alone for simplicity (empty save restore pairs may now exist)
Chris Craik28ce94a2013-05-31 11:38:03 -0700677 if (mBatches[i] && mBatches[i]->purelyDrawBatch()) {
Chris Craik28ce94a2013-05-31 11:38:03 -0700678 delete mBatches[i];
John Reck272a6852015-07-29 16:48:58 -0700679 mBatches[i] = nullptr;
Chris Craik28ce94a2013-05-31 11:38:03 -0700680 }
681 }
682 mEarliestUnclearedIndex = maxIndex + 1;
683}
684
Chris Craikc3566d02013-02-04 16:16:33 -0800685}; // namespace uirenderer
686}; // namespace android