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 | |
kkinnunen | 5a2315e | 2015-01-05 11:51:13 -0800 | [diff] [blame] | 14 | SkDebugger::SkDebugger() |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 15 | : fPicture(nullptr) |
kkinnunen | 5a2315e | 2015-01-05 11:51:13 -0800 | [diff] [blame] | 16 | , fIndex(-1) { |
chudy@google.com | 607357f | 2012-08-07 16:12:23 +0000 | [diff] [blame] | 17 | // Create this some other dynamic way? |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 18 | fDebugCanvas = new SkDebugCanvas(0, 0); |
chudy@google.com | 607357f | 2012-08-07 16:12:23 +0000 | [diff] [blame] | 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); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 35 | fDebugCanvas->setPicture(nullptr); |
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 | 587ea71 | 2016-03-25 07:04:35 -0700 | [diff] [blame] | 39 | sk_sp<SkPicture> SkDebugger::copyPicture() { |
robertphillips@google.com | 25bc2f8 | 2013-01-22 18:03:56 +0000 | [diff] [blame] | 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 | |
chudy@google.com | 607357f | 2012-08-07 16:12:23 +0000 | [diff] [blame] | 53 | fDebugCanvas->draw(canvas); |
commit-bot@chromium.org | 57f74e0 | 2014-03-25 23:31:33 +0000 | [diff] [blame] | 54 | |
commit-bot@chromium.org | 57f74e0 | 2014-03-25 23:31:33 +0000 | [diff] [blame] | 55 | fDebugCanvas->setMegaVizMode(vizMode); |
| 56 | fDebugCanvas->setOverdrawViz(overDraw); |
commit-bot@chromium.org | 2a67e12 | 2014-05-19 13:53:10 +0000 | [diff] [blame] | 57 | fDebugCanvas->setAllowSimplifyClip(pathOps); |
commit-bot@chromium.org | 57f74e0 | 2014-03-25 23:31:33 +0000 | [diff] [blame] | 58 | |
robertphillips | 587ea71 | 2016-03-25 07:04:35 -0700 | [diff] [blame] | 59 | return recorder.finishRecordingAsPicture(); |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 60 | } |
borenet@google.com | 2d9dbd4 | 2013-03-12 13:07:40 +0000 | [diff] [blame] | 61 | |
| 62 | void SkDebugger::getOverviewText(const SkTDArray<double>* typeTimes, |
| 63 | double totTime, |
robertphillips@google.com | e428f9b | 2013-03-12 15:33:40 +0000 | [diff] [blame] | 64 | SkString* overview, |
| 65 | int numRuns) { |
borenet@google.com | 2d9dbd4 | 2013-03-12 13:07:40 +0000 | [diff] [blame] | 66 | const SkTDArray<SkDrawCommand*>& commands = this->getDrawCommands(); |
| 67 | |
| 68 | SkTDArray<int> counts; |
robertphillips | 9bafc30 | 2015-02-13 11:13:00 -0800 | [diff] [blame] | 69 | counts.setCount(SkDrawCommand::kOpTypeCount); |
| 70 | for (int i = 0; i < SkDrawCommand::kOpTypeCount; ++i) { |
borenet@google.com | 2d9dbd4 | 2013-03-12 13:07:40 +0000 | [diff] [blame] | 71 | counts[i] = 0; |
| 72 | } |
| 73 | |
| 74 | for (int i = 0; i < commands.count(); i++) { |
| 75 | counts[commands[i]->getType()]++; |
| 76 | } |
| 77 | |
| 78 | overview->reset(); |
| 79 | int total = 0; |
| 80 | #ifdef SK_DEBUG |
| 81 | double totPercent = 0, tempSum = 0; |
| 82 | #endif |
robertphillips | 9bafc30 | 2015-02-13 11:13:00 -0800 | [diff] [blame] | 83 | for (int i = 0; i < SkDrawCommand::kOpTypeCount; ++i) { |
borenet@google.com | 2d9dbd4 | 2013-03-12 13:07:40 +0000 | [diff] [blame] | 84 | if (0 == counts[i]) { |
| 85 | // if there were no commands of this type then they should've consumed no time |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 86 | SkASSERT(nullptr == typeTimes || 0.0 == (*typeTimes)[i]); |
borenet@google.com | 2d9dbd4 | 2013-03-12 13:07:40 +0000 | [diff] [blame] | 87 | continue; |
| 88 | } |
| 89 | |
robertphillips | 9bafc30 | 2015-02-13 11:13:00 -0800 | [diff] [blame] | 90 | overview->append(SkDrawCommand::GetCommandString((SkDrawCommand::OpType) i)); |
borenet@google.com | 2d9dbd4 | 2013-03-12 13:07:40 +0000 | [diff] [blame] | 91 | overview->append(": "); |
robertphillips@google.com | e428f9b | 2013-03-12 15:33:40 +0000 | [diff] [blame] | 92 | overview->appendS32(counts[i]); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 93 | if (typeTimes && totTime >= 0.0) { |
borenet@google.com | 2d9dbd4 | 2013-03-12 13:07:40 +0000 | [diff] [blame] | 94 | overview->append(" - "); |
robertphillips@google.com | e428f9b | 2013-03-12 15:33:40 +0000 | [diff] [blame] | 95 | overview->appendf("%.2f", (*typeTimes)[i]/(float)numRuns); |
borenet@google.com | 2d9dbd4 | 2013-03-12 13:07:40 +0000 | [diff] [blame] | 96 | overview->append("ms"); |
| 97 | overview->append(" - "); |
| 98 | double percent = 100.0*(*typeTimes)[i]/totTime; |
robertphillips@google.com | e428f9b | 2013-03-12 15:33:40 +0000 | [diff] [blame] | 99 | overview->appendf("%.2f", percent); |
borenet@google.com | 2d9dbd4 | 2013-03-12 13:07:40 +0000 | [diff] [blame] | 100 | overview->append("%"); |
| 101 | #ifdef SK_DEBUG |
| 102 | totPercent += percent; |
| 103 | tempSum += (*typeTimes)[i]; |
| 104 | #endif |
| 105 | } |
| 106 | overview->append("<br/>"); |
| 107 | total += counts[i]; |
| 108 | } |
| 109 | #ifdef SK_DEBUG |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 110 | if (typeTimes) { |
skia.committer@gmail.com | 91274b9 | 2013-03-13 07:01:04 +0000 | [diff] [blame] | 111 | SkASSERT(SkScalarNearlyEqual(SkDoubleToScalar(totPercent), |
robertphillips@google.com | e428f9b | 2013-03-12 15:33:40 +0000 | [diff] [blame] | 112 | SkDoubleToScalar(100.0))); |
skia.committer@gmail.com | 91274b9 | 2013-03-13 07:01:04 +0000 | [diff] [blame] | 113 | SkASSERT(SkScalarNearlyEqual(SkDoubleToScalar(tempSum), |
robertphillips@google.com | e428f9b | 2013-03-12 15:33:40 +0000 | [diff] [blame] | 114 | SkDoubleToScalar(totTime))); |
borenet@google.com | 2d9dbd4 | 2013-03-12 13:07:40 +0000 | [diff] [blame] | 115 | } |
| 116 | #endif |
| 117 | |
| 118 | if (totTime > 0.0) { |
| 119 | overview->append("Total Time: "); |
robertphillips@google.com | e428f9b | 2013-03-12 15:33:40 +0000 | [diff] [blame] | 120 | overview->appendf("%.2f", totTime/(float)numRuns); |
borenet@google.com | 2d9dbd4 | 2013-03-12 13:07:40 +0000 | [diff] [blame] | 121 | overview->append("ms"); |
| 122 | #ifdef SK_DEBUG |
| 123 | overview->append(" "); |
robertphillips@google.com | e428f9b | 2013-03-12 15:33:40 +0000 | [diff] [blame] | 124 | overview->appendScalar(SkDoubleToScalar(totPercent)); |
borenet@google.com | 2d9dbd4 | 2013-03-12 13:07:40 +0000 | [diff] [blame] | 125 | overview->append("% "); |
| 126 | #endif |
| 127 | overview->append("<br/>"); |
| 128 | } |
| 129 | |
| 130 | SkString totalStr; |
| 131 | totalStr.append("Total Draw Commands: "); |
robertphillips@google.com | e428f9b | 2013-03-12 15:33:40 +0000 | [diff] [blame] | 132 | totalStr.appendScalar(SkDoubleToScalar(total)); |
borenet@google.com | 2d9dbd4 | 2013-03-12 13:07:40 +0000 | [diff] [blame] | 133 | totalStr.append("<br/>"); |
| 134 | overview->insert(0, totalStr); |
| 135 | |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 136 | overview->append("<br/>SkPicture L: "); |
| 137 | overview->appendScalar(this->pictureCull().fLeft); |
| 138 | overview->append(" T: "); |
| 139 | overview->appendScalar(this->pictureCull().fTop); |
| 140 | overview->append(" R: "); |
| 141 | overview->appendScalar(this->pictureCull().fRight); |
| 142 | overview->append(" B: "); |
| 143 | overview->appendScalar(this->pictureCull().fBottom); |
borenet@google.com | 2d9dbd4 | 2013-03-12 13:07:40 +0000 | [diff] [blame] | 144 | overview->append("<br/>"); |
borenet@google.com | 2d9dbd4 | 2013-03-12 13:07:40 +0000 | [diff] [blame] | 145 | } |
commit-bot@chromium.org | 2a67e12 | 2014-05-19 13:53:10 +0000 | [diff] [blame] | 146 | |
| 147 | void SkDebugger::getClipStackText(SkString* clipStack) { |
| 148 | clipStack->set(fDebugCanvas->clipStackData()); |
| 149 | } |