blob: a998594747d0bdc66da81e1e8efd89fb365d71d5 [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
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400233 if (op->mPaint && mOps[0].op->mPaint &&
234 op->mPaint->getShader() != mOps[0].op->mPaint->getShader()) {
235 return false;
236 }
237
Chris Craik527a3aa2013-03-04 10:19:31 -0800238 /* Draw Modifiers compatibility check
239 *
240 * Shadows are ignored, as only text uses them, and in that case they are drawn
241 * per-DrawTextOp, before the unified text draw. Because of this, it's always safe to merge
242 * text UNLESS a later draw's shadow should overlays a previous draw's text. This is covered
243 * above with the intersection check.
244 *
245 * OverrideLayerAlpha is also ignored, as it's only used for drawing layers, which are never
246 * merged.
247 *
248 * These ignore cases prevent us from simply memcmp'ing the drawModifiers
249 */
Chris Craikc1c5f082013-09-11 16:23:37 -0700250 const DrawModifiers& lhsMod = lhs->mDrawModifiers;
251 const DrawModifiers& rhsMod = rhs->mDrawModifiers;
Chris Craik527a3aa2013-03-04 10:19:31 -0800252
253 // Draw filter testing expects bit fields to be clear if filter not set.
254 if (lhsMod.mHasDrawFilter != rhsMod.mHasDrawFilter) return false;
255 if (lhsMod.mPaintFilterClearBits != rhsMod.mPaintFilterClearBits) return false;
256 if (lhsMod.mPaintFilterSetBits != rhsMod.mPaintFilterSetBits) return false;
257
258 return true;
259 }
260
Chris Craik5af5fc52013-09-13 14:41:31 -0700261 virtual void add(DrawOp* op, const DeferredDisplayState* state, bool opaqueOverBounds) {
Chris Craikc1c5f082013-09-11 16:23:37 -0700262 DrawBatch::add(op, state, opaqueOverBounds);
Chris Craik28ce94a2013-05-31 11:38:03 -0700263
Chris Craikc1c5f082013-09-11 16:23:37 -0700264 const int newClipSideFlags = state->mClipSideFlags;
Chris Craik28ce94a2013-05-31 11:38:03 -0700265 mClipSideFlags |= newClipSideFlags;
Chris Craikc1c5f082013-09-11 16:23:37 -0700266 if (newClipSideFlags & kClipSide_Left) mClipRect.left = state->mClip.left;
267 if (newClipSideFlags & kClipSide_Top) mClipRect.top = state->mClip.top;
268 if (newClipSideFlags & kClipSide_Right) mClipRect.right = state->mClip.right;
269 if (newClipSideFlags & kClipSide_Bottom) mClipRect.bottom = state->mClip.bottom;
Chris Craik28ce94a2013-05-31 11:38:03 -0700270 }
271
Chris Craik527a3aa2013-03-04 10:19:31 -0800272 virtual status_t replay(OpenGLRenderer& renderer, Rect& dirty, int index) {
Chris Craik28ce94a2013-05-31 11:38:03 -0700273 DEFER_LOGD("%d replaying MergingDrawBatch %p, with %d ops,"
274 " clip flags %x (batch id %x, merge id %p)",
275 index, this, mOps.size(), mClipSideFlags, getBatchId(), getMergeId());
Chris Craik527a3aa2013-03-04 10:19:31 -0800276 if (mOps.size() == 1) {
Chris Craik28ce94a2013-05-31 11:38:03 -0700277 return DrawBatch::replay(renderer, dirty, -1);
Chris Craik527a3aa2013-03-04 10:19:31 -0800278 }
279
Chris Craik28ce94a2013-05-31 11:38:03 -0700280 // clipping in the merged case is done ahead of time since all ops share the clip (if any)
281 renderer.setupMergedMultiDraw(mClipSideFlags ? &mClipRect : NULL);
282
Chris Craikc1c5f082013-09-11 16:23:37 -0700283 DrawOp* op = mOps[0].op;
Chris Craik527a3aa2013-03-04 10:19:31 -0800284 DisplayListLogBuffer& buffer = DisplayListLogBuffer::getInstance();
285 buffer.writeCommand(0, "multiDraw");
286 buffer.writeCommand(1, op->name());
Chris Craikd965bc52013-09-16 14:47:13 -0700287
288#if DEBUG_DISPLAY_LIST_OPS_AS_EVENTS
289 renderer.eventMark("multiDraw");
290 renderer.eventMark(op->name());
291#endif
Chris Craikd4b43b32013-05-09 13:07:52 -0700292 status_t status = op->multiDraw(renderer, dirty, mOps, mBounds);
Chris Craik527a3aa2013-03-04 10:19:31 -0800293
294#if DEBUG_MERGE_BEHAVIOR
295 renderer.drawScreenSpaceColorRect(mBounds.left, mBounds.top, mBounds.right, mBounds.bottom,
296 DEBUG_COLOR_MERGEDBATCH);
297#endif
298 return status;
299 }
Chris Craik28ce94a2013-05-31 11:38:03 -0700300
301private:
302 /*
303 * Contains the effective clip rect shared by all merged ops. Initialized to the layer viewport,
304 * it will shrink if an op must be clipped on a certain side. The clipped sides are reflected in
305 * mClipSideFlags.
306 */
307 Rect mClipRect;
308 int mClipSideFlags;
Chris Craik527a3aa2013-03-04 10:19:31 -0800309};
310
311class StateOpBatch : public Batch {
Chris Craikff785832013-03-08 13:12:16 -0800312public:
313 // creates a single operation batch
Chris Craikc1c5f082013-09-11 16:23:37 -0700314 StateOpBatch(const StateOp* op, const DeferredDisplayState* state) : mOp(op), mState(state) {}
Chris Craikff785832013-03-08 13:12:16 -0800315
Chris Craik527a3aa2013-03-04 10:19:31 -0800316 virtual status_t replay(OpenGLRenderer& renderer, Rect& dirty, int index) {
Chris Craikff785832013-03-08 13:12:16 -0800317 DEFER_LOGD("replaying state op batch %p", this);
Chris Craikc1c5f082013-09-11 16:23:37 -0700318 renderer.restoreDisplayState(*mState);
Chris Craikff785832013-03-08 13:12:16 -0800319
320 // use invalid save count because it won't be used at flush time - RestoreToCountOp is the
321 // only one to use it, and we don't use that class at flush time, instead calling
322 // renderer.restoreToCount directly
323 int saveCount = -1;
324 mOp->applyState(renderer, saveCount);
325 return DrawGlInfo::kStatusDone;
326 }
327
328private:
Chris Craik7273daa2013-03-28 11:25:24 -0700329 const StateOp* mOp;
Chris Craikc1c5f082013-09-11 16:23:37 -0700330 const DeferredDisplayState* mState;
Chris Craikff785832013-03-08 13:12:16 -0800331};
332
Chris Craik527a3aa2013-03-04 10:19:31 -0800333class RestoreToCountBatch : public Batch {
Chris Craikff785832013-03-08 13:12:16 -0800334public:
Chris Craikc1c5f082013-09-11 16:23:37 -0700335 RestoreToCountBatch(const StateOp* op, const DeferredDisplayState* state, int restoreCount) :
336 mOp(op), mState(state), mRestoreCount(restoreCount) {}
Chris Craikff785832013-03-08 13:12:16 -0800337
Chris Craik527a3aa2013-03-04 10:19:31 -0800338 virtual status_t replay(OpenGLRenderer& renderer, Rect& dirty, int index) {
Chris Craikff785832013-03-08 13:12:16 -0800339 DEFER_LOGD("batch %p restoring to count %d", this, mRestoreCount);
Chris Craik7273daa2013-03-28 11:25:24 -0700340
Chris Craikc1c5f082013-09-11 16:23:37 -0700341 renderer.restoreDisplayState(*mState);
Chris Craikff785832013-03-08 13:12:16 -0800342 renderer.restoreToCount(mRestoreCount);
Chris Craikff785832013-03-08 13:12:16 -0800343 return DrawGlInfo::kStatusDone;
344 }
345
346private:
Chris Craik7273daa2013-03-28 11:25:24 -0700347 // we use the state storage for the RestoreToCountOp, but don't replay the op itself
348 const StateOp* mOp;
Chris Craikc1c5f082013-09-11 16:23:37 -0700349 const DeferredDisplayState* mState;
350
Chris Craikff785832013-03-08 13:12:16 -0800351 /*
352 * The count used here represents the flush() time saveCount. This is as opposed to the
353 * DisplayList record time, or defer() time values (which are RestoreToCountOp's mCount, and
354 * (saveCount + mCount) respectively). Since the count is different from the original
355 * RestoreToCountOp, we don't store a pointer to the op, as elsewhere.
356 */
357 const int mRestoreCount;
358};
359
Chris Craik527a3aa2013-03-04 10:19:31 -0800360#if DEBUG_MERGE_BEHAVIOR
361class BarrierDebugBatch : public Batch {
362 virtual status_t replay(OpenGLRenderer& renderer, Rect& dirty, int index) {
363 renderer.drawScreenSpaceColorRect(0, 0, 10000, 10000, DEBUG_COLOR_BARRIER);
364 return DrawGlInfo::kStatusDrew;
365 }
366};
367#endif
368
Chris Craikff785832013-03-08 13:12:16 -0800369/////////////////////////////////////////////////////////////////////////////////
370// DeferredDisplayList
371/////////////////////////////////////////////////////////////////////////////////
372
373void DeferredDisplayList::resetBatchingState() {
Chris Craikc3566d02013-02-04 16:16:33 -0800374 for (int i = 0; i < kOpBatch_Count; i++) {
Chris Craik527a3aa2013-03-04 10:19:31 -0800375 mBatchLookup[i] = NULL;
376 mMergingBatches[i].clear();
Chris Craikc3566d02013-02-04 16:16:33 -0800377 }
Chris Craik527a3aa2013-03-04 10:19:31 -0800378#if DEBUG_MERGE_BEHAVIOR
379 if (mBatches.size() != 0) {
380 mBatches.add(new BarrierDebugBatch());
381 }
382#endif
383 mEarliestBatchIndex = mBatches.size();
Chris Craikff785832013-03-08 13:12:16 -0800384}
385
386void DeferredDisplayList::clear() {
387 resetBatchingState();
388 mComplexClipStackStart = -1;
389
Chris Craikc3566d02013-02-04 16:16:33 -0800390 for (unsigned int i = 0; i < mBatches.size(); i++) {
391 delete mBatches[i];
392 }
393 mBatches.clear();
Chris Craikff785832013-03-08 13:12:16 -0800394 mSaveStack.clear();
Chris Craik527a3aa2013-03-04 10:19:31 -0800395 mEarliestBatchIndex = 0;
Chris Craik28ce94a2013-05-31 11:38:03 -0700396 mEarliestUnclearedIndex = 0;
Chris Craikc3566d02013-02-04 16:16:33 -0800397}
398
Chris Craikff785832013-03-08 13:12:16 -0800399/////////////////////////////////////////////////////////////////////////////////
400// Operation adding
401/////////////////////////////////////////////////////////////////////////////////
402
403int DeferredDisplayList::getStateOpDeferFlags() const {
404 // For both clipOp and save(Layer)Op, we don't want to save drawing info, and only want to save
405 // the clip if we aren't recording a complex clip (and can thus trust it to be a rect)
406 return recordingComplexClip() ? 0 : kStateDeferFlag_Clip;
407}
408
409int DeferredDisplayList::getDrawOpDeferFlags() const {
410 return kStateDeferFlag_Draw | getStateOpDeferFlags();
411}
412
413/**
414 * When an clipping operation occurs that could cause a complex clip, record the operation and all
415 * subsequent clipOps, save/restores (if the clip flag is set). During a flush, instead of loading
416 * the clip from deferred state, we play back all of the relevant state operations that generated
417 * the complex clip.
418 *
419 * Note that we don't need to record the associated restore operation, since operations at defer
420 * time record whether they should store the renderer's current clip
421 */
422void DeferredDisplayList::addClip(OpenGLRenderer& renderer, ClipOp* op) {
423 if (recordingComplexClip() || op->canCauseComplexClip() || !renderer.hasRectToRectTransform()) {
424 DEFER_LOGD("%p Received complex clip operation %p", this, op);
425
426 // NOTE: defer clip op before setting mComplexClipStackStart so previous clip is recorded
427 storeStateOpBarrier(renderer, op);
428
429 if (!recordingComplexClip()) {
430 mComplexClipStackStart = renderer.getSaveCount() - 1;
431 DEFER_LOGD(" Starting complex clip region, start is %d", mComplexClipStackStart);
Chris Craikc3566d02013-02-04 16:16:33 -0800432 }
Chris Craikff785832013-03-08 13:12:16 -0800433 }
434}
435
436/**
437 * For now, we record save layer operations as barriers in the batch list, preventing drawing
438 * operations from reordering around the saveLayer and it's associated restore()
439 *
440 * In the future, we should send saveLayer commands (if they can be played out of order) and their
441 * contained drawing operations to a seperate list of batches, so that they may draw at the
442 * beginning of the frame. This would avoid targetting and removing an FBO in the middle of a frame.
443 *
444 * saveLayer operations should be pulled to the beginning of the frame if the canvas doesn't have a
445 * complex clip, and if the flags (kClip_SaveFlag & kClipToLayer_SaveFlag) are set.
446 */
447void DeferredDisplayList::addSaveLayer(OpenGLRenderer& renderer,
448 SaveLayerOp* op, int newSaveCount) {
449 DEFER_LOGD("%p adding saveLayerOp %p, flags %x, new count %d",
450 this, op, op->getFlags(), newSaveCount);
451
452 storeStateOpBarrier(renderer, op);
453 mSaveStack.push(newSaveCount);
454}
455
456/**
457 * Takes save op and it's return value - the new save count - and stores it into the stream as a
458 * barrier if it's needed to properly modify a complex clip
459 */
460void DeferredDisplayList::addSave(OpenGLRenderer& renderer, SaveOp* op, int newSaveCount) {
461 int saveFlags = op->getFlags();
462 DEFER_LOGD("%p adding saveOp %p, flags %x, new count %d", this, op, saveFlags, newSaveCount);
463
464 if (recordingComplexClip() && (saveFlags & SkCanvas::kClip_SaveFlag)) {
465 // store and replay the save operation, as it may be needed to correctly playback the clip
466 DEFER_LOGD(" adding save barrier with new save count %d", newSaveCount);
467 storeStateOpBarrier(renderer, op);
468 mSaveStack.push(newSaveCount);
469 }
470}
471
472/**
473 * saveLayer() commands must be associated with a restoreToCount batch that will clean up and draw
474 * the layer in the deferred list
475 *
476 * other save() commands which occur as children of a snapshot with complex clip will be deferred,
477 * and must be restored
478 *
479 * Either will act as a barrier to draw operation reordering, as we want to play back layer
480 * save/restore and complex canvas modifications (including save/restore) in order.
481 */
Chris Craik7273daa2013-03-28 11:25:24 -0700482void DeferredDisplayList::addRestoreToCount(OpenGLRenderer& renderer, StateOp* op,
483 int newSaveCount) {
Chris Craikff785832013-03-08 13:12:16 -0800484 DEFER_LOGD("%p addRestoreToCount %d", this, newSaveCount);
485
486 if (recordingComplexClip() && newSaveCount <= mComplexClipStackStart) {
487 mComplexClipStackStart = -1;
488 resetBatchingState();
489 }
490
491 if (mSaveStack.isEmpty() || newSaveCount > mSaveStack.top()) {
492 return;
493 }
494
495 while (!mSaveStack.isEmpty() && mSaveStack.top() >= newSaveCount) mSaveStack.pop();
496
Chris Craik1ed30c92013-04-03 12:37:35 -0700497 storeRestoreToCountBarrier(renderer, op, mSaveStack.size() + FLUSH_SAVE_STACK_DEPTH);
Chris Craikff785832013-03-08 13:12:16 -0800498}
499
500void DeferredDisplayList::addDrawOp(OpenGLRenderer& renderer, DrawOp* op) {
Chris Craikc1c5f082013-09-11 16:23:37 -0700501 /* 1: op calculates local bounds */
502 DeferredDisplayState* const state = createState();
John Reck3b202512014-06-23 13:13:08 -0700503 if (op->getLocalBounds(state->mBounds)) {
Chris Craikc1c5f082013-09-11 16:23:37 -0700504 if (state->mBounds.isEmpty()) {
505 // valid empty bounds, don't bother deferring
506 tryRecycleState(state);
507 return;
508 }
509 } else {
510 state->mBounds.setEmpty();
511 }
512
513 /* 2: renderer calculates global bounds + stores state */
514 if (renderer.storeDisplayState(*state, getDrawOpDeferFlags())) {
515 tryRecycleState(state);
Chris Craikff785832013-03-08 13:12:16 -0800516 return; // quick rejected
517 }
518
Chris Craikc1c5f082013-09-11 16:23:37 -0700519 /* 3: ask op for defer info, given renderer state */
Chris Craik28ce94a2013-05-31 11:38:03 -0700520 DeferInfo deferInfo;
Chris Craikc1c5f082013-09-11 16:23:37 -0700521 op->onDefer(renderer, deferInfo, *state);
Chris Craik527a3aa2013-03-04 10:19:31 -0800522
523 // complex clip has a complex set of expectations on the renderer state - for now, avoid taking
524 // the merge path in those cases
Chris Craik28ce94a2013-05-31 11:38:03 -0700525 deferInfo.mergeable &= !recordingComplexClip();
Chris Craik0e87f002013-06-19 16:54:59 -0700526 deferInfo.opaqueOverBounds &= !recordingComplexClip() && mSaveStack.isEmpty();
Chris Craik28ce94a2013-05-31 11:38:03 -0700527
Chris Craikef8d6f22014-12-17 11:10:28 -0800528 if (CC_LIKELY(mAvoidOverdraw) && mBatches.size() &&
Chris Craikc1c5f082013-09-11 16:23:37 -0700529 state->mClipSideFlags != kClipSide_ConservativeFull &&
530 deferInfo.opaqueOverBounds && state->mBounds.contains(mBounds)) {
Chris Craikf70119c2013-06-13 11:21:22 -0700531 // avoid overdraw by resetting drawing state + discarding drawing ops
Chris Craik28ce94a2013-05-31 11:38:03 -0700532 discardDrawingBatches(mBatches.size() - 1);
Chris Craikf70119c2013-06-13 11:21:22 -0700533 resetBatchingState();
Chris Craik28ce94a2013-05-31 11:38:03 -0700534 }
Chris Craikff785832013-03-08 13:12:16 -0800535
536 if (CC_UNLIKELY(renderer.getCaches().drawReorderDisabled)) {
537 // TODO: elegant way to reuse batches?
Chris Craik28ce94a2013-05-31 11:38:03 -0700538 DrawBatch* b = new DrawBatch(deferInfo);
Chris Craikc1c5f082013-09-11 16:23:37 -0700539 b->add(op, state, deferInfo.opaqueOverBounds);
Chris Craikc3566d02013-02-04 16:16:33 -0800540 mBatches.add(b);
541 return;
542 }
543
Chris Craik527a3aa2013-03-04 10:19:31 -0800544 // find the latest batch of the new op's type, and try to merge the new op into it
545 DrawBatch* targetBatch = NULL;
Chris Craikc3566d02013-02-04 16:16:33 -0800546
Chris Craik527a3aa2013-03-04 10:19:31 -0800547 // insertion point of a new batch, will hopefully be immediately after similar batch
548 // (eventually, should be similar shader)
549 int insertBatchIndex = mBatches.size();
Chris Craikc3566d02013-02-04 16:16:33 -0800550 if (!mBatches.isEmpty()) {
Chris Craikc1c5f082013-09-11 16:23:37 -0700551 if (state->mBounds.isEmpty()) {
Chris Craikc3566d02013-02-04 16:16:33 -0800552 // 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 -0700553 DrawBatch* b = new DrawBatch(deferInfo);
Chris Craikc1c5f082013-09-11 16:23:37 -0700554 b->add(op, state, deferInfo.opaqueOverBounds);
Chris Craik527a3aa2013-03-04 10:19:31 -0800555 mBatches.add(b);
556 resetBatchingState();
Chris Craikc3566d02013-02-04 16:16:33 -0800557#if DEBUG_DEFER
558 DEFER_LOGD("Warning: Encountered op with empty bounds, resetting batches");
559 op->output(2);
560#endif
561 return;
562 }
563
Chris Craik28ce94a2013-05-31 11:38:03 -0700564 if (deferInfo.mergeable) {
Chris Craik527a3aa2013-03-04 10:19:31 -0800565 // Try to merge with any existing batch with same mergeId.
Chris Craik28ce94a2013-05-31 11:38:03 -0700566 if (mMergingBatches[deferInfo.batchId].get(deferInfo.mergeId, targetBatch)) {
Chris Craikc1c5f082013-09-11 16:23:37 -0700567 if (!((MergingDrawBatch*) targetBatch)->canMergeWith(op, state)) {
Chris Craik527a3aa2013-03-04 10:19:31 -0800568 targetBatch = NULL;
569 }
570 }
571 } else {
572 // join with similar, non-merging batch
Chris Craik28ce94a2013-05-31 11:38:03 -0700573 targetBatch = (DrawBatch*)mBatchLookup[deferInfo.batchId];
Chris Craik527a3aa2013-03-04 10:19:31 -0800574 }
575
Chris Craik28ce94a2013-05-31 11:38:03 -0700576 if (targetBatch || deferInfo.mergeable) {
Chris Craikc3566d02013-02-04 16:16:33 -0800577 // iterate back toward target to see if anything drawn since should overlap the new op
Chris Craik527a3aa2013-03-04 10:19:31 -0800578 // if no target, merging ops still interate to find similar batch to insert after
579 for (int i = mBatches.size() - 1; i >= mEarliestBatchIndex; i--) {
580 DrawBatch* overBatch = (DrawBatch*)mBatches[i];
581
582 if (overBatch == targetBatch) break;
583
584 // TODO: also consider shader shared between batch types
Chris Craik28ce94a2013-05-31 11:38:03 -0700585 if (deferInfo.batchId == overBatch->getBatchId()) {
Chris Craik527a3aa2013-03-04 10:19:31 -0800586 insertBatchIndex = i + 1;
587 if (!targetBatch) break; // found insert position, quit
588 }
589
Chris Craikc1c5f082013-09-11 16:23:37 -0700590 if (overBatch->intersects(state->mBounds)) {
Chris Craik527a3aa2013-03-04 10:19:31 -0800591 // NOTE: it may be possible to optimize for special cases where two operations
592 // of the same batch/paint could swap order, such as with a non-mergeable
593 // (clipped) and a mergeable text operation
Chris Craikc3566d02013-02-04 16:16:33 -0800594 targetBatch = NULL;
595#if DEBUG_DEFER
Chris Craikc1c5f082013-09-11 16:23:37 -0700596 DEFER_LOGD("op couldn't join batch %p, was intersected by batch %d",
597 targetBatch, i);
Chris Craikc3566d02013-02-04 16:16:33 -0800598 op->output(2);
599#endif
600 break;
601 }
602 }
603 }
604 }
Chris Craik527a3aa2013-03-04 10:19:31 -0800605
Chris Craikc3566d02013-02-04 16:16:33 -0800606 if (!targetBatch) {
Chris Craik28ce94a2013-05-31 11:38:03 -0700607 if (deferInfo.mergeable) {
Chris Craik0e87f002013-06-19 16:54:59 -0700608 targetBatch = new MergingDrawBatch(deferInfo,
609 renderer.getViewportWidth(), renderer.getViewportHeight());
Chris Craik28ce94a2013-05-31 11:38:03 -0700610 mMergingBatches[deferInfo.batchId].put(deferInfo.mergeId, targetBatch);
Chris Craik527a3aa2013-03-04 10:19:31 -0800611 } else {
Chris Craik28ce94a2013-05-31 11:38:03 -0700612 targetBatch = new DrawBatch(deferInfo);
613 mBatchLookup[deferInfo.batchId] = targetBatch;
Chris Craikc3566d02013-02-04 16:16:33 -0800614 }
Chris Craik527a3aa2013-03-04 10:19:31 -0800615
Chris Craikf70119c2013-06-13 11:21:22 -0700616 DEFER_LOGD("creating %singBatch %p, bid %x, at %d",
617 deferInfo.mergeable ? "Merg" : "Draw",
618 targetBatch, deferInfo.batchId, insertBatchIndex);
Chris Craik527a3aa2013-03-04 10:19:31 -0800619 mBatches.insertAt(targetBatch, insertBatchIndex);
Chris Craikc3566d02013-02-04 16:16:33 -0800620 }
Chris Craik527a3aa2013-03-04 10:19:31 -0800621
Chris Craikc1c5f082013-09-11 16:23:37 -0700622 targetBatch->add(op, state, deferInfo.opaqueOverBounds);
Chris Craikc3566d02013-02-04 16:16:33 -0800623}
624
Chris Craikff785832013-03-08 13:12:16 -0800625void DeferredDisplayList::storeStateOpBarrier(OpenGLRenderer& renderer, StateOp* op) {
626 DEFER_LOGD("%p adding state op barrier at pos %d", this, mBatches.size());
627
Chris Craikc1c5f082013-09-11 16:23:37 -0700628 DeferredDisplayState* state = createState();
629 renderer.storeDisplayState(*state, getStateOpDeferFlags());
630 mBatches.add(new StateOpBatch(op, state));
Chris Craikff785832013-03-08 13:12:16 -0800631 resetBatchingState();
632}
633
Chris Craik7273daa2013-03-28 11:25:24 -0700634void DeferredDisplayList::storeRestoreToCountBarrier(OpenGLRenderer& renderer, StateOp* op,
635 int newSaveCount) {
Chris Craikff785832013-03-08 13:12:16 -0800636 DEFER_LOGD("%p adding restore to count %d barrier, pos %d",
637 this, newSaveCount, mBatches.size());
638
Chris Craik7273daa2013-03-28 11:25:24 -0700639 // store displayState for the restore operation, as it may be associated with a saveLayer that
640 // doesn't have kClip_SaveFlag set
Chris Craikc1c5f082013-09-11 16:23:37 -0700641 DeferredDisplayState* state = createState();
642 renderer.storeDisplayState(*state, getStateOpDeferFlags());
643 mBatches.add(new RestoreToCountBatch(op, state, newSaveCount));
Chris Craikff785832013-03-08 13:12:16 -0800644 resetBatchingState();
645}
646
647/////////////////////////////////////////////////////////////////////////////////
648// Replay / flush
649/////////////////////////////////////////////////////////////////////////////////
650
Chris Craik527a3aa2013-03-04 10:19:31 -0800651static status_t replayBatchList(const Vector<Batch*>& batchList,
Chris Craikff785832013-03-08 13:12:16 -0800652 OpenGLRenderer& renderer, Rect& dirty) {
653 status_t status = DrawGlInfo::kStatusDone;
654
Chris Craikff785832013-03-08 13:12:16 -0800655 for (unsigned int i = 0; i < batchList.size(); i++) {
Chris Craik28ce94a2013-05-31 11:38:03 -0700656 if (batchList[i]) {
657 status |= batchList[i]->replay(renderer, dirty, i);
658 }
Chris Craikff785832013-03-08 13:12:16 -0800659 }
Chris Craik527a3aa2013-03-04 10:19:31 -0800660 DEFER_LOGD("--flushed, drew %d batches", batchList.size());
Chris Craikff785832013-03-08 13:12:16 -0800661 return status;
662}
663
664status_t DeferredDisplayList::flush(OpenGLRenderer& renderer, Rect& dirty) {
665 ATRACE_NAME("flush drawing commands");
Romain Guycf51a412013-04-08 19:40:31 -0700666 Caches::getInstance().fontRenderer->endPrecaching();
667
Chris Craikc3566d02013-02-04 16:16:33 -0800668 status_t status = DrawGlInfo::kStatusDone;
669
670 if (isEmpty()) return status; // nothing to flush
Chris Craika4e16c52013-03-22 10:00:48 -0700671 renderer.restoreToCount(1);
Chris Craikc3566d02013-02-04 16:16:33 -0800672
673 DEFER_LOGD("--flushing");
Romain Guy0f667532013-03-01 14:31:04 -0800674 renderer.eventMark("Flush");
675
Chris Craika4e16c52013-03-22 10:00:48 -0700676 // save and restore (with draw modifiers) so that reordering doesn't affect final state
Chris Craikd90144d2013-03-19 15:03:48 -0700677 DrawModifiers restoreDrawModifiers = renderer.getDrawModifiers();
Chris Craika4e16c52013-03-22 10:00:48 -0700678 renderer.save(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
679
Chris Craikef8d6f22014-12-17 11:10:28 -0800680 if (CC_LIKELY(mAvoidOverdraw)) {
681 for (unsigned int i = 1; i < mBatches.size(); i++) {
682 if (mBatches[i] && mBatches[i]->coversBounds(mBounds)) {
683 discardDrawingBatches(i - 1);
684 }
Chris Craik28ce94a2013-05-31 11:38:03 -0700685 }
686 }
Chris Craik1ed30c92013-04-03 12:37:35 -0700687 // NOTE: depth of the save stack at this point, before playback, should be reflected in
688 // FLUSH_SAVE_STACK_DEPTH, so that save/restores match up correctly
Chris Craikff785832013-03-08 13:12:16 -0800689 status |= replayBatchList(mBatches, renderer, dirty);
Chris Craika4e16c52013-03-22 10:00:48 -0700690
691 renderer.restoreToCount(1);
Chris Craikd90144d2013-03-19 15:03:48 -0700692 renderer.setDrawModifiers(restoreDrawModifiers);
Chris Craikc3566d02013-02-04 16:16:33 -0800693
Chris Craikff785832013-03-08 13:12:16 -0800694 DEFER_LOGD("--flush complete, returning %x", status);
Chris Craikc3566d02013-02-04 16:16:33 -0800695 clear();
696 return status;
697}
698
Chris Craikf70119c2013-06-13 11:21:22 -0700699void DeferredDisplayList::discardDrawingBatches(const unsigned int maxIndex) {
Chris Craik28ce94a2013-05-31 11:38:03 -0700700 for (unsigned int i = mEarliestUnclearedIndex; i <= maxIndex; i++) {
Chris Craikf70119c2013-06-13 11:21:22 -0700701 // leave deferred state ops alone for simplicity (empty save restore pairs may now exist)
Chris Craik28ce94a2013-05-31 11:38:03 -0700702 if (mBatches[i] && mBatches[i]->purelyDrawBatch()) {
703 DrawBatch* b = (DrawBatch*) mBatches[i];
704 delete mBatches[i];
705 mBatches.replaceAt(NULL, i);
706 }
707 }
708 mEarliestUnclearedIndex = maxIndex + 1;
709}
710
Chris Craikc3566d02013-02-04 16:16:33 -0800711}; // namespace uirenderer
712}; // namespace android