michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "gfxDrawable.h" michael@0: #include "gfxPlatform.h" michael@0: #include "gfxUtils.h" michael@0: #include "mozilla/gfx/2D.h" michael@0: #include "mozilla/RefPtr.h" michael@0: michael@0: #include "ClippedImage.h" michael@0: #include "Orientation.h" michael@0: #include "SVGImageContext.h" michael@0: michael@0: using namespace mozilla; michael@0: using namespace mozilla::gfx; michael@0: using mozilla::layers::LayerManager; michael@0: using mozilla::layers::ImageContainer; michael@0: michael@0: namespace mozilla { michael@0: namespace image { michael@0: michael@0: class ClippedImageCachedSurface michael@0: { michael@0: public: michael@0: ClippedImageCachedSurface(TemporaryRef aSurface, michael@0: const nsIntSize& aViewportSize, michael@0: const SVGImageContext* aSVGContext, michael@0: float aFrame, michael@0: uint32_t aFlags) michael@0: : mSurface(aSurface) michael@0: , mViewportSize(aViewportSize) michael@0: , mFrame(aFrame) michael@0: , mFlags(aFlags) michael@0: { michael@0: MOZ_ASSERT(mSurface, "Must have a valid surface"); michael@0: if (aSVGContext) { michael@0: mSVGContext.construct(*aSVGContext); michael@0: } michael@0: } michael@0: michael@0: bool Matches(const nsIntSize& aViewportSize, michael@0: const SVGImageContext* aSVGContext, michael@0: float aFrame, michael@0: uint32_t aFlags) michael@0: { michael@0: bool matchesSVGContext = (!aSVGContext && mSVGContext.empty()) || michael@0: *aSVGContext == mSVGContext.ref(); michael@0: return mViewportSize == aViewportSize && michael@0: matchesSVGContext && michael@0: mFrame == aFrame && michael@0: mFlags == aFlags; michael@0: } michael@0: michael@0: TemporaryRef Surface() { michael@0: return mSurface; michael@0: } michael@0: michael@0: private: michael@0: RefPtr mSurface; michael@0: const nsIntSize mViewportSize; michael@0: Maybe mSVGContext; michael@0: const float mFrame; michael@0: const uint32_t mFlags; michael@0: }; michael@0: michael@0: class DrawSingleTileCallback : public gfxDrawingCallback michael@0: { michael@0: public: michael@0: DrawSingleTileCallback(ClippedImage* aImage, michael@0: const nsIntRect& aClip, michael@0: const nsIntSize& aViewportSize, michael@0: const SVGImageContext* aSVGContext, michael@0: uint32_t aWhichFrame, michael@0: uint32_t aFlags) michael@0: : mImage(aImage) michael@0: , mClip(aClip) michael@0: , mViewportSize(aViewportSize) michael@0: , mSVGContext(aSVGContext) michael@0: , mWhichFrame(aWhichFrame) michael@0: , mFlags(aFlags) michael@0: { michael@0: MOZ_ASSERT(mImage, "Must have an image to clip"); michael@0: } michael@0: michael@0: virtual bool operator()(gfxContext* aContext, michael@0: const gfxRect& aFillRect, michael@0: const GraphicsFilter& aFilter, michael@0: const gfxMatrix& aTransform) michael@0: { michael@0: // Draw the image. |gfxCallbackDrawable| always calls this function with michael@0: // arguments that guarantee we never tile. michael@0: mImage->DrawSingleTile(aContext, aFilter, aTransform, aFillRect, mClip, michael@0: mViewportSize, mSVGContext, mWhichFrame, mFlags); michael@0: michael@0: return true; michael@0: } michael@0: michael@0: private: michael@0: nsRefPtr mImage; michael@0: const nsIntRect mClip; michael@0: const nsIntSize mViewportSize; michael@0: const SVGImageContext* mSVGContext; michael@0: const uint32_t mWhichFrame; michael@0: const uint32_t mFlags; michael@0: }; michael@0: michael@0: ClippedImage::ClippedImage(Image* aImage, michael@0: nsIntRect aClip) michael@0: : ImageWrapper(aImage) michael@0: , mClip(aClip) michael@0: { michael@0: MOZ_ASSERT(aImage != nullptr, "ClippedImage requires an existing Image"); michael@0: } michael@0: michael@0: ClippedImage::~ClippedImage() michael@0: { } michael@0: michael@0: bool michael@0: ClippedImage::ShouldClip() michael@0: { michael@0: // We need to evaluate the clipping region against the image's width and height michael@0: // once they're available to determine if it's valid and whether we actually michael@0: // need to do any work. We may fail if the image's width and height aren't michael@0: // available yet, in which case we'll try again later. michael@0: if (mShouldClip.empty()) { michael@0: int32_t width, height; michael@0: nsRefPtr innerImageStatusTracker = michael@0: InnerImage()->GetStatusTracker(); michael@0: if (InnerImage()->HasError()) { michael@0: // If there's a problem with the inner image we'll let it handle everything. michael@0: mShouldClip.construct(false); michael@0: } else if (NS_SUCCEEDED(InnerImage()->GetWidth(&width)) && width > 0 && michael@0: NS_SUCCEEDED(InnerImage()->GetHeight(&height)) && height > 0) { michael@0: // Clamp the clipping region to the size of the underlying image. michael@0: mClip = mClip.Intersect(nsIntRect(0, 0, width, height)); michael@0: michael@0: // If the clipping region is the same size as the underlying image we michael@0: // don't have to do anything. michael@0: mShouldClip.construct(!mClip.IsEqualInterior(nsIntRect(0, 0, width, height))); michael@0: } else if (innerImageStatusTracker && michael@0: innerImageStatusTracker->IsLoading()) { michael@0: // The image just hasn't finished loading yet. We don't yet know whether michael@0: // clipping with be needed or not for now. Just return without memoizing michael@0: // anything. michael@0: return false; michael@0: } else { michael@0: // We have a fully loaded image without a clearly defined width and michael@0: // height. This can happen with SVG images. michael@0: mShouldClip.construct(false); michael@0: } michael@0: } michael@0: michael@0: MOZ_ASSERT(!mShouldClip.empty(), "Should have computed a result"); michael@0: return mShouldClip.ref(); michael@0: } michael@0: michael@0: NS_IMPL_ISUPPORTS(ClippedImage, imgIContainer) michael@0: michael@0: nsIntRect michael@0: ClippedImage::FrameRect(uint32_t aWhichFrame) michael@0: { michael@0: if (!ShouldClip()) { michael@0: return InnerImage()->FrameRect(aWhichFrame); michael@0: } michael@0: michael@0: return nsIntRect(0, 0, mClip.width, mClip.height); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: ClippedImage::GetWidth(int32_t* aWidth) michael@0: { michael@0: if (!ShouldClip()) { michael@0: return InnerImage()->GetWidth(aWidth); michael@0: } michael@0: michael@0: *aWidth = mClip.width; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: ClippedImage::GetHeight(int32_t* aHeight) michael@0: { michael@0: if (!ShouldClip()) { michael@0: return InnerImage()->GetHeight(aHeight); michael@0: } michael@0: michael@0: *aHeight = mClip.height; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: ClippedImage::GetIntrinsicSize(nsSize* aSize) michael@0: { michael@0: if (!ShouldClip()) { michael@0: return InnerImage()->GetIntrinsicSize(aSize); michael@0: } michael@0: michael@0: *aSize = nsSize(mClip.width, mClip.height); michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: ClippedImage::GetIntrinsicRatio(nsSize* aRatio) michael@0: { michael@0: if (!ShouldClip()) { michael@0: return InnerImage()->GetIntrinsicRatio(aRatio); michael@0: } michael@0: michael@0: *aRatio = nsSize(mClip.width, mClip.height); michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP_(TemporaryRef) michael@0: ClippedImage::GetFrame(uint32_t aWhichFrame, michael@0: uint32_t aFlags) michael@0: { michael@0: return GetFrameInternal(mClip.Size(), nullptr, aWhichFrame, aFlags); michael@0: } michael@0: michael@0: TemporaryRef michael@0: ClippedImage::GetFrameInternal(const nsIntSize& aViewportSize, michael@0: const SVGImageContext* aSVGContext, michael@0: uint32_t aWhichFrame, michael@0: uint32_t aFlags) michael@0: { michael@0: if (!ShouldClip()) { michael@0: return InnerImage()->GetFrame(aWhichFrame, aFlags); michael@0: } michael@0: michael@0: float frameToDraw = InnerImage()->GetFrameIndex(aWhichFrame); michael@0: if (!mCachedSurface || !mCachedSurface->Matches(aViewportSize, michael@0: aSVGContext, michael@0: frameToDraw, michael@0: aFlags)) { michael@0: // Create a surface to draw into. michael@0: RefPtr target = gfxPlatform::GetPlatform()-> michael@0: CreateOffscreenContentDrawTarget(IntSize(mClip.width, mClip.height), michael@0: SurfaceFormat::B8G8R8A8); michael@0: michael@0: nsRefPtr ctx = new gfxContext(target); michael@0: michael@0: // Create our callback. michael@0: nsRefPtr drawTileCallback = michael@0: new DrawSingleTileCallback(this, mClip, aViewportSize, aSVGContext, aWhichFrame, aFlags); michael@0: nsRefPtr drawable = michael@0: new gfxCallbackDrawable(drawTileCallback, mClip.Size()); michael@0: michael@0: // Actually draw. The callback will end up invoking DrawSingleTile. michael@0: gfxRect imageRect(0, 0, mClip.width, mClip.height); michael@0: gfxUtils::DrawPixelSnapped(ctx, drawable, gfxMatrix(), michael@0: imageRect, imageRect, imageRect, imageRect, michael@0: gfxImageFormat::ARGB32, michael@0: GraphicsFilter::FILTER_FAST); michael@0: michael@0: // Cache the resulting surface. michael@0: mCachedSurface = new ClippedImageCachedSurface(target->Snapshot(), michael@0: aViewportSize, michael@0: aSVGContext, michael@0: frameToDraw, michael@0: aFlags); michael@0: } michael@0: michael@0: MOZ_ASSERT(mCachedSurface, "Should have a cached surface now"); michael@0: return mCachedSurface->Surface(); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: ClippedImage::GetImageContainer(LayerManager* aManager, ImageContainer** _retval) michael@0: { michael@0: // XXX(seth): We currently don't have a way of clipping the result of michael@0: // GetImageContainer. We work around this by always returning null, but if it michael@0: // ever turns out that ClippedImage is widely used on codepaths that can michael@0: // actually benefit from GetImageContainer, it would be a good idea to fix michael@0: // that method for performance reasons. michael@0: michael@0: if (!ShouldClip()) { michael@0: return InnerImage()->GetImageContainer(aManager, _retval); michael@0: } michael@0: michael@0: *_retval = nullptr; michael@0: return NS_OK; michael@0: } michael@0: michael@0: bool michael@0: ClippedImage::MustCreateSurface(gfxContext* aContext, michael@0: const gfxMatrix& aTransform, michael@0: const gfxRect& aSourceRect, michael@0: const nsIntRect& aSubimage, michael@0: const uint32_t aFlags) const michael@0: { michael@0: gfxRect gfxImageRect(0, 0, mClip.width, mClip.height); michael@0: nsIntRect intImageRect(0, 0, mClip.width, mClip.height); michael@0: bool willTile = !gfxImageRect.Contains(aSourceRect) && michael@0: !(aFlags & imgIContainer::FLAG_CLAMP); michael@0: bool willResample = (aContext->CurrentMatrix().HasNonIntegerTranslation() || michael@0: aTransform.HasNonIntegerTranslation()) && michael@0: (willTile || !aSubimage.Contains(intImageRect)); michael@0: return willTile || willResample; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: ClippedImage::Draw(gfxContext* aContext, michael@0: GraphicsFilter aFilter, michael@0: const gfxMatrix& aUserSpaceToImageSpace, michael@0: const gfxRect& aFill, michael@0: const nsIntRect& aSubimage, michael@0: const nsIntSize& aViewportSize, michael@0: const SVGImageContext* aSVGContext, michael@0: uint32_t aWhichFrame, michael@0: uint32_t aFlags) michael@0: { michael@0: if (!ShouldClip()) { michael@0: return InnerImage()->Draw(aContext, aFilter, aUserSpaceToImageSpace, michael@0: aFill, aSubimage, aViewportSize, aSVGContext, michael@0: aWhichFrame, aFlags); michael@0: } michael@0: michael@0: // Check for tiling. If we need to tile then we need to create a michael@0: // gfxCallbackDrawable to handle drawing for us. michael@0: gfxRect sourceRect = aUserSpaceToImageSpace.Transform(aFill); michael@0: if (MustCreateSurface(aContext, aUserSpaceToImageSpace, sourceRect, aSubimage, aFlags)) { michael@0: // Create a temporary surface containing a single tile of this image. michael@0: // GetFrame will call DrawSingleTile internally. michael@0: RefPtr surface = michael@0: GetFrameInternal(aViewportSize, aSVGContext, aWhichFrame, aFlags); michael@0: NS_ENSURE_TRUE(surface, NS_ERROR_FAILURE); michael@0: michael@0: // Create a drawable from that surface. michael@0: nsRefPtr drawable = michael@0: new gfxSurfaceDrawable(surface, gfxIntSize(mClip.width, mClip.height)); michael@0: michael@0: // Draw. michael@0: gfxRect imageRect(0, 0, mClip.width, mClip.height); michael@0: gfxRect subimage(aSubimage.x, aSubimage.y, aSubimage.width, aSubimage.height); michael@0: gfxUtils::DrawPixelSnapped(aContext, drawable, aUserSpaceToImageSpace, michael@0: subimage, sourceRect, imageRect, aFill, michael@0: gfxImageFormat::ARGB32, aFilter); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: // Determine the appropriate subimage for the inner image. michael@0: nsIntRect innerSubimage(aSubimage); michael@0: innerSubimage.MoveBy(mClip.x, mClip.y); michael@0: innerSubimage.Intersect(mClip); michael@0: michael@0: return DrawSingleTile(aContext, aFilter, aUserSpaceToImageSpace, aFill, innerSubimage, michael@0: aViewportSize, aSVGContext, aWhichFrame, aFlags); michael@0: } michael@0: michael@0: gfxFloat michael@0: ClippedImage::ClampFactor(const gfxFloat aToClamp, const int aReference) const michael@0: { michael@0: return aToClamp > aReference ? aReference / aToClamp michael@0: : 1.0; michael@0: } michael@0: michael@0: nsresult michael@0: ClippedImage::DrawSingleTile(gfxContext* aContext, michael@0: GraphicsFilter aFilter, michael@0: const gfxMatrix& aUserSpaceToImageSpace, michael@0: const gfxRect& aFill, michael@0: const nsIntRect& aSubimage, michael@0: const nsIntSize& aViewportSize, michael@0: const SVGImageContext* aSVGContext, michael@0: uint32_t aWhichFrame, michael@0: uint32_t aFlags) michael@0: { michael@0: MOZ_ASSERT(!MustCreateSurface(aContext, aUserSpaceToImageSpace, michael@0: aUserSpaceToImageSpace.Transform(aFill), michael@0: aSubimage - nsIntPoint(mClip.x, mClip.y), aFlags), michael@0: "DrawSingleTile shouldn't need to create a surface"); michael@0: michael@0: // Make the viewport reflect the original image's size. michael@0: nsIntSize viewportSize(aViewportSize); michael@0: int32_t imgWidth, imgHeight; michael@0: if (NS_SUCCEEDED(InnerImage()->GetWidth(&imgWidth)) && michael@0: NS_SUCCEEDED(InnerImage()->GetHeight(&imgHeight))) { michael@0: viewportSize = nsIntSize(imgWidth, imgHeight); michael@0: } else { michael@0: MOZ_ASSERT(false, "If ShouldClip() led us to draw then we should never get here"); michael@0: } michael@0: michael@0: // Add a translation to the transform to reflect the clipping region. michael@0: gfxMatrix transform(aUserSpaceToImageSpace); michael@0: transform.Multiply(gfxMatrix().Translate(gfxPoint(mClip.x, mClip.y))); michael@0: michael@0: // "Clamp the source rectangle" to the clipping region's width and height. michael@0: // Really, this means modifying the transform to get the results we want. michael@0: gfxRect sourceRect = transform.Transform(aFill); michael@0: if (sourceRect.width > mClip.width || sourceRect.height > mClip.height) { michael@0: gfxMatrix clampSource; michael@0: clampSource.Translate(gfxPoint(sourceRect.x, sourceRect.y)); michael@0: clampSource.Scale(ClampFactor(sourceRect.width, mClip.width), michael@0: ClampFactor(sourceRect.height, mClip.height)); michael@0: clampSource.Translate(gfxPoint(-sourceRect.x, -sourceRect.y)); michael@0: transform.Multiply(clampSource); michael@0: } michael@0: michael@0: return InnerImage()->Draw(aContext, aFilter, transform, aFill, aSubimage, michael@0: viewportSize, aSVGContext, aWhichFrame, aFlags); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: ClippedImage::RequestDiscard() michael@0: { michael@0: // We're very aggressive about discarding. michael@0: mCachedSurface = nullptr; michael@0: michael@0: return InnerImage()->RequestDiscard(); michael@0: } michael@0: michael@0: NS_IMETHODIMP_(Orientation) michael@0: ClippedImage::GetOrientation() michael@0: { michael@0: // XXX(seth): This should not actually be here; this is just to work around a michael@0: // what appears to be a bug in MSVC's linker. michael@0: return InnerImage()->GetOrientation(); michael@0: } michael@0: michael@0: } // namespace image michael@0: } // namespace mozilla