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/. */
5 
6 #include "ClippedImage.h"
7 
8 #include <algorithm>
9 #include <new>  // Workaround for bug in VS10; see bug 981264.
10 #include <cmath>
11 #include <utility>
12 
13 #include "gfxDrawable.h"
14 #include "gfxPlatform.h"
15 #include "gfxUtils.h"
16 #include "mozilla/gfx/2D.h"
17 #include "mozilla/Move.h"
18 #include "mozilla/RefPtr.h"
19 #include "mozilla/Pair.h"
20 #include "mozilla/Tuple.h"
21 
22 #include "ImageRegion.h"
23 #include "Orientation.h"
24 #include "SVGImageContext.h"
25 
26 namespace mozilla {
27 
28 using namespace gfx;
29 using layers::ImageContainer;
30 using layers::LayerManager;
31 using std::make_pair;
32 using std::max;
33 using std::modf;
34 using std::pair;
35 
36 namespace image {
37 
38 class ClippedImageCachedSurface {
39  public:
ClippedImageCachedSurface(already_AddRefed<SourceSurface> aSurface,const nsIntSize & aSize,const Maybe<SVGImageContext> & aSVGContext,float aFrame,uint32_t aFlags,ImgDrawResult aDrawResult)40   ClippedImageCachedSurface(already_AddRefed<SourceSurface> aSurface,
41                             const nsIntSize& aSize,
42                             const Maybe<SVGImageContext>& aSVGContext,
43                             float aFrame, uint32_t aFlags,
44                             ImgDrawResult aDrawResult)
45       : mSurface(aSurface),
46         mSize(aSize),
47         mSVGContext(aSVGContext),
48         mFrame(aFrame),
49         mFlags(aFlags),
50         mDrawResult(aDrawResult) {
51     MOZ_ASSERT(mSurface, "Must have a valid surface");
52   }
53 
Matches(const nsIntSize & aSize,const Maybe<SVGImageContext> & aSVGContext,float aFrame,uint32_t aFlags) const54   bool Matches(const nsIntSize& aSize,
55                const Maybe<SVGImageContext>& aSVGContext, float aFrame,
56                uint32_t aFlags) const {
57     return mSize == aSize && mSVGContext == aSVGContext && mFrame == aFrame &&
58            mFlags == aFlags;
59   }
60 
Surface() const61   already_AddRefed<SourceSurface> Surface() const {
62     RefPtr<SourceSurface> surf(mSurface);
63     return surf.forget();
64   }
65 
GetDrawResult() const66   ImgDrawResult GetDrawResult() const { return mDrawResult; }
67 
NeedsRedraw() const68   bool NeedsRedraw() const {
69     return mDrawResult != ImgDrawResult::SUCCESS &&
70            mDrawResult != ImgDrawResult::BAD_IMAGE;
71   }
72 
73  private:
74   RefPtr<SourceSurface> mSurface;
75   const nsIntSize mSize;
76   Maybe<SVGImageContext> mSVGContext;
77   const float mFrame;
78   const uint32_t mFlags;
79   const ImgDrawResult mDrawResult;
80 };
81 
82 class DrawSingleTileCallback : public gfxDrawingCallback {
83  public:
DrawSingleTileCallback(ClippedImage * aImage,const nsIntSize & aSize,const Maybe<SVGImageContext> & aSVGContext,uint32_t aWhichFrame,uint32_t aFlags,float aOpacity)84   DrawSingleTileCallback(ClippedImage* aImage, const nsIntSize& aSize,
85                          const Maybe<SVGImageContext>& aSVGContext,
86                          uint32_t aWhichFrame, uint32_t aFlags, float aOpacity)
87       : mImage(aImage),
88         mSize(aSize),
89         mSVGContext(aSVGContext),
90         mWhichFrame(aWhichFrame),
91         mFlags(aFlags),
92         mDrawResult(ImgDrawResult::NOT_READY),
93         mOpacity(aOpacity) {
94     MOZ_ASSERT(mImage, "Must have an image to clip");
95   }
96 
operator ()(gfxContext * aContext,const gfxRect & aFillRect,const SamplingFilter aSamplingFilter,const gfxMatrix & aTransform)97   virtual bool operator()(gfxContext* aContext, const gfxRect& aFillRect,
98                           const SamplingFilter aSamplingFilter,
99                           const gfxMatrix& aTransform) override {
100     MOZ_ASSERT(aTransform.IsIdentity(),
101                "Caller is probably CreateSamplingRestrictedDrawable, "
102                "which should not happen");
103 
104     // Draw the image. |gfxCallbackDrawable| always calls this function with
105     // arguments that guarantee we never tile.
106     mDrawResult = mImage->DrawSingleTile(
107         aContext, mSize, ImageRegion::Create(aFillRect), mWhichFrame,
108         aSamplingFilter, mSVGContext, mFlags, mOpacity);
109 
110     return true;
111   }
112 
GetDrawResult()113   ImgDrawResult GetDrawResult() { return mDrawResult; }
114 
115  private:
116   RefPtr<ClippedImage> mImage;
117   const nsIntSize mSize;
118   const Maybe<SVGImageContext>& mSVGContext;
119   const uint32_t mWhichFrame;
120   const uint32_t mFlags;
121   ImgDrawResult mDrawResult;
122   float mOpacity;
123 };
124 
ClippedImage(Image * aImage,nsIntRect aClip,const Maybe<nsSize> & aSVGViewportSize)125 ClippedImage::ClippedImage(Image* aImage, nsIntRect aClip,
126                            const Maybe<nsSize>& aSVGViewportSize)
127     : ImageWrapper(aImage), mClip(aClip) {
128   MOZ_ASSERT(aImage != nullptr, "ClippedImage requires an existing Image");
129   MOZ_ASSERT_IF(aSVGViewportSize,
130                 aImage->GetType() == imgIContainer::TYPE_VECTOR);
131   if (aSVGViewportSize) {
132     mSVGViewportSize = Some(aSVGViewportSize->ToNearestPixels(
133         nsPresContext::AppUnitsPerCSSPixel()));
134   }
135 }
136 
~ClippedImage()137 ClippedImage::~ClippedImage() {}
138 
ShouldClip()139 bool ClippedImage::ShouldClip() {
140   // We need to evaluate the clipping region against the image's width and
141   // height once they're available to determine if it's valid and whether we
142   // actually need to do any work. We may fail if the image's width and height
143   // aren't available yet, in which case we'll try again later.
144   if (mShouldClip.isNothing()) {
145     int32_t width, height;
146     RefPtr<ProgressTracker> progressTracker =
147         InnerImage()->GetProgressTracker();
148     if (InnerImage()->HasError()) {
149       // If there's a problem with the inner image we'll let it handle
150       // everything.
151       mShouldClip.emplace(false);
152     } else if (mSVGViewportSize && !mSVGViewportSize->IsEmpty()) {
153       // Clamp the clipping region to the size of the SVG viewport.
154       nsIntRect svgViewportRect(nsIntPoint(0, 0), *mSVGViewportSize);
155 
156       mClip = mClip.Intersect(svgViewportRect);
157 
158       // If the clipping region is the same size as the SVG viewport size
159       // we don't have to do anything.
160       mShouldClip.emplace(!mClip.IsEqualInterior(svgViewportRect));
161     } else if (NS_SUCCEEDED(InnerImage()->GetWidth(&width)) && width > 0 &&
162                NS_SUCCEEDED(InnerImage()->GetHeight(&height)) && height > 0) {
163       // Clamp the clipping region to the size of the underlying image.
164       mClip = mClip.Intersect(nsIntRect(0, 0, width, height));
165 
166       // If the clipping region is the same size as the underlying image we
167       // don't have to do anything.
168       mShouldClip.emplace(
169           !mClip.IsEqualInterior(nsIntRect(0, 0, width, height)));
170     } else if (progressTracker &&
171                !(progressTracker->GetProgress() & FLAG_LOAD_COMPLETE)) {
172       // The image just hasn't finished loading yet. We don't yet know whether
173       // clipping with be needed or not for now. Just return without memorizing
174       // anything.
175       return false;
176     } else {
177       // We have a fully loaded image without a clearly defined width and
178       // height. This can happen with SVG images.
179       mShouldClip.emplace(false);
180     }
181   }
182 
183   MOZ_ASSERT(mShouldClip.isSome(), "Should have computed a result");
184   return *mShouldClip;
185 }
186 
187 NS_IMETHODIMP
GetWidth(int32_t * aWidth)188 ClippedImage::GetWidth(int32_t* aWidth) {
189   if (!ShouldClip()) {
190     return InnerImage()->GetWidth(aWidth);
191   }
192 
193   *aWidth = mClip.Width();
194   return NS_OK;
195 }
196 
197 NS_IMETHODIMP
GetHeight(int32_t * aHeight)198 ClippedImage::GetHeight(int32_t* aHeight) {
199   if (!ShouldClip()) {
200     return InnerImage()->GetHeight(aHeight);
201   }
202 
203   *aHeight = mClip.Height();
204   return NS_OK;
205 }
206 
207 NS_IMETHODIMP
GetIntrinsicSize(nsSize * aSize)208 ClippedImage::GetIntrinsicSize(nsSize* aSize) {
209   if (!ShouldClip()) {
210     return InnerImage()->GetIntrinsicSize(aSize);
211   }
212 
213   *aSize = nsSize(mClip.Width(), mClip.Height());
214   return NS_OK;
215 }
216 
217 NS_IMETHODIMP
GetIntrinsicRatio(nsSize * aRatio)218 ClippedImage::GetIntrinsicRatio(nsSize* aRatio) {
219   if (!ShouldClip()) {
220     return InnerImage()->GetIntrinsicRatio(aRatio);
221   }
222 
223   *aRatio = nsSize(mClip.Width(), mClip.Height());
224   return NS_OK;
225 }
226 
NS_IMETHODIMP_(already_AddRefed<SourceSurface>)227 NS_IMETHODIMP_(already_AddRefed<SourceSurface>)
228 ClippedImage::GetFrame(uint32_t aWhichFrame, uint32_t aFlags) {
229   ImgDrawResult result;
230   RefPtr<SourceSurface> surface;
231   Tie(result, surface) =
232       GetFrameInternal(mClip.Size(), Nothing(), aWhichFrame, aFlags, 1.0);
233   return surface.forget();
234 }
235 
NS_IMETHODIMP_(already_AddRefed<SourceSurface>)236 NS_IMETHODIMP_(already_AddRefed<SourceSurface>)
237 ClippedImage::GetFrameAtSize(const IntSize& aSize, uint32_t aWhichFrame,
238                              uint32_t aFlags) {
239   // XXX(seth): It'd be nice to support downscale-during-decode for this case,
240   // but right now we just fall back to the intrinsic size.
241   return GetFrame(aWhichFrame, aFlags);
242 }
243 
GetFrameInternal(const nsIntSize & aSize,const Maybe<SVGImageContext> & aSVGContext,uint32_t aWhichFrame,uint32_t aFlags,float aOpacity)244 Pair<ImgDrawResult, RefPtr<SourceSurface>> ClippedImage::GetFrameInternal(
245     const nsIntSize& aSize, const Maybe<SVGImageContext>& aSVGContext,
246     uint32_t aWhichFrame, uint32_t aFlags, float aOpacity) {
247   if (!ShouldClip()) {
248     RefPtr<SourceSurface> surface = InnerImage()->GetFrame(aWhichFrame, aFlags);
249     return MakePair(surface ? ImgDrawResult::SUCCESS : ImgDrawResult::NOT_READY,
250                     Move(surface));
251   }
252 
253   float frameToDraw = InnerImage()->GetFrameIndex(aWhichFrame);
254   if (!mCachedSurface ||
255       !mCachedSurface->Matches(aSize, aSVGContext, frameToDraw, aFlags) ||
256       mCachedSurface->NeedsRedraw()) {
257     // Create a surface to draw into.
258     RefPtr<DrawTarget> target =
259         gfxPlatform::GetPlatform()->CreateOffscreenContentDrawTarget(
260             IntSize(aSize.width, aSize.height), SurfaceFormat::B8G8R8A8);
261     if (!target || !target->IsValid()) {
262       NS_ERROR("Could not create a DrawTarget");
263       return MakePair(ImgDrawResult::TEMPORARY_ERROR, RefPtr<SourceSurface>());
264     }
265 
266     RefPtr<gfxContext> ctx = gfxContext::CreateOrNull(target);
267     MOZ_ASSERT(ctx);  // already checked the draw target above
268 
269     // Create our callback.
270     RefPtr<DrawSingleTileCallback> drawTileCallback =
271         new DrawSingleTileCallback(this, aSize, aSVGContext, aWhichFrame,
272                                    aFlags, aOpacity);
273     RefPtr<gfxDrawable> drawable =
274         new gfxCallbackDrawable(drawTileCallback, aSize);
275 
276     // Actually draw. The callback will end up invoking DrawSingleTile.
277     gfxUtils::DrawPixelSnapped(ctx, drawable, SizeDouble(aSize),
278                                ImageRegion::Create(aSize),
279                                SurfaceFormat::B8G8R8A8, SamplingFilter::LINEAR,
280                                imgIContainer::FLAG_CLAMP);
281 
282     // Cache the resulting surface.
283     mCachedSurface = MakeUnique<ClippedImageCachedSurface>(
284         target->Snapshot(), aSize, aSVGContext, frameToDraw, aFlags,
285         drawTileCallback->GetDrawResult());
286   }
287 
288   MOZ_ASSERT(mCachedSurface, "Should have a cached surface now");
289   RefPtr<SourceSurface> surface = mCachedSurface->Surface();
290   return MakePair(mCachedSurface->GetDrawResult(), Move(surface));
291 }
292 
NS_IMETHODIMP_(bool)293 NS_IMETHODIMP_(bool)
294 ClippedImage::IsImageContainerAvailable(LayerManager* aManager,
295                                         uint32_t aFlags) {
296   if (!ShouldClip()) {
297     return InnerImage()->IsImageContainerAvailable(aManager, aFlags);
298   }
299   return false;
300 }
301 
NS_IMETHODIMP_(already_AddRefed<ImageContainer>)302 NS_IMETHODIMP_(already_AddRefed<ImageContainer>)
303 ClippedImage::GetImageContainer(LayerManager* aManager, uint32_t aFlags) {
304   // XXX(seth): We currently don't have a way of clipping the result of
305   // GetImageContainer. We work around this by always returning null, but if it
306   // ever turns out that ClippedImage is widely used on codepaths that can
307   // actually benefit from GetImageContainer, it would be a good idea to fix
308   // that method for performance reasons.
309 
310   if (!ShouldClip()) {
311     return InnerImage()->GetImageContainer(aManager, aFlags);
312   }
313 
314   return nullptr;
315 }
316 
NS_IMETHODIMP_(bool)317 NS_IMETHODIMP_(bool)
318 ClippedImage::IsImageContainerAvailableAtSize(LayerManager* aManager,
319                                               const IntSize& aSize,
320                                               uint32_t aFlags) {
321   if (!ShouldClip()) {
322     return InnerImage()->IsImageContainerAvailableAtSize(aManager, aSize,
323                                                          aFlags);
324   }
325   return false;
326 }
327 
NS_IMETHODIMP_(already_AddRefed<ImageContainer>)328 NS_IMETHODIMP_(already_AddRefed<ImageContainer>)
329 ClippedImage::GetImageContainerAtSize(LayerManager* aManager,
330                                       const IntSize& aSize,
331                                       const Maybe<SVGImageContext>& aSVGContext,
332                                       uint32_t aFlags) {
333   // XXX(seth): We currently don't have a way of clipping the result of
334   // GetImageContainer. We work around this by always returning null, but if it
335   // ever turns out that ClippedImage is widely used on codepaths that can
336   // actually benefit from GetImageContainer, it would be a good idea to fix
337   // that method for performance reasons.
338 
339   if (!ShouldClip()) {
340     return InnerImage()->GetImageContainerAtSize(aManager, aSize, aSVGContext,
341                                                  aFlags);
342   }
343 
344   return nullptr;
345 }
346 
MustCreateSurface(gfxContext * aContext,const nsIntSize & aSize,const ImageRegion & aRegion,const uint32_t aFlags)347 static bool MustCreateSurface(gfxContext* aContext, const nsIntSize& aSize,
348                               const ImageRegion& aRegion,
349                               const uint32_t aFlags) {
350   gfxRect imageRect(0, 0, aSize.width, aSize.height);
351   bool willTile = !imageRect.Contains(aRegion.Rect()) &&
352                   !(aFlags & imgIContainer::FLAG_CLAMP);
353   bool willResample = aContext->CurrentMatrix().HasNonIntegerTranslation() &&
354                       (willTile || !aRegion.RestrictionContains(imageRect));
355   return willTile || willResample;
356 }
357 
NS_IMETHODIMP_(ImgDrawResult)358 NS_IMETHODIMP_(ImgDrawResult)
359 ClippedImage::Draw(gfxContext* aContext, const nsIntSize& aSize,
360                    const ImageRegion& aRegion, uint32_t aWhichFrame,
361                    SamplingFilter aSamplingFilter,
362                    const Maybe<SVGImageContext>& aSVGContext, uint32_t aFlags,
363                    float aOpacity) {
364   if (!ShouldClip()) {
365     return InnerImage()->Draw(aContext, aSize, aRegion, aWhichFrame,
366                               aSamplingFilter, aSVGContext, aFlags, aOpacity);
367   }
368 
369   // Check for tiling. If we need to tile then we need to create a
370   // gfxCallbackDrawable to handle drawing for us.
371   if (MustCreateSurface(aContext, aSize, aRegion, aFlags)) {
372     // Create a temporary surface containing a single tile of this image.
373     // GetFrame will call DrawSingleTile internally.
374     ImgDrawResult result;
375     RefPtr<SourceSurface> surface;
376     Tie(result, surface) =
377         GetFrameInternal(aSize, aSVGContext, aWhichFrame, aFlags, aOpacity);
378     if (!surface) {
379       MOZ_ASSERT(result != ImgDrawResult::SUCCESS);
380       return result;
381     }
382 
383     // Create a drawable from that surface.
384     RefPtr<gfxSurfaceDrawable> drawable =
385         new gfxSurfaceDrawable(surface, aSize);
386 
387     // Draw.
388     gfxUtils::DrawPixelSnapped(aContext, drawable, SizeDouble(aSize), aRegion,
389                                SurfaceFormat::B8G8R8A8, aSamplingFilter,
390                                aOpacity);
391 
392     return result;
393   }
394 
395   return DrawSingleTile(aContext, aSize, aRegion, aWhichFrame, aSamplingFilter,
396                         aSVGContext, aFlags, aOpacity);
397 }
398 
DrawSingleTile(gfxContext * aContext,const nsIntSize & aSize,const ImageRegion & aRegion,uint32_t aWhichFrame,SamplingFilter aSamplingFilter,const Maybe<SVGImageContext> & aSVGContext,uint32_t aFlags,float aOpacity)399 ImgDrawResult ClippedImage::DrawSingleTile(
400     gfxContext* aContext, const nsIntSize& aSize, const ImageRegion& aRegion,
401     uint32_t aWhichFrame, SamplingFilter aSamplingFilter,
402     const Maybe<SVGImageContext>& aSVGContext, uint32_t aFlags,
403     float aOpacity) {
404   MOZ_ASSERT(!MustCreateSurface(aContext, aSize, aRegion, aFlags),
405              "Shouldn't need to create a surface");
406 
407   gfxRect clip(mClip.X(), mClip.Y(), mClip.Width(), mClip.Height());
408   nsIntSize size(aSize), innerSize(aSize);
409   bool needScale = false;
410   if (mSVGViewportSize && !mSVGViewportSize->IsEmpty()) {
411     innerSize = *mSVGViewportSize;
412     needScale = true;
413   } else if (NS_SUCCEEDED(InnerImage()->GetWidth(&innerSize.width)) &&
414              NS_SUCCEEDED(InnerImage()->GetHeight(&innerSize.height))) {
415     needScale = true;
416   } else {
417     MOZ_ASSERT_UNREACHABLE(
418         "If ShouldClip() led us to draw then we should never get here");
419   }
420 
421   if (needScale) {
422     double scaleX = aSize.width / clip.Width();
423     double scaleY = aSize.height / clip.Height();
424 
425     // Map the clip and size to the scale requested by the caller.
426     clip.Scale(scaleX, scaleY);
427     size = innerSize;
428     size.Scale(scaleX, scaleY);
429   }
430 
431   // We restrict our drawing to only the clipping region, and translate so that
432   // the clipping region is placed at the position the caller expects.
433   ImageRegion region(aRegion);
434   region.MoveBy(clip.X(), clip.Y());
435   region = region.Intersect(clip);
436 
437   gfxContextMatrixAutoSaveRestore saveMatrix(aContext);
438   aContext->Multiply(gfxMatrix::Translation(-clip.X(), -clip.Y()));
439 
440   auto unclipViewport = [&](const SVGImageContext& aOldContext) {
441     // Map the viewport to the inner image. Note that we don't take the aSize
442     // parameter of imgIContainer::Draw into account, just the clipping region.
443     // The size in pixels at which the output will ultimately be drawn is
444     // irrelevant here since the purpose of the SVG viewport size is to
445     // determine what *region* of the SVG document will be drawn.
446     SVGImageContext context(aOldContext);
447     auto oldViewport = aOldContext.GetViewportSize();
448     if (oldViewport) {
449       CSSIntSize newViewport;
450       newViewport.width =
451           ceil(oldViewport->width * double(innerSize.width) / mClip.Width());
452       newViewport.height =
453           ceil(oldViewport->height * double(innerSize.height) / mClip.Height());
454       context.SetViewportSize(Some(newViewport));
455     }
456     return context;
457   };
458 
459   return InnerImage()->Draw(aContext, size, region, aWhichFrame,
460                             aSamplingFilter, aSVGContext.map(unclipViewport),
461                             aFlags, aOpacity);
462 }
463 
464 NS_IMETHODIMP
RequestDiscard()465 ClippedImage::RequestDiscard() {
466   // We're very aggressive about discarding.
467   mCachedSurface = nullptr;
468 
469   return InnerImage()->RequestDiscard();
470 }
471 
NS_IMETHODIMP_(Orientation)472 NS_IMETHODIMP_(Orientation)
473 ClippedImage::GetOrientation() {
474   // XXX(seth): This should not actually be here; this is just to work around a
475   // what appears to be a bug in MSVC's linker.
476   return InnerImage()->GetOrientation();
477 }
478 
OptimalImageSizeForDest(const gfxSize & aDest,uint32_t aWhichFrame,SamplingFilter aSamplingFilter,uint32_t aFlags)479 nsIntSize ClippedImage::OptimalImageSizeForDest(const gfxSize& aDest,
480                                                 uint32_t aWhichFrame,
481                                                 SamplingFilter aSamplingFilter,
482                                                 uint32_t aFlags) {
483   if (!ShouldClip()) {
484     return InnerImage()->OptimalImageSizeForDest(aDest, aWhichFrame,
485                                                  aSamplingFilter, aFlags);
486   }
487 
488   int32_t imgWidth, imgHeight;
489   bool needScale = false;
490   bool forceUniformScaling = false;
491   if (mSVGViewportSize && !mSVGViewportSize->IsEmpty()) {
492     imgWidth = mSVGViewportSize->width;
493     imgHeight = mSVGViewportSize->height;
494     needScale = true;
495     forceUniformScaling = (aFlags & imgIContainer::FLAG_FORCE_UNIFORM_SCALING);
496   } else if (NS_SUCCEEDED(InnerImage()->GetWidth(&imgWidth)) &&
497              NS_SUCCEEDED(InnerImage()->GetHeight(&imgHeight))) {
498     needScale = true;
499   }
500 
501   if (needScale) {
502     // To avoid ugly sampling artifacts, ClippedImage needs the image size to
503     // be chosen such that the clipping region lies on pixel boundaries.
504 
505     // First, we select a scale that's good for ClippedImage. An integer
506     // multiple of the size of the clipping region is always fine.
507     IntSize scale = IntSize::Ceil(aDest.width / mClip.Width(),
508                                   aDest.height / mClip.Height());
509 
510     if (forceUniformScaling) {
511       scale.width = scale.height = max(scale.height, scale.width);
512     }
513 
514     // Determine the size we'd prefer to render the inner image at, and ask the
515     // inner image what size we should actually use.
516     gfxSize desiredSize(imgWidth * scale.width, imgHeight * scale.height);
517     nsIntSize innerDesiredSize = InnerImage()->OptimalImageSizeForDest(
518         desiredSize, aWhichFrame, aSamplingFilter, aFlags);
519 
520     // To get our final result, we take the inner image's desired size and
521     // determine how large the clipped region would be at that scale. (Again, we
522     // ensure an integer multiple of the size of the clipping region.)
523     IntSize finalScale =
524         IntSize::Ceil(double(innerDesiredSize.width) / imgWidth,
525                       double(innerDesiredSize.height) / imgHeight);
526     return mClip.Size() * finalScale;
527   }
528 
529   MOZ_ASSERT(false,
530              "If ShouldClip() led us to draw then we should never get here");
531   return InnerImage()->OptimalImageSizeForDest(aDest, aWhichFrame,
532                                                aSamplingFilter, aFlags);
533 }
534 
NS_IMETHODIMP_(nsIntRect)535 NS_IMETHODIMP_(nsIntRect)
536 ClippedImage::GetImageSpaceInvalidationRect(const nsIntRect& aRect) {
537   if (!ShouldClip()) {
538     return InnerImage()->GetImageSpaceInvalidationRect(aRect);
539   }
540 
541   nsIntRect rect(InnerImage()->GetImageSpaceInvalidationRect(aRect));
542   rect = rect.Intersect(mClip);
543   rect.MoveBy(-mClip.X(), -mClip.Y());
544   return rect;
545 }
546 
547 }  // namespace image
548 }  // namespace mozilla
549