gfx/2d/Blur.h

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

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

mercurial