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 <stdio.h>
9
10#include "SkCommandLineFlags.h"
11#include "SkGraphics.h"
12#include "SkPicture.h"
13#include "SkRecordOpts.h"
14#include "SkRecorder.h"
15#include "SkStream.h"
16
17#include "DumpRecord.h"
18#include "LazyDecodeBitmap.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_int32(tile, 1000000000, "Simulated tile size.");
24DEFINE_bool(timeWithCommand, false, "If true, print time next to command, else in first column.");
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
38
39int tool_main(int argc, char** argv);
40int tool_main(int argc, char** argv) {
41    SkCommandLineFlags::Parse(argc, argv);
42    SkAutoGraphics ag;
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        SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(FLAGS_skps[i]));
50        if (!stream) {
51            SkDebugf("Could not read %s.\n", FLAGS_skps[i]);
52            exit(1);
53        }
54        SkAutoTUnref<SkPicture> src(
55                SkPicture::CreateFromStream(stream, sk_tools::LazyDecodeBitmap));
56        if (!src) {
57            SkDebugf("Could not read %s as an SkPicture.\n", FLAGS_skps[i]);
58            exit(1);
59        }
60        const int w = SkScalarCeilToInt(src->cullRect().width());
61        const int h = SkScalarCeilToInt(src->cullRect().height());
62
63        SkRecord record;
64        SkRecorder canvas(&record, w, h);
65        src->playback(&canvas);
66
67        if (FLAGS_optimize) {
68            SkRecordOptimize(&record);
69        }
70
71        dump(FLAGS_skps[i], w, h, record);
72    }
73
74    return 0;
75}
76
77#if !defined SK_BUILD_FOR_IOS
78int main(int argc, char * const argv[]) {
79    return tool_main(argc, (char**) argv);
80}
81#endif
82