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 "SkPicture.h"
11#include "SkPictureRecorder.h"
12#include "SkRecordDraw.h"
13#include "SkRecordOpts.h"
14#include "SkRecorder.h"
15#include "SkStream.h"
16#include <stdio.h>
17
18DEFINE_string2(skps, r, "", ".SKPs to dump.");
19DEFINE_string(match, "", "The usual filters on file names to dump.");
20DEFINE_bool2(optimize, O, false, "Run SkRecordOptimize before dumping.");
21DEFINE_bool(optimize2, false, "Run SkRecordOptimize2 before dumping.");
22DEFINE_int32(tile, 1000000000, "Simulated tile size.");
23DEFINE_bool(timeWithCommand, false, "If true, print time next to command, else in first column.");
24DEFINE_string2(write, w, "", "Write the (optimized) picture to the named file.");
25
26static void dump(const char* name, int w, int h, const SkRecord& record) {
27    SkBitmap bitmap;
28    bitmap.allocN32Pixels(w, h);
29    SkCanvas canvas(bitmap);
30    canvas.clipRect(SkRect::MakeWH(SkIntToScalar(FLAGS_tile),
31                                   SkIntToScalar(FLAGS_tile)));
32
33    printf("%s %s\n", FLAGS_optimize ? "optimized" : "not-optimized", name);
34
35    DumpRecord(record, &canvas, FLAGS_timeWithCommand);
36}
37
38int tool_main(int argc, char** argv);
39int tool_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        SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(FLAGS_skps[i]));
48        if (!stream) {
49            SkDebugf("Could not read %s.\n", FLAGS_skps[i]);
50            return 1;
51        }
52        SkAutoTUnref<SkPicture> src(SkPicture::CreateFromStream(stream));
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            SkAutoTUnref<SkPicture> dst(r.endRecording());
83            SkFILEWStream ostream(FLAGS_write[0]);
84            dst->serialize(&ostream);
85        }
86    }
87
88    return 0;
89}
90
91#if !defined SK_BUILD_FOR_IOS
92int main(int argc, char * const argv[]) {
93    return tool_main(argc, (char**) argv);
94}
95#endif
96