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