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 "DynamicImage.h"
7 #include "gfxContext.h"
8 #include "gfxPlatform.h"
10 #include "mozilla/gfx/2D.h"
11 #include "mozilla/gfx/Logging.h"
12 #include "mozilla/RefPtr.h"
13 #include "mozilla/SVGImageContext.h"
14 #include "ImageRegion.h"
15 #include "Orientation.h"
16 #include "mozilla/image/Resolution.h"
18 #include "mozilla/MemoryReporting.h"
20 using namespace mozilla
;
21 using namespace mozilla::gfx
;
23 namespace mozilla::image
{
25 // Inherited methods from Image.
27 already_AddRefed
<ProgressTracker
> DynamicImage::GetProgressTracker() {
31 size_t DynamicImage::SizeOfSourceWithComputedFallback(
32 SizeOfState
& aState
) const {
36 void DynamicImage::CollectSizeOfSurfaces(
37 nsTArray
<SurfaceMemoryCounter
>& aCounters
,
38 MallocSizeOf aMallocSizeOf
) const {
39 // We can't report anything useful because gfxDrawable doesn't expose this
43 void DynamicImage::IncrementAnimationConsumers() {}
45 void DynamicImage::DecrementAnimationConsumers() {}
48 uint32_t DynamicImage::GetAnimationConsumers() { return 0; }
51 nsresult
DynamicImage::OnImageDataAvailable(nsIRequest
* aRequest
,
52 nsIInputStream
* aInStr
,
53 uint64_t aSourceOffset
,
58 nsresult
DynamicImage::OnImageDataComplete(nsIRequest
* aRequest
,
59 nsresult aStatus
, bool aLastPart
) {
63 void DynamicImage::OnSurfaceDiscarded(const SurfaceKey
& aSurfaceKey
) {}
65 void DynamicImage::SetInnerWindowID(uint64_t aInnerWindowId
) {}
67 uint64_t DynamicImage::InnerWindowID() const { return 0; }
69 bool DynamicImage::HasError() { return !mDrawable
; }
71 void DynamicImage::SetHasError() {}
73 nsIURI
* DynamicImage::GetURI() const { return nullptr; }
75 // Methods inherited from XPCOM interfaces.
77 NS_IMPL_ISUPPORTS(DynamicImage
, imgIContainer
)
80 DynamicImage::GetWidth(int32_t* aWidth
) {
81 *aWidth
= mDrawable
->Size().width
;
86 DynamicImage::GetHeight(int32_t* aHeight
) {
87 *aHeight
= mDrawable
->Size().height
;
91 void DynamicImage::MediaFeatureValuesChangedAllDocuments(
92 const mozilla::MediaFeatureChange
& aChange
) {}
94 nsresult
DynamicImage::GetNativeSizes(nsTArray
<IntSize
>&) {
95 return NS_ERROR_NOT_IMPLEMENTED
;
98 size_t DynamicImage::GetNativeSizesLength() { return 0; }
101 DynamicImage::GetIntrinsicSize(nsSize
* aSize
) {
102 IntSize
intSize(mDrawable
->Size());
103 *aSize
= nsSize(intSize
.width
, intSize
.height
);
107 Maybe
<AspectRatio
> DynamicImage::GetIntrinsicRatio() {
108 auto size
= mDrawable
->Size();
109 return Some(AspectRatio::FromSize(size
.width
, size
.height
));
112 NS_IMETHODIMP_(Orientation
)
113 DynamicImage::GetOrientation() { return Orientation(); }
115 NS_IMETHODIMP_(Resolution
)
116 DynamicImage::GetResolution() { return {}; }
119 DynamicImage::GetType(uint16_t* aType
) {
120 *aType
= imgIContainer::TYPE_RASTER
;
125 DynamicImage::GetProviderId(uint32_t* aId
) {
131 DynamicImage::GetAnimated(bool* aAnimated
) {
136 NS_IMETHODIMP_(already_AddRefed
<SourceSurface
>)
137 DynamicImage::GetFrame(uint32_t aWhichFrame
, uint32_t aFlags
) {
138 IntSize
size(mDrawable
->Size());
139 return GetFrameAtSize(IntSize(size
.width
, size
.height
), aWhichFrame
, aFlags
);
142 NS_IMETHODIMP_(already_AddRefed
<SourceSurface
>)
143 DynamicImage::GetFrameAtSize(const IntSize
& aSize
, uint32_t aWhichFrame
,
145 RefPtr
<DrawTarget
> dt
=
146 gfxPlatform::GetPlatform()->CreateOffscreenContentDrawTarget(
147 aSize
, SurfaceFormat::OS_RGBA
);
148 if (!dt
|| !dt
->IsValid()) {
150 << "DynamicImage::GetFrame failed in CreateOffscreenContentDrawTarget";
153 RefPtr
<gfxContext
> context
= gfxContext::CreateOrNull(dt
);
154 MOZ_ASSERT(context
); // already checked the draw target above
156 auto result
= Draw(context
, aSize
, ImageRegion::Create(aSize
), aWhichFrame
,
157 SamplingFilter::POINT
, SVGImageContext(), aFlags
, 1.0);
159 return result
== ImgDrawResult::SUCCESS
? dt
->Snapshot() : nullptr;
163 DynamicImage::WillDrawOpaqueNow() { return false; }
166 DynamicImage::IsImageContainerAvailable(WindowRenderer
* aRenderer
,
171 NS_IMETHODIMP_(ImgDrawResult
)
172 DynamicImage::GetImageProvider(WindowRenderer
* aRenderer
,
173 const gfx::IntSize
& aSize
,
174 const SVGImageContext
& aSVGContext
,
175 const Maybe
<ImageIntRegion
>& aRegion
,
177 WebRenderImageProvider
** aProvider
) {
178 return ImgDrawResult::NOT_SUPPORTED
;
181 NS_IMETHODIMP_(ImgDrawResult
)
182 DynamicImage::Draw(gfxContext
* aContext
, const nsIntSize
& aSize
,
183 const ImageRegion
& aRegion
, uint32_t aWhichFrame
,
184 SamplingFilter aSamplingFilter
,
185 const SVGImageContext
& aSVGContext
, uint32_t aFlags
,
187 MOZ_ASSERT(!aSize
.IsEmpty(), "Unexpected empty size");
189 IntSize
drawableSize(mDrawable
->Size());
191 if (aSize
== drawableSize
) {
192 gfxUtils::DrawPixelSnapped(aContext
, mDrawable
, SizeDouble(drawableSize
),
193 aRegion
, SurfaceFormat::OS_RGBA
, aSamplingFilter
,
195 return ImgDrawResult::SUCCESS
;
198 MatrixScalesDouble
scale(double(aSize
.width
) / drawableSize
.width
,
199 double(aSize
.height
) / drawableSize
.height
);
201 ImageRegion
region(aRegion
);
202 region
.Scale(1.0 / scale
.xScale
, 1.0 / scale
.yScale
);
204 gfxContextMatrixAutoSaveRestore
saveMatrix(aContext
);
205 aContext
->Multiply(gfxMatrix::Scaling(scale
));
207 gfxUtils::DrawPixelSnapped(aContext
, mDrawable
, SizeDouble(drawableSize
),
208 region
, SurfaceFormat::OS_RGBA
, aSamplingFilter
,
210 return ImgDrawResult::SUCCESS
;
214 DynamicImage::StartDecoding(uint32_t aFlags
, uint32_t aWhichFrame
) {
218 bool DynamicImage::StartDecodingWithResult(uint32_t aFlags
,
219 uint32_t aWhichFrame
) {
223 bool DynamicImage::HasDecodedPixels() { return true; }
225 imgIContainer::DecodeResult
DynamicImage::RequestDecodeWithResult(
226 uint32_t aFlags
, uint32_t aWhichFrame
) {
227 return imgIContainer::DECODE_SURFACE_AVAILABLE
;
231 DynamicImage::RequestDecodeForSize(const nsIntSize
& aSize
, uint32_t aFlags
,
232 uint32_t aWhichFrame
) {
237 DynamicImage::LockImage() { return NS_OK
; }
240 DynamicImage::UnlockImage() { return NS_OK
; }
243 DynamicImage::RequestDiscard() { return NS_OK
; }
246 DynamicImage::RequestRefresh(const mozilla::TimeStamp
& aTime
) {}
249 DynamicImage::GetAnimationMode(uint16_t* aAnimationMode
) {
250 *aAnimationMode
= kNormalAnimMode
;
255 DynamicImage::SetAnimationMode(uint16_t aAnimationMode
) { return NS_OK
; }
258 DynamicImage::ResetAnimation() { return NS_OK
; }
260 NS_IMETHODIMP_(float)
261 DynamicImage::GetFrameIndex(uint32_t aWhichFrame
) { return 0; }
263 NS_IMETHODIMP_(int32_t)
264 DynamicImage::GetFirstFrameDelay() { return 0; }
267 DynamicImage::SetAnimationStartTime(const mozilla::TimeStamp
& aTime
) {}
269 nsIntSize
DynamicImage::OptimalImageSizeForDest(const gfxSize
& aDest
,
270 uint32_t aWhichFrame
,
271 SamplingFilter aSamplingFilter
,
273 IntSize
size(mDrawable
->Size());
274 return nsIntSize(size
.width
, size
.height
);
277 NS_IMETHODIMP_(nsIntRect
)
278 DynamicImage::GetImageSpaceInvalidationRect(const nsIntRect
& aRect
) {
282 already_AddRefed
<imgIContainer
> DynamicImage::Unwrap() {
283 nsCOMPtr
<imgIContainer
> self(this);
284 return self
.forget();
287 void DynamicImage::PropagateUseCounters(dom::Document
*) {
291 nsresult
DynamicImage::GetHotspotX(int32_t* aX
) {
292 return Image::GetHotspotX(aX
);
295 nsresult
DynamicImage::GetHotspotY(int32_t* aY
) {
296 return Image::GetHotspotY(aY
);
299 } // namespace mozilla::image