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