aboutsummaryrefslogtreecommitdiffhomepage
path: root/experimental/sksg/geometry/SkSGTrimEffect.cpp
blob: fea82491610eddb91eabe5ebf5a8caf3d2d45bdc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
 * Copyright 2017 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "SkSGTrimEffect.h"

#include "SkCanvas.h"
#include "SkStrokeRec.h"
#include "SkTrimPathEffect.h"

namespace sksg {

TrimEffect::TrimEffect(sk_sp<GeometryNode> child)
    : fChild(std::move(child)) {
    this->observeInval(fChild);
}

TrimEffect::~TrimEffect() {
    this->unobserveInval(fChild);
}

void TrimEffect::onClip(SkCanvas* canvas, bool antiAlias) const {
    canvas->clipPath(fTrimmedPath, SkClipOp::kIntersect, antiAlias);
}

void TrimEffect::onDraw(SkCanvas* canvas, const SkPaint& paint) const {
    SkASSERT(!paint.getPathEffect());

    canvas->drawPath(fTrimmedPath, paint);
}

SkPath TrimEffect::onAsPath() const {
    return fTrimmedPath;
}

SkRect TrimEffect::onRevalidate(InvalidationController* ic, const SkMatrix& ctm) {
    SkASSERT(this->hasInval());

    const auto childbounds = fChild->revalidate(ic, ctm);
    const auto path        = fChild->asPath();

    // TODO: relocate these funky semantics to a Skottie composite helper,
    //       and refactor TrimEffect as a thin SkTrimpPathEffect wrapper.
    auto start = SkTMin(fStart, fEnd) + fOffset,
         stop  = SkTMax(fStart, fEnd) + fOffset;

    sk_sp<SkPathEffect> trim;
    if (stop - start < 1) {
        start -= SkScalarFloorToScalar(start);
        stop  -= SkScalarFloorToScalar(stop);

        trim = start <= stop
            ? SkTrimPathEffect::Make(start, stop, SkTrimPathEffect::Mode::kNormal)
            : SkTrimPathEffect::Make(stop, start, SkTrimPathEffect::Mode::kInverted);
    }

    if (trim) {
        fTrimmedPath.reset();
        SkStrokeRec rec(SkStrokeRec::kHairline_InitStyle);
        SkAssertResult(trim->filterPath(&fTrimmedPath, path, &rec, &childbounds));
    } else {
        fTrimmedPath = path;
    }

    return fTrimmedPath.computeTightBounds();
}

} // namespace sksg