skia2/tools/viewer/SkottieSlide.cpp
Mike Klein c0bd9f9fe5 rewrite includes to not need so much -Ifoo
Current strategy: everything from the top

Things to look at first are the manual changes:

   - added tools/rewrite_includes.py
   - removed -Idirectives from BUILD.gn
   - various compile.sh simplifications
   - tweak tools/embed_resources.py
   - update gn/find_headers.py to write paths from the top
   - update gn/gn_to_bp.py SkUserConfig.h layout
     so that #include "include/config/SkUserConfig.h" always
     gets the header we want.

No-Presubmit: true
Change-Id: I73a4b181654e0e38d229bc456c0d0854bae3363e
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/209706
Commit-Queue: Mike Klein <mtklein@google.com>
Reviewed-by: Hal Canary <halcanary@google.com>
Reviewed-by: Brian Osman <brianosman@google.com>
Reviewed-by: Florin Malita <fmalita@chromium.org>
2019-04-24 16:27:11 +00:00

180 lines
5.7 KiB
C++

/*
* Copyright 2017 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "tools/viewer/SkottieSlide.h"
#if defined(SK_ENABLE_SKOTTIE)
#include "include/core/SkCanvas.h"
#include "include/core/SkFont.h"
#include "modules/skottie/include/Skottie.h"
#include "modules/skottie/utils/SkottieUtils.h"
#include "src/utils/SkOSPath.h"
#include "tools/timer/AnimTimer.h"
#include <cmath>
static void draw_stats_box(SkCanvas* canvas, const skottie::Animation::Builder::Stats& stats) {
static constexpr SkRect kR = { 10, 10, 280, 120 };
static constexpr SkScalar kTextSize = 20;
SkPaint paint;
paint.setAntiAlias(true);
paint.setColor(0xffeeeeee);
SkFont font(nullptr, kTextSize);
canvas->drawRect(kR, paint);
paint.setColor(SK_ColorBLACK);
const auto json_size = SkStringPrintf("Json size: %lu bytes",
stats.fJsonSize);
canvas->drawString(json_size, kR.x() + 10, kR.y() + kTextSize * 1, font, paint);
const auto animator_count = SkStringPrintf("Animator count: %lu",
stats.fAnimatorCount);
canvas->drawString(animator_count, kR.x() + 10, kR.y() + kTextSize * 2, font, paint);
const auto json_parse_time = SkStringPrintf("Json parse time: %.3f ms",
stats.fJsonParseTimeMS);
canvas->drawString(json_parse_time, kR.x() + 10, kR.y() + kTextSize * 3, font, paint);
const auto scene_parse_time = SkStringPrintf("Scene build time: %.3f ms",
stats.fSceneParseTimeMS);
canvas->drawString(scene_parse_time, kR.x() + 10, kR.y() + kTextSize * 4, font, paint);
const auto total_load_time = SkStringPrintf("Total load time: %.3f ms",
stats.fTotalLoadTimeMS);
canvas->drawString(total_load_time, kR.x() + 10, kR.y() + kTextSize * 5, font, paint);
paint.setStyle(SkPaint::kStroke_Style);
canvas->drawRect(kR, paint);
}
SkottieSlide::SkottieSlide(const SkString& name, const SkString& path)
: fPath(path) {
fName = name;
}
void SkottieSlide::load(SkScalar w, SkScalar h) {
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>();
skottie::Animation::Builder builder;
fAnimation = builder
.setLogger(logger)
.setResourceProvider(
skottie_utils::FileResourceProvider::Make(SkOSPath::Dirname(fPath.c_str())))
.makeFromFile(fPath.c_str());
fAnimationStats = builder.getStats();
fWinSize = SkSize::Make(w, h);
fTimeBase = 0; // force a time reset
if (fAnimation) {
fAnimation->setShowInval(fShowAnimationInval);
SkDebugf("Loaded Bodymovin animation v: %s, size: [%f %f]\n",
fAnimation->version().c_str(),
fAnimation->size().width(),
fAnimation->size().height());
logger->report();
} else {
SkDebugf("failed to load Bodymovin animation: %s\n", fPath.c_str());
}
}
void SkottieSlide::unload() {
fAnimation.reset();
}
SkISize SkottieSlide::getDimensions() const {
// We always scale to fill the window.
return fWinSize.toCeil();
}
void SkottieSlide::draw(SkCanvas* canvas) {
if (fAnimation) {
SkAutoCanvasRestore acr(canvas, true);
const auto dstR = SkRect::MakeSize(fWinSize);
fAnimation->render(canvas, &dstR);
if (fShowAnimationStats) {
draw_stats_box(canvas, fAnimationStats);
}
}
}
bool SkottieSlide::animate(const AnimTimer& timer) {
if (fTimeBase == 0) {
// Reset the animation time.
fTimeBase = timer.msec();
}
if (fAnimation) {
const auto t = timer.msec() - fTimeBase;
const auto d = fAnimation->duration() * 1000;
fAnimation->seek(std::fmod(t, d) / d);
}
return true;
}
bool SkottieSlide::onChar(SkUnichar c) {
switch (c) {
case 'I':
fShowAnimationStats = !fShowAnimationStats;
break;
default:
break;
}
return INHERITED::onChar(c);
}
bool SkottieSlide::onMouse(SkScalar x, SkScalar y, sk_app::Window::InputState state, uint32_t) {
switch (state) {
case sk_app::Window::kUp_InputState:
fShowAnimationInval = !fShowAnimationInval;
fShowAnimationStats = !fShowAnimationStats;
fAnimation->setShowInval(fShowAnimationInval);
break;
default:
break;
}
return false;
}
#endif // SK_ENABLE_SKOTTIE