Line data Source code
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/. */
5 :
6 : /**
7 : * An interface for tasks which can execute on the ImageLib DecodePool, and
8 : * various implementations.
9 : */
10 :
11 : #ifndef mozilla_image_IDecodingTask_h
12 : #define mozilla_image_IDecodingTask_h
13 :
14 : #include "mozilla/NotNull.h"
15 : #include "mozilla/RefPtr.h"
16 :
17 : #include "imgFrame.h"
18 : #include "SourceBuffer.h"
19 :
20 : namespace mozilla {
21 : namespace image {
22 :
23 : class Decoder;
24 : class RasterImage;
25 :
26 : /// A priority hint that DecodePool can use when scheduling an IDecodingTask.
27 : enum class TaskPriority : uint8_t
28 : {
29 : eLow,
30 : eHigh
31 : };
32 :
33 : /**
34 : * An interface for tasks which can execute on the ImageLib DecodePool.
35 : */
36 33 : class IDecodingTask : public IResumable
37 : {
38 : public:
39 : /// Run the task.
40 : virtual void Run() = 0;
41 :
42 : /// @return true if, given the option, this task prefers to run synchronously.
43 : virtual bool ShouldPreferSyncRun() const = 0;
44 :
45 : /// @return a priority hint that DecodePool can use when scheduling this task.
46 : virtual TaskPriority Priority() const = 0;
47 :
48 : /// A default implementation of IResumable which resubmits the task to the
49 : /// DecodePool. Subclasses can override this if they need different behavior.
50 : void Resume() override;
51 :
52 : protected:
53 : /// Notify @aImage of @aDecoder's progress.
54 : static void NotifyProgress(NotNull<RasterImage*> aImage,
55 : NotNull<Decoder*> aDecoder);
56 :
57 : /// Notify @aImage that @aDecoder has finished.
58 : static void NotifyDecodeComplete(NotNull<RasterImage*> aImage,
59 : NotNull<Decoder*> aDecoder);
60 :
61 19 : virtual ~IDecodingTask() { }
62 : };
63 :
64 :
65 : /**
66 : * An IDecodingTask implementation for metadata decodes of images.
67 : */
68 : class MetadataDecodingTask final : public IDecodingTask
69 : {
70 : public:
71 76 : NS_INLINE_DECL_THREADSAFE_REFCOUNTING(MetadataDecodingTask, override)
72 :
73 : explicit MetadataDecodingTask(NotNull<Decoder*> aDecoder);
74 :
75 : void Run() override;
76 :
77 : // Metadata decodes are very fast (since they only need to examine an image's
78 : // header) so there's no reason to refuse to run them synchronously if the
79 : // caller will allow us to.
80 0 : bool ShouldPreferSyncRun() const override { return true; }
81 :
82 : // Metadata decodes run at the highest priority because they block layout and
83 : // page load.
84 19 : TaskPriority Priority() const override { return TaskPriority::eHigh; }
85 :
86 : private:
87 57 : virtual ~MetadataDecodingTask() { }
88 :
89 : /// Mutex protecting access to mDecoder.
90 : Mutex mMutex;
91 :
92 : NotNull<RefPtr<Decoder>> mDecoder;
93 : };
94 :
95 :
96 : /**
97 : * An IDecodingTask implementation for anonymous decoders - that is, decoders
98 : * with no associated Image object.
99 : */
100 : class AnonymousDecodingTask final : public IDecodingTask
101 : {
102 : public:
103 0 : NS_INLINE_DECL_THREADSAFE_REFCOUNTING(AnonymousDecodingTask, override)
104 :
105 : explicit AnonymousDecodingTask(NotNull<Decoder*> aDecoder);
106 :
107 : void Run() override;
108 :
109 0 : bool ShouldPreferSyncRun() const override { return true; }
110 0 : TaskPriority Priority() const override { return TaskPriority::eLow; }
111 :
112 : // Anonymous decoders normally get all their data at once. We have tests where
113 : // they don't; in these situations, the test re-runs them manually. So no
114 : // matter what, we don't want to resume by posting a task to the DecodePool.
115 0 : void Resume() override { }
116 :
117 : private:
118 0 : virtual ~AnonymousDecodingTask() { }
119 :
120 : NotNull<RefPtr<Decoder>> mDecoder;
121 : };
122 :
123 : } // namespace image
124 : } // namespace mozilla
125 :
126 : #endif // mozilla_image_IDecodingTask_h
|