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 SkPathEffect_DEFINED
     11 #define SkPathEffect_DEFINED
     12 
     13 #include "SkFlattenable.h"
     14 #include "SkPath.h"
     15 #include "SkPoint.h"
     16 #include "SkRect.h"
     17 #include "SkStrokeRec.h"
     18 #include "SkTDArray.h"
     19 
     20 class SkPath;
     21 
     22 /** \class SkPathEffect
     23 
     24     SkPathEffect is the base class for objects in the SkPaint that affect
     25     the geometry of a drawing primitive before it is transformed by the
     26     canvas' matrix and drawn.
     27 
     28     Dashing is implemented as a subclass of SkPathEffect.
     29 */
     30 class SK_API SkPathEffect : public SkFlattenable {
     31 public:
     32     SK_DECLARE_INST_COUNT(SkPathEffect)
     33 
     34     SkPathEffect() {}
     35 
     36     /**
     37      *  Given a src path (input) and a stroke-rec (input and output), apply
     38      *  this effect to the src path, returning the new path in dst, and return
     39      *  true. If this effect cannot be applied, return false and ignore dst
     40      *  and stroke-rec.
     41      *
     42      *  The stroke-rec specifies the initial request for stroking (if any).
     43      *  The effect can treat this as input only, or it can choose to change
     44      *  the rec as well. For example, the effect can decide to change the
     45      *  stroke's width or join, or the effect can change the rec from stroke
     46      *  to fill (or fill to stroke) in addition to returning a new (dst) path.
     47      *
     48      *  If this method returns true, the caller will apply (as needed) the
     49      *  resulting stroke-rec to dst and then draw.
     50      */
     51     virtual bool filterPath(SkPath* dst, const SkPath& src,
     52                             SkStrokeRec*, const SkRect* cullR) const = 0;
     53 
     54     /**
     55      *  Compute a conservative bounds for its effect, given the src bounds.
     56      *  The baseline implementation just assigns src to dst.
     57      */
     58     virtual void computeFastBounds(SkRect* dst, const SkRect& src) const;
     59 
     60     /** \class PointData
     61 
     62         PointData aggregates all the information needed to draw the point
     63         primitives returned by an 'asPoints' call.
     64     */
     65     class PointData {
     66     public:
     67         PointData()
     68             : fFlags(0)
     69             , fPoints(NULL)
     70             , fNumPoints(0) {
     71             fSize.set(SK_Scalar1, SK_Scalar1);
     72             // 'asPoints' needs to initialize/fill-in 'fClipRect' if it sets
     73             // the kUseClip flag
     74         };
     75         ~PointData() {
     76             delete [] fPoints;
     77         }
     78 
     79         // TODO: consider using passed-in flags to limit the work asPoints does.
     80         // For example, a kNoPath flag could indicate don't bother generating
     81         // stamped solutions.
     82 
     83         // Currently none of these flags are supported.
     84         enum PointFlags {
     85             kCircles_PointFlag            = 0x01,   // draw points as circles (instead of rects)
     86             kUsePath_PointFlag            = 0x02,   // draw points as stamps of the returned path
     87             kUseClip_PointFlag            = 0x04,   // apply 'fClipRect' before drawing the points
     88         };
     89 
     90         uint32_t           fFlags;      // flags that impact the drawing of the points
     91         SkPoint*           fPoints;     // the center point of each generated point
     92         int                fNumPoints;  // number of points in fPoints
     93         SkVector           fSize;       // the size to draw the points
     94         SkRect             fClipRect;   // clip required to draw the points (if kUseClip is set)
     95         SkPath             fPath;       // 'stamp' to be used at each point (if kUsePath is set)
     96 
     97         SkPath             fFirst;      // If not empty, contains geometry for first point
     98         SkPath             fLast;       // If not empty, contains geometry for last point
     99     };
    100 
    101     /**
    102      *  Does applying this path effect to 'src' yield a set of points? If so,
    103      *  optionally return the points in 'results'.
    104      */
    105     virtual bool asPoints(PointData* results, const SkPath& src,
    106                           const SkStrokeRec&, const SkMatrix&,
    107                           const SkRect* cullR) const;
    108 
    109 protected:
    110     SkPathEffect(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) {}
    111 
    112 private:
    113     // illegal
    114     SkPathEffect(const SkPathEffect&);
    115     SkPathEffect& operator=(const SkPathEffect&);
    116 
    117     typedef SkFlattenable INHERITED;
    118 };
    119 
    120 /** \class SkPairPathEffect
    121 
    122     Common baseclass for Compose and Sum. This subclass manages two pathEffects,
    123     including flattening them. It does nothing in filterPath, and is only useful
    124     for managing the lifetimes of its two arguments.
    125 */
    126 class SkPairPathEffect : public SkPathEffect {
    127 public:
    128     SkPairPathEffect(SkPathEffect* pe0, SkPathEffect* pe1);
    129     virtual ~SkPairPathEffect();
    130 
    131 protected:
    132     SkPairPathEffect(SkFlattenableReadBuffer&);
    133     virtual void flatten(SkFlattenableWriteBuffer&) const SK_OVERRIDE;
    134 
    135     // these are visible to our subclasses
    136     SkPathEffect* fPE0, *fPE1;
    137 
    138 private:
    139     typedef SkPathEffect INHERITED;
    140 };
    141 
    142 /** \class SkComposePathEffect
    143 
    144     This subclass of SkPathEffect composes its two arguments, to create
    145     a compound pathEffect.
    146 */
    147 class SkComposePathEffect : public SkPairPathEffect {
    148 public:
    149     /** Construct a pathEffect whose effect is to apply first the inner pathEffect
    150         and the the outer pathEffect (e.g. outer(inner(path)))
    151         The reference counts for outer and inner are both incremented in the constructor,
    152         and decremented in the destructor.
    153     */
    154     SkComposePathEffect(SkPathEffect* outer, SkPathEffect* inner)
    155         : INHERITED(outer, inner) {}
    156 
    157     virtual bool filterPath(SkPath* dst, const SkPath& src,
    158                             SkStrokeRec*, const SkRect*) const SK_OVERRIDE;
    159 
    160     SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkComposePathEffect)
    161 
    162 protected:
    163     SkComposePathEffect(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) {}
    164 
    165 private:
    166     // illegal
    167     SkComposePathEffect(const SkComposePathEffect&);
    168     SkComposePathEffect& operator=(const SkComposePathEffect&);
    169 
    170     typedef SkPairPathEffect INHERITED;
    171 };
    172 
    173 /** \class SkSumPathEffect
    174 
    175     This subclass of SkPathEffect applies two pathEffects, one after the other.
    176     Its filterPath() returns true if either of the effects succeeded.
    177 */
    178 class SkSumPathEffect : public SkPairPathEffect {
    179 public:
    180     /** Construct a pathEffect whose effect is to apply two effects, in sequence.
    181         (e.g. first(path) + second(path))
    182         The reference counts for first and second are both incremented in the constructor,
    183         and decremented in the destructor.
    184     */
    185     SkSumPathEffect(SkPathEffect* first, SkPathEffect* second)
    186         : INHERITED(first, second) {}
    187 
    188     virtual bool filterPath(SkPath* dst, const SkPath& src,
    189                             SkStrokeRec*, const SkRect*) const SK_OVERRIDE;
    190 
    191     SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkSumPathEffect)
    192 
    193 protected:
    194     SkSumPathEffect(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) {}
    195 
    196 private:
    197     // illegal
    198     SkSumPathEffect(const SkSumPathEffect&);
    199     SkSumPathEffect& operator=(const SkSumPathEffect&);
    200 
    201     typedef SkPairPathEffect INHERITED;
    202 };
    203 
    204 #endif
    205