forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 1
/
IDecodingTask.cpp
190 lines (156 loc) · 7.49 KB
/
IDecodingTask.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "IDecodingTask.h"
#include "nsThreadUtils.h"
#include "mozilla/AppShutdown.h"
#include "Decoder.h"
#include "DecodePool.h"
#include "RasterImage.h"
#include "SurfaceCache.h"
namespace mozilla {
using gfx::IntRect;
namespace image {
///////////////////////////////////////////////////////////////////////////////
// Helpers for sending notifications to the image associated with a decoder.
///////////////////////////////////////////////////////////////////////////////
void IDecodingTask::NotifyProgress(NotNull<RasterImage*> aImage,
NotNull<Decoder*> aDecoder) {
MOZ_ASSERT(aDecoder->HasProgress() && !aDecoder->IsMetadataDecode());
// Capture the decoder's state. If we need to notify asynchronously, it's
// important that we don't wait until the lambda actually runs to capture the
// state that we're going to notify. That would both introduce data races on
// the decoder's state and cause inconsistencies between the NotifyProgress()
// calls we make off-main-thread and the notifications that RasterImage
// actually receives, which would cause bugs.
Progress progress = aDecoder->TakeProgress();
OrientedIntRect invalidRect = aDecoder->TakeInvalidRect();
Maybe<uint32_t> frameCount = aDecoder->TakeCompleteFrameCount();
DecoderFlags decoderFlags = aDecoder->GetDecoderFlags();
SurfaceFlags surfaceFlags = aDecoder->GetSurfaceFlags();
// Synchronously notify if we can.
if (NS_IsMainThread() && !(decoderFlags & DecoderFlags::ASYNC_NOTIFY)) {
aImage->NotifyProgress(progress, invalidRect, frameCount, decoderFlags,
surfaceFlags);
return;
}
// Don't try to dispatch after shutdown, we'll just leak the runnable.
if (NS_WARN_IF(
AppShutdown::IsInOrBeyond(ShutdownPhase::XPCOMShutdownThreads))) {
return;
}
// We're forced to notify asynchronously.
NotNull<RefPtr<RasterImage>> image = aImage;
nsCOMPtr<nsIEventTarget> eventTarget = GetMainThreadSerialEventTarget();
eventTarget->Dispatch(CreateRenderBlockingRunnable(NS_NewRunnableFunction(
"IDecodingTask::NotifyProgress",
[=]() -> void {
image->NotifyProgress(progress, invalidRect,
frameCount, decoderFlags,
surfaceFlags);
})),
NS_DISPATCH_NORMAL);
}
void IDecodingTask::NotifyDecodeComplete(NotNull<RasterImage*> aImage,
NotNull<Decoder*> aDecoder) {
MOZ_ASSERT(aDecoder->HasError() || !aDecoder->InFrame(),
"Decode complete in the middle of a frame?");
// Capture the decoder's state.
DecoderFinalStatus finalStatus = aDecoder->FinalStatus();
ImageMetadata metadata = aDecoder->GetImageMetadata();
DecoderTelemetry telemetry = aDecoder->Telemetry();
Progress progress = aDecoder->TakeProgress();
OrientedIntRect invalidRect = aDecoder->TakeInvalidRect();
Maybe<uint32_t> frameCount = aDecoder->TakeCompleteFrameCount();
DecoderFlags decoderFlags = aDecoder->GetDecoderFlags();
SurfaceFlags surfaceFlags = aDecoder->GetSurfaceFlags();
// Synchronously notify if we can.
if (NS_IsMainThread() && !(decoderFlags & DecoderFlags::ASYNC_NOTIFY)) {
aImage->NotifyDecodeComplete(finalStatus, metadata, telemetry, progress,
invalidRect, frameCount, decoderFlags,
surfaceFlags);
return;
}
// Don't try to dispatch after shutdown, we'll just leak the runnable.
if (NS_WARN_IF(
AppShutdown::IsInOrBeyond(ShutdownPhase::XPCOMShutdownThreads))) {
return;
}
// We're forced to notify asynchronously.
NotNull<RefPtr<RasterImage>> image = aImage;
nsCOMPtr<nsIEventTarget> eventTarget = GetMainThreadSerialEventTarget();
eventTarget->Dispatch(CreateRenderBlockingRunnable(NS_NewRunnableFunction(
"IDecodingTask::NotifyDecodeComplete",
[=]() -> void {
image->NotifyDecodeComplete(
finalStatus, metadata, telemetry, progress,
invalidRect, frameCount, decoderFlags,
surfaceFlags);
})),
NS_DISPATCH_NORMAL);
}
///////////////////////////////////////////////////////////////////////////////
// IDecodingTask implementation.
///////////////////////////////////////////////////////////////////////////////
void IDecodingTask::Resume() { DecodePool::Singleton()->AsyncRun(this); }
///////////////////////////////////////////////////////////////////////////////
// MetadataDecodingTask implementation.
///////////////////////////////////////////////////////////////////////////////
MetadataDecodingTask::MetadataDecodingTask(NotNull<Decoder*> aDecoder)
: mMutex("mozilla::image::MetadataDecodingTask"), mDecoder(aDecoder) {
MOZ_ASSERT(mDecoder->IsMetadataDecode(),
"Use DecodingTask for non-metadata decodes");
}
void MetadataDecodingTask::Run() {
MutexAutoLock lock(mMutex);
LexerResult result = mDecoder->Decode(WrapNotNull(this));
if (result.is<TerminalState>()) {
NotifyDecodeComplete(mDecoder->GetImage(), mDecoder);
return; // We're done.
}
if (result == LexerResult(Yield::NEED_MORE_DATA)) {
// We can't make any more progress right now. We also don't want to report
// any progress, because it's important that metadata decode results are
// delivered atomically. The decoder itself will ensure that we get
// reenqueued when more data is available; just return for now.
return;
}
MOZ_ASSERT_UNREACHABLE("Metadata decode yielded for an unexpected reason");
}
///////////////////////////////////////////////////////////////////////////////
// AnonymousDecodingTask implementation.
///////////////////////////////////////////////////////////////////////////////
AnonymousDecodingTask::AnonymousDecodingTask(NotNull<Decoder*> aDecoder,
bool aResumable)
: mDecoder(aDecoder), mResumable(aResumable) {}
void AnonymousDecodingTask::Run() {
while (true) {
LexerResult result = mDecoder->Decode(WrapNotNull(this));
if (result.is<TerminalState>()) {
return; // We're done.
}
if (result == LexerResult(Yield::NEED_MORE_DATA)) {
// We can't make any more progress right now. Let the caller decide how to
// handle it.
return;
}
// Right now we don't do anything special for other kinds of yields, so just
// keep working.
MOZ_ASSERT(result.is<Yield>());
}
}
void AnonymousDecodingTask::Resume() {
// Anonymous decoders normally get all their data at once. We have tests
// where they don't; typically in these situations, the test re-runs them
// manually. However some tests want to verify Resume works, so they will
// explicitly request this behaviour.
if (mResumable) {
RefPtr<AnonymousDecodingTask> self(this);
NS_DispatchToMainThread(
NS_NewRunnableFunction("image::AnonymousDecodingTask::Resume",
[self]() -> void { self->Run(); }));
}
}
} // namespace image
} // namespace mozilla