• 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 #ifndef SkSGPaint_DEFINED
9 #define SkSGPaint_DEFINED
10 
11 #include "modules/sksg/include/SkSGNode.h"
12 
13 #include "include/core/SkBlendMode.h"
14 #include "include/core/SkColor.h"
15 #include "include/core/SkPaint.h"
16 
17 namespace sksg {
18 
19 class Shader;
20 
21 /**
22  * Base class for nodes which provide a 'paint' (as opposed to geometry) for
23  * drawing (e.g. colors, gradients, patterns).
24  *
25  * Roughly equivalent to Skia's SkPaint.
26  */
27 class PaintNode : public Node {
28 public:
29     SkPaint makePaint() const;
30 
31     SG_ATTRIBUTE(AntiAlias  , bool          , fAntiAlias  )
32     SG_ATTRIBUTE(Opacity    , SkScalar      , fOpacity    )
33     SG_ATTRIBUTE(BlendMode  , SkBlendMode   , fBlendMode  )
34     SG_ATTRIBUTE(StrokeWidth, SkScalar      , fStrokeWidth)
35     SG_ATTRIBUTE(StrokeMiter, SkScalar      , fStrokeMiter)
36     SG_ATTRIBUTE(Style      , SkPaint::Style, fStyle      )
37     SG_ATTRIBUTE(StrokeJoin , SkPaint::Join , fStrokeJoin )
38     SG_ATTRIBUTE(StrokeCap  , SkPaint::Cap  , fStrokeCap  )
39 
40 protected:
41     PaintNode();
42 
43     virtual void onApplyToPaint(SkPaint*) const = 0;
44 
45 private:
46     SkScalar       fOpacity     = 1,
47                    fStrokeWidth = 1,
48                    fStrokeMiter = 4;
49     bool           fAntiAlias   = false;
50     SkBlendMode    fBlendMode   = SkBlendMode::kSrcOver;
51     SkPaint::Style fStyle       = SkPaint::kFill_Style;
52     SkPaint::Join  fStrokeJoin  = SkPaint::kMiter_Join;
53     SkPaint::Cap   fStrokeCap   = SkPaint::kButt_Cap;
54 
55     using INHERITED = Node;
56 };
57 
58 /**
59  * Concrete Paint node, wrapping an SkColor.
60  */
61 class Color : public PaintNode {
62 public:
63     static sk_sp<Color> Make(SkColor c);
64 
65     SG_ATTRIBUTE(Color, SkColor, fColor)
66 
67 protected:
68     SkRect onRevalidate(InvalidationController*, const SkMatrix&) override;
69 
70     void onApplyToPaint(SkPaint*) const override;
71 
72 private:
73     explicit Color(SkColor);
74 
75     SkColor fColor;
76 };
77 
78 /**
79  * Shader-based paint.
80  */
81 class ShaderPaint final : public PaintNode {
82 public:
83     ~ShaderPaint() override;
84 
85     static sk_sp<ShaderPaint> Make(sk_sp<Shader>);
86 
87 protected:
88     SkRect onRevalidate(InvalidationController*, const SkMatrix&) override;
89 
90     void onApplyToPaint(SkPaint*) const override;
91 
92 private:
93     explicit ShaderPaint(sk_sp<Shader>);
94 
95     const sk_sp<Shader> fShader;
96 
97     using INHERITED = PaintNode;
98 };
99 
100 } // namespace sksg
101 
102 #endif // SkSGPaint_DEFINED
103