Debian Codegraph @

src/core/SkMaskFilterBase.h

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