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 "DumpRecord.h"
9 #include "SkCommandLineFlags.h"
10 #include "SkDeferredCanvas.h"
11 #include "SkPicture.h"
12 #include "SkPictureRecorder.h"
13 #include "SkRecordDraw.h"
14 #include "SkRecordOpts.h"
15 #include "SkRecorder.h"
16 #include "SkStream.h"
17 #include <stdio.h>
18
19 DEFINE_string2(skps, r, "", ".SKPs to dump.");
20 DEFINE_string(match, "", "The usual filters on file names to dump.");
21 DEFINE_bool2(optimize, O, false, "Run SkRecordOptimize before dumping.");
22 DEFINE_bool(optimize2, false, "Run SkRecordOptimize2 before dumping.");
23 DEFINE_int32(tile, 1000000000, "Simulated tile size.");
24 DEFINE_bool(timeWithCommand, false, "If true, print time next to command, else in first column.");
25 DEFINE_string2(write, w, "", "Write the (optimized) picture to the named file.");
26 DEFINE_bool(defer, false, "Defer clips and translates");
27
dump(const char * name,int w,int h,const SkRecord & record)28 static void dump(const char* name, int w, int h, const SkRecord& record) {
29 SkBitmap bitmap;
30 bitmap.allocN32Pixels(w, h);
31 SkCanvas canvas(bitmap);
32 canvas.clipRect(SkRect::MakeWH(SkIntToScalar(FLAGS_tile),
33 SkIntToScalar(FLAGS_tile)));
34
35 printf("%s %s\n", FLAGS_optimize ? "optimized" : "not-optimized", name);
36
37 DumpRecord(record, &canvas, FLAGS_timeWithCommand);
38 }
39
main(int argc,char ** argv)40 int main(int argc, char** argv) {
41 SkCommandLineFlags::Parse(argc, argv);
42
43 for (int i = 0; i < FLAGS_skps.count(); i++) {
44 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, FLAGS_skps[i])) {
45 continue;
46 }
47
48 std::unique_ptr<SkStream> stream = SkStream::MakeFromFile(FLAGS_skps[i]);
49 if (!stream) {
50 SkDebugf("Could not read %s.\n", FLAGS_skps[i]);
51 return 1;
52 }
53 sk_sp<SkPicture> src(SkPicture::MakeFromStream(stream.get()));
54 if (!src) {
55 SkDebugf("Could not read %s as an SkPicture.\n", FLAGS_skps[i]);
56 return 1;
57 }
58 if (FLAGS_defer) {
59 SkPictureRecorder recorder;
60 SkDeferredCanvas deferred(recorder.beginRecording(src->cullRect()),
61 SkDeferredCanvas::kEager);
62 src->playback(&deferred);
63 src = recorder.finishRecordingAsPicture();
64 }
65 const int w = SkScalarCeilToInt(src->cullRect().width());
66 const int h = SkScalarCeilToInt(src->cullRect().height());
67
68 SkRecord record;
69 SkRecorder canvas(&record, w, h);
70 src->playback(&canvas);
71
72 if (FLAGS_optimize) {
73 SkRecordOptimize(&record);
74 }
75 if (FLAGS_optimize2) {
76 SkRecordOptimize2(&record);
77 }
78
79 dump(FLAGS_skps[i], w, h, record);
80
81 if (FLAGS_write.count() > 0) {
82 SkPictureRecorder r;
83 SkRecordDraw(record,
84 r.beginRecording(SkRect::MakeIWH(w, h)),
85 nullptr,
86 nullptr,
87 0,
88 nullptr,
89 nullptr);
90 sk_sp<SkPicture> dst(r.finishRecordingAsPicture());
91 SkFILEWStream ostream(FLAGS_write[0]);
92 dst->serialize(&ostream);
93 }
94 }
95
96 return 0;
97 }
98