1 /* 2 * Copyright 2006 The Android Open Source Project 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 SkPathEffect_DEFINED 9 #define SkPathEffect_DEFINED 10 11 #include "include/core/SkFlattenable.h" 12 #include "include/core/SkPath.h" 13 #include "include/core/SkPoint.h" 14 #include "include/core/SkRect.h" 15 16 class SkPath; 17 class SkStrokeRec; 18 19 /** \class SkPathEffect 20 21 SkPathEffect is the base class for objects in the SkPaint that affect 22 the geometry of a drawing primitive before it is transformed by the 23 canvas' matrix and drawn. 24 25 Dashing is implemented as a subclass of SkPathEffect. 26 */ 27 class SK_API SkPathEffect : public SkFlattenable { 28 public: 29 /** 30 * Returns a patheffect that apples each effect (first and second) to the original path, 31 * and returns a path with the sum of these. 32 * 33 * result = first(path) + second(path) 34 * 35 */ 36 static sk_sp<SkPathEffect> MakeSum(sk_sp<SkPathEffect> first, sk_sp<SkPathEffect> second); 37 38 /** 39 * Returns a patheffect that applies the inner effect to the path, and then applies the 40 * outer effect to the result of the inner's. 41 * 42 * result = outer(inner(path)) 43 */ 44 static sk_sp<SkPathEffect> MakeCompose(sk_sp<SkPathEffect> outer, sk_sp<SkPathEffect> inner); 45 46 /** 47 * Given a src path (input) and a stroke-rec (input and output), apply 48 * this effect to the src path, returning the new path in dst, and return 49 * true. If this effect cannot be applied, return false and ignore dst 50 * and stroke-rec. 51 * 52 * The stroke-rec specifies the initial request for stroking (if any). 53 * The effect can treat this as input only, or it can choose to change 54 * the rec as well. For example, the effect can decide to change the 55 * stroke's width or join, or the effect can change the rec from stroke 56 * to fill (or fill to stroke) in addition to returning a new (dst) path. 57 * 58 * If this method returns true, the caller will apply (as needed) the 59 * resulting stroke-rec to dst and then draw. 60 */ 61 bool filterPath(SkPath* dst, const SkPath& src, SkStrokeRec*, const SkRect* cullR) const; 62 63 /** \class PointData 64 65 PointData aggregates all the information needed to draw the point 66 primitives returned by an 'asPoints' call. 67 */ 68 class PointData { 69 public: PointData()70 PointData() 71 : fFlags(0) 72 , fPoints(nullptr) 73 , fNumPoints(0) { 74 fSize.set(SK_Scalar1, SK_Scalar1); 75 // 'asPoints' needs to initialize/fill-in 'fClipRect' if it sets 76 // the kUseClip flag 77 } ~PointData()78 ~PointData() { 79 delete [] fPoints; 80 } 81 82 // TODO: consider using passed-in flags to limit the work asPoints does. 83 // For example, a kNoPath flag could indicate don't bother generating 84 // stamped solutions. 85 86 // Currently none of these flags are supported. 87 enum PointFlags { 88 kCircles_PointFlag = 0x01, // draw points as circles (instead of rects) 89 kUsePath_PointFlag = 0x02, // draw points as stamps of the returned path 90 kUseClip_PointFlag = 0x04, // apply 'fClipRect' before drawing the points 91 }; 92 93 uint32_t fFlags; // flags that impact the drawing of the points 94 SkPoint* fPoints; // the center point of each generated point 95 int fNumPoints; // number of points in fPoints 96 SkVector fSize; // the size to draw the points 97 SkRect fClipRect; // clip required to draw the points (if kUseClip is set) 98 SkPath fPath; // 'stamp' to be used at each point (if kUsePath is set) 99 100 SkPath fFirst; // If not empty, contains geometry for first point 101 SkPath fLast; // If not empty, contains geometry for last point 102 }; 103 104 /** 105 * Does applying this path effect to 'src' yield a set of points? If so, 106 * optionally return the points in 'results'. 107 */ 108 bool asPoints(PointData* results, const SkPath& src, 109 const SkStrokeRec&, const SkMatrix&, 110 const SkRect* cullR) const; 111 112 /** 113 * If the PathEffect can be represented as a dash pattern, asADash will return kDash_DashType 114 * and None otherwise. If a non NULL info is passed in, the various DashInfo will be filled 115 * in if the PathEffect can be a dash pattern. If passed in info has an fCount equal or 116 * greater to that of the effect, it will memcpy the values of the dash intervals into the 117 * info. Thus the general approach will be call asADash once with default info to get DashType 118 * and fCount. If effect can be represented as a dash pattern, allocate space for the intervals 119 * in info, then call asADash again with the same info and the intervals will get copied in. 120 */ 121 122 enum DashType { 123 kNone_DashType, //!< ignores the info parameter 124 kDash_DashType, //!< fills in all of the info parameter 125 }; 126 127 struct DashInfo { DashInfoDashInfo128 DashInfo() : fIntervals(nullptr), fCount(0), fPhase(0) {} DashInfoDashInfo129 DashInfo(SkScalar* intervals, int32_t count, SkScalar phase) 130 : fIntervals(intervals), fCount(count), fPhase(phase) {} 131 132 SkScalar* fIntervals; //!< Length of on/off intervals for dashed lines 133 // Even values represent ons, and odds offs 134 int32_t fCount; //!< Number of intervals in the dash. Should be even number 135 SkScalar fPhase; //!< Offset into the dashed interval pattern 136 // mod the sum of all intervals 137 }; 138 139 DashType asADash(DashInfo* info) const; 140 141 static void RegisterFlattenables(); 142 GetFlattenableType()143 static SkFlattenable::Type GetFlattenableType() { 144 return kSkPathEffect_Type; 145 } 146 getFlattenableType()147 SkFlattenable::Type getFlattenableType() const override { 148 return kSkPathEffect_Type; 149 } 150 151 static sk_sp<SkPathEffect> Deserialize(const void* data, size_t size, 152 const SkDeserialProcs* procs = nullptr) { 153 return sk_sp<SkPathEffect>(static_cast<SkPathEffect*>( 154 SkFlattenable::Deserialize( 155 kSkPathEffect_Type, data, size, procs).release())); 156 } 157 158 protected: SkPathEffect()159 SkPathEffect() {} 160 161 virtual bool onFilterPath(SkPath*, const SkPath&, SkStrokeRec*, const SkRect*) const = 0; onAsPoints(PointData *,const SkPath &,const SkStrokeRec &,const SkMatrix &,const SkRect *)162 virtual bool onAsPoints(PointData*, const SkPath&, const SkStrokeRec&, const SkMatrix&, 163 const SkRect*) const { 164 return false; 165 } onAsADash(DashInfo *)166 virtual DashType onAsADash(DashInfo*) const { 167 return kNone_DashType; 168 } 169 170 private: 171 friend class SkPathEffectPriv; 172 173 // Compute a conservative bounds for its effect, given the bounds of the path. 'bounds' is 174 // both the input and output; if false is returned, fast bounds could not be calculated and 175 // 'bounds' is undefined. 176 // 177 // If 'bounds' is null, performs a dry-run determining if bounds could be computed. 178 virtual bool computeFastBounds(SkRect* bounds) const = 0; 179 180 // illegal 181 SkPathEffect(const SkPathEffect&); 182 SkPathEffect& operator=(const SkPathEffect&); 183 184 using INHERITED = SkFlattenable; 185 }; 186 187 #endif 188