blob: 447c56d5111528479ae5ac45e2a4d10f897e8adf [file] [log] [blame]
kkinnunen3e980c32015-12-23 01:33:00 -08001/*
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
10#include <stdlib.h>
11
bsalomon3724e572016-03-30 18:56:19 -070012#if SK_SUPPORT_GPU
13using sk_gpu_test::GrContextFactory;
14#endif
15
kkinnunen3e980c32015-12-23 01:33:00 -080016static const char defaultConfigs[] =
17 "565 8888 gpu nonrendering"
18#if SK_ANGLE
19#ifdef SK_BUILD_FOR_WIN
20 " angle"
21#endif
22#endif
23#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
24 " hwui"
25#endif
26 ;
27
28static const char configHelp[] =
bsalomonb8797bb2016-04-05 08:49:38 -070029 "Options: 565 8888 debug gpu gl gpudebug gpudft gpunull "
30 "msaa16 msaa4 glmsaa4 gpuf16 gpusrgb glsrgb nonrendering null nullgpu "
31 "nvpr16 nvpr4 nvprdit16 nvprdit4 glnvpr4 glnvprdit4 pdf skp svg xps"
csmartdaltone0d36292016-07-29 08:14:20 -070032 "glinst glinst4 glinstdit4 glinst16 glinstdit16 esinst esinst4 esinsdit4"
kkinnunen3e980c32015-12-23 01:33:00 -080033#if SK_ANGLE
34#ifdef SK_BUILD_FOR_WIN
brianosman744898a2016-03-01 13:44:28 -080035 " angle"
kkinnunen3e980c32015-12-23 01:33:00 -080036#endif
37 " angle-gl"
38#endif
39#if SK_COMMAND_BUFFER
40 " commandbuffer"
41#endif
42#if SK_MESA
43 " mesa"
44#endif
45#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
46 " hwui"
47#endif
bsalomondc0fcd42016-04-11 14:21:33 -070048#ifdef SK_VULKAN
brianosmana56800a2016-05-23 10:11:07 -070049 " vk vksrgb"
bsalomondc0fcd42016-04-11 14:21:33 -070050#endif
kkinnunen3e980c32015-12-23 01:33:00 -080051 " or use extended form 'backend(option=value,...)'.\n";
52
53static const char configExtendedHelp[] =
54 "Extended form: 'backend(option=value,...)'\n\n"
55 "Possible backends and options:\n"
56#if SK_SUPPORT_GPU
57 "\n"
csmartdaltone0d36292016-07-29 08:14:20 -070058 "gpu(api=string,color=string,dit=bool,nvpr=bool,inst=bool,samples=int)\tGPU backend\n"
kkinnunen3e980c32015-12-23 01:33:00 -080059 "\tapi\ttype: string\tdefault: native.\n"
60 "\t Select graphics API to use with gpu backend.\n"
61 "\t Options:\n"
62 "\t\tnative\t\t\tUse platform default OpenGL or OpenGL ES backend.\n"
63 "\t\tgl \t\t\tUse OpenGL.\n"
64 "\t\tgles \t\t\tUse OpenGL ES.\n"
65 "\t\tdebug \t\t\tUse debug OpenGL.\n"
66 "\t\tnull \t\t\tUse null OpenGL.\n"
67#if SK_ANGLE
68#ifdef SK_BUILD_FOR_WIN
69 "\t\tangle\t\t\tUse ANGLE DirectX.\n"
70#endif
71 "\t\tangle-gl\t\t\tUse ANGLE OpenGL.\n"
72#endif
73#if SK_COMMAND_BUFFER
74 "\t\tcommandbuffer\t\tUse command buffer.\n"
75#endif
76#if SK_MESA
77 "\t\tmesa\t\t\tUse MESA.\n"
78#endif
bsalomondc0fcd42016-04-11 14:21:33 -070079#ifdef SK_VULKAN
80 "\t\tvulkan\t\t\tUse Vulkan.\n"
81#endif
brianosmand93c1202016-03-10 07:49:08 -080082 "\tcolor\ttype: string\tdefault: 8888.\n"
83 "\t Select framebuffer color format.\n"
84 "\t Options:\n"
85 "\t\t8888\t\t\tLinear 8888.\n"
86 "\t\tf16 \t\t\tLinear 16-bit floating point.\n"
87 "\t\tsrgb\t\t\tsRGB 8888.\n"
kkinnunen3e980c32015-12-23 01:33:00 -080088 "\tdit\ttype: bool\tdefault: false.\n"
89 "\t Use device independent text.\n"
90 "\tnvpr\ttype: bool\tdefault: false.\n"
91 "\t Use NV_path_rendering OpenGL and OpenGL ES extension.\n"
92 "\tsamples\ttype: int\tdefault: 0.\n"
93 "\t Use multisampling with N samples.\n"
94 "\n"
95 "Predefined configs:\n\n"
cdaltonc28afdb2016-03-29 20:05:07 -070096 "\tgpu \t= gpu()\n"
bsalomonb8797bb2016-04-05 08:49:38 -070097 "\tgl \t= gpu(api=gl)\n"
cdaltonc28afdb2016-03-29 20:05:07 -070098 "\tmsaa4 \t= gpu(samples=4)\n"
bsalomonb8797bb2016-04-05 08:49:38 -070099 "\tglmsaa4 \t= gpu(api=gl,samples=4)\n"
cdaltonc28afdb2016-03-29 20:05:07 -0700100 "\tmsaa16 \t= gpu(samples=16)\n"
101 "\tnvpr4 \t= gpu(nvpr=true,samples=4)\n"
bsalomonb8797bb2016-04-05 08:49:38 -0700102 "\tglnvpr4 \t= gpu(api=gl,nvpr=true,samples=4)\n"
cdaltonc28afdb2016-03-29 20:05:07 -0700103 "\tnvpr16 \t= gpu(nvpr=true,samples=16)\n"
104 "\tnvprdit4 \t= gpu(nvpr=true,samples=4,dit=true)\n"
bsalomonb8797bb2016-04-05 08:49:38 -0700105 "\tglnvprdit4\t= gpu(api=gl,nvpr=true,samples=4,dit=true)\n"
cdaltonc28afdb2016-03-29 20:05:07 -0700106 "\tnvprdit16 \t= gpu(nvpr=true,samples=16,dit=true)\n"
brianosmand93c1202016-03-10 07:49:08 -0800107 "\tgpuf16 \t= gpu(color=f16)\n"
108 "\tgpusrgb \t= gpu(color=srgb)\n"
bsalomonb8797bb2016-04-05 08:49:38 -0700109 "\tglsrgb \t= gpu(api=gl,color=srgb)\n"
kkinnunen3e980c32015-12-23 01:33:00 -0800110 "\tgpudft \t= gpu(dit=true)\n"
111 "\tgpudebug \t= gpu(api=debug)\n"
112 "\tgpunull \t= gpu(api=null)\n"
113 "\tdebug \t= gpu(api=debug)\n"
114 "\tnullgpu \t= gpu(api=null)\n"
115#if SK_ANGLE
116#ifdef SK_BUILD_FOR_WIN
117 "\tangle \t= gpu(api=angle)\n"
118#endif
119 "\tangle-gl \t= gpu(api=angle-gl)\n"
120#endif
121#if SK_COMMAND_BUFFER
122 "\tcommandbuffer\t= gpu(api=commandbuffer)\n"
123#endif
124#if SK_MESA
125 "\tmesa \t= gpu(api=mesa)\n"
126#endif
bsalomondc0fcd42016-04-11 14:21:33 -0700127#ifdef SK_VULKAN
brianosmana56800a2016-05-23 10:11:07 -0700128 "\tvk \t= gpu(api=vulkan)\n"
129 "\tvksrgb \t= gpu(api=vulkan,color=srgb)\n"
egdanielbf63e612016-08-17 06:26:16 -0700130 "\tvkmsaa4 \t= gpu(api=gl,samples=4)\n"
131 "\tvkmsaa16 \t= gpu(api=gl,samples=16)\n"
bsalomondc0fcd42016-04-11 14:21:33 -0700132#endif
kkinnunen3e980c32015-12-23 01:33:00 -0800133#endif
134 ;
135
136DEFINE_extended_string(config, defaultConfigs, configHelp, configExtendedHelp);
137
138static const struct {
139 const char* predefinedConfig;
140 const char* backend;
141 const char* options;
142} gPredefinedConfigs[] = {
143#if SK_SUPPORT_GPU
csmartdaltone0d36292016-07-29 08:14:20 -0700144 { "gpu", "gpu", "" },
145 { "gl", "gpu", "api=gl" },
146 { "msaa4", "gpu", "samples=4" },
147 { "glmsaa4", "gpu", "api=gl,samples=4" },
148 { "msaa16", "gpu", "samples=16" },
149 { "nvpr4", "gpu", "nvpr=true,samples=4" },
150 { "glnvpr4", "gpu", "api=gl,nvpr=true,samples=4" },
151 { "nvpr16", "gpu", "nvpr=true,samples=16" },
152 { "nvprdit4", "gpu", "nvpr=true,samples=4,dit=true" },
153 { "glnvprdit4", "gpu", "api=gl,nvpr=true,samples=4,dit=true" },
154 { "nvprdit16", "gpu", "nvpr=true,samples=16,dit=true" },
155 { "glinst", "gpu", "api=gl,inst=true" },
156 { "glinst4", "gpu", "api=gl,inst=true,samples=4" },
157 { "glinstdit4", "gpu", "api=gl,inst=true,samples=4,dit=true" },
158 { "glinst16", "gpu", "api=gl,inst=true,samples=16" },
159 { "glinstdit16", "gpu", "api=gl,inst=true,samples=16,dit=true" },
160 { "esinst", "gpu", "api=gles,inst=true" },
161 { "esinst4", "gpu", "api=gles,inst=true,samples=4" },
162 { "esinstdit4", "gpu", "api=gles,inst=true,samples=4,dit=true" },
163 { "gpuf16", "gpu", "color=f16" },
164 { "gpusrgb", "gpu", "color=srgb" },
165 { "glsrgb", "gpu", "api=gl,color=srgb" },
166 { "gpudft", "gpu", "dit=true" },
167 { "gpudebug", "gpu", "api=debug" },
168 { "gpunull", "gpu", "api=null" },
169 { "debug", "gpu", "api=debug" },
170 { "nullgpu", "gpu", "api=null" }
kkinnunen3e980c32015-12-23 01:33:00 -0800171#if SK_ANGLE
172#ifdef SK_BUILD_FOR_WIN
173 , { "angle", "gpu", "api=angle" }
174#endif
175 , { "angle-gl", "gpu", "api=angle-gl" }
176#endif
177#if SK_COMMAND_BUFFER
178 , { "commandbuffer", "gpu", "api=commandbuffer" }
179#endif
180#if SK_MESA
181 , { "mesa", "gpu", "api=mesa" }
182#endif
bsalomondc0fcd42016-04-11 14:21:33 -0700183#ifdef SK_VULKAN
egdanielbf63e612016-08-17 06:26:16 -0700184 , { "vk", "gpu", "api=vulkan" }
185 , { "vksrgb", "gpu", "api=vulkan,color=srgb" }
186 , { "vkmsaa4", "gpu", "api=vulkan,samples=4" }
187 , { "vkmsaa16", "gpu", "api=vulkan,samples=16" }
bsalomondc0fcd42016-04-11 14:21:33 -0700188#endif
189
kkinnunen3e980c32015-12-23 01:33:00 -0800190#else
191 { "", "", "" }
192#endif
193};
194
195SkCommandLineConfig::SkCommandLineConfig(const SkString& tag, const SkString& backend,
196 const SkTArray<SkString>& viaParts)
197 : fTag(tag)
198 , fBackend(backend)
199 , fViaParts(viaParts) {
200}
201SkCommandLineConfig::~SkCommandLineConfig() {
202}
203
204#if SK_SUPPORT_GPU
205SkCommandLineConfigGpu::SkCommandLineConfigGpu(
csmartdaltone0d36292016-07-29 08:14:20 -0700206 const SkString& tag, const SkTArray<SkString>& viaParts, ContextType contextType, bool useNVPR,
207 bool useInstanced, bool useDIText, int samples, SkColorType colorType,
208 sk_sp<SkColorSpace> colorSpace)
kkinnunen3e980c32015-12-23 01:33:00 -0800209 : SkCommandLineConfig(tag, SkString("gpu"), viaParts)
210 , fContextType(contextType)
211 , fUseNVPR(useNVPR)
csmartdaltone0d36292016-07-29 08:14:20 -0700212 , fUseInstanced(useInstanced)
kkinnunen3e980c32015-12-23 01:33:00 -0800213 , fUseDIText(useDIText)
brianosmand93c1202016-03-10 07:49:08 -0800214 , fSamples(samples)
215 , fColorType(colorType)
brianosmanb109b8c2016-06-16 13:03:24 -0700216 , fColorSpace(std::move(colorSpace)) {
kkinnunen3e980c32015-12-23 01:33:00 -0800217}
218static bool parse_option_int(const SkString& value, int* outInt) {
219 if (value.isEmpty()) {
220 return false;
221 }
222 char* endptr = nullptr;
223 long intValue = strtol(value.c_str(), &endptr, 10);
224 if (*endptr != '\0') {
225 return false;
226 }
227 *outInt = static_cast<int>(intValue);
228 return true;
229}
230static bool parse_option_bool(const SkString& value, bool* outBool) {
231 if (value.equals("true")) {
232 *outBool = true;
233 return true;
234 }
235 if (value.equals("false")) {
236 *outBool = false;
237 return true;
238 }
239 return false;
240}
241static bool parse_option_gpu_api(const SkString& value,
242 SkCommandLineConfigGpu::ContextType* outContextType) {
kkinnunen3e980c32015-12-23 01:33:00 -0800243 if (value.equals("gl")) {
bsalomon85b4b532016-04-05 11:06:27 -0700244 *outContextType = GrContextFactory::kGL_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800245 return true;
246 }
247 if (value.equals("gles")) {
bsalomon85b4b532016-04-05 11:06:27 -0700248 *outContextType = GrContextFactory::kGLES_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800249 return true;
250 }
251 if (value.equals("debug")) {
bsalomon85b4b532016-04-05 11:06:27 -0700252 *outContextType = GrContextFactory::kDebugGL_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800253 return true;
254 }
255 if (value.equals("null")) {
bsalomon85b4b532016-04-05 11:06:27 -0700256 *outContextType = GrContextFactory::kNullGL_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800257 return true;
258 }
259#if SK_ANGLE
260#ifdef SK_BUILD_FOR_WIN
261 if (value.equals("angle")) {
bsalomon85b4b532016-04-05 11:06:27 -0700262 *outContextType = GrContextFactory::kANGLE_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800263 return true;
264 }
265#endif
266 if (value.equals("angle-gl")) {
bsalomon85b4b532016-04-05 11:06:27 -0700267 *outContextType = GrContextFactory::kANGLE_GL_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800268 return true;
269 }
270#endif
271#if SK_COMMAND_BUFFER
272 if (value.equals("commandbuffer")) {
bsalomon85b4b532016-04-05 11:06:27 -0700273 *outContextType = GrContextFactory::kCommandBuffer_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800274 return true;
275 }
276#endif
277#if SK_MESA
278 if (value.equals("mesa")) {
bsalomon85b4b532016-04-05 11:06:27 -0700279 *outContextType = GrContextFactory::kMESA_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800280 return true;
281 }
282#endif
bsalomondc0fcd42016-04-11 14:21:33 -0700283#ifdef SK_VULKAN
284 if (value.equals("vulkan")) {
285 *outContextType = GrContextFactory::kVulkan_ContextType;
286 return true;
287 }
288#endif
kkinnunen3e980c32015-12-23 01:33:00 -0800289 return false;
290}
brianosmand93c1202016-03-10 07:49:08 -0800291static bool parse_option_gpu_color(const SkString& value,
292 SkColorType* outColorType,
brianosmanb109b8c2016-06-16 13:03:24 -0700293 sk_sp<SkColorSpace>* outColorSpace) {
brianosmand93c1202016-03-10 07:49:08 -0800294 if (value.equals("8888")) {
295 *outColorType = kN32_SkColorType;
brianosmanb109b8c2016-06-16 13:03:24 -0700296 *outColorSpace = nullptr;
brianosmand93c1202016-03-10 07:49:08 -0800297 return true;
298 }
299 if (value.equals("f16")) {
300 *outColorType = kRGBA_F16_SkColorType;
brianosman0e22eb82016-08-30 07:07:59 -0700301 *outColorSpace = SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named)->makeLinearGamma();
brianosmand93c1202016-03-10 07:49:08 -0800302 return true;
303 }
304 if (value.equals("srgb")) {
305 *outColorType = kN32_SkColorType;
brianosmanb109b8c2016-06-16 13:03:24 -0700306 *outColorSpace = SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named);
brianosmand93c1202016-03-10 07:49:08 -0800307 return true;
308 }
309 return false;
310}
kkinnunen3e980c32015-12-23 01:33:00 -0800311
312SkCommandLineConfigGpu* parse_command_line_config_gpu(const SkString& tag,
313 const SkTArray<SkString>& vias,
314 const SkString& options) {
315 // Defaults for GPU backend.
316 bool seenAPI = false;
bsalomon85b4b532016-04-05 11:06:27 -0700317 SkCommandLineConfigGpu::ContextType contextType = GrContextFactory::kNativeGL_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800318 bool seenUseNVPR = false;
319 bool useNVPR = false;
csmartdaltone0d36292016-07-29 08:14:20 -0700320 bool seenUseInstanced = false;
321 bool useInstanced = false;
kkinnunen3e980c32015-12-23 01:33:00 -0800322 bool seenUseDIText =false;
323 bool useDIText = false;
324 bool seenSamples = false;
325 int samples = 0;
brianosmand93c1202016-03-10 07:49:08 -0800326 bool seenColor = false;
327 SkColorType colorType = kN32_SkColorType;
brianosmanb109b8c2016-06-16 13:03:24 -0700328 sk_sp<SkColorSpace> colorSpace = nullptr;
kkinnunen3e980c32015-12-23 01:33:00 -0800329
330 SkTArray<SkString> optionParts;
331 SkStrSplit(options.c_str(), ",", kStrict_SkStrSplitMode, &optionParts);
332 for (int i = 0; i < optionParts.count(); ++i) {
333 SkTArray<SkString> keyValueParts;
334 SkStrSplit(optionParts[i].c_str(), "=", kStrict_SkStrSplitMode, &keyValueParts);
335 if (keyValueParts.count() != 2) {
336 return nullptr;
337 }
338 const SkString& key = keyValueParts[0];
339 const SkString& value = keyValueParts[1];
340 bool valueOk = false;
341 if (key.equals("api") && !seenAPI) {
342 valueOk = parse_option_gpu_api(value, &contextType);
343 seenAPI = true;
344 } else if (key.equals("nvpr") && !seenUseNVPR) {
345 valueOk = parse_option_bool(value, &useNVPR);
346 seenUseNVPR = true;
csmartdaltone0d36292016-07-29 08:14:20 -0700347 } else if (key.equals("inst") && !seenUseInstanced) {
348 valueOk = parse_option_bool(value, &useInstanced);
349 seenUseInstanced = true;
kkinnunen3e980c32015-12-23 01:33:00 -0800350 } else if (key.equals("dit") && !seenUseDIText) {
351 valueOk = parse_option_bool(value, &useDIText);
352 seenUseDIText = true;
353 } else if (key.equals("samples") && !seenSamples) {
354 valueOk = parse_option_int(value, &samples);
355 seenSamples = true;
brianosmand93c1202016-03-10 07:49:08 -0800356 } else if (key.equals("color") && !seenColor) {
brianosmanb109b8c2016-06-16 13:03:24 -0700357 valueOk = parse_option_gpu_color(value, &colorType, &colorSpace);
brianosmand93c1202016-03-10 07:49:08 -0800358 seenColor = true;
kkinnunen3e980c32015-12-23 01:33:00 -0800359 }
360 if (!valueOk) {
361 return nullptr;
362 }
363 }
csmartdaltone0d36292016-07-29 08:14:20 -0700364 return new SkCommandLineConfigGpu(tag, vias, contextType, useNVPR, useInstanced, useDIText,
365 samples, colorType, colorSpace);
kkinnunen3e980c32015-12-23 01:33:00 -0800366}
367#endif
368
369void ParseConfigs(const SkCommandLineFlags::StringArray& configs,
370 SkCommandLineConfigArray* outResult) {
371 outResult->reset();
372 for (int i = 0; i < configs.count(); ++i) {
373 SkString extendedBackend;
374 SkString extendedOptions;
375 SkString simpleBackend;
376 SkTArray<SkString> vias;
377
378 SkString tag(configs[i]);
379 SkTArray<SkString> parts;
380 SkStrSplit(tag.c_str(), "(", kStrict_SkStrSplitMode, &parts);
381 if (parts.count() == 2) {
382 SkTArray<SkString> parts2;
383 SkStrSplit(parts[1].c_str(), ")", kStrict_SkStrSplitMode, &parts2);
384 if (parts2.count() == 2 && parts2[1].isEmpty()) {
385 SkStrSplit(parts[0].c_str(), "-", kStrict_SkStrSplitMode, &vias);
386 if (vias.count()) {
387 extendedBackend = vias[vias.count() - 1];
388 vias.pop_back();
389 } else {
390 extendedBackend = parts[0];
391 }
392 extendedOptions = parts2[0];
393 simpleBackend.printf("%s(%s)", extendedBackend.c_str(), extendedOptions.c_str());
394 }
395 }
396
397 if (extendedBackend.isEmpty()) {
398 simpleBackend = tag;
399 SkStrSplit(tag.c_str(), "-", kStrict_SkStrSplitMode, &vias);
400 if (vias.count()) {
401 simpleBackend = vias[vias.count() - 1];
402 vias.pop_back();
403 }
404 // Note: no #if SK_ANGLE: this is a special rule in the via-tag grammar.
405 if (vias.count() && simpleBackend.equals("gl") &&
406 vias[vias.count() - 1].equals("angle")) {
407 simpleBackend = "angle-gl";
408 vias.pop_back();
409 }
410
411 for (auto& predefinedConfig : gPredefinedConfigs) {
412 if (simpleBackend.equals(predefinedConfig.predefinedConfig)) {
413 extendedBackend = predefinedConfig.backend;
414 extendedOptions = predefinedConfig.options;
415 break;
416 }
417 }
418 }
419 SkCommandLineConfig* parsedConfig = nullptr;
420#if SK_SUPPORT_GPU
421 if (extendedBackend.equals("gpu")) {
422 parsedConfig = parse_command_line_config_gpu(tag, vias, extendedOptions);
423 }
424#endif
425 if (!parsedConfig) {
426 parsedConfig = new SkCommandLineConfig(tag, simpleBackend, vias);
427 }
428 outResult->emplace_back(parsedConfig);
429 }
430}