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_IMAGELIB_IMAGE_H_
7 #define MOZILLA_IMAGELIB_IMAGE_H_
9 #include "mozilla/MemoryReporting.h"
10 #include "mozilla/TimeStamp.h"
11 #include "gfx2DGlue.h" // for gfxMemoryLocation
12 #include "imgIContainer.h"
13 #include "ProgressTracker.h"
15 #include "nsStringFwd.h"
23 class Image
: public imgIContainer
26 // Mimetype translation
30 eDecoderType_jpeg
= 2,
33 eDecoderType_icon
= 5,
34 eDecoderType_webp
= 6,
37 eDecoderType_unknown
= 8
39 eDecoderType_unknown
= 7
42 static eDecoderType
GetDecoderType(const char* aMimeType
);
45 * Flags for Image initialization.
49 * INIT_FLAG_NONE: Lack of flags
51 * INIT_FLAG_DISCARDABLE: The container should be discardable
53 * INIT_FLAG_DECODE_ON_DRAW: The container should decode on draw rather than
56 * INIT_FLAG_TRANSIENT: The container is likely to exist for only a short time
57 * before being destroyed. (For example, containers for
58 * multipart/x-mixed-replace image parts fall into this category.) If this
59 * flag is set, INIT_FLAG_DISCARDABLE and INIT_FLAG_DECODE_ON_DRAW must not be
62 * INIT_FLAG_DOWNSCALE_DURING_DECODE: The container should attempt to
63 * downscale images during decoding instead of decoding them to their
66 static const uint32_t INIT_FLAG_NONE
= 0x0;
67 static const uint32_t INIT_FLAG_DISCARDABLE
= 0x1;
68 static const uint32_t INIT_FLAG_DECODE_ON_DRAW
= 0x2;
69 static const uint32_t INIT_FLAG_TRANSIENT
= 0x4;
70 static const uint32_t INIT_FLAG_DOWNSCALE_DURING_DECODE
= 0x8;
71 static const uint32_t INIT_FLAG_SYNC_LOAD
= 0x20;
74 * Creates a new image container.
76 * @param aMimeType The mimetype of the image.
77 * @param aFlags Initialization flags of the INIT_FLAG_* variety.
79 virtual nsresult
Init(const char* aMimeType
,
82 virtual already_AddRefed
<ProgressTracker
> GetProgressTracker() = 0;
83 virtual void SetProgressTracker(ProgressTracker
* aProgressTracker
) {}
86 * The size, in bytes, occupied by the compressed source data of the image.
87 * If MallocSizeOf does not work on this platform, uses a fallback approach to
88 * ensure that something reasonable is always returned.
90 virtual size_t SizeOfSourceWithComputedFallback(
91 MallocSizeOf aMallocSizeOf
) const = 0;
94 * The size, in bytes, occupied by the image's decoded data.
96 virtual size_t SizeOfDecoded(gfxMemoryLocation aLocation
,
97 MallocSizeOf aMallocSizeOf
) const = 0;
99 virtual void IncrementAnimationConsumers() = 0;
100 virtual void DecrementAnimationConsumers() = 0;
102 virtual uint32_t GetAnimationConsumers() = 0;
106 * Called from OnDataAvailable when the stream associated with the image has
107 * received new image data. The arguments are the same as OnDataAvailable's,
108 * but by separating this functionality into a different method we don't
109 * interfere with subclasses which wish to implement nsIStreamListener.
111 * Images should not do anything that could send out notifications until they
112 * have received their first OnImageDataAvailable notification; in
113 * particular, this means that instantiating decoders should be deferred
114 * until OnImageDataAvailable is called.
116 virtual nsresult
OnImageDataAvailable(nsIRequest
* aRequest
,
117 nsISupports
* aContext
,
118 nsIInputStream
* aInStr
,
119 uint64_t aSourceOffset
,
120 uint32_t aCount
) = 0;
123 * Called from OnStopRequest when the image's underlying request completes.
125 * @param aRequest The completed request.
126 * @param aContext Context from Necko's OnStopRequest.
127 * @param aStatus A success or failure code.
128 * @param aLastPart Whether this is the final part of the underlying request.
130 virtual nsresult
OnImageDataComplete(nsIRequest
* aRequest
,
131 nsISupports
* aContext
,
136 * Called when the SurfaceCache discards a persistent surface belonging to
139 virtual void OnSurfaceDiscarded() = 0;
141 virtual void SetInnerWindowID(uint64_t aInnerWindowId
) = 0;
142 virtual uint64_t InnerWindowID() const = 0;
144 virtual bool HasError() = 0;
145 virtual void SetHasError() = 0;
147 virtual ImageURL
* GetURI() = 0;
150 class ImageResource
: public Image
153 already_AddRefed
<ProgressTracker
> GetProgressTracker() override
155 nsRefPtr
<ProgressTracker
> progressTracker
= mProgressTracker
;
156 MOZ_ASSERT(progressTracker
);
157 return progressTracker
.forget();
160 void SetProgressTracker(
161 ProgressTracker
* aProgressTracker
) override final
163 MOZ_ASSERT(aProgressTracker
);
164 MOZ_ASSERT(!mProgressTracker
);
165 mProgressTracker
= aProgressTracker
;
168 virtual void IncrementAnimationConsumers() override
;
169 virtual void DecrementAnimationConsumers() override
;
171 virtual uint32_t GetAnimationConsumers() override
173 return mAnimationConsumers
;
177 virtual void OnSurfaceDiscarded() override
{ }
179 virtual void SetInnerWindowID(uint64_t aInnerWindowId
) override
181 mInnerWindowId
= aInnerWindowId
;
183 virtual uint64_t InnerWindowID() const override
{ return mInnerWindowId
; }
185 virtual bool HasError() override
{ return mError
; }
186 virtual void SetHasError() override
{ mError
= true; }
189 * Returns a non-AddRefed pointer to the URI associated with this image.
190 * Illegal to use off-main-thread.
192 virtual ImageURL
* GetURI() override
{ return mURI
.get(); }
195 explicit ImageResource(ImageURL
* aURI
);
197 // Shared functionality for implementors of imgIContainer. Every
198 // implementation of attribute animationMode should forward here.
199 nsresult
GetAnimationModeInternal(uint16_t* aAnimationMode
);
200 nsresult
SetAnimationModeInternal(uint16_t aAnimationMode
);
203 * Helper for RequestRefresh.
205 * If we've had a "recent" refresh (i.e. if this image is being used in
206 * multiple documents & some other document *just* called RequestRefresh() on
207 * this image with a timestamp close to aTime), this method returns true.
209 * Otherwise, this method updates mLastRefreshTime to aTime & returns false.
211 bool HadRecentRefresh(const TimeStamp
& aTime
);
214 * Decides whether animation should or should not be happening,
215 * and makes sure the right thing is being done.
217 virtual void EvaluateAnimation();
220 * Extended by child classes, if they have additional
221 * conditions for being able to animate.
223 virtual bool ShouldAnimate() {
224 return mAnimationConsumers
> 0 && mAnimationMode
!= kDontAnimMode
;
227 virtual nsresult
StartAnimation() = 0;
228 virtual nsresult
StopAnimation() = 0;
230 // Member data shared by all implementations of this abstract class
231 nsRefPtr
<ProgressTracker
> mProgressTracker
;
232 nsRefPtr
<ImageURL
> mURI
;
233 TimeStamp mLastRefreshTime
;
234 uint64_t mInnerWindowId
;
235 uint32_t mAnimationConsumers
;
236 uint16_t mAnimationMode
; // Enum values in imgIContainer
237 bool mInitialized
:1; // Have we been initalized?
238 bool mAnimating
:1; // Are we currently animating?
239 bool mError
:1; // Error handling
243 } // namespace mozilla
245 #endif // MOZILLA_IMAGELIB_IMAGE_H_