blob: 6639d2c829e750af8387c21e1534c384dff561d9 [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"
robertphillips@google.com770963f2014-04-18 18:04:41 +000010#include "SkPictureRecorder.h"
borenet@google.com2d9dbd42013-03-12 13:07:40 +000011#include "SkString.h"
12
chudy@google.com902ebe52012-06-29 14:21:22 +000013
kkinnunen5a2315e2015-01-05 11:51:13 -080014SkDebugger::SkDebugger()
halcanary96fcdcc2015-08-27 07:41:13 -070015 : fPicture(nullptr)
kkinnunen5a2315e2015-01-05 11:51:13 -080016 , fIndex(-1) {
chudy@google.com607357f2012-08-07 16:12:23 +000017 // Create this some other dynamic way?
robertphillipsa8d7f0b2014-08-29 08:03:56 -070018 fDebugCanvas = new SkDebugCanvas(0, 0);
chudy@google.com607357f2012-08-07 16:12:23 +000019}
20
21SkDebugger::~SkDebugger() {
22 // Need to inherit from SkRef object in order for following to work
23 SkSafeUnref(fDebugCanvas);
24 SkSafeUnref(fPicture);
25}
26
27void SkDebugger::loadPicture(SkPicture* picture) {
robertphillipsa8d7f0b2014-08-29 08:03:56 -070028 SkRefCnt_SafeAssign(fPicture, picture);
29
chudy@google.com607357f2012-08-07 16:12:23 +000030 delete fDebugCanvas;
robertphillipsa8d7f0b2014-08-29 08:03:56 -070031 fDebugCanvas = new SkDebugCanvas(SkScalarCeilToInt(this->pictureCull().width()),
32 SkScalarCeilToInt(this->pictureCull().height()));
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +000033 fDebugCanvas->setPicture(picture);
robertphillipsc5ba71d2014-09-04 08:42:50 -070034 picture->playback(fDebugCanvas);
halcanary96fcdcc2015-08-27 07:41:13 -070035 fDebugCanvas->setPicture(nullptr);
chudy@google.com607357f2012-08-07 16:12:23 +000036 fIndex = fDebugCanvas->getSize() - 1;
chudy@google.com607357f2012-08-07 16:12:23 +000037}
38
robertphillips587ea712016-03-25 07:04:35 -070039sk_sp<SkPicture> SkDebugger::copyPicture() {
robertphillips@google.com25bc2f82013-01-22 18:03:56 +000040 // We can't just call clone here since we want to removed the "deleted"
41 // commands. Playing back will strip those out.
robertphillips@google.com84b18c72014-04-13 19:09:42 +000042 SkPictureRecorder recorder;
robertphillipsa8d7f0b2014-08-29 08:03:56 -070043 SkCanvas* canvas = recorder.beginRecording(this->pictureCull().width(),
44 this->pictureCull().height());
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +000045
46 bool vizMode = fDebugCanvas->getMegaVizMode();
47 fDebugCanvas->setMegaVizMode(false);
48 bool overDraw = fDebugCanvas->getOverdrawViz();
49 fDebugCanvas->setOverdrawViz(false);
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +000050 bool pathOps = fDebugCanvas->getAllowSimplifyClip();
51 fDebugCanvas->setAllowSimplifyClip(false);
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +000052
chudy@google.com607357f2012-08-07 16:12:23 +000053 fDebugCanvas->draw(canvas);
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +000054
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +000055 fDebugCanvas->setMegaVizMode(vizMode);
56 fDebugCanvas->setOverdrawViz(overDraw);
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +000057 fDebugCanvas->setAllowSimplifyClip(pathOps);
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +000058
robertphillips587ea712016-03-25 07:04:35 -070059 return recorder.finishRecordingAsPicture();
chudy@google.com902ebe52012-06-29 14:21:22 +000060}
borenet@google.com2d9dbd42013-03-12 13:07:40 +000061
62void SkDebugger::getOverviewText(const SkTDArray<double>* typeTimes,
63 double totTime,
robertphillips@google.come428f9b2013-03-12 15:33:40 +000064 SkString* overview,
65 int numRuns) {
borenet@google.com2d9dbd42013-03-12 13:07:40 +000066 const SkTDArray<SkDrawCommand*>& commands = this->getDrawCommands();
67
68 SkTDArray<int> counts;
robertphillips9bafc302015-02-13 11:13:00 -080069 counts.setCount(SkDrawCommand::kOpTypeCount);
70 for (int i = 0; i < SkDrawCommand::kOpTypeCount; ++i) {
borenet@google.com2d9dbd42013-03-12 13:07:40 +000071 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
robertphillips9bafc302015-02-13 11:13:00 -080083 for (int i = 0; i < SkDrawCommand::kOpTypeCount; ++i) {
borenet@google.com2d9dbd42013-03-12 13:07:40 +000084 if (0 == counts[i]) {
85 // if there were no commands of this type then they should've consumed no time
halcanary96fcdcc2015-08-27 07:41:13 -070086 SkASSERT(nullptr == typeTimes || 0.0 == (*typeTimes)[i]);
borenet@google.com2d9dbd42013-03-12 13:07:40 +000087 continue;
88 }
89
robertphillips9bafc302015-02-13 11:13:00 -080090 overview->append(SkDrawCommand::GetCommandString((SkDrawCommand::OpType) i));
borenet@google.com2d9dbd42013-03-12 13:07:40 +000091 overview->append(": ");
robertphillips@google.come428f9b2013-03-12 15:33:40 +000092 overview->appendS32(counts[i]);
bsalomon49f085d2014-09-05 13:34:00 -070093 if (typeTimes && totTime >= 0.0) {
borenet@google.com2d9dbd42013-03-12 13:07:40 +000094 overview->append(" - ");
robertphillips@google.come428f9b2013-03-12 15:33:40 +000095 overview->appendf("%.2f", (*typeTimes)[i]/(float)numRuns);
borenet@google.com2d9dbd42013-03-12 13:07:40 +000096 overview->append("ms");
97 overview->append(" - ");
98 double percent = 100.0*(*typeTimes)[i]/totTime;
robertphillips@google.come428f9b2013-03-12 15:33:40 +000099 overview->appendf("%.2f", percent);
borenet@google.com2d9dbd42013-03-12 13:07:40 +0000100 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
bsalomon49f085d2014-09-05 13:34:00 -0700110 if (typeTimes) {
skia.committer@gmail.com91274b92013-03-13 07:01:04 +0000111 SkASSERT(SkScalarNearlyEqual(SkDoubleToScalar(totPercent),
robertphillips@google.come428f9b2013-03-12 15:33:40 +0000112 SkDoubleToScalar(100.0)));
skia.committer@gmail.com91274b92013-03-13 07:01:04 +0000113 SkASSERT(SkScalarNearlyEqual(SkDoubleToScalar(tempSum),
robertphillips@google.come428f9b2013-03-12 15:33:40 +0000114 SkDoubleToScalar(totTime)));
borenet@google.com2d9dbd42013-03-12 13:07:40 +0000115 }
116#endif
117
118 if (totTime > 0.0) {
119 overview->append("Total Time: ");
robertphillips@google.come428f9b2013-03-12 15:33:40 +0000120 overview->appendf("%.2f", totTime/(float)numRuns);
borenet@google.com2d9dbd42013-03-12 13:07:40 +0000121 overview->append("ms");
122#ifdef SK_DEBUG
123 overview->append(" ");
robertphillips@google.come428f9b2013-03-12 15:33:40 +0000124 overview->appendScalar(SkDoubleToScalar(totPercent));
borenet@google.com2d9dbd42013-03-12 13:07:40 +0000125 overview->append("% ");
126#endif
127 overview->append("<br/>");
128 }
129
130 SkString totalStr;
131 totalStr.append("Total Draw Commands: ");
robertphillips@google.come428f9b2013-03-12 15:33:40 +0000132 totalStr.appendScalar(SkDoubleToScalar(total));
borenet@google.com2d9dbd42013-03-12 13:07:40 +0000133 totalStr.append("<br/>");
134 overview->insert(0, totalStr);
135
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700136 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.com2d9dbd42013-03-12 13:07:40 +0000144 overview->append("<br/>");
borenet@google.com2d9dbd42013-03-12 13:07:40 +0000145}
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +0000146
147void SkDebugger::getClipStackText(SkString* clipStack) {
148 clipStack->set(fDebugCanvas->clipStackData());
149}