blob: 73740775b892bdfacf16f7a77d1f7fc09b75b3b9 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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.com8a1c16f2008-12-17 15:59:43 +00008
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009#include "include/core/SkPath.h"
10#include "include/core/SkRegion.h"
11#include "include/core/SkStrokeRec.h"
12#include "include/effects/Sk2DPathEffect.h"
13#include "src/core/SkReadBuffer.h"
14#include "src/core/SkWriteBuffer.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000015
reed@google.com16edff22011-08-12 14:10:27 +000016Sk2DPathEffect::Sk2DPathEffect(const SkMatrix& mat) : fMatrix(mat) {
bungeman6c3baa62016-09-16 07:21:28 -070017 // Calling invert will set the type mask on both matrices, making them thread safe.
18 fMatrixIsInvertible = fMatrix.invert(&fInverse);
reed@android.com8a1c16f2008-12-17 15:59:43 +000019}
20
Mike Reed6d10f8b2018-08-16 13:22:16 -040021bool Sk2DPathEffect::onFilterPath(SkPath* dst, const SkPath& src,
22 SkStrokeRec*, const SkRect*) const {
mike@reedtribe.org90bf4272012-04-14 19:06:16 +000023 if (!fMatrixIsInvertible) {
24 return false;
25 }
26
reed@google.com6db93752012-08-09 19:18:02 +000027 SkPath tmp;
28 SkIRect ir;
reed@android.com8a1c16f2008-12-17 15:59:43 +000029
30 src.transform(fInverse, &tmp);
reed@android.comd252db02009-04-01 18:31:44 +000031 tmp.getBounds().round(&ir);
reed@android.com8a1c16f2008-12-17 15:59:43 +000032 if (!ir.isEmpty()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000033 this->begin(ir, dst);
reed@google.com6db93752012-08-09 19:18:02 +000034
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.com8a1c16f2008-12-17 15:59:43 +000045 this->end(dst);
46 }
47 return true;
48}
49
reed@google.com548a1f32012-12-18 16:12:09 +000050void Sk2DPathEffect::nextSpan(int x, int y, int count, SkPath* path) const {
mike@reedtribe.org90bf4272012-04-14 19:06:16 +000051 if (!fMatrixIsInvertible) {
52 return;
53 }
Kevin Lubick493f89e2020-09-14 08:37:35 -040054#if defined(SK_BUILD_FOR_FUZZER)
55 if (count > 100) {
56 return;
57 }
58#endif
mike@reedtribe.org90bf4272012-04-14 19:06:16 +000059
reed@android.com8a1c16f2008-12-17 15:59:43 +000060 const SkMatrix& mat = this->getMatrix();
61 SkPoint src, dst;
62
63 src.set(SkIntToScalar(x) + SK_ScalarHalf, SkIntToScalar(y) + SK_ScalarHalf);
64 do {
65 mat.mapPoints(&dst, &src, 1);
66 this->next(dst, x++, y, path);
67 src.fX += SK_Scalar1;
68 } while (--count > 0);
69}
70
reed@google.com548a1f32012-12-18 16:12:09 +000071void Sk2DPathEffect::begin(const SkIRect& uvBounds, SkPath* dst) const {}
72void Sk2DPathEffect::next(const SkPoint& loc, int u, int v, SkPath* dst) const {}
73void Sk2DPathEffect::end(SkPath* dst) const {}
reed@android.com8a1c16f2008-12-17 15:59:43 +000074
reed@google.com16edff22011-08-12 14:10:27 +000075///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +000076
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000077void Sk2DPathEffect::flatten(SkWriteBuffer& buffer) const {
djsollen@google.com54924242012-03-29 15:18:04 +000078 this->INHERITED::flatten(buffer);
djsollen@google.comd2700ee2012-05-30 16:54:13 +000079 buffer.writeMatrix(fMatrix);
reed@android.com8a1c16f2008-12-17 15:59:43 +000080}
81
reed@google.come28b9172011-08-09 18:14:31 +000082///////////////////////////////////////////////////////////////////////////////
scroggo@google.comd8a6cc82012-09-12 18:53:49 +000083
Mike Reed6d10f8b2018-08-16 13:22:16 -040084bool SkLine2DPathEffect::onFilterPath(SkPath* dst, const SkPath& src,
85 SkStrokeRec* rec, const SkRect* cullRect) const {
Mike Reed90f10ef2018-08-23 16:31:44 -040086 if (this->INHERITED::onFilterPath(dst, src, rec, cullRect)) {
scroggo@google.comd8a6cc82012-09-12 18:53:49 +000087 rec->setStrokeStyle(fWidth);
88 return true;
89 }
90 return false;
91}
92
reed@google.com548a1f32012-12-18 16:12:09 +000093void SkLine2DPathEffect::nextSpan(int u, int v, int ucount, SkPath* dst) const {
scroggo@google.comd8a6cc82012-09-12 18:53:49 +000094 if (ucount > 1) {
95 SkPoint src[2], dstP[2];
96
97 src[0].set(SkIntToScalar(u) + SK_ScalarHalf, SkIntToScalar(v) + SK_ScalarHalf);
98 src[1].set(SkIntToScalar(u+ucount) + SK_ScalarHalf, SkIntToScalar(v) + SK_ScalarHalf);
99 this->getMatrix().mapPoints(dstP, src, 2);
100
101 dst->moveTo(dstP[0]);
102 dst->lineTo(dstP[1]);
103 }
104}
105
reed60c9b582016-04-03 09:11:13 -0700106sk_sp<SkFlattenable> SkLine2DPathEffect::CreateProc(SkReadBuffer& buffer) {
reed9fa60da2014-08-21 07:59:51 -0700107 SkMatrix matrix;
108 buffer.readMatrix(&matrix);
109 SkScalar width = buffer.readScalar();
reed60c9b582016-04-03 09:11:13 -0700110 return SkLine2DPathEffect::Make(width, matrix);
reed9fa60da2014-08-21 07:59:51 -0700111}
scroggo@google.comd8a6cc82012-09-12 18:53:49 +0000112
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000113void SkLine2DPathEffect::flatten(SkWriteBuffer &buffer) const {
reed9fa60da2014-08-21 07:59:51 -0700114 buffer.writeMatrix(this->getMatrix());
scroggo@google.comd8a6cc82012-09-12 18:53:49 +0000115 buffer.writeScalar(fWidth);
116}
117
reed@google.com18dc4772011-08-09 18:47:40 +0000118///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000119
reed@google.com18dc4772011-08-09 18:47:40 +0000120SkPath2DPathEffect::SkPath2DPathEffect(const SkMatrix& m, const SkPath& p)
121 : INHERITED(m), fPath(p) {
122}
123
reed60c9b582016-04-03 09:11:13 -0700124sk_sp<SkFlattenable> SkPath2DPathEffect::CreateProc(SkReadBuffer& buffer) {
reed9fa60da2014-08-21 07:59:51 -0700125 SkMatrix matrix;
126 buffer.readMatrix(&matrix);
127 SkPath path;
128 buffer.readPath(&path);
reed60c9b582016-04-03 09:11:13 -0700129 return SkPath2DPathEffect::Make(matrix, path);
reed9fa60da2014-08-21 07:59:51 -0700130}
reed@google.com18dc4772011-08-09 18:47:40 +0000131
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000132void SkPath2DPathEffect::flatten(SkWriteBuffer& buffer) const {
reed9fa60da2014-08-21 07:59:51 -0700133 buffer.writeMatrix(this->getMatrix());
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000134 buffer.writePath(fPath);
reed@google.com18dc4772011-08-09 18:47:40 +0000135}
136
reed@google.com548a1f32012-12-18 16:12:09 +0000137void SkPath2DPathEffect::next(const SkPoint& loc, int u, int v,
138 SkPath* dst) const {
reed@google.com18dc4772011-08-09 18:47:40 +0000139 dst->addPath(fPath, loc.fX, loc.fY);
140}