[skottie] Add custom props rendering GM
Also fix a couple of custom props issues: - solid layer colors were not dispatched - text values were not sync'ed TBR= Change-Id: I827f8c1d8c8bb73b03f05de15e1c7c96753a631e Reviewed-on: https://skia-review.googlesource.com/c/skia/+/264936 Reviewed-by: Florin Malita <fmalita@chromium.org> Commit-Queue: Florin Malita <fmalita@chromium.org>
This commit is contained in:
parent
d58b643f10
commit
7c7cd30550
@ -132,6 +132,7 @@ if (skia_enable_skottie) {
|
||||
configs += [ "../..:skia_private" ]
|
||||
sources = [
|
||||
"gm/3dgm.cpp",
|
||||
"gm/ExternalProperties.cpp",
|
||||
"gm/SkottieGM.cpp",
|
||||
]
|
||||
|
||||
|
136
modules/skottie/gm/ExternalProperties.cpp
Normal file
136
modules/skottie/gm/ExternalProperties.cpp
Normal file
@ -0,0 +1,136 @@
|
||||
/*
|
||||
* 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 "gm/gm.h"
|
||||
#include "include/core/SkColor.h"
|
||||
#include "include/utils/SkAnimCodecPlayer.h"
|
||||
#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 "tools/Resources.h"
|
||||
|
||||
#include <cmath>
|
||||
#include <vector>
|
||||
|
||||
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 skresources::ResourceProvider {
|
||||
public:
|
||||
FakeWebFontProvider() : fFontData(GetResourceAsData(kWebFontResource)) {}
|
||||
|
||||
sk_sp<SkData> loadFont(const char[], const char[]) const override {
|
||||
return fFontData;
|
||||
}
|
||||
|
||||
private:
|
||||
sk_sp<SkData> fFontData;
|
||||
|
||||
using INHERITED = skresources::ResourceProvider;
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
class SkottieExternalPropsGM : public skiagm::GM {
|
||||
public:
|
||||
protected:
|
||||
SkString onShortName() override {
|
||||
return SkString("skottie_external_props");
|
||||
}
|
||||
|
||||
SkISize onISize() override {
|
||||
return SkISize::Make(kSize, kSize);
|
||||
}
|
||||
|
||||
void onOnceBeforeDraw() override {
|
||||
if (auto stream = GetResourceAsStream(kSkottieResource)) {
|
||||
fPropManager = std::make_unique<skottie_utils::CustomPropertyManager>();
|
||||
fAnimation = skottie::Animation::Builder()
|
||||
.setResourceProvider(sk_make_sp<FakeWebFontProvider>())
|
||||
.setPropertyObserver(fPropManager->getPropertyObserver())
|
||||
.make(stream.get());
|
||||
}
|
||||
}
|
||||
|
||||
DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
|
||||
if (!fAnimation) {
|
||||
*errorMsg = "No animation";
|
||||
return DrawResult::kFail;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < 4; ++i) {
|
||||
this->update_props(i);
|
||||
auto dest = SkRect::MakeWH(kSize/2, kSize/2).makeOffset(kSize * (i & 1) / 2,
|
||||
kSize * (i & 2) / 4);
|
||||
fAnimation->render(canvas, &dest);
|
||||
}
|
||||
return DrawResult::kOk;
|
||||
}
|
||||
|
||||
bool onAnimate(double nanos) override {
|
||||
if (!fAnimation) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const auto duration = fAnimation->duration();
|
||||
fAnimation->seek(std::fmod(1e-9 * nanos, duration) / duration);
|
||||
return true;
|
||||
}
|
||||
|
||||
private:
|
||||
void update_props(size_t i) {
|
||||
|
||||
SkASSERT(i < 4);
|
||||
if (!i) {
|
||||
return;
|
||||
}
|
||||
|
||||
static constexpr struct {
|
||||
const char* txt_string;
|
||||
SkColor txt_color,
|
||||
solid_color;
|
||||
float transform_scale;
|
||||
} gTests[] = {
|
||||
{ "update #1", SK_ColorRED , SK_ColorYELLOW, 100.f },
|
||||
{ "update #2", SK_ColorGREEN , SK_ColorBLUE , 50.f },
|
||||
{ "update #3", SK_ColorMAGENTA, SK_ColorCYAN , 150.f },
|
||||
};
|
||||
|
||||
SkASSERT(i - 1 < SK_ARRAY_COUNT(gTests));
|
||||
const auto& tst = gTests[i - 1];
|
||||
|
||||
for (const auto& prop : fPropManager->getColorProps()) {
|
||||
SkAssertResult(fPropManager->setColor(prop, tst.solid_color));
|
||||
}
|
||||
|
||||
for (const auto& prop : fPropManager->getTransformProps()) {
|
||||
auto t = fPropManager->getTransform(prop);
|
||||
t.fScale = {tst.transform_scale, tst.transform_scale};
|
||||
SkAssertResult(fPropManager->setTransform(prop, t));
|
||||
}
|
||||
|
||||
for (const auto& prop : fPropManager->getTextProps()) {
|
||||
auto txt = fPropManager->getText(prop);
|
||||
txt.fText.set(tst.txt_string);
|
||||
txt.fFillColor = tst.txt_color;
|
||||
SkAssertResult(fPropManager->setText(prop, txt));
|
||||
}
|
||||
}
|
||||
|
||||
static constexpr SkScalar kSize = 800;
|
||||
|
||||
sk_sp<skottie::Animation> fAnimation;
|
||||
std::unique_ptr<skottie_utils::CustomPropertyManager> fPropManager;
|
||||
|
||||
using INHERITED = skiagm::GM;
|
||||
};
|
||||
|
||||
DEF_GM(return new SkottieExternalPropsGM;)
|
@ -36,6 +36,8 @@ sk_sp<sksg::RenderNode> AnimationBuilder::attachSolidLayer(const skjson::ObjectV
|
||||
auto solid_paint = sksg::Color::Make(color);
|
||||
solid_paint->setAntiAlias(true);
|
||||
|
||||
this->dispatchColorProperty(solid_paint);
|
||||
|
||||
return sksg::Draw::Make(sksg::Rect::Make(SkRect::MakeSize(layer_info->fSize)),
|
||||
std::move(solid_paint));
|
||||
}
|
||||
|
@ -177,6 +177,7 @@ void TextAdapter::buildDomainMaps(const Shaper::Result& shape_result) {
|
||||
|
||||
void TextAdapter::setText(const TextValue& txt) {
|
||||
fText.fCurrentValue = txt;
|
||||
this->onSync();
|
||||
}
|
||||
|
||||
void TextAdapter::reshape() {
|
||||
|
@ -37,6 +37,14 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
void onTextProperty(const char node_name[],
|
||||
const LazyHandle<skottie::TextPropertyHandle>& t) override {
|
||||
const auto key = fMgr->acceptKey(node_name);
|
||||
if (!key.empty()) {
|
||||
fMgr->fTextMap[key].push_back(t());
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
CustomPropertyManager* fMgr;
|
||||
};
|
||||
@ -138,9 +146,26 @@ CustomPropertyManager::getTransformProps() const {
|
||||
return this->getProps(fTransformMap);
|
||||
}
|
||||
|
||||
skottie::TransformPropertyValue CustomPropertyManager::getTransform(const PropKey& key) const {
|
||||
return this->get<skottie::TransformPropertyValue>(key, fTransformMap);
|
||||
}
|
||||
|
||||
bool CustomPropertyManager::setTransform(const PropKey& key,
|
||||
const skottie::TransformPropertyValue& t) {
|
||||
return this->set(key, t, fTransformMap);
|
||||
}
|
||||
|
||||
std::vector<CustomPropertyManager::PropKey>
|
||||
CustomPropertyManager::getTextProps() const {
|
||||
return this->getProps(fTextMap);
|
||||
}
|
||||
|
||||
skottie::TextPropertyValue CustomPropertyManager::getText(const PropKey& key) const {
|
||||
return this->get<skottie::TextPropertyValue>(key, fTextMap);
|
||||
}
|
||||
|
||||
bool CustomPropertyManager::setText(const PropKey& key, const skottie::TextPropertyValue& o) {
|
||||
return this->set(key, o, fTextMap);
|
||||
}
|
||||
|
||||
} // namespace skottie_utils
|
||||
|
@ -49,6 +49,10 @@ public:
|
||||
skottie::TransformPropertyValue getTransform(const PropKey&) const;
|
||||
bool setTransform(const PropKey&, const skottie::TransformPropertyValue&);
|
||||
|
||||
std::vector<PropKey> getTextProps() const;
|
||||
skottie::TextPropertyValue getText(const PropKey&) const;
|
||||
bool setText(const PropKey&, const skottie::TextPropertyValue&);
|
||||
|
||||
struct MarkerInfo {
|
||||
std::string name;
|
||||
float t0, t1;
|
||||
@ -94,6 +98,7 @@ private:
|
||||
PropMap<skottie::ColorPropertyHandle> fColorMap;
|
||||
PropMap<skottie::OpacityPropertyHandle> fOpacityMap;
|
||||
PropMap<skottie::TransformPropertyHandle> fTransformMap;
|
||||
PropMap<skottie::TextPropertyHandle> fTextMap;
|
||||
std::vector<MarkerInfo> fMarkers;
|
||||
};
|
||||
|
||||
|
@ -64,7 +64,7 @@
|
||||
]
|
||||
}
|
||||
},
|
||||
"nm": "Deep Cyan Solid 1",
|
||||
"nm": "$Deep Cyan Solid 1",
|
||||
"op": 261,
|
||||
"sc": "#00b794",
|
||||
"sh": 50,
|
||||
@ -118,7 +118,7 @@
|
||||
]
|
||||
}
|
||||
},
|
||||
"nm": "text_center",
|
||||
"nm": "$text_center",
|
||||
"op": 261,
|
||||
"sr": 1,
|
||||
"st": 0,
|
||||
@ -205,7 +205,7 @@
|
||||
]
|
||||
}
|
||||
},
|
||||
"nm": "Deep Cyan Solid 1",
|
||||
"nm": "$Deep Cyan Solid 2",
|
||||
"op": 261,
|
||||
"sc": "#00b794",
|
||||
"sh": 50,
|
||||
@ -259,7 +259,7 @@
|
||||
]
|
||||
}
|
||||
},
|
||||
"nm": "Deep Cyan Solid 1",
|
||||
"nm": "$Deep Cyan Solid 3",
|
||||
"op": 261,
|
||||
"sc": "#00b794",
|
||||
"sh": 50,
|
||||
@ -313,7 +313,7 @@
|
||||
]
|
||||
}
|
||||
},
|
||||
"nm": "text_left",
|
||||
"nm": "$text_left",
|
||||
"op": 261,
|
||||
"sr": 1,
|
||||
"st": 0,
|
||||
@ -400,7 +400,7 @@
|
||||
]
|
||||
}
|
||||
},
|
||||
"nm": "Deep Cyan Solid 1",
|
||||
"nm": "$Deep Cyan Solid 4",
|
||||
"op": 261,
|
||||
"sc": "#00b794",
|
||||
"sh": 50,
|
||||
@ -454,7 +454,7 @@
|
||||
]
|
||||
}
|
||||
},
|
||||
"nm": "Deep Cyan Solid 1",
|
||||
"nm": "$Deep Cyan Solid 5",
|
||||
"op": 261,
|
||||
"sc": "#00b794",
|
||||
"sh": 50,
|
||||
@ -508,7 +508,7 @@
|
||||
]
|
||||
}
|
||||
},
|
||||
"nm": "text_right",
|
||||
"nm": "$text_right",
|
||||
"op": 261,
|
||||
"sr": 1,
|
||||
"st": 0,
|
||||
@ -595,7 +595,7 @@
|
||||
]
|
||||
}
|
||||
},
|
||||
"nm": "Deep Cyan Solid 1",
|
||||
"nm": "$Deep Cyan Solid 6",
|
||||
"op": 261,
|
||||
"sc": "#00b794",
|
||||
"sh": 50,
|
||||
|
Loading…
Reference in New Issue
Block a user