Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/image/IDecodingTask.h
Line
Count
Source (jump to first uncovered line)
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 "imgFrame.h"
15
#include "mozilla/NotNull.h"
16
#include "mozilla/RefPtr.h"
17
#include "nsIEventTarget.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
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
0
  virtual ~IDecodingTask() { }
54
55
  /// Notify @aImage of @aDecoder's progress.
56
  void NotifyProgress(NotNull<RasterImage*> aImage,
57
                      NotNull<Decoder*> aDecoder);
58
59
  /// Notify @aImage that @aDecoder has finished.
60
  void NotifyDecodeComplete(NotNull<RasterImage*> aImage,
61
                            NotNull<Decoder*> aDecoder);
62
63
private:
64
  void EnsureHasEventTarget(NotNull<RasterImage*> aImage);
65
66
  bool IsOnEventTarget() const;
67
68
  nsCOMPtr<nsIEventTarget> mEventTarget;
69
};
70
71
72
/**
73
 * An IDecodingTask implementation for metadata decodes of images.
74
 */
75
class MetadataDecodingTask final : public IDecodingTask
76
{
77
public:
78
  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(MetadataDecodingTask, override)
79
80
  explicit MetadataDecodingTask(NotNull<Decoder*> aDecoder);
81
82
  void Run() override;
83
84
  // Metadata decodes are very fast (since they only need to examine an image's
85
  // header) so there's no reason to refuse to run them synchronously if the
86
  // caller will allow us to.
87
0
  bool ShouldPreferSyncRun() const override { return true; }
88
89
  // Metadata decodes run at the highest priority because they block layout and
90
  // page load.
91
0
  TaskPriority Priority() const override { return TaskPriority::eHigh; }
92
93
private:
94
0
  virtual ~MetadataDecodingTask() { }
95
96
  /// Mutex protecting access to mDecoder.
97
  Mutex mMutex;
98
99
  NotNull<RefPtr<Decoder>> mDecoder;
100
};
101
102
103
/**
104
 * An IDecodingTask implementation for anonymous decoders - that is, decoders
105
 * with no associated Image object.
106
 */
107
class AnonymousDecodingTask final : public IDecodingTask
108
{
109
public:
110
  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(AnonymousDecodingTask, override)
111
112
  explicit AnonymousDecodingTask(NotNull<Decoder*> aDecoder);
113
114
  void Run() override;
115
116
0
  bool ShouldPreferSyncRun() const override { return true; }
117
0
  TaskPriority Priority() const override { return TaskPriority::eLow; }
118
119
  // Anonymous decoders normally get all their data at once. We have tests where
120
  // they don't; in these situations, the test re-runs them manually. So no
121
  // matter what, we don't want to resume by posting a task to the DecodePool.
122
0
  void Resume() override { }
123
124
private:
125
0
  virtual ~AnonymousDecodingTask() { }
126
127
  NotNull<RefPtr<Decoder>> mDecoder;
128
};
129
130
} // namespace image
131
} // namespace mozilla
132
133
#endif // mozilla_image_IDecodingTask_h