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