blob: d0e5f60e4f8a082c2f15947c545c2e186949cadb [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"
csmartdalton6270e552016-09-13 10:41:49 -07009#include "SkImageInfo.h"
kkinnunen3e980c32015-12-23 01:33:00 -080010
11#include <stdlib.h>
12
bsalomon3724e572016-03-30 18:56:19 -070013#if SK_SUPPORT_GPU
14using sk_gpu_test::GrContextFactory;
15#endif
16
kkinnunen3e980c32015-12-23 01:33:00 -080017static const char defaultConfigs[] =
mtklein896ddb72016-09-14 10:33:12 -070018 "8888 gpu nonrendering"
kkinnunen3e980c32015-12-23 01:33:00 -080019#if SK_ANGLE
20#ifdef SK_BUILD_FOR_WIN
21 " angle"
22#endif
23#endif
24#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
25 " hwui"
26#endif
27 ;
28
brianosman37e23342016-09-20 08:06:30 -070029static const struct {
30 const char* predefinedConfig;
31 const char* backend;
32 const char* options;
33} gPredefinedConfigs[] ={
34#if SK_SUPPORT_GPU
35 { "gpu", "gpu", "" },
36 { "gl", "gpu", "api=gl" },
37 { "msaa4", "gpu", "samples=4" },
38 { "glmsaa4", "gpu", "api=gl,samples=4" },
39 { "msaa16", "gpu", "samples=16" },
40 { "nvpr4", "gpu", "nvpr=true,samples=4" },
41 { "glnvpr4", "gpu", "api=gl,nvpr=true,samples=4" },
42 { "nvpr16", "gpu", "nvpr=true,samples=16" },
43 { "nvprdit4", "gpu", "nvpr=true,samples=4,dit=true" },
44 { "glnvprdit4", "gpu", "api=gl,nvpr=true,samples=4,dit=true" },
45 { "nvprdit16", "gpu", "nvpr=true,samples=16,dit=true" },
46 { "glinst", "gpu", "api=gl,inst=true" },
47 { "glinst4", "gpu", "api=gl,inst=true,samples=4" },
48 { "glinstdit4", "gpu", "api=gl,inst=true,samples=4,dit=true" },
49 { "glinst16", "gpu", "api=gl,inst=true,samples=16" },
50 { "glinstdit16", "gpu", "api=gl,inst=true,samples=16,dit=true" },
51 { "esinst", "gpu", "api=gles,inst=true" },
52 { "esinst4", "gpu", "api=gles,inst=true,samples=4" },
53 { "esinstdit4", "gpu", "api=gles,inst=true,samples=4,dit=true" },
54 { "gpuf16", "gpu", "color=f16" },
55 { "gpusrgb", "gpu", "color=srgb" },
56 { "glsrgb", "gpu", "api=gl,color=srgb" },
57 { "glwide", "gpu", "api=gl,color=f16_wide" },
58 { "gpudft", "gpu", "dit=true" },
59 { "gpudebug", "gpu", "api=debug" },
60 { "gpunull", "gpu", "api=null" },
61 { "debug", "gpu", "api=debug" },
62 { "nullgpu", "gpu", "api=null" }
kkinnunen3e980c32015-12-23 01:33:00 -080063#if SK_ANGLE
64#ifdef SK_BUILD_FOR_WIN
brianosman37e23342016-09-20 08:06:30 -070065 ,{ "angle", "gpu", "api=angle" }
kkinnunen3e980c32015-12-23 01:33:00 -080066#endif
brianosman37e23342016-09-20 08:06:30 -070067 ,{ "angle-gl", "gpu", "api=angle-gl" }
kkinnunen3e980c32015-12-23 01:33:00 -080068#endif
brianosman37e23342016-09-20 08:06:30 -070069 ,{ "commandbuffer", "gpu", "api=commandbuffer" }
kkinnunen3e980c32015-12-23 01:33:00 -080070#if SK_MESA
brianosman37e23342016-09-20 08:06:30 -070071 ,{ "mesa", "gpu", "api=mesa" }
kkinnunen3e980c32015-12-23 01:33:00 -080072#endif
brianosman37e23342016-09-20 08:06:30 -070073#ifdef SK_VULKAN
74 ,{ "vk", "gpu", "api=vulkan" }
75 ,{ "vksrgb", "gpu", "api=vulkan,color=srgb" }
76 ,{ "vkwide", "gpu", "api=vulkan,color=f16_wide" }
77 ,{ "vkmsaa4", "gpu", "api=vulkan,samples=4" }
78 ,{ "vkmsaa16", "gpu", "api=vulkan,samples=16" }
79#endif
80
81#else
82{ "", "", "" }
83#endif
84};
85
86static const char configHelp[] =
87 "Options: 565 8888 srgb f16 nonrendering null pdf pdfa skp pipe svg xps"
kkinnunen3e980c32015-12-23 01:33:00 -080088#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
89 " hwui"
90#endif
brianosman37e23342016-09-20 08:06:30 -070091 ;
92
93static const char* config_help_fn() {
94 static SkString helpString;
95 helpString.set(configHelp);
96 for (const auto& config : gPredefinedConfigs) {
97 helpString.appendf(" %s", config.predefinedConfig);
98 }
99 helpString.append(" or use extended form 'backend(option=value,...)'.\n");
100 return helpString.c_str();
101}
kkinnunen3e980c32015-12-23 01:33:00 -0800102
103static const char configExtendedHelp[] =
104 "Extended form: 'backend(option=value,...)'\n\n"
105 "Possible backends and options:\n"
106#if SK_SUPPORT_GPU
107 "\n"
brianosman37e23342016-09-20 08:06:30 -0700108 "gpu(api=string,color=string,dit=bool,nvpr=bool,inst=bool,samples=int)\n"
kkinnunen3e980c32015-12-23 01:33:00 -0800109 "\tapi\ttype: string\tdefault: native.\n"
110 "\t Select graphics API to use with gpu backend.\n"
111 "\t Options:\n"
112 "\t\tnative\t\t\tUse platform default OpenGL or OpenGL ES backend.\n"
113 "\t\tgl \t\t\tUse OpenGL.\n"
114 "\t\tgles \t\t\tUse OpenGL ES.\n"
115 "\t\tdebug \t\t\tUse debug OpenGL.\n"
116 "\t\tnull \t\t\tUse null OpenGL.\n"
117#if SK_ANGLE
118#ifdef SK_BUILD_FOR_WIN
119 "\t\tangle\t\t\tUse ANGLE DirectX.\n"
120#endif
121 "\t\tangle-gl\t\t\tUse ANGLE OpenGL.\n"
122#endif
kkinnunen3e980c32015-12-23 01:33:00 -0800123 "\t\tcommandbuffer\t\tUse command buffer.\n"
kkinnunen3e980c32015-12-23 01:33:00 -0800124#if SK_MESA
125 "\t\tmesa\t\t\tUse MESA.\n"
126#endif
bsalomondc0fcd42016-04-11 14:21:33 -0700127#ifdef SK_VULKAN
128 "\t\tvulkan\t\t\tUse Vulkan.\n"
129#endif
brianosmand93c1202016-03-10 07:49:08 -0800130 "\tcolor\ttype: string\tdefault: 8888.\n"
131 "\t Select framebuffer color format.\n"
132 "\t Options:\n"
133 "\t\t8888\t\t\tLinear 8888.\n"
brianosman4562f6e2016-09-19 14:42:04 -0700134 "\t\tf16{_gamut}\t\tLinear 16-bit floating point.\n"
135 "\t\tsrgb{_gamut}\t\tsRGB 8888.\n"
136 "\t gamut\ttype: string\tdefault: srgb.\n"
137 "\t Select color gamut for f16 or sRGB format buffers.\n"
138 "\t Options:\n"
139 "\t\tsrgb\t\t\tsRGB gamut.\n"
140 "\t\twide\t\t\tWide Gamut RGB.\n"
kkinnunen3e980c32015-12-23 01:33:00 -0800141 "\tdit\ttype: bool\tdefault: false.\n"
142 "\t Use device independent text.\n"
143 "\tnvpr\ttype: bool\tdefault: false.\n"
144 "\t Use NV_path_rendering OpenGL and OpenGL ES extension.\n"
145 "\tsamples\ttype: int\tdefault: 0.\n"
146 "\t Use multisampling with N samples.\n"
147 "\n"
148 "Predefined configs:\n\n"
brianosman37e23342016-09-20 08:06:30 -0700149 // Help text for pre-defined configs is auto-generated from gPredefinedConfigs
kkinnunen3e980c32015-12-23 01:33:00 -0800150#endif
151 ;
152
brianosman37e23342016-09-20 08:06:30 -0700153static const char* config_extended_help_fn() {
154 static SkString helpString;
155 helpString.set(configExtendedHelp);
156 for (const auto& config : gPredefinedConfigs) {
157 helpString.appendf("\t%-10s\t= gpu(%s)\n", config.predefinedConfig, config.options);
158 }
159 return helpString.c_str();
160}
kkinnunen3e980c32015-12-23 01:33:00 -0800161
brianosman37e23342016-09-20 08:06:30 -0700162DEFINE_extended_string(config, defaultConfigs, config_help_fn(), config_extended_help_fn());
kkinnunen3e980c32015-12-23 01:33:00 -0800163
164SkCommandLineConfig::SkCommandLineConfig(const SkString& tag, const SkString& backend,
165 const SkTArray<SkString>& viaParts)
166 : fTag(tag)
167 , fBackend(backend)
168 , fViaParts(viaParts) {
169}
170SkCommandLineConfig::~SkCommandLineConfig() {
171}
172
173#if SK_SUPPORT_GPU
174SkCommandLineConfigGpu::SkCommandLineConfigGpu(
csmartdaltone0d36292016-07-29 08:14:20 -0700175 const SkString& tag, const SkTArray<SkString>& viaParts, ContextType contextType, bool useNVPR,
176 bool useInstanced, bool useDIText, int samples, SkColorType colorType,
177 sk_sp<SkColorSpace> colorSpace)
kkinnunen3e980c32015-12-23 01:33:00 -0800178 : SkCommandLineConfig(tag, SkString("gpu"), viaParts)
179 , fContextType(contextType)
csmartdalton6270e552016-09-13 10:41:49 -0700180 , fContextOptions(ContextOptions::kNone)
kkinnunen3e980c32015-12-23 01:33:00 -0800181 , fUseDIText(useDIText)
brianosmand93c1202016-03-10 07:49:08 -0800182 , fSamples(samples)
183 , fColorType(colorType)
brianosmanb109b8c2016-06-16 13:03:24 -0700184 , fColorSpace(std::move(colorSpace)) {
csmartdalton6270e552016-09-13 10:41:49 -0700185 if (useNVPR) {
186 fContextOptions |= ContextOptions::kEnableNVPR;
187 }
188 if (useInstanced) {
189 fContextOptions |= ContextOptions::kUseInstanced;
190 }
191 if (SkColorAndColorSpaceAreGammaCorrect(colorType, colorSpace.get())) {
192 fContextOptions |= ContextOptions::kRequireSRGBSupport;
193 }
kkinnunen3e980c32015-12-23 01:33:00 -0800194}
195static bool parse_option_int(const SkString& value, int* outInt) {
196 if (value.isEmpty()) {
197 return false;
198 }
199 char* endptr = nullptr;
200 long intValue = strtol(value.c_str(), &endptr, 10);
201 if (*endptr != '\0') {
202 return false;
203 }
204 *outInt = static_cast<int>(intValue);
205 return true;
206}
207static bool parse_option_bool(const SkString& value, bool* outBool) {
208 if (value.equals("true")) {
209 *outBool = true;
210 return true;
211 }
212 if (value.equals("false")) {
213 *outBool = false;
214 return true;
215 }
216 return false;
217}
218static bool parse_option_gpu_api(const SkString& value,
219 SkCommandLineConfigGpu::ContextType* outContextType) {
kkinnunen3e980c32015-12-23 01:33:00 -0800220 if (value.equals("gl")) {
bsalomon85b4b532016-04-05 11:06:27 -0700221 *outContextType = GrContextFactory::kGL_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800222 return true;
223 }
224 if (value.equals("gles")) {
bsalomon85b4b532016-04-05 11:06:27 -0700225 *outContextType = GrContextFactory::kGLES_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800226 return true;
227 }
228 if (value.equals("debug")) {
bsalomon85b4b532016-04-05 11:06:27 -0700229 *outContextType = GrContextFactory::kDebugGL_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800230 return true;
231 }
232 if (value.equals("null")) {
bsalomon85b4b532016-04-05 11:06:27 -0700233 *outContextType = GrContextFactory::kNullGL_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800234 return true;
235 }
236#if SK_ANGLE
237#ifdef SK_BUILD_FOR_WIN
238 if (value.equals("angle")) {
bsalomon85b4b532016-04-05 11:06:27 -0700239 *outContextType = GrContextFactory::kANGLE_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800240 return true;
241 }
242#endif
243 if (value.equals("angle-gl")) {
bsalomon85b4b532016-04-05 11:06:27 -0700244 *outContextType = GrContextFactory::kANGLE_GL_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800245 return true;
246 }
247#endif
kkinnunen3e980c32015-12-23 01:33:00 -0800248 if (value.equals("commandbuffer")) {
bsalomon85b4b532016-04-05 11:06:27 -0700249 *outContextType = GrContextFactory::kCommandBuffer_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800250 return true;
251 }
kkinnunen3e980c32015-12-23 01:33:00 -0800252#if SK_MESA
253 if (value.equals("mesa")) {
bsalomon85b4b532016-04-05 11:06:27 -0700254 *outContextType = GrContextFactory::kMESA_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800255 return true;
256 }
257#endif
bsalomondc0fcd42016-04-11 14:21:33 -0700258#ifdef SK_VULKAN
259 if (value.equals("vulkan")) {
260 *outContextType = GrContextFactory::kVulkan_ContextType;
261 return true;
262 }
263#endif
kkinnunen3e980c32015-12-23 01:33:00 -0800264 return false;
265}
brianosmand93c1202016-03-10 07:49:08 -0800266static bool parse_option_gpu_color(const SkString& value,
267 SkColorType* outColorType,
brianosmanb109b8c2016-06-16 13:03:24 -0700268 sk_sp<SkColorSpace>* outColorSpace) {
brianosmand93c1202016-03-10 07:49:08 -0800269 if (value.equals("8888")) {
270 *outColorType = kN32_SkColorType;
brianosmanb109b8c2016-06-16 13:03:24 -0700271 *outColorSpace = nullptr;
brianosmand93c1202016-03-10 07:49:08 -0800272 return true;
273 }
brianosman4562f6e2016-09-19 14:42:04 -0700274
275 SkTArray<SkString> commands;
276 SkStrSplit(value.c_str(), "_", &commands);
277 if (commands.count() < 1 || commands.count() > 2) {
278 return false;
279 }
280
281 // First, figure out color gamut that we'll work in (default to sRGB)
282 sk_sp<SkColorSpace> colorSpace = SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named);
283 if (commands.count() == 2) {
284 if (commands[1].equals("srgb")) {
285 // sRGB gamut (which is our default)
286 } else if (commands[1].equals("wide")) {
287 // WideGamut RGB
288 const float gWideGamutRGB_toXYZD50[]{
289 0.7161046f, 0.1009296f, 0.1471858f, // -> X
290 0.2581874f, 0.7249378f, 0.0168748f, // -> Y
291 0.0000000f, 0.0517813f, 0.7734287f, // -> Z
292 };
293 SkMatrix44 wideGamutRGBMatrix(SkMatrix44::kUninitialized_Constructor);
294 wideGamutRGBMatrix.set3x3RowMajorf(gWideGamutRGB_toXYZD50);
295 colorSpace = SkColorSpace::NewRGB(SkColorSpace::kSRGB_RenderTargetGamma,
296 wideGamutRGBMatrix);
297 } else {
298 // Unknown color gamut
299 return false;
300 }
301 }
302
303 // Now pick a color type
304 if (commands[0].equals("f16")) {
brianosmand93c1202016-03-10 07:49:08 -0800305 *outColorType = kRGBA_F16_SkColorType;
brianosman4562f6e2016-09-19 14:42:04 -0700306 *outColorSpace = colorSpace->makeLinearGamma();
brianosmand93c1202016-03-10 07:49:08 -0800307 return true;
308 }
brianosman4562f6e2016-09-19 14:42:04 -0700309 if (commands[0].equals("srgb")) {
brianosmand93c1202016-03-10 07:49:08 -0800310 *outColorType = kN32_SkColorType;
brianosman4562f6e2016-09-19 14:42:04 -0700311 *outColorSpace = colorSpace;
brianosmand93c1202016-03-10 07:49:08 -0800312 return true;
313 }
314 return false;
315}
kkinnunen3e980c32015-12-23 01:33:00 -0800316
317SkCommandLineConfigGpu* parse_command_line_config_gpu(const SkString& tag,
318 const SkTArray<SkString>& vias,
319 const SkString& options) {
320 // Defaults for GPU backend.
321 bool seenAPI = false;
bsalomon85b4b532016-04-05 11:06:27 -0700322 SkCommandLineConfigGpu::ContextType contextType = GrContextFactory::kNativeGL_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800323 bool seenUseNVPR = false;
324 bool useNVPR = false;
csmartdaltone0d36292016-07-29 08:14:20 -0700325 bool seenUseInstanced = false;
326 bool useInstanced = false;
kkinnunen3e980c32015-12-23 01:33:00 -0800327 bool seenUseDIText =false;
328 bool useDIText = false;
329 bool seenSamples = false;
330 int samples = 0;
brianosmand93c1202016-03-10 07:49:08 -0800331 bool seenColor = false;
332 SkColorType colorType = kN32_SkColorType;
brianosmanb109b8c2016-06-16 13:03:24 -0700333 sk_sp<SkColorSpace> colorSpace = nullptr;
kkinnunen3e980c32015-12-23 01:33:00 -0800334
335 SkTArray<SkString> optionParts;
336 SkStrSplit(options.c_str(), ",", kStrict_SkStrSplitMode, &optionParts);
337 for (int i = 0; i < optionParts.count(); ++i) {
338 SkTArray<SkString> keyValueParts;
339 SkStrSplit(optionParts[i].c_str(), "=", kStrict_SkStrSplitMode, &keyValueParts);
340 if (keyValueParts.count() != 2) {
341 return nullptr;
342 }
343 const SkString& key = keyValueParts[0];
344 const SkString& value = keyValueParts[1];
345 bool valueOk = false;
346 if (key.equals("api") && !seenAPI) {
347 valueOk = parse_option_gpu_api(value, &contextType);
348 seenAPI = true;
349 } else if (key.equals("nvpr") && !seenUseNVPR) {
350 valueOk = parse_option_bool(value, &useNVPR);
351 seenUseNVPR = true;
csmartdaltone0d36292016-07-29 08:14:20 -0700352 } else if (key.equals("inst") && !seenUseInstanced) {
353 valueOk = parse_option_bool(value, &useInstanced);
354 seenUseInstanced = true;
kkinnunen3e980c32015-12-23 01:33:00 -0800355 } else if (key.equals("dit") && !seenUseDIText) {
356 valueOk = parse_option_bool(value, &useDIText);
357 seenUseDIText = true;
358 } else if (key.equals("samples") && !seenSamples) {
359 valueOk = parse_option_int(value, &samples);
360 seenSamples = true;
brianosmand93c1202016-03-10 07:49:08 -0800361 } else if (key.equals("color") && !seenColor) {
brianosmanb109b8c2016-06-16 13:03:24 -0700362 valueOk = parse_option_gpu_color(value, &colorType, &colorSpace);
brianosmand93c1202016-03-10 07:49:08 -0800363 seenColor = true;
kkinnunen3e980c32015-12-23 01:33:00 -0800364 }
365 if (!valueOk) {
366 return nullptr;
367 }
368 }
csmartdaltone0d36292016-07-29 08:14:20 -0700369 return new SkCommandLineConfigGpu(tag, vias, contextType, useNVPR, useInstanced, useDIText,
370 samples, colorType, colorSpace);
kkinnunen3e980c32015-12-23 01:33:00 -0800371}
372#endif
373
374void ParseConfigs(const SkCommandLineFlags::StringArray& configs,
375 SkCommandLineConfigArray* outResult) {
376 outResult->reset();
377 for (int i = 0; i < configs.count(); ++i) {
378 SkString extendedBackend;
379 SkString extendedOptions;
380 SkString simpleBackend;
381 SkTArray<SkString> vias;
382
383 SkString tag(configs[i]);
384 SkTArray<SkString> parts;
385 SkStrSplit(tag.c_str(), "(", kStrict_SkStrSplitMode, &parts);
386 if (parts.count() == 2) {
387 SkTArray<SkString> parts2;
388 SkStrSplit(parts[1].c_str(), ")", kStrict_SkStrSplitMode, &parts2);
389 if (parts2.count() == 2 && parts2[1].isEmpty()) {
390 SkStrSplit(parts[0].c_str(), "-", kStrict_SkStrSplitMode, &vias);
391 if (vias.count()) {
392 extendedBackend = vias[vias.count() - 1];
393 vias.pop_back();
394 } else {
395 extendedBackend = parts[0];
396 }
397 extendedOptions = parts2[0];
398 simpleBackend.printf("%s(%s)", extendedBackend.c_str(), extendedOptions.c_str());
399 }
400 }
401
402 if (extendedBackend.isEmpty()) {
403 simpleBackend = tag;
404 SkStrSplit(tag.c_str(), "-", kStrict_SkStrSplitMode, &vias);
405 if (vias.count()) {
406 simpleBackend = vias[vias.count() - 1];
407 vias.pop_back();
408 }
409 // Note: no #if SK_ANGLE: this is a special rule in the via-tag grammar.
410 if (vias.count() && simpleBackend.equals("gl") &&
411 vias[vias.count() - 1].equals("angle")) {
412 simpleBackend = "angle-gl";
413 vias.pop_back();
414 }
415
416 for (auto& predefinedConfig : gPredefinedConfigs) {
417 if (simpleBackend.equals(predefinedConfig.predefinedConfig)) {
418 extendedBackend = predefinedConfig.backend;
419 extendedOptions = predefinedConfig.options;
420 break;
421 }
422 }
423 }
424 SkCommandLineConfig* parsedConfig = nullptr;
425#if SK_SUPPORT_GPU
426 if (extendedBackend.equals("gpu")) {
427 parsedConfig = parse_command_line_config_gpu(tag, vias, extendedOptions);
428 }
429#endif
430 if (!parsedConfig) {
431 parsedConfig = new SkCommandLineConfig(tag, simpleBackend, vias);
432 }
433 outResult->emplace_back(parsedConfig);
434 }
435}