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 "Layers.h" // for LayerManager
9 #include "nsRefreshDriver.h"
10 #include "nsContentUtils.h"
11 #include "mozilla/SizeOfState.h"
12 #include "mozilla/TimeStamp.h"
13 #include "mozilla/Tuple.h" // for Tie
14 #include "mozilla/layers/SharedSurfacesChild.h"
19 ///////////////////////////////////////////////////////////////////////////////
21 ///////////////////////////////////////////////////////////////////////////////
23 ImageMemoryCounter::ImageMemoryCounter(Image
* aImage
, SizeOfState
& aState
,
28 // Extract metadata about the image.
29 nsCOMPtr
<nsIURI
> imageURL(aImage
->GetURI());
31 imageURL
->GetSpec(mURI
);
36 aImage
->GetWidth(&width
);
37 aImage
->GetHeight(&height
);
38 mIntrinsicSize
.SizeTo(width
, height
);
40 mType
= aImage
->GetType();
42 // Populate memory counters for source and decoded data.
43 mValues
.SetSource(aImage
->SizeOfSourceWithComputedFallback(aState
));
44 aImage
->CollectSizeOfSurfaces(mSurfaces
, aState
.mMallocSizeOf
);
47 for (const SurfaceMemoryCounter
& surfaceCounter
: mSurfaces
) {
48 mValues
+= surfaceCounter
.Values();
52 ///////////////////////////////////////////////////////////////////////////////
54 ///////////////////////////////////////////////////////////////////////////////
56 bool ImageResource::GetSpecTruncatedTo1k(nsCString
& aSpec
) const {
57 static const size_t sMaxTruncatedLength
= 1024;
60 if (sMaxTruncatedLength
>= aSpec
.Length()) {
64 aSpec
.Truncate(sMaxTruncatedLength
);
68 void ImageResource::SetCurrentImage(ImageContainer
* aContainer
,
69 SourceSurface
* aSurface
,
70 const Maybe
<IntRect
>& aDirtyRect
) {
71 MOZ_ASSERT(NS_IsMainThread());
72 MOZ_ASSERT(aContainer
);
75 // The OS threw out some or all of our buffer. We'll need to wait for the
76 // redecode (which was automatically triggered by GetFrame) to complete.
80 // |image| holds a reference to a SourceSurface which in turn holds a lock on
81 // the current frame's data buffer, ensuring that it doesn't get freed as
82 // long as the layer system keeps this ImageContainer alive.
83 RefPtr
<layers::Image
> image
= new layers::SourceSurfaceImage(aSurface
);
85 // We can share the producer ID with other containers because it is only
86 // used internally to validate the frames given to a particular container
87 // so that another object cannot add its own. Similarly the frame ID is
88 // only used internally to ensure it is always increasing, and skipping
89 // IDs from an individual container's perspective is acceptable.
90 AutoTArray
<ImageContainer::NonOwningImage
, 1> imageList
;
91 imageList
.AppendElement(ImageContainer::NonOwningImage(
92 image
, TimeStamp(), mLastFrameID
++, mImageProducerID
));
95 aContainer
->SetCurrentImagesInTransaction(imageList
);
97 aContainer
->SetCurrentImages(imageList
);
100 // If we are animated, then we should request that the image container be
101 // treated as such, to avoid display list rebuilding to update frames for
103 if (mProgressTracker
->GetProgress() & FLAG_IS_ANIMATED
) {
105 layers::SharedSurfacesChild::UpdateAnimation(aContainer
, aSurface
,
108 IntRect
dirtyRect(IntPoint(0, 0), aSurface
->GetSize());
109 layers::SharedSurfacesChild::UpdateAnimation(aContainer
, aSurface
,
115 ImgDrawResult
ImageResource::GetImageContainerImpl(
116 LayerManager
* aManager
, const IntSize
& aSize
,
117 const Maybe
<SVGImageContext
>& aSVGContext
, uint32_t aFlags
,
118 ImageContainer
** aOutContainer
) {
119 MOZ_ASSERT(NS_IsMainThread());
120 MOZ_ASSERT(aManager
);
122 (aFlags
& ~(FLAG_SYNC_DECODE
| FLAG_SYNC_DECODE_IF_FAST
|
123 FLAG_ASYNC_NOTIFY
| FLAG_HIGH_QUALITY_SCALING
)) == FLAG_NONE
,
124 "Unsupported flag passed to GetImageContainer");
126 ImgDrawResult drawResult
;
128 Tie(drawResult
, size
) = GetImageContainerSize(aManager
, aSize
, aFlags
);
129 if (drawResult
!= ImgDrawResult::SUCCESS
) {
133 MOZ_ASSERT(!size
.IsEmpty());
135 if (mAnimationConsumers
== 0) {
136 SendOnUnlockedDraw(aFlags
);
139 uint32_t flags
= (aFlags
& ~(FLAG_SYNC_DECODE
| FLAG_SYNC_DECODE_IF_FAST
)) |
141 RefPtr
<layers::ImageContainer
> container
;
142 ImageContainerEntry
* entry
= nullptr;
143 int i
= mImageContainers
.Length() - 1;
144 for (; i
>= 0; --i
) {
145 entry
= &mImageContainers
[i
];
146 container
= entry
->mContainer
.get();
147 if (size
== entry
->mSize
&& flags
== entry
->mFlags
&&
148 aSVGContext
== entry
->mSVGContext
) {
149 // Lack of a container is handled below.
151 } else if (!container
) {
152 // Stop tracking if our weak pointer to the image container was freed.
153 mImageContainers
.RemoveElementAt(i
);
155 // It isn't a match, but still valid. Forget the container so we don't
156 // try to reuse it below.
162 switch (entry
->mLastDrawResult
) {
163 case ImgDrawResult::SUCCESS
:
164 case ImgDrawResult::BAD_IMAGE
:
165 case ImgDrawResult::BAD_ARGS
:
166 case ImgDrawResult::NOT_SUPPORTED
:
167 container
.forget(aOutContainer
);
168 return entry
->mLastDrawResult
;
169 case ImgDrawResult::NOT_READY
:
170 case ImgDrawResult::INCOMPLETE
:
171 case ImgDrawResult::TEMPORARY_ERROR
:
172 // Temporary conditions where we need to rerequest the frame to recover.
174 case ImgDrawResult::WRONG_SIZE
:
175 // Unused by GetFrameInternal
177 MOZ_ASSERT_UNREACHABLE("Unhandled ImgDrawResult type!");
178 container
.forget(aOutContainer
);
179 return entry
->mLastDrawResult
;
184 NotifyDrawingObservers();
188 RefPtr
<SourceSurface
> surface
;
189 Tie(drawResult
, bestSize
, surface
) = GetFrameInternal(
190 size
, aSVGContext
, FRAME_CURRENT
, aFlags
| FLAG_ASYNC_NOTIFY
);
192 // The requested size might be refused by the surface cache (i.e. due to
193 // factor-of-2 mode). In that case we don't want to create an entry for this
194 // specific size, but rather re-use the entry for the substituted size.
195 if (bestSize
!= size
) {
196 MOZ_ASSERT(!bestSize
.IsEmpty());
198 // We can only remove the entry if we no longer have a container, because if
199 // there are strong references to it remaining, we need to still update it
200 // in UpdateImageContainer.
201 if (i
>= 0 && !container
) {
202 mImageContainers
.RemoveElementAt(i
);
205 // Forget about the stale container, if any. This lets the entry creation
206 // logic do its job below, if it turns out there is no existing best entry
207 // or the best entry doesn't have a container.
210 // We need to do the entry search again for the new size. We skip pruning
211 // because we did this above once already, but ImageContainer is threadsafe,
212 // so there is a remote possibility it got freed.
213 i
= mImageContainers
.Length() - 1;
214 for (; i
>= 0; --i
) {
215 entry
= &mImageContainers
[i
];
216 if (bestSize
== entry
->mSize
&& flags
== entry
->mFlags
&&
217 aSVGContext
== entry
->mSVGContext
) {
218 container
= entry
->mContainer
.get();
220 switch (entry
->mLastDrawResult
) {
221 case ImgDrawResult::SUCCESS
:
222 case ImgDrawResult::BAD_IMAGE
:
223 case ImgDrawResult::BAD_ARGS
:
224 case ImgDrawResult::NOT_SUPPORTED
:
225 container
.forget(aOutContainer
);
226 return entry
->mLastDrawResult
;
227 case ImgDrawResult::NOT_READY
:
228 case ImgDrawResult::INCOMPLETE
:
229 case ImgDrawResult::TEMPORARY_ERROR
:
230 // Temporary conditions where we need to rerequest the frame to
231 // recover. We have already done so!
233 case ImgDrawResult::WRONG_SIZE
:
234 // Unused by GetFrameInternal
236 MOZ_ASSERT_UNREACHABLE("Unhandled DrawResult type!");
237 container
.forget(aOutContainer
);
238 return entry
->mLastDrawResult
;
247 // We need a new ImageContainer, so create one.
248 container
= LayerManager::CreateImageContainer();
251 entry
->mContainer
= container
;
253 entry
= mImageContainers
.AppendElement(
254 ImageContainerEntry(bestSize
, aSVGContext
, container
.get(), flags
));
258 SetCurrentImage(container
, surface
, Nothing());
259 entry
->mLastDrawResult
= drawResult
;
260 container
.forget(aOutContainer
);
264 bool ImageResource::UpdateImageContainer(const Maybe
<IntRect
>& aDirtyRect
) {
265 MOZ_ASSERT(NS_IsMainThread());
267 for (int i
= mImageContainers
.Length() - 1; i
>= 0; --i
) {
268 ImageContainerEntry
& entry
= mImageContainers
[i
];
269 RefPtr
<ImageContainer
> container
= entry
.mContainer
.get();
272 RefPtr
<SourceSurface
> surface
;
273 Tie(entry
.mLastDrawResult
, bestSize
, surface
) = GetFrameInternal(
274 entry
.mSize
, entry
.mSVGContext
, FRAME_CURRENT
, entry
.mFlags
);
276 // It is possible that this is a factor-of-2 substitution. Since we
277 // managed to convert the weak reference into a strong reference, that
278 // means that an imagelib user still is holding onto the container. thus
279 // we cannot consolidate and must keep updating the duplicate container.
281 SetCurrentImage(container
, surface
, aDirtyRect
);
283 IntRect
dirtyRect(IntPoint(0, 0), bestSize
);
284 SetCurrentImage(container
, surface
, Some(dirtyRect
));
287 // Stop tracking if our weak pointer to the image container was freed.
288 mImageContainers
.RemoveElementAt(i
);
292 return !mImageContainers
.IsEmpty();
295 void ImageResource::ReleaseImageContainer() {
296 MOZ_ASSERT(NS_IsMainThread());
297 mImageContainers
.Clear();
301 ImageResource::ImageResource(nsIURI
* aURI
)
304 mAnimationConsumers(0),
305 mAnimationMode(kNormalAnimMode
),
309 mImageProducerID(ImageContainer::AllocateProducerID()),
312 ImageResource::~ImageResource() {
313 // Ask our ProgressTracker to drop its weak reference to us.
314 mProgressTracker
->ResetImage();
317 void ImageResource::IncrementAnimationConsumers() {
318 MOZ_ASSERT(NS_IsMainThread(),
319 "Main thread only to encourage serialization "
320 "with DecrementAnimationConsumers");
321 mAnimationConsumers
++;
324 void ImageResource::DecrementAnimationConsumers() {
325 MOZ_ASSERT(NS_IsMainThread(),
326 "Main thread only to encourage serialization "
327 "with IncrementAnimationConsumers");
328 MOZ_ASSERT(mAnimationConsumers
>= 1, "Invalid no. of animation consumers!");
329 mAnimationConsumers
--;
332 nsresult
ImageResource::GetAnimationModeInternal(uint16_t* aAnimationMode
) {
334 return NS_ERROR_FAILURE
;
337 NS_ENSURE_ARG_POINTER(aAnimationMode
);
339 *aAnimationMode
= mAnimationMode
;
343 nsresult
ImageResource::SetAnimationModeInternal(uint16_t aAnimationMode
) {
345 return NS_ERROR_FAILURE
;
348 NS_ASSERTION(aAnimationMode
== kNormalAnimMode
||
349 aAnimationMode
== kDontAnimMode
||
350 aAnimationMode
== kLoopOnceAnimMode
,
351 "Wrong Animation Mode is being set!");
353 mAnimationMode
= aAnimationMode
;
358 bool ImageResource::HadRecentRefresh(const TimeStamp
& aTime
) {
359 // Our threshold for "recent" is 1/2 of the default refresh-driver interval.
360 // This ensures that we allow for frame rates at least as fast as the
361 // refresh driver's default rate.
362 static TimeDuration recentThreshold
=
363 TimeDuration::FromMilliseconds(nsRefreshDriver::DefaultInterval() / 2.0);
365 if (!mLastRefreshTime
.IsNull() &&
366 aTime
- mLastRefreshTime
< recentThreshold
) {
370 // else, we can proceed with a refresh.
371 // But first, update our last refresh time:
372 mLastRefreshTime
= aTime
;
376 void ImageResource::EvaluateAnimation() {
377 if (!mAnimating
&& ShouldAnimate()) {
378 nsresult rv
= StartAnimation();
379 mAnimating
= NS_SUCCEEDED(rv
);
380 } else if (mAnimating
&& !ShouldAnimate()) {
385 void ImageResource::SendOnUnlockedDraw(uint32_t aFlags
) {
386 if (!mProgressTracker
) {
390 if (!(aFlags
& FLAG_ASYNC_NOTIFY
)) {
391 mProgressTracker
->OnUnlockedDraw();
393 NotNull
<RefPtr
<ImageResource
>> image
= WrapNotNull(this);
394 nsCOMPtr
<nsIEventTarget
> eventTarget
= mProgressTracker
->GetEventTarget();
395 nsCOMPtr
<nsIRunnable
> ev
= NS_NewRunnableFunction(
396 "image::ImageResource::SendOnUnlockedDraw", [=]() -> void {
397 RefPtr
<ProgressTracker
> tracker
= image
->GetProgressTracker();
399 tracker
->OnUnlockedDraw();
402 eventTarget
->Dispatch(CreateMediumHighRunnable(ev
.forget()),
408 void ImageResource::NotifyDrawingObservers() {
409 if (!mURI
|| !NS_IsMainThread()) {
413 if (!mURI
->SchemeIs("resource") && !mURI
->SchemeIs("chrome")) {
417 // Record the image drawing for startup performance testing.
418 nsCOMPtr
<nsIURI
> uri
= mURI
;
419 nsContentUtils::AddScriptRunner(NS_NewRunnableFunction(
420 "image::ImageResource::NotifyDrawingObservers", [uri
]() {
421 nsCOMPtr
<nsIObserverService
> obs
= services::GetObserverService();
422 NS_WARNING_ASSERTION(obs
, "Can't get an observer service handle");
426 obs
->NotifyObservers(nullptr, "image-drawing",
427 NS_ConvertUTF8toUTF16(spec
).get());
434 } // namespace mozilla