blob: 3016814deafcaf8414905362996fdc176ee1be87 [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:
Chris Craik527a3aa2013-03-04 10:19:31 -080055 virtual status_t replay(OpenGLRenderer& renderer, Rect& dirty, int index) = 0;
56 virtual ~Batch() {}
Chris Craik28ce94a2013-05-31 11:38:03 -070057 virtual bool purelyDrawBatch() { return false; }
58 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
Chris Craik527a3aa2013-03-04 10:19:31 -080094 virtual status_t 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
98 status_t status = DrawGlInfo::kStatusDone;
99 DisplayListLogBuffer& logBuffer = DisplayListLogBuffer::getInstance();
100 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
Chris Craikff785832013-03-08 13:12:16 -0800108 logBuffer.writeCommand(0, op->name());
Chris Craikd4b43b32013-05-09 13:07:52 -0700109 status |= op->applyDraw(renderer, dirty);
Chris Craik527a3aa2013-03-04 10:19:31 -0800110
111#if DEBUG_MERGE_BEHAVIOR
Chris Craikc1c5f082013-09-11 16:23:37 -0700112 const Rect& bounds = state->mBounds;
Chris Craik527a3aa2013-03-04 10:19:31 -0800113 int batchColor = 0x1f000000;
114 if (getBatchId() & 0x1) batchColor |= 0x0000ff;
115 if (getBatchId() & 0x2) batchColor |= 0x00ff00;
116 if (getBatchId() & 0x4) batchColor |= 0xff0000;
117 renderer.drawScreenSpaceColorRect(bounds.left, bounds.top, bounds.right, bounds.bottom,
118 batchColor);
119#endif
Chris Craikff785832013-03-08 13:12:16 -0800120 }
121 return status;
122 }
123
Chris Craik28ce94a2013-05-31 11:38:03 -0700124 virtual bool purelyDrawBatch() { return true; }
125
126 virtual bool coversBounds(const Rect& bounds) {
127 if (CC_LIKELY(!mAllOpsOpaque || !mBounds.contains(bounds) || count() == 1)) return false;
128
129 Region uncovered(android::Rect(bounds.left, bounds.top, bounds.right, bounds.bottom));
130 for (unsigned int i = 0; i < mOps.size(); i++) {
Chris Craikc1c5f082013-09-11 16:23:37 -0700131 const Rect &r = mOps[i].state->mBounds;
Chris Craik28ce94a2013-05-31 11:38:03 -0700132 uncovered.subtractSelf(android::Rect(r.left, r.top, r.right, r.bottom));
133 }
134 return uncovered.isEmpty();
135 }
136
Chris Craik527a3aa2013-03-04 10:19:31 -0800137 inline int getBatchId() const { return mBatchId; }
138 inline mergeid_t getMergeId() const { return mMergeId; }
Chris Craikff785832013-03-08 13:12:16 -0800139 inline int count() const { return mOps.size(); }
Chris Craik527a3aa2013-03-04 10:19:31 -0800140
141protected:
Chris Craikc1c5f082013-09-11 16:23:37 -0700142 Vector<OpStatePair> mOps;
Chris Craik28ce94a2013-05-31 11:38:03 -0700143 Rect mBounds; // union of bounds of contained ops
Chris Craik527a3aa2013-03-04 10:19:31 -0800144private:
Chris Craik28ce94a2013-05-31 11:38:03 -0700145 bool mAllOpsOpaque;
Chris Craik527a3aa2013-03-04 10:19:31 -0800146 int mBatchId;
147 mergeid_t mMergeId;
Chris Craikc3566d02013-02-04 16:16:33 -0800148};
149
Chris Craik527a3aa2013-03-04 10:19:31 -0800150class MergingDrawBatch : public DrawBatch {
151public:
Chris Craik0e87f002013-06-19 16:54:59 -0700152 MergingDrawBatch(DeferInfo& deferInfo, int width, int height) :
153 DrawBatch(deferInfo), mClipRect(width, height),
154 mClipSideFlags(kClipSide_None) {}
Chris Craika02c4ed2013-06-14 13:43:58 -0700155
156 /*
157 * Helper for determining if a new op can merge with a MergingDrawBatch based on their bounds
158 * and clip side flags. Positive bounds delta means new bounds fit in old.
159 */
160 static inline bool checkSide(const int currentFlags, const int newFlags, const int side,
161 float boundsDelta) {
162 bool currentClipExists = currentFlags & side;
163 bool newClipExists = newFlags & side;
164
165 // if current is clipped, we must be able to fit new bounds in current
166 if (boundsDelta > 0 && currentClipExists) return false;
167
168 // if new is clipped, we must be able to fit current bounds in new
169 if (boundsDelta < 0 && newClipExists) return false;
170
171 return true;
172 }
Chris Craik527a3aa2013-03-04 10:19:31 -0800173
174 /*
175 * Checks if a (mergeable) op can be merged into this batch
176 *
177 * If true, the op's multiDraw must be guaranteed to handle both ops simultaneously, so it is
178 * important to consider all paint attributes used in the draw calls in deciding both a) if an
Chris Craika02c4ed2013-06-14 13:43:58 -0700179 * op tries to merge at all, and b) if the op can merge with another set of ops
Chris Craik527a3aa2013-03-04 10:19:31 -0800180 *
181 * False positives can lead to information from the paints of subsequent merged operations being
182 * dropped, so we make simplifying qualifications on the ops that can merge, per op type.
183 */
Chris Craikc1c5f082013-09-11 16:23:37 -0700184 bool canMergeWith(const DrawOp* op, const DeferredDisplayState* state) {
Chris Craik527a3aa2013-03-04 10:19:31 -0800185 bool isTextBatch = getBatchId() == DeferredDisplayList::kOpBatch_Text ||
186 getBatchId() == DeferredDisplayList::kOpBatch_ColorText;
187
188 // Overlapping other operations is only allowed for text without shadow. For other ops,
189 // multiDraw isn't guaranteed to overdraw correctly
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -0400190 if (!isTextBatch || op->hasTextShadow()) {
Chris Craikc1c5f082013-09-11 16:23:37 -0700191 if (intersects(state->mBounds)) return false;
Chris Craik527a3aa2013-03-04 10:19:31 -0800192 }
Chris Craikc1c5f082013-09-11 16:23:37 -0700193 const DeferredDisplayState* lhs = state;
194 const DeferredDisplayState* rhs = mOps[0].state;
Chris Craik527a3aa2013-03-04 10:19:31 -0800195
Chris Craikdeeda3d2014-05-05 19:09:33 -0700196 if (!MathUtils::areEqual(lhs->mAlpha, rhs->mAlpha)) return false;
197
198 // Identical round rect clip state means both ops will clip in the same way, or not at all.
199 // As the state objects are const, we can compare their pointers to determine mergeability
200 if (lhs->mRoundRectClipState != rhs->mRoundRectClipState) return false;
Chris Craik527a3aa2013-03-04 10:19:31 -0800201
Chris Craika02c4ed2013-06-14 13:43:58 -0700202 /* Clipping compatibility check
203 *
204 * Exploits the fact that if a op or batch is clipped on a side, its bounds will equal its
205 * clip for that side.
206 */
207 const int currentFlags = mClipSideFlags;
Chris Craikc1c5f082013-09-11 16:23:37 -0700208 const int newFlags = state->mClipSideFlags;
Chris Craika02c4ed2013-06-14 13:43:58 -0700209 if (currentFlags != kClipSide_None || newFlags != kClipSide_None) {
Chris Craikc1c5f082013-09-11 16:23:37 -0700210 const Rect& opBounds = state->mBounds;
Chris Craika02c4ed2013-06-14 13:43:58 -0700211 float boundsDelta = mBounds.left - opBounds.left;
212 if (!checkSide(currentFlags, newFlags, kClipSide_Left, boundsDelta)) return false;
213 boundsDelta = mBounds.top - opBounds.top;
214 if (!checkSide(currentFlags, newFlags, kClipSide_Top, boundsDelta)) return false;
215
216 // right and bottom delta calculation reversed to account for direction
217 boundsDelta = opBounds.right - mBounds.right;
218 if (!checkSide(currentFlags, newFlags, kClipSide_Right, boundsDelta)) return false;
219 boundsDelta = opBounds.bottom - mBounds.bottom;
220 if (!checkSide(currentFlags, newFlags, kClipSide_Bottom, boundsDelta)) return false;
Chris Craik28ce94a2013-05-31 11:38:03 -0700221 }
Chris Craik28ce94a2013-05-31 11:38:03 -0700222
Chris Craik527a3aa2013-03-04 10:19:31 -0800223 // if paints are equal, then modifiers + paint attribs don't need to be compared
Chris Craikc1c5f082013-09-11 16:23:37 -0700224 if (op->mPaint == mOps[0].op->mPaint) return true;
Chris Craik527a3aa2013-03-04 10:19:31 -0800225
Chris Craikc1c5f082013-09-11 16:23:37 -0700226 if (op->getPaintAlpha() != mOps[0].op->getPaintAlpha()) return false;
Chris Craik527a3aa2013-03-04 10:19:31 -0800227
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500228 if (op->mPaint && mOps[0].op->mPaint &&
229 op->mPaint->getColorFilter() != mOps[0].op->mPaint->getColorFilter()) {
230 return false;
231 }
232
Chris Craik527a3aa2013-03-04 10:19:31 -0800233 /* Draw Modifiers compatibility check
234 *
235 * Shadows are ignored, as only text uses them, and in that case they are drawn
236 * per-DrawTextOp, before the unified text draw. Because of this, it's always safe to merge
237 * text UNLESS a later draw's shadow should overlays a previous draw's text. This is covered
238 * above with the intersection check.
239 *
240 * OverrideLayerAlpha is also ignored, as it's only used for drawing layers, which are never
241 * merged.
242 *
243 * These ignore cases prevent us from simply memcmp'ing the drawModifiers
244 */
Chris Craikc1c5f082013-09-11 16:23:37 -0700245 const DrawModifiers& lhsMod = lhs->mDrawModifiers;
246 const DrawModifiers& rhsMod = rhs->mDrawModifiers;
Chris Craik527a3aa2013-03-04 10:19:31 -0800247 if (lhsMod.mShader != rhsMod.mShader) return false;
Chris Craik527a3aa2013-03-04 10:19:31 -0800248
249 // Draw filter testing expects bit fields to be clear if filter not set.
250 if (lhsMod.mHasDrawFilter != rhsMod.mHasDrawFilter) return false;
251 if (lhsMod.mPaintFilterClearBits != rhsMod.mPaintFilterClearBits) return false;
252 if (lhsMod.mPaintFilterSetBits != rhsMod.mPaintFilterSetBits) return false;
253
254 return true;
255 }
256
Chris Craik5af5fc52013-09-13 14:41:31 -0700257 virtual void add(DrawOp* op, const DeferredDisplayState* state, bool opaqueOverBounds) {
Chris Craikc1c5f082013-09-11 16:23:37 -0700258 DrawBatch::add(op, state, opaqueOverBounds);
Chris Craik28ce94a2013-05-31 11:38:03 -0700259
Chris Craikc1c5f082013-09-11 16:23:37 -0700260 const int newClipSideFlags = state->mClipSideFlags;
Chris Craik28ce94a2013-05-31 11:38:03 -0700261 mClipSideFlags |= newClipSideFlags;
Chris Craikc1c5f082013-09-11 16:23:37 -0700262 if (newClipSideFlags & kClipSide_Left) mClipRect.left = state->mClip.left;
263 if (newClipSideFlags & kClipSide_Top) mClipRect.top = state->mClip.top;
264 if (newClipSideFlags & kClipSide_Right) mClipRect.right = state->mClip.right;
265 if (newClipSideFlags & kClipSide_Bottom) mClipRect.bottom = state->mClip.bottom;
Chris Craik28ce94a2013-05-31 11:38:03 -0700266 }
267
Chris Craik527a3aa2013-03-04 10:19:31 -0800268 virtual status_t replay(OpenGLRenderer& renderer, Rect& dirty, int index) {
Chris Craik28ce94a2013-05-31 11:38:03 -0700269 DEFER_LOGD("%d replaying MergingDrawBatch %p, with %d ops,"
270 " clip flags %x (batch id %x, merge id %p)",
271 index, this, mOps.size(), mClipSideFlags, getBatchId(), getMergeId());
Chris Craik527a3aa2013-03-04 10:19:31 -0800272 if (mOps.size() == 1) {
Chris Craik28ce94a2013-05-31 11:38:03 -0700273 return DrawBatch::replay(renderer, dirty, -1);
Chris Craik527a3aa2013-03-04 10:19:31 -0800274 }
275
Chris Craik28ce94a2013-05-31 11:38:03 -0700276 // clipping in the merged case is done ahead of time since all ops share the clip (if any)
277 renderer.setupMergedMultiDraw(mClipSideFlags ? &mClipRect : NULL);
278
Chris Craikc1c5f082013-09-11 16:23:37 -0700279 DrawOp* op = mOps[0].op;
Chris Craik527a3aa2013-03-04 10:19:31 -0800280 DisplayListLogBuffer& buffer = DisplayListLogBuffer::getInstance();
281 buffer.writeCommand(0, "multiDraw");
282 buffer.writeCommand(1, op->name());
Chris Craikd965bc52013-09-16 14:47:13 -0700283
284#if DEBUG_DISPLAY_LIST_OPS_AS_EVENTS
285 renderer.eventMark("multiDraw");
286 renderer.eventMark(op->name());
287#endif
Chris Craikd4b43b32013-05-09 13:07:52 -0700288 status_t status = op->multiDraw(renderer, dirty, mOps, mBounds);
Chris Craik527a3aa2013-03-04 10:19:31 -0800289
290#if DEBUG_MERGE_BEHAVIOR
291 renderer.drawScreenSpaceColorRect(mBounds.left, mBounds.top, mBounds.right, mBounds.bottom,
292 DEBUG_COLOR_MERGEDBATCH);
293#endif
294 return status;
295 }
Chris Craik28ce94a2013-05-31 11:38:03 -0700296
297private:
298 /*
299 * Contains the effective clip rect shared by all merged ops. Initialized to the layer viewport,
300 * it will shrink if an op must be clipped on a certain side. The clipped sides are reflected in
301 * mClipSideFlags.
302 */
303 Rect mClipRect;
304 int mClipSideFlags;
Chris Craik527a3aa2013-03-04 10:19:31 -0800305};
306
307class StateOpBatch : public Batch {
Chris Craikff785832013-03-08 13:12:16 -0800308public:
309 // creates a single operation batch
Chris Craikc1c5f082013-09-11 16:23:37 -0700310 StateOpBatch(const StateOp* op, const DeferredDisplayState* state) : mOp(op), mState(state) {}
Chris Craikff785832013-03-08 13:12:16 -0800311
Chris Craik527a3aa2013-03-04 10:19:31 -0800312 virtual status_t replay(OpenGLRenderer& renderer, Rect& dirty, int index) {
Chris Craikff785832013-03-08 13:12:16 -0800313 DEFER_LOGD("replaying state op batch %p", this);
Chris Craikc1c5f082013-09-11 16:23:37 -0700314 renderer.restoreDisplayState(*mState);
Chris Craikff785832013-03-08 13:12:16 -0800315
316 // use invalid save count because it won't be used at flush time - RestoreToCountOp is the
317 // only one to use it, and we don't use that class at flush time, instead calling
318 // renderer.restoreToCount directly
319 int saveCount = -1;
320 mOp->applyState(renderer, saveCount);
321 return DrawGlInfo::kStatusDone;
322 }
323
324private:
Chris Craik7273daa2013-03-28 11:25:24 -0700325 const StateOp* mOp;
Chris Craikc1c5f082013-09-11 16:23:37 -0700326 const DeferredDisplayState* mState;
Chris Craikff785832013-03-08 13:12:16 -0800327};
328
Chris Craik527a3aa2013-03-04 10:19:31 -0800329class RestoreToCountBatch : public Batch {
Chris Craikff785832013-03-08 13:12:16 -0800330public:
Chris Craikc1c5f082013-09-11 16:23:37 -0700331 RestoreToCountBatch(const StateOp* op, const DeferredDisplayState* state, int restoreCount) :
332 mOp(op), mState(state), mRestoreCount(restoreCount) {}
Chris Craikff785832013-03-08 13:12:16 -0800333
Chris Craik527a3aa2013-03-04 10:19:31 -0800334 virtual status_t replay(OpenGLRenderer& renderer, Rect& dirty, int index) {
Chris Craikff785832013-03-08 13:12:16 -0800335 DEFER_LOGD("batch %p restoring to count %d", this, mRestoreCount);
Chris Craik7273daa2013-03-28 11:25:24 -0700336
Chris Craikc1c5f082013-09-11 16:23:37 -0700337 renderer.restoreDisplayState(*mState);
Chris Craikff785832013-03-08 13:12:16 -0800338 renderer.restoreToCount(mRestoreCount);
Chris Craikff785832013-03-08 13:12:16 -0800339 return DrawGlInfo::kStatusDone;
340 }
341
342private:
Chris Craik7273daa2013-03-28 11:25:24 -0700343 // we use the state storage for the RestoreToCountOp, but don't replay the op itself
344 const StateOp* mOp;
Chris Craikc1c5f082013-09-11 16:23:37 -0700345 const DeferredDisplayState* mState;
346
Chris Craikff785832013-03-08 13:12:16 -0800347 /*
348 * The count used here represents the flush() time saveCount. This is as opposed to the
349 * DisplayList record time, or defer() time values (which are RestoreToCountOp's mCount, and
350 * (saveCount + mCount) respectively). Since the count is different from the original
351 * RestoreToCountOp, we don't store a pointer to the op, as elsewhere.
352 */
353 const int mRestoreCount;
354};
355
Chris Craik527a3aa2013-03-04 10:19:31 -0800356#if DEBUG_MERGE_BEHAVIOR
357class BarrierDebugBatch : public Batch {
358 virtual status_t replay(OpenGLRenderer& renderer, Rect& dirty, int index) {
359 renderer.drawScreenSpaceColorRect(0, 0, 10000, 10000, DEBUG_COLOR_BARRIER);
360 return DrawGlInfo::kStatusDrew;
361 }
362};
363#endif
364
Chris Craikff785832013-03-08 13:12:16 -0800365/////////////////////////////////////////////////////////////////////////////////
366// DeferredDisplayList
367/////////////////////////////////////////////////////////////////////////////////
368
369void DeferredDisplayList::resetBatchingState() {
Chris Craikc3566d02013-02-04 16:16:33 -0800370 for (int i = 0; i < kOpBatch_Count; i++) {
Chris Craik527a3aa2013-03-04 10:19:31 -0800371 mBatchLookup[i] = NULL;
372 mMergingBatches[i].clear();
Chris Craikc3566d02013-02-04 16:16:33 -0800373 }
Chris Craik527a3aa2013-03-04 10:19:31 -0800374#if DEBUG_MERGE_BEHAVIOR
375 if (mBatches.size() != 0) {
376 mBatches.add(new BarrierDebugBatch());
377 }
378#endif
379 mEarliestBatchIndex = mBatches.size();
Chris Craikff785832013-03-08 13:12:16 -0800380}
381
382void DeferredDisplayList::clear() {
383 resetBatchingState();
384 mComplexClipStackStart = -1;
385
Chris Craikc3566d02013-02-04 16:16:33 -0800386 for (unsigned int i = 0; i < mBatches.size(); i++) {
387 delete mBatches[i];
388 }
389 mBatches.clear();
Chris Craikff785832013-03-08 13:12:16 -0800390 mSaveStack.clear();
Chris Craik527a3aa2013-03-04 10:19:31 -0800391 mEarliestBatchIndex = 0;
Chris Craik28ce94a2013-05-31 11:38:03 -0700392 mEarliestUnclearedIndex = 0;
Chris Craikc3566d02013-02-04 16:16:33 -0800393}
394
Chris Craikff785832013-03-08 13:12:16 -0800395/////////////////////////////////////////////////////////////////////////////////
396// Operation adding
397/////////////////////////////////////////////////////////////////////////////////
398
399int DeferredDisplayList::getStateOpDeferFlags() const {
400 // For both clipOp and save(Layer)Op, we don't want to save drawing info, and only want to save
401 // the clip if we aren't recording a complex clip (and can thus trust it to be a rect)
402 return recordingComplexClip() ? 0 : kStateDeferFlag_Clip;
403}
404
405int DeferredDisplayList::getDrawOpDeferFlags() const {
406 return kStateDeferFlag_Draw | getStateOpDeferFlags();
407}
408
409/**
410 * When an clipping operation occurs that could cause a complex clip, record the operation and all
411 * subsequent clipOps, save/restores (if the clip flag is set). During a flush, instead of loading
412 * the clip from deferred state, we play back all of the relevant state operations that generated
413 * the complex clip.
414 *
415 * Note that we don't need to record the associated restore operation, since operations at defer
416 * time record whether they should store the renderer's current clip
417 */
418void DeferredDisplayList::addClip(OpenGLRenderer& renderer, ClipOp* op) {
419 if (recordingComplexClip() || op->canCauseComplexClip() || !renderer.hasRectToRectTransform()) {
420 DEFER_LOGD("%p Received complex clip operation %p", this, op);
421
422 // NOTE: defer clip op before setting mComplexClipStackStart so previous clip is recorded
423 storeStateOpBarrier(renderer, op);
424
425 if (!recordingComplexClip()) {
426 mComplexClipStackStart = renderer.getSaveCount() - 1;
427 DEFER_LOGD(" Starting complex clip region, start is %d", mComplexClipStackStart);
Chris Craikc3566d02013-02-04 16:16:33 -0800428 }
Chris Craikff785832013-03-08 13:12:16 -0800429 }
430}
431
432/**
433 * For now, we record save layer operations as barriers in the batch list, preventing drawing
434 * operations from reordering around the saveLayer and it's associated restore()
435 *
436 * In the future, we should send saveLayer commands (if they can be played out of order) and their
437 * contained drawing operations to a seperate list of batches, so that they may draw at the
438 * beginning of the frame. This would avoid targetting and removing an FBO in the middle of a frame.
439 *
440 * saveLayer operations should be pulled to the beginning of the frame if the canvas doesn't have a
441 * complex clip, and if the flags (kClip_SaveFlag & kClipToLayer_SaveFlag) are set.
442 */
443void DeferredDisplayList::addSaveLayer(OpenGLRenderer& renderer,
444 SaveLayerOp* op, int newSaveCount) {
445 DEFER_LOGD("%p adding saveLayerOp %p, flags %x, new count %d",
446 this, op, op->getFlags(), newSaveCount);
447
448 storeStateOpBarrier(renderer, op);
449 mSaveStack.push(newSaveCount);
450}
451
452/**
453 * Takes save op and it's return value - the new save count - and stores it into the stream as a
454 * barrier if it's needed to properly modify a complex clip
455 */
456void DeferredDisplayList::addSave(OpenGLRenderer& renderer, SaveOp* op, int newSaveCount) {
457 int saveFlags = op->getFlags();
458 DEFER_LOGD("%p adding saveOp %p, flags %x, new count %d", this, op, saveFlags, newSaveCount);
459
460 if (recordingComplexClip() && (saveFlags & SkCanvas::kClip_SaveFlag)) {
461 // store and replay the save operation, as it may be needed to correctly playback the clip
462 DEFER_LOGD(" adding save barrier with new save count %d", newSaveCount);
463 storeStateOpBarrier(renderer, op);
464 mSaveStack.push(newSaveCount);
465 }
466}
467
468/**
469 * saveLayer() commands must be associated with a restoreToCount batch that will clean up and draw
470 * the layer in the deferred list
471 *
472 * other save() commands which occur as children of a snapshot with complex clip will be deferred,
473 * and must be restored
474 *
475 * Either will act as a barrier to draw operation reordering, as we want to play back layer
476 * save/restore and complex canvas modifications (including save/restore) in order.
477 */
Chris Craik7273daa2013-03-28 11:25:24 -0700478void DeferredDisplayList::addRestoreToCount(OpenGLRenderer& renderer, StateOp* op,
479 int newSaveCount) {
Chris Craikff785832013-03-08 13:12:16 -0800480 DEFER_LOGD("%p addRestoreToCount %d", this, newSaveCount);
481
482 if (recordingComplexClip() && newSaveCount <= mComplexClipStackStart) {
483 mComplexClipStackStart = -1;
484 resetBatchingState();
485 }
486
487 if (mSaveStack.isEmpty() || newSaveCount > mSaveStack.top()) {
488 return;
489 }
490
491 while (!mSaveStack.isEmpty() && mSaveStack.top() >= newSaveCount) mSaveStack.pop();
492
Chris Craik1ed30c92013-04-03 12:37:35 -0700493 storeRestoreToCountBarrier(renderer, op, mSaveStack.size() + FLUSH_SAVE_STACK_DEPTH);
Chris Craikff785832013-03-08 13:12:16 -0800494}
495
496void DeferredDisplayList::addDrawOp(OpenGLRenderer& renderer, DrawOp* op) {
Chris Craikc1c5f082013-09-11 16:23:37 -0700497 /* 1: op calculates local bounds */
498 DeferredDisplayState* const state = createState();
499 if (op->getLocalBounds(renderer.getDrawModifiers(), state->mBounds)) {
500 if (state->mBounds.isEmpty()) {
501 // valid empty bounds, don't bother deferring
502 tryRecycleState(state);
503 return;
504 }
505 } else {
506 state->mBounds.setEmpty();
507 }
508
509 /* 2: renderer calculates global bounds + stores state */
510 if (renderer.storeDisplayState(*state, getDrawOpDeferFlags())) {
511 tryRecycleState(state);
Chris Craikff785832013-03-08 13:12:16 -0800512 return; // quick rejected
513 }
514
Chris Craikc1c5f082013-09-11 16:23:37 -0700515 /* 3: ask op for defer info, given renderer state */
Chris Craik28ce94a2013-05-31 11:38:03 -0700516 DeferInfo deferInfo;
Chris Craikc1c5f082013-09-11 16:23:37 -0700517 op->onDefer(renderer, deferInfo, *state);
Chris Craik527a3aa2013-03-04 10:19:31 -0800518
519 // complex clip has a complex set of expectations on the renderer state - for now, avoid taking
520 // the merge path in those cases
Chris Craik28ce94a2013-05-31 11:38:03 -0700521 deferInfo.mergeable &= !recordingComplexClip();
Chris Craik0e87f002013-06-19 16:54:59 -0700522 deferInfo.opaqueOverBounds &= !recordingComplexClip() && mSaveStack.isEmpty();
Chris Craik28ce94a2013-05-31 11:38:03 -0700523
524 if (CC_LIKELY(mAvoidOverdraw) && mBatches.size() &&
Chris Craikc1c5f082013-09-11 16:23:37 -0700525 state->mClipSideFlags != kClipSide_ConservativeFull &&
526 deferInfo.opaqueOverBounds && state->mBounds.contains(mBounds)) {
Chris Craikf70119c2013-06-13 11:21:22 -0700527 // avoid overdraw by resetting drawing state + discarding drawing ops
Chris Craik28ce94a2013-05-31 11:38:03 -0700528 discardDrawingBatches(mBatches.size() - 1);
Chris Craikf70119c2013-06-13 11:21:22 -0700529 resetBatchingState();
Chris Craik28ce94a2013-05-31 11:38:03 -0700530 }
Chris Craikff785832013-03-08 13:12:16 -0800531
532 if (CC_UNLIKELY(renderer.getCaches().drawReorderDisabled)) {
533 // TODO: elegant way to reuse batches?
Chris Craik28ce94a2013-05-31 11:38:03 -0700534 DrawBatch* b = new DrawBatch(deferInfo);
Chris Craikc1c5f082013-09-11 16:23:37 -0700535 b->add(op, state, deferInfo.opaqueOverBounds);
Chris Craikc3566d02013-02-04 16:16:33 -0800536 mBatches.add(b);
537 return;
538 }
539
Chris Craik527a3aa2013-03-04 10:19:31 -0800540 // find the latest batch of the new op's type, and try to merge the new op into it
541 DrawBatch* targetBatch = NULL;
Chris Craikc3566d02013-02-04 16:16:33 -0800542
Chris Craik527a3aa2013-03-04 10:19:31 -0800543 // insertion point of a new batch, will hopefully be immediately after similar batch
544 // (eventually, should be similar shader)
545 int insertBatchIndex = mBatches.size();
Chris Craikc3566d02013-02-04 16:16:33 -0800546 if (!mBatches.isEmpty()) {
Chris Craikc1c5f082013-09-11 16:23:37 -0700547 if (state->mBounds.isEmpty()) {
Chris Craikc3566d02013-02-04 16:16:33 -0800548 // 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 -0700549 DrawBatch* b = new DrawBatch(deferInfo);
Chris Craikc1c5f082013-09-11 16:23:37 -0700550 b->add(op, state, deferInfo.opaqueOverBounds);
Chris Craik527a3aa2013-03-04 10:19:31 -0800551 mBatches.add(b);
552 resetBatchingState();
Chris Craikc3566d02013-02-04 16:16:33 -0800553#if DEBUG_DEFER
554 DEFER_LOGD("Warning: Encountered op with empty bounds, resetting batches");
555 op->output(2);
556#endif
557 return;
558 }
559
Chris Craik28ce94a2013-05-31 11:38:03 -0700560 if (deferInfo.mergeable) {
Chris Craik527a3aa2013-03-04 10:19:31 -0800561 // Try to merge with any existing batch with same mergeId.
Chris Craik28ce94a2013-05-31 11:38:03 -0700562 if (mMergingBatches[deferInfo.batchId].get(deferInfo.mergeId, targetBatch)) {
Chris Craikc1c5f082013-09-11 16:23:37 -0700563 if (!((MergingDrawBatch*) targetBatch)->canMergeWith(op, state)) {
Chris Craik527a3aa2013-03-04 10:19:31 -0800564 targetBatch = NULL;
565 }
566 }
567 } else {
568 // join with similar, non-merging batch
Chris Craik28ce94a2013-05-31 11:38:03 -0700569 targetBatch = (DrawBatch*)mBatchLookup[deferInfo.batchId];
Chris Craik527a3aa2013-03-04 10:19:31 -0800570 }
571
Chris Craik28ce94a2013-05-31 11:38:03 -0700572 if (targetBatch || deferInfo.mergeable) {
Chris Craikc3566d02013-02-04 16:16:33 -0800573 // iterate back toward target to see if anything drawn since should overlap the new op
Chris Craik527a3aa2013-03-04 10:19:31 -0800574 // if no target, merging ops still interate to find similar batch to insert after
575 for (int i = mBatches.size() - 1; i >= mEarliestBatchIndex; i--) {
576 DrawBatch* overBatch = (DrawBatch*)mBatches[i];
577
578 if (overBatch == targetBatch) break;
579
580 // TODO: also consider shader shared between batch types
Chris Craik28ce94a2013-05-31 11:38:03 -0700581 if (deferInfo.batchId == overBatch->getBatchId()) {
Chris Craik527a3aa2013-03-04 10:19:31 -0800582 insertBatchIndex = i + 1;
583 if (!targetBatch) break; // found insert position, quit
584 }
585
Chris Craikc1c5f082013-09-11 16:23:37 -0700586 if (overBatch->intersects(state->mBounds)) {
Chris Craik527a3aa2013-03-04 10:19:31 -0800587 // NOTE: it may be possible to optimize for special cases where two operations
588 // of the same batch/paint could swap order, such as with a non-mergeable
589 // (clipped) and a mergeable text operation
Chris Craikc3566d02013-02-04 16:16:33 -0800590 targetBatch = NULL;
591#if DEBUG_DEFER
Chris Craikc1c5f082013-09-11 16:23:37 -0700592 DEFER_LOGD("op couldn't join batch %p, was intersected by batch %d",
593 targetBatch, i);
Chris Craikc3566d02013-02-04 16:16:33 -0800594 op->output(2);
595#endif
596 break;
597 }
598 }
599 }
600 }
Chris Craik527a3aa2013-03-04 10:19:31 -0800601
Chris Craikc3566d02013-02-04 16:16:33 -0800602 if (!targetBatch) {
Chris Craik28ce94a2013-05-31 11:38:03 -0700603 if (deferInfo.mergeable) {
Chris Craik0e87f002013-06-19 16:54:59 -0700604 targetBatch = new MergingDrawBatch(deferInfo,
605 renderer.getViewportWidth(), renderer.getViewportHeight());
Chris Craik28ce94a2013-05-31 11:38:03 -0700606 mMergingBatches[deferInfo.batchId].put(deferInfo.mergeId, targetBatch);
Chris Craik527a3aa2013-03-04 10:19:31 -0800607 } else {
Chris Craik28ce94a2013-05-31 11:38:03 -0700608 targetBatch = new DrawBatch(deferInfo);
609 mBatchLookup[deferInfo.batchId] = targetBatch;
Chris Craikc3566d02013-02-04 16:16:33 -0800610 }
Chris Craik527a3aa2013-03-04 10:19:31 -0800611
Chris Craikf70119c2013-06-13 11:21:22 -0700612 DEFER_LOGD("creating %singBatch %p, bid %x, at %d",
613 deferInfo.mergeable ? "Merg" : "Draw",
614 targetBatch, deferInfo.batchId, insertBatchIndex);
Chris Craik527a3aa2013-03-04 10:19:31 -0800615 mBatches.insertAt(targetBatch, insertBatchIndex);
Chris Craikc3566d02013-02-04 16:16:33 -0800616 }
Chris Craik527a3aa2013-03-04 10:19:31 -0800617
Chris Craikc1c5f082013-09-11 16:23:37 -0700618 targetBatch->add(op, state, deferInfo.opaqueOverBounds);
Chris Craikc3566d02013-02-04 16:16:33 -0800619}
620
Chris Craikff785832013-03-08 13:12:16 -0800621void DeferredDisplayList::storeStateOpBarrier(OpenGLRenderer& renderer, StateOp* op) {
622 DEFER_LOGD("%p adding state op barrier at pos %d", this, mBatches.size());
623
Chris Craikc1c5f082013-09-11 16:23:37 -0700624 DeferredDisplayState* state = createState();
625 renderer.storeDisplayState(*state, getStateOpDeferFlags());
626 mBatches.add(new StateOpBatch(op, state));
Chris Craikff785832013-03-08 13:12:16 -0800627 resetBatchingState();
628}
629
Chris Craik7273daa2013-03-28 11:25:24 -0700630void DeferredDisplayList::storeRestoreToCountBarrier(OpenGLRenderer& renderer, StateOp* op,
631 int newSaveCount) {
Chris Craikff785832013-03-08 13:12:16 -0800632 DEFER_LOGD("%p adding restore to count %d barrier, pos %d",
633 this, newSaveCount, mBatches.size());
634
Chris Craik7273daa2013-03-28 11:25:24 -0700635 // store displayState for the restore operation, as it may be associated with a saveLayer that
636 // doesn't have kClip_SaveFlag set
Chris Craikc1c5f082013-09-11 16:23:37 -0700637 DeferredDisplayState* state = createState();
638 renderer.storeDisplayState(*state, getStateOpDeferFlags());
639 mBatches.add(new RestoreToCountBatch(op, state, newSaveCount));
Chris Craikff785832013-03-08 13:12:16 -0800640 resetBatchingState();
641}
642
643/////////////////////////////////////////////////////////////////////////////////
644// Replay / flush
645/////////////////////////////////////////////////////////////////////////////////
646
Chris Craik527a3aa2013-03-04 10:19:31 -0800647static status_t replayBatchList(const Vector<Batch*>& batchList,
Chris Craikff785832013-03-08 13:12:16 -0800648 OpenGLRenderer& renderer, Rect& dirty) {
649 status_t status = DrawGlInfo::kStatusDone;
650
Chris Craikff785832013-03-08 13:12:16 -0800651 for (unsigned int i = 0; i < batchList.size(); i++) {
Chris Craik28ce94a2013-05-31 11:38:03 -0700652 if (batchList[i]) {
653 status |= batchList[i]->replay(renderer, dirty, i);
654 }
Chris Craikff785832013-03-08 13:12:16 -0800655 }
Chris Craik527a3aa2013-03-04 10:19:31 -0800656 DEFER_LOGD("--flushed, drew %d batches", batchList.size());
Chris Craikff785832013-03-08 13:12:16 -0800657 return status;
658}
659
660status_t DeferredDisplayList::flush(OpenGLRenderer& renderer, Rect& dirty) {
661 ATRACE_NAME("flush drawing commands");
Romain Guycf51a412013-04-08 19:40:31 -0700662 Caches::getInstance().fontRenderer->endPrecaching();
663
Chris Craikc3566d02013-02-04 16:16:33 -0800664 status_t status = DrawGlInfo::kStatusDone;
665
666 if (isEmpty()) return status; // nothing to flush
Chris Craika4e16c52013-03-22 10:00:48 -0700667 renderer.restoreToCount(1);
Chris Craikc3566d02013-02-04 16:16:33 -0800668
669 DEFER_LOGD("--flushing");
Romain Guy0f667532013-03-01 14:31:04 -0800670 renderer.eventMark("Flush");
671
Chris Craika4e16c52013-03-22 10:00:48 -0700672 // save and restore (with draw modifiers) so that reordering doesn't affect final state
Chris Craikd90144d2013-03-19 15:03:48 -0700673 DrawModifiers restoreDrawModifiers = renderer.getDrawModifiers();
Chris Craika4e16c52013-03-22 10:00:48 -0700674 renderer.save(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
675
Chris Craik28ce94a2013-05-31 11:38:03 -0700676 if (CC_LIKELY(mAvoidOverdraw)) {
677 for (unsigned int i = 1; i < mBatches.size(); i++) {
678 if (mBatches[i] && mBatches[i]->coversBounds(mBounds)) {
679 discardDrawingBatches(i - 1);
680 }
681 }
682 }
Chris Craik1ed30c92013-04-03 12:37:35 -0700683 // NOTE: depth of the save stack at this point, before playback, should be reflected in
684 // FLUSH_SAVE_STACK_DEPTH, so that save/restores match up correctly
Chris Craikff785832013-03-08 13:12:16 -0800685 status |= replayBatchList(mBatches, renderer, dirty);
Chris Craika4e16c52013-03-22 10:00:48 -0700686
687 renderer.restoreToCount(1);
Chris Craikd90144d2013-03-19 15:03:48 -0700688 renderer.setDrawModifiers(restoreDrawModifiers);
Chris Craikc3566d02013-02-04 16:16:33 -0800689
Chris Craikff785832013-03-08 13:12:16 -0800690 DEFER_LOGD("--flush complete, returning %x", status);
Chris Craikc3566d02013-02-04 16:16:33 -0800691 clear();
692 return status;
693}
694
Chris Craikf70119c2013-06-13 11:21:22 -0700695void DeferredDisplayList::discardDrawingBatches(const unsigned int maxIndex) {
Chris Craik28ce94a2013-05-31 11:38:03 -0700696 for (unsigned int i = mEarliestUnclearedIndex; i <= maxIndex; i++) {
Chris Craikf70119c2013-06-13 11:21:22 -0700697 // leave deferred state ops alone for simplicity (empty save restore pairs may now exist)
Chris Craik28ce94a2013-05-31 11:38:03 -0700698 if (mBatches[i] && mBatches[i]->purelyDrawBatch()) {
699 DrawBatch* b = (DrawBatch*) mBatches[i];
700 delete mBatches[i];
701 mBatches.replaceAt(NULL, i);
702 }
703 }
704 mEarliestUnclearedIndex = maxIndex + 1;
705}
706
Chris Craikc3566d02013-02-04 16:16:33 -0800707}; // namespace uirenderer
708}; // namespace android