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