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