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