blob: 215fbdbfc00a6923fae705c7e633967487145c50 [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"
bsalomondc0fcd42016-04-11 14:21:33 -0700130#endif
kkinnunen3e980c32015-12-23 01:33:00 -0800131#endif
132 ;
133
134DEFINE_extended_string(config, defaultConfigs, configHelp, configExtendedHelp);
135
136static const struct {
137 const char* predefinedConfig;
138 const char* backend;
139 const char* options;
140} gPredefinedConfigs[] = {
141#if SK_SUPPORT_GPU
csmartdaltone0d36292016-07-29 08:14:20 -0700142 { "gpu", "gpu", "" },
143 { "gl", "gpu", "api=gl" },
144 { "msaa4", "gpu", "samples=4" },
145 { "glmsaa4", "gpu", "api=gl,samples=4" },
146 { "msaa16", "gpu", "samples=16" },
147 { "nvpr4", "gpu", "nvpr=true,samples=4" },
148 { "glnvpr4", "gpu", "api=gl,nvpr=true,samples=4" },
149 { "nvpr16", "gpu", "nvpr=true,samples=16" },
150 { "nvprdit4", "gpu", "nvpr=true,samples=4,dit=true" },
151 { "glnvprdit4", "gpu", "api=gl,nvpr=true,samples=4,dit=true" },
152 { "nvprdit16", "gpu", "nvpr=true,samples=16,dit=true" },
153 { "glinst", "gpu", "api=gl,inst=true" },
154 { "glinst4", "gpu", "api=gl,inst=true,samples=4" },
155 { "glinstdit4", "gpu", "api=gl,inst=true,samples=4,dit=true" },
156 { "glinst16", "gpu", "api=gl,inst=true,samples=16" },
157 { "glinstdit16", "gpu", "api=gl,inst=true,samples=16,dit=true" },
158 { "esinst", "gpu", "api=gles,inst=true" },
159 { "esinst4", "gpu", "api=gles,inst=true,samples=4" },
160 { "esinstdit4", "gpu", "api=gles,inst=true,samples=4,dit=true" },
161 { "gpuf16", "gpu", "color=f16" },
162 { "gpusrgb", "gpu", "color=srgb" },
163 { "glsrgb", "gpu", "api=gl,color=srgb" },
164 { "gpudft", "gpu", "dit=true" },
165 { "gpudebug", "gpu", "api=debug" },
166 { "gpunull", "gpu", "api=null" },
167 { "debug", "gpu", "api=debug" },
168 { "nullgpu", "gpu", "api=null" }
kkinnunen3e980c32015-12-23 01:33:00 -0800169#if SK_ANGLE
170#ifdef SK_BUILD_FOR_WIN
171 , { "angle", "gpu", "api=angle" }
172#endif
173 , { "angle-gl", "gpu", "api=angle-gl" }
174#endif
175#if SK_COMMAND_BUFFER
176 , { "commandbuffer", "gpu", "api=commandbuffer" }
177#endif
178#if SK_MESA
179 , { "mesa", "gpu", "api=mesa" }
180#endif
bsalomondc0fcd42016-04-11 14:21:33 -0700181#ifdef SK_VULKAN
182 , { "vk", "gpu", "api=vulkan" }
brianosmana56800a2016-05-23 10:11:07 -0700183 , { "vksrgb", "gpu", "api=vulkan,color=srgb" }
bsalomondc0fcd42016-04-11 14:21:33 -0700184#endif
185
kkinnunen3e980c32015-12-23 01:33:00 -0800186#else
187 { "", "", "" }
188#endif
189};
190
191SkCommandLineConfig::SkCommandLineConfig(const SkString& tag, const SkString& backend,
192 const SkTArray<SkString>& viaParts)
193 : fTag(tag)
194 , fBackend(backend)
195 , fViaParts(viaParts) {
196}
197SkCommandLineConfig::~SkCommandLineConfig() {
198}
199
200#if SK_SUPPORT_GPU
201SkCommandLineConfigGpu::SkCommandLineConfigGpu(
csmartdaltone0d36292016-07-29 08:14:20 -0700202 const SkString& tag, const SkTArray<SkString>& viaParts, ContextType contextType, bool useNVPR,
203 bool useInstanced, bool useDIText, int samples, SkColorType colorType,
204 sk_sp<SkColorSpace> colorSpace)
kkinnunen3e980c32015-12-23 01:33:00 -0800205 : SkCommandLineConfig(tag, SkString("gpu"), viaParts)
206 , fContextType(contextType)
207 , fUseNVPR(useNVPR)
csmartdaltone0d36292016-07-29 08:14:20 -0700208 , fUseInstanced(useInstanced)
kkinnunen3e980c32015-12-23 01:33:00 -0800209 , fUseDIText(useDIText)
brianosmand93c1202016-03-10 07:49:08 -0800210 , fSamples(samples)
211 , fColorType(colorType)
brianosmanb109b8c2016-06-16 13:03:24 -0700212 , fColorSpace(std::move(colorSpace)) {
kkinnunen3e980c32015-12-23 01:33:00 -0800213}
214static bool parse_option_int(const SkString& value, int* outInt) {
215 if (value.isEmpty()) {
216 return false;
217 }
218 char* endptr = nullptr;
219 long intValue = strtol(value.c_str(), &endptr, 10);
220 if (*endptr != '\0') {
221 return false;
222 }
223 *outInt = static_cast<int>(intValue);
224 return true;
225}
226static bool parse_option_bool(const SkString& value, bool* outBool) {
227 if (value.equals("true")) {
228 *outBool = true;
229 return true;
230 }
231 if (value.equals("false")) {
232 *outBool = false;
233 return true;
234 }
235 return false;
236}
237static bool parse_option_gpu_api(const SkString& value,
238 SkCommandLineConfigGpu::ContextType* outContextType) {
kkinnunen3e980c32015-12-23 01:33:00 -0800239 if (value.equals("gl")) {
bsalomon85b4b532016-04-05 11:06:27 -0700240 *outContextType = GrContextFactory::kGL_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800241 return true;
242 }
243 if (value.equals("gles")) {
bsalomon85b4b532016-04-05 11:06:27 -0700244 *outContextType = GrContextFactory::kGLES_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800245 return true;
246 }
247 if (value.equals("debug")) {
bsalomon85b4b532016-04-05 11:06:27 -0700248 *outContextType = GrContextFactory::kDebugGL_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800249 return true;
250 }
251 if (value.equals("null")) {
bsalomon85b4b532016-04-05 11:06:27 -0700252 *outContextType = GrContextFactory::kNullGL_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800253 return true;
254 }
255#if SK_ANGLE
256#ifdef SK_BUILD_FOR_WIN
257 if (value.equals("angle")) {
bsalomon85b4b532016-04-05 11:06:27 -0700258 *outContextType = GrContextFactory::kANGLE_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800259 return true;
260 }
261#endif
262 if (value.equals("angle-gl")) {
bsalomon85b4b532016-04-05 11:06:27 -0700263 *outContextType = GrContextFactory::kANGLE_GL_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800264 return true;
265 }
266#endif
267#if SK_COMMAND_BUFFER
268 if (value.equals("commandbuffer")) {
bsalomon85b4b532016-04-05 11:06:27 -0700269 *outContextType = GrContextFactory::kCommandBuffer_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800270 return true;
271 }
272#endif
273#if SK_MESA
274 if (value.equals("mesa")) {
bsalomon85b4b532016-04-05 11:06:27 -0700275 *outContextType = GrContextFactory::kMESA_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800276 return true;
277 }
278#endif
bsalomondc0fcd42016-04-11 14:21:33 -0700279#ifdef SK_VULKAN
280 if (value.equals("vulkan")) {
281 *outContextType = GrContextFactory::kVulkan_ContextType;
282 return true;
283 }
284#endif
kkinnunen3e980c32015-12-23 01:33:00 -0800285 return false;
286}
brianosmand93c1202016-03-10 07:49:08 -0800287static bool parse_option_gpu_color(const SkString& value,
288 SkColorType* outColorType,
brianosmanb109b8c2016-06-16 13:03:24 -0700289 sk_sp<SkColorSpace>* outColorSpace) {
brianosmand93c1202016-03-10 07:49:08 -0800290 if (value.equals("8888")) {
291 *outColorType = kN32_SkColorType;
brianosmanb109b8c2016-06-16 13:03:24 -0700292 *outColorSpace = nullptr;
brianosmand93c1202016-03-10 07:49:08 -0800293 return true;
294 }
295 if (value.equals("f16")) {
296 *outColorType = kRGBA_F16_SkColorType;
brianosmanefded512016-07-26 08:11:50 -0700297 *outColorSpace = SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named);
brianosmand93c1202016-03-10 07:49:08 -0800298 return true;
299 }
300 if (value.equals("srgb")) {
301 *outColorType = kN32_SkColorType;
brianosmanb109b8c2016-06-16 13:03:24 -0700302 *outColorSpace = SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named);
brianosmand93c1202016-03-10 07:49:08 -0800303 return true;
304 }
305 return false;
306}
kkinnunen3e980c32015-12-23 01:33:00 -0800307
308SkCommandLineConfigGpu* parse_command_line_config_gpu(const SkString& tag,
309 const SkTArray<SkString>& vias,
310 const SkString& options) {
311 // Defaults for GPU backend.
312 bool seenAPI = false;
bsalomon85b4b532016-04-05 11:06:27 -0700313 SkCommandLineConfigGpu::ContextType contextType = GrContextFactory::kNativeGL_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800314 bool seenUseNVPR = false;
315 bool useNVPR = false;
csmartdaltone0d36292016-07-29 08:14:20 -0700316 bool seenUseInstanced = false;
317 bool useInstanced = false;
kkinnunen3e980c32015-12-23 01:33:00 -0800318 bool seenUseDIText =false;
319 bool useDIText = false;
320 bool seenSamples = false;
321 int samples = 0;
brianosmand93c1202016-03-10 07:49:08 -0800322 bool seenColor = false;
323 SkColorType colorType = kN32_SkColorType;
brianosmanb109b8c2016-06-16 13:03:24 -0700324 sk_sp<SkColorSpace> colorSpace = nullptr;
kkinnunen3e980c32015-12-23 01:33:00 -0800325
326 SkTArray<SkString> optionParts;
327 SkStrSplit(options.c_str(), ",", kStrict_SkStrSplitMode, &optionParts);
328 for (int i = 0; i < optionParts.count(); ++i) {
329 SkTArray<SkString> keyValueParts;
330 SkStrSplit(optionParts[i].c_str(), "=", kStrict_SkStrSplitMode, &keyValueParts);
331 if (keyValueParts.count() != 2) {
332 return nullptr;
333 }
334 const SkString& key = keyValueParts[0];
335 const SkString& value = keyValueParts[1];
336 bool valueOk = false;
337 if (key.equals("api") && !seenAPI) {
338 valueOk = parse_option_gpu_api(value, &contextType);
339 seenAPI = true;
340 } else if (key.equals("nvpr") && !seenUseNVPR) {
341 valueOk = parse_option_bool(value, &useNVPR);
342 seenUseNVPR = true;
csmartdaltone0d36292016-07-29 08:14:20 -0700343 } else if (key.equals("inst") && !seenUseInstanced) {
344 valueOk = parse_option_bool(value, &useInstanced);
345 seenUseInstanced = true;
kkinnunen3e980c32015-12-23 01:33:00 -0800346 } else if (key.equals("dit") && !seenUseDIText) {
347 valueOk = parse_option_bool(value, &useDIText);
348 seenUseDIText = true;
349 } else if (key.equals("samples") && !seenSamples) {
350 valueOk = parse_option_int(value, &samples);
351 seenSamples = true;
brianosmand93c1202016-03-10 07:49:08 -0800352 } else if (key.equals("color") && !seenColor) {
brianosmanb109b8c2016-06-16 13:03:24 -0700353 valueOk = parse_option_gpu_color(value, &colorType, &colorSpace);
brianosmand93c1202016-03-10 07:49:08 -0800354 seenColor = true;
kkinnunen3e980c32015-12-23 01:33:00 -0800355 }
356 if (!valueOk) {
357 return nullptr;
358 }
359 }
csmartdaltone0d36292016-07-29 08:14:20 -0700360 return new SkCommandLineConfigGpu(tag, vias, contextType, useNVPR, useInstanced, useDIText,
361 samples, colorType, colorSpace);
kkinnunen3e980c32015-12-23 01:33:00 -0800362}
363#endif
364
365void ParseConfigs(const SkCommandLineFlags::StringArray& configs,
366 SkCommandLineConfigArray* outResult) {
367 outResult->reset();
368 for (int i = 0; i < configs.count(); ++i) {
369 SkString extendedBackend;
370 SkString extendedOptions;
371 SkString simpleBackend;
372 SkTArray<SkString> vias;
373
374 SkString tag(configs[i]);
375 SkTArray<SkString> parts;
376 SkStrSplit(tag.c_str(), "(", kStrict_SkStrSplitMode, &parts);
377 if (parts.count() == 2) {
378 SkTArray<SkString> parts2;
379 SkStrSplit(parts[1].c_str(), ")", kStrict_SkStrSplitMode, &parts2);
380 if (parts2.count() == 2 && parts2[1].isEmpty()) {
381 SkStrSplit(parts[0].c_str(), "-", kStrict_SkStrSplitMode, &vias);
382 if (vias.count()) {
383 extendedBackend = vias[vias.count() - 1];
384 vias.pop_back();
385 } else {
386 extendedBackend = parts[0];
387 }
388 extendedOptions = parts2[0];
389 simpleBackend.printf("%s(%s)", extendedBackend.c_str(), extendedOptions.c_str());
390 }
391 }
392
393 if (extendedBackend.isEmpty()) {
394 simpleBackend = tag;
395 SkStrSplit(tag.c_str(), "-", kStrict_SkStrSplitMode, &vias);
396 if (vias.count()) {
397 simpleBackend = vias[vias.count() - 1];
398 vias.pop_back();
399 }
400 // Note: no #if SK_ANGLE: this is a special rule in the via-tag grammar.
401 if (vias.count() && simpleBackend.equals("gl") &&
402 vias[vias.count() - 1].equals("angle")) {
403 simpleBackend = "angle-gl";
404 vias.pop_back();
405 }
406
407 for (auto& predefinedConfig : gPredefinedConfigs) {
408 if (simpleBackend.equals(predefinedConfig.predefinedConfig)) {
409 extendedBackend = predefinedConfig.backend;
410 extendedOptions = predefinedConfig.options;
411 break;
412 }
413 }
414 }
415 SkCommandLineConfig* parsedConfig = nullptr;
416#if SK_SUPPORT_GPU
417 if (extendedBackend.equals("gpu")) {
418 parsedConfig = parse_command_line_config_gpu(tag, vias, extendedOptions);
419 }
420#endif
421 if (!parsedConfig) {
422 parsedConfig = new SkCommandLineConfig(tag, simpleBackend, vias);
423 }
424 outResult->emplace_back(parsedConfig);
425 }
426}