blob: 5bdc8a9fb4dc9bd4f4b5c9faee0f14774f5ea948 [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"
borenet@google.com2d9dbd42013-03-12 13:07:40 +000010#include "SkString.h"
11
chudy@google.com902ebe52012-06-29 14:21:22 +000012
chudy@google.com607357f2012-08-07 16:12:23 +000013SkDebugger::SkDebugger() {
14 // Create this some other dynamic way?
15 fDebugCanvas = new SkDebugCanvas(100, 100);
16 fPicture = NULL;
17 fPictureWidth = 0;
18 fPictureHeight = 0;
19 fIndex = 0;
20}
21
22SkDebugger::~SkDebugger() {
23 // Need to inherit from SkRef object in order for following to work
24 SkSafeUnref(fDebugCanvas);
25 SkSafeUnref(fPicture);
26}
27
28void SkDebugger::loadPicture(SkPicture* picture) {
29 fPictureWidth = picture->width();
30 fPictureHeight = picture->height();
31 delete fDebugCanvas;
32 fDebugCanvas = new SkDebugCanvas(fPictureWidth, fPictureHeight);
33 fDebugCanvas->setBounds(fPictureWidth, fPictureHeight);
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +000034 fDebugCanvas->setPicture(picture);
chudy@google.com607357f2012-08-07 16:12:23 +000035 picture->draw(fDebugCanvas);
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +000036 fDebugCanvas->setPicture(NULL);
chudy@google.com607357f2012-08-07 16:12:23 +000037 fIndex = fDebugCanvas->getSize() - 1;
38 SkRefCnt_SafeAssign(fPicture, picture);
39}
40
robertphillips@google.com25bc2f82013-01-22 18:03:56 +000041SkPicture* SkDebugger::copyPicture() {
42 // We can't just call clone here since we want to removed the "deleted"
43 // commands. Playing back will strip those out.
robertphillips@google.com84b18c72014-04-13 19:09:42 +000044 SkPictureRecorder recorder;
45 SkCanvas* canvas = recorder.beginRecording(fPictureWidth, fPictureHeight);
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +000046
47 bool vizMode = fDebugCanvas->getMegaVizMode();
48 fDebugCanvas->setMegaVizMode(false);
49 bool overDraw = fDebugCanvas->getOverdrawViz();
50 fDebugCanvas->setOverdrawViz(false);
51 int saveCount = fDebugCanvas->getOutstandingSaveCount();
52 fDebugCanvas->setOutstandingSaveCount(0);
53
chudy@google.com607357f2012-08-07 16:12:23 +000054 fDebugCanvas->draw(canvas);
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +000055
56 int temp = fDebugCanvas->getOutstandingSaveCount();
57 for (int i = 0; i < temp; ++i) {
58 canvas->restore();
59 }
60
61 fDebugCanvas->setMegaVizMode(vizMode);
62 fDebugCanvas->setOverdrawViz(overDraw);
63 fDebugCanvas->setOutstandingSaveCount(saveCount);
64
robertphillips@google.com84b18c72014-04-13 19:09:42 +000065 return recorder.endRecording();
chudy@google.com902ebe52012-06-29 14:21:22 +000066}
borenet@google.com2d9dbd42013-03-12 13:07:40 +000067
68void SkDebugger::getOverviewText(const SkTDArray<double>* typeTimes,
69 double totTime,
robertphillips@google.come428f9b2013-03-12 15:33:40 +000070 SkString* overview,
71 int numRuns) {
borenet@google.com2d9dbd42013-03-12 13:07:40 +000072 const SkTDArray<SkDrawCommand*>& commands = this->getDrawCommands();
73
74 SkTDArray<int> counts;
75 counts.setCount(LAST_DRAWTYPE_ENUM+1);
76 for (int i = 0; i < LAST_DRAWTYPE_ENUM+1; ++i) {
77 counts[i] = 0;
78 }
79
80 for (int i = 0; i < commands.count(); i++) {
81 counts[commands[i]->getType()]++;
82 }
83
84 overview->reset();
85 int total = 0;
86#ifdef SK_DEBUG
87 double totPercent = 0, tempSum = 0;
88#endif
89 for (int i = 0; i < LAST_DRAWTYPE_ENUM+1; ++i) {
90 if (0 == counts[i]) {
91 // if there were no commands of this type then they should've consumed no time
92 SkASSERT(NULL == typeTimes || 0.0 == (*typeTimes)[i]);
93 continue;
94 }
95
96 overview->append(SkDrawCommand::GetCommandString((DrawType) i));
97 overview->append(": ");
robertphillips@google.come428f9b2013-03-12 15:33:40 +000098 overview->appendS32(counts[i]);
robertphillips@google.com6d9c92b2013-05-23 13:21:18 +000099 if (NULL != typeTimes && totTime >= 0.0) {
borenet@google.com2d9dbd42013-03-12 13:07:40 +0000100 overview->append(" - ");
robertphillips@google.come428f9b2013-03-12 15:33:40 +0000101 overview->appendf("%.2f", (*typeTimes)[i]/(float)numRuns);
borenet@google.com2d9dbd42013-03-12 13:07:40 +0000102 overview->append("ms");
103 overview->append(" - ");
104 double percent = 100.0*(*typeTimes)[i]/totTime;
robertphillips@google.come428f9b2013-03-12 15:33:40 +0000105 overview->appendf("%.2f", percent);
borenet@google.com2d9dbd42013-03-12 13:07:40 +0000106 overview->append("%");
107#ifdef SK_DEBUG
108 totPercent += percent;
109 tempSum += (*typeTimes)[i];
110#endif
111 }
112 overview->append("<br/>");
113 total += counts[i];
114 }
115#ifdef SK_DEBUG
116 if (NULL != typeTimes) {
skia.committer@gmail.com91274b92013-03-13 07:01:04 +0000117 SkASSERT(SkScalarNearlyEqual(SkDoubleToScalar(totPercent),
robertphillips@google.come428f9b2013-03-12 15:33:40 +0000118 SkDoubleToScalar(100.0)));
skia.committer@gmail.com91274b92013-03-13 07:01:04 +0000119 SkASSERT(SkScalarNearlyEqual(SkDoubleToScalar(tempSum),
robertphillips@google.come428f9b2013-03-12 15:33:40 +0000120 SkDoubleToScalar(totTime)));
borenet@google.com2d9dbd42013-03-12 13:07:40 +0000121 }
122#endif
123
124 if (totTime > 0.0) {
125 overview->append("Total Time: ");
robertphillips@google.come428f9b2013-03-12 15:33:40 +0000126 overview->appendf("%.2f", totTime/(float)numRuns);
borenet@google.com2d9dbd42013-03-12 13:07:40 +0000127 overview->append("ms");
128#ifdef SK_DEBUG
129 overview->append(" ");
robertphillips@google.come428f9b2013-03-12 15:33:40 +0000130 overview->appendScalar(SkDoubleToScalar(totPercent));
borenet@google.com2d9dbd42013-03-12 13:07:40 +0000131 overview->append("% ");
132#endif
133 overview->append("<br/>");
134 }
135
136 SkString totalStr;
137 totalStr.append("Total Draw Commands: ");
robertphillips@google.come428f9b2013-03-12 15:33:40 +0000138 totalStr.appendScalar(SkDoubleToScalar(total));
borenet@google.com2d9dbd42013-03-12 13:07:40 +0000139 totalStr.append("<br/>");
140 overview->insert(0, totalStr);
141
142 overview->append("<br/>");
143 overview->append("SkPicture Width: ");
robertphillips@google.come428f9b2013-03-12 15:33:40 +0000144 overview->appendS32(pictureWidth());
borenet@google.com2d9dbd42013-03-12 13:07:40 +0000145 overview->append("px<br/>");
146 overview->append("SkPicture Height: ");
robertphillips@google.come428f9b2013-03-12 15:33:40 +0000147 overview->appendS32(pictureHeight());
borenet@google.com2d9dbd42013-03-12 13:07:40 +0000148 overview->append("px");
149}