blob: e19cdeb117fb91153c53c5f14c8cdd999f2bba2b [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2006 The Android Open Source Project
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
reed@android.com8a1c16f2008-12-17 15:59:43 +00009
10#include "Sk2DPathEffect.h"
11#include "SkBlitter.h"
12#include "SkPath.h"
13#include "SkScan.h"
14
15class Sk2DPathEffectBlitter : public SkBlitter {
16public:
17 Sk2DPathEffectBlitter(Sk2DPathEffect* pe, SkPath* dst)
reed@google.com16edff22011-08-12 14:10:27 +000018 : fPE(pe), fDst(dst) {}
19
20 virtual void blitH(int x, int y, int count) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000021 fPE->nextSpan(x, y, count, fDst);
22 }
23private:
24 Sk2DPathEffect* fPE;
25 SkPath* fDst;
26};
27
reed@google.com16edff22011-08-12 14:10:27 +000028///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +000029
reed@google.com16edff22011-08-12 14:10:27 +000030Sk2DPathEffect::Sk2DPathEffect(const SkMatrix& mat) : fMatrix(mat) {
mike@reedtribe.org90bf4272012-04-14 19:06:16 +000031 fMatrixIsInvertible = mat.invert(&fInverse);
reed@android.com8a1c16f2008-12-17 15:59:43 +000032}
33
reed@google.comfd4be262012-05-25 01:04:12 +000034bool Sk2DPathEffect::filterPath(SkPath* dst, const SkPath& src, SkStrokeRec*) {
mike@reedtribe.org90bf4272012-04-14 19:06:16 +000035 if (!fMatrixIsInvertible) {
36 return false;
37 }
38
reed@android.com8a1c16f2008-12-17 15:59:43 +000039 Sk2DPathEffectBlitter blitter(this, dst);
40 SkPath tmp;
reed@android.com8a1c16f2008-12-17 15:59:43 +000041 SkIRect ir;
42
43 src.transform(fInverse, &tmp);
reed@android.comd252db02009-04-01 18:31:44 +000044 tmp.getBounds().round(&ir);
reed@android.com8a1c16f2008-12-17 15:59:43 +000045 if (!ir.isEmpty()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000046 this->begin(ir, dst);
reed@google.com045e62d2011-10-24 12:19:46 +000047 SkScan::FillPath(tmp, ir, &blitter);
reed@android.com8a1c16f2008-12-17 15:59:43 +000048 this->end(dst);
49 }
50 return true;
51}
52
reed@google.com16edff22011-08-12 14:10:27 +000053void Sk2DPathEffect::nextSpan(int x, int y, int count, SkPath* path) {
mike@reedtribe.org90bf4272012-04-14 19:06:16 +000054 if (!fMatrixIsInvertible) {
55 return;
56 }
57
reed@android.com8a1c16f2008-12-17 15:59:43 +000058 const SkMatrix& mat = this->getMatrix();
59 SkPoint src, dst;
60
61 src.set(SkIntToScalar(x) + SK_ScalarHalf, SkIntToScalar(y) + SK_ScalarHalf);
62 do {
63 mat.mapPoints(&dst, &src, 1);
64 this->next(dst, x++, y, path);
65 src.fX += SK_Scalar1;
66 } while (--count > 0);
67}
68
69void Sk2DPathEffect::begin(const SkIRect& uvBounds, SkPath* dst) {}
70void Sk2DPathEffect::next(const SkPoint& loc, int u, int v, SkPath* dst) {}
71void Sk2DPathEffect::end(SkPath* dst) {}
72
reed@google.com16edff22011-08-12 14:10:27 +000073///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +000074
djsollen@google.com54924242012-03-29 15:18:04 +000075void Sk2DPathEffect::flatten(SkFlattenableWriteBuffer& buffer) const {
76 this->INHERITED::flatten(buffer);
djsollen@google.comd2700ee2012-05-30 16:54:13 +000077 buffer.writeMatrix(fMatrix);
reed@android.com8a1c16f2008-12-17 15:59:43 +000078}
79
reed@google.com16edff22011-08-12 14:10:27 +000080Sk2DPathEffect::Sk2DPathEffect(SkFlattenableReadBuffer& buffer) {
djsollen@google.comd2700ee2012-05-30 16:54:13 +000081 buffer.readMatrix(&fMatrix);
mike@reedtribe.org90bf4272012-04-14 19:06:16 +000082 fMatrixIsInvertible = fMatrix.invert(&fInverse);
reed@android.com8a1c16f2008-12-17 15:59:43 +000083}
84
reed@google.come28b9172011-08-09 18:14:31 +000085///////////////////////////////////////////////////////////////////////////////
reed@google.com18dc4772011-08-09 18:47:40 +000086///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +000087
reed@google.com18dc4772011-08-09 18:47:40 +000088SkPath2DPathEffect::SkPath2DPathEffect(const SkMatrix& m, const SkPath& p)
89 : INHERITED(m), fPath(p) {
90}
91
92SkPath2DPathEffect::SkPath2DPathEffect(SkFlattenableReadBuffer& buffer)
93 : INHERITED(buffer) {
djsollen@google.com2b2ede32012-04-12 13:24:04 +000094 buffer.readPath(&fPath);
reed@google.com18dc4772011-08-09 18:47:40 +000095}
96
djsollen@google.com54924242012-03-29 15:18:04 +000097void SkPath2DPathEffect::flatten(SkFlattenableWriteBuffer& buffer) const {
reed@google.com18dc4772011-08-09 18:47:40 +000098 this->INHERITED::flatten(buffer);
djsollen@google.com2b2ede32012-04-12 13:24:04 +000099 buffer.writePath(fPath);
reed@google.com18dc4772011-08-09 18:47:40 +0000100}
101
reed@google.com18dc4772011-08-09 18:47:40 +0000102void SkPath2DPathEffect::next(const SkPoint& loc, int u, int v, SkPath* dst) {
103 dst->addPath(fPath, loc.fX, loc.fY);
104}
105
djsollen@google.coma2ca41e2012-03-23 19:00:34 +0000106///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000107
djsollen@google.coma2ca41e2012-03-23 19:00:34 +0000108SK_DEFINE_FLATTENABLE_REGISTRAR(SkPath2DPathEffect)