• 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 "modules/sksg/include/SkSGDraw.h"
9 
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkPath.h"
12 #include "include/core/SkPathUtils.h"
13 #include "modules/sksg/include/SkSGGeometryNode.h"
14 #include "modules/sksg/include/SkSGInvalidationController.h"
15 #include "modules/sksg/include/SkSGPaint.h"
16 #include "src/base/SkTLazy.h"
17 
18 namespace sksg {
19 
Draw(sk_sp<GeometryNode> geometry,sk_sp<PaintNode> paint)20 Draw::Draw(sk_sp<GeometryNode> geometry, sk_sp<PaintNode> paint)
21     : fGeometry(std::move(geometry))
22     , fPaint(std::move(paint)) {
23     this->observeInval(fGeometry);
24     this->observeInval(fPaint);
25 }
26 
~Draw()27 Draw::~Draw() {
28     this->unobserveInval(fGeometry);
29     this->unobserveInval(fPaint);
30 }
31 
onRender(SkCanvas * canvas,const RenderContext * ctx) const32 void Draw::onRender(SkCanvas* canvas, const RenderContext* ctx) const {
33     auto paint = fPaint->makePaint();
34     if (ctx) {
35         ctx->modulatePaint(canvas->getTotalMatrix(), &paint);
36     }
37 
38     const auto skipDraw = paint.nothingToDraw() ||
39             (paint.getStyle() == SkPaint::kStroke_Style && paint.getStrokeWidth() <= 0);
40 
41     if (!skipDraw) {
42         fGeometry->draw(canvas, paint);
43     }
44 }
45 
onNodeAt(const SkPoint & p) const46 const RenderNode* Draw::onNodeAt(const SkPoint& p) const {
47     const auto paint = fPaint->makePaint();
48 
49     if (!paint.getAlpha()) {
50         return nullptr;
51     }
52 
53     if (paint.getStyle() == SkPaint::Style::kFill_Style && fGeometry->contains(p)) {
54         return this;
55     }
56 
57     SkPath stroke_path;
58     if (!skpathutils::FillPathWithPaint(fGeometry->asPath(), paint, &stroke_path)) {
59         return nullptr;
60     }
61 
62     return stroke_path.contains(p.x(), p.y()) ? this : nullptr;
63 }
64 
onRevalidate(InvalidationController * ic,const SkMatrix & ctm)65 SkRect Draw::onRevalidate(InvalidationController* ic, const SkMatrix& ctm) {
66     SkASSERT(this->hasInval());
67 
68     auto bounds = fGeometry->revalidate(ic, ctm);
69     fPaint->revalidate(ic, ctm);
70 
71     const auto paint = fPaint->makePaint();
72     SkASSERT(paint.canComputeFastBounds());
73 
74     return paint.computeFastBounds(bounds, &bounds);
75 }
76 
77 } // namespace sksg
78