blob: 2d055169c14f7b28f02a4ba5e79983d48fc0c00b [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"
mtklein9ac68ee2014-06-20 11:29:20 -070014#include "Timer.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)
reed6be2aa92014-11-18 11:08:05 -080024 , fDraw(canvas, NULL, 0, NULL)
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) {
mtklein9ac68ee2014-06-20 11:29:20 -070034 Timer timer;
commit-bot@chromium.org85fd1932014-05-15 16:10:37 +000035 timer.start();
36 fDraw(command);
37 timer.end();
38
39 this->print(command, timer.fCpu);
40 }
41
42 void operator()(const SkRecords::NoOp&) {
43 // Move on without printing anything.
44 }
45
46 template <typename T>
47 void print(const T& command, double time) {
48 this->printNameAndTime(command, time);
49 }
50
51 void print(const SkRecords::Restore& command, double time) {
52 --fIndent;
53 this->printNameAndTime(command, time);
54 }
55
56 void print(const SkRecords::Save& command, double time) {
57 this->printNameAndTime(command, time);
58 ++fIndent;
59 }
60
61 void print(const SkRecords::SaveLayer& command, double time) {
62 this->printNameAndTime(command, time);
63 ++fIndent;
64 }
65
66private:
67 template <typename T>
68 void printNameAndTime(const T& command, double time) {
69 if (!fTimeWithCommand) {
70 printf("%6.1f ", time * 1000);
71 }
mtklein00f30bd2014-09-02 12:03:31 -070072 printf("%*d ", fDigits, fIndex++);
commit-bot@chromium.org85fd1932014-05-15 16:10:37 +000073 for (int i = 0; i < fIndent; i++) {
74 putchar('\t');
75 }
76 if (fTimeWithCommand) {
77 printf("%6.1f ", time * 1000);
78 }
79 puts(NameOf(command));
80 }
81
82 template <typename T>
83 static const char* NameOf(const T&) {
84 #define CASE(U) case SkRecords::U##_Type: return #U;
85 switch(T::kType) { SK_RECORD_TYPES(CASE); }
86 #undef CASE
87 SkDEBUGFAIL("Unknown T");
88 return "Unknown T";
89 }
90
91 static const char* NameOf(const SkRecords::SaveLayer&) {
92 return "\x1b[31;1mSaveLayer\x1b[0m"; // Bold red.
93 }
94
95 int fDigits;
96 int fIndent;
mtklein00f30bd2014-09-02 12:03:31 -070097 int fIndex;
commit-bot@chromium.org85fd1932014-05-15 16:10:37 +000098 SkRecords::Draw fDraw;
99 const bool fTimeWithCommand;
100};
101
102} // namespace
103
104void DumpRecord(const SkRecord& record,
105 SkCanvas* canvas,
106 bool timeWithCommand) {
mtklein00f30bd2014-09-02 12:03:31 -0700107 Dumper dumper(canvas, record.count(), timeWithCommand);
108 for (unsigned i = 0; i < record.count(); i++) {
109 record.visit<void>(i, dumper);
commit-bot@chromium.org85fd1932014-05-15 16:10:37 +0000110 }
111}