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" |
Florin Malita | f530586 | 2016-11-15 10:03:32 -0500 | [diff] [blame] | 10 | #include "SkMakeUnique.h" |
robertphillips@google.com | 770963f | 2014-04-18 18:04:41 +0000 | [diff] [blame] | 11 | #include "SkPictureRecorder.h" |
borenet@google.com | 2d9dbd4 | 2013-03-12 13:07:40 +0000 | [diff] [blame] | 12 | #include "SkString.h" |
| 13 | |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 14 | |
kkinnunen | 5a2315e | 2015-01-05 11:51:13 -0800 | [diff] [blame] | 15 | SkDebugger::SkDebugger() |
Florin Malita | f530586 | 2016-11-15 10:03:32 -0500 | [diff] [blame] | 16 | : fDebugCanvas(skstd::make_unique<SkDebugCanvas>(0, 0)) |
| 17 | , fIndex(-1) { } |
chudy@google.com | 607357f | 2012-08-07 16:12:23 +0000 | [diff] [blame] | 18 | |
Florin Malita | f530586 | 2016-11-15 10:03:32 -0500 | [diff] [blame] | 19 | SkDebugger::~SkDebugger() {} |
chudy@google.com | 607357f | 2012-08-07 16:12:23 +0000 | [diff] [blame] | 20 | |
| 21 | void SkDebugger::loadPicture(SkPicture* picture) { |
Florin Malita | f530586 | 2016-11-15 10:03:32 -0500 | [diff] [blame] | 22 | fPicture = sk_ref_sp(picture); |
| 23 | fDebugCanvas = skstd::make_unique<SkDebugCanvas>( |
| 24 | SkScalarCeilToInt(this->pictureCull().width()), |
| 25 | SkScalarCeilToInt(this->pictureCull().height())); |
commit-bot@chromium.org | 57f74e0 | 2014-03-25 23:31:33 +0000 | [diff] [blame] | 26 | fDebugCanvas->setPicture(picture); |
Florin Malita | f530586 | 2016-11-15 10:03:32 -0500 | [diff] [blame] | 27 | picture->playback(fDebugCanvas.get()); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 28 | fDebugCanvas->setPicture(nullptr); |
chudy@google.com | 607357f | 2012-08-07 16:12:23 +0000 | [diff] [blame] | 29 | fIndex = fDebugCanvas->getSize() - 1; |
chudy@google.com | 607357f | 2012-08-07 16:12:23 +0000 | [diff] [blame] | 30 | } |
| 31 | |
robertphillips | 587ea71 | 2016-03-25 07:04:35 -0700 | [diff] [blame] | 32 | sk_sp<SkPicture> SkDebugger::copyPicture() { |
robertphillips@google.com | 25bc2f8 | 2013-01-22 18:03:56 +0000 | [diff] [blame] | 33 | // We can't just call clone here since we want to removed the "deleted" |
| 34 | // commands. Playing back will strip those out. |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 35 | SkPictureRecorder recorder; |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 36 | SkCanvas* canvas = recorder.beginRecording(this->pictureCull().width(), |
| 37 | this->pictureCull().height()); |
commit-bot@chromium.org | 57f74e0 | 2014-03-25 23:31:33 +0000 | [diff] [blame] | 38 | |
| 39 | bool vizMode = fDebugCanvas->getMegaVizMode(); |
| 40 | fDebugCanvas->setMegaVizMode(false); |
| 41 | bool overDraw = fDebugCanvas->getOverdrawViz(); |
| 42 | fDebugCanvas->setOverdrawViz(false); |
commit-bot@chromium.org | 2a67e12 | 2014-05-19 13:53:10 +0000 | [diff] [blame] | 43 | bool pathOps = fDebugCanvas->getAllowSimplifyClip(); |
| 44 | fDebugCanvas->setAllowSimplifyClip(false); |
commit-bot@chromium.org | 57f74e0 | 2014-03-25 23:31:33 +0000 | [diff] [blame] | 45 | |
chudy@google.com | 607357f | 2012-08-07 16:12:23 +0000 | [diff] [blame] | 46 | fDebugCanvas->draw(canvas); |
commit-bot@chromium.org | 57f74e0 | 2014-03-25 23:31:33 +0000 | [diff] [blame] | 47 | |
commit-bot@chromium.org | 57f74e0 | 2014-03-25 23:31:33 +0000 | [diff] [blame] | 48 | fDebugCanvas->setMegaVizMode(vizMode); |
| 49 | fDebugCanvas->setOverdrawViz(overDraw); |
commit-bot@chromium.org | 2a67e12 | 2014-05-19 13:53:10 +0000 | [diff] [blame] | 50 | fDebugCanvas->setAllowSimplifyClip(pathOps); |
commit-bot@chromium.org | 57f74e0 | 2014-03-25 23:31:33 +0000 | [diff] [blame] | 51 | |
robertphillips | 587ea71 | 2016-03-25 07:04:35 -0700 | [diff] [blame] | 52 | return recorder.finishRecordingAsPicture(); |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 53 | } |
borenet@google.com | 2d9dbd4 | 2013-03-12 13:07:40 +0000 | [diff] [blame] | 54 | |
| 55 | void SkDebugger::getOverviewText(const SkTDArray<double>* typeTimes, |
| 56 | double totTime, |
robertphillips@google.com | e428f9b | 2013-03-12 15:33:40 +0000 | [diff] [blame] | 57 | SkString* overview, |
| 58 | int numRuns) { |
borenet@google.com | 2d9dbd4 | 2013-03-12 13:07:40 +0000 | [diff] [blame] | 59 | const SkTDArray<SkDrawCommand*>& commands = this->getDrawCommands(); |
| 60 | |
| 61 | SkTDArray<int> counts; |
robertphillips | 9bafc30 | 2015-02-13 11:13:00 -0800 | [diff] [blame] | 62 | counts.setCount(SkDrawCommand::kOpTypeCount); |
| 63 | for (int i = 0; i < SkDrawCommand::kOpTypeCount; ++i) { |
borenet@google.com | 2d9dbd4 | 2013-03-12 13:07:40 +0000 | [diff] [blame] | 64 | counts[i] = 0; |
| 65 | } |
| 66 | |
| 67 | for (int i = 0; i < commands.count(); i++) { |
| 68 | counts[commands[i]->getType()]++; |
| 69 | } |
| 70 | |
| 71 | overview->reset(); |
| 72 | int total = 0; |
| 73 | #ifdef SK_DEBUG |
| 74 | double totPercent = 0, tempSum = 0; |
| 75 | #endif |
robertphillips | 9bafc30 | 2015-02-13 11:13:00 -0800 | [diff] [blame] | 76 | for (int i = 0; i < SkDrawCommand::kOpTypeCount; ++i) { |
borenet@google.com | 2d9dbd4 | 2013-03-12 13:07:40 +0000 | [diff] [blame] | 77 | if (0 == counts[i]) { |
| 78 | // 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] | 79 | SkASSERT(nullptr == typeTimes || 0.0 == (*typeTimes)[i]); |
borenet@google.com | 2d9dbd4 | 2013-03-12 13:07:40 +0000 | [diff] [blame] | 80 | continue; |
| 81 | } |
| 82 | |
robertphillips | 9bafc30 | 2015-02-13 11:13:00 -0800 | [diff] [blame] | 83 | overview->append(SkDrawCommand::GetCommandString((SkDrawCommand::OpType) i)); |
borenet@google.com | 2d9dbd4 | 2013-03-12 13:07:40 +0000 | [diff] [blame] | 84 | overview->append(": "); |
robertphillips@google.com | e428f9b | 2013-03-12 15:33:40 +0000 | [diff] [blame] | 85 | overview->appendS32(counts[i]); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 86 | if (typeTimes && totTime >= 0.0) { |
borenet@google.com | 2d9dbd4 | 2013-03-12 13:07:40 +0000 | [diff] [blame] | 87 | overview->append(" - "); |
robertphillips@google.com | e428f9b | 2013-03-12 15:33:40 +0000 | [diff] [blame] | 88 | overview->appendf("%.2f", (*typeTimes)[i]/(float)numRuns); |
borenet@google.com | 2d9dbd4 | 2013-03-12 13:07:40 +0000 | [diff] [blame] | 89 | overview->append("ms"); |
| 90 | overview->append(" - "); |
| 91 | double percent = 100.0*(*typeTimes)[i]/totTime; |
robertphillips@google.com | e428f9b | 2013-03-12 15:33:40 +0000 | [diff] [blame] | 92 | overview->appendf("%.2f", percent); |
borenet@google.com | 2d9dbd4 | 2013-03-12 13:07:40 +0000 | [diff] [blame] | 93 | overview->append("%"); |
| 94 | #ifdef SK_DEBUG |
| 95 | totPercent += percent; |
| 96 | tempSum += (*typeTimes)[i]; |
| 97 | #endif |
| 98 | } |
| 99 | overview->append("<br/>"); |
| 100 | total += counts[i]; |
| 101 | } |
| 102 | #ifdef SK_DEBUG |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 103 | if (typeTimes) { |
skia.committer@gmail.com | 91274b9 | 2013-03-13 07:01:04 +0000 | [diff] [blame] | 104 | SkASSERT(SkScalarNearlyEqual(SkDoubleToScalar(totPercent), |
robertphillips@google.com | e428f9b | 2013-03-12 15:33:40 +0000 | [diff] [blame] | 105 | SkDoubleToScalar(100.0))); |
skia.committer@gmail.com | 91274b9 | 2013-03-13 07:01:04 +0000 | [diff] [blame] | 106 | SkASSERT(SkScalarNearlyEqual(SkDoubleToScalar(tempSum), |
robertphillips@google.com | e428f9b | 2013-03-12 15:33:40 +0000 | [diff] [blame] | 107 | SkDoubleToScalar(totTime))); |
borenet@google.com | 2d9dbd4 | 2013-03-12 13:07:40 +0000 | [diff] [blame] | 108 | } |
| 109 | #endif |
| 110 | |
| 111 | if (totTime > 0.0) { |
| 112 | overview->append("Total Time: "); |
robertphillips@google.com | e428f9b | 2013-03-12 15:33:40 +0000 | [diff] [blame] | 113 | overview->appendf("%.2f", totTime/(float)numRuns); |
borenet@google.com | 2d9dbd4 | 2013-03-12 13:07:40 +0000 | [diff] [blame] | 114 | overview->append("ms"); |
| 115 | #ifdef SK_DEBUG |
| 116 | overview->append(" "); |
robertphillips@google.com | e428f9b | 2013-03-12 15:33:40 +0000 | [diff] [blame] | 117 | overview->appendScalar(SkDoubleToScalar(totPercent)); |
borenet@google.com | 2d9dbd4 | 2013-03-12 13:07:40 +0000 | [diff] [blame] | 118 | overview->append("% "); |
| 119 | #endif |
| 120 | overview->append("<br/>"); |
| 121 | } |
| 122 | |
| 123 | SkString totalStr; |
| 124 | totalStr.append("Total Draw Commands: "); |
robertphillips@google.com | e428f9b | 2013-03-12 15:33:40 +0000 | [diff] [blame] | 125 | totalStr.appendScalar(SkDoubleToScalar(total)); |
borenet@google.com | 2d9dbd4 | 2013-03-12 13:07:40 +0000 | [diff] [blame] | 126 | totalStr.append("<br/>"); |
| 127 | overview->insert(0, totalStr); |
| 128 | |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 129 | overview->append("<br/>SkPicture L: "); |
| 130 | overview->appendScalar(this->pictureCull().fLeft); |
| 131 | overview->append(" T: "); |
| 132 | overview->appendScalar(this->pictureCull().fTop); |
| 133 | overview->append(" R: "); |
| 134 | overview->appendScalar(this->pictureCull().fRight); |
| 135 | overview->append(" B: "); |
| 136 | overview->appendScalar(this->pictureCull().fBottom); |
borenet@google.com | 2d9dbd4 | 2013-03-12 13:07:40 +0000 | [diff] [blame] | 137 | overview->append("<br/>"); |
borenet@google.com | 2d9dbd4 | 2013-03-12 13:07:40 +0000 | [diff] [blame] | 138 | } |
commit-bot@chromium.org | 2a67e12 | 2014-05-19 13:53:10 +0000 | [diff] [blame] | 139 | |
| 140 | void SkDebugger::getClipStackText(SkString* clipStack) { |
| 141 | clipStack->set(fDebugCanvas->clipStackData()); |
| 142 | } |