Bug 1622408 [wpt PR 22244] - Restore the event delegate for a CSSTransition after...
[gecko.git] / image / Image.cpp
blob0ac85f534be35201dc23fb9ae27127f8c009b736
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 "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"
17 namespace mozilla {
18 namespace image {
20 ///////////////////////////////////////////////////////////////////////////////
21 // Memory Reporting
22 ///////////////////////////////////////////////////////////////////////////////
24 ImageMemoryCounter::ImageMemoryCounter(imgRequest* aRequest,
25 SizeOfState& aState, bool aIsUsed)
26 : mProgress(UINT32_MAX),
27 mType(UINT16_MAX),
28 mIsUsed(aIsUsed),
29 mHasError(false),
30 mValidating(false) {
31 MOZ_ASSERT(aRequest);
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();
45 if (tracker) {
46 mProgress = tracker->GetProgress();
50 ImageMemoryCounter::ImageMemoryCounter(imgRequest* aRequest, Image* aImage,
51 SizeOfState& aState, bool aIsUsed)
52 : mProgress(UINT32_MAX),
53 mType(UINT16_MAX),
54 mIsUsed(aIsUsed),
55 mHasError(false),
56 mValidating(false) {
57 MOZ_ASSERT(aRequest);
58 MOZ_ASSERT(aImage);
60 // Extract metadata about the image.
61 nsCOMPtr<nsIURI> imageURL(aImage->GetURI());
62 if (imageURL) {
63 imageURL->GetSpec(mURI);
66 int32_t width = 0;
67 int32_t height = 0;
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();
77 if (tracker) {
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);
85 // Compute totals.
86 for (const SurfaceMemoryCounter& surfaceCounter : mSurfaces) {
87 mValues += surfaceCounter.Values();
91 ///////////////////////////////////////////////////////////////////////////////
92 // Image Base Types
93 ///////////////////////////////////////////////////////////////////////////////
95 bool ImageResource::GetSpecTruncatedTo1k(nsCString& aSpec) const {
96 static const size_t sMaxTruncatedLength = 1024;
98 mURI->GetSpec(aSpec);
99 if (sMaxTruncatedLength >= aSpec.Length()) {
100 return true;
103 aSpec.Truncate(sMaxTruncatedLength);
104 return false;
107 void ImageResource::SetCurrentImage(ImageContainer* aContainer,
108 SourceSurface* aSurface,
109 const Maybe<IntRect>& aDirtyRect) {
110 MOZ_ASSERT(NS_IsMainThread());
111 MOZ_ASSERT(aContainer);
113 if (!aSurface) {
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.
116 return;
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));
133 if (aDirtyRect) {
134 aContainer->SetCurrentImagesInTransaction(imageList);
135 } else {
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
141 // WebRender.
142 if (mProgressTracker->GetProgress() & FLAG_IS_ANIMATED) {
143 if (aDirtyRect) {
144 layers::SharedSurfacesChild::UpdateAnimation(aContainer, aSurface,
145 aDirtyRect.ref());
146 } else {
147 IntRect dirtyRect(IntPoint(0, 0), aSurface->GetSize());
148 layers::SharedSurfacesChild::UpdateAnimation(aContainer, aSurface,
149 dirtyRect);
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);
160 MOZ_ASSERT(
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;
166 IntSize size;
167 Tie(drawResult, size) = GetImageContainerSize(aManager, aSize, aFlags);
168 if (drawResult != ImgDrawResult::SUCCESS) {
169 return drawResult;
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)) |
179 FLAG_ASYNC_NOTIFY;
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.
189 break;
190 } else if (!container) {
191 // Stop tracking if our weak pointer to the image container was freed.
192 mImageContainers.RemoveElementAt(i);
193 } else {
194 // It isn't a match, but still valid. Forget the container so we don't
195 // try to reuse it below.
196 container = nullptr;
200 if (container) {
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.
212 break;
213 case ImgDrawResult::WRONG_SIZE:
214 // Unused by GetFrameInternal
215 default:
216 MOZ_ASSERT_UNREACHABLE("Unhandled ImgDrawResult type!");
217 container.forget(aOutContainer);
218 return entry->mLastDrawResult;
222 #ifdef DEBUG
223 NotifyDrawingObservers();
224 #endif
226 IntSize bestSize;
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.
247 container = nullptr;
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();
258 if (container) {
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!
271 break;
272 case ImgDrawResult::WRONG_SIZE:
273 // Unused by GetFrameInternal
274 default:
275 MOZ_ASSERT_UNREACHABLE("Unhandled DrawResult type!");
276 container.forget(aOutContainer);
277 return entry->mLastDrawResult;
280 break;
285 if (!container) {
286 // We need a new ImageContainer, so create one.
287 container = LayerManager::CreateImageContainer();
289 if (i >= 0) {
290 entry->mContainer = container;
291 } else {
292 entry = mImageContainers.AppendElement(
293 ImageContainerEntry(bestSize, aSVGContext, container.get(), flags));
297 SetCurrentImage(container, surface, Nothing());
298 entry->mLastDrawResult = drawResult;
299 container.forget(aOutContainer);
300 return drawResult;
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();
309 if (container) {
310 IntSize bestSize;
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.
319 if (aDirtyRect) {
320 SetCurrentImage(container, surface, aDirtyRect);
321 } else {
322 IntRect dirtyRect(IntPoint(0, 0), bestSize);
323 SetCurrentImage(container, surface, Some(dirtyRect));
325 } else {
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();
339 // Constructor
340 ImageResource::ImageResource(nsIURI* aURI)
341 : mURI(aURI),
342 mInnerWindowId(0),
343 mAnimationConsumers(0),
344 mAnimationMode(kNormalAnimMode),
345 mInitialized(false),
346 mAnimating(false),
347 mError(false),
348 mImageProducerID(ImageContainer::AllocateProducerID()),
349 mLastFrameID(0) {}
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) {
372 if (mError) {
373 return NS_ERROR_FAILURE;
376 NS_ENSURE_ARG_POINTER(aAnimationMode);
378 *aAnimationMode = mAnimationMode;
379 return NS_OK;
382 nsresult ImageResource::SetAnimationModeInternal(uint16_t aAnimationMode) {
383 if (mError) {
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;
394 return NS_OK;
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) {
406 return true;
409 // else, we can proceed with a refresh.
410 // But first, update our last refresh time:
411 mLastRefreshTime = aTime;
412 return false;
415 void ImageResource::EvaluateAnimation() {
416 if (!mAnimating && ShouldAnimate()) {
417 nsresult rv = StartAnimation();
418 mAnimating = NS_SUCCEEDED(rv);
419 } else if (mAnimating && !ShouldAnimate()) {
420 StopAnimation();
424 void ImageResource::SendOnUnlockedDraw(uint32_t aFlags) {
425 if (!mProgressTracker) {
426 return;
429 if (!(aFlags & FLAG_ASYNC_NOTIFY)) {
430 mProgressTracker->OnUnlockedDraw();
431 } else {
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();
437 if (tracker) {
438 tracker->OnUnlockedDraw();
441 eventTarget->Dispatch(CreateMediumHighRunnable(ev.forget()),
442 NS_DISPATCH_NORMAL);
446 #ifdef DEBUG
447 void ImageResource::NotifyDrawingObservers() {
448 if (!mURI || !NS_IsMainThread()) {
449 return;
452 if (!mURI->SchemeIs("resource") && !mURI->SchemeIs("chrome")) {
453 return;
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");
462 if (obs) {
463 nsAutoCString spec;
464 uri->GetSpec(spec);
465 obs->NotifyObservers(nullptr, "image-drawing",
466 NS_ConvertUTF8toUTF16(spec).get());
468 }));
470 #endif
472 } // namespace image
473 } // namespace mozilla