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/. */
7 * An interface for tasks which can execute on the ImageLib DecodePool, and
8 * various implementations.
11 #ifndef mozilla_image_IDecodingTask_h
12 #define mozilla_image_IDecodingTask_h
15 #include "mozilla/NotNull.h"
16 #include "mozilla/RefPtr.h"
17 #include "nsIEventTarget.h"
18 #include "SourceBuffer.h"
26 /// A priority hint that DecodePool can use when scheduling an IDecodingTask.
27 enum class TaskPriority
: uint8_t { eLow
, eHigh
};
30 * An interface for tasks which can execute on the ImageLib DecodePool.
32 class IDecodingTask
: public IResumable
{
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
;
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
);
58 void EnsureHasEventTarget(NotNull
<RasterImage
*> aImage
);
60 bool IsOnEventTarget() const;
62 nsCOMPtr
<nsIEventTarget
> mEventTarget
;
66 * An IDecodingTask implementation for metadata decodes of images.
68 class MetadataDecodingTask final
: public IDecodingTask
{
70 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(MetadataDecodingTask
, override
)
72 explicit MetadataDecodingTask(NotNull
<Decoder
*> aDecoder
);
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
83 TaskPriority
Priority() const override
{ return TaskPriority::eHigh
; }
86 virtual ~MetadataDecodingTask() {}
88 /// Mutex protecting access to mDecoder.
89 Mutex mMutex MOZ_UNANNOTATED
;
91 NotNull
<RefPtr
<Decoder
>> mDecoder
;
95 * An IDecodingTask implementation for anonymous decoders - that is, decoders
96 * with no associated Image object.
98 class AnonymousDecodingTask final
: public IDecodingTask
{
100 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(AnonymousDecodingTask
, override
)
102 explicit AnonymousDecodingTask(NotNull
<Decoder
*> aDecoder
, bool aResumable
);
106 bool ShouldPreferSyncRun() const override
{ return true; }
107 TaskPriority
Priority() const override
{ return TaskPriority::eLow
; }
109 void Resume() override
;
112 virtual ~AnonymousDecodingTask() {}
114 NotNull
<RefPtr
<Decoder
>> mDecoder
;
119 } // namespace mozilla
121 #endif // mozilla_image_IDecodingTask_h