epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkPath.h" |
| 9 | #include "include/core/SkRegion.h" |
| 10 | #include "include/core/SkStrokeRec.h" |
| 11 | #include "include/effects/Sk2DPathEffect.h" |
Mike Reed | ec9d0e8 | 2021-05-21 17:42:14 -0400 | [diff] [blame] | 12 | #include "src/core/SkPathEffectBase.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "src/core/SkReadBuffer.h" |
| 14 | #include "src/core/SkWriteBuffer.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 15 | |
Mike Reed | ec9d0e8 | 2021-05-21 17:42:14 -0400 | [diff] [blame] | 16 | class Sk2DPathEffect : public SkPathEffectBase { |
Mike Reed | ec87dc1 | 2021-05-20 15:16:34 -0400 | [diff] [blame] | 17 | public: |
| 18 | Sk2DPathEffect(const SkMatrix& mat) : fMatrix(mat) { |
| 19 | // Calling invert will set the type mask on both matrices, making them thread safe. |
| 20 | fMatrixIsInvertible = fMatrix.invert(&fInverse); |
| 21 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 22 | |
Mike Reed | ec87dc1 | 2021-05-20 15:16:34 -0400 | [diff] [blame] | 23 | protected: |
| 24 | /** New virtual, to be overridden by subclasses. |
| 25 | This is called once from filterPath, and provides the |
| 26 | uv parameter bounds for the path. Subsequent calls to |
| 27 | next() will receive u and v values within these bounds, |
| 28 | and then a call to end() will signal the end of processing. |
| 29 | */ |
| 30 | virtual void begin(const SkIRect& uvBounds, SkPath* dst) const {} |
| 31 | virtual void next(const SkPoint& loc, int u, int v, SkPath* dst) const {} |
| 32 | virtual void end(SkPath* dst) const {} |
| 33 | |
| 34 | /** Low-level virtual called per span of locations in the u-direction. |
| 35 | The default implementation calls next() repeatedly with each |
| 36 | location. |
| 37 | */ |
| 38 | virtual void nextSpan(int x, int y, int ucount, SkPath* path) const { |
| 39 | if (!fMatrixIsInvertible) { |
| 40 | return; |
| 41 | } |
| 42 | #if defined(SK_BUILD_FOR_FUZZER) |
Michael Ludwig | db285de | 2021-09-16 11:46:13 -0400 | [diff] [blame] | 43 | if (ucount > 100) { |
Mike Reed | ec87dc1 | 2021-05-20 15:16:34 -0400 | [diff] [blame] | 44 | return; |
| 45 | } |
| 46 | #endif |
| 47 | |
| 48 | const SkMatrix& mat = this->getMatrix(); |
| 49 | SkPoint src, dst; |
| 50 | |
| 51 | src.set(SkIntToScalar(x) + SK_ScalarHalf, SkIntToScalar(y) + SK_ScalarHalf); |
| 52 | do { |
| 53 | mat.mapPoints(&dst, &src, 1); |
| 54 | this->next(dst, x++, y, path); |
| 55 | src.fX += SK_Scalar1; |
| 56 | } while (--ucount > 0); |
| 57 | } |
| 58 | |
| 59 | const SkMatrix& getMatrix() const { return fMatrix; } |
| 60 | |
| 61 | void flatten(SkWriteBuffer& buffer) const override { |
| 62 | this->INHERITED::flatten(buffer); |
| 63 | buffer.writeMatrix(fMatrix); |
| 64 | } |
| 65 | |
| 66 | bool onFilterPath(SkPath* dst, const SkPath& src, SkStrokeRec* rec, |
Tyler Denniston | f8b7c1a | 2021-07-13 13:22:19 -0400 | [diff] [blame] | 67 | const SkRect* cullRect, const SkMatrix&) const override { |
Mike Reed | ec87dc1 | 2021-05-20 15:16:34 -0400 | [diff] [blame] | 68 | if (!fMatrixIsInvertible) { |
| 69 | return false; |
| 70 | } |
| 71 | |
| 72 | SkPath tmp; |
| 73 | SkIRect ir; |
| 74 | |
| 75 | src.transform(fInverse, &tmp); |
| 76 | tmp.getBounds().round(&ir); |
| 77 | if (!ir.isEmpty()) { |
| 78 | this->begin(ir, dst); |
| 79 | |
| 80 | SkRegion rgn; |
| 81 | rgn.setPath(tmp, SkRegion(ir)); |
| 82 | SkRegion::Iterator iter(rgn); |
| 83 | for (; !iter.done(); iter.next()) { |
| 84 | const SkIRect& rect = iter.rect(); |
| 85 | for (int y = rect.fTop; y < rect.fBottom; ++y) { |
| 86 | this->nextSpan(rect.fLeft, y, rect.width(), dst); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | this->end(dst); |
| 91 | } |
| 92 | return true; |
| 93 | } |
| 94 | |
| 95 | private: |
| 96 | SkMatrix fMatrix, fInverse; |
| 97 | bool fMatrixIsInvertible; |
| 98 | |
| 99 | // For simplicity, assume fast bounds cannot be computed |
| 100 | bool computeFastBounds(SkRect*) const override { return false; } |
| 101 | |
| 102 | friend class Sk2DPathEffectBlitter; |
| 103 | using INHERITED = SkPathEffect; |
| 104 | }; |
| 105 | |
| 106 | /////////////////////////////////////////////////////////////////////////////// |
| 107 | |
| 108 | class SkLine2DPathEffectImpl : public Sk2DPathEffect { |
| 109 | public: |
| 110 | SkLine2DPathEffectImpl(SkScalar width, const SkMatrix& matrix) |
| 111 | : Sk2DPathEffect(matrix) |
| 112 | , fWidth(width) |
| 113 | { |
| 114 | SkASSERT(width >= 0); |
| 115 | } |
| 116 | |
| 117 | bool onFilterPath(SkPath* dst, const SkPath& src, SkStrokeRec* rec, |
Tyler Denniston | f8b7c1a | 2021-07-13 13:22:19 -0400 | [diff] [blame] | 118 | const SkRect* cullRect, const SkMatrix& ctm) const override { |
| 119 | if (this->INHERITED::onFilterPath(dst, src, rec, cullRect, ctm)) { |
Mike Reed | ec87dc1 | 2021-05-20 15:16:34 -0400 | [diff] [blame] | 120 | rec->setStrokeStyle(fWidth); |
| 121 | return true; |
| 122 | } |
mike@reedtribe.org | 90bf427 | 2012-04-14 19:06:16 +0000 | [diff] [blame] | 123 | return false; |
| 124 | } |
| 125 | |
Mike Reed | ec87dc1 | 2021-05-20 15:16:34 -0400 | [diff] [blame] | 126 | void nextSpan(int u, int v, int ucount, SkPath* dst) const override { |
| 127 | if (ucount > 1) { |
| 128 | SkPoint src[2], dstP[2]; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 129 | |
Mike Reed | ec87dc1 | 2021-05-20 15:16:34 -0400 | [diff] [blame] | 130 | src[0].set(SkIntToScalar(u) + SK_ScalarHalf, SkIntToScalar(v) + SK_ScalarHalf); |
| 131 | src[1].set(SkIntToScalar(u+ucount) + SK_ScalarHalf, SkIntToScalar(v) + SK_ScalarHalf); |
| 132 | this->getMatrix().mapPoints(dstP, src, 2); |
reed@google.com | 6db9375 | 2012-08-09 19:18:02 +0000 | [diff] [blame] | 133 | |
Mike Reed | ec87dc1 | 2021-05-20 15:16:34 -0400 | [diff] [blame] | 134 | dst->moveTo(dstP[0]); |
| 135 | dst->lineTo(dstP[1]); |
reed@google.com | 6db9375 | 2012-08-09 19:18:02 +0000 | [diff] [blame] | 136 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 137 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 138 | |
Mike Reed | ec87dc1 | 2021-05-20 15:16:34 -0400 | [diff] [blame] | 139 | static sk_sp<SkFlattenable> CreateProc(SkReadBuffer& buffer) { |
| 140 | SkMatrix matrix; |
| 141 | buffer.readMatrix(&matrix); |
| 142 | SkScalar width = buffer.readScalar(); |
| 143 | return SkLine2DPathEffect::Make(width, matrix); |
mike@reedtribe.org | 90bf427 | 2012-04-14 19:06:16 +0000 | [diff] [blame] | 144 | } |
Mike Reed | ec87dc1 | 2021-05-20 15:16:34 -0400 | [diff] [blame] | 145 | |
| 146 | void flatten(SkWriteBuffer &buffer) const override { |
| 147 | buffer.writeMatrix(this->getMatrix()); |
| 148 | buffer.writeScalar(fWidth); |
Kevin Lubick | 493f89e | 2020-09-14 08:37:35 -0400 | [diff] [blame] | 149 | } |
mike@reedtribe.org | 90bf427 | 2012-04-14 19:06:16 +0000 | [diff] [blame] | 150 | |
Mike Reed | ec87dc1 | 2021-05-20 15:16:34 -0400 | [diff] [blame] | 151 | Factory getFactory() const override { return CreateProc; } |
| 152 | const char* getTypeName() const override { return "SkLine2DPathEffect"; } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 153 | |
Mike Reed | ec87dc1 | 2021-05-20 15:16:34 -0400 | [diff] [blame] | 154 | private: |
| 155 | SkScalar fWidth; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 156 | |
Mike Reed | ec87dc1 | 2021-05-20 15:16:34 -0400 | [diff] [blame] | 157 | using INHERITED = Sk2DPathEffect; |
| 158 | }; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 159 | |
Mike Reed | ec87dc1 | 2021-05-20 15:16:34 -0400 | [diff] [blame] | 160 | ///////////////////////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 161 | |
Mike Reed | ec87dc1 | 2021-05-20 15:16:34 -0400 | [diff] [blame] | 162 | class SK_API SkPath2DPathEffectImpl : public Sk2DPathEffect { |
| 163 | public: |
| 164 | SkPath2DPathEffectImpl(const SkMatrix& m, const SkPath& p) : INHERITED(m), fPath(p) {} |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 165 | |
Mike Reed | ec87dc1 | 2021-05-20 15:16:34 -0400 | [diff] [blame] | 166 | void next(const SkPoint& loc, int u, int v, SkPath* dst) const override { |
| 167 | dst->addPath(fPath, loc.fX, loc.fY); |
scroggo@google.com | d8a6cc8 | 2012-09-12 18:53:49 +0000 | [diff] [blame] | 168 | } |
scroggo@google.com | d8a6cc8 | 2012-09-12 18:53:49 +0000 | [diff] [blame] | 169 | |
Mike Reed | ec87dc1 | 2021-05-20 15:16:34 -0400 | [diff] [blame] | 170 | static sk_sp<SkFlattenable> CreateProc(SkReadBuffer& buffer) { |
| 171 | SkMatrix matrix; |
| 172 | buffer.readMatrix(&matrix); |
| 173 | SkPath path; |
| 174 | buffer.readPath(&path); |
| 175 | return SkPath2DPathEffect::Make(matrix, path); |
scroggo@google.com | d8a6cc8 | 2012-09-12 18:53:49 +0000 | [diff] [blame] | 176 | } |
Mike Reed | ec87dc1 | 2021-05-20 15:16:34 -0400 | [diff] [blame] | 177 | |
| 178 | void flatten(SkWriteBuffer& buffer) const override { |
| 179 | buffer.writeMatrix(this->getMatrix()); |
| 180 | buffer.writePath(fPath); |
| 181 | } |
| 182 | |
| 183 | Factory getFactory() const override { return CreateProc; } |
| 184 | const char* getTypeName() const override { return "SkPath2DPathEffect"; } |
| 185 | |
| 186 | private: |
| 187 | SkPath fPath; |
| 188 | |
| 189 | using INHERITED = Sk2DPathEffect; |
| 190 | }; |
| 191 | |
| 192 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
| 193 | |
| 194 | sk_sp<SkPathEffect> SkLine2DPathEffect::Make(SkScalar width, const SkMatrix& matrix) { |
| 195 | if (!(width >= 0)) { |
| 196 | return nullptr; |
| 197 | } |
| 198 | return sk_sp<SkPathEffect>(new SkLine2DPathEffectImpl(width, matrix)); |
scroggo@google.com | d8a6cc8 | 2012-09-12 18:53:49 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Mike Reed | ec87dc1 | 2021-05-20 15:16:34 -0400 | [diff] [blame] | 201 | sk_sp<SkPathEffect> SkPath2DPathEffect::Make(const SkMatrix& matrix, const SkPath& path) { |
| 202 | return sk_sp<SkPathEffect>(new SkPath2DPathEffectImpl(matrix, path)); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 203 | } |
scroggo@google.com | d8a6cc8 | 2012-09-12 18:53:49 +0000 | [diff] [blame] | 204 | |
Mike Reed | ec87dc1 | 2021-05-20 15:16:34 -0400 | [diff] [blame] | 205 | void SkLine2DPathEffect::RegisterFlattenables() { |
| 206 | SK_REGISTER_FLATTENABLE(SkLine2DPathEffectImpl); |
scroggo@google.com | d8a6cc8 | 2012-09-12 18:53:49 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Mike Reed | ec87dc1 | 2021-05-20 15:16:34 -0400 | [diff] [blame] | 209 | void SkPath2DPathEffect::RegisterFlattenables() { |
| 210 | SK_REGISTER_FLATTENABLE(SkPath2DPathEffectImpl); |
reed@google.com | 18dc477 | 2011-08-09 18:47:40 +0000 | [diff] [blame] | 211 | } |