blob: d5007e12ebf99847a5a4b760a1e57a0735d941eb [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"
26#include "DisplayListOp.h"
27#include "OpenGLRenderer.h"
28
29#if DEBUG_DEFER
30 #define DEFER_LOGD(...) ALOGD(__VA_ARGS__)
31#else
32 #define DEFER_LOGD(...)
33#endif
34
35namespace android {
36namespace uirenderer {
37
Chris Craik1ed30c92013-04-03 12:37:35 -070038// Depth of the save stack at the beginning of batch playback at flush time
39#define FLUSH_SAVE_STACK_DEPTH 2
40
Chris Craikff785832013-03-08 13:12:16 -080041/////////////////////////////////////////////////////////////////////////////////
42// Operation Batches
43/////////////////////////////////////////////////////////////////////////////////
44
Chris Craikc3566d02013-02-04 16:16:33 -080045class DrawOpBatch {
46public:
Chris Craikff785832013-03-08 13:12:16 -080047 DrawOpBatch() { mOps.clear(); }
Chris Craikc3566d02013-02-04 16:16:33 -080048
Chris Craikff785832013-03-08 13:12:16 -080049 virtual ~DrawOpBatch() { mOps.clear(); }
Chris Craikc3566d02013-02-04 16:16:33 -080050
51 void add(DrawOp* op) {
52 // NOTE: ignore empty bounds special case, since we don't merge across those ops
53 mBounds.unionWith(op->state.mBounds);
54 mOps.add(op);
55 }
56
Chris Craikff785832013-03-08 13:12:16 -080057 virtual bool intersects(Rect& rect) {
Chris Craikc3566d02013-02-04 16:16:33 -080058 if (!rect.intersects(mBounds)) return false;
Chris Craikff785832013-03-08 13:12:16 -080059
Chris Craikc3566d02013-02-04 16:16:33 -080060 for (unsigned int i = 0; i < mOps.size(); i++) {
61 if (rect.intersects(mOps[i]->state.mBounds)) {
62#if DEBUG_DEFER
63 DEFER_LOGD("op intersects with op %p with bounds %f %f %f %f:", mOps[i],
64 mOps[i]->state.mBounds.left, mOps[i]->state.mBounds.top,
65 mOps[i]->state.mBounds.right, mOps[i]->state.mBounds.bottom);
66 mOps[i]->output(2);
67#endif
68 return true;
69 }
70 }
71 return false;
72 }
73
Chris Craikff785832013-03-08 13:12:16 -080074 virtual status_t replay(OpenGLRenderer& renderer, Rect& dirty) {
75 DEFER_LOGD("replaying draw batch %p", this);
76
77 status_t status = DrawGlInfo::kStatusDone;
78 DisplayListLogBuffer& logBuffer = DisplayListLogBuffer::getInstance();
79 for (unsigned int i = 0; i < mOps.size(); i++) {
80 DrawOp* op = mOps[i];
81
Chris Craik7273daa2013-03-28 11:25:24 -070082 renderer.restoreDisplayState(op->state);
Chris Craikff785832013-03-08 13:12:16 -080083
84#if DEBUG_DISPLAY_LIST_OPS_AS_EVENTS
Chris Craikd90144d2013-03-19 15:03:48 -070085 renderer.eventMark(op->name());
Chris Craikff785832013-03-08 13:12:16 -080086#endif
Chris Craika08f95c2013-03-15 17:24:33 -070087 status |= op->applyDraw(renderer, dirty, 0);
Chris Craikff785832013-03-08 13:12:16 -080088 logBuffer.writeCommand(0, op->name());
89 }
90 return status;
91 }
92
93 inline int count() const { return mOps.size(); }
Chris Craikc3566d02013-02-04 16:16:33 -080094private:
Chris Craikff785832013-03-08 13:12:16 -080095 Vector<DrawOp*> mOps;
Chris Craikc3566d02013-02-04 16:16:33 -080096 Rect mBounds;
97};
98
Chris Craikff785832013-03-08 13:12:16 -080099class StateOpBatch : public DrawOpBatch {
100public:
101 // creates a single operation batch
102 StateOpBatch(StateOp* op) : mOp(op) {}
103
104 bool intersects(Rect& rect) {
105 // if something checks for intersection, it's trying to go backwards across a state op,
106 // something not currently supported - state ops are always barriers
107 CRASH();
108 return false;
109 }
110
111 virtual status_t replay(OpenGLRenderer& renderer, Rect& dirty) {
112 DEFER_LOGD("replaying state op batch %p", this);
Chris Craik7273daa2013-03-28 11:25:24 -0700113 renderer.restoreDisplayState(mOp->state);
Chris Craikff785832013-03-08 13:12:16 -0800114
115 // use invalid save count because it won't be used at flush time - RestoreToCountOp is the
116 // only one to use it, and we don't use that class at flush time, instead calling
117 // renderer.restoreToCount directly
118 int saveCount = -1;
119 mOp->applyState(renderer, saveCount);
120 return DrawGlInfo::kStatusDone;
121 }
122
123private:
Chris Craik7273daa2013-03-28 11:25:24 -0700124 const StateOp* mOp;
Chris Craikff785832013-03-08 13:12:16 -0800125};
126
127class RestoreToCountBatch : public DrawOpBatch {
128public:
Chris Craik7273daa2013-03-28 11:25:24 -0700129 RestoreToCountBatch(StateOp* op, int restoreCount) : mOp(op), mRestoreCount(restoreCount) {}
Chris Craikff785832013-03-08 13:12:16 -0800130
131 bool intersects(Rect& rect) {
132 // if something checks for intersection, it's trying to go backwards across a state op,
133 // something not currently supported - state ops are always barriers
134 CRASH();
135 return false;
136 }
137
138 virtual status_t replay(OpenGLRenderer& renderer, Rect& dirty) {
139 DEFER_LOGD("batch %p restoring to count %d", this, mRestoreCount);
Chris Craik7273daa2013-03-28 11:25:24 -0700140
141 renderer.restoreDisplayState(mOp->state);
Chris Craikff785832013-03-08 13:12:16 -0800142 renderer.restoreToCount(mRestoreCount);
Chris Craikff785832013-03-08 13:12:16 -0800143 return DrawGlInfo::kStatusDone;
144 }
145
146private:
Chris Craik7273daa2013-03-28 11:25:24 -0700147 // we use the state storage for the RestoreToCountOp, but don't replay the op itself
148 const StateOp* mOp;
Chris Craikff785832013-03-08 13:12:16 -0800149 /*
150 * The count used here represents the flush() time saveCount. This is as opposed to the
151 * DisplayList record time, or defer() time values (which are RestoreToCountOp's mCount, and
152 * (saveCount + mCount) respectively). Since the count is different from the original
153 * RestoreToCountOp, we don't store a pointer to the op, as elsewhere.
154 */
155 const int mRestoreCount;
156};
157
158/////////////////////////////////////////////////////////////////////////////////
159// DeferredDisplayList
160/////////////////////////////////////////////////////////////////////////////////
161
162void DeferredDisplayList::resetBatchingState() {
Chris Craikc3566d02013-02-04 16:16:33 -0800163 for (int i = 0; i < kOpBatch_Count; i++) {
164 mBatchIndices[i] = -1;
165 }
Chris Craikff785832013-03-08 13:12:16 -0800166}
167
168void DeferredDisplayList::clear() {
169 resetBatchingState();
170 mComplexClipStackStart = -1;
171
Chris Craikc3566d02013-02-04 16:16:33 -0800172 for (unsigned int i = 0; i < mBatches.size(); i++) {
173 delete mBatches[i];
174 }
175 mBatches.clear();
Chris Craikff785832013-03-08 13:12:16 -0800176 mSaveStack.clear();
Chris Craikc3566d02013-02-04 16:16:33 -0800177}
178
Chris Craikff785832013-03-08 13:12:16 -0800179/////////////////////////////////////////////////////////////////////////////////
180// Operation adding
181/////////////////////////////////////////////////////////////////////////////////
182
183int DeferredDisplayList::getStateOpDeferFlags() const {
184 // For both clipOp and save(Layer)Op, we don't want to save drawing info, and only want to save
185 // the clip if we aren't recording a complex clip (and can thus trust it to be a rect)
186 return recordingComplexClip() ? 0 : kStateDeferFlag_Clip;
187}
188
189int DeferredDisplayList::getDrawOpDeferFlags() const {
190 return kStateDeferFlag_Draw | getStateOpDeferFlags();
191}
192
193/**
194 * When an clipping operation occurs that could cause a complex clip, record the operation and all
195 * subsequent clipOps, save/restores (if the clip flag is set). During a flush, instead of loading
196 * the clip from deferred state, we play back all of the relevant state operations that generated
197 * the complex clip.
198 *
199 * Note that we don't need to record the associated restore operation, since operations at defer
200 * time record whether they should store the renderer's current clip
201 */
202void DeferredDisplayList::addClip(OpenGLRenderer& renderer, ClipOp* op) {
203 if (recordingComplexClip() || op->canCauseComplexClip() || !renderer.hasRectToRectTransform()) {
204 DEFER_LOGD("%p Received complex clip operation %p", this, op);
205
206 // NOTE: defer clip op before setting mComplexClipStackStart so previous clip is recorded
207 storeStateOpBarrier(renderer, op);
208
209 if (!recordingComplexClip()) {
210 mComplexClipStackStart = renderer.getSaveCount() - 1;
211 DEFER_LOGD(" Starting complex clip region, start is %d", mComplexClipStackStart);
Chris Craikc3566d02013-02-04 16:16:33 -0800212 }
Chris Craikff785832013-03-08 13:12:16 -0800213 }
214}
215
216/**
217 * For now, we record save layer operations as barriers in the batch list, preventing drawing
218 * operations from reordering around the saveLayer and it's associated restore()
219 *
220 * In the future, we should send saveLayer commands (if they can be played out of order) and their
221 * contained drawing operations to a seperate list of batches, so that they may draw at the
222 * beginning of the frame. This would avoid targetting and removing an FBO in the middle of a frame.
223 *
224 * saveLayer operations should be pulled to the beginning of the frame if the canvas doesn't have a
225 * complex clip, and if the flags (kClip_SaveFlag & kClipToLayer_SaveFlag) are set.
226 */
227void DeferredDisplayList::addSaveLayer(OpenGLRenderer& renderer,
228 SaveLayerOp* op, int newSaveCount) {
229 DEFER_LOGD("%p adding saveLayerOp %p, flags %x, new count %d",
230 this, op, op->getFlags(), newSaveCount);
231
232 storeStateOpBarrier(renderer, op);
233 mSaveStack.push(newSaveCount);
234}
235
236/**
237 * Takes save op and it's return value - the new save count - and stores it into the stream as a
238 * barrier if it's needed to properly modify a complex clip
239 */
240void DeferredDisplayList::addSave(OpenGLRenderer& renderer, SaveOp* op, int newSaveCount) {
241 int saveFlags = op->getFlags();
242 DEFER_LOGD("%p adding saveOp %p, flags %x, new count %d", this, op, saveFlags, newSaveCount);
243
244 if (recordingComplexClip() && (saveFlags & SkCanvas::kClip_SaveFlag)) {
245 // store and replay the save operation, as it may be needed to correctly playback the clip
246 DEFER_LOGD(" adding save barrier with new save count %d", newSaveCount);
247 storeStateOpBarrier(renderer, op);
248 mSaveStack.push(newSaveCount);
249 }
250}
251
252/**
253 * saveLayer() commands must be associated with a restoreToCount batch that will clean up and draw
254 * the layer in the deferred list
255 *
256 * other save() commands which occur as children of a snapshot with complex clip will be deferred,
257 * and must be restored
258 *
259 * Either will act as a barrier to draw operation reordering, as we want to play back layer
260 * save/restore and complex canvas modifications (including save/restore) in order.
261 */
Chris Craik7273daa2013-03-28 11:25:24 -0700262void DeferredDisplayList::addRestoreToCount(OpenGLRenderer& renderer, StateOp* op,
263 int newSaveCount) {
Chris Craikff785832013-03-08 13:12:16 -0800264 DEFER_LOGD("%p addRestoreToCount %d", this, newSaveCount);
265
266 if (recordingComplexClip() && newSaveCount <= mComplexClipStackStart) {
267 mComplexClipStackStart = -1;
268 resetBatchingState();
269 }
270
271 if (mSaveStack.isEmpty() || newSaveCount > mSaveStack.top()) {
272 return;
273 }
274
275 while (!mSaveStack.isEmpty() && mSaveStack.top() >= newSaveCount) mSaveStack.pop();
276
Chris Craik1ed30c92013-04-03 12:37:35 -0700277 storeRestoreToCountBarrier(renderer, op, mSaveStack.size() + FLUSH_SAVE_STACK_DEPTH);
Chris Craikff785832013-03-08 13:12:16 -0800278}
279
280void DeferredDisplayList::addDrawOp(OpenGLRenderer& renderer, DrawOp* op) {
281 if (renderer.storeDisplayState(op->state, getDrawOpDeferFlags())) {
282 return; // quick rejected
283 }
284
285 op->onDrawOpDeferred(renderer);
286
287 if (CC_UNLIKELY(renderer.getCaches().drawReorderDisabled)) {
288 // TODO: elegant way to reuse batches?
Chris Craikc3566d02013-02-04 16:16:33 -0800289 DrawOpBatch* b = new DrawOpBatch();
290 b->add(op);
291 mBatches.add(b);
292 return;
293 }
294
295 // disallowReorder isn't set, so find the latest batch of the new op's type, and try to merge
296 // the new op into it
297 DrawOpBatch* targetBatch = NULL;
298 int batchId = op->getBatchId();
299
300 if (!mBatches.isEmpty()) {
301 if (op->state.mBounds.isEmpty()) {
302 // don't know the bounds for op, so add to last batch and start from scratch on next op
303 mBatches.top()->add(op);
304 for (int i = 0; i < kOpBatch_Count; i++) {
305 mBatchIndices[i] = -1;
306 }
307#if DEBUG_DEFER
308 DEFER_LOGD("Warning: Encountered op with empty bounds, resetting batches");
309 op->output(2);
310#endif
311 return;
312 }
313
314 if (batchId >= 0 && mBatchIndices[batchId] != -1) {
315 int targetIndex = mBatchIndices[batchId];
316 targetBatch = mBatches[targetIndex];
317 // iterate back toward target to see if anything drawn since should overlap the new op
318 for (int i = mBatches.size() - 1; i > targetIndex; i--) {
319 DrawOpBatch* overBatch = mBatches[i];
320 if (overBatch->intersects(op->state.mBounds)) {
321 targetBatch = NULL;
322#if DEBUG_DEFER
323 DEFER_LOGD("op couldn't join batch %d, was intersected by batch %d",
324 targetIndex, i);
325 op->output(2);
326#endif
327 break;
328 }
329 }
330 }
331 }
332 if (!targetBatch) {
333 targetBatch = new DrawOpBatch();
334 mBatches.add(targetBatch);
335 if (batchId >= 0) {
336 mBatchIndices[batchId] = mBatches.size() - 1;
337 }
338 }
339 targetBatch->add(op);
340}
341
Chris Craikff785832013-03-08 13:12:16 -0800342void DeferredDisplayList::storeStateOpBarrier(OpenGLRenderer& renderer, StateOp* op) {
343 DEFER_LOGD("%p adding state op barrier at pos %d", this, mBatches.size());
344
345 renderer.storeDisplayState(op->state, getStateOpDeferFlags());
346 mBatches.add(new StateOpBatch(op));
347 resetBatchingState();
348}
349
Chris Craik7273daa2013-03-28 11:25:24 -0700350void DeferredDisplayList::storeRestoreToCountBarrier(OpenGLRenderer& renderer, StateOp* op,
351 int newSaveCount) {
Chris Craikff785832013-03-08 13:12:16 -0800352 DEFER_LOGD("%p adding restore to count %d barrier, pos %d",
353 this, newSaveCount, mBatches.size());
354
Chris Craik7273daa2013-03-28 11:25:24 -0700355 // store displayState for the restore operation, as it may be associated with a saveLayer that
356 // doesn't have kClip_SaveFlag set
357 renderer.storeDisplayState(op->state, getStateOpDeferFlags());
358 mBatches.add(new RestoreToCountBatch(op, newSaveCount));
Chris Craikff785832013-03-08 13:12:16 -0800359 resetBatchingState();
360}
361
362/////////////////////////////////////////////////////////////////////////////////
363// Replay / flush
364/////////////////////////////////////////////////////////////////////////////////
365
366static status_t replayBatchList(Vector<DrawOpBatch*>& batchList,
367 OpenGLRenderer& renderer, Rect& dirty) {
368 status_t status = DrawGlInfo::kStatusDone;
369
370 int opCount = 0;
371 for (unsigned int i = 0; i < batchList.size(); i++) {
372 status |= batchList[i]->replay(renderer, dirty);
373 opCount += batchList[i]->count();
374 }
375 DEFER_LOGD("--flushed, drew %d batches (total %d ops)", batchList.size(), opCount);
376 return status;
377}
378
379status_t DeferredDisplayList::flush(OpenGLRenderer& renderer, Rect& dirty) {
380 ATRACE_NAME("flush drawing commands");
Romain Guycf51a412013-04-08 19:40:31 -0700381 Caches::getInstance().fontRenderer->endPrecaching();
382
Chris Craikc3566d02013-02-04 16:16:33 -0800383 status_t status = DrawGlInfo::kStatusDone;
384
385 if (isEmpty()) return status; // nothing to flush
Chris Craika4e16c52013-03-22 10:00:48 -0700386 renderer.restoreToCount(1);
Chris Craikc3566d02013-02-04 16:16:33 -0800387
388 DEFER_LOGD("--flushing");
Romain Guy0f667532013-03-01 14:31:04 -0800389 renderer.eventMark("Flush");
390
Chris Craika4e16c52013-03-22 10:00:48 -0700391 // save and restore (with draw modifiers) so that reordering doesn't affect final state
Chris Craikd90144d2013-03-19 15:03:48 -0700392 DrawModifiers restoreDrawModifiers = renderer.getDrawModifiers();
Chris Craika4e16c52013-03-22 10:00:48 -0700393 renderer.save(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
394
Chris Craik1ed30c92013-04-03 12:37:35 -0700395 // NOTE: depth of the save stack at this point, before playback, should be reflected in
396 // FLUSH_SAVE_STACK_DEPTH, so that save/restores match up correctly
Chris Craikff785832013-03-08 13:12:16 -0800397 status |= replayBatchList(mBatches, renderer, dirty);
Chris Craika4e16c52013-03-22 10:00:48 -0700398
399 renderer.restoreToCount(1);
Chris Craikd90144d2013-03-19 15:03:48 -0700400 renderer.setDrawModifiers(restoreDrawModifiers);
Chris Craikc3566d02013-02-04 16:16:33 -0800401
Chris Craikff785832013-03-08 13:12:16 -0800402 DEFER_LOGD("--flush complete, returning %x", status);
Chris Craikc3566d02013-02-04 16:16:33 -0800403
Chris Craikc3566d02013-02-04 16:16:33 -0800404 clear();
405 return status;
406}
407
408}; // namespace uirenderer
409}; // namespace android