blob: ee081f1aea572bb0329e568dbfc96b13c3ecc5f8 [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()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000042 this->begin(ir, dst);
reed@google.com045e62d2011-10-24 12:19:46 +000043 SkScan::FillPath(tmp, ir, &blitter);
reed@android.com8a1c16f2008-12-17 15:59:43 +000044 this->end(dst);
45 }
46 return true;
47}
48
reed@google.com16edff22011-08-12 14:10:27 +000049void Sk2DPathEffect::nextSpan(int x, int y, int count, SkPath* path) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000050 const SkMatrix& mat = this->getMatrix();
51 SkPoint src, dst;
52
53 src.set(SkIntToScalar(x) + SK_ScalarHalf, SkIntToScalar(y) + SK_ScalarHalf);
54 do {
55 mat.mapPoints(&dst, &src, 1);
56 this->next(dst, x++, y, path);
57 src.fX += SK_Scalar1;
58 } while (--count > 0);
59}
60
61void Sk2DPathEffect::begin(const SkIRect& uvBounds, SkPath* dst) {}
62void Sk2DPathEffect::next(const SkPoint& loc, int u, int v, SkPath* dst) {}
63void Sk2DPathEffect::end(SkPath* dst) {}
64
reed@google.com16edff22011-08-12 14:10:27 +000065///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +000066
reed@google.com16edff22011-08-12 14:10:27 +000067void Sk2DPathEffect::flatten(SkFlattenableWriteBuffer& buffer) {
senorblanco@chromium.org1a394932011-05-20 19:19:09 +000068 char storage[SkMatrix::kMaxFlattenSize];
69 uint32_t size = fMatrix.flatten(storage);
70 buffer.write32(size);
71 buffer.write(storage, size);
reed@android.com8a1c16f2008-12-17 15:59:43 +000072}
73
reed@google.com16edff22011-08-12 14:10:27 +000074Sk2DPathEffect::Sk2DPathEffect(SkFlattenableReadBuffer& buffer) {
senorblanco@chromium.org1a394932011-05-20 19:19:09 +000075 char storage[SkMatrix::kMaxFlattenSize];
76 uint32_t size = buffer.readS32();
77 SkASSERT(size <= sizeof(storage));
78 buffer.read(storage, size);
79 fMatrix.unflatten(storage);
reed@android.com8a1c16f2008-12-17 15:59:43 +000080 fMatrix.invert(&fInverse);
81}
82
reed@google.com16edff22011-08-12 14:10:27 +000083SkFlattenable::Factory Sk2DPathEffect::getFactory() {
reed@android.com8a1c16f2008-12-17 15:59:43 +000084 return CreateProc;
85}
86
reed@google.com16edff22011-08-12 14:10:27 +000087SkFlattenable* Sk2DPathEffect::CreateProc(SkFlattenableReadBuffer& buffer) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000088 return SkNEW_ARGS(Sk2DPathEffect, (buffer));
89}
90
reed@google.come28b9172011-08-09 18:14:31 +000091///////////////////////////////////////////////////////////////////////////////
reed@google.com18dc4772011-08-09 18:47:40 +000092///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +000093
reed@google.com18dc4772011-08-09 18:47:40 +000094SkPath2DPathEffect::SkPath2DPathEffect(const SkMatrix& m, const SkPath& p)
95 : INHERITED(m), fPath(p) {
96}
97
98SkPath2DPathEffect::SkPath2DPathEffect(SkFlattenableReadBuffer& buffer)
99 : INHERITED(buffer) {
100 fPath.unflatten(buffer);
101}
102
103SkFlattenable* SkPath2DPathEffect::CreateProc(SkFlattenableReadBuffer& buffer) {
104 return SkNEW_ARGS(SkPath2DPathEffect, (buffer));
105}
106
107void SkPath2DPathEffect::flatten(SkFlattenableWriteBuffer& buffer) {
108 this->INHERITED::flatten(buffer);
109 fPath.flatten(buffer);
110}
111
112SkFlattenable::Factory SkPath2DPathEffect::getFactory() {
113 return CreateProc;
114}
115
116void SkPath2DPathEffect::next(const SkPoint& loc, int u, int v, SkPath* dst) {
117 dst->addPath(fPath, loc.fX, loc.fY);
118}
119
120static SkFlattenable::Registrar gReg("SkPath2DPathEffect",
121 SkPath2DPathEffect::CreateProc);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000122