1/*
2 * Copyright 2015 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 GrContextOptions_DEFINED
9#define GrContextOptions_DEFINED
10
11#include "include/core/SkData.h"
12#include "include/core/SkTypes.h"
13#include "include/gpu/GrDriverBugWorkarounds.h"
14#include "include/gpu/GrTypes.h"
15#include "include/private/GrTypesPriv.h"
16
17#include <vector>
18
19class SkExecutor;
20
21#if SK_SUPPORT_GPU
22struct SK_API GrContextOptions {
23 enum class Enable {
24 /** Forces an option to be disabled. */
25 kNo,
26 /** Forces an option to be enabled. */
27 kYes,
28 /**
29 * Uses Skia's default behavior, which may use runtime properties (e.g. driver version).
30 */
31 kDefault
32 };
33
34 enum class ShaderCacheStrategy {
35 kSkSL,
36 kBackendSource,
37 kBackendBinary,
38 };
39
40 /**
41 * Abstract class which stores Skia data in a cache that persists between sessions. Currently,
42 * Skia stores compiled shader binaries (only when glProgramBinary / glGetProgramBinary are
43 * supported) when provided a persistent cache, but this may extend to other data in the future.
44 */
45 class SK_API PersistentCache {
46 public:
47 virtual ~PersistentCache() {}
48
49 /**
50 * Returns the data for the key if it exists in the cache, otherwise returns null.
51 */
52 virtual sk_sp<SkData> load(const SkData& key) = 0;
53
54 virtual void store(const SkData& key, const SkData& data) = 0;
55 };
56
57 /**
58 * Abstract class to report errors when compiling shaders. If fShaderErrorHandler is present,
59 * it will be called to report any compilation failures. Otherwise, failures will be reported
60 * via SkDebugf and asserts.
61 */
62 class SK_API ShaderErrorHandler {
63 public:
64 virtual ~ShaderErrorHandler() {}
65 virtual void compileError(const char* shader, const char* errors) = 0;
66 };
67
68 GrContextOptions() {}
69
70 // Suppress prints for the GrContext.
71 bool fSuppressPrints = false;
72
73 /**
74 * Controls whether we check for GL errors after functions that allocate resources (e.g.
75 * glTexImage2D), for shader compilation success, and program link success. Ignored on
76 * backends other than GL.
77 */
78 Enable fSkipGLErrorChecks = Enable::kDefault;
79
80 /** Overrides: These options override feature detection using backend API queries. These
81 overrides can only reduce the feature set or limits, never increase them beyond the
82 detected values. */
83
84 int fMaxTextureSizeOverride = SK_MaxS32;
85
86 /** the threshold in bytes above which we will use a buffer mapping API to map vertex and index
87 buffers to CPU memory in order to update them. A value of -1 means the GrContext should
88 deduce the optimal value for this platform. */
89 int fBufferMapThreshold = -1;
90
91 /**
92 * Executor to handle threaded work within Ganesh. If this is nullptr, then all work will be
93 * done serially on the main thread. To have worker threads assist with various tasks, set this
94 * to a valid SkExecutor instance. Currently, used for software path rendering, but may be used
95 * for other tasks.
96 */
97 SkExecutor* fExecutor = nullptr;
98
99 /** Construct mipmaps manually, via repeated downsampling draw-calls. This is used when
100 the driver's implementation (glGenerateMipmap) contains bugs. This requires mipmap
101 level and LOD control (ie desktop or ES3). */
102 bool fDoManualMipmapping = false;
103
104 /**
105 * Disables the use of coverage counting shortcuts to render paths. Coverage counting can cause
106 * artifacts along shared edges if care isn't taken to ensure both contours wind in the same
107 * direction.
108 */
109 // FIXME: Once this is removed from Chrome and Android, rename to fEnable"".
110 bool fDisableCoverageCountingPaths = true;
111
112 /**
113 * Disables distance field rendering for paths. Distance field computation can be expensive,
114 * and yields no benefit if a path is not rendered multiple times with different transforms.
115 */
116 bool fDisableDistanceFieldPaths = false;
117
118 /**
119 * If true this allows path mask textures to be cached. This is only really useful if paths
120 * are commonly rendered at the same scale and fractional translation.
121 */
122 bool fAllowPathMaskCaching = true;
123
124 /**
125 * If true, the GPU will not be used to perform YUV -> RGB conversion when generating
126 * textures from codec-backed images.
127 */
128 bool fDisableGpuYUVConversion = false;
129
130 /**
131 * The maximum size of cache textures used for Skia's Glyph cache.
132 */
133 size_t fGlyphCacheTextureMaximumBytes = 2048 * 1024 * 4;
134
135 /**
136 * Below this threshold size in device space distance field fonts won't be used. Distance field
137 * fonts don't support hinting which is more important at smaller sizes. A negative value means
138 * use the default threshold.
139 */
140 float fMinDistanceFieldFontSize = -1.f;
141
142 /**
143 * Above this threshold size in device space glyphs are drawn as individual paths. A negative
144 * value means use the default threshold.
145 */
146 float fGlyphsAsPathsFontSize = -1.f;
147
148 /**
149 * Can the glyph atlas use multiple textures. If allowed, the each texture's size is bound by
150 * fGlypheCacheTextureMaximumBytes.
151 */
152 Enable fAllowMultipleGlyphCacheTextures = Enable::kDefault;
153
154 /**
155 * Bugs on certain drivers cause stencil buffers to leak. This flag causes Skia to avoid
156 * allocating stencil buffers and use alternate rasterization paths, avoiding the leak.
157 */
158 bool fAvoidStencilBuffers = false;
159
160 /**
161 * If true, texture fetches from mip-mapped textures will be biased to read larger MIP levels.
162 * This has the effect of sharpening those textures, at the cost of some aliasing, and possible
163 * performance impact.
164 */
165 bool fSharpenMipmappedTextures = false;
166
167 /**
168 * Enables driver workaround to use draws instead of HW clears, e.g. glClear on the GL backend.
169 */
170 Enable fUseDrawInsteadOfClear = Enable::kDefault;
171
172 /**
173 * Allow Ganesh to more aggressively reorder operations.
174 * Eventually this will just be what is done and will not be optional.
175 */
176 Enable fReduceOpsTaskSplitting = Enable::kDefault;
177
178 /**
179 * Some ES3 contexts report the ES2 external image extension, but not the ES3 version.
180 * If support for external images is critical, enabling this option will cause Ganesh to limit
181 * shaders to the ES2 shading language in that situation.
182 */
183 bool fPreferExternalImagesOverES3 = false;
184
185 /**
186 * Disables correctness workarounds that are enabled for particular GPUs, OSes, or drivers.
187 * This does not affect code path choices that are made for perfomance reasons nor does it
188 * override other GrContextOption settings.
189 */
190 bool fDisableDriverCorrectnessWorkarounds = false;
191
192 /**
193 * Maximum number of GPU programs or pipelines to keep active in the runtime cache.
194 */
195 int fRuntimeProgramCacheSize = 256;
196
197 /**
198 * Cache in which to store compiled shader binaries between runs.
199 */
200 PersistentCache* fPersistentCache = nullptr;
201
202 /**
203 * This affects the usage of the PersistentCache. We can cache SkSL, backend source (GLSL), or
204 * backend binaries (GL program binaries). By default we cache binaries, but if the driver's
205 * binary loading/storing is believed to have bugs, this can be limited to caching GLSL.
206 * Caching GLSL strings still saves CPU work when a GL program is created.
207 */
208 ShaderCacheStrategy fShaderCacheStrategy = ShaderCacheStrategy::kBackendBinary;
209
210 /**
211 * If present, use this object to report shader compilation failures. If not, report failures
212 * via SkDebugf and assert.
213 */
214 ShaderErrorHandler* fShaderErrorHandler = nullptr;
215
216 /**
217 * Specifies the number of samples Ganesh should use when performing internal draws with MSAA or
218 * mixed samples (hardware capabilities permitting).
219 *
220 * If 0, Ganesh will disable internal code paths that use multisampling.
221 */
222 int fInternalMultisampleCount = 4;
223
224#if GR_TEST_UTILS
225 /**
226 * Private options that are only meant for testing within Skia's tools.
227 */
228
229 /**
230 * If non-zero, overrides the maximum size of a tile for sw-backed images and bitmaps rendered
231 * by SkGpuDevice.
232 */
233 int fMaxTileSizeOverride = 0;
234
235 /**
236 * Prevents use of dual source blending, to test that all xfer modes work correctly without it.
237 */
238 bool fSuppressDualSourceBlending = false;
239
240 /**
241 * If true, the caps will never support geometry shaders.
242 */
243 bool fSuppressGeometryShaders = false;
244
245 /**
246 * Render everything in wireframe
247 */
248 bool fWireframeMode = false;
249
250 /**
251 * Enforces clearing of all textures when they're created.
252 */
253 bool fClearAllTextures = false;
254
255 /**
256 * Include or exclude specific GPU path renderers.
257 */
258 GpuPathRenderers fGpuPathRenderers = GpuPathRenderers::kDefault;
259#endif
260
261#if SK_SUPPORT_ATLAS_TEXT
262 /**
263 * Controls whether distance field glyph vertices always have 3 components even when the view
264 * matrix does not have perspective.
265 */
266 Enable fDistanceFieldGlyphVerticesAlwaysHaveW = Enable::kDefault;
267#endif
268
269 GrDriverBugWorkarounds fDriverBugWorkarounds;
270};
271#else
272struct GrContextOptions {
273 struct PersistentCache {};
274};
275#endif
276
277#endif
278