Bug 1578501 [wpt PR 18803] - WebKit export of https://bugs.webkit.org/show_bug.cgi...
[gecko.git] / image / ClippedImage.cpp
blobd002f46275eaf72572439d8d66e0bcff0d2bae50
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"
8 #include <algorithm>
9 #include <new> // Workaround for bug in VS10; see bug 981264.
10 #include <cmath>
11 #include <utility>
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"
22 #include "ImageRegion.h"
23 #include "Orientation.h"
24 #include "SVGImageContext.h"
26 namespace mozilla {
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;
36 namespace image {
38 class ClippedImageCachedSurface {
39 public:
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");
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 &&
58 mFlags == aFlags;
61 already_AddRefed<SourceSurface> Surface() const {
62 RefPtr<SourceSurface> surf(mSurface);
63 return surf.forget();
66 ImgDrawResult GetDrawResult() const { return mDrawResult; }
68 bool NeedsRedraw() const {
69 return mDrawResult != ImgDrawResult::SUCCESS &&
70 mDrawResult != ImgDrawResult::BAD_IMAGE;
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;
82 class DrawSingleTileCallback : public gfxDrawingCallback {
83 public:
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");
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);
110 return true;
113 ImgDrawResult GetDrawResult() { return mDrawResult; }
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;
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 =
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
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);
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.
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);
183 MOZ_ASSERT(mShouldClip.isSome(), "Should have computed a result");
184 return *mShouldClip;
187 NS_IMETHODIMP
188 ClippedImage::GetWidth(int32_t* aWidth) {
189 if (!ShouldClip()) {
190 return InnerImage()->GetWidth(aWidth);
193 *aWidth = mClip.Width();
194 return NS_OK;
197 NS_IMETHODIMP
198 ClippedImage::GetHeight(int32_t* aHeight) {
199 if (!ShouldClip()) {
200 return InnerImage()->GetHeight(aHeight);
203 *aHeight = mClip.Height();
204 return NS_OK;
207 NS_IMETHODIMP
208 ClippedImage::GetIntrinsicSize(nsSize* aSize) {
209 if (!ShouldClip()) {
210 return InnerImage()->GetIntrinsicSize(aSize);
213 *aSize = nsSize(mClip.Width(), mClip.Height());
214 return NS_OK;
217 Maybe<AspectRatio> ClippedImage::GetIntrinsicRatio() {
218 if (!ShouldClip()) {
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,
236 uint32_t aFlags) {
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) {
245 if (!ShouldClip()) {
246 RefPtr<SourceSurface> surface = InnerImage()->GetFrame(aWhichFrame, aFlags);
247 return MakePair(surface ? ImgDrawResult::SUCCESS : ImgDrawResult::NOT_READY,
248 std::move(surface));
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,
270 aFlags, aOpacity);
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));
291 NS_IMETHODIMP_(bool)
292 ClippedImage::IsImageContainerAvailable(LayerManager* aManager,
293 uint32_t aFlags) {
294 if (!ShouldClip()) {
295 return InnerImage()->IsImageContainerAvailable(aManager, aFlags);
297 return false;
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.
308 if (!ShouldClip()) {
309 return InnerImage()->GetImageContainer(aManager, aFlags);
312 return nullptr;
315 NS_IMETHODIMP_(bool)
316 ClippedImage::IsImageContainerAvailableAtSize(LayerManager* aManager,
317 const IntSize& aSize,
318 uint32_t aFlags) {
319 if (!ShouldClip()) {
320 return InnerImage()->IsImageContainerAvailableAtSize(aManager, aSize,
321 aFlags);
323 return false;
326 NS_IMETHODIMP_(ImgDrawResult)
327 ClippedImage::GetImageContainerAtSize(layers::LayerManager* aManager,
328 const gfx::IntSize& aSize,
329 const Maybe<SVGImageContext>& aSVGContext,
330 uint32_t aFlags,
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.
338 if (!ShouldClip()) {
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,
362 float aOpacity) {
363 if (!ShouldClip()) {
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);
377 if (!surface) {
378 MOZ_ASSERT(result != ImgDrawResult::SUCCESS);
379 return result;
382 // Create a drawable from that surface.
383 RefPtr<gfxSurfaceDrawable> drawable =
384 new gfxSurfaceDrawable(surface, aSize);
386 // Draw.
387 gfxUtils::DrawPixelSnapped(aContext, drawable, SizeDouble(aSize), aRegion,
388 SurfaceFormat::B8G8R8A8, aSamplingFilter,
389 aOpacity);
391 return result;
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,
402 float aOpacity) {
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;
411 needScale = true;
412 } else if (NS_SUCCEEDED(InnerImage()->GetWidth(&innerSize.width)) &&
413 NS_SUCCEEDED(InnerImage()->GetHeight(&innerSize.height))) {
414 needScale = true;
415 } else {
416 MOZ_ASSERT_UNREACHABLE(
417 "If ShouldClip() led us to draw then we should never get here");
420 if (needScale) {
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);
426 size = innerSize;
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();
447 if (oldViewport) {
448 CSSIntSize newViewport;
449 newViewport.width =
450 ceil(oldViewport->width * double(innerSize.width) / mClip.Width());
451 newViewport.height =
452 ceil(oldViewport->height * double(innerSize.height) / mClip.Height());
453 context.SetViewportSize(Some(newViewport));
455 return context;
458 return InnerImage()->Draw(aContext, size, region, aWhichFrame,
459 aSamplingFilter, aSVGContext.map(unclipViewport),
460 aFlags, aOpacity);
463 NS_IMETHODIMP
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,
481 uint32_t aFlags) {
482 if (!ShouldClip()) {
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;
493 needScale = true;
494 forceUniformScaling = (aFlags & imgIContainer::FLAG_FORCE_UNIFORM_SCALING);
495 } else if (NS_SUCCEEDED(InnerImage()->GetWidth(&imgWidth)) &&
496 NS_SUCCEEDED(InnerImage()->GetHeight(&imgHeight))) {
497 needScale = true;
500 if (needScale) {
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.)
522 IntSize finalScale =
523 IntSize::Ceil(double(innerDesiredSize.width) / imgWidth,
524 double(innerDesiredSize.height) / imgHeight);
525 return mClip.Size() * finalScale;
528 MOZ_ASSERT(false,
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) {
536 if (!ShouldClip()) {
537 return InnerImage()->GetImageSpaceInvalidationRect(aRect);
540 nsIntRect rect(InnerImage()->GetImageSpaceInvalidationRect(aRect));
541 rect = rect.Intersect(mClip);
542 rect.MoveBy(-mClip.X(), -mClip.Y());
543 return rect;
546 } // namespace image
547 } // namespace mozilla