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 "SkDeferredCanvas.h"
12#include "SkPicture.h"
13#include "SkPictureRecorder.h"
14#include "SkRecordDraw.h"
15#include "SkRecordOpts.h"
16#include "SkRecorder.h"
17#include "SkStream.h"
18#include <stdio.h>
19
20DEFINE_string2(skps, r, "", ".SKPs to dump.");
21DEFINE_string(match, "", "The usual filters on file names to dump.");
22DEFINE_bool2(optimize, O, false, "Run SkRecordOptimize before dumping.");
23DEFINE_bool(optimize2, false, "Run SkRecordOptimize2 before dumping.");
24DEFINE_int32(tile, 1000000000, "Simulated tile size.");
25DEFINE_bool(timeWithCommand, false, "If true, print time next to command, else in first column.");
26DEFINE_string2(write, w, "", "Write the (optimized) picture to the named file.");
27DEFINE_bool(defer, false, "Defer clips and translates");
28
29static 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
41int main(int argc, char** argv) {
42    SkCommandLineFlags::Parse(argc, argv);
43
44    for (int i = 0; i < FLAGS_skps.count(); i++) {
45        if (SkCommandLineFlags::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        if (FLAGS_defer) {
60            SkPictureRecorder recorder;
61            SkDeferredCanvas deferred(recorder.beginRecording(src->cullRect()),
62                                      SkDeferredCanvas::kEager);
63            src->playback(&deferred);
64            src = recorder.finishRecordingAsPicture();
65        }
66        const int w = SkScalarCeilToInt(src->cullRect().width());
67        const int h = SkScalarCeilToInt(src->cullRect().height());
68
69        SkRecord record;
70        SkRecorder canvas(&record, w, h);
71        src->playback(&canvas);
72
73        if (FLAGS_optimize) {
74            SkRecordOptimize(&record);
75        }
76        if (FLAGS_optimize2) {
77            SkRecordOptimize2(&record);
78        }
79
80        dump(FLAGS_skps[i], w, h, record);
81
82        if (FLAGS_write.count() > 0) {
83            SkPictureRecorder r;
84            SkRecordDraw(record,
85                         r.beginRecording(SkRect::MakeIWH(w, h)),
86                         nullptr,
87                         nullptr,
88                         0,
89                         nullptr,
90                         nullptr);
91            sk_sp<SkPicture> dst(r.finishRecordingAsPicture());
92            SkFILEWStream ostream(FLAGS_write[0]);
93            dst->serialize(&ostream);
94        }
95    }
96
97    return 0;
98}
99