epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 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 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 9 | |
| 10 | #include "SkDashPathEffect.h" |
| 11 | #include "SkBuffer.h" |
| 12 | #include "SkPathMeasure.h" |
| 13 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 14 | static inline int is_even(int x) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 15 | return (~x) << 31; |
| 16 | } |
| 17 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 18 | static SkScalar FindFirstInterval(const SkScalar intervals[], SkScalar phase, |
| 19 | int32_t* index) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 20 | int i; |
| 21 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 22 | for (i = 0; phase > intervals[i]; i++) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 23 | phase -= intervals[i]; |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 24 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 25 | *index = i; |
| 26 | return intervals[i] - phase; |
| 27 | } |
| 28 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 29 | SkDashPathEffect::SkDashPathEffect(const SkScalar intervals[], int count, |
| 30 | SkScalar phase, bool scaleToFit) |
| 31 | : fScaleToFit(scaleToFit) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 32 | SkASSERT(intervals); |
| 33 | SkASSERT(count > 1 && SkAlign2(count) == count); |
| 34 | |
| 35 | fIntervals = (SkScalar*)sk_malloc_throw(sizeof(SkScalar) * count); |
| 36 | fCount = count; |
| 37 | |
| 38 | SkScalar len = 0; |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 39 | for (int i = 0; i < count; i++) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 40 | SkASSERT(intervals[i] >= 0); |
| 41 | fIntervals[i] = intervals[i]; |
| 42 | len += intervals[i]; |
| 43 | } |
| 44 | fIntervalLength = len; |
| 45 | |
epoger@google.com | 20bf4ca | 2012-04-27 13:34:52 +0000 | [diff] [blame] | 46 | // watch out for values that might make us go out of bounds |
| 47 | if ((len > 0) && SkScalarIsFinite(phase) && SkScalarIsFinite(len)) { |
| 48 | |
| 49 | // Adjust phase to be between 0 and len, "flipping" phase if negative. |
| 50 | // e.g., if len is 100, then phase of -20 (or -120) is equivalent to 80 |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 51 | if (phase < 0) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 52 | phase = -phase; |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 53 | if (phase > len) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 54 | phase = SkScalarMod(phase, len); |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 55 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 56 | phase = len - phase; |
epoger@google.com | 20bf4ca | 2012-04-27 13:34:52 +0000 | [diff] [blame] | 57 | |
| 58 | // Due to finite precision, it's possible that phase == len, |
| 59 | // even after the subtract (if len >>> phase), so fix that here. |
| 60 | // This fixes http://crbug.com/124652 . |
reed@google.com | 1df888b | 2012-04-24 22:47:21 +0000 | [diff] [blame] | 61 | SkASSERT(phase <= len); |
| 62 | if (phase == len) { |
| 63 | phase = 0; |
| 64 | } |
epoger@google.com | 20bf4ca | 2012-04-27 13:34:52 +0000 | [diff] [blame] | 65 | } else if (phase >= len) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 66 | phase = SkScalarMod(phase, len); |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 67 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 68 | SkASSERT(phase >= 0 && phase < len); |
epoger@google.com | 20bf4ca | 2012-04-27 13:34:52 +0000 | [diff] [blame] | 69 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 70 | fInitialDashLength = FindFirstInterval(intervals, phase, &fInitialDashIndex); |
| 71 | |
| 72 | SkASSERT(fInitialDashLength >= 0); |
| 73 | SkASSERT(fInitialDashIndex >= 0 && fInitialDashIndex < fCount); |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 74 | } else { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 75 | fInitialDashLength = -1; // signal bad dash intervals |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 76 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 77 | } |
| 78 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 79 | SkDashPathEffect::~SkDashPathEffect() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 80 | sk_free(fIntervals); |
| 81 | } |
| 82 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 83 | bool SkDashPathEffect::filterPath(SkPath* dst, const SkPath& src, |
reed@google.com | fd4be26 | 2012-05-25 01:04:12 +0000 | [diff] [blame] | 84 | SkStrokeRec* rec) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 85 | // we do nothing if the src wants to be filled, or if our dashlength is 0 |
reed@google.com | fd4be26 | 2012-05-25 01:04:12 +0000 | [diff] [blame] | 86 | if (rec->isFillStyle() || fInitialDashLength < 0) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 87 | return false; |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 88 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 89 | |
| 90 | SkPathMeasure meas(src, false); |
| 91 | const SkScalar* intervals = fIntervals; |
| 92 | |
| 93 | do { |
| 94 | bool skipFirstSegment = meas.isClosed(); |
| 95 | bool addedSegment = false; |
| 96 | SkScalar length = meas.getLength(); |
| 97 | int index = fInitialDashIndex; |
| 98 | SkScalar scale = SK_Scalar1; |
| 99 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 100 | if (fScaleToFit) { |
| 101 | if (fIntervalLength >= length) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 102 | scale = SkScalarDiv(length, fIntervalLength); |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 103 | } else { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 104 | SkScalar div = SkScalarDiv(length, fIntervalLength); |
| 105 | int n = SkScalarFloor(div); |
| 106 | scale = SkScalarDiv(length, n * fIntervalLength); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | SkScalar distance = 0; |
| 111 | SkScalar dlen = SkScalarMul(fInitialDashLength, scale); |
| 112 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 113 | while (distance < length) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 114 | SkASSERT(dlen >= 0); |
| 115 | addedSegment = false; |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 116 | if (is_even(index) && dlen > 0 && !skipFirstSegment) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 117 | addedSegment = true; |
| 118 | meas.getSegment(distance, distance + dlen, dst, true); |
| 119 | } |
| 120 | distance += dlen; |
| 121 | |
| 122 | // clear this so we only respect it the first time around |
| 123 | skipFirstSegment = false; |
| 124 | |
| 125 | // wrap around our intervals array if necessary |
| 126 | index += 1; |
| 127 | SkASSERT(index <= fCount); |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 128 | if (index == fCount) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 129 | index = 0; |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 130 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 131 | |
| 132 | // fetch our next dlen |
| 133 | dlen = SkScalarMul(intervals[index], scale); |
| 134 | } |
| 135 | |
| 136 | // extend if we ended on a segment and we need to join up with the (skipped) initial segment |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 137 | if (meas.isClosed() && is_even(fInitialDashIndex) && |
| 138 | fInitialDashLength > 0) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 139 | meas.getSegment(0, SkScalarMul(fInitialDashLength, scale), dst, !addedSegment); |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 140 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 141 | } while (meas.nextContour()); |
| 142 | return true; |
| 143 | } |
| 144 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 145 | SkFlattenable::Factory SkDashPathEffect::getFactory() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 146 | return fInitialDashLength < 0 ? NULL : CreateProc; |
| 147 | } |
| 148 | |
djsollen@google.com | 5492424 | 2012-03-29 15:18:04 +0000 | [diff] [blame] | 149 | void SkDashPathEffect::flatten(SkFlattenableWriteBuffer& buffer) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 150 | SkASSERT(fInitialDashLength >= 0); |
| 151 | |
djsollen@google.com | 5492424 | 2012-03-29 15:18:04 +0000 | [diff] [blame] | 152 | this->INHERITED::flatten(buffer); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 153 | buffer.write32(fCount); |
| 154 | buffer.write32(fInitialDashIndex); |
| 155 | buffer.writeScalar(fInitialDashLength); |
| 156 | buffer.writeScalar(fIntervalLength); |
| 157 | buffer.write32(fScaleToFit); |
| 158 | buffer.writeMul4(fIntervals, fCount * sizeof(fIntervals[0])); |
| 159 | } |
| 160 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 161 | SkFlattenable* SkDashPathEffect::CreateProc(SkFlattenableReadBuffer& buffer) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 162 | return SkNEW_ARGS(SkDashPathEffect, (buffer)); |
| 163 | } |
| 164 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 165 | SkDashPathEffect::SkDashPathEffect(SkFlattenableReadBuffer& buffer) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 166 | fCount = buffer.readS32(); |
| 167 | fInitialDashIndex = buffer.readS32(); |
| 168 | fInitialDashLength = buffer.readScalar(); |
| 169 | fIntervalLength = buffer.readScalar(); |
| 170 | fScaleToFit = (buffer.readS32() != 0); |
| 171 | |
| 172 | fIntervals = (SkScalar*)sk_malloc_throw(sizeof(SkScalar) * fCount); |
| 173 | buffer.read(fIntervals, fCount * sizeof(fIntervals[0])); |
| 174 | } |
| 175 | |
reed@google.com | 6bac947 | 2011-06-21 19:24:00 +0000 | [diff] [blame] | 176 | /////////////////////////////////////////////////////////////////////////////// |
| 177 | |
caryclark@google.com | d26147a | 2011-12-15 14:16:43 +0000 | [diff] [blame] | 178 | SK_DEFINE_FLATTENABLE_REGISTRAR(SkDashPathEffect) |