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
);
59 * An IDecodingTask implementation for metadata decodes of images.
61 class MetadataDecodingTask final
: public IDecodingTask
{
63 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(MetadataDecodingTask
, override
)
65 explicit MetadataDecodingTask(NotNull
<Decoder
*> aDecoder
);
69 // Metadata decodes are very fast (since they only need to examine an image's
70 // header) so there's no reason to refuse to run them synchronously if the
71 // caller will allow us to.
72 bool ShouldPreferSyncRun() const override
{ return true; }
74 // Metadata decodes run at the highest priority because they block layout and
76 TaskPriority
Priority() const override
{ return TaskPriority::eHigh
; }
79 virtual ~MetadataDecodingTask() {}
81 /// Mutex protecting access to mDecoder.
82 Mutex mMutex MOZ_UNANNOTATED
;
84 NotNull
<RefPtr
<Decoder
>> mDecoder
;
88 * An IDecodingTask implementation for anonymous decoders - that is, decoders
89 * with no associated Image object.
91 class AnonymousDecodingTask
: public IDecodingTask
{
93 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(AnonymousDecodingTask
, override
)
95 explicit AnonymousDecodingTask(NotNull
<Decoder
*> aDecoder
, bool aResumable
);
99 bool ShouldPreferSyncRun() const override
{ return true; }
100 TaskPriority
Priority() const override
{ return TaskPriority::eLow
; }
102 void Resume() override
;
105 virtual ~AnonymousDecodingTask() {}
107 NotNull
<RefPtr
<Decoder
>> mDecoder
;
112 } // namespace mozilla
114 #endif // mozilla_image_IDecodingTask_h