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