blob: 3a135798942787979f4e40422550ece7197aca97 [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,
robertphillips@google.come428f9b2013-03-12 15:33:40 +000051 SkString* overview,
52 int numRuns) {
borenet@google.com2d9dbd42013-03-12 13:07:40 +000053 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(": ");
robertphillips@google.come428f9b2013-03-12 15:33:40 +000079 overview->appendS32(counts[i]);
borenet@google.com2d9dbd42013-03-12 13:07:40 +000080 if (NULL != typeTimes) {
81 overview->append(" - ");
robertphillips@google.come428f9b2013-03-12 15:33:40 +000082 overview->appendf("%.2f", (*typeTimes)[i]/(float)numRuns);
borenet@google.com2d9dbd42013-03-12 13:07:40 +000083 overview->append("ms");
84 overview->append(" - ");
85 double percent = 100.0*(*typeTimes)[i]/totTime;
robertphillips@google.come428f9b2013-03-12 15:33:40 +000086 overview->appendf("%.2f", percent);
borenet@google.com2d9dbd42013-03-12 13:07:40 +000087 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) {
skia.committer@gmail.com91274b92013-03-13 07:01:04 +000098 SkASSERT(SkScalarNearlyEqual(SkDoubleToScalar(totPercent),
robertphillips@google.come428f9b2013-03-12 15:33:40 +000099 SkDoubleToScalar(100.0)));
skia.committer@gmail.com91274b92013-03-13 07:01:04 +0000100 SkASSERT(SkScalarNearlyEqual(SkDoubleToScalar(tempSum),
robertphillips@google.come428f9b2013-03-12 15:33:40 +0000101 SkDoubleToScalar(totTime)));
borenet@google.com2d9dbd42013-03-12 13:07:40 +0000102 }
103#endif
104
105 if (totTime > 0.0) {
106 overview->append("Total Time: ");
robertphillips@google.come428f9b2013-03-12 15:33:40 +0000107 overview->appendf("%.2f", totTime/(float)numRuns);
borenet@google.com2d9dbd42013-03-12 13:07:40 +0000108 overview->append("ms");
109#ifdef SK_DEBUG
110 overview->append(" ");
robertphillips@google.come428f9b2013-03-12 15:33:40 +0000111 overview->appendScalar(SkDoubleToScalar(totPercent));
borenet@google.com2d9dbd42013-03-12 13:07:40 +0000112 overview->append("% ");
113#endif
114 overview->append("<br/>");
115 }
116
117 SkString totalStr;
118 totalStr.append("Total Draw Commands: ");
robertphillips@google.come428f9b2013-03-12 15:33:40 +0000119 totalStr.appendScalar(SkDoubleToScalar(total));
borenet@google.com2d9dbd42013-03-12 13:07:40 +0000120 totalStr.append("<br/>");
121 overview->insert(0, totalStr);
122
123 overview->append("<br/>");
124 overview->append("SkPicture Width: ");
robertphillips@google.come428f9b2013-03-12 15:33:40 +0000125 overview->appendS32(pictureWidth());
borenet@google.com2d9dbd42013-03-12 13:07:40 +0000126 overview->append("px<br/>");
127 overview->append("SkPicture Height: ");
robertphillips@google.come428f9b2013-03-12 15:33:40 +0000128 overview->appendS32(pictureHeight());
borenet@google.com2d9dbd42013-03-12 13:07:40 +0000129 overview->append("px");
130}
scroggo@google.coma53e9462013-04-12 14:50:10 +0000131
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);
scroggo@google.comaaec8512013-04-15 22:05:03 +0000141 SkDEBUGCODE(creator = ) CreateWEBPImageDecoder();
142 SkASSERT(creator);
scroggo@google.com81a51a22013-04-12 15:04:33 +0000143#if defined(SK_BUILD_FOR_UNIX) && !defined(SK_BUILD_FOR_NACL)
scroggo@google.coma53e9462013-04-12 14:50:10 +0000144 SkDEBUGCODE(creator = ) CreateGIFImageDecoder();
145 SkASSERT(creator);
146#endif
147}