blob: 95f425fd15388d001d8054eb1e503704943337a2 [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) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000031 mat.invert(&fInverse);
32}
33
reed@google.com16edff22011-08-12 14:10:27 +000034bool Sk2DPathEffect::filterPath(SkPath* dst, const SkPath& src, SkScalar* width) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000035 Sk2DPathEffectBlitter blitter(this, dst);
36 SkPath tmp;
reed@android.com8a1c16f2008-12-17 15:59:43 +000037 SkIRect ir;
38
39 src.transform(fInverse, &tmp);
reed@android.comd252db02009-04-01 18:31:44 +000040 tmp.getBounds().round(&ir);
reed@android.com8a1c16f2008-12-17 15:59:43 +000041 if (!ir.isEmpty()) {
42 // need to pass a clip to fillpath, required for inverse filltypes,
43 // even though those do not make sense for this patheffect
44 SkRegion clip(ir);
45
46 this->begin(ir, dst);
47 SkScan::FillPath(tmp, clip, &blitter);
48 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) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000054 const SkMatrix& mat = this->getMatrix();
55 SkPoint src, dst;
56
57 src.set(SkIntToScalar(x) + SK_ScalarHalf, SkIntToScalar(y) + SK_ScalarHalf);
58 do {
59 mat.mapPoints(&dst, &src, 1);
60 this->next(dst, x++, y, path);
61 src.fX += SK_Scalar1;
62 } while (--count > 0);
63}
64
65void Sk2DPathEffect::begin(const SkIRect& uvBounds, SkPath* dst) {}
66void Sk2DPathEffect::next(const SkPoint& loc, int u, int v, SkPath* dst) {}
67void Sk2DPathEffect::end(SkPath* dst) {}
68
reed@google.com16edff22011-08-12 14:10:27 +000069///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +000070
reed@google.com16edff22011-08-12 14:10:27 +000071void Sk2DPathEffect::flatten(SkFlattenableWriteBuffer& buffer) {
senorblanco@chromium.org1a394932011-05-20 19:19:09 +000072 char storage[SkMatrix::kMaxFlattenSize];
73 uint32_t size = fMatrix.flatten(storage);
74 buffer.write32(size);
75 buffer.write(storage, size);
reed@android.com8a1c16f2008-12-17 15:59:43 +000076}
77
reed@google.com16edff22011-08-12 14:10:27 +000078Sk2DPathEffect::Sk2DPathEffect(SkFlattenableReadBuffer& buffer) {
senorblanco@chromium.org1a394932011-05-20 19:19:09 +000079 char storage[SkMatrix::kMaxFlattenSize];
80 uint32_t size = buffer.readS32();
81 SkASSERT(size <= sizeof(storage));
82 buffer.read(storage, size);
83 fMatrix.unflatten(storage);
reed@android.com8a1c16f2008-12-17 15:59:43 +000084 fMatrix.invert(&fInverse);
85}
86
reed@google.com16edff22011-08-12 14:10:27 +000087SkFlattenable::Factory Sk2DPathEffect::getFactory() {
reed@android.com8a1c16f2008-12-17 15:59:43 +000088 return CreateProc;
89}
90
reed@google.com16edff22011-08-12 14:10:27 +000091SkFlattenable* Sk2DPathEffect::CreateProc(SkFlattenableReadBuffer& buffer) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000092 return SkNEW_ARGS(Sk2DPathEffect, (buffer));
93}
94
reed@google.come28b9172011-08-09 18:14:31 +000095///////////////////////////////////////////////////////////////////////////////
reed@google.com18dc4772011-08-09 18:47:40 +000096///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +000097
reed@google.com18dc4772011-08-09 18:47:40 +000098SkPath2DPathEffect::SkPath2DPathEffect(const SkMatrix& m, const SkPath& p)
99 : INHERITED(m), fPath(p) {
100}
101
102SkPath2DPathEffect::SkPath2DPathEffect(SkFlattenableReadBuffer& buffer)
103 : INHERITED(buffer) {
104 fPath.unflatten(buffer);
105}
106
107SkFlattenable* SkPath2DPathEffect::CreateProc(SkFlattenableReadBuffer& buffer) {
108 return SkNEW_ARGS(SkPath2DPathEffect, (buffer));
109}
110
111void SkPath2DPathEffect::flatten(SkFlattenableWriteBuffer& buffer) {
112 this->INHERITED::flatten(buffer);
113 fPath.flatten(buffer);
114}
115
116SkFlattenable::Factory SkPath2DPathEffect::getFactory() {
117 return CreateProc;
118}
119
120void SkPath2DPathEffect::next(const SkPoint& loc, int u, int v, SkPath* dst) {
121 dst->addPath(fPath, loc.fX, loc.fY);
122}
123
124static SkFlattenable::Registrar gReg("SkPath2DPathEffect",
125 SkPath2DPathEffect::CreateProc);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000126