1 /*
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 "include/core/SkBitmap.h"
9 #include "include/core/SkPicture.h"
10 #include "include/core/SkPictureRecorder.h"
11 #include "include/core/SkStream.h"
12 #include "include/core/SkTime.h"
13 #include "src/core/SkPicturePriv.h"
14 #include "src/core/SkRecord.h"
15 #include "src/core/SkRecordDraw.h"
16 #include "src/core/SkRecordOpts.h"
17 #include "src/core/SkRecorder.h"
18 #include "tools/flags/CommandLineFlags.h"
19 #include <stdio.h>
20
21 static DEFINE_string2(skps, r, "", ".SKPs to dump.");
22 static DEFINE_string(match, "", "The usual filters on file names to dump.");
23 static DEFINE_bool2(optimize, O, false, "Run SkRecordOptimize before dumping.");
24 static DEFINE_bool(optimize2, false, "Run SkRecordOptimize2 before dumping.");
25 static DEFINE_int(tile, 1000000000, "Simulated tile size.");
26 static DEFINE_bool(timeWithCommand, false,
27 "If true, print time next to command, else in first column.");
28 static DEFINE_string2(write, w, "", "Write the (optimized) picture to the named file.");
29
30 class Dumper {
31 public:
Dumper(SkCanvas * canvas,int count)32 explicit Dumper(SkCanvas* canvas, int count)
33 : fDigits(0)
34 , fIndent(0)
35 , fIndex(0)
36 , fDraw(canvas, nullptr, nullptr, 0, nullptr)
37 {
38 while (count > 0) {
39 count /= 10;
40 fDigits++;
41 }
42 }
43
44 template <typename T>
operator ()(const T & command)45 void operator()(const T& command) {
46 auto start = SkTime::GetNSecs();
47 fDraw(command);
48 this->print(command, SkTime::GetNSecs() - start);
49 }
50
operator ()(const SkRecords::NoOp &)51 void operator()(const SkRecords::NoOp&) {
52 // Move on without printing anything.
53 }
54
55 template <typename T>
print(const T & command,double ns)56 void print(const T& command, double ns) {
57 this->printNameAndTime(command, ns);
58 }
59
print(const SkRecords::Restore & command,double ns)60 void print(const SkRecords::Restore& command, double ns) {
61 --fIndent;
62 this->printNameAndTime(command, ns);
63 }
64
print(const SkRecords::Save & command,double ns)65 void print(const SkRecords::Save& command, double ns) {
66 this->printNameAndTime(command, ns);
67 ++fIndent;
68 }
69
print(const SkRecords::SaveLayer & command,double ns)70 void print(const SkRecords::SaveLayer& command, double ns) {
71 this->printNameAndTime(command, ns);
72 ++fIndent;
73 }
74
print(const SkRecords::DrawPicture & command,double ns)75 void print(const SkRecords::DrawPicture& command, double ns) {
76 this->printNameAndTime(command, ns);
77
78 if (auto bp = SkPicturePriv::AsSkBigPicture(command.picture)) {
79 ++fIndent;
80
81 const SkRecord& record = *bp->record();
82 for (int i = 0; i < record.count(); i++) {
83 record.visit(i, *this);
84 }
85
86 --fIndent;
87 }
88 }
89
print(const SkRecords::DrawAnnotation & command,double ns)90 void print(const SkRecords::DrawAnnotation& command, double ns) {
91 int us = (int)(ns * 1e-3);
92 if (!FLAGS_timeWithCommand) {
93 printf("%6dus ", us);
94 }
95 printf("%*d ", fDigits, fIndex++);
96 for (int i = 0; i < fIndent; i++) {
97 printf(" ");
98 }
99 if (FLAGS_timeWithCommand) {
100 printf("%6dus ", us);
101 }
102 printf("DrawAnnotation [%g %g %g %g] %s\n",
103 command.rect.left(), command.rect.top(), command.rect.right(), command.rect.bottom(),
104 command.key.c_str());
105 }
106
107 private:
108 template <typename T>
printNameAndTime(const T & command,double ns)109 void printNameAndTime(const T& command, double ns) {
110 int us = (int)(ns * 1e-3);
111 if (!FLAGS_timeWithCommand) {
112 printf("%6dus ", us);
113 }
114 printf("%*d ", fDigits, fIndex++);
115 for (int i = 0; i < fIndent; i++) {
116 printf(" ");
117 }
118 if (FLAGS_timeWithCommand) {
119 printf("%6dus ", us);
120 }
121 puts(NameOf(command));
122 }
123
124 template <typename T>
NameOf(const T &)125 static const char* NameOf(const T&) {
126 #define CASE(U) case SkRecords::U##_Type: return #U;
127 switch (T::kType) { SK_RECORD_TYPES(CASE) }
128 #undef CASE
129 SkDEBUGFAIL("Unknown T");
130 return "Unknown T";
131 }
132
NameOf(const SkRecords::SaveLayer &)133 static const char* NameOf(const SkRecords::SaveLayer&) {
134 return "\x1b[31;1mSaveLayer\x1b[0m"; // Bold red.
135 }
136
137 int fDigits;
138 int fIndent;
139 int fIndex;
140 SkRecords::Draw fDraw;
141 };
142
main(int argc,char ** argv)143 int main(int argc, char** argv) {
144 CommandLineFlags::Parse(argc, argv);
145
146 for (int i = 0; i < FLAGS_skps.count(); i++) {
147 if (CommandLineFlags::ShouldSkip(FLAGS_match, FLAGS_skps[i])) {
148 continue;
149 }
150
151 std::unique_ptr<SkStream> stream = SkStream::MakeFromFile(FLAGS_skps[i]);
152 if (!stream) {
153 SkDebugf("Could not read %s.\n", FLAGS_skps[i]);
154 return 1;
155 }
156 sk_sp<SkPicture> src(SkPicture::MakeFromStream(stream.get()));
157 if (!src) {
158 SkDebugf("Could not read %s as an SkPicture.\n", FLAGS_skps[i]);
159 return 1;
160 }
161 const int w = SkScalarCeilToInt(src->cullRect().width());
162 const int h = SkScalarCeilToInt(src->cullRect().height());
163
164 SkRecord record;
165 SkRecorder rec(&record, w, h);
166 src->playback(&rec);
167
168 if (FLAGS_optimize) {
169 SkRecordOptimize(&record);
170 }
171 if (FLAGS_optimize2) {
172 SkRecordOptimize2(&record);
173 }
174
175 SkBitmap bitmap;
176 bitmap.allocN32Pixels(w, h);
177 SkCanvas canvas(bitmap);
178 canvas.clipRect(SkRect::MakeWH(SkIntToScalar(FLAGS_tile),
179 SkIntToScalar(FLAGS_tile)));
180
181 printf("%s %s\n", FLAGS_optimize ? "optimized" : "not-optimized", FLAGS_skps[i]);
182
183 Dumper dumper(&canvas, record.count());
184 for (int j = 0; j < record.count(); j++) {
185 record.visit(j, dumper);
186 }
187
188 if (FLAGS_write.count() > 0) {
189 SkPictureRecorder r;
190 SkRecordDraw(record,
191 r.beginRecording(SkRect::MakeIWH(w, h)),
192 nullptr,
193 nullptr,
194 0,
195 nullptr,
196 nullptr);
197 sk_sp<SkPicture> dst(r.finishRecordingAsPicture());
198 SkFILEWStream ostream(FLAGS_write[0]);
199 dst->serialize(&ostream);
200 }
201 }
202
203 return 0;
204 }
205