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 gfxContext
context(dt
);
155 auto result
= Draw(&context
, aSize
, ImageRegion::Create(aSize
), aWhichFrame
,
156 SamplingFilter::POINT
, SVGImageContext(), aFlags
, 1.0);
158 return result
== ImgDrawResult::SUCCESS
? dt
->Snapshot() : nullptr;
162 DynamicImage::WillDrawOpaqueNow() { return false; }
165 DynamicImage::IsImageContainerAvailable(WindowRenderer
* aRenderer
,
170 NS_IMETHODIMP_(ImgDrawResult
)
171 DynamicImage::GetImageProvider(WindowRenderer
* aRenderer
,
172 const gfx::IntSize
& aSize
,
173 const SVGImageContext
& aSVGContext
,
174 const Maybe
<ImageIntRegion
>& aRegion
,
176 WebRenderImageProvider
** aProvider
) {
177 return ImgDrawResult::NOT_SUPPORTED
;
180 NS_IMETHODIMP_(ImgDrawResult
)
181 DynamicImage::Draw(gfxContext
* aContext
, const nsIntSize
& aSize
,
182 const ImageRegion
& aRegion
, uint32_t aWhichFrame
,
183 SamplingFilter aSamplingFilter
,
184 const SVGImageContext
& aSVGContext
, uint32_t aFlags
,
186 MOZ_ASSERT(!aSize
.IsEmpty(), "Unexpected empty size");
188 IntSize
drawableSize(mDrawable
->Size());
190 if (aSize
== drawableSize
) {
191 gfxUtils::DrawPixelSnapped(aContext
, mDrawable
, SizeDouble(drawableSize
),
192 aRegion
, SurfaceFormat::OS_RGBA
, aSamplingFilter
,
194 return ImgDrawResult::SUCCESS
;
197 MatrixScalesDouble
scale(double(aSize
.width
) / drawableSize
.width
,
198 double(aSize
.height
) / drawableSize
.height
);
200 ImageRegion
region(aRegion
);
201 region
.Scale(1.0 / scale
.xScale
, 1.0 / scale
.yScale
);
203 gfxContextMatrixAutoSaveRestore
saveMatrix(aContext
);
204 aContext
->Multiply(gfxMatrix::Scaling(scale
));
206 gfxUtils::DrawPixelSnapped(aContext
, mDrawable
, SizeDouble(drawableSize
),
207 region
, SurfaceFormat::OS_RGBA
, aSamplingFilter
,
209 return ImgDrawResult::SUCCESS
;
213 DynamicImage::StartDecoding(uint32_t aFlags
, uint32_t aWhichFrame
) {
217 bool DynamicImage::StartDecodingWithResult(uint32_t aFlags
,
218 uint32_t aWhichFrame
) {
222 bool DynamicImage::HasDecodedPixels() { return true; }
224 imgIContainer::DecodeResult
DynamicImage::RequestDecodeWithResult(
225 uint32_t aFlags
, uint32_t aWhichFrame
) {
226 return imgIContainer::DECODE_SURFACE_AVAILABLE
;
230 DynamicImage::RequestDecodeForSize(const nsIntSize
& aSize
, uint32_t aFlags
,
231 uint32_t aWhichFrame
) {
236 DynamicImage::LockImage() { return NS_OK
; }
239 DynamicImage::UnlockImage() { return NS_OK
; }
242 DynamicImage::RequestDiscard() { return NS_OK
; }
245 DynamicImage::RequestRefresh(const mozilla::TimeStamp
& aTime
) {}
248 DynamicImage::GetAnimationMode(uint16_t* aAnimationMode
) {
249 *aAnimationMode
= kNormalAnimMode
;
254 DynamicImage::SetAnimationMode(uint16_t aAnimationMode
) { return NS_OK
; }
257 DynamicImage::ResetAnimation() { return NS_OK
; }
259 NS_IMETHODIMP_(float)
260 DynamicImage::GetFrameIndex(uint32_t aWhichFrame
) { return 0; }
262 NS_IMETHODIMP_(int32_t)
263 DynamicImage::GetFirstFrameDelay() { return 0; }
266 DynamicImage::SetAnimationStartTime(const mozilla::TimeStamp
& aTime
) {}
268 nsIntSize
DynamicImage::OptimalImageSizeForDest(const gfxSize
& aDest
,
269 uint32_t aWhichFrame
,
270 SamplingFilter aSamplingFilter
,
272 IntSize
size(mDrawable
->Size());
273 return nsIntSize(size
.width
, size
.height
);
276 NS_IMETHODIMP_(nsIntRect
)
277 DynamicImage::GetImageSpaceInvalidationRect(const nsIntRect
& aRect
) {
281 already_AddRefed
<imgIContainer
> DynamicImage::Unwrap() {
282 nsCOMPtr
<imgIContainer
> self(this);
283 return self
.forget();
286 void DynamicImage::PropagateUseCounters(dom::Document
*) {
290 nsresult
DynamicImage::GetHotspotX(int32_t* aX
) {
291 return Image::GetHotspotX(aX
);
294 nsresult
DynamicImage::GetHotspotY(int32_t* aY
) {
295 return Image::GetHotspotY(aY
);
298 } // namespace mozilla::image