blob: fb2fbb0eb4092b0c5908baa7ec5c3df232a882e1 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "src/core/SkPicturePriv.h"
11#include "src/core/SkRecord.h"
12#include "src/core/SkRecordDraw.h"
commit-bot@chromium.org85fd1932014-05-15 16:10:37 +000013
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "include/core/SkTime.h"
15#include "tools/DumpRecord.h"
commit-bot@chromium.org85fd1932014-05-15 16:10:37 +000016
17namespace {
18
19class Dumper {
20public:
21 explicit Dumper(SkCanvas* canvas, int count, bool timeWithCommand)
22 : fDigits(0)
23 , fIndent(0)
mtklein00f30bd2014-09-02 12:03:31 -070024 , fIndex(0)
halcanary96fcdcc2015-08-27 07:41:13 -070025 , fDraw(canvas, nullptr, nullptr, 0, nullptr)
commit-bot@chromium.org85fd1932014-05-15 16:10:37 +000026 , fTimeWithCommand(timeWithCommand) {
27 while (count > 0) {
28 count /= 10;
29 fDigits++;
30 }
31 }
32
commit-bot@chromium.org85fd1932014-05-15 16:10:37 +000033 template <typename T>
34 void operator()(const T& command) {
mtklein2f2903d2015-11-18 11:06:37 -080035 auto start = SkTime::GetNSecs();
36 fDraw(command);
37 this->print(command, SkTime::GetNSecs() - start);
commit-bot@chromium.org85fd1932014-05-15 16:10:37 +000038 }
39
40 void operator()(const SkRecords::NoOp&) {
41 // Move on without printing anything.
42 }
43
44 template <typename T>
mtklein2f2903d2015-11-18 11:06:37 -080045 void print(const T& command, double ns) {
46 this->printNameAndTime(command, ns);
commit-bot@chromium.org85fd1932014-05-15 16:10:37 +000047 }
48
mtklein2f2903d2015-11-18 11:06:37 -080049 void print(const SkRecords::Restore& command, double ns) {
commit-bot@chromium.org85fd1932014-05-15 16:10:37 +000050 --fIndent;
mtklein2f2903d2015-11-18 11:06:37 -080051 this->printNameAndTime(command, ns);
commit-bot@chromium.org85fd1932014-05-15 16:10:37 +000052 }
53
mtklein2f2903d2015-11-18 11:06:37 -080054 void print(const SkRecords::Save& command, double ns) {
55 this->printNameAndTime(command, ns);
commit-bot@chromium.org85fd1932014-05-15 16:10:37 +000056 ++fIndent;
57 }
58
mtklein2f2903d2015-11-18 11:06:37 -080059 void print(const SkRecords::SaveLayer& command, double ns) {
60 this->printNameAndTime(command, ns);
commit-bot@chromium.org85fd1932014-05-15 16:10:37 +000061 ++fIndent;
62 }
63
mtklein3425cba2016-01-20 08:46:40 -080064 void print(const SkRecords::DrawPicture& command, double ns) {
65 this->printNameAndTime(command, ns);
66
Cary Clarkefd99cc2018-06-11 16:25:43 -040067 if (auto bp = SkPicturePriv::AsSkBigPicture(command.picture)) {
mtklein3425cba2016-01-20 08:46:40 -080068 ++fIndent;
69
70 const SkRecord& record = *bp->record();
71 for (int i = 0; i < record.count(); i++) {
mtklein343a63d2016-03-22 11:46:53 -070072 record.visit(i, *this);
mtklein3425cba2016-01-20 08:46:40 -080073 }
74
75 --fIndent;
76 }
77 }
78
Mike Reed5cb76172016-10-05 14:52:50 -040079#if 1
reedbabc3de2016-07-08 08:43:27 -070080 void print(const SkRecords::DrawAnnotation& command, double ns) {
81 int us = (int)(ns * 1e-3);
82 if (!fTimeWithCommand) {
83 printf("%6dus ", us);
84 }
85 printf("%*d ", fDigits, fIndex++);
86 for (int i = 0; i < fIndent; i++) {
87 printf(" ");
88 }
89 if (fTimeWithCommand) {
90 printf("%6dus ", us);
91 }
92 printf("DrawAnnotation [%g %g %g %g] %s\n",
93 command.rect.left(), command.rect.top(), command.rect.right(), command.rect.bottom(),
94 command.key.c_str());
95 }
96#endif
97
commit-bot@chromium.org85fd1932014-05-15 16:10:37 +000098private:
99 template <typename T>
mtklein2f2903d2015-11-18 11:06:37 -0800100 void printNameAndTime(const T& command, double ns) {
101 int us = (int)(ns * 1e-3);
commit-bot@chromium.org85fd1932014-05-15 16:10:37 +0000102 if (!fTimeWithCommand) {
mtklein2f2903d2015-11-18 11:06:37 -0800103 printf("%6dus ", us);
commit-bot@chromium.org85fd1932014-05-15 16:10:37 +0000104 }
mtklein00f30bd2014-09-02 12:03:31 -0700105 printf("%*d ", fDigits, fIndex++);
commit-bot@chromium.org85fd1932014-05-15 16:10:37 +0000106 for (int i = 0; i < fIndent; i++) {
mtklein2f2903d2015-11-18 11:06:37 -0800107 printf(" ");
commit-bot@chromium.org85fd1932014-05-15 16:10:37 +0000108 }
109 if (fTimeWithCommand) {
mtklein2f2903d2015-11-18 11:06:37 -0800110 printf("%6dus ", us);
commit-bot@chromium.org85fd1932014-05-15 16:10:37 +0000111 }
112 puts(NameOf(command));
113 }
114
115 template <typename T>
116 static const char* NameOf(const T&) {
117 #define CASE(U) case SkRecords::U##_Type: return #U;
Brian Salomon23356442018-11-30 15:33:19 -0500118 switch (T::kType) { SK_RECORD_TYPES(CASE) }
119#undef CASE
commit-bot@chromium.org85fd1932014-05-15 16:10:37 +0000120 SkDEBUGFAIL("Unknown T");
121 return "Unknown T";
122 }
123
124 static const char* NameOf(const SkRecords::SaveLayer&) {
125 return "\x1b[31;1mSaveLayer\x1b[0m"; // Bold red.
126 }
127
128 int fDigits;
129 int fIndent;
mtklein00f30bd2014-09-02 12:03:31 -0700130 int fIndex;
commit-bot@chromium.org85fd1932014-05-15 16:10:37 +0000131 SkRecords::Draw fDraw;
132 const bool fTimeWithCommand;
133};
134
135} // namespace
136
137void DumpRecord(const SkRecord& record,
138 SkCanvas* canvas,
139 bool timeWithCommand) {
mtklein00f30bd2014-09-02 12:03:31 -0700140 Dumper dumper(canvas, record.count(), timeWithCommand);
mtkleinc6ad06a2015-08-19 09:51:00 -0700141 for (int i = 0; i < record.count(); i++) {
mtklein343a63d2016-03-22 11:46:53 -0700142 record.visit(i, dumper);
commit-bot@chromium.org85fd1932014-05-15 16:10:37 +0000143 }
144}