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