Skip to content

Commit

Permalink
[skottie] pass slotID to Opacity callback
Browse files Browse the repository at this point in the history
This CL effectively relands
https://skia-review.googlesource.com/c/skia/+/675124

We guard the seek call to avoid breaking other team's internal expression tests.

Change-Id: I7fc9f6e9eb8553e11d38d6ef6528f2f5dd3a04d3
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/676896
Commit-Queue: Jorge Betancourt <[email protected]>
Reviewed-by: Florin Malita <[email protected]>
  • Loading branch information
jbeta51 authored and SkCQ committed Apr 25, 2023
1 parent 3575d35 commit 933bac6
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 7 deletions.
16 changes: 12 additions & 4 deletions modules/skottie/src/Skottie.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,11 @@ sk_sp<sksg::RenderNode> AnimationBuilder::attachOpacity(const skjson::ObjectValu
return nullptr;

auto adapter = OpacityAdapter::Make(jobject, child_node, *this);
const auto dispatched = this->dispatchOpacityProperty(adapter->node());

if (adapter->isStatic()) {
adapter->seek(0);
}
const auto dispatched = this->dispatchOpacityProperty(adapter->node(), jobject["o"]);
if (adapter->isStatic()) {
if (!dispatched && adapter->node()->getOpacity() >= 1) {
// No obeservable effects - we can discard.
return child_node;
Expand Down Expand Up @@ -217,11 +218,18 @@ bool AnimationBuilder::dispatchColorProperty(const sk_sp<sksg::Color>& c,
return dispatched;
}

bool AnimationBuilder::dispatchOpacityProperty(const sk_sp<sksg::OpacityEffect>& o) const {
bool AnimationBuilder::dispatchOpacityProperty(const sk_sp<sksg::OpacityEffect>& o,
const skjson::ObjectValue* jopacity) const {
bool dispatched = false;

if (fPropertyObserver) {
fPropertyObserver->onOpacityProperty(fPropertyObserverContext,
const char * node_name = fPropertyObserverContext;
if (jopacity) {
if (const skjson::StringValue* slotID = (*jopacity)["sid"]) {
node_name = slotID->begin();
}
}
fPropertyObserver->onOpacityProperty(node_name,
[&]() {
dispatched = true;
return std::make_unique<OpacityPropertyHandle>(o);
Expand Down
3 changes: 2 additions & 1 deletion modules/skottie/src/SkottiePriv.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,8 @@ class AnimationBuilder final : public SkNoncopyable {

bool dispatchColorProperty(const sk_sp<sksg::Color>&,
const skjson::ObjectValue* jcolor = nullptr) const;
bool dispatchOpacityProperty(const sk_sp<sksg::OpacityEffect>&) const;
bool dispatchOpacityProperty(const sk_sp<sksg::OpacityEffect>&,
const skjson::ObjectValue* jopacity) const;
bool dispatchTextProperty(const sk_sp<TextAdapter>&,
const skjson::ObjectValue* jtext) const;
bool dispatchTransformProperty(const sk_sp<TransformAdapter2D>&) const;
Expand Down
17 changes: 16 additions & 1 deletion modules/skottie/utils/SkottieUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,16 @@ class SlotManager::SlottablePropertyObserver final : public skottie::PropertyObs
}
}

void onOpacityProperty(const char node_name[],
const LazyHandle<skottie::OpacityPropertyHandle>& o) override {
if (node_name) {
const auto it = fOpacityMap.find(node_name);
if (it != fOpacityMap.end()) {
o()->set(it->second);
}
}
}

void onTextProperty(const char node_name[],
const LazyHandle<skottie::TextPropertyHandle>& t) override {
const auto it = fTextMap.find(node_name);
Expand All @@ -303,11 +313,12 @@ class SlotManager::SlottablePropertyObserver final : public skottie::PropertyObs
}
}

// TODO(jmbetancourt): add support for other PropertyObserver callbacks
// TODO(jmbetancourt): add support for onTransformProperty
private:
using SlotID = std::string;

std::unordered_map<SlotID, skottie::ColorPropertyValue> fColorMap;
std::unordered_map<SlotID, SkScalar> fOpacityMap;
std::unordered_map<SlotID, SkString> fTextMap;

friend class SlotManager;
Expand All @@ -323,6 +334,10 @@ void SlotManager::setColorSlot(std::string slotID, SkColor color) {
fPropertyObserver->fColorMap[slotID] = color;
}

void SlotManager::setOpacitySlot(std::string slotID, SkScalar opacity) {
fPropertyObserver->fOpacityMap[slotID] = opacity;
}

void SlotManager::setTextStringSlot(std::string slotID, SkString text) {
fPropertyObserver->fTextMap[slotID] = std::move(text);
}
Expand Down
1 change: 1 addition & 0 deletions modules/skottie/utils/SkottieUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ class SlotManager final : public SkRefCnt {
SlotManager();

void setColorSlot(std::string, SkColor);
void setOpacitySlot(std::string, SkScalar);
void setTextStringSlot(std::string, SkString);
void setImageSlot(std::string, sk_sp<skresources::ImageAsset>);

Expand Down
17 changes: 16 additions & 1 deletion tools/viewer/SkottieSlide.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,18 @@ class SkottieSlide::SlotManagerWrapper {
std::array<char, kBufferLen> s = {'\0'};
fColorSlots.push_back(std::make_pair(s, std::array{1.0f, 1.0f, 1.0f, 1.0f}));
}

ImGui::Text("Opacity Slots");
for (size_t i = 0; i < fOpacitySlots.size(); i++) {
auto& oSlot = fOpacitySlots.at(i);
ImGui::PushID(i);
ImGui::InputText("OpacitySlotID", oSlot.first.data(), oSlot.first.size());
ImGui::InputFloat("Opacity", &(oSlot.second));
ImGui::PopID();
}
if(ImGui::Button("+ Opacity")) {
std::array<char, kBufferLen> s = {'\0'};
fOpacitySlots.push_back(std::make_pair(s, 0.0f));
}
ImGui::Text("Text Slots");
for (size_t i = 0; i < fTextStringSlots.size(); i++) {
auto& tSlot = fTextStringSlots.at(i);
Expand Down Expand Up @@ -316,6 +327,9 @@ class SkottieSlide::SlotManagerWrapper {
fSlotManager->setColorSlot(s.first.data(), SkColor4f{s.second[0], s.second[1],
s.second[2], s.second[3]}.toSkColor());
}
for(const auto& s : fOpacitySlots) {
fSlotManager->setOpacitySlot(s.first.data(), s.second);
}
for(const auto& s : fTextStringSlots) {
fSlotManager->setTextStringSlot(s.first.data(), SkString(s.second.data()));
}
Expand Down Expand Up @@ -355,6 +369,7 @@ class SkottieSlide::SlotManagerWrapper {
using GuiTextBuffer = std::array<char, kBufferLen>;

std::vector<std::pair<GuiTextBuffer, std::array<float, 4>>> fColorSlots;
std::vector<std::pair<GuiTextBuffer, float>> fOpacitySlots;
std::vector<std::pair<GuiTextBuffer, GuiTextBuffer>> fTextStringSlots;
std::vector<std::pair<GuiTextBuffer, std::string>> fImageSlots;

Expand Down

0 comments on commit 933bac6

Please sign in to comment.