blob: 9323a3ae676a3264c0ac948cc29998f01e2242a5 [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>
23
Romain Guycf51a412013-04-08 19:40:31 -070024#include "Caches.h"
Chris Craikc3566d02013-02-04 16:16:33 -080025#include "Debug.h"
Chris Craik527a3aa2013-03-04 10:19:31 -080026#include "DeferredDisplayList.h"
Chris Craikc3566d02013-02-04 16:16:33 -080027#include "DisplayListOp.h"
28#include "OpenGLRenderer.h"
29
30#if DEBUG_DEFER
31 #define DEFER_LOGD(...) ALOGD(__VA_ARGS__)
32#else
33 #define DEFER_LOGD(...)
34#endif
35
36namespace android {
37namespace uirenderer {
38
Chris Craik1ed30c92013-04-03 12:37:35 -070039// Depth of the save stack at the beginning of batch playback at flush time
40#define FLUSH_SAVE_STACK_DEPTH 2
41
Chris Craik527a3aa2013-03-04 10:19:31 -080042#define DEBUG_COLOR_BARRIER 0x1f000000
43#define DEBUG_COLOR_MERGEDBATCH 0x5f7f7fff
44#define DEBUG_COLOR_MERGEDBATCH_SOLO 0x5f7fff7f
45
Chris Craikff785832013-03-08 13:12:16 -080046/////////////////////////////////////////////////////////////////////////////////
47// Operation Batches
48/////////////////////////////////////////////////////////////////////////////////
49
Chris Craik527a3aa2013-03-04 10:19:31 -080050class Batch {
Chris Craikc3566d02013-02-04 16:16:33 -080051public:
Chris Craik527a3aa2013-03-04 10:19:31 -080052 virtual status_t replay(OpenGLRenderer& renderer, Rect& dirty, int index) = 0;
53 virtual ~Batch() {}
54};
Chris Craikc3566d02013-02-04 16:16:33 -080055
Chris Craik527a3aa2013-03-04 10:19:31 -080056class DrawBatch : public Batch {
57public:
58 DrawBatch(int batchId, mergeid_t mergeId) : mBatchId(batchId), mMergeId(mergeId) {
59 mOps.clear();
60 }
61
62 virtual ~DrawBatch() { mOps.clear(); }
Chris Craikc3566d02013-02-04 16:16:33 -080063
64 void add(DrawOp* op) {
65 // NOTE: ignore empty bounds special case, since we don't merge across those ops
66 mBounds.unionWith(op->state.mBounds);
67 mOps.add(op);
68 }
69
Chris Craik527a3aa2013-03-04 10:19:31 -080070 bool intersects(Rect& rect) {
Chris Craikc3566d02013-02-04 16:16:33 -080071 if (!rect.intersects(mBounds)) return false;
Chris Craikff785832013-03-08 13:12:16 -080072
Chris Craikc3566d02013-02-04 16:16:33 -080073 for (unsigned int i = 0; i < mOps.size(); i++) {
74 if (rect.intersects(mOps[i]->state.mBounds)) {
75#if DEBUG_DEFER
76 DEFER_LOGD("op intersects with op %p with bounds %f %f %f %f:", mOps[i],
77 mOps[i]->state.mBounds.left, mOps[i]->state.mBounds.top,
78 mOps[i]->state.mBounds.right, mOps[i]->state.mBounds.bottom);
79 mOps[i]->output(2);
80#endif
81 return true;
82 }
83 }
84 return false;
85 }
86
Chris Craik527a3aa2013-03-04 10:19:31 -080087 virtual status_t replay(OpenGLRenderer& renderer, Rect& dirty, int index) {
88 DEFER_LOGD("%d replaying DrawingBatch %p, with %d ops (batch id %x, merge id %p)",
89 index, this, mOps.size(), mOps[0]->getBatchId(), mOps[0]->getMergeId());
Chris Craikff785832013-03-08 13:12:16 -080090
91 status_t status = DrawGlInfo::kStatusDone;
92 DisplayListLogBuffer& logBuffer = DisplayListLogBuffer::getInstance();
93 for (unsigned int i = 0; i < mOps.size(); i++) {
94 DrawOp* op = mOps[i];
95
Chris Craik7273daa2013-03-28 11:25:24 -070096 renderer.restoreDisplayState(op->state);
Chris Craikff785832013-03-08 13:12:16 -080097
98#if DEBUG_DISPLAY_LIST_OPS_AS_EVENTS
Chris Craikd90144d2013-03-19 15:03:48 -070099 renderer.eventMark(op->name());
Chris Craikff785832013-03-08 13:12:16 -0800100#endif
Chris Craikff785832013-03-08 13:12:16 -0800101 logBuffer.writeCommand(0, op->name());
Chris Craikd4b43b32013-05-09 13:07:52 -0700102 status |= op->applyDraw(renderer, dirty);
Chris Craik527a3aa2013-03-04 10:19:31 -0800103
104#if DEBUG_MERGE_BEHAVIOR
105 Rect& bounds = mOps[i]->state.mBounds;
106 int batchColor = 0x1f000000;
107 if (getBatchId() & 0x1) batchColor |= 0x0000ff;
108 if (getBatchId() & 0x2) batchColor |= 0x00ff00;
109 if (getBatchId() & 0x4) batchColor |= 0xff0000;
110 renderer.drawScreenSpaceColorRect(bounds.left, bounds.top, bounds.right, bounds.bottom,
111 batchColor);
112#endif
Chris Craikff785832013-03-08 13:12:16 -0800113 }
114 return status;
115 }
116
Chris Craik527a3aa2013-03-04 10:19:31 -0800117 inline int getBatchId() const { return mBatchId; }
118 inline mergeid_t getMergeId() const { return mMergeId; }
Chris Craikff785832013-03-08 13:12:16 -0800119 inline int count() const { return mOps.size(); }
Chris Craik527a3aa2013-03-04 10:19:31 -0800120
121protected:
Chris Craikff785832013-03-08 13:12:16 -0800122 Vector<DrawOp*> mOps;
Chris Craikc3566d02013-02-04 16:16:33 -0800123 Rect mBounds;
Chris Craik527a3aa2013-03-04 10:19:31 -0800124private:
125 int mBatchId;
126 mergeid_t mMergeId;
Chris Craikc3566d02013-02-04 16:16:33 -0800127};
128
Chris Craik527a3aa2013-03-04 10:19:31 -0800129// compare alphas approximately, with a small margin
130#define NEQ_FALPHA(lhs, rhs) \
131 fabs((float)lhs - (float)rhs) > 0.001f
132
133class MergingDrawBatch : public DrawBatch {
134public:
135 MergingDrawBatch(int batchId, mergeid_t mergeId) : DrawBatch(batchId, mergeId) {}
136
137 /*
138 * Checks if a (mergeable) op can be merged into this batch
139 *
140 * If true, the op's multiDraw must be guaranteed to handle both ops simultaneously, so it is
141 * important to consider all paint attributes used in the draw calls in deciding both a) if an
142 * op tries to merge at all, and b) if the op
143 *
144 * False positives can lead to information from the paints of subsequent merged operations being
145 * dropped, so we make simplifying qualifications on the ops that can merge, per op type.
146 */
147 bool canMergeWith(DrawOp* op) {
148 if (!op->state.mMatrix.isPureTranslate()) return false;
149
150 bool isTextBatch = getBatchId() == DeferredDisplayList::kOpBatch_Text ||
151 getBatchId() == DeferredDisplayList::kOpBatch_ColorText;
152
153 // Overlapping other operations is only allowed for text without shadow. For other ops,
154 // multiDraw isn't guaranteed to overdraw correctly
155 if (!isTextBatch || op->state.mDrawModifiers.mHasShadow) {
156 if (intersects(op->state.mBounds)) return false;
157 }
158
159 const DeferredDisplayState& lhs = op->state;
160 const DeferredDisplayState& rhs = mOps[0]->state;
161
162 if (NEQ_FALPHA(lhs.mAlpha, rhs.mAlpha)) return false;
163
164 // if paints are equal, then modifiers + paint attribs don't need to be compared
165 if (op->mPaint == mOps[0]->mPaint) return true;
166
167 if (op->getPaintAlpha() != mOps[0]->getPaintAlpha()) return false;
168
169 /* Draw Modifiers compatibility check
170 *
171 * Shadows are ignored, as only text uses them, and in that case they are drawn
172 * per-DrawTextOp, before the unified text draw. Because of this, it's always safe to merge
173 * text UNLESS a later draw's shadow should overlays a previous draw's text. This is covered
174 * above with the intersection check.
175 *
176 * OverrideLayerAlpha is also ignored, as it's only used for drawing layers, which are never
177 * merged.
178 *
179 * These ignore cases prevent us from simply memcmp'ing the drawModifiers
180 */
181
182 const DrawModifiers& lhsMod = lhs.mDrawModifiers;
183 const DrawModifiers& rhsMod = rhs.mDrawModifiers;
184 if (lhsMod.mShader != rhsMod.mShader) return false;
185 if (lhsMod.mColorFilter != rhsMod.mColorFilter) return false;
186
187 // Draw filter testing expects bit fields to be clear if filter not set.
188 if (lhsMod.mHasDrawFilter != rhsMod.mHasDrawFilter) return false;
189 if (lhsMod.mPaintFilterClearBits != rhsMod.mPaintFilterClearBits) return false;
190 if (lhsMod.mPaintFilterSetBits != rhsMod.mPaintFilterSetBits) return false;
191
192 return true;
193 }
194
195 virtual status_t replay(OpenGLRenderer& renderer, Rect& dirty, int index) {
196 DEFER_LOGD("%d replaying DrawingBatch %p, with %d ops (batch id %x, merge id %p)",
197 index, this, mOps.size(), getBatchId(), getMergeId());
198 if (mOps.size() == 1) {
199 return DrawBatch::replay(renderer, dirty, false);
200 }
201
202 DrawOp* op = mOps[0];
Chris Craik527a3aa2013-03-04 10:19:31 -0800203 DisplayListLogBuffer& buffer = DisplayListLogBuffer::getInstance();
204 buffer.writeCommand(0, "multiDraw");
205 buffer.writeCommand(1, op->name());
Chris Craikd4b43b32013-05-09 13:07:52 -0700206 status_t status = op->multiDraw(renderer, dirty, mOps, mBounds);
Chris Craik527a3aa2013-03-04 10:19:31 -0800207
208#if DEBUG_MERGE_BEHAVIOR
209 renderer.drawScreenSpaceColorRect(mBounds.left, mBounds.top, mBounds.right, mBounds.bottom,
210 DEBUG_COLOR_MERGEDBATCH);
211#endif
212 return status;
213 }
214};
215
216class StateOpBatch : public Batch {
Chris Craikff785832013-03-08 13:12:16 -0800217public:
218 // creates a single operation batch
219 StateOpBatch(StateOp* op) : mOp(op) {}
220
Chris Craik527a3aa2013-03-04 10:19:31 -0800221 virtual status_t replay(OpenGLRenderer& renderer, Rect& dirty, int index) {
Chris Craikff785832013-03-08 13:12:16 -0800222 DEFER_LOGD("replaying state op batch %p", this);
Chris Craik7273daa2013-03-28 11:25:24 -0700223 renderer.restoreDisplayState(mOp->state);
Chris Craikff785832013-03-08 13:12:16 -0800224
225 // use invalid save count because it won't be used at flush time - RestoreToCountOp is the
226 // only one to use it, and we don't use that class at flush time, instead calling
227 // renderer.restoreToCount directly
228 int saveCount = -1;
229 mOp->applyState(renderer, saveCount);
230 return DrawGlInfo::kStatusDone;
231 }
232
233private:
Chris Craik7273daa2013-03-28 11:25:24 -0700234 const StateOp* mOp;
Chris Craikff785832013-03-08 13:12:16 -0800235};
236
Chris Craik527a3aa2013-03-04 10:19:31 -0800237class RestoreToCountBatch : public Batch {
Chris Craikff785832013-03-08 13:12:16 -0800238public:
Chris Craik7273daa2013-03-28 11:25:24 -0700239 RestoreToCountBatch(StateOp* op, int restoreCount) : mOp(op), mRestoreCount(restoreCount) {}
Chris Craikff785832013-03-08 13:12:16 -0800240
Chris Craik527a3aa2013-03-04 10:19:31 -0800241 virtual status_t replay(OpenGLRenderer& renderer, Rect& dirty, int index) {
Chris Craikff785832013-03-08 13:12:16 -0800242 DEFER_LOGD("batch %p restoring to count %d", this, mRestoreCount);
Chris Craik7273daa2013-03-28 11:25:24 -0700243
244 renderer.restoreDisplayState(mOp->state);
Chris Craikff785832013-03-08 13:12:16 -0800245 renderer.restoreToCount(mRestoreCount);
Chris Craikff785832013-03-08 13:12:16 -0800246 return DrawGlInfo::kStatusDone;
247 }
248
249private:
Chris Craik7273daa2013-03-28 11:25:24 -0700250 // we use the state storage for the RestoreToCountOp, but don't replay the op itself
251 const StateOp* mOp;
Chris Craikff785832013-03-08 13:12:16 -0800252 /*
253 * The count used here represents the flush() time saveCount. This is as opposed to the
254 * DisplayList record time, or defer() time values (which are RestoreToCountOp's mCount, and
255 * (saveCount + mCount) respectively). Since the count is different from the original
256 * RestoreToCountOp, we don't store a pointer to the op, as elsewhere.
257 */
258 const int mRestoreCount;
259};
260
Chris Craik527a3aa2013-03-04 10:19:31 -0800261#if DEBUG_MERGE_BEHAVIOR
262class BarrierDebugBatch : public Batch {
263 virtual status_t replay(OpenGLRenderer& renderer, Rect& dirty, int index) {
264 renderer.drawScreenSpaceColorRect(0, 0, 10000, 10000, DEBUG_COLOR_BARRIER);
265 return DrawGlInfo::kStatusDrew;
266 }
267};
268#endif
269
Chris Craikff785832013-03-08 13:12:16 -0800270/////////////////////////////////////////////////////////////////////////////////
271// DeferredDisplayList
272/////////////////////////////////////////////////////////////////////////////////
273
274void DeferredDisplayList::resetBatchingState() {
Chris Craikc3566d02013-02-04 16:16:33 -0800275 for (int i = 0; i < kOpBatch_Count; i++) {
Chris Craik527a3aa2013-03-04 10:19:31 -0800276 mBatchLookup[i] = NULL;
277 mMergingBatches[i].clear();
Chris Craikc3566d02013-02-04 16:16:33 -0800278 }
Chris Craik527a3aa2013-03-04 10:19:31 -0800279#if DEBUG_MERGE_BEHAVIOR
280 if (mBatches.size() != 0) {
281 mBatches.add(new BarrierDebugBatch());
282 }
283#endif
284 mEarliestBatchIndex = mBatches.size();
Chris Craikff785832013-03-08 13:12:16 -0800285}
286
287void DeferredDisplayList::clear() {
288 resetBatchingState();
289 mComplexClipStackStart = -1;
290
Chris Craikc3566d02013-02-04 16:16:33 -0800291 for (unsigned int i = 0; i < mBatches.size(); i++) {
292 delete mBatches[i];
293 }
294 mBatches.clear();
Chris Craikff785832013-03-08 13:12:16 -0800295 mSaveStack.clear();
Chris Craik527a3aa2013-03-04 10:19:31 -0800296 mEarliestBatchIndex = 0;
Chris Craikc3566d02013-02-04 16:16:33 -0800297}
298
Chris Craikff785832013-03-08 13:12:16 -0800299/////////////////////////////////////////////////////////////////////////////////
300// Operation adding
301/////////////////////////////////////////////////////////////////////////////////
302
303int DeferredDisplayList::getStateOpDeferFlags() const {
304 // For both clipOp and save(Layer)Op, we don't want to save drawing info, and only want to save
305 // the clip if we aren't recording a complex clip (and can thus trust it to be a rect)
306 return recordingComplexClip() ? 0 : kStateDeferFlag_Clip;
307}
308
309int DeferredDisplayList::getDrawOpDeferFlags() const {
310 return kStateDeferFlag_Draw | getStateOpDeferFlags();
311}
312
313/**
314 * When an clipping operation occurs that could cause a complex clip, record the operation and all
315 * subsequent clipOps, save/restores (if the clip flag is set). During a flush, instead of loading
316 * the clip from deferred state, we play back all of the relevant state operations that generated
317 * the complex clip.
318 *
319 * Note that we don't need to record the associated restore operation, since operations at defer
320 * time record whether they should store the renderer's current clip
321 */
322void DeferredDisplayList::addClip(OpenGLRenderer& renderer, ClipOp* op) {
323 if (recordingComplexClip() || op->canCauseComplexClip() || !renderer.hasRectToRectTransform()) {
324 DEFER_LOGD("%p Received complex clip operation %p", this, op);
325
326 // NOTE: defer clip op before setting mComplexClipStackStart so previous clip is recorded
327 storeStateOpBarrier(renderer, op);
328
329 if (!recordingComplexClip()) {
330 mComplexClipStackStart = renderer.getSaveCount() - 1;
331 DEFER_LOGD(" Starting complex clip region, start is %d", mComplexClipStackStart);
Chris Craikc3566d02013-02-04 16:16:33 -0800332 }
Chris Craikff785832013-03-08 13:12:16 -0800333 }
334}
335
336/**
337 * For now, we record save layer operations as barriers in the batch list, preventing drawing
338 * operations from reordering around the saveLayer and it's associated restore()
339 *
340 * In the future, we should send saveLayer commands (if they can be played out of order) and their
341 * contained drawing operations to a seperate list of batches, so that they may draw at the
342 * beginning of the frame. This would avoid targetting and removing an FBO in the middle of a frame.
343 *
344 * saveLayer operations should be pulled to the beginning of the frame if the canvas doesn't have a
345 * complex clip, and if the flags (kClip_SaveFlag & kClipToLayer_SaveFlag) are set.
346 */
347void DeferredDisplayList::addSaveLayer(OpenGLRenderer& renderer,
348 SaveLayerOp* op, int newSaveCount) {
349 DEFER_LOGD("%p adding saveLayerOp %p, flags %x, new count %d",
350 this, op, op->getFlags(), newSaveCount);
351
352 storeStateOpBarrier(renderer, op);
353 mSaveStack.push(newSaveCount);
354}
355
356/**
357 * Takes save op and it's return value - the new save count - and stores it into the stream as a
358 * barrier if it's needed to properly modify a complex clip
359 */
360void DeferredDisplayList::addSave(OpenGLRenderer& renderer, SaveOp* op, int newSaveCount) {
361 int saveFlags = op->getFlags();
362 DEFER_LOGD("%p adding saveOp %p, flags %x, new count %d", this, op, saveFlags, newSaveCount);
363
364 if (recordingComplexClip() && (saveFlags & SkCanvas::kClip_SaveFlag)) {
365 // store and replay the save operation, as it may be needed to correctly playback the clip
366 DEFER_LOGD(" adding save barrier with new save count %d", newSaveCount);
367 storeStateOpBarrier(renderer, op);
368 mSaveStack.push(newSaveCount);
369 }
370}
371
372/**
373 * saveLayer() commands must be associated with a restoreToCount batch that will clean up and draw
374 * the layer in the deferred list
375 *
376 * other save() commands which occur as children of a snapshot with complex clip will be deferred,
377 * and must be restored
378 *
379 * Either will act as a barrier to draw operation reordering, as we want to play back layer
380 * save/restore and complex canvas modifications (including save/restore) in order.
381 */
Chris Craik7273daa2013-03-28 11:25:24 -0700382void DeferredDisplayList::addRestoreToCount(OpenGLRenderer& renderer, StateOp* op,
383 int newSaveCount) {
Chris Craikff785832013-03-08 13:12:16 -0800384 DEFER_LOGD("%p addRestoreToCount %d", this, newSaveCount);
385
386 if (recordingComplexClip() && newSaveCount <= mComplexClipStackStart) {
387 mComplexClipStackStart = -1;
388 resetBatchingState();
389 }
390
391 if (mSaveStack.isEmpty() || newSaveCount > mSaveStack.top()) {
392 return;
393 }
394
395 while (!mSaveStack.isEmpty() && mSaveStack.top() >= newSaveCount) mSaveStack.pop();
396
Chris Craik1ed30c92013-04-03 12:37:35 -0700397 storeRestoreToCountBarrier(renderer, op, mSaveStack.size() + FLUSH_SAVE_STACK_DEPTH);
Chris Craikff785832013-03-08 13:12:16 -0800398}
399
400void DeferredDisplayList::addDrawOp(OpenGLRenderer& renderer, DrawOp* op) {
401 if (renderer.storeDisplayState(op->state, getDrawOpDeferFlags())) {
402 return; // quick rejected
403 }
404
Chris Craik527a3aa2013-03-04 10:19:31 -0800405 int batchId = kOpBatch_None;
406 mergeid_t mergeId = (mergeid_t) -1;
407 bool mergeable = op->onDefer(renderer, &batchId, &mergeId);
408
409 // complex clip has a complex set of expectations on the renderer state - for now, avoid taking
410 // the merge path in those cases
411 mergeable &= !recordingComplexClip();
Chris Craikff785832013-03-08 13:12:16 -0800412
413 if (CC_UNLIKELY(renderer.getCaches().drawReorderDisabled)) {
414 // TODO: elegant way to reuse batches?
Chris Craik527a3aa2013-03-04 10:19:31 -0800415 DrawBatch* b = new DrawBatch(batchId, mergeId);
Chris Craikc3566d02013-02-04 16:16:33 -0800416 b->add(op);
417 mBatches.add(b);
418 return;
419 }
420
Chris Craik527a3aa2013-03-04 10:19:31 -0800421 // find the latest batch of the new op's type, and try to merge the new op into it
422 DrawBatch* targetBatch = NULL;
Chris Craikc3566d02013-02-04 16:16:33 -0800423
Chris Craik527a3aa2013-03-04 10:19:31 -0800424 // insertion point of a new batch, will hopefully be immediately after similar batch
425 // (eventually, should be similar shader)
426 int insertBatchIndex = mBatches.size();
Chris Craikc3566d02013-02-04 16:16:33 -0800427 if (!mBatches.isEmpty()) {
428 if (op->state.mBounds.isEmpty()) {
429 // don't know the bounds for op, so add to last batch and start from scratch on next op
Chris Craik527a3aa2013-03-04 10:19:31 -0800430 DrawBatch* b = new DrawBatch(batchId, mergeId);
431 b->add(op);
432 mBatches.add(b);
433 resetBatchingState();
Chris Craikc3566d02013-02-04 16:16:33 -0800434#if DEBUG_DEFER
435 DEFER_LOGD("Warning: Encountered op with empty bounds, resetting batches");
436 op->output(2);
437#endif
438 return;
439 }
440
Chris Craik527a3aa2013-03-04 10:19:31 -0800441 if (mergeable) {
442 // Try to merge with any existing batch with same mergeId.
443 if (mMergingBatches[batchId].get(mergeId, targetBatch)) {
444 if (!((MergingDrawBatch*) targetBatch)->canMergeWith(op)) {
445 targetBatch = NULL;
446 }
447 }
448 } else {
449 // join with similar, non-merging batch
450 targetBatch = (DrawBatch*)mBatchLookup[batchId];
451 }
452
453 if (targetBatch || mergeable) {
Chris Craikc3566d02013-02-04 16:16:33 -0800454 // iterate back toward target to see if anything drawn since should overlap the new op
Chris Craik527a3aa2013-03-04 10:19:31 -0800455 // if no target, merging ops still interate to find similar batch to insert after
456 for (int i = mBatches.size() - 1; i >= mEarliestBatchIndex; i--) {
457 DrawBatch* overBatch = (DrawBatch*)mBatches[i];
458
459 if (overBatch == targetBatch) break;
460
461 // TODO: also consider shader shared between batch types
462 if (batchId == overBatch->getBatchId()) {
463 insertBatchIndex = i + 1;
464 if (!targetBatch) break; // found insert position, quit
465 }
466
Chris Craikc3566d02013-02-04 16:16:33 -0800467 if (overBatch->intersects(op->state.mBounds)) {
Chris Craik527a3aa2013-03-04 10:19:31 -0800468 // NOTE: it may be possible to optimize for special cases where two operations
469 // of the same batch/paint could swap order, such as with a non-mergeable
470 // (clipped) and a mergeable text operation
Chris Craikc3566d02013-02-04 16:16:33 -0800471 targetBatch = NULL;
472#if DEBUG_DEFER
473 DEFER_LOGD("op couldn't join batch %d, was intersected by batch %d",
474 targetIndex, i);
475 op->output(2);
476#endif
477 break;
478 }
479 }
480 }
481 }
Chris Craik527a3aa2013-03-04 10:19:31 -0800482
Chris Craikc3566d02013-02-04 16:16:33 -0800483 if (!targetBatch) {
Chris Craik527a3aa2013-03-04 10:19:31 -0800484 if (mergeable) {
485 targetBatch = new MergingDrawBatch(batchId, mergeId);
486 mMergingBatches[batchId].put(mergeId, targetBatch);
487 } else {
488 targetBatch = new DrawBatch(batchId, mergeId);
489 mBatchLookup[batchId] = targetBatch;
490 DEFER_LOGD("creating Batch %p, bid %x, at %d",
491 targetBatch, batchId, insertBatchIndex);
Chris Craikc3566d02013-02-04 16:16:33 -0800492 }
Chris Craik527a3aa2013-03-04 10:19:31 -0800493
494 mBatches.insertAt(targetBatch, insertBatchIndex);
Chris Craikc3566d02013-02-04 16:16:33 -0800495 }
Chris Craik527a3aa2013-03-04 10:19:31 -0800496
Chris Craikc3566d02013-02-04 16:16:33 -0800497 targetBatch->add(op);
498}
499
Chris Craikff785832013-03-08 13:12:16 -0800500void DeferredDisplayList::storeStateOpBarrier(OpenGLRenderer& renderer, StateOp* op) {
501 DEFER_LOGD("%p adding state op barrier at pos %d", this, mBatches.size());
502
503 renderer.storeDisplayState(op->state, getStateOpDeferFlags());
504 mBatches.add(new StateOpBatch(op));
505 resetBatchingState();
506}
507
Chris Craik7273daa2013-03-28 11:25:24 -0700508void DeferredDisplayList::storeRestoreToCountBarrier(OpenGLRenderer& renderer, StateOp* op,
509 int newSaveCount) {
Chris Craikff785832013-03-08 13:12:16 -0800510 DEFER_LOGD("%p adding restore to count %d barrier, pos %d",
511 this, newSaveCount, mBatches.size());
512
Chris Craik7273daa2013-03-28 11:25:24 -0700513 // store displayState for the restore operation, as it may be associated with a saveLayer that
514 // doesn't have kClip_SaveFlag set
515 renderer.storeDisplayState(op->state, getStateOpDeferFlags());
516 mBatches.add(new RestoreToCountBatch(op, newSaveCount));
Chris Craikff785832013-03-08 13:12:16 -0800517 resetBatchingState();
518}
519
520/////////////////////////////////////////////////////////////////////////////////
521// Replay / flush
522/////////////////////////////////////////////////////////////////////////////////
523
Chris Craik527a3aa2013-03-04 10:19:31 -0800524static status_t replayBatchList(const Vector<Batch*>& batchList,
Chris Craikff785832013-03-08 13:12:16 -0800525 OpenGLRenderer& renderer, Rect& dirty) {
526 status_t status = DrawGlInfo::kStatusDone;
527
Chris Craikff785832013-03-08 13:12:16 -0800528 for (unsigned int i = 0; i < batchList.size(); i++) {
Chris Craik527a3aa2013-03-04 10:19:31 -0800529 status |= batchList[i]->replay(renderer, dirty, i);
Chris Craikff785832013-03-08 13:12:16 -0800530 }
Chris Craik527a3aa2013-03-04 10:19:31 -0800531 DEFER_LOGD("--flushed, drew %d batches", batchList.size());
Chris Craikff785832013-03-08 13:12:16 -0800532 return status;
533}
534
535status_t DeferredDisplayList::flush(OpenGLRenderer& renderer, Rect& dirty) {
536 ATRACE_NAME("flush drawing commands");
Romain Guycf51a412013-04-08 19:40:31 -0700537 Caches::getInstance().fontRenderer->endPrecaching();
538
Chris Craikc3566d02013-02-04 16:16:33 -0800539 status_t status = DrawGlInfo::kStatusDone;
540
541 if (isEmpty()) return status; // nothing to flush
Chris Craika4e16c52013-03-22 10:00:48 -0700542 renderer.restoreToCount(1);
Chris Craikc3566d02013-02-04 16:16:33 -0800543
544 DEFER_LOGD("--flushing");
Romain Guy0f667532013-03-01 14:31:04 -0800545 renderer.eventMark("Flush");
546
Chris Craika4e16c52013-03-22 10:00:48 -0700547 // save and restore (with draw modifiers) so that reordering doesn't affect final state
Chris Craikd90144d2013-03-19 15:03:48 -0700548 DrawModifiers restoreDrawModifiers = renderer.getDrawModifiers();
Chris Craika4e16c52013-03-22 10:00:48 -0700549 renderer.save(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
550
Chris Craik1ed30c92013-04-03 12:37:35 -0700551 // NOTE: depth of the save stack at this point, before playback, should be reflected in
552 // FLUSH_SAVE_STACK_DEPTH, so that save/restores match up correctly
Chris Craikff785832013-03-08 13:12:16 -0800553 status |= replayBatchList(mBatches, renderer, dirty);
Chris Craika4e16c52013-03-22 10:00:48 -0700554
555 renderer.restoreToCount(1);
Chris Craikd90144d2013-03-19 15:03:48 -0700556 renderer.setDrawModifiers(restoreDrawModifiers);
Chris Craikc3566d02013-02-04 16:16:33 -0800557
Chris Craikff785832013-03-08 13:12:16 -0800558 DEFER_LOGD("--flush complete, returning %x", status);
Chris Craikc3566d02013-02-04 16:16:33 -0800559 clear();
560 return status;
561}
562
563}; // namespace uirenderer
564}; // namespace android