1
2/*
3 * Copyright 2006 The Android Open Source Project
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9
10#ifndef SkMaskFilter_DEFINED
11#define SkMaskFilter_DEFINED
12
13#include "SkFlattenable.h"
14#include "SkMask.h"
15#include "SkPaint.h"
16
17class GrContext;
18class SkBitmap;
19class SkBlitter;
20class SkBounder;
21class SkMatrix;
22class SkPath;
23class SkRasterClip;
24class SkRRect;
25
26/** \class SkMaskFilter
27
28    SkMaskFilter is the base class for object that perform transformations on
29    an alpha-channel mask before drawing it. A subclass of SkMaskFilter may be
30    installed into a SkPaint. Once there, each time a primitive is drawn, it
31    is first scan converted into a SkMask::kA8_Format mask, and handed to the
32    filter, calling its filterMask() method. If this returns true, then the
33    new mask is used to render into the device.
34
35    Blur and emboss are implemented as subclasses of SkMaskFilter.
36*/
37class SK_API SkMaskFilter : public SkFlattenable {
38public:
39    SK_DECLARE_INST_COUNT(SkMaskFilter)
40
41    SkMaskFilter() {}
42
43    /** Returns the format of the resulting mask that this subclass will return
44        when its filterMask() method is called.
45    */
46    virtual SkMask::Format getFormat() const = 0;
47
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    */
61    virtual bool filterMask(SkMask* dst, const SkMask& src, const SkMatrix&,
62                            SkIPoint* margin) const;
63
64#if SK_SUPPORT_GPU
65    /**
66     *  Returns true if the filter can be expressed a single-pass
67     *  GrEffect, used to process this filter on the GPU, or false if
68     *  not.
69     *
70     *  If effect is non-NULL, a new GrEffect instance is stored
71     *  in it.  The caller assumes ownership of the stage, and it is up to the
72     *  caller to unref it.
73     */
74    virtual bool asNewEffect(GrEffectRef** effect, GrTexture*) const;
75
76    /**
77     *  Returns true if the filter can be processed on the GPU.  This is most
78     *  often used for multi-pass effects, where intermediate results must be
79     *  rendered to textures.  For single-pass effects, use asNewEffect().
80     *
81     *  'maskRect' returns the device space portion of the mask the the filter
82     *  needs. The mask passed into 'filterMaskGPU' should have the same extent
83     *  as 'maskRect' but be translated to the upper-left corner of the mask
84     *  (i.e., (maskRect.fLeft, maskRect.fTop) appears at (0, 0) in the mask).
85     */
86    virtual bool canFilterMaskGPU(const SkRect& devBounds,
87                                  const SkIRect& clipBounds,
88                                  const SkMatrix& ctm,
89                                  SkRect* maskRect) const;
90
91    /**
92     *  Perform this mask filter on the GPU.  This is most often used for
93     *  multi-pass effects, where intermediate results must be rendered to
94     *  textures.  For single-pass effects, use asNewEffect().  'src' is the
95     *  source image for processing, as a texture-backed bitmap.  'result' is
96     *  the destination bitmap, which should contain a texture-backed pixelref
97     *  on success. 'maskRect' should be the rect returned from canFilterMaskGPU.
98     */
99    bool filterMaskGPU(GrContext* context,
100                       const SkBitmap& src,
101                       const SkRect& maskRect,
102                       SkBitmap* result) const;
103
104    /**
105     *  This flavor of 'filterMaskGPU' provides a more direct means of accessing
106     *  the filtering capabilities. Setting 'canOverwriteSrc' can allow some
107     *  filters to skip the allocation of an additional texture.
108     */
109    virtual bool filterMaskGPU(GrTexture* src,
110                               const SkRect& maskRect,
111                               GrTexture** result,
112                               bool canOverwriteSrc) const;
113#endif
114
115    /**
116     * The fast bounds function is used to enable the paint to be culled early
117     * in the drawing pipeline. This function accepts the current bounds of the
118     * paint as its src param and the filter adjust those bounds using its
119     * current mask and returns the result using the dest param. Callers are
120     * allowed to provide the same struct for both src and dest so each
121     * implementation must accomodate that behavior.
122     *
123     *  The default impl calls filterMask with the src mask having no image,
124     *  but subclasses may override this if they can compute the rect faster.
125     */
126    virtual void computeFastBounds(const SkRect& src, SkRect* dest) const;
127
128    SkDEVCODE(virtual void toString(SkString* str) const = 0;)
129    SK_DEFINE_FLATTENABLE_TYPE(SkMaskFilter)
130
131protected:
132    // empty for now, but lets get our subclass to remember to init us for the future
133    SkMaskFilter(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) {}
134
135    enum FilterReturn {
136        kFalse_FilterReturn,
137        kTrue_FilterReturn,
138        kUnimplemented_FilterReturn
139    };
140
141    struct NinePatch {
142        SkMask      fMask;      // fBounds must have [0,0] in its top-left
143        SkIRect     fOuterRect; // width/height must be >= fMask.fBounds'
144        SkIPoint    fCenter;    // identifies center row/col for stretching
145    };
146
147    /**
148     *  Override if your subclass can filter a rect, and return the answer as
149     *  a ninepatch mask to be stretched over the returned outerRect. On success
150     *  return kTrue_FilterReturn. On failure (e.g. out of memory) return
151     *  kFalse_FilterReturn. If the normal filterMask() entry-point should be
152     *  called (the default) return kUnimplemented_FilterReturn.
153     *
154     *  By convention, the caller will take the center rol/col from the returned
155     *  mask as the slice it can replicate horizontally and vertically as we
156     *  stretch the mask to fit inside outerRect. It is an error for outerRect
157     *  to be smaller than the mask's bounds. This would imply that the width
158     *  and height of the mask should be odd. This is not required, just that
159     *  the caller will call mask.fBounds.centerX() and centerY() to find the
160     *  strips that will be replicated.
161     */
162    virtual FilterReturn filterRectsToNine(const SkRect[], int count,
163                                           const SkMatrix&,
164                                           const SkIRect& clipBounds,
165                                           NinePatch*) const;
166    /**
167     *  Similar to filterRectsToNine, except it performs the work on a round rect.
168     */
169    virtual FilterReturn filterRRectToNine(const SkRRect&, const SkMatrix&,
170                                           const SkIRect& clipBounds,
171                                           NinePatch*) const;
172
173private:
174    friend class SkDraw;
175
176    /** Helper method that, given a path in device space, will rasterize it into a kA8_Format mask
177     and then call filterMask(). If this returns true, the specified blitter will be called
178     to render that mask. Returns false if filterMask() returned false.
179     This method is not exported to java.
180     */
181    bool filterPath(const SkPath& devPath, const SkMatrix& devMatrix,
182                    const SkRasterClip&, SkBounder*, SkBlitter* blitter,
183                    SkPaint::Style style) const;
184
185    /** Helper method that, given a roundRect in device space, will rasterize it into a kA8_Format
186     mask and then call filterMask(). If this returns true, the specified blitter will be called
187     to render that mask. Returns false if filterMask() returned false.
188     */
189    bool filterRRect(const SkRRect& devRRect, const SkMatrix& devMatrix,
190                     const SkRasterClip&, SkBounder*, SkBlitter* blitter,
191                     SkPaint::Style style) const;
192
193    typedef SkFlattenable INHERITED;
194};
195
196#endif
197