Bug 1768646 [wpt PR 34018] - [mq-4] Enable CSSMediaQueries4 for stable, a=testonly
[gecko.git] / image / IDecodingTask.h
blobb3bce74757e4c7548bb774d517efd228c829a371
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 interface for tasks which can execute on the ImageLib DecodePool, and
8 * various implementations.
9 */
11 #ifndef mozilla_image_IDecodingTask_h
12 #define mozilla_image_IDecodingTask_h
14 #include "imgFrame.h"
15 #include "mozilla/NotNull.h"
16 #include "mozilla/RefPtr.h"
17 #include "nsIEventTarget.h"
18 #include "SourceBuffer.h"
20 namespace mozilla {
21 namespace image {
23 class Decoder;
24 class RasterImage;
26 /// A priority hint that DecodePool can use when scheduling an IDecodingTask.
27 enum class TaskPriority : uint8_t { eLow, eHigh };
29 /**
30 * An interface for tasks which can execute on the ImageLib DecodePool.
32 class IDecodingTask : public IResumable {
33 public:
34 /// Run the task.
35 virtual void Run() = 0;
37 /// @return true if, given the option, this task prefers to run synchronously.
38 virtual bool ShouldPreferSyncRun() const = 0;
40 /// @return a priority hint that DecodePool can use when scheduling this task.
41 virtual TaskPriority Priority() const = 0;
43 /// A default implementation of IResumable which resubmits the task to the
44 /// DecodePool. Subclasses can override this if they need different behavior.
45 void Resume() override;
47 protected:
48 virtual ~IDecodingTask() {}
50 /// Notify @aImage of @aDecoder's progress.
51 void NotifyProgress(NotNull<RasterImage*> aImage, NotNull<Decoder*> aDecoder);
53 /// Notify @aImage that @aDecoder has finished.
54 void NotifyDecodeComplete(NotNull<RasterImage*> aImage,
55 NotNull<Decoder*> aDecoder);
57 private:
58 void EnsureHasEventTarget(NotNull<RasterImage*> aImage);
60 bool IsOnEventTarget() const;
62 nsCOMPtr<nsIEventTarget> mEventTarget;
65 /**
66 * An IDecodingTask implementation for metadata decodes of images.
68 class MetadataDecodingTask final : public IDecodingTask {
69 public:
70 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(MetadataDecodingTask, override)
72 explicit MetadataDecodingTask(NotNull<Decoder*> aDecoder);
74 void Run() override;
76 // Metadata decodes are very fast (since they only need to examine an image's
77 // header) so there's no reason to refuse to run them synchronously if the
78 // caller will allow us to.
79 bool ShouldPreferSyncRun() const override { return true; }
81 // Metadata decodes run at the highest priority because they block layout and
82 // page load.
83 TaskPriority Priority() const override { return TaskPriority::eHigh; }
85 private:
86 virtual ~MetadataDecodingTask() {}
88 /// Mutex protecting access to mDecoder.
89 Mutex mMutex MOZ_UNANNOTATED;
91 NotNull<RefPtr<Decoder>> mDecoder;
94 /**
95 * An IDecodingTask implementation for anonymous decoders - that is, decoders
96 * with no associated Image object.
98 class AnonymousDecodingTask final : public IDecodingTask {
99 public:
100 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(AnonymousDecodingTask, override)
102 explicit AnonymousDecodingTask(NotNull<Decoder*> aDecoder, bool aResumable);
104 void Run() override;
106 bool ShouldPreferSyncRun() const override { return true; }
107 TaskPriority Priority() const override { return TaskPriority::eLow; }
109 void Resume() override;
111 private:
112 virtual ~AnonymousDecodingTask() {}
114 NotNull<RefPtr<Decoder>> mDecoder;
115 bool mResumable;
118 } // namespace image
119 } // namespace mozilla
121 #endif // mozilla_image_IDecodingTask_h