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     SK_DEFINE_FLATTENABLE_TYPE(SkPathEffect)
    110 
    111 protected:
    112     SkPathEffect(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) {}
    113 
    114 private:
    115     // illegal
    116     SkPathEffect(const SkPathEffect&);
    117     SkPathEffect& operator=(const SkPathEffect&);
    118 
    119     typedef SkFlattenable INHERITED;
    120 };
    121 
    122 /** \class SkPairPathEffect
    123 
    124     Common baseclass for Compose and Sum. This subclass manages two pathEffects,
    125     including flattening them. It does nothing in filterPath, and is only useful
    126     for managing the lifetimes of its two arguments.
    127 */
    128 class SkPairPathEffect : public SkPathEffect {
    129 public:
    130     SkPairPathEffect(SkPathEffect* pe0, SkPathEffect* pe1);
    131     virtual ~SkPairPathEffect();
    132 
    133 protected:
    134     SkPairPathEffect(SkFlattenableReadBuffer&);
    135     virtual void flatten(SkFlattenableWriteBuffer&) const SK_OVERRIDE;
    136 
    137     // these are visible to our subclasses
    138     SkPathEffect* fPE0, *fPE1;
    139 
    140 private:
    141     typedef SkPathEffect INHERITED;
    142 };
    143 
    144 /** \class SkComposePathEffect
    145 
    146     This subclass of SkPathEffect composes its two arguments, to create
    147     a compound pathEffect.
    148 */
    149 class SkComposePathEffect : public SkPairPathEffect {
    150 public:
    151     /** Construct a pathEffect whose effect is to apply first the inner pathEffect
    152         and the the outer pathEffect (e.g. outer(inner(path)))
    153         The reference counts for outer and inner are both incremented in the constructor,
    154         and decremented in the destructor.
    155     */
    156     SkComposePathEffect(SkPathEffect* outer, SkPathEffect* inner)
    157         : INHERITED(outer, inner) {}
    158 
    159     virtual bool filterPath(SkPath* dst, const SkPath& src,
    160                             SkStrokeRec*, const SkRect*) const SK_OVERRIDE;
    161 
    162     SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkComposePathEffect)
    163 
    164 protected:
    165     SkComposePathEffect(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) {}
    166 
    167 private:
    168     // illegal
    169     SkComposePathEffect(const SkComposePathEffect&);
    170     SkComposePathEffect& operator=(const SkComposePathEffect&);
    171 
    172     typedef SkPairPathEffect INHERITED;
    173 };
    174 
    175 /** \class SkSumPathEffect
    176 
    177     This subclass of SkPathEffect applies two pathEffects, one after the other.
    178     Its filterPath() returns true if either of the effects succeeded.
    179 */
    180 class SkSumPathEffect : public SkPairPathEffect {
    181 public:
    182     /** Construct a pathEffect whose effect is to apply two effects, in sequence.
    183         (e.g. first(path) + second(path))
    184         The reference counts for first and second are both incremented in the constructor,
    185         and decremented in the destructor.
    186     */
    187     SkSumPathEffect(SkPathEffect* first, SkPathEffect* second)
    188         : INHERITED(first, second) {}
    189 
    190     virtual bool filterPath(SkPath* dst, const SkPath& src,
    191                             SkStrokeRec*, const SkRect*) const SK_OVERRIDE;
    192 
    193     SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkSumPathEffect)
    194 
    195 protected:
    196     SkSumPathEffect(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) {}
    197 
    198 private:
    199     // illegal
    200     SkSumPathEffect(const SkSumPathEffect&);
    201     SkSumPathEffect& operator=(const SkSumPathEffect&);
    202 
    203     typedef SkPairPathEffect INHERITED;
    204 };
    205 
    206 #endif
    207