Skia
2D Graphics Library
SkSGRenderNode.h
Go to the documentation of this file.
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 SkSGRenderNode_DEFINED
9 #define SkSGRenderNode_DEFINED
10 
12 
13 #include "include/core/SkBlender.h"
15 #include "include/core/SkMatrix.h"
16 #include "include/core/SkShader.h"
17 
18 class SkCanvas;
19 class SkImageFilter;
20 class SkPaint;
21 
22 namespace sksg {
23 
27 class RenderNode : public Node {
28 protected:
29  struct RenderContext;
30 
31 public:
32  // Render the node and its descendants to the canvas.
33  void render(SkCanvas*, const RenderContext* = nullptr) const;
34 
35  // Perform a front-to-back hit-test, and return the RenderNode located at |point|.
36  // Normally, hit-testing stops at leaf Draw nodes.
37  const RenderNode* nodeAt(const SkPoint& point) const;
38 
39  // Controls the visibility of the render node. Invisible nodes are not rendered,
40  // but they still participate in revalidation.
41  bool isVisible() const;
42  void setVisible(bool);
43 
44 protected:
45  explicit RenderNode(uint32_t inval_traits = 0);
46 
47  virtual void onRender(SkCanvas*, const RenderContext*) const = 0;
48  virtual const RenderNode* onNodeAt(const SkPoint& p) const = 0;
49 
50  // Paint property overrides.
51  // These are deferred until we can determine whether they can be applied to the individual
52  // draw paints, or whether they require content isolation (applied to a layer).
53  struct RenderContext {
60  float fOpacity = 1;
61 
62  // Returns true if the paint overrides require a layer when applied to non-atomic draws.
63  bool requiresIsolation() const;
64 
65  void modulatePaint(const SkMatrix& ctm, SkPaint*, bool is_layer_paint = false) const;
66  };
67 
68  class ScopedRenderContext final {
69  public:
72 
73  ScopedRenderContext(ScopedRenderContext&& that) { *this = std::move(that); }
74 
76  fCanvas = that.fCanvas;
77  fCtx = std::move(that.fCtx);
78  fMaskShader = std::move(that.fMaskShader);
79  fRestoreCount = that.fRestoreCount;
80 
81  // scope ownership is being transferred
82  that.fRestoreCount = -1;
83 
84  return *this;
85  }
86 
87  operator const RenderContext* () const { return &fCtx; }
88  const RenderContext* operator->() const { return &fCtx; }
89 
90  // Add (cumulative) paint overrides to a render node sub-DAG.
96 
97  // Force content isolation for a node sub-DAG by applying the RenderContext
98  // overrides via a layer.
100  bool do_isolate);
101 
102  // Similarly, force content isolation by applying the RenderContext overrides and
103  // an image filter via a single layer.
106 
107  private:
108  // stack-only
109  void* operator new(size_t) = delete;
110  void* operator new(size_t, void*) = delete;
111 
112  // Scopes cannot be copied.
113  ScopedRenderContext(const ScopedRenderContext&) = delete;
115 
116  SkCanvas* fCanvas;
117  RenderContext fCtx;
118  sk_sp<SkShader> fMaskShader; // to be applied at isolation layer restore time
119  int fRestoreCount;
120  };
121 
122 private:
123  friend class ImageFilterEffect;
124 
125  using INHERITED = Node;
126 };
127 
133 class CustomRenderNode : public RenderNode {
134 protected:
135  explicit CustomRenderNode(std::vector<sk_sp<RenderNode>>&& children);
136  ~CustomRenderNode() override;
137 
138  const std::vector<sk_sp<RenderNode>>& children() const { return fChildren; }
139 
140  bool hasChildrenInval() const;
141 
142 private:
143  std::vector<sk_sp<RenderNode>> fChildren;
144 
145  using INHERITED = RenderNode;
146 };
147 
148 } // namespace sksg
149 
150 #endif // SkSGRenderNode_DEFINED
SkCanvas provides an interface for drawing, and how the drawing is clipped and transformed.
Definition: SkCanvas.h:99
Base class for image filters.
Definition: SkImageFilter.h:35
SkMatrix holds a 3x3 matrix for transforming coordinates.
Definition: SkMatrix.h:53
static const SkMatrix & I()
Returns reference to const identity SkMatrix.
SkPaint controls options applied when drawing.
Definition: SkPaint.h:44
Definition: SkRefCnt.h:119
Clients outside SkSG looking to implement custom render nodes, should derive from this class instead ...
Definition: SkSGRenderNode.h:133
CustomRenderNode(std::vector< sk_sp< RenderNode >> &&children)
~CustomRenderNode() override
bool hasChildrenInval() const
const std::vector< sk_sp< RenderNode > > & children() const
Definition: SkSGRenderNode.h:138
Attaches an ImageFilter (chain) to the render DAG.
Definition: SkSGRenderEffect.h:130
Base class for all scene graph nodes.
Definition: SkSGNode.h:32
const SkRect & bounds() const
Definition: SkSGNode.h:54
friend class RenderNode
Definition: SkSGNode.h:93
Node(uint32_t invalTraits)
Definition: SkSGRenderNode.h:68
ScopedRenderContext && setIsolation(const SkRect &bounds, const SkMatrix &ctm, bool do_isolate)
ScopedRenderContext && modulateColorFilter(sk_sp< SkColorFilter >)
ScopedRenderContext & operator=(ScopedRenderContext &&that)
Definition: SkSGRenderNode.h:75
ScopedRenderContext && modulateBlender(sk_sp< SkBlender >)
ScopedRenderContext(ScopedRenderContext &&that)
Definition: SkSGRenderNode.h:73
const RenderContext * operator->() const
Definition: SkSGRenderNode.h:88
ScopedRenderContext(SkCanvas *, const RenderContext *)
ScopedRenderContext && modulateOpacity(float opacity)
ScopedRenderContext && modulateShader(sk_sp< SkShader >, const SkMatrix &shader_ctm)
ScopedRenderContext && modulateMaskShader(sk_sp< SkShader >, const SkMatrix &ms_ctm)
ScopedRenderContext && setFilterIsolation(const SkRect &bounds, const SkMatrix &ctm, sk_sp< SkImageFilter >)
Base class for nodes which can render to a canvas.
Definition: SkSGRenderNode.h:27
void render(SkCanvas *, const RenderContext *=nullptr) const
bool isVisible() const
const RenderNode * nodeAt(const SkPoint &point) const
virtual const RenderNode * onNodeAt(const SkPoint &p) const =0
virtual void onRender(SkCanvas *, const RenderContext *) const =0
void setVisible(bool)
RenderNode(uint32_t inval_traits=0)
Definition: Skottie.h:30
SkRect holds four float coordinates describing the upper and lower bounds of a rectangle.
Definition: SkRect.h:582
Definition: SkSGRenderNode.h:53
sk_sp< SkBlender > fBlender
Definition: SkSGRenderNode.h:57
sk_sp< SkColorFilter > fColorFilter
Definition: SkSGRenderNode.h:54
void modulatePaint(const SkMatrix &ctm, SkPaint *, bool is_layer_paint=false) const
SkMatrix fShaderCTM
Definition: SkSGRenderNode.h:58
sk_sp< SkShader > fShader
Definition: SkSGRenderNode.h:55
sk_sp< SkShader > fMaskShader
Definition: SkSGRenderNode.h:56
float fOpacity
Definition: SkSGRenderNode.h:60
SkMatrix fMaskCTM
Definition: SkSGRenderNode.h:59