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