Debian Codegraph @

src/gpu/graphite/RendererProvider.h

raw
1 /*
2 * Copyright 2022 Google LLC
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 skgpu_graphite_RendererProvider_DEFINED
9 #define skgpu_graphite_RendererProvider_DEFINED
10
11 #include "include/core/SkPathTypes.h"
12 #include "include/core/SkVertices.h"
13 #include "include/private/base/SkTArray.h"
14 #include "src/gpu/AtlasTypes.h"
15 #include "src/gpu/graphite/Renderer.h"
16
17 namespace skgpu::graphite {
18
19 class Caps;
20 class StaticBufferManager;
21
22 #ifdef SK_ENABLE_VELLO_SHADERS
23 class VelloRenderer;
24 #endif
25
26 /**
27 * PathRendererStrategy defines how paths are rendered by Graphite. This is determined in Caps based
28 * on available hardware features and heuristics. A single strategy is chosen for a Context so that
29 * shared resources can be configured efficiently (e.g. atlases). This also helps mitigate pipeline
30 * variations that might otherwise come about because a render target had different supported
31 * features. A texture is only considered renderable if it can be rendered into with the Cap's
32 * chosen PathRendererStrategy.
33 *
34 * Strategy choice does not impact how simple shapes (e.g. filled [r]rects, hairlines, and circular
35 * stroked rrects) or speciality shapes (e.g. edge-AA quads, text, vertices, blurs) are rendered.
36 *
37 * It is also possible for internal tools (viewer, dm, nanobench) to override the Caps' default
38 * selection process by using command line flags.
39 */
40 enum class PathRendererStrategy {
41 // Paths are rendered using tessellation and the classic stencil-and-cover algorithm w/ MSAA.
42 // Caps::defaultMSAASampleCount() determines the AA quality
43 // (ContextOptions::fInternalMSAACount <= 1 disables this strategy).
44 kTessellation,
45
46 // Like kTessellation with a texture atlas for higher quality AA. Small paths (WH <=
47 // Caps::minPathSizeForMSAA()) are rendered using CPU rasterization and packed into an atlas.
48 // For now, all clipping paths use MSAA like kTessellation; only small drawn paths are
49 // SW rasterized.
50 //
51 // ContextOptions::fMinPathSizeForMSAA == 0 disables this option.
52 // TODO(michaelludwig): Add SkSurfaceProperties flag to opt-out of SmallAtlas behavior to
53 // downgrade to just kTessellation behavior on a per-surface basis.
54 kTessellationAndSmallAtlas,
55
56 // All paths (and clips) are rendered using CPU rasterization and packed into coverage atlases.
57 // This strategy is chosen when MSAA is disabled in ContextOptions or unsupported.
58 kRasterAtlas,
59
60 // EXPERIMENTAL
61
62 // All paths are rasterized into coverage masks using a GPU compute approach. This method
63 // always uses analytic anti-aliasing. Clipping paths are rendered using kTessellation.
64 kComputeAnalyticAA,
65
66 // All paths are rasterized into coverage masks using a GPU compute approach. This method
67 // supports 16 and 8 sample SW-emulated MSAA. Clipping paths are rendered using kTessellation.
68 kComputeMSAA16,
69 kComputeMSAA8,
70 };
71
72 /**
73 * Graphite defines a limited set of renderers in order to increase the likelihood of batching
74 * across draw calls, and reducing the number of shader permutations required. These Renderers are
75 * stateless singletons and remain alive for the life of the Context and its Recorders.
76 *
77 * Because Renderers are immutable and the defined Renderers are created at context initialization,
78 * RendererProvider is trivially thread-safe.
79 */
80 class RendererProvider {
81 public:
82 ~RendererProvider();
83
84 static bool IsSupported(PathRendererStrategy, const Caps*);
85
86 // A given Caps may support more than one strategy, but only one will be used for all rendering.
87 PathRendererStrategy pathRendererStrategy() const { return fStrategy; }
88
89 // TODO: Add configuration options to disable "optimization" renderers in favor of the more
90 // general case, or renderers that won't be used by the application. When that's added, these
91 // functions could return null.
92
93 // Path rendering for fills and strokes, used by the kTessellation[AndSmallAtlas] strategies.
94 const Renderer* stencilTessellatedCurvesAndTris(SkPathFillType type) const {
95 return &fStencilTessellatedCurves[(int) type];
96 }
97 const Renderer* stencilTessellatedWedges(SkPathFillType type) const {
98 return &fStencilTessellatedWedges[(int) type];
99 }
100 const Renderer* convexTessellatedWedges() const { return &fConvexTessellatedWedges; }
101 const Renderer* tessellatedStrokes() const { return &fTessellatedStrokes; }
102
103 // Coverage mask rendering. Used by the atlas path rendering strategies and rendering mask
104 // filter results.
105 const Renderer* coverageMask() const { return &fCoverageMask; }
106
107 // ** Specialized renderers that are used regardless of general path rendering strategy.
108
109 // Atlased text rendering
110 const Renderer* bitmapText(bool useLCDText, skgpu::MaskFormat format) const {
111 // We use 565 here to represent all LCD rendering, regardless of texture format
112 if (useLCDText) {
113 return &fBitmapText[(int)skgpu::MaskFormat::kA565];
114 }
115 SkASSERT(format != skgpu::MaskFormat::kA565);
116 return &fBitmapText[(int)format];
117 }
118 const Renderer* sdfText(bool useLCDText) const { return &fSDFText[useLCDText]; }
119
120 // Mesh rendering
121 const Renderer* vertices(SkVertices::VertexMode mode, bool hasColors, bool hasTexCoords) const {
122 SkASSERT(mode != SkVertices::kTriangleFan_VertexMode); // Should be converted to kTriangles
123 bool triStrip = mode == SkVertices::kTriangleStrip_VertexMode;
124 return &fVertices[4*triStrip + 2*hasColors + hasTexCoords];
125 }
126
127 // Filled and stroked [r]rects
128 const Renderer* analyticRRect() const { return &fAnalyticRRect; }
129
130 // Per-edge AA quadrilaterals
131 const Renderer* perEdgeAAQuad() const { return &fPerEdgeAAQuad; }
132
133 // Non-AA bounds filling (can handle inverse "fills" but will touch every pixel within the clip)
134 const Renderer* nonAABounds() const { return &fNonAABoundsFill; }
135
136 // Circular arcs
137 const Renderer* circularArc() const { return &fCircularArc; }
138
139 const Renderer* analyticBlur() const { return &fAnalyticBlur; }
140
141 // TODO: May need to add support for inverse filled strokes (need to check SVG spec if this is a
142 // real thing).
143
144 // Iterate over all available Renderers to combine with specified paint combinations when
145 // pre-compiling pipelines.
146 SkSpan<const Renderer* const> renderers() const {
147 return {fRenderers.data(), (size_t)fRenderers.size()};
148 }
149
150 const RenderStep* lookup(RenderStep::RenderStepID renderStepID) const {
151 return fRenderSteps[(int) renderStepID].get();
152 }
153
154 #ifdef SK_ENABLE_VELLO_SHADERS
155 // Compute shader-based path renderer and compositor. Used with the kCompute related strategies
156 // to coordinate the ComputeSteps that feed into the coverageMask() renderer.
157 const VelloRenderer* velloRenderer() const { return fVelloRenderer.get(); }
158 #endif
159
160 private:
161 static constexpr int kPathTypeCount = 4;
162 static constexpr int kVerticesCount = 8; // 2 modes * 2 color configs * 2 tex coord configs
163
164 friend class Context; // for ctor
165
166 RendererProvider(const Caps*, StaticBufferManager* bufferManager);
167
168 // Cannot be moved or copied
169 RendererProvider(const RendererProvider&) = delete;
170 RendererProvider(RendererProvider&&) = delete;
171
172 RenderStep* assumeOwnership(std::unique_ptr<RenderStep> renderStep) {
173 int index = (int) renderStep->renderStepID();
174 SkASSERT(!fRenderSteps[index]);
175 fRenderSteps[index] = std::move(renderStep);
176 return fRenderSteps[index].get();
177 }
178 template<typename... Args>
179 void initRenderer(Renderer* member, Args... args) {
180 *member = Renderer(args...);
181 fRenderers.push_back(member);
182 }
183
184 PathRendererStrategy fStrategy;
185
186 // Renderers are composed of 1+ steps, and some steps can be shared by multiple Renderers.
187 // Renderers don't keep their RenderSteps alive so RendererProvider holds them here.
188 std::unique_ptr<RenderStep> fRenderSteps[RenderStep::kNumRenderSteps];
189
190 // Use initRenderer() to set each member and register with `fRenderers`.
191 Renderer fStencilTessellatedCurves[kPathTypeCount];
192 Renderer fStencilTessellatedWedges[kPathTypeCount];
193 Renderer fConvexTessellatedWedges;
194 Renderer fTessellatedStrokes;
195
196 Renderer fCoverageMask;
197
198 Renderer fBitmapText[3]; // int variant
199 Renderer fSDFText[2]; // bool isLCD
200
201 Renderer fAnalyticRRect;
202 Renderer fPerEdgeAAQuad;
203 Renderer fNonAABoundsFill;
204 Renderer fCircularArc;
205
206 Renderer fAnalyticBlur;
207
208 Renderer fVertices[kVerticesCount];
209
210 // Aggregate of all enabled Renderers for convenient iteration when pre-compiling
211 skia_private::TArray<const Renderer*> fRenderers;
212
213 #ifdef SK_ENABLE_VELLO_SHADERS
214 std::unique_ptr<VelloRenderer> fVelloRenderer;
215 #endif
216 };
217
218 } // namespace skgpu::graphite
219
220 #endif // skgpu_graphite_RendererProvider_DEFINED