blob: 197a27ff8aa9958fb6fd1340f01f123e25649b16 [file] [log] [blame]
chudy@google.com902ebe52012-06-29 14:21:22 +00001
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.com607357f2012-08-07 16:12:23 +00009#include "SkDebugger.h"
Florin Malitaf5305862016-11-15 10:03:32 -050010#include "SkMakeUnique.h"
robertphillips@google.com770963f2014-04-18 18:04:41 +000011#include "SkPictureRecorder.h"
borenet@google.com2d9dbd42013-03-12 13:07:40 +000012#include "SkString.h"
13
chudy@google.com902ebe52012-06-29 14:21:22 +000014
kkinnunen5a2315e2015-01-05 11:51:13 -080015SkDebugger::SkDebugger()
Florin Malitaf5305862016-11-15 10:03:32 -050016 : fDebugCanvas(skstd::make_unique<SkDebugCanvas>(0, 0))
17 , fIndex(-1) { }
chudy@google.com607357f2012-08-07 16:12:23 +000018
Florin Malitaf5305862016-11-15 10:03:32 -050019SkDebugger::~SkDebugger() {}
chudy@google.com607357f2012-08-07 16:12:23 +000020
21void SkDebugger::loadPicture(SkPicture* picture) {
Florin Malitaf5305862016-11-15 10:03:32 -050022 fPicture = sk_ref_sp(picture);
23 fDebugCanvas = skstd::make_unique<SkDebugCanvas>(
24 SkScalarCeilToInt(this->pictureCull().width()),
25 SkScalarCeilToInt(this->pictureCull().height()));
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +000026 fDebugCanvas->setPicture(picture);
Florin Malitaf5305862016-11-15 10:03:32 -050027 picture->playback(fDebugCanvas.get());
halcanary96fcdcc2015-08-27 07:41:13 -070028 fDebugCanvas->setPicture(nullptr);
chudy@google.com607357f2012-08-07 16:12:23 +000029 fIndex = fDebugCanvas->getSize() - 1;
chudy@google.com607357f2012-08-07 16:12:23 +000030}
31
robertphillips587ea712016-03-25 07:04:35 -070032sk_sp<SkPicture> SkDebugger::copyPicture() {
robertphillips@google.com25bc2f82013-01-22 18:03:56 +000033 // We can't just call clone here since we want to removed the "deleted"
34 // commands. Playing back will strip those out.
robertphillips@google.com84b18c72014-04-13 19:09:42 +000035 SkPictureRecorder recorder;
robertphillipsa8d7f0b2014-08-29 08:03:56 -070036 SkCanvas* canvas = recorder.beginRecording(this->pictureCull().width(),
37 this->pictureCull().height());
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +000038
39 bool vizMode = fDebugCanvas->getMegaVizMode();
40 fDebugCanvas->setMegaVizMode(false);
41 bool overDraw = fDebugCanvas->getOverdrawViz();
42 fDebugCanvas->setOverdrawViz(false);
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +000043 bool pathOps = fDebugCanvas->getAllowSimplifyClip();
44 fDebugCanvas->setAllowSimplifyClip(false);
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +000045
chudy@google.com607357f2012-08-07 16:12:23 +000046 fDebugCanvas->draw(canvas);
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +000047
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +000048 fDebugCanvas->setMegaVizMode(vizMode);
49 fDebugCanvas->setOverdrawViz(overDraw);
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +000050 fDebugCanvas->setAllowSimplifyClip(pathOps);
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +000051
robertphillips587ea712016-03-25 07:04:35 -070052 return recorder.finishRecordingAsPicture();
chudy@google.com902ebe52012-06-29 14:21:22 +000053}
borenet@google.com2d9dbd42013-03-12 13:07:40 +000054
55void SkDebugger::getOverviewText(const SkTDArray<double>* typeTimes,
56 double totTime,
robertphillips@google.come428f9b2013-03-12 15:33:40 +000057 SkString* overview,
58 int numRuns) {
borenet@google.com2d9dbd42013-03-12 13:07:40 +000059 const SkTDArray<SkDrawCommand*>& commands = this->getDrawCommands();
60
61 SkTDArray<int> counts;
robertphillips9bafc302015-02-13 11:13:00 -080062 counts.setCount(SkDrawCommand::kOpTypeCount);
63 for (int i = 0; i < SkDrawCommand::kOpTypeCount; ++i) {
borenet@google.com2d9dbd42013-03-12 13:07:40 +000064 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
robertphillips9bafc302015-02-13 11:13:00 -080076 for (int i = 0; i < SkDrawCommand::kOpTypeCount; ++i) {
borenet@google.com2d9dbd42013-03-12 13:07:40 +000077 if (0 == counts[i]) {
78 // if there were no commands of this type then they should've consumed no time
halcanary96fcdcc2015-08-27 07:41:13 -070079 SkASSERT(nullptr == typeTimes || 0.0 == (*typeTimes)[i]);
borenet@google.com2d9dbd42013-03-12 13:07:40 +000080 continue;
81 }
82
robertphillips9bafc302015-02-13 11:13:00 -080083 overview->append(SkDrawCommand::GetCommandString((SkDrawCommand::OpType) i));
borenet@google.com2d9dbd42013-03-12 13:07:40 +000084 overview->append(": ");
robertphillips@google.come428f9b2013-03-12 15:33:40 +000085 overview->appendS32(counts[i]);
bsalomon49f085d2014-09-05 13:34:00 -070086 if (typeTimes && totTime >= 0.0) {
borenet@google.com2d9dbd42013-03-12 13:07:40 +000087 overview->append(" - ");
robertphillips@google.come428f9b2013-03-12 15:33:40 +000088 overview->appendf("%.2f", (*typeTimes)[i]/(float)numRuns);
borenet@google.com2d9dbd42013-03-12 13:07:40 +000089 overview->append("ms");
90 overview->append(" - ");
91 double percent = 100.0*(*typeTimes)[i]/totTime;
robertphillips@google.come428f9b2013-03-12 15:33:40 +000092 overview->appendf("%.2f", percent);
borenet@google.com2d9dbd42013-03-12 13:07:40 +000093 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
bsalomon49f085d2014-09-05 13:34:00 -0700103 if (typeTimes) {
skia.committer@gmail.com91274b92013-03-13 07:01:04 +0000104 SkASSERT(SkScalarNearlyEqual(SkDoubleToScalar(totPercent),
robertphillips@google.come428f9b2013-03-12 15:33:40 +0000105 SkDoubleToScalar(100.0)));
skia.committer@gmail.com91274b92013-03-13 07:01:04 +0000106 SkASSERT(SkScalarNearlyEqual(SkDoubleToScalar(tempSum),
robertphillips@google.come428f9b2013-03-12 15:33:40 +0000107 SkDoubleToScalar(totTime)));
borenet@google.com2d9dbd42013-03-12 13:07:40 +0000108 }
109#endif
110
111 if (totTime > 0.0) {
112 overview->append("Total Time: ");
robertphillips@google.come428f9b2013-03-12 15:33:40 +0000113 overview->appendf("%.2f", totTime/(float)numRuns);
borenet@google.com2d9dbd42013-03-12 13:07:40 +0000114 overview->append("ms");
115#ifdef SK_DEBUG
116 overview->append(" ");
robertphillips@google.come428f9b2013-03-12 15:33:40 +0000117 overview->appendScalar(SkDoubleToScalar(totPercent));
borenet@google.com2d9dbd42013-03-12 13:07:40 +0000118 overview->append("% ");
119#endif
120 overview->append("<br/>");
121 }
122
123 SkString totalStr;
124 totalStr.append("Total Draw Commands: ");
robertphillips@google.come428f9b2013-03-12 15:33:40 +0000125 totalStr.appendScalar(SkDoubleToScalar(total));
borenet@google.com2d9dbd42013-03-12 13:07:40 +0000126 totalStr.append("<br/>");
127 overview->insert(0, totalStr);
128
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700129 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.com2d9dbd42013-03-12 13:07:40 +0000137 overview->append("<br/>");
borenet@google.com2d9dbd42013-03-12 13:07:40 +0000138}
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +0000139
140void SkDebugger::getClipStackText(SkString* clipStack) {
141 clipStack->set(fDebugCanvas->clipStackData());
142}