• 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/SkBlurTypes.h"
12 #include "include/core/SkFlattenable.h"
13 #include "include/core/SkMaskFilter.h"
14 #include "include/core/SkPaint.h"
15 #include "include/core/SkStrokeRec.h"
16 #include "include/private/base/SkNoncopyable.h"
17 #include "include/private/gpu/ganesh/GrTypesPriv.h"
18 #include "src/base/SkTLazy.h"
19 #include "src/core/SkMask.h"
20 #include "src/gpu/ganesh/GrSurfaceProxyView.h"
21 
22 class GrClip;
23 struct GrFPArgs;
24 class GrFragmentProcessor;
25 class GrPaint;
26 class GrRecordingContext;
27 class GrRenderTarget;
28 namespace skgpu {
29 namespace ganesh {
30 class SurfaceDrawContext;
31 }
32 }  // namespace skgpu
33 class GrResourceProvider;
34 class GrStyledShape;
35 class GrSurfaceProxyView;
36 class GrTexture;
37 class GrTextureProxy;
38 
39 class SkBitmap;
40 class SkBlitter;
41 class SkCachedData;
42 class SkMatrix;
43 class SkPath;
44 class SkRasterClip;
45 class SkRRect;
46 
47 class SkMaskFilterBase : public SkMaskFilter {
48 public:
49     /** Returns the format of the resulting mask that this subclass will return
50         when its filterMask() method is called.
51     */
52     virtual SkMask::Format getFormat() const = 0;
53 
54 #ifdef SKIA_OHOS
55     /**
56      *  Try to directly render a rounded rect SDFshadow into the target.  Returns
57      *  true if drawing was successful.  If false is returned then paint is unmodified.
58      */
59     virtual bool directFilterRRectMaskGPU(GrRecordingContext* context,
60                                           skgpu::ganesh::SurfaceDrawContext* sdc,
61                                           GrPaint&& paint,
62                                           const GrClip* clip,
63                                           const SkMatrix& viewMatrix,
64                                           const GrStyledShape& shape,
65                                           const SkRRect& srcRRect) const;
66     /**
67      *  According to the advice in skia, We prefer to blur paths with small blur radii on the CPU.
68      */
69     virtual bool quick_check_gpu_draw(const SkMatrix& viewMatrix,
70                                       SkIRect& devSpaceShapeBounds) const;
71 #endif // SKIA_OHOS
72 
73     /** Create a new mask by filter the src mask.
74         If src.fImage == null, then do not allocate or create the dst image
75         but do fill out the other fields in dstMask.
76         If you do allocate a dst image, use SkMask::AllocImage()
77         If this returns false, dst mask is ignored.
78         @param  dst the result of the filter. If src.fImage == null, dst should not allocate its image
79         @param src the original image to be filtered.
80         @param matrix the CTM
81         @param margin   if not null, return the buffer dx/dy need when calculating the effect. Used when
82                         drawing a clipped object to know how much larger to allocate the src before
83                         applying the filter. If returning false, ignore this parameter.
84         @return true if the dst mask was correctly created.
85     */
86     virtual bool filterMask(SkMaskBuilder* dst, const SkMask& src, const SkMatrix&,
87                             SkIPoint* margin) const = 0;
88 
89     enum class Type {
90         kBlur,
91         kEmboss,
92         kSDF,
93         kShader,
94         kTable,
95     };
96 
97     virtual Type type() const = 0;
98 
99     /**
100      * The fast bounds function is used to enable the paint to be culled early
101      * in the drawing pipeline. This function accepts the current bounds of the
102      * paint as its src param and the filter adjust those bounds using its
103      * current mask and returns the result using the dest param. Callers are
104      * allowed to provide the same struct for both src and dest so each
105      * implementation must accommodate that behavior.
106      *
107      *  The default impl calls filterMask with the src mask having no image,
108      *  but subclasses may override this if they can compute the rect faster.
109      */
110     virtual void computeFastBounds(const SkRect& src, SkRect* dest) const;
111 
112     struct BlurRec {
113         SkScalar        fSigma;
114         SkBlurStyle     fStyle;
115     };
116     /**
117      *  If this filter can be represented by a BlurRec, return true and (if not null) fill in the
118      *  provided BlurRec parameter. If this effect cannot be represented as a BlurRec, return false
119      *  and ignore the BlurRec parameter.
120      */
121     virtual bool asABlur(BlurRec*) const;
122 
123     /**
124      * Return an SkImageFilter representation of this mask filter that SkCanvas can apply to an
125      * alpha-only image to produce an equivalent effect to running the mask filter directly.
126      */
127     virtual sk_sp<SkImageFilter> asImageFilter(const SkMatrix& ctm) const;
128 
GetFlattenableType()129     static SkFlattenable::Type GetFlattenableType() {
130         return kSkMaskFilter_Type;
131     }
132 
getFlattenableType()133     SkFlattenable::Type getFlattenableType() const override {
134         return kSkMaskFilter_Type;
135     }
136 
137 protected:
SkMaskFilterBase()138     SkMaskFilterBase() {}
139 
140     enum FilterReturn {
141         kFalse_FilterReturn,
142         kTrue_FilterReturn,
143         kUnimplemented_FilterReturn
144     };
145 
146     class NinePatch : ::SkNoncopyable {
147     public:
NinePatch(const SkMask & mask,SkIRect outerRect,SkIPoint center,SkCachedData * cache)148         NinePatch(const SkMask& mask, SkIRect outerRect, SkIPoint center, SkCachedData* cache)
149             : fMask(mask), fOuterRect(outerRect), fCenter(center), fCache(cache) {}
150         ~NinePatch();
151 
152         SkMask      fMask;      // fBounds must have [0,0] in its top-left
153         SkIRect     fOuterRect; // width/height must be >= fMask.fBounds'
154         SkIPoint    fCenter;    // identifies center row/col for stretching
155         SkCachedData* fCache = nullptr;
156     };
157 
158     /**
159      *  Override if your subclass can filter a rect, and return the answer as
160      *  a ninepatch mask to be stretched over the returned outerRect. On success
161      *  return kTrue_FilterReturn. On failure (e.g. out of memory) return
162      *  kFalse_FilterReturn. If the normal filterMask() entry-point should be
163      *  called (the default) return kUnimplemented_FilterReturn.
164      *
165      *  By convention, the caller will take the center rol/col from the returned
166      *  mask as the slice it can replicate horizontally and vertically as we
167      *  stretch the mask to fit inside outerRect. It is an error for outerRect
168      *  to be smaller than the mask's bounds. This would imply that the width
169      *  and height of the mask should be odd. This is not required, just that
170      *  the caller will call mask.fBounds.centerX() and centerY() to find the
171      *  strips that will be replicated.
172      */
173     virtual FilterReturn filterRectsToNine(const SkRect[], int count,
174                                            const SkMatrix&,
175                                            const SkIRect& clipBounds,
176                                            SkTLazy<NinePatch>*) const;
177     /**
178      *  Similar to filterRectsToNine, except it performs the work on a round rect.
179      */
180     virtual FilterReturn filterRRectToNine(const SkRRect&, const SkMatrix&,
181                                            const SkIRect& clipBounds,
182                                            SkTLazy<NinePatch>*) const;
183 
184 private:
185     friend class SkDraw;
186     friend class SkDrawBase;
187 
188     /** Helper method that, given a path in device space, will rasterize it into a kA8_Format mask
189      and then call filterMask(). If this returns true, the specified blitter will be called
190      to render that mask. Returns false if filterMask() returned false.
191      This method is not exported to java.
192      */
193     bool filterPath(const SkPath& devPath, const SkMatrix& ctm, const SkRasterClip&, SkBlitter*,
194                     SkStrokeRec::InitStyle) const;
195 
196     /** Helper method that, given a roundRect in device space, will rasterize it into a kA8_Format
197      mask and then call filterMask(). If this returns true, the specified blitter will be called
198      to render that mask. Returns false if filterMask() returned false.
199      */
200     bool filterRRect(const SkRRect& devRRect, const SkMatrix& ctm, const SkRasterClip&,
201                      SkBlitter*) const;
202 
203     using INHERITED = SkFlattenable;
204 };
205 
as_MFB(SkMaskFilter * mf)206 inline SkMaskFilterBase* as_MFB(SkMaskFilter* mf) {
207     return static_cast<SkMaskFilterBase*>(mf);
208 }
209 
as_MFB(const SkMaskFilter * mf)210 inline const SkMaskFilterBase* as_MFB(const SkMaskFilter* mf) {
211     return static_cast<const SkMaskFilterBase*>(mf);
212 }
213 
as_MFB(const sk_sp<SkMaskFilter> & mf)214 inline const SkMaskFilterBase* as_MFB(const sk_sp<SkMaskFilter>& mf) {
215     return static_cast<SkMaskFilterBase*>(mf.get());
216 }
217 
218 // For RegisterFlattenables access to the blur mask filter implementation
219 extern void sk_register_blur_maskfilter_createproc();
220 
221 #endif
222