Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
michael@0 | 2 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #ifndef GFX_BLUR_H |
michael@0 | 7 | #define GFX_BLUR_H |
michael@0 | 8 | |
michael@0 | 9 | #include "gfxTypes.h" |
michael@0 | 10 | #include "nsSize.h" |
michael@0 | 11 | #include "nsAutoPtr.h" |
michael@0 | 12 | #include "gfxPoint.h" |
michael@0 | 13 | #include "mozilla/RefPtr.h" |
michael@0 | 14 | |
michael@0 | 15 | class gfxContext; |
michael@0 | 16 | struct gfxRect; |
michael@0 | 17 | struct gfxRGBA; |
michael@0 | 18 | class gfxCornerSizes; |
michael@0 | 19 | class gfxMatrix; |
michael@0 | 20 | |
michael@0 | 21 | namespace mozilla { |
michael@0 | 22 | namespace gfx { |
michael@0 | 23 | class AlphaBoxBlur; |
michael@0 | 24 | class SourceSurface; |
michael@0 | 25 | class DrawTarget; |
michael@0 | 26 | } |
michael@0 | 27 | } |
michael@0 | 28 | |
michael@0 | 29 | /** |
michael@0 | 30 | * Implementation of a triple box blur approximation of a Gaussian blur. |
michael@0 | 31 | * |
michael@0 | 32 | * A Gaussian blur is good for blurring because, when done independently |
michael@0 | 33 | * in the horizontal and vertical directions, it matches the result that |
michael@0 | 34 | * would be obtained using a different (rotated) set of axes. A triple |
michael@0 | 35 | * box blur is a very close approximation of a Gaussian. |
michael@0 | 36 | * |
michael@0 | 37 | * Creates an 8-bit alpha channel context for callers to draw in, |
michael@0 | 38 | * spreads the contents of that context, blurs the contents, and applies |
michael@0 | 39 | * it as an alpha mask on a different existing context. |
michael@0 | 40 | * |
michael@0 | 41 | * A spread N makes each output pixel the maximum value of all source |
michael@0 | 42 | * pixels within a square of side length 2N+1 centered on the output pixel. |
michael@0 | 43 | * |
michael@0 | 44 | * A temporary surface is created in the Init function. The caller then draws |
michael@0 | 45 | * any desired content onto the context acquired through GetContext, and lastly |
michael@0 | 46 | * calls Paint to apply the blurred content as an alpha mask. |
michael@0 | 47 | */ |
michael@0 | 48 | class gfxAlphaBoxBlur |
michael@0 | 49 | { |
michael@0 | 50 | public: |
michael@0 | 51 | gfxAlphaBoxBlur(); |
michael@0 | 52 | |
michael@0 | 53 | ~gfxAlphaBoxBlur(); |
michael@0 | 54 | |
michael@0 | 55 | /** |
michael@0 | 56 | * Constructs a box blur and initializes the temporary surface. |
michael@0 | 57 | * @param aRect The coordinates of the surface to create in device units. |
michael@0 | 58 | * |
michael@0 | 59 | * @param aBlurRadius The blur radius in pixels. This is the radius of |
michael@0 | 60 | * the entire (triple) kernel function. Each individual box blur has |
michael@0 | 61 | * radius approximately 1/3 this value, or diameter approximately 2/3 |
michael@0 | 62 | * this value. This parameter should nearly always be computed using |
michael@0 | 63 | * CalculateBlurRadius, below. |
michael@0 | 64 | * |
michael@0 | 65 | * @param aDirtyRect A pointer to a dirty rect, measured in device units, |
michael@0 | 66 | * if available. This will be used for optimizing the blur operation. It |
michael@0 | 67 | * is safe to pass nullptr here. |
michael@0 | 68 | * |
michael@0 | 69 | * @param aSkipRect A pointer to a rect, measured in device units, that |
michael@0 | 70 | * represents an area where blurring is unnecessary and shouldn't be done |
michael@0 | 71 | * for speed reasons. It is safe to pass nullptr here. |
michael@0 | 72 | */ |
michael@0 | 73 | gfxContext* Init(const gfxRect& aRect, |
michael@0 | 74 | const gfxIntSize& aSpreadRadius, |
michael@0 | 75 | const gfxIntSize& aBlurRadius, |
michael@0 | 76 | const gfxRect* aDirtyRect, |
michael@0 | 77 | const gfxRect* aSkipRect); |
michael@0 | 78 | |
michael@0 | 79 | /** |
michael@0 | 80 | * Returns the context that should be drawn to supply the alpha mask to be |
michael@0 | 81 | * blurred. If the returned surface is null, then there was an error in |
michael@0 | 82 | * its creation. |
michael@0 | 83 | */ |
michael@0 | 84 | gfxContext* GetContext() |
michael@0 | 85 | { |
michael@0 | 86 | return mContext; |
michael@0 | 87 | } |
michael@0 | 88 | |
michael@0 | 89 | /** |
michael@0 | 90 | * Does the actual blurring/spreading and mask applying. Users of this |
michael@0 | 91 | * object must have drawn whatever they want to be blurred onto the internal |
michael@0 | 92 | * gfxContext returned by GetContext before calling this. |
michael@0 | 93 | * |
michael@0 | 94 | * @param aDestinationCtx The graphics context on which to apply the |
michael@0 | 95 | * blurred mask. |
michael@0 | 96 | */ |
michael@0 | 97 | void Paint(gfxContext* aDestinationCtx); |
michael@0 | 98 | |
michael@0 | 99 | /** |
michael@0 | 100 | * Calculates a blur radius that, when used with box blur, approximates |
michael@0 | 101 | * a Gaussian blur with the given standard deviation. The result of |
michael@0 | 102 | * this function should be used as the aBlurRadius parameter to Init, |
michael@0 | 103 | * above. |
michael@0 | 104 | */ |
michael@0 | 105 | static gfxIntSize CalculateBlurRadius(const gfxPoint& aStandardDeviation); |
michael@0 | 106 | |
michael@0 | 107 | /** |
michael@0 | 108 | * Blurs a coloured rectangle onto aDestinationCtx. This is equivalent |
michael@0 | 109 | * to calling Init(), drawing a rectangle onto the returned surface |
michael@0 | 110 | * and then calling Paint, but may let us optimize better in the |
michael@0 | 111 | * backend. |
michael@0 | 112 | * |
michael@0 | 113 | * @param aDestinationCtx The destination to blur to. |
michael@0 | 114 | * @param aRect The rectangle to blur in device pixels. |
michael@0 | 115 | * @param aCornerRadii Corner radii for aRect, if it is a rounded |
michael@0 | 116 | * rectangle. |
michael@0 | 117 | * @param aBlurRadius The standard deviation of the blur. |
michael@0 | 118 | * @param aShadowColor The color to draw the blurred shadow. |
michael@0 | 119 | * @param aDirtyRect An area in device pixels that is dirty and needs |
michael@0 | 120 | * to be redrawn. |
michael@0 | 121 | * @param aSkipRect An area in device pixels to avoid blurring over, |
michael@0 | 122 | * to prevent unnecessary work. |
michael@0 | 123 | */ |
michael@0 | 124 | static void BlurRectangle(gfxContext *aDestinationCtx, |
michael@0 | 125 | const gfxRect& aRect, |
michael@0 | 126 | gfxCornerSizes* aCornerRadii, |
michael@0 | 127 | const gfxPoint& aBlurStdDev, |
michael@0 | 128 | const gfxRGBA& aShadowColor, |
michael@0 | 129 | const gfxRect& aDirtyRect, |
michael@0 | 130 | const gfxRect& aSkipRect); |
michael@0 | 131 | |
michael@0 | 132 | |
michael@0 | 133 | |
michael@0 | 134 | protected: |
michael@0 | 135 | /** |
michael@0 | 136 | * The context of the temporary alpha surface. |
michael@0 | 137 | */ |
michael@0 | 138 | nsRefPtr<gfxContext> mContext; |
michael@0 | 139 | |
michael@0 | 140 | /** |
michael@0 | 141 | * The temporary alpha surface. |
michael@0 | 142 | */ |
michael@0 | 143 | nsAutoArrayPtr<unsigned char> mData; |
michael@0 | 144 | |
michael@0 | 145 | /** |
michael@0 | 146 | * The object that actually does the blurring for us. |
michael@0 | 147 | */ |
michael@0 | 148 | mozilla::gfx::AlphaBoxBlur *mBlur; |
michael@0 | 149 | }; |
michael@0 | 150 | |
michael@0 | 151 | #endif /* GFX_BLUR_H */ |