epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 9 | |
| 10 | #include "Sk2DPathEffect.h" |
| 11 | #include "SkBlitter.h" |
| 12 | #include "SkPath.h" |
| 13 | #include "SkScan.h" |
| 14 | |
| 15 | class Sk2DPathEffectBlitter : public SkBlitter { |
| 16 | public: |
| 17 | Sk2DPathEffectBlitter(Sk2DPathEffect* pe, SkPath* dst) |
reed@google.com | 16edff2 | 2011-08-12 14:10:27 +0000 | [diff] [blame^] | 18 | : fPE(pe), fDst(dst) {} |
| 19 | |
| 20 | virtual void blitH(int x, int y, int count) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 21 | fPE->nextSpan(x, y, count, fDst); |
| 22 | } |
| 23 | private: |
| 24 | Sk2DPathEffect* fPE; |
| 25 | SkPath* fDst; |
| 26 | }; |
| 27 | |
reed@google.com | 16edff2 | 2011-08-12 14:10:27 +0000 | [diff] [blame^] | 28 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 29 | |
reed@google.com | 16edff2 | 2011-08-12 14:10:27 +0000 | [diff] [blame^] | 30 | Sk2DPathEffect::Sk2DPathEffect(const SkMatrix& mat) : fMatrix(mat) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 31 | mat.invert(&fInverse); |
| 32 | } |
| 33 | |
reed@google.com | 16edff2 | 2011-08-12 14:10:27 +0000 | [diff] [blame^] | 34 | bool Sk2DPathEffect::filterPath(SkPath* dst, const SkPath& src, SkScalar* width) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 35 | Sk2DPathEffectBlitter blitter(this, dst); |
| 36 | SkPath tmp; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 37 | SkIRect ir; |
| 38 | |
| 39 | src.transform(fInverse, &tmp); |
reed@android.com | d252db0 | 2009-04-01 18:31:44 +0000 | [diff] [blame] | 40 | tmp.getBounds().round(&ir); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 41 | 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.com | 16edff2 | 2011-08-12 14:10:27 +0000 | [diff] [blame^] | 53 | void Sk2DPathEffect::nextSpan(int x, int y, int count, SkPath* path) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 54 | 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 | |
| 65 | void Sk2DPathEffect::begin(const SkIRect& uvBounds, SkPath* dst) {} |
| 66 | void Sk2DPathEffect::next(const SkPoint& loc, int u, int v, SkPath* dst) {} |
| 67 | void Sk2DPathEffect::end(SkPath* dst) {} |
| 68 | |
reed@google.com | 16edff2 | 2011-08-12 14:10:27 +0000 | [diff] [blame^] | 69 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 70 | |
reed@google.com | 16edff2 | 2011-08-12 14:10:27 +0000 | [diff] [blame^] | 71 | void Sk2DPathEffect::flatten(SkFlattenableWriteBuffer& buffer) { |
senorblanco@chromium.org | 1a39493 | 2011-05-20 19:19:09 +0000 | [diff] [blame] | 72 | char storage[SkMatrix::kMaxFlattenSize]; |
| 73 | uint32_t size = fMatrix.flatten(storage); |
| 74 | buffer.write32(size); |
| 75 | buffer.write(storage, size); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 76 | } |
| 77 | |
reed@google.com | 16edff2 | 2011-08-12 14:10:27 +0000 | [diff] [blame^] | 78 | Sk2DPathEffect::Sk2DPathEffect(SkFlattenableReadBuffer& buffer) { |
senorblanco@chromium.org | 1a39493 | 2011-05-20 19:19:09 +0000 | [diff] [blame] | 79 | 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 84 | fMatrix.invert(&fInverse); |
| 85 | } |
| 86 | |
reed@google.com | 16edff2 | 2011-08-12 14:10:27 +0000 | [diff] [blame^] | 87 | SkFlattenable::Factory Sk2DPathEffect::getFactory() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 88 | return CreateProc; |
| 89 | } |
| 90 | |
reed@google.com | 16edff2 | 2011-08-12 14:10:27 +0000 | [diff] [blame^] | 91 | SkFlattenable* Sk2DPathEffect::CreateProc(SkFlattenableReadBuffer& buffer) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 92 | return SkNEW_ARGS(Sk2DPathEffect, (buffer)); |
| 93 | } |
| 94 | |
reed@google.com | e28b917 | 2011-08-09 18:14:31 +0000 | [diff] [blame] | 95 | /////////////////////////////////////////////////////////////////////////////// |
reed@google.com | 18dc477 | 2011-08-09 18:47:40 +0000 | [diff] [blame] | 96 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 97 | |
reed@google.com | 18dc477 | 2011-08-09 18:47:40 +0000 | [diff] [blame] | 98 | SkPath2DPathEffect::SkPath2DPathEffect(const SkMatrix& m, const SkPath& p) |
| 99 | : INHERITED(m), fPath(p) { |
| 100 | } |
| 101 | |
| 102 | SkPath2DPathEffect::SkPath2DPathEffect(SkFlattenableReadBuffer& buffer) |
| 103 | : INHERITED(buffer) { |
| 104 | fPath.unflatten(buffer); |
| 105 | } |
| 106 | |
| 107 | SkFlattenable* SkPath2DPathEffect::CreateProc(SkFlattenableReadBuffer& buffer) { |
| 108 | return SkNEW_ARGS(SkPath2DPathEffect, (buffer)); |
| 109 | } |
| 110 | |
| 111 | void SkPath2DPathEffect::flatten(SkFlattenableWriteBuffer& buffer) { |
| 112 | this->INHERITED::flatten(buffer); |
| 113 | fPath.flatten(buffer); |
| 114 | } |
| 115 | |
| 116 | SkFlattenable::Factory SkPath2DPathEffect::getFactory() { |
| 117 | return CreateProc; |
| 118 | } |
| 119 | |
| 120 | void SkPath2DPathEffect::next(const SkPoint& loc, int u, int v, SkPath* dst) { |
| 121 | dst->addPath(fPath, loc.fX, loc.fY); |
| 122 | } |
| 123 | |
| 124 | static SkFlattenable::Registrar gReg("SkPath2DPathEffect", |
| 125 | SkPath2DPathEffect::CreateProc); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 126 | |