Update configs. IGNORE BROKEN CHANGESETS CLOSED TREE NO BUG a=release ba=release
[gecko.git] / gfx / 2d / Blur.h
blobe0f18c4c39cd3a33a9cdf7275a00effb7b056565
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef MOZILLA_GFX_BLUR_H_
8 #define MOZILLA_GFX_BLUR_H_
10 #include "mozilla/gfx/Rect.h"
11 #include "mozilla/gfx/Point.h"
12 #include "mozilla/CheckedInt.h"
14 namespace mozilla {
15 namespace gfx {
17 #ifdef _MSC_VER
18 # pragma warning(disable : 4251)
19 #endif
21 /**
22 * Implementation of a triple box blur approximation of a Gaussian blur.
24 * A Gaussian blur is good for blurring because, when done independently
25 * in the horizontal and vertical directions, it matches the result that
26 * would be obtained using a different (rotated) set of axes. A triple
27 * box blur is a very close approximation of a Gaussian.
29 * This is a "service" class; the constructors set up all the information
30 * based on the values and compute the minimum size for an 8-bit alpha
31 * channel context.
32 * The callers are responsible for creating and managing the backing surface
33 * and passing the pointer to the data to the Blur() method. This class does
34 * not retain the pointer to the data outside of the Blur() call.
36 * A spread N makes each output pixel the maximum value of all source
37 * pixels within a square of side length 2N+1 centered on the output pixel.
39 class GFX2D_API AlphaBoxBlur final {
40 public:
41 /** Constructs a box blur and computes the backing surface size.
43 * @param aRect The coordinates of the surface to create in device units.
45 * @param aBlurRadius The blur radius in pixels. This is the radius of the
46 * entire (triple) kernel function. Each individual box blur has radius
47 * approximately 1/3 this value, or diameter approximately 2/3 this value.
48 * This parameter should nearly always be computed using
49 * CalculateBlurRadius, below.
51 * @param aDirtyRect A pointer to a dirty rect, measured in device units, if
52 * available. This will be used for optimizing the blur operation. It is
53 * safe to pass nullptr here.
55 * @param aSkipRect A pointer to a rect, measured in device units, that
56 * represents an area where blurring is unnecessary and shouldn't be done
57 * for speed reasons. It is safe to pass nullptr here.
59 AlphaBoxBlur(const Rect& aRect, const IntSize& aSpreadRadius,
60 const IntSize& aBlurRadius, const Rect* aDirtyRect,
61 const Rect* aSkipRect);
63 AlphaBoxBlur(const Rect& aRect, int32_t aStride, float aSigmaX,
64 float aSigmaY);
66 AlphaBoxBlur();
68 void Init(const Rect& aRect, const IntSize& aSpreadRadius,
69 const IntSize& aBlurRadius, const Rect* aDirtyRect,
70 const Rect* aSkipRect);
72 ~AlphaBoxBlur();
74 /**
75 * Return the size, in pixels, of the 8-bit alpha surface we'd use.
77 IntSize GetSize() const;
79 /**
80 * Return the stride, in bytes, of the 8-bit alpha surface we'd use.
82 int32_t GetStride() const;
84 /**
85 * Returns the device-space rectangle the 8-bit alpha surface covers.
87 IntRect GetRect() const;
89 /**
90 * Return a pointer to a dirty rect, as passed in to the constructor, or
91 * nullptr if none was passed in.
93 Rect* GetDirtyRect();
95 /**
96 * Return the spread radius, in pixels.
98 IntSize GetSpreadRadius() const { return mSpreadRadius; }
101 * Return the blur radius, in pixels.
103 IntSize GetBlurRadius() const { return mBlurRadius; }
106 * Return the minimum buffer size that should be given to Blur() method. If
107 * zero, the class is not properly setup for blurring. Note that this
108 * includes the extra three bytes on top of the stride*width, where something
109 * like gfxImageSurface::GetDataSize() would report without it, even if it
110 * happens to have the extra bytes.
112 size_t GetSurfaceAllocationSize() const;
115 * Perform the blur in-place on the surface backed by specified 8-bit
116 * alpha surface data. The size must be at least that returned by
117 * GetSurfaceAllocationSize() or bad things will happen.
119 void Blur(uint8_t* aData) const;
122 * Calculates a blur radius that, when used with box blur, approximates a
123 * Gaussian blur with the given standard deviation. The result of this
124 * function should be used as the aBlurRadius parameter to AlphaBoxBlur's
125 * constructor, above.
127 static IntSize CalculateBlurRadius(const Point& aStandardDeviation);
128 static Float CalculateBlurSigma(int32_t aBlurRadius);
130 private:
131 void BoxBlur_C(uint8_t* aData, int32_t aLeftLobe, int32_t aRightLobe,
132 int32_t aTopLobe, int32_t aBottomLobe,
133 uint32_t* aIntegralImage, size_t aIntegralImageStride) const;
134 void BoxBlur_SSE2(uint8_t* aData, int32_t aLeftLobe, int32_t aRightLobe,
135 int32_t aTopLobe, int32_t aBottomLobe,
136 uint32_t* aIntegralImage,
137 size_t aIntegralImageStride) const;
138 void BoxBlur_NEON(uint8_t* aData, int32_t aLeftLobe, int32_t aRightLobe,
139 int32_t aTopLobe, int32_t aBottomLobe,
140 uint32_t* aIntegralImage,
141 size_t aIntegralImageStride) const;
142 #ifdef _MIPS_ARCH_LOONGSON3A
143 void BoxBlur_LS3(uint8_t* aData, int32_t aLeftLobe, int32_t aRightLobe,
144 int32_t aTopLobe, int32_t aBottomLobe,
145 uint32_t* aIntegralImage, size_t aIntegralImageStride) const;
146 #endif
148 static CheckedInt<int32_t> RoundUpToMultipleOf4(int32_t aVal);
151 * A rect indicating the area where blurring is unnecessary, and the blur
152 * algorithm should skip over it.
154 * This is guaranteed to be 4-pixel aligned in the x axis.
156 IntRect mSkipRect;
159 * The device-space rectangle the the backing 8-bit alpha surface covers.
161 IntRect mRect;
164 * A copy of the dirty rect passed to the constructor. This will only be valid
165 * if mHasDirtyRect is true.
167 Rect mDirtyRect;
170 * The spread radius, in pixels.
172 IntSize mSpreadRadius;
175 * The blur radius, in pixels.
177 IntSize mBlurRadius;
180 * The stride of the data passed to Blur()
182 int32_t mStride;
185 * The minimum size of the buffer needed for the Blur() operation.
187 size_t mSurfaceAllocationSize;
190 * Whether mDirtyRect contains valid data.
192 bool mHasDirtyRect;
195 } // namespace gfx
196 } // namespace mozilla
198 #endif /* MOZILLA_GFX_BLUR_H_ */