mirrored from https://skia.googlesource.com/skia
/
SkottieTool.cpp
213 lines (167 loc) · 6.43 KB
/
SkottieTool.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
* Copyright 2018 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "include/core/SkCanvas.h"
#include "include/core/SkGraphics.h"
#include "include/core/SkPictureRecorder.h"
#include "include/core/SkStream.h"
#include "include/core/SkSurface.h"
#include "modules/skottie/include/Skottie.h"
#include "modules/skottie/utils/SkottieUtils.h"
#include "src/core/SkMakeUnique.h"
#include "src/core/SkOSFile.h"
#include "src/utils/SkOSPath.h"
#include "tools/flags/CommandLineFlags.h"
#include <vector>
static DEFINE_string2(input , i, nullptr, "Input .json file.");
static DEFINE_string2(writePath, w, nullptr, "Output directory. Frames are names [0-9]{6}.png.");
static DEFINE_string2(format , f, "png" , "Output format (png or skp)");
static DEFINE_double(t0, 0, "Timeline start [0..1].");
static DEFINE_double(t1, 1, "Timeline stop [0..1].");
static DEFINE_double(fps, 30, "Decode frames per second.");
static DEFINE_int(width , 800, "Render width.");
static DEFINE_int(height, 600, "Render height.");
namespace {
class Sink {
public:
virtual ~Sink() = default;
Sink(const Sink&) = delete;
Sink& operator=(const Sink&) = delete;
bool handleFrame(const sk_sp<skottie::Animation>& anim, size_t idx) const {
const auto frame_file = SkStringPrintf("0%06d.%s", idx, fExtension.c_str());
SkFILEWStream stream (SkOSPath::Join(FLAGS_writePath[0], frame_file.c_str()).c_str());
if (!stream.isValid()) {
SkDebugf("Could not open '%s/%s' for writing.\n",
FLAGS_writePath[0], frame_file.c_str());
return false;
}
return this->saveFrame(anim, &stream);
}
protected:
Sink(const char* ext) : fExtension(ext) {}
virtual bool saveFrame(const sk_sp<skottie::Animation>& anim, SkFILEWStream*) const = 0;
private:
const SkString fExtension;
};
class PNGSink final : public Sink {
public:
PNGSink()
: INHERITED("png")
, fSurface(SkSurface::MakeRasterN32Premul(FLAGS_width, FLAGS_height)) {
if (!fSurface) {
SkDebugf("Could not allocate a %d x %d surface.\n", FLAGS_width, FLAGS_height);
}
}
bool saveFrame(const sk_sp<skottie::Animation>& anim, SkFILEWStream* stream) const override {
if (!fSurface) return false;
auto* canvas = fSurface->getCanvas();
SkAutoCanvasRestore acr(canvas, true);
canvas->concat(SkMatrix::MakeRectToRect(SkRect::MakeSize(anim->size()),
SkRect::MakeIWH(FLAGS_width, FLAGS_height),
SkMatrix::kCenter_ScaleToFit));
canvas->clear(SK_ColorTRANSPARENT);
anim->render(canvas);
auto png_data = fSurface->makeImageSnapshot()->encodeToData();
if (!png_data) {
SkDebugf("Failed to encode frame!\n");
return false;
}
return stream->write(png_data->data(), png_data->size());
}
private:
const sk_sp<SkSurface> fSurface;
using INHERITED = Sink;
};
class SKPSink final : public Sink {
public:
SKPSink() : INHERITED("skp") {}
bool saveFrame(const sk_sp<skottie::Animation>& anim, SkFILEWStream* stream) const override {
SkPictureRecorder recorder;
auto canvas = recorder.beginRecording(FLAGS_width, FLAGS_height);
canvas->concat(SkMatrix::MakeRectToRect(SkRect::MakeSize(anim->size()),
SkRect::MakeIWH(FLAGS_width, FLAGS_height),
SkMatrix::kCenter_ScaleToFit));
anim->render(canvas);
recorder.finishRecordingAsPicture()->serialize(stream);
return true;
}
private:
const sk_sp<SkSurface> fSurface;
using INHERITED = Sink;
};
class Logger final : public skottie::Logger {
public:
struct LogEntry {
SkString fMessage,
fJSON;
};
void log(skottie::Logger::Level lvl, const char message[], const char json[]) override {
auto& log = lvl == skottie::Logger::Level::kError ? fErrors : fWarnings;
log.push_back({ SkString(message), json ? SkString(json) : SkString() });
}
void report() const {
SkDebugf("Animation loaded with %lu error%s, %lu warning%s.\n",
fErrors.size(), fErrors.size() == 1 ? "" : "s",
fWarnings.size(), fWarnings.size() == 1 ? "" : "s");
const auto& show = [](const LogEntry& log, const char prefix[]) {
SkDebugf("%s%s", prefix, log.fMessage.c_str());
if (!log.fJSON.isEmpty())
SkDebugf(" : %s", log.fJSON.c_str());
SkDebugf("\n");
};
for (const auto& err : fErrors) show(err, " !! ");
for (const auto& wrn : fWarnings) show(wrn, " ?? ");
}
private:
std::vector<LogEntry> fErrors,
fWarnings;
};
} // namespace
int main(int argc, char** argv) {
CommandLineFlags::Parse(argc, argv);
SkAutoGraphics ag;
if (FLAGS_input.isEmpty() || FLAGS_writePath.isEmpty()) {
SkDebugf("Missing required 'input' and 'writePath' args.\n");
return 1;
}
if (FLAGS_fps <= 0) {
SkDebugf("Invalid fps: %f.\n", FLAGS_fps);
return 1;
}
if (!sk_mkdir(FLAGS_writePath[0])) {
return 1;
}
std::unique_ptr<Sink> sink;
if (0 == strcmp(FLAGS_format[0], "png")) {
sink = skstd::make_unique<PNGSink>();
} else if (0 == strcmp(FLAGS_format[0], "skp")) {
sink = skstd::make_unique<SKPSink>();
} else {
SkDebugf("Unknown format: %s\n", FLAGS_format[0]);
return 1;
}
auto logger = sk_make_sp<Logger>();
auto anim = skottie::Animation::Builder()
.setLogger(logger)
.setResourceProvider(
skottie_utils::FileResourceProvider::Make(SkOSPath::Dirname(FLAGS_input[0])))
.makeFromFile(FLAGS_input[0]);
if (!anim) {
SkDebugf("Could not load animation: '%s'.\n", FLAGS_input[0]);
return 1;
}
logger->report();
static constexpr double kMaxFrames = 10000;
const auto t0 = SkTPin(FLAGS_t0, 0.0, 1.0),
t1 = SkTPin(FLAGS_t1, t0, 1.0),
advance = 1 / std::min(anim->duration() * FLAGS_fps, kMaxFrames);
size_t frame_index = 0;
for (auto t = t0; t <= t1; t += advance) {
anim->seek(t);
sink->handleFrame(anim, frame_index++);
}
return 0;
}