Bug 1669129 - [devtools] Enable devtools.overflow.debugging.enabled. r=jdescottes
[gecko.git] / image / ClippedImage.cpp
blob2950116b072d99e7880911f1b078b9c1651e4ec5
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 "gfxDrawable.h"
16 #include "gfxPlatform.h"
17 #include "gfxUtils.h"
18 #include "mozilla/RefPtr.h"
19 #include "mozilla/SVGImageContext.h"
20 #include "mozilla/Tuple.h"
21 #include "mozilla/gfx/2D.h"
23 namespace mozilla {
25 using namespace gfx;
26 using layers::ImageContainer;
27 using layers::LayerManager;
28 using std::make_pair;
29 using std::max;
30 using std::modf;
31 using std::pair;
33 namespace image {
35 class ClippedImageCachedSurface {
36 public:
37 ClippedImageCachedSurface(already_AddRefed<SourceSurface> aSurface,
38 const nsIntSize& aSize,
39 const Maybe<SVGImageContext>& aSVGContext,
40 float aFrame, uint32_t aFlags,
41 ImgDrawResult aDrawResult)
42 : mSurface(aSurface),
43 mSize(aSize),
44 mSVGContext(aSVGContext),
45 mFrame(aFrame),
46 mFlags(aFlags),
47 mDrawResult(aDrawResult) {
48 MOZ_ASSERT(mSurface, "Must have a valid surface");
51 bool Matches(const nsIntSize& aSize,
52 const Maybe<SVGImageContext>& aSVGContext, float aFrame,
53 uint32_t aFlags) const {
54 return mSize == aSize && mSVGContext == aSVGContext && mFrame == aFrame &&
55 mFlags == aFlags;
58 already_AddRefed<SourceSurface> Surface() const {
59 RefPtr<SourceSurface> surf(mSurface);
60 return surf.forget();
63 ImgDrawResult GetDrawResult() const { return mDrawResult; }
65 bool NeedsRedraw() const {
66 return mDrawResult != ImgDrawResult::SUCCESS &&
67 mDrawResult != ImgDrawResult::BAD_IMAGE;
70 private:
71 RefPtr<SourceSurface> mSurface;
72 const nsIntSize mSize;
73 Maybe<SVGImageContext> mSVGContext;
74 const float mFrame;
75 const uint32_t mFlags;
76 const ImgDrawResult mDrawResult;
79 class DrawSingleTileCallback : public gfxDrawingCallback {
80 public:
81 DrawSingleTileCallback(ClippedImage* aImage, const nsIntSize& aSize,
82 const Maybe<SVGImageContext>& aSVGContext,
83 uint32_t aWhichFrame, uint32_t aFlags, float aOpacity)
84 : mImage(aImage),
85 mSize(aSize),
86 mSVGContext(aSVGContext),
87 mWhichFrame(aWhichFrame),
88 mFlags(aFlags),
89 mDrawResult(ImgDrawResult::NOT_READY),
90 mOpacity(aOpacity) {
91 MOZ_ASSERT(mImage, "Must have an image to clip");
94 virtual bool operator()(gfxContext* aContext, const gfxRect& aFillRect,
95 const SamplingFilter aSamplingFilter,
96 const gfxMatrix& aTransform) override {
97 MOZ_ASSERT(aTransform.IsIdentity(),
98 "Caller is probably CreateSamplingRestrictedDrawable, "
99 "which should not happen");
101 // Draw the image. |gfxCallbackDrawable| always calls this function with
102 // arguments that guarantee we never tile.
103 mDrawResult = mImage->DrawSingleTile(
104 aContext, mSize, ImageRegion::Create(aFillRect), mWhichFrame,
105 aSamplingFilter, mSVGContext, mFlags, mOpacity);
107 return true;
110 ImgDrawResult GetDrawResult() { return mDrawResult; }
112 private:
113 RefPtr<ClippedImage> mImage;
114 const nsIntSize mSize;
115 const Maybe<SVGImageContext>& mSVGContext;
116 const uint32_t mWhichFrame;
117 const uint32_t mFlags;
118 ImgDrawResult mDrawResult;
119 float mOpacity;
122 ClippedImage::ClippedImage(Image* aImage, nsIntRect aClip,
123 const Maybe<nsSize>& aSVGViewportSize)
124 : ImageWrapper(aImage), mClip(aClip) {
125 MOZ_ASSERT(aImage != nullptr, "ClippedImage requires an existing Image");
126 MOZ_ASSERT_IF(aSVGViewportSize,
127 aImage->GetType() == imgIContainer::TYPE_VECTOR);
128 if (aSVGViewportSize) {
129 mSVGViewportSize =
130 Some(aSVGViewportSize->ToNearestPixels(AppUnitsPerCSSPixel()));
134 ClippedImage::~ClippedImage() {}
136 bool ClippedImage::ShouldClip() {
137 // We need to evaluate the clipping region against the image's width and
138 // height once they're available to determine if it's valid and whether we
139 // actually need to do any work. We may fail if the image's width and height
140 // aren't available yet, in which case we'll try again later.
141 if (mShouldClip.isNothing()) {
142 int32_t width, height;
143 RefPtr<ProgressTracker> progressTracker =
144 InnerImage()->GetProgressTracker();
145 if (InnerImage()->HasError()) {
146 // If there's a problem with the inner image we'll let it handle
147 // everything.
148 mShouldClip.emplace(false);
149 } else if (mSVGViewportSize && !mSVGViewportSize->IsEmpty()) {
150 // Clamp the clipping region to the size of the SVG viewport.
151 nsIntRect svgViewportRect(nsIntPoint(0, 0), *mSVGViewportSize);
153 mClip = mClip.Intersect(svgViewportRect);
155 // If the clipping region is the same size as the SVG viewport size
156 // we don't have to do anything.
157 mShouldClip.emplace(!mClip.IsEqualInterior(svgViewportRect));
158 } else if (NS_SUCCEEDED(InnerImage()->GetWidth(&width)) && width > 0 &&
159 NS_SUCCEEDED(InnerImage()->GetHeight(&height)) && height > 0) {
160 // Clamp the clipping region to the size of the underlying image.
161 mClip = mClip.Intersect(nsIntRect(0, 0, width, height));
163 // If the clipping region is the same size as the underlying image we
164 // don't have to do anything.
165 mShouldClip.emplace(
166 !mClip.IsEqualInterior(nsIntRect(0, 0, width, height)));
167 } else if (progressTracker &&
168 !(progressTracker->GetProgress() & FLAG_LOAD_COMPLETE)) {
169 // The image just hasn't finished loading yet. We don't yet know whether
170 // clipping with be needed or not for now. Just return without memorizing
171 // anything.
172 return false;
173 } else {
174 // We have a fully loaded image without a clearly defined width and
175 // height. This can happen with SVG images.
176 mShouldClip.emplace(false);
180 MOZ_ASSERT(mShouldClip.isSome(), "Should have computed a result");
181 return *mShouldClip;
184 NS_IMETHODIMP
185 ClippedImage::GetWidth(int32_t* aWidth) {
186 if (!ShouldClip()) {
187 return InnerImage()->GetWidth(aWidth);
190 *aWidth = mClip.Width();
191 return NS_OK;
194 NS_IMETHODIMP
195 ClippedImage::GetHeight(int32_t* aHeight) {
196 if (!ShouldClip()) {
197 return InnerImage()->GetHeight(aHeight);
200 *aHeight = mClip.Height();
201 return NS_OK;
204 NS_IMETHODIMP
205 ClippedImage::GetIntrinsicSize(nsSize* aSize) {
206 if (!ShouldClip()) {
207 return InnerImage()->GetIntrinsicSize(aSize);
210 *aSize = nsSize(mClip.Width(), mClip.Height());
211 return NS_OK;
214 Maybe<AspectRatio> ClippedImage::GetIntrinsicRatio() {
215 if (!ShouldClip()) {
216 return InnerImage()->GetIntrinsicRatio();
219 return Some(AspectRatio::FromSize(mClip.Width(), mClip.Height()));
222 NS_IMETHODIMP_(already_AddRefed<SourceSurface>)
223 ClippedImage::GetFrame(uint32_t aWhichFrame, uint32_t aFlags) {
224 ImgDrawResult result;
225 RefPtr<SourceSurface> surface;
226 Tie(result, surface) =
227 GetFrameInternal(mClip.Size(), Nothing(), aWhichFrame, aFlags, 1.0);
228 return surface.forget();
231 NS_IMETHODIMP_(already_AddRefed<SourceSurface>)
232 ClippedImage::GetFrameAtSize(const IntSize& aSize, uint32_t aWhichFrame,
233 uint32_t aFlags) {
234 // XXX(seth): It'd be nice to support downscale-during-decode for this case,
235 // but right now we just fall back to the intrinsic size.
236 return GetFrame(aWhichFrame, aFlags);
239 std::pair<ImgDrawResult, RefPtr<SourceSurface>> ClippedImage::GetFrameInternal(
240 const nsIntSize& aSize, const Maybe<SVGImageContext>& aSVGContext,
241 uint32_t aWhichFrame, uint32_t aFlags, float aOpacity) {
242 if (!ShouldClip()) {
243 RefPtr<SourceSurface> surface = InnerImage()->GetFrame(aWhichFrame, aFlags);
244 return std::make_pair(
245 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 std::make_pair(ImgDrawResult::TEMPORARY_ERROR,
260 RefPtr<SourceSurface>());
263 RefPtr<gfxContext> ctx = gfxContext::CreateOrNull(target);
264 MOZ_ASSERT(ctx); // already checked the draw target above
266 // Create our callback.
267 RefPtr<DrawSingleTileCallback> drawTileCallback =
268 new DrawSingleTileCallback(this, aSize, aSVGContext, aWhichFrame,
269 aFlags, aOpacity);
270 RefPtr<gfxDrawable> drawable =
271 new gfxCallbackDrawable(drawTileCallback, aSize);
273 // Actually draw. The callback will end up invoking DrawSingleTile.
274 gfxUtils::DrawPixelSnapped(ctx, drawable, SizeDouble(aSize),
275 ImageRegion::Create(aSize),
276 SurfaceFormat::OS_RGBA, SamplingFilter::LINEAR,
277 imgIContainer::FLAG_CLAMP);
279 // Cache the resulting surface.
280 mCachedSurface = MakeUnique<ClippedImageCachedSurface>(
281 target->Snapshot(), aSize, aSVGContext, frameToDraw, aFlags,
282 drawTileCallback->GetDrawResult());
285 MOZ_ASSERT(mCachedSurface, "Should have a cached surface now");
286 RefPtr<SourceSurface> surface = mCachedSurface->Surface();
287 return std::make_pair(mCachedSurface->GetDrawResult(), std::move(surface));
290 NS_IMETHODIMP_(bool)
291 ClippedImage::IsImageContainerAvailable(LayerManager* aManager,
292 uint32_t aFlags) {
293 if (!ShouldClip()) {
294 return InnerImage()->IsImageContainerAvailable(aManager, aFlags);
296 return false;
299 NS_IMETHODIMP_(already_AddRefed<ImageContainer>)
300 ClippedImage::GetImageContainer(LayerManager* aManager, uint32_t aFlags) {
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.
307 if (!ShouldClip()) {
308 return InnerImage()->GetImageContainer(aManager, aFlags);
311 return nullptr;
314 NS_IMETHODIMP_(bool)
315 ClippedImage::IsImageContainerAvailableAtSize(LayerManager* aManager,
316 const IntSize& aSize,
317 uint32_t aFlags) {
318 if (!ShouldClip()) {
319 return InnerImage()->IsImageContainerAvailableAtSize(aManager, aSize,
320 aFlags);
322 return false;
325 NS_IMETHODIMP_(ImgDrawResult)
326 ClippedImage::GetImageContainerAtSize(layers::LayerManager* aManager,
327 const gfx::IntSize& aSize,
328 const Maybe<SVGImageContext>& aSVGContext,
329 uint32_t aFlags,
330 layers::ImageContainer** aOutContainer) {
331 // XXX(seth): We currently don't have a way of clipping the result of
332 // GetImageContainer. We work around this by always returning null, but if it
333 // ever turns out that ClippedImage is widely used on codepaths that can
334 // actually benefit from GetImageContainer, it would be a good idea to fix
335 // that method for performance reasons.
337 if (!ShouldClip()) {
338 return InnerImage()->GetImageContainerAtSize(aManager, aSize, aSVGContext,
339 aFlags, aOutContainer);
342 return ImgDrawResult::NOT_SUPPORTED;
345 static bool MustCreateSurface(gfxContext* aContext, const nsIntSize& aSize,
346 const ImageRegion& aRegion,
347 const uint32_t aFlags) {
348 gfxRect imageRect(0, 0, aSize.width, aSize.height);
349 bool willTile = !imageRect.Contains(aRegion.Rect()) &&
350 !(aFlags & imgIContainer::FLAG_CLAMP);
351 bool willResample = aContext->CurrentMatrix().HasNonIntegerTranslation() &&
352 (willTile || !aRegion.RestrictionContains(imageRect));
353 return willTile || willResample;
356 NS_IMETHODIMP_(ImgDrawResult)
357 ClippedImage::Draw(gfxContext* aContext, const nsIntSize& aSize,
358 const ImageRegion& aRegion, uint32_t aWhichFrame,
359 SamplingFilter aSamplingFilter,
360 const Maybe<SVGImageContext>& aSVGContext, uint32_t aFlags,
361 float aOpacity) {
362 if (!ShouldClip()) {
363 return InnerImage()->Draw(aContext, aSize, aRegion, aWhichFrame,
364 aSamplingFilter, aSVGContext, aFlags, aOpacity);
367 // Check for tiling. If we need to tile then we need to create a
368 // gfxCallbackDrawable to handle drawing for us.
369 if (MustCreateSurface(aContext, aSize, aRegion, aFlags)) {
370 // Create a temporary surface containing a single tile of this image.
371 // GetFrame will call DrawSingleTile internally.
372 ImgDrawResult result;
373 RefPtr<SourceSurface> surface;
374 Tie(result, surface) =
375 GetFrameInternal(aSize, aSVGContext, aWhichFrame, aFlags, aOpacity);
376 if (!surface) {
377 MOZ_ASSERT(result != ImgDrawResult::SUCCESS);
378 return result;
381 // Create a drawable from that surface.
382 RefPtr<gfxSurfaceDrawable> drawable =
383 new gfxSurfaceDrawable(surface, aSize);
385 // Draw.
386 gfxUtils::DrawPixelSnapped(aContext, drawable, SizeDouble(aSize), aRegion,
387 SurfaceFormat::OS_RGBA, aSamplingFilter,
388 aOpacity);
390 return result;
393 return DrawSingleTile(aContext, aSize, aRegion, aWhichFrame, aSamplingFilter,
394 aSVGContext, aFlags, aOpacity);
397 ImgDrawResult ClippedImage::DrawSingleTile(
398 gfxContext* aContext, const nsIntSize& aSize, const ImageRegion& aRegion,
399 uint32_t aWhichFrame, SamplingFilter aSamplingFilter,
400 const Maybe<SVGImageContext>& aSVGContext, uint32_t aFlags,
401 float aOpacity) {
402 MOZ_ASSERT(!MustCreateSurface(aContext, aSize, aRegion, aFlags),
403 "Shouldn't need to create a surface");
405 gfxRect clip(mClip.X(), mClip.Y(), mClip.Width(), mClip.Height());
406 nsIntSize size(aSize), innerSize(aSize);
407 bool needScale = false;
408 if (mSVGViewportSize && !mSVGViewportSize->IsEmpty()) {
409 innerSize = *mSVGViewportSize;
410 needScale = true;
411 } else if (NS_SUCCEEDED(InnerImage()->GetWidth(&innerSize.width)) &&
412 NS_SUCCEEDED(InnerImage()->GetHeight(&innerSize.height))) {
413 needScale = true;
414 } else {
415 MOZ_ASSERT_UNREACHABLE(
416 "If ShouldClip() led us to draw then we should never get here");
419 if (needScale) {
420 double scaleX = aSize.width / clip.Width();
421 double scaleY = aSize.height / clip.Height();
423 // Map the clip and size to the scale requested by the caller.
424 clip.Scale(scaleX, scaleY);
425 size = innerSize;
426 size.Scale(scaleX, scaleY);
429 // We restrict our drawing to only the clipping region, and translate so that
430 // the clipping region is placed at the position the caller expects.
431 ImageRegion region(aRegion);
432 region.MoveBy(clip.X(), clip.Y());
433 region = region.Intersect(clip);
435 gfxContextMatrixAutoSaveRestore saveMatrix(aContext);
436 aContext->Multiply(gfxMatrix::Translation(-clip.X(), -clip.Y()));
438 auto unclipViewport = [&](const SVGImageContext& aOldContext) {
439 // Map the viewport to the inner image. Note that we don't take the aSize
440 // parameter of imgIContainer::Draw into account, just the clipping region.
441 // The size in pixels at which the output will ultimately be drawn is
442 // irrelevant here since the purpose of the SVG viewport size is to
443 // determine what *region* of the SVG document will be drawn.
444 SVGImageContext context(aOldContext);
445 auto oldViewport = aOldContext.GetViewportSize();
446 if (oldViewport) {
447 CSSIntSize newViewport;
448 newViewport.width =
449 ceil(oldViewport->width * double(innerSize.width) / mClip.Width());
450 newViewport.height =
451 ceil(oldViewport->height * double(innerSize.height) / mClip.Height());
452 context.SetViewportSize(Some(newViewport));
454 return context;
457 return InnerImage()->Draw(aContext, size, region, aWhichFrame,
458 aSamplingFilter, aSVGContext.map(unclipViewport),
459 aFlags, aOpacity);
462 NS_IMETHODIMP
463 ClippedImage::RequestDiscard() {
464 // We're very aggressive about discarding.
465 mCachedSurface = nullptr;
467 return InnerImage()->RequestDiscard();
470 NS_IMETHODIMP_(Orientation)
471 ClippedImage::GetOrientation() {
472 // XXX(seth): This should not actually be here; this is just to work around a
473 // what appears to be a bug in MSVC's linker.
474 return InnerImage()->GetOrientation();
477 nsIntSize ClippedImage::OptimalImageSizeForDest(const gfxSize& aDest,
478 uint32_t aWhichFrame,
479 SamplingFilter aSamplingFilter,
480 uint32_t aFlags) {
481 if (!ShouldClip()) {
482 return InnerImage()->OptimalImageSizeForDest(aDest, aWhichFrame,
483 aSamplingFilter, aFlags);
486 int32_t imgWidth, imgHeight;
487 bool needScale = false;
488 bool forceUniformScaling = false;
489 if (mSVGViewportSize && !mSVGViewportSize->IsEmpty()) {
490 imgWidth = mSVGViewportSize->width;
491 imgHeight = mSVGViewportSize->height;
492 needScale = true;
493 forceUniformScaling = (aFlags & imgIContainer::FLAG_FORCE_UNIFORM_SCALING);
494 } else if (NS_SUCCEEDED(InnerImage()->GetWidth(&imgWidth)) &&
495 NS_SUCCEEDED(InnerImage()->GetHeight(&imgHeight))) {
496 needScale = true;
499 if (needScale) {
500 // To avoid ugly sampling artifacts, ClippedImage needs the image size to
501 // be chosen such that the clipping region lies on pixel boundaries.
503 // First, we select a scale that's good for ClippedImage. An integer
504 // multiple of the size of the clipping region is always fine.
505 IntSize scale = IntSize::Ceil(aDest.width / mClip.Width(),
506 aDest.height / mClip.Height());
508 if (forceUniformScaling) {
509 scale.width = scale.height = max(scale.height, scale.width);
512 // Determine the size we'd prefer to render the inner image at, and ask the
513 // inner image what size we should actually use.
514 gfxSize desiredSize(double(imgWidth) * scale.width,
515 double(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