blob: 394c0ad7c82e70c25949947b985ca4ab8bdb3ead [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);
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +000052 bool pathOps = fDebugCanvas->getAllowSimplifyClip();
53 fDebugCanvas->setAllowSimplifyClip(false);
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +000054 int saveCount = fDebugCanvas->getOutstandingSaveCount();
55 fDebugCanvas->setOutstandingSaveCount(0);
56
chudy@google.com607357f2012-08-07 16:12:23 +000057 fDebugCanvas->draw(canvas);
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +000058
59 int temp = fDebugCanvas->getOutstandingSaveCount();
60 for (int i = 0; i < temp; ++i) {
61 canvas->restore();
62 }
63
64 fDebugCanvas->setMegaVizMode(vizMode);
65 fDebugCanvas->setOverdrawViz(overDraw);
66 fDebugCanvas->setOutstandingSaveCount(saveCount);
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +000067 fDebugCanvas->setAllowSimplifyClip(pathOps);
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +000068
robertphillips@google.com84b18c72014-04-13 19:09:42 +000069 return recorder.endRecording();
chudy@google.com902ebe52012-06-29 14:21:22 +000070}
borenet@google.com2d9dbd42013-03-12 13:07:40 +000071
72void SkDebugger::getOverviewText(const SkTDArray<double>* typeTimes,
73 double totTime,
robertphillips@google.come428f9b2013-03-12 15:33:40 +000074 SkString* overview,
75 int numRuns) {
borenet@google.com2d9dbd42013-03-12 13:07:40 +000076 const SkTDArray<SkDrawCommand*>& commands = this->getDrawCommands();
77
78 SkTDArray<int> counts;
79 counts.setCount(LAST_DRAWTYPE_ENUM+1);
80 for (int i = 0; i < LAST_DRAWTYPE_ENUM+1; ++i) {
81 counts[i] = 0;
82 }
83
84 for (int i = 0; i < commands.count(); i++) {
85 counts[commands[i]->getType()]++;
86 }
87
88 overview->reset();
89 int total = 0;
90#ifdef SK_DEBUG
91 double totPercent = 0, tempSum = 0;
92#endif
93 for (int i = 0; i < LAST_DRAWTYPE_ENUM+1; ++i) {
94 if (0 == counts[i]) {
95 // if there were no commands of this type then they should've consumed no time
96 SkASSERT(NULL == typeTimes || 0.0 == (*typeTimes)[i]);
97 continue;
98 }
99
100 overview->append(SkDrawCommand::GetCommandString((DrawType) i));
101 overview->append(": ");
robertphillips@google.come428f9b2013-03-12 15:33:40 +0000102 overview->appendS32(counts[i]);
robertphillips@google.com6d9c92b2013-05-23 13:21:18 +0000103 if (NULL != typeTimes && totTime >= 0.0) {
borenet@google.com2d9dbd42013-03-12 13:07:40 +0000104 overview->append(" - ");
robertphillips@google.come428f9b2013-03-12 15:33:40 +0000105 overview->appendf("%.2f", (*typeTimes)[i]/(float)numRuns);
borenet@google.com2d9dbd42013-03-12 13:07:40 +0000106 overview->append("ms");
107 overview->append(" - ");
108 double percent = 100.0*(*typeTimes)[i]/totTime;
robertphillips@google.come428f9b2013-03-12 15:33:40 +0000109 overview->appendf("%.2f", percent);
borenet@google.com2d9dbd42013-03-12 13:07:40 +0000110 overview->append("%");
111#ifdef SK_DEBUG
112 totPercent += percent;
113 tempSum += (*typeTimes)[i];
114#endif
115 }
116 overview->append("<br/>");
117 total += counts[i];
118 }
119#ifdef SK_DEBUG
120 if (NULL != typeTimes) {
skia.committer@gmail.com91274b92013-03-13 07:01:04 +0000121 SkASSERT(SkScalarNearlyEqual(SkDoubleToScalar(totPercent),
robertphillips@google.come428f9b2013-03-12 15:33:40 +0000122 SkDoubleToScalar(100.0)));
skia.committer@gmail.com91274b92013-03-13 07:01:04 +0000123 SkASSERT(SkScalarNearlyEqual(SkDoubleToScalar(tempSum),
robertphillips@google.come428f9b2013-03-12 15:33:40 +0000124 SkDoubleToScalar(totTime)));
borenet@google.com2d9dbd42013-03-12 13:07:40 +0000125 }
126#endif
127
128 if (totTime > 0.0) {
129 overview->append("Total Time: ");
robertphillips@google.come428f9b2013-03-12 15:33:40 +0000130 overview->appendf("%.2f", totTime/(float)numRuns);
borenet@google.com2d9dbd42013-03-12 13:07:40 +0000131 overview->append("ms");
132#ifdef SK_DEBUG
133 overview->append(" ");
robertphillips@google.come428f9b2013-03-12 15:33:40 +0000134 overview->appendScalar(SkDoubleToScalar(totPercent));
borenet@google.com2d9dbd42013-03-12 13:07:40 +0000135 overview->append("% ");
136#endif
137 overview->append("<br/>");
138 }
139
140 SkString totalStr;
141 totalStr.append("Total Draw Commands: ");
robertphillips@google.come428f9b2013-03-12 15:33:40 +0000142 totalStr.appendScalar(SkDoubleToScalar(total));
borenet@google.com2d9dbd42013-03-12 13:07:40 +0000143 totalStr.append("<br/>");
144 overview->insert(0, totalStr);
145
146 overview->append("<br/>");
147 overview->append("SkPicture Width: ");
robertphillips@google.come428f9b2013-03-12 15:33:40 +0000148 overview->appendS32(pictureWidth());
borenet@google.com2d9dbd42013-03-12 13:07:40 +0000149 overview->append("px<br/>");
150 overview->append("SkPicture Height: ");
robertphillips@google.come428f9b2013-03-12 15:33:40 +0000151 overview->appendS32(pictureHeight());
borenet@google.com2d9dbd42013-03-12 13:07:40 +0000152 overview->append("px");
153}
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +0000154
155void SkDebugger::getClipStackText(SkString* clipStack) {
156 clipStack->set(fDebugCanvas->clipStackData());
157}
158