2013-04-06 01:14:32 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2016-06-30 20:27:03 +03:00
|
|
|
#include "ClippedImage.h"
|
|
|
|
|
2016-07-01 03:27:00 +03:00
|
|
|
#include <algorithm>
|
2014-08-23 00:12:38 +04:00
|
|
|
#include <new> // Workaround for bug in VS10; see bug 981264.
|
2014-07-29 01:27:40 +04:00
|
|
|
#include <cmath>
|
2014-08-23 00:12:38 +04:00
|
|
|
#include <utility>
|
2014-07-29 01:27:40 +04:00
|
|
|
|
2013-04-06 01:14:32 +04:00
|
|
|
#include "gfxDrawable.h"
|
|
|
|
#include "gfxPlatform.h"
|
|
|
|
#include "gfxUtils.h"
|
2014-04-15 22:02:23 +04:00
|
|
|
#include "mozilla/gfx/2D.h"
|
2015-10-01 21:31:41 +03:00
|
|
|
#include "mozilla/Move.h"
|
2015-10-18 08:24:48 +03:00
|
|
|
#include "mozilla/RefPtr.h"
|
2015-10-01 21:31:41 +03:00
|
|
|
#include "mozilla/Pair.h"
|
|
|
|
#include "mozilla/Tuple.h"
|
2013-04-06 01:14:32 +04:00
|
|
|
|
2014-08-23 00:12:38 +04:00
|
|
|
#include "ImageRegion.h"
|
2013-08-25 11:19:42 +04:00
|
|
|
#include "Orientation.h"
|
2013-09-07 17:01:08 +04:00
|
|
|
#include "SVGImageContext.h"
|
2013-04-06 01:14:32 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
2014-07-10 19:00:31 +04:00
|
|
|
|
|
|
|
using namespace gfx;
|
|
|
|
using layers::ImageContainer;
|
|
|
|
using layers::LayerManager;
|
2014-08-23 00:12:38 +04:00
|
|
|
using std::make_pair;
|
2016-07-01 03:27:00 +03:00
|
|
|
using std::max;
|
2014-07-29 01:27:40 +04:00
|
|
|
using std::modf;
|
2014-08-23 00:12:38 +04:00
|
|
|
using std::pair;
|
2014-07-10 19:00:31 +04:00
|
|
|
|
2013-04-06 01:14:32 +04:00
|
|
|
namespace image {
|
|
|
|
|
2013-04-26 02:58:32 +04:00
|
|
|
class ClippedImageCachedSurface {
|
|
|
|
public:
|
2015-06-17 17:00:52 +03:00
|
|
|
ClippedImageCachedSurface(already_AddRefed<SourceSurface> aSurface,
|
2014-08-23 00:12:38 +04:00
|
|
|
const nsIntSize& aSize,
|
|
|
|
const Maybe<SVGImageContext>& aSVGContext,
|
2015-10-01 21:31:41 +03:00
|
|
|
float aFrame, uint32_t aFlags,
|
2017-12-11 18:37:59 +03:00
|
|
|
ImgDrawResult aDrawResult)
|
2013-04-26 02:58:32 +04:00
|
|
|
: mSurface(aSurface),
|
2014-08-23 00:12:38 +04:00
|
|
|
mSize(aSize),
|
2015-10-01 21:31:41 +03:00
|
|
|
mSVGContext(aSVGContext),
|
2013-04-26 02:58:32 +04:00
|
|
|
mFrame(aFrame),
|
|
|
|
mFlags(aFlags),
|
2015-10-01 21:31:41 +03:00
|
|
|
mDrawResult(aDrawResult) {
|
2013-04-26 02:58:32 +04:00
|
|
|
MOZ_ASSERT(mSurface, "Must have a valid surface");
|
|
|
|
}
|
|
|
|
|
2014-08-23 00:12:38 +04:00
|
|
|
bool Matches(const nsIntSize& aSize,
|
|
|
|
const Maybe<SVGImageContext>& aSVGContext, float aFrame,
|
2015-10-01 21:31:41 +03:00
|
|
|
uint32_t aFlags) const {
|
2014-08-23 00:12:38 +04:00
|
|
|
return mSize == aSize && mSVGContext == aSVGContext && mFrame == aFrame &&
|
2013-04-26 02:58:32 +04:00
|
|
|
mFlags == aFlags;
|
|
|
|
}
|
|
|
|
|
2015-10-01 21:31:41 +03:00
|
|
|
already_AddRefed<SourceSurface> Surface() const {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<SourceSurface> surf(mSurface);
|
2015-05-01 16:14:16 +03:00
|
|
|
return surf.forget();
|
2013-05-07 21:47:42 +04:00
|
|
|
}
|
2013-04-26 02:58:32 +04:00
|
|
|
|
2017-12-11 18:37:59 +03:00
|
|
|
ImgDrawResult GetDrawResult() const { return mDrawResult; }
|
2015-10-01 21:31:41 +03:00
|
|
|
|
|
|
|
bool NeedsRedraw() const {
|
2017-12-11 18:37:59 +03:00
|
|
|
return mDrawResult != ImgDrawResult::SUCCESS &&
|
|
|
|
mDrawResult != ImgDrawResult::BAD_IMAGE;
|
2015-10-01 21:31:41 +03:00
|
|
|
}
|
|
|
|
|
2013-04-26 02:58:32 +04:00
|
|
|
private:
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<SourceSurface> mSurface;
|
2014-08-23 00:12:38 +04:00
|
|
|
const nsIntSize mSize;
|
|
|
|
Maybe<SVGImageContext> mSVGContext;
|
|
|
|
const float mFrame;
|
|
|
|
const uint32_t mFlags;
|
2017-12-11 18:37:59 +03:00
|
|
|
const ImgDrawResult mDrawResult;
|
2013-04-26 02:58:32 +04:00
|
|
|
};
|
|
|
|
|
2013-04-06 01:14:32 +04:00
|
|
|
class DrawSingleTileCallback : public gfxDrawingCallback {
|
|
|
|
public:
|
|
|
|
DrawSingleTileCallback(ClippedImage* aImage, const nsIntSize& aSize,
|
2014-08-23 00:12:38 +04:00
|
|
|
const Maybe<SVGImageContext>& aSVGContext,
|
2013-04-06 01:14:32 +04:00
|
|
|
uint32_t aWhichFrame, uint32_t aFlags, float aOpacity)
|
|
|
|
: mImage(aImage),
|
2014-08-23 00:12:38 +04:00
|
|
|
mSize(aSize),
|
2013-04-06 01:14:32 +04:00
|
|
|
mSVGContext(aSVGContext),
|
|
|
|
mWhichFrame(aWhichFrame),
|
|
|
|
mFlags(aFlags),
|
2017-12-11 18:37:59 +03:00
|
|
|
mDrawResult(ImgDrawResult::NOT_READY),
|
2017-01-03 08:53:22 +03:00
|
|
|
mOpacity(aOpacity) {
|
2013-04-06 01:14:32 +04:00
|
|
|
MOZ_ASSERT(mImage, "Must have an image to clip");
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool operator()(gfxContext* aContext, const gfxRect& aFillRect,
|
2016-05-25 19:01:18 +03:00
|
|
|
const SamplingFilter aSamplingFilter,
|
2017-11-06 06:37:28 +03:00
|
|
|
const gfxMatrix& aTransform) override {
|
2014-08-23 00:12:38 +04:00
|
|
|
MOZ_ASSERT(aTransform.IsIdentity(),
|
|
|
|
"Caller is probably CreateSamplingRestrictedDrawable, "
|
|
|
|
"which should not happen");
|
|
|
|
|
2013-04-06 01:14:32 +04:00
|
|
|
// Draw the image. |gfxCallbackDrawable| always calls this function with
|
|
|
|
// arguments that guarantee we never tile.
|
2015-10-01 21:31:41 +03:00
|
|
|
mDrawResult = mImage->DrawSingleTile(
|
|
|
|
aContext, mSize, ImageRegion::Create(aFillRect), mWhichFrame,
|
2017-01-03 08:53:22 +03:00
|
|
|
aSamplingFilter, mSVGContext, mFlags, mOpacity);
|
2013-04-06 01:14:32 +04:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-12-11 18:37:59 +03:00
|
|
|
ImgDrawResult GetDrawResult() { return mDrawResult; }
|
2015-10-01 21:31:41 +03:00
|
|
|
|
2013-04-06 01:14:32 +04:00
|
|
|
private:
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ClippedImage> mImage;
|
2014-08-23 00:12:38 +04:00
|
|
|
const nsIntSize mSize;
|
|
|
|
const Maybe<SVGImageContext>& mSVGContext;
|
|
|
|
const uint32_t mWhichFrame;
|
|
|
|
const uint32_t mFlags;
|
2017-12-11 18:37:59 +03:00
|
|
|
ImgDrawResult mDrawResult;
|
2017-01-03 08:53:22 +03:00
|
|
|
float mOpacity;
|
2013-04-06 01:14:32 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
ClippedImage::ClippedImage(Image* aImage, nsIntRect aClip,
|
2016-03-08 10:54:13 +03:00
|
|
|
const Maybe<nsSize>& aSVGViewportSize)
|
2013-04-06 01:14:32 +04:00
|
|
|
: ImageWrapper(aImage), mClip(aClip) {
|
|
|
|
MOZ_ASSERT(aImage != nullptr, "ClippedImage requires an existing Image");
|
2016-03-08 10:54:13 +03:00
|
|
|
MOZ_ASSERT_IF(aSVGViewportSize,
|
|
|
|
aImage->GetType() == imgIContainer::TYPE_VECTOR);
|
|
|
|
if (aSVGViewportSize) {
|
|
|
|
mSVGViewportSize =
|
|
|
|
Some(aSVGViewportSize->ToNearestPixels(AppUnitsPerCSSPixel()));
|
|
|
|
}
|
2013-04-06 01:14:32 +04:00
|
|
|
}
|
|
|
|
|
2013-04-26 02:58:32 +04:00
|
|
|
ClippedImage::~ClippedImage() {}
|
|
|
|
|
2013-04-06 01:14:32 +04:00
|
|
|
bool ClippedImage::ShouldClip() {
|
2015-04-04 22:57:00 +03:00
|
|
|
// We need to evaluate the clipping region against the image's width and
|
|
|
|
// height once they're available to determine if it's valid and whether we
|
|
|
|
// actually need to do any work. We may fail if the image's width and height
|
|
|
|
// aren't available yet, in which case we'll try again later.
|
2014-08-14 02:39:41 +04:00
|
|
|
if (mShouldClip.isNothing()) {
|
2013-04-06 01:14:32 +04:00
|
|
|
int32_t width, height;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ProgressTracker> progressTracker =
|
2014-11-15 07:10:47 +03:00
|
|
|
InnerImage()->GetProgressTracker();
|
2013-04-06 01:14:32 +04:00
|
|
|
if (InnerImage()->HasError()) {
|
2015-04-04 22:57:00 +03:00
|
|
|
// If there's a problem with the inner image we'll let it handle
|
|
|
|
// everything.
|
2014-08-14 02:39:41 +04:00
|
|
|
mShouldClip.emplace(false);
|
2016-03-08 10:54:13 +03:00
|
|
|
} else if (mSVGViewportSize && !mSVGViewportSize->IsEmpty()) {
|
|
|
|
// Clamp the clipping region to the size of the SVG viewport.
|
|
|
|
nsIntRect svgViewportRect(nsIntPoint(0, 0), *mSVGViewportSize);
|
|
|
|
|
|
|
|
mClip = mClip.Intersect(svgViewportRect);
|
|
|
|
|
|
|
|
// If the clipping region is the same size as the SVG viewport size
|
|
|
|
// we don't have to do anything.
|
|
|
|
mShouldClip.emplace(!mClip.IsEqualInterior(svgViewportRect));
|
2013-04-06 01:14:32 +04:00
|
|
|
} else if (NS_SUCCEEDED(InnerImage()->GetWidth(&width)) && width > 0 &&
|
|
|
|
NS_SUCCEEDED(InnerImage()->GetHeight(&height)) && height > 0) {
|
|
|
|
// Clamp the clipping region to the size of the underlying image.
|
|
|
|
mClip = mClip.Intersect(nsIntRect(0, 0, width, height));
|
|
|
|
|
|
|
|
// If the clipping region is the same size as the underlying image we
|
|
|
|
// don't have to do anything.
|
2015-04-04 22:57:00 +03:00
|
|
|
mShouldClip.emplace(
|
|
|
|
!mClip.IsEqualInterior(nsIntRect(0, 0, width, height)));
|
2015-01-25 10:16:46 +03:00
|
|
|
} else if (progressTracker &&
|
|
|
|
!(progressTracker->GetProgress() & FLAG_LOAD_COMPLETE)) {
|
2013-04-06 01:14:32 +04:00
|
|
|
// The image just hasn't finished loading yet. We don't yet know whether
|
2015-04-04 22:57:00 +03:00
|
|
|
// clipping with be needed or not for now. Just return without memorizing
|
2013-04-06 01:14:32 +04:00
|
|
|
// anything.
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
// We have a fully loaded image without a clearly defined width and
|
|
|
|
// height. This can happen with SVG images.
|
2014-08-14 02:39:41 +04:00
|
|
|
mShouldClip.emplace(false);
|
2013-04-06 01:14:32 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-14 02:39:41 +04:00
|
|
|
MOZ_ASSERT(mShouldClip.isSome(), "Should have computed a result");
|
|
|
|
return *mShouldClip;
|
2013-04-06 01:14:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
ClippedImage::GetWidth(int32_t* aWidth) {
|
|
|
|
if (!ShouldClip()) {
|
|
|
|
return InnerImage()->GetWidth(aWidth);
|
|
|
|
}
|
|
|
|
|
2017-08-14 15:29:56 +03:00
|
|
|
*aWidth = mClip.Width();
|
2013-04-06 01:14:32 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
ClippedImage::GetHeight(int32_t* aHeight) {
|
|
|
|
if (!ShouldClip()) {
|
|
|
|
return InnerImage()->GetHeight(aHeight);
|
|
|
|
}
|
|
|
|
|
2017-08-14 15:29:56 +03:00
|
|
|
*aHeight = mClip.Height();
|
2013-04-06 01:14:32 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
ClippedImage::GetIntrinsicSize(nsSize* aSize) {
|
|
|
|
if (!ShouldClip()) {
|
|
|
|
return InnerImage()->GetIntrinsicSize(aSize);
|
|
|
|
}
|
|
|
|
|
2017-08-14 15:29:56 +03:00
|
|
|
*aSize = nsSize(mClip.Width(), mClip.Height());
|
2013-04-06 01:14:32 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-05-03 02:28:21 +03:00
|
|
|
Maybe<AspectRatio> ClippedImage::GetIntrinsicRatio() {
|
2013-04-06 01:14:32 +04:00
|
|
|
if (!ShouldClip()) {
|
2019-05-03 02:28:21 +03:00
|
|
|
return InnerImage()->GetIntrinsicRatio();
|
2013-04-06 01:14:32 +04:00
|
|
|
}
|
|
|
|
|
2019-05-03 02:28:21 +03:00
|
|
|
return Some(AspectRatio::FromSize(mClip.Width(), mClip.Height()));
|
2013-04-06 01:14:32 +04:00
|
|
|
}
|
|
|
|
|
2015-06-17 17:00:52 +03:00
|
|
|
NS_IMETHODIMP_(already_AddRefed<SourceSurface>)
|
2013-04-06 01:14:32 +04:00
|
|
|
ClippedImage::GetFrame(uint32_t aWhichFrame, uint32_t aFlags) {
|
2017-12-11 18:37:59 +03:00
|
|
|
ImgDrawResult result;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<SourceSurface> surface;
|
2017-01-03 08:53:22 +03:00
|
|
|
Tie(result, surface) =
|
|
|
|
GetFrameInternal(mClip.Size(), Nothing(), aWhichFrame, aFlags, 1.0);
|
2015-10-01 21:31:41 +03:00
|
|
|
return surface.forget();
|
2013-04-06 01:14:32 +04:00
|
|
|
}
|
|
|
|
|
2015-09-19 23:34:09 +03:00
|
|
|
NS_IMETHODIMP_(already_AddRefed<SourceSurface>)
|
|
|
|
ClippedImage::GetFrameAtSize(const IntSize& aSize, uint32_t aWhichFrame,
|
|
|
|
uint32_t aFlags) {
|
|
|
|
// XXX(seth): It'd be nice to support downscale-during-decode for this case,
|
|
|
|
// but right now we just fall back to the intrinsic size.
|
|
|
|
return GetFrame(aWhichFrame, aFlags);
|
|
|
|
}
|
|
|
|
|
2017-12-11 18:37:59 +03:00
|
|
|
Pair<ImgDrawResult, RefPtr<SourceSurface>> ClippedImage::GetFrameInternal(
|
2014-08-23 00:12:38 +04:00
|
|
|
const nsIntSize& aSize, const Maybe<SVGImageContext>& aSVGContext,
|
2013-04-06 01:14:32 +04:00
|
|
|
uint32_t aWhichFrame, uint32_t aFlags, float aOpacity) {
|
|
|
|
if (!ShouldClip()) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<SourceSurface> surface = InnerImage()->GetFrame(aWhichFrame, aFlags);
|
2017-12-11 18:37:59 +03:00
|
|
|
return MakePair(surface ? ImgDrawResult::SUCCESS : ImgDrawResult::NOT_READY,
|
2018-05-30 22:15:35 +03:00
|
|
|
std::move(surface));
|
2013-04-06 01:14:32 +04:00
|
|
|
}
|
|
|
|
|
2013-04-26 02:58:32 +04:00
|
|
|
float frameToDraw = InnerImage()->GetFrameIndex(aWhichFrame);
|
2015-10-01 21:31:41 +03:00
|
|
|
if (!mCachedSurface ||
|
|
|
|
!mCachedSurface->Matches(aSize, aSVGContext, frameToDraw, aFlags) ||
|
|
|
|
mCachedSurface->NeedsRedraw()) {
|
2013-04-26 02:58:32 +04:00
|
|
|
// Create a surface to draw into.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<DrawTarget> target =
|
2014-08-23 00:12:38 +04:00
|
|
|
gfxPlatform::GetPlatform()->CreateOffscreenContentDrawTarget(
|
|
|
|
IntSize(aSize.width, aSize.height), SurfaceFormat::B8G8R8A8);
|
2016-04-12 22:18:11 +03:00
|
|
|
if (!target || !target->IsValid()) {
|
2014-05-31 14:26:04 +04:00
|
|
|
NS_ERROR("Could not create a DrawTarget");
|
2017-12-11 18:37:59 +03:00
|
|
|
return MakePair(ImgDrawResult::TEMPORARY_ERROR, RefPtr<SourceSurface>());
|
2014-05-31 14:26:04 +04:00
|
|
|
}
|
2013-10-16 05:00:30 +04:00
|
|
|
|
2016-06-07 02:39:56 +03:00
|
|
|
RefPtr<gfxContext> ctx = gfxContext::CreateOrNull(target);
|
2016-04-12 22:18:11 +03:00
|
|
|
MOZ_ASSERT(ctx); // already checked the draw target above
|
2013-04-26 02:58:32 +04:00
|
|
|
|
|
|
|
// Create our callback.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<DrawSingleTileCallback> drawTileCallback =
|
2017-01-03 08:53:22 +03:00
|
|
|
new DrawSingleTileCallback(this, aSize, aSVGContext, aWhichFrame,
|
|
|
|
aFlags, aOpacity);
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<gfxDrawable> drawable =
|
2014-08-23 00:12:38 +04:00
|
|
|
new gfxCallbackDrawable(drawTileCallback, aSize);
|
2013-04-26 02:58:32 +04:00
|
|
|
|
|
|
|
// Actually draw. The callback will end up invoking DrawSingleTile.
|
2017-06-28 18:41:21 +03:00
|
|
|
gfxUtils::DrawPixelSnapped(ctx, drawable, SizeDouble(aSize),
|
2014-08-23 00:12:38 +04:00
|
|
|
ImageRegion::Create(aSize),
|
2014-04-20 05:28:38 +04:00
|
|
|
SurfaceFormat::B8G8R8A8, SamplingFilter::LINEAR,
|
2014-08-23 00:12:38 +04:00
|
|
|
imgIContainer::FLAG_CLAMP);
|
2013-04-26 02:58:32 +04:00
|
|
|
|
|
|
|
// Cache the resulting surface.
|
2015-10-17 13:53:28 +03:00
|
|
|
mCachedSurface = MakeUnique<ClippedImageCachedSurface>(
|
|
|
|
target->Snapshot(), aSize, aSVGContext, frameToDraw, aFlags,
|
|
|
|
drawTileCallback->GetDrawResult());
|
2013-04-26 02:58:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(mCachedSurface, "Should have a cached surface now");
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<SourceSurface> surface = mCachedSurface->Surface();
|
2018-05-30 22:15:35 +03:00
|
|
|
return MakePair(mCachedSurface->GetDrawResult(), std::move(surface));
|
2013-04-06 01:14:32 +04:00
|
|
|
}
|
|
|
|
|
2015-05-13 10:23:44 +03:00
|
|
|
NS_IMETHODIMP_(bool)
|
|
|
|
ClippedImage::IsImageContainerAvailable(LayerManager* aManager,
|
|
|
|
uint32_t aFlags) {
|
|
|
|
if (!ShouldClip()) {
|
|
|
|
return InnerImage()->IsImageContainerAvailable(aManager, aFlags);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-03-18 05:40:16 +03:00
|
|
|
NS_IMETHODIMP_(already_AddRefed<ImageContainer>)
|
|
|
|
ClippedImage::GetImageContainer(LayerManager* aManager, uint32_t aFlags) {
|
2013-04-06 01:14:32 +04:00
|
|
|
// XXX(seth): We currently don't have a way of clipping the result of
|
|
|
|
// GetImageContainer. We work around this by always returning null, but if it
|
|
|
|
// ever turns out that ClippedImage is widely used on codepaths that can
|
|
|
|
// actually benefit from GetImageContainer, it would be a good idea to fix
|
|
|
|
// that method for performance reasons.
|
|
|
|
|
2013-04-26 02:58:32 +04:00
|
|
|
if (!ShouldClip()) {
|
2015-03-18 05:40:16 +03:00
|
|
|
return InnerImage()->GetImageContainer(aManager, aFlags);
|
2013-04-26 02:58:32 +04:00
|
|
|
}
|
|
|
|
|
2015-05-13 10:23:44 +03:00
|
|
|
return nullptr;
|
2013-04-06 01:14:32 +04:00
|
|
|
}
|
|
|
|
|
2017-11-17 14:45:26 +03:00
|
|
|
NS_IMETHODIMP_(bool)
|
|
|
|
ClippedImage::IsImageContainerAvailableAtSize(LayerManager* aManager,
|
|
|
|
const IntSize& aSize,
|
|
|
|
uint32_t aFlags) {
|
|
|
|
if (!ShouldClip()) {
|
|
|
|
return InnerImage()->IsImageContainerAvailableAtSize(aManager, aSize,
|
|
|
|
aFlags);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-09-14 02:48:21 +03:00
|
|
|
NS_IMETHODIMP_(ImgDrawResult)
|
|
|
|
ClippedImage::GetImageContainerAtSize(layers::LayerManager* aManager,
|
|
|
|
const gfx::IntSize& aSize,
|
2017-11-17 22:08:52 +03:00
|
|
|
const Maybe<SVGImageContext>& aSVGContext,
|
2018-09-14 02:48:21 +03:00
|
|
|
uint32_t aFlags,
|
|
|
|
layers::ImageContainer** aOutContainer) {
|
2017-11-17 14:45:26 +03:00
|
|
|
// XXX(seth): We currently don't have a way of clipping the result of
|
|
|
|
// GetImageContainer. We work around this by always returning null, but if it
|
|
|
|
// ever turns out that ClippedImage is widely used on codepaths that can
|
|
|
|
// actually benefit from GetImageContainer, it would be a good idea to fix
|
|
|
|
// that method for performance reasons.
|
|
|
|
|
|
|
|
if (!ShouldClip()) {
|
2018-09-14 02:48:21 +03:00
|
|
|
return InnerImage()->GetImageContainerAtSize(aManager, aSize, aSVGContext,
|
|
|
|
aFlags, aOutContainer);
|
2017-11-17 14:45:26 +03:00
|
|
|
}
|
|
|
|
|
2018-09-14 02:48:21 +03:00
|
|
|
return ImgDrawResult::NOT_SUPPORTED;
|
2017-11-17 14:45:26 +03:00
|
|
|
}
|
|
|
|
|
2014-08-23 00:12:38 +04:00
|
|
|
static bool MustCreateSurface(gfxContext* aContext, const nsIntSize& aSize,
|
|
|
|
const ImageRegion& aRegion,
|
|
|
|
const uint32_t aFlags) {
|
|
|
|
gfxRect imageRect(0, 0, aSize.width, aSize.height);
|
|
|
|
bool willTile = !imageRect.Contains(aRegion.Rect()) &&
|
2013-04-06 01:14:32 +04:00
|
|
|
!(aFlags & imgIContainer::FLAG_CLAMP);
|
2014-08-23 00:12:38 +04:00
|
|
|
bool willResample = aContext->CurrentMatrix().HasNonIntegerTranslation() &&
|
|
|
|
(willTile || !aRegion.RestrictionContains(imageRect));
|
2013-04-06 01:14:32 +04:00
|
|
|
return willTile || willResample;
|
|
|
|
}
|
|
|
|
|
2017-12-11 18:37:59 +03:00
|
|
|
NS_IMETHODIMP_(ImgDrawResult)
|
2013-04-06 01:14:32 +04:00
|
|
|
ClippedImage::Draw(gfxContext* aContext, const nsIntSize& aSize,
|
2014-08-23 00:12:38 +04:00
|
|
|
const ImageRegion& aRegion, uint32_t aWhichFrame,
|
2016-05-25 19:01:18 +03:00
|
|
|
SamplingFilter aSamplingFilter,
|
2014-08-23 00:12:38 +04:00
|
|
|
const Maybe<SVGImageContext>& aSVGContext, uint32_t aFlags,
|
2017-01-03 08:53:22 +03:00
|
|
|
float aOpacity) {
|
2013-04-06 01:14:32 +04:00
|
|
|
if (!ShouldClip()) {
|
2014-08-23 00:12:38 +04:00
|
|
|
return InnerImage()->Draw(aContext, aSize, aRegion, aWhichFrame,
|
2017-01-03 08:53:22 +03:00
|
|
|
aSamplingFilter, aSVGContext, aFlags, aOpacity);
|
2013-04-06 01:14:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check for tiling. If we need to tile then we need to create a
|
|
|
|
// gfxCallbackDrawable to handle drawing for us.
|
2014-08-23 00:12:38 +04:00
|
|
|
if (MustCreateSurface(aContext, aSize, aRegion, aFlags)) {
|
2013-04-06 01:14:32 +04:00
|
|
|
// Create a temporary surface containing a single tile of this image.
|
|
|
|
// GetFrame will call DrawSingleTile internally.
|
2017-12-11 18:37:59 +03:00
|
|
|
ImgDrawResult result;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<SourceSurface> surface;
|
2015-10-01 21:31:41 +03:00
|
|
|
Tie(result, surface) =
|
2017-01-03 08:53:22 +03:00
|
|
|
GetFrameInternal(aSize, aSVGContext, aWhichFrame, aFlags, aOpacity);
|
2015-02-05 00:50:56 +03:00
|
|
|
if (!surface) {
|
2017-12-11 18:37:59 +03:00
|
|
|
MOZ_ASSERT(result != ImgDrawResult::SUCCESS);
|
2015-10-01 21:31:41 +03:00
|
|
|
return result;
|
2015-02-05 00:50:56 +03:00
|
|
|
}
|
2013-04-06 01:14:32 +04:00
|
|
|
|
|
|
|
// Create a drawable from that surface.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<gfxSurfaceDrawable> drawable =
|
2014-08-23 00:12:38 +04:00
|
|
|
new gfxSurfaceDrawable(surface, aSize);
|
2013-04-06 01:14:32 +04:00
|
|
|
|
|
|
|
// Draw.
|
2017-06-28 18:41:21 +03:00
|
|
|
gfxUtils::DrawPixelSnapped(aContext, drawable, SizeDouble(aSize), aRegion,
|
2017-01-03 08:53:22 +03:00
|
|
|
SurfaceFormat::B8G8R8A8, aSamplingFilter,
|
|
|
|
aOpacity);
|
2013-04-06 01:14:32 +04:00
|
|
|
|
2015-10-01 21:31:41 +03:00
|
|
|
return result;
|
2013-04-06 01:14:32 +04:00
|
|
|
}
|
|
|
|
|
2014-08-23 00:12:38 +04:00
|
|
|
return DrawSingleTile(aContext, aSize, aRegion, aWhichFrame, aSamplingFilter,
|
2017-01-03 08:53:22 +03:00
|
|
|
aSVGContext, aFlags, aOpacity);
|
2013-04-06 01:14:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
ImgDrawResult ClippedImage::DrawSingleTile(
|
2014-08-23 00:12:38 +04:00
|
|
|
gfxContext* aContext, const nsIntSize& aSize, const ImageRegion& aRegion,
|
2016-05-25 19:01:18 +03:00
|
|
|
uint32_t aWhichFrame, SamplingFilter aSamplingFilter,
|
2014-08-23 00:12:38 +04:00
|
|
|
const Maybe<SVGImageContext>& aSVGContext, uint32_t aFlags,
|
2017-01-03 08:53:22 +03:00
|
|
|
float aOpacity) {
|
2014-08-23 00:12:38 +04:00
|
|
|
MOZ_ASSERT(!MustCreateSurface(aContext, aSize, aRegion, aFlags),
|
|
|
|
"Shouldn't need to create a surface");
|
|
|
|
|
2017-12-21 00:46:28 +03:00
|
|
|
gfxRect clip(mClip.X(), mClip.Y(), mClip.Width(), mClip.Height());
|
2014-08-23 00:12:38 +04:00
|
|
|
nsIntSize size(aSize), innerSize(aSize);
|
2016-03-08 10:54:13 +03:00
|
|
|
bool needScale = false;
|
|
|
|
if (mSVGViewportSize && !mSVGViewportSize->IsEmpty()) {
|
|
|
|
innerSize = *mSVGViewportSize;
|
|
|
|
needScale = true;
|
|
|
|
} else if (NS_SUCCEEDED(InnerImage()->GetWidth(&innerSize.width)) &&
|
|
|
|
NS_SUCCEEDED(InnerImage()->GetHeight(&innerSize.height))) {
|
|
|
|
needScale = true;
|
|
|
|
} else {
|
|
|
|
MOZ_ASSERT_UNREACHABLE(
|
|
|
|
"If ShouldClip() led us to draw then we should never get here");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (needScale) {
|
2017-08-14 15:29:56 +03:00
|
|
|
double scaleX = aSize.width / clip.Width();
|
|
|
|
double scaleY = aSize.height / clip.Height();
|
2015-04-04 22:57:00 +03:00
|
|
|
|
2014-08-23 00:12:38 +04:00
|
|
|
// Map the clip and size to the scale requested by the caller.
|
|
|
|
clip.Scale(scaleX, scaleY);
|
|
|
|
size = innerSize;
|
|
|
|
size.Scale(scaleX, scaleY);
|
2013-04-06 01:14:32 +04:00
|
|
|
}
|
|
|
|
|
2014-08-23 00:12:38 +04:00
|
|
|
// We restrict our drawing to only the clipping region, and translate so that
|
|
|
|
// the clipping region is placed at the position the caller expects.
|
|
|
|
ImageRegion region(aRegion);
|
2017-12-21 00:46:28 +03:00
|
|
|
region.MoveBy(clip.X(), clip.Y());
|
2014-08-23 00:12:38 +04:00
|
|
|
region = region.Intersect(clip);
|
|
|
|
|
|
|
|
gfxContextMatrixAutoSaveRestore saveMatrix(aContext);
|
2017-12-21 00:46:28 +03:00
|
|
|
aContext->Multiply(gfxMatrix::Translation(-clip.X(), -clip.Y()));
|
2013-04-06 01:14:32 +04:00
|
|
|
|
2016-06-22 23:34:07 +03:00
|
|
|
auto unclipViewport = [&](const SVGImageContext& aOldContext) {
|
|
|
|
// Map the viewport to the inner image. Note that we don't take the aSize
|
|
|
|
// parameter of imgIContainer::Draw into account, just the clipping region.
|
|
|
|
// The size in pixels at which the output will ultimately be drawn is
|
|
|
|
// irrelevant here since the purpose of the SVG viewport size is to
|
|
|
|
// determine what *region* of the SVG document will be drawn.
|
2017-01-09 22:12:02 +03:00
|
|
|
SVGImageContext context(aOldContext);
|
2017-01-30 16:25:00 +03:00
|
|
|
auto oldViewport = aOldContext.GetViewportSize();
|
|
|
|
if (oldViewport) {
|
|
|
|
CSSIntSize newViewport;
|
|
|
|
newViewport.width =
|
2017-08-14 15:29:56 +03:00
|
|
|
ceil(oldViewport->width * double(innerSize.width) / mClip.Width());
|
2017-01-30 16:25:00 +03:00
|
|
|
newViewport.height =
|
2017-08-14 15:29:56 +03:00
|
|
|
ceil(oldViewport->height * double(innerSize.height) / mClip.Height());
|
2017-01-30 16:25:00 +03:00
|
|
|
context.SetViewportSize(Some(newViewport));
|
|
|
|
}
|
2017-01-09 22:12:02 +03:00
|
|
|
return context;
|
2016-06-22 23:34:07 +03:00
|
|
|
};
|
|
|
|
|
2014-08-23 00:12:38 +04:00
|
|
|
return InnerImage()->Draw(aContext, size, region, aWhichFrame,
|
2016-06-22 23:34:07 +03:00
|
|
|
aSamplingFilter, aSVGContext.map(unclipViewport),
|
2017-01-03 08:53:22 +03:00
|
|
|
aFlags, aOpacity);
|
2013-04-06 01:14:32 +04:00
|
|
|
}
|
|
|
|
|
2013-04-26 02:58:32 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
ClippedImage::RequestDiscard() {
|
|
|
|
// We're very aggressive about discarding.
|
|
|
|
mCachedSurface = nullptr;
|
|
|
|
|
|
|
|
return InnerImage()->RequestDiscard();
|
|
|
|
}
|
|
|
|
|
2013-08-25 11:19:42 +04:00
|
|
|
NS_IMETHODIMP_(Orientation)
|
|
|
|
ClippedImage::GetOrientation() {
|
|
|
|
// XXX(seth): This should not actually be here; this is just to work around a
|
|
|
|
// what appears to be a bug in MSVC's linker.
|
|
|
|
return InnerImage()->GetOrientation();
|
|
|
|
}
|
|
|
|
|
2015-04-04 22:57:00 +03:00
|
|
|
nsIntSize ClippedImage::OptimalImageSizeForDest(const gfxSize& aDest,
|
|
|
|
uint32_t aWhichFrame,
|
2016-05-25 19:01:18 +03:00
|
|
|
SamplingFilter aSamplingFilter,
|
|
|
|
uint32_t aFlags) {
|
2014-07-29 01:27:40 +04:00
|
|
|
if (!ShouldClip()) {
|
2016-05-25 19:01:18 +03:00
|
|
|
return InnerImage()->OptimalImageSizeForDest(aDest, aWhichFrame,
|
|
|
|
aSamplingFilter, aFlags);
|
2014-07-29 01:27:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int32_t imgWidth, imgHeight;
|
2016-03-08 10:54:13 +03:00
|
|
|
bool needScale = false;
|
2016-07-01 03:27:00 +03:00
|
|
|
bool forceUniformScaling = false;
|
2016-03-08 10:54:13 +03:00
|
|
|
if (mSVGViewportSize && !mSVGViewportSize->IsEmpty()) {
|
|
|
|
imgWidth = mSVGViewportSize->width;
|
|
|
|
imgHeight = mSVGViewportSize->height;
|
|
|
|
needScale = true;
|
2016-07-01 03:27:00 +03:00
|
|
|
forceUniformScaling = (aFlags & imgIContainer::FLAG_FORCE_UNIFORM_SCALING);
|
2016-03-08 10:54:13 +03:00
|
|
|
} else if (NS_SUCCEEDED(InnerImage()->GetWidth(&imgWidth)) &&
|
|
|
|
NS_SUCCEEDED(InnerImage()->GetHeight(&imgHeight))) {
|
|
|
|
needScale = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (needScale) {
|
2014-07-29 01:27:40 +04:00
|
|
|
// To avoid ugly sampling artifacts, ClippedImage needs the image size to
|
|
|
|
// be chosen such that the clipping region lies on pixel boundaries.
|
|
|
|
|
2015-04-04 22:57:00 +03:00
|
|
|
// First, we select a scale that's good for ClippedImage. An integer
|
|
|
|
// multiple of the size of the clipping region is always fine.
|
2017-08-14 15:29:56 +03:00
|
|
|
IntSize scale = IntSize::Ceil(aDest.width / mClip.Width(),
|
|
|
|
aDest.height / mClip.Height());
|
2014-07-29 01:27:40 +04:00
|
|
|
|
2016-07-01 03:27:00 +03:00
|
|
|
if (forceUniformScaling) {
|
|
|
|
scale.width = scale.height = max(scale.height, scale.width);
|
|
|
|
}
|
|
|
|
|
2014-07-29 01:27:40 +04:00
|
|
|
// Determine the size we'd prefer to render the inner image at, and ask the
|
|
|
|
// inner image what size we should actually use.
|
|
|
|
gfxSize desiredSize(imgWidth * scale.width, imgHeight * scale.height);
|
|
|
|
nsIntSize innerDesiredSize = InnerImage()->OptimalImageSizeForDest(
|
2016-05-25 19:01:18 +03:00
|
|
|
desiredSize, aWhichFrame, aSamplingFilter, aFlags);
|
2014-07-29 01:27:40 +04:00
|
|
|
|
|
|
|
// To get our final result, we take the inner image's desired size and
|
|
|
|
// determine how large the clipped region would be at that scale. (Again, we
|
|
|
|
// ensure an integer multiple of the size of the clipping region.)
|
2016-07-26 17:48:30 +03:00
|
|
|
IntSize finalScale =
|
|
|
|
IntSize::Ceil(double(innerDesiredSize.width) / imgWidth,
|
|
|
|
double(innerDesiredSize.height) / imgHeight);
|
2014-07-29 01:27:40 +04:00
|
|
|
return mClip.Size() * finalScale;
|
|
|
|
}
|
2016-03-08 10:54:13 +03:00
|
|
|
|
|
|
|
MOZ_ASSERT(false,
|
|
|
|
"If ShouldClip() led us to draw then we should never get here");
|
2016-05-25 19:01:18 +03:00
|
|
|
return InnerImage()->OptimalImageSizeForDest(aDest, aWhichFrame,
|
|
|
|
aSamplingFilter, aFlags);
|
2014-07-29 01:27:40 +04:00
|
|
|
}
|
|
|
|
|
2014-06-27 03:02:04 +04:00
|
|
|
NS_IMETHODIMP_(nsIntRect)
|
|
|
|
ClippedImage::GetImageSpaceInvalidationRect(const nsIntRect& aRect) {
|
|
|
|
if (!ShouldClip()) {
|
|
|
|
return InnerImage()->GetImageSpaceInvalidationRect(aRect);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIntRect rect(InnerImage()->GetImageSpaceInvalidationRect(aRect));
|
|
|
|
rect = rect.Intersect(mClip);
|
2017-12-21 00:46:28 +03:00
|
|
|
rect.MoveBy(-mClip.X(), -mClip.Y());
|
2014-06-27 03:02:04 +04:00
|
|
|
return rect;
|
|
|
|
}
|
|
|
|
|
2013-04-06 01:14:32 +04:00
|
|
|
} // namespace image
|
|
|
|
} // namespace mozilla
|