blob: e7d76f05155fe8ef6cff396b38a7fadceabb8403 [file] [log] [blame]
commit-bot@chromium.org85fd1932014-05-15 16:10:37 +00001/*
2 * Copyright 2014 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include <stdio.h>
9
10#include "SkRecord.h"
11#include "SkRecordDraw.h"
12
commit-bot@chromium.org85fd1932014-05-15 16:10:37 +000013#include "DumpRecord.h"
mtklein2f2903d2015-11-18 11:06:37 -080014#include "SkTime.h"
commit-bot@chromium.org85fd1932014-05-15 16:10:37 +000015
16namespace {
17
18class Dumper {
19public:
20 explicit Dumper(SkCanvas* canvas, int count, bool timeWithCommand)
21 : fDigits(0)
22 , fIndent(0)
mtklein00f30bd2014-09-02 12:03:31 -070023 , fIndex(0)
halcanary96fcdcc2015-08-27 07:41:13 -070024 , fDraw(canvas, nullptr, nullptr, 0, nullptr)
commit-bot@chromium.org85fd1932014-05-15 16:10:37 +000025 , fTimeWithCommand(timeWithCommand) {
26 while (count > 0) {
27 count /= 10;
28 fDigits++;
29 }
30 }
31
commit-bot@chromium.org85fd1932014-05-15 16:10:37 +000032 template <typename T>
33 void operator()(const T& command) {
mtklein2f2903d2015-11-18 11:06:37 -080034 auto start = SkTime::GetNSecs();
35 fDraw(command);
36 this->print(command, SkTime::GetNSecs() - start);
commit-bot@chromium.org85fd1932014-05-15 16:10:37 +000037 }
38
39 void operator()(const SkRecords::NoOp&) {
40 // Move on without printing anything.
41 }
42
43 template <typename T>
mtklein2f2903d2015-11-18 11:06:37 -080044 void print(const T& command, double ns) {
45 this->printNameAndTime(command, ns);
commit-bot@chromium.org85fd1932014-05-15 16:10:37 +000046 }
47
mtklein2f2903d2015-11-18 11:06:37 -080048 void print(const SkRecords::Restore& command, double ns) {
commit-bot@chromium.org85fd1932014-05-15 16:10:37 +000049 --fIndent;
mtklein2f2903d2015-11-18 11:06:37 -080050 this->printNameAndTime(command, ns);
commit-bot@chromium.org85fd1932014-05-15 16:10:37 +000051 }
52
mtklein2f2903d2015-11-18 11:06:37 -080053 void print(const SkRecords::Save& command, double ns) {
54 this->printNameAndTime(command, ns);
commit-bot@chromium.org85fd1932014-05-15 16:10:37 +000055 ++fIndent;
56 }
57
mtklein2f2903d2015-11-18 11:06:37 -080058 void print(const SkRecords::SaveLayer& command, double ns) {
59 this->printNameAndTime(command, ns);
commit-bot@chromium.org85fd1932014-05-15 16:10:37 +000060 ++fIndent;
61 }
62
mtklein3425cba2016-01-20 08:46:40 -080063 void print(const SkRecords::DrawPicture& command, double ns) {
64 this->printNameAndTime(command, ns);
65
66 if (auto bp = command.picture->asSkBigPicture()) {
67 ++fIndent;
68
69 const SkRecord& record = *bp->record();
70 for (int i = 0; i < record.count(); i++) {
71 record.visit<void>(i, *this);
72 }
73
74 --fIndent;
75 }
76 }
77
commit-bot@chromium.org85fd1932014-05-15 16:10:37 +000078private:
79 template <typename T>
mtklein2f2903d2015-11-18 11:06:37 -080080 void printNameAndTime(const T& command, double ns) {
81 int us = (int)(ns * 1e-3);
commit-bot@chromium.org85fd1932014-05-15 16:10:37 +000082 if (!fTimeWithCommand) {
mtklein2f2903d2015-11-18 11:06:37 -080083 printf("%6dus ", us);
commit-bot@chromium.org85fd1932014-05-15 16:10:37 +000084 }
mtklein00f30bd2014-09-02 12:03:31 -070085 printf("%*d ", fDigits, fIndex++);
commit-bot@chromium.org85fd1932014-05-15 16:10:37 +000086 for (int i = 0; i < fIndent; i++) {
mtklein2f2903d2015-11-18 11:06:37 -080087 printf(" ");
commit-bot@chromium.org85fd1932014-05-15 16:10:37 +000088 }
89 if (fTimeWithCommand) {
mtklein2f2903d2015-11-18 11:06:37 -080090 printf("%6dus ", us);
commit-bot@chromium.org85fd1932014-05-15 16:10:37 +000091 }
92 puts(NameOf(command));
93 }
94
95 template <typename T>
96 static const char* NameOf(const T&) {
97 #define CASE(U) case SkRecords::U##_Type: return #U;
98 switch(T::kType) { SK_RECORD_TYPES(CASE); }
99 #undef CASE
100 SkDEBUGFAIL("Unknown T");
101 return "Unknown T";
102 }
103
104 static const char* NameOf(const SkRecords::SaveLayer&) {
105 return "\x1b[31;1mSaveLayer\x1b[0m"; // Bold red.
106 }
107
108 int fDigits;
109 int fIndent;
mtklein00f30bd2014-09-02 12:03:31 -0700110 int fIndex;
commit-bot@chromium.org85fd1932014-05-15 16:10:37 +0000111 SkRecords::Draw fDraw;
112 const bool fTimeWithCommand;
113};
114
115} // namespace
116
117void DumpRecord(const SkRecord& record,
118 SkCanvas* canvas,
119 bool timeWithCommand) {
mtklein00f30bd2014-09-02 12:03:31 -0700120 Dumper dumper(canvas, record.count(), timeWithCommand);
mtkleinc6ad06a2015-08-19 09:51:00 -0700121 for (int i = 0; i < record.count(); i++) {
mtklein00f30bd2014-09-02 12:03:31 -0700122 record.visit<void>(i, dumper);
commit-bot@chromium.org85fd1932014-05-15 16:10:37 +0000123 }
124}