Bug 1885489 - Part 9: Add SnapshotIterator::readObject(). r=iain
[gecko.git] / image / IDecodingTask.h
blob380e680950cfbc6a21903c3af580c290e726e712
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);
58 /**
59 * An IDecodingTask implementation for metadata decodes of images.
61 class MetadataDecodingTask final : public IDecodingTask {
62 public:
63 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(MetadataDecodingTask, override)
65 explicit MetadataDecodingTask(NotNull<Decoder*> aDecoder);
67 void Run() override;
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
75 // page load.
76 TaskPriority Priority() const override { return TaskPriority::eHigh; }
78 private:
79 virtual ~MetadataDecodingTask() {}
81 /// Mutex protecting access to mDecoder.
82 Mutex mMutex MOZ_UNANNOTATED;
84 NotNull<RefPtr<Decoder>> mDecoder;
87 /**
88 * An IDecodingTask implementation for anonymous decoders - that is, decoders
89 * with no associated Image object.
91 class AnonymousDecodingTask final : public IDecodingTask {
92 public:
93 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(AnonymousDecodingTask, override)
95 explicit AnonymousDecodingTask(NotNull<Decoder*> aDecoder, bool aResumable);
97 void Run() override;
99 bool ShouldPreferSyncRun() const override { return true; }
100 TaskPriority Priority() const override { return TaskPriority::eLow; }
102 void Resume() override;
104 private:
105 virtual ~AnonymousDecodingTask() {}
107 NotNull<RefPtr<Decoder>> mDecoder;
108 bool mResumable;
111 } // namespace image
112 } // namespace mozilla
114 #endif // mozilla_image_IDecodingTask_h