Bug 1578501 [wpt PR 18803] - WebKit export of https://bugs.webkit.org/show_bug.cgi...
[gecko.git] / image / Image.cpp
blobc4c3f0b04b16ea2fa3ec9627ff277053e0733ae0
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 "Image.h"
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"
16 namespace mozilla {
17 namespace image {
19 ///////////////////////////////////////////////////////////////////////////////
20 // Memory Reporting
21 ///////////////////////////////////////////////////////////////////////////////
23 ImageMemoryCounter::ImageMemoryCounter(Image* aImage, SizeOfState& aState,
24 bool aIsUsed)
25 : mIsUsed(aIsUsed) {
26 MOZ_ASSERT(aImage);
28 // Extract metadata about the image.
29 nsCOMPtr<nsIURI> imageURL(aImage->GetURI());
30 if (imageURL) {
31 imageURL->GetSpec(mURI);
34 int32_t width = 0;
35 int32_t height = 0;
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);
46 // Compute totals.
47 for (const SurfaceMemoryCounter& surfaceCounter : mSurfaces) {
48 mValues += surfaceCounter.Values();
52 ///////////////////////////////////////////////////////////////////////////////
53 // Image Base Types
54 ///////////////////////////////////////////////////////////////////////////////
56 bool ImageResource::GetSpecTruncatedTo1k(nsCString& aSpec) const {
57 static const size_t sMaxTruncatedLength = 1024;
59 mURI->GetSpec(aSpec);
60 if (sMaxTruncatedLength >= aSpec.Length()) {
61 return true;
64 aSpec.Truncate(sMaxTruncatedLength);
65 return false;
68 void ImageResource::SetCurrentImage(ImageContainer* aContainer,
69 SourceSurface* aSurface,
70 const Maybe<IntRect>& aDirtyRect) {
71 MOZ_ASSERT(NS_IsMainThread());
72 MOZ_ASSERT(aContainer);
74 if (!aSurface) {
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.
77 return;
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));
94 if (aDirtyRect) {
95 aContainer->SetCurrentImagesInTransaction(imageList);
96 } else {
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
102 // WebRender.
103 if (mProgressTracker->GetProgress() & FLAG_IS_ANIMATED) {
104 if (aDirtyRect) {
105 layers::SharedSurfacesChild::UpdateAnimation(aContainer, aSurface,
106 aDirtyRect.ref());
107 } else {
108 IntRect dirtyRect(IntPoint(0, 0), aSurface->GetSize());
109 layers::SharedSurfacesChild::UpdateAnimation(aContainer, aSurface,
110 dirtyRect);
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);
121 MOZ_ASSERT(
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;
127 IntSize size;
128 Tie(drawResult, size) = GetImageContainerSize(aManager, aSize, aFlags);
129 if (drawResult != ImgDrawResult::SUCCESS) {
130 return drawResult;
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)) |
140 FLAG_ASYNC_NOTIFY;
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.
150 break;
151 } else if (!container) {
152 // Stop tracking if our weak pointer to the image container was freed.
153 mImageContainers.RemoveElementAt(i);
154 } else {
155 // It isn't a match, but still valid. Forget the container so we don't
156 // try to reuse it below.
157 container = nullptr;
161 if (container) {
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.
173 break;
174 case ImgDrawResult::WRONG_SIZE:
175 // Unused by GetFrameInternal
176 default:
177 MOZ_ASSERT_UNREACHABLE("Unhandled ImgDrawResult type!");
178 container.forget(aOutContainer);
179 return entry->mLastDrawResult;
183 #ifdef DEBUG
184 NotifyDrawingObservers();
185 #endif
187 IntSize bestSize;
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.
208 container = nullptr;
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();
219 if (container) {
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!
232 break;
233 case ImgDrawResult::WRONG_SIZE:
234 // Unused by GetFrameInternal
235 default:
236 MOZ_ASSERT_UNREACHABLE("Unhandled DrawResult type!");
237 container.forget(aOutContainer);
238 return entry->mLastDrawResult;
241 break;
246 if (!container) {
247 // We need a new ImageContainer, so create one.
248 container = LayerManager::CreateImageContainer();
250 if (i >= 0) {
251 entry->mContainer = container;
252 } else {
253 entry = mImageContainers.AppendElement(
254 ImageContainerEntry(bestSize, aSVGContext, container.get(), flags));
258 SetCurrentImage(container, surface, Nothing());
259 entry->mLastDrawResult = drawResult;
260 container.forget(aOutContainer);
261 return drawResult;
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();
270 if (container) {
271 IntSize bestSize;
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.
280 if (aDirtyRect) {
281 SetCurrentImage(container, surface, aDirtyRect);
282 } else {
283 IntRect dirtyRect(IntPoint(0, 0), bestSize);
284 SetCurrentImage(container, surface, Some(dirtyRect));
286 } else {
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();
300 // Constructor
301 ImageResource::ImageResource(nsIURI* aURI)
302 : mURI(aURI),
303 mInnerWindowId(0),
304 mAnimationConsumers(0),
305 mAnimationMode(kNormalAnimMode),
306 mInitialized(false),
307 mAnimating(false),
308 mError(false),
309 mImageProducerID(ImageContainer::AllocateProducerID()),
310 mLastFrameID(0) {}
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) {
333 if (mError) {
334 return NS_ERROR_FAILURE;
337 NS_ENSURE_ARG_POINTER(aAnimationMode);
339 *aAnimationMode = mAnimationMode;
340 return NS_OK;
343 nsresult ImageResource::SetAnimationModeInternal(uint16_t aAnimationMode) {
344 if (mError) {
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;
355 return NS_OK;
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) {
367 return true;
370 // else, we can proceed with a refresh.
371 // But first, update our last refresh time:
372 mLastRefreshTime = aTime;
373 return false;
376 void ImageResource::EvaluateAnimation() {
377 if (!mAnimating && ShouldAnimate()) {
378 nsresult rv = StartAnimation();
379 mAnimating = NS_SUCCEEDED(rv);
380 } else if (mAnimating && !ShouldAnimate()) {
381 StopAnimation();
385 void ImageResource::SendOnUnlockedDraw(uint32_t aFlags) {
386 if (!mProgressTracker) {
387 return;
390 if (!(aFlags & FLAG_ASYNC_NOTIFY)) {
391 mProgressTracker->OnUnlockedDraw();
392 } else {
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();
398 if (tracker) {
399 tracker->OnUnlockedDraw();
402 eventTarget->Dispatch(CreateMediumHighRunnable(ev.forget()),
403 NS_DISPATCH_NORMAL);
407 #ifdef DEBUG
408 void ImageResource::NotifyDrawingObservers() {
409 if (!mURI || !NS_IsMainThread()) {
410 return;
413 if (!mURI->SchemeIs("resource") && !mURI->SchemeIs("chrome")) {
414 return;
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");
423 if (obs) {
424 nsAutoCString spec;
425 uri->GetSpec(spec);
426 obs->NotifyObservers(nullptr, "image-drawing",
427 NS_ConvertUTF8toUTF16(spec).get());
429 }));
431 #endif
433 } // namespace image
434 } // namespace mozilla