Bug 1890277: part 4) Add CSPParser support for the `trusted-types` directive, guarded...
[gecko.git] / image / AnimationSurfaceProvider.h
blob920638279ec244b51bbc41b966b786fa73a4c2bf
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/. */
6 /**
7 * An ISurfaceProvider for animated images.
8 */
10 #ifndef mozilla_image_AnimationSurfaceProvider_h
11 #define mozilla_image_AnimationSurfaceProvider_h
13 #include "mozilla/UniquePtr.h"
15 #include "Decoder.h"
16 #include "FrameAnimator.h"
17 #include "IDecodingTask.h"
18 #include "ISurfaceProvider.h"
19 #include "AnimationFrameBuffer.h"
21 namespace mozilla {
22 namespace layers {
23 class SharedSurfacesAnimation;
26 namespace image {
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.
33 class AnimationSurfaceProvider final : public ISurfaceProvider,
34 public IDecodingTask,
35 public IDecoderFrameRecycler {
36 public:
37 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(AnimationSurfaceProvider, override)
39 AnimationSurfaceProvider(NotNull<RasterImage*> aImage,
40 const SurfaceKey& aSurfaceKey,
41 NotNull<Decoder*> aDecoder, size_t aCurrentFrame);
43 //////////////////////////////////////////////////////////////////////////////
44 // ISurfaceProvider implementation.
45 //////////////////////////////////////////////////////////////////////////////
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; }
58 protected:
59 DrawableFrameRef DrawableRef(size_t aFrame) override;
60 already_AddRefed<imgFrame> GetFrame(size_t aFrame) override;
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 {}
70 //////////////////////////////////////////////////////////////////////////////
71 // IDecodingTask implementation.
72 //////////////////////////////////////////////////////////////////////////////
74 public:
75 void Run() override;
76 bool ShouldPreferSyncRun() const override;
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; }
82 //////////////////////////////////////////////////////////////////////////////
83 // IDecoderFrameRecycler implementation.
84 //////////////////////////////////////////////////////////////////////////////
86 public:
87 RawAccessFrameRef RecycleFrame(gfx::IntRect& aRecycleRect) override;
89 //////////////////////////////////////////////////////////////////////////////
90 // IDecoderFrameRecycler implementation.
91 //////////////////////////////////////////////////////////////////////////////
93 public:
94 nsresult UpdateKey(layers::RenderRootStateManager* aManager,
95 wr::IpcResourceUpdateQueue& aResources,
96 wr::ImageKey& aKey) override;
98 private:
99 virtual ~AnimationSurfaceProvider();
101 void DropImageReference();
102 void AnnounceSurfaceAvailable();
103 void FinishDecoding();
104 void RequestFrameDiscarding();
106 // @returns Whether or not we should continue decoding.
107 bool CheckForNewFrameAtYield();
109 // @returns Whether or not we should restart decoding.
110 bool CheckForNewFrameAtTerminalState();
112 /// The image associated with our decoder.
113 RefPtr<RasterImage> mImage;
115 /// A mutex to protect mDecoder. Always taken before mFramesMutex.
116 mutable Mutex mDecodingMutex MOZ_UNANNOTATED;
118 /// The decoder used to decode this animation.
119 RefPtr<Decoder> mDecoder;
121 /// A mutex to protect mFrames. Always taken after mDecodingMutex.
122 mutable Mutex mFramesMutex MOZ_UNANNOTATED;
124 /// The frames of this animation, in order.
125 UniquePtr<AnimationFrameBuffer> mFrames;
127 /// Whether the current frame was requested for display since the last time we
128 /// advanced the animation.
129 bool mCompositedFrameRequested;
132 RefPtr<layers::SharedSurfacesAnimation> mSharedAnimation;
135 } // namespace image
136 } // namespace mozilla
138 #endif // mozilla_image_AnimationSurfaceProvider_h