Bug 1686495 [wpt PR 27132] - Add tests for proposed WebDriver Shadow DOM support...
[gecko.git] / image / ClippedImage.cpp
blobddb6bf77b9f3fcd743995a1c2325b62335ae1609
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) =
228 GetFrameInternal(mClip.Size(), Nothing(), 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 uint32_t aWhichFrame, uint32_t aFlags, float aOpacity) {
243 if (!ShouldClip()) {
244 RefPtr<SourceSurface> surface = InnerImage()->GetFrame(aWhichFrame, aFlags);
245 return std::make_pair(
246 surface ? ImgDrawResult::SUCCESS : ImgDrawResult::NOT_READY,
247 std::move(surface));
250 float frameToDraw = InnerImage()->GetFrameIndex(aWhichFrame);
251 if (!mCachedSurface ||
252 !mCachedSurface->Matches(aSize, aSVGContext, frameToDraw, aFlags) ||
253 mCachedSurface->NeedsRedraw()) {
254 // Create a surface to draw into.
255 RefPtr<DrawTarget> target =
256 gfxPlatform::GetPlatform()->CreateOffscreenContentDrawTarget(
257 IntSize(aSize.width, aSize.height), SurfaceFormat::OS_RGBA);
258 if (!target || !target->IsValid()) {
259 NS_ERROR("Could not create a DrawTarget");
260 return std::make_pair(ImgDrawResult::TEMPORARY_ERROR,
261 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::OS_RGBA, 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 std::make_pair(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::OS_RGBA, 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(double(imgWidth) * scale.width,
516 double(imgHeight) * scale.height);
517 nsIntSize innerDesiredSize = InnerImage()->OptimalImageSizeForDest(
518 desiredSize, aWhichFrame, aSamplingFilter, aFlags);
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;
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);
535 NS_IMETHODIMP_(nsIntRect)
536 ClippedImage::GetImageSpaceInvalidationRect(const nsIntRect& aRect) {
537 if (!ShouldClip()) {
538 return InnerImage()->GetImageSpaceInvalidationRect(aRect);
541 nsIntRect rect(InnerImage()->GetImageSpaceInvalidationRect(aRect));
542 rect = rect.Intersect(mClip);
543 rect.MoveBy(-mClip.X(), -mClip.Y());
544 return rect;
547 } // namespace image
548 } // namespace mozilla