Home | History | Annotate | Download | only in core
      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 "SkBlurTypes.h"
     12 #include "SkFlattenable.h"
     13 #include "SkMask.h"
     14 #include "SkMaskFilter.h"
     15 #include "SkPaint.h"
     16 #include "SkStrokeRec.h"
     17 
     18 class GrClip;
     19 class GrContext;
     20 struct GrFPArgs;
     21 class GrRenderTargetContext;
     22 class GrPaint;
     23 class GrFragmentProcessor;
     24 class GrRenderTarget;
     25 class GrResourceProvider;
     26 class GrTexture;
     27 class GrTextureProxy;
     28 class SkBitmap;
     29 class SkBlitter;
     30 class SkCachedData;
     31 class SkMatrix;
     32 class SkPath;
     33 class SkRasterClip;
     34 class SkRRect;
     35 
     36 class SkMaskFilterBase : public SkMaskFilter {
     37 public:
     38     /** Returns the format of the resulting mask that this subclass will return
     39         when its filterMask() method is called.
     40     */
     41     virtual SkMask::Format getFormat() const = 0;
     42 
     43     /** Create a new mask by filter the src mask.
     44         If src.fImage == null, then do not allocate or create the dst image
     45         but do fill out the other fields in dstMask.
     46         If you do allocate a dst image, use SkMask::AllocImage()
     47         If this returns false, dst mask is ignored.
     48         @param  dst the result of the filter. If src.fImage == null, dst should not allocate its image
     49         @param src the original image to be filtered.
     50         @param matrix the CTM
     51         @param margin   if not null, return the buffer dx/dy need when calculating the effect. Used when
     52                         drawing a clipped object to know how much larger to allocate the src before
     53                         applying the filter. If returning false, ignore this parameter.
     54         @return true if the dst mask was correctly created.
     55     */
     56     virtual bool filterMask(SkMask* dst, const SkMask& src, const SkMatrix&,
     57                             SkIPoint* margin) const;
     58 
     59 #if SK_SUPPORT_GPU
     60     /**
     61      *  Returns a processor if the filter can be expressed a single-pass GrProcessor without
     62      *  requiring an explicit input mask. Per-pixel, the effect receives the incoming mask's
     63      *  coverage as the input color and outputs the filtered covereage value. This means that each
     64      *  pixel's filtered coverage must only depend on the unfiltered mask value for that pixel and
     65      *  not on surrounding values.
     66      */
     67     std::unique_ptr<GrFragmentProcessor> asFragmentProcessor(const GrFPArgs& args) const;
     68 
     69     /**
     70      *  Returns true iff asFragmentProcessor() will return a processor
     71      */
     72     bool hasFragmentProcessor() const;
     73 
     74     /**
     75      *  If asFragmentProcessor() fails the filter may be implemented on the GPU by a subclass
     76      *  overriding filterMaskGPU (declared below). That code path requires constructing a
     77      *  src mask as input. Since that is a potentially expensive operation, the subclass must also
     78      *  override this function to indicate whether filterTextureMaskGPU would succeeed if the mask
     79      *  were to be created.
     80      *
     81      *  'maskRect' returns the device space portion of the mask that the filter needs. The mask
     82      *  passed into 'filterMaskGPU' should have the same extent as 'maskRect' but be
     83      *  translated to the upper-left corner of the mask (i.e., (maskRect.fLeft, maskRect.fTop)
     84      *  appears at (0, 0) in the mask).
     85      *
     86      * Logically, how this works is:
     87      *    canFilterMaskGPU is called
     88      *    if (it returns true)
     89      *        the returned mask rect is used for quick rejecting
     90      *        either directFilterMaskGPU or directFilterRRectMaskGPU is then called
     91      *        if (neither of them handle the blur)
     92      *            the mask rect is used to generate the mask
     93      *            filterMaskGPU is called to filter the mask
     94      *
     95      * TODO: this should work as:
     96      *    if (canFilterMaskGPU(devShape, ...)) // rect, rrect, drrect, path
     97      *        filterMaskGPU(devShape, ...)
     98      * this would hide the RRect special case and the mask generation
     99      */
    100     virtual bool canFilterMaskGPU(const SkRRect& devRRect,
    101                                   const SkIRect& clipBounds,
    102                                   const SkMatrix& ctm,
    103                                   SkRect* maskRect) const;
    104 
    105     /**
    106      *  Try to directly render the mask filter into the target. Returns true if drawing was
    107      *  successful. If false is returned then paint is unmodified.
    108      */
    109     virtual bool directFilterMaskGPU(GrContext*,
    110                                      GrRenderTargetContext* renderTargetContext,
    111                                      GrPaint&& paint,
    112                                      const GrClip&,
    113                                      const SkMatrix& viewMatrix,
    114                                      const SkStrokeRec& strokeRec,
    115                                      const SkPath& path) const;
    116     /**
    117      *  Try to directly render a rounded rect mask filter into the target.  Returns
    118      *  true if drawing was successful.  If false is returned then paint is unmodified.
    119      */
    120     virtual bool directFilterRRectMaskGPU(GrContext*,
    121                                           GrRenderTargetContext* renderTargetContext,
    122                                           GrPaint&& paint,
    123                                           const GrClip&,
    124                                           const SkMatrix& viewMatrix,
    125                                           const SkStrokeRec& strokeRec,
    126                                           const SkRRect& rrect,
    127                                           const SkRRect& devRRect) const;
    128 
    129     /**
    130      * This function is used to implement filters that require an explicit src mask. It should only
    131      * be called if canFilterMaskGPU returned true and the maskRect param should be the output from
    132      * that call.
    133      * Implementations are free to get the GrContext from the src texture in order to create
    134      * additional textures and perform multiple passes.
    135      */
    136     virtual sk_sp<GrTextureProxy> filterMaskGPU(GrContext*,
    137                                                 sk_sp<GrTextureProxy> srcProxy,
    138                                                 const SkMatrix& ctm,
    139                                                 const SkIRect& maskRect) const;
    140 #endif
    141 
    142     /**
    143      * The fast bounds function is used to enable the paint to be culled early
    144      * in the drawing pipeline. This function accepts the current bounds of the
    145      * paint as its src param and the filter adjust those bounds using its
    146      * current mask and returns the result using the dest param. Callers are
    147      * allowed to provide the same struct for both src and dest so each
    148      * implementation must accomodate that behavior.
    149      *
    150      *  The default impl calls filterMask with the src mask having no image,
    151      *  but subclasses may override this if they can compute the rect faster.
    152      */
    153     virtual void computeFastBounds(const SkRect& src, SkRect* dest) const;
    154 
    155     struct BlurRec {
    156         SkScalar        fSigma;
    157         SkBlurStyle     fStyle;
    158         SkBlurQuality   fQuality;
    159     };
    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 
    167 protected:
    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 
    218 private:
    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 
    239 inline SkMaskFilterBase* as_MFB(SkMaskFilter* mf) {
    240     return static_cast<SkMaskFilterBase*>(mf);
    241 }
    242 
    243 inline const SkMaskFilterBase* as_MFB(const SkMaskFilter* mf) {
    244     return static_cast<const SkMaskFilterBase*>(mf);
    245 }
    246 
    247 inline const SkMaskFilterBase* as_MFB(const sk_sp<SkMaskFilter>& mf) {
    248     return static_cast<SkMaskFilterBase*>(mf.get());
    249 }
    250 
    251 #endif
    252