• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
18 #include <stdio.h>
19 
20 DEFINE_string2(skps, r, "", ".SKPs to dump.");
21 DEFINE_string(match, "", "The usual filters on file names to dump.");
22 DEFINE_bool2(optimize, O, false, "Run SkRecordOptimize before dumping.");
23 DEFINE_bool(optimize2, false, "Run SkRecordOptimize2 before dumping.");
24 DEFINE_int32(tile, 1000000000, "Simulated tile size.");
25 DEFINE_bool(timeWithCommand, false, "If true, print time next to command, else in first column.");
26 DEFINE_string2(write, w, "", "Write the (optimized) picture to the named file.");
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         const int w = SkScalarCeilToInt(src->cullRect().width());
59         const int h = SkScalarCeilToInt(src->cullRect().height());
60 
61         SkRecord record;
62         SkRecorder canvas(&record, w, h);
63         src->playback(&canvas);
64 
65         if (FLAGS_optimize) {
66             SkRecordOptimize(&record);
67         }
68         if (FLAGS_optimize2) {
69             SkRecordOptimize2(&record);
70         }
71 
72         dump(FLAGS_skps[i], w, h, record);
73 
74         if (FLAGS_write.count() > 0) {
75             SkPictureRecorder r;
76             SkRecordDraw(record,
77                          r.beginRecording(SkRect::MakeIWH(w, h)),
78                          nullptr,
79                          nullptr,
80                          0,
81                          nullptr,
82                          nullptr);
83             sk_sp<SkPicture> dst(r.finishRecordingAsPicture());
84             SkFILEWStream ostream(FLAGS_write[0]);
85             dst->serialize(&ostream);
86         }
87     }
88 
89     return 0;
90 }
91