Bug 1523562 [wpt PR 14847] - Make named constructors' |prototype|s have the right...
[gecko.git] / image / IDecodingTask.cpp
blob3d7b5a4c94ee9d2af85f41d3d59946ad93e04fcc
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 #include "IDecodingTask.h"
8 #include "gfxPrefs.h"
9 #include "nsThreadUtils.h"
11 #include "Decoder.h"
12 #include "DecodePool.h"
13 #include "RasterImage.h"
14 #include "SurfaceCache.h"
16 #include "mozilla/SystemGroup.h"
18 namespace mozilla {
20 using gfx::IntRect;
22 namespace image {
24 ///////////////////////////////////////////////////////////////////////////////
25 // Helpers for sending notifications to the image associated with a decoder.
26 ///////////////////////////////////////////////////////////////////////////////
28 void IDecodingTask::EnsureHasEventTarget(NotNull<RasterImage*> aImage) {
29 if (!mEventTarget) {
30 // We determine the event target as late as possible, at the first dispatch
31 // time, because the observers bound to an imgRequest will affect it.
32 // We cache it rather than query for the event target each time because the
33 // event target can change. We don't want to risk events being executed in
34 // a different order than they are dispatched, which can happen if we
35 // selected scheduler groups which have no ordering guarantees relative to
36 // each other (e.g. it moves from scheduler group A for doc group DA to
37 // scheduler group B for doc group DB due to changing observers -- if we
38 // dispatched the first event on A, and the second on B, we don't know which
39 // will execute first.)
40 RefPtr<ProgressTracker> tracker = aImage->GetProgressTracker();
41 if (tracker) {
42 mEventTarget = tracker->GetEventTarget();
43 } else {
44 mEventTarget = SystemGroup::EventTargetFor(TaskCategory::Other);
49 bool IDecodingTask::IsOnEventTarget() const {
50 // This is essentially equivalent to NS_IsOnMainThread() because all of the
51 // event targets are for the main thread (although perhaps with a different
52 // label / scheduler group). The observers in ProgressTracker may have
53 // different event targets from this, so this is just a best effort guess.
54 bool current = false;
55 mEventTarget->IsOnCurrentThread(&current);
56 return current;
59 void IDecodingTask::NotifyProgress(NotNull<RasterImage*> aImage,
60 NotNull<Decoder*> aDecoder) {
61 MOZ_ASSERT(aDecoder->HasProgress() && !aDecoder->IsMetadataDecode());
62 EnsureHasEventTarget(aImage);
64 // Capture the decoder's state. If we need to notify asynchronously, it's
65 // important that we don't wait until the lambda actually runs to capture the
66 // state that we're going to notify. That would both introduce data races on
67 // the decoder's state and cause inconsistencies between the NotifyProgress()
68 // calls we make off-main-thread and the notifications that RasterImage
69 // actually receives, which would cause bugs.
70 Progress progress = aDecoder->TakeProgress();
71 IntRect invalidRect = aDecoder->TakeInvalidRect();
72 Maybe<uint32_t> frameCount = aDecoder->TakeCompleteFrameCount();
73 DecoderFlags decoderFlags = aDecoder->GetDecoderFlags();
74 SurfaceFlags surfaceFlags = aDecoder->GetSurfaceFlags();
76 // Synchronously notify if we can.
77 if (IsOnEventTarget() && !(decoderFlags & DecoderFlags::ASYNC_NOTIFY)) {
78 aImage->NotifyProgress(progress, invalidRect, frameCount, decoderFlags,
79 surfaceFlags);
80 return;
83 // We're forced to notify asynchronously.
84 NotNull<RefPtr<RasterImage>> image = aImage;
85 mEventTarget->Dispatch(NS_NewRunnableFunction("IDecodingTask::NotifyProgress",
86 [=]() -> void {
87 image->NotifyProgress(
88 progress, invalidRect,
89 frameCount, decoderFlags,
90 surfaceFlags);
91 }),
92 NS_DISPATCH_NORMAL);
95 void IDecodingTask::NotifyDecodeComplete(NotNull<RasterImage*> aImage,
96 NotNull<Decoder*> aDecoder) {
97 MOZ_ASSERT(aDecoder->HasError() || !aDecoder->InFrame(),
98 "Decode complete in the middle of a frame?");
99 EnsureHasEventTarget(aImage);
101 // Capture the decoder's state.
102 DecoderFinalStatus finalStatus = aDecoder->FinalStatus();
103 ImageMetadata metadata = aDecoder->GetImageMetadata();
104 DecoderTelemetry telemetry = aDecoder->Telemetry();
105 Progress progress = aDecoder->TakeProgress();
106 IntRect invalidRect = aDecoder->TakeInvalidRect();
107 Maybe<uint32_t> frameCount = aDecoder->TakeCompleteFrameCount();
108 DecoderFlags decoderFlags = aDecoder->GetDecoderFlags();
109 SurfaceFlags surfaceFlags = aDecoder->GetSurfaceFlags();
111 // Synchronously notify if we can.
112 if (IsOnEventTarget() && !(decoderFlags & DecoderFlags::ASYNC_NOTIFY)) {
113 aImage->NotifyDecodeComplete(finalStatus, metadata, telemetry, progress,
114 invalidRect, frameCount, decoderFlags,
115 surfaceFlags);
116 return;
119 // We're forced to notify asynchronously.
120 NotNull<RefPtr<RasterImage>> image = aImage;
121 mEventTarget->Dispatch(
122 NS_NewRunnableFunction("IDecodingTask::NotifyDecodeComplete",
123 [=]() -> void {
124 image->NotifyDecodeComplete(
125 finalStatus, metadata, telemetry, progress,
126 invalidRect, frameCount, decoderFlags,
127 surfaceFlags);
129 NS_DISPATCH_NORMAL);
132 ///////////////////////////////////////////////////////////////////////////////
133 // IDecodingTask implementation.
134 ///////////////////////////////////////////////////////////////////////////////
136 void IDecodingTask::Resume() { DecodePool::Singleton()->AsyncRun(this); }
138 ///////////////////////////////////////////////////////////////////////////////
139 // MetadataDecodingTask implementation.
140 ///////////////////////////////////////////////////////////////////////////////
142 MetadataDecodingTask::MetadataDecodingTask(NotNull<Decoder*> aDecoder)
143 : mMutex("mozilla::image::MetadataDecodingTask"), mDecoder(aDecoder) {
144 MOZ_ASSERT(mDecoder->IsMetadataDecode(),
145 "Use DecodingTask for non-metadata decodes");
148 void MetadataDecodingTask::Run() {
149 MutexAutoLock lock(mMutex);
151 LexerResult result = mDecoder->Decode(WrapNotNull(this));
153 if (result.is<TerminalState>()) {
154 NotifyDecodeComplete(mDecoder->GetImage(), mDecoder);
155 return; // We're done.
158 if (result == LexerResult(Yield::NEED_MORE_DATA)) {
159 // We can't make any more progress right now. We also don't want to report
160 // any progress, because it's important that metadata decode results are
161 // delivered atomically. The decoder itself will ensure that we get
162 // reenqueued when more data is available; just return for now.
163 return;
166 MOZ_ASSERT_UNREACHABLE("Metadata decode yielded for an unexpected reason");
169 ///////////////////////////////////////////////////////////////////////////////
170 // AnonymousDecodingTask implementation.
171 ///////////////////////////////////////////////////////////////////////////////
173 AnonymousDecodingTask::AnonymousDecodingTask(NotNull<Decoder*> aDecoder,
174 bool aResumable)
175 : mDecoder(aDecoder), mResumable(aResumable) {}
177 void AnonymousDecodingTask::Run() {
178 while (true) {
179 LexerResult result = mDecoder->Decode(WrapNotNull(this));
181 if (result.is<TerminalState>()) {
182 return; // We're done.
185 if (result == LexerResult(Yield::NEED_MORE_DATA)) {
186 // We can't make any more progress right now. Let the caller decide how to
187 // handle it.
188 return;
191 // Right now we don't do anything special for other kinds of yields, so just
192 // keep working.
193 MOZ_ASSERT(result.is<Yield>());
197 void AnonymousDecodingTask::Resume() {
198 // Anonymous decoders normally get all their data at once. We have tests
199 // where they don't; typically in these situations, the test re-runs them
200 // manually. However some tests want to verify Resume works, so they will
201 // explicitly request this behaviour.
202 if (mResumable) {
203 RefPtr<AnonymousDecodingTask> self(this);
204 NS_DispatchToMainThread(
205 NS_NewRunnableFunction("image::AnonymousDecodingTask::Resume",
206 [self]() -> void { self->Run(); }));
210 } // namespace image
211 } // namespace mozilla