blob: 8833df3e7bd30aed15b5900c61c7c8e04ad88912 [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
chudy@google.com607357f2012-08-07 16:12:23 +000014SkDebugger::SkDebugger() {
15 // Create this some other dynamic way?
robertphillipsa8d7f0b2014-08-29 08:03:56 -070016 fDebugCanvas = new SkDebugCanvas(0, 0);
chudy@google.com607357f2012-08-07 16:12:23 +000017 fPicture = NULL;
chudy@google.com607357f2012-08-07 16:12:23 +000018 fIndex = 0;
19}
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);
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +000035 fDebugCanvas->setPicture(NULL);
chudy@google.com607357f2012-08-07 16:12:23 +000036 fIndex = fDebugCanvas->getSize() - 1;
chudy@google.com607357f2012-08-07 16:12:23 +000037}
38
robertphillips@google.com25bc2f82013-01-22 18:03:56 +000039SkPicture* SkDebugger::copyPicture() {
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.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 int saveCount = fDebugCanvas->getOutstandingSaveCount();
53 fDebugCanvas->setOutstandingSaveCount(0);
54
chudy@google.com607357f2012-08-07 16:12:23 +000055 fDebugCanvas->draw(canvas);
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +000056
57 int temp = fDebugCanvas->getOutstandingSaveCount();
58 for (int i = 0; i < temp; ++i) {
59 canvas->restore();
60 }
61
62 fDebugCanvas->setMegaVizMode(vizMode);
63 fDebugCanvas->setOverdrawViz(overDraw);
64 fDebugCanvas->setOutstandingSaveCount(saveCount);
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +000065 fDebugCanvas->setAllowSimplifyClip(pathOps);
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +000066
robertphillips@google.com84b18c72014-04-13 19:09:42 +000067 return recorder.endRecording();
chudy@google.com902ebe52012-06-29 14:21:22 +000068}
borenet@google.com2d9dbd42013-03-12 13:07:40 +000069
70void SkDebugger::getOverviewText(const SkTDArray<double>* typeTimes,
71 double totTime,
robertphillips@google.come428f9b2013-03-12 15:33:40 +000072 SkString* overview,
73 int numRuns) {
borenet@google.com2d9dbd42013-03-12 13:07:40 +000074 const SkTDArray<SkDrawCommand*>& commands = this->getDrawCommands();
75
76 SkTDArray<int> counts;
77 counts.setCount(LAST_DRAWTYPE_ENUM+1);
78 for (int i = 0; i < LAST_DRAWTYPE_ENUM+1; ++i) {
79 counts[i] = 0;
80 }
81
82 for (int i = 0; i < commands.count(); i++) {
83 counts[commands[i]->getType()]++;
84 }
85
86 overview->reset();
87 int total = 0;
88#ifdef SK_DEBUG
89 double totPercent = 0, tempSum = 0;
90#endif
91 for (int i = 0; i < LAST_DRAWTYPE_ENUM+1; ++i) {
92 if (0 == counts[i]) {
93 // if there were no commands of this type then they should've consumed no time
94 SkASSERT(NULL == typeTimes || 0.0 == (*typeTimes)[i]);
95 continue;
96 }
97
98 overview->append(SkDrawCommand::GetCommandString((DrawType) i));
99 overview->append(": ");
robertphillips@google.come428f9b2013-03-12 15:33:40 +0000100 overview->appendS32(counts[i]);
robertphillips@google.com6d9c92b2013-05-23 13:21:18 +0000101 if (NULL != typeTimes && totTime >= 0.0) {
borenet@google.com2d9dbd42013-03-12 13:07:40 +0000102 overview->append(" - ");
robertphillips@google.come428f9b2013-03-12 15:33:40 +0000103 overview->appendf("%.2f", (*typeTimes)[i]/(float)numRuns);
borenet@google.com2d9dbd42013-03-12 13:07:40 +0000104 overview->append("ms");
105 overview->append(" - ");
106 double percent = 100.0*(*typeTimes)[i]/totTime;
robertphillips@google.come428f9b2013-03-12 15:33:40 +0000107 overview->appendf("%.2f", percent);
borenet@google.com2d9dbd42013-03-12 13:07:40 +0000108 overview->append("%");
109#ifdef SK_DEBUG
110 totPercent += percent;
111 tempSum += (*typeTimes)[i];
112#endif
113 }
114 overview->append("<br/>");
115 total += counts[i];
116 }
117#ifdef SK_DEBUG
118 if (NULL != typeTimes) {
skia.committer@gmail.com91274b92013-03-13 07:01:04 +0000119 SkASSERT(SkScalarNearlyEqual(SkDoubleToScalar(totPercent),
robertphillips@google.come428f9b2013-03-12 15:33:40 +0000120 SkDoubleToScalar(100.0)));
skia.committer@gmail.com91274b92013-03-13 07:01:04 +0000121 SkASSERT(SkScalarNearlyEqual(SkDoubleToScalar(tempSum),
robertphillips@google.come428f9b2013-03-12 15:33:40 +0000122 SkDoubleToScalar(totTime)));
borenet@google.com2d9dbd42013-03-12 13:07:40 +0000123 }
124#endif
125
126 if (totTime > 0.0) {
127 overview->append("Total Time: ");
robertphillips@google.come428f9b2013-03-12 15:33:40 +0000128 overview->appendf("%.2f", totTime/(float)numRuns);
borenet@google.com2d9dbd42013-03-12 13:07:40 +0000129 overview->append("ms");
130#ifdef SK_DEBUG
131 overview->append(" ");
robertphillips@google.come428f9b2013-03-12 15:33:40 +0000132 overview->appendScalar(SkDoubleToScalar(totPercent));
borenet@google.com2d9dbd42013-03-12 13:07:40 +0000133 overview->append("% ");
134#endif
135 overview->append("<br/>");
136 }
137
138 SkString totalStr;
139 totalStr.append("Total Draw Commands: ");
robertphillips@google.come428f9b2013-03-12 15:33:40 +0000140 totalStr.appendScalar(SkDoubleToScalar(total));
borenet@google.com2d9dbd42013-03-12 13:07:40 +0000141 totalStr.append("<br/>");
142 overview->insert(0, totalStr);
143
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700144 overview->append("<br/>SkPicture L: ");
145 overview->appendScalar(this->pictureCull().fLeft);
146 overview->append(" T: ");
147 overview->appendScalar(this->pictureCull().fTop);
148 overview->append(" R: ");
149 overview->appendScalar(this->pictureCull().fRight);
150 overview->append(" B: ");
151 overview->appendScalar(this->pictureCull().fBottom);
borenet@google.com2d9dbd42013-03-12 13:07:40 +0000152 overview->append("<br/>");
borenet@google.com2d9dbd42013-03-12 13:07:40 +0000153}
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +0000154
155void SkDebugger::getClipStackText(SkString* clipStack) {
156 clipStack->set(fDebugCanvas->clipStackData());
157}