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(0, 0);
17    fPicture = NULL;
18    fIndex = 0;
19}
20
21SkDebugger::~SkDebugger() {
22    // Need to inherit from SkRef object in order for following to work
23    SkSafeUnref(fDebugCanvas);
24    SkSafeUnref(fPicture);
25}
26
27void SkDebugger::loadPicture(SkPicture* picture) {
28    SkRefCnt_SafeAssign(fPicture, picture);
29
30    delete fDebugCanvas;
31    fDebugCanvas = new SkDebugCanvas(SkScalarCeilToInt(this->pictureCull().width()),
32                                     SkScalarCeilToInt(this->pictureCull().height()));
33    fDebugCanvas->setPicture(picture);
34    picture->playback(fDebugCanvas);
35    fDebugCanvas->setPicture(NULL);
36    fIndex = fDebugCanvas->getSize() - 1;
37}
38
39SkPicture* 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    SkPictureRecorder recorder;
43    SkCanvas* canvas = recorder.beginRecording(this->pictureCull().width(),
44                                               this->pictureCull().height());
45
46    bool vizMode = fDebugCanvas->getMegaVizMode();
47    fDebugCanvas->setMegaVizMode(false);
48    bool overDraw = fDebugCanvas->getOverdrawViz();
49    fDebugCanvas->setOverdrawViz(false);
50    bool pathOps = fDebugCanvas->getAllowSimplifyClip();
51    fDebugCanvas->setAllowSimplifyClip(false);
52    int saveCount = fDebugCanvas->getOutstandingSaveCount();
53    fDebugCanvas->setOutstandingSaveCount(0);
54
55    fDebugCanvas->draw(canvas);
56
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    fDebugCanvas->setAllowSimplifyClip(pathOps);
66
67    return recorder.endRecording();
68}
69
70void SkDebugger::getOverviewText(const SkTDArray<double>* typeTimes,
71                                 double totTime,
72                                 SkString* overview,
73                                 int numRuns) {
74    const SkTDArray<SkDrawCommand*>& commands = this->getDrawCommands();
75
76    SkTDArray<int> counts;
77    counts.setCount(LAST_DRAWTYPE_ENUM+1);
78    for (int i = 0; i < LAST_DRAWTYPE_ENUM+1; ++i) {
79        counts[i] = 0;
80    }
81
82    for (int i = 0; i < commands.count(); i++) {
83        counts[commands[i]->getType()]++;
84    }
85
86    overview->reset();
87    int total = 0;
88#ifdef SK_DEBUG
89    double totPercent = 0, tempSum = 0;
90#endif
91    for (int i = 0; i < LAST_DRAWTYPE_ENUM+1; ++i) {
92        if (0 == counts[i]) {
93            // if there were no commands of this type then they should've consumed no time
94            SkASSERT(NULL == typeTimes || 0.0 == (*typeTimes)[i]);
95            continue;
96        }
97
98        overview->append(SkDrawCommand::GetCommandString((DrawType) i));
99        overview->append(": ");
100        overview->appendS32(counts[i]);
101        if (typeTimes && totTime >= 0.0) {
102            overview->append(" - ");
103            overview->appendf("%.2f", (*typeTimes)[i]/(float)numRuns);
104            overview->append("ms");
105            overview->append(" - ");
106            double percent = 100.0*(*typeTimes)[i]/totTime;
107            overview->appendf("%.2f", percent);
108            overview->append("%");
109#ifdef SK_DEBUG
110            totPercent += percent;
111            tempSum += (*typeTimes)[i];
112#endif
113        }
114        overview->append("<br/>");
115        total += counts[i];
116    }
117#ifdef SK_DEBUG
118    if (typeTimes) {
119        SkASSERT(SkScalarNearlyEqual(SkDoubleToScalar(totPercent),
120                                     SkDoubleToScalar(100.0)));
121        SkASSERT(SkScalarNearlyEqual(SkDoubleToScalar(tempSum),
122                                     SkDoubleToScalar(totTime)));
123    }
124#endif
125
126    if (totTime > 0.0) {
127        overview->append("Total Time: ");
128        overview->appendf("%.2f", totTime/(float)numRuns);
129        overview->append("ms");
130#ifdef SK_DEBUG
131        overview->append(" ");
132        overview->appendScalar(SkDoubleToScalar(totPercent));
133        overview->append("% ");
134#endif
135        overview->append("<br/>");
136    }
137
138    SkString totalStr;
139    totalStr.append("Total Draw Commands: ");
140    totalStr.appendScalar(SkDoubleToScalar(total));
141    totalStr.append("<br/>");
142    overview->insert(0, totalStr);
143
144    overview->append("<br/>SkPicture L: ");
145    overview->appendScalar(this->pictureCull().fLeft);
146    overview->append(" T: ");
147    overview->appendScalar(this->pictureCull().fTop);
148    overview->append(" R: ");
149    overview->appendScalar(this->pictureCull().fRight);
150    overview->append(" B: ");
151    overview->appendScalar(this->pictureCull().fBottom);
152    overview->append("<br/>");
153}
154
155void SkDebugger::getClipStackText(SkString* clipStack) {
156    clipStack->set(fDebugCanvas->clipStackData());
157}
158