blob: 1e980d20e99f46fe32495c02693e6139e46ae0d1 [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
raftias7c602de2016-10-13 10:45:44 -07008#include "SkColorSpace_Base.h"
kkinnunen3e980c32015-12-23 01:33:00 -08009#include "SkCommonFlagsConfig.h"
csmartdalton6270e552016-09-13 10:41:49 -070010#include "SkImageInfo.h"
kkinnunen3e980c32015-12-23 01:33:00 -080011
12#include <stdlib.h>
13
bsalomon3724e572016-03-30 18:56:19 -070014#if SK_SUPPORT_GPU
15using sk_gpu_test::GrContextFactory;
16#endif
17
Brian Salomon6405e712017-03-20 08:54:16 -040018#if defined(SK_BUILD_FOR_ANDROID) || defined(SK_BUILD_FOR_IOS)
19# define DEFAULT_GPU_CONFIG "gles"
20#else
21# define DEFAULT_GPU_CONFIG "gl"
22#endif
23
kkinnunen3e980c32015-12-23 01:33:00 -080024static const char defaultConfigs[] =
Brian Salomon6405e712017-03-20 08:54:16 -040025 "8888 " DEFAULT_GPU_CONFIG " nonrendering "
bsalomon11abd8d2016-10-14 08:13:48 -070026#if defined(SK_BUILD_FOR_WIN)
27 " angle_d3d11_es2"
kkinnunen3e980c32015-12-23 01:33:00 -080028#endif
kkinnunen3e980c32015-12-23 01:33:00 -080029 ;
30
Brian Salomon6405e712017-03-20 08:54:16 -040031#undef DEFAULT_GPU_CONFIG
32
brianosman37e23342016-09-20 08:06:30 -070033static const struct {
34 const char* predefinedConfig;
35 const char* backend;
36 const char* options;
37} gPredefinedConfigs[] ={
38#if SK_SUPPORT_GPU
Brian Salomon002c1202016-10-18 11:28:20 -040039 { "gl", "gpu", "api=gl" },
Brian Salomon50f66d82017-03-17 14:32:05 -040040 { "gles", "gpu", "api=gles" },
Brian Salomon002c1202016-10-18 11:28:20 -040041 { "glmsaa4", "gpu", "api=gl,samples=4" },
Brian Salomondcf0ab02017-03-20 11:10:21 -040042 { "glmsaa8" , "gpu", "api=gl,samples=8" },
Brian Salomon50f66d82017-03-17 14:32:05 -040043 { "glesmsaa4", "gpu", "api=gles,samples=4" },
Brian Salomon002c1202016-10-18 11:28:20 -040044 { "glnvpr4", "gpu", "api=gl,nvpr=true,samples=4" },
Brian Salomondcf0ab02017-03-20 11:10:21 -040045 { "glnvpr8" , "gpu", "api=gl,nvpr=true,samples=8" },
Brian Salomon002c1202016-10-18 11:28:20 -040046 { "glnvprdit4", "gpu", "api=gl,nvpr=true,samples=4,dit=true" },
Brian Salomondcf0ab02017-03-20 11:10:21 -040047 { "glnvprdit8" , "gpu", "api=gl,nvpr=true,samples=8,dit=true" },
Brian Salomon50f66d82017-03-17 14:32:05 -040048 { "glesnvpr4", "gpu", "api=gles,nvpr=true,samples=4" },
49 { "glesnvprdit4", "gpu", "api=gles,nvpr=true,samples=4,dit=true" },
Brian Salomon002c1202016-10-18 11:28:20 -040050 { "glinst", "gpu", "api=gl,inst=true" },
51 { "glinst4", "gpu", "api=gl,inst=true,samples=4" },
52 { "glinstdit4", "gpu", "api=gl,inst=true,samples=4,dit=true" },
Brian Salomondcf0ab02017-03-20 11:10:21 -040053 { "glinst8" , "gpu", "api=gl,inst=true,samples=8" },
54 { "glinstdit8" , "gpu", "api=gl,inst=true,samples=8,dit=true" },
Brian Salomon50f66d82017-03-17 14:32:05 -040055 { "glesinst", "gpu", "api=gles,inst=true" },
56 { "glesinst4", "gpu", "api=gles,inst=true,samples=4" },
57 { "glesinstdit4", "gpu", "api=gles,inst=true,samples=4,dit=true" },
Brian Salomon6405e712017-03-20 08:54:16 -040058 { "glf16", "gpu", "api=gl,color=f16" },
59 { "glsrgb", "gpu", "api=gl,color=srgb" },
60 { "glsrgbnl", "gpu", "api=gl,color=srgbnl" },
61 { "glesf16", "gpu", "api=gles,color=f16" },
62 { "glessrgb", "gpu", "api=gles,color=srgb" },
63 { "glessrgbnl", "gpu", "api=gles,color=srgbnl" },
Brian Salomon002c1202016-10-18 11:28:20 -040064 { "glsrgb", "gpu", "api=gl,color=srgb" },
65 { "glwide", "gpu", "api=gl,color=f16_wide" },
66 { "glnarrow", "gpu", "api=gl,color=f16_narrow" },
Brian Salomon50f66d82017-03-17 14:32:05 -040067 { "glessrgb", "gpu", "api=gles,color=srgb" },
68 { "gleswide", "gpu", "api=gles,color=f16_wide" },
69 { "glesnarrow", "gpu", "api=gles,color=f16_narrow" },
Brian Salomon50f66d82017-03-17 14:32:05 -040070 { "gldft", "gpu", "api=gl,dit=true" },
71 { "glesdft", "gpu", "api=gles,dit=true" },
Brian Salomon6405e712017-03-20 08:54:16 -040072 { "debuggl", "gpu", "api=debuggl" },
73 { "nullgl", "gpu", "api=nullgl" },
Brian Salomon002c1202016-10-18 11:28:20 -040074 { "angle_d3d11_es2", "gpu", "api=angle_d3d11_es2" },
Brian Osman21d742d2017-01-10 13:31:33 -050075 { "angle_d3d11_es3", "gpu", "api=angle_d3d11_es3" },
Brian Salomon002c1202016-10-18 11:28:20 -040076 { "angle_d3d9_es2", "gpu", "api=angle_d3d9_es2" },
77 { "angle_d3d11_es2_msaa4", "gpu", "api=angle_d3d11_es2,samples=4" },
Brian Salomon528ca9b2017-03-24 11:15:09 -040078 { "angle_d3d11_es2_msaa8", "gpu", "api=angle_d3d11_es2,samples=8" },
Brian Salomon002c1202016-10-18 11:28:20 -040079 { "angle_gl_es2", "gpu", "api=angle_gl_es2" },
Brian Osman21d742d2017-01-10 13:31:33 -050080 { "angle_gl_es3", "gpu", "api=angle_gl_es3" },
Brian Salomon002c1202016-10-18 11:28:20 -040081 { "commandbuffer", "gpu", "api=commandbuffer" }
kkinnunen3e980c32015-12-23 01:33:00 -080082#if SK_MESA
Brian Salomon002c1202016-10-18 11:28:20 -040083 ,{ "mesa", "gpu", "api=mesa" }
kkinnunen3e980c32015-12-23 01:33:00 -080084#endif
brianosman37e23342016-09-20 08:06:30 -070085#ifdef SK_VULKAN
Brian Salomon002c1202016-10-18 11:28:20 -040086 ,{ "vk", "gpu", "api=vulkan" }
87 ,{ "vksrgb", "gpu", "api=vulkan,color=srgb" }
88 ,{ "vkwide", "gpu", "api=vulkan,color=f16_wide" }
89 ,{ "vkmsaa4", "gpu", "api=vulkan,samples=4" }
Brian Salomondcf0ab02017-03-20 11:10:21 -040090 ,{ "vkmsaa8", "gpu", "api=vulkan,samples=8" }
brianosman37e23342016-09-20 08:06:30 -070091#endif
92
93#else
94{ "", "", "" }
95#endif
96};
97
98static const char configHelp[] =
Derek Sollenbergerc65386a2017-01-05 09:50:22 -050099 "Options: 565 8888 srgb f16 nonrendering null pdf pdfa skp pipe svg xps";
brianosman37e23342016-09-20 08:06:30 -0700100
101static const char* config_help_fn() {
102 static SkString helpString;
103 helpString.set(configHelp);
104 for (const auto& config : gPredefinedConfigs) {
105 helpString.appendf(" %s", config.predefinedConfig);
106 }
bsalomon808ecbb2016-09-28 12:40:22 -0700107 helpString.append(" or use extended form 'backend[option=value,...]'.\n");
brianosman37e23342016-09-20 08:06:30 -0700108 return helpString.c_str();
109}
kkinnunen3e980c32015-12-23 01:33:00 -0800110
111static const char configExtendedHelp[] =
112 "Extended form: 'backend(option=value,...)'\n\n"
113 "Possible backends and options:\n"
114#if SK_SUPPORT_GPU
115 "\n"
bsalomon808ecbb2016-09-28 12:40:22 -0700116 "gpu[api=string,color=string,dit=bool,nvpr=bool,inst=bool,samples=int]\n"
Brian Salomon6405e712017-03-20 08:54:16 -0400117 "\tapi\ttype: string\trequired\n"
kkinnunen3e980c32015-12-23 01:33:00 -0800118 "\t Select graphics API to use with gpu backend.\n"
119 "\t Options:\n"
120 "\t\tnative\t\t\tUse platform default OpenGL or OpenGL ES backend.\n"
121 "\t\tgl \t\t\tUse OpenGL.\n"
122 "\t\tgles \t\t\tUse OpenGL ES.\n"
Brian Salomon6405e712017-03-20 08:54:16 -0400123 "\t\tdebuggl \t\t\tUse debug OpenGL.\n"
124 "\t\tnullgl \t\t\tUse null OpenGL.\n"
bsalomon11abd8d2016-10-14 08:13:48 -0700125 "\t\tangle_d3d9_es2\t\t\tUse OpenGL ES2 on the ANGLE Direct3D9 backend.\n"
126 "\t\tangle_d3d11_es2\t\t\tUse OpenGL ES2 on the ANGLE Direct3D11 backend.\n"
127 "\t\tangle_d3d11_es3\t\t\tUse OpenGL ES3 on the ANGLE Direct3D11 backend.\n"
128 "\t\tangle_gl_es2\t\t\tUse OpenGL ES2 on the ANGLE OpenGL backend.\n"
129 "\t\tangle_gl_es3\t\t\tUse OpenGL ES3 on the ANGLE OpenGL backend.\n"
kkinnunen3e980c32015-12-23 01:33:00 -0800130 "\t\tcommandbuffer\t\tUse command buffer.\n"
kkinnunen3e980c32015-12-23 01:33:00 -0800131#if SK_MESA
132 "\t\tmesa\t\t\tUse MESA.\n"
133#endif
bsalomondc0fcd42016-04-11 14:21:33 -0700134#ifdef SK_VULKAN
135 "\t\tvulkan\t\t\tUse Vulkan.\n"
136#endif
brianosmand93c1202016-03-10 07:49:08 -0800137 "\tcolor\ttype: string\tdefault: 8888.\n"
138 "\t Select framebuffer color format.\n"
139 "\t Options:\n"
140 "\t\t8888\t\t\tLinear 8888.\n"
brianosman4562f6e2016-09-19 14:42:04 -0700141 "\t\tf16{_gamut}\t\tLinear 16-bit floating point.\n"
142 "\t\tsrgb{_gamut}\t\tsRGB 8888.\n"
143 "\t gamut\ttype: string\tdefault: srgb.\n"
144 "\t Select color gamut for f16 or sRGB format buffers.\n"
145 "\t Options:\n"
146 "\t\tsrgb\t\t\tsRGB gamut.\n"
147 "\t\twide\t\t\tWide Gamut RGB.\n"
kkinnunen3e980c32015-12-23 01:33:00 -0800148 "\tdit\ttype: bool\tdefault: false.\n"
149 "\t Use device independent text.\n"
150 "\tnvpr\ttype: bool\tdefault: false.\n"
151 "\t Use NV_path_rendering OpenGL and OpenGL ES extension.\n"
152 "\tsamples\ttype: int\tdefault: 0.\n"
153 "\t Use multisampling with N samples.\n"
154 "\n"
155 "Predefined configs:\n\n"
brianosman37e23342016-09-20 08:06:30 -0700156 // Help text for pre-defined configs is auto-generated from gPredefinedConfigs
kkinnunen3e980c32015-12-23 01:33:00 -0800157#endif
158 ;
159
brianosman37e23342016-09-20 08:06:30 -0700160static const char* config_extended_help_fn() {
161 static SkString helpString;
162 helpString.set(configExtendedHelp);
163 for (const auto& config : gPredefinedConfigs) {
164 helpString.appendf("\t%-10s\t= gpu(%s)\n", config.predefinedConfig, config.options);
165 }
166 return helpString.c_str();
167}
kkinnunen3e980c32015-12-23 01:33:00 -0800168
brianosman37e23342016-09-20 08:06:30 -0700169DEFINE_extended_string(config, defaultConfigs, config_help_fn(), config_extended_help_fn());
kkinnunen3e980c32015-12-23 01:33:00 -0800170
171SkCommandLineConfig::SkCommandLineConfig(const SkString& tag, const SkString& backend,
172 const SkTArray<SkString>& viaParts)
173 : fTag(tag)
174 , fBackend(backend)
175 , fViaParts(viaParts) {
176}
177SkCommandLineConfig::~SkCommandLineConfig() {
178}
179
180#if SK_SUPPORT_GPU
181SkCommandLineConfigGpu::SkCommandLineConfigGpu(
csmartdaltone0d36292016-07-29 08:14:20 -0700182 const SkString& tag, const SkTArray<SkString>& viaParts, ContextType contextType, bool useNVPR,
183 bool useInstanced, bool useDIText, int samples, SkColorType colorType,
184 sk_sp<SkColorSpace> colorSpace)
kkinnunen3e980c32015-12-23 01:33:00 -0800185 : SkCommandLineConfig(tag, SkString("gpu"), viaParts)
186 , fContextType(contextType)
csmartdaltone812d492017-02-21 12:36:05 -0700187 , fContextOverrides(ContextOverrides::kNone)
kkinnunen3e980c32015-12-23 01:33:00 -0800188 , fUseDIText(useDIText)
brianosmand93c1202016-03-10 07:49:08 -0800189 , fSamples(samples)
190 , fColorType(colorType)
brianosmanb109b8c2016-06-16 13:03:24 -0700191 , fColorSpace(std::move(colorSpace)) {
csmartdalton6270e552016-09-13 10:41:49 -0700192 if (useNVPR) {
csmartdaltone812d492017-02-21 12:36:05 -0700193 fContextOverrides |= ContextOverrides::kRequireNVPRSupport;
194 } else if (!useInstanced) {
195 // We don't disable NVPR for instanced configs. Otherwise the caps wouldn't use mixed
196 // samples and we couldn't test the mixed samples backend for simple shapes.
197 fContextOverrides |= ContextOverrides::kDisableNVPR;
csmartdalton6270e552016-09-13 10:41:49 -0700198 }
199 if (useInstanced) {
csmartdaltone812d492017-02-21 12:36:05 -0700200 fContextOverrides |= ContextOverrides::kUseInstanced;
csmartdalton6270e552016-09-13 10:41:49 -0700201 }
brianosman20471892016-12-02 06:43:32 -0800202 // Subtle logic: If the config has a color space attached, we're going to be rendering to sRGB,
203 // so we need that capability. In addition, to get the widest test coverage, we DO NOT require
204 // that we can disable sRGB decode. (That's for rendering sRGB sources to legacy surfaces).
205 //
206 // If the config doesn't have a color space attached, we're going to be rendering in legacy
csmartdaltone812d492017-02-21 12:36:05 -0700207 // mode. In that case, we don't require sRGB capability and we defer to the client to decide on
208 // sRGB decode control.
Brian Osman7039f742016-11-01 15:56:16 -0400209 if (fColorSpace) {
csmartdaltone812d492017-02-21 12:36:05 -0700210 fContextOverrides |= ContextOverrides::kRequireSRGBSupport;
211 fContextOverrides |= ContextOverrides::kAllowSRGBWithoutDecodeControl;
csmartdalton6270e552016-09-13 10:41:49 -0700212 }
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 }
Brian Salomon6405e712017-03-20 08:54:16 -0400247 if (value.equals("debuggl")) {
bsalomon85b4b532016-04-05 11:06:27 -0700248 *outContextType = GrContextFactory::kDebugGL_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800249 return true;
250 }
Brian Salomon6405e712017-03-20 08:54:16 -0400251 if (value.equals("nullgl")) {
bsalomon85b4b532016-04-05 11:06:27 -0700252 *outContextType = GrContextFactory::kNullGL_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800253 return true;
254 }
bsalomon11abd8d2016-10-14 08:13:48 -0700255 if (value.equals("angle_d3d9_es2")) {
256 *outContextType = GrContextFactory::kANGLE_D3D9_ES2_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800257 return true;
258 }
bsalomon11abd8d2016-10-14 08:13:48 -0700259 if (value.equals("angle_d3d11_es2")) {
260 *outContextType = GrContextFactory::kANGLE_D3D11_ES2_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800261 return true;
262 }
bsalomon11abd8d2016-10-14 08:13:48 -0700263 if (value.equals("angle_d3d11_es3")) {
264 *outContextType = GrContextFactory::kANGLE_D3D11_ES3_ContextType;
265 return true;
266 }
267 if (value.equals("angle_gl_es2")) {
268 *outContextType = GrContextFactory::kANGLE_GL_ES2_ContextType;
269 return true;
270 }
271 if (value.equals("angle_gl_es3")) {
272 *outContextType = GrContextFactory::kANGLE_GL_ES3_ContextType;
273 return true;
274 }
kkinnunen3e980c32015-12-23 01:33:00 -0800275 if (value.equals("commandbuffer")) {
bsalomon85b4b532016-04-05 11:06:27 -0700276 *outContextType = GrContextFactory::kCommandBuffer_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800277 return true;
278 }
kkinnunen3e980c32015-12-23 01:33:00 -0800279#if SK_MESA
280 if (value.equals("mesa")) {
bsalomon85b4b532016-04-05 11:06:27 -0700281 *outContextType = GrContextFactory::kMESA_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800282 return true;
283 }
284#endif
bsalomondc0fcd42016-04-11 14:21:33 -0700285#ifdef SK_VULKAN
286 if (value.equals("vulkan")) {
287 *outContextType = GrContextFactory::kVulkan_ContextType;
288 return true;
289 }
290#endif
kkinnunen3e980c32015-12-23 01:33:00 -0800291 return false;
292}
brianosmand93c1202016-03-10 07:49:08 -0800293static bool parse_option_gpu_color(const SkString& value,
294 SkColorType* outColorType,
brianosmanb109b8c2016-06-16 13:03:24 -0700295 sk_sp<SkColorSpace>* outColorSpace) {
brianosmand93c1202016-03-10 07:49:08 -0800296 if (value.equals("8888")) {
bsalomon33069252016-09-28 08:49:53 -0700297 *outColorType = kRGBA_8888_SkColorType;
brianosmanb109b8c2016-06-16 13:03:24 -0700298 *outColorSpace = nullptr;
brianosmand93c1202016-03-10 07:49:08 -0800299 return true;
300 }
brianosman4562f6e2016-09-19 14:42:04 -0700301
302 SkTArray<SkString> commands;
303 SkStrSplit(value.c_str(), "_", &commands);
304 if (commands.count() < 1 || commands.count() > 2) {
305 return false;
306 }
307
raftias94888332016-10-18 10:02:51 -0700308 const bool linearGamma = commands[0].equals("f16");
Matt Sarettd2228302017-03-02 08:53:46 -0500309 SkColorSpace::Gamut gamut = SkColorSpace::kSRGB_Gamut;
310 SkColorSpace::RenderTargetGamma gamma = linearGamma ? SkColorSpace::kLinear_RenderTargetGamma
311 : SkColorSpace::kSRGB_RenderTargetGamma;
Matt Sarettf3880932017-03-24 10:06:03 -0400312 *outColorSpace = SkColorSpace::MakeRGB(gamma, gamut);
Matt Sarettd2228302017-03-02 08:53:46 -0500313
brianosman4562f6e2016-09-19 14:42:04 -0700314 if (commands.count() == 2) {
315 if (commands[1].equals("srgb")) {
316 // sRGB gamut (which is our default)
317 } else if (commands[1].equals("wide")) {
318 // WideGamut RGB
319 const float gWideGamutRGB_toXYZD50[]{
320 0.7161046f, 0.1009296f, 0.1471858f, // -> X
321 0.2581874f, 0.7249378f, 0.0168748f, // -> Y
322 0.0000000f, 0.0517813f, 0.7734287f, // -> Z
323 };
324 SkMatrix44 wideGamutRGBMatrix(SkMatrix44::kUninitialized_Constructor);
325 wideGamutRGBMatrix.set3x3RowMajorf(gWideGamutRGB_toXYZD50);
Matt Sarettf3880932017-03-24 10:06:03 -0400326 *outColorSpace = SkColorSpace::MakeRGB(gamma, wideGamutRGBMatrix);
Brian Osman4a6b28e2016-10-17 11:14:02 -0400327 } else if (commands[1].equals("narrow")) {
328 // NarrowGamut RGB (an artifically smaller than sRGB gamut)
329 SkColorSpacePrimaries primaries ={
330 0.54f, 0.33f, // Rx, Ry
331 0.33f, 0.50f, // Gx, Gy
332 0.25f, 0.20f, // Bx, By
333 0.3127f, 0.3290f, // Wx, Wy
334 };
335 SkMatrix44 narrowGamutRGBMatrix(SkMatrix44::kUninitialized_Constructor);
336 primaries.toXYZD50(&narrowGamutRGBMatrix);
Matt Sarettf3880932017-03-24 10:06:03 -0400337 *outColorSpace = SkColorSpace::MakeRGB(gamma, narrowGamutRGBMatrix);
brianosman4562f6e2016-09-19 14:42:04 -0700338 } else {
339 // Unknown color gamut
340 return false;
341 }
342 }
343
344 // Now pick a color type
345 if (commands[0].equals("f16")) {
brianosmand93c1202016-03-10 07:49:08 -0800346 *outColorType = kRGBA_F16_SkColorType;
brianosmand93c1202016-03-10 07:49:08 -0800347 return true;
348 }
Matt Sarettd2228302017-03-02 08:53:46 -0500349 if (commands[0].equals("srgb") || commands[0].equals("srgbnl")) {
bsalomon33069252016-09-28 08:49:53 -0700350 *outColorType = kRGBA_8888_SkColorType;
brianosmand93c1202016-03-10 07:49:08 -0800351 return true;
352 }
353 return false;
354}
kkinnunen3e980c32015-12-23 01:33:00 -0800355
356SkCommandLineConfigGpu* parse_command_line_config_gpu(const SkString& tag,
357 const SkTArray<SkString>& vias,
358 const SkString& options) {
359 // Defaults for GPU backend.
360 bool seenAPI = false;
Brian Salomon6405e712017-03-20 08:54:16 -0400361 SkCommandLineConfigGpu::ContextType contextType = GrContextFactory::kGL_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800362 bool seenUseNVPR = false;
363 bool useNVPR = false;
csmartdaltone0d36292016-07-29 08:14:20 -0700364 bool seenUseInstanced = false;
365 bool useInstanced = false;
kkinnunen3e980c32015-12-23 01:33:00 -0800366 bool seenUseDIText =false;
367 bool useDIText = false;
368 bool seenSamples = false;
369 int samples = 0;
brianosmand93c1202016-03-10 07:49:08 -0800370 bool seenColor = false;
bsalomon33069252016-09-28 08:49:53 -0700371 SkColorType colorType = kRGBA_8888_SkColorType;
brianosmanb109b8c2016-06-16 13:03:24 -0700372 sk_sp<SkColorSpace> colorSpace = nullptr;
kkinnunen3e980c32015-12-23 01:33:00 -0800373
374 SkTArray<SkString> optionParts;
375 SkStrSplit(options.c_str(), ",", kStrict_SkStrSplitMode, &optionParts);
376 for (int i = 0; i < optionParts.count(); ++i) {
377 SkTArray<SkString> keyValueParts;
378 SkStrSplit(optionParts[i].c_str(), "=", kStrict_SkStrSplitMode, &keyValueParts);
379 if (keyValueParts.count() != 2) {
380 return nullptr;
381 }
382 const SkString& key = keyValueParts[0];
383 const SkString& value = keyValueParts[1];
384 bool valueOk = false;
385 if (key.equals("api") && !seenAPI) {
386 valueOk = parse_option_gpu_api(value, &contextType);
387 seenAPI = true;
388 } else if (key.equals("nvpr") && !seenUseNVPR) {
389 valueOk = parse_option_bool(value, &useNVPR);
390 seenUseNVPR = true;
csmartdaltone0d36292016-07-29 08:14:20 -0700391 } else if (key.equals("inst") && !seenUseInstanced) {
392 valueOk = parse_option_bool(value, &useInstanced);
393 seenUseInstanced = true;
kkinnunen3e980c32015-12-23 01:33:00 -0800394 } else if (key.equals("dit") && !seenUseDIText) {
395 valueOk = parse_option_bool(value, &useDIText);
396 seenUseDIText = true;
397 } else if (key.equals("samples") && !seenSamples) {
398 valueOk = parse_option_int(value, &samples);
399 seenSamples = true;
brianosmand93c1202016-03-10 07:49:08 -0800400 } else if (key.equals("color") && !seenColor) {
brianosmanb109b8c2016-06-16 13:03:24 -0700401 valueOk = parse_option_gpu_color(value, &colorType, &colorSpace);
brianosmand93c1202016-03-10 07:49:08 -0800402 seenColor = true;
kkinnunen3e980c32015-12-23 01:33:00 -0800403 }
404 if (!valueOk) {
405 return nullptr;
406 }
407 }
Brian Salomon6405e712017-03-20 08:54:16 -0400408 if (!seenAPI) {
409 return nullptr;
410 }
csmartdaltone0d36292016-07-29 08:14:20 -0700411 return new SkCommandLineConfigGpu(tag, vias, contextType, useNVPR, useInstanced, useDIText,
412 samples, colorType, colorSpace);
kkinnunen3e980c32015-12-23 01:33:00 -0800413}
414#endif
415
416void ParseConfigs(const SkCommandLineFlags::StringArray& configs,
417 SkCommandLineConfigArray* outResult) {
418 outResult->reset();
419 for (int i = 0; i < configs.count(); ++i) {
420 SkString extendedBackend;
421 SkString extendedOptions;
422 SkString simpleBackend;
423 SkTArray<SkString> vias;
424
425 SkString tag(configs[i]);
426 SkTArray<SkString> parts;
bsalomon808ecbb2016-09-28 12:40:22 -0700427 SkStrSplit(tag.c_str(), "[", kStrict_SkStrSplitMode, &parts);
kkinnunen3e980c32015-12-23 01:33:00 -0800428 if (parts.count() == 2) {
429 SkTArray<SkString> parts2;
bsalomon808ecbb2016-09-28 12:40:22 -0700430 SkStrSplit(parts[1].c_str(), "]", kStrict_SkStrSplitMode, &parts2);
kkinnunen3e980c32015-12-23 01:33:00 -0800431 if (parts2.count() == 2 && parts2[1].isEmpty()) {
432 SkStrSplit(parts[0].c_str(), "-", kStrict_SkStrSplitMode, &vias);
433 if (vias.count()) {
434 extendedBackend = vias[vias.count() - 1];
435 vias.pop_back();
436 } else {
437 extendedBackend = parts[0];
438 }
439 extendedOptions = parts2[0];
bsalomon808ecbb2016-09-28 12:40:22 -0700440 simpleBackend.printf("%s[%s]", extendedBackend.c_str(), extendedOptions.c_str());
kkinnunen3e980c32015-12-23 01:33:00 -0800441 }
442 }
443
444 if (extendedBackend.isEmpty()) {
445 simpleBackend = tag;
446 SkStrSplit(tag.c_str(), "-", kStrict_SkStrSplitMode, &vias);
447 if (vias.count()) {
448 simpleBackend = vias[vias.count() - 1];
449 vias.pop_back();
450 }
kkinnunen3e980c32015-12-23 01:33:00 -0800451 for (auto& predefinedConfig : gPredefinedConfigs) {
452 if (simpleBackend.equals(predefinedConfig.predefinedConfig)) {
453 extendedBackend = predefinedConfig.backend;
454 extendedOptions = predefinedConfig.options;
455 break;
456 }
457 }
458 }
459 SkCommandLineConfig* parsedConfig = nullptr;
460#if SK_SUPPORT_GPU
461 if (extendedBackend.equals("gpu")) {
462 parsedConfig = parse_command_line_config_gpu(tag, vias, extendedOptions);
463 }
464#endif
465 if (!parsedConfig) {
466 parsedConfig = new SkCommandLineConfig(tag, simpleBackend, vias);
467 }
468 outResult->emplace_back(parsedConfig);
469 }
470}