blob: 33e45552e89f95f5e1463798309a274db134977c [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@android.com8a1c16f2008-12-17 15:59:43 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2006 The Android Open Source Project
reed@android.com8a1c16f2008-12-17 15:59:43 +00004 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00005 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@android.com8a1c16f2008-12-17 15:59:43 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
reed@android.com8a1c16f2008-12-17 15:59:43 +000010#ifndef SkMaskFilter_DEFINED
11#define SkMaskFilter_DEFINED
12
13#include "SkFlattenable.h"
14#include "SkMask.h"
junov@chromium.org2ac4ef52012-04-04 15:16:51 +000015#include "SkPaint.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000016
senorblanco@chromium.orgfbcd4152013-10-23 14:03:22 +000017class GrContext;
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +000018class GrPaint;
robertphillips@google.com49149312013-07-03 15:34:35 +000019class SkBitmap;
reed@android.com8a1c16f2008-12-17 15:59:43 +000020class SkBlitter;
21class SkBounder;
22class SkMatrix;
23class SkPath;
reed@google.com045e62d2011-10-24 12:19:46 +000024class SkRasterClip;
scroggo@google.coma8e33a92013-11-08 18:02:53 +000025class SkRRect;
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +000026class SkStrokeRec;
reed@android.com8a1c16f2008-12-17 15:59:43 +000027
28/** \class SkMaskFilter
29
30 SkMaskFilter is the base class for object that perform transformations on
31 an alpha-channel mask before drawing it. A subclass of SkMaskFilter may be
32 installed into a SkPaint. Once there, each time a primitive is drawn, it
33 is first scan converted into a SkMask::kA8_Format mask, and handed to the
34 filter, calling its filterMask() method. If this returns true, then the
35 new mask is used to render into the device.
36
37 Blur and emboss are implemented as subclasses of SkMaskFilter.
38*/
bungeman@google.com4200dfe2012-10-13 17:13:18 +000039class SK_API SkMaskFilter : public SkFlattenable {
reed@android.com8a1c16f2008-12-17 15:59:43 +000040public:
robertphillips@google.com0456e0b2012-06-27 14:03:26 +000041 SK_DECLARE_INST_COUNT(SkMaskFilter)
42
reed@android.com8a1c16f2008-12-17 15:59:43 +000043 /** Returns the format of the resulting mask that this subclass will return
44 when its filterMask() method is called.
45 */
reed@google.com30711b72012-12-18 19:18:39 +000046 virtual SkMask::Format getFormat() const = 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +000047
48 /** Create a new mask by filter the src mask.
49 If src.fImage == null, then do not allocate or create the dst image
50 but do fill out the other fields in dstMask.
51 If you do allocate a dst image, use SkMask::AllocImage()
52 If this returns false, dst mask is ignored.
53 @param dst the result of the filter. If src.fImage == null, dst should not allocate its image
54 @param src the original image to be filtered.
55 @param matrix the CTM
56 @param margin if not null, return the buffer dx/dy need when calculating the effect. Used when
57 drawing a clipped object to know how much larger to allocate the src before
58 applying the filter. If returning false, ignore this parameter.
59 @return true if the dst mask was correctly created.
60 */
mike@reedtribe.org4135f282011-04-20 11:04:30 +000061 virtual bool filterMask(SkMask* dst, const SkMask& src, const SkMatrix&,
reed@google.com30711b72012-12-18 19:18:39 +000062 SkIPoint* margin) const;
reed@android.com8a1c16f2008-12-17 15:59:43 +000063
robertphillips@google.com49149312013-07-03 15:34:35 +000064#if SK_SUPPORT_GPU
65 /**
commit-bot@chromium.org41bf9302014-01-08 22:25:53 +000066 * Returns true if the filter can be expressed a single-pass GrEffect without requiring an
67 * explicit input mask. Per-pixel, the effect receives the incoming mask's coverage as
68 * the input color and outputs the filtered covereage value. This means that each pixel's
69 * filtered coverage must only depend on the unfiltered mask value for that pixel and not on
70 * surrounding values.
robertphillips@google.com49149312013-07-03 15:34:35 +000071 *
commit-bot@chromium.org41bf9302014-01-08 22:25:53 +000072 * If effect is non-NULL, a new GrEffect instance is stored in it. The caller assumes ownership
73 * of the effect and must unref it.
robertphillips@google.com49149312013-07-03 15:34:35 +000074 */
commit-bot@chromium.org41bf9302014-01-08 22:25:53 +000075 virtual bool asNewEffect(GrEffectRef** effect,
76 GrTexture*,
77 const SkMatrix& ctm) const;
reed@google.com2b75f422011-07-07 13:43:38 +000078
79 /**
commit-bot@chromium.org41bf9302014-01-08 22:25:53 +000080 * If asNewEffect() fails the filter may be implemented on the GPU by a subclass overriding
81 * filterMaskGPU (declared below). That code path requires constructing a src mask as input.
82 * Since that is a potentially expensive operation, the subclass must also override this
83 * function to indicate whether filterTextureMaskGPU would succeeed if the mask were to be
84 * created.
robertphillips@google.com49149312013-07-03 15:34:35 +000085 *
commit-bot@chromium.org41bf9302014-01-08 22:25:53 +000086 * 'maskRect' returns the device space portion of the mask that the filter needs. The mask
87 * passed into 'filterMaskGPU' should have the same extent as 'maskRect' but be translated
88 * to the upper-left corner of the mask (i.e., (maskRect.fLeft, maskRect.fTop) appears at
89 * (0, 0) in the mask).
reed@google.com2b75f422011-07-07 13:43:38 +000090 */
skia.committer@gmail.com1842adf2013-07-04 07:01:07 +000091 virtual bool canFilterMaskGPU(const SkRect& devBounds,
robertphillips@google.com49149312013-07-03 15:34:35 +000092 const SkIRect& clipBounds,
93 const SkMatrix& ctm,
94 SkRect* maskRect) const;
95
96 /**
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +000097 * Try to directly render the mask filter into the target. Returns
98 * true if drawing was successful.
99 */
100 virtual bool directFilterMaskGPU(GrContext* context,
101 GrPaint* grp,
102 const SkStrokeRec& strokeRec,
103 const SkPath& path) const;
104
105 /**
commit-bot@chromium.org41bf9302014-01-08 22:25:53 +0000106 * This function is used to implement filters that require an explicit src mask. It should only
107 * be called if canFilterMaskGPU returned true and the maskRect param should be the output from
108 * that call. canOverwriteSrc indicates whether the implementation may treat src as a scratch
109 * texture and overwrite its contents. When true it is also legal to return src as the result.
110 * Implementations are free to get the GrContext from the src texture in order to create
111 * additional textures and perform multiple passes.
robertphillips@google.com49149312013-07-03 15:34:35 +0000112 */
skia.committer@gmail.com1842adf2013-07-04 07:01:07 +0000113 virtual bool filterMaskGPU(GrTexture* src,
commit-bot@chromium.org41bf9302014-01-08 22:25:53 +0000114 const SkMatrix& ctm,
skia.committer@gmail.com1842adf2013-07-04 07:01:07 +0000115 const SkRect& maskRect,
robertphillips@google.com49149312013-07-03 15:34:35 +0000116 GrTexture** result,
117 bool canOverwriteSrc) const;
118#endif
reed@google.com2b75f422011-07-07 13:43:38 +0000119
reed@google.com9efd9a02012-01-30 15:41:43 +0000120 /**
121 * The fast bounds function is used to enable the paint to be culled early
122 * in the drawing pipeline. This function accepts the current bounds of the
123 * paint as its src param and the filter adjust those bounds using its
124 * current mask and returns the result using the dest param. Callers are
125 * allowed to provide the same struct for both src and dest so each
126 * implementation must accomodate that behavior.
127 *
128 * The default impl calls filterMask with the src mask having no image,
129 * but subclasses may override this if they can compute the rect faster.
130 */
reed@google.com30711b72012-12-18 19:18:39 +0000131 virtual void computeFastBounds(const SkRect& src, SkRect* dest) const;
reed@google.com9efd9a02012-01-30 15:41:43 +0000132
robertphillips@google.com0bd80fa2013-03-18 17:53:38 +0000133 SkDEVCODE(virtual void toString(SkString* str) const = 0;)
commit-bot@chromium.orgc0b7e102013-10-23 17:06:21 +0000134 SK_DEFINE_FLATTENABLE_TYPE(SkMaskFilter)
robertphillips@google.com0bd80fa2013-03-18 17:53:38 +0000135
reed@android.com8a1c16f2008-12-17 15:59:43 +0000136protected:
commit-bot@chromium.org7c9d0f32014-02-21 10:13:32 +0000137 SkMaskFilter() {}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000138 // empty for now, but lets get our subclass to remember to init us for the future
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000139 SkMaskFilter(SkReadBuffer& buffer) : INHERITED(buffer) {}
reed@google.com045e62d2011-10-24 12:19:46 +0000140
reed@google.comd729b3e2012-11-09 14:30:48 +0000141 enum FilterReturn {
142 kFalse_FilterReturn,
143 kTrue_FilterReturn,
144 kUnimplemented_FilterReturn
145 };
146
reed@google.comdab9b4f2012-11-19 16:45:14 +0000147 struct NinePatch {
148 SkMask fMask; // fBounds must have [0,0] in its top-left
149 SkIRect fOuterRect; // width/height must be >= fMask.fBounds'
150 SkIPoint fCenter; // identifies center row/col for stretching
151 };
152
reed@google.comd729b3e2012-11-09 14:30:48 +0000153 /**
154 * Override if your subclass can filter a rect, and return the answer as
155 * a ninepatch mask to be stretched over the returned outerRect. On success
156 * return kTrue_FilterReturn. On failure (e.g. out of memory) return
157 * kFalse_FilterReturn. If the normal filterMask() entry-point should be
158 * called (the default) return kUnimplemented_FilterReturn.
159 *
160 * By convention, the caller will take the center rol/col from the returned
161 * mask as the slice it can replicate horizontally and vertically as we
162 * stretch the mask to fit inside outerRect. It is an error for outerRect
163 * to be smaller than the mask's bounds. This would imply that the width
164 * and height of the mask should be odd. This is not required, just that
165 * the caller will call mask.fBounds.centerX() and centerY() to find the
166 * strips that will be replicated.
167 */
reed@google.comdab9b4f2012-11-19 16:45:14 +0000168 virtual FilterReturn filterRectsToNine(const SkRect[], int count,
169 const SkMatrix&,
170 const SkIRect& clipBounds,
reed@google.com30711b72012-12-18 19:18:39 +0000171 NinePatch*) const;
scroggo@google.coma8e33a92013-11-08 18:02:53 +0000172 /**
173 * Similar to filterRectsToNine, except it performs the work on a round rect.
174 */
175 virtual FilterReturn filterRRectToNine(const SkRRect&, const SkMatrix&,
176 const SkIRect& clipBounds,
177 NinePatch*) const;
reed@google.comd729b3e2012-11-09 14:30:48 +0000178
reed@google.com045e62d2011-10-24 12:19:46 +0000179private:
180 friend class SkDraw;
181
182 /** Helper method that, given a path in device space, will rasterize it into a kA8_Format mask
183 and then call filterMask(). If this returns true, the specified blitter will be called
184 to render that mask. Returns false if filterMask() returned false.
185 This method is not exported to java.
186 */
187 bool filterPath(const SkPath& devPath, const SkMatrix& devMatrix,
junov@chromium.org2ac4ef52012-04-04 15:16:51 +0000188 const SkRasterClip&, SkBounder*, SkBlitter* blitter,
reed@google.com30711b72012-12-18 19:18:39 +0000189 SkPaint::Style style) const;
djsollen@google.com54924242012-03-29 15:18:04 +0000190
scroggo@google.coma8e33a92013-11-08 18:02:53 +0000191 /** 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, const SkMatrix& devMatrix,
196 const SkRasterClip&, SkBounder*, SkBlitter* blitter,
197 SkPaint::Style style) const;
198
djsollen@google.com54924242012-03-29 15:18:04 +0000199 typedef SkFlattenable INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000200};
201
reed@android.com8a1c16f2008-12-17 15:59:43 +0000202#endif