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