AnimationSurfaceProvider.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. /**
  6. * An ISurfaceProvider for animated images.
  7. */
  8. #ifndef mozilla_image_AnimationSurfaceProvider_h
  9. #define mozilla_image_AnimationSurfaceProvider_h
  10. #include "FrameAnimator.h"
  11. #include "IDecodingTask.h"
  12. #include "ISurfaceProvider.h"
  13. namespace mozilla {
  14. namespace image {
  15. /**
  16. * An ISurfaceProvider that manages the decoding of animated images and
  17. * dynamically generates surfaces for the current playback state of the
  18. * animation.
  19. */
  20. class AnimationSurfaceProvider final
  21. : public ISurfaceProvider
  22. , public IDecodingTask
  23. {
  24. public:
  25. NS_INLINE_DECL_THREADSAFE_REFCOUNTING(AnimationSurfaceProvider, override)
  26. AnimationSurfaceProvider(NotNull<RasterImage*> aImage,
  27. const SurfaceKey& aSurfaceKey,
  28. NotNull<Decoder*> aDecoder);
  29. //////////////////////////////////////////////////////////////////////////////
  30. // ISurfaceProvider implementation.
  31. //////////////////////////////////////////////////////////////////////////////
  32. public:
  33. // We use the ISurfaceProvider constructor of DrawableSurface to indicate that
  34. // our surfaces are computed lazily.
  35. DrawableSurface Surface() override { return DrawableSurface(WrapNotNull(this)); }
  36. bool IsFinished() const override;
  37. size_t LogicalSizeInBytes() const override;
  38. void AddSizeOfExcludingThis(MallocSizeOf aMallocSizeOf,
  39. size_t& aHeapSizeOut,
  40. size_t& aNonHeapSizeOut) override;
  41. protected:
  42. DrawableFrameRef DrawableRef(size_t aFrame) override;
  43. // Animation frames are always locked. This is because we only want to release
  44. // their memory atomically (due to the surface cache discarding them). If they
  45. // were unlocked, the OS could end up releasing the memory of random frames
  46. // from the middle of the animation, which is not worth the complexity of
  47. // dealing with.
  48. bool IsLocked() const override { return true; }
  49. void SetLocked(bool) override { }
  50. //////////////////////////////////////////////////////////////////////////////
  51. // IDecodingTask implementation.
  52. //////////////////////////////////////////////////////////////////////////////
  53. public:
  54. void Run() override;
  55. bool ShouldPreferSyncRun() const override;
  56. // Full decodes are low priority compared to metadata decodes because they
  57. // don't block layout or page load.
  58. TaskPriority Priority() const override { return TaskPriority::eLow; }
  59. private:
  60. virtual ~AnimationSurfaceProvider();
  61. void DropImageReference();
  62. void CheckForNewFrameAtYield();
  63. void CheckForNewFrameAtTerminalState();
  64. void AnnounceSurfaceAvailable();
  65. void FinishDecoding();
  66. /// The image associated with our decoder.
  67. RefPtr<RasterImage> mImage;
  68. /// A mutex to protect mDecoder. Always taken before mFramesMutex.
  69. mutable Mutex mDecodingMutex;
  70. /// The decoder used to decode this animation.
  71. RefPtr<Decoder> mDecoder;
  72. /// A mutex to protect mFrames. Always taken after mDecodingMutex.
  73. mutable Mutex mFramesMutex;
  74. /// The frames of this animation, in order.
  75. nsTArray<RawAccessFrameRef> mFrames;
  76. };
  77. } // namespace image
  78. } // namespace mozilla
  79. #endif // mozilla_image_AnimationSurfaceProvider_h