chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2012 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | |
chudy@google.com | 607357f | 2012-08-07 16:12:23 +0000 | [diff] [blame] | 9 | #include "SkDebugger.h" |
robertphillips@google.com | 770963f | 2014-04-18 18:04:41 +0000 | [diff] [blame] | 10 | #include "SkPictureRecorder.h" |
borenet@google.com | 2d9dbd4 | 2013-03-12 13:07:40 +0000 | [diff] [blame] | 11 | #include "SkString.h" |
| 12 | |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 13 | |
chudy@google.com | 607357f | 2012-08-07 16:12:23 +0000 | [diff] [blame] | 14 | SkDebugger::SkDebugger() { |
| 15 | // Create this some other dynamic way? |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 16 | fDebugCanvas = new SkDebugCanvas(0, 0); |
chudy@google.com | 607357f | 2012-08-07 16:12:23 +0000 | [diff] [blame] | 17 | fPicture = NULL; |
chudy@google.com | 607357f | 2012-08-07 16:12:23 +0000 | [diff] [blame] | 18 | fIndex = 0; |
| 19 | } |
| 20 | |
| 21 | SkDebugger::~SkDebugger() { |
| 22 | // Need to inherit from SkRef object in order for following to work |
| 23 | SkSafeUnref(fDebugCanvas); |
| 24 | SkSafeUnref(fPicture); |
| 25 | } |
| 26 | |
| 27 | void SkDebugger::loadPicture(SkPicture* picture) { |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 28 | SkRefCnt_SafeAssign(fPicture, picture); |
| 29 | |
chudy@google.com | 607357f | 2012-08-07 16:12:23 +0000 | [diff] [blame] | 30 | delete fDebugCanvas; |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 31 | fDebugCanvas = new SkDebugCanvas(SkScalarCeilToInt(this->pictureCull().width()), |
| 32 | SkScalarCeilToInt(this->pictureCull().height())); |
commit-bot@chromium.org | 57f74e0 | 2014-03-25 23:31:33 +0000 | [diff] [blame] | 33 | fDebugCanvas->setPicture(picture); |
robertphillips | c5ba71d | 2014-09-04 08:42:50 -0700 | [diff] [blame] | 34 | picture->playback(fDebugCanvas); |
commit-bot@chromium.org | 57f74e0 | 2014-03-25 23:31:33 +0000 | [diff] [blame] | 35 | fDebugCanvas->setPicture(NULL); |
chudy@google.com | 607357f | 2012-08-07 16:12:23 +0000 | [diff] [blame] | 36 | fIndex = fDebugCanvas->getSize() - 1; |
chudy@google.com | 607357f | 2012-08-07 16:12:23 +0000 | [diff] [blame] | 37 | } |
| 38 | |
robertphillips@google.com | 25bc2f8 | 2013-01-22 18:03:56 +0000 | [diff] [blame] | 39 | SkPicture* SkDebugger::copyPicture() { |
| 40 | // We can't just call clone here since we want to removed the "deleted" |
| 41 | // commands. Playing back will strip those out. |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 42 | SkPictureRecorder recorder; |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 43 | SkCanvas* canvas = recorder.beginRecording(this->pictureCull().width(), |
| 44 | this->pictureCull().height()); |
commit-bot@chromium.org | 57f74e0 | 2014-03-25 23:31:33 +0000 | [diff] [blame] | 45 | |
| 46 | bool vizMode = fDebugCanvas->getMegaVizMode(); |
| 47 | fDebugCanvas->setMegaVizMode(false); |
| 48 | bool overDraw = fDebugCanvas->getOverdrawViz(); |
| 49 | fDebugCanvas->setOverdrawViz(false); |
commit-bot@chromium.org | 2a67e12 | 2014-05-19 13:53:10 +0000 | [diff] [blame] | 50 | bool pathOps = fDebugCanvas->getAllowSimplifyClip(); |
| 51 | fDebugCanvas->setAllowSimplifyClip(false); |
commit-bot@chromium.org | 57f74e0 | 2014-03-25 23:31:33 +0000 | [diff] [blame] | 52 | int saveCount = fDebugCanvas->getOutstandingSaveCount(); |
| 53 | fDebugCanvas->setOutstandingSaveCount(0); |
| 54 | |
chudy@google.com | 607357f | 2012-08-07 16:12:23 +0000 | [diff] [blame] | 55 | fDebugCanvas->draw(canvas); |
commit-bot@chromium.org | 57f74e0 | 2014-03-25 23:31:33 +0000 | [diff] [blame] | 56 | |
| 57 | int temp = fDebugCanvas->getOutstandingSaveCount(); |
| 58 | for (int i = 0; i < temp; ++i) { |
| 59 | canvas->restore(); |
| 60 | } |
| 61 | |
| 62 | fDebugCanvas->setMegaVizMode(vizMode); |
| 63 | fDebugCanvas->setOverdrawViz(overDraw); |
| 64 | fDebugCanvas->setOutstandingSaveCount(saveCount); |
commit-bot@chromium.org | 2a67e12 | 2014-05-19 13:53:10 +0000 | [diff] [blame] | 65 | fDebugCanvas->setAllowSimplifyClip(pathOps); |
commit-bot@chromium.org | 57f74e0 | 2014-03-25 23:31:33 +0000 | [diff] [blame] | 66 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 67 | return recorder.endRecording(); |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 68 | } |
borenet@google.com | 2d9dbd4 | 2013-03-12 13:07:40 +0000 | [diff] [blame] | 69 | |
| 70 | void SkDebugger::getOverviewText(const SkTDArray<double>* typeTimes, |
| 71 | double totTime, |
robertphillips@google.com | e428f9b | 2013-03-12 15:33:40 +0000 | [diff] [blame] | 72 | SkString* overview, |
| 73 | int numRuns) { |
borenet@google.com | 2d9dbd4 | 2013-03-12 13:07:40 +0000 | [diff] [blame] | 74 | const SkTDArray<SkDrawCommand*>& commands = this->getDrawCommands(); |
| 75 | |
| 76 | SkTDArray<int> counts; |
| 77 | counts.setCount(LAST_DRAWTYPE_ENUM+1); |
| 78 | for (int i = 0; i < LAST_DRAWTYPE_ENUM+1; ++i) { |
| 79 | counts[i] = 0; |
| 80 | } |
| 81 | |
| 82 | for (int i = 0; i < commands.count(); i++) { |
| 83 | counts[commands[i]->getType()]++; |
| 84 | } |
| 85 | |
| 86 | overview->reset(); |
| 87 | int total = 0; |
| 88 | #ifdef SK_DEBUG |
| 89 | double totPercent = 0, tempSum = 0; |
| 90 | #endif |
| 91 | for (int i = 0; i < LAST_DRAWTYPE_ENUM+1; ++i) { |
| 92 | if (0 == counts[i]) { |
| 93 | // if there were no commands of this type then they should've consumed no time |
| 94 | SkASSERT(NULL == typeTimes || 0.0 == (*typeTimes)[i]); |
| 95 | continue; |
| 96 | } |
| 97 | |
| 98 | overview->append(SkDrawCommand::GetCommandString((DrawType) i)); |
| 99 | overview->append(": "); |
robertphillips@google.com | e428f9b | 2013-03-12 15:33:40 +0000 | [diff] [blame] | 100 | overview->appendS32(counts[i]); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 101 | if (typeTimes && totTime >= 0.0) { |
borenet@google.com | 2d9dbd4 | 2013-03-12 13:07:40 +0000 | [diff] [blame] | 102 | overview->append(" - "); |
robertphillips@google.com | e428f9b | 2013-03-12 15:33:40 +0000 | [diff] [blame] | 103 | overview->appendf("%.2f", (*typeTimes)[i]/(float)numRuns); |
borenet@google.com | 2d9dbd4 | 2013-03-12 13:07:40 +0000 | [diff] [blame] | 104 | overview->append("ms"); |
| 105 | overview->append(" - "); |
| 106 | double percent = 100.0*(*typeTimes)[i]/totTime; |
robertphillips@google.com | e428f9b | 2013-03-12 15:33:40 +0000 | [diff] [blame] | 107 | overview->appendf("%.2f", percent); |
borenet@google.com | 2d9dbd4 | 2013-03-12 13:07:40 +0000 | [diff] [blame] | 108 | overview->append("%"); |
| 109 | #ifdef SK_DEBUG |
| 110 | totPercent += percent; |
| 111 | tempSum += (*typeTimes)[i]; |
| 112 | #endif |
| 113 | } |
| 114 | overview->append("<br/>"); |
| 115 | total += counts[i]; |
| 116 | } |
| 117 | #ifdef SK_DEBUG |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 118 | if (typeTimes) { |
skia.committer@gmail.com | 91274b9 | 2013-03-13 07:01:04 +0000 | [diff] [blame] | 119 | SkASSERT(SkScalarNearlyEqual(SkDoubleToScalar(totPercent), |
robertphillips@google.com | e428f9b | 2013-03-12 15:33:40 +0000 | [diff] [blame] | 120 | SkDoubleToScalar(100.0))); |
skia.committer@gmail.com | 91274b9 | 2013-03-13 07:01:04 +0000 | [diff] [blame] | 121 | SkASSERT(SkScalarNearlyEqual(SkDoubleToScalar(tempSum), |
robertphillips@google.com | e428f9b | 2013-03-12 15:33:40 +0000 | [diff] [blame] | 122 | SkDoubleToScalar(totTime))); |
borenet@google.com | 2d9dbd4 | 2013-03-12 13:07:40 +0000 | [diff] [blame] | 123 | } |
| 124 | #endif |
| 125 | |
| 126 | if (totTime > 0.0) { |
| 127 | overview->append("Total Time: "); |
robertphillips@google.com | e428f9b | 2013-03-12 15:33:40 +0000 | [diff] [blame] | 128 | overview->appendf("%.2f", totTime/(float)numRuns); |
borenet@google.com | 2d9dbd4 | 2013-03-12 13:07:40 +0000 | [diff] [blame] | 129 | overview->append("ms"); |
| 130 | #ifdef SK_DEBUG |
| 131 | overview->append(" "); |
robertphillips@google.com | e428f9b | 2013-03-12 15:33:40 +0000 | [diff] [blame] | 132 | overview->appendScalar(SkDoubleToScalar(totPercent)); |
borenet@google.com | 2d9dbd4 | 2013-03-12 13:07:40 +0000 | [diff] [blame] | 133 | overview->append("% "); |
| 134 | #endif |
| 135 | overview->append("<br/>"); |
| 136 | } |
| 137 | |
| 138 | SkString totalStr; |
| 139 | totalStr.append("Total Draw Commands: "); |
robertphillips@google.com | e428f9b | 2013-03-12 15:33:40 +0000 | [diff] [blame] | 140 | totalStr.appendScalar(SkDoubleToScalar(total)); |
borenet@google.com | 2d9dbd4 | 2013-03-12 13:07:40 +0000 | [diff] [blame] | 141 | totalStr.append("<br/>"); |
| 142 | overview->insert(0, totalStr); |
| 143 | |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 144 | overview->append("<br/>SkPicture L: "); |
| 145 | overview->appendScalar(this->pictureCull().fLeft); |
| 146 | overview->append(" T: "); |
| 147 | overview->appendScalar(this->pictureCull().fTop); |
| 148 | overview->append(" R: "); |
| 149 | overview->appendScalar(this->pictureCull().fRight); |
| 150 | overview->append(" B: "); |
| 151 | overview->appendScalar(this->pictureCull().fBottom); |
borenet@google.com | 2d9dbd4 | 2013-03-12 13:07:40 +0000 | [diff] [blame] | 152 | overview->append("<br/>"); |
borenet@google.com | 2d9dbd4 | 2013-03-12 13:07:40 +0000 | [diff] [blame] | 153 | } |
commit-bot@chromium.org | 2a67e12 | 2014-05-19 13:53:10 +0000 | [diff] [blame] | 154 | |
| 155 | void SkDebugger::getClipStackText(SkString* clipStack) { |
| 156 | clipStack->set(fDebugCanvas->clipStackData()); |
| 157 | } |