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