Bug 1610955 [wpt PR 21357] - Order animations of same class by tree-order of their...
[gecko.git] / image / ClippedImage.cpp
blob4897cfae41b0a27792b5e8e3a0bf3cbe7877f0af
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 "SVGImageContext.h"
16 #include "gfxDrawable.h"
17 #include "gfxPlatform.h"
18 #include "gfxUtils.h"
19 #include "mozilla/Pair.h"
20 #include "mozilla/RefPtr.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 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 MakePair(surface ? ImgDrawResult::SUCCESS : ImgDrawResult::NOT_READY,
246 std::move(surface));
249 float frameToDraw = InnerImage()->GetFrameIndex(aWhichFrame);
250 if (!mCachedSurface ||
251 !mCachedSurface->Matches(aSize, aSVGContext, frameToDraw, aFlags) ||
252 mCachedSurface->NeedsRedraw()) {
253 // Create a surface to draw into.
254 RefPtr<DrawTarget> target =
255 gfxPlatform::GetPlatform()->CreateOffscreenContentDrawTarget(
256 IntSize(aSize.width, aSize.height), SurfaceFormat::OS_RGBA);
257 if (!target || !target->IsValid()) {
258 NS_ERROR("Could not create a DrawTarget");
259 return MakePair(ImgDrawResult::TEMPORARY_ERROR, RefPtr<SourceSurface>());
262 RefPtr<gfxContext> ctx = gfxContext::CreateOrNull(target);
263 MOZ_ASSERT(ctx); // already checked the draw target above
265 // Create our callback.
266 RefPtr<DrawSingleTileCallback> drawTileCallback =
267 new DrawSingleTileCallback(this, aSize, aSVGContext, aWhichFrame,
268 aFlags, aOpacity);
269 RefPtr<gfxDrawable> drawable =
270 new gfxCallbackDrawable(drawTileCallback, aSize);
272 // Actually draw. The callback will end up invoking DrawSingleTile.
273 gfxUtils::DrawPixelSnapped(ctx, drawable, SizeDouble(aSize),
274 ImageRegion::Create(aSize),
275 SurfaceFormat::OS_RGBA, SamplingFilter::LINEAR,
276 imgIContainer::FLAG_CLAMP);
278 // Cache the resulting surface.
279 mCachedSurface = MakeUnique<ClippedImageCachedSurface>(
280 target->Snapshot(), aSize, aSVGContext, frameToDraw, aFlags,
281 drawTileCallback->GetDrawResult());
284 MOZ_ASSERT(mCachedSurface, "Should have a cached surface now");
285 RefPtr<SourceSurface> surface = mCachedSurface->Surface();
286 return MakePair(mCachedSurface->GetDrawResult(), std::move(surface));
289 NS_IMETHODIMP_(bool)
290 ClippedImage::IsImageContainerAvailable(LayerManager* aManager,
291 uint32_t aFlags) {
292 if (!ShouldClip()) {
293 return InnerImage()->IsImageContainerAvailable(aManager, aFlags);
295 return false;
298 NS_IMETHODIMP_(already_AddRefed<ImageContainer>)
299 ClippedImage::GetImageContainer(LayerManager* aManager, uint32_t aFlags) {
300 // XXX(seth): We currently don't have a way of clipping the result of
301 // GetImageContainer. We work around this by always returning null, but if it
302 // ever turns out that ClippedImage is widely used on codepaths that can
303 // actually benefit from GetImageContainer, it would be a good idea to fix
304 // that method for performance reasons.
306 if (!ShouldClip()) {
307 return InnerImage()->GetImageContainer(aManager, aFlags);
310 return nullptr;
313 NS_IMETHODIMP_(bool)
314 ClippedImage::IsImageContainerAvailableAtSize(LayerManager* aManager,
315 const IntSize& aSize,
316 uint32_t aFlags) {
317 if (!ShouldClip()) {
318 return InnerImage()->IsImageContainerAvailableAtSize(aManager, aSize,
319 aFlags);
321 return false;
324 NS_IMETHODIMP_(ImgDrawResult)
325 ClippedImage::GetImageContainerAtSize(layers::LayerManager* aManager,
326 const gfx::IntSize& aSize,
327 const Maybe<SVGImageContext>& aSVGContext,
328 uint32_t aFlags,
329 layers::ImageContainer** aOutContainer) {
330 // XXX(seth): We currently don't have a way of clipping the result of
331 // GetImageContainer. We work around this by always returning null, but if it
332 // ever turns out that ClippedImage is widely used on codepaths that can
333 // actually benefit from GetImageContainer, it would be a good idea to fix
334 // that method for performance reasons.
336 if (!ShouldClip()) {
337 return InnerImage()->GetImageContainerAtSize(aManager, aSize, aSVGContext,
338 aFlags, aOutContainer);
341 return ImgDrawResult::NOT_SUPPORTED;
344 static bool MustCreateSurface(gfxContext* aContext, const nsIntSize& aSize,
345 const ImageRegion& aRegion,
346 const uint32_t aFlags) {
347 gfxRect imageRect(0, 0, aSize.width, aSize.height);
348 bool willTile = !imageRect.Contains(aRegion.Rect()) &&
349 !(aFlags & imgIContainer::FLAG_CLAMP);
350 bool willResample = aContext->CurrentMatrix().HasNonIntegerTranslation() &&
351 (willTile || !aRegion.RestrictionContains(imageRect));
352 return willTile || willResample;
355 NS_IMETHODIMP_(ImgDrawResult)
356 ClippedImage::Draw(gfxContext* aContext, const nsIntSize& aSize,
357 const ImageRegion& aRegion, uint32_t aWhichFrame,
358 SamplingFilter aSamplingFilter,
359 const Maybe<SVGImageContext>& aSVGContext, uint32_t aFlags,
360 float aOpacity) {
361 if (!ShouldClip()) {
362 return InnerImage()->Draw(aContext, aSize, aRegion, aWhichFrame,
363 aSamplingFilter, aSVGContext, aFlags, aOpacity);
366 // Check for tiling. If we need to tile then we need to create a
367 // gfxCallbackDrawable to handle drawing for us.
368 if (MustCreateSurface(aContext, aSize, aRegion, aFlags)) {
369 // Create a temporary surface containing a single tile of this image.
370 // GetFrame will call DrawSingleTile internally.
371 ImgDrawResult result;
372 RefPtr<SourceSurface> surface;
373 Tie(result, surface) =
374 GetFrameInternal(aSize, aSVGContext, aWhichFrame, aFlags, aOpacity);
375 if (!surface) {
376 MOZ_ASSERT(result != ImgDrawResult::SUCCESS);
377 return result;
380 // Create a drawable from that surface.
381 RefPtr<gfxSurfaceDrawable> drawable =
382 new gfxSurfaceDrawable(surface, aSize);
384 // Draw.
385 gfxUtils::DrawPixelSnapped(aContext, drawable, SizeDouble(aSize), aRegion,
386 SurfaceFormat::OS_RGBA, aSamplingFilter,
387 aOpacity);
389 return result;
392 return DrawSingleTile(aContext, aSize, aRegion, aWhichFrame, aSamplingFilter,
393 aSVGContext, aFlags, aOpacity);
396 ImgDrawResult ClippedImage::DrawSingleTile(
397 gfxContext* aContext, const nsIntSize& aSize, const ImageRegion& aRegion,
398 uint32_t aWhichFrame, SamplingFilter aSamplingFilter,
399 const Maybe<SVGImageContext>& aSVGContext, uint32_t aFlags,
400 float aOpacity) {
401 MOZ_ASSERT(!MustCreateSurface(aContext, aSize, aRegion, aFlags),
402 "Shouldn't need to create a surface");
404 gfxRect clip(mClip.X(), mClip.Y(), mClip.Width(), mClip.Height());
405 nsIntSize size(aSize), innerSize(aSize);
406 bool needScale = false;
407 if (mSVGViewportSize && !mSVGViewportSize->IsEmpty()) {
408 innerSize = *mSVGViewportSize;
409 needScale = true;
410 } else if (NS_SUCCEEDED(InnerImage()->GetWidth(&innerSize.width)) &&
411 NS_SUCCEEDED(InnerImage()->GetHeight(&innerSize.height))) {
412 needScale = true;
413 } else {
414 MOZ_ASSERT_UNREACHABLE(
415 "If ShouldClip() led us to draw then we should never get here");
418 if (needScale) {
419 double scaleX = aSize.width / clip.Width();
420 double scaleY = aSize.height / clip.Height();
422 // Map the clip and size to the scale requested by the caller.
423 clip.Scale(scaleX, scaleY);
424 size = innerSize;
425 size.Scale(scaleX, scaleY);
428 // We restrict our drawing to only the clipping region, and translate so that
429 // the clipping region is placed at the position the caller expects.
430 ImageRegion region(aRegion);
431 region.MoveBy(clip.X(), clip.Y());
432 region = region.Intersect(clip);
434 gfxContextMatrixAutoSaveRestore saveMatrix(aContext);
435 aContext->Multiply(gfxMatrix::Translation(-clip.X(), -clip.Y()));
437 auto unclipViewport = [&](const SVGImageContext& aOldContext) {
438 // Map the viewport to the inner image. Note that we don't take the aSize
439 // parameter of imgIContainer::Draw into account, just the clipping region.
440 // The size in pixels at which the output will ultimately be drawn is
441 // irrelevant here since the purpose of the SVG viewport size is to
442 // determine what *region* of the SVG document will be drawn.
443 SVGImageContext context(aOldContext);
444 auto oldViewport = aOldContext.GetViewportSize();
445 if (oldViewport) {
446 CSSIntSize newViewport;
447 newViewport.width =
448 ceil(oldViewport->width * double(innerSize.width) / mClip.Width());
449 newViewport.height =
450 ceil(oldViewport->height * double(innerSize.height) / mClip.Height());
451 context.SetViewportSize(Some(newViewport));
453 return context;
456 return InnerImage()->Draw(aContext, size, region, aWhichFrame,
457 aSamplingFilter, aSVGContext.map(unclipViewport),
458 aFlags, aOpacity);
461 NS_IMETHODIMP
462 ClippedImage::RequestDiscard() {
463 // We're very aggressive about discarding.
464 mCachedSurface = nullptr;
466 return InnerImage()->RequestDiscard();
469 NS_IMETHODIMP_(Orientation)
470 ClippedImage::GetOrientation() {
471 // XXX(seth): This should not actually be here; this is just to work around a
472 // what appears to be a bug in MSVC's linker.
473 return InnerImage()->GetOrientation();
476 nsIntSize ClippedImage::OptimalImageSizeForDest(const gfxSize& aDest,
477 uint32_t aWhichFrame,
478 SamplingFilter aSamplingFilter,
479 uint32_t aFlags) {
480 if (!ShouldClip()) {
481 return InnerImage()->OptimalImageSizeForDest(aDest, aWhichFrame,
482 aSamplingFilter, aFlags);
485 int32_t imgWidth, imgHeight;
486 bool needScale = false;
487 bool forceUniformScaling = false;
488 if (mSVGViewportSize && !mSVGViewportSize->IsEmpty()) {
489 imgWidth = mSVGViewportSize->width;
490 imgHeight = mSVGViewportSize->height;
491 needScale = true;
492 forceUniformScaling = (aFlags & imgIContainer::FLAG_FORCE_UNIFORM_SCALING);
493 } else if (NS_SUCCEEDED(InnerImage()->GetWidth(&imgWidth)) &&
494 NS_SUCCEEDED(InnerImage()->GetHeight(&imgHeight))) {
495 needScale = true;
498 if (needScale) {
499 // To avoid ugly sampling artifacts, ClippedImage needs the image size to
500 // be chosen such that the clipping region lies on pixel boundaries.
502 // First, we select a scale that's good for ClippedImage. An integer
503 // multiple of the size of the clipping region is always fine.
504 IntSize scale = IntSize::Ceil(aDest.width / mClip.Width(),
505 aDest.height / mClip.Height());
507 if (forceUniformScaling) {
508 scale.width = scale.height = max(scale.height, scale.width);
511 // Determine the size we'd prefer to render the inner image at, and ask the
512 // inner image what size we should actually use.
513 gfxSize desiredSize(double(imgWidth) * scale.width,
514 double(imgHeight) * scale.height);
515 nsIntSize innerDesiredSize = InnerImage()->OptimalImageSizeForDest(
516 desiredSize, aWhichFrame, aSamplingFilter, aFlags);
518 // To get our final result, we take the inner image's desired size and
519 // determine how large the clipped region would be at that scale. (Again, we
520 // ensure an integer multiple of the size of the clipping region.)
521 IntSize finalScale =
522 IntSize::Ceil(double(innerDesiredSize.width) / imgWidth,
523 double(innerDesiredSize.height) / imgHeight);
524 return mClip.Size() * finalScale;
527 MOZ_ASSERT(false,
528 "If ShouldClip() led us to draw then we should never get here");
529 return InnerImage()->OptimalImageSizeForDest(aDest, aWhichFrame,
530 aSamplingFilter, aFlags);
533 NS_IMETHODIMP_(nsIntRect)
534 ClippedImage::GetImageSpaceInvalidationRect(const nsIntRect& aRect) {
535 if (!ShouldClip()) {
536 return InnerImage()->GetImageSpaceInvalidationRect(aRect);
539 nsIntRect rect(InnerImage()->GetImageSpaceInvalidationRect(aRect));
540 rect = rect.Intersect(mClip);
541 rect.MoveBy(-mClip.X(), -mClip.Y());
542 return rect;
545 } // namespace image
546 } // namespace mozilla