Skia
2D Graphics Library
SlotManager.h
Go to the documentation of this file.
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/SkString.h"
13 #include "include/private/base/SkTArray.h"
14 #include "modules/skottie/src/SkottieValue.h"
15 #include "src/core/SkTHash.h"
16 
17 #include <optional>
18 
19 namespace skjson {
20 class ObjectValue;
21 }
22 
23 namespace skresources {
24 class ImageAsset;
25 }
26 
27 namespace sksg {
28 class Node;
29 }
30 namespace skottie {
31 
32 struct TextPropertyValue;
33 
34 namespace internal {
35 class AnimationBuilder;
36 class SceneGraphRevalidator;
37 class AnimatablePropertyContainer;
38 class TextAdapter;
39 } // namespace internal
40 
41 using namespace skia_private;
42 
43 class SK_API SlotManager final : public SkRefCnt {
44 
45 public:
46  using SlotID = SkString;
47 
49  ~SlotManager() override;
50 
51  bool setColorSlot(const SlotID&, SkColor);
53  bool setScalarSlot(const SlotID&, float);
54  bool setVec2Slot(const SlotID&, SkV2);
55  bool setTextSlot(const SlotID&, const TextPropertyValue&);
56 
57  std::optional<SkColor> getColorSlot(const SlotID&) const;
59  std::optional<float> getScalarSlot(const SlotID&) const;
60  std::optional<SkV2> getVec2Slot(const SlotID&) const;
61  std::optional<TextPropertyValue> getTextSlot(const SlotID&) const;
62 
63  struct SlotInfo {
64  TArray<SlotID> fColorSlotIDs;
65  TArray<SlotID> fScalarSlotIDs;
66  TArray<SlotID> fVec2SlotIDs;
67  TArray<SlotID> fImageSlotIDs;
68  TArray<SlotID> fTextSlotIDs;
69  };
70 
71  // Helper function to get all slot IDs and their value types
73 
74 private:
75 
76  // pass value to the SlotManager for manipulation and node for invalidation
77  void trackColorValue(const SlotID&, ColorValue*,
80  void trackScalarValue(const SlotID&, ScalarValue*,
82  void trackVec2Value(const SlotID&, Vec2Value*,
84  void trackTextValue(const SlotID&, sk_sp<skottie::internal::TextAdapter>);
85 
86  // ValuePair tracks a pointer to a value to change, and a means to invalidate the render tree.
87  // For the latter, we can take either a node in the scene graph that directly the scene graph,
88  // or an adapter which takes the value passed and interprets it before pushing to the scene
89  // (clamping, normalizing, etc.)
90  // Only one should be set, it is UB to create a ValuePair with both a node and an adapter.
91  template <typename T>
92  struct ValuePair
93  {
94  T value;
96  };
97 
98  class ImageAssetProxy;
99  template <typename T>
100  using SlotMap = THashMap<SlotID, TArray<T>>;
101 
102  SlotMap<ValuePair<ColorValue*>> fColorMap;
103  SlotMap<ValuePair<ScalarValue*>> fScalarMap;
104  SlotMap<ValuePair<Vec2Value*>> fVec2Map;
105  SlotMap<sk_sp<ImageAssetProxy>> fImageMap;
106  SlotMap<sk_sp<skottie::internal::TextAdapter>> fTextMap;
107 
109 
110  friend class skottie::internal::AnimationBuilder;
111  friend class skottie::internal::AnimatablePropertyContainer;
112 };
113 
114 } // namespace skottie
115 
116 #endif // SlotManager_DEFINED
Types, consts, functions, and macros for colors.
uint32_t SkColor
32-bit ARGB color value, unpremultiplied.
Definition: SkColor.h:37
Definition: SkRefCnt.h:119
Light weight class for managing strings.
Definition: SkString.h:118
Definition: SlotManager.h:43
sk_sp< const skresources::ImageAsset > getImageSlot(const SlotID &) const
bool setColorSlot(const SlotID &, SkColor)
std::optional< float > getScalarSlot(const SlotID &) const
SlotManager(sk_sp< skottie::internal::SceneGraphRevalidator >)
bool setScalarSlot(const SlotID &, float)
~SlotManager() override
SlotInfo getSlotInfo() const
bool setImageSlot(const SlotID &, const sk_sp< skresources::ImageAsset > &)
bool setVec2Slot(const SlotID &, SkV2)
std::optional< SkColor > getColorSlot(const SlotID &) const
bool setTextSlot(const SlotID &, const TextPropertyValue &)
std::optional< SkV2 > getVec2Slot(const SlotID &) const
std::optional< TextPropertyValue > getTextSlot(const SlotID &) const
Definition: Skottie.h:28
Definition: ExternalLayer.h:16
skresources::ImageAsset ImageAsset
Definition: Skottie.h:41
Definition: SlotManager.h:23
Definition: Skottie.h:30
Definition: SkM44.h:19
Definition: SlotManager.h:63
TArray< SlotID > fTextSlotIDs
Definition: SlotManager.h:68
TArray< SlotID > fColorSlotIDs
Definition: SlotManager.h:64
TArray< SlotID > fVec2SlotIDs
Definition: SlotManager.h:66
TArray< SlotID > fImageSlotIDs
Definition: SlotManager.h:67
TArray< SlotID > fScalarSlotIDs
Definition: SlotManager.h:65
Definition: SkottieProperty.h:63