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); |
| 34 | picture->draw(fDebugCanvas); |
| 35 | fIndex = fDebugCanvas->getSize() - 1; |
| 36 | SkRefCnt_SafeAssign(fPicture, picture); |
| 37 | } |
| 38 | |
robertphillips@google.com | 25bc2f8 | 2013-01-22 18:03:56 +0000 | [diff] [blame] | 39 | SkPicture* 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.com | 607357f | 2012-08-07 16:12:23 +0000 | [diff] [blame] | 44 | fDebugCanvas->draw(canvas); |
robertphillips@google.com | 25bc2f8 | 2013-01-22 18:03:56 +0000 | [diff] [blame] | 45 | newPicture->endRecording(); |
| 46 | return newPicture; |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 47 | } |
borenet@google.com | 2d9dbd4 | 2013-03-12 13:07:40 +0000 | [diff] [blame] | 48 | |
| 49 | void SkDebugger::getOverviewText(const SkTDArray<double>* typeTimes, |
| 50 | double totTime, |
robertphillips@google.com | e428f9b | 2013-03-12 15:33:40 +0000 | [diff] [blame] | 51 | SkString* overview, |
| 52 | int numRuns) { |
borenet@google.com | 2d9dbd4 | 2013-03-12 13:07:40 +0000 | [diff] [blame] | 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(": "); |
robertphillips@google.com | e428f9b | 2013-03-12 15:33:40 +0000 | [diff] [blame] | 79 | overview->appendS32(counts[i]); |
robertphillips@google.com | 6d9c92b | 2013-05-23 13:21:18 +0000 | [diff] [blame] | 80 | if (NULL != typeTimes && totTime >= 0.0) { |
borenet@google.com | 2d9dbd4 | 2013-03-12 13:07:40 +0000 | [diff] [blame] | 81 | overview->append(" - "); |
robertphillips@google.com | e428f9b | 2013-03-12 15:33:40 +0000 | [diff] [blame] | 82 | overview->appendf("%.2f", (*typeTimes)[i]/(float)numRuns); |
borenet@google.com | 2d9dbd4 | 2013-03-12 13:07:40 +0000 | [diff] [blame] | 83 | overview->append("ms"); |
| 84 | overview->append(" - "); |
| 85 | double percent = 100.0*(*typeTimes)[i]/totTime; |
robertphillips@google.com | e428f9b | 2013-03-12 15:33:40 +0000 | [diff] [blame] | 86 | overview->appendf("%.2f", percent); |
borenet@google.com | 2d9dbd4 | 2013-03-12 13:07:40 +0000 | [diff] [blame] | 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) { |
skia.committer@gmail.com | 91274b9 | 2013-03-13 07:01:04 +0000 | [diff] [blame] | 98 | SkASSERT(SkScalarNearlyEqual(SkDoubleToScalar(totPercent), |
robertphillips@google.com | e428f9b | 2013-03-12 15:33:40 +0000 | [diff] [blame] | 99 | SkDoubleToScalar(100.0))); |
skia.committer@gmail.com | 91274b9 | 2013-03-13 07:01:04 +0000 | [diff] [blame] | 100 | SkASSERT(SkScalarNearlyEqual(SkDoubleToScalar(tempSum), |
robertphillips@google.com | e428f9b | 2013-03-12 15:33:40 +0000 | [diff] [blame] | 101 | SkDoubleToScalar(totTime))); |
borenet@google.com | 2d9dbd4 | 2013-03-12 13:07:40 +0000 | [diff] [blame] | 102 | } |
| 103 | #endif |
| 104 | |
| 105 | if (totTime > 0.0) { |
| 106 | overview->append("Total Time: "); |
robertphillips@google.com | e428f9b | 2013-03-12 15:33:40 +0000 | [diff] [blame] | 107 | overview->appendf("%.2f", totTime/(float)numRuns); |
borenet@google.com | 2d9dbd4 | 2013-03-12 13:07:40 +0000 | [diff] [blame] | 108 | overview->append("ms"); |
| 109 | #ifdef SK_DEBUG |
| 110 | overview->append(" "); |
robertphillips@google.com | e428f9b | 2013-03-12 15:33:40 +0000 | [diff] [blame] | 111 | overview->appendScalar(SkDoubleToScalar(totPercent)); |
borenet@google.com | 2d9dbd4 | 2013-03-12 13:07:40 +0000 | [diff] [blame] | 112 | overview->append("% "); |
| 113 | #endif |
| 114 | overview->append("<br/>"); |
| 115 | } |
| 116 | |
| 117 | SkString totalStr; |
| 118 | totalStr.append("Total Draw Commands: "); |
robertphillips@google.com | e428f9b | 2013-03-12 15:33:40 +0000 | [diff] [blame] | 119 | totalStr.appendScalar(SkDoubleToScalar(total)); |
borenet@google.com | 2d9dbd4 | 2013-03-12 13:07:40 +0000 | [diff] [blame] | 120 | totalStr.append("<br/>"); |
| 121 | overview->insert(0, totalStr); |
| 122 | |
| 123 | overview->append("<br/>"); |
| 124 | overview->append("SkPicture Width: "); |
robertphillips@google.com | e428f9b | 2013-03-12 15:33:40 +0000 | [diff] [blame] | 125 | overview->appendS32(pictureWidth()); |
borenet@google.com | 2d9dbd4 | 2013-03-12 13:07:40 +0000 | [diff] [blame] | 126 | overview->append("px<br/>"); |
| 127 | overview->append("SkPicture Height: "); |
robertphillips@google.com | e428f9b | 2013-03-12 15:33:40 +0000 | [diff] [blame] | 128 | overview->appendS32(pictureHeight()); |
borenet@google.com | 2d9dbd4 | 2013-03-12 13:07:40 +0000 | [diff] [blame] | 129 | overview->append("px"); |
| 130 | } |