SkDebugger.cpp revision aaec851ad16d312390398f245ca66aaa6d304ebb
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 "SkString.h"
11
12
13SkDebugger::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
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    SkPicture* newPicture = new SkPicture;
43    SkCanvas* canvas = newPicture->beginRecording(fPictureWidth, fPictureHeight);
44    fDebugCanvas->draw(canvas);
45    newPicture->endRecording();
46    return newPicture;
47}
48
49void SkDebugger::getOverviewText(const SkTDArray<double>* typeTimes,
50                                 double totTime,
51                                 SkString* overview,
52                                 int numRuns) {
53    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(": ");
79        overview->appendS32(counts[i]);
80        if (NULL != typeTimes) {
81            overview->append(" - ");
82            overview->appendf("%.2f", (*typeTimes)[i]/(float)numRuns);
83            overview->append("ms");
84            overview->append(" - ");
85            double percent = 100.0*(*typeTimes)[i]/totTime;
86            overview->appendf("%.2f", percent);
87            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) {
98        SkASSERT(SkScalarNearlyEqual(SkDoubleToScalar(totPercent),
99                                     SkDoubleToScalar(100.0)));
100        SkASSERT(SkScalarNearlyEqual(SkDoubleToScalar(tempSum),
101                                     SkDoubleToScalar(totTime)));
102    }
103#endif
104
105    if (totTime > 0.0) {
106        overview->append("Total Time: ");
107        overview->appendf("%.2f", totTime/(float)numRuns);
108        overview->append("ms");
109#ifdef SK_DEBUG
110        overview->append(" ");
111        overview->appendScalar(SkDoubleToScalar(totPercent));
112        overview->append("% ");
113#endif
114        overview->append("<br/>");
115    }
116
117    SkString totalStr;
118    totalStr.append("Total Draw Commands: ");
119    totalStr.appendScalar(SkDoubleToScalar(total));
120    totalStr.append("<br/>");
121    overview->insert(0, totalStr);
122
123    overview->append("<br/>");
124    overview->append("SkPicture Width: ");
125    overview->appendS32(pictureWidth());
126    overview->append("px<br/>");
127    overview->append("SkPicture Height: ");
128    overview->appendS32(pictureHeight());
129    overview->append("px");
130}
131
132#include "SkImageDecoder.h"
133
134void forceLinking();
135void forceLinking() {
136    // This function leaks, but that is okay because it is not intended
137    // to be called. It is only here so that the linker will include the
138    // decoders.
139    SkDEBUGCODE(SkImageDecoder *creator = ) CreateJPEGImageDecoder();
140    SkASSERT(creator);
141    SkDEBUGCODE(creator = ) CreateWEBPImageDecoder();
142    SkASSERT(creator);
143#if defined(SK_BUILD_FOR_UNIX) && !defined(SK_BUILD_FOR_NACL)
144    SkDEBUGCODE(creator = ) CreateGIFImageDecoder();
145    SkASSERT(creator);
146#endif
147}
148