diff --git a/image/public/imgIContainer.idl b/image/public/imgIContainer.idl index de2ca0c9e51..ae7f86f9cb9 100644 --- a/image/public/imgIContainer.idl +++ b/image/public/imgIContainer.idl @@ -52,12 +52,13 @@ native nsSize(nsSize); /** * imgIContainer is the interface that represents an image. It allows - * access to frames as Thebes surfaces. It also allows drawing of images - * onto Thebes contexts. + * access to frames as Thebes surfaces, and permits users to extract subregions + * as other imgIContainers. It also allows drawing of images on to Thebes + * contexts. * * Internally, imgIContainer also manages animation of images. */ -[scriptable, builtinclass, uuid(0c1caf24-bce7-4db5-971d-8e1b6ed07540)] +[scriptable, builtinclass, uuid(01c4f92f-f883-4837-a127-d8f30920e374)] interface imgIContainer : nsISupports { /** @@ -177,6 +178,19 @@ interface imgIContainer : nsISupports */ [noscript] ImageContainer getImageContainer(in LayerManager aManager); + /** + * Create a new imgContainer that contains only a single frame, which itself + * contains a subregion of the given frame. + * + * @param aWhichFrame Frame specifier of the FRAME_* variety. + * @param aRect the area of the current frame to be duplicated in the + * returned imgContainer's frame. + * @param aFlags Flags of the FLAG_* variety + */ + [noscript] imgIContainer extractFrame(in uint32_t aWhichFrame, + [const] in nsIntRect aRect, + in uint32_t aFlags); + /** * Draw a frame onto the context specified. * @@ -215,10 +229,11 @@ interface imgIContainer : nsISupports /* * Ensures that an image is decoding. Calling this function guarantees that - * the image will at some point fire off decode notifications. Calling draw() - * or getFrame() triggers the same mechanism internally. Thus, if you want to - * be sure that the image will be decoded but don't want to access it until - * then, you must call requestDecode(). + * the image will at some point fire off decode notifications. Calling draw(), + * getFrame(), copyFrame(), or extractCurrentFrame() triggers the same + * mechanism internally. Thus, if you want to be sure that the image will be + * decoded but don't want to access it until then, you must call + * requestDecode(). */ void requestDecode(); diff --git a/image/src/ClippedImage.cpp b/image/src/ClippedImage.cpp deleted file mode 100644 index dfdc3f04aea..00000000000 --- a/image/src/ClippedImage.cpp +++ /dev/null @@ -1,324 +0,0 @@ -/* -*- 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/. */ - -#include "gfxDrawable.h" -#include "gfxPlatform.h" -#include "gfxUtils.h" - -#include "ClippedImage.h" - -using mozilla::layers::LayerManager; -using mozilla::layers::ImageContainer; - -namespace mozilla { -namespace image { - -class DrawSingleTileCallback : public gfxDrawingCallback -{ -public: - DrawSingleTileCallback(ClippedImage* aImage, - const nsIntRect& aClip, - const nsIntSize& aViewportSize, - const SVGImageContext* aSVGContext, - uint32_t aWhichFrame, - uint32_t aFlags) - : mImage(aImage) - , mClip(aClip) - , mViewportSize(aViewportSize) - , mSVGContext(aSVGContext) - , mWhichFrame(aWhichFrame) - , mFlags(aFlags) - { - MOZ_ASSERT(mImage, "Must have an image to clip"); - } - - virtual bool operator()(gfxContext* aContext, - const gfxRect& aFillRect, - const gfxPattern::GraphicsFilter& aFilter, - const gfxMatrix& aTransform) - { - // Draw the image. |gfxCallbackDrawable| always calls this function with - // arguments that guarantee we never tile. - mImage->DrawSingleTile(aContext, aFilter, aTransform, aFillRect, mClip, - mViewportSize, mSVGContext, mWhichFrame, mFlags); - - return true; - } - -private: - nsRefPtr mImage; - const nsIntRect mClip; - const nsIntSize mViewportSize; - const SVGImageContext* mSVGContext; - const uint32_t mWhichFrame; - const uint32_t mFlags; -}; - -ClippedImage::ClippedImage(Image* aImage, - nsIntRect aClip) - : ImageWrapper(aImage) - , mClip(aClip) -{ - MOZ_ASSERT(aImage != nullptr, "ClippedImage requires an existing Image"); -} - -bool -ClippedImage::ShouldClip() -{ - // 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. - if (mShouldClip.empty()) { - int32_t width, height; - if (InnerImage()->HasError()) { - // If there's a problem with the inner image we'll let it handle everything. - mShouldClip.construct(false); - } 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. - mShouldClip.construct(!mClip.IsEqualInterior(nsIntRect(0, 0, width, height))); - } else if (InnerImage()->GetStatusTracker().IsLoading()) { - // The image just hasn't finished loading yet. We don't yet know whether - // clipping with be needed or not for now. Just return without memoizing - // anything. - return false; - } else { - // We have a fully loaded image without a clearly defined width and - // height. This can happen with SVG images. - mShouldClip.construct(false); - } - } - - MOZ_ASSERT(!mShouldClip.empty(), "Should have computed a result"); - return mShouldClip.ref(); -} - -NS_IMPL_ISUPPORTS1(ClippedImage, imgIContainer) - -nsIntRect -ClippedImage::FrameRect(uint32_t aWhichFrame) -{ - if (!ShouldClip()) { - return InnerImage()->FrameRect(aWhichFrame); - } - - return nsIntRect(0, 0, mClip.width, mClip.height); -} - -NS_IMETHODIMP -ClippedImage::GetWidth(int32_t* aWidth) -{ - if (!ShouldClip()) { - return InnerImage()->GetWidth(aWidth); - } - - *aWidth = mClip.width; - return NS_OK; -} - -NS_IMETHODIMP -ClippedImage::GetHeight(int32_t* aHeight) -{ - if (!ShouldClip()) { - return InnerImage()->GetHeight(aHeight); - } - - *aHeight = mClip.height; - return NS_OK; -} - -NS_IMETHODIMP -ClippedImage::GetIntrinsicSize(nsSize* aSize) -{ - if (!ShouldClip()) { - return InnerImage()->GetIntrinsicSize(aSize); - } - - *aSize = nsSize(mClip.width, mClip.height); - return NS_OK; -} - -NS_IMETHODIMP -ClippedImage::GetIntrinsicRatio(nsSize* aRatio) -{ - if (!ShouldClip()) { - return InnerImage()->GetIntrinsicRatio(aRatio); - } - - *aRatio = nsSize(mClip.width, mClip.height); - return NS_OK; -} - -NS_IMETHODIMP -ClippedImage::GetFrame(uint32_t aWhichFrame, - uint32_t aFlags, - gfxASurface** _retval) -{ - if (!ShouldClip()) { - return InnerImage()->GetFrame(aWhichFrame, aFlags, _retval); - } - - // Create a surface to draw into. - gfxImageSurface::gfxImageFormat format = gfxASurface::ImageFormatARGB32; - nsRefPtr surface = gfxPlatform::GetPlatform() - ->CreateOffscreenSurface(gfxIntSize(mClip.width, mClip.height), - gfxImageSurface::ContentFromFormat(format)); - // Create our callback. - nsRefPtr drawTileCallback = - new DrawSingleTileCallback(this, mClip, mClip.Size(), nullptr, aWhichFrame, aFlags); - nsRefPtr drawable = - new gfxCallbackDrawable(drawTileCallback, mClip.Size()); - - // Actually draw. The callback will end up invoking DrawSingleTile. - nsRefPtr ctx = new gfxContext(surface); - gfxRect imageRect(0, 0, mClip.width, mClip.height); - gfxUtils::DrawPixelSnapped(ctx, drawable, gfxMatrix(), - imageRect, imageRect, imageRect, imageRect, - gfxASurface::ImageFormatARGB32, gfxPattern::FILTER_FAST); - - *_retval = surface.forget().get(); - return NS_OK; -} - -NS_IMETHODIMP -ClippedImage::GetImageContainer(LayerManager* aManager, ImageContainer** _retval) -{ - // 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. - - *_retval = nullptr; - return NS_OK; -} - -bool -ClippedImage::MustCreateSurface(gfxContext* aContext, - const gfxMatrix& aTransform, - const gfxRect& aSourceRect, - const nsIntRect& aSubimage, - const uint32_t aFlags) const -{ - gfxRect gfxImageRect(0, 0, mClip.width, mClip.height); - nsIntRect intImageRect(0, 0, mClip.width, mClip.height); - bool willTile = !gfxImageRect.Contains(aSourceRect) && - !(aFlags & imgIContainer::FLAG_CLAMP); - bool willResample = (aContext->CurrentMatrix().HasNonIntegerTranslation() || - aTransform.HasNonIntegerTranslation()) && - (willTile || !aSubimage.Contains(intImageRect)); - return willTile || willResample; -} - -NS_IMETHODIMP -ClippedImage::Draw(gfxContext* aContext, - gfxPattern::GraphicsFilter aFilter, - const gfxMatrix& aUserSpaceToImageSpace, - const gfxRect& aFill, - const nsIntRect& aSubimage, - const nsIntSize& aViewportSize, - const SVGImageContext* aSVGContext, - uint32_t aWhichFrame, - uint32_t aFlags) -{ - if (!ShouldClip()) { - return InnerImage()->Draw(aContext, aFilter, aUserSpaceToImageSpace, - aFill, aSubimage, aViewportSize, aSVGContext, - aWhichFrame, aFlags); - } - - // Check for tiling. If we need to tile then we need to create a - // gfxCallbackDrawable to handle drawing for us. - gfxRect sourceRect = aUserSpaceToImageSpace.Transform(aFill); - if (MustCreateSurface(aContext, aUserSpaceToImageSpace, sourceRect, aSubimage, aFlags)) { - // Create a temporary surface containing a single tile of this image. - // GetFrame will call DrawSingleTile internally. - nsRefPtr surface; - GetFrame(aWhichFrame, aFlags, getter_AddRefs(surface)); - NS_ENSURE_TRUE(surface, NS_ERROR_FAILURE); - - // Create a drawable from that surface. - nsRefPtr drawable = - new gfxSurfaceDrawable(surface, gfxIntSize(mClip.width, mClip.height)); - - // Draw. - gfxRect imageRect(0, 0, mClip.width, mClip.height); - gfxRect subimage(aSubimage.x, aSubimage.y, aSubimage.width, aSubimage.height); - gfxUtils::DrawPixelSnapped(aContext, drawable, aUserSpaceToImageSpace, - subimage, sourceRect, imageRect, aFill, - gfxASurface::ImageFormatARGB32, aFilter); - - return NS_OK; - } - - // Determine the appropriate subimage for the inner image. - nsIntRect innerSubimage(aSubimage); - innerSubimage.MoveBy(mClip.x, mClip.y); - innerSubimage.Intersect(mClip); - - return DrawSingleTile(aContext, aFilter, aUserSpaceToImageSpace, aFill, innerSubimage, - aViewportSize, aSVGContext, aWhichFrame, aFlags); -} - -gfxFloat -ClippedImage::ClampFactor(const gfxFloat aToClamp, const int aReference) const -{ - return aToClamp > aReference ? aReference / aToClamp - : 1.0; -} - -nsresult -ClippedImage::DrawSingleTile(gfxContext* aContext, - gfxPattern::GraphicsFilter aFilter, - const gfxMatrix& aUserSpaceToImageSpace, - const gfxRect& aFill, - const nsIntRect& aSubimage, - const nsIntSize& aViewportSize, - const SVGImageContext* aSVGContext, - uint32_t aWhichFrame, - uint32_t aFlags) -{ - MOZ_ASSERT(!MustCreateSurface(aContext, aUserSpaceToImageSpace, - aUserSpaceToImageSpace.Transform(aFill), - aSubimage - nsIntPoint(mClip.x, mClip.y), aFlags), - "DrawSingleTile shouldn't need to create a surface"); - - // Make the viewport reflect the original image's size. - nsIntSize viewportSize(aViewportSize); - int32_t imgWidth, imgHeight; - if (NS_SUCCEEDED(InnerImage()->GetWidth(&imgWidth)) && - NS_SUCCEEDED(InnerImage()->GetHeight(&imgHeight))) { - viewportSize = nsIntSize(imgWidth, imgHeight); - } else { - MOZ_ASSERT(false, "If ShouldClip() led us to draw then we should never get here"); - } - - // Add a translation to the transform to reflect the clipping region. - gfxMatrix transform(aUserSpaceToImageSpace); - transform.Multiply(gfxMatrix().Translate(gfxPoint(mClip.x, mClip.y))); - - // "Clamp the source rectangle" to the clipping region's width and height. - // Really, this means modifying the transform to get the results we want. - gfxRect sourceRect = transform.Transform(aFill); - if (sourceRect.width > mClip.width || sourceRect.height > mClip.height) { - gfxMatrix clampSource; - clampSource.Translate(gfxPoint(sourceRect.x, sourceRect.y)); - clampSource.Scale(ClampFactor(sourceRect.width, mClip.width), - ClampFactor(sourceRect.height, mClip.height)); - clampSource.Translate(gfxPoint(-sourceRect.x, -sourceRect.y)); - transform.Multiply(clampSource); - } - - return InnerImage()->Draw(aContext, aFilter, transform, aFill, aSubimage, - viewportSize, aSVGContext, aWhichFrame, aFlags); -} - -} // namespace image -} // namespace mozilla diff --git a/image/src/ClippedImage.h b/image/src/ClippedImage.h deleted file mode 100644 index 39d47b7f3cc..00000000000 --- a/image/src/ClippedImage.h +++ /dev/null @@ -1,82 +0,0 @@ -/* -*- 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/. */ - -#ifndef MOZILLA_IMAGELIB_CLIPPEDIMAGE_H_ -#define MOZILLA_IMAGELIB_CLIPPEDIMAGE_H_ - -#include "ImageWrapper.h" - -namespace mozilla { -namespace image { - -class DrawSingleTileCallback; - -/** - * An Image wrapper that clips an image against a rectangle. Right now only - * absolute coordinates in pixels are supported. - * - * XXX(seth): There a known (performance, not correctness) issue with - * GetImageContainer. See the comments for that method for more information. - */ -class ClippedImage : public ImageWrapper -{ -public: - NS_DECL_ISUPPORTS - - virtual ~ClippedImage() { } - - virtual nsIntRect FrameRect(uint32_t aWhichFrame) MOZ_OVERRIDE; - - NS_IMETHOD GetWidth(int32_t* aWidth) MOZ_OVERRIDE; - NS_IMETHOD GetHeight(int32_t* aHeight) MOZ_OVERRIDE; - NS_IMETHOD GetIntrinsicSize(nsSize* aSize) MOZ_OVERRIDE; - NS_IMETHOD GetIntrinsicRatio(nsSize* aRatio) MOZ_OVERRIDE; - NS_IMETHOD GetFrame(uint32_t aWhichFrame, - uint32_t aFlags, - gfxASurface** _retval) MOZ_OVERRIDE; - NS_IMETHOD GetImageContainer(mozilla::layers::LayerManager* aManager, - mozilla::layers::ImageContainer** _retval) MOZ_OVERRIDE; - NS_IMETHOD Draw(gfxContext* aContext, - gfxPattern::GraphicsFilter aFilter, - const gfxMatrix& aUserSpaceToImageSpace, - const gfxRect& aFill, - const nsIntRect& aSubimage, - const nsIntSize& aViewportSize, - const SVGImageContext* aSVGContext, - uint32_t aWhichFrame, - uint32_t aFlags) MOZ_OVERRIDE; - -protected: - ClippedImage(Image* aImage, nsIntRect aClip); - -private: - bool ShouldClip(); - bool MustCreateSurface(gfxContext* aContext, - const gfxMatrix& aTransform, - const gfxRect& aSourceRect, - const nsIntRect& aSubimage, - const uint32_t aFlags) const; - gfxFloat ClampFactor(const gfxFloat aToClamp, const int aReference) const; - nsresult DrawSingleTile(gfxContext* aContext, - gfxPattern::GraphicsFilter aFilter, - const gfxMatrix& aUserSpaceToImageSpace, - const gfxRect& aFill, - const nsIntRect& aSubimage, - const nsIntSize& aViewportSize, - const SVGImageContext* aSVGContext, - uint32_t aWhichFrame, - uint32_t aFlags); - - nsIntRect mClip; // The region to clip to. - Maybe mShouldClip; // Memoized ShouldClip() if present. - - friend class DrawSingleTileCallback; - friend class ImageOps; -}; - -} // namespace image -} // namespace mozilla - -#endif // MOZILLA_IMAGELIB_CLIPPEDIMAGE_H_ diff --git a/image/src/FrozenImage.cpp b/image/src/FrozenImage.cpp index 333b7b88e00..aa7e19661f5 100644 --- a/image/src/FrozenImage.cpp +++ b/image/src/FrozenImage.cpp @@ -68,6 +68,15 @@ FrozenImage::GetImageContainer(layers::LayerManager* aManager, return NS_OK; } +NS_IMETHODIMP +FrozenImage::ExtractFrame(uint32_t aWhichFrame, + const nsIntRect& aRegion, + uint32_t aFlags, + imgIContainer** _retval) +{ + return InnerImage()->ExtractFrame(FRAME_FIRST, aRegion, aFlags, _retval); +} + NS_IMETHODIMP FrozenImage::Draw(gfxContext* aContext, gfxPattern::GraphicsFilter aFilter, diff --git a/image/src/FrozenImage.h b/image/src/FrozenImage.h index 65428ce7a3c..077caa04027 100644 --- a/image/src/FrozenImage.h +++ b/image/src/FrozenImage.h @@ -40,6 +40,10 @@ public: NS_IMETHOD_(bool) FrameIsOpaque(uint32_t aWhichFrame) MOZ_OVERRIDE; NS_IMETHOD GetImageContainer(layers::LayerManager* aManager, layers::ImageContainer** _retval) MOZ_OVERRIDE; + NS_IMETHOD ExtractFrame(uint32_t aWhichFrame, + const nsIntRect& aRegion, + uint32_t aFlags, + imgIContainer** _retval) MOZ_OVERRIDE; NS_IMETHOD Draw(gfxContext* aContext, gfxPattern::GraphicsFilter aFilter, const gfxMatrix& aUserSpaceToImageSpace, @@ -58,7 +62,7 @@ protected: FrozenImage(Image* aImage) : ImageWrapper(aImage) { } private: - friend class ImageOps; + friend class ImageFactory; }; } // namespace image diff --git a/image/src/ImageFactory.cpp b/image/src/ImageFactory.cpp index 92ed5a526c4..e44416b669b 100644 --- a/image/src/ImageFactory.cpp +++ b/image/src/ImageFactory.cpp @@ -21,6 +21,7 @@ #include "imgStatusTracker.h" #include "RasterImage.h" #include "VectorImage.h" +#include "FrozenImage.h" #include "Image.h" #include "nsMediaFragmentURIParser.h" @@ -174,6 +175,13 @@ GetContentSize(nsIRequest* aRequest) return 0; } +/* static */ already_AddRefed +ImageFactory::Freeze(Image* aImage) +{ + nsRefPtr frozenImage = new FrozenImage(aImage); + return frozenImage.forget(); +} + /* static */ already_AddRefed ImageFactory::CreateRasterImage(nsIRequest* aRequest, imgStatusTracker* aStatusTracker, diff --git a/image/src/ImageFactory.h b/image/src/ImageFactory.h index dedac3fb615..716ac979761 100644 --- a/image/src/ImageFactory.h +++ b/image/src/ImageFactory.h @@ -46,6 +46,14 @@ public: */ static already_AddRefed CreateAnonymousImage(const nsCString& aMimeType); + /** + * Creates a version of an existing image which does not animate and is frozen + * at the first frame. + * + * @param aImage The existing image. + */ + static already_AddRefed Freeze(Image* aImage); + private: // Factory functions that create specific types of image containers. static already_AddRefed CreateRasterImage(nsIRequest* aRequest, diff --git a/image/src/ImageOps.cpp b/image/src/ImageOps.cpp deleted file mode 100644 index add83a89f99..00000000000 --- a/image/src/ImageOps.cpp +++ /dev/null @@ -1,48 +0,0 @@ -/* -*- 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/. */ - -#include "imgIContainer.h" -#include "ClippedImage.h" -#include "FrozenImage.h" -#include "Image.h" - -#include "ImageOps.h" - -namespace mozilla { -namespace image { - -/* static */ already_AddRefed -ImageOps::Freeze(Image* aImage) -{ - nsRefPtr frozenImage = new FrozenImage(aImage); - return frozenImage.forget(); -} - -/* static */ already_AddRefed -ImageOps::Freeze(imgIContainer* aImage) -{ - nsCOMPtr frozenImage = - new FrozenImage(static_cast(aImage)); - return frozenImage.forget(); -} - -/* static */ already_AddRefed -ImageOps::Clip(Image* aImage, nsIntRect aClip) -{ - nsRefPtr clippedImage = new ClippedImage(aImage, aClip); - return clippedImage.forget(); -} - -/* static */ already_AddRefed -ImageOps::Clip(imgIContainer* aImage, nsIntRect aClip) -{ - nsCOMPtr clippedImage = - new ClippedImage(static_cast(aImage), aClip); - return clippedImage.forget(); -} - -} // namespace image -} // namespace mozilla diff --git a/image/src/ImageOps.h b/image/src/ImageOps.h deleted file mode 100644 index e68571f19a0..00000000000 --- a/image/src/ImageOps.h +++ /dev/null @@ -1,49 +0,0 @@ -/* -*- 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/. */ - -#ifndef MOZILLA_IMAGELIB_IMAGEOPS_H_ -#define MOZILLA_IMAGELIB_IMAGEOPS_H_ - -#include "nsCOMPtr.h" -#include "nsRect.h" - -class imgIContainer; - -namespace mozilla { -namespace image { - -class Image; - -class ImageOps -{ -public: - /** - * Creates a version of an existing image which does not animate and is frozen - * at the first frame. - * - * @param aImage The existing image. - */ - static already_AddRefed Freeze(Image* aImage); - static already_AddRefed Freeze(imgIContainer* aImage); - - /** - * Creates a clipped version of an existing image. Animation is unaffected. - * - * @param aImage The existing image. - * @param aClip The rectangle to clip the image against. - */ - static already_AddRefed Clip(Image* aImage, nsIntRect aClip); - static already_AddRefed Clip(imgIContainer* aImage, nsIntRect aClip); - -private: - // This is a static utility class, so disallow instantiation. - virtual ~ImageOps() = 0; -}; - -} // namespace image -} // namespace mozilla - -#endif // MOZILLA_IMAGELIB_IMAGEOPS_H_ diff --git a/image/src/ImageWrapper.cpp b/image/src/ImageWrapper.cpp index d67e684d9d4..193aac3ffc1 100644 --- a/image/src/ImageWrapper.cpp +++ b/image/src/ImageWrapper.cpp @@ -203,6 +203,15 @@ ImageWrapper::GetImageContainer(LayerManager* aManager, ImageContainer** _retval return mInnerImage->GetImageContainer(aManager, _retval); } +NS_IMETHODIMP +ImageWrapper::ExtractFrame(uint32_t aWhichFrame, + const nsIntRect& aRegion, + uint32_t aFlags, + imgIContainer** _retval) +{ + return mInnerImage->ExtractFrame(aWhichFrame, aRegion, aFlags, _retval); +} + NS_IMETHODIMP ImageWrapper::Draw(gfxContext* aContext, gfxPattern::GraphicsFilter aFilter, diff --git a/image/src/Makefile.in b/image/src/Makefile.in index 897a40f02bd..fd271061709 100644 --- a/image/src/Makefile.in +++ b/image/src/Makefile.in @@ -20,16 +20,13 @@ FAIL_ON_WARNINGS = 1 EXPORTS = imgLoader.h \ imgRequest.h \ imgRequestProxy.h \ - ImageOps.h \ $(NULL) CPPSRCS = \ Image.cpp \ ImageFactory.cpp \ ImageMetadata.cpp \ - ImageOps.cpp \ ImageWrapper.cpp \ - ClippedImage.cpp \ Decoder.cpp \ DiscardTracker.cpp \ FrozenImage.cpp \ diff --git a/image/src/RasterImage.cpp b/image/src/RasterImage.cpp index 1cecf24bc3e..6ca0e55bc59 100644 --- a/image/src/RasterImage.cpp +++ b/image/src/RasterImage.cpp @@ -507,6 +507,14 @@ RasterImage::Init(const char* aMimeType, discardable_source_bytes += mSourceData.Length(); } + // If we're being called from ExtractFrame (used by borderimage), + // we don't actually do any decoding. Bail early. + // XXX - This should be removed when we fix borderimage + if (mSourceDataMimeType.Length() == 0) { + mInitialized = true; + return NS_OK; + } + // Instantiate the decoder nsresult rv = InitDecoder(/* aDoSizeDecode = */ true); CONTAINER_ENSURE_SUCCESS(rv); @@ -680,6 +688,85 @@ RasterImage::RequestRefresh(const mozilla::TimeStamp& aTime) } } +//****************************************************************************** +/* [noscript] imgIContainer extractFrame(uint32_t aWhichFrame, + * [const] in nsIntRect aRegion, + * in uint32_t aFlags); */ +NS_IMETHODIMP +RasterImage::ExtractFrame(uint32_t aWhichFrame, + const nsIntRect &aRegion, + uint32_t aFlags, + imgIContainer **_retval) +{ + NS_ENSURE_ARG_POINTER(_retval); + + nsresult rv; + + if (aWhichFrame > FRAME_MAX_VALUE) + return NS_ERROR_INVALID_ARG; + + if (mError) + return NS_ERROR_FAILURE; + + // Disallowed in the API + if (mInDecoder && (aFlags & imgIContainer::FLAG_SYNC_DECODE)) + return NS_ERROR_FAILURE; + + // Make a new container. This should switch to another class with bug 505959. + nsRefPtr img(new RasterImage()); + + // We don't actually have a mimetype in this case. The empty string tells the + // init routine not to try to instantiate a decoder. This should be fixed in + // bug 505959. + img->Init("", INIT_FLAG_NONE); + img->SetSize(aRegion.width, aRegion.height); + img->mDecoded = true; // Also, we need to mark the image as decoded + img->mHasBeenDecoded = true; + img->mFrameDecodeFlags = aFlags & DECODE_FLAGS_MASK; + + if (!ApplyDecodeFlags(aFlags)) + return NS_ERROR_NOT_AVAILABLE; + + // If a synchronous decode was requested, do it + if (aFlags & FLAG_SYNC_DECODE) { + rv = SyncDecode(); + CONTAINER_ENSURE_SUCCESS(rv); + } + + // Get the frame. If it's not there, it's probably the caller's fault for + // not waiting for the data to be loaded from the network or not passing + // FLAG_SYNC_DECODE + uint32_t frameIndex = (aWhichFrame == FRAME_FIRST) ? + 0 : GetCurrentImgFrameIndex(); + imgFrame *frame = GetDrawableImgFrame(frameIndex); + if (!frame) { + *_retval = nullptr; + return NS_ERROR_FAILURE; + } + + // The frame can be smaller than the image. We want to extract only the part + // of the frame that actually exists. + nsIntRect framerect = frame->GetRect(); + framerect.IntersectRect(framerect, aRegion); + + if (framerect.IsEmpty()) + return NS_ERROR_NOT_AVAILABLE; + + nsAutoPtr subframe; + rv = frame->Extract(framerect, getter_Transfers(subframe)); + if (NS_FAILED(rv)) + return rv; + + img->mFrames.AppendElement(subframe.forget()); + + img->mStatusTracker->RecordLoaded(); + img->mStatusTracker->RecordDecoded(); + + *_retval = img.forget().get(); + + return NS_OK; +} + //****************************************************************************** /* readonly attribute int32_t width; */ NS_IMETHODIMP @@ -2661,10 +2748,11 @@ RasterImage::WriteToDecoder(const char *aBuffer, uint32_t aCount) } // This function is called in situations where it's clear that we want the -// frames in decoded form (Draw, GetFrame, etc). If we're completely decoded, -// this method resets the discard timer (if we're discardable), since wanting -// the frames now is a good indicator of wanting them again soon. If we're not -// decoded, this method kicks off asynchronous decoding to generate the frames. +// frames in decoded form (Draw, GetFrame, CopyFrame, ExtractFrame, etc). +// If we're completely decoded, this method resets the discard timer (if +// we're discardable), since wanting the frames now is a good indicator of +// wanting them again soon. If we're not decoded, this method kicks off +// asynchronous decoding to generate the frames. nsresult RasterImage::WantDecodedFrames() { diff --git a/image/src/VectorImage.cpp b/image/src/VectorImage.cpp index 9c1631a0d78..334dd620050 100644 --- a/image/src/VectorImage.cpp +++ b/image/src/VectorImage.cpp @@ -301,10 +301,12 @@ NS_IMPL_ISUPPORTS3(VectorImage, VectorImage::VectorImage(imgStatusTracker* aStatusTracker, nsIURI* aURI /* = nullptr */) : ImageResource(aStatusTracker, aURI), // invoke superclass's constructor + mRestrictedRegion(0, 0, 0, 0), mIsInitialized(false), mIsFullyLoaded(false), mIsDrawing(false), - mHaveAnimations(false) + mHaveAnimations(false), + mHaveRestrictedRegion(false) { } @@ -324,7 +326,8 @@ VectorImage::Init(const char* aMimeType, if (mIsInitialized) return NS_ERROR_ILLEGAL_VALUE; - MOZ_ASSERT(!mIsFullyLoaded && !mHaveAnimations && !mError, + MOZ_ASSERT(!mIsFullyLoaded && !mHaveAnimations && + !mHaveRestrictedRegion && !mError, "Flags unexpectedly set before initialization"); MOZ_ASSERT(!strcmp(aMimeType, IMAGE_SVG_XML), "Unexpected mimetype"); @@ -592,7 +595,14 @@ VectorImage::GetFrame(uint32_t aWhichFrame, // --------------------------------------------- // Make our surface the size of what will ultimately be drawn to it. // (either the full image size, or the restricted region) - gfxIntSize surfaceSize(imageIntSize.width, imageIntSize.height); + gfxIntSize surfaceSize; + if (mHaveRestrictedRegion) { + surfaceSize.width = mRestrictedRegion.width; + surfaceSize.height = mRestrictedRegion.height; + } else { + surfaceSize.width = imageIntSize.width; + surfaceSize.height = imageIntSize.height; + } nsRefPtr surface = new gfxImageSurface(surfaceSize, gfxASurface::ImageFormatARGB32); @@ -621,6 +631,54 @@ VectorImage::GetImageContainer(LayerManager* aManager, return NS_OK; } +//****************************************************************************** +/* [noscript] imgIContainer extractFrame(uint32_t aWhichFrame, + * [const] in nsIntRect aRegion, + * in uint32_t aFlags); */ +NS_IMETHODIMP +VectorImage::ExtractFrame(uint32_t aWhichFrame, + const nsIntRect& aRegion, + uint32_t aFlags, + imgIContainer** _retval) +{ + NS_ENSURE_ARG_POINTER(_retval); + if (mError || !mIsFullyLoaded) + return NS_ERROR_FAILURE; + + // XXXdholbert NOTE: This method assumes FRAME_CURRENT (not FRAME_FIRST) + // right now, because mozilla doesn't actually contain any clients of this + // method that use FRAME_FIRST. If it's needed, we *could* handle + // FRAME_FIRST by saving the helper-doc's current SMIL time, seeking it to + // time 0, rendering to a RasterImage, and then restoring our saved time. + if (aWhichFrame != FRAME_CURRENT) { + NS_WARNING("VectorImage::ExtractFrame with something other than " + "FRAME_CURRENT isn't supported yet. Assuming FRAME_CURRENT."); + } + + // XXXdholbert This method also doesn't actually freeze animation in the + // returned imgIContainer, because it shares our helper-document. To + // get a true snapshot, we need to clone the document - see bug 590792. + + // Make a new container with same SVG document. + nsRefPtr extractedImg = new VectorImage(); + extractedImg->mSVGDocumentWrapper = mSVGDocumentWrapper; + extractedImg->mAnimationMode = kDontAnimMode; + + extractedImg->mRestrictedRegion.x = aRegion.x; + extractedImg->mRestrictedRegion.y = aRegion.y; + + // (disallow negative width/height on our restricted region) + extractedImg->mRestrictedRegion.width = std::max(aRegion.width, 0); + extractedImg->mRestrictedRegion.height = std::max(aRegion.height, 0); + + extractedImg->mIsInitialized = true; + extractedImg->mIsFullyLoaded = true; + extractedImg->mHaveRestrictedRegion = true; + + *_retval = extractedImg.forget().get(); + return NS_OK; +} + //****************************************************************************** /* [noscript] void draw(in gfxContext aContext, * in gfxGraphicsFilter aFilter, @@ -664,19 +722,24 @@ VectorImage::Draw(gfxContext* aContext, mSVGDocumentWrapper->UpdateViewportBounds(aViewportSize); mSVGDocumentWrapper->FlushImageTransformInvalidation(); + nsIntSize imageSize = mHaveRestrictedRegion ? + mRestrictedRegion.Size() : aViewportSize; + // XXXdholbert Do we need to convert image size from // CSS pixels to dev pixels here? (is gfxCallbackDrawable's 2nd arg in dev // pixels?) - gfxIntSize imageSizeGfx(aViewportSize.width, aViewportSize.height); + gfxIntSize imageSizeGfx(imageSize.width, imageSize.height); // Based on imgFrame::Draw gfxRect sourceRect = aUserSpaceToImageSpace.Transform(aFill); - gfxRect imageRect(0, 0, aViewportSize.width, aViewportSize.height); + gfxRect imageRect(0, 0, imageSize.width, imageSize.height); gfxRect subimage(aSubimage.x, aSubimage.y, aSubimage.width, aSubimage.height); nsRefPtr cb = new SVGDrawingCallback(mSVGDocumentWrapper, + mHaveRestrictedRegion ? + mRestrictedRegion : nsIntRect(nsIntPoint(0, 0), aViewportSize), aFlags); @@ -687,9 +750,12 @@ VectorImage::Draw(gfxContext* aContext, subimage, sourceRect, imageRect, aFill, gfxASurface::ImageFormatARGB32, aFilter); - // Allow ourselves to fire FrameChanged and OnStopFrame again. - MOZ_ASSERT(mRenderingObserver, "Should have a rendering observer by now"); - mRenderingObserver->ResumeListening(); + MOZ_ASSERT(mRenderingObserver || mHaveRestrictedRegion, + "Should have a rendering observer by now unless ExtractFrame created us"); + if (mRenderingObserver) { + // Allow ourselves to fire FrameChanged and OnStopFrame again. + mRenderingObserver->ResumeListening(); + } return NS_OK; } diff --git a/image/src/VectorImage.h b/image/src/VectorImage.h index 80746ddf9ff..69a547b8256 100644 --- a/image/src/VectorImage.h +++ b/image/src/VectorImage.h @@ -84,11 +84,18 @@ private: nsRefPtr mLoadEventListener; nsRefPtr mParseCompleteListener; + nsIntRect mRestrictedRegion; // If we were created by + // ExtractFrame, this is the region + // that we're restricted to using. + // Otherwise, this is ignored. + bool mIsInitialized; // Have we been initalized? bool mIsFullyLoaded; // Has the SVG document finished loading? bool mIsDrawing; // Are we currently drawing? bool mHaveAnimations; // Is our SVG content SMIL-animated? // (Only set after mIsFullyLoaded.) + bool mHaveRestrictedRegion; // Are we a restricted-region clone + // created via ExtractFrame? friend class ImageFactory; }; diff --git a/image/src/imgRequestProxy.cpp b/image/src/imgRequestProxy.cpp index d5e99542254..d6ad4a34d0d 100644 --- a/image/src/imgRequestProxy.cpp +++ b/image/src/imgRequestProxy.cpp @@ -18,7 +18,7 @@ #include "nsCRT.h" #include "Image.h" -#include "ImageOps.h" +#include "ImageFactory.h" #include "nsError.h" #include "ImageLogging.h" @@ -915,7 +915,7 @@ imgRequestProxy::GetStaticRequest(imgRequestProxy** aReturn) } // We are animated. We need to create a frozen version of this image. - nsRefPtr frozenImage = ImageOps::Freeze(image); + nsRefPtr frozenImage = ImageFactory::Freeze(image); // Create a static imgRequestProxy with our new extracted frame. nsCOMPtr currentPrincipal; diff --git a/layout/base/nsCSSRendering.cpp b/layout/base/nsCSSRendering.cpp index c0c63b5bf9e..f2ccb4e5b98 100644 --- a/layout/base/nsCSSRendering.cpp +++ b/layout/base/nsCSSRendering.cpp @@ -30,7 +30,6 @@ #include "nsIScrollableFrame.h" #include "imgIRequest.h" #include "imgIContainer.h" -#include "ImageOps.h" #include "nsCSSRendering.h" #include "nsCSSColorUtils.h" #include "nsITheme.h" @@ -61,7 +60,6 @@ using namespace mozilla; using namespace mozilla::css; -using mozilla::image::ImageOps; static int gFrameTreeLockCount = 0; @@ -3316,10 +3314,20 @@ DrawBorderImageComponent(nsRenderingContext& aRenderingContext, if (aFill.IsEmpty() || aSrc.IsEmpty()) return; + // Don't bother trying to cache sub images if the border image is animated + // We can only sucessfully call GetAnimated() if we are fully decoded, so default to true + bool animated = true; + aImage->GetAnimated(&animated); + nsCOMPtr subImage; - if ((subImage = aStyleBorder.GetSubImage(aIndex)) == nullptr) { - subImage = ImageOps::Clip(aImage, aSrc); - aStyleBorder.SetSubImage(aIndex, subImage); + if (animated || (subImage = aStyleBorder.GetSubImage(aIndex)) == 0) { + if (NS_FAILED(aImage->ExtractFrame(imgIContainer::FRAME_CURRENT, aSrc, + imgIContainer::FLAG_SYNC_DECODE, + getter_AddRefs(subImage)))) + return; + + if (!animated) + aStyleBorder.SetSubImage(aIndex, subImage); } gfxPattern::GraphicsFilter graphicsFilter = @@ -4285,7 +4293,18 @@ nsImageRenderer::PrepareImage() // The cropped image is identical to the source image mImageContainer.swap(srcImage); } else { - nsCOMPtr subImage = ImageOps::Clip(srcImage, actualCropRect); + nsCOMPtr subImage; + uint32_t aExtractFlags = (mFlags & FLAG_SYNC_DECODE_IMAGES) + ? (uint32_t) imgIContainer::FLAG_SYNC_DECODE + : (uint32_t) imgIContainer::FLAG_NONE; + nsresult rv = srcImage->ExtractFrame(imgIContainer::FRAME_CURRENT, + actualCropRect, aExtractFlags, + getter_AddRefs(subImage)); + if (NS_FAILED(rv)) { + NS_WARNING("The cropped image contains no pixels to draw; " + "maybe the crop rect is outside the image frame rect"); + return false; + } mImageContainer.swap(subImage); } }