skottie_wrapper.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Copyright 2018 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef CC_PAINT_SKOTTIE_WRAPPER_H_
  5. #define CC_PAINT_SKOTTIE_WRAPPER_H_
  6. #include <string>
  7. #include <vector>
  8. #include "base/callback.h"
  9. #include "base/containers/flat_set.h"
  10. #include "base/containers/span.h"
  11. #include "base/memory/ref_counted.h"
  12. #include "cc/paint/paint_export.h"
  13. #include "cc/paint/skottie_color_map.h"
  14. #include "cc/paint/skottie_marker.h"
  15. #include "cc/paint/skottie_resource_metadata.h"
  16. #include "cc/paint/skottie_text_property_value.h"
  17. #include "cc/paint/skottie_transform_property_value.h"
  18. #include "third_party/skia/include/core/SkRefCnt.h"
  19. #include "third_party/skia/include/core/SkSamplingOptions.h"
  20. class SkCanvas;
  21. class SkImage;
  22. struct SkRect;
  23. struct SkSize;
  24. namespace cc {
  25. // A wrapper over Skia's Skottie object that can be shared by multiple
  26. // SkiaVectorAnimation objects. This class is thread safe when performing a draw
  27. // on an SkCanvas.
  28. //
  29. // The API intentionally does not have any dependencies on the Skottie public
  30. // header files. This is to facilitate a "dummy" implementation for builds where
  31. // the Skottie library should not be compiled/linked into the final binary.
  32. class CC_PAINT_EXPORT SkottieWrapper
  33. : public base::RefCountedThreadSafe<SkottieWrapper> {
  34. public:
  35. // Creates an instance that can be serialized for IPC. This uses additional
  36. // memory to store the raw animation data.
  37. static scoped_refptr<SkottieWrapper> CreateSerializable(
  38. std::vector<uint8_t> data);
  39. // Creates a non serializable instance of the class. This uses less memory.
  40. static scoped_refptr<SkottieWrapper> CreateNonSerializable(
  41. base::span<const uint8_t> data);
  42. SkottieWrapper(const SkottieWrapper&) = delete;
  43. SkottieWrapper& operator=(const SkottieWrapper&) = delete;
  44. // Returns true if this object contains a valid skottie animation.
  45. virtual bool is_valid() const = 0;
  46. // Returns the set of all image assets in the animation and their
  47. // corresponding metadata. The returned map is effectively immutable; it
  48. // does not change during SkottieWrapper's lifetime.
  49. virtual const SkottieResourceMetadataMap& GetImageAssetMetadata() const = 0;
  50. // Returns the set of text nodes in the animation. There shall be an entry in
  51. // GetCurrentTextPropertyValues() for each returned node. The returned set is
  52. // immutable and does not change during SkottieWrapper's lifetime.
  53. virtual const base::flat_set<std::string>& GetTextNodeNames() const = 0;
  54. // Returns a map from hashed animation node name to its current property
  55. // value in the animation (see SkottieProperty.h). Some properties' values
  56. // can be updated via its corresponding argument in Draw().
  57. virtual SkottieTextPropertyValueMap GetCurrentTextPropertyValues() const = 0;
  58. virtual SkottieTransformPropertyValueMap GetCurrentTransformPropertyValues()
  59. const = 0;
  60. virtual SkottieColorMap GetCurrentColorPropertyValues() const = 0;
  61. // Returns all markers present in the animation. The returned list is
  62. // immutable and does not change during SkottieWrapper's lifetime.
  63. virtual const std::vector<SkottieMarker>& GetAllMarkers() const = 0;
  64. // FrameDataCallback is implemented by the caller and invoked
  65. // synchronously during calls to Seek() and Draw(). The callback is used by
  66. // SkottieWrapper to fetch the corresponding image for each asset that is
  67. // present in the frame with the desired timestamp. It is invoked once for
  68. // each asset. A null callback may be passed to Seek() and Draw() if the
  69. // animation is known to not have any image assets.
  70. enum class FrameDataFetchResult {
  71. // A new image is available for the given asset, and the callback's output
  72. // parameters have been filled with the new frame data.
  73. NEW_DATA_AVAILABLE,
  74. // The callback's output parameters have not been filled and will be
  75. // ignored by SkottieWrapper. In this case, SkottieWrapper will reuse the
  76. // frame data that was most recently provided for the given asset (it caches
  77. // this internally). Note it is acceptable to set |image_out| to a null
  78. // SkImage; Skottie will simply skip the image asset while rendering the
  79. // rest of the frame.
  80. NO_UPDATE,
  81. };
  82. // The callback's implementation must synchronously fill the output
  83. // arguments. |asset_id| is guaranteed to be a valid asset that's present
  84. // in GetImageAssetMetadata(). See skresources::ImageAsset::getFrame() for
  85. // the semantics of |t|.
  86. using FrameDataCallback = base::RepeatingCallback<FrameDataFetchResult(
  87. SkottieResourceIdHash asset_id,
  88. float t,
  89. sk_sp<SkImage>& image_out,
  90. SkSamplingOptions& sampling_out)>;
  91. // Seeks to the normalized time instant |t|, but does not render. This method
  92. // is thread safe.
  93. virtual void Seek(float t, FrameDataCallback frame_data_cb) = 0;
  94. // Variant with null FrameDataCallback() if the animation does not have image
  95. // assets.
  96. void Seek(float t);
  97. // A thread safe call that will draw an image with bounds |rect| for the
  98. // frame at normalized time instant |t| onto the |canvas|.
  99. //
  100. // |text_map| may be an incremental update and only contain a subset of the
  101. // text nodes in the animation. If a text node is absent from |text_map|, it
  102. // will maintain the same contents as the previous call to Draw().
  103. virtual void Draw(SkCanvas* canvas,
  104. float t,
  105. const SkRect& rect,
  106. FrameDataCallback frame_data_cb,
  107. const SkottieColorMap& color_map,
  108. const SkottieTextPropertyValueMap& text_map) = 0;
  109. virtual float duration() const = 0;
  110. virtual SkSize size() const = 0;
  111. virtual base::span<const uint8_t> raw_data() const = 0;
  112. virtual uint32_t id() const = 0;
  113. protected:
  114. SkottieWrapper() = default;
  115. virtual ~SkottieWrapper() = default;
  116. private:
  117. friend class base::RefCountedThreadSafe<SkottieWrapper>;
  118. };
  119. } // namespace cc
  120. #endif // CC_PAINT_SKOTTIE_WRAPPER_H_