Bug 1645920 [wpt PR 24157] - [AspectRatio] Make intrinsic sizes respect aspect-ratio...
[gecko.git] / image / ClippedImage.h
blob7caddc99b7729a6ba8bd2015436152b1cb67f83e
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 #ifndef mozilla_image_ClippedImage_h
7 #define mozilla_image_ClippedImage_h
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"
15 #include <utility>
17 namespace mozilla {
18 namespace image {
20 class ClippedImageCachedSurface;
21 class DrawSingleTileCallback;
23 /**
24 * An Image wrapper that clips an image against a rectangle. Right now only
25 * absolute coordinates in pixels are supported.
27 * XXX(seth): There a known (performance, not correctness) issue with
28 * GetImageContainer. See the comments for that method for more information.
30 class ClippedImage : public ImageWrapper {
31 typedef gfx::SourceSurface SourceSurface;
33 public:
34 NS_INLINE_DECL_REFCOUNTING_INHERITED(ClippedImage, ImageWrapper)
36 NS_IMETHOD GetWidth(int32_t* aWidth) override;
37 NS_IMETHOD GetHeight(int32_t* aHeight) override;
38 NS_IMETHOD GetIntrinsicSize(nsSize* aSize) override;
39 Maybe<AspectRatio> GetIntrinsicRatio() override;
40 NS_IMETHOD_(already_AddRefed<SourceSurface>)
41 GetFrame(uint32_t aWhichFrame, uint32_t aFlags) override;
42 NS_IMETHOD_(already_AddRefed<SourceSurface>)
43 GetFrameAtSize(const gfx::IntSize& aSize, uint32_t aWhichFrame,
44 uint32_t aFlags) override;
45 NS_IMETHOD_(bool)
46 IsImageContainerAvailable(layers::LayerManager* aManager,
47 uint32_t aFlags) override;
48 NS_IMETHOD_(already_AddRefed<layers::ImageContainer>)
49 GetImageContainer(layers::LayerManager* aManager, uint32_t aFlags) override;
50 NS_IMETHOD_(bool)
51 IsImageContainerAvailableAtSize(layers::LayerManager* aManager,
52 const gfx::IntSize& aSize,
53 uint32_t aFlags) override;
54 NS_IMETHOD_(ImgDrawResult)
55 GetImageContainerAtSize(layers::LayerManager* aManager,
56 const gfx::IntSize& aSize,
57 const Maybe<SVGImageContext>& aSVGContext,
58 uint32_t aFlags,
59 layers::ImageContainer** aOutContainer) override;
60 NS_IMETHOD_(ImgDrawResult)
61 Draw(gfxContext* aContext, const nsIntSize& aSize, const ImageRegion& aRegion,
62 uint32_t aWhichFrame, gfx::SamplingFilter aSamplingFilter,
63 const Maybe<SVGImageContext>& aSVGContext, uint32_t aFlags,
64 float aOpacity) override;
65 NS_IMETHOD RequestDiscard() override;
66 NS_IMETHOD_(Orientation) GetOrientation() override;
67 NS_IMETHOD_(nsIntRect)
68 GetImageSpaceInvalidationRect(const nsIntRect& aRect) override;
69 nsIntSize OptimalImageSizeForDest(const gfxSize& aDest, uint32_t aWhichFrame,
70 gfx::SamplingFilter aSamplingFilter,
71 uint32_t aFlags) override;
73 protected:
74 ClippedImage(Image* aImage, nsIntRect aClip,
75 const Maybe<nsSize>& aSVGViewportSize);
77 virtual ~ClippedImage();
79 private:
80 std::pair<ImgDrawResult, RefPtr<SourceSurface>> GetFrameInternal(
81 const nsIntSize& aSize, const Maybe<SVGImageContext>& aSVGContext,
82 uint32_t aWhichFrame, uint32_t aFlags, float aOpacity);
83 bool ShouldClip();
84 ImgDrawResult DrawSingleTile(gfxContext* aContext, const nsIntSize& aSize,
85 const ImageRegion& aRegion, uint32_t aWhichFrame,
86 gfx::SamplingFilter aSamplingFilter,
87 const Maybe<SVGImageContext>& aSVGContext,
88 uint32_t aFlags, float aOpacity);
90 // If we are forced to draw a temporary surface, we cache it here.
91 UniquePtr<ClippedImageCachedSurface> mCachedSurface;
93 nsIntRect mClip; // The region to clip to.
94 Maybe<bool> mShouldClip; // Memoized ShouldClip() if present.
95 Maybe<nsIntSize> mSVGViewportSize; // If we're clipping a VectorImage, this
96 // is the size of viewport of that image.
97 friend class DrawSingleTileCallback;
98 friend class ImageOps;
101 } // namespace image
102 } // namespace mozilla
104 #endif // mozilla_image_ClippedImage_h