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 "ClippedImage.h"
9 #include <new> // Workaround for bug in VS10; see bug 981264.
13 #include "gfxDrawable.h"
14 #include "gfxPlatform.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"
22 #include "ImageRegion.h"
23 #include "Orientation.h"
24 #include "SVGImageContext.h"
29 using layers::ImageContainer
;
30 using layers::LayerManager
;
38 class ClippedImageCachedSurface
{
40 ClippedImageCachedSurface(already_AddRefed
<SourceSurface
> aSurface
,
41 const nsIntSize
& aSize
,
42 const Maybe
<SVGImageContext
>& aSVGContext
,
43 float aFrame
, uint32_t aFlags
,
44 ImgDrawResult aDrawResult
)
47 mSVGContext(aSVGContext
),
50 mDrawResult(aDrawResult
) {
51 MOZ_ASSERT(mSurface
, "Must have a valid surface");
54 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
&&
61 already_AddRefed
<SourceSurface
> Surface() const {
62 RefPtr
<SourceSurface
> surf(mSurface
);
66 ImgDrawResult
GetDrawResult() const { return mDrawResult
; }
68 bool NeedsRedraw() const {
69 return mDrawResult
!= ImgDrawResult::SUCCESS
&&
70 mDrawResult
!= ImgDrawResult::BAD_IMAGE
;
74 RefPtr
<SourceSurface
> mSurface
;
75 const nsIntSize mSize
;
76 Maybe
<SVGImageContext
> mSVGContext
;
78 const uint32_t mFlags
;
79 const ImgDrawResult mDrawResult
;
82 class DrawSingleTileCallback
: public gfxDrawingCallback
{
84 DrawSingleTileCallback(ClippedImage
* aImage
, const nsIntSize
& aSize
,
85 const Maybe
<SVGImageContext
>& aSVGContext
,
86 uint32_t aWhichFrame
, uint32_t aFlags
, float aOpacity
)
89 mSVGContext(aSVGContext
),
90 mWhichFrame(aWhichFrame
),
92 mDrawResult(ImgDrawResult::NOT_READY
),
94 MOZ_ASSERT(mImage
, "Must have an image to clip");
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");
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
);
113 ImgDrawResult
GetDrawResult() { return mDrawResult
; }
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
;
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
) {
133 Some(aSVGViewportSize
->ToNearestPixels(AppUnitsPerCSSPixel()));
137 ClippedImage::~ClippedImage() {}
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
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
);
156 mClip
= mClip
.Intersect(svgViewportRect
);
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
));
166 // If the clipping region is the same size as the underlying image we
167 // don't have to do anything.
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
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);
183 MOZ_ASSERT(mShouldClip
.isSome(), "Should have computed a result");
188 ClippedImage::GetWidth(int32_t* aWidth
) {
190 return InnerImage()->GetWidth(aWidth
);
193 *aWidth
= mClip
.Width();
198 ClippedImage::GetHeight(int32_t* aHeight
) {
200 return InnerImage()->GetHeight(aHeight
);
203 *aHeight
= mClip
.Height();
208 ClippedImage::GetIntrinsicSize(nsSize
* aSize
) {
210 return InnerImage()->GetIntrinsicSize(aSize
);
213 *aSize
= nsSize(mClip
.Width(), mClip
.Height());
217 Maybe
<AspectRatio
> ClippedImage::GetIntrinsicRatio() {
219 return InnerImage()->GetIntrinsicRatio();
222 return Some(AspectRatio::FromSize(mClip
.Width(), mClip
.Height()));
225 NS_IMETHODIMP_(already_AddRefed
<SourceSurface
>)
226 ClippedImage::GetFrame(uint32_t aWhichFrame
, uint32_t aFlags
) {
227 ImgDrawResult result
;
228 RefPtr
<SourceSurface
> surface
;
229 Tie(result
, surface
) =
230 GetFrameInternal(mClip
.Size(), Nothing(), aWhichFrame
, aFlags
, 1.0);
231 return surface
.forget();
234 NS_IMETHODIMP_(already_AddRefed
<SourceSurface
>)
235 ClippedImage::GetFrameAtSize(const IntSize
& aSize
, uint32_t aWhichFrame
,
237 // XXX(seth): It'd be nice to support downscale-during-decode for this case,
238 // but right now we just fall back to the intrinsic size.
239 return GetFrame(aWhichFrame
, aFlags
);
242 Pair
<ImgDrawResult
, RefPtr
<SourceSurface
>> ClippedImage::GetFrameInternal(
243 const nsIntSize
& aSize
, const Maybe
<SVGImageContext
>& aSVGContext
,
244 uint32_t aWhichFrame
, uint32_t aFlags
, float aOpacity
) {
246 RefPtr
<SourceSurface
> surface
= InnerImage()->GetFrame(aWhichFrame
, aFlags
);
247 return MakePair(surface
? ImgDrawResult::SUCCESS
: ImgDrawResult::NOT_READY
,
251 float frameToDraw
= InnerImage()->GetFrameIndex(aWhichFrame
);
252 if (!mCachedSurface
||
253 !mCachedSurface
->Matches(aSize
, aSVGContext
, frameToDraw
, aFlags
) ||
254 mCachedSurface
->NeedsRedraw()) {
255 // Create a surface to draw into.
256 RefPtr
<DrawTarget
> target
=
257 gfxPlatform::GetPlatform()->CreateOffscreenContentDrawTarget(
258 IntSize(aSize
.width
, aSize
.height
), SurfaceFormat::B8G8R8A8
);
259 if (!target
|| !target
->IsValid()) {
260 NS_ERROR("Could not create a DrawTarget");
261 return MakePair(ImgDrawResult::TEMPORARY_ERROR
, RefPtr
<SourceSurface
>());
264 RefPtr
<gfxContext
> ctx
= gfxContext::CreateOrNull(target
);
265 MOZ_ASSERT(ctx
); // already checked the draw target above
267 // Create our callback.
268 RefPtr
<DrawSingleTileCallback
> drawTileCallback
=
269 new DrawSingleTileCallback(this, aSize
, aSVGContext
, aWhichFrame
,
271 RefPtr
<gfxDrawable
> drawable
=
272 new gfxCallbackDrawable(drawTileCallback
, aSize
);
274 // Actually draw. The callback will end up invoking DrawSingleTile.
275 gfxUtils::DrawPixelSnapped(ctx
, drawable
, SizeDouble(aSize
),
276 ImageRegion::Create(aSize
),
277 SurfaceFormat::B8G8R8A8
, SamplingFilter::LINEAR
,
278 imgIContainer::FLAG_CLAMP
);
280 // Cache the resulting surface.
281 mCachedSurface
= MakeUnique
<ClippedImageCachedSurface
>(
282 target
->Snapshot(), aSize
, aSVGContext
, frameToDraw
, aFlags
,
283 drawTileCallback
->GetDrawResult());
286 MOZ_ASSERT(mCachedSurface
, "Should have a cached surface now");
287 RefPtr
<SourceSurface
> surface
= mCachedSurface
->Surface();
288 return MakePair(mCachedSurface
->GetDrawResult(), std::move(surface
));
292 ClippedImage::IsImageContainerAvailable(LayerManager
* aManager
,
295 return InnerImage()->IsImageContainerAvailable(aManager
, aFlags
);
300 NS_IMETHODIMP_(already_AddRefed
<ImageContainer
>)
301 ClippedImage::GetImageContainer(LayerManager
* aManager
, uint32_t aFlags
) {
302 // XXX(seth): We currently don't have a way of clipping the result of
303 // GetImageContainer. We work around this by always returning null, but if it
304 // ever turns out that ClippedImage is widely used on codepaths that can
305 // actually benefit from GetImageContainer, it would be a good idea to fix
306 // that method for performance reasons.
309 return InnerImage()->GetImageContainer(aManager
, aFlags
);
316 ClippedImage::IsImageContainerAvailableAtSize(LayerManager
* aManager
,
317 const IntSize
& aSize
,
320 return InnerImage()->IsImageContainerAvailableAtSize(aManager
, aSize
,
326 NS_IMETHODIMP_(ImgDrawResult
)
327 ClippedImage::GetImageContainerAtSize(layers::LayerManager
* aManager
,
328 const gfx::IntSize
& aSize
,
329 const Maybe
<SVGImageContext
>& aSVGContext
,
331 layers::ImageContainer
** aOutContainer
) {
332 // XXX(seth): We currently don't have a way of clipping the result of
333 // GetImageContainer. We work around this by always returning null, but if it
334 // ever turns out that ClippedImage is widely used on codepaths that can
335 // actually benefit from GetImageContainer, it would be a good idea to fix
336 // that method for performance reasons.
339 return InnerImage()->GetImageContainerAtSize(aManager
, aSize
, aSVGContext
,
340 aFlags
, aOutContainer
);
343 return ImgDrawResult::NOT_SUPPORTED
;
346 static bool MustCreateSurface(gfxContext
* aContext
, const nsIntSize
& aSize
,
347 const ImageRegion
& aRegion
,
348 const uint32_t aFlags
) {
349 gfxRect
imageRect(0, 0, aSize
.width
, aSize
.height
);
350 bool willTile
= !imageRect
.Contains(aRegion
.Rect()) &&
351 !(aFlags
& imgIContainer::FLAG_CLAMP
);
352 bool willResample
= aContext
->CurrentMatrix().HasNonIntegerTranslation() &&
353 (willTile
|| !aRegion
.RestrictionContains(imageRect
));
354 return willTile
|| willResample
;
357 NS_IMETHODIMP_(ImgDrawResult
)
358 ClippedImage::Draw(gfxContext
* aContext
, const nsIntSize
& aSize
,
359 const ImageRegion
& aRegion
, uint32_t aWhichFrame
,
360 SamplingFilter aSamplingFilter
,
361 const Maybe
<SVGImageContext
>& aSVGContext
, uint32_t aFlags
,
364 return InnerImage()->Draw(aContext
, aSize
, aRegion
, aWhichFrame
,
365 aSamplingFilter
, aSVGContext
, aFlags
, aOpacity
);
368 // Check for tiling. If we need to tile then we need to create a
369 // gfxCallbackDrawable to handle drawing for us.
370 if (MustCreateSurface(aContext
, aSize
, aRegion
, aFlags
)) {
371 // Create a temporary surface containing a single tile of this image.
372 // GetFrame will call DrawSingleTile internally.
373 ImgDrawResult result
;
374 RefPtr
<SourceSurface
> surface
;
375 Tie(result
, surface
) =
376 GetFrameInternal(aSize
, aSVGContext
, aWhichFrame
, aFlags
, aOpacity
);
378 MOZ_ASSERT(result
!= ImgDrawResult::SUCCESS
);
382 // Create a drawable from that surface.
383 RefPtr
<gfxSurfaceDrawable
> drawable
=
384 new gfxSurfaceDrawable(surface
, aSize
);
387 gfxUtils::DrawPixelSnapped(aContext
, drawable
, SizeDouble(aSize
), aRegion
,
388 SurfaceFormat::B8G8R8A8
, aSamplingFilter
,
394 return DrawSingleTile(aContext
, aSize
, aRegion
, aWhichFrame
, aSamplingFilter
,
395 aSVGContext
, aFlags
, aOpacity
);
398 ImgDrawResult
ClippedImage::DrawSingleTile(
399 gfxContext
* aContext
, const nsIntSize
& aSize
, const ImageRegion
& aRegion
,
400 uint32_t aWhichFrame
, SamplingFilter aSamplingFilter
,
401 const Maybe
<SVGImageContext
>& aSVGContext
, uint32_t aFlags
,
403 MOZ_ASSERT(!MustCreateSurface(aContext
, aSize
, aRegion
, aFlags
),
404 "Shouldn't need to create a surface");
406 gfxRect
clip(mClip
.X(), mClip
.Y(), mClip
.Width(), mClip
.Height());
407 nsIntSize
size(aSize
), innerSize(aSize
);
408 bool needScale
= false;
409 if (mSVGViewportSize
&& !mSVGViewportSize
->IsEmpty()) {
410 innerSize
= *mSVGViewportSize
;
412 } else if (NS_SUCCEEDED(InnerImage()->GetWidth(&innerSize
.width
)) &&
413 NS_SUCCEEDED(InnerImage()->GetHeight(&innerSize
.height
))) {
416 MOZ_ASSERT_UNREACHABLE(
417 "If ShouldClip() led us to draw then we should never get here");
421 double scaleX
= aSize
.width
/ clip
.Width();
422 double scaleY
= aSize
.height
/ clip
.Height();
424 // Map the clip and size to the scale requested by the caller.
425 clip
.Scale(scaleX
, scaleY
);
427 size
.Scale(scaleX
, scaleY
);
430 // We restrict our drawing to only the clipping region, and translate so that
431 // the clipping region is placed at the position the caller expects.
432 ImageRegion
region(aRegion
);
433 region
.MoveBy(clip
.X(), clip
.Y());
434 region
= region
.Intersect(clip
);
436 gfxContextMatrixAutoSaveRestore
saveMatrix(aContext
);
437 aContext
->Multiply(gfxMatrix::Translation(-clip
.X(), -clip
.Y()));
439 auto unclipViewport
= [&](const SVGImageContext
& aOldContext
) {
440 // Map the viewport to the inner image. Note that we don't take the aSize
441 // parameter of imgIContainer::Draw into account, just the clipping region.
442 // The size in pixels at which the output will ultimately be drawn is
443 // irrelevant here since the purpose of the SVG viewport size is to
444 // determine what *region* of the SVG document will be drawn.
445 SVGImageContext
context(aOldContext
);
446 auto oldViewport
= aOldContext
.GetViewportSize();
448 CSSIntSize newViewport
;
450 ceil(oldViewport
->width
* double(innerSize
.width
) / mClip
.Width());
452 ceil(oldViewport
->height
* double(innerSize
.height
) / mClip
.Height());
453 context
.SetViewportSize(Some(newViewport
));
458 return InnerImage()->Draw(aContext
, size
, region
, aWhichFrame
,
459 aSamplingFilter
, aSVGContext
.map(unclipViewport
),
464 ClippedImage::RequestDiscard() {
465 // We're very aggressive about discarding.
466 mCachedSurface
= nullptr;
468 return InnerImage()->RequestDiscard();
471 NS_IMETHODIMP_(Orientation
)
472 ClippedImage::GetOrientation() {
473 // XXX(seth): This should not actually be here; this is just to work around a
474 // what appears to be a bug in MSVC's linker.
475 return InnerImage()->GetOrientation();
478 nsIntSize
ClippedImage::OptimalImageSizeForDest(const gfxSize
& aDest
,
479 uint32_t aWhichFrame
,
480 SamplingFilter aSamplingFilter
,
483 return InnerImage()->OptimalImageSizeForDest(aDest
, aWhichFrame
,
484 aSamplingFilter
, aFlags
);
487 int32_t imgWidth
, imgHeight
;
488 bool needScale
= false;
489 bool forceUniformScaling
= false;
490 if (mSVGViewportSize
&& !mSVGViewportSize
->IsEmpty()) {
491 imgWidth
= mSVGViewportSize
->width
;
492 imgHeight
= mSVGViewportSize
->height
;
494 forceUniformScaling
= (aFlags
& imgIContainer::FLAG_FORCE_UNIFORM_SCALING
);
495 } else if (NS_SUCCEEDED(InnerImage()->GetWidth(&imgWidth
)) &&
496 NS_SUCCEEDED(InnerImage()->GetHeight(&imgHeight
))) {
501 // To avoid ugly sampling artifacts, ClippedImage needs the image size to
502 // be chosen such that the clipping region lies on pixel boundaries.
504 // First, we select a scale that's good for ClippedImage. An integer
505 // multiple of the size of the clipping region is always fine.
506 IntSize scale
= IntSize::Ceil(aDest
.width
/ mClip
.Width(),
507 aDest
.height
/ mClip
.Height());
509 if (forceUniformScaling
) {
510 scale
.width
= scale
.height
= max(scale
.height
, scale
.width
);
513 // Determine the size we'd prefer to render the inner image at, and ask the
514 // inner image what size we should actually use.
515 gfxSize
desiredSize(imgWidth
* scale
.width
, imgHeight
* scale
.height
);
516 nsIntSize innerDesiredSize
= InnerImage()->OptimalImageSizeForDest(
517 desiredSize
, aWhichFrame
, aSamplingFilter
, aFlags
);
519 // To get our final result, we take the inner image's desired size and
520 // determine how large the clipped region would be at that scale. (Again, we
521 // ensure an integer multiple of the size of the clipping region.)
523 IntSize::Ceil(double(innerDesiredSize
.width
) / imgWidth
,
524 double(innerDesiredSize
.height
) / imgHeight
);
525 return mClip
.Size() * finalScale
;
529 "If ShouldClip() led us to draw then we should never get here");
530 return InnerImage()->OptimalImageSizeForDest(aDest
, aWhichFrame
,
531 aSamplingFilter
, aFlags
);
534 NS_IMETHODIMP_(nsIntRect
)
535 ClippedImage::GetImageSpaceInvalidationRect(const nsIntRect
& aRect
) {
537 return InnerImage()->GetImageSpaceInvalidationRect(aRect
);
540 nsIntRect
rect(InnerImage()->GetImageSpaceInvalidationRect(aRect
));
541 rect
= rect
.Intersect(mClip
);
542 rect
.MoveBy(-mClip
.X(), -mClip
.Y());
547 } // namespace mozilla