Debian Codegraph @

src/gpu/ganesh/PathRenderer.h

raw
1 /*
2 * Copyright 2011 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 PathRenderer_DEFINED
9 #define PathRenderer_DEFINED
10
11 #include "include/core/SkRefCnt.h"
12 #include "include/private/base/SkAssert.h"
13 #include "include/private/base/SkDebug.h"
14 #include "src/gpu/ganesh/GrPaint.h"
15
16 #include <string.h>
17
18 class GrCaps;
19 class GrClip;
20 class GrHardClip;
21 class GrRecordingContext;
22 class GrRenderTargetProxy;
23 class GrStyledShape;
24 class SkMatrix;
25 class SkPath;
26 class SkSurfaceProps;
27 enum class GrAA : bool;
28 enum class GrAAType : unsigned int;
29 struct GrUserStencilSettings;
30 struct SkIRect;
31 struct SkISize;
32 struct SkRect;
33
34 namespace skgpu::ganesh {
35
36 class SurfaceDrawContext;
37
38 /**
39 * Base class for drawing paths into a OpsTask.
40 */
41 class PathRenderer : public SkRefCnt {
42 public:
43 // Used to guard TriangulatingPathRenderer and DefaultPathRenderer from excessively complex
44 // paths, which may cause OOM or other stalls
45 static constexpr int kMaxGPUPathRendererVerbs = 1 << 14;
46
47 PathRenderer() = default;
48
49 virtual const char* name() const = 0;
50
51 /**
52 * A caller may wish to use a path renderer to draw a path into the stencil buffer. However,
53 * the path renderer itself may require use of the stencil buffer. Also a path renderer may
54 * use a GrProcessor coverage stage that sets coverage to zero to eliminate pixels that are
55 * covered by bounding geometry but outside the path. These exterior pixels would still be
56 * rendered into the stencil.
57 *
58 * A PathRenderer can provide three levels of support for stenciling paths:
59 * 1) kNoRestriction: This is the most general. The caller passes a GrPaint and calls drawPath().
60 * The path is rendered exactly as the draw state indicates including support
61 * for simultaneous color and stenciling with arbitrary stenciling rules.
62 * Pixels partially covered by AA paths are affected by the stencil settings.
63 * 2) kStencilOnly: The path renderer cannot apply arbitrary stencil rules nor shade and stencil
64 * simultaneously. The path renderer does support the stencilPath() function
65 * which performs no color writes and writes a non-zero stencil value to pixels
66 * covered by the path.
67 * 3) kNoSupport: This path renderer cannot be used to stencil the path.
68 */
69 enum StencilSupport {
70 kNoSupport_StencilSupport,
71 kStencilOnly_StencilSupport,
72 kNoRestriction_StencilSupport,
73 };
74
75 /**
76 * This function is to get the stencil support for a particular path. The path's fill must
77 * not be an inverse type. The path will always be filled and not stroked.
78 *
79 * @param shape the shape that will be drawn. Must be simple fill styled and non-inverse
80 * filled.
81 */
82 StencilSupport getStencilSupport(const GrStyledShape& shape) const;
83
84 enum class CanDrawPath {
85 kNo,
86 kAsBackup, // i.e. This renderer is better than SW fallback if no others can draw the path.
87 kYes
88 };
89
90 struct CanDrawPathArgs {
91 SkDEBUGCODE(CanDrawPathArgs() { memset(this, 0, sizeof(*this)); }) // For validation.
92
93 const GrCaps* fCaps;
94 const GrRenderTargetProxy* fProxy;
95 const SkIRect* fClipConservativeBounds;
96 const SkMatrix* fViewMatrix;
97 const GrStyledShape* fShape;
98 const GrPaint* fPaint;
99 const SkSurfaceProps* fSurfaceProps;
100 GrAAType fAAType;
101
102 // This is only used by TessellationPathRenderer
103 bool fHasUserStencilSettings;
104
105 #ifdef SK_DEBUG
106 void validate() const {
107 SkASSERT(fCaps);
108 SkASSERT(fProxy);
109 SkASSERT(fClipConservativeBounds);
110 SkASSERT(fViewMatrix);
111 SkASSERT(fShape);
112 SkASSERT(fSurfaceProps);
113 }
114 #endif
115 };
116
117 /**
118 * Returns how well this path renderer is able to render the given path. Returning kNo or
119 * kAsBackup allows the caller to keep searching for a better path renderer. This function is
120 * called when searching for the best path renderer to draw a path.
121 */
122 CanDrawPath canDrawPath(const CanDrawPathArgs& args) const {
123 SkDEBUGCODE(args.validate();)
124 return this->onCanDrawPath(args);
125 }
126
127 struct DrawPathArgs {
128 GrRecordingContext* fContext;
129 GrPaint&& fPaint;
130 const GrUserStencilSettings* fUserStencilSettings;
131 SurfaceDrawContext* fSurfaceDrawContext;
132 const GrClip* fClip;
133 const SkIRect* fClipConservativeBounds;
134 const SkMatrix* fViewMatrix;
135 const GrStyledShape* fShape;
136 GrAAType fAAType;
137 bool fGammaCorrect;
138 #ifdef SK_DEBUG
139 void validate() const {
140 SkASSERT(fContext);
141 SkASSERT(fUserStencilSettings);
142 SkASSERT(fSurfaceDrawContext);
143 SkASSERT(fClipConservativeBounds);
144 SkASSERT(fViewMatrix);
145 SkASSERT(fShape);
146 }
147 #endif
148 };
149
150 /**
151 * Draws the path into the draw target. If getStencilSupport() would return kNoRestriction then
152 * the subclass must respect the stencil settings.
153 */
154 bool drawPath(const DrawPathArgs& args);
155 /**
156 * Args to stencilPath(). fAAType cannot be kCoverage.
157 */
158 struct StencilPathArgs {
159 SkDEBUGCODE(StencilPathArgs() { memset(this, 0, sizeof(*this)); }) // For validation.
160
161 GrRecordingContext* fContext;
162 SurfaceDrawContext* fSurfaceDrawContext;
163 const GrHardClip* fClip;
164 const SkIRect* fClipConservativeBounds;
165 const SkMatrix* fViewMatrix;
166 const GrStyledShape* fShape;
167 GrAA fDoStencilMSAA;
168
169 SkDEBUGCODE(void validate() const;)
170 };
171
172 /**
173 * Draws the path to the stencil buffer. Assume the writable stencil bits are already
174 * initialized to zero. The pixels inside the path will have non-zero stencil values afterwards.
175 */
176 void stencilPath(const StencilPathArgs& args) {
177 SkDEBUGCODE(args.validate();)
178 SkASSERT(kNoSupport_StencilSupport != this->getStencilSupport(*args.fShape));
179 this->onStencilPath(args);
180 }
181
182 protected:
183 // Helper for getting the device bounds of a path. Inverse filled paths will have bounds set
184 // by devSize. Non-inverse path bounds will not necessarily be clipped to devSize.
185 static void GetPathDevBounds(const SkPath& path,
186 SkISize devSize,
187 const SkMatrix& matrix,
188 SkRect* bounds);
189
190 private:
191 /**
192 * Subclass overrides if it has any limitations of stenciling support.
193 */
194 virtual StencilSupport onGetStencilSupport(const GrStyledShape&) const {
195 return kNoRestriction_StencilSupport;
196 }
197
198 /**
199 * Subclass implementation of drawPath()
200 */
201 virtual bool onDrawPath(const DrawPathArgs& args) = 0;
202
203 /**
204 * Subclass implementation of canDrawPath()
205 */
206 virtual CanDrawPath onCanDrawPath(const CanDrawPathArgs& args) const = 0;
207
208 /**
209 * Subclass implementation of stencilPath(). Subclass must override iff it ever returns
210 * kStencilOnly in onGetStencilSupport().
211 */
212 virtual void onStencilPath(const StencilPathArgs&);
213
214 using INHERITED = SkRefCnt;
215 };
216
217 } // namespace skgpu::ganesh
218
219 #endif // PathRenderer_DEFINED