SkDebugger.cpp revision 26a00de4f303fe74527f00b777a871843ae4437e
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    : fPicture(NULL)
16    , fIndex(-1) {
17    // Create this some other dynamic way?
18    fDebugCanvas = new SkDebugCanvas(0, 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
53    fDebugCanvas->draw(canvas);
54
55    fDebugCanvas->setMegaVizMode(vizMode);
56    fDebugCanvas->setOverdrawViz(overDraw);
57    fDebugCanvas->setAllowSimplifyClip(pathOps);
58
59    return recorder.endRecording();
60}
61
62void SkDebugger::getOverviewText(const SkTDArray<double>* typeTimes,
63                                 double totTime,
64                                 SkString* overview,
65                                 int numRuns) {
66    const SkTDArray<SkDrawCommand*>& commands = this->getDrawCommands();
67
68    SkTDArray<int> counts;
69    counts.setCount(LAST_DRAWTYPE_ENUM+1);
70    for (int i = 0; i < LAST_DRAWTYPE_ENUM+1; ++i) {
71        counts[i] = 0;
72    }
73
74    for (int i = 0; i < commands.count(); i++) {
75        counts[commands[i]->getType()]++;
76    }
77
78    overview->reset();
79    int total = 0;
80#ifdef SK_DEBUG
81    double totPercent = 0, tempSum = 0;
82#endif
83    for (int i = 0; i < LAST_DRAWTYPE_ENUM+1; ++i) {
84        if (0 == counts[i]) {
85            // if there were no commands of this type then they should've consumed no time
86            SkASSERT(NULL == typeTimes || 0.0 == (*typeTimes)[i]);
87            continue;
88        }
89
90        overview->append(SkDrawCommand::GetCommandString((DrawType) i));
91        overview->append(": ");
92        overview->appendS32(counts[i]);
93        if (typeTimes && totTime >= 0.0) {
94            overview->append(" - ");
95            overview->appendf("%.2f", (*typeTimes)[i]/(float)numRuns);
96            overview->append("ms");
97            overview->append(" - ");
98            double percent = 100.0*(*typeTimes)[i]/totTime;
99            overview->appendf("%.2f", percent);
100            overview->append("%");
101#ifdef SK_DEBUG
102            totPercent += percent;
103            tempSum += (*typeTimes)[i];
104#endif
105        }
106        overview->append("<br/>");
107        total += counts[i];
108    }
109#ifdef SK_DEBUG
110    if (typeTimes) {
111        SkASSERT(SkScalarNearlyEqual(SkDoubleToScalar(totPercent),
112                                     SkDoubleToScalar(100.0)));
113        SkASSERT(SkScalarNearlyEqual(SkDoubleToScalar(tempSum),
114                                     SkDoubleToScalar(totTime)));
115    }
116#endif
117
118    if (totTime > 0.0) {
119        overview->append("Total Time: ");
120        overview->appendf("%.2f", totTime/(float)numRuns);
121        overview->append("ms");
122#ifdef SK_DEBUG
123        overview->append(" ");
124        overview->appendScalar(SkDoubleToScalar(totPercent));
125        overview->append("% ");
126#endif
127        overview->append("<br/>");
128    }
129
130    SkString totalStr;
131    totalStr.append("Total Draw Commands: ");
132    totalStr.appendScalar(SkDoubleToScalar(total));
133    totalStr.append("<br/>");
134    overview->insert(0, totalStr);
135
136    overview->append("<br/>SkPicture L: ");
137    overview->appendScalar(this->pictureCull().fLeft);
138    overview->append(" T: ");
139    overview->appendScalar(this->pictureCull().fTop);
140    overview->append(" R: ");
141    overview->appendScalar(this->pictureCull().fRight);
142    overview->append(" B: ");
143    overview->appendScalar(this->pictureCull().fBottom);
144    overview->append("<br/>");
145}
146
147void SkDebugger::getClipStackText(SkString* clipStack) {
148    clipStack->set(fDebugCanvas->clipStackData());
149}
150