• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2017 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #include "SkSGMerge.h"
9 
10 #include "SkCanvas.h"
11 #include "SkPathOps.h"
12 
13 namespace sksg {
14 
Merge(std::vector<Rec> && recs)15 Merge::Merge(std::vector<Rec>&& recs)
16     : fRecs(std::move(recs)) {
17     for (const auto& rec : fRecs) {
18         this->observeInval(rec.fGeo);
19     }
20 }
21 
~Merge()22 Merge::~Merge() {
23     for (const auto& rec : fRecs) {
24         this->unobserveInval(rec.fGeo);
25     }
26 }
27 
onClip(SkCanvas * canvas,bool antiAlias) const28 void Merge::onClip(SkCanvas* canvas, bool antiAlias) const {
29     canvas->clipPath(fMerged, SkClipOp::kIntersect, antiAlias);
30 }
31 
onDraw(SkCanvas * canvas,const SkPaint & paint) const32 void Merge::onDraw(SkCanvas* canvas, const SkPaint& paint) const {
33     canvas->drawPath(fMerged, paint);
34 }
35 
onAsPath() const36 SkPath Merge::onAsPath() const {
37     return fMerged;
38 }
39 
mode_to_op(Merge::Mode mode)40 static SkPathOp mode_to_op(Merge::Mode mode) {
41     switch (mode) {
42     case Merge::Mode::kUnion:
43         return kUnion_SkPathOp;
44     case Merge::Mode::kIntersect:
45         return kIntersect_SkPathOp;
46     case Merge::Mode::kDifference:
47         return kDifference_SkPathOp;
48     case Merge::Mode::kReverseDifference:
49         return kReverseDifference_SkPathOp;
50     case Merge::Mode::kXOR:
51         return kXOR_SkPathOp;
52     default:
53         break;
54     }
55 
56     return kUnion_SkPathOp;
57 }
58 
onRevalidate(InvalidationController * ic,const SkMatrix & ctm)59 SkRect Merge::onRevalidate(InvalidationController* ic, const SkMatrix& ctm) {
60     SkASSERT(this->hasInval());
61 
62     SkOpBuilder builder;
63 
64     fMerged.reset();
65     bool in_builder = false;
66 
67     for (const auto& rec : fRecs) {
68         rec.fGeo->revalidate(ic, ctm);
69 
70         // Merge is not currently supported by SkOpBuidler.
71         if (rec.fMode == Mode::kMerge) {
72             if (in_builder) {
73                 builder.resolve(&fMerged);
74                 in_builder = false;
75             }
76 
77             fMerged.addPath(rec.fGeo->asPath());
78             continue;
79         }
80 
81         if (!in_builder) {
82             builder.add(fMerged, kUnion_SkPathOp);
83             in_builder = true;
84         }
85 
86         builder.add(rec.fGeo->asPath(), mode_to_op(rec.fMode));
87     }
88 
89     if (in_builder) {
90         builder.resolve(&fMerged);
91     }
92 
93     fMerged.shrinkToFit();
94 
95     return fMerged.computeTightBounds();
96 }
97 
98 } // namespace sksg
99