1
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
9#include "SkDebugger.h"
10#include "SkPictureRecorder.h"
11#include "SkString.h"
12
13
14SkDebugger::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);
35    fDebugCanvas->setPicture(picture);
36    picture->draw(fDebugCanvas);
37    fDebugCanvas->setPicture(NULL);
38    fIndex = fDebugCanvas->getSize() - 1;
39    SkRefCnt_SafeAssign(fPicture, picture);
40}
41
42SkPicture* 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.
45    SkPictureRecorder recorder;
46    SkCanvas* canvas = recorder.beginRecording(fPictureWidth, fPictureHeight, NULL, 0);
47
48    bool vizMode = fDebugCanvas->getMegaVizMode();
49    fDebugCanvas->setMegaVizMode(false);
50    bool overDraw = fDebugCanvas->getOverdrawViz();
51    fDebugCanvas->setOverdrawViz(false);
52    bool pathOps = fDebugCanvas->getAllowSimplifyClip();
53    fDebugCanvas->setAllowSimplifyClip(false);
54    int saveCount = fDebugCanvas->getOutstandingSaveCount();
55    fDebugCanvas->setOutstandingSaveCount(0);
56
57    fDebugCanvas->draw(canvas);
58
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);
67    fDebugCanvas->setAllowSimplifyClip(pathOps);
68
69    return recorder.endRecording();
70}
71
72void SkDebugger::getOverviewText(const SkTDArray<double>* typeTimes,
73                                 double totTime,
74                                 SkString* overview,
75                                 int numRuns) {
76    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(": ");
102        overview->appendS32(counts[i]);
103        if (NULL != typeTimes && totTime >= 0.0) {
104            overview->append(" - ");
105            overview->appendf("%.2f", (*typeTimes)[i]/(float)numRuns);
106            overview->append("ms");
107            overview->append(" - ");
108            double percent = 100.0*(*typeTimes)[i]/totTime;
109            overview->appendf("%.2f", percent);
110            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) {
121        SkASSERT(SkScalarNearlyEqual(SkDoubleToScalar(totPercent),
122                                     SkDoubleToScalar(100.0)));
123        SkASSERT(SkScalarNearlyEqual(SkDoubleToScalar(tempSum),
124                                     SkDoubleToScalar(totTime)));
125    }
126#endif
127
128    if (totTime > 0.0) {
129        overview->append("Total Time: ");
130        overview->appendf("%.2f", totTime/(float)numRuns);
131        overview->append("ms");
132#ifdef SK_DEBUG
133        overview->append(" ");
134        overview->appendScalar(SkDoubleToScalar(totPercent));
135        overview->append("% ");
136#endif
137        overview->append("<br/>");
138    }
139
140    SkString totalStr;
141    totalStr.append("Total Draw Commands: ");
142    totalStr.appendScalar(SkDoubleToScalar(total));
143    totalStr.append("<br/>");
144    overview->insert(0, totalStr);
145
146    overview->append("<br/>");
147    overview->append("SkPicture Width: ");
148    overview->appendS32(pictureWidth());
149    overview->append("px<br/>");
150    overview->append("SkPicture Height: ");
151    overview->appendS32(pictureHeight());
152    overview->append("px");
153}
154
155void SkDebugger::getClipStackText(SkString* clipStack) {
156    clipStack->set(fDebugCanvas->clipStackData());
157}
158