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 "SkBitmap.h"
10 #include "SkCommandLineFlags.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 
27 static void dump(const char* name, int w, int h, const SkRecord& record) {
28     SkBitmap bitmap;
29     bitmap.allocN32Pixels(w, h);
30     SkCanvas canvas(bitmap);
31     canvas.clipRect(SkRect::MakeWH(SkIntToScalar(FLAGS_tile),
32                                    SkIntToScalar(FLAGS_tile)));
33 
34     printf("%s %s\n", FLAGS_optimize ? "optimized" : "not-optimized", name);
35 
36     DumpRecord(record, &canvas, FLAGS_timeWithCommand);
37 }
38 
39 int main(int argc, char** argv) {
40     SkCommandLineFlags::Parse(argc, argv);
41 
42     for (int i = 0; i < FLAGS_skps.count(); i++) {
43         if (SkCommandLineFlags::ShouldSkip(FLAGS_match, FLAGS_skps[i])) {
44             continue;
45         }
46 
47         std::unique_ptr<SkStream> stream = SkStream::MakeFromFile(FLAGS_skps[i]);
48         if (!stream) {
49             SkDebugf("Could not read %s.\n", FLAGS_skps[i]);
50             return 1;
51         }
52         sk_sp<SkPicture> src(SkPicture::MakeFromStream(stream.get()));
53         if (!src) {
54             SkDebugf("Could not read %s as an SkPicture.\n", FLAGS_skps[i]);
55             return 1;
56         }
57         const int w = SkScalarCeilToInt(src->cullRect().width());
58         const int h = SkScalarCeilToInt(src->cullRect().height());
59 
60         SkRecord record;
61         SkRecorder canvas(&record, w, h);
62         src->playback(&canvas);
63 
64         if (FLAGS_optimize) {
65             SkRecordOptimize(&record);
66         }
67         if (FLAGS_optimize2) {
68             SkRecordOptimize2(&record);
69         }
70 
71         dump(FLAGS_skps[i], w, h, record);
72 
73         if (FLAGS_write.count() > 0) {
74             SkPictureRecorder r;
75             SkRecordDraw(record,
76                          r.beginRecording(SkRect::MakeIWH(w, h)),
77                          nullptr,
78                          nullptr,
79                          0,
80                          nullptr,
81                          nullptr);
82             sk_sp<SkPicture> dst(r.finishRecordingAsPicture());
83             SkFILEWStream ostream(FLAGS_write[0]);
84             dst->serialize(&ostream);
85         }
86     }
87 
88     return 0;
89 }
90