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 #ifndef mozilla_image_Image_h
7 #define mozilla_image_Image_h
9 #include "mozilla/MemoryReporting.h"
10 #include "mozilla/Tuple.h"
11 #include "mozilla/TimeStamp.h"
12 #include "gfx2DGlue.h"
13 #include "imgIContainer.h"
15 #include "ImageContainer.h"
16 #include "LookupResult.h"
17 #include "nsStringFwd.h"
18 #include "ProgressTracker.h"
19 #include "SurfaceCache.h"
29 ///////////////////////////////////////////////////////////////////////////////
31 ///////////////////////////////////////////////////////////////////////////////
42 void SetSource(size_t aCount
) { mSource
= aCount
; }
43 size_t Source() const { return mSource
; }
44 void SetDecodedHeap(size_t aCount
) { mDecodedHeap
= aCount
; }
45 size_t DecodedHeap() const { return mDecodedHeap
; }
46 void SetDecodedNonHeap(size_t aCount
) { mDecodedNonHeap
= aCount
; }
47 size_t DecodedNonHeap() const { return mDecodedNonHeap
; }
48 void SetExternalHandles(size_t aCount
) { mExternalHandles
= aCount
; }
49 size_t ExternalHandles() const { return mExternalHandles
; }
51 MemoryCounter
& operator+=(const MemoryCounter
& aOther
)
53 mSource
+= aOther
.mSource
;
54 mDecodedHeap
+= aOther
.mDecodedHeap
;
55 mDecodedNonHeap
+= aOther
.mDecodedNonHeap
;
56 mExternalHandles
+= aOther
.mExternalHandles
;
63 size_t mDecodedNonHeap
;
64 size_t mExternalHandles
;
67 enum class SurfaceMemoryCounterType
74 struct SurfaceMemoryCounter
76 SurfaceMemoryCounter(const SurfaceKey
& aKey
,
78 bool aCannotSubstitute
,
80 SurfaceMemoryCounterType aType
=
81 SurfaceMemoryCounterType::NORMAL
)
84 , mIsLocked(aIsLocked
)
85 , mCannotSubstitute(aCannotSubstitute
)
86 , mIsFactor2(aIsFactor2
)
89 const SurfaceKey
& Key() const { return mKey
; }
90 MemoryCounter
& Values() { return mValues
; }
91 const MemoryCounter
& Values() const { return mValues
; }
92 SurfaceMemoryCounterType
Type() const { return mType
; }
93 bool IsLocked() const { return mIsLocked
; }
94 bool CannotSubstitute() const { return mCannotSubstitute
; }
95 bool IsFactor2() const { return mIsFactor2
; }
98 const SurfaceKey mKey
;
99 MemoryCounter mValues
;
100 const SurfaceMemoryCounterType mType
;
101 const bool mIsLocked
;
102 const bool mCannotSubstitute
;
103 const bool mIsFactor2
;
106 struct ImageMemoryCounter
108 ImageMemoryCounter(Image
* aImage
, SizeOfState
& aState
, bool aIsUsed
);
110 nsCString
& URI() { return mURI
; }
111 const nsCString
& URI() const { return mURI
; }
112 const nsTArray
<SurfaceMemoryCounter
>& Surfaces() const { return mSurfaces
; }
113 const gfx::IntSize
IntrinsicSize() const { return mIntrinsicSize
; }
114 const MemoryCounter
& Values() const { return mValues
; }
115 uint16_t Type() const { return mType
; }
116 bool IsUsed() const { return mIsUsed
; }
118 bool IsNotable() const
120 const size_t NotableThreshold
= 16 * 1024;
121 size_t total
= mValues
.Source() + mValues
.DecodedHeap()
122 + mValues
.DecodedNonHeap();
123 return total
>= NotableThreshold
;
128 nsTArray
<SurfaceMemoryCounter
> mSurfaces
;
129 gfx::IntSize mIntrinsicSize
;
130 MemoryCounter mValues
;
136 ///////////////////////////////////////////////////////////////////////////////
138 ///////////////////////////////////////////////////////////////////////////////
140 class Image
: public imgIContainer
144 * Flags for Image initialization.
148 * INIT_FLAG_NONE: Lack of flags
150 * INIT_FLAG_DISCARDABLE: The container should be discardable
152 * INIT_FLAG_DECODE_IMMEDIATELY: The container should decode as soon as
153 * possible, regardless of what our heuristics say.
155 * INIT_FLAG_TRANSIENT: The container is likely to exist for only a short time
156 * before being destroyed. (For example, containers for
157 * multipart/x-mixed-replace image parts fall into this category.) If this
158 * flag is set, INIT_FLAG_DISCARDABLE and INIT_FLAG_DECODE_ONLY_ON_DRAW must
161 * INIT_FLAG_SYNC_LOAD: The container is being loaded synchronously, so
162 * it should avoid relying on async workers to get the container ready.
164 static const uint32_t INIT_FLAG_NONE
= 0x0;
165 static const uint32_t INIT_FLAG_DISCARDABLE
= 0x1;
166 static const uint32_t INIT_FLAG_DECODE_IMMEDIATELY
= 0x2;
167 static const uint32_t INIT_FLAG_TRANSIENT
= 0x4;
168 static const uint32_t INIT_FLAG_SYNC_LOAD
= 0x8;
170 virtual already_AddRefed
<ProgressTracker
> GetProgressTracker() = 0;
171 virtual void SetProgressTracker(ProgressTracker
* aProgressTracker
) {}
174 * The size, in bytes, occupied by the compressed source data of the image.
175 * If MallocSizeOf does not work on this platform, uses a fallback approach to
176 * ensure that something reasonable is always returned.
179 SizeOfSourceWithComputedFallback(SizeOfState
& aState
) const = 0;
182 * Collect an accounting of the memory occupied by the image's surfaces (which
183 * together make up its decoded data). Each surface is recorded as a separate
184 * SurfaceMemoryCounter, stored in @aCounters.
186 virtual void CollectSizeOfSurfaces(nsTArray
<SurfaceMemoryCounter
>& aCounters
,
187 MallocSizeOf aMallocSizeOf
) const = 0;
189 virtual void IncrementAnimationConsumers() = 0;
190 virtual void DecrementAnimationConsumers() = 0;
192 virtual uint32_t GetAnimationConsumers() = 0;
196 * Called from OnDataAvailable when the stream associated with the image has
197 * received new image data. The arguments are the same as OnDataAvailable's,
198 * but by separating this functionality into a different method we don't
199 * interfere with subclasses which wish to implement nsIStreamListener.
201 * Images should not do anything that could send out notifications until they
202 * have received their first OnImageDataAvailable notification; in
203 * particular, this means that instantiating decoders should be deferred
204 * until OnImageDataAvailable is called.
206 virtual nsresult
OnImageDataAvailable(nsIRequest
* aRequest
,
207 nsISupports
* aContext
,
208 nsIInputStream
* aInStr
,
209 uint64_t aSourceOffset
,
210 uint32_t aCount
) = 0;
213 * Called from OnStopRequest when the image's underlying request completes.
215 * @param aRequest The completed request.
216 * @param aContext Context from Necko's OnStopRequest.
217 * @param aStatus A success or failure code.
218 * @param aLastPart Whether this is the final part of the underlying request.
220 virtual nsresult
OnImageDataComplete(nsIRequest
* aRequest
,
221 nsISupports
* aContext
,
226 * Called when the SurfaceCache discards a surface belonging to this image.
228 virtual void OnSurfaceDiscarded(const SurfaceKey
& aSurfaceKey
) = 0;
230 virtual void SetInnerWindowID(uint64_t aInnerWindowId
) = 0;
231 virtual uint64_t InnerWindowID() const = 0;
233 virtual bool HasError() = 0;
234 virtual void SetHasError() = 0;
236 virtual ImageURL
* GetURI() = 0;
238 virtual void ReportUseCounters() { }
241 class ImageResource
: public Image
244 already_AddRefed
<ProgressTracker
> GetProgressTracker() override
246 RefPtr
<ProgressTracker
> progressTracker
= mProgressTracker
;
247 MOZ_ASSERT(progressTracker
);
248 return progressTracker
.forget();
251 void SetProgressTracker(ProgressTracker
* aProgressTracker
) final
253 MOZ_ASSERT(aProgressTracker
);
254 MOZ_ASSERT(!mProgressTracker
);
255 mProgressTracker
= aProgressTracker
;
258 virtual void IncrementAnimationConsumers() override
;
259 virtual void DecrementAnimationConsumers() override
;
261 virtual uint32_t GetAnimationConsumers() override
263 return mAnimationConsumers
;
267 virtual void OnSurfaceDiscarded(const SurfaceKey
& aSurfaceKey
) override
{ }
269 virtual void SetInnerWindowID(uint64_t aInnerWindowId
) override
271 mInnerWindowId
= aInnerWindowId
;
273 virtual uint64_t InnerWindowID() const override
{ return mInnerWindowId
; }
275 virtual bool HasError() override
{ return mError
; }
276 virtual void SetHasError() override
{ mError
= true; }
279 * Returns a non-AddRefed pointer to the URI associated with this image.
280 * Illegal to use off-main-thread.
282 virtual ImageURL
* GetURI() override
{ return mURI
.get(); }
285 explicit ImageResource(ImageURL
* aURI
);
288 // Shared functionality for implementors of imgIContainer. Every
289 // implementation of attribute animationMode should forward here.
290 nsresult
GetAnimationModeInternal(uint16_t* aAnimationMode
);
291 nsresult
SetAnimationModeInternal(uint16_t aAnimationMode
);
294 * Helper for RequestRefresh.
296 * If we've had a "recent" refresh (i.e. if this image is being used in
297 * multiple documents & some other document *just* called RequestRefresh() on
298 * this image with a timestamp close to aTime), this method returns true.
300 * Otherwise, this method updates mLastRefreshTime to aTime & returns false.
302 bool HadRecentRefresh(const TimeStamp
& aTime
);
305 * Decides whether animation should or should not be happening,
306 * and makes sure the right thing is being done.
308 virtual void EvaluateAnimation();
311 * Extended by child classes, if they have additional
312 * conditions for being able to animate.
314 virtual bool ShouldAnimate() {
315 return mAnimationConsumers
> 0 && mAnimationMode
!= kDontAnimMode
;
318 virtual nsresult
StartAnimation() = 0;
319 virtual nsresult
StopAnimation() = 0;
321 void SendOnUnlockedDraw(uint32_t aFlags
);
324 // Records the image drawing for startup performance testing.
325 void NotifyDrawingObservers();
328 // Member data shared by all implementations of this abstract class
329 RefPtr
<ProgressTracker
> mProgressTracker
;
330 RefPtr
<ImageURL
> mURI
;
331 TimeStamp mLastRefreshTime
;
332 uint64_t mInnerWindowId
;
333 uint32_t mAnimationConsumers
;
334 uint16_t mAnimationMode
; // Enum values in imgIContainer
335 bool mInitialized
:1; // Have we been initalized?
336 bool mAnimating
:1; // Are we currently animating?
337 bool mError
:1; // Error handling
339 virtual Tuple
<ImgDrawResult
, gfx::IntSize
, RefPtr
<gfx::SourceSurface
>>
340 GetFrameInternal(const gfx::IntSize
& aSize
,
341 const Maybe
<SVGImageContext
>& aSVGContext
,
342 uint32_t aWhichFrame
,
345 return MakeTuple(ImgDrawResult::BAD_IMAGE
, aSize
,
346 RefPtr
<gfx::SourceSurface
>());
350 * Calculate the estimated size to use for an image container with the given
351 * parameters. It may not be the same as the given size, and it may not be
352 * the same as the size of the surface in the image container, but it is the
353 * best effort estimate.
355 virtual gfx::IntSize
GetImageContainerSize(layers::LayerManager
* aManager
,
356 const gfx::IntSize
& aSize
,
359 return gfx::IntSize(0, 0);
362 already_AddRefed
<layers::ImageContainer
>
363 GetImageContainerImpl(layers::LayerManager
* aManager
,
364 const gfx::IntSize
& aSize
,
365 const Maybe
<SVGImageContext
>& aSVGContext
,
368 void UpdateImageContainer();
370 void ReleaseImageContainer();
373 void SetCurrentImage(layers::ImageContainer
* aContainer
,
374 gfx::SourceSurface
* aSurface
,
375 bool aInTransaction
);
377 struct ImageContainerEntry
{
378 ImageContainerEntry(const gfx::IntSize
& aSize
,
379 const Maybe
<SVGImageContext
>& aSVGContext
,
380 layers::ImageContainer
* aContainer
,
383 , mSVGContext(aSVGContext
)
384 , mContainer(aContainer
)
385 , mLastDrawResult(ImgDrawResult::NOT_READY
)
390 Maybe
<SVGImageContext
> mSVGContext
;
391 // A weak pointer to our ImageContainer, which stays alive only as long as
392 // the layer system needs it.
393 WeakPtr
<layers::ImageContainer
> mContainer
;
394 // If mContainer is non-null, this contains the ImgDrawResult we obtained
395 // the last time we updated it.
396 ImgDrawResult mLastDrawResult
;
397 // Cached flags to use for decoding. FLAG_ASYNC_NOTIFY should always be set
398 // but FLAG_HIGH_QUALITY_SCALING may vary.
402 AutoTArray
<ImageContainerEntry
, 1> mImageContainers
;
403 layers::ImageContainer::ProducerID mImageProducerID
;
404 layers::ImageContainer::FrameID mLastFrameID
;
408 } // namespace mozilla
410 #endif // mozilla_image_Image_h