skottie_mru_resource_provider.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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_MRU_RESOURCE_PROVIDER_H_
  5. #define CC_PAINT_SKOTTIE_MRU_RESOURCE_PROVIDER_H_
  6. #include <string>
  7. #include "base/containers/flat_map.h"
  8. #include "base/sequence_checker.h"
  9. #include "cc/paint/paint_export.h"
  10. #include "cc/paint/skottie_resource_metadata.h"
  11. #include "cc/paint/skottie_wrapper.h"
  12. #include "third_party/skia/modules/skresources/include/SkResources.h"
  13. #include "ui/gfx/geometry/size.h"
  14. namespace cc {
  15. // Provides Skottie the most recent SkImage that was returned by a
  16. // SkottieWrapper::FrameDataCallback for each ImageAsset. Note this is a
  17. // "multi-frame" ResourceProvider, so the caller is capable of supporting
  18. // animations where the image assets do/don't change between frames.
  19. //
  20. // Not thread-safe. All public methods must be called from the sequence that
  21. // SkottieMRUResourceProvider is constructed on.
  22. class CC_PAINT_EXPORT SkottieMRUResourceProvider
  23. : public skresources::ResourceProvider {
  24. public:
  25. using FrameDataCallback = SkottieWrapper::FrameDataCallback;
  26. SkottieMRUResourceProvider(FrameDataCallback frame_data_cb,
  27. base::StringPiece animation_json);
  28. SkottieMRUResourceProvider(const SkottieMRUResourceProvider&) = delete;
  29. SkottieMRUResourceProvider& operator=(const SkottieMRUResourceProvider&) =
  30. delete;
  31. ~SkottieMRUResourceProvider() override;
  32. // Contains the metadata for all currently known ImageAssets in the animation.
  33. const SkottieResourceMetadataMap& GetImageAssetMetadata() const;
  34. private:
  35. // skresources::ResourceProvider implementation:
  36. sk_sp<skresources::ImageAsset> loadImageAsset(
  37. const char resource_path[],
  38. const char resource_name[],
  39. const char resource_id[]) const override;
  40. const SkottieWrapper::FrameDataCallback frame_data_cb_;
  41. const base::flat_map</*asset_id*/ std::string, gfx::Size> image_asset_sizes_;
  42. // SkResources.h declares loadImageAsset() as a "const" method. Although the
  43. // method is logically const, these book-keeping members need to be updated in
  44. // that method. Hence, they're marked "mutable".
  45. mutable SkottieResourceMetadataMap image_asset_metadata_
  46. GUARDED_BY_CONTEXT(sequence_checker_);
  47. SEQUENCE_CHECKER(sequence_checker_);
  48. };
  49. } // namespace cc
  50. #endif // CC_PAINT_SKOTTIE_MRU_RESOURCE_PROVIDER_H_