ClippedImage.h 4.0 KB

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