• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "SkColorSpacePriv.h"
9 #include "SkCommonFlagsConfig.h"
10 #include "SkImageInfo.h"
11 #include "SkTHash.h"
12 
13 #include <stdlib.h>
14 
15 using sk_gpu_test::GrContextFactory;
16 
17 #if defined(SK_BUILD_FOR_ANDROID) || defined(SK_BUILD_FOR_IOS)
18 #    define DEFAULT_GPU_CONFIG "gles"
19 #else
20 #    define DEFAULT_GPU_CONFIG "gl"
21 #endif
22 
23 static const char defaultConfigs[] =
24     "8888 " DEFAULT_GPU_CONFIG " nonrendering "
25 #if defined(SK_BUILD_FOR_WIN)
26     " angle_d3d11_es2"
27 #endif
28     ;
29 
30 #undef DEFAULT_GPU_CONFIG
31 
32 // clang-format off
33 static const struct {
34     const char* predefinedConfig;
35     const char* backend;
36     const char* options;
37 } gPredefinedConfigs[] = {
38     { "gl",                    "gpu", "api=gl" },
39     { "gles",                  "gpu", "api=gles" },
40     { "glmsaa4",               "gpu", "api=gl,samples=4" },
41     { "glmsaa8" ,              "gpu", "api=gl,samples=8" },
42     { "glesmsaa4",             "gpu", "api=gles,samples=4" },
43     { "glnvpr4",               "gpu", "api=gl,nvpr=true,samples=4" },
44     { "glnvpr8" ,              "gpu", "api=gl,nvpr=true,samples=8" },
45     { "glesnvpr4",             "gpu", "api=gles,nvpr=true,samples=4" },
46     { "glbetex",               "gpu", "api=gl,surf=betex" },
47     { "glesbetex",             "gpu", "api=gles,surf=betex" },
48     { "glbert",                "gpu", "api=gl,surf=bert" },
49     { "glesbert",              "gpu", "api=gles,surf=bert" },
50     { "gl4444",                "gpu", "api=gl,color=4444" },
51     { "gles4444",              "gpu", "api=gles,color=4444" },
52     { "gl565",                 "gpu", "api=gl,color=565" },
53     { "gl888x",                "gpu", "api=gl,color=888x" },
54     { "gles888x",              "gpu", "api=gles,color=888x" },
55     { "gl1010102",             "gpu", "api=gl,color=1010102" },
56     { "gles1010102",           "gpu", "api=gles,color=1010102" },
57     { "glsrgb",                "gpu", "api=gl,color=srgb" },
58     { "glp3",                  "gpu", "api=gl,color=p3" },
59     { "glesrgb",               "gpu", "api=gl,color=esrgb" },
60     { "glnarrow",              "gpu", "api=gl,color=narrow" },
61     { "glenarrow",             "gpu", "api=gl,color=enarrow" },
62     { "glf16",                 "gpu", "api=gl,color=f16" },
63     { "glessrgb",              "gpu", "api=gles,color=srgb" },
64     { "glesesrgb",             "gpu", "api=gles,color=esrgb" },
65     { "glesnarrow",            "gpu", "api=gles,color=narrow" },
66     { "glesenarrow",           "gpu", "api=gles,color=enarrow" },
67     { "glesf16",               "gpu", "api=gles,color=f16" },
68     { "glnostencils",          "gpu", "api=gl,stencils=false" },
69     { "gldft",                 "gpu", "api=gl,dit=true" },
70     { "glesdft",               "gpu", "api=gles,dit=true" },
71     { "gltestthreading",       "gpu", "api=gl,testThreading=true" },
72     { "gltestpersistentcache", "gpu", "api=gl,testPersistentCache=true" },
73     { "nullgl",                "gpu", "api=nullgl" },
74     { "angle_d3d11_es2",       "gpu", "api=angle_d3d11_es2" },
75     { "angle_d3d11_es3",       "gpu", "api=angle_d3d11_es3" },
76     { "angle_d3d9_es2",        "gpu", "api=angle_d3d9_es2" },
77     { "angle_d3d11_es2_msaa4", "gpu", "api=angle_d3d11_es2,samples=4" },
78     { "angle_d3d11_es2_msaa8", "gpu", "api=angle_d3d11_es2,samples=8" },
79     { "angle_d3d11_es3_msaa4", "gpu", "api=angle_d3d11_es3,samples=4" },
80     { "angle_d3d11_es3_msaa8", "gpu", "api=angle_d3d11_es3,samples=8" },
81     { "angle_gl_es2",          "gpu", "api=angle_gl_es2" },
82     { "angle_gl_es3",          "gpu", "api=angle_gl_es3" },
83     { "angle_gl_es2_msaa8",    "gpu", "api=angle_gl_es2,samples=8" },
84     { "angle_gl_es3_msaa8",    "gpu", "api=angle_gl_es3,samples=8" },
85     { "commandbuffer",         "gpu", "api=commandbuffer" },
86     { "mock",                  "gpu", "api=mock" },
87 #ifdef SK_VULKAN
88     { "vk",                    "gpu", "api=vulkan" },
89     { "vknostencils",          "gpu", "api=vulkan,stencils=false" },
90     { "vk1010102",             "gpu", "api=vulkan,color=1010102" },
91     { "vksrgb",                "gpu", "api=vulkan,color=srgb" },
92     { "vkesrgb",               "gpu", "api=vulkan,color=esrgb" },
93     { "vknarrow",              "gpu", "api=vulkan,color=narrow" },
94     { "vkenarrow",             "gpu", "api=vulkan,color=enarrow" },
95     { "vkf16",                 "gpu", "api=vulkan,color=f16" },
96     { "vkmsaa4",               "gpu", "api=vulkan,samples=4" },
97     { "vkmsaa8",               "gpu", "api=vulkan,samples=8" },
98     { "vkbetex",               "gpu", "api=vulkan,surf=betex" },
99     { "vkbert",                "gpu", "api=vulkan,surf=bert" },
100     { "vktestpersistentcache", "gpu", "api=vulkan,testPersistentCache=true" },
101 #endif
102 #ifdef SK_METAL
103     { "mtl",                   "gpu", "api=metal" },
104     { "mtl1010102",            "gpu", "api=metal,color=1010102" },
105     { "mtlmsaa4",              "gpu", "api=metal,samples=4" },
106     { "mtlmsaa8",              "gpu", "api=metal,samples=8" },
107 #endif
108 };
109 // clang-format on
110 
111 static const char configHelp[] =
112     "Options: 565 8888 srgb f16 nonrendering null pdf pdfa skp pipe svg xps";
113 
config_help_fn()114 static const char* config_help_fn() {
115     static SkString helpString;
116     helpString.set(configHelp);
117     for (const auto& config : gPredefinedConfigs) {
118         helpString.appendf(" %s", config.predefinedConfig);
119     }
120     helpString.append(" or use extended form 'backend[option=value,...]'.\n");
121     return helpString.c_str();
122 }
123 
124 static const char configExtendedHelp[] =
125         "Extended form: 'backend(option=value,...)'\n\n"
126         "Possible backends and options:\n"
127         "\n"
128         "gpu[api=string,color=string,dit=bool,nvpr=bool,inst=bool,samples=int]\n"
129         "\tapi\ttype: string\trequired\n"
130         "\t    Select graphics API to use with gpu backend.\n"
131         "\t    Options:\n"
132         "\t\tgl    \t\t\tUse OpenGL.\n"
133         "\t\tgles  \t\t\tUse OpenGL ES.\n"
134         "\t\tnullgl \t\t\tUse null OpenGL.\n"
135         "\t\tangle_d3d9_es2\t\tUse OpenGL ES2 on the ANGLE Direct3D9 backend.\n"
136         "\t\tangle_d3d11_es2\t\tUse OpenGL ES2 on the ANGLE Direct3D11 backend.\n"
137         "\t\tangle_d3d11_es3\t\tUse OpenGL ES3 on the ANGLE Direct3D11 backend.\n"
138         "\t\tangle_gl_es2\t\tUse OpenGL ES2 on the ANGLE OpenGL backend.\n"
139         "\t\tangle_gl_es3\t\tUse OpenGL ES3 on the ANGLE OpenGL backend.\n"
140         "\t\tcommandbuffer\t\tUse command buffer.\n"
141         "\t\tmock\t\t\tUse mock context.\n"
142 #ifdef SK_VULKAN
143         "\t\tvulkan\t\t\tUse Vulkan.\n"
144 #endif
145 #ifdef SK_METAL
146         "\t\tmetal\t\t\tUse Metal.\n"
147 #endif
148         "\tcolor\ttype: string\tdefault: 8888.\n"
149         "\t    Select framebuffer color format.\n"
150         "\t    Options:\n"
151         "\t\t8888\t\t\tLinear 8888.\n"
152         "\t\t888x\t\t\tLinear 888x.\n"
153         "\t\t4444\t\t\tLinear 4444.\n"
154         "\t\t565\t\t\tLinear 565.\n"
155         "\t\t1010102\t\t\tLinear 1010102.\n"
156         "\t\tsrgb\t\t\tsRGB 8888.\n"
157         "\t\tesrgb\t\t\tsRGB 16-bit floating point.\n"
158         "\t\tnarrow\t\t\tNarrow gamut 8888.\n"
159         "\t\tenarrow\t\t\tNarrow gamut 16-bit floating point.\n"
160         "\t\tf16\t\t\tLinearly blended 16-bit floating point.\n"
161         "\tdit\ttype: bool\tdefault: false.\n"
162         "\t    Use device independent text.\n"
163         "\tnvpr\ttype: bool\tdefault: false.\n"
164         "\t    Use NV_path_rendering OpenGL and OpenGL ES extension.\n"
165         "\tsamples\ttype: int\tdefault: 0.\n"
166         "\t    Use multisampling with N samples.\n"
167         "\tstencils\ttype: bool\tdefault: true.\n"
168         "\t    Allow the use of stencil buffers.\n"
169         "\ttestThreading\ttype: bool\tdefault: false.\n"
170         "\t    Run with and without worker threads, check that results match.\n"
171         "\ttestPersistentCache\ttype: bool\tdefault: false.\n"
172         "\t    Run using a pre-warmed GrContextOption::fPersistentCache.\n"
173         "\tsurf\ttype: string\tdefault: default.\n"
174         "\t    Controls the type of backing store for SkSurfaces.\n"
175         "\t    Options:\n"
176         "\t\tdefault\t\t\tA renderable texture created in Skia's resource cache.\n"
177         "\t\tbetex\t\t\tA wrapped backend texture.\n"
178         "\t\tbert\t\t\tA wrapped backend render target\n"
179         "\n"
180         "Predefined configs:\n\n"
181         // Help text for pre-defined configs is auto-generated from gPredefinedConfigs
182         ;
183 
config_extended_help_fn()184 static const char* config_extended_help_fn() {
185     static SkString helpString;
186     helpString.set(configExtendedHelp);
187     for (const auto& config : gPredefinedConfigs) {
188         helpString.appendf("\t%-10s\t= gpu(%s)\n", config.predefinedConfig, config.options);
189     }
190     return helpString.c_str();
191 }
192 
193 DEFINE_extended_string(config, defaultConfigs, config_help_fn(), config_extended_help_fn());
194 
SkCommandLineConfig(const SkString & tag,const SkString & backend,const SkTArray<SkString> & viaParts)195 SkCommandLineConfig::SkCommandLineConfig(const SkString& tag, const SkString& backend,
196                                          const SkTArray<SkString>& viaParts)
197         : fTag(tag)
198         , fBackend(backend)
199         , fViaParts(viaParts) {
200 }
~SkCommandLineConfig()201 SkCommandLineConfig::~SkCommandLineConfig() {
202 }
203 
parse_option_int(const SkString & value,int * outInt)204 static bool parse_option_int(const SkString& value, int* outInt) {
205     if (value.isEmpty()) {
206         return false;
207     }
208     char* endptr = nullptr;
209     long intValue = strtol(value.c_str(), &endptr, 10);
210     if (*endptr != '\0') {
211         return false;
212     }
213     *outInt = static_cast<int>(intValue);
214     return true;
215 }
parse_option_bool(const SkString & value,bool * outBool)216 static bool parse_option_bool(const SkString& value, bool* outBool) {
217     if (value.equals("true")) {
218         *outBool = true;
219         return true;
220     }
221     if (value.equals("false")) {
222         *outBool = false;
223         return true;
224     }
225     return false;
226 }
parse_option_gpu_api(const SkString & value,SkCommandLineConfigGpu::ContextType * outContextType)227 static bool parse_option_gpu_api(const SkString& value,
228                                  SkCommandLineConfigGpu::ContextType* outContextType) {
229     if (value.equals("gl")) {
230         *outContextType = GrContextFactory::kGL_ContextType;
231         return true;
232     }
233     if (value.equals("gles")) {
234         *outContextType = GrContextFactory::kGLES_ContextType;
235         return true;
236     }
237     if (value.equals("nullgl")) {
238         *outContextType = GrContextFactory::kNullGL_ContextType;
239         return true;
240     }
241     if (value.equals("angle_d3d9_es2")) {
242         *outContextType = GrContextFactory::kANGLE_D3D9_ES2_ContextType;
243         return true;
244     }
245     if (value.equals("angle_d3d11_es2")) {
246         *outContextType = GrContextFactory::kANGLE_D3D11_ES2_ContextType;
247         return true;
248     }
249     if (value.equals("angle_d3d11_es3")) {
250         *outContextType = GrContextFactory::kANGLE_D3D11_ES3_ContextType;
251         return true;
252     }
253     if (value.equals("angle_gl_es2")) {
254         *outContextType = GrContextFactory::kANGLE_GL_ES2_ContextType;
255         return true;
256     }
257     if (value.equals("angle_gl_es3")) {
258         *outContextType = GrContextFactory::kANGLE_GL_ES3_ContextType;
259         return true;
260     }
261     if (value.equals("commandbuffer")) {
262         *outContextType = GrContextFactory::kCommandBuffer_ContextType;
263         return true;
264     }
265     if (value.equals("mock")) {
266         *outContextType = GrContextFactory::kMock_ContextType;
267         return true;
268     }
269 #ifdef SK_VULKAN
270     if (value.equals("vulkan")) {
271         *outContextType = GrContextFactory::kVulkan_ContextType;
272         return true;
273     }
274 #endif
275 #ifdef SK_METAL
276     if (value.equals("metal")) {
277         *outContextType = GrContextFactory::kMetal_ContextType;
278         return true;
279     }
280 #endif
281     return false;
282 }
283 
parse_option_gpu_color(const SkString & value,SkColorType * outColorType,SkAlphaType * alphaType,sk_sp<SkColorSpace> * outColorSpace)284 static bool parse_option_gpu_color(const SkString& value,
285                                    SkColorType* outColorType,
286                                    SkAlphaType* alphaType,
287                                    sk_sp<SkColorSpace>* outColorSpace) {
288     // We always use premul unless the color type is 565.
289     *alphaType = kPremul_SkAlphaType;
290 
291     if (value.equals("8888")) {
292         *outColorType = kRGBA_8888_SkColorType;
293         *outColorSpace = nullptr;
294     } else if (value.equals("888x")) {
295         *outColorType = kRGB_888x_SkColorType;
296         *outColorSpace = nullptr;
297     } else if (value.equals("4444")) {
298         *outColorType = kARGB_4444_SkColorType;
299         *outColorSpace = nullptr;
300     } else if (value.equals("565")) {
301         *outColorType = kRGB_565_SkColorType;
302         *alphaType = kOpaque_SkAlphaType;
303         *outColorSpace = nullptr;
304     } else if (value.equals("1010102")) {
305         *outColorType = kRGBA_1010102_SkColorType;
306         *outColorSpace = nullptr;
307     } else if (value.equals("srgb")) {
308         *outColorType = kRGBA_8888_SkColorType;
309         *outColorSpace = SkColorSpace::MakeSRGB();
310     } else if (value.equals("p3")) {
311         *outColorType = kRGBA_8888_SkColorType;
312         *outColorSpace = SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB, SkNamedGamut::kDCIP3);
313     } else if (value.equals("esrgb")) {
314         *outColorType = kRGBA_F16_SkColorType;
315         *outColorSpace = SkColorSpace::MakeSRGB();
316     } else if (value.equals("narrow") || value.equals("enarrow")) {
317         *outColorType = value.equals("narrow") ? kRGBA_8888_SkColorType : kRGBA_F16_SkColorType;
318         *outColorSpace = SkColorSpace::MakeRGB(SkNamedTransferFn::k2Dot2, gNarrow_toXYZD50);
319     } else if (value.equals("f16")) {
320         *outColorType = kRGBA_F16_SkColorType;
321         *outColorSpace = SkColorSpace::MakeSRGBLinear();
322     } else {
323         return false;
324     }
325     return true;
326 }
327 
parse_option_gpu_surf_type(const SkString & value,SkCommandLineConfigGpu::SurfType * surfType)328 static bool parse_option_gpu_surf_type(const SkString& value,
329                                        SkCommandLineConfigGpu::SurfType* surfType) {
330     if (value.equals("default")) {
331         *surfType = SkCommandLineConfigGpu::SurfType::kDefault;
332         return true;
333     }
334     if (value.equals("betex")) {
335         *surfType = SkCommandLineConfigGpu::SurfType::kBackendTexture;
336         return true;
337     }
338     if (value.equals("bert")) {
339         *surfType = SkCommandLineConfigGpu::SurfType::kBackendRenderTarget;
340         return true;
341     }
342     return false;
343 }
344 
345 // Extended options take form --config item[key1=value1,key2=value2,...]
346 // Example: --config gpu[api=gl,color=8888]
347 class ExtendedOptions {
348 public:
ExtendedOptions(const SkString & optionsString,bool * outParseSucceeded)349     ExtendedOptions(const SkString& optionsString, bool* outParseSucceeded) {
350         SkTArray<SkString> optionParts;
351         SkStrSplit(optionsString.c_str(), ",", kStrict_SkStrSplitMode, &optionParts);
352         for (int i = 0; i < optionParts.count(); ++i) {
353             SkTArray<SkString> keyValueParts;
354             SkStrSplit(optionParts[i].c_str(), "=", kStrict_SkStrSplitMode, &keyValueParts);
355             if (keyValueParts.count() != 2) {
356                 *outParseSucceeded = false;
357                 return;
358             }
359             const SkString& key = keyValueParts[0];
360             const SkString& value = keyValueParts[1];
361             if (fOptionsMap.find(key) == nullptr) {
362                 fOptionsMap.set(key, value);
363             } else {
364                 // Duplicate values are not allowed.
365                 *outParseSucceeded = false;
366                 return;
367             }
368         }
369         *outParseSucceeded = true;
370     }
371 
get_option_gpu_color(const char * optionKey,SkColorType * outColorType,SkAlphaType * alphaType,sk_sp<SkColorSpace> * outColorSpace,bool optional=true) const372     bool get_option_gpu_color(const char* optionKey,
373                               SkColorType* outColorType,
374                               SkAlphaType* alphaType,
375                               sk_sp<SkColorSpace>* outColorSpace,
376                               bool optional = true) const {
377         SkString* optionValue = fOptionsMap.find(SkString(optionKey));
378         if (optionValue == nullptr) {
379             return optional;
380         }
381         return parse_option_gpu_color(*optionValue, outColorType, alphaType, outColorSpace);
382     }
383 
get_option_gpu_api(const char * optionKey,SkCommandLineConfigGpu::ContextType * outContextType,bool optional=true) const384     bool get_option_gpu_api(const char* optionKey,
385                             SkCommandLineConfigGpu::ContextType* outContextType,
386                             bool optional = true) const {
387         SkString* optionValue = fOptionsMap.find(SkString(optionKey));
388         if (optionValue == nullptr) {
389             return optional;
390         }
391         return parse_option_gpu_api(*optionValue, outContextType);
392     }
393 
get_option_gpu_surf_type(const char * optionKey,SkCommandLineConfigGpu::SurfType * outSurfType,bool optional=true) const394     bool get_option_gpu_surf_type(const char* optionKey,
395                                   SkCommandLineConfigGpu::SurfType* outSurfType,
396                                   bool optional = true) const {
397         SkString* optionValue = fOptionsMap.find(SkString(optionKey));
398         if (optionValue == nullptr) {
399             return optional;
400         }
401         return parse_option_gpu_surf_type(*optionValue, outSurfType);
402     }
403 
get_option_int(const char * optionKey,int * outInt,bool optional=true) const404     bool get_option_int(const char* optionKey, int* outInt, bool optional = true) const {
405         SkString* optionValue = fOptionsMap.find(SkString(optionKey));
406         if (optionValue == nullptr) {
407             return optional;
408         }
409         return parse_option_int(*optionValue, outInt);
410     }
411 
get_option_bool(const char * optionKey,bool * outBool,bool optional=true) const412     bool get_option_bool(const char* optionKey, bool* outBool, bool optional = true) const {
413         SkString* optionValue = fOptionsMap.find(SkString(optionKey));
414         if (optionValue == nullptr) {
415             return optional;
416         }
417         return parse_option_bool(*optionValue, outBool);
418     }
419 
420 private:
421     SkTHashMap<SkString, SkString> fOptionsMap;
422 };
423 
SkCommandLineConfigGpu(const SkString & tag,const SkTArray<SkString> & viaParts,ContextType contextType,bool useNVPR,bool useDIText,int samples,SkColorType colorType,SkAlphaType alphaType,sk_sp<SkColorSpace> colorSpace,bool useStencilBuffers,bool testThreading,bool testPersistentCache,SurfType surfType)424 SkCommandLineConfigGpu::SkCommandLineConfigGpu(
425         const SkString& tag, const SkTArray<SkString>& viaParts, ContextType contextType,
426         bool useNVPR, bool useDIText, int samples, SkColorType colorType, SkAlphaType alphaType,
427         sk_sp<SkColorSpace> colorSpace, bool useStencilBuffers, bool testThreading,
428         bool testPersistentCache, SurfType surfType)
429         : SkCommandLineConfig(tag, SkString("gpu"), viaParts)
430         , fContextType(contextType)
431         , fContextOverrides(ContextOverrides::kNone)
432         , fUseDIText(useDIText)
433         , fSamples(samples)
434         , fColorType(colorType)
435         , fAlphaType(alphaType)
436         , fColorSpace(std::move(colorSpace))
437         , fTestThreading(testThreading)
438         , fTestPersistentCache(testPersistentCache)
439         , fSurfType(surfType) {
440     if (useNVPR) {
441         fContextOverrides |= ContextOverrides::kRequireNVPRSupport;
442     } else {
443         // We don't disable NVPR for instanced configs. Otherwise the caps wouldn't use mixed
444         // samples and we couldn't test the mixed samples backend for simple shapes.
445         fContextOverrides |= ContextOverrides::kDisableNVPR;
446     }
447     if (!useStencilBuffers) {
448         fContextOverrides |= ContextOverrides::kAvoidStencilBuffers;
449     }
450 }
451 
parse_command_line_config_gpu(const SkString & tag,const SkTArray<SkString> & vias,const SkString & options)452 SkCommandLineConfigGpu* parse_command_line_config_gpu(const SkString& tag,
453                                                       const SkTArray<SkString>& vias,
454                                                       const SkString& options) {
455     // Defaults for GPU backend.
456     SkCommandLineConfigGpu::ContextType contextType = GrContextFactory::kGL_ContextType;
457     bool useNVPR = false;
458     bool useDIText = false;
459     int samples = 1;
460     SkColorType colorType = kRGBA_8888_SkColorType;
461     SkAlphaType alphaType = kPremul_SkAlphaType;
462     sk_sp<SkColorSpace> colorSpace = nullptr;
463     bool useStencils = true;
464     bool testThreading = false;
465     bool testPersistentCache = false;
466     SkCommandLineConfigGpu::SurfType surfType = SkCommandLineConfigGpu::SurfType::kDefault;
467 
468     bool parseSucceeded = false;
469     ExtendedOptions extendedOptions(options, &parseSucceeded);
470     if (!parseSucceeded) {
471         return nullptr;
472     }
473 
474     bool validOptions =
475             extendedOptions.get_option_gpu_api("api", &contextType, false) &&
476             extendedOptions.get_option_bool("nvpr", &useNVPR) &&
477             extendedOptions.get_option_bool("dit", &useDIText) &&
478             extendedOptions.get_option_int("samples", &samples) &&
479             extendedOptions.get_option_gpu_color("color", &colorType, &alphaType, &colorSpace) &&
480             extendedOptions.get_option_bool("stencils", &useStencils) &&
481             extendedOptions.get_option_bool("testThreading", &testThreading) &&
482             extendedOptions.get_option_bool("testPersistentCache", &testPersistentCache) &&
483             extendedOptions.get_option_gpu_surf_type("surf", &surfType);
484 
485     // testing threading and the persistent cache are mutually exclusive.
486     if (!validOptions || (testThreading && testPersistentCache)) {
487         return nullptr;
488     }
489 
490     return new SkCommandLineConfigGpu(tag, vias, contextType, useNVPR, useDIText, samples,
491                                       colorType, alphaType, colorSpace, useStencils, testThreading,
492                                       testPersistentCache, surfType);
493 }
494 
SkCommandLineConfigSvg(const SkString & tag,const SkTArray<SkString> & viaParts,int pageIndex)495 SkCommandLineConfigSvg::SkCommandLineConfigSvg(const SkString& tag,
496                                                const SkTArray<SkString>& viaParts, int pageIndex)
497         : SkCommandLineConfig(tag, SkString("svg"), viaParts), fPageIndex(pageIndex) {}
498 
parse_command_line_config_svg(const SkString & tag,const SkTArray<SkString> & vias,const SkString & options)499 SkCommandLineConfigSvg* parse_command_line_config_svg(const SkString& tag,
500                                                       const SkTArray<SkString>& vias,
501                                                       const SkString& options) {
502     // Defaults for SVG backend.
503     int pageIndex = 0;
504 
505     bool parseSucceeded = false;
506     ExtendedOptions extendedOptions(options, &parseSucceeded);
507     if (!parseSucceeded) {
508         return nullptr;
509     }
510 
511     bool validOptions = extendedOptions.get_option_int("page", &pageIndex);
512 
513     if (!validOptions) {
514         return nullptr;
515     }
516 
517     return new SkCommandLineConfigSvg(tag, vias, pageIndex);
518 }
519 
ParseConfigs(const SkCommandLineFlags::StringArray & configs,SkCommandLineConfigArray * outResult)520 void ParseConfigs(const SkCommandLineFlags::StringArray& configs,
521                   SkCommandLineConfigArray* outResult) {
522     outResult->reset();
523     for (int i = 0; i < configs.count(); ++i) {
524         SkString extendedBackend;
525         SkString extendedOptions;
526         SkString simpleBackend;
527         SkTArray<SkString> vias;
528 
529         SkString tag(configs[i]);
530         SkTArray<SkString> parts;
531         SkStrSplit(tag.c_str(), "[", kStrict_SkStrSplitMode, &parts);
532         if (parts.count() == 2) {
533             SkTArray<SkString> parts2;
534             SkStrSplit(parts[1].c_str(), "]", kStrict_SkStrSplitMode, &parts2);
535             if (parts2.count() == 2 && parts2[1].isEmpty()) {
536                 SkStrSplit(parts[0].c_str(), "-", kStrict_SkStrSplitMode, &vias);
537                 if (vias.count()) {
538                     extendedBackend = vias[vias.count() - 1];
539                     vias.pop_back();
540                 } else {
541                     extendedBackend = parts[0];
542                 }
543                 extendedOptions = parts2[0];
544                 simpleBackend.printf("%s[%s]", extendedBackend.c_str(), extendedOptions.c_str());
545             }
546         }
547 
548         if (extendedBackend.isEmpty()) {
549             simpleBackend = tag;
550             SkStrSplit(tag.c_str(), "-", kStrict_SkStrSplitMode, &vias);
551             if (vias.count()) {
552                 simpleBackend = vias[vias.count() - 1];
553                 vias.pop_back();
554             }
555             for (auto& predefinedConfig : gPredefinedConfigs) {
556                 if (simpleBackend.equals(predefinedConfig.predefinedConfig)) {
557                     extendedBackend = predefinedConfig.backend;
558                     extendedOptions = predefinedConfig.options;
559                     break;
560                 }
561             }
562         }
563         SkCommandLineConfig* parsedConfig = nullptr;
564         if (extendedBackend.equals("gpu")) {
565             parsedConfig = parse_command_line_config_gpu(tag, vias, extendedOptions);
566         }
567         if (extendedBackend.equals("svg")) {
568             parsedConfig = parse_command_line_config_svg(tag, vias, extendedOptions);
569         }
570         if (!parsedConfig) {
571             parsedConfig = new SkCommandLineConfig(tag, simpleBackend, vias);
572         }
573         outResult->emplace_back(parsedConfig);
574     }
575 }
576