reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1 | /* libs/graphics/effects/SkDashPathEffect.cpp |
| 2 | ** |
| 3 | ** Copyright 2006, The Android Open Source Project |
| 4 | ** |
| 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | ** you may not use this file except in compliance with the License. |
| 7 | ** You may obtain a copy of the License at |
| 8 | ** |
| 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | ** |
| 11 | ** Unless required by applicable law or agreed to in writing, software |
| 12 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | ** See the License for the specific language governing permissions and |
| 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #include "SkDashPathEffect.h" |
| 19 | #include "SkBuffer.h" |
| 20 | #include "SkPathMeasure.h" |
| 21 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 22 | static inline int is_even(int x) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 23 | return (~x) << 31; |
| 24 | } |
| 25 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 26 | static SkScalar FindFirstInterval(const SkScalar intervals[], SkScalar phase, |
| 27 | int32_t* index) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 28 | int i; |
| 29 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 30 | for (i = 0; phase > intervals[i]; i++) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 31 | phase -= intervals[i]; |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 32 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 33 | *index = i; |
| 34 | return intervals[i] - phase; |
| 35 | } |
| 36 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 37 | SkDashPathEffect::SkDashPathEffect(const SkScalar intervals[], int count, |
| 38 | SkScalar phase, bool scaleToFit) |
| 39 | : fScaleToFit(scaleToFit) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 40 | SkASSERT(intervals); |
| 41 | SkASSERT(count > 1 && SkAlign2(count) == count); |
| 42 | |
| 43 | fIntervals = (SkScalar*)sk_malloc_throw(sizeof(SkScalar) * count); |
| 44 | fCount = count; |
| 45 | |
| 46 | SkScalar len = 0; |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 47 | for (int i = 0; i < count; i++) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 48 | SkASSERT(intervals[i] >= 0); |
| 49 | fIntervals[i] = intervals[i]; |
| 50 | len += intervals[i]; |
| 51 | } |
| 52 | fIntervalLength = len; |
| 53 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 54 | if (len > 0) { // we don't handle 0 length dash arrays |
| 55 | if (phase < 0) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 56 | phase = -phase; |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 57 | if (phase > len) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 58 | phase = SkScalarMod(phase, len); |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 59 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 60 | phase = len - phase; |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 61 | } else if (phase >= len) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 62 | phase = SkScalarMod(phase, len); |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 63 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 64 | |
reed@google.com | 9de4dc9 | 2011-07-15 14:14:45 +0000 | [diff] [blame] | 65 | // got to watch out for values that might make us go out of bounds |
| 66 | if (!SkScalarIsFinite(phase) || !SkScalarIsFinite(len)) { |
| 67 | goto BAD_DASH; |
| 68 | } |
| 69 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 70 | SkASSERT(phase >= 0 && phase < len); |
| 71 | fInitialDashLength = FindFirstInterval(intervals, phase, &fInitialDashIndex); |
| 72 | |
| 73 | SkASSERT(fInitialDashLength >= 0); |
| 74 | SkASSERT(fInitialDashIndex >= 0 && fInitialDashIndex < fCount); |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 75 | } else { |
reed@google.com | 9de4dc9 | 2011-07-15 14:14:45 +0000 | [diff] [blame] | 76 | BAD_DASH: |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 77 | fInitialDashLength = -1; // signal bad dash intervals |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 78 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 79 | } |
| 80 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 81 | SkDashPathEffect::~SkDashPathEffect() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 82 | sk_free(fIntervals); |
| 83 | } |
| 84 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 85 | bool SkDashPathEffect::filterPath(SkPath* dst, const SkPath& src, |
| 86 | SkScalar* width) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 87 | // we do nothing if the src wants to be filled, or if our dashlength is 0 |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 88 | if (*width < 0 || fInitialDashLength < 0) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 89 | return false; |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 90 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 91 | |
| 92 | SkPathMeasure meas(src, false); |
| 93 | const SkScalar* intervals = fIntervals; |
| 94 | |
| 95 | do { |
| 96 | bool skipFirstSegment = meas.isClosed(); |
| 97 | bool addedSegment = false; |
| 98 | SkScalar length = meas.getLength(); |
| 99 | int index = fInitialDashIndex; |
| 100 | SkScalar scale = SK_Scalar1; |
| 101 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 102 | if (fScaleToFit) { |
| 103 | if (fIntervalLength >= length) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 104 | scale = SkScalarDiv(length, fIntervalLength); |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 105 | } else { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 106 | SkScalar div = SkScalarDiv(length, fIntervalLength); |
| 107 | int n = SkScalarFloor(div); |
| 108 | scale = SkScalarDiv(length, n * fIntervalLength); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | SkScalar distance = 0; |
| 113 | SkScalar dlen = SkScalarMul(fInitialDashLength, scale); |
| 114 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 115 | while (distance < length) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 116 | SkASSERT(dlen >= 0); |
| 117 | addedSegment = false; |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 118 | if (is_even(index) && dlen > 0 && !skipFirstSegment) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 119 | addedSegment = true; |
| 120 | meas.getSegment(distance, distance + dlen, dst, true); |
| 121 | } |
| 122 | distance += dlen; |
| 123 | |
| 124 | // clear this so we only respect it the first time around |
| 125 | skipFirstSegment = false; |
| 126 | |
| 127 | // wrap around our intervals array if necessary |
| 128 | index += 1; |
| 129 | SkASSERT(index <= fCount); |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 130 | if (index == fCount) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 131 | index = 0; |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 132 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 133 | |
| 134 | // fetch our next dlen |
| 135 | dlen = SkScalarMul(intervals[index], scale); |
| 136 | } |
| 137 | |
| 138 | // 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] | 139 | if (meas.isClosed() && is_even(fInitialDashIndex) && |
| 140 | fInitialDashLength > 0) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 141 | meas.getSegment(0, SkScalarMul(fInitialDashLength, scale), dst, !addedSegment); |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 142 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 143 | } while (meas.nextContour()); |
| 144 | return true; |
| 145 | } |
| 146 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 147 | SkFlattenable::Factory SkDashPathEffect::getFactory() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 148 | return fInitialDashLength < 0 ? NULL : CreateProc; |
| 149 | } |
| 150 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 151 | void SkDashPathEffect::flatten(SkFlattenableWriteBuffer& buffer) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 152 | SkASSERT(fInitialDashLength >= 0); |
| 153 | |
| 154 | buffer.write32(fCount); |
| 155 | buffer.write32(fInitialDashIndex); |
| 156 | buffer.writeScalar(fInitialDashLength); |
| 157 | buffer.writeScalar(fIntervalLength); |
| 158 | buffer.write32(fScaleToFit); |
| 159 | buffer.writeMul4(fIntervals, fCount * sizeof(fIntervals[0])); |
| 160 | } |
| 161 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 162 | SkFlattenable* SkDashPathEffect::CreateProc(SkFlattenableReadBuffer& buffer) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 163 | return SkNEW_ARGS(SkDashPathEffect, (buffer)); |
| 164 | } |
| 165 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 166 | SkDashPathEffect::SkDashPathEffect(SkFlattenableReadBuffer& buffer) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 167 | fCount = buffer.readS32(); |
| 168 | fInitialDashIndex = buffer.readS32(); |
| 169 | fInitialDashLength = buffer.readScalar(); |
| 170 | fIntervalLength = buffer.readScalar(); |
| 171 | fScaleToFit = (buffer.readS32() != 0); |
| 172 | |
| 173 | fIntervals = (SkScalar*)sk_malloc_throw(sizeof(SkScalar) * fCount); |
| 174 | buffer.read(fIntervals, fCount * sizeof(fIntervals[0])); |
| 175 | } |
| 176 | |
reed@google.com | 6bac947 | 2011-06-21 19:24:00 +0000 | [diff] [blame] | 177 | /////////////////////////////////////////////////////////////////////////////// |
| 178 | |
| 179 | static SkFlattenable::Registrar gReg("SkDashPathEffect", |
| 180 | SkDashPathEffect::CreateProc); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 181 | |