blob: 5ed11870d442be8f13b042c08674213e80162c1c [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,
51 SkString* overview) {
52 const SkTDArray<SkDrawCommand*>& commands = this->getDrawCommands();
53
54 SkTDArray<int> counts;
55 counts.setCount(LAST_DRAWTYPE_ENUM+1);
56 for (int i = 0; i < LAST_DRAWTYPE_ENUM+1; ++i) {
57 counts[i] = 0;
58 }
59
60 for (int i = 0; i < commands.count(); i++) {
61 counts[commands[i]->getType()]++;
62 }
63
64 overview->reset();
65 int total = 0;
66#ifdef SK_DEBUG
67 double totPercent = 0, tempSum = 0;
68#endif
69 for (int i = 0; i < LAST_DRAWTYPE_ENUM+1; ++i) {
70 if (0 == counts[i]) {
71 // if there were no commands of this type then they should've consumed no time
72 SkASSERT(NULL == typeTimes || 0.0 == (*typeTimes)[i]);
73 continue;
74 }
75
76 overview->append(SkDrawCommand::GetCommandString((DrawType) i));
77 overview->append(": ");
78 overview->appendScalar(counts[i]);
79 if (NULL != typeTimes) {
80 overview->append(" - ");
81 overview->appendScalar((*typeTimes)[i]);
82 overview->append("ms");
83 overview->append(" - ");
84 double percent = 100.0*(*typeTimes)[i]/totTime;
85 overview->appendScalar(percent);
86 overview->append("%");
87#ifdef SK_DEBUG
88 totPercent += percent;
89 tempSum += (*typeTimes)[i];
90#endif
91 }
92 overview->append("<br/>");
93 total += counts[i];
94 }
95#ifdef SK_DEBUG
96 if (NULL != typeTimes) {
97 SkASSERT(SkScalarNearlyEqual(totPercent, 100.0));
98 SkASSERT(SkScalarNearlyEqual(tempSum, totTime));
99 }
100#endif
101
102 if (totTime > 0.0) {
103 overview->append("Total Time: ");
104 overview->appendScalar(totTime);
105 overview->append("ms");
106#ifdef SK_DEBUG
107 overview->append(" ");
108 overview->appendScalar(totPercent);
109 overview->append("% ");
110#endif
111 overview->append("<br/>");
112 }
113
114 SkString totalStr;
115 totalStr.append("Total Draw Commands: ");
116 totalStr.appendScalar(total);
117 totalStr.append("<br/>");
118 overview->insert(0, totalStr);
119
120 overview->append("<br/>");
121 overview->append("SkPicture Width: ");
122 overview->appendScalar(pictureWidth());
123 overview->append("px<br/>");
124 overview->append("SkPicture Height: ");
125 overview->appendScalar(pictureHeight());
126 overview->append("px");
127}