blob: 486dc47edc1fda9dea67924fd4eff261881c19dc [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)
18 : fPE(pe), fDst(dst)
19 {}
20 virtual void blitH(int x, int y, int count)
21 {
22 fPE->nextSpan(x, y, count, fDst);
23 }
24private:
25 Sk2DPathEffect* fPE;
26 SkPath* fDst;
27};
28
29////////////////////////////////////////////////////////////////////////////////////
30
31Sk2DPathEffect::Sk2DPathEffect(const SkMatrix& mat) : fMatrix(mat)
32{
33 mat.invert(&fInverse);
34}
35
36bool Sk2DPathEffect::filterPath(SkPath* dst, const SkPath& src, SkScalar* width)
37{
38 Sk2DPathEffectBlitter blitter(this, dst);
39 SkPath tmp;
reed@android.com8a1c16f2008-12-17 15:59:43 +000040 SkIRect ir;
41
42 src.transform(fInverse, &tmp);
reed@android.comd252db02009-04-01 18:31:44 +000043 tmp.getBounds().round(&ir);
reed@android.com8a1c16f2008-12-17 15:59:43 +000044 if (!ir.isEmpty()) {
45 // need to pass a clip to fillpath, required for inverse filltypes,
46 // even though those do not make sense for this patheffect
47 SkRegion clip(ir);
48
49 this->begin(ir, dst);
50 SkScan::FillPath(tmp, clip, &blitter);
51 this->end(dst);
52 }
53 return true;
54}
55
56void Sk2DPathEffect::nextSpan(int x, int y, int count, SkPath* path)
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 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
73////////////////////////////////////////////////////////////////////////////////
74
75void Sk2DPathEffect::flatten(SkFlattenableWriteBuffer& buffer)
76{
senorblanco@chromium.org1a394932011-05-20 19:19:09 +000077 char storage[SkMatrix::kMaxFlattenSize];
78 uint32_t size = fMatrix.flatten(storage);
79 buffer.write32(size);
80 buffer.write(storage, size);
reed@android.com8a1c16f2008-12-17 15:59:43 +000081}
82
83Sk2DPathEffect::Sk2DPathEffect(SkFlattenableReadBuffer& buffer)
84{
senorblanco@chromium.org1a394932011-05-20 19:19:09 +000085 char storage[SkMatrix::kMaxFlattenSize];
86 uint32_t size = buffer.readS32();
87 SkASSERT(size <= sizeof(storage));
88 buffer.read(storage, size);
89 fMatrix.unflatten(storage);
reed@android.com8a1c16f2008-12-17 15:59:43 +000090 fMatrix.invert(&fInverse);
91}
92
93SkFlattenable::Factory Sk2DPathEffect::getFactory()
94{
95 return CreateProc;
96}
97
98SkFlattenable* Sk2DPathEffect::CreateProc(SkFlattenableReadBuffer& buffer)
99{
100 return SkNEW_ARGS(Sk2DPathEffect, (buffer));
101}
102
reed@google.come28b9172011-08-09 18:14:31 +0000103///////////////////////////////////////////////////////////////////////////////
reed@google.com18dc4772011-08-09 18:47:40 +0000104///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000105
reed@google.com18dc4772011-08-09 18:47:40 +0000106SkPath2DPathEffect::SkPath2DPathEffect(const SkMatrix& m, const SkPath& p)
107 : INHERITED(m), fPath(p) {
108}
109
110SkPath2DPathEffect::SkPath2DPathEffect(SkFlattenableReadBuffer& buffer)
111 : INHERITED(buffer) {
112 fPath.unflatten(buffer);
113}
114
115SkFlattenable* SkPath2DPathEffect::CreateProc(SkFlattenableReadBuffer& buffer) {
116 return SkNEW_ARGS(SkPath2DPathEffect, (buffer));
117}
118
119void SkPath2DPathEffect::flatten(SkFlattenableWriteBuffer& buffer) {
120 this->INHERITED::flatten(buffer);
121 fPath.flatten(buffer);
122}
123
124SkFlattenable::Factory SkPath2DPathEffect::getFactory() {
125 return CreateProc;
126}
127
128void SkPath2DPathEffect::next(const SkPoint& loc, int u, int v, SkPath* dst) {
129 dst->addPath(fPath, loc.fX, loc.fY);
130}
131
132static SkFlattenable::Registrar gReg("SkPath2DPathEffect",
133 SkPath2DPathEffect::CreateProc);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000134