tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

AnimationSurfaceProvider.h (4867B)


      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 /**
      7 * An ISurfaceProvider for animated images.
      8 */
      9 
     10 #ifndef mozilla_image_AnimationSurfaceProvider_h
     11 #define mozilla_image_AnimationSurfaceProvider_h
     12 
     13 #include "mozilla/UniquePtr.h"
     14 
     15 #include "Decoder.h"
     16 #include "FrameAnimator.h"
     17 #include "IDecodingTask.h"
     18 #include "ISurfaceProvider.h"
     19 #include "AnimationFrameBuffer.h"
     20 
     21 namespace mozilla {
     22 namespace layers {
     23 class SharedSurfacesAnimation;
     24 }
     25 
     26 namespace image {
     27 
     28 /**
     29 * An ISurfaceProvider that manages the decoding of animated images and
     30 * dynamically generates surfaces for the current playback state of the
     31 * animation.
     32 */
     33 class AnimationSurfaceProvider final : public ISurfaceProvider,
     34                                       public IDecodingTask,
     35                                       public IDecoderFrameRecycler {
     36 public:
     37  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(AnimationSurfaceProvider, override)
     38 
     39  AnimationSurfaceProvider(NotNull<RasterImage*> aImage,
     40                           const SurfaceKey& aSurfaceKey,
     41                           NotNull<Decoder*> aDecoder, size_t aCurrentFrame);
     42 
     43  //////////////////////////////////////////////////////////////////////////////
     44  // ISurfaceProvider implementation.
     45  //////////////////////////////////////////////////////////////////////////////
     46 
     47 public:
     48  bool IsFinished() const override;
     49  bool IsFullyDecoded() const override;
     50  size_t LogicalSizeInBytes() const override;
     51  void AddSizeOfExcludingThis(MallocSizeOf aMallocSizeOf,
     52                              const AddSizeOfCb& aCallback) override;
     53  void Reset() override;
     54  void Advance(size_t aFrame) override;
     55  bool MayAdvance() const override { return mCompositedFrameRequested; }
     56  void MarkMayAdvance() override { mCompositedFrameRequested = true; }
     57 
     58 protected:
     59  DrawableFrameRef DrawableRef(size_t aFrame) override;
     60  already_AddRefed<imgFrame> GetFrame(size_t aFrame) override;
     61 
     62  // Animation frames are always locked. This is because we only want to release
     63  // their memory atomically (due to the surface cache discarding them). If they
     64  // were unlocked, the OS could end up releasing the memory of random frames
     65  // from the middle of the animation, which is not worth the complexity of
     66  // dealing with.
     67  bool IsLocked() const override { return true; }
     68  void SetLocked(bool) override {}
     69 
     70  //////////////////////////////////////////////////////////////////////////////
     71  // IDecodingTask implementation.
     72  //////////////////////////////////////////////////////////////////////////////
     73 
     74 public:
     75  void Run() override;
     76  bool ShouldPreferSyncRun() const override;
     77 
     78  // Full decodes are low priority compared to metadata decodes because they
     79  // don't block layout or page load.
     80  TaskPriority Priority() const override { return TaskPriority::eLow; }
     81 
     82  //////////////////////////////////////////////////////////////////////////////
     83  // IDecoderFrameRecycler implementation.
     84  //////////////////////////////////////////////////////////////////////////////
     85 
     86 public:
     87  RawAccessFrameRef RecycleFrame(gfx::IntRect& aRecycleRect) override;
     88 
     89  //////////////////////////////////////////////////////////////////////////////
     90  // IDecoderFrameRecycler implementation.
     91  //////////////////////////////////////////////////////////////////////////////
     92 
     93 public:
     94  nsresult UpdateKey(layers::RenderRootStateManager* aManager,
     95                     wr::IpcResourceUpdateQueue& aResources,
     96                     wr::ImageKey& aKey) override;
     97 
     98 private:
     99  virtual ~AnimationSurfaceProvider();
    100 
    101  void DropImageReference();
    102  void AnnounceSurfaceAvailable();
    103  void FinishDecoding();
    104  void RequestFrameDiscarding();
    105 
    106  // @returns Whether or not we should continue decoding.
    107  bool CheckForNewFrameAtYield();
    108 
    109  // @returns Whether or not we should restart decoding.
    110  bool CheckForNewFrameAtTerminalState();
    111 
    112  /// The image associated with our decoder.
    113  RefPtr<RasterImage> mImage;
    114 
    115  /// A mutex to protect mDecoder. Always taken before mFramesMutex.
    116  mutable Mutex mDecodingMutex MOZ_UNANNOTATED;
    117 
    118  /// The decoder used to decode this animation.
    119  RefPtr<Decoder> mDecoder;
    120 
    121  /// A mutex to protect mFrames. Always taken after mDecodingMutex.
    122  mutable Mutex mFramesMutex MOZ_UNANNOTATED;
    123 
    124  /// The frames of this animation, in order.
    125  UniquePtr<AnimationFrameBuffer> mFrames;
    126 
    127  /// Whether the current frame was requested for display since the last time we
    128  /// advanced the animation.
    129  bool mCompositedFrameRequested;
    130 
    131  ///
    132  RefPtr<layers::SharedSurfacesAnimation> mSharedAnimation;
    133 };
    134 
    135 }  // namespace image
    136 }  // namespace mozilla
    137 
    138 #endif  // mozilla_image_AnimationSurfaceProvider_h