1 /*
2  * Copyright 2006 The Android Open Source Project
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #ifndef SkBlurMaskFilter_DEFINED
9 #define SkBlurMaskFilter_DEFINED
10 
11 // we include this since our callers will need to at least be able to ref/unref
12 #include "SkMaskFilter.h"
13 #include "SkRect.h"
14 #include "SkScalar.h"
15 #include "SkBlurTypes.h"
16 
17 class SkRRect;
18 
19 class SK_API SkBlurMaskFilter {
20 public:
21     /**
22      *  If radius > 0, return the corresponding sigma, else return 0. Use this to convert from the
23      *  (legacy) idea of specify the blur "radius" to the standard notion of specifying its sigma.
24      */
25     static SkScalar ConvertRadiusToSigma(SkScalar radius);
26 
27     enum BlurFlags {
28         kNone_BlurFlag              = 0x00,
29         /** The blur layer's radius is not affected by transforms */
30         kIgnoreTransform_BlurFlag   = 0x01,
31         /** Use a smother, higher qulity blur algorithm */
32         kHighQuality_BlurFlag       = 0x02,
33         /** mask for all blur flags */
34         kAll_BlurFlag               = 0x03
35     };
36 
37     /** Create a blur maskfilter.
38      *  @param style     The SkBlurStyle to use
39      *  @param sigma     Standard deviation of the Gaussian blur to apply. Must be > 0.
40      *  @param occluder  The rect for which no pixels need be drawn (b.c. it will be overdrawn
41      *                   with some opaque object. This is just a hint which backends are free to
42      *                   ignore.
43      *  @param flags     Flags to use - defaults to none
44      *  @return The new blur maskfilter
45      */
46     static sk_sp<SkMaskFilter> Make(SkBlurStyle style, SkScalar sigma,
47                                     const SkRect& occluder, uint32_t flags = kNone_BlurFlag);
48 
49     static sk_sp<SkMaskFilter> Make(SkBlurStyle style, SkScalar sigma,
50                                     uint32_t flags = kNone_BlurFlag) {
51         return Make(style, sigma, SkRect::MakeEmpty(), flags);
52     }
53 
54 #ifdef SK_SUPPORT_LEGACY_EMBOSSMASKFILTER
55     /** Create an emboss maskfilter
56         @param blurSigma    standard deviation of the Gaussian blur to apply
57                             before applying lighting (e.g. 3)
58         @param direction    array of 3 scalars [x, y, z] specifying the direction of the light source
59         @param ambient      0...1 amount of ambient light
60         @param specular     coefficient for specular highlights (e.g. 8)
61         @return the emboss maskfilter
62     */
63     static sk_sp<SkMaskFilter> MakeEmboss(SkScalar blurSigma, const SkScalar direction[3],
64                                           SkScalar ambient, SkScalar specular);
65 #endif
66 
67     static const int kMaxDivisions = 6;
68 
69     // This method computes all the parameters for drawing a partially occluded nine-patched
70     // blurred rrect mask:
71     //   rrectToDraw - the integerized rrect to draw in the mask
72     //   widthHeight - how large to make the mask (rrectToDraw will be centered in this coord sys)
73     //   rectXs, rectYs - the x & y coordinates of the covering geometry lattice
74     //   texXs, texYs - the texture coordinate at each point in rectXs & rectYs
75     //   numXs, numYs - number of coordinates in the x & y directions
76     //   skipMask - bit mask that contains a 1-bit whenever one of the cells is occluded
77     // It returns true if 'devRRect' is nine-patchable
78     static bool ComputeBlurredRRectParams(const SkRRect& srcRRect, const SkRRect& devRRect,
79                                           const SkRect& occluder,
80                                           SkScalar sigma, SkScalar xformedSigma,
81                                           SkRRect* rrectToDraw,
82                                           SkISize* widthHeight,
83                                           SkScalar rectXs[kMaxDivisions],
84                                           SkScalar rectYs[kMaxDivisions],
85                                           SkScalar texXs[kMaxDivisions],
86                                           SkScalar texYs[kMaxDivisions],
87                                           int* numXs, int* numYs, uint32_t* skipMask);
88 
89     SK_DECLARE_FLATTENABLE_REGISTRAR_GROUP()
90 
91 private:
92     SkBlurMaskFilter(); // can't be instantiated
93 };
94 
95 #endif
96