chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 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 | |
chudy@google.com | 607357f | 2012-08-07 16:12:23 +0000 | [diff] [blame] | 9 | #include "SkDebugger.h" |
borenet@google.com | 2d9dbd4 | 2013-03-12 13:07:40 +0000 | [diff] [blame] | 10 | #include "SkString.h" |
| 11 | |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 12 | |
chudy@google.com | 607357f | 2012-08-07 16:12:23 +0000 | [diff] [blame] | 13 | SkDebugger::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 | |
| 22 | SkDebugger::~SkDebugger() { |
| 23 | // Need to inherit from SkRef object in order for following to work |
| 24 | SkSafeUnref(fDebugCanvas); |
| 25 | SkSafeUnref(fPicture); |
| 26 | } |
| 27 | |
| 28 | void 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); |
commit-bot@chromium.org | 57f74e0 | 2014-03-25 23:31:33 +0000 | [diff] [blame^] | 34 | fDebugCanvas->setPicture(picture); |
chudy@google.com | 607357f | 2012-08-07 16:12:23 +0000 | [diff] [blame] | 35 | picture->draw(fDebugCanvas); |
commit-bot@chromium.org | 57f74e0 | 2014-03-25 23:31:33 +0000 | [diff] [blame^] | 36 | fDebugCanvas->setPicture(NULL); |
chudy@google.com | 607357f | 2012-08-07 16:12:23 +0000 | [diff] [blame] | 37 | fIndex = fDebugCanvas->getSize() - 1; |
| 38 | SkRefCnt_SafeAssign(fPicture, picture); |
| 39 | } |
| 40 | |
robertphillips@google.com | 25bc2f8 | 2013-01-22 18:03:56 +0000 | [diff] [blame] | 41 | SkPicture* SkDebugger::copyPicture() { |
| 42 | // We can't just call clone here since we want to removed the "deleted" |
| 43 | // commands. Playing back will strip those out. |
| 44 | SkPicture* newPicture = new SkPicture; |
| 45 | SkCanvas* canvas = newPicture->beginRecording(fPictureWidth, fPictureHeight); |
commit-bot@chromium.org | 57f74e0 | 2014-03-25 23:31:33 +0000 | [diff] [blame^] | 46 | |
| 47 | bool vizMode = fDebugCanvas->getMegaVizMode(); |
| 48 | fDebugCanvas->setMegaVizMode(false); |
| 49 | bool overDraw = fDebugCanvas->getOverdrawViz(); |
| 50 | fDebugCanvas->setOverdrawViz(false); |
| 51 | int saveCount = fDebugCanvas->getOutstandingSaveCount(); |
| 52 | fDebugCanvas->setOutstandingSaveCount(0); |
| 53 | |
chudy@google.com | 607357f | 2012-08-07 16:12:23 +0000 | [diff] [blame] | 54 | fDebugCanvas->draw(canvas); |
commit-bot@chromium.org | 57f74e0 | 2014-03-25 23:31:33 +0000 | [diff] [blame^] | 55 | |
| 56 | int temp = fDebugCanvas->getOutstandingSaveCount(); |
| 57 | for (int i = 0; i < temp; ++i) { |
| 58 | canvas->restore(); |
| 59 | } |
| 60 | |
| 61 | fDebugCanvas->setMegaVizMode(vizMode); |
| 62 | fDebugCanvas->setOverdrawViz(overDraw); |
| 63 | fDebugCanvas->setOutstandingSaveCount(saveCount); |
| 64 | |
robertphillips@google.com | 25bc2f8 | 2013-01-22 18:03:56 +0000 | [diff] [blame] | 65 | newPicture->endRecording(); |
| 66 | return newPicture; |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 67 | } |
borenet@google.com | 2d9dbd4 | 2013-03-12 13:07:40 +0000 | [diff] [blame] | 68 | |
| 69 | void SkDebugger::getOverviewText(const SkTDArray<double>* typeTimes, |
| 70 | double totTime, |
robertphillips@google.com | e428f9b | 2013-03-12 15:33:40 +0000 | [diff] [blame] | 71 | SkString* overview, |
| 72 | int numRuns) { |
borenet@google.com | 2d9dbd4 | 2013-03-12 13:07:40 +0000 | [diff] [blame] | 73 | const SkTDArray<SkDrawCommand*>& commands = this->getDrawCommands(); |
| 74 | |
| 75 | SkTDArray<int> counts; |
| 76 | counts.setCount(LAST_DRAWTYPE_ENUM+1); |
| 77 | for (int i = 0; i < LAST_DRAWTYPE_ENUM+1; ++i) { |
| 78 | counts[i] = 0; |
| 79 | } |
| 80 | |
| 81 | for (int i = 0; i < commands.count(); i++) { |
| 82 | counts[commands[i]->getType()]++; |
| 83 | } |
| 84 | |
| 85 | overview->reset(); |
| 86 | int total = 0; |
| 87 | #ifdef SK_DEBUG |
| 88 | double totPercent = 0, tempSum = 0; |
| 89 | #endif |
| 90 | for (int i = 0; i < LAST_DRAWTYPE_ENUM+1; ++i) { |
| 91 | if (0 == counts[i]) { |
| 92 | // if there were no commands of this type then they should've consumed no time |
| 93 | SkASSERT(NULL == typeTimes || 0.0 == (*typeTimes)[i]); |
| 94 | continue; |
| 95 | } |
| 96 | |
| 97 | overview->append(SkDrawCommand::GetCommandString((DrawType) i)); |
| 98 | overview->append(": "); |
robertphillips@google.com | e428f9b | 2013-03-12 15:33:40 +0000 | [diff] [blame] | 99 | overview->appendS32(counts[i]); |
robertphillips@google.com | 6d9c92b | 2013-05-23 13:21:18 +0000 | [diff] [blame] | 100 | if (NULL != typeTimes && totTime >= 0.0) { |
borenet@google.com | 2d9dbd4 | 2013-03-12 13:07:40 +0000 | [diff] [blame] | 101 | overview->append(" - "); |
robertphillips@google.com | e428f9b | 2013-03-12 15:33:40 +0000 | [diff] [blame] | 102 | overview->appendf("%.2f", (*typeTimes)[i]/(float)numRuns); |
borenet@google.com | 2d9dbd4 | 2013-03-12 13:07:40 +0000 | [diff] [blame] | 103 | overview->append("ms"); |
| 104 | overview->append(" - "); |
| 105 | double percent = 100.0*(*typeTimes)[i]/totTime; |
robertphillips@google.com | e428f9b | 2013-03-12 15:33:40 +0000 | [diff] [blame] | 106 | overview->appendf("%.2f", percent); |
borenet@google.com | 2d9dbd4 | 2013-03-12 13:07:40 +0000 | [diff] [blame] | 107 | overview->append("%"); |
| 108 | #ifdef SK_DEBUG |
| 109 | totPercent += percent; |
| 110 | tempSum += (*typeTimes)[i]; |
| 111 | #endif |
| 112 | } |
| 113 | overview->append("<br/>"); |
| 114 | total += counts[i]; |
| 115 | } |
| 116 | #ifdef SK_DEBUG |
| 117 | if (NULL != typeTimes) { |
skia.committer@gmail.com | 91274b9 | 2013-03-13 07:01:04 +0000 | [diff] [blame] | 118 | SkASSERT(SkScalarNearlyEqual(SkDoubleToScalar(totPercent), |
robertphillips@google.com | e428f9b | 2013-03-12 15:33:40 +0000 | [diff] [blame] | 119 | SkDoubleToScalar(100.0))); |
skia.committer@gmail.com | 91274b9 | 2013-03-13 07:01:04 +0000 | [diff] [blame] | 120 | SkASSERT(SkScalarNearlyEqual(SkDoubleToScalar(tempSum), |
robertphillips@google.com | e428f9b | 2013-03-12 15:33:40 +0000 | [diff] [blame] | 121 | SkDoubleToScalar(totTime))); |
borenet@google.com | 2d9dbd4 | 2013-03-12 13:07:40 +0000 | [diff] [blame] | 122 | } |
| 123 | #endif |
| 124 | |
| 125 | if (totTime > 0.0) { |
| 126 | overview->append("Total Time: "); |
robertphillips@google.com | e428f9b | 2013-03-12 15:33:40 +0000 | [diff] [blame] | 127 | overview->appendf("%.2f", totTime/(float)numRuns); |
borenet@google.com | 2d9dbd4 | 2013-03-12 13:07:40 +0000 | [diff] [blame] | 128 | overview->append("ms"); |
| 129 | #ifdef SK_DEBUG |
| 130 | overview->append(" "); |
robertphillips@google.com | e428f9b | 2013-03-12 15:33:40 +0000 | [diff] [blame] | 131 | overview->appendScalar(SkDoubleToScalar(totPercent)); |
borenet@google.com | 2d9dbd4 | 2013-03-12 13:07:40 +0000 | [diff] [blame] | 132 | overview->append("% "); |
| 133 | #endif |
| 134 | overview->append("<br/>"); |
| 135 | } |
| 136 | |
| 137 | SkString totalStr; |
| 138 | totalStr.append("Total Draw Commands: "); |
robertphillips@google.com | e428f9b | 2013-03-12 15:33:40 +0000 | [diff] [blame] | 139 | totalStr.appendScalar(SkDoubleToScalar(total)); |
borenet@google.com | 2d9dbd4 | 2013-03-12 13:07:40 +0000 | [diff] [blame] | 140 | totalStr.append("<br/>"); |
| 141 | overview->insert(0, totalStr); |
| 142 | |
| 143 | overview->append("<br/>"); |
| 144 | overview->append("SkPicture Width: "); |
robertphillips@google.com | e428f9b | 2013-03-12 15:33:40 +0000 | [diff] [blame] | 145 | overview->appendS32(pictureWidth()); |
borenet@google.com | 2d9dbd4 | 2013-03-12 13:07:40 +0000 | [diff] [blame] | 146 | overview->append("px<br/>"); |
| 147 | overview->append("SkPicture Height: "); |
robertphillips@google.com | e428f9b | 2013-03-12 15:33:40 +0000 | [diff] [blame] | 148 | overview->appendS32(pictureHeight()); |
borenet@google.com | 2d9dbd4 | 2013-03-12 13:07:40 +0000 | [diff] [blame] | 149 | overview->append("px"); |
| 150 | } |