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) |
| 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 | } |
| 24 | private: |
| 25 | Sk2DPathEffect* fPE; |
| 26 | SkPath* fDst; |
| 27 | }; |
| 28 | |
| 29 | //////////////////////////////////////////////////////////////////////////////////// |
| 30 | |
| 31 | Sk2DPathEffect::Sk2DPathEffect(const SkMatrix& mat) : fMatrix(mat) |
| 32 | { |
| 33 | mat.invert(&fInverse); |
| 34 | } |
| 35 | |
| 36 | bool Sk2DPathEffect::filterPath(SkPath* dst, const SkPath& src, SkScalar* width) |
| 37 | { |
| 38 | Sk2DPathEffectBlitter blitter(this, dst); |
| 39 | SkPath tmp; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 40 | SkIRect ir; |
| 41 | |
| 42 | src.transform(fInverse, &tmp); |
reed@android.com | d252db0 | 2009-04-01 18:31:44 +0000 | [diff] [blame] | 43 | tmp.getBounds().round(&ir); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 44 | 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 | |
| 56 | void 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 | |
| 69 | void Sk2DPathEffect::begin(const SkIRect& uvBounds, SkPath* dst) {} |
| 70 | void Sk2DPathEffect::next(const SkPoint& loc, int u, int v, SkPath* dst) {} |
| 71 | void Sk2DPathEffect::end(SkPath* dst) {} |
| 72 | |
| 73 | //////////////////////////////////////////////////////////////////////////////// |
| 74 | |
| 75 | void Sk2DPathEffect::flatten(SkFlattenableWriteBuffer& buffer) |
| 76 | { |
senorblanco@chromium.org | 1a39493 | 2011-05-20 19:19:09 +0000 | [diff] [blame] | 77 | char storage[SkMatrix::kMaxFlattenSize]; |
| 78 | uint32_t size = fMatrix.flatten(storage); |
| 79 | buffer.write32(size); |
| 80 | buffer.write(storage, size); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | Sk2DPathEffect::Sk2DPathEffect(SkFlattenableReadBuffer& buffer) |
| 84 | { |
senorblanco@chromium.org | 1a39493 | 2011-05-20 19:19:09 +0000 | [diff] [blame] | 85 | 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 90 | fMatrix.invert(&fInverse); |
| 91 | } |
| 92 | |
| 93 | SkFlattenable::Factory Sk2DPathEffect::getFactory() |
| 94 | { |
| 95 | return CreateProc; |
| 96 | } |
| 97 | |
| 98 | SkFlattenable* Sk2DPathEffect::CreateProc(SkFlattenableReadBuffer& buffer) |
| 99 | { |
| 100 | return SkNEW_ARGS(Sk2DPathEffect, (buffer)); |
| 101 | } |
| 102 | |
| 103 | |
| 104 | |