Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 1 | /* |
| 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 Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 20 | #include <SkCanvas.h> |
| 21 | |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 22 | #include <utils/Trace.h> |
| 23 | |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 24 | #include "Caches.h" |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 25 | #include "Debug.h" |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 26 | #include "DeferredDisplayList.h" |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 27 | #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 | |
| 36 | namespace android { |
| 37 | namespace uirenderer { |
| 38 | |
Chris Craik | 1ed30c9 | 2013-04-03 12:37:35 -0700 | [diff] [blame] | 39 | // Depth of the save stack at the beginning of batch playback at flush time |
| 40 | #define FLUSH_SAVE_STACK_DEPTH 2 |
| 41 | |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 42 | #define DEBUG_COLOR_BARRIER 0x1f000000 |
| 43 | #define DEBUG_COLOR_MERGEDBATCH 0x5f7f7fff |
| 44 | #define DEBUG_COLOR_MERGEDBATCH_SOLO 0x5f7fff7f |
| 45 | |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 46 | ///////////////////////////////////////////////////////////////////////////////// |
| 47 | // Operation Batches |
| 48 | ///////////////////////////////////////////////////////////////////////////////// |
| 49 | |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 50 | class Batch { |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 51 | public: |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 52 | virtual status_t replay(OpenGLRenderer& renderer, Rect& dirty, int index) = 0; |
| 53 | virtual ~Batch() {} |
| 54 | }; |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 55 | |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 56 | class DrawBatch : public Batch { |
| 57 | public: |
| 58 | DrawBatch(int batchId, mergeid_t mergeId) : mBatchId(batchId), mMergeId(mergeId) { |
| 59 | mOps.clear(); |
| 60 | } |
| 61 | |
| 62 | virtual ~DrawBatch() { mOps.clear(); } |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 63 | |
| 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 Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 70 | bool intersects(Rect& rect) { |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 71 | if (!rect.intersects(mBounds)) return false; |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 72 | |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 73 | 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 Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 87 | 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 Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 90 | |
| 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 Craik | 7273daa | 2013-03-28 11:25:24 -0700 | [diff] [blame] | 96 | renderer.restoreDisplayState(op->state); |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 97 | |
| 98 | #if DEBUG_DISPLAY_LIST_OPS_AS_EVENTS |
Chris Craik | d90144d | 2013-03-19 15:03:48 -0700 | [diff] [blame] | 99 | renderer.eventMark(op->name()); |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 100 | #endif |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 101 | logBuffer.writeCommand(0, op->name()); |
Chris Craik | d4b43b3 | 2013-05-09 13:07:52 -0700 | [diff] [blame] | 102 | status |= op->applyDraw(renderer, dirty); |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 103 | |
| 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 Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 113 | } |
| 114 | return status; |
| 115 | } |
| 116 | |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 117 | inline int getBatchId() const { return mBatchId; } |
| 118 | inline mergeid_t getMergeId() const { return mMergeId; } |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 119 | inline int count() const { return mOps.size(); } |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 120 | |
| 121 | protected: |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 122 | Vector<DrawOp*> mOps; |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 123 | Rect mBounds; |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 124 | private: |
| 125 | int mBatchId; |
| 126 | mergeid_t mMergeId; |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 127 | }; |
| 128 | |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 129 | // compare alphas approximately, with a small margin |
| 130 | #define NEQ_FALPHA(lhs, rhs) \ |
| 131 | fabs((float)lhs - (float)rhs) > 0.001f |
| 132 | |
| 133 | class MergingDrawBatch : public DrawBatch { |
| 134 | public: |
| 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 Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 203 | DisplayListLogBuffer& buffer = DisplayListLogBuffer::getInstance(); |
| 204 | buffer.writeCommand(0, "multiDraw"); |
| 205 | buffer.writeCommand(1, op->name()); |
Chris Craik | d4b43b3 | 2013-05-09 13:07:52 -0700 | [diff] [blame] | 206 | status_t status = op->multiDraw(renderer, dirty, mOps, mBounds); |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 207 | |
| 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 | |
| 216 | class StateOpBatch : public Batch { |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 217 | public: |
| 218 | // creates a single operation batch |
| 219 | StateOpBatch(StateOp* op) : mOp(op) {} |
| 220 | |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 221 | virtual status_t replay(OpenGLRenderer& renderer, Rect& dirty, int index) { |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 222 | DEFER_LOGD("replaying state op batch %p", this); |
Chris Craik | 7273daa | 2013-03-28 11:25:24 -0700 | [diff] [blame] | 223 | renderer.restoreDisplayState(mOp->state); |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 224 | |
| 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 | |
| 233 | private: |
Chris Craik | 7273daa | 2013-03-28 11:25:24 -0700 | [diff] [blame] | 234 | const StateOp* mOp; |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 235 | }; |
| 236 | |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 237 | class RestoreToCountBatch : public Batch { |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 238 | public: |
Chris Craik | 7273daa | 2013-03-28 11:25:24 -0700 | [diff] [blame] | 239 | RestoreToCountBatch(StateOp* op, int restoreCount) : mOp(op), mRestoreCount(restoreCount) {} |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 240 | |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 241 | virtual status_t replay(OpenGLRenderer& renderer, Rect& dirty, int index) { |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 242 | DEFER_LOGD("batch %p restoring to count %d", this, mRestoreCount); |
Chris Craik | 7273daa | 2013-03-28 11:25:24 -0700 | [diff] [blame] | 243 | |
| 244 | renderer.restoreDisplayState(mOp->state); |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 245 | renderer.restoreToCount(mRestoreCount); |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 246 | return DrawGlInfo::kStatusDone; |
| 247 | } |
| 248 | |
| 249 | private: |
Chris Craik | 7273daa | 2013-03-28 11:25:24 -0700 | [diff] [blame] | 250 | // we use the state storage for the RestoreToCountOp, but don't replay the op itself |
| 251 | const StateOp* mOp; |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 252 | /* |
| 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 Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 261 | #if DEBUG_MERGE_BEHAVIOR |
| 262 | class 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 Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 270 | ///////////////////////////////////////////////////////////////////////////////// |
| 271 | // DeferredDisplayList |
| 272 | ///////////////////////////////////////////////////////////////////////////////// |
| 273 | |
| 274 | void DeferredDisplayList::resetBatchingState() { |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 275 | for (int i = 0; i < kOpBatch_Count; i++) { |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 276 | mBatchLookup[i] = NULL; |
| 277 | mMergingBatches[i].clear(); |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 278 | } |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 279 | #if DEBUG_MERGE_BEHAVIOR |
| 280 | if (mBatches.size() != 0) { |
| 281 | mBatches.add(new BarrierDebugBatch()); |
| 282 | } |
| 283 | #endif |
| 284 | mEarliestBatchIndex = mBatches.size(); |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | void DeferredDisplayList::clear() { |
| 288 | resetBatchingState(); |
| 289 | mComplexClipStackStart = -1; |
| 290 | |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 291 | for (unsigned int i = 0; i < mBatches.size(); i++) { |
| 292 | delete mBatches[i]; |
| 293 | } |
| 294 | mBatches.clear(); |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 295 | mSaveStack.clear(); |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 296 | mEarliestBatchIndex = 0; |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 297 | } |
| 298 | |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 299 | ///////////////////////////////////////////////////////////////////////////////// |
| 300 | // Operation adding |
| 301 | ///////////////////////////////////////////////////////////////////////////////// |
| 302 | |
| 303 | int 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 | |
| 309 | int 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 | */ |
| 322 | void 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 Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 332 | } |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 333 | } |
| 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 | */ |
| 347 | void 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 | */ |
| 360 | void 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 Craik | 7273daa | 2013-03-28 11:25:24 -0700 | [diff] [blame] | 382 | void DeferredDisplayList::addRestoreToCount(OpenGLRenderer& renderer, StateOp* op, |
| 383 | int newSaveCount) { |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 384 | 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 Craik | 1ed30c9 | 2013-04-03 12:37:35 -0700 | [diff] [blame] | 397 | storeRestoreToCountBarrier(renderer, op, mSaveStack.size() + FLUSH_SAVE_STACK_DEPTH); |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | void DeferredDisplayList::addDrawOp(OpenGLRenderer& renderer, DrawOp* op) { |
| 401 | if (renderer.storeDisplayState(op->state, getDrawOpDeferFlags())) { |
| 402 | return; // quick rejected |
| 403 | } |
| 404 | |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 405 | 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 Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 412 | |
| 413 | if (CC_UNLIKELY(renderer.getCaches().drawReorderDisabled)) { |
| 414 | // TODO: elegant way to reuse batches? |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 415 | DrawBatch* b = new DrawBatch(batchId, mergeId); |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 416 | b->add(op); |
| 417 | mBatches.add(b); |
| 418 | return; |
| 419 | } |
| 420 | |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 421 | // find the latest batch of the new op's type, and try to merge the new op into it |
| 422 | DrawBatch* targetBatch = NULL; |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 423 | |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 424 | // 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 Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 427 | 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 Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 430 | DrawBatch* b = new DrawBatch(batchId, mergeId); |
| 431 | b->add(op); |
| 432 | mBatches.add(b); |
| 433 | resetBatchingState(); |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 434 | #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 Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 441 | 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 Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 454 | // iterate back toward target to see if anything drawn since should overlap the new op |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 455 | // 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 Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 467 | if (overBatch->intersects(op->state.mBounds)) { |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 468 | // 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 Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 471 | 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 Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 482 | |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 483 | if (!targetBatch) { |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 484 | 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 Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 492 | } |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 493 | |
| 494 | mBatches.insertAt(targetBatch, insertBatchIndex); |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 495 | } |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 496 | |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 497 | targetBatch->add(op); |
| 498 | } |
| 499 | |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 500 | void 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 Craik | 7273daa | 2013-03-28 11:25:24 -0700 | [diff] [blame] | 508 | void DeferredDisplayList::storeRestoreToCountBarrier(OpenGLRenderer& renderer, StateOp* op, |
| 509 | int newSaveCount) { |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 510 | DEFER_LOGD("%p adding restore to count %d barrier, pos %d", |
| 511 | this, newSaveCount, mBatches.size()); |
| 512 | |
Chris Craik | 7273daa | 2013-03-28 11:25:24 -0700 | [diff] [blame] | 513 | // 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 Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 517 | resetBatchingState(); |
| 518 | } |
| 519 | |
| 520 | ///////////////////////////////////////////////////////////////////////////////// |
| 521 | // Replay / flush |
| 522 | ///////////////////////////////////////////////////////////////////////////////// |
| 523 | |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 524 | static status_t replayBatchList(const Vector<Batch*>& batchList, |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 525 | OpenGLRenderer& renderer, Rect& dirty) { |
| 526 | status_t status = DrawGlInfo::kStatusDone; |
| 527 | |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 528 | for (unsigned int i = 0; i < batchList.size(); i++) { |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 529 | status |= batchList[i]->replay(renderer, dirty, i); |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 530 | } |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 531 | DEFER_LOGD("--flushed, drew %d batches", batchList.size()); |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 532 | return status; |
| 533 | } |
| 534 | |
| 535 | status_t DeferredDisplayList::flush(OpenGLRenderer& renderer, Rect& dirty) { |
| 536 | ATRACE_NAME("flush drawing commands"); |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 537 | Caches::getInstance().fontRenderer->endPrecaching(); |
| 538 | |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 539 | status_t status = DrawGlInfo::kStatusDone; |
| 540 | |
| 541 | if (isEmpty()) return status; // nothing to flush |
Chris Craik | a4e16c5 | 2013-03-22 10:00:48 -0700 | [diff] [blame] | 542 | renderer.restoreToCount(1); |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 543 | |
| 544 | DEFER_LOGD("--flushing"); |
Romain Guy | 0f66753 | 2013-03-01 14:31:04 -0800 | [diff] [blame] | 545 | renderer.eventMark("Flush"); |
| 546 | |
Chris Craik | a4e16c5 | 2013-03-22 10:00:48 -0700 | [diff] [blame] | 547 | // save and restore (with draw modifiers) so that reordering doesn't affect final state |
Chris Craik | d90144d | 2013-03-19 15:03:48 -0700 | [diff] [blame] | 548 | DrawModifiers restoreDrawModifiers = renderer.getDrawModifiers(); |
Chris Craik | a4e16c5 | 2013-03-22 10:00:48 -0700 | [diff] [blame] | 549 | renderer.save(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag); |
| 550 | |
Chris Craik | 1ed30c9 | 2013-04-03 12:37:35 -0700 | [diff] [blame] | 551 | // 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 Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 553 | status |= replayBatchList(mBatches, renderer, dirty); |
Chris Craik | a4e16c5 | 2013-03-22 10:00:48 -0700 | [diff] [blame] | 554 | |
| 555 | renderer.restoreToCount(1); |
Chris Craik | d90144d | 2013-03-19 15:03:48 -0700 | [diff] [blame] | 556 | renderer.setDrawModifiers(restoreDrawModifiers); |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 557 | |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 558 | DEFER_LOGD("--flush complete, returning %x", status); |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 559 | clear(); |
| 560 | return status; |
| 561 | } |
| 562 | |
| 563 | }; // namespace uirenderer |
| 564 | }; // namespace android |