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"
10 #include <new> // Workaround for bug in VS10; see bug 981264.
13 #include "ImageRegion.h"
14 #include "Orientation.h"
15 #include "gfxContext.h"
16 #include "gfxDrawable.h"
17 #include "gfxPlatform.h"
19 #include "mozilla/RefPtr.h"
20 #include "mozilla/SVGImageContext.h"
21 #include "mozilla/Tuple.h"
22 #include "mozilla/gfx/2D.h"
31 class ClippedImageCachedSurface
{
33 ClippedImageCachedSurface(already_AddRefed
<SourceSurface
> aSurface
,
34 const nsIntSize
& aSize
,
35 const SVGImageContext
& aSVGContext
, float aFrame
,
36 uint32_t aFlags
, ImgDrawResult aDrawResult
)
39 mSVGContext(aSVGContext
),
42 mDrawResult(aDrawResult
) {
43 MOZ_ASSERT(mSurface
, "Must have a valid surface");
46 bool Matches(const nsIntSize
& aSize
, const SVGImageContext
& aSVGContext
,
47 float aFrame
, uint32_t aFlags
) const {
48 return mSize
== aSize
&& mSVGContext
== aSVGContext
&& mFrame
== aFrame
&&
52 already_AddRefed
<SourceSurface
> Surface() const {
53 RefPtr
<SourceSurface
> surf(mSurface
);
57 ImgDrawResult
GetDrawResult() const { return mDrawResult
; }
59 bool NeedsRedraw() const {
60 return mDrawResult
!= ImgDrawResult::SUCCESS
&&
61 mDrawResult
!= ImgDrawResult::BAD_IMAGE
;
65 RefPtr
<SourceSurface
> mSurface
;
66 const nsIntSize mSize
;
67 SVGImageContext mSVGContext
;
69 const uint32_t mFlags
;
70 const ImgDrawResult mDrawResult
;
73 class DrawSingleTileCallback
: public gfxDrawingCallback
{
75 DrawSingleTileCallback(ClippedImage
* aImage
, const nsIntSize
& aSize
,
76 const SVGImageContext
& aSVGContext
,
77 uint32_t aWhichFrame
, uint32_t aFlags
, float aOpacity
)
80 mSVGContext(aSVGContext
),
81 mWhichFrame(aWhichFrame
),
83 mDrawResult(ImgDrawResult::NOT_READY
),
85 MOZ_ASSERT(mImage
, "Must have an image to clip");
88 virtual bool operator()(gfxContext
* aContext
, const gfxRect
& aFillRect
,
89 const SamplingFilter aSamplingFilter
,
90 const gfxMatrix
& aTransform
) override
{
91 MOZ_ASSERT(aTransform
.IsIdentity(),
92 "Caller is probably CreateSamplingRestrictedDrawable, "
93 "which should not happen");
95 // Draw the image. |gfxCallbackDrawable| always calls this function with
96 // arguments that guarantee we never tile.
97 mDrawResult
= mImage
->DrawSingleTile(
98 aContext
, mSize
, ImageRegion::Create(aFillRect
), mWhichFrame
,
99 aSamplingFilter
, mSVGContext
, mFlags
, mOpacity
);
104 ImgDrawResult
GetDrawResult() { return mDrawResult
; }
107 RefPtr
<ClippedImage
> mImage
;
108 const nsIntSize mSize
;
109 const SVGImageContext
& mSVGContext
;
110 const uint32_t mWhichFrame
;
111 const uint32_t mFlags
;
112 ImgDrawResult mDrawResult
;
116 ClippedImage::ClippedImage(Image
* aImage
, nsIntRect aClip
,
117 const Maybe
<nsSize
>& aSVGViewportSize
)
118 : ImageWrapper(aImage
), mClip(aClip
) {
119 MOZ_ASSERT(aImage
!= nullptr, "ClippedImage requires an existing Image");
120 MOZ_ASSERT_IF(aSVGViewportSize
,
121 aImage
->GetType() == imgIContainer::TYPE_VECTOR
);
122 if (aSVGViewportSize
) {
124 Some(aSVGViewportSize
->ToNearestPixels(AppUnitsPerCSSPixel()));
128 ClippedImage::~ClippedImage() {}
130 bool ClippedImage::ShouldClip() {
131 // We need to evaluate the clipping region against the image's width and
132 // height once they're available to determine if it's valid and whether we
133 // actually need to do any work. We may fail if the image's width and height
134 // aren't available yet, in which case we'll try again later.
135 if (mShouldClip
.isNothing()) {
136 int32_t width
, height
;
137 RefPtr
<ProgressTracker
> progressTracker
=
138 InnerImage()->GetProgressTracker();
139 if (InnerImage()->HasError()) {
140 // If there's a problem with the inner image we'll let it handle
142 mShouldClip
.emplace(false);
143 } else if (mSVGViewportSize
&& !mSVGViewportSize
->IsEmpty()) {
144 // Clamp the clipping region to the size of the SVG viewport.
145 nsIntRect
svgViewportRect(nsIntPoint(0, 0), *mSVGViewportSize
);
147 mClip
= mClip
.Intersect(svgViewportRect
);
149 // If the clipping region is the same size as the SVG viewport size
150 // we don't have to do anything.
151 mShouldClip
.emplace(!mClip
.IsEqualInterior(svgViewportRect
));
152 } else if (NS_SUCCEEDED(InnerImage()->GetWidth(&width
)) && width
> 0 &&
153 NS_SUCCEEDED(InnerImage()->GetHeight(&height
)) && height
> 0) {
154 // Clamp the clipping region to the size of the underlying image.
155 mClip
= mClip
.Intersect(nsIntRect(0, 0, width
, height
));
157 // If the clipping region is the same size as the underlying image we
158 // don't have to do anything.
160 !mClip
.IsEqualInterior(nsIntRect(0, 0, width
, height
)));
161 } else if (progressTracker
&&
162 !(progressTracker
->GetProgress() & FLAG_LOAD_COMPLETE
)) {
163 // The image just hasn't finished loading yet. We don't yet know whether
164 // clipping with be needed or not for now. Just return without memorizing
168 // We have a fully loaded image without a clearly defined width and
169 // height. This can happen with SVG images.
170 mShouldClip
.emplace(false);
174 MOZ_ASSERT(mShouldClip
.isSome(), "Should have computed a result");
179 ClippedImage::GetWidth(int32_t* aWidth
) {
181 return InnerImage()->GetWidth(aWidth
);
184 *aWidth
= mClip
.Width();
189 ClippedImage::GetHeight(int32_t* aHeight
) {
191 return InnerImage()->GetHeight(aHeight
);
194 *aHeight
= mClip
.Height();
199 ClippedImage::GetIntrinsicSize(nsSize
* aSize
) {
201 return InnerImage()->GetIntrinsicSize(aSize
);
204 *aSize
= nsSize(mClip
.Width(), mClip
.Height());
208 Maybe
<AspectRatio
> ClippedImage::GetIntrinsicRatio() {
210 return InnerImage()->GetIntrinsicRatio();
213 return Some(AspectRatio::FromSize(mClip
.Width(), mClip
.Height()));
216 NS_IMETHODIMP_(already_AddRefed
<SourceSurface
>)
217 ClippedImage::GetFrame(uint32_t aWhichFrame
, uint32_t aFlags
) {
218 ImgDrawResult result
;
219 RefPtr
<SourceSurface
> surface
;
220 Tie(result
, surface
) = GetFrameInternal(mClip
.Size(), SVGImageContext(),
221 Nothing(), aWhichFrame
, aFlags
, 1.0);
222 return surface
.forget();
225 NS_IMETHODIMP_(already_AddRefed
<SourceSurface
>)
226 ClippedImage::GetFrameAtSize(const IntSize
& aSize
, uint32_t aWhichFrame
,
228 // XXX(seth): It'd be nice to support downscale-during-decode for this case,
229 // but right now we just fall back to the intrinsic size.
230 return GetFrame(aWhichFrame
, aFlags
);
233 std::pair
<ImgDrawResult
, RefPtr
<SourceSurface
>> ClippedImage::GetFrameInternal(
234 const nsIntSize
& aSize
, const SVGImageContext
& aSVGContext
,
235 const Maybe
<ImageIntRegion
>& aRegion
, uint32_t aWhichFrame
, uint32_t aFlags
,
238 RefPtr
<SourceSurface
> surface
= InnerImage()->GetFrame(aWhichFrame
, aFlags
);
239 return std::make_pair(
240 surface
? ImgDrawResult::SUCCESS
: ImgDrawResult::NOT_READY
,
244 float frameToDraw
= InnerImage()->GetFrameIndex(aWhichFrame
);
245 if (!mCachedSurface
||
246 !mCachedSurface
->Matches(aSize
, aSVGContext
, frameToDraw
, aFlags
) ||
247 mCachedSurface
->NeedsRedraw()) {
248 // Create a surface to draw into.
249 RefPtr
<DrawTarget
> target
=
250 gfxPlatform::GetPlatform()->CreateOffscreenContentDrawTarget(
251 IntSize(aSize
.width
, aSize
.height
), SurfaceFormat::OS_RGBA
);
252 if (!target
|| !target
->IsValid()) {
253 NS_ERROR("Could not create a DrawTarget");
254 return std::make_pair(ImgDrawResult::TEMPORARY_ERROR
,
255 RefPtr
<SourceSurface
>());
258 RefPtr
<gfxContext
> ctx
= gfxContext::CreateOrNull(target
);
259 MOZ_ASSERT(ctx
); // already checked the draw target above
261 // Create our callback.
262 RefPtr
<DrawSingleTileCallback
> drawTileCallback
=
263 new DrawSingleTileCallback(this, aSize
, aSVGContext
, aWhichFrame
,
265 RefPtr
<gfxDrawable
> drawable
=
266 new gfxCallbackDrawable(drawTileCallback
, aSize
);
268 // Actually draw. The callback will end up invoking DrawSingleTile.
269 gfxUtils::DrawPixelSnapped(ctx
, drawable
, SizeDouble(aSize
),
270 ImageRegion::Create(aSize
),
271 SurfaceFormat::OS_RGBA
, SamplingFilter::LINEAR
,
272 imgIContainer::FLAG_CLAMP
);
274 // Cache the resulting surface.
275 mCachedSurface
= MakeUnique
<ClippedImageCachedSurface
>(
276 target
->Snapshot(), aSize
, aSVGContext
, frameToDraw
, aFlags
,
277 drawTileCallback
->GetDrawResult());
280 MOZ_ASSERT(mCachedSurface
, "Should have a cached surface now");
281 RefPtr
<SourceSurface
> surface
= mCachedSurface
->Surface();
282 return std::make_pair(mCachedSurface
->GetDrawResult(), std::move(surface
));
286 ClippedImage::IsImageContainerAvailable(WindowRenderer
* aRenderer
,
289 return InnerImage()->IsImageContainerAvailable(aRenderer
, aFlags
);
294 NS_IMETHODIMP_(ImgDrawResult
)
295 ClippedImage::GetImageProvider(WindowRenderer
* aRenderer
,
296 const gfx::IntSize
& aSize
,
297 const SVGImageContext
& aSVGContext
,
298 const Maybe
<ImageIntRegion
>& aRegion
,
300 WebRenderImageProvider
** aProvider
) {
301 // XXX(seth): We currently don't have a way of clipping the result of
302 // GetImageContainer. We work around this by always returning null, but if it
303 // ever turns out that ClippedImage is widely used on codepaths that can
304 // actually benefit from GetImageContainer, it would be a good idea to fix
305 // that method for performance reasons.
308 return InnerImage()->GetImageProvider(aRenderer
, aSize
, aSVGContext
,
309 aRegion
, aFlags
, aProvider
);
312 return ImgDrawResult::NOT_SUPPORTED
;
315 static bool MustCreateSurface(gfxContext
* aContext
, const nsIntSize
& aSize
,
316 const ImageRegion
& aRegion
,
317 const uint32_t aFlags
) {
318 gfxRect
imageRect(0, 0, aSize
.width
, aSize
.height
);
319 bool willTile
= !imageRect
.Contains(aRegion
.Rect()) &&
320 !(aFlags
& imgIContainer::FLAG_CLAMP
);
321 bool willResample
= aContext
->CurrentMatrix().HasNonIntegerTranslation() &&
322 (willTile
|| !aRegion
.RestrictionContains(imageRect
));
323 return willTile
|| willResample
;
326 NS_IMETHODIMP_(ImgDrawResult
)
327 ClippedImage::Draw(gfxContext
* aContext
, const nsIntSize
& aSize
,
328 const ImageRegion
& aRegion
, uint32_t aWhichFrame
,
329 SamplingFilter aSamplingFilter
,
330 const SVGImageContext
& aSVGContext
, uint32_t aFlags
,
333 return InnerImage()->Draw(aContext
, aSize
, aRegion
, aWhichFrame
,
334 aSamplingFilter
, aSVGContext
, aFlags
, aOpacity
);
337 // Check for tiling. If we need to tile then we need to create a
338 // gfxCallbackDrawable to handle drawing for us.
339 if (MustCreateSurface(aContext
, aSize
, aRegion
, aFlags
)) {
340 // Create a temporary surface containing a single tile of this image.
341 // GetFrame will call DrawSingleTile internally.
342 ImgDrawResult result
;
343 RefPtr
<SourceSurface
> surface
;
344 Tie(result
, surface
) = GetFrameInternal(aSize
, aSVGContext
, Nothing(),
345 aWhichFrame
, aFlags
, aOpacity
);
347 MOZ_ASSERT(result
!= ImgDrawResult::SUCCESS
);
351 // Create a drawable from that surface.
352 RefPtr
<gfxSurfaceDrawable
> drawable
=
353 new gfxSurfaceDrawable(surface
, aSize
);
356 gfxUtils::DrawPixelSnapped(aContext
, drawable
, SizeDouble(aSize
), aRegion
,
357 SurfaceFormat::OS_RGBA
, aSamplingFilter
,
363 return DrawSingleTile(aContext
, aSize
, aRegion
, aWhichFrame
, aSamplingFilter
,
364 aSVGContext
, aFlags
, aOpacity
);
367 ImgDrawResult
ClippedImage::DrawSingleTile(
368 gfxContext
* aContext
, const nsIntSize
& aSize
, const ImageRegion
& aRegion
,
369 uint32_t aWhichFrame
, SamplingFilter aSamplingFilter
,
370 const SVGImageContext
& aSVGContext
, uint32_t aFlags
, float aOpacity
) {
371 MOZ_ASSERT(!MustCreateSurface(aContext
, aSize
, aRegion
, aFlags
),
372 "Shouldn't need to create a surface");
374 gfxRect
clip(mClip
.X(), mClip
.Y(), mClip
.Width(), mClip
.Height());
375 nsIntSize
size(aSize
), innerSize(aSize
);
376 bool needScale
= false;
377 if (mSVGViewportSize
&& !mSVGViewportSize
->IsEmpty()) {
378 innerSize
= *mSVGViewportSize
;
380 } else if (NS_SUCCEEDED(InnerImage()->GetWidth(&innerSize
.width
)) &&
381 NS_SUCCEEDED(InnerImage()->GetHeight(&innerSize
.height
))) {
384 MOZ_ASSERT_UNREACHABLE(
385 "If ShouldClip() led us to draw then we should never get here");
389 double scaleX
= aSize
.width
/ clip
.Width();
390 double scaleY
= aSize
.height
/ clip
.Height();
392 // Map the clip and size to the scale requested by the caller.
393 clip
.Scale(scaleX
, scaleY
);
395 size
.Scale(scaleX
, scaleY
);
398 // We restrict our drawing to only the clipping region, and translate so that
399 // the clipping region is placed at the position the caller expects.
400 ImageRegion
region(aRegion
);
401 region
.MoveBy(clip
.X(), clip
.Y());
402 region
= region
.Intersect(clip
);
404 gfxContextMatrixAutoSaveRestore
saveMatrix(aContext
);
405 aContext
->Multiply(gfxMatrix::Translation(-clip
.X(), -clip
.Y()));
407 auto unclipViewport
= [&](const SVGImageContext
& aOldContext
) {
408 // Map the viewport to the inner image. Note that we don't take the aSize
409 // parameter of imgIContainer::Draw into account, just the clipping region.
410 // The size in pixels at which the output will ultimately be drawn is
411 // irrelevant here since the purpose of the SVG viewport size is to
412 // determine what *region* of the SVG document will be drawn.
413 SVGImageContext
context(aOldContext
);
414 auto oldViewport
= aOldContext
.GetViewportSize();
416 CSSIntSize newViewport
;
418 ceil(oldViewport
->width
* double(innerSize
.width
) / mClip
.Width());
420 ceil(oldViewport
->height
* double(innerSize
.height
) / mClip
.Height());
421 context
.SetViewportSize(Some(newViewport
));
426 return InnerImage()->Draw(aContext
, size
, region
, aWhichFrame
,
427 aSamplingFilter
, unclipViewport(aSVGContext
),
432 ClippedImage::RequestDiscard() {
433 // We're very aggressive about discarding.
434 mCachedSurface
= nullptr;
436 return InnerImage()->RequestDiscard();
439 NS_IMETHODIMP_(Orientation
)
440 ClippedImage::GetOrientation() {
441 // XXX(seth): This should not actually be here; this is just to work around a
442 // what appears to be a bug in MSVC's linker.
443 return InnerImage()->GetOrientation();
446 nsIntSize
ClippedImage::OptimalImageSizeForDest(const gfxSize
& aDest
,
447 uint32_t aWhichFrame
,
448 SamplingFilter aSamplingFilter
,
451 return InnerImage()->OptimalImageSizeForDest(aDest
, aWhichFrame
,
452 aSamplingFilter
, aFlags
);
455 int32_t imgWidth
, imgHeight
;
456 bool needScale
= false;
457 bool forceUniformScaling
= false;
458 if (mSVGViewportSize
&& !mSVGViewportSize
->IsEmpty()) {
459 imgWidth
= mSVGViewportSize
->width
;
460 imgHeight
= mSVGViewportSize
->height
;
462 forceUniformScaling
= (aFlags
& imgIContainer::FLAG_FORCE_UNIFORM_SCALING
);
463 } else if (NS_SUCCEEDED(InnerImage()->GetWidth(&imgWidth
)) &&
464 NS_SUCCEEDED(InnerImage()->GetHeight(&imgHeight
))) {
469 // To avoid ugly sampling artifacts, ClippedImage needs the image size to
470 // be chosen such that the clipping region lies on pixel boundaries.
472 // First, we select a scale that's good for ClippedImage. An integer
473 // multiple of the size of the clipping region is always fine.
474 IntSize scale
= IntSize::Ceil(aDest
.width
/ mClip
.Width(),
475 aDest
.height
/ mClip
.Height());
477 if (forceUniformScaling
) {
478 scale
.width
= scale
.height
= max(scale
.height
, scale
.width
);
481 // Determine the size we'd prefer to render the inner image at, and ask the
482 // inner image what size we should actually use.
483 gfxSize
desiredSize(double(imgWidth
) * scale
.width
,
484 double(imgHeight
) * scale
.height
);
485 nsIntSize innerDesiredSize
= InnerImage()->OptimalImageSizeForDest(
486 desiredSize
, aWhichFrame
, aSamplingFilter
, aFlags
);
488 // To get our final result, we take the inner image's desired size and
489 // determine how large the clipped region would be at that scale. (Again, we
490 // ensure an integer multiple of the size of the clipping region.)
492 IntSize::Ceil(double(innerDesiredSize
.width
) / imgWidth
,
493 double(innerDesiredSize
.height
) / imgHeight
);
494 return mClip
.Size() * finalScale
;
498 "If ShouldClip() led us to draw then we should never get here");
499 return InnerImage()->OptimalImageSizeForDest(aDest
, aWhichFrame
,
500 aSamplingFilter
, aFlags
);
503 NS_IMETHODIMP_(nsIntRect
)
504 ClippedImage::GetImageSpaceInvalidationRect(const nsIntRect
& aRect
) {
506 return InnerImage()->GetImageSpaceInvalidationRect(aRect
);
509 nsIntRect
rect(InnerImage()->GetImageSpaceInvalidationRect(aRect
));
510 rect
= rect
.Intersect(mClip
);
511 rect
.MoveBy(-mClip
.X(), -mClip
.Y());
516 } // namespace mozilla