Skip to content

Commit

Permalink
Copy SkottieUtils' classes into skresources
Browse files Browse the repository at this point in the history
For now, we'll have two copies. Once clients are using the skresources
versions exclusively, we can remove the originals from skottie.

Change-Id: I3152f526b0505b8374bdd9b4513a80bddc702ccc
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/256416
Commit-Queue: Brian Osman <[email protected]>
Reviewed-by: Florin Malita <[email protected]>
Reviewed-by: Kevin Lubick <[email protected]>
  • Loading branch information
brianosman authored and Skia Commit-Bot committed Nov 26, 2019
1 parent 40a7dfc commit 849f4d6
Show file tree
Hide file tree
Showing 11 changed files with 300 additions and 41 deletions.
6 changes: 3 additions & 3 deletions dm/DMSrcSink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@

#if defined(SK_ENABLE_SKOTTIE)
#include "modules/skottie/include/Skottie.h"
#include "modules/skottie/utils/SkottieUtils.h"
#include "modules/skresources/include/SkResources.h"
#endif

#if defined(SK_XML)
Expand Down Expand Up @@ -1116,8 +1116,8 @@ SkottieSrc::SkottieSrc(Path path) : fPath(std::move(path)) {}
Error SkottieSrc::draw(SkCanvas* canvas) const {
auto animation = skottie::Animation::Builder()
.setResourceProvider(
skottie_utils::DataURIResourceProviderProxy::Make(
skottie_utils::FileResourceProvider::Make(SkOSPath::Dirname(fPath.c_str()),
skresources::DataURIResourceProviderProxy::Make(
skresources::FileResourceProvider::Make(SkOSPath::Dirname(fPath.c_str()),
/*predecode=*/true),
/*predecode=*/true))
.makeFromFile(fPath.c_str());
Expand Down
3 changes: 2 additions & 1 deletion modules/canvaskit/compile.sh
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ fi

MANAGED_SKOTTIE_BINDINGS="\
-DSK_INCLUDE_MANAGED_SKOTTIE=1 \
modules/skottie/utils/SkottieUtils.cpp"
modules/skottie/utils/SkottieUtils.cpp \
modules/skresources/src/SkResources.cpp"
if [[ $@ == *no_managed_skottie* ]]; then
echo "Omitting managed Skottie"
MANAGED_SKOTTIE_BINDINGS="-DSK_INCLUDE_MANAGED_SKOTTIE=0"
Expand Down
5 changes: 3 additions & 2 deletions modules/canvaskit/skottie_bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#if SK_INCLUDE_MANAGED_SKOTTIE
#include "modules/skottie/include/SkottieProperty.h"
#include "modules/skottie/utils/SkottieUtils.h"
#include "modules/skresources/include/SkResources.h"
#endif // SK_INCLUDE_MANAGED_SKOTTIE

using namespace emscripten;
Expand Down Expand Up @@ -53,7 +54,7 @@ class SkottieAssetProvider : public skottie::ResourceProvider {
const char[] /* id */) const override {
// For CK/Skottie we ignore paths & IDs, and identify images based solely on name.
if (auto data = this->findAsset(name)) {
return skottie_utils::MultiFrameImageAsset::Make(std::move(data));
return skresources::MultiFrameImageAsset::Make(std::move(data));
}

return nullptr;
Expand Down Expand Up @@ -246,7 +247,7 @@ EMSCRIPTEN_BINDINGS(Skottie) {
}

return ManagedAnimation::Make(json,
skottie_utils::DataURIResourceProviderProxy::Make(
skresources::DataURIResourceProviderProxy::Make(
SkottieAssetProvider::Make(std::move(assets))));
}));
constant("managed_skottie", true);
Expand Down
36 changes: 17 additions & 19 deletions modules/skottie/gm/SkottieGM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,20 @@
#include "modules/skottie/include/Skottie.h"
#include "modules/skottie/include/SkottieProperty.h"
#include "modules/skottie/utils/SkottieUtils.h"
#include "modules/skresources/include/SkResources.h"
#include "src/core/SkMakeUnique.h"
#include "tools/Resources.h"

#include <cmath>
#include <vector>

using namespace skottie;

namespace {

static constexpr char kWebFontResource[] = "fonts/Roboto-Regular.ttf";
static constexpr char kSkottieResource[] = "skottie/skottie_sample_webfont.json";

// Dummy web font loader which serves a single local font (checked in under resources/).
class FakeWebFontProvider final : public ResourceProvider {
class FakeWebFontProvider final : public skresources::ResourceProvider {
public:
FakeWebFontProvider() : fFontData(GetResourceAsData(kWebFontResource)) {}

Expand All @@ -36,7 +35,7 @@ class FakeWebFontProvider final : public ResourceProvider {
private:
sk_sp<SkData> fFontData;

using INHERITED = ResourceProvider;
using INHERITED = skresources::ResourceProvider;
};

} // namespace
Expand All @@ -54,7 +53,7 @@ class SkottieWebFontGM : public skiagm::GM {

void onOnceBeforeDraw() override {
if (auto stream = GetResourceAsStream(kSkottieResource)) {
fAnimation = Animation::Builder()
fAnimation = skottie::Animation::Builder()
.setResourceProvider(sk_make_sp<FakeWebFontProvider>())
.make(stream.get());
}
Expand Down Expand Up @@ -84,15 +83,13 @@ class SkottieWebFontGM : public skiagm::GM {
private:
static constexpr SkScalar kSize = 800;

sk_sp<Animation> fAnimation;
sk_sp<skottie::Animation> fAnimation;

using INHERITED = skiagm::GM;
};

DEF_GM(return new SkottieWebFontGM;)

using namespace skottie_utils;

class SkottieColorizeGM : public skiagm::GM {
protected:
SkString onShortName() override {
Expand All @@ -105,8 +102,8 @@ class SkottieColorizeGM : public skiagm::GM {

void onOnceBeforeDraw() override {
if (auto stream = GetResourceAsStream("skottie/skottie_sample_search.json")) {
fPropManager = skstd::make_unique<CustomPropertyManager>();
fAnimation = Animation::Builder()
fPropManager = skstd::make_unique<skottie_utils::CustomPropertyManager>();
fAnimation = skottie::Animation::Builder()
.setPropertyObserver(fPropManager->getPropertyObserver())
.make(stream.get());
fColors = fPropManager->getColorProps();
Expand Down Expand Up @@ -157,10 +154,10 @@ class SkottieColorizeGM : public skiagm::GM {
private:
static constexpr SkScalar kSize = 800;

sk_sp<Animation> fAnimation;
std::unique_ptr<CustomPropertyManager> fPropManager;
std::vector<CustomPropertyManager::PropKey> fColors;
size_t fColorIndex = 0;
sk_sp<skottie::Animation> fAnimation;
std::unique_ptr<skottie_utils::CustomPropertyManager> fPropManager;
std::vector<skottie_utils::CustomPropertyManager::PropKey> fColors;
size_t fColorIndex = 0;

using INHERITED = skiagm::GM;
};
Expand All @@ -180,7 +177,7 @@ class SkottieMultiFrameGM : public skiagm::GM {

void onOnceBeforeDraw() override {
if (auto stream = GetResourceAsStream("skottie/skottie_sample_multiframe.json")) {
fAnimation = Animation::Builder()
fAnimation = skottie::Animation::Builder()
.setResourceProvider(sk_make_sp<MultiFrameResourceProvider>())
.make(stream.get());
}
Expand Down Expand Up @@ -208,17 +205,18 @@ class SkottieMultiFrameGM : public skiagm::GM {
}

private:
class MultiFrameResourceProvider final : public skottie::ResourceProvider {
class MultiFrameResourceProvider final : public skresources::ResourceProvider {
public:
sk_sp<ImageAsset> loadImageAsset(const char[], const char[], const char[]) const override {
return skottie_utils::MultiFrameImageAsset::Make(
sk_sp<skresources::ImageAsset> loadImageAsset(const char[], const char[],
const char[]) const override {
return skresources::MultiFrameImageAsset::Make(
GetResourceAsData("images/flightAnim.gif"));
}
};

static constexpr SkScalar kSize = 800;

sk_sp<Animation> fAnimation;
sk_sp<skottie::Animation> fAnimation;

using INHERITED = skiagm::GM;
};
Expand Down
8 changes: 4 additions & 4 deletions modules/skottie/src/SkottieTool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include "include/core/SkSurface.h"
#include "include/encode/SkPngEncoder.h"
#include "modules/skottie/include/Skottie.h"
#include "modules/skottie/utils/SkottieUtils.h"
#include "modules/skresources/include/SkResources.h"
#include "src/core/SkMakeUnique.h"
#include "src/core/SkOSFile.h"
#include "src/core/SkTaskGroup.h"
Expand Down Expand Up @@ -260,9 +260,9 @@ int main(int argc, char** argv) {
}

auto logger = sk_make_sp<Logger>();
auto rp = skottie_utils::CachingResourceProvider::Make(
skottie_utils::DataURIResourceProviderProxy::Make(
skottie_utils::FileResourceProvider::Make(SkOSPath::Dirname(FLAGS_input[0]),
auto rp = skresources::CachingResourceProvider::Make(
skresources::DataURIResourceProviderProxy::Make(
skresources::FileResourceProvider::Make(SkOSPath::Dirname(FLAGS_input[0]),
/*predecode=*/true),
/*predecode=*/true));
auto data = SkData::MakeFromFileName(FLAGS_input[0]);
Expand Down
92 changes: 92 additions & 0 deletions modules/skresources/include/SkResources.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,14 @@

#include "include/core/SkData.h"
#include "include/core/SkRefCnt.h"
#include "include/core/SkString.h"
#include "include/core/SkTypes.h"
#include "include/private/SkMutex.h"
#include "include/private/SkTHash.h"

#include <memory>

class SkAnimCodecPlayer;
class SkImage;

namespace skresources {
Expand Down Expand Up @@ -40,6 +46,30 @@ class SK_API ImageAsset : public SkRefCnt {
virtual sk_sp<SkImage> getFrame(float t) = 0;
};

class MultiFrameImageAsset final : public ImageAsset {
public:
/**
* By default, images are decoded on-the-fly, at rasterization time.
* Large images may cause jank as decoding is expensive (and can thrash internal caches).
*
* Pass |predecode| true to force-decode all images upfront, at the cost of potentially more RAM
* and slower animation build times.
*/
static sk_sp<MultiFrameImageAsset> Make(sk_sp<SkData>, bool predecode = false);

bool isMultiFrame() override;

sk_sp<SkImage> getFrame(float t) override;

private:
explicit MultiFrameImageAsset(std::unique_ptr<SkAnimCodecPlayer>, bool predecode);

std::unique_ptr<SkAnimCodecPlayer> fPlayer;
bool fPreDecode;

using INHERITED = ImageAsset;
};

/**
* ResourceProvider is an interface that lets rich-content modules defer loading of external
* resources (images, fonts, etc.) to embedding clients.
Expand Down Expand Up @@ -84,6 +114,68 @@ class SK_API ResourceProvider : public SkRefCnt {
}
};

class FileResourceProvider final : public ResourceProvider {
public:
static sk_sp<FileResourceProvider> Make(SkString base_dir, bool predecode = false);

sk_sp<SkData> load(const char resource_path[], const char resource_name[]) const override;

sk_sp<ImageAsset> loadImageAsset(const char[], const char[], const char[]) const override;

private:
FileResourceProvider(SkString, bool);

const SkString fDir;
const bool fPredecode;

using INHERITED = ResourceProvider;
};

class ResourceProviderProxyBase : public ResourceProvider {
protected:
explicit ResourceProviderProxyBase(sk_sp<ResourceProvider>);

sk_sp<SkData> load(const char[], const char[]) const override;
sk_sp<ImageAsset> loadImageAsset(const char[], const char[], const char[]) const override;
sk_sp<SkData> loadFont(const char[], const char[]) const override;

private:
const sk_sp<ResourceProvider> fProxy;
};

class CachingResourceProvider final : public ResourceProviderProxyBase {
public:
static sk_sp<CachingResourceProvider> Make(sk_sp<ResourceProvider> rp) {
return rp ? sk_sp<CachingResourceProvider>(new CachingResourceProvider(std::move(rp)))
: nullptr;
}

private:
explicit CachingResourceProvider(sk_sp<ResourceProvider>);

sk_sp<ImageAsset> loadImageAsset(const char[], const char[], const char[]) const override;

mutable SkMutex fMutex;
mutable SkTHashMap<SkString, sk_sp<ImageAsset>> fImageCache;

using INHERITED = ResourceProviderProxyBase;
};

class DataURIResourceProviderProxy final : public ResourceProviderProxyBase {
public:
static sk_sp<DataURIResourceProviderProxy> Make(sk_sp<ResourceProvider> rp,
bool predecode = false);

private:
DataURIResourceProviderProxy(sk_sp<ResourceProvider>, bool);

sk_sp<ImageAsset> loadImageAsset(const char[], const char[], const char[]) const override;

const bool fPredecode;

using INHERITED = ResourceProviderProxyBase;
};

} // namespace skresources

#endif // SkResources_DEFINED
Loading

0 comments on commit 849f4d6

Please sign in to comment.