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 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 8 | |
| 9 | #include "Sk2DPathEffect.h" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 10 | #include "SkReadBuffer.h" |
| 11 | #include "SkWriteBuffer.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 12 | #include "SkPath.h" |
reed@google.com | 6db9375 | 2012-08-09 19:18:02 +0000 | [diff] [blame] | 13 | #include "SkRegion.h" |
halcanary | 435657f | 2015-09-15 12:53:07 -0700 | [diff] [blame] | 14 | #include "SkStrokeRec.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 15 | |
reed@google.com | 16edff2 | 2011-08-12 14:10:27 +0000 | [diff] [blame] | 16 | Sk2DPathEffect::Sk2DPathEffect(const SkMatrix& mat) : fMatrix(mat) { |
bungeman | 6c3baa6 | 2016-09-16 07:21:28 -0700 | [diff] [blame] | 17 | // Calling invert will set the type mask on both matrices, making them thread safe. |
| 18 | fMatrixIsInvertible = fMatrix.invert(&fInverse); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 19 | } |
| 20 | |
reed@google.com | 548a1f3 | 2012-12-18 16:12:09 +0000 | [diff] [blame] | 21 | bool Sk2DPathEffect::filterPath(SkPath* dst, const SkPath& src, |
reed@google.com | 4bbdeac | 2013-01-24 21:03:11 +0000 | [diff] [blame] | 22 | SkStrokeRec*, const SkRect*) const { |
mike@reedtribe.org | 90bf427 | 2012-04-14 19:06:16 +0000 | [diff] [blame] | 23 | if (!fMatrixIsInvertible) { |
| 24 | return false; |
| 25 | } |
| 26 | |
reed@google.com | 6db9375 | 2012-08-09 19:18:02 +0000 | [diff] [blame] | 27 | SkPath tmp; |
| 28 | SkIRect ir; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 29 | |
| 30 | src.transform(fInverse, &tmp); |
reed@android.com | d252db0 | 2009-04-01 18:31:44 +0000 | [diff] [blame] | 31 | tmp.getBounds().round(&ir); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 32 | if (!ir.isEmpty()) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 33 | this->begin(ir, dst); |
reed@google.com | 6db9375 | 2012-08-09 19:18:02 +0000 | [diff] [blame] | 34 | |
| 35 | SkRegion rgn; |
| 36 | rgn.setPath(tmp, SkRegion(ir)); |
| 37 | SkRegion::Iterator iter(rgn); |
| 38 | for (; !iter.done(); iter.next()) { |
| 39 | const SkIRect& rect = iter.rect(); |
| 40 | for (int y = rect.fTop; y < rect.fBottom; ++y) { |
| 41 | this->nextSpan(rect.fLeft, y, rect.width(), dst); |
| 42 | } |
| 43 | } |
| 44 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 45 | this->end(dst); |
| 46 | } |
| 47 | return true; |
| 48 | } |
| 49 | |
reed@google.com | 548a1f3 | 2012-12-18 16:12:09 +0000 | [diff] [blame] | 50 | void Sk2DPathEffect::nextSpan(int x, int y, int count, SkPath* path) const { |
mike@reedtribe.org | 90bf427 | 2012-04-14 19:06:16 +0000 | [diff] [blame] | 51 | if (!fMatrixIsInvertible) { |
| 52 | return; |
| 53 | } |
| 54 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 55 | const SkMatrix& mat = this->getMatrix(); |
| 56 | SkPoint src, dst; |
| 57 | |
| 58 | src.set(SkIntToScalar(x) + SK_ScalarHalf, SkIntToScalar(y) + SK_ScalarHalf); |
| 59 | do { |
| 60 | mat.mapPoints(&dst, &src, 1); |
| 61 | this->next(dst, x++, y, path); |
| 62 | src.fX += SK_Scalar1; |
| 63 | } while (--count > 0); |
| 64 | } |
| 65 | |
reed@google.com | 548a1f3 | 2012-12-18 16:12:09 +0000 | [diff] [blame] | 66 | void Sk2DPathEffect::begin(const SkIRect& uvBounds, SkPath* dst) const {} |
| 67 | void Sk2DPathEffect::next(const SkPoint& loc, int u, int v, SkPath* dst) const {} |
| 68 | void Sk2DPathEffect::end(SkPath* dst) const {} |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 69 | |
reed@google.com | 16edff2 | 2011-08-12 14:10:27 +0000 | [diff] [blame] | 70 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 71 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 72 | void Sk2DPathEffect::flatten(SkWriteBuffer& buffer) const { |
djsollen@google.com | 5492424 | 2012-03-29 15:18:04 +0000 | [diff] [blame] | 73 | this->INHERITED::flatten(buffer); |
djsollen@google.com | d2700ee | 2012-05-30 16:54:13 +0000 | [diff] [blame] | 74 | buffer.writeMatrix(fMatrix); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Cary Clark | 32a4910 | 2018-05-20 23:15:43 +0000 | [diff] [blame] | 77 | void Sk2DPathEffect::toString(SkString* str) const { |
| 78 | str->appendf("(matrix: %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f)", |
| 79 | fMatrix[SkMatrix::kMScaleX], fMatrix[SkMatrix::kMSkewX], fMatrix[SkMatrix::kMTransX], |
| 80 | fMatrix[SkMatrix::kMSkewY], fMatrix[SkMatrix::kMScaleY], fMatrix[SkMatrix::kMTransY], |
| 81 | fMatrix[SkMatrix::kMPersp0], fMatrix[SkMatrix::kMPersp1], fMatrix[SkMatrix::kMPersp2]); |
| 82 | } |
| 83 | |
reed@google.com | e28b917 | 2011-08-09 18:14:31 +0000 | [diff] [blame] | 84 | /////////////////////////////////////////////////////////////////////////////// |
scroggo@google.com | d8a6cc8 | 2012-09-12 18:53:49 +0000 | [diff] [blame] | 85 | |
reed@google.com | 548a1f3 | 2012-12-18 16:12:09 +0000 | [diff] [blame] | 86 | bool SkLine2DPathEffect::filterPath(SkPath* dst, const SkPath& src, |
reed@google.com | 4bbdeac | 2013-01-24 21:03:11 +0000 | [diff] [blame] | 87 | SkStrokeRec* rec, const SkRect* cullRect) const { |
| 88 | if (this->INHERITED::filterPath(dst, src, rec, cullRect)) { |
scroggo@google.com | d8a6cc8 | 2012-09-12 18:53:49 +0000 | [diff] [blame] | 89 | rec->setStrokeStyle(fWidth); |
| 90 | return true; |
| 91 | } |
| 92 | return false; |
| 93 | } |
| 94 | |
reed@google.com | 548a1f3 | 2012-12-18 16:12:09 +0000 | [diff] [blame] | 95 | void SkLine2DPathEffect::nextSpan(int u, int v, int ucount, SkPath* dst) const { |
scroggo@google.com | d8a6cc8 | 2012-09-12 18:53:49 +0000 | [diff] [blame] | 96 | if (ucount > 1) { |
| 97 | SkPoint src[2], dstP[2]; |
| 98 | |
| 99 | src[0].set(SkIntToScalar(u) + SK_ScalarHalf, SkIntToScalar(v) + SK_ScalarHalf); |
| 100 | src[1].set(SkIntToScalar(u+ucount) + SK_ScalarHalf, SkIntToScalar(v) + SK_ScalarHalf); |
| 101 | this->getMatrix().mapPoints(dstP, src, 2); |
| 102 | |
| 103 | dst->moveTo(dstP[0]); |
| 104 | dst->lineTo(dstP[1]); |
| 105 | } |
| 106 | } |
| 107 | |
reed | 60c9b58 | 2016-04-03 09:11:13 -0700 | [diff] [blame] | 108 | sk_sp<SkFlattenable> SkLine2DPathEffect::CreateProc(SkReadBuffer& buffer) { |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 109 | SkMatrix matrix; |
| 110 | buffer.readMatrix(&matrix); |
| 111 | SkScalar width = buffer.readScalar(); |
reed | 60c9b58 | 2016-04-03 09:11:13 -0700 | [diff] [blame] | 112 | return SkLine2DPathEffect::Make(width, matrix); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 113 | } |
scroggo@google.com | d8a6cc8 | 2012-09-12 18:53:49 +0000 | [diff] [blame] | 114 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 115 | void SkLine2DPathEffect::flatten(SkWriteBuffer &buffer) const { |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 116 | buffer.writeMatrix(this->getMatrix()); |
scroggo@google.com | d8a6cc8 | 2012-09-12 18:53:49 +0000 | [diff] [blame] | 117 | buffer.writeScalar(fWidth); |
| 118 | } |
| 119 | |
Cary Clark | 32a4910 | 2018-05-20 23:15:43 +0000 | [diff] [blame] | 120 | |
| 121 | void SkLine2DPathEffect::toString(SkString* str) const { |
| 122 | str->appendf("SkLine2DPathEffect: ("); |
| 123 | this->INHERITED::toString(str); |
| 124 | str->appendf("width: %f", fWidth); |
| 125 | str->appendf(")"); |
| 126 | } |
| 127 | |
reed@google.com | 18dc477 | 2011-08-09 18:47:40 +0000 | [diff] [blame] | 128 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 129 | |
reed@google.com | 18dc477 | 2011-08-09 18:47:40 +0000 | [diff] [blame] | 130 | SkPath2DPathEffect::SkPath2DPathEffect(const SkMatrix& m, const SkPath& p) |
| 131 | : INHERITED(m), fPath(p) { |
| 132 | } |
| 133 | |
reed | 60c9b58 | 2016-04-03 09:11:13 -0700 | [diff] [blame] | 134 | sk_sp<SkFlattenable> SkPath2DPathEffect::CreateProc(SkReadBuffer& buffer) { |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 135 | SkMatrix matrix; |
| 136 | buffer.readMatrix(&matrix); |
| 137 | SkPath path; |
| 138 | buffer.readPath(&path); |
reed | 60c9b58 | 2016-04-03 09:11:13 -0700 | [diff] [blame] | 139 | return SkPath2DPathEffect::Make(matrix, path); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 140 | } |
reed@google.com | 18dc477 | 2011-08-09 18:47:40 +0000 | [diff] [blame] | 141 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 142 | void SkPath2DPathEffect::flatten(SkWriteBuffer& buffer) const { |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 143 | buffer.writeMatrix(this->getMatrix()); |
djsollen@google.com | 2b2ede3 | 2012-04-12 13:24:04 +0000 | [diff] [blame] | 144 | buffer.writePath(fPath); |
reed@google.com | 18dc477 | 2011-08-09 18:47:40 +0000 | [diff] [blame] | 145 | } |
| 146 | |
reed@google.com | 548a1f3 | 2012-12-18 16:12:09 +0000 | [diff] [blame] | 147 | void SkPath2DPathEffect::next(const SkPoint& loc, int u, int v, |
| 148 | SkPath* dst) const { |
reed@google.com | 18dc477 | 2011-08-09 18:47:40 +0000 | [diff] [blame] | 149 | dst->addPath(fPath, loc.fX, loc.fY); |
| 150 | } |
Cary Clark | 32a4910 | 2018-05-20 23:15:43 +0000 | [diff] [blame] | 151 | |
| 152 | void SkPath2DPathEffect::toString(SkString* str) const { |
| 153 | str->appendf("SkPath2DPathEffect: ("); |
| 154 | this->INHERITED::toString(str); |
| 155 | // TODO: print out path information |
| 156 | str->appendf(")"); |
| 157 | } |
| 158 | |