2017-12-30 17:27:00 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2017 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "tools/viewer/SkottieSlide.h"
|
2017-12-30 17:27:00 +00:00
|
|
|
|
2018-05-26 13:49:28 +00:00
|
|
|
#if defined(SK_ENABLE_SKOTTIE)
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkFont.h"
|
2019-12-10 19:23:32 +00:00
|
|
|
#include "include/core/SkTime.h"
|
2020-10-16 16:04:18 +00:00
|
|
|
#include "include/private/SkTPin.h"
|
2020-08-13 16:03:37 +00:00
|
|
|
#include "modules/audioplayer/SkAudioPlayer.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "modules/skottie/include/Skottie.h"
|
2020-05-06 19:55:18 +00:00
|
|
|
#include "modules/skottie/utils/SkottieUtils.h"
|
2019-11-26 13:58:26 +00:00
|
|
|
#include "modules/skresources/include/SkResources.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/utils/SkOSPath.h"
|
2019-07-11 20:32:53 +00:00
|
|
|
#include "tools/timer/TimeUtils.h"
|
2017-12-30 17:27:00 +00:00
|
|
|
|
2018-05-29 17:46:54 +00:00
|
|
|
#include <cmath>
|
|
|
|
|
2019-12-10 18:38:45 +00:00
|
|
|
#include "imgui.h"
|
|
|
|
|
2020-08-13 16:27:19 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class Track final : public skresources::ExternalTrackAsset {
|
|
|
|
public:
|
|
|
|
explicit Track(std::unique_ptr<SkAudioPlayer> player) : fPlayer(std::move(player)) {}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void seek(float t) override {
|
|
|
|
if (fPlayer->isStopped() && t >=0) {
|
|
|
|
fPlayer->play();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fPlayer->isPlaying()) {
|
|
|
|
if (t < 0) {
|
|
|
|
fPlayer->stop();
|
|
|
|
} else {
|
|
|
|
static constexpr float kTolerance = 0.075f;
|
|
|
|
const auto player_pos = fPlayer->time();
|
|
|
|
|
|
|
|
if (std::abs(player_pos - t) > kTolerance) {
|
|
|
|
fPlayer->setTime(t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::unique_ptr<SkAudioPlayer> fPlayer;
|
|
|
|
};
|
|
|
|
|
|
|
|
class AudioProviderProxy final : public skresources::ResourceProviderProxyBase {
|
|
|
|
public:
|
|
|
|
explicit AudioProviderProxy(sk_sp<skresources::ResourceProvider> rp)
|
|
|
|
: INHERITED(std::move(rp)) {}
|
|
|
|
|
|
|
|
private:
|
|
|
|
sk_sp<skresources::ExternalTrackAsset> loadAudioAsset(const char path[],
|
|
|
|
const char name[],
|
|
|
|
const char[] /*id*/) override {
|
|
|
|
if (auto data = this->load(path, name)) {
|
|
|
|
if (auto player = SkAudioPlayer::Make(std::move(data))) {
|
|
|
|
return sk_make_sp<Track>(std::move(player));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
using INHERITED = skresources::ResourceProviderProxyBase;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2018-08-23 00:37:04 +00:00
|
|
|
static void draw_stats_box(SkCanvas* canvas, const skottie::Animation::Builder::Stats& stats) {
|
2018-04-30 14:32:18 +00:00
|
|
|
static constexpr SkRect kR = { 10, 10, 280, 120 };
|
|
|
|
static constexpr SkScalar kTextSize = 20;
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setColor(0xffeeeeee);
|
2019-01-08 14:38:02 +00:00
|
|
|
|
|
|
|
SkFont font(nullptr, kTextSize);
|
2018-04-30 14:32:18 +00:00
|
|
|
|
|
|
|
canvas->drawRect(kR, paint);
|
|
|
|
|
|
|
|
paint.setColor(SK_ColorBLACK);
|
|
|
|
|
2020-05-12 14:41:04 +00:00
|
|
|
const auto json_size = SkStringPrintf("Json size: %zu bytes",
|
2018-04-30 14:32:18 +00:00
|
|
|
stats.fJsonSize);
|
2019-01-08 14:38:02 +00:00
|
|
|
canvas->drawString(json_size, kR.x() + 10, kR.y() + kTextSize * 1, font, paint);
|
2020-05-12 14:41:04 +00:00
|
|
|
const auto animator_count = SkStringPrintf("Animator count: %zu",
|
2018-04-30 14:32:18 +00:00
|
|
|
stats.fAnimatorCount);
|
2019-01-08 14:38:02 +00:00
|
|
|
canvas->drawString(animator_count, kR.x() + 10, kR.y() + kTextSize * 2, font, paint);
|
2018-04-30 14:32:18 +00:00
|
|
|
const auto json_parse_time = SkStringPrintf("Json parse time: %.3f ms",
|
|
|
|
stats.fJsonParseTimeMS);
|
2019-01-08 14:38:02 +00:00
|
|
|
canvas->drawString(json_parse_time, kR.x() + 10, kR.y() + kTextSize * 3, font, paint);
|
2018-04-30 14:32:18 +00:00
|
|
|
const auto scene_parse_time = SkStringPrintf("Scene build time: %.3f ms",
|
|
|
|
stats.fSceneParseTimeMS);
|
2019-01-08 14:38:02 +00:00
|
|
|
canvas->drawString(scene_parse_time, kR.x() + 10, kR.y() + kTextSize * 4, font, paint);
|
2018-04-30 14:32:18 +00:00
|
|
|
const auto total_load_time = SkStringPrintf("Total load time: %.3f ms",
|
|
|
|
stats.fTotalLoadTimeMS);
|
2019-01-08 14:38:02 +00:00
|
|
|
canvas->drawString(total_load_time, kR.x() + 10, kR.y() + kTextSize * 5, font, paint);
|
2018-04-30 14:32:18 +00:00
|
|
|
|
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
canvas->drawRect(kR, paint);
|
|
|
|
}
|
|
|
|
|
2018-01-16 22:04:30 +00:00
|
|
|
SkottieSlide::SkottieSlide(const SkString& name, const SkString& path)
|
2017-12-30 17:27:00 +00:00
|
|
|
: fPath(path) {
|
|
|
|
fName = name;
|
|
|
|
}
|
|
|
|
|
2018-02-09 16:15:32 +00:00
|
|
|
void SkottieSlide::load(SkScalar w, SkScalar h) {
|
2018-10-02 16:48:00 +00:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
|
|
|
auto logger = sk_make_sp<Logger>();
|
2020-05-20 21:04:21 +00:00
|
|
|
|
|
|
|
uint32_t flags = 0;
|
|
|
|
if (fPreferGlyphPaths) {
|
|
|
|
flags |= skottie::Animation::Builder::kPreferEmbeddedFonts;
|
|
|
|
}
|
|
|
|
skottie::Animation::Builder builder(flags);
|
2018-10-02 16:48:00 +00:00
|
|
|
|
2020-05-06 19:55:18 +00:00
|
|
|
auto resource_provider =
|
2020-08-13 16:27:19 +00:00
|
|
|
sk_make_sp<AudioProviderProxy>(
|
2020-05-06 19:55:18 +00:00
|
|
|
skresources::DataURIResourceProviderProxy::Make(
|
|
|
|
skresources::FileResourceProvider::Make(SkOSPath::Dirname(fPath.c_str()),
|
|
|
|
/*predecode=*/true),
|
2020-08-13 16:27:19 +00:00
|
|
|
/*predecode=*/true));
|
2020-05-06 19:55:18 +00:00
|
|
|
|
|
|
|
static constexpr char kInterceptPrefix[] = "__";
|
|
|
|
auto precomp_interceptor =
|
|
|
|
sk_make_sp<skottie_utils::ExternalAnimationPrecompInterceptor>(resource_provider,
|
|
|
|
kInterceptPrefix);
|
2018-11-09 21:19:44 +00:00
|
|
|
fAnimation = builder
|
|
|
|
.setLogger(logger)
|
2020-05-06 19:55:18 +00:00
|
|
|
.setResourceProvider(std::move(resource_provider))
|
|
|
|
.setPrecompInterceptor(std::move(precomp_interceptor))
|
2018-11-09 21:19:44 +00:00
|
|
|
.makeFromFile(fPath.c_str());
|
2018-08-23 00:37:04 +00:00
|
|
|
fAnimationStats = builder.getStats();
|
|
|
|
fWinSize = SkSize::Make(w, h);
|
|
|
|
fTimeBase = 0; // force a time reset
|
2017-12-30 17:27:00 +00:00
|
|
|
|
|
|
|
if (fAnimation) {
|
2019-12-09 16:09:38 +00:00
|
|
|
fAnimation->seek(0);
|
2019-12-10 19:23:32 +00:00
|
|
|
fFrameTimes.resize(SkScalarCeilToInt(fAnimation->duration() * fAnimation->fps()));
|
2018-10-02 16:48:00 +00:00
|
|
|
SkDebugf("Loaded Bodymovin animation v: %s, size: [%f %f]\n",
|
2017-12-30 17:27:00 +00:00
|
|
|
fAnimation->version().c_str(),
|
|
|
|
fAnimation->size().width(),
|
2018-05-31 20:45:29 +00:00
|
|
|
fAnimation->size().height());
|
2018-10-02 16:48:00 +00:00
|
|
|
logger->report();
|
2017-12-30 17:27:00 +00:00
|
|
|
} else {
|
|
|
|
SkDebugf("failed to load Bodymovin animation: %s\n", fPath.c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-16 22:04:30 +00:00
|
|
|
void SkottieSlide::unload() {
|
2017-12-30 17:27:00 +00:00
|
|
|
fAnimation.reset();
|
|
|
|
}
|
|
|
|
|
2019-12-10 18:38:45 +00:00
|
|
|
void SkottieSlide::resize(SkScalar w, SkScalar h) {
|
|
|
|
fWinSize = { w, h };
|
|
|
|
}
|
|
|
|
|
2018-01-16 22:04:30 +00:00
|
|
|
SkISize SkottieSlide::getDimensions() const {
|
2018-02-09 16:15:32 +00:00
|
|
|
// We always scale to fill the window.
|
|
|
|
return fWinSize.toCeil();
|
2017-12-30 17:27:00 +00:00
|
|
|
}
|
|
|
|
|
2018-01-16 22:04:30 +00:00
|
|
|
void SkottieSlide::draw(SkCanvas* canvas) {
|
2017-12-30 17:27:00 +00:00
|
|
|
if (fAnimation) {
|
2018-01-02 19:37:37 +00:00
|
|
|
SkAutoCanvasRestore acr(canvas, true);
|
2018-02-09 16:15:32 +00:00
|
|
|
const auto dstR = SkRect::MakeSize(fWinSize);
|
2019-12-10 19:23:32 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
const auto t0 = SkTime::GetNSecs();
|
|
|
|
fAnimation->render(canvas, &dstR);
|
|
|
|
|
|
|
|
// TODO: this does not capture GPU flush time!
|
2019-12-11 13:55:46 +00:00
|
|
|
const auto frame_index = static_cast<size_t>(fCurrentFrame);
|
2019-12-10 19:23:32 +00:00
|
|
|
fFrameTimes[frame_index] = static_cast<float>((SkTime::GetNSecs() - t0) * 1e-6);
|
|
|
|
}
|
2018-04-30 14:32:18 +00:00
|
|
|
|
|
|
|
if (fShowAnimationStats) {
|
|
|
|
draw_stats_box(canvas, fAnimationStats);
|
|
|
|
}
|
2019-07-22 16:05:41 +00:00
|
|
|
if (fShowAnimationInval) {
|
|
|
|
const auto t = SkMatrix::MakeRectToRect(SkRect::MakeSize(fAnimation->size()),
|
|
|
|
dstR,
|
|
|
|
SkMatrix::kCenter_ScaleToFit);
|
|
|
|
SkPaint fill, stroke;
|
|
|
|
fill.setAntiAlias(true);
|
|
|
|
fill.setColor(0x40ff0000);
|
|
|
|
stroke.setAntiAlias(true);
|
|
|
|
stroke.setColor(0xffff0000);
|
|
|
|
stroke.setStyle(SkPaint::kStroke_Style);
|
|
|
|
|
|
|
|
for (const auto& r : fInvalController) {
|
|
|
|
SkRect bounds;
|
|
|
|
t.mapRect(&bounds, r);
|
|
|
|
canvas->drawRect(bounds, fill);
|
|
|
|
canvas->drawRect(bounds, stroke);
|
|
|
|
}
|
|
|
|
}
|
2019-12-10 18:38:45 +00:00
|
|
|
if (fShowUI) {
|
|
|
|
this->renderUI();
|
|
|
|
}
|
|
|
|
|
2017-12-30 17:27:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-11 20:32:53 +00:00
|
|
|
bool SkottieSlide::animate(double nanos) {
|
2019-12-11 13:55:46 +00:00
|
|
|
if (!fTimeBase) {
|
2017-12-30 17:27:00 +00:00
|
|
|
// Reset the animation time.
|
2019-12-11 13:55:46 +00:00
|
|
|
fTimeBase = nanos;
|
2017-12-30 17:27:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (fAnimation) {
|
2019-07-22 16:05:41 +00:00
|
|
|
fInvalController.reset();
|
2019-12-10 18:38:45 +00:00
|
|
|
|
2019-12-11 13:55:46 +00:00
|
|
|
const auto frame_count = fAnimation->duration() * fAnimation->fps();
|
|
|
|
|
2019-12-10 18:38:45 +00:00
|
|
|
if (!fDraggingProgress) {
|
|
|
|
// Clock-driven progress: update current frame.
|
2019-12-11 13:55:46 +00:00
|
|
|
const double t_sec = (nanos - fTimeBase) * 1e-9;
|
|
|
|
fCurrentFrame = std::fmod(t_sec * fAnimation->fps(), frame_count);
|
2019-12-10 18:38:45 +00:00
|
|
|
} else {
|
|
|
|
// Slider-driven progress: update the time origin.
|
2019-12-11 13:55:46 +00:00
|
|
|
fTimeBase = nanos - fCurrentFrame / fAnimation->fps() * 1e9;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sanitize and rate-lock the current frame.
|
|
|
|
fCurrentFrame = SkTPin<float>(fCurrentFrame, 0.0f, frame_count - 1);
|
|
|
|
if (fFrameRate > 0) {
|
|
|
|
const auto fps_scale = fFrameRate / fAnimation->fps();
|
|
|
|
fCurrentFrame = std::trunc(fCurrentFrame * fps_scale) / fps_scale;
|
2019-12-10 18:38:45 +00:00
|
|
|
}
|
|
|
|
|
2020-07-29 14:51:28 +00:00
|
|
|
fAnimation->seekFrame(fCurrentFrame, fShowAnimationInval ? &fInvalController
|
|
|
|
: nullptr);
|
2017-12-30 17:27:00 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-01-16 22:04:30 +00:00
|
|
|
bool SkottieSlide::onChar(SkUnichar c) {
|
2017-12-30 17:27:00 +00:00
|
|
|
switch (c) {
|
|
|
|
case 'I':
|
2018-04-30 14:32:18 +00:00
|
|
|
fShowAnimationStats = !fShowAnimationStats;
|
2020-05-20 21:04:21 +00:00
|
|
|
return true;
|
|
|
|
case 'G':
|
|
|
|
fPreferGlyphPaths = !fPreferGlyphPaths;
|
|
|
|
this->load(fWinSize.width(), fWinSize.height());
|
|
|
|
return true;
|
2017-12-30 17:27:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return INHERITED::onChar(c);
|
|
|
|
}
|
2018-02-20 21:49:20 +00:00
|
|
|
|
2019-08-29 14:39:22 +00:00
|
|
|
bool SkottieSlide::onMouse(SkScalar x, SkScalar y, skui::InputState state, skui::ModifierKey) {
|
2018-02-20 21:49:20 +00:00
|
|
|
switch (state) {
|
2019-08-29 14:39:22 +00:00
|
|
|
case skui::InputState::kUp:
|
2018-02-20 21:49:20 +00:00
|
|
|
fShowAnimationInval = !fShowAnimationInval;
|
2018-04-30 14:32:18 +00:00
|
|
|
fShowAnimationStats = !fShowAnimationStats;
|
2018-02-20 21:49:20 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-12-10 18:38:45 +00:00
|
|
|
fShowUI = this->UIArea().contains(x, y);
|
|
|
|
|
2018-02-21 18:03:41 +00:00
|
|
|
return false;
|
2018-02-20 21:49:20 +00:00
|
|
|
}
|
2018-05-26 13:49:28 +00:00
|
|
|
|
2019-12-10 18:38:45 +00:00
|
|
|
SkRect SkottieSlide::UIArea() const {
|
2019-12-11 13:55:46 +00:00
|
|
|
static constexpr float kUIHeight = 120.0f;
|
2019-12-10 18:38:45 +00:00
|
|
|
|
|
|
|
return SkRect::MakeXYWH(0, fWinSize.height() - kUIHeight, fWinSize.width(), kUIHeight);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SkottieSlide::renderUI() {
|
2019-12-10 19:23:32 +00:00
|
|
|
static constexpr auto kUI_opacity = 0.35f,
|
2019-12-11 13:55:46 +00:00
|
|
|
kUI_hist_height = 50.0f,
|
|
|
|
kUI_fps_width = 100.0f;
|
|
|
|
|
|
|
|
auto add_frame_rate_option = [this](const char* label, double rate) {
|
|
|
|
const auto is_selected = (fFrameRate == rate);
|
|
|
|
if (ImGui::Selectable(label, is_selected)) {
|
|
|
|
fFrameRate = rate;
|
|
|
|
fFrameRateLabel = label;
|
|
|
|
}
|
|
|
|
if (is_selected) {
|
|
|
|
ImGui::SetItemDefaultFocus();
|
|
|
|
}
|
|
|
|
};
|
2019-12-10 18:38:45 +00:00
|
|
|
|
|
|
|
ImGui::SetNextWindowBgAlpha(kUI_opacity);
|
|
|
|
if (ImGui::Begin("Skottie Controls", nullptr, ImGuiWindowFlags_NoDecoration |
|
|
|
|
ImGuiWindowFlags_NoResize |
|
|
|
|
ImGuiWindowFlags_NoMove |
|
|
|
|
ImGuiWindowFlags_NoSavedSettings |
|
|
|
|
ImGuiWindowFlags_NoFocusOnAppearing |
|
|
|
|
ImGuiWindowFlags_NoNav)) {
|
|
|
|
const auto ui_area = this->UIArea();
|
|
|
|
ImGui::SetWindowPos(ImVec2(ui_area.x(), ui_area.y()));
|
|
|
|
ImGui::SetWindowSize(ImVec2(ui_area.width(), ui_area.height()));
|
|
|
|
|
|
|
|
ImGui::PushItemWidth(-1);
|
2019-12-10 19:23:32 +00:00
|
|
|
ImGui::PlotHistogram("", fFrameTimes.data(), fFrameTimes.size(),
|
2019-12-11 13:55:46 +00:00
|
|
|
0, nullptr, FLT_MAX, FLT_MAX, ImVec2(0, kUI_hist_height));
|
2019-12-10 19:23:32 +00:00
|
|
|
ImGui::SliderFloat("", &fCurrentFrame, 0, fAnimation->duration() * fAnimation->fps() - 1);
|
2019-12-10 18:38:45 +00:00
|
|
|
fDraggingProgress = ImGui::IsItemActive();
|
2019-12-11 13:55:46 +00:00
|
|
|
ImGui::PopItemWidth();
|
2019-12-10 18:38:45 +00:00
|
|
|
|
2019-12-11 13:55:46 +00:00
|
|
|
ImGui::PushItemWidth(kUI_fps_width);
|
|
|
|
if (ImGui::BeginCombo("FPS", fFrameRateLabel)) {
|
|
|
|
add_frame_rate_option("", 0.0);
|
|
|
|
add_frame_rate_option("Native", fAnimation->fps());
|
|
|
|
add_frame_rate_option( "1", 1.0);
|
|
|
|
add_frame_rate_option("15", 15.0);
|
|
|
|
add_frame_rate_option("24", 24.0);
|
|
|
|
add_frame_rate_option("30", 30.0);
|
|
|
|
add_frame_rate_option("60", 60.0);
|
|
|
|
ImGui::EndCombo();
|
|
|
|
}
|
2019-12-10 18:38:45 +00:00
|
|
|
ImGui::PopItemWidth();
|
|
|
|
}
|
|
|
|
ImGui::End();
|
|
|
|
}
|
|
|
|
|
2018-05-26 13:49:28 +00:00
|
|
|
#endif // SK_ENABLE_SKOTTIE
|