1 /* 2 * Copyright 2023 Google Inc. 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 8 #ifndef SlotManager_DEFINED 9 #define SlotManager_DEFINED 10 11 #include "include/core/SkColor.h" 12 #include "include/core/SkRefCnt.h" 13 #include "include/core/SkString.h" 14 #include "include/private/base/SkAPI.h" 15 #include "include/private/base/SkTArray.h" 16 #include "modules/skottie/src/SkottieValue.h" 17 #include "modules/skottie/src/text/TextAdapter.h" 18 #include "src/core/SkTHash.h" 19 20 #include <optional> 21 22 struct SkV2; 23 24 namespace skresources { 25 class ImageAsset; 26 } 27 28 namespace skottie { 29 30 struct TextPropertyValue; 31 32 namespace internal { 33 class AnimationBuilder; 34 class SceneGraphRevalidator; 35 class AnimatablePropertyContainer; 36 } // namespace internal 37 38 using namespace skia_private; 39 40 class SK_API SlotManager final : public SkRefCnt { 41 42 public: 43 using SlotID = SkString; 44 45 SlotManager(sk_sp<skottie::internal::SceneGraphRevalidator>); 46 ~SlotManager() override; 47 48 bool setColorSlot(const SlotID&, SkColor); 49 bool setImageSlot(const SlotID&, const sk_sp<skresources::ImageAsset>&); 50 bool setScalarSlot(const SlotID&, float); 51 bool setVec2Slot(const SlotID&, SkV2); 52 bool setTextSlot(const SlotID&, const TextPropertyValue&); 53 54 std::optional<SkColor> getColorSlot(const SlotID&) const; 55 sk_sp<const skresources::ImageAsset> getImageSlot(const SlotID&) const; 56 std::optional<float> getScalarSlot(const SlotID&) const; 57 std::optional<SkV2> getVec2Slot(const SlotID&) const; 58 std::optional<TextPropertyValue> getTextSlot(const SlotID&) const; 59 60 struct SlotInfo { 61 TArray<SlotID> fColorSlotIDs; 62 TArray<SlotID> fScalarSlotIDs; 63 TArray<SlotID> fVec2SlotIDs; 64 TArray<SlotID> fImageSlotIDs; 65 TArray<SlotID> fTextSlotIDs; 66 }; 67 68 // Helper function to get all slot IDs and their value types 69 SlotInfo getSlotInfo() const; 70 71 private: 72 73 // pass value to the SlotManager for manipulation and node for invalidation 74 void trackColorValue(const SlotID&, ColorValue*, 75 sk_sp<skottie::internal::AnimatablePropertyContainer>); 76 sk_sp<skresources::ImageAsset> trackImageValue(const SlotID&, sk_sp<skresources::ImageAsset>); 77 void trackScalarValue(const SlotID&, ScalarValue*, 78 sk_sp<skottie::internal::AnimatablePropertyContainer>); 79 void trackVec2Value(const SlotID&, Vec2Value*, 80 sk_sp<skottie::internal::AnimatablePropertyContainer>); 81 void trackTextValue(const SlotID&, sk_sp<skottie::internal::TextAdapter>); 82 83 // ValuePair tracks a pointer to a value to change, and a means to invalidate the render tree. 84 // For the latter, we can take either a node in the scene graph that directly the scene graph, 85 // or an adapter which takes the value passed and interprets it before pushing to the scene 86 // (clamping, normalizing, etc.) 87 // Only one should be set, it is UB to create a ValuePair with both a node and an adapter. 88 template <typename T> 89 struct ValuePair 90 { 91 T value; 92 sk_sp<skottie::internal::AnimatablePropertyContainer> adapter; 93 }; 94 95 class ImageAssetProxy; 96 template <typename T> 97 using SlotMap = THashMap<SlotID, TArray<T>>; 98 99 SlotMap<ValuePair<ColorValue*>> fColorMap; 100 SlotMap<ValuePair<ScalarValue*>> fScalarMap; 101 SlotMap<ValuePair<Vec2Value*>> fVec2Map; 102 SlotMap<sk_sp<ImageAssetProxy>> fImageMap; 103 SlotMap<sk_sp<skottie::internal::TextAdapter>> fTextMap; 104 105 const sk_sp<skottie::internal::SceneGraphRevalidator> fRevalidator; 106 107 friend class skottie::internal::AnimationBuilder; 108 friend class skottie::internal::AnimatablePropertyContainer; 109 }; 110 111 } // namespace skottie 112 113 #endif // SlotManager_DEFINED 114