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 "SkCommonFlagsConfig.h"
9 #include "SkImageInfo.h"
10
11 #include <stdlib.h>
12
13 #if SK_SUPPORT_GPU
14 using sk_gpu_test::GrContextFactory;
15 #endif
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 static const struct {
33 const char* predefinedConfig;
34 const char* backend;
35 const char* options;
36 } gPredefinedConfigs[] ={
37 #if SK_SUPPORT_GPU
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 { "glnvprdit4", "gpu", "api=gl,nvpr=true,samples=4,dit=true" },
46 { "glnvprdit8" , "gpu", "api=gl,nvpr=true,samples=8,dit=true" },
47 { "glesnvpr4", "gpu", "api=gles,nvpr=true,samples=4" },
48 { "glesnvprdit4", "gpu", "api=gles,nvpr=true,samples=4,dit=true" },
49 { "gl4444", "gpu", "api=gl,color=4444" },
50 { "gl565", "gpu", "api=gl,color=565" },
51 { "glf16", "gpu", "api=gl,color=f16" },
52 { "glsrgb", "gpu", "api=gl,color=srgb" },
53 { "glsrgbnl", "gpu", "api=gl,color=srgbnl" },
54 { "glesf16", "gpu", "api=gles,color=f16" },
55 { "glessrgb", "gpu", "api=gles,color=srgb" },
56 { "glessrgbnl", "gpu", "api=gles,color=srgbnl" },
57 { "glsrgb", "gpu", "api=gl,color=srgb" },
58 { "glwide", "gpu", "api=gl,color=f16_wide" },
59 { "glnarrow", "gpu", "api=gl,color=f16_narrow" },
60 { "glnostencils", "gpu", "api=gl,stencils=false" },
61 { "gles4444", "gpu", "api=gles,color=4444" },
62 { "glessrgb", "gpu", "api=gles,color=srgb" },
63 { "gleswide", "gpu", "api=gles,color=f16_wide" },
64 { "glesnarrow", "gpu", "api=gles,color=f16_narrow" },
65 { "gldft", "gpu", "api=gl,dit=true" },
66 { "glesdft", "gpu", "api=gles,dit=true" },
67 { "gltestthreading", "gpu", "api=gl,testThreading=true" },
68 { "debuggl", "gpu", "api=debuggl" },
69 { "nullgl", "gpu", "api=nullgl" },
70 { "angle_d3d11_es2", "gpu", "api=angle_d3d11_es2" },
71 { "angle_d3d11_es3", "gpu", "api=angle_d3d11_es3" },
72 { "angle_d3d9_es2", "gpu", "api=angle_d3d9_es2" },
73 { "angle_d3d11_es2_msaa4", "gpu", "api=angle_d3d11_es2,samples=4" },
74 { "angle_d3d11_es2_msaa8", "gpu", "api=angle_d3d11_es2,samples=8" },
75 { "angle_d3d11_es3_msaa4", "gpu", "api=angle_d3d11_es3,samples=4" },
76 { "angle_d3d11_es3_msaa8", "gpu", "api=angle_d3d11_es3,samples=8" },
77 { "angle_gl_es2", "gpu", "api=angle_gl_es2" },
78 { "angle_gl_es3", "gpu", "api=angle_gl_es3" },
79 { "commandbuffer", "gpu", "api=commandbuffer" },
80 { "mock", "gpu", "api=mock" }
81 #ifdef SK_VULKAN
82 ,{ "vk", "gpu", "api=vulkan" }
83 ,{ "vksrgb", "gpu", "api=vulkan,color=srgb" }
84 ,{ "vkwide", "gpu", "api=vulkan,color=f16_wide" }
85 ,{ "vkmsaa4", "gpu", "api=vulkan,samples=4" }
86 ,{ "vkmsaa8", "gpu", "api=vulkan,samples=8" }
87 #endif
88 #ifdef SK_METAL
89 ,{ "mtl", "gpu", "api=metal" }
90 ,{ "mtlsrgb", "gpu", "api=metal,color=srgb" }
91 ,{ "mtlwide", "gpu", "api=metal,color=f16_wide" }
92 ,{ "mtlmsaa4", "gpu", "api=metal,samples=4" }
93 ,{ "mtlmsaa8", "gpu", "api=metal,samples=8" }
94 #endif
95 #else
96 { "", "", "" }
97 #endif
98 };
99
100 static const char configHelp[] =
101 "Options: 565 8888 srgb f16 nonrendering null pdf pdfa skp pipe svg xps";
102
config_help_fn()103 static const char* config_help_fn() {
104 static SkString helpString;
105 helpString.set(configHelp);
106 for (const auto& config : gPredefinedConfigs) {
107 helpString.appendf(" %s", config.predefinedConfig);
108 }
109 helpString.append(" or use extended form 'backend[option=value,...]'.\n");
110 return helpString.c_str();
111 }
112
113 static const char configExtendedHelp[] =
114 "Extended form: 'backend(option=value,...)'\n\n"
115 "Possible backends and options:\n"
116 #if SK_SUPPORT_GPU
117 "\n"
118 "gpu[api=string,color=string,dit=bool,nvpr=bool,inst=bool,samples=int]\n"
119 "\tapi\ttype: string\trequired\n"
120 "\t Select graphics API to use with gpu backend.\n"
121 "\t Options:\n"
122 "\t\tgl \t\t\tUse OpenGL.\n"
123 "\t\tgles \t\t\tUse OpenGL ES.\n"
124 "\t\tdebuggl \t\t\tUse debug OpenGL.\n"
125 "\t\tnullgl \t\t\tUse null OpenGL.\n"
126 "\t\tangle_d3d9_es2\t\t\tUse OpenGL ES2 on the ANGLE Direct3D9 backend.\n"
127 "\t\tangle_d3d11_es2\t\t\tUse OpenGL ES2 on the ANGLE Direct3D11 backend.\n"
128 "\t\tangle_d3d11_es3\t\t\tUse OpenGL ES3 on the ANGLE Direct3D11 backend.\n"
129 "\t\tangle_gl_es2\t\t\tUse OpenGL ES2 on the ANGLE OpenGL backend.\n"
130 "\t\tangle_gl_es3\t\t\tUse OpenGL ES3 on the ANGLE OpenGL backend.\n"
131 "\t\tcommandbuffer\t\tUse command buffer.\n"
132 "\t\tmock\t\tUse mock context.\n"
133 #ifdef SK_VULKAN
134 "\t\tvulkan\t\t\tUse Vulkan.\n"
135 #endif
136 #ifdef SK_METAL
137 "\t\tmetal\t\t\tUse Metal.\n"
138 #endif
139 "\tcolor\ttype: string\tdefault: 8888.\n"
140 "\t Select framebuffer color format.\n"
141 "\t Options:\n"
142 "\t\t8888\t\t\tLinear 8888.\n"
143 "\t\t4444\t\t\tLinear 4444.\n"
144 "\t\t565\t\t\tLinear 565.\n"
145 "\t\tf16{_gamut}\t\tLinear 16-bit floating point.\n"
146 "\t\tsrgb{_gamut}\t\tsRGB 8888.\n"
147 "\t gamut\ttype: string\tdefault: srgb.\n"
148 "\t Select color gamut for f16 or sRGB format buffers.\n"
149 "\t Options:\n"
150 "\t\tsrgb\t\t\tsRGB gamut.\n"
151 "\t\twide\t\t\tWide Gamut RGB.\n"
152 "\tdit\ttype: bool\tdefault: false.\n"
153 "\t Use device independent text.\n"
154 "\tnvpr\ttype: bool\tdefault: false.\n"
155 "\t Use NV_path_rendering OpenGL and OpenGL ES extension.\n"
156 "\tsamples\ttype: int\tdefault: 0.\n"
157 "\t Use multisampling with N samples.\n"
158 "\tstencils\ttype: bool\tdefault: true.\n"
159 "\t Allow the use of stencil buffers.\n"
160 "\ttestThreading\ttype: bool\tdefault: false.\n"
161 "\t Run config with and without worker threads, check that results match.\n"
162 "\n"
163 "Predefined configs:\n\n"
164 // Help text for pre-defined configs is auto-generated from gPredefinedConfigs
165 #endif
166 ;
167
config_extended_help_fn()168 static const char* config_extended_help_fn() {
169 static SkString helpString;
170 helpString.set(configExtendedHelp);
171 for (const auto& config : gPredefinedConfigs) {
172 helpString.appendf("\t%-10s\t= gpu(%s)\n", config.predefinedConfig, config.options);
173 }
174 return helpString.c_str();
175 }
176
177 DEFINE_extended_string(config, defaultConfigs, config_help_fn(), config_extended_help_fn());
178
SkCommandLineConfig(const SkString & tag,const SkString & backend,const SkTArray<SkString> & viaParts)179 SkCommandLineConfig::SkCommandLineConfig(const SkString& tag, const SkString& backend,
180 const SkTArray<SkString>& viaParts)
181 : fTag(tag)
182 , fBackend(backend)
183 , fViaParts(viaParts) {
184 }
~SkCommandLineConfig()185 SkCommandLineConfig::~SkCommandLineConfig() {
186 }
187
188 #if SK_SUPPORT_GPU
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)189 SkCommandLineConfigGpu::SkCommandLineConfigGpu(
190 const SkString& tag, const SkTArray<SkString>& viaParts, ContextType contextType, bool useNVPR,
191 bool useDIText, int samples, SkColorType colorType, SkAlphaType alphaType,
192 sk_sp<SkColorSpace> colorSpace, bool useStencilBuffers, bool testThreading)
193 : SkCommandLineConfig(tag, SkString("gpu"), viaParts)
194 , fContextType(contextType)
195 , fContextOverrides(ContextOverrides::kNone)
196 , fUseDIText(useDIText)
197 , fSamples(samples)
198 , fColorType(colorType)
199 , fAlphaType(alphaType)
200 , fColorSpace(std::move(colorSpace))
201 , fTestThreading(testThreading) {
202 if (useNVPR) {
203 fContextOverrides |= ContextOverrides::kRequireNVPRSupport;
204 } else {
205 // We don't disable NVPR for instanced configs. Otherwise the caps wouldn't use mixed
206 // samples and we couldn't test the mixed samples backend for simple shapes.
207 fContextOverrides |= ContextOverrides::kDisableNVPR;
208 }
209 // Subtle logic: If the config has a color space attached, we're going to be rendering to sRGB,
210 // so we need that capability. In addition, to get the widest test coverage, we DO NOT require
211 // that we can disable sRGB decode. (That's for rendering sRGB sources to legacy surfaces).
212 //
213 // If the config doesn't have a color space attached, we're going to be rendering in legacy
214 // mode. In that case, we don't require sRGB capability and we defer to the client to decide on
215 // sRGB decode control.
216 if (fColorSpace) {
217 fContextOverrides |= ContextOverrides::kRequireSRGBSupport;
218 fContextOverrides |= ContextOverrides::kAllowSRGBWithoutDecodeControl;
219 }
220 if (!useStencilBuffers) {
221 fContextOverrides |= ContextOverrides::kAvoidStencilBuffers;
222 }
223 }
parse_option_int(const SkString & value,int * outInt)224 static bool parse_option_int(const SkString& value, int* outInt) {
225 if (value.isEmpty()) {
226 return false;
227 }
228 char* endptr = nullptr;
229 long intValue = strtol(value.c_str(), &endptr, 10);
230 if (*endptr != '\0') {
231 return false;
232 }
233 *outInt = static_cast<int>(intValue);
234 return true;
235 }
parse_option_bool(const SkString & value,bool * outBool)236 static bool parse_option_bool(const SkString& value, bool* outBool) {
237 if (value.equals("true")) {
238 *outBool = true;
239 return true;
240 }
241 if (value.equals("false")) {
242 *outBool = false;
243 return true;
244 }
245 return false;
246 }
parse_option_gpu_api(const SkString & value,SkCommandLineConfigGpu::ContextType * outContextType)247 static bool parse_option_gpu_api(const SkString& value,
248 SkCommandLineConfigGpu::ContextType* outContextType) {
249 if (value.equals("gl")) {
250 *outContextType = GrContextFactory::kGL_ContextType;
251 return true;
252 }
253 if (value.equals("gles")) {
254 *outContextType = GrContextFactory::kGLES_ContextType;
255 return true;
256 }
257 if (value.equals("debuggl")) {
258 *outContextType = GrContextFactory::kDebugGL_ContextType;
259 return true;
260 }
261 if (value.equals("nullgl")) {
262 *outContextType = GrContextFactory::kNullGL_ContextType;
263 return true;
264 }
265 if (value.equals("angle_d3d9_es2")) {
266 *outContextType = GrContextFactory::kANGLE_D3D9_ES2_ContextType;
267 return true;
268 }
269 if (value.equals("angle_d3d11_es2")) {
270 *outContextType = GrContextFactory::kANGLE_D3D11_ES2_ContextType;
271 return true;
272 }
273 if (value.equals("angle_d3d11_es3")) {
274 *outContextType = GrContextFactory::kANGLE_D3D11_ES3_ContextType;
275 return true;
276 }
277 if (value.equals("angle_gl_es2")) {
278 *outContextType = GrContextFactory::kANGLE_GL_ES2_ContextType;
279 return true;
280 }
281 if (value.equals("angle_gl_es3")) {
282 *outContextType = GrContextFactory::kANGLE_GL_ES3_ContextType;
283 return true;
284 }
285 if (value.equals("commandbuffer")) {
286 *outContextType = GrContextFactory::kCommandBuffer_ContextType;
287 return true;
288 }
289 if (value.equals("mock")) {
290 *outContextType = GrContextFactory::kMock_ContextType;
291 return true;
292 }
293 #ifdef SK_VULKAN
294 if (value.equals("vulkan")) {
295 *outContextType = GrContextFactory::kVulkan_ContextType;
296 return true;
297 }
298 #endif
299 #ifdef SK_METAL
300 if (value.equals("metal")) {
301 *outContextType = GrContextFactory::kMetal_ContextType;
302 return true;
303 }
304 #endif
305 return false;
306 }
parse_option_gpu_color(const SkString & value,SkColorType * outColorType,SkAlphaType * alphaType,sk_sp<SkColorSpace> * outColorSpace)307 static bool parse_option_gpu_color(const SkString& value,
308 SkColorType* outColorType,
309 SkAlphaType* alphaType,
310 sk_sp<SkColorSpace>* outColorSpace) {
311 // We always use premul unless the color type is 565.
312 *alphaType = kPremul_SkAlphaType;
313
314 if (value.equals("8888")) {
315 *outColorType = kRGBA_8888_SkColorType;
316 *outColorSpace = nullptr;
317 return true;
318 } else if (value.equals("4444")) {
319 *outColorType = kARGB_4444_SkColorType;
320 *outColorSpace = nullptr;
321 return true;
322 } else if (value.equals("565")) {
323 *outColorType = kRGB_565_SkColorType;
324 *alphaType = kOpaque_SkAlphaType;
325 *outColorSpace = nullptr;
326 return true;
327 }
328
329 SkTArray<SkString> commands;
330 SkStrSplit(value.c_str(), "_", &commands);
331 if (commands.count() < 1 || commands.count() > 2) {
332 return false;
333 }
334
335 const bool linearGamma = commands[0].equals("f16");
336 SkColorSpace::Gamut gamut = SkColorSpace::kSRGB_Gamut;
337 SkColorSpace::RenderTargetGamma gamma = linearGamma ? SkColorSpace::kLinear_RenderTargetGamma
338 : SkColorSpace::kSRGB_RenderTargetGamma;
339 *outColorSpace = SkColorSpace::MakeRGB(gamma, gamut);
340
341 if (commands.count() == 2) {
342 if (commands[1].equals("srgb")) {
343 // sRGB gamut (which is our default)
344 } else if (commands[1].equals("wide")) {
345 // WideGamut RGB
346 const float gWideGamutRGB_toXYZD50[]{
347 0.7161046f, 0.1009296f, 0.1471858f, // -> X
348 0.2581874f, 0.7249378f, 0.0168748f, // -> Y
349 0.0000000f, 0.0517813f, 0.7734287f, // -> Z
350 };
351 SkMatrix44 wideGamutRGBMatrix(SkMatrix44::kUninitialized_Constructor);
352 wideGamutRGBMatrix.set3x3RowMajorf(gWideGamutRGB_toXYZD50);
353 *outColorSpace = SkColorSpace::MakeRGB(gamma, wideGamutRGBMatrix);
354 } else if (commands[1].equals("narrow")) {
355 // NarrowGamut RGB (an artifically smaller than sRGB gamut)
356 SkColorSpacePrimaries primaries ={
357 0.54f, 0.33f, // Rx, Ry
358 0.33f, 0.50f, // Gx, Gy
359 0.25f, 0.20f, // Bx, By
360 0.3127f, 0.3290f, // Wx, Wy
361 };
362 SkMatrix44 narrowGamutRGBMatrix(SkMatrix44::kUninitialized_Constructor);
363 primaries.toXYZD50(&narrowGamutRGBMatrix);
364 *outColorSpace = SkColorSpace::MakeRGB(gamma, narrowGamutRGBMatrix);
365 } else {
366 // Unknown color gamut
367 return false;
368 }
369 }
370
371 // Now pick a color type
372 if (commands[0].equals("f16")) {
373 *outColorType = kRGBA_F16_SkColorType;
374 return true;
375 }
376 if (commands[0].equals("srgb") || commands[0].equals("srgbnl")) {
377 *outColorType = kRGBA_8888_SkColorType;
378 return true;
379 }
380 return false;
381 }
382
parse_command_line_config_gpu(const SkString & tag,const SkTArray<SkString> & vias,const SkString & options)383 SkCommandLineConfigGpu* parse_command_line_config_gpu(const SkString& tag,
384 const SkTArray<SkString>& vias,
385 const SkString& options) {
386 // Defaults for GPU backend.
387 bool seenAPI = false;
388 SkCommandLineConfigGpu::ContextType contextType = GrContextFactory::kGL_ContextType;
389 bool seenUseNVPR = false;
390 bool useNVPR = false;
391 bool seenUseDIText =false;
392 bool useDIText = false;
393 bool seenSamples = false;
394 int samples = 1;
395 bool seenColor = false;
396 SkColorType colorType = kRGBA_8888_SkColorType;
397 SkAlphaType alphaType = kPremul_SkAlphaType;
398 sk_sp<SkColorSpace> colorSpace = nullptr;
399 bool seenUseStencils = false;
400 bool useStencils = true;
401 bool seenTestThreading = false;
402 bool testThreading = false;
403
404 SkTArray<SkString> optionParts;
405 SkStrSplit(options.c_str(), ",", kStrict_SkStrSplitMode, &optionParts);
406 for (int i = 0; i < optionParts.count(); ++i) {
407 SkTArray<SkString> keyValueParts;
408 SkStrSplit(optionParts[i].c_str(), "=", kStrict_SkStrSplitMode, &keyValueParts);
409 if (keyValueParts.count() != 2) {
410 return nullptr;
411 }
412 const SkString& key = keyValueParts[0];
413 const SkString& value = keyValueParts[1];
414 bool valueOk = false;
415 if (key.equals("api") && !seenAPI) {
416 valueOk = parse_option_gpu_api(value, &contextType);
417 seenAPI = true;
418 } else if (key.equals("nvpr") && !seenUseNVPR) {
419 valueOk = parse_option_bool(value, &useNVPR);
420 seenUseNVPR = true;
421 } else if (key.equals("dit") && !seenUseDIText) {
422 valueOk = parse_option_bool(value, &useDIText);
423 seenUseDIText = true;
424 } else if (key.equals("samples") && !seenSamples) {
425 valueOk = parse_option_int(value, &samples);
426 seenSamples = true;
427 } else if (key.equals("color") && !seenColor) {
428 valueOk = parse_option_gpu_color(value, &colorType, &alphaType, &colorSpace);
429 seenColor = true;
430 } else if (key.equals("stencils") && !seenUseStencils) {
431 valueOk = parse_option_bool(value, &useStencils);
432 seenUseStencils = true;
433 } else if (key.equals("testThreading") && !seenTestThreading) {
434 valueOk = parse_option_bool(value, &testThreading);
435 seenTestThreading = true;
436 }
437 if (!valueOk) {
438 return nullptr;
439 }
440 }
441 if (!seenAPI) {
442 return nullptr;
443 }
444 return new SkCommandLineConfigGpu(tag, vias, contextType, useNVPR, useDIText,
445 samples, colorType, alphaType, colorSpace, useStencils,
446 testThreading);
447 }
448 #endif
449
ParseConfigs(const SkCommandLineFlags::StringArray & configs,SkCommandLineConfigArray * outResult)450 void ParseConfigs(const SkCommandLineFlags::StringArray& configs,
451 SkCommandLineConfigArray* outResult) {
452 outResult->reset();
453 for (int i = 0; i < configs.count(); ++i) {
454 SkString extendedBackend;
455 SkString extendedOptions;
456 SkString simpleBackend;
457 SkTArray<SkString> vias;
458
459 SkString tag(configs[i]);
460 SkTArray<SkString> parts;
461 SkStrSplit(tag.c_str(), "[", kStrict_SkStrSplitMode, &parts);
462 if (parts.count() == 2) {
463 SkTArray<SkString> parts2;
464 SkStrSplit(parts[1].c_str(), "]", kStrict_SkStrSplitMode, &parts2);
465 if (parts2.count() == 2 && parts2[1].isEmpty()) {
466 SkStrSplit(parts[0].c_str(), "-", kStrict_SkStrSplitMode, &vias);
467 if (vias.count()) {
468 extendedBackend = vias[vias.count() - 1];
469 vias.pop_back();
470 } else {
471 extendedBackend = parts[0];
472 }
473 extendedOptions = parts2[0];
474 simpleBackend.printf("%s[%s]", extendedBackend.c_str(), extendedOptions.c_str());
475 }
476 }
477
478 if (extendedBackend.isEmpty()) {
479 simpleBackend = tag;
480 SkStrSplit(tag.c_str(), "-", kStrict_SkStrSplitMode, &vias);
481 if (vias.count()) {
482 simpleBackend = vias[vias.count() - 1];
483 vias.pop_back();
484 }
485 for (auto& predefinedConfig : gPredefinedConfigs) {
486 if (simpleBackend.equals(predefinedConfig.predefinedConfig)) {
487 extendedBackend = predefinedConfig.backend;
488 extendedOptions = predefinedConfig.options;
489 break;
490 }
491 }
492 }
493 SkCommandLineConfig* parsedConfig = nullptr;
494 #if SK_SUPPORT_GPU
495 if (extendedBackend.equals("gpu")) {
496 parsedConfig = parse_command_line_config_gpu(tag, vias, extendedOptions);
497 }
498 #endif
499 if (!parsedConfig) {
500 parsedConfig = new SkCommandLineConfig(tag, simpleBackend, vias);
501 }
502 outResult->emplace_back(parsedConfig);
503 }
504 }
505