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/. */
8 #include "imgRequest.h"
9 #include "Layers.h" // for LayerManager
10 #include "nsRefreshDriver.h"
11 #include "nsContentUtils.h"
12 #include "mozilla/SizeOfState.h"
13 #include "mozilla/TimeStamp.h"
14 #include "mozilla/Tuple.h" // for Tie
15 #include "mozilla/layers/SharedSurfacesChild.h"
20 ///////////////////////////////////////////////////////////////////////////////
22 ///////////////////////////////////////////////////////////////////////////////
24 ImageMemoryCounter::ImageMemoryCounter(imgRequest
* aRequest
,
25 SizeOfState
& aState
, bool aIsUsed
)
26 : mProgress(UINT32_MAX
),
33 // We don't have the image object yet, but we can get some information.
34 nsCOMPtr
<nsIURI
> imageURL
;
35 nsresult rv
= aRequest
->GetURI(getter_AddRefs(imageURL
));
36 if (NS_SUCCEEDED(rv
) && imageURL
) {
37 imageURL
->GetSpec(mURI
);
40 mType
= imgIContainer::TYPE_REQUEST
;
41 mHasError
= NS_FAILED(aRequest
->GetImageErrorCode());
42 mValidating
= !!aRequest
->GetValidator();
44 RefPtr
<ProgressTracker
> tracker
= aRequest
->GetProgressTracker();
46 mProgress
= tracker
->GetProgress();
50 ImageMemoryCounter::ImageMemoryCounter(imgRequest
* aRequest
, Image
* aImage
,
51 SizeOfState
& aState
, bool aIsUsed
)
52 : mProgress(UINT32_MAX
),
60 // Extract metadata about the image.
61 nsCOMPtr
<nsIURI
> imageURL(aImage
->GetURI());
63 imageURL
->GetSpec(mURI
);
68 aImage
->GetWidth(&width
);
69 aImage
->GetHeight(&height
);
70 mIntrinsicSize
.SizeTo(width
, height
);
72 mType
= aImage
->GetType();
73 mHasError
= aImage
->HasError();
74 mValidating
= !!aRequest
->GetValidator();
76 RefPtr
<ProgressTracker
> tracker
= aImage
->GetProgressTracker();
78 mProgress
= tracker
->GetProgress();
81 // Populate memory counters for source and decoded data.
82 mValues
.SetSource(aImage
->SizeOfSourceWithComputedFallback(aState
));
83 aImage
->CollectSizeOfSurfaces(mSurfaces
, aState
.mMallocSizeOf
);
86 for (const SurfaceMemoryCounter
& surfaceCounter
: mSurfaces
) {
87 mValues
+= surfaceCounter
.Values();
91 ///////////////////////////////////////////////////////////////////////////////
93 ///////////////////////////////////////////////////////////////////////////////
95 bool ImageResource::GetSpecTruncatedTo1k(nsCString
& aSpec
) const {
96 static const size_t sMaxTruncatedLength
= 1024;
99 if (sMaxTruncatedLength
>= aSpec
.Length()) {
103 aSpec
.Truncate(sMaxTruncatedLength
);
107 void ImageResource::SetCurrentImage(ImageContainer
* aContainer
,
108 SourceSurface
* aSurface
,
109 const Maybe
<IntRect
>& aDirtyRect
) {
110 MOZ_ASSERT(NS_IsMainThread());
111 MOZ_ASSERT(aContainer
);
114 // The OS threw out some or all of our buffer. We'll need to wait for the
115 // redecode (which was automatically triggered by GetFrame) to complete.
119 // |image| holds a reference to a SourceSurface which in turn holds a lock on
120 // the current frame's data buffer, ensuring that it doesn't get freed as
121 // long as the layer system keeps this ImageContainer alive.
122 RefPtr
<layers::Image
> image
= new layers::SourceSurfaceImage(aSurface
);
124 // We can share the producer ID with other containers because it is only
125 // used internally to validate the frames given to a particular container
126 // so that another object cannot add its own. Similarly the frame ID is
127 // only used internally to ensure it is always increasing, and skipping
128 // IDs from an individual container's perspective is acceptable.
129 AutoTArray
<ImageContainer::NonOwningImage
, 1> imageList
;
130 imageList
.AppendElement(ImageContainer::NonOwningImage(
131 image
, TimeStamp(), mLastFrameID
++, mImageProducerID
));
134 aContainer
->SetCurrentImagesInTransaction(imageList
);
136 aContainer
->SetCurrentImages(imageList
);
139 // If we are animated, then we should request that the image container be
140 // treated as such, to avoid display list rebuilding to update frames for
142 if (mProgressTracker
->GetProgress() & FLAG_IS_ANIMATED
) {
144 layers::SharedSurfacesChild::UpdateAnimation(aContainer
, aSurface
,
147 IntRect
dirtyRect(IntPoint(0, 0), aSurface
->GetSize());
148 layers::SharedSurfacesChild::UpdateAnimation(aContainer
, aSurface
,
154 ImgDrawResult
ImageResource::GetImageContainerImpl(
155 LayerManager
* aManager
, const IntSize
& aSize
,
156 const Maybe
<SVGImageContext
>& aSVGContext
, uint32_t aFlags
,
157 ImageContainer
** aOutContainer
) {
158 MOZ_ASSERT(NS_IsMainThread());
159 MOZ_ASSERT(aManager
);
161 (aFlags
& ~(FLAG_SYNC_DECODE
| FLAG_SYNC_DECODE_IF_FAST
|
162 FLAG_ASYNC_NOTIFY
| FLAG_HIGH_QUALITY_SCALING
)) == FLAG_NONE
,
163 "Unsupported flag passed to GetImageContainer");
165 ImgDrawResult drawResult
;
167 Tie(drawResult
, size
) = GetImageContainerSize(aManager
, aSize
, aFlags
);
168 if (drawResult
!= ImgDrawResult::SUCCESS
) {
172 MOZ_ASSERT(!size
.IsEmpty());
174 if (mAnimationConsumers
== 0) {
175 SendOnUnlockedDraw(aFlags
);
178 uint32_t flags
= (aFlags
& ~(FLAG_SYNC_DECODE
| FLAG_SYNC_DECODE_IF_FAST
)) |
180 RefPtr
<layers::ImageContainer
> container
;
181 ImageContainerEntry
* entry
= nullptr;
182 int i
= mImageContainers
.Length() - 1;
183 for (; i
>= 0; --i
) {
184 entry
= &mImageContainers
[i
];
185 container
= entry
->mContainer
.get();
186 if (size
== entry
->mSize
&& flags
== entry
->mFlags
&&
187 aSVGContext
== entry
->mSVGContext
) {
188 // Lack of a container is handled below.
190 } else if (!container
) {
191 // Stop tracking if our weak pointer to the image container was freed.
192 mImageContainers
.RemoveElementAt(i
);
194 // It isn't a match, but still valid. Forget the container so we don't
195 // try to reuse it below.
201 switch (entry
->mLastDrawResult
) {
202 case ImgDrawResult::SUCCESS
:
203 case ImgDrawResult::BAD_IMAGE
:
204 case ImgDrawResult::BAD_ARGS
:
205 case ImgDrawResult::NOT_SUPPORTED
:
206 container
.forget(aOutContainer
);
207 return entry
->mLastDrawResult
;
208 case ImgDrawResult::NOT_READY
:
209 case ImgDrawResult::INCOMPLETE
:
210 case ImgDrawResult::TEMPORARY_ERROR
:
211 // Temporary conditions where we need to rerequest the frame to recover.
213 case ImgDrawResult::WRONG_SIZE
:
214 // Unused by GetFrameInternal
216 MOZ_ASSERT_UNREACHABLE("Unhandled ImgDrawResult type!");
217 container
.forget(aOutContainer
);
218 return entry
->mLastDrawResult
;
223 NotifyDrawingObservers();
227 RefPtr
<SourceSurface
> surface
;
228 Tie(drawResult
, bestSize
, surface
) = GetFrameInternal(
229 size
, aSVGContext
, FRAME_CURRENT
, aFlags
| FLAG_ASYNC_NOTIFY
);
231 // The requested size might be refused by the surface cache (i.e. due to
232 // factor-of-2 mode). In that case we don't want to create an entry for this
233 // specific size, but rather re-use the entry for the substituted size.
234 if (bestSize
!= size
) {
235 MOZ_ASSERT(!bestSize
.IsEmpty());
237 // We can only remove the entry if we no longer have a container, because if
238 // there are strong references to it remaining, we need to still update it
239 // in UpdateImageContainer.
240 if (i
>= 0 && !container
) {
241 mImageContainers
.RemoveElementAt(i
);
244 // Forget about the stale container, if any. This lets the entry creation
245 // logic do its job below, if it turns out there is no existing best entry
246 // or the best entry doesn't have a container.
249 // We need to do the entry search again for the new size. We skip pruning
250 // because we did this above once already, but ImageContainer is threadsafe,
251 // so there is a remote possibility it got freed.
252 i
= mImageContainers
.Length() - 1;
253 for (; i
>= 0; --i
) {
254 entry
= &mImageContainers
[i
];
255 if (bestSize
== entry
->mSize
&& flags
== entry
->mFlags
&&
256 aSVGContext
== entry
->mSVGContext
) {
257 container
= entry
->mContainer
.get();
259 switch (entry
->mLastDrawResult
) {
260 case ImgDrawResult::SUCCESS
:
261 case ImgDrawResult::BAD_IMAGE
:
262 case ImgDrawResult::BAD_ARGS
:
263 case ImgDrawResult::NOT_SUPPORTED
:
264 container
.forget(aOutContainer
);
265 return entry
->mLastDrawResult
;
266 case ImgDrawResult::NOT_READY
:
267 case ImgDrawResult::INCOMPLETE
:
268 case ImgDrawResult::TEMPORARY_ERROR
:
269 // Temporary conditions where we need to rerequest the frame to
270 // recover. We have already done so!
272 case ImgDrawResult::WRONG_SIZE
:
273 // Unused by GetFrameInternal
275 MOZ_ASSERT_UNREACHABLE("Unhandled DrawResult type!");
276 container
.forget(aOutContainer
);
277 return entry
->mLastDrawResult
;
286 // We need a new ImageContainer, so create one.
287 container
= LayerManager::CreateImageContainer();
290 entry
->mContainer
= container
;
292 entry
= mImageContainers
.AppendElement(
293 ImageContainerEntry(bestSize
, aSVGContext
, container
.get(), flags
));
297 SetCurrentImage(container
, surface
, Nothing());
298 entry
->mLastDrawResult
= drawResult
;
299 container
.forget(aOutContainer
);
303 bool ImageResource::UpdateImageContainer(const Maybe
<IntRect
>& aDirtyRect
) {
304 MOZ_ASSERT(NS_IsMainThread());
306 for (int i
= mImageContainers
.Length() - 1; i
>= 0; --i
) {
307 ImageContainerEntry
& entry
= mImageContainers
[i
];
308 RefPtr
<ImageContainer
> container
= entry
.mContainer
.get();
311 RefPtr
<SourceSurface
> surface
;
312 Tie(entry
.mLastDrawResult
, bestSize
, surface
) = GetFrameInternal(
313 entry
.mSize
, entry
.mSVGContext
, FRAME_CURRENT
, entry
.mFlags
);
315 // It is possible that this is a factor-of-2 substitution. Since we
316 // managed to convert the weak reference into a strong reference, that
317 // means that an imagelib user still is holding onto the container. thus
318 // we cannot consolidate and must keep updating the duplicate container.
320 SetCurrentImage(container
, surface
, aDirtyRect
);
322 IntRect
dirtyRect(IntPoint(0, 0), bestSize
);
323 SetCurrentImage(container
, surface
, Some(dirtyRect
));
326 // Stop tracking if our weak pointer to the image container was freed.
327 mImageContainers
.RemoveElementAt(i
);
331 return !mImageContainers
.IsEmpty();
334 void ImageResource::ReleaseImageContainer() {
335 MOZ_ASSERT(NS_IsMainThread());
336 mImageContainers
.Clear();
340 ImageResource::ImageResource(nsIURI
* aURI
)
343 mAnimationConsumers(0),
344 mAnimationMode(kNormalAnimMode
),
348 mImageProducerID(ImageContainer::AllocateProducerID()),
351 ImageResource::~ImageResource() {
352 // Ask our ProgressTracker to drop its weak reference to us.
353 mProgressTracker
->ResetImage();
356 void ImageResource::IncrementAnimationConsumers() {
357 MOZ_ASSERT(NS_IsMainThread(),
358 "Main thread only to encourage serialization "
359 "with DecrementAnimationConsumers");
360 mAnimationConsumers
++;
363 void ImageResource::DecrementAnimationConsumers() {
364 MOZ_ASSERT(NS_IsMainThread(),
365 "Main thread only to encourage serialization "
366 "with IncrementAnimationConsumers");
367 MOZ_ASSERT(mAnimationConsumers
>= 1, "Invalid no. of animation consumers!");
368 mAnimationConsumers
--;
371 nsresult
ImageResource::GetAnimationModeInternal(uint16_t* aAnimationMode
) {
373 return NS_ERROR_FAILURE
;
376 NS_ENSURE_ARG_POINTER(aAnimationMode
);
378 *aAnimationMode
= mAnimationMode
;
382 nsresult
ImageResource::SetAnimationModeInternal(uint16_t aAnimationMode
) {
384 return NS_ERROR_FAILURE
;
387 NS_ASSERTION(aAnimationMode
== kNormalAnimMode
||
388 aAnimationMode
== kDontAnimMode
||
389 aAnimationMode
== kLoopOnceAnimMode
,
390 "Wrong Animation Mode is being set!");
392 mAnimationMode
= aAnimationMode
;
397 bool ImageResource::HadRecentRefresh(const TimeStamp
& aTime
) {
398 // Our threshold for "recent" is 1/2 of the default refresh-driver interval.
399 // This ensures that we allow for frame rates at least as fast as the
400 // refresh driver's default rate.
401 static TimeDuration recentThreshold
=
402 TimeDuration::FromMilliseconds(nsRefreshDriver::DefaultInterval() / 2.0);
404 if (!mLastRefreshTime
.IsNull() &&
405 aTime
- mLastRefreshTime
< recentThreshold
) {
409 // else, we can proceed with a refresh.
410 // But first, update our last refresh time:
411 mLastRefreshTime
= aTime
;
415 void ImageResource::EvaluateAnimation() {
416 if (!mAnimating
&& ShouldAnimate()) {
417 nsresult rv
= StartAnimation();
418 mAnimating
= NS_SUCCEEDED(rv
);
419 } else if (mAnimating
&& !ShouldAnimate()) {
424 void ImageResource::SendOnUnlockedDraw(uint32_t aFlags
) {
425 if (!mProgressTracker
) {
429 if (!(aFlags
& FLAG_ASYNC_NOTIFY
)) {
430 mProgressTracker
->OnUnlockedDraw();
432 NotNull
<RefPtr
<ImageResource
>> image
= WrapNotNull(this);
433 nsCOMPtr
<nsIEventTarget
> eventTarget
= mProgressTracker
->GetEventTarget();
434 nsCOMPtr
<nsIRunnable
> ev
= NS_NewRunnableFunction(
435 "image::ImageResource::SendOnUnlockedDraw", [=]() -> void {
436 RefPtr
<ProgressTracker
> tracker
= image
->GetProgressTracker();
438 tracker
->OnUnlockedDraw();
441 eventTarget
->Dispatch(CreateMediumHighRunnable(ev
.forget()),
447 void ImageResource::NotifyDrawingObservers() {
448 if (!mURI
|| !NS_IsMainThread()) {
452 if (!mURI
->SchemeIs("resource") && !mURI
->SchemeIs("chrome")) {
456 // Record the image drawing for startup performance testing.
457 nsCOMPtr
<nsIURI
> uri
= mURI
;
458 nsContentUtils::AddScriptRunner(NS_NewRunnableFunction(
459 "image::ImageResource::NotifyDrawingObservers", [uri
]() {
460 nsCOMPtr
<nsIObserverService
> obs
= services::GetObserverService();
461 NS_WARNING_ASSERTION(obs
, "Can't get an observer service handle");
465 obs
->NotifyObservers(nullptr, "image-drawing",
466 NS_ConvertUTF8toUTF16(spec
).get());
473 } // namespace mozilla