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