gfxBlur.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  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 GFX_BLUR_H
  6. #define GFX_BLUR_H
  7. #include "gfxTypes.h"
  8. #include "nsSize.h"
  9. #include "gfxPoint.h"
  10. #include "mozilla/RefPtr.h"
  11. #include "mozilla/UniquePtr.h"
  12. class gfxContext;
  13. struct gfxRect;
  14. namespace mozilla {
  15. namespace gfx {
  16. class AlphaBoxBlur;
  17. struct Color;
  18. struct RectCornerRadii;
  19. class SourceSurface;
  20. class DrawTarget;
  21. } // namespace gfx
  22. } // namespace mozilla
  23. /**
  24. * Implementation of a triple box blur approximation of a Gaussian blur.
  25. *
  26. * A Gaussian blur is good for blurring because, when done independently
  27. * in the horizontal and vertical directions, it matches the result that
  28. * would be obtained using a different (rotated) set of axes. A triple
  29. * box blur is a very close approximation of a Gaussian.
  30. *
  31. * Creates an 8-bit alpha channel context for callers to draw in,
  32. * spreads the contents of that context, blurs the contents, and applies
  33. * it as an alpha mask on a different existing context.
  34. *
  35. * A spread N makes each output pixel the maximum value of all source
  36. * pixels within a square of side length 2N+1 centered on the output pixel.
  37. *
  38. * A temporary surface is created in the Init function. The caller then draws
  39. * any desired content onto the context acquired through GetContext, and lastly
  40. * calls Paint to apply the blurred content as an alpha mask.
  41. */
  42. class gfxAlphaBoxBlur
  43. {
  44. typedef mozilla::gfx::Color Color;
  45. typedef mozilla::gfx::DrawTarget DrawTarget;
  46. typedef mozilla::gfx::RectCornerRadii RectCornerRadii;
  47. public:
  48. gfxAlphaBoxBlur();
  49. ~gfxAlphaBoxBlur();
  50. /**
  51. * Constructs a box blur and initializes the temporary surface.
  52. * @param aRect The coordinates of the surface to create in device units.
  53. *
  54. * @param aBlurRadius The blur radius in pixels. This is the radius of
  55. * the entire (triple) kernel function. Each individual box blur has
  56. * radius approximately 1/3 this value, or diameter approximately 2/3
  57. * this value. This parameter should nearly always be computed using
  58. * CalculateBlurRadius, below.
  59. *
  60. * @param aDirtyRect A pointer to a dirty rect, measured in device units,
  61. * if available. This will be used for optimizing the blur operation. It
  62. * is safe to pass nullptr here.
  63. *
  64. * @param aSkipRect A pointer to a rect, measured in device units, that
  65. * represents an area where blurring is unnecessary and shouldn't be done
  66. * for speed reasons. It is safe to pass nullptr here.
  67. */
  68. gfxContext* Init(const gfxRect& aRect,
  69. const mozilla::gfx::IntSize& aSpreadRadius,
  70. const mozilla::gfx::IntSize& aBlurRadius,
  71. const gfxRect* aDirtyRect,
  72. const gfxRect* aSkipRect);
  73. /**
  74. * Returns the context that should be drawn to supply the alpha mask to be
  75. * blurred. If the returned surface is null, then there was an error in
  76. * its creation.
  77. */
  78. gfxContext* GetContext()
  79. {
  80. return mContext;
  81. }
  82. already_AddRefed<mozilla::gfx::SourceSurface>
  83. DoBlur(DrawTarget* aDT, mozilla::gfx::IntPoint* aTopLeft);
  84. /**
  85. * Does the actual blurring/spreading and mask applying. Users of this
  86. * object must have drawn whatever they want to be blurred onto the internal
  87. * gfxContext returned by GetContext before calling this.
  88. *
  89. * @param aDestinationCtx The graphics context on which to apply the
  90. * blurred mask.
  91. */
  92. void Paint(gfxContext* aDestinationCtx);
  93. /**
  94. * Calculates a blur radius that, when used with box blur, approximates
  95. * a Gaussian blur with the given standard deviation. The result of
  96. * this function should be used as the aBlurRadius parameter to Init,
  97. * above.
  98. */
  99. static mozilla::gfx::IntSize CalculateBlurRadius(const gfxPoint& aStandardDeviation);
  100. /**
  101. * Blurs a coloured rectangle onto aDestinationCtx. This is equivalent
  102. * to calling Init(), drawing a rectangle onto the returned surface
  103. * and then calling Paint, but may let us optimize better in the
  104. * backend.
  105. *
  106. * @param aDestinationCtx The destination to blur to.
  107. * @param aRect The rectangle to blur in device pixels.
  108. * @param aCornerRadii Corner radii for aRect, if it is a rounded
  109. * rectangle.
  110. * @param aBlurRadius The standard deviation of the blur.
  111. * @param aShadowColor The color to draw the blurred shadow.
  112. * @param aDirtyRect An area in device pixels that is dirty and needs
  113. * to be redrawn.
  114. * @param aSkipRect An area in device pixels to avoid blurring over,
  115. * to prevent unnecessary work.
  116. */
  117. static void BlurRectangle(gfxContext *aDestinationCtx,
  118. const gfxRect& aRect,
  119. RectCornerRadii* aCornerRadii,
  120. const gfxPoint& aBlurStdDev,
  121. const Color& aShadowColor,
  122. const gfxRect& aDirtyRect,
  123. const gfxRect& aSkipRect);
  124. static void ShutdownBlurCache();
  125. /***
  126. * Blurs an inset box shadow according to a given path.
  127. * This is equivalent to calling Init(), drawing the inset path,
  128. * and calling paint. Do not call Init() if using this method.
  129. *
  130. * @param aDestinationCtx The destination to blur to.
  131. * @param aDestinationRect The destination rect in device pixels
  132. * @param aShadowClipRect The destiniation inner rect of the
  133. * inset path in device pixels.
  134. * @param aBlurRadius The standard deviation of the blur.
  135. * @param aSpreadRadius The spread radius in device pixels.
  136. * @param aShadowColor The color of the blur.
  137. * @param aHasBorderRadius If this element also has a border radius
  138. * @param aInnerClipRadii Corner radii for the inside rect if it is a rounded rect.
  139. * @param aSKipRect An area in device pixels we don't have to paint in.
  140. */
  141. void BlurInsetBox(gfxContext* aDestinationCtx,
  142. const mozilla::gfx::Rect aDestinationRect,
  143. const mozilla::gfx::Rect aShadowClipRect,
  144. const mozilla::gfx::IntSize aBlurRadius,
  145. const mozilla::gfx::IntSize aSpreadRadius,
  146. const mozilla::gfx::Color& aShadowColor,
  147. const bool aHasBorderRadius,
  148. const RectCornerRadii& aInnerClipRadii,
  149. const mozilla::gfx::Rect aSkipRect,
  150. const mozilla::gfx::Point aShadowOffset);
  151. protected:
  152. already_AddRefed<mozilla::gfx::SourceSurface>
  153. GetInsetBlur(const mozilla::gfx::Rect aOuterRect,
  154. const mozilla::gfx::Rect aWhitespaceRect,
  155. const bool aIsDestRect,
  156. const mozilla::gfx::Color& aShadowColor,
  157. const mozilla::gfx::IntSize& aBlurRadius,
  158. const bool aHasBorderRadius,
  159. const RectCornerRadii& aInnerClipRadii,
  160. DrawTarget* aDestDrawTarget);
  161. /**
  162. * The context of the temporary alpha surface.
  163. */
  164. RefPtr<gfxContext> mContext;
  165. /**
  166. * The temporary alpha surface.
  167. */
  168. mozilla::UniquePtr<unsigned char[]> mData;
  169. /**
  170. * The object that actually does the blurring for us.
  171. */
  172. mozilla::UniquePtr<mozilla::gfx::AlphaBoxBlur> mBlur;
  173. };
  174. #endif /* GFX_BLUR_H */