skottie_frame_data_provider.h 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright 2021 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_FRAME_DATA_PROVIDER_H_
  5. #define CC_PAINT_SKOTTIE_FRAME_DATA_PROVIDER_H_
  6. #include "base/files/file_path.h"
  7. #include "base/memory/ref_counted.h"
  8. #include "base/memory/scoped_refptr.h"
  9. #include "base/strings/string_piece.h"
  10. #include "cc/paint/paint_export.h"
  11. #include "cc/paint/skottie_frame_data.h"
  12. #include "third_party/abseil-cpp/absl/types/optional.h"
  13. #include "ui/gfx/geometry/size.h"
  14. namespace cc {
  15. // A Chromium-specific version of the skresources::ResourceProvider API, which
  16. // allows the code driving the animation to specify which image should be used
  17. // for each asset in each frame of the animation. Callers rendering Skottie
  18. // animations that may have images embedded in them must implement this API. In
  19. // the most basic case where an image asset does not change throughout the
  20. // course of the animation, the same image can be provided for every frame. But
  21. // more complex logic such as providing a different image at the start of each
  22. // animation cycle can be implemented if desired.
  23. //
  24. // Implementations are not required to be thread-safe (the provider and its
  25. // ImageAssets shall always be invoked from the same sequence).
  26. class CC_PAINT_EXPORT SkottieFrameDataProvider {
  27. public:
  28. class CC_PAINT_EXPORT ImageAsset : public base::RefCounted<ImageAsset> {
  29. public:
  30. // Returns the image to use for an asset in a frame of a skottie animation.
  31. // May return a blank SkottieFrameData instance with an empty |image|.
  32. // Skottie handles this gracefully and simply skips the image asset while
  33. // still rendering the rest of the frame.
  34. //
  35. // |t|: See skresources::ImageAsset::getFrame(). Same semantics. Specifies
  36. // the frame of interest in the animation that's about to be rendered.
  37. // |scale_factor|: See |image_scale| in gfx::Canvas. Can be used to generate
  38. // a PaintImage from a gfx::ImageSkia instance.
  39. virtual SkottieFrameData GetFrameData(float t, float scale_factor) = 0;
  40. protected:
  41. virtual ~ImageAsset() = default;
  42. private:
  43. friend class base::RefCounted<ImageAsset>;
  44. };
  45. virtual ~SkottieFrameDataProvider() = default;
  46. // Loads the image asset in the animation with the given |resource_id|, as it
  47. // appears in the lottie json file. The ImageAsset instance that's returned
  48. // for the given |resource_id| gets re-used for the lifetime of the animation;
  49. // LoadImageAsset() is not called multiple times for the same |resource_id|.
  50. // The returned value must never be null.
  51. //
  52. // |size| contains this asset's dimensions as specified in the Lottie
  53. // animation file. Note that the ultimate image(s) returned by GetFrameData()
  54. // are not required to have dimensions that match this |size|. It's provided
  55. // here as a guide that implementations can optionally use to transform
  56. // their images if desired. May be null if the asset didn't have dimensions
  57. // specified in the Lottie file.
  58. virtual scoped_refptr<ImageAsset> LoadImageAsset(
  59. base::StringPiece resource_id,
  60. const base::FilePath& resource_path,
  61. const absl::optional<gfx::Size>& size) = 0;
  62. };
  63. } // namespace cc
  64. #endif // CC_PAINT_SKOTTIE_FRAME_DATA_PROVIDER_H_