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 "MultipartImage.h"
8 #include "imgINotificationObserver.h"
13 using gfx::SourceSurface
;
17 ///////////////////////////////////////////////////////////////////////////////
19 ///////////////////////////////////////////////////////////////////////////////
21 class NextPartObserver
: public IProgressObserver
{
23 MOZ_DECLARE_REFCOUNTED_TYPENAME(NextPartObserver
)
24 NS_INLINE_DECL_REFCOUNTING(NextPartObserver
, override
)
26 explicit NextPartObserver(MultipartImage
* aOwner
) : mOwner(aOwner
) {
30 void BeginObserving(Image
* aImage
) {
34 RefPtr
<ProgressTracker
> tracker
= mImage
->GetProgressTracker();
35 tracker
->AddObserver(this);
38 void BlockUntilDecodedAndFinishObserving() {
39 // Use RequestDecodeForSize() to block until our image finishes decoding.
40 // The size is ignored because we don't pass the FLAG_HIGH_QUALITY_SCALING
42 mImage
->RequestDecodeForSize(gfx::IntSize(0, 0),
43 imgIContainer::FLAG_SYNC_DECODE
);
45 if (mImage
&& mImage
->GetType() == imgIContainer::TYPE_VECTOR
) {
46 // We don't want to make a pending part the current part until it has had
47 // it's load event because when we transition from the current part to the
48 // next part we remove the multipart image as an observer of the current
49 // part and the progress tracker will send a fake load event with the
50 // lastpart bit set whenever an observer is removed without having the
51 // load complete progress. That fake load event with the lastpart bit set
52 // will get out of the multipart image and to the imgRequestProxy and
53 // further, which will make it look like we got the last part of the
54 // multipart image and confuse things. If we get here then we are still
55 // waiting to make mNextPart the current part, but we've gotten
56 // OnDataAvailable for the part after mNextPart. That means we must have
57 // gotten OnStopRequest for mNextPart; OnStopRequest calls
58 // OnImageDataComplete. For raster images that are part of a multipart
59 // image OnImageDataComplete will synchronously fire the load event
60 // because we synchronously perform the metadata decode in that function,
61 // because parts of multipart images have the transient flag set. So for
62 // raster images we are assured that the load event has happened by this
63 // point for mNextPart. For vector images there is no such luck because
64 // OnImageDataComplete needs to wait for the load event in the underlying
65 // svg document, which we can't force to happen synchronously. So we just
66 // send a fake load event for mNextPart. We are the only listener for it
67 // right now so it won't confuse anyone. When the real load event comes,
68 // progress tracker won't send it out because it's already in the
69 // progress. And nobody should be caring about load events on the current
70 // part of multipart images since they might be sent multiple times or
71 // might not be sent at all depending on timing. So this should be okay.
73 RefPtr
<ProgressTracker
> tracker
= mImage
->GetProgressTracker();
74 if (tracker
&& !(tracker
->GetProgress() & FLAG_LOAD_COMPLETE
)) {
75 Progress loadProgress
=
76 LoadCompleteProgress(/* aLastPart = */ false, /* aError = */ false,
77 /* aStatus = */ NS_OK
);
78 tracker
->SyncNotifyProgress(loadProgress
| FLAG_SIZE_AVAILABLE
);
82 // RequestDecodeForSize() should've sent synchronous notifications that
83 // would have caused us to call FinishObserving() (and null out mImage)
84 // already. If for some reason it didn't, we should do so here.
90 virtual void Notify(int32_t aType
,
91 const nsIntRect
* aRect
= nullptr) override
{
93 // We've already finished observing the last image we were given.
97 if (aType
!= imgINotificationObserver::FRAME_COMPLETE
) {
101 if (mImage
&& mImage
->GetType() == imgIContainer::TYPE_VECTOR
) {
102 RefPtr
<ProgressTracker
> tracker
= mImage
->GetProgressTracker();
103 if (tracker
&& !(tracker
->GetProgress() & FLAG_LOAD_COMPLETE
)) {
104 // Vector images can send frame complete during loading (any mutation in
105 // the underlying svg doc will call OnRenderingChange which sends frame
106 // complete). Whereas raster images can only send frame complete before
107 // the load event if there has been something to trigger a decode, but
108 // we do not request a decode. So we will ignore this for vector images
109 // so as to enforce the invariant described above in
110 // BlockUntilDecodedAndFinishObserving that the current part always has
111 // the load complete progress.
119 virtual void OnLoadComplete(bool aLastPart
) override
{
121 // We've already finished observing the last image we were given.
125 // Retrieve the image's intrinsic size.
128 mImage
->GetWidth(&width
);
129 mImage
->GetHeight(&height
);
131 // Request decoding at the intrinsic size.
132 mImage
->RequestDecodeForSize(IntSize(width
, height
),
133 imgIContainer::DECODE_FLAGS_DEFAULT
|
134 imgIContainer::FLAG_HIGH_QUALITY_SCALING
);
136 // If there's already an error, we may never get a FRAME_COMPLETE
137 // notification, so go ahead and notify our owner right away.
138 RefPtr
<ProgressTracker
> tracker
= mImage
->GetProgressTracker();
139 if (tracker
->GetProgress() & FLAG_HAS_ERROR
) {
144 if (mImage
&& mImage
->GetType() == imgIContainer::TYPE_VECTOR
&&
145 (tracker
->GetProgress() & FLAG_FRAME_COMPLETE
)) {
146 // It's a vector image that we may have already got a frame complete
147 // before load that we ignored (see Notify above). Now that we have the
148 // load event too, we make this part current.
153 // Other notifications are ignored.
154 virtual void SetHasImage() override
{}
155 virtual bool NotificationsDeferred() const override
{ return false; }
156 virtual void MarkPendingNotify() override
{}
157 virtual void ClearPendingNotify() override
{}
160 virtual ~NextPartObserver() {}
162 void FinishObserving() {
165 RefPtr
<ProgressTracker
> tracker
= mImage
->GetProgressTracker();
166 tracker
->RemoveObserver(this);
169 mOwner
->FinishTransition();
172 MultipartImage
* mOwner
;
173 RefPtr
<Image
> mImage
;
176 ///////////////////////////////////////////////////////////////////////////////
178 ///////////////////////////////////////////////////////////////////////////////
180 MultipartImage::MultipartImage(Image
* aFirstPart
)
181 : ImageWrapper(aFirstPart
), mPendingNotify(false) {
182 mNextPartObserver
= new NextPartObserver(this);
185 void MultipartImage::Init() {
186 MOZ_ASSERT(NS_IsMainThread());
187 MOZ_ASSERT(mTracker
, "Should've called SetProgressTracker() by now");
189 // Start observing the first part.
190 RefPtr
<ProgressTracker
> firstPartTracker
= InnerImage()->GetProgressTracker();
191 firstPartTracker
->AddObserver(this);
192 InnerImage()->IncrementAnimationConsumers();
195 MultipartImage::~MultipartImage() {
196 // Ask our ProgressTracker to drop its weak reference to us.
197 mTracker
->ResetImage();
200 NS_IMPL_ISUPPORTS_INHERITED0(MultipartImage
, ImageWrapper
)
202 void MultipartImage::BeginTransitionToPart(Image
* aNextPart
) {
203 MOZ_ASSERT(NS_IsMainThread());
204 MOZ_ASSERT(aNextPart
);
207 // Let the decoder catch up so we don't drop frames.
208 mNextPartObserver
->BlockUntilDecodedAndFinishObserving();
209 MOZ_ASSERT(!mNextPart
);
212 mNextPart
= aNextPart
;
214 // Start observing the next part; we'll complete the transition when
215 // NextPartObserver calls FinishTransition.
216 mNextPartObserver
->BeginObserving(mNextPart
);
217 mNextPart
->IncrementAnimationConsumers();
220 static Progress
FilterProgress(Progress aProgress
) {
221 // Filter out onload blocking notifications, since we don't want to block
222 // onload for multipart images.
223 // Filter out errors, since we don't want errors in one part to error out
225 return aProgress
& ~FLAG_HAS_ERROR
;
228 void MultipartImage::FinishTransition() {
229 MOZ_ASSERT(NS_IsMainThread());
230 MOZ_ASSERT(mNextPart
, "Should have a next part here");
232 RefPtr
<ProgressTracker
> newCurrentPartTracker
=
233 mNextPart
->GetProgressTracker();
234 if (newCurrentPartTracker
->GetProgress() & FLAG_HAS_ERROR
) {
235 // This frame has an error; drop it.
238 // We still need to notify, though.
239 mTracker
->ResetForNewRequest();
240 RefPtr
<ProgressTracker
> currentPartTracker
=
241 InnerImage()->GetProgressTracker();
242 mTracker
->SyncNotifyProgress(
243 FilterProgress(currentPartTracker
->GetProgress()));
248 // Stop observing the current part.
250 RefPtr
<ProgressTracker
> currentPartTracker
=
251 InnerImage()->GetProgressTracker();
252 currentPartTracker
->RemoveObserver(this);
255 // Make the next part become the current part.
256 mTracker
->ResetForNewRequest();
257 SetInnerImage(mNextPart
);
259 newCurrentPartTracker
->AddObserver(this);
261 // Finally, send all the notifications for the new current part and send a
262 // FRAME_UPDATE notification so that observers know to redraw.
263 mTracker
->SyncNotifyProgress(
264 FilterProgress(newCurrentPartTracker
->GetProgress()),
265 GetMaxSizedIntRect());
268 already_AddRefed
<imgIContainer
> MultipartImage::Unwrap() {
269 // Although we wrap another image, we don't allow callers to unwrap as. As far
270 // as external code is concerned, MultipartImage is atomic.
271 nsCOMPtr
<imgIContainer
> image
= this;
272 return image
.forget();
275 already_AddRefed
<ProgressTracker
> MultipartImage::GetProgressTracker() {
276 MOZ_ASSERT(mTracker
);
277 RefPtr
<ProgressTracker
> tracker
= mTracker
;
278 return tracker
.forget();
281 void MultipartImage::SetProgressTracker(ProgressTracker
* aTracker
) {
282 MOZ_ASSERT(aTracker
);
283 MOZ_ASSERT(!mTracker
);
287 nsresult
MultipartImage::OnImageDataAvailable(nsIRequest
* aRequest
,
288 nsIInputStream
* aInStr
,
289 uint64_t aSourceOffset
,
291 // Note that this method is special in that we forward it to the next part if
292 // one exists, and *not* the current part.
294 // We may trigger notifications that will free mNextPart, so keep it alive.
295 RefPtr
<Image
> nextPart
= mNextPart
;
297 nextPart
->OnImageDataAvailable(aRequest
, aInStr
, aSourceOffset
, aCount
);
299 InnerImage()->OnImageDataAvailable(aRequest
, aInStr
, aSourceOffset
, aCount
);
305 nsresult
MultipartImage::OnImageDataComplete(nsIRequest
* aRequest
,
306 nsresult aStatus
, bool aLastPart
) {
307 // Note that this method is special in that we forward it to the next part if
308 // one exists, and *not* the current part.
310 // We may trigger notifications that will free mNextPart, so keep it alive.
311 RefPtr
<Image
> nextPart
= mNextPart
;
313 nextPart
->OnImageDataComplete(aRequest
, aStatus
, aLastPart
);
315 InnerImage()->OnImageDataComplete(aRequest
, aStatus
, aLastPart
);
321 void MultipartImage::Notify(int32_t aType
,
322 const nsIntRect
* aRect
/* = nullptr*/) {
323 if (aType
== imgINotificationObserver::SIZE_AVAILABLE
) {
324 mTracker
->SyncNotifyProgress(FLAG_SIZE_AVAILABLE
);
325 } else if (aType
== imgINotificationObserver::FRAME_UPDATE
) {
326 mTracker
->SyncNotifyProgress(NoProgress
, *aRect
);
327 } else if (aType
== imgINotificationObserver::FRAME_COMPLETE
) {
328 mTracker
->SyncNotifyProgress(FLAG_FRAME_COMPLETE
);
329 } else if (aType
== imgINotificationObserver::LOAD_COMPLETE
) {
330 mTracker
->SyncNotifyProgress(FLAG_LOAD_COMPLETE
);
331 } else if (aType
== imgINotificationObserver::DECODE_COMPLETE
) {
332 mTracker
->SyncNotifyProgress(FLAG_DECODE_COMPLETE
);
333 } else if (aType
== imgINotificationObserver::DISCARD
) {
334 mTracker
->OnDiscard();
335 } else if (aType
== imgINotificationObserver::UNLOCKED_DRAW
) {
336 mTracker
->OnUnlockedDraw();
337 } else if (aType
== imgINotificationObserver::IS_ANIMATED
) {
338 mTracker
->SyncNotifyProgress(FLAG_IS_ANIMATED
);
339 } else if (aType
== imgINotificationObserver::HAS_TRANSPARENCY
) {
340 mTracker
->SyncNotifyProgress(FLAG_HAS_TRANSPARENCY
);
342 MOZ_ASSERT_UNREACHABLE("Notification list should be exhaustive");
346 void MultipartImage::OnLoadComplete(bool aLastPart
) {
347 Progress progress
= FLAG_LOAD_COMPLETE
;
349 progress
|= FLAG_LAST_PART_COMPLETE
;
351 mTracker
->SyncNotifyProgress(progress
);
354 void MultipartImage::SetHasImage() { mTracker
->OnImageAvailable(); }
356 bool MultipartImage::NotificationsDeferred() const { return mPendingNotify
; }
358 void MultipartImage::MarkPendingNotify() { mPendingNotify
= true; }
360 void MultipartImage::ClearPendingNotify() { mPendingNotify
= false; }
363 } // namespace mozilla