Debian Codegraph @

tools/flags/CommonFlagsConfig.cpp

raw
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 #include "tools/flags/CommonFlagsConfig.h"
9
10 #include "include/core/SkImageInfo.h"
11 #include "include/core/SkSurfaceProps.h"
12 #include "src/core/SkColorSpacePriv.h"
13 #include "src/core/SkStringUtils.h"
14 #include "src/core/SkSurfacePriv.h"
15 #include "src/core/SkTHash.h"
16
17 #include <stdlib.h>
18 #include <string_view>
19 #include <unordered_map>
20
21 using namespace skia_private;
22
23 #if defined(SK_GANESH)
24 #if defined(SK_BUILD_FOR_ANDROID) || defined(SK_BUILD_FOR_IOS)
25 #define DEFAULT_GPU_CONFIG "gles"
26 #else
27 #define DEFAULT_GPU_CONFIG "gl"
28 #endif
29 #else
30 #define DEFAULT_GPU_CONFIG ""
31 #endif
32
33 static const char defaultConfigs[] = "8888 " DEFAULT_GPU_CONFIG
34 " nonrendering "
35 #if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
36 " angle_d3d11_es2"
37 #endif
38 ;
39
40 #undef DEFAULT_GPU_CONFIG
41
42 // clang-format off
43 static const struct {
44 const char* predefinedConfig;
45 const char* backend;
46 const char* options;
47 } gPredefinedConfigs[] = {
48 { "gl", "gpu", "api=gl" },
49 { "glf16", "gpu", "api=gl,color=f16" },
50 { "glf16norm", "gpu", "api=gl,color=f16norm" },
51 { "glsrgba", "gpu", "api=gl,color=srgba" },
52 { "gl1010102", "gpu", "api=gl,color=1010102" },
53 { "gles", "gpu", "api=gles" },
54 { "glesf16", "gpu", "api=gles,color=f16" },
55 { "glessrgba", "gpu", "api=gles,color=srgba" },
56 { "gles1010102", "gpu", "api=gles,color=1010102" },
57 { "glesfakev2", "gpu", "api=glesfakev2" },
58 { "gldmsaa", "gpu", "api=gl,dmsaa=true" },
59 { "glesdmsaa", "gpu", "api=gles,dmsaa=true" },
60 { "glmsaa4", "gpu", "api=gl,samples=4" },
61 { "glmsaa8" , "gpu", "api=gl,samples=8" },
62 { "glesmsaa4", "gpu", "api=gles,samples=4" },
63 { "glbetex", "gpu", "api=gl,surf=betex" },
64 { "glesbetex", "gpu", "api=gles,surf=betex" },
65 { "glbert", "gpu", "api=gl,surf=bert" },
66 { "glesbert", "gpu", "api=gles,surf=bert" },
67 { "gl4444", "gpu", "api=gl,color=4444" },
68 { "gles4444", "gpu", "api=gles,color=4444" },
69 { "gl565", "gpu", "api=gl,color=565" },
70 { "gl888x", "gpu", "api=gl,color=888x" },
71 { "gles888x", "gpu", "api=gles,color=888x" },
72 { "glr8", "gpu", "api=gl,color=r8" },
73 { "glnostencils", "gpu", "api=gl,stencils=false" },
74 { "gldft", "gpu", "api=gl,dit=true" },
75 { "glesdft", "gpu", "api=gles,dit=true" },
76 { "glslug", "gpu", "api=gl,slug=true" },
77 { "glserializeslug", "gpu", "api=gl,serializeSlug=true" },
78 { "glremoteslug", "gpu", "api=gl,remoteSlug=true" },
79 { "gltestpersistentcache", "gpu", "api=gl,testPersistentCache=1" },
80 { "gltestglslcache", "gpu", "api=gl,testPersistentCache=2" },
81 { "gltestprecompile", "gpu", "api=gl,testPrecompileGanesh=true" },
82 { "glestestprecompile", "gpu", "api=gles,testPrecompileGanesh=true" },
83 { "glddl", "gpu", "api=gl,useDDLSink=true" },
84 { "glreducedshaders", "gpu", "api=gl,reducedShaders=true" },
85 { "glesreducedshaders", "gpu", "api=gles,reducedShaders=true" },
86 { "angle_d3d11_es2", "gpu", "api=angle_d3d11_es2" },
87 { "angle_d3d11_es3", "gpu", "api=angle_d3d11_es3" },
88 { "angle_d3d9_es2", "gpu", "api=angle_d3d9_es2" },
89 { "angle_d3d11_es2_msaa4", "gpu", "api=angle_d3d11_es2,samples=4" },
90 { "angle_d3d11_es2_msaa8", "gpu", "api=angle_d3d11_es2,samples=8" },
91 { "angle_d3d11_es2_dmsaa", "gpu", "api=angle_d3d11_es2,dmsaa=true" },
92 { "angle_d3d11_es3_msaa4", "gpu", "api=angle_d3d11_es3,samples=4" },
93 { "angle_d3d11_es3_msaa8", "gpu", "api=angle_d3d11_es3,samples=8" },
94 { "angle_d3d11_es3_dmsaa", "gpu", "api=angle_d3d11_es3,dmsaa=true" },
95 { "angle_gl_es2", "gpu", "api=angle_gl_es2" },
96 { "angle_gl_es3", "gpu", "api=angle_gl_es3" },
97 { "angle_gl_es2_msaa4", "gpu", "api=angle_gl_es2,samples=4" },
98 { "angle_gl_es2_msaa8", "gpu", "api=angle_gl_es2,samples=8" },
99 { "angle_gl_es2_dmsaa", "gpu", "api=angle_gl_es2,dmsaa=true" },
100 { "angle_gl_es3_msaa4", "gpu", "api=angle_gl_es3,samples=4" },
101 { "angle_gl_es3_msaa8", "gpu", "api=angle_gl_es3,samples=8" },
102 { "angle_gl_es3_dmsaa", "gpu", "api=angle_gl_es3,dmsaa=true" },
103 { "angle_mtl_es2", "gpu", "api=angle_mtl_es2" },
104 { "angle_mtl_es3", "gpu", "api=angle_mtl_es3" },
105 { "mock", "gpu", "api=mock" },
106 #ifdef SK_VULKAN
107 { "vk", "gpu", "api=vulkan" },
108 { "vkf16", "gpu", "api=vulkan,color=f16" },
109 { "vk1010102", "gpu", "api=vulkan,color=1010102" },
110 { "vkdmsaa", "gpu", "api=vulkan,dmsaa=true" },
111 { "vknostencils", "gpu", "api=vulkan,stencils=false" },
112 { "vkmsaa4", "gpu", "api=vulkan,samples=4" },
113 { "vkmsaa8", "gpu", "api=vulkan,samples=8" },
114 { "vkbetex", "gpu", "api=vulkan,surf=betex" },
115 { "vkbert", "gpu", "api=vulkan,surf=bert" },
116 { "vktestpersistentcache", "gpu", "api=vulkan,testPersistentCache=1" },
117 { "vkddl", "gpu", "api=vulkan,useDDLSink=true" },
118 #endif
119 #ifdef SK_METAL
120 { "mtl", "gpu", "api=metal" },
121 { "mtlf16", "gpu", "api=metal,color=f16" },
122 { "mtlf16norm", "gpu", "api=metal,color=f16norm" },
123 { "mtlsrgba", "gpu", "api=metal,color=srgba"},
124 { "mtl1010102", "gpu", "api=metal,color=1010102" },
125 { "mtl_dmsaa", "gpu", "api=metal,dmsaa=true" },
126 { "mtlmsaa4", "gpu", "api=metal,samples=4" },
127 { "mtlmsaa8", "gpu", "api=metal,samples=8" },
128 { "mtlddl", "gpu", "api=metal,useDDLSink=true" },
129 { "mtltestprecompile", "gpu", "api=metal,testPrecompileGanesh=true" },
130 { "mtlreducedshaders", "gpu", "api=metal,reducedShaders=true" },
131 #endif
132 #ifdef SK_DIRECT3D
133 { "d3d", "gpu", "api=direct3d" },
134 { "d3dmsaa4", "gpu", "api=direct3d,samples=4" },
135 { "d3dmsaa8", "gpu", "api=direct3d,samples=8" },
136 #endif
137
138 #if defined(SK_GRAPHITE)
139 #ifdef SK_DIRECT3D
140 { "grd3d", "graphite", "api=direct3d" },
141 #endif
142 #ifdef SK_DAWN
143 { "grdawn_d3d11", "graphite", "api=dawn_d3d11" },
144 { "grdawn_d3d12", "graphite", "api=dawn_d3d12" },
145 { "grdawn_mtl", "graphite", "api=dawn_mtl" },
146 { "grdawn_vk", "graphite", "api=dawn_vk" },
147 { "grdawn_gl", "graphite", "api=dawn_gl" },
148 { "grdawn_gles", "graphite", "api=dawn_gles" },
149 #if defined(SK_ENABLE_PRECOMPILE)
150 { "grdawn_mtltestprecompile", "graphite", "api=dawn_mtl,testPrecompileGraphite=true" },
151 { "grdawn_vktestprecompile", "graphite", "api=dawn_vk, testPrecompileGraphite=true" },
152 #endif
153 { "grdawn_mtltesttracking", "graphite", "api=dawn_mtl,testPipelineTracking=true" },
154 { "grdawn_vktesttracking", "graphite", "api=dawn_vk, testPipelineTracking=true" },
155 #endif
156 #ifdef SK_METAL
157 { "grmtl", "graphite", "api=metal" },
158 { "grmtlf16", "graphite", "api=metal,color=f16" },
159 { "grmtlf16norm", "graphite", "api=metal,color=f16norm" },
160 { "grmtlsrgba", "graphite", "api=metal,color=srgba"},
161 { "grmtl1010102", "graphite", "api=metal,color=1010102" },
162 #if defined(SK_ENABLE_PRECOMPILE)
163 { "grmtltestprecompile", "graphite", "api=metal,testPrecompileGraphite=true" },
164 { "grmtltestprecompilef16", "graphite", "api=metal,testPrecompileGraphite=true,color=f16" },
165 #endif
166 { "grmtltesttracking", "graphite", "api=metal,testPipelineTracking=true" },
167 #endif
168 #ifdef SK_VULKAN
169 { "grvk", "graphite", "api=vulkan" },
170 #if defined(SK_ENABLE_PRECOMPILE)
171 { "grvktestprecompile", "graphite", "api=vulkan,testPrecompileGraphite=true" },
172 #endif
173 { "grvktesttracking", "graphite", "api=vulkan,testPipelineTracking=true" },
174 { "grvktestpersistentstorage","graphite", "api=vulkan,testPersistentStorage=true" },
175 #endif
176 #endif
177
178 };
179 // clang-format on
180
181 static const char configHelp[] =
182 "Options: 565 4444 8888 rgba bgra rgbx 1010102 101010x bgra1010102 bgr101010x f16 f16norm f16f16f16x "
183 "f32 nonrendering null pdf pdfa pdf300 skp svg xps";
184
185 static const char* config_help_fn() {
186 static SkString helpString;
187 helpString.set(configHelp);
188 for (const auto& config : gPredefinedConfigs) {
189 helpString.appendf(" %s", config.predefinedConfig);
190 }
191 helpString.append(" or use extended form 'backend[option=value,...]'.\n");
192 return helpString.c_str();
193 }
194
195 static const char configExtendedHelp[] =
196 "Extended form: 'backend(option=value,...)'\n\n"
197 "Possible backends and options:\n"
198 "\n"
199 "gpu[api=string,color=string,dit=bool,dmsaa=bool,samples=int]\n"
200 "\tapi\ttype: string\trequired\n"
201 "\t Select graphics API to use with gpu backend.\n"
202 "\t Options:\n"
203 "\t\tgl \t\t\tUse OpenGL.\n"
204 "\t\tgles \t\t\tUse OpenGL ES.\n"
205 "\t\tglesfakev2 \t\t\tUse OpenGL ES with version faked as 2.0.\n"
206 "\t\tnullgl \t\t\tUse null OpenGL.\n"
207 "\t\tangle_d3d9_es2\t\tUse OpenGL ES2 on the ANGLE Direct3D9 backend.\n"
208 "\t\tangle_d3d11_es2\t\tUse OpenGL ES2 on the ANGLE Direct3D11 backend.\n"
209 "\t\tangle_d3d11_es3\t\tUse OpenGL ES3 on the ANGLE Direct3D11 backend.\n"
210 "\t\tangle_gl_es2\t\tUse OpenGL ES2 on the ANGLE OpenGL backend.\n"
211 "\t\tangle_gl_es3\t\tUse OpenGL ES3 on the ANGLE OpenGL backend.\n"
212 "\t\tmock\t\t\tUse mock context.\n"
213 #ifdef SK_VULKAN
214 "\t\tvulkan\t\t\tUse Vulkan.\n"
215 #endif
216 #ifdef SK_METAL
217 "\t\tmetal\t\t\tUse Metal.\n"
218 #endif
219 "\tcolor\ttype: string\tdefault: 8888.\n"
220 "\t Select framebuffer color format.\n"
221 "\t Options:\n"
222 "\t\t8888\t\t\tLinear 8888.\n"
223 "\t\t888x\t\t\tLinear 888x.\n"
224 "\t\t4444\t\t\tLinear 4444.\n"
225 "\t\t565\t\t\tLinear 565.\n"
226 "\t\t1010102\t\t\tLinear 1010102.\n"
227 "\t\tf16\t\t\t16-bit floating point.\n"
228 "\tdit\ttype: bool\tdefault: false.\n"
229 "\t Use device independent text.\n"
230 "\tdmsaa\ttype: bool\tdefault: false.\n"
231 "\t Use internal MSAA to render to non-MSAA surfaces.\n"
232 "\tsamples\ttype: int\tdefault: 0.\n"
233 "\t Use multisampling with N samples.\n"
234 "\tstencils\ttype: bool\tdefault: true.\n"
235 "\t Allow the use of stencil buffers.\n"
236 "\t Run with and without worker threads, check that results match.\n"
237 "\ttestPersistentCache\ttype: int\tdefault: 0.\n"
238 "\t 1: Run using a pre-warmed binary GrContextOptions::fPersistentCache.\n"
239 "\t 2: Run using a pre-warmed GLSL GrContextOptions::fPersistentCache.\n"
240 "\ttestPersistentStorage\ttype: bool\tdefault: false.\n"
241 "\t Store Pipeline data in ContextOptions::fPersistentPipelineStorage.\n"
242 "\ttestPrecompileGraphite\ttype: bool\tdefault: false.\n"
243 "\t Use the precompilation testing Sink.\n"
244 "\ttestPipelineTracking\ttype: bool\tdefault: false.\n"
245 "\t Use the pipeline tracking testing Sink.\n"
246 "\tsurf\ttype: string\tdefault: default.\n"
247 "\t Controls the type of backing store for SkSurfaces.\n"
248 "\t Options:\n"
249 "\t\tdefault\t\t\tA renderable texture created in Skia's resource cache.\n"
250 "\t\tbetex\t\t\tA wrapped backend texture.\n"
251 "\t\tbert\t\t\tA wrapped backend render target\n"
252 "\n"
253 "Predefined configs:\n\n"
254 // Help text for pre-defined configs is auto-generated from gPredefinedConfigs
255 ;
256
257 static const char* config_extended_help_fn() {
258 static SkString helpString;
259 helpString.set(configExtendedHelp);
260 for (const auto& config : gPredefinedConfigs) {
261 helpString.appendf("\t%-10s\t= gpu(%s)\n", config.predefinedConfig, config.options);
262 }
263 return helpString.c_str();
264 }
265
266 DEFINE_extended_string(config, defaultConfigs, config_help_fn(), config_extended_help_fn());
267
268 DEFINE_int(gpuThreads,
269 2,
270 "Create this many extra threads to assist with GPU work, "
271 "including software path rendering. Defaults to two.");
272
273 DEFINE_int(internalSamples, -1,
274 "Number of samples for internal draws that use MSAA, or default value if negative.");
275
276 SkCommandLineConfig::SkCommandLineConfig(const SkString& tag,
277 const SkString& backend,
278 const TArray<SkString>& viaParts)
279 : fTag(tag), fBackend(backend) {
280 static const auto* kColorSpaces = new std::unordered_map<std::string_view, SkColorSpace*>{
281 {"narrow", // 'narrow' has a gamut narrower than sRGB, and different transfer function.
282 SkColorSpace::MakeRGB(SkNamedTransferFn::k2Dot2, gNarrow_toXYZD50).release()},
283 {"srgb",
284 SkColorSpace::MakeSRGB().release()},
285 {"srgb2", // The same as "srgb" but works around ignoring prior images in Gold
286 SkColorSpace::MakeSRGB().release()},
287 {"linear",
288 SkColorSpace::MakeSRGBLinear().release()},
289 {"p3",
290 SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB, SkNamedGamut::kDisplayP3).release()},
291 {"spin",
292 SkColorSpace::MakeSRGB()->makeColorSpin().release()},
293 {"rec2020",
294 SkColorSpace::MakeRGB(SkNamedTransferFn::kRec2020, SkNamedGamut::kRec2020).release()},
295 };
296
297 // Strip off any via parts that refer to color spaces (and remember the last one we see)
298 for (const SkString& via : viaParts) {
299 auto it = kColorSpaces->find(via.c_str());
300 if (it == kColorSpaces->end()) {
301 fViaParts.push_back(via);
302 } else {
303 fColorSpace = sk_ref_sp(it->second);
304 }
305 }
306 }
307
308 SkCommandLineConfig::~SkCommandLineConfig() {}
309
310 static bool parse_option_int(const SkString& value, int* outInt) {
311 if (value.isEmpty()) {
312 return false;
313 }
314 char* endptr = nullptr;
315 long intValue = strtol(value.c_str(), &endptr, 10);
316 if (*endptr != '\0') {
317 return false;
318 }
319 *outInt = static_cast<int>(intValue);
320 return true;
321 }
322 static bool parse_option_bool(const SkString& value, bool* outBool) {
323 if (value.equals("true")) {
324 *outBool = true;
325 return true;
326 }
327 if (value.equals("false")) {
328 *outBool = false;
329 return true;
330 }
331 return false;
332 }
333
334 #if defined(SK_GANESH)
335 static bool parse_option_gpu_api(const SkString& value,
336 SkCommandLineConfigGpu::ContextType* outContextType,
337 bool* outFakeGLESVersion2) {
338 *outFakeGLESVersion2 = false;
339 if (value.equals("gl")) {
340 *outContextType = skgpu::ContextType::kGL;
341 return true;
342 }
343 if (value.equals("gles")) {
344 *outContextType = skgpu::ContextType::kGLES;
345 return true;
346 }
347 if (value.equals("glesfakev2")) {
348 *outContextType = skgpu::ContextType::kGLES;
349 *outFakeGLESVersion2 = true;
350 return true;
351 }
352 if (value.equals("angle_d3d9_es2")) {
353 *outContextType = skgpu::ContextType::kANGLE_D3D9_ES2;
354 return true;
355 }
356 if (value.equals("angle_d3d11_es2")) {
357 *outContextType = skgpu::ContextType::kANGLE_D3D11_ES2;
358 return true;
359 }
360 if (value.equals("angle_d3d11_es3")) {
361 *outContextType = skgpu::ContextType::kANGLE_D3D11_ES3;
362 return true;
363 }
364 if (value.equals("angle_gl_es2")) {
365 *outContextType = skgpu::ContextType::kANGLE_GL_ES2;
366 return true;
367 }
368 if (value.equals("angle_gl_es3")) {
369 *outContextType = skgpu::ContextType::kANGLE_GL_ES3;
370 return true;
371 }
372 if (value.equals("angle_mtl_es2")) {
373 *outContextType = skgpu::ContextType::kANGLE_Metal_ES2;
374 return true;
375 }
376 if (value.equals("angle_mtl_es3")) {
377 *outContextType = skgpu::ContextType::kANGLE_Metal_ES3;
378 return true;
379 }
380 if (value.equals("mock")) {
381 *outContextType = skgpu::ContextType::kMock;
382 return true;
383 }
384 #ifdef SK_VULKAN
385 if (value.equals("vulkan")) {
386 *outContextType = skgpu::ContextType::kVulkan;
387 return true;
388 }
389 #endif
390 #ifdef SK_METAL
391 if (value.equals("metal")) {
392 *outContextType = skgpu::ContextType::kMetal;
393 return true;
394 }
395 #endif
396 #ifdef SK_DIRECT3D
397 if (value.equals("direct3d")) {
398 *outContextType = skgpu::ContextType::kDirect3D;
399 return true;
400 }
401 #endif
402 return false;
403 }
404
405 static bool parse_option_gpu_surf_type(const SkString& value,
406 SkCommandLineConfigGpu::SurfType* surfType) {
407 if (value.equals("default")) {
408 *surfType = SkCommandLineConfigGpu::SurfType::kDefault;
409 return true;
410 }
411 if (value.equals("betex")) {
412 *surfType = SkCommandLineConfigGpu::SurfType::kBackendTexture;
413 return true;
414 }
415 if (value.equals("bert")) {
416 *surfType = SkCommandLineConfigGpu::SurfType::kBackendRenderTarget;
417 return true;
418 }
419 return false;
420 }
421 #endif // SK_GANESH
422
423 static bool parse_option_gpu_color(const SkString& value,
424 SkColorType* outColorType,
425 SkAlphaType* alphaType) {
426 // We always use premul unless the color type is 565.
427 *alphaType = kPremul_SkAlphaType;
428
429 if (value.equals("8888")) {
430 *outColorType = kRGBA_8888_SkColorType;
431 } else if (value.equals("888x")) {
432 *outColorType = kRGB_888x_SkColorType;
433 } else if (value.equals("bgra8")) {
434 *outColorType = kBGRA_8888_SkColorType;
435 } else if (value.equals("4444")) {
436 *outColorType = kARGB_4444_SkColorType;
437 } else if (value.equals("565")) {
438 *outColorType = kRGB_565_SkColorType;
439 *alphaType = kOpaque_SkAlphaType;
440 } else if (value.equals("1010102")) {
441 *outColorType = kRGBA_1010102_SkColorType;
442 } else if (value.equals("f16")) {
443 *outColorType = kRGBA_F16_SkColorType;
444 } else if (value.equals("f16norm")) {
445 *outColorType = kRGBA_F16Norm_SkColorType;
446 } else if (value.equals("srgba")) {
447 *outColorType = kSRGBA_8888_SkColorType;
448 } else if (value.equals("r8")) {
449 *outColorType = kR8_unorm_SkColorType;
450 *alphaType = kOpaque_SkAlphaType;
451 } else {
452 return false;
453 }
454 return true;
455 }
456
457 // Extended options take form --config item[key1=value1,key2=value2,...]
458 // Example: --config gpu[api=gl,color=8888]
459 class ExtendedOptions {
460 public:
461 ExtendedOptions(const SkString& optionsString, bool* outParseSucceeded) {
462 TArray<SkString> optionParts;
463 SkStrSplit(optionsString.c_str(), ",", kStrict_SkStrSplitMode, &optionParts);
464 for (int i = 0; i < optionParts.size(); ++i) {
465 TArray<SkString> keyValueParts;
466 SkStrSplit(optionParts[i].c_str(), "=", kStrict_SkStrSplitMode, &keyValueParts);
467 if (keyValueParts.size() != 2) {
468 *outParseSucceeded = false;
469 return;
470 }
471 const SkString& key = keyValueParts[0];
472 const SkString& value = keyValueParts[1];
473 if (fOptionsMap.find(key) == nullptr) {
474 fOptionsMap.set(key, value);
475 } else {
476 // Duplicate values are not allowed.
477 *outParseSucceeded = false;
478 return;
479 }
480 }
481 *outParseSucceeded = true;
482 }
483
484 bool get_option_gpu_color(const char* optionKey,
485 SkColorType* outColorType,
486 SkAlphaType* alphaType,
487 bool optional = true) const {
488 SkString* optionValue = fOptionsMap.find(SkString(optionKey));
489 if (optionValue == nullptr) {
490 return optional;
491 }
492 return parse_option_gpu_color(*optionValue, outColorType, alphaType);
493 }
494
495 #if defined(SK_GANESH)
496 bool get_option_gpu_api(const char* optionKey,
497 SkCommandLineConfigGpu::ContextType* outContextType,
498 bool* outFakeGLESVersion2,
499 bool optional = true) const {
500 SkString* optionValue = fOptionsMap.find(SkString(optionKey));
501 if (optionValue == nullptr) {
502 return optional;
503 }
504 return parse_option_gpu_api(*optionValue, outContextType, outFakeGLESVersion2);
505 }
506
507 bool get_option_gpu_surf_type(const char* optionKey,
508 SkCommandLineConfigGpu::SurfType* outSurfType,
509 bool optional = true) const {
510 SkString* optionValue = fOptionsMap.find(SkString(optionKey));
511 if (optionValue == nullptr) {
512 return optional;
513 }
514 return parse_option_gpu_surf_type(*optionValue, outSurfType);
515 }
516 #endif
517
518 #if defined(SK_GRAPHITE)
519 bool get_option_graphite_api(const char* optionKey,
520 SkCommandLineConfigGraphite::ContextType* outContextType) const {
521 SkString* optionValue = fOptionsMap.find(SkString(optionKey));
522 if (optionValue == nullptr) {
523 return false;
524 }
525 #ifdef SK_DAWN
526 if (optionValue->equals("dawn_d3d11")) {
527 *outContextType = skgpu::ContextType::kDawn_D3D11;
528 return true;
529 }
530 if (optionValue->equals("dawn_d3d12")) {
531 *outContextType = skgpu::ContextType::kDawn_D3D12;
532 return true;
533 }
534 if (optionValue->equals("dawn_mtl")) {
535 *outContextType = skgpu::ContextType::kDawn_Metal;
536 return true;
537 }
538 if (optionValue->equals("dawn_vk")) {
539 *outContextType = skgpu::ContextType::kDawn_Vulkan;
540 return true;
541 }
542 if (optionValue->equals("dawn_gl")) {
543 *outContextType = skgpu::ContextType::kDawn_OpenGL;
544 return true;
545 }
546 if (optionValue->equals("dawn_gles")) {
547 *outContextType = skgpu::ContextType::kDawn_OpenGLES;
548 return true;
549 }
550 #endif
551 #ifdef SK_DIRECT3D
552 if (optionValue->equals("direct3d")) {
553 *outContextType = skgpu::ContextType::kDirect3D;
554 return true;
555 }
556 #endif
557 #ifdef SK_METAL
558 if (optionValue->equals("metal")) {
559 *outContextType = skgpu::ContextType::kMetal;
560 return true;
561 }
562 #endif
563 #ifdef SK_VULKAN
564 if (optionValue->equals("vulkan")) {
565 *outContextType = skgpu::ContextType::kVulkan;
566 return true;
567 }
568 #endif
569
570 return false;
571 }
572 #endif
573
574 bool get_option_int(const char* optionKey, int* outInt, bool optional = true) const {
575 SkString* optionValue = fOptionsMap.find(SkString(optionKey));
576 if (optionValue == nullptr) {
577 return optional;
578 }
579 return parse_option_int(*optionValue, outInt);
580 }
581
582 bool get_option_bool(const char* optionKey, bool* outBool, bool optional = true) const {
583 SkString* optionValue = fOptionsMap.find(SkString(optionKey));
584 if (optionValue == nullptr) {
585 return optional;
586 }
587 return parse_option_bool(*optionValue, outBool);
588 }
589
590 private:
591 THashMap<SkString, SkString> fOptionsMap;
592 };
593
594 #if defined(SK_GANESH)
595 SkCommandLineConfigGpu::SkCommandLineConfigGpu(const SkString& tag,
596 const TArray<SkString>& viaParts,
597 ContextType contextType,
598 bool fakeGLESVersion2,
599 uint32_t surfaceFlags,
600 int samples,
601 SkColorType colorType,
602 SkAlphaType alphaType,
603 bool useStencilBuffers,
604 int testPersistentCache,
605 bool testPrecompileGanesh,
606 bool useDDLSink,
607 bool slug,
608 bool serializeSlug,
609 bool remoteSlug,
610 bool reducedShaders,
611 SurfType surfType)
612 : SkCommandLineConfig(tag, SkString("gpu"), viaParts)
613 , fContextType(contextType)
614 , fContextOverrides(ContextOverrides::kNone)
615 , fSurfaceFlags(surfaceFlags)
616 , fSamples(samples)
617 , fColorType(colorType)
618 , fAlphaType(alphaType)
619 , fTestPersistentCache(testPersistentCache)
620 , fTestPrecompileGanesh(testPrecompileGanesh)
621 , fUseDDLSink(useDDLSink)
622 , fSlug(slug)
623 , fSerializeSlug(serializeSlug)
624 , fRemoteSlug(remoteSlug)
625 , fReducedShaders(reducedShaders)
626 , fSurfType(surfType) {
627 if (!useStencilBuffers) {
628 fContextOverrides |= ContextOverrides::kAvoidStencilBuffers;
629 }
630 if (fakeGLESVersion2) {
631 fContextOverrides |= ContextOverrides::kFakeGLESVersionAs2;
632 }
633 if (reducedShaders) {
634 fContextOverrides |= ContextOverrides::kReducedShaders;
635 }
636 }
637
638 SkCommandLineConfigGpu* parse_command_line_config_gpu(const SkString& tag,
639 const TArray<SkString>& vias,
640 const SkString& options) {
641 // Defaults for GPU backend.
642 SkCommandLineConfigGpu::ContextType contextType = skgpu::ContextType::kGL;
643 bool useDIText = false;
644 bool useDMSAA = false;
645 int samples = 1;
646 SkColorType colorType = kRGBA_8888_SkColorType;
647 SkAlphaType alphaType = kPremul_SkAlphaType;
648 bool useStencils = true;
649 int testPersistentCache = 0;
650 bool testPrecompileGanesh= false;
651 bool useDDLs = false;
652 bool slug = false;
653 bool serializeSlug = false;
654 bool remoteSlug = false;
655 bool reducedShaders = false;
656 bool fakeGLESVersion2 = false;
657 SkCommandLineConfigGpu::SurfType surfType = SkCommandLineConfigGpu::SurfType::kDefault;
658
659 bool parseSucceeded = false;
660 ExtendedOptions extendedOptions(options, &parseSucceeded);
661 if (!parseSucceeded) {
662 return nullptr;
663 }
664
665 bool validOptions =
666 extendedOptions.get_option_gpu_api("api", &contextType, &fakeGLESVersion2, false) &&
667 extendedOptions.get_option_bool("dit", &useDIText) &&
668 extendedOptions.get_option_int("samples", &samples) &&
669 extendedOptions.get_option_bool("dmsaa", &useDMSAA) &&
670 extendedOptions.get_option_gpu_color("color", &colorType, &alphaType) &&
671 extendedOptions.get_option_bool("stencils", &useStencils) &&
672 extendedOptions.get_option_int("testPersistentCache", &testPersistentCache) &&
673 extendedOptions.get_option_bool("testPrecompileGanesh", &testPrecompileGanesh) &&
674 extendedOptions.get_option_bool("useDDLSink", &useDDLs) &&
675 extendedOptions.get_option_bool("slug", &slug) &&
676 extendedOptions.get_option_bool("serializeSlug", &serializeSlug) &&
677 extendedOptions.get_option_bool("remoteSlug", &remoteSlug) &&
678 extendedOptions.get_option_bool("reducedShaders", &reducedShaders) &&
679 extendedOptions.get_option_gpu_surf_type("surf", &surfType);
680
681 if (!validOptions) {
682 return nullptr;
683 }
684
685 uint32_t surfaceFlags = 0;
686 if (useDIText) {
687 surfaceFlags |= SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
688 }
689 if (useDMSAA) {
690 surfaceFlags |= SkSurfaceProps::kDynamicMSAA_Flag;
691 }
692
693 return new SkCommandLineConfigGpu(tag,
694 vias,
695 contextType,
696 fakeGLESVersion2,
697 surfaceFlags,
698 samples,
699 colorType,
700 alphaType,
701 useStencils,
702 testPersistentCache,
703 testPrecompileGanesh,
704 useDDLs,
705 slug,
706 serializeSlug,
707 remoteSlug,
708 reducedShaders,
709 surfType);
710 }
711 #endif
712
713 #if defined(SK_GRAPHITE)
714
715 SkCommandLineConfigGraphite* parse_command_line_config_graphite(const SkString& tag,
716 const TArray<SkString>& vias,
717 const SkString& options) {
718 using ContextType = skgpu::ContextType;
719
720 ContextType contextType = skgpu::ContextType::kMetal;
721 SkColorType colorType = kRGBA_8888_SkColorType;
722 SkAlphaType alphaType = kPremul_SkAlphaType;
723 bool testPersistentStorage = false;
724 bool testPrecompileGraphite = false;
725 bool testPipelineTracking = false;
726
727 bool parseSucceeded = false;
728 ExtendedOptions extendedOptions(options, &parseSucceeded);
729 if (!parseSucceeded) {
730 return nullptr;
731 }
732
733 bool validOptions =
734 extendedOptions.get_option_graphite_api("api", &contextType) &&
735 extendedOptions.get_option_gpu_color("color", &colorType, &alphaType) &&
736 extendedOptions.get_option_bool("testPersistentStorage", &testPersistentStorage) &&
737 extendedOptions.get_option_bool("testPrecompileGraphite", &testPrecompileGraphite) &&
738 extendedOptions.get_option_bool("testPipelineTracking", &testPipelineTracking);
739 if (!validOptions) {
740 return nullptr;
741 }
742
743 return new SkCommandLineConfigGraphite(tag,
744 vias,
745 contextType,
746 colorType,
747 alphaType,
748 testPersistentStorage,
749 testPrecompileGraphite,
750 testPipelineTracking);
751 }
752
753 #endif
754
755 SkCommandLineConfigSvg::SkCommandLineConfigSvg(const SkString& tag,
756 const TArray<SkString>& viaParts,
757 int pageIndex)
758 : SkCommandLineConfig(tag, SkString("svg"), viaParts), fPageIndex(pageIndex) {}
759
760 SkCommandLineConfigSvg* parse_command_line_config_svg(const SkString& tag,
761 const TArray<SkString>& vias,
762 const SkString& options) {
763 // Defaults for SVG backend.
764 int pageIndex = 0;
765
766 bool parseSucceeded = false;
767 ExtendedOptions extendedOptions(options, &parseSucceeded);
768 if (!parseSucceeded) {
769 return nullptr;
770 }
771
772 bool validOptions = extendedOptions.get_option_int("page", &pageIndex);
773
774 if (!validOptions) {
775 return nullptr;
776 }
777
778 return new SkCommandLineConfigSvg(tag, vias, pageIndex);
779 }
780
781 void ParseConfigs(const CommandLineFlags::StringArray& configs,
782 SkCommandLineConfigArray* outResult) {
783 outResult->clear();
784 for (int i = 0; i < configs.size(); ++i) {
785 SkString extendedBackend;
786 SkString extendedOptions;
787 SkString simpleBackend;
788 TArray<SkString> vias;
789
790 SkString tag(configs[i]);
791 TArray<SkString> parts;
792 SkStrSplit(tag.c_str(), "[", kStrict_SkStrSplitMode, &parts);
793 if (parts.size() == 2) {
794 TArray<SkString> parts2;
795 SkStrSplit(parts[1].c_str(), "]", kStrict_SkStrSplitMode, &parts2);
796 if (parts2.size() == 2 && parts2[1].isEmpty()) {
797 SkStrSplit(parts[0].c_str(), "-", kStrict_SkStrSplitMode, &vias);
798 if (vias.size()) {
799 extendedBackend = vias[vias.size() - 1];
800 vias.pop_back();
801 } else {
802 extendedBackend = parts[0];
803 }
804 extendedOptions = parts2[0];
805 simpleBackend.printf("%s[%s]", extendedBackend.c_str(), extendedOptions.c_str());
806 }
807 }
808
809 if (extendedBackend.isEmpty()) {
810 simpleBackend = tag;
811 SkStrSplit(tag.c_str(), "-", kStrict_SkStrSplitMode, &vias);
812 if (vias.size()) {
813 simpleBackend = vias[vias.size() - 1];
814 vias.pop_back();
815 }
816 for (auto& predefinedConfig : gPredefinedConfigs) {
817 if (simpleBackend.equals(predefinedConfig.predefinedConfig)) {
818 extendedBackend = predefinedConfig.backend;
819 extendedOptions = predefinedConfig.options;
820 break;
821 }
822 }
823 }
824 SkCommandLineConfig* parsedConfig = nullptr;
825 #if defined(SK_GANESH)
826 if (extendedBackend.equals("gpu")) {
827 parsedConfig = parse_command_line_config_gpu(tag, vias, extendedOptions);
828 }
829 #endif
830 #if defined(SK_GRAPHITE)
831 if (extendedBackend.equals("graphite")) {
832 parsedConfig = parse_command_line_config_graphite(tag, vias, extendedOptions);
833 }
834 #endif
835 if (extendedBackend.equals("svg")) {
836 parsedConfig = parse_command_line_config_svg(tag, vias, extendedOptions);
837 }
838 if (!parsedConfig) {
839 parsedConfig = new SkCommandLineConfig(tag, simpleBackend, vias);
840 }
841 outResult->emplace_back(parsedConfig);
842 }
843 }