blob: 1a09a92d6bc363d59919837daa8c864a5b755db5 [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"
djsollen@google.comc73dd5c2012-08-07 15:54:32 +000012#include "SkFlattenableBuffers.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000013#include "SkPath.h"
14#include "SkScan.h"
15
16class Sk2DPathEffectBlitter : public SkBlitter {
17public:
18 Sk2DPathEffectBlitter(Sk2DPathEffect* pe, SkPath* dst)
reed@google.com16edff22011-08-12 14:10:27 +000019 : fPE(pe), fDst(dst) {}
20
21 virtual void blitH(int x, int y, int count) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000022 fPE->nextSpan(x, y, count, fDst);
23 }
24private:
25 Sk2DPathEffect* fPE;
26 SkPath* fDst;
27};
28
reed@google.com16edff22011-08-12 14:10:27 +000029///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +000030
reed@google.com16edff22011-08-12 14:10:27 +000031Sk2DPathEffect::Sk2DPathEffect(const SkMatrix& mat) : fMatrix(mat) {
mike@reedtribe.org90bf4272012-04-14 19:06:16 +000032 fMatrixIsInvertible = mat.invert(&fInverse);
reed@android.com8a1c16f2008-12-17 15:59:43 +000033}
34
reed@google.comfd4be262012-05-25 01:04:12 +000035bool Sk2DPathEffect::filterPath(SkPath* dst, const SkPath& src, SkStrokeRec*) {
mike@reedtribe.org90bf4272012-04-14 19:06:16 +000036 if (!fMatrixIsInvertible) {
37 return false;
38 }
39
reed@android.com8a1c16f2008-12-17 15:59:43 +000040 Sk2DPathEffectBlitter blitter(this, dst);
41 SkPath tmp;
reed@android.com8a1c16f2008-12-17 15:59:43 +000042 SkIRect ir;
43
44 src.transform(fInverse, &tmp);
reed@android.comd252db02009-04-01 18:31:44 +000045 tmp.getBounds().round(&ir);
reed@android.com8a1c16f2008-12-17 15:59:43 +000046 if (!ir.isEmpty()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000047 this->begin(ir, dst);
reed@google.com045e62d2011-10-24 12:19:46 +000048 SkScan::FillPath(tmp, ir, &blitter);
reed@android.com8a1c16f2008-12-17 15:59:43 +000049 this->end(dst);
50 }
51 return true;
52}
53
reed@google.com16edff22011-08-12 14:10:27 +000054void Sk2DPathEffect::nextSpan(int x, int y, int count, SkPath* path) {
mike@reedtribe.org90bf4272012-04-14 19:06:16 +000055 if (!fMatrixIsInvertible) {
56 return;
57 }
58
reed@android.com8a1c16f2008-12-17 15:59:43 +000059 const SkMatrix& mat = this->getMatrix();
60 SkPoint src, dst;
61
62 src.set(SkIntToScalar(x) + SK_ScalarHalf, SkIntToScalar(y) + SK_ScalarHalf);
63 do {
64 mat.mapPoints(&dst, &src, 1);
65 this->next(dst, x++, y, path);
66 src.fX += SK_Scalar1;
67 } while (--count > 0);
68}
69
70void Sk2DPathEffect::begin(const SkIRect& uvBounds, SkPath* dst) {}
71void Sk2DPathEffect::next(const SkPoint& loc, int u, int v, SkPath* dst) {}
72void Sk2DPathEffect::end(SkPath* dst) {}
73
reed@google.com16edff22011-08-12 14:10:27 +000074///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +000075
djsollen@google.com54924242012-03-29 15:18:04 +000076void Sk2DPathEffect::flatten(SkFlattenableWriteBuffer& buffer) const {
77 this->INHERITED::flatten(buffer);
djsollen@google.comd2700ee2012-05-30 16:54:13 +000078 buffer.writeMatrix(fMatrix);
reed@android.com8a1c16f2008-12-17 15:59:43 +000079}
80
reed@google.com16edff22011-08-12 14:10:27 +000081Sk2DPathEffect::Sk2DPathEffect(SkFlattenableReadBuffer& buffer) {
djsollen@google.comd2700ee2012-05-30 16:54:13 +000082 buffer.readMatrix(&fMatrix);
mike@reedtribe.org90bf4272012-04-14 19:06:16 +000083 fMatrixIsInvertible = fMatrix.invert(&fInverse);
reed@android.com8a1c16f2008-12-17 15:59:43 +000084}
85
reed@google.come28b9172011-08-09 18:14:31 +000086///////////////////////////////////////////////////////////////////////////////
reed@google.com18dc4772011-08-09 18:47:40 +000087///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +000088
reed@google.com18dc4772011-08-09 18:47:40 +000089SkPath2DPathEffect::SkPath2DPathEffect(const SkMatrix& m, const SkPath& p)
90 : INHERITED(m), fPath(p) {
91}
92
93SkPath2DPathEffect::SkPath2DPathEffect(SkFlattenableReadBuffer& buffer)
94 : INHERITED(buffer) {
djsollen@google.com2b2ede32012-04-12 13:24:04 +000095 buffer.readPath(&fPath);
reed@google.com18dc4772011-08-09 18:47:40 +000096}
97
djsollen@google.com54924242012-03-29 15:18:04 +000098void SkPath2DPathEffect::flatten(SkFlattenableWriteBuffer& buffer) const {
reed@google.com18dc4772011-08-09 18:47:40 +000099 this->INHERITED::flatten(buffer);
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000100 buffer.writePath(fPath);
reed@google.com18dc4772011-08-09 18:47:40 +0000101}
102
reed@google.com18dc4772011-08-09 18:47:40 +0000103void SkPath2DPathEffect::next(const SkPoint& loc, int u, int v, SkPath* dst) {
104 dst->addPath(fPath, loc.fX, loc.fY);
105}
106
djsollen@google.coma2ca41e2012-03-23 19:00:34 +0000107///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000108
djsollen@google.coma2ca41e2012-03-23 19:00:34 +0000109SK_DEFINE_FLATTENABLE_REGISTRAR(SkPath2DPathEffect)