• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2018 Google Inc.
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 SkMaskFilterBase_DEFINED
9 #define SkMaskFilterBase_DEFINED
10 
11 #include "include/core/SkFlattenable.h"
12 #include "include/core/SkMaskFilter.h"
13 #include "include/core/SkPoint.h"
14 #include "include/core/SkRect.h"
15 #include "include/core/SkRefCnt.h"
16 #include "include/core/SkScalar.h"
17 #include "include/core/SkSpan.h"
18 #include "include/core/SkStrokeRec.h"
19 #include "include/private/base/SkNoncopyable.h"
20 #include "src/core/SkMask.h"
21 
22 #include <optional>
23 
24 class SkBlitter;
25 class SkImageFilter;
26 class SkCachedData;
27 class SkMatrix;
28 class SkPath;
29 class SkRRect;
30 class SkRasterClip;
31 enum SkBlurStyle : int;
32 
33 class SkMaskFilterBase : public SkMaskFilter {
34 public:
35     /** Returns the format of the resulting mask that this subclass will return
36         when its filterMask() method is called.
37     */
38     virtual SkMask::Format getFormat() const = 0;
39 
40     /** Create a new mask by filter the src mask.
41         If src.fImage == null, then do not allocate or create the dst image
42         but do fill out the other fields in dstMask.
43         If you do allocate a dst image, use SkMask::AllocImage()
44         If this returns false, dst mask is ignored.
45         @param  dst the result of the filter. If src.fImage == null, dst should not allocate its image
46         @param src the original image to be filtered.
47         @param matrix the CTM
48         @param margin   if not null, return the buffer dx/dy need when calculating the effect. Used when
49                         drawing a clipped object to know how much larger to allocate the src before
50                         applying the filter. If returning false, ignore this parameter.
51         @return true if the dst mask was correctly created.
52     */
53     virtual bool filterMask(SkMaskBuilder* dst, const SkMask& src, const SkMatrix&,
54                             SkIPoint* margin) const = 0;
55 
56     enum class Type {
57         kBlur,
58         kEmboss,
59         kSDF,
60         kShader,
61         kTable,
62     };
63 
64     virtual Type type() const = 0;
65 
66     /**
67      * The fast bounds function is used to enable the paint to be culled early
68      * in the drawing pipeline. This function accepts the current bounds of the
69      * paint as its src param and the filter adjust those bounds using its
70      * current mask and returns the result using the dest param. Callers are
71      * allowed to provide the same struct for both src and dest so each
72      * implementation must accommodate that behavior.
73      *
74      *  The default impl calls filterMask with the src mask having no image,
75      *  but subclasses may override this if they can compute the rect faster.
76      */
77     virtual void computeFastBounds(const SkRect& src, SkRect* dest) const;
78 
79     struct BlurRec {
80         SkScalar        fSigma;
81         SkBlurStyle     fStyle;
82     };
83     /**
84      *  If this filter can be represented by a BlurRec, return true and (if not null) fill in the
85      *  provided BlurRec parameter. If this effect cannot be represented as a BlurRec, return false
86      *  and ignore the BlurRec parameter.
87      */
88     virtual bool asABlur(BlurRec*) const;
89 
90     /**
91      * Return an SkImageFilter representation of this mask filter that SkCanvas can apply to an
92      * alpha-only image to produce an equivalent effect to running the mask filter directly.
93      */
94     virtual sk_sp<SkImageFilter> asImageFilter(const SkMatrix& ctm) const;
95 
GetFlattenableType()96     static SkFlattenable::Type GetFlattenableType() {
97         return kSkMaskFilter_Type;
98     }
99 
getFlattenableType()100     SkFlattenable::Type getFlattenableType() const override {
101         return kSkMaskFilter_Type;
102     }
103 
104 protected:
SkMaskFilterBase()105     SkMaskFilterBase() {}
106 
107     enum class FilterReturn {
108         kFalse,
109         kTrue,
110         kUnimplemented,
111     };
112 
113     class NinePatch final : ::SkNoncopyable {
114     public:
NinePatch(const SkMask & mask,SkIRect outerRect,SkIPoint center,SkCachedData * cache)115         NinePatch(const SkMask& mask, SkIRect outerRect, SkIPoint center, SkCachedData* cache)
116             : fMask(mask), fOuterRect(outerRect), fCenter(center), fCache(cache) {}
117         NinePatch(NinePatch&&) = delete;  // the transfer of fCache makes this not work
118         ~NinePatch();
119 
120         SkMask      fMask;      // fBounds must have [0,0] in its top-left
121         SkIRect     fOuterRect; // width/height must be >= fMask.fBounds'
122         SkIPoint    fCenter;    // identifies center row/col for stretching
123         SkCachedData* fCache = nullptr;
124     };
125 
126     /**
127      *  As an optimization, some filters can be applied to a smaller nine-patch
128      *  instead of the full-sized rectangle. These nine-patches are not only smaller,
129      *  but more re-usable/cacheable. Then, when drawing/blitting, the ninepatch
130      *  can be expanded to the desired size.
131      *
132      *  Override if your subclass can filter a rect, and return the answer as
133      *  a ninepatch mask to be stretched over the returned outerRect. On success
134      *  return FilterReturn::kTrue. On failure (e.g. out of memory) return
135      *  FilterReturn::kFalse. If the normal filterMask() entry-point should be
136      *  called (the default) return FilterReturn::kUnimplemented.
137      *
138      *  By convention, the caller will take the center rol/col from the returned
139      *  mask as the slice it can replicate horizontally and vertically as we
140      *  stretch the mask to fit inside outerRect. It is an error for outerRect
141      *  to be smaller than the mask's bounds. This would imply that the width
142      *  and height of the mask should be odd. This is not required, just that
143      *  the caller will call mask.fBounds.centerX() and centerY() to find the
144      *  strips that will be replicated.
145      */
146     virtual FilterReturn filterRectsToNine(SkSpan<const SkRect>,
147                                            const SkMatrix&,
148                                            const SkIRect& clipBounds,
149                                            std::optional<NinePatch>*) const;
150     /**
151      *  Similar to filterRectsToNine, except it performs the work on a round rect.
152      */
153     virtual std::optional<NinePatch> filterRRectToNine(const SkRRect&,
154                                                        const SkMatrix&,
155                                                        const SkIRect& clipBounds) const;
156 
157 private:
158     friend class SkDraw;
159     friend class SkDrawBase;
160 
161     /** Helper method that, given a path in device space, will rasterize it into a kA8_Format mask
162      and then call filterMask(). If this returns true, the specified blitter will be called
163      to render that mask. Returns false if filterMask() returned false.
164      This method is not exported to java.
165      */
166     bool filterPath(const SkPath& devPath, const SkMatrix& ctm, const SkRasterClip&, SkBlitter*,
167                     SkStrokeRec::InitStyle) const;
168 
169     /** Helper method that, given a roundRect in device space, will rasterize it into a kA8_Format
170      mask and then call filterMask(). If this returns true, the specified blitter will be called
171      to render that mask. Returns false if filterMask() returned false.
172      */
173     bool filterRRect(const SkRRect& devRRect,
174                      const SkMatrix& ctm,
175                      const SkRasterClip&,
176                      SkBlitter*) const;
177 };
178 
as_MFB(SkMaskFilter * mf)179 inline SkMaskFilterBase* as_MFB(SkMaskFilter* mf) {
180     return static_cast<SkMaskFilterBase*>(mf);
181 }
182 
as_MFB(const SkMaskFilter * mf)183 inline const SkMaskFilterBase* as_MFB(const SkMaskFilter* mf) {
184     return static_cast<const SkMaskFilterBase*>(mf);
185 }
186 
as_MFB(const sk_sp<SkMaskFilter> & mf)187 inline const SkMaskFilterBase* as_MFB(const sk_sp<SkMaskFilter>& mf) {
188     return static_cast<SkMaskFilterBase*>(mf.get());
189 }
190 
191 // For RegisterFlattenables access to the blur mask filter implementation
192 extern void sk_register_blur_maskfilter_createproc();
193 
194 #endif
195