1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/thebes/gfxBlur.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,151 @@ 1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- 1.5 + * This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#ifndef GFX_BLUR_H 1.10 +#define GFX_BLUR_H 1.11 + 1.12 +#include "gfxTypes.h" 1.13 +#include "nsSize.h" 1.14 +#include "nsAutoPtr.h" 1.15 +#include "gfxPoint.h" 1.16 +#include "mozilla/RefPtr.h" 1.17 + 1.18 +class gfxContext; 1.19 +struct gfxRect; 1.20 +struct gfxRGBA; 1.21 +class gfxCornerSizes; 1.22 +class gfxMatrix; 1.23 + 1.24 +namespace mozilla { 1.25 + namespace gfx { 1.26 + class AlphaBoxBlur; 1.27 + class SourceSurface; 1.28 + class DrawTarget; 1.29 + } 1.30 +} 1.31 + 1.32 +/** 1.33 + * Implementation of a triple box blur approximation of a Gaussian blur. 1.34 + * 1.35 + * A Gaussian blur is good for blurring because, when done independently 1.36 + * in the horizontal and vertical directions, it matches the result that 1.37 + * would be obtained using a different (rotated) set of axes. A triple 1.38 + * box blur is a very close approximation of a Gaussian. 1.39 + * 1.40 + * Creates an 8-bit alpha channel context for callers to draw in, 1.41 + * spreads the contents of that context, blurs the contents, and applies 1.42 + * it as an alpha mask on a different existing context. 1.43 + * 1.44 + * A spread N makes each output pixel the maximum value of all source 1.45 + * pixels within a square of side length 2N+1 centered on the output pixel. 1.46 + * 1.47 + * A temporary surface is created in the Init function. The caller then draws 1.48 + * any desired content onto the context acquired through GetContext, and lastly 1.49 + * calls Paint to apply the blurred content as an alpha mask. 1.50 + */ 1.51 +class gfxAlphaBoxBlur 1.52 +{ 1.53 +public: 1.54 + gfxAlphaBoxBlur(); 1.55 + 1.56 + ~gfxAlphaBoxBlur(); 1.57 + 1.58 + /** 1.59 + * Constructs a box blur and initializes the temporary surface. 1.60 + * @param aRect The coordinates of the surface to create in device units. 1.61 + * 1.62 + * @param aBlurRadius The blur radius in pixels. This is the radius of 1.63 + * the entire (triple) kernel function. Each individual box blur has 1.64 + * radius approximately 1/3 this value, or diameter approximately 2/3 1.65 + * this value. This parameter should nearly always be computed using 1.66 + * CalculateBlurRadius, below. 1.67 + * 1.68 + * @param aDirtyRect A pointer to a dirty rect, measured in device units, 1.69 + * if available. This will be used for optimizing the blur operation. It 1.70 + * is safe to pass nullptr here. 1.71 + * 1.72 + * @param aSkipRect A pointer to a rect, measured in device units, that 1.73 + * represents an area where blurring is unnecessary and shouldn't be done 1.74 + * for speed reasons. It is safe to pass nullptr here. 1.75 + */ 1.76 + gfxContext* Init(const gfxRect& aRect, 1.77 + const gfxIntSize& aSpreadRadius, 1.78 + const gfxIntSize& aBlurRadius, 1.79 + const gfxRect* aDirtyRect, 1.80 + const gfxRect* aSkipRect); 1.81 + 1.82 + /** 1.83 + * Returns the context that should be drawn to supply the alpha mask to be 1.84 + * blurred. If the returned surface is null, then there was an error in 1.85 + * its creation. 1.86 + */ 1.87 + gfxContext* GetContext() 1.88 + { 1.89 + return mContext; 1.90 + } 1.91 + 1.92 + /** 1.93 + * Does the actual blurring/spreading and mask applying. Users of this 1.94 + * object must have drawn whatever they want to be blurred onto the internal 1.95 + * gfxContext returned by GetContext before calling this. 1.96 + * 1.97 + * @param aDestinationCtx The graphics context on which to apply the 1.98 + * blurred mask. 1.99 + */ 1.100 + void Paint(gfxContext* aDestinationCtx); 1.101 + 1.102 + /** 1.103 + * Calculates a blur radius that, when used with box blur, approximates 1.104 + * a Gaussian blur with the given standard deviation. The result of 1.105 + * this function should be used as the aBlurRadius parameter to Init, 1.106 + * above. 1.107 + */ 1.108 + static gfxIntSize CalculateBlurRadius(const gfxPoint& aStandardDeviation); 1.109 + 1.110 + /** 1.111 + * Blurs a coloured rectangle onto aDestinationCtx. This is equivalent 1.112 + * to calling Init(), drawing a rectangle onto the returned surface 1.113 + * and then calling Paint, but may let us optimize better in the 1.114 + * backend. 1.115 + * 1.116 + * @param aDestinationCtx The destination to blur to. 1.117 + * @param aRect The rectangle to blur in device pixels. 1.118 + * @param aCornerRadii Corner radii for aRect, if it is a rounded 1.119 + * rectangle. 1.120 + * @param aBlurRadius The standard deviation of the blur. 1.121 + * @param aShadowColor The color to draw the blurred shadow. 1.122 + * @param aDirtyRect An area in device pixels that is dirty and needs 1.123 + * to be redrawn. 1.124 + * @param aSkipRect An area in device pixels to avoid blurring over, 1.125 + * to prevent unnecessary work. 1.126 + */ 1.127 + static void BlurRectangle(gfxContext *aDestinationCtx, 1.128 + const gfxRect& aRect, 1.129 + gfxCornerSizes* aCornerRadii, 1.130 + const gfxPoint& aBlurStdDev, 1.131 + const gfxRGBA& aShadowColor, 1.132 + const gfxRect& aDirtyRect, 1.133 + const gfxRect& aSkipRect); 1.134 + 1.135 + 1.136 + 1.137 +protected: 1.138 + /** 1.139 + * The context of the temporary alpha surface. 1.140 + */ 1.141 + nsRefPtr<gfxContext> mContext; 1.142 + 1.143 + /** 1.144 + * The temporary alpha surface. 1.145 + */ 1.146 + nsAutoArrayPtr<unsigned char> mData; 1.147 + 1.148 + /** 1.149 + * The object that actually does the blurring for us. 1.150 + */ 1.151 + mozilla::gfx::AlphaBoxBlur *mBlur; 1.152 +}; 1.153 + 1.154 +#endif /* GFX_BLUR_H */