blob: c0b77c7d9a07cf979c47c6b0d91b6ca05bd83aab [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"
31
32#if DEBUG_DEFER
33 #define DEFER_LOGD(...) ALOGD(__VA_ARGS__)
34#else
35 #define DEFER_LOGD(...)
36#endif
37
38namespace android {
39namespace uirenderer {
40
Chris Craik1ed30c92013-04-03 12:37:35 -070041// Depth of the save stack at the beginning of batch playback at flush time
42#define FLUSH_SAVE_STACK_DEPTH 2
43
Chris Craik527a3aa2013-03-04 10:19:31 -080044#define DEBUG_COLOR_BARRIER 0x1f000000
45#define DEBUG_COLOR_MERGEDBATCH 0x5f7f7fff
46#define DEBUG_COLOR_MERGEDBATCH_SOLO 0x5f7fff7f
47
Chris Craikff785832013-03-08 13:12:16 -080048/////////////////////////////////////////////////////////////////////////////////
49// Operation Batches
50/////////////////////////////////////////////////////////////////////////////////
51
Chris Craik527a3aa2013-03-04 10:19:31 -080052class Batch {
Chris Craikc3566d02013-02-04 16:16:33 -080053public:
Chris Craik527a3aa2013-03-04 10:19:31 -080054 virtual status_t replay(OpenGLRenderer& renderer, Rect& dirty, int index) = 0;
55 virtual ~Batch() {}
Chris Craik28ce94a2013-05-31 11:38:03 -070056 virtual bool purelyDrawBatch() { return false; }
57 virtual bool coversBounds(const Rect& bounds) { return false; }
Chris Craik527a3aa2013-03-04 10:19:31 -080058};
Chris Craikc3566d02013-02-04 16:16:33 -080059
Chris Craik527a3aa2013-03-04 10:19:31 -080060class DrawBatch : public Batch {
61public:
Chris Craik28ce94a2013-05-31 11:38:03 -070062 DrawBatch(const DeferInfo& deferInfo) : mAllOpsOpaque(true),
63 mBatchId(deferInfo.batchId), mMergeId(deferInfo.mergeId) {
Chris Craik527a3aa2013-03-04 10:19:31 -080064 mOps.clear();
65 }
66
67 virtual ~DrawBatch() { mOps.clear(); }
Chris Craikc3566d02013-02-04 16:16:33 -080068
Chris Craikc1c5f082013-09-11 16:23:37 -070069 virtual void add(DrawOp* op, const DeferredDisplayState* state, bool opaqueOverBounds) {
Chris Craikc3566d02013-02-04 16:16:33 -080070 // NOTE: ignore empty bounds special case, since we don't merge across those ops
Chris Craikc1c5f082013-09-11 16:23:37 -070071 mBounds.unionWith(state->mBounds);
Chris Craik28ce94a2013-05-31 11:38:03 -070072 mAllOpsOpaque &= opaqueOverBounds;
Chris Craikc1c5f082013-09-11 16:23:37 -070073 mOps.add(OpStatePair(op, state));
Chris Craikc3566d02013-02-04 16:16:33 -080074 }
75
Chris Craikc1c5f082013-09-11 16:23:37 -070076 bool intersects(const Rect& rect) {
Chris Craikc3566d02013-02-04 16:16:33 -080077 if (!rect.intersects(mBounds)) return false;
Chris Craikff785832013-03-08 13:12:16 -080078
Chris Craikc3566d02013-02-04 16:16:33 -080079 for (unsigned int i = 0; i < mOps.size(); i++) {
Chris Craikc1c5f082013-09-11 16:23:37 -070080 if (rect.intersects(mOps[i].state->mBounds)) {
Chris Craikc3566d02013-02-04 16:16:33 -080081#if DEBUG_DEFER
Chris Craikc1c5f082013-09-11 16:23:37 -070082 DEFER_LOGD("op intersects with op %p with bounds %f %f %f %f:", mOps[i].op,
83 mOps[i].state->mBounds.left, mOps[i].state->mBounds.top,
84 mOps[i].state->mBounds.right, mOps[i].state->mBounds.bottom);
85 mOps[i].op->output(2);
Chris Craikc3566d02013-02-04 16:16:33 -080086#endif
87 return true;
88 }
89 }
90 return false;
91 }
92
Chris Craik527a3aa2013-03-04 10:19:31 -080093 virtual status_t replay(OpenGLRenderer& renderer, Rect& dirty, int index) {
Chris Craik41541822013-05-03 16:35:54 -070094 DEFER_LOGD("%d replaying DrawBatch %p, with %d ops (batch id %x, merge id %p)",
95 index, this, mOps.size(), getBatchId(), getMergeId());
Chris Craikff785832013-03-08 13:12:16 -080096
97 status_t status = DrawGlInfo::kStatusDone;
98 DisplayListLogBuffer& logBuffer = DisplayListLogBuffer::getInstance();
99 for (unsigned int i = 0; i < mOps.size(); i++) {
Chris Craikc1c5f082013-09-11 16:23:37 -0700100 DrawOp* op = mOps[i].op;
101 const DeferredDisplayState* state = mOps[i].state;
102 renderer.restoreDisplayState(*state);
Chris Craikff785832013-03-08 13:12:16 -0800103
104#if DEBUG_DISPLAY_LIST_OPS_AS_EVENTS
Chris Craikd90144d2013-03-19 15:03:48 -0700105 renderer.eventMark(op->name());
Chris Craikff785832013-03-08 13:12:16 -0800106#endif
Chris Craikff785832013-03-08 13:12:16 -0800107 logBuffer.writeCommand(0, op->name());
Chris Craikd4b43b32013-05-09 13:07:52 -0700108 status |= 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 }
120 return status;
121 }
122
Chris Craik28ce94a2013-05-31 11:38:03 -0700123 virtual bool purelyDrawBatch() { return true; }
124
125 virtual bool coversBounds(const Rect& bounds) {
126 if (CC_LIKELY(!mAllOpsOpaque || !mBounds.contains(bounds) || count() == 1)) return false;
127
128 Region uncovered(android::Rect(bounds.left, bounds.top, bounds.right, bounds.bottom));
129 for (unsigned int i = 0; i < mOps.size(); i++) {
Chris Craikc1c5f082013-09-11 16:23:37 -0700130 const Rect &r = mOps[i].state->mBounds;
Chris Craik28ce94a2013-05-31 11:38:03 -0700131 uncovered.subtractSelf(android::Rect(r.left, r.top, r.right, r.bottom));
132 }
133 return uncovered.isEmpty();
134 }
135
Chris Craik527a3aa2013-03-04 10:19:31 -0800136 inline int getBatchId() const { return mBatchId; }
137 inline mergeid_t getMergeId() const { return mMergeId; }
Chris Craikff785832013-03-08 13:12:16 -0800138 inline int count() const { return mOps.size(); }
Chris Craik527a3aa2013-03-04 10:19:31 -0800139
140protected:
Chris Craikc1c5f082013-09-11 16:23:37 -0700141 Vector<OpStatePair> mOps;
Chris Craik28ce94a2013-05-31 11:38:03 -0700142 Rect mBounds; // union of bounds of contained ops
Chris Craik527a3aa2013-03-04 10:19:31 -0800143private:
Chris Craik28ce94a2013-05-31 11:38:03 -0700144 bool mAllOpsOpaque;
Chris Craik527a3aa2013-03-04 10:19:31 -0800145 int mBatchId;
146 mergeid_t mMergeId;
Chris Craikc3566d02013-02-04 16:16:33 -0800147};
148
Chris Craik527a3aa2013-03-04 10:19:31 -0800149// compare alphas approximately, with a small margin
150#define NEQ_FALPHA(lhs, rhs) \
151 fabs((float)lhs - (float)rhs) > 0.001f
152
153class MergingDrawBatch : public DrawBatch {
154public:
Chris Craik0e87f002013-06-19 16:54:59 -0700155 MergingDrawBatch(DeferInfo& deferInfo, int width, int height) :
156 DrawBatch(deferInfo), mClipRect(width, height),
157 mClipSideFlags(kClipSide_None) {}
Chris Craika02c4ed2013-06-14 13:43:58 -0700158
159 /*
160 * Helper for determining if a new op can merge with a MergingDrawBatch based on their bounds
161 * and clip side flags. Positive bounds delta means new bounds fit in old.
162 */
163 static inline bool checkSide(const int currentFlags, const int newFlags, const int side,
164 float boundsDelta) {
165 bool currentClipExists = currentFlags & side;
166 bool newClipExists = newFlags & side;
167
168 // if current is clipped, we must be able to fit new bounds in current
169 if (boundsDelta > 0 && currentClipExists) return false;
170
171 // if new is clipped, we must be able to fit current bounds in new
172 if (boundsDelta < 0 && newClipExists) return false;
173
174 return true;
175 }
Chris Craik527a3aa2013-03-04 10:19:31 -0800176
177 /*
178 * Checks if a (mergeable) op can be merged into this batch
179 *
180 * If true, the op's multiDraw must be guaranteed to handle both ops simultaneously, so it is
181 * important to consider all paint attributes used in the draw calls in deciding both a) if an
Chris Craika02c4ed2013-06-14 13:43:58 -0700182 * 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 -0800183 *
184 * False positives can lead to information from the paints of subsequent merged operations being
185 * dropped, so we make simplifying qualifications on the ops that can merge, per op type.
186 */
Chris Craikc1c5f082013-09-11 16:23:37 -0700187 bool canMergeWith(const DrawOp* op, const DeferredDisplayState* state) {
Chris Craik527a3aa2013-03-04 10:19:31 -0800188 bool isTextBatch = getBatchId() == DeferredDisplayList::kOpBatch_Text ||
189 getBatchId() == DeferredDisplayList::kOpBatch_ColorText;
190
191 // Overlapping other operations is only allowed for text without shadow. For other ops,
192 // multiDraw isn't guaranteed to overdraw correctly
Chris Craikc1c5f082013-09-11 16:23:37 -0700193 if (!isTextBatch || state->mDrawModifiers.mHasShadow) {
194 if (intersects(state->mBounds)) return false;
Chris Craik527a3aa2013-03-04 10:19:31 -0800195 }
Chris Craikc1c5f082013-09-11 16:23:37 -0700196 const DeferredDisplayState* lhs = state;
197 const DeferredDisplayState* rhs = mOps[0].state;
Chris Craik527a3aa2013-03-04 10:19:31 -0800198
Chris Craikc1c5f082013-09-11 16:23:37 -0700199 if (NEQ_FALPHA(lhs->mAlpha, rhs->mAlpha)) 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 Craikc1c5f082013-09-11 16:23:37 -0700225 if (op->getPaintAlpha() != mOps[0].op->getPaintAlpha()) return false;
Chris Craik527a3aa2013-03-04 10:19:31 -0800226
227 /* Draw Modifiers compatibility check
228 *
229 * Shadows are ignored, as only text uses them, and in that case they are drawn
230 * per-DrawTextOp, before the unified text draw. Because of this, it's always safe to merge
231 * text UNLESS a later draw's shadow should overlays a previous draw's text. This is covered
232 * above with the intersection check.
233 *
234 * OverrideLayerAlpha is also ignored, as it's only used for drawing layers, which are never
235 * merged.
236 *
237 * These ignore cases prevent us from simply memcmp'ing the drawModifiers
238 */
Chris Craikc1c5f082013-09-11 16:23:37 -0700239 const DrawModifiers& lhsMod = lhs->mDrawModifiers;
240 const DrawModifiers& rhsMod = rhs->mDrawModifiers;
Chris Craik527a3aa2013-03-04 10:19:31 -0800241 if (lhsMod.mShader != rhsMod.mShader) return false;
242 if (lhsMod.mColorFilter != rhsMod.mColorFilter) return false;
243
244 // Draw filter testing expects bit fields to be clear if filter not set.
245 if (lhsMod.mHasDrawFilter != rhsMod.mHasDrawFilter) return false;
246 if (lhsMod.mPaintFilterClearBits != rhsMod.mPaintFilterClearBits) return false;
247 if (lhsMod.mPaintFilterSetBits != rhsMod.mPaintFilterSetBits) return false;
248
249 return true;
250 }
251
Chris Craik5af5fc52013-09-13 14:41:31 -0700252 virtual void add(DrawOp* op, const DeferredDisplayState* state, bool opaqueOverBounds) {
Chris Craikc1c5f082013-09-11 16:23:37 -0700253 DrawBatch::add(op, state, opaqueOverBounds);
Chris Craik28ce94a2013-05-31 11:38:03 -0700254
Chris Craikc1c5f082013-09-11 16:23:37 -0700255 const int newClipSideFlags = state->mClipSideFlags;
Chris Craik28ce94a2013-05-31 11:38:03 -0700256 mClipSideFlags |= newClipSideFlags;
Chris Craikc1c5f082013-09-11 16:23:37 -0700257 if (newClipSideFlags & kClipSide_Left) mClipRect.left = state->mClip.left;
258 if (newClipSideFlags & kClipSide_Top) mClipRect.top = state->mClip.top;
259 if (newClipSideFlags & kClipSide_Right) mClipRect.right = state->mClip.right;
260 if (newClipSideFlags & kClipSide_Bottom) mClipRect.bottom = state->mClip.bottom;
Chris Craik28ce94a2013-05-31 11:38:03 -0700261 }
262
Chris Craik527a3aa2013-03-04 10:19:31 -0800263 virtual status_t replay(OpenGLRenderer& renderer, Rect& dirty, int index) {
Chris Craik28ce94a2013-05-31 11:38:03 -0700264 DEFER_LOGD("%d replaying MergingDrawBatch %p, with %d ops,"
265 " clip flags %x (batch id %x, merge id %p)",
266 index, this, mOps.size(), mClipSideFlags, getBatchId(), getMergeId());
Chris Craik527a3aa2013-03-04 10:19:31 -0800267 if (mOps.size() == 1) {
Chris Craik28ce94a2013-05-31 11:38:03 -0700268 return DrawBatch::replay(renderer, dirty, -1);
Chris Craik527a3aa2013-03-04 10:19:31 -0800269 }
270
Chris Craik28ce94a2013-05-31 11:38:03 -0700271 // clipping in the merged case is done ahead of time since all ops share the clip (if any)
272 renderer.setupMergedMultiDraw(mClipSideFlags ? &mClipRect : NULL);
273
Chris Craikc1c5f082013-09-11 16:23:37 -0700274 DrawOp* op = mOps[0].op;
Chris Craik527a3aa2013-03-04 10:19:31 -0800275 DisplayListLogBuffer& buffer = DisplayListLogBuffer::getInstance();
276 buffer.writeCommand(0, "multiDraw");
277 buffer.writeCommand(1, op->name());
Chris Craikd4b43b32013-05-09 13:07:52 -0700278 status_t status = op->multiDraw(renderer, dirty, mOps, mBounds);
Chris Craik527a3aa2013-03-04 10:19:31 -0800279
280#if DEBUG_MERGE_BEHAVIOR
281 renderer.drawScreenSpaceColorRect(mBounds.left, mBounds.top, mBounds.right, mBounds.bottom,
282 DEBUG_COLOR_MERGEDBATCH);
283#endif
284 return status;
285 }
Chris Craik28ce94a2013-05-31 11:38:03 -0700286
287private:
288 /*
289 * Contains the effective clip rect shared by all merged ops. Initialized to the layer viewport,
290 * it will shrink if an op must be clipped on a certain side. The clipped sides are reflected in
291 * mClipSideFlags.
292 */
293 Rect mClipRect;
294 int mClipSideFlags;
Chris Craik527a3aa2013-03-04 10:19:31 -0800295};
296
297class StateOpBatch : public Batch {
Chris Craikff785832013-03-08 13:12:16 -0800298public:
299 // creates a single operation batch
Chris Craikc1c5f082013-09-11 16:23:37 -0700300 StateOpBatch(const StateOp* op, const DeferredDisplayState* state) : mOp(op), mState(state) {}
Chris Craikff785832013-03-08 13:12:16 -0800301
Chris Craik527a3aa2013-03-04 10:19:31 -0800302 virtual status_t replay(OpenGLRenderer& renderer, Rect& dirty, int index) {
Chris Craikff785832013-03-08 13:12:16 -0800303 DEFER_LOGD("replaying state op batch %p", this);
Chris Craikc1c5f082013-09-11 16:23:37 -0700304 renderer.restoreDisplayState(*mState);
Chris Craikff785832013-03-08 13:12:16 -0800305
306 // use invalid save count because it won't be used at flush time - RestoreToCountOp is the
307 // only one to use it, and we don't use that class at flush time, instead calling
308 // renderer.restoreToCount directly
309 int saveCount = -1;
310 mOp->applyState(renderer, saveCount);
311 return DrawGlInfo::kStatusDone;
312 }
313
314private:
Chris Craik7273daa2013-03-28 11:25:24 -0700315 const StateOp* mOp;
Chris Craikc1c5f082013-09-11 16:23:37 -0700316 const DeferredDisplayState* mState;
Chris Craikff785832013-03-08 13:12:16 -0800317};
318
Chris Craik527a3aa2013-03-04 10:19:31 -0800319class RestoreToCountBatch : public Batch {
Chris Craikff785832013-03-08 13:12:16 -0800320public:
Chris Craikc1c5f082013-09-11 16:23:37 -0700321 RestoreToCountBatch(const StateOp* op, const DeferredDisplayState* state, int restoreCount) :
322 mOp(op), mState(state), mRestoreCount(restoreCount) {}
Chris Craikff785832013-03-08 13:12:16 -0800323
Chris Craik527a3aa2013-03-04 10:19:31 -0800324 virtual status_t replay(OpenGLRenderer& renderer, Rect& dirty, int index) {
Chris Craikff785832013-03-08 13:12:16 -0800325 DEFER_LOGD("batch %p restoring to count %d", this, mRestoreCount);
Chris Craik7273daa2013-03-28 11:25:24 -0700326
Chris Craikc1c5f082013-09-11 16:23:37 -0700327 renderer.restoreDisplayState(*mState);
Chris Craikff785832013-03-08 13:12:16 -0800328 renderer.restoreToCount(mRestoreCount);
Chris Craikff785832013-03-08 13:12:16 -0800329 return DrawGlInfo::kStatusDone;
330 }
331
332private:
Chris Craik7273daa2013-03-28 11:25:24 -0700333 // we use the state storage for the RestoreToCountOp, but don't replay the op itself
334 const StateOp* mOp;
Chris Craikc1c5f082013-09-11 16:23:37 -0700335 const DeferredDisplayState* mState;
336
Chris Craikff785832013-03-08 13:12:16 -0800337 /*
338 * The count used here represents the flush() time saveCount. This is as opposed to the
339 * DisplayList record time, or defer() time values (which are RestoreToCountOp's mCount, and
340 * (saveCount + mCount) respectively). Since the count is different from the original
341 * RestoreToCountOp, we don't store a pointer to the op, as elsewhere.
342 */
343 const int mRestoreCount;
344};
345
Chris Craik527a3aa2013-03-04 10:19:31 -0800346#if DEBUG_MERGE_BEHAVIOR
347class BarrierDebugBatch : public Batch {
348 virtual status_t replay(OpenGLRenderer& renderer, Rect& dirty, int index) {
349 renderer.drawScreenSpaceColorRect(0, 0, 10000, 10000, DEBUG_COLOR_BARRIER);
350 return DrawGlInfo::kStatusDrew;
351 }
352};
353#endif
354
Chris Craikff785832013-03-08 13:12:16 -0800355/////////////////////////////////////////////////////////////////////////////////
356// DeferredDisplayList
357/////////////////////////////////////////////////////////////////////////////////
358
359void DeferredDisplayList::resetBatchingState() {
Chris Craikc3566d02013-02-04 16:16:33 -0800360 for (int i = 0; i < kOpBatch_Count; i++) {
Chris Craik527a3aa2013-03-04 10:19:31 -0800361 mBatchLookup[i] = NULL;
362 mMergingBatches[i].clear();
Chris Craikc3566d02013-02-04 16:16:33 -0800363 }
Chris Craik527a3aa2013-03-04 10:19:31 -0800364#if DEBUG_MERGE_BEHAVIOR
365 if (mBatches.size() != 0) {
366 mBatches.add(new BarrierDebugBatch());
367 }
368#endif
369 mEarliestBatchIndex = mBatches.size();
Chris Craikff785832013-03-08 13:12:16 -0800370}
371
372void DeferredDisplayList::clear() {
373 resetBatchingState();
374 mComplexClipStackStart = -1;
375
Chris Craikc3566d02013-02-04 16:16:33 -0800376 for (unsigned int i = 0; i < mBatches.size(); i++) {
377 delete mBatches[i];
378 }
379 mBatches.clear();
Chris Craikff785832013-03-08 13:12:16 -0800380 mSaveStack.clear();
Chris Craik527a3aa2013-03-04 10:19:31 -0800381 mEarliestBatchIndex = 0;
Chris Craik28ce94a2013-05-31 11:38:03 -0700382 mEarliestUnclearedIndex = 0;
Chris Craikc3566d02013-02-04 16:16:33 -0800383}
384
Chris Craikff785832013-03-08 13:12:16 -0800385/////////////////////////////////////////////////////////////////////////////////
386// Operation adding
387/////////////////////////////////////////////////////////////////////////////////
388
389int DeferredDisplayList::getStateOpDeferFlags() const {
390 // For both clipOp and save(Layer)Op, we don't want to save drawing info, and only want to save
391 // the clip if we aren't recording a complex clip (and can thus trust it to be a rect)
392 return recordingComplexClip() ? 0 : kStateDeferFlag_Clip;
393}
394
395int DeferredDisplayList::getDrawOpDeferFlags() const {
396 return kStateDeferFlag_Draw | getStateOpDeferFlags();
397}
398
399/**
400 * When an clipping operation occurs that could cause a complex clip, record the operation and all
401 * subsequent clipOps, save/restores (if the clip flag is set). During a flush, instead of loading
402 * the clip from deferred state, we play back all of the relevant state operations that generated
403 * the complex clip.
404 *
405 * Note that we don't need to record the associated restore operation, since operations at defer
406 * time record whether they should store the renderer's current clip
407 */
408void DeferredDisplayList::addClip(OpenGLRenderer& renderer, ClipOp* op) {
409 if (recordingComplexClip() || op->canCauseComplexClip() || !renderer.hasRectToRectTransform()) {
410 DEFER_LOGD("%p Received complex clip operation %p", this, op);
411
412 // NOTE: defer clip op before setting mComplexClipStackStart so previous clip is recorded
413 storeStateOpBarrier(renderer, op);
414
415 if (!recordingComplexClip()) {
416 mComplexClipStackStart = renderer.getSaveCount() - 1;
417 DEFER_LOGD(" Starting complex clip region, start is %d", mComplexClipStackStart);
Chris Craikc3566d02013-02-04 16:16:33 -0800418 }
Chris Craikff785832013-03-08 13:12:16 -0800419 }
420}
421
422/**
423 * For now, we record save layer operations as barriers in the batch list, preventing drawing
424 * operations from reordering around the saveLayer and it's associated restore()
425 *
426 * In the future, we should send saveLayer commands (if they can be played out of order) and their
427 * contained drawing operations to a seperate list of batches, so that they may draw at the
428 * beginning of the frame. This would avoid targetting and removing an FBO in the middle of a frame.
429 *
430 * saveLayer operations should be pulled to the beginning of the frame if the canvas doesn't have a
431 * complex clip, and if the flags (kClip_SaveFlag & kClipToLayer_SaveFlag) are set.
432 */
433void DeferredDisplayList::addSaveLayer(OpenGLRenderer& renderer,
434 SaveLayerOp* op, int newSaveCount) {
435 DEFER_LOGD("%p adding saveLayerOp %p, flags %x, new count %d",
436 this, op, op->getFlags(), newSaveCount);
437
438 storeStateOpBarrier(renderer, op);
439 mSaveStack.push(newSaveCount);
440}
441
442/**
443 * Takes save op and it's return value - the new save count - and stores it into the stream as a
444 * barrier if it's needed to properly modify a complex clip
445 */
446void DeferredDisplayList::addSave(OpenGLRenderer& renderer, SaveOp* op, int newSaveCount) {
447 int saveFlags = op->getFlags();
448 DEFER_LOGD("%p adding saveOp %p, flags %x, new count %d", this, op, saveFlags, newSaveCount);
449
450 if (recordingComplexClip() && (saveFlags & SkCanvas::kClip_SaveFlag)) {
451 // store and replay the save operation, as it may be needed to correctly playback the clip
452 DEFER_LOGD(" adding save barrier with new save count %d", newSaveCount);
453 storeStateOpBarrier(renderer, op);
454 mSaveStack.push(newSaveCount);
455 }
456}
457
458/**
459 * saveLayer() commands must be associated with a restoreToCount batch that will clean up and draw
460 * the layer in the deferred list
461 *
462 * other save() commands which occur as children of a snapshot with complex clip will be deferred,
463 * and must be restored
464 *
465 * Either will act as a barrier to draw operation reordering, as we want to play back layer
466 * save/restore and complex canvas modifications (including save/restore) in order.
467 */
Chris Craik7273daa2013-03-28 11:25:24 -0700468void DeferredDisplayList::addRestoreToCount(OpenGLRenderer& renderer, StateOp* op,
469 int newSaveCount) {
Chris Craikff785832013-03-08 13:12:16 -0800470 DEFER_LOGD("%p addRestoreToCount %d", this, newSaveCount);
471
472 if (recordingComplexClip() && newSaveCount <= mComplexClipStackStart) {
473 mComplexClipStackStart = -1;
474 resetBatchingState();
475 }
476
477 if (mSaveStack.isEmpty() || newSaveCount > mSaveStack.top()) {
478 return;
479 }
480
481 while (!mSaveStack.isEmpty() && mSaveStack.top() >= newSaveCount) mSaveStack.pop();
482
Chris Craik1ed30c92013-04-03 12:37:35 -0700483 storeRestoreToCountBarrier(renderer, op, mSaveStack.size() + FLUSH_SAVE_STACK_DEPTH);
Chris Craikff785832013-03-08 13:12:16 -0800484}
485
486void DeferredDisplayList::addDrawOp(OpenGLRenderer& renderer, DrawOp* op) {
Chris Craikc1c5f082013-09-11 16:23:37 -0700487 /* 1: op calculates local bounds */
488 DeferredDisplayState* const state = createState();
489 if (op->getLocalBounds(renderer.getDrawModifiers(), state->mBounds)) {
490 if (state->mBounds.isEmpty()) {
491 // valid empty bounds, don't bother deferring
492 tryRecycleState(state);
493 return;
494 }
495 } else {
496 state->mBounds.setEmpty();
497 }
498
499 /* 2: renderer calculates global bounds + stores state */
500 if (renderer.storeDisplayState(*state, getDrawOpDeferFlags())) {
501 tryRecycleState(state);
Chris Craikff785832013-03-08 13:12:16 -0800502 return; // quick rejected
503 }
504
Chris Craikc1c5f082013-09-11 16:23:37 -0700505 /* 3: ask op for defer info, given renderer state */
Chris Craik28ce94a2013-05-31 11:38:03 -0700506 DeferInfo deferInfo;
Chris Craikc1c5f082013-09-11 16:23:37 -0700507 op->onDefer(renderer, deferInfo, *state);
Chris Craik527a3aa2013-03-04 10:19:31 -0800508
509 // complex clip has a complex set of expectations on the renderer state - for now, avoid taking
510 // the merge path in those cases
Chris Craik28ce94a2013-05-31 11:38:03 -0700511 deferInfo.mergeable &= !recordingComplexClip();
Chris Craik0e87f002013-06-19 16:54:59 -0700512 deferInfo.opaqueOverBounds &= !recordingComplexClip() && mSaveStack.isEmpty();
Chris Craik28ce94a2013-05-31 11:38:03 -0700513
514 if (CC_LIKELY(mAvoidOverdraw) && mBatches.size() &&
Chris Craikc1c5f082013-09-11 16:23:37 -0700515 state->mClipSideFlags != kClipSide_ConservativeFull &&
516 deferInfo.opaqueOverBounds && state->mBounds.contains(mBounds)) {
Chris Craikf70119c2013-06-13 11:21:22 -0700517 // avoid overdraw by resetting drawing state + discarding drawing ops
Chris Craik28ce94a2013-05-31 11:38:03 -0700518 discardDrawingBatches(mBatches.size() - 1);
Chris Craikf70119c2013-06-13 11:21:22 -0700519 resetBatchingState();
Chris Craik28ce94a2013-05-31 11:38:03 -0700520 }
Chris Craikff785832013-03-08 13:12:16 -0800521
522 if (CC_UNLIKELY(renderer.getCaches().drawReorderDisabled)) {
523 // TODO: elegant way to reuse batches?
Chris Craik28ce94a2013-05-31 11:38:03 -0700524 DrawBatch* b = new DrawBatch(deferInfo);
Chris Craikc1c5f082013-09-11 16:23:37 -0700525 b->add(op, state, deferInfo.opaqueOverBounds);
Chris Craikc3566d02013-02-04 16:16:33 -0800526 mBatches.add(b);
527 return;
528 }
529
Chris Craik527a3aa2013-03-04 10:19:31 -0800530 // find the latest batch of the new op's type, and try to merge the new op into it
531 DrawBatch* targetBatch = NULL;
Chris Craikc3566d02013-02-04 16:16:33 -0800532
Chris Craik527a3aa2013-03-04 10:19:31 -0800533 // insertion point of a new batch, will hopefully be immediately after similar batch
534 // (eventually, should be similar shader)
535 int insertBatchIndex = mBatches.size();
Chris Craikc3566d02013-02-04 16:16:33 -0800536 if (!mBatches.isEmpty()) {
Chris Craikc1c5f082013-09-11 16:23:37 -0700537 if (state->mBounds.isEmpty()) {
Chris Craikc3566d02013-02-04 16:16:33 -0800538 // 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 -0700539 DrawBatch* b = new DrawBatch(deferInfo);
Chris Craikc1c5f082013-09-11 16:23:37 -0700540 b->add(op, state, deferInfo.opaqueOverBounds);
Chris Craik527a3aa2013-03-04 10:19:31 -0800541 mBatches.add(b);
542 resetBatchingState();
Chris Craikc3566d02013-02-04 16:16:33 -0800543#if DEBUG_DEFER
544 DEFER_LOGD("Warning: Encountered op with empty bounds, resetting batches");
545 op->output(2);
546#endif
547 return;
548 }
549
Chris Craik28ce94a2013-05-31 11:38:03 -0700550 if (deferInfo.mergeable) {
Chris Craik527a3aa2013-03-04 10:19:31 -0800551 // Try to merge with any existing batch with same mergeId.
Chris Craik28ce94a2013-05-31 11:38:03 -0700552 if (mMergingBatches[deferInfo.batchId].get(deferInfo.mergeId, targetBatch)) {
Chris Craikc1c5f082013-09-11 16:23:37 -0700553 if (!((MergingDrawBatch*) targetBatch)->canMergeWith(op, state)) {
Chris Craik527a3aa2013-03-04 10:19:31 -0800554 targetBatch = NULL;
555 }
556 }
557 } else {
558 // join with similar, non-merging batch
Chris Craik28ce94a2013-05-31 11:38:03 -0700559 targetBatch = (DrawBatch*)mBatchLookup[deferInfo.batchId];
Chris Craik527a3aa2013-03-04 10:19:31 -0800560 }
561
Chris Craik28ce94a2013-05-31 11:38:03 -0700562 if (targetBatch || deferInfo.mergeable) {
Chris Craikc3566d02013-02-04 16:16:33 -0800563 // iterate back toward target to see if anything drawn since should overlap the new op
Chris Craik527a3aa2013-03-04 10:19:31 -0800564 // if no target, merging ops still interate to find similar batch to insert after
565 for (int i = mBatches.size() - 1; i >= mEarliestBatchIndex; i--) {
566 DrawBatch* overBatch = (DrawBatch*)mBatches[i];
567
568 if (overBatch == targetBatch) break;
569
570 // TODO: also consider shader shared between batch types
Chris Craik28ce94a2013-05-31 11:38:03 -0700571 if (deferInfo.batchId == overBatch->getBatchId()) {
Chris Craik527a3aa2013-03-04 10:19:31 -0800572 insertBatchIndex = i + 1;
573 if (!targetBatch) break; // found insert position, quit
574 }
575
Chris Craikc1c5f082013-09-11 16:23:37 -0700576 if (overBatch->intersects(state->mBounds)) {
Chris Craik527a3aa2013-03-04 10:19:31 -0800577 // NOTE: it may be possible to optimize for special cases where two operations
578 // of the same batch/paint could swap order, such as with a non-mergeable
579 // (clipped) and a mergeable text operation
Chris Craikc3566d02013-02-04 16:16:33 -0800580 targetBatch = NULL;
581#if DEBUG_DEFER
Chris Craikc1c5f082013-09-11 16:23:37 -0700582 DEFER_LOGD("op couldn't join batch %p, was intersected by batch %d",
583 targetBatch, i);
Chris Craikc3566d02013-02-04 16:16:33 -0800584 op->output(2);
585#endif
586 break;
587 }
588 }
589 }
590 }
Chris Craik527a3aa2013-03-04 10:19:31 -0800591
Chris Craikc3566d02013-02-04 16:16:33 -0800592 if (!targetBatch) {
Chris Craik28ce94a2013-05-31 11:38:03 -0700593 if (deferInfo.mergeable) {
Chris Craik0e87f002013-06-19 16:54:59 -0700594 targetBatch = new MergingDrawBatch(deferInfo,
595 renderer.getViewportWidth(), renderer.getViewportHeight());
Chris Craik28ce94a2013-05-31 11:38:03 -0700596 mMergingBatches[deferInfo.batchId].put(deferInfo.mergeId, targetBatch);
Chris Craik527a3aa2013-03-04 10:19:31 -0800597 } else {
Chris Craik28ce94a2013-05-31 11:38:03 -0700598 targetBatch = new DrawBatch(deferInfo);
599 mBatchLookup[deferInfo.batchId] = targetBatch;
Chris Craikc3566d02013-02-04 16:16:33 -0800600 }
Chris Craik527a3aa2013-03-04 10:19:31 -0800601
Chris Craikf70119c2013-06-13 11:21:22 -0700602 DEFER_LOGD("creating %singBatch %p, bid %x, at %d",
603 deferInfo.mergeable ? "Merg" : "Draw",
604 targetBatch, deferInfo.batchId, insertBatchIndex);
Chris Craik527a3aa2013-03-04 10:19:31 -0800605 mBatches.insertAt(targetBatch, insertBatchIndex);
Chris Craikc3566d02013-02-04 16:16:33 -0800606 }
Chris Craik527a3aa2013-03-04 10:19:31 -0800607
Chris Craikc1c5f082013-09-11 16:23:37 -0700608 targetBatch->add(op, state, deferInfo.opaqueOverBounds);
Chris Craikc3566d02013-02-04 16:16:33 -0800609}
610
Chris Craikff785832013-03-08 13:12:16 -0800611void DeferredDisplayList::storeStateOpBarrier(OpenGLRenderer& renderer, StateOp* op) {
612 DEFER_LOGD("%p adding state op barrier at pos %d", this, mBatches.size());
613
Chris Craikc1c5f082013-09-11 16:23:37 -0700614 DeferredDisplayState* state = createState();
615 renderer.storeDisplayState(*state, getStateOpDeferFlags());
616 mBatches.add(new StateOpBatch(op, state));
Chris Craikff785832013-03-08 13:12:16 -0800617 resetBatchingState();
618}
619
Chris Craik7273daa2013-03-28 11:25:24 -0700620void DeferredDisplayList::storeRestoreToCountBarrier(OpenGLRenderer& renderer, StateOp* op,
621 int newSaveCount) {
Chris Craikff785832013-03-08 13:12:16 -0800622 DEFER_LOGD("%p adding restore to count %d barrier, pos %d",
623 this, newSaveCount, mBatches.size());
624
Chris Craik7273daa2013-03-28 11:25:24 -0700625 // store displayState for the restore operation, as it may be associated with a saveLayer that
626 // doesn't have kClip_SaveFlag set
Chris Craikc1c5f082013-09-11 16:23:37 -0700627 DeferredDisplayState* state = createState();
628 renderer.storeDisplayState(*state, getStateOpDeferFlags());
629 mBatches.add(new RestoreToCountBatch(op, state, newSaveCount));
Chris Craikff785832013-03-08 13:12:16 -0800630 resetBatchingState();
631}
632
633/////////////////////////////////////////////////////////////////////////////////
634// Replay / flush
635/////////////////////////////////////////////////////////////////////////////////
636
Chris Craik527a3aa2013-03-04 10:19:31 -0800637static status_t replayBatchList(const Vector<Batch*>& batchList,
Chris Craikff785832013-03-08 13:12:16 -0800638 OpenGLRenderer& renderer, Rect& dirty) {
639 status_t status = DrawGlInfo::kStatusDone;
640
Chris Craikff785832013-03-08 13:12:16 -0800641 for (unsigned int i = 0; i < batchList.size(); i++) {
Chris Craik28ce94a2013-05-31 11:38:03 -0700642 if (batchList[i]) {
643 status |= batchList[i]->replay(renderer, dirty, i);
644 }
Chris Craikff785832013-03-08 13:12:16 -0800645 }
Chris Craik527a3aa2013-03-04 10:19:31 -0800646 DEFER_LOGD("--flushed, drew %d batches", batchList.size());
Chris Craikff785832013-03-08 13:12:16 -0800647 return status;
648}
649
650status_t DeferredDisplayList::flush(OpenGLRenderer& renderer, Rect& dirty) {
651 ATRACE_NAME("flush drawing commands");
Romain Guycf51a412013-04-08 19:40:31 -0700652 Caches::getInstance().fontRenderer->endPrecaching();
653
Chris Craikc3566d02013-02-04 16:16:33 -0800654 status_t status = DrawGlInfo::kStatusDone;
655
656 if (isEmpty()) return status; // nothing to flush
Chris Craika4e16c52013-03-22 10:00:48 -0700657 renderer.restoreToCount(1);
Chris Craikc3566d02013-02-04 16:16:33 -0800658
659 DEFER_LOGD("--flushing");
Romain Guy0f667532013-03-01 14:31:04 -0800660 renderer.eventMark("Flush");
661
Chris Craika4e16c52013-03-22 10:00:48 -0700662 // save and restore (with draw modifiers) so that reordering doesn't affect final state
Chris Craikd90144d2013-03-19 15:03:48 -0700663 DrawModifiers restoreDrawModifiers = renderer.getDrawModifiers();
Chris Craika4e16c52013-03-22 10:00:48 -0700664 renderer.save(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
665
Chris Craik28ce94a2013-05-31 11:38:03 -0700666 if (CC_LIKELY(mAvoidOverdraw)) {
667 for (unsigned int i = 1; i < mBatches.size(); i++) {
668 if (mBatches[i] && mBatches[i]->coversBounds(mBounds)) {
669 discardDrawingBatches(i - 1);
670 }
671 }
672 }
Chris Craik1ed30c92013-04-03 12:37:35 -0700673 // NOTE: depth of the save stack at this point, before playback, should be reflected in
674 // FLUSH_SAVE_STACK_DEPTH, so that save/restores match up correctly
Chris Craikff785832013-03-08 13:12:16 -0800675 status |= replayBatchList(mBatches, renderer, dirty);
Chris Craika4e16c52013-03-22 10:00:48 -0700676
677 renderer.restoreToCount(1);
Chris Craikd90144d2013-03-19 15:03:48 -0700678 renderer.setDrawModifiers(restoreDrawModifiers);
Chris Craikc3566d02013-02-04 16:16:33 -0800679
Chris Craikff785832013-03-08 13:12:16 -0800680 DEFER_LOGD("--flush complete, returning %x", status);
Chris Craikc3566d02013-02-04 16:16:33 -0800681 clear();
682 return status;
683}
684
Chris Craikf70119c2013-06-13 11:21:22 -0700685void DeferredDisplayList::discardDrawingBatches(const unsigned int maxIndex) {
Chris Craik28ce94a2013-05-31 11:38:03 -0700686 for (unsigned int i = mEarliestUnclearedIndex; i <= maxIndex; i++) {
Chris Craikf70119c2013-06-13 11:21:22 -0700687 // leave deferred state ops alone for simplicity (empty save restore pairs may now exist)
Chris Craik28ce94a2013-05-31 11:38:03 -0700688 if (mBatches[i] && mBatches[i]->purelyDrawBatch()) {
689 DrawBatch* b = (DrawBatch*) mBatches[i];
690 delete mBatches[i];
691 mBatches.replaceAt(NULL, i);
692 }
693 }
694 mEarliestUnclearedIndex = maxIndex + 1;
695}
696
Chris Craikc3566d02013-02-04 16:16:33 -0800697}; // namespace uirenderer
698}; // namespace android