Bug 1850904 - Expand media.allow-audio-non-utility to early beta r=alwu
[gecko.git] / image / DynamicImage.cpp
blob923e91d352582e411f522977fec1b6ce2604f947
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"
9 #include "gfxUtils.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() {
28 return nullptr;
31 size_t DynamicImage::SizeOfSourceWithComputedFallback(
32 SizeOfState& aState) const {
33 return 0;
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
40 // information.
43 void DynamicImage::IncrementAnimationConsumers() {}
45 void DynamicImage::DecrementAnimationConsumers() {}
47 #ifdef DEBUG
48 uint32_t DynamicImage::GetAnimationConsumers() { return 0; }
49 #endif
51 nsresult DynamicImage::OnImageDataAvailable(nsIRequest* aRequest,
52 nsIInputStream* aInStr,
53 uint64_t aSourceOffset,
54 uint32_t aCount) {
55 return NS_OK;
58 nsresult DynamicImage::OnImageDataComplete(nsIRequest* aRequest,
59 nsresult aStatus, bool aLastPart) {
60 return NS_OK;
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)
79 NS_IMETHODIMP
80 DynamicImage::GetWidth(int32_t* aWidth) {
81 *aWidth = mDrawable->Size().width;
82 return NS_OK;
85 NS_IMETHODIMP
86 DynamicImage::GetHeight(int32_t* aHeight) {
87 *aHeight = mDrawable->Size().height;
88 return NS_OK;
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; }
100 NS_IMETHODIMP
101 DynamicImage::GetIntrinsicSize(nsSize* aSize) {
102 IntSize intSize(mDrawable->Size());
103 *aSize = nsSize(intSize.width, intSize.height);
104 return NS_OK;
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 {}; }
118 NS_IMETHODIMP
119 DynamicImage::GetType(uint16_t* aType) {
120 *aType = imgIContainer::TYPE_RASTER;
121 return NS_OK;
124 NS_IMETHODIMP
125 DynamicImage::GetProviderId(uint32_t* aId) {
126 *aId = 0;
127 return NS_OK;
130 NS_IMETHODIMP
131 DynamicImage::GetAnimated(bool* aAnimated) {
132 *aAnimated = false;
133 return NS_OK;
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,
144 uint32_t aFlags) {
145 RefPtr<DrawTarget> dt =
146 gfxPlatform::GetPlatform()->CreateOffscreenContentDrawTarget(
147 aSize, SurfaceFormat::OS_RGBA);
148 if (!dt || !dt->IsValid()) {
149 gfxWarning()
150 << "DynamicImage::GetFrame failed in CreateOffscreenContentDrawTarget";
151 return nullptr;
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;
161 NS_IMETHODIMP_(bool)
162 DynamicImage::WillDrawOpaqueNow() { return false; }
164 NS_IMETHODIMP_(bool)
165 DynamicImage::IsImageContainerAvailable(WindowRenderer* aRenderer,
166 uint32_t aFlags) {
167 return false;
170 NS_IMETHODIMP_(ImgDrawResult)
171 DynamicImage::GetImageProvider(WindowRenderer* aRenderer,
172 const gfx::IntSize& aSize,
173 const SVGImageContext& aSVGContext,
174 const Maybe<ImageIntRegion>& aRegion,
175 uint32_t aFlags,
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,
185 float aOpacity) {
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,
193 aOpacity);
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,
208 aOpacity);
209 return ImgDrawResult::SUCCESS;
212 NS_IMETHODIMP
213 DynamicImage::StartDecoding(uint32_t aFlags, uint32_t aWhichFrame) {
214 return NS_OK;
217 bool DynamicImage::StartDecodingWithResult(uint32_t aFlags,
218 uint32_t aWhichFrame) {
219 return true;
222 bool DynamicImage::HasDecodedPixels() { return true; }
224 imgIContainer::DecodeResult DynamicImage::RequestDecodeWithResult(
225 uint32_t aFlags, uint32_t aWhichFrame) {
226 return imgIContainer::DECODE_SURFACE_AVAILABLE;
229 NS_IMETHODIMP
230 DynamicImage::RequestDecodeForSize(const nsIntSize& aSize, uint32_t aFlags,
231 uint32_t aWhichFrame) {
232 return NS_OK;
235 NS_IMETHODIMP
236 DynamicImage::LockImage() { return NS_OK; }
238 NS_IMETHODIMP
239 DynamicImage::UnlockImage() { return NS_OK; }
241 NS_IMETHODIMP
242 DynamicImage::RequestDiscard() { return NS_OK; }
244 NS_IMETHODIMP_(void)
245 DynamicImage::RequestRefresh(const mozilla::TimeStamp& aTime) {}
247 NS_IMETHODIMP
248 DynamicImage::GetAnimationMode(uint16_t* aAnimationMode) {
249 *aAnimationMode = kNormalAnimMode;
250 return NS_OK;
253 NS_IMETHODIMP
254 DynamicImage::SetAnimationMode(uint16_t aAnimationMode) { return NS_OK; }
256 NS_IMETHODIMP
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; }
265 NS_IMETHODIMP_(void)
266 DynamicImage::SetAnimationStartTime(const mozilla::TimeStamp& aTime) {}
268 nsIntSize DynamicImage::OptimalImageSizeForDest(const gfxSize& aDest,
269 uint32_t aWhichFrame,
270 SamplingFilter aSamplingFilter,
271 uint32_t aFlags) {
272 IntSize size(mDrawable->Size());
273 return nsIntSize(size.width, size.height);
276 NS_IMETHODIMP_(nsIntRect)
277 DynamicImage::GetImageSpaceInvalidationRect(const nsIntRect& aRect) {
278 return aRect;
281 already_AddRefed<imgIContainer> DynamicImage::Unwrap() {
282 nsCOMPtr<imgIContainer> self(this);
283 return self.forget();
286 void DynamicImage::PropagateUseCounters(dom::Document*) {
287 // No use counters.
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