blob: c21ee1e4126648130562ffd1b3fabbe37b66107b [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 Craik28ce94a2013-05-31 11:38:03 -070069 virtual void add(DrawOp* op, bool opaqueOverBounds) {
Chris Craikc3566d02013-02-04 16:16:33 -080070 // NOTE: ignore empty bounds special case, since we don't merge across those ops
71 mBounds.unionWith(op->state.mBounds);
Chris Craik28ce94a2013-05-31 11:38:03 -070072 mAllOpsOpaque &= opaqueOverBounds;
Chris Craikc3566d02013-02-04 16:16:33 -080073 mOps.add(op);
74 }
75
Chris Craik527a3aa2013-03-04 10:19:31 -080076 bool intersects(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++) {
80 if (rect.intersects(mOps[i]->state.mBounds)) {
81#if DEBUG_DEFER
82 DEFER_LOGD("op intersects with op %p with bounds %f %f %f %f:", mOps[i],
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]->output(2);
86#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++) {
100 DrawOp* op = mOps[i];
101
Chris Craik7273daa2013-03-28 11:25:24 -0700102 renderer.restoreDisplayState(op->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
111 Rect& bounds = mOps[i]->state.mBounds;
112 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++) {
130 Rect &r = mOps[i]->state.mBounds;
131 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 Craikff785832013-03-08 13:12:16 -0800141 Vector<DrawOp*> 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 Craik28ce94a2013-05-31 11:38:03 -0700155 MergingDrawBatch(DeferInfo& deferInfo, Rect viewport) :
Chris Craika02c4ed2013-06-14 13:43:58 -0700156 DrawBatch(deferInfo), mClipRect(viewport), mClipSideFlags(kClipSide_None) {}
157
158 /*
159 * Helper for determining if a new op can merge with a MergingDrawBatch based on their bounds
160 * and clip side flags. Positive bounds delta means new bounds fit in old.
161 */
162 static inline bool checkSide(const int currentFlags, const int newFlags, const int side,
163 float boundsDelta) {
164 bool currentClipExists = currentFlags & side;
165 bool newClipExists = newFlags & side;
166
167 // if current is clipped, we must be able to fit new bounds in current
168 if (boundsDelta > 0 && currentClipExists) return false;
169
170 // if new is clipped, we must be able to fit current bounds in new
171 if (boundsDelta < 0 && newClipExists) return false;
172
173 return true;
174 }
Chris Craik527a3aa2013-03-04 10:19:31 -0800175
176 /*
177 * Checks if a (mergeable) op can be merged into this batch
178 *
179 * If true, the op's multiDraw must be guaranteed to handle both ops simultaneously, so it is
180 * important to consider all paint attributes used in the draw calls in deciding both a) if an
Chris Craika02c4ed2013-06-14 13:43:58 -0700181 * 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 -0800182 *
183 * False positives can lead to information from the paints of subsequent merged operations being
184 * dropped, so we make simplifying qualifications on the ops that can merge, per op type.
185 */
186 bool canMergeWith(DrawOp* op) {
Chris Craik527a3aa2013-03-04 10:19:31 -0800187 bool isTextBatch = getBatchId() == DeferredDisplayList::kOpBatch_Text ||
188 getBatchId() == DeferredDisplayList::kOpBatch_ColorText;
189
190 // Overlapping other operations is only allowed for text without shadow. For other ops,
191 // multiDraw isn't guaranteed to overdraw correctly
192 if (!isTextBatch || op->state.mDrawModifiers.mHasShadow) {
193 if (intersects(op->state.mBounds)) return false;
194 }
Chris Craik527a3aa2013-03-04 10:19:31 -0800195 const DeferredDisplayState& lhs = op->state;
196 const DeferredDisplayState& rhs = mOps[0]->state;
197
198 if (NEQ_FALPHA(lhs.mAlpha, rhs.mAlpha)) return false;
199
Chris Craika02c4ed2013-06-14 13:43:58 -0700200 /* Clipping compatibility check
201 *
202 * Exploits the fact that if a op or batch is clipped on a side, its bounds will equal its
203 * clip for that side.
204 */
205 const int currentFlags = mClipSideFlags;
206 const int newFlags = op->state.mClipSideFlags;
207 if (currentFlags != kClipSide_None || newFlags != kClipSide_None) {
208 const Rect& opBounds = op->state.mBounds;
209 float boundsDelta = mBounds.left - opBounds.left;
210 if (!checkSide(currentFlags, newFlags, kClipSide_Left, boundsDelta)) return false;
211 boundsDelta = mBounds.top - opBounds.top;
212 if (!checkSide(currentFlags, newFlags, kClipSide_Top, boundsDelta)) return false;
213
214 // right and bottom delta calculation reversed to account for direction
215 boundsDelta = opBounds.right - mBounds.right;
216 if (!checkSide(currentFlags, newFlags, kClipSide_Right, boundsDelta)) return false;
217 boundsDelta = opBounds.bottom - mBounds.bottom;
218 if (!checkSide(currentFlags, newFlags, kClipSide_Bottom, boundsDelta)) return false;
Chris Craik28ce94a2013-05-31 11:38:03 -0700219 }
Chris Craik28ce94a2013-05-31 11:38:03 -0700220
Chris Craik527a3aa2013-03-04 10:19:31 -0800221 // if paints are equal, then modifiers + paint attribs don't need to be compared
222 if (op->mPaint == mOps[0]->mPaint) return true;
223
224 if (op->getPaintAlpha() != mOps[0]->getPaintAlpha()) return false;
225
226 /* Draw Modifiers compatibility check
227 *
228 * Shadows are ignored, as only text uses them, and in that case they are drawn
229 * per-DrawTextOp, before the unified text draw. Because of this, it's always safe to merge
230 * text UNLESS a later draw's shadow should overlays a previous draw's text. This is covered
231 * above with the intersection check.
232 *
233 * OverrideLayerAlpha is also ignored, as it's only used for drawing layers, which are never
234 * merged.
235 *
236 * These ignore cases prevent us from simply memcmp'ing the drawModifiers
237 */
Chris Craik527a3aa2013-03-04 10:19:31 -0800238 const DrawModifiers& lhsMod = lhs.mDrawModifiers;
239 const DrawModifiers& rhsMod = rhs.mDrawModifiers;
240 if (lhsMod.mShader != rhsMod.mShader) return false;
241 if (lhsMod.mColorFilter != rhsMod.mColorFilter) return false;
242
243 // Draw filter testing expects bit fields to be clear if filter not set.
244 if (lhsMod.mHasDrawFilter != rhsMod.mHasDrawFilter) return false;
245 if (lhsMod.mPaintFilterClearBits != rhsMod.mPaintFilterClearBits) return false;
246 if (lhsMod.mPaintFilterSetBits != rhsMod.mPaintFilterSetBits) return false;
247
248 return true;
249 }
250
Chris Craik28ce94a2013-05-31 11:38:03 -0700251 virtual void add(DrawOp* op, bool opaqueOverBounds) {
252 DrawBatch::add(op, opaqueOverBounds);
253
254 const int newClipSideFlags = op->state.mClipSideFlags;
255 mClipSideFlags |= newClipSideFlags;
256 if (newClipSideFlags & kClipSide_Left) mClipRect.left = op->state.mClip.left;
257 if (newClipSideFlags & kClipSide_Top) mClipRect.top = op->state.mClip.top;
258 if (newClipSideFlags & kClipSide_Right) mClipRect.right = op->state.mClip.right;
259 if (newClipSideFlags & kClipSide_Bottom) mClipRect.bottom = op->state.mClip.bottom;
260 }
261
Chris Craik527a3aa2013-03-04 10:19:31 -0800262 virtual status_t replay(OpenGLRenderer& renderer, Rect& dirty, int index) {
Chris Craik28ce94a2013-05-31 11:38:03 -0700263 DEFER_LOGD("%d replaying MergingDrawBatch %p, with %d ops,"
264 " clip flags %x (batch id %x, merge id %p)",
265 index, this, mOps.size(), mClipSideFlags, getBatchId(), getMergeId());
Chris Craik527a3aa2013-03-04 10:19:31 -0800266 if (mOps.size() == 1) {
Chris Craik28ce94a2013-05-31 11:38:03 -0700267 return DrawBatch::replay(renderer, dirty, -1);
Chris Craik527a3aa2013-03-04 10:19:31 -0800268 }
269
Chris Craik28ce94a2013-05-31 11:38:03 -0700270 // clipping in the merged case is done ahead of time since all ops share the clip (if any)
271 renderer.setupMergedMultiDraw(mClipSideFlags ? &mClipRect : NULL);
272
Chris Craik527a3aa2013-03-04 10:19:31 -0800273 DrawOp* op = mOps[0];
Chris Craik527a3aa2013-03-04 10:19:31 -0800274 DisplayListLogBuffer& buffer = DisplayListLogBuffer::getInstance();
275 buffer.writeCommand(0, "multiDraw");
276 buffer.writeCommand(1, op->name());
Chris Craikd4b43b32013-05-09 13:07:52 -0700277 status_t status = op->multiDraw(renderer, dirty, mOps, mBounds);
Chris Craik527a3aa2013-03-04 10:19:31 -0800278
279#if DEBUG_MERGE_BEHAVIOR
280 renderer.drawScreenSpaceColorRect(mBounds.left, mBounds.top, mBounds.right, mBounds.bottom,
281 DEBUG_COLOR_MERGEDBATCH);
282#endif
283 return status;
284 }
Chris Craik28ce94a2013-05-31 11:38:03 -0700285
286private:
287 /*
288 * Contains the effective clip rect shared by all merged ops. Initialized to the layer viewport,
289 * it will shrink if an op must be clipped on a certain side. The clipped sides are reflected in
290 * mClipSideFlags.
291 */
292 Rect mClipRect;
293 int mClipSideFlags;
Chris Craik527a3aa2013-03-04 10:19:31 -0800294};
295
296class StateOpBatch : public Batch {
Chris Craikff785832013-03-08 13:12:16 -0800297public:
298 // creates a single operation batch
299 StateOpBatch(StateOp* op) : mOp(op) {}
300
Chris Craik527a3aa2013-03-04 10:19:31 -0800301 virtual status_t replay(OpenGLRenderer& renderer, Rect& dirty, int index) {
Chris Craikff785832013-03-08 13:12:16 -0800302 DEFER_LOGD("replaying state op batch %p", this);
Chris Craik7273daa2013-03-28 11:25:24 -0700303 renderer.restoreDisplayState(mOp->state);
Chris Craikff785832013-03-08 13:12:16 -0800304
305 // use invalid save count because it won't be used at flush time - RestoreToCountOp is the
306 // only one to use it, and we don't use that class at flush time, instead calling
307 // renderer.restoreToCount directly
308 int saveCount = -1;
309 mOp->applyState(renderer, saveCount);
310 return DrawGlInfo::kStatusDone;
311 }
312
313private:
Chris Craik7273daa2013-03-28 11:25:24 -0700314 const StateOp* mOp;
Chris Craikff785832013-03-08 13:12:16 -0800315};
316
Chris Craik527a3aa2013-03-04 10:19:31 -0800317class RestoreToCountBatch : public Batch {
Chris Craikff785832013-03-08 13:12:16 -0800318public:
Chris Craik7273daa2013-03-28 11:25:24 -0700319 RestoreToCountBatch(StateOp* op, int restoreCount) : mOp(op), mRestoreCount(restoreCount) {}
Chris Craikff785832013-03-08 13:12:16 -0800320
Chris Craik527a3aa2013-03-04 10:19:31 -0800321 virtual status_t replay(OpenGLRenderer& renderer, Rect& dirty, int index) {
Chris Craikff785832013-03-08 13:12:16 -0800322 DEFER_LOGD("batch %p restoring to count %d", this, mRestoreCount);
Chris Craik7273daa2013-03-28 11:25:24 -0700323
324 renderer.restoreDisplayState(mOp->state);
Chris Craikff785832013-03-08 13:12:16 -0800325 renderer.restoreToCount(mRestoreCount);
Chris Craikff785832013-03-08 13:12:16 -0800326 return DrawGlInfo::kStatusDone;
327 }
328
329private:
Chris Craik7273daa2013-03-28 11:25:24 -0700330 // we use the state storage for the RestoreToCountOp, but don't replay the op itself
331 const StateOp* mOp;
Chris Craikff785832013-03-08 13:12:16 -0800332 /*
333 * The count used here represents the flush() time saveCount. This is as opposed to the
334 * DisplayList record time, or defer() time values (which are RestoreToCountOp's mCount, and
335 * (saveCount + mCount) respectively). Since the count is different from the original
336 * RestoreToCountOp, we don't store a pointer to the op, as elsewhere.
337 */
338 const int mRestoreCount;
339};
340
Chris Craik527a3aa2013-03-04 10:19:31 -0800341#if DEBUG_MERGE_BEHAVIOR
342class BarrierDebugBatch : public Batch {
343 virtual status_t replay(OpenGLRenderer& renderer, Rect& dirty, int index) {
344 renderer.drawScreenSpaceColorRect(0, 0, 10000, 10000, DEBUG_COLOR_BARRIER);
345 return DrawGlInfo::kStatusDrew;
346 }
347};
348#endif
349
Chris Craikff785832013-03-08 13:12:16 -0800350/////////////////////////////////////////////////////////////////////////////////
351// DeferredDisplayList
352/////////////////////////////////////////////////////////////////////////////////
353
354void DeferredDisplayList::resetBatchingState() {
Chris Craikc3566d02013-02-04 16:16:33 -0800355 for (int i = 0; i < kOpBatch_Count; i++) {
Chris Craik527a3aa2013-03-04 10:19:31 -0800356 mBatchLookup[i] = NULL;
357 mMergingBatches[i].clear();
Chris Craikc3566d02013-02-04 16:16:33 -0800358 }
Chris Craik527a3aa2013-03-04 10:19:31 -0800359#if DEBUG_MERGE_BEHAVIOR
360 if (mBatches.size() != 0) {
361 mBatches.add(new BarrierDebugBatch());
362 }
363#endif
364 mEarliestBatchIndex = mBatches.size();
Chris Craikff785832013-03-08 13:12:16 -0800365}
366
367void DeferredDisplayList::clear() {
368 resetBatchingState();
369 mComplexClipStackStart = -1;
370
Chris Craikc3566d02013-02-04 16:16:33 -0800371 for (unsigned int i = 0; i < mBatches.size(); i++) {
372 delete mBatches[i];
373 }
374 mBatches.clear();
Chris Craikff785832013-03-08 13:12:16 -0800375 mSaveStack.clear();
Chris Craik527a3aa2013-03-04 10:19:31 -0800376 mEarliestBatchIndex = 0;
Chris Craik28ce94a2013-05-31 11:38:03 -0700377 mEarliestUnclearedIndex = 0;
Chris Craikc3566d02013-02-04 16:16:33 -0800378}
379
Chris Craikff785832013-03-08 13:12:16 -0800380/////////////////////////////////////////////////////////////////////////////////
381// Operation adding
382/////////////////////////////////////////////////////////////////////////////////
383
384int DeferredDisplayList::getStateOpDeferFlags() const {
385 // For both clipOp and save(Layer)Op, we don't want to save drawing info, and only want to save
386 // the clip if we aren't recording a complex clip (and can thus trust it to be a rect)
387 return recordingComplexClip() ? 0 : kStateDeferFlag_Clip;
388}
389
390int DeferredDisplayList::getDrawOpDeferFlags() const {
391 return kStateDeferFlag_Draw | getStateOpDeferFlags();
392}
393
394/**
395 * When an clipping operation occurs that could cause a complex clip, record the operation and all
396 * subsequent clipOps, save/restores (if the clip flag is set). During a flush, instead of loading
397 * the clip from deferred state, we play back all of the relevant state operations that generated
398 * the complex clip.
399 *
400 * Note that we don't need to record the associated restore operation, since operations at defer
401 * time record whether they should store the renderer's current clip
402 */
403void DeferredDisplayList::addClip(OpenGLRenderer& renderer, ClipOp* op) {
404 if (recordingComplexClip() || op->canCauseComplexClip() || !renderer.hasRectToRectTransform()) {
405 DEFER_LOGD("%p Received complex clip operation %p", this, op);
406
407 // NOTE: defer clip op before setting mComplexClipStackStart so previous clip is recorded
408 storeStateOpBarrier(renderer, op);
409
410 if (!recordingComplexClip()) {
411 mComplexClipStackStart = renderer.getSaveCount() - 1;
412 DEFER_LOGD(" Starting complex clip region, start is %d", mComplexClipStackStart);
Chris Craikc3566d02013-02-04 16:16:33 -0800413 }
Chris Craikff785832013-03-08 13:12:16 -0800414 }
415}
416
417/**
418 * For now, we record save layer operations as barriers in the batch list, preventing drawing
419 * operations from reordering around the saveLayer and it's associated restore()
420 *
421 * In the future, we should send saveLayer commands (if they can be played out of order) and their
422 * contained drawing operations to a seperate list of batches, so that they may draw at the
423 * beginning of the frame. This would avoid targetting and removing an FBO in the middle of a frame.
424 *
425 * saveLayer operations should be pulled to the beginning of the frame if the canvas doesn't have a
426 * complex clip, and if the flags (kClip_SaveFlag & kClipToLayer_SaveFlag) are set.
427 */
428void DeferredDisplayList::addSaveLayer(OpenGLRenderer& renderer,
429 SaveLayerOp* op, int newSaveCount) {
430 DEFER_LOGD("%p adding saveLayerOp %p, flags %x, new count %d",
431 this, op, op->getFlags(), newSaveCount);
432
433 storeStateOpBarrier(renderer, op);
434 mSaveStack.push(newSaveCount);
435}
436
437/**
438 * Takes save op and it's return value - the new save count - and stores it into the stream as a
439 * barrier if it's needed to properly modify a complex clip
440 */
441void DeferredDisplayList::addSave(OpenGLRenderer& renderer, SaveOp* op, int newSaveCount) {
442 int saveFlags = op->getFlags();
443 DEFER_LOGD("%p adding saveOp %p, flags %x, new count %d", this, op, saveFlags, newSaveCount);
444
445 if (recordingComplexClip() && (saveFlags & SkCanvas::kClip_SaveFlag)) {
446 // store and replay the save operation, as it may be needed to correctly playback the clip
447 DEFER_LOGD(" adding save barrier with new save count %d", newSaveCount);
448 storeStateOpBarrier(renderer, op);
449 mSaveStack.push(newSaveCount);
450 }
451}
452
453/**
454 * saveLayer() commands must be associated with a restoreToCount batch that will clean up and draw
455 * the layer in the deferred list
456 *
457 * other save() commands which occur as children of a snapshot with complex clip will be deferred,
458 * and must be restored
459 *
460 * Either will act as a barrier to draw operation reordering, as we want to play back layer
461 * save/restore and complex canvas modifications (including save/restore) in order.
462 */
Chris Craik7273daa2013-03-28 11:25:24 -0700463void DeferredDisplayList::addRestoreToCount(OpenGLRenderer& renderer, StateOp* op,
464 int newSaveCount) {
Chris Craikff785832013-03-08 13:12:16 -0800465 DEFER_LOGD("%p addRestoreToCount %d", this, newSaveCount);
466
467 if (recordingComplexClip() && newSaveCount <= mComplexClipStackStart) {
468 mComplexClipStackStart = -1;
469 resetBatchingState();
470 }
471
472 if (mSaveStack.isEmpty() || newSaveCount > mSaveStack.top()) {
473 return;
474 }
475
476 while (!mSaveStack.isEmpty() && mSaveStack.top() >= newSaveCount) mSaveStack.pop();
477
Chris Craik1ed30c92013-04-03 12:37:35 -0700478 storeRestoreToCountBarrier(renderer, op, mSaveStack.size() + FLUSH_SAVE_STACK_DEPTH);
Chris Craikff785832013-03-08 13:12:16 -0800479}
480
481void DeferredDisplayList::addDrawOp(OpenGLRenderer& renderer, DrawOp* op) {
482 if (renderer.storeDisplayState(op->state, getDrawOpDeferFlags())) {
483 return; // quick rejected
484 }
485
Chris Craik28ce94a2013-05-31 11:38:03 -0700486 DeferInfo deferInfo;
487 op->onDefer(renderer, deferInfo);
Chris Craik527a3aa2013-03-04 10:19:31 -0800488
489 // complex clip has a complex set of expectations on the renderer state - for now, avoid taking
490 // the merge path in those cases
Chris Craik28ce94a2013-05-31 11:38:03 -0700491 deferInfo.mergeable &= !recordingComplexClip();
492
493 if (CC_LIKELY(mAvoidOverdraw) && mBatches.size() &&
494 deferInfo.opaqueOverBounds && op->state.mBounds.contains(mBounds)) {
Chris Craikf70119c2013-06-13 11:21:22 -0700495 // avoid overdraw by resetting drawing state + discarding drawing ops
Chris Craik28ce94a2013-05-31 11:38:03 -0700496 discardDrawingBatches(mBatches.size() - 1);
Chris Craikf70119c2013-06-13 11:21:22 -0700497 resetBatchingState();
Chris Craik28ce94a2013-05-31 11:38:03 -0700498 }
Chris Craikff785832013-03-08 13:12:16 -0800499
500 if (CC_UNLIKELY(renderer.getCaches().drawReorderDisabled)) {
501 // TODO: elegant way to reuse batches?
Chris Craik28ce94a2013-05-31 11:38:03 -0700502 DrawBatch* b = new DrawBatch(deferInfo);
503 b->add(op, deferInfo.opaqueOverBounds);
Chris Craikc3566d02013-02-04 16:16:33 -0800504 mBatches.add(b);
505 return;
506 }
507
Chris Craik527a3aa2013-03-04 10:19:31 -0800508 // find the latest batch of the new op's type, and try to merge the new op into it
509 DrawBatch* targetBatch = NULL;
Chris Craikc3566d02013-02-04 16:16:33 -0800510
Chris Craik527a3aa2013-03-04 10:19:31 -0800511 // insertion point of a new batch, will hopefully be immediately after similar batch
512 // (eventually, should be similar shader)
513 int insertBatchIndex = mBatches.size();
Chris Craikc3566d02013-02-04 16:16:33 -0800514 if (!mBatches.isEmpty()) {
515 if (op->state.mBounds.isEmpty()) {
516 // 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 -0700517 DrawBatch* b = new DrawBatch(deferInfo);
518 b->add(op, deferInfo.opaqueOverBounds);
Chris Craik527a3aa2013-03-04 10:19:31 -0800519 mBatches.add(b);
520 resetBatchingState();
Chris Craikc3566d02013-02-04 16:16:33 -0800521#if DEBUG_DEFER
522 DEFER_LOGD("Warning: Encountered op with empty bounds, resetting batches");
523 op->output(2);
524#endif
525 return;
526 }
527
Chris Craik28ce94a2013-05-31 11:38:03 -0700528 if (deferInfo.mergeable) {
Chris Craik527a3aa2013-03-04 10:19:31 -0800529 // Try to merge with any existing batch with same mergeId.
Chris Craik28ce94a2013-05-31 11:38:03 -0700530 if (mMergingBatches[deferInfo.batchId].get(deferInfo.mergeId, targetBatch)) {
Chris Craik527a3aa2013-03-04 10:19:31 -0800531 if (!((MergingDrawBatch*) targetBatch)->canMergeWith(op)) {
532 targetBatch = NULL;
533 }
534 }
535 } else {
536 // join with similar, non-merging batch
Chris Craik28ce94a2013-05-31 11:38:03 -0700537 targetBatch = (DrawBatch*)mBatchLookup[deferInfo.batchId];
Chris Craik527a3aa2013-03-04 10:19:31 -0800538 }
539
Chris Craik28ce94a2013-05-31 11:38:03 -0700540 if (targetBatch || deferInfo.mergeable) {
Chris Craikc3566d02013-02-04 16:16:33 -0800541 // iterate back toward target to see if anything drawn since should overlap the new op
Chris Craik527a3aa2013-03-04 10:19:31 -0800542 // if no target, merging ops still interate to find similar batch to insert after
543 for (int i = mBatches.size() - 1; i >= mEarliestBatchIndex; i--) {
544 DrawBatch* overBatch = (DrawBatch*)mBatches[i];
545
546 if (overBatch == targetBatch) break;
547
548 // TODO: also consider shader shared between batch types
Chris Craik28ce94a2013-05-31 11:38:03 -0700549 if (deferInfo.batchId == overBatch->getBatchId()) {
Chris Craik527a3aa2013-03-04 10:19:31 -0800550 insertBatchIndex = i + 1;
551 if (!targetBatch) break; // found insert position, quit
552 }
553
Chris Craikc3566d02013-02-04 16:16:33 -0800554 if (overBatch->intersects(op->state.mBounds)) {
Chris Craik527a3aa2013-03-04 10:19:31 -0800555 // NOTE: it may be possible to optimize for special cases where two operations
556 // of the same batch/paint could swap order, such as with a non-mergeable
557 // (clipped) and a mergeable text operation
Chris Craikc3566d02013-02-04 16:16:33 -0800558 targetBatch = NULL;
559#if DEBUG_DEFER
560 DEFER_LOGD("op couldn't join batch %d, was intersected by batch %d",
561 targetIndex, i);
562 op->output(2);
563#endif
564 break;
565 }
566 }
567 }
568 }
Chris Craik527a3aa2013-03-04 10:19:31 -0800569
Chris Craikc3566d02013-02-04 16:16:33 -0800570 if (!targetBatch) {
Chris Craik28ce94a2013-05-31 11:38:03 -0700571 if (deferInfo.mergeable) {
572 targetBatch = new MergingDrawBatch(deferInfo, mBounds);
573 mMergingBatches[deferInfo.batchId].put(deferInfo.mergeId, targetBatch);
Chris Craik527a3aa2013-03-04 10:19:31 -0800574 } else {
Chris Craik28ce94a2013-05-31 11:38:03 -0700575 targetBatch = new DrawBatch(deferInfo);
576 mBatchLookup[deferInfo.batchId] = targetBatch;
Chris Craikc3566d02013-02-04 16:16:33 -0800577 }
Chris Craik527a3aa2013-03-04 10:19:31 -0800578
Chris Craikf70119c2013-06-13 11:21:22 -0700579 DEFER_LOGD("creating %singBatch %p, bid %x, at %d",
580 deferInfo.mergeable ? "Merg" : "Draw",
581 targetBatch, deferInfo.batchId, insertBatchIndex);
Chris Craik527a3aa2013-03-04 10:19:31 -0800582 mBatches.insertAt(targetBatch, insertBatchIndex);
Chris Craikc3566d02013-02-04 16:16:33 -0800583 }
Chris Craik527a3aa2013-03-04 10:19:31 -0800584
Chris Craik28ce94a2013-05-31 11:38:03 -0700585 targetBatch->add(op, deferInfo.opaqueOverBounds);
Chris Craikc3566d02013-02-04 16:16:33 -0800586}
587
Chris Craikff785832013-03-08 13:12:16 -0800588void DeferredDisplayList::storeStateOpBarrier(OpenGLRenderer& renderer, StateOp* op) {
589 DEFER_LOGD("%p adding state op barrier at pos %d", this, mBatches.size());
590
591 renderer.storeDisplayState(op->state, getStateOpDeferFlags());
592 mBatches.add(new StateOpBatch(op));
593 resetBatchingState();
594}
595
Chris Craik7273daa2013-03-28 11:25:24 -0700596void DeferredDisplayList::storeRestoreToCountBarrier(OpenGLRenderer& renderer, StateOp* op,
597 int newSaveCount) {
Chris Craikff785832013-03-08 13:12:16 -0800598 DEFER_LOGD("%p adding restore to count %d barrier, pos %d",
599 this, newSaveCount, mBatches.size());
600
Chris Craik7273daa2013-03-28 11:25:24 -0700601 // store displayState for the restore operation, as it may be associated with a saveLayer that
602 // doesn't have kClip_SaveFlag set
603 renderer.storeDisplayState(op->state, getStateOpDeferFlags());
604 mBatches.add(new RestoreToCountBatch(op, newSaveCount));
Chris Craikff785832013-03-08 13:12:16 -0800605 resetBatchingState();
606}
607
608/////////////////////////////////////////////////////////////////////////////////
609// Replay / flush
610/////////////////////////////////////////////////////////////////////////////////
611
Chris Craik527a3aa2013-03-04 10:19:31 -0800612static status_t replayBatchList(const Vector<Batch*>& batchList,
Chris Craikff785832013-03-08 13:12:16 -0800613 OpenGLRenderer& renderer, Rect& dirty) {
614 status_t status = DrawGlInfo::kStatusDone;
615
Chris Craikff785832013-03-08 13:12:16 -0800616 for (unsigned int i = 0; i < batchList.size(); i++) {
Chris Craik28ce94a2013-05-31 11:38:03 -0700617 if (batchList[i]) {
618 status |= batchList[i]->replay(renderer, dirty, i);
619 }
Chris Craikff785832013-03-08 13:12:16 -0800620 }
Chris Craik527a3aa2013-03-04 10:19:31 -0800621 DEFER_LOGD("--flushed, drew %d batches", batchList.size());
Chris Craikff785832013-03-08 13:12:16 -0800622 return status;
623}
624
625status_t DeferredDisplayList::flush(OpenGLRenderer& renderer, Rect& dirty) {
626 ATRACE_NAME("flush drawing commands");
Romain Guycf51a412013-04-08 19:40:31 -0700627 Caches::getInstance().fontRenderer->endPrecaching();
628
Chris Craikc3566d02013-02-04 16:16:33 -0800629 status_t status = DrawGlInfo::kStatusDone;
630
631 if (isEmpty()) return status; // nothing to flush
Chris Craika4e16c52013-03-22 10:00:48 -0700632 renderer.restoreToCount(1);
Chris Craikc3566d02013-02-04 16:16:33 -0800633
634 DEFER_LOGD("--flushing");
Romain Guy0f6675332013-03-01 14:31:04 -0800635 renderer.eventMark("Flush");
636
Chris Craika4e16c52013-03-22 10:00:48 -0700637 // save and restore (with draw modifiers) so that reordering doesn't affect final state
Chris Craikd90144d2013-03-19 15:03:48 -0700638 DrawModifiers restoreDrawModifiers = renderer.getDrawModifiers();
Chris Craika4e16c52013-03-22 10:00:48 -0700639 renderer.save(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
640
Chris Craik28ce94a2013-05-31 11:38:03 -0700641 if (CC_LIKELY(mAvoidOverdraw)) {
642 for (unsigned int i = 1; i < mBatches.size(); i++) {
643 if (mBatches[i] && mBatches[i]->coversBounds(mBounds)) {
644 discardDrawingBatches(i - 1);
645 }
646 }
647 }
Chris Craik1ed30c92013-04-03 12:37:35 -0700648 // NOTE: depth of the save stack at this point, before playback, should be reflected in
649 // FLUSH_SAVE_STACK_DEPTH, so that save/restores match up correctly
Chris Craikff785832013-03-08 13:12:16 -0800650 status |= replayBatchList(mBatches, renderer, dirty);
Chris Craika4e16c52013-03-22 10:00:48 -0700651
652 renderer.restoreToCount(1);
Chris Craikd90144d2013-03-19 15:03:48 -0700653 renderer.setDrawModifiers(restoreDrawModifiers);
Chris Craikc3566d02013-02-04 16:16:33 -0800654
Chris Craikff785832013-03-08 13:12:16 -0800655 DEFER_LOGD("--flush complete, returning %x", status);
Chris Craikc3566d02013-02-04 16:16:33 -0800656 clear();
657 return status;
658}
659
Chris Craikf70119c2013-06-13 11:21:22 -0700660void DeferredDisplayList::discardDrawingBatches(const unsigned int maxIndex) {
Chris Craik28ce94a2013-05-31 11:38:03 -0700661 for (unsigned int i = mEarliestUnclearedIndex; i <= maxIndex; i++) {
Chris Craikf70119c2013-06-13 11:21:22 -0700662 // leave deferred state ops alone for simplicity (empty save restore pairs may now exist)
Chris Craik28ce94a2013-05-31 11:38:03 -0700663 if (mBatches[i] && mBatches[i]->purelyDrawBatch()) {
664 DrawBatch* b = (DrawBatch*) mBatches[i];
665 delete mBatches[i];
666 mBatches.replaceAt(NULL, i);
667 }
668 }
669 mEarliestUnclearedIndex = maxIndex + 1;
670}
671
Chris Craikc3566d02013-02-04 16:16:33 -0800672}; // namespace uirenderer
673}; // namespace android