blob: dd067be18699c10c313c7340cc606382223ef6ae [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@google.comfc9a3be2012-04-12 13:52:14 +000031 if (!mat.invert(&fInverse)) {
32 fInverse.reset();
33 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000034}
35
reed@google.com16edff22011-08-12 14:10:27 +000036bool Sk2DPathEffect::filterPath(SkPath* dst, const SkPath& src, SkScalar* width) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000037 Sk2DPathEffectBlitter blitter(this, dst);
38 SkPath tmp;
reed@android.com8a1c16f2008-12-17 15:59:43 +000039 SkIRect ir;
40
41 src.transform(fInverse, &tmp);
reed@android.comd252db02009-04-01 18:31:44 +000042 tmp.getBounds().round(&ir);
reed@android.com8a1c16f2008-12-17 15:59:43 +000043 if (!ir.isEmpty()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000044 this->begin(ir, dst);
reed@google.com045e62d2011-10-24 12:19:46 +000045 SkScan::FillPath(tmp, ir, &blitter);
reed@android.com8a1c16f2008-12-17 15:59:43 +000046 this->end(dst);
47 }
48 return true;
49}
50
reed@google.com16edff22011-08-12 14:10:27 +000051void Sk2DPathEffect::nextSpan(int x, int y, int count, SkPath* path) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000052 const SkMatrix& mat = this->getMatrix();
53 SkPoint src, dst;
54
55 src.set(SkIntToScalar(x) + SK_ScalarHalf, SkIntToScalar(y) + SK_ScalarHalf);
56 do {
57 mat.mapPoints(&dst, &src, 1);
58 this->next(dst, x++, y, path);
59 src.fX += SK_Scalar1;
60 } while (--count > 0);
61}
62
63void Sk2DPathEffect::begin(const SkIRect& uvBounds, SkPath* dst) {}
64void Sk2DPathEffect::next(const SkPoint& loc, int u, int v, SkPath* dst) {}
65void Sk2DPathEffect::end(SkPath* dst) {}
66
reed@google.com16edff22011-08-12 14:10:27 +000067///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +000068
djsollen@google.com54924242012-03-29 15:18:04 +000069void Sk2DPathEffect::flatten(SkFlattenableWriteBuffer& buffer) const {
70 this->INHERITED::flatten(buffer);
senorblanco@chromium.org1a394932011-05-20 19:19:09 +000071 char storage[SkMatrix::kMaxFlattenSize];
72 uint32_t size = fMatrix.flatten(storage);
73 buffer.write32(size);
74 buffer.write(storage, size);
reed@android.com8a1c16f2008-12-17 15:59:43 +000075}
76
reed@google.com16edff22011-08-12 14:10:27 +000077Sk2DPathEffect::Sk2DPathEffect(SkFlattenableReadBuffer& buffer) {
senorblanco@chromium.org1a394932011-05-20 19:19:09 +000078 char storage[SkMatrix::kMaxFlattenSize];
79 uint32_t size = buffer.readS32();
80 SkASSERT(size <= sizeof(storage));
81 buffer.read(storage, size);
82 fMatrix.unflatten(storage);
reed@google.comfc9a3be2012-04-12 13:52:14 +000083 if (!fMatrix.invert(&fInverse)) {
84 fInverse.reset();
85 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000086}
87
reed@google.come28b9172011-08-09 18:14:31 +000088///////////////////////////////////////////////////////////////////////////////
reed@google.com18dc4772011-08-09 18:47:40 +000089///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +000090
reed@google.com18dc4772011-08-09 18:47:40 +000091SkPath2DPathEffect::SkPath2DPathEffect(const SkMatrix& m, const SkPath& p)
92 : INHERITED(m), fPath(p) {
93}
94
95SkPath2DPathEffect::SkPath2DPathEffect(SkFlattenableReadBuffer& buffer)
96 : INHERITED(buffer) {
djsollen@google.com2b2ede32012-04-12 13:24:04 +000097 buffer.readPath(&fPath);
reed@google.com18dc4772011-08-09 18:47:40 +000098}
99
djsollen@google.com54924242012-03-29 15:18:04 +0000100void SkPath2DPathEffect::flatten(SkFlattenableWriteBuffer& buffer) const {
reed@google.com18dc4772011-08-09 18:47:40 +0000101 this->INHERITED::flatten(buffer);
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000102 buffer.writePath(fPath);
reed@google.com18dc4772011-08-09 18:47:40 +0000103}
104
reed@google.com18dc4772011-08-09 18:47:40 +0000105void SkPath2DPathEffect::next(const SkPoint& loc, int u, int v, SkPath* dst) {
106 dst->addPath(fPath, loc.fX, loc.fY);
107}
108
djsollen@google.coma2ca41e2012-03-23 19:00:34 +0000109///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000110
djsollen@google.coma2ca41e2012-03-23 19:00:34 +0000111SK_DEFINE_FLATTENABLE_REGISTRAR(SkPath2DPathEffect)