blob: b40e6ecef5b26aae961b7fadd9cb8bb23ab0e5f2 [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);
34 picture->draw(fDebugCanvas);
35 fIndex = fDebugCanvas->getSize() - 1;
36 SkRefCnt_SafeAssign(fPicture, picture);
37}
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.
42 SkPicture* newPicture = new SkPicture;
43 SkCanvas* canvas = newPicture->beginRecording(fPictureWidth, fPictureHeight);
chudy@google.com607357f2012-08-07 16:12:23 +000044 fDebugCanvas->draw(canvas);
robertphillips@google.com25bc2f82013-01-22 18:03:56 +000045 newPicture->endRecording();
46 return newPicture;
chudy@google.com902ebe52012-06-29 14:21:22 +000047}
borenet@google.com2d9dbd42013-03-12 13:07:40 +000048
49void SkDebugger::getOverviewText(const SkTDArray<double>* typeTimes,
50 double totTime,
robertphillips@google.come428f9b2013-03-12 15:33:40 +000051 SkString* overview,
52 int numRuns) {
borenet@google.com2d9dbd42013-03-12 13:07:40 +000053 const SkTDArray<SkDrawCommand*>& commands = this->getDrawCommands();
54
55 SkTDArray<int> counts;
56 counts.setCount(LAST_DRAWTYPE_ENUM+1);
57 for (int i = 0; i < LAST_DRAWTYPE_ENUM+1; ++i) {
58 counts[i] = 0;
59 }
60
61 for (int i = 0; i < commands.count(); i++) {
62 counts[commands[i]->getType()]++;
63 }
64
65 overview->reset();
66 int total = 0;
67#ifdef SK_DEBUG
68 double totPercent = 0, tempSum = 0;
69#endif
70 for (int i = 0; i < LAST_DRAWTYPE_ENUM+1; ++i) {
71 if (0 == counts[i]) {
72 // if there were no commands of this type then they should've consumed no time
73 SkASSERT(NULL == typeTimes || 0.0 == (*typeTimes)[i]);
74 continue;
75 }
76
77 overview->append(SkDrawCommand::GetCommandString((DrawType) i));
78 overview->append(": ");
robertphillips@google.come428f9b2013-03-12 15:33:40 +000079 overview->appendS32(counts[i]);
borenet@google.com2d9dbd42013-03-12 13:07:40 +000080 if (NULL != typeTimes) {
81 overview->append(" - ");
robertphillips@google.come428f9b2013-03-12 15:33:40 +000082 overview->appendf("%.2f", (*typeTimes)[i]/(float)numRuns);
borenet@google.com2d9dbd42013-03-12 13:07:40 +000083 overview->append("ms");
84 overview->append(" - ");
85 double percent = 100.0*(*typeTimes)[i]/totTime;
robertphillips@google.come428f9b2013-03-12 15:33:40 +000086 overview->appendf("%.2f", percent);
borenet@google.com2d9dbd42013-03-12 13:07:40 +000087 overview->append("%");
88#ifdef SK_DEBUG
89 totPercent += percent;
90 tempSum += (*typeTimes)[i];
91#endif
92 }
93 overview->append("<br/>");
94 total += counts[i];
95 }
96#ifdef SK_DEBUG
97 if (NULL != typeTimes) {
robertphillips@google.come428f9b2013-03-12 15:33:40 +000098 SkASSERT(SkScalarNearlyEqual(SkDoubleToScalar(totPercent),
99 SkDoubleToScalar(100.0)));
100 SkASSERT(SkScalarNearlyEqual(SkDoubleToScalar(tempSum),
101 SkDoubleToScalar(totTime)));
borenet@google.com2d9dbd42013-03-12 13:07:40 +0000102 }
103#endif
104
105 if (totTime > 0.0) {
106 overview->append("Total Time: ");
robertphillips@google.come428f9b2013-03-12 15:33:40 +0000107 overview->appendf("%.2f", totTime/(float)numRuns);
borenet@google.com2d9dbd42013-03-12 13:07:40 +0000108 overview->append("ms");
109#ifdef SK_DEBUG
110 overview->append(" ");
robertphillips@google.come428f9b2013-03-12 15:33:40 +0000111 overview->appendScalar(SkDoubleToScalar(totPercent));
borenet@google.com2d9dbd42013-03-12 13:07:40 +0000112 overview->append("% ");
113#endif
114 overview->append("<br/>");
115 }
116
117 SkString totalStr;
118 totalStr.append("Total Draw Commands: ");
robertphillips@google.come428f9b2013-03-12 15:33:40 +0000119 totalStr.appendScalar(SkDoubleToScalar(total));
borenet@google.com2d9dbd42013-03-12 13:07:40 +0000120 totalStr.append("<br/>");
121 overview->insert(0, totalStr);
122
123 overview->append("<br/>");
124 overview->append("SkPicture Width: ");
robertphillips@google.come428f9b2013-03-12 15:33:40 +0000125 overview->appendS32(pictureWidth());
borenet@google.com2d9dbd42013-03-12 13:07:40 +0000126 overview->append("px<br/>");
127 overview->append("SkPicture Height: ");
robertphillips@google.come428f9b2013-03-12 15:33:40 +0000128 overview->appendS32(pictureHeight());
borenet@google.com2d9dbd42013-03-12 13:07:40 +0000129 overview->append("px");
130}