SkottiePriv.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. * Copyright 2018 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. #ifndef SkottiePriv_DEFINED
  8. #define SkottiePriv_DEFINED
  9. #include "modules/skottie/include/Skottie.h"
  10. #include "include/core/SkFontStyle.h"
  11. #include "include/core/SkString.h"
  12. #include "include/core/SkTypeface.h"
  13. #include "include/private/SkTHash.h"
  14. #include "modules/skottie/include/SkottieProperty.h"
  15. #include "modules/sksg/include/SkSGScene.h"
  16. #include "src/utils/SkUTF.h"
  17. #include <functional>
  18. class SkFontMgr;
  19. namespace skjson {
  20. class ArrayValue;
  21. class ObjectValue;
  22. class Value;
  23. } // namespace skjson
  24. namespace sksg {
  25. class Color;
  26. class Path;
  27. class RenderNode;
  28. class Transform;
  29. } // namespace sksg
  30. namespace skottie {
  31. class TransformAdapter2D;
  32. class TransformAdapter3D;
  33. namespace internal {
  34. using AnimatorScope = sksg::AnimatorList;
  35. class AnimationBuilder final : public SkNoncopyable {
  36. public:
  37. AnimationBuilder(sk_sp<ResourceProvider>, sk_sp<SkFontMgr>, sk_sp<PropertyObserver>,
  38. sk_sp<Logger>, sk_sp<MarkerObserver>,
  39. Animation::Builder::Stats*, const SkSize& size,
  40. float duration, float framerate);
  41. std::unique_ptr<sksg::Scene> parse(const skjson::ObjectValue&);
  42. struct FontInfo {
  43. SkString fFamily,
  44. fStyle;
  45. SkScalar fAscentPct;
  46. sk_sp<SkTypeface> fTypeface;
  47. bool matches(const char family[], const char style[]) const;
  48. };
  49. const FontInfo* findFont(const SkString& name) const;
  50. // This is the workhorse for property binding: depending on whether the property is animated,
  51. // it will either apply immediately or instantiate and attach a keyframe animator.
  52. template <typename T>
  53. bool bindProperty(const skjson::Value&,
  54. AnimatorScope*,
  55. std::function<void(const T&)>&&,
  56. const T* default_igore = nullptr) const;
  57. template <typename T>
  58. bool bindProperty(const skjson::Value& jv,
  59. AnimatorScope* ascope,
  60. std::function<void(const T&)>&& apply,
  61. const T& default_ignore) const {
  62. return this->bindProperty(jv, ascope, std::move(apply), &default_ignore);
  63. }
  64. void log(Logger::Level, const skjson::Value*, const char fmt[], ...) const;
  65. sk_sp<sksg::Color> attachColor(const skjson::ObjectValue&, AnimatorScope*,
  66. const char prop_name[]) const;
  67. sk_sp<sksg::Transform> attachMatrix2D(const skjson::ObjectValue&, AnimatorScope*,
  68. sk_sp<sksg::Transform>) const;
  69. sk_sp<sksg::Transform> attachMatrix3D(const skjson::ObjectValue&, AnimatorScope*,
  70. sk_sp<sksg::Transform>,
  71. sk_sp<TransformAdapter3D> = nullptr,
  72. bool precompose_parent = false) const;
  73. sk_sp<sksg::RenderNode> attachOpacity(const skjson::ObjectValue&, AnimatorScope*,
  74. sk_sp<sksg::RenderNode>) const;
  75. sk_sp<sksg::Path> attachPath(const skjson::Value&, AnimatorScope*) const;
  76. bool hasNontrivialBlending() const { return fHasNontrivialBlending; }
  77. private:
  78. struct AttachLayerContext;
  79. struct AttachShapeContext;
  80. struct ImageAssetInfo;
  81. struct LayerInfo;
  82. void parseAssets(const skjson::ArrayValue*);
  83. void parseFonts (const skjson::ObjectValue* jfonts,
  84. const skjson::ArrayValue* jchars);
  85. void dispatchMarkers(const skjson::ArrayValue*) const;
  86. sk_sp<sksg::RenderNode> attachComposition(const skjson::ObjectValue&, AnimatorScope*) const;
  87. sk_sp<sksg::RenderNode> attachLayer(const skjson::ObjectValue*,
  88. AnimatorScope*,
  89. AttachLayerContext*) const;
  90. sk_sp<sksg::RenderNode> attachBlendMode(const skjson::ObjectValue&,
  91. sk_sp<sksg::RenderNode>) const;
  92. sk_sp<sksg::RenderNode> attachShape(const skjson::ArrayValue*, AttachShapeContext*) const;
  93. sk_sp<sksg::RenderNode> attachAssetRef(const skjson::ObjectValue&, AnimatorScope*,
  94. const std::function<sk_sp<sksg::RenderNode>(const skjson::ObjectValue&,
  95. AnimatorScope* ctx)>&) const;
  96. const ImageAssetInfo* loadImageAsset(const skjson::ObjectValue&) const;
  97. sk_sp<sksg::RenderNode> attachImageAsset(const skjson::ObjectValue&, LayerInfo*,
  98. AnimatorScope*) const;
  99. sk_sp<sksg::RenderNode> attachNestedAnimation(const char* name, AnimatorScope* ascope) const;
  100. sk_sp<sksg::RenderNode> attachImageLayer (const skjson::ObjectValue&, LayerInfo*,
  101. AnimatorScope*) const;
  102. sk_sp<sksg::RenderNode> attachNullLayer (const skjson::ObjectValue&, LayerInfo*,
  103. AnimatorScope*) const;
  104. sk_sp<sksg::RenderNode> attachPrecompLayer(const skjson::ObjectValue&, LayerInfo*,
  105. AnimatorScope*) const;
  106. sk_sp<sksg::RenderNode> attachShapeLayer (const skjson::ObjectValue&, LayerInfo*,
  107. AnimatorScope*) const;
  108. sk_sp<sksg::RenderNode> attachSolidLayer (const skjson::ObjectValue&, LayerInfo*,
  109. AnimatorScope*) const;
  110. sk_sp<sksg::RenderNode> attachTextLayer (const skjson::ObjectValue&, LayerInfo*,
  111. AnimatorScope*) const;
  112. bool dispatchColorProperty(const sk_sp<sksg::Color>&) const;
  113. bool dispatchOpacityProperty(const sk_sp<sksg::OpacityEffect>&) const;
  114. bool dispatchTransformProperty(const sk_sp<TransformAdapter2D>&) const;
  115. // Delay resolving the fontmgr until it is actually needed.
  116. struct LazyResolveFontMgr {
  117. LazyResolveFontMgr(sk_sp<SkFontMgr> fontMgr) : fFontMgr(std::move(fontMgr)) {}
  118. const sk_sp<SkFontMgr>& get() {
  119. if (!fFontMgr) {
  120. fFontMgr = SkFontMgr::RefDefault();
  121. SkASSERT(fFontMgr);
  122. }
  123. return fFontMgr;
  124. }
  125. const sk_sp<SkFontMgr>& getMaybeNull() const { return fFontMgr; }
  126. private:
  127. sk_sp<SkFontMgr> fFontMgr;
  128. };
  129. class AutoPropertyTracker {
  130. public:
  131. AutoPropertyTracker(const AnimationBuilder* builder, const skjson::ObjectValue& obj)
  132. : fBuilder(builder)
  133. , fPrevContext(builder->fPropertyObserverContext) {
  134. if (fBuilder->fPropertyObserver) {
  135. this->updateContext(builder->fPropertyObserver.get(), obj);
  136. }
  137. }
  138. ~AutoPropertyTracker() {
  139. if (fBuilder->fPropertyObserver) {
  140. fBuilder->fPropertyObserverContext = fPrevContext;
  141. }
  142. }
  143. private:
  144. void updateContext(PropertyObserver*, const skjson::ObjectValue&);
  145. const AnimationBuilder* fBuilder;
  146. const char* fPrevContext;
  147. };
  148. sk_sp<ResourceProvider> fResourceProvider;
  149. LazyResolveFontMgr fLazyFontMgr;
  150. sk_sp<PropertyObserver> fPropertyObserver;
  151. sk_sp<Logger> fLogger;
  152. sk_sp<MarkerObserver> fMarkerObserver;
  153. Animation::Builder::Stats* fStats;
  154. const SkSize fSize;
  155. const float fDuration,
  156. fFrameRate;
  157. mutable const char* fPropertyObserverContext;
  158. mutable bool fHasNontrivialBlending : 1;
  159. struct LayerInfo {
  160. SkSize fSize;
  161. const float fInPoint,
  162. fOutPoint;
  163. };
  164. struct AssetInfo {
  165. const skjson::ObjectValue* fAsset;
  166. mutable bool fIsAttaching; // Used for cycle detection
  167. };
  168. struct ImageAssetInfo {
  169. sk_sp<ImageAsset> fAsset;
  170. SkISize fSize;
  171. };
  172. SkTHashMap<SkString, AssetInfo> fAssets;
  173. SkTHashMap<SkString, FontInfo> fFonts;
  174. mutable SkTHashMap<SkString, ImageAssetInfo> fImageAssetCache;
  175. using INHERITED = SkNoncopyable;
  176. };
  177. struct AnimationBuilder::AttachLayerContext {
  178. explicit AttachLayerContext(const skjson::ArrayValue&);
  179. ~AttachLayerContext();
  180. struct TransformRec {
  181. sk_sp<sksg::Transform> fTransformNode;
  182. AnimatorScope fTransformScope;
  183. };
  184. const skjson::ArrayValue& fLayerList;
  185. SkTHashMap<int, TransformRec> fLayerTransformMap;
  186. sk_sp<sksg::RenderNode> fCurrentMatte;
  187. sk_sp<sksg::Transform> fCameraTransform;
  188. size_t fMotionBlurSamples = 1;
  189. float fMotionBlurAngle = 0,
  190. fMotionBlurPhase = 0;
  191. enum class TransformType { kLayer, kCamera };
  192. TransformRec attachLayerTransform(const skjson::ObjectValue& jlayer,
  193. const AnimationBuilder* abuilder,
  194. TransformType type = TransformType::kLayer);
  195. bool hasMotionBlur(const skjson::ObjectValue& jlayer) const;
  196. private:
  197. sk_sp<sksg::Transform> attachParentLayerTransform(const skjson::ObjectValue& jlayer,
  198. const AnimationBuilder* abuilder,
  199. int layer_index);
  200. sk_sp<sksg::Transform> attachTransformNode(const skjson::ObjectValue& jlayer,
  201. const AnimationBuilder* abuilder,
  202. AnimatorScope*,
  203. sk_sp<sksg::Transform> parent_transform,
  204. TransformType type) const;
  205. TransformRec* attachLayerTransformImpl(const skjson::ObjectValue& jlayer,
  206. const AnimationBuilder* abuilder,
  207. TransformType type, int layer_index);
  208. };
  209. } // namespace internal
  210. } // namespace skottie
  211. #endif // SkottiePriv_DEFINED