Bug 1490079 [wpt PR 12936] - Mark tools/wpt Windows test failures as xfail, a=testonly
[gecko.git] / image / Image.h
blobab405e6228a5317aa0b4012e5b5a8dd44e2dabad
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"
14 #include "ImageContainer.h"
15 #include "LookupResult.h"
16 #include "nsStringFwd.h"
17 #include "ProgressTracker.h"
18 #include "SurfaceCache.h"
20 class nsIRequest;
21 class nsIInputStream;
23 namespace mozilla {
24 namespace image {
26 class Image;
28 ///////////////////////////////////////////////////////////////////////////////
29 // Memory Reporting
30 ///////////////////////////////////////////////////////////////////////////////
32 struct MemoryCounter
34 MemoryCounter()
35 : mSource(0)
36 , mDecodedHeap(0)
37 , mDecodedNonHeap(0)
38 , mExternalHandles(0)
39 { }
41 void SetSource(size_t aCount) { mSource = aCount; }
42 size_t Source() const { return mSource; }
43 void SetDecodedHeap(size_t aCount) { mDecodedHeap = aCount; }
44 size_t DecodedHeap() const { return mDecodedHeap; }
45 void SetDecodedNonHeap(size_t aCount) { mDecodedNonHeap = aCount; }
46 size_t DecodedNonHeap() const { return mDecodedNonHeap; }
47 void SetExternalHandles(size_t aCount) { mExternalHandles = aCount; }
48 size_t ExternalHandles() const { return mExternalHandles; }
50 MemoryCounter& operator+=(const MemoryCounter& aOther)
52 mSource += aOther.mSource;
53 mDecodedHeap += aOther.mDecodedHeap;
54 mDecodedNonHeap += aOther.mDecodedNonHeap;
55 mExternalHandles += aOther.mExternalHandles;
56 return *this;
59 private:
60 size_t mSource;
61 size_t mDecodedHeap;
62 size_t mDecodedNonHeap;
63 size_t mExternalHandles;
66 enum class SurfaceMemoryCounterType
68 NORMAL,
69 COMPOSITING,
70 COMPOSITING_PREV
73 struct SurfaceMemoryCounter
75 SurfaceMemoryCounter(const SurfaceKey& aKey,
76 bool aIsLocked,
77 bool aCannotSubstitute,
78 bool aIsFactor2,
79 SurfaceMemoryCounterType aType =
80 SurfaceMemoryCounterType::NORMAL)
81 : mKey(aKey)
82 , mType(aType)
83 , mIsLocked(aIsLocked)
84 , mCannotSubstitute(aCannotSubstitute)
85 , mIsFactor2(aIsFactor2)
86 { }
88 const SurfaceKey& Key() const { return mKey; }
89 MemoryCounter& Values() { return mValues; }
90 const MemoryCounter& Values() const { return mValues; }
91 SurfaceMemoryCounterType Type() const { return mType; }
92 bool IsLocked() const { return mIsLocked; }
93 bool CannotSubstitute() const { return mCannotSubstitute; }
94 bool IsFactor2() const { return mIsFactor2; }
96 private:
97 const SurfaceKey mKey;
98 MemoryCounter mValues;
99 const SurfaceMemoryCounterType mType;
100 const bool mIsLocked;
101 const bool mCannotSubstitute;
102 const bool mIsFactor2;
105 struct ImageMemoryCounter
107 ImageMemoryCounter(Image* aImage, SizeOfState& aState, bool aIsUsed);
109 nsCString& URI() { return mURI; }
110 const nsCString& URI() const { return mURI; }
111 const nsTArray<SurfaceMemoryCounter>& Surfaces() const { return mSurfaces; }
112 const gfx::IntSize IntrinsicSize() const { return mIntrinsicSize; }
113 const MemoryCounter& Values() const { return mValues; }
114 uint16_t Type() const { return mType; }
115 bool IsUsed() const { return mIsUsed; }
117 bool IsNotable() const
119 const size_t NotableThreshold = 16 * 1024;
120 size_t total = mValues.Source() + mValues.DecodedHeap()
121 + mValues.DecodedNonHeap();
122 return total >= NotableThreshold;
125 private:
126 nsCString mURI;
127 nsTArray<SurfaceMemoryCounter> mSurfaces;
128 gfx::IntSize mIntrinsicSize;
129 MemoryCounter mValues;
130 uint16_t mType;
131 const bool mIsUsed;
135 ///////////////////////////////////////////////////////////////////////////////
136 // Image Base Types
137 ///////////////////////////////////////////////////////////////////////////////
139 class Image : public imgIContainer
141 public:
143 * Flags for Image initialization.
145 * Meanings:
147 * INIT_FLAG_NONE: Lack of flags
149 * INIT_FLAG_DISCARDABLE: The container should be discardable
151 * INIT_FLAG_DECODE_IMMEDIATELY: The container should decode as soon as
152 * possible, regardless of what our heuristics say.
154 * INIT_FLAG_TRANSIENT: The container is likely to exist for only a short time
155 * before being destroyed. (For example, containers for
156 * multipart/x-mixed-replace image parts fall into this category.) If this
157 * flag is set, INIT_FLAG_DISCARDABLE and INIT_FLAG_DECODE_ONLY_ON_DRAW must
158 * not be set.
160 * INIT_FLAG_SYNC_LOAD: The container is being loaded synchronously, so
161 * it should avoid relying on async workers to get the container ready.
163 static const uint32_t INIT_FLAG_NONE = 0x0;
164 static const uint32_t INIT_FLAG_DISCARDABLE = 0x1;
165 static const uint32_t INIT_FLAG_DECODE_IMMEDIATELY = 0x2;
166 static const uint32_t INIT_FLAG_TRANSIENT = 0x4;
167 static const uint32_t INIT_FLAG_SYNC_LOAD = 0x8;
169 virtual already_AddRefed<ProgressTracker> GetProgressTracker() = 0;
170 virtual void SetProgressTracker(ProgressTracker* aProgressTracker) {}
173 * The size, in bytes, occupied by the compressed source data of the image.
174 * If MallocSizeOf does not work on this platform, uses a fallback approach to
175 * ensure that something reasonable is always returned.
177 virtual size_t
178 SizeOfSourceWithComputedFallback(SizeOfState& aState) const = 0;
181 * Collect an accounting of the memory occupied by the image's surfaces (which
182 * together make up its decoded data). Each surface is recorded as a separate
183 * SurfaceMemoryCounter, stored in @aCounters.
185 virtual void CollectSizeOfSurfaces(nsTArray<SurfaceMemoryCounter>& aCounters,
186 MallocSizeOf aMallocSizeOf) const = 0;
188 virtual void IncrementAnimationConsumers() = 0;
189 virtual void DecrementAnimationConsumers() = 0;
190 #ifdef DEBUG
191 virtual uint32_t GetAnimationConsumers() = 0;
192 #endif
195 * Called from OnDataAvailable when the stream associated with the image has
196 * received new image data. The arguments are the same as OnDataAvailable's,
197 * but by separating this functionality into a different method we don't
198 * interfere with subclasses which wish to implement nsIStreamListener.
200 * Images should not do anything that could send out notifications until they
201 * have received their first OnImageDataAvailable notification; in
202 * particular, this means that instantiating decoders should be deferred
203 * until OnImageDataAvailable is called.
205 virtual nsresult OnImageDataAvailable(nsIRequest* aRequest,
206 nsISupports* aContext,
207 nsIInputStream* aInStr,
208 uint64_t aSourceOffset,
209 uint32_t aCount) = 0;
212 * Called from OnStopRequest when the image's underlying request completes.
214 * @param aRequest The completed request.
215 * @param aContext Context from Necko's OnStopRequest.
216 * @param aStatus A success or failure code.
217 * @param aLastPart Whether this is the final part of the underlying request.
219 virtual nsresult OnImageDataComplete(nsIRequest* aRequest,
220 nsISupports* aContext,
221 nsresult aStatus,
222 bool aLastPart) = 0;
225 * Called when the SurfaceCache discards a surface belonging to this image.
227 virtual void OnSurfaceDiscarded(const SurfaceKey& aSurfaceKey) = 0;
229 virtual void SetInnerWindowID(uint64_t aInnerWindowId) = 0;
230 virtual uint64_t InnerWindowID() const = 0;
232 virtual bool HasError() = 0;
233 virtual void SetHasError() = 0;
235 virtual nsIURI* GetURI() const = 0;
237 virtual void ReportUseCounters() { }
240 class ImageResource : public Image
242 public:
243 already_AddRefed<ProgressTracker> GetProgressTracker() override
245 RefPtr<ProgressTracker> progressTracker = mProgressTracker;
246 MOZ_ASSERT(progressTracker);
247 return progressTracker.forget();
250 void SetProgressTracker(ProgressTracker* aProgressTracker) final
252 MOZ_ASSERT(aProgressTracker);
253 MOZ_ASSERT(!mProgressTracker);
254 mProgressTracker = aProgressTracker;
257 virtual void IncrementAnimationConsumers() override;
258 virtual void DecrementAnimationConsumers() override;
259 #ifdef DEBUG
260 virtual uint32_t GetAnimationConsumers() override
262 return mAnimationConsumers;
264 #endif
266 virtual void OnSurfaceDiscarded(const SurfaceKey& aSurfaceKey) override { }
268 virtual void SetInnerWindowID(uint64_t aInnerWindowId) override
270 mInnerWindowId = aInnerWindowId;
272 virtual uint64_t InnerWindowID() const override { return mInnerWindowId; }
274 virtual bool HasError() override { return mError; }
275 virtual void SetHasError() override { mError = true; }
278 * Returns a non-AddRefed pointer to the URI associated with this image.
279 * Illegal to use off-main-thread.
281 nsIURI* GetURI() const override { return mURI; }
283 protected:
284 explicit ImageResource(nsIURI* aURI);
285 ~ImageResource();
287 bool GetSpecTruncatedTo1k(nsCString& aSpec) const;
289 // Shared functionality for implementors of imgIContainer. Every
290 // implementation of attribute animationMode should forward here.
291 nsresult GetAnimationModeInternal(uint16_t* aAnimationMode);
292 nsresult SetAnimationModeInternal(uint16_t aAnimationMode);
295 * Helper for RequestRefresh.
297 * If we've had a "recent" refresh (i.e. if this image is being used in
298 * multiple documents & some other document *just* called RequestRefresh() on
299 * this image with a timestamp close to aTime), this method returns true.
301 * Otherwise, this method updates mLastRefreshTime to aTime & returns false.
303 bool HadRecentRefresh(const TimeStamp& aTime);
306 * Decides whether animation should or should not be happening,
307 * and makes sure the right thing is being done.
309 virtual void EvaluateAnimation();
312 * Extended by child classes, if they have additional
313 * conditions for being able to animate.
315 virtual bool ShouldAnimate() {
316 return mAnimationConsumers > 0 && mAnimationMode != kDontAnimMode;
319 virtual nsresult StartAnimation() = 0;
320 virtual nsresult StopAnimation() = 0;
322 void SendOnUnlockedDraw(uint32_t aFlags);
324 #ifdef DEBUG
325 // Records the image drawing for startup performance testing.
326 void NotifyDrawingObservers();
327 #endif
329 // Member data shared by all implementations of this abstract class
330 RefPtr<ProgressTracker> mProgressTracker;
331 nsCOMPtr<nsIURI> mURI;
332 TimeStamp mLastRefreshTime;
333 uint64_t mInnerWindowId;
334 uint32_t mAnimationConsumers;
335 uint16_t mAnimationMode; // Enum values in imgIContainer
336 bool mInitialized:1; // Have we been initalized?
337 bool mAnimating:1; // Are we currently animating?
338 bool mError:1; // Error handling
340 virtual Tuple<ImgDrawResult, gfx::IntSize, RefPtr<gfx::SourceSurface>>
341 GetFrameInternal(const gfx::IntSize& aSize,
342 const Maybe<SVGImageContext>& aSVGContext,
343 uint32_t aWhichFrame,
344 uint32_t aFlags)
346 return MakeTuple(ImgDrawResult::BAD_IMAGE, aSize,
347 RefPtr<gfx::SourceSurface>());
351 * Calculate the estimated size to use for an image container with the given
352 * parameters. It may not be the same as the given size, and it may not be
353 * the same as the size of the surface in the image container, but it is the
354 * best effort estimate.
356 virtual Tuple<ImgDrawResult, gfx::IntSize>
357 GetImageContainerSize(layers::LayerManager* aManager,
358 const gfx::IntSize& aSize,
359 uint32_t aFlags)
361 return MakeTuple(ImgDrawResult::NOT_SUPPORTED, gfx::IntSize(0, 0));
364 ImgDrawResult GetImageContainerImpl(layers::LayerManager* aManager,
365 const gfx::IntSize& aSize,
366 const Maybe<SVGImageContext>& aSVGContext,
367 uint32_t aFlags,
368 layers::ImageContainer** aContainer);
370 void UpdateImageContainer();
372 void ReleaseImageContainer();
374 private:
375 void SetCurrentImage(layers::ImageContainer* aContainer,
376 gfx::SourceSurface* aSurface,
377 bool aInTransaction);
379 struct ImageContainerEntry {
380 ImageContainerEntry(const gfx::IntSize& aSize,
381 const Maybe<SVGImageContext>& aSVGContext,
382 layers::ImageContainer* aContainer,
383 uint32_t aFlags)
384 : mSize(aSize)
385 , mSVGContext(aSVGContext)
386 , mContainer(aContainer)
387 , mLastDrawResult(ImgDrawResult::NOT_READY)
388 , mFlags(aFlags)
391 gfx::IntSize mSize;
392 Maybe<SVGImageContext> mSVGContext;
393 // A weak pointer to our ImageContainer, which stays alive only as long as
394 // the layer system needs it.
395 WeakPtr<layers::ImageContainer> mContainer;
396 // If mContainer is non-null, this contains the ImgDrawResult we obtained
397 // the last time we updated it.
398 ImgDrawResult mLastDrawResult;
399 // Cached flags to use for decoding. FLAG_ASYNC_NOTIFY should always be set
400 // but FLAG_HIGH_QUALITY_SCALING may vary.
401 uint32_t mFlags;
404 AutoTArray<ImageContainerEntry, 1> mImageContainers;
405 layers::ImageContainer::ProducerID mImageProducerID;
406 layers::ImageContainer::FrameID mLastFrameID;
409 } // namespace image
410 } // namespace mozilla
412 #endif // mozilla_image_Image_h