Flutter Engine
The Flutter Engine
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
17
18#include <cstddef>
19#include <cstdint>
20#include <utility>
21#include <vector>
22
23class SkCanvas;
24class SkImageFilter;
25class SkPaint;
26struct SkPoint;
27struct SkRect;
28
29namespace sksg {
30
31/**
32 * Base class for nodes which can render to a canvas.
33 */
34class RenderNode : public Node {
35protected:
36 struct RenderContext;
37
38public:
39 // Render the node and its descendants to the canvas.
40 void render(SkCanvas*, const RenderContext* = nullptr) const;
41
42 // Perform a front-to-back hit-test, and return the RenderNode located at |point|.
43 // Normally, hit-testing stops at leaf Draw nodes.
44 const RenderNode* nodeAt(const SkPoint& point) const;
45
46 // Controls the visibility of the render node. Invisible nodes are not rendered,
47 // but they still participate in revalidation.
48 bool isVisible() const;
49 void setVisible(bool);
50
51protected:
52 explicit RenderNode(uint32_t inval_traits = 0);
53
54 virtual void onRender(SkCanvas*, const RenderContext*) const = 0;
55 virtual const RenderNode* onNodeAt(const SkPoint& p) const = 0;
56
57 // Paint property overrides.
58 // These are deferred until we can determine whether they can be applied to the individual
59 // draw paints, or whether they require content isolation (applied to a layer).
67 float fOpacity = 1;
68
69 // Returns true if the paint overrides require a layer when applied to non-atomic draws.
70 bool requiresIsolation() const;
71
72 void modulatePaint(const SkMatrix& ctm, SkPaint*, bool is_layer_paint = false) const;
73 };
74
75 class ScopedRenderContext final {
76 public:
79
80 ScopedRenderContext(ScopedRenderContext&& that) { *this = std::move(that); }
81
83 fCanvas = that.fCanvas;
84 fCtx = std::move(that.fCtx);
85 fMaskShader = std::move(that.fMaskShader);
86 fRestoreCount = that.fRestoreCount;
87
88 // scope ownership is being transferred
89 that.fRestoreCount = -1;
90
91 return *this;
92 }
93
94 operator const RenderContext* () const { return &fCtx; }
95 const RenderContext* operator->() const { return &fCtx; }
96
97 // Add (cumulative) paint overrides to a render node sub-DAG.
98 ScopedRenderContext&& modulateOpacity(float opacity);
103
104 // Force content isolation for a node sub-DAG by applying the RenderContext
105 // overrides via a layer.
107 bool do_isolate);
108
109 // Similarly, force content isolation by applying the RenderContext overrides and
110 // an image filter via a single layer.
113
114 private:
115 // stack-only
116 void* operator new(size_t) = delete;
117 void* operator new(size_t, void*) = delete;
118
119 // Scopes cannot be copied.
122
123 SkCanvas* fCanvas;
124 RenderContext fCtx;
125 sk_sp<SkShader> fMaskShader; // to be applied at isolation layer restore time
126 int fRestoreCount;
127 };
128
129private:
130 friend class ImageFilterEffect;
131
132 using INHERITED = Node;
133};
134
135/**
136 * Clients outside SkSG looking to implement custom render nodes,
137 * should derive from this class instead of RenderNode. It handles
138 * various book-keeping, and provides a controlled extension point.
139 */
141protected:
142 explicit CustomRenderNode(std::vector<sk_sp<RenderNode>>&& children);
143 ~CustomRenderNode() override;
144
145 const std::vector<sk_sp<RenderNode>>& children() const { return fChildren; }
146
147 bool hasChildrenInval() const;
148
149private:
150 std::vector<sk_sp<RenderNode>> fChildren;
151
152 using INHERITED = RenderNode;
153};
154
155} // namespace sksg
156
157#endif // SkSGRenderNode_DEFINED
static const SkMatrix & I()
Definition: SkMatrix.cpp:1544
const std::vector< sk_sp< RenderNode > > & children() const
CustomRenderNode(std::vector< sk_sp< RenderNode > > &&children)
friend class RenderNode
Definition: SkSGNode.h:94
const SkRect & bounds() const
Definition: SkSGNode.h:55
Node(uint32_t invalTraits)
Definition: SkSGNode.cpp:45
ScopedRenderContext & operator=(ScopedRenderContext &&that)
const RenderContext * operator->() const
ScopedRenderContext && setFilterIsolation(const SkRect &bounds, const SkMatrix &ctm, sk_sp< SkImageFilter >)
ScopedRenderContext && modulateOpacity(float opacity)
ScopedRenderContext(ScopedRenderContext &&that)
ScopedRenderContext && modulateMaskShader(sk_sp< SkShader >, const SkMatrix &ms_ctm)
ScopedRenderContext && setIsolation(const SkRect &bounds, const SkMatrix &ctm, bool do_isolate)
ScopedRenderContext(SkCanvas *, const RenderContext *)
ScopedRenderContext && modulateColorFilter(sk_sp< SkColorFilter >)
ScopedRenderContext && modulateBlender(sk_sp< SkBlender >)
ScopedRenderContext && modulateShader(sk_sp< SkShader >, const SkMatrix &shader_ctm)
void render(SkCanvas *, const RenderContext *=nullptr) const
virtual const RenderNode * onNodeAt(const SkPoint &p) const =0
bool isVisible() const
const RenderNode * nodeAt(const SkPoint &point) const
virtual void onRender(SkCanvas *, const RenderContext *) const =0
void setVisible(bool)
Definition: Skottie.h:32
sk_sp< SkColorFilter > fColorFilter
void modulatePaint(const SkMatrix &ctm, SkPaint *, bool is_layer_paint=false) const