blob: f4730d61b5306116c2eb933f9a96d2cce04c3357 [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?
16 fDebugCanvas = new SkDebugCanvas(100, 100);
17 fPicture = NULL;
18 fPictureWidth = 0;
19 fPictureHeight = 0;
20 fIndex = 0;
21}
22
23SkDebugger::~SkDebugger() {
24 // Need to inherit from SkRef object in order for following to work
25 SkSafeUnref(fDebugCanvas);
26 SkSafeUnref(fPicture);
27}
28
29void SkDebugger::loadPicture(SkPicture* picture) {
30 fPictureWidth = picture->width();
31 fPictureHeight = picture->height();
32 delete fDebugCanvas;
33 fDebugCanvas = new SkDebugCanvas(fPictureWidth, fPictureHeight);
34 fDebugCanvas->setBounds(fPictureWidth, fPictureHeight);
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +000035 fDebugCanvas->setPicture(picture);
chudy@google.com607357f2012-08-07 16:12:23 +000036 picture->draw(fDebugCanvas);
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +000037 fDebugCanvas->setPicture(NULL);
chudy@google.com607357f2012-08-07 16:12:23 +000038 fIndex = fDebugCanvas->getSize() - 1;
39 SkRefCnt_SafeAssign(fPicture, picture);
40}
41
robertphillips@google.com25bc2f82013-01-22 18:03:56 +000042SkPicture* SkDebugger::copyPicture() {
43 // We can't just call clone here since we want to removed the "deleted"
44 // commands. Playing back will strip those out.
robertphillips@google.com84b18c72014-04-13 19:09:42 +000045 SkPictureRecorder recorder;
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +000046 SkCanvas* canvas = recorder.beginRecording(fPictureWidth, fPictureHeight, NULL, 0);
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +000047
48 bool vizMode = fDebugCanvas->getMegaVizMode();
49 fDebugCanvas->setMegaVizMode(false);
50 bool overDraw = fDebugCanvas->getOverdrawViz();
51 fDebugCanvas->setOverdrawViz(false);
52 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);
65
robertphillips@google.com84b18c72014-04-13 19:09:42 +000066 return recorder.endRecording();
chudy@google.com902ebe52012-06-29 14:21:22 +000067}
borenet@google.com2d9dbd42013-03-12 13:07:40 +000068
69void SkDebugger::getOverviewText(const SkTDArray<double>* typeTimes,
70 double totTime,
robertphillips@google.come428f9b2013-03-12 15:33:40 +000071 SkString* overview,
72 int numRuns) {
borenet@google.com2d9dbd42013-03-12 13:07:40 +000073 const SkTDArray<SkDrawCommand*>& commands = this->getDrawCommands();
74
75 SkTDArray<int> counts;
76 counts.setCount(LAST_DRAWTYPE_ENUM+1);
77 for (int i = 0; i < LAST_DRAWTYPE_ENUM+1; ++i) {
78 counts[i] = 0;
79 }
80
81 for (int i = 0; i < commands.count(); i++) {
82 counts[commands[i]->getType()]++;
83 }
84
85 overview->reset();
86 int total = 0;
87#ifdef SK_DEBUG
88 double totPercent = 0, tempSum = 0;
89#endif
90 for (int i = 0; i < LAST_DRAWTYPE_ENUM+1; ++i) {
91 if (0 == counts[i]) {
92 // if there were no commands of this type then they should've consumed no time
93 SkASSERT(NULL == typeTimes || 0.0 == (*typeTimes)[i]);
94 continue;
95 }
96
97 overview->append(SkDrawCommand::GetCommandString((DrawType) i));
98 overview->append(": ");
robertphillips@google.come428f9b2013-03-12 15:33:40 +000099 overview->appendS32(counts[i]);
robertphillips@google.com6d9c92b2013-05-23 13:21:18 +0000100 if (NULL != typeTimes && totTime >= 0.0) {
borenet@google.com2d9dbd42013-03-12 13:07:40 +0000101 overview->append(" - ");
robertphillips@google.come428f9b2013-03-12 15:33:40 +0000102 overview->appendf("%.2f", (*typeTimes)[i]/(float)numRuns);
borenet@google.com2d9dbd42013-03-12 13:07:40 +0000103 overview->append("ms");
104 overview->append(" - ");
105 double percent = 100.0*(*typeTimes)[i]/totTime;
robertphillips@google.come428f9b2013-03-12 15:33:40 +0000106 overview->appendf("%.2f", percent);
borenet@google.com2d9dbd42013-03-12 13:07:40 +0000107 overview->append("%");
108#ifdef SK_DEBUG
109 totPercent += percent;
110 tempSum += (*typeTimes)[i];
111#endif
112 }
113 overview->append("<br/>");
114 total += counts[i];
115 }
116#ifdef SK_DEBUG
117 if (NULL != typeTimes) {
skia.committer@gmail.com91274b92013-03-13 07:01:04 +0000118 SkASSERT(SkScalarNearlyEqual(SkDoubleToScalar(totPercent),
robertphillips@google.come428f9b2013-03-12 15:33:40 +0000119 SkDoubleToScalar(100.0)));
skia.committer@gmail.com91274b92013-03-13 07:01:04 +0000120 SkASSERT(SkScalarNearlyEqual(SkDoubleToScalar(tempSum),
robertphillips@google.come428f9b2013-03-12 15:33:40 +0000121 SkDoubleToScalar(totTime)));
borenet@google.com2d9dbd42013-03-12 13:07:40 +0000122 }
123#endif
124
125 if (totTime > 0.0) {
126 overview->append("Total Time: ");
robertphillips@google.come428f9b2013-03-12 15:33:40 +0000127 overview->appendf("%.2f", totTime/(float)numRuns);
borenet@google.com2d9dbd42013-03-12 13:07:40 +0000128 overview->append("ms");
129#ifdef SK_DEBUG
130 overview->append(" ");
robertphillips@google.come428f9b2013-03-12 15:33:40 +0000131 overview->appendScalar(SkDoubleToScalar(totPercent));
borenet@google.com2d9dbd42013-03-12 13:07:40 +0000132 overview->append("% ");
133#endif
134 overview->append("<br/>");
135 }
136
137 SkString totalStr;
138 totalStr.append("Total Draw Commands: ");
robertphillips@google.come428f9b2013-03-12 15:33:40 +0000139 totalStr.appendScalar(SkDoubleToScalar(total));
borenet@google.com2d9dbd42013-03-12 13:07:40 +0000140 totalStr.append("<br/>");
141 overview->insert(0, totalStr);
142
143 overview->append("<br/>");
144 overview->append("SkPicture Width: ");
robertphillips@google.come428f9b2013-03-12 15:33:40 +0000145 overview->appendS32(pictureWidth());
borenet@google.com2d9dbd42013-03-12 13:07:40 +0000146 overview->append("px<br/>");
147 overview->append("SkPicture Height: ");
robertphillips@google.come428f9b2013-03-12 15:33:40 +0000148 overview->appendS32(pictureHeight());
borenet@google.com2d9dbd42013-03-12 13:07:40 +0000149 overview->append("px");
150}