• 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/SkNoncopyable.h"
17 #include "src/core/SkMask.h"
18 
19 class GrClip;
20 struct GrFPArgs;
21 class GrFragmentProcessor;
22 class GrPaint;
23 class GrRecordingContext;
24 class GrRenderTarget;
25 class GrRenderTargetContext;
26 class GrResourceProvider;
27 class GrShape;
28 class GrTexture;
29 class GrTextureProxy;
30 
31 class SkBitmap;
32 class SkBlitter;
33 class SkCachedData;
34 class SkMatrix;
35 class SkPath;
36 class SkRasterClip;
37 class SkRRect;
38 
39 class SkMaskFilterBase : public SkMaskFilter {
40 public:
41     /** Returns the format of the resulting mask that this subclass will return
42         when its filterMask() method is called.
43     */
44     virtual SkMask::Format getFormat() const = 0;
45 
46     /** Create a new mask by filter the src mask.
47         If src.fImage == null, then do not allocate or create the dst image
48         but do fill out the other fields in dstMask.
49         If you do allocate a dst image, use SkMask::AllocImage()
50         If this returns false, dst mask is ignored.
51         @param  dst the result of the filter. If src.fImage == null, dst should not allocate its image
52         @param src the original image to be filtered.
53         @param matrix the CTM
54         @param margin   if not null, return the buffer dx/dy need when calculating the effect. Used when
55                         drawing a clipped object to know how much larger to allocate the src before
56                         applying the filter. If returning false, ignore this parameter.
57         @return true if the dst mask was correctly created.
58     */
59     virtual bool filterMask(SkMask* dst, const SkMask& src, const SkMatrix&,
60                             SkIPoint* margin) const = 0;
61 
62 #if SK_SUPPORT_GPU
63     /**
64      *  Returns a processor if the filter can be expressed a single-pass GrProcessor without
65      *  requiring an explicit input mask. Per-pixel, the effect receives the incoming mask's
66      *  coverage as the input color and outputs the filtered covereage value. This means that each
67      *  pixel's filtered coverage must only depend on the unfiltered mask value for that pixel and
68      *  not on surrounding values.
69      */
70     std::unique_ptr<GrFragmentProcessor> asFragmentProcessor(const GrFPArgs& args) const;
71 
72     /**
73      *  Returns true iff asFragmentProcessor() will return a processor
74      */
75     bool hasFragmentProcessor() const;
76 
77     /**
78      *  If asFragmentProcessor() fails the filter may be implemented on the GPU by a subclass
79      *  overriding filterMaskGPU (declared below). That code path requires constructing a
80      *  src mask as input. Since that is a potentially expensive operation, the subclass must also
81      *  override this function to indicate whether filterTextureMaskGPU would succeeed if the mask
82      *  were to be created.
83      *
84      *  'maskRect' returns the device space portion of the mask that the filter needs. The mask
85      *  passed into 'filterMaskGPU' should have the same extent as 'maskRect' but be
86      *  translated to the upper-left corner of the mask (i.e., (maskRect.fLeft, maskRect.fTop)
87      *  appears at (0, 0) in the mask).
88      *
89      * Logically, how this works is:
90      *    canFilterMaskGPU is called
91      *    if (it returns true)
92      *        the returned mask rect is used for quick rejecting
93      *            the mask rect is used to generate the mask
94      *            filterMaskGPU is called to filter the mask
95      *
96      * TODO: this should work as:
97      *    if (canFilterMaskGPU(devShape, ...)) // rect, rrect, drrect, path
98      *        filterMaskGPU(devShape, ...)
99      * this would hide the RRect special case and the mask generation
100      */
101     virtual bool canFilterMaskGPU(const GrShape&,
102                                   const SkIRect& devSpaceShapeBounds,
103                                   const SkIRect& clipBounds,
104                                   const SkMatrix& ctm,
105                                   SkIRect* maskRect) const;
106 
107     /**
108      *  Try to directly render the mask filter into the target. Returns true if drawing was
109      *  successful. If false is returned then paint is unmodified.
110      */
111     virtual bool directFilterMaskGPU(GrRecordingContext*,
112                                      GrRenderTargetContext*,
113                                      GrPaint&& paint,
114                                      const GrClip&,
115                                      const SkMatrix& viewMatrix,
116                                      const GrShape& shape) const;
117 
118     /**
119      * This function is used to implement filters that require an explicit src mask. It should only
120      * be called if canFilterMaskGPU returned true and the maskRect param should be the output from
121      * that call.
122      * Implementations are free to get the GrContext from the src texture in order to create
123      * additional textures and perform multiple passes.
124      */
125     virtual sk_sp<GrTextureProxy> filterMaskGPU(GrRecordingContext*,
126                                                 sk_sp<GrTextureProxy> srcProxy,
127                                                 const SkMatrix& ctm,
128                                                 const SkIRect& maskRect) const;
129 #endif
130 
131     /**
132      * The fast bounds function is used to enable the paint to be culled early
133      * in the drawing pipeline. This function accepts the current bounds of the
134      * paint as its src param and the filter adjust those bounds using its
135      * current mask and returns the result using the dest param. Callers are
136      * allowed to provide the same struct for both src and dest so each
137      * implementation must accomodate that behavior.
138      *
139      *  The default impl calls filterMask with the src mask having no image,
140      *  but subclasses may override this if they can compute the rect faster.
141      */
142     virtual void computeFastBounds(const SkRect& src, SkRect* dest) const;
143 
144     struct BlurRec {
145         SkScalar        fSigma;
146         SkBlurStyle     fStyle;
147     };
148     /**
149      *  If this filter can be represented by a BlurRec, return true and (if not null) fill in the
150      *  provided BlurRec parameter. If this effect cannot be represented as a BlurRec, return false
151      *  and ignore the BlurRec parameter.
152      */
153     virtual bool asABlur(BlurRec*) const;
154 
155 protected:
SkMaskFilterBase()156     SkMaskFilterBase() {}
157 
158 #if SK_SUPPORT_GPU
159     virtual std::unique_ptr<GrFragmentProcessor> onAsFragmentProcessor(const GrFPArgs&) const;
160     virtual bool onHasFragmentProcessor() const;
161 #endif
162 
163     enum FilterReturn {
164         kFalse_FilterReturn,
165         kTrue_FilterReturn,
166         kUnimplemented_FilterReturn
167     };
168 
169     class NinePatch : ::SkNoncopyable {
170     public:
NinePatch()171         NinePatch() : fCache(nullptr) { }
172         ~NinePatch();
173 
174         SkMask      fMask;      // fBounds must have [0,0] in its top-left
175         SkIRect     fOuterRect; // width/height must be >= fMask.fBounds'
176         SkIPoint    fCenter;    // identifies center row/col for stretching
177         SkCachedData* fCache;
178     };
179 
180     /**
181      *  Override if your subclass can filter a rect, and return the answer as
182      *  a ninepatch mask to be stretched over the returned outerRect. On success
183      *  return kTrue_FilterReturn. On failure (e.g. out of memory) return
184      *  kFalse_FilterReturn. If the normal filterMask() entry-point should be
185      *  called (the default) return kUnimplemented_FilterReturn.
186      *
187      *  By convention, the caller will take the center rol/col from the returned
188      *  mask as the slice it can replicate horizontally and vertically as we
189      *  stretch the mask to fit inside outerRect. It is an error for outerRect
190      *  to be smaller than the mask's bounds. This would imply that the width
191      *  and height of the mask should be odd. This is not required, just that
192      *  the caller will call mask.fBounds.centerX() and centerY() to find the
193      *  strips that will be replicated.
194      */
195     virtual FilterReturn filterRectsToNine(const SkRect[], int count,
196                                            const SkMatrix&,
197                                            const SkIRect& clipBounds,
198                                            NinePatch*) const;
199     /**
200      *  Similar to filterRectsToNine, except it performs the work on a round rect.
201      */
202     virtual FilterReturn filterRRectToNine(const SkRRect&, const SkMatrix&,
203                                            const SkIRect& clipBounds,
204                                            NinePatch*) const;
205 
206 private:
207     friend class SkDraw;
208 
209     /** Helper method that, given a path in device space, will rasterize it into a kA8_Format mask
210      and then call filterMask(). If this returns true, the specified blitter will be called
211      to render that mask. Returns false if filterMask() returned false.
212      This method is not exported to java.
213      */
214     bool filterPath(const SkPath& devPath, const SkMatrix& ctm, const SkRasterClip&, SkBlitter*,
215                     SkStrokeRec::InitStyle) const;
216 
217     /** Helper method that, given a roundRect in device space, will rasterize it into a kA8_Format
218      mask and then call filterMask(). If this returns true, the specified blitter will be called
219      to render that mask. Returns false if filterMask() returned false.
220      */
221     bool filterRRect(const SkRRect& devRRect, const SkMatrix& ctm, const SkRasterClip&,
222                      SkBlitter*) const;
223 
224     typedef SkFlattenable INHERITED;
225 };
226 
as_MFB(SkMaskFilter * mf)227 inline SkMaskFilterBase* as_MFB(SkMaskFilter* mf) {
228     return static_cast<SkMaskFilterBase*>(mf);
229 }
230 
as_MFB(const SkMaskFilter * mf)231 inline const SkMaskFilterBase* as_MFB(const SkMaskFilter* mf) {
232     return static_cast<const SkMaskFilterBase*>(mf);
233 }
234 
as_MFB(const sk_sp<SkMaskFilter> & mf)235 inline const SkMaskFilterBase* as_MFB(const sk_sp<SkMaskFilter>& mf) {
236     return static_cast<SkMaskFilterBase*>(mf.get());
237 }
238 
239 #endif
240