skia2/tools/viewer/MSKPSlide.cpp
Brian Salomon 06c9e2962b Revert "Revert "Add mskp player, use in viewer slide""
This reverts commit 0d174586c4.

Use SkTLazy instead of std::optional (C++17 library feature)

Bug: skia:11900
Change-Id: Ia41caa9322d812f9ba6644dd14ede7d0015cf8b3
Cq-Include-Trybots: luci.skia.skia.primary:Housekeeper-PerCommit-CreateDockerImage_Skia_Release,Build-Debian10-Clang-x86_64-Release-CMake
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/402642
Reviewed-by: Brian Salomon <bsalomon@google.com>
Commit-Queue: Brian Salomon <bsalomon@google.com>
2021-04-29 19:40:08 +00:00

62 lines
1.7 KiB
C++

/*
* Copyright 2021 Google LLC
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "tools/viewer/MSKPSlide.h"
#include "include/core/SkCanvas.h"
#include "include/core/SkStream.h"
#include "src/core/SkOSFile.h"
MSKPSlide::MSKPSlide(const SkString& name, const SkString& path)
: MSKPSlide(name, SkStream::MakeFromFile(path.c_str())) {}
MSKPSlide::MSKPSlide(const SkString& name, std::unique_ptr<SkStreamSeekable> stream)
: fStream(std::move(stream)) {
fName = name;
}
SkISize MSKPSlide::getDimensions() const {
return fPlayer ? fPlayer->maxDimensions() : SkISize{0, 0};
}
void MSKPSlide::draw(SkCanvas* canvas) {
if (fPlayer) {
fPlayer->playFrame(canvas, fFrame);
}
}
bool MSKPSlide::animate(double nanos) {
if (!fPlayer) {
return false;
}
double elapsed = nanos - fLastFrameTime;
double frameTime = 1E9/fFPS;
int framesToAdvance = elapsed/frameTime;
fFrame = (fFrame + framesToAdvance)%fPlayer->numFrames();
// Instead of just adding elapsed, note the time when this frame should have begun.
fLastFrameTime += framesToAdvance*frameTime;
return framesToAdvance%fPlayer->numFrames() != 0;
}
void MSKPSlide::load(SkScalar, SkScalar) {
if (!fStream) {
SkDebugf("No skp stream for slide %s.\n", fName.c_str());
return;
}
fStream->rewind();
fPlayer = MSKPPlayer::Make(fStream.get());
if (!fPlayer) {
SkDebugf("Could parse MSKP from stream for slide %s.\n", fName.c_str());
return;
}
}
void MSKPSlide::unload() { fPlayer.reset(); }
void MSKPSlide::gpuTeardown() { fPlayer->resetLayers(); }