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