ClippedImage.h (4070B)
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/. */ 5 6 #ifndef mozilla_image_ClippedImage_h 7 #define mozilla_image_ClippedImage_h 8 9 #include "ImageWrapper.h" 10 #include "mozilla/gfx/2D.h" 11 #include "mozilla/Maybe.h" 12 #include "mozilla/RefPtr.h" 13 #include "mozilla/UniquePtr.h" 14 15 #include <utility> 16 17 namespace mozilla { 18 namespace image { 19 20 class ClippedImageCachedSurface; 21 class DrawSingleTileCallback; 22 23 /** 24 * An Image wrapper that clips an image against a rectangle. Right now only 25 * absolute coordinates in pixels are supported. 26 * 27 * XXX(seth): There a known (performance, not correctness) issue with 28 * GetImageContainer. See the comments for that method for more information. 29 */ 30 class ClippedImage : public ImageWrapper { 31 typedef gfx::SourceSurface SourceSurface; 32 33 public: 34 NS_INLINE_DECL_REFCOUNTING_INHERITED(ClippedImage, ImageWrapper) 35 36 NS_IMETHOD GetWidth(int32_t* aWidth) override; 37 NS_IMETHOD GetHeight(int32_t* aHeight) override; 38 NS_IMETHOD GetIntrinsicSize(ImageIntrinsicSize* aIntrinsicSize) override; 39 NS_IMETHOD GetIntrinsicSizeInAppUnits(nsSize* aSize) override; 40 AspectRatio GetIntrinsicRatio() override; 41 NS_IMETHOD_(already_AddRefed<SourceSurface>) 42 GetFrame(uint32_t aWhichFrame, uint32_t aFlags) override; 43 NS_IMETHOD_(already_AddRefed<SourceSurface>) 44 GetFrameAtSize(const gfx::IntSize& aSize, uint32_t aWhichFrame, 45 uint32_t aFlags) override; 46 NS_IMETHOD_(bool) 47 IsImageContainerAvailable(WindowRenderer* aRenderer, 48 uint32_t aFlags) override; 49 NS_IMETHOD_(ImgDrawResult) 50 GetImageProvider(WindowRenderer* aRenderer, const gfx::IntSize& aSize, 51 const SVGImageContext& aSVGContext, 52 const Maybe<ImageIntRegion>& aRegion, uint32_t aFlags, 53 WebRenderImageProvider** aProvider) override; 54 NS_IMETHOD_(ImgDrawResult) 55 Draw(gfxContext* aContext, const nsIntSize& aSize, const ImageRegion& aRegion, 56 uint32_t aWhichFrame, gfx::SamplingFilter aSamplingFilter, 57 const SVGImageContext& aSVGContext, uint32_t aFlags, 58 float aOpacity) override; 59 NS_IMETHOD RequestDiscard() override; 60 NS_IMETHOD_(Orientation) GetOrientation() override; 61 NS_IMETHOD_(nsIntRect) 62 GetImageSpaceInvalidationRect(const nsIntRect& aRect) override; 63 nsIntSize OptimalImageSizeForDest(const gfxSize& aDest, uint32_t aWhichFrame, 64 gfx::SamplingFilter aSamplingFilter, 65 uint32_t aFlags) override; 66 67 protected: 68 ClippedImage(Image* aImage, nsIntRect aClip, 69 const Maybe<nsSize>& aSVGViewportSize); 70 71 virtual ~ClippedImage(); 72 73 private: 74 std::pair<ImgDrawResult, RefPtr<SourceSurface>> GetFrameInternal( 75 const nsIntSize& aSize, const SVGImageContext& aSVGContext, 76 const Maybe<ImageIntRegion>& aRegion, uint32_t aWhichFrame, 77 uint32_t aFlags, float aOpacity); 78 bool ShouldClip(); 79 ImgDrawResult DrawSingleTile(gfxContext* aContext, const nsIntSize& aSize, 80 const ImageRegion& aRegion, uint32_t aWhichFrame, 81 gfx::SamplingFilter aSamplingFilter, 82 const SVGImageContext& aSVGContext, 83 uint32_t aFlags, float aOpacity); 84 85 // If we are forced to draw a temporary surface, we cache it here. 86 UniquePtr<ClippedImageCachedSurface> mCachedSurface; 87 88 nsIntRect mClip; // The region to clip to. 89 Maybe<bool> mShouldClip; // Memoized ShouldClip() if present. 90 Maybe<nsIntSize> mSVGViewportSize; // If we're clipping a VectorImage, this 91 // is the size of viewport of that image. 92 friend class DrawSingleTileCallback; 93 friend class ImageOps; 94 }; 95 96 } // namespace image 97 } // namespace mozilla 98 99 #endif // mozilla_image_ClippedImage_h