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