SkPathEffectBase.h (6011B)
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 SkPathEffectBase_DEFINED 9 #define SkPathEffectBase_DEFINED 10 11 #include "include/core/SkPath.h" 12 #include "include/core/SkPathEffect.h" 13 #include "include/core/SkPoint.h" 14 #include "include/core/SkRect.h" 15 #include "include/core/SkSpan.h" 16 17 #include <optional> 18 19 class SkPathBuilder; 20 class SkStrokeRec; 21 22 class SkPathEffectBase : public SkPathEffect { 23 public: 24 SkPathEffectBase() {} 25 26 /** \class PointData 27 28 PointData aggregates all the information needed to draw the point 29 primitives returned by an 'asPoints' call. 30 */ 31 class PointData { 32 public: 33 PointData() 34 : fFlags(0) 35 , fPoints(nullptr) 36 , fNumPoints(0) { 37 fSize.set(SK_Scalar1, SK_Scalar1); 38 // 'asPoints' needs to initialize/fill-in 'fClipRect' if it sets 39 // the kUseClip flag 40 } 41 ~PointData() { 42 delete [] fPoints; 43 } 44 45 // TODO: consider using passed-in flags to limit the work asPoints does. 46 // For example, a kNoPath flag could indicate don't bother generating 47 // stamped solutions. 48 49 // Currently none of these flags are supported. 50 enum PointFlags { 51 kCircles_PointFlag = 0x01, // draw points as circles (instead of rects) 52 kUsePath_PointFlag = 0x02, // draw points as stamps of the returned path 53 kUseClip_PointFlag = 0x04, // apply 'fClipRect' before drawing the points 54 }; 55 56 uint32_t fFlags; // flags that impact the drawing of the points 57 SkPoint* fPoints; // the center point of each generated point 58 int fNumPoints; // number of points in fPoints 59 SkVector fSize; // the size to draw the points 60 SkRect fClipRect; // clip required to draw the points (if kUseClip is set) 61 SkPath fPath; // 'stamp' to be used at each point (if kUsePath is set) 62 63 SkPath fFirst; // If not empty, contains geometry for first point 64 SkPath fLast; // If not empty, contains geometry for last point 65 66 SkSpan<SkPoint> points() { return {fPoints, fNumPoints}; } 67 }; 68 69 /** 70 * Does applying this path effect to 'src' yield a set of points? If so, 71 * optionally return the points in 'results'. 72 */ 73 bool asPoints(PointData* results, const SkPath& src, 74 const SkStrokeRec&, const SkMatrix&, 75 const SkRect* cullR) const; 76 77 /** 78 * If the PathEffect can be represented as a dash pattern, asADash will return kDash_DashType 79 * and None otherwise. If a non NULL info is passed in, the various DashInfo will be filled 80 * in if the PathEffect can be a dash pattern. If passed in info has an fCount equal or 81 * greater to that of the effect, it will memcpy the values of the dash intervals into the 82 * info. Thus the general approach will be call asADash once with default info to get DashType 83 * and fCount. If effect can be represented as a dash pattern, allocate space for the intervals 84 * in info, then call asADash again with the same info and the intervals will get copied in. 85 */ 86 87 SkFlattenable::Type getFlattenableType() const override { 88 return kSkPathEffect_Type; 89 } 90 91 static sk_sp<SkPathEffect> Deserialize(const void* data, size_t size, 92 const SkDeserialProcs* procs = nullptr) { 93 return sk_sp<SkPathEffect>(static_cast<SkPathEffect*>( 94 SkFlattenable::Deserialize( 95 kSkPathEffect_Type, data, size, procs).release())); 96 } 97 98 /** 99 * Filter the input path. 100 * 101 * The CTM parameter is provided for path effects that can use the information. 102 * The output of path effects must always be in the original (input) coordinate system, 103 * regardless of whether the path effect uses the CTM or not. 104 */ 105 virtual bool onFilterPath(SkPathBuilder*, const SkPath&, SkStrokeRec*, const SkRect*, 106 const SkMatrix& /* ctm */) const = 0; 107 108 /** Path effects *requiring* a valid CTM should override to return true. */ 109 virtual bool onNeedsCTM() const { return false; } 110 111 virtual bool onAsPoints(PointData*, const SkPath&, const SkStrokeRec&, const SkMatrix&, 112 const SkRect*) const { 113 return false; 114 } 115 116 struct DashInfo { 117 SkSpan<const SkScalar> fIntervals; 118 SkScalar fPhase; 119 }; 120 121 virtual std::optional<DashInfo> asADash() const { 122 return {}; 123 } 124 125 126 // Compute a conservative bounds for its effect, given the bounds of the path. 'bounds' is 127 // both the input and output; if false is returned, fast bounds could not be calculated and 128 // 'bounds' is undefined. 129 // 130 // If 'bounds' is null, performs a dry-run determining if bounds could be computed. 131 virtual bool computeFastBounds(SkRect* bounds) const = 0; 132 133 static void RegisterFlattenables(); 134 135 private: 136 using INHERITED = SkPathEffect; 137 }; 138 139 //////////////////////////////////////////////////////////////////////////////////////////////// 140 141 static inline SkPathEffectBase* as_PEB(SkPathEffect* effect) { 142 return static_cast<SkPathEffectBase*>(effect); 143 } 144 145 static inline const SkPathEffectBase* as_PEB(const SkPathEffect* effect) { 146 return static_cast<const SkPathEffectBase*>(effect); 147 } 148 149 static inline const SkPathEffectBase* as_PEB(const sk_sp<SkPathEffect>& effect) { 150 return static_cast<SkPathEffectBase*>(effect.get()); 151 } 152 153 static inline sk_sp<SkPathEffectBase> as_PEB_sp(sk_sp<SkPathEffect> effect) { 154 return sk_sp<SkPathEffectBase>(static_cast<SkPathEffectBase*>(effect.release())); 155 } 156 157 #endif