blob: 52f8f8c0b8245771077be2365ff06d0f445b0ab8 [file] [log] [blame]
mtklein2f2903d2015-11-18 11:06:37 -08001/*
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"
mtklein2f2903d2015-11-18 11:06:37 -08009#include "SkCommandLineFlags.h"
10#include "SkPicture.h"
reedff53af82015-11-19 08:18:04 -080011#include "SkPictureRecorder.h"
12#include "SkRecordDraw.h"
mtklein2f2903d2015-11-18 11:06:37 -080013#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.");
reedf38b0d82015-11-19 10:31:08 -080021DEFINE_bool(optimize2, false, "Run SkRecordOptimize2 before dumping.");
mtklein2f2903d2015-11-18 11:06:37 -080022DEFINE_int32(tile, 1000000000, "Simulated tile size.");
23DEFINE_bool(timeWithCommand, false, "If true, print time next to command, else in first column.");
reedff53af82015-11-19 08:18:04 -080024DEFINE_string2(write, w, "", "Write the (optimized) picture to the named file.");
mtklein2f2903d2015-11-18 11:06:37 -080025
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
mtklein2f2903d2015-11-18 11:06:37 -080038int 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 }
reedca2622b2016-03-18 07:25:55 -070052 sk_sp<SkPicture> src(SkPicture::MakeFromStream(stream));
mtklein2f2903d2015-11-18 11:06:37 -080053 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 }
reedf38b0d82015-11-19 10:31:08 -080067 if (FLAGS_optimize2) {
68 SkRecordOptimize2(&record);
69 }
mtklein2f2903d2015-11-18 11:06:37 -080070
71 dump(FLAGS_skps[i], w, h, record);
reedff53af82015-11-19 08:18:04 -080072
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);
reedca2622b2016-03-18 07:25:55 -070082 sk_sp<SkPicture> dst(r.finishRecordingAsPicture());
reedff53af82015-11-19 08:18:04 -080083 SkFILEWStream ostream(FLAGS_write[0]);
84 dst->serialize(&ostream);
85 }
mtklein2f2903d2015-11-18 11:06:37 -080086 }
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