blob: 0ca0482ebc6a7bdac0eb9cb9929e266892a0d665 [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"
Bryce Thomas95a7b762018-03-02 13:54:21 -080010#include "SkTHash.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;
Brian Salomonf865b052018-03-09 09:01:53 -050037} gPredefinedConfigs[] = {
brianosman37e23342016-09-20 08:06:30 -070038#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 Salomon50f66d82017-03-17 14:32:05 -040046 { "glesnvpr4", "gpu", "api=gles,nvpr=true,samples=4" },
Brian Salomonf865b052018-03-09 09:01:53 -050047 { "glbetex", "gpu", "api=gl,surf=betex" },
48 { "glesbetex", "gpu", "api=gles,surf=betex" },
49 { "glbert", "gpu", "api=gl,surf=bert" },
50 { "glesbert", "gpu", "api=gles,surf=bert" },
Brian Salomonce5ee602017-07-17 11:31:31 -040051 { "gl4444", "gpu", "api=gl,color=4444" },
52 { "gl565", "gpu", "api=gl,color=565" },
Brian Salomon6405e712017-03-20 08:54:16 -040053 { "glf16", "gpu", "api=gl,color=f16" },
Brian Salomon5fba7ad2018-03-22 10:01:16 -040054 { "gl888x", "gpu", "api=gl,color=888x" },
Brian Osman10fc6fd2018-03-02 11:01:10 -050055 { "gl1010102", "gpu", "api=gl,color=1010102" },
Brian Salomon6405e712017-03-20 08:54:16 -040056 { "glsrgb", "gpu", "api=gl,color=srgb" },
57 { "glsrgbnl", "gpu", "api=gl,color=srgbnl" },
58 { "glesf16", "gpu", "api=gles,color=f16" },
Brian Salomon5fba7ad2018-03-22 10:01:16 -040059 { "gles888x", "gpu", "api=gles,color=888x" },
Brian Osman10fc6fd2018-03-02 11:01:10 -050060 { "gles1010102", "gpu", "api=gles,color=1010102" },
Brian Salomon6405e712017-03-20 08:54:16 -040061 { "glessrgb", "gpu", "api=gles,color=srgb" },
62 { "glessrgbnl", "gpu", "api=gles,color=srgbnl" },
Brian Salomon002c1202016-10-18 11:28:20 -040063 { "glwide", "gpu", "api=gl,color=f16_wide" },
64 { "glnarrow", "gpu", "api=gl,color=f16_narrow" },
Eric Karl5c779752017-05-08 12:02:07 -070065 { "glnostencils", "gpu", "api=gl,stencils=false" },
Brian Salomond1e61922018-01-24 17:25:03 -050066 { "gles4444", "gpu", "api=gles,color=4444" },
Brian Salomon50f66d82017-03-17 14:32:05 -040067 { "gleswide", "gpu", "api=gles,color=f16_wide" },
68 { "glesnarrow", "gpu", "api=gles,color=f16_narrow" },
Brian Salomon50f66d82017-03-17 14:32:05 -040069 { "gldft", "gpu", "api=gl,dit=true" },
70 { "glesdft", "gpu", "api=gles,dit=true" },
Brian Osmanf9810662017-08-30 10:02:10 -040071 { "gltestthreading", "gpu", "api=gl,testThreading=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 Salomon8c865882017-06-22 09:56:24 -040079 { "angle_d3d11_es3_msaa4", "gpu", "api=angle_d3d11_es3,samples=4" },
80 { "angle_d3d11_es3_msaa8", "gpu", "api=angle_d3d11_es3,samples=8" },
Brian Salomon002c1202016-10-18 11:28:20 -040081 { "angle_gl_es2", "gpu", "api=angle_gl_es2" },
Brian Osman21d742d2017-01-10 13:31:33 -050082 { "angle_gl_es3", "gpu", "api=angle_gl_es3" },
Ben Wagner7464a262018-04-19 15:49:18 -040083 { "angle_gl_es2_msaa8", "gpu", "api=angle_gl_es2,samples=8" },
84 { "angle_gl_es3_msaa8", "gpu", "api=angle_gl_es3,samples=8" },
Brian Salomon8fe24272017-07-07 12:56:11 -040085 { "commandbuffer", "gpu", "api=commandbuffer" },
86 { "mock", "gpu", "api=mock" }
brianosman37e23342016-09-20 08:06:30 -070087#ifdef SK_VULKAN
Brian Salomon002c1202016-10-18 11:28:20 -040088 ,{ "vk", "gpu", "api=vulkan" }
Chris Dalton6a0e54e2018-05-15 13:24:20 -060089 ,{ "vknostencils", "gpu", "api=vulkan,stencils=false" }
Brian Osman10fc6fd2018-03-02 11:01:10 -050090 ,{ "vk1010102", "gpu", "api=vulkan,color=1010102" }
Brian Salomon002c1202016-10-18 11:28:20 -040091 ,{ "vksrgb", "gpu", "api=vulkan,color=srgb" }
92 ,{ "vkwide", "gpu", "api=vulkan,color=f16_wide" }
93 ,{ "vkmsaa4", "gpu", "api=vulkan,samples=4" }
Brian Salomondcf0ab02017-03-20 11:10:21 -040094 ,{ "vkmsaa8", "gpu", "api=vulkan,samples=8" }
Brian Salomon52e943a2018-03-13 09:32:39 -040095 ,{ "vkbetex", "gpu", "api=vulkan,surf=betex" }
96 ,{ "vkbert", "gpu", "api=vulkan,surf=bert" }
brianosman37e23342016-09-20 08:06:30 -070097#endif
Greg Daniel2811aa22017-07-13 15:34:56 -040098#ifdef SK_METAL
99 ,{ "mtl", "gpu", "api=metal" }
Brian Osman10fc6fd2018-03-02 11:01:10 -0500100 ,{ "mtl1010102", "gpu", "api=metal,color=1010102" }
Greg Daniel2811aa22017-07-13 15:34:56 -0400101 ,{ "mtlsrgb", "gpu", "api=metal,color=srgb" }
102 ,{ "mtlwide", "gpu", "api=metal,color=f16_wide" }
103 ,{ "mtlmsaa4", "gpu", "api=metal,samples=4" }
104 ,{ "mtlmsaa8", "gpu", "api=metal,samples=8" }
105#endif
brianosman37e23342016-09-20 08:06:30 -0700106#else
Brian Salomon8fe24272017-07-07 12:56:11 -0400107 { "", "", "" }
brianosman37e23342016-09-20 08:06:30 -0700108#endif
109};
110
111static const char configHelp[] =
Derek Sollenbergerc65386a2017-01-05 09:50:22 -0500112 "Options: 565 8888 srgb f16 nonrendering null pdf pdfa skp pipe svg xps";
brianosman37e23342016-09-20 08:06:30 -0700113
114static const char* config_help_fn() {
115 static SkString helpString;
116 helpString.set(configHelp);
117 for (const auto& config : gPredefinedConfigs) {
118 helpString.appendf(" %s", config.predefinedConfig);
119 }
bsalomon808ecbb2016-09-28 12:40:22 -0700120 helpString.append(" or use extended form 'backend[option=value,...]'.\n");
brianosman37e23342016-09-20 08:06:30 -0700121 return helpString.c_str();
122}
kkinnunen3e980c32015-12-23 01:33:00 -0800123
124static const char configExtendedHelp[] =
125 "Extended form: 'backend(option=value,...)'\n\n"
126 "Possible backends and options:\n"
127#if SK_SUPPORT_GPU
128 "\n"
bsalomon808ecbb2016-09-28 12:40:22 -0700129 "gpu[api=string,color=string,dit=bool,nvpr=bool,inst=bool,samples=int]\n"
Brian Salomon6405e712017-03-20 08:54:16 -0400130 "\tapi\ttype: string\trequired\n"
kkinnunen3e980c32015-12-23 01:33:00 -0800131 "\t Select graphics API to use with gpu backend.\n"
132 "\t Options:\n"
kkinnunen3e980c32015-12-23 01:33:00 -0800133 "\t\tgl \t\t\tUse OpenGL.\n"
134 "\t\tgles \t\t\tUse OpenGL ES.\n"
Brian Salomon6405e712017-03-20 08:54:16 -0400135 "\t\tdebuggl \t\t\tUse debug OpenGL.\n"
136 "\t\tnullgl \t\t\tUse null OpenGL.\n"
bsalomon11abd8d2016-10-14 08:13:48 -0700137 "\t\tangle_d3d9_es2\t\t\tUse OpenGL ES2 on the ANGLE Direct3D9 backend.\n"
138 "\t\tangle_d3d11_es2\t\t\tUse OpenGL ES2 on the ANGLE Direct3D11 backend.\n"
139 "\t\tangle_d3d11_es3\t\t\tUse OpenGL ES3 on the ANGLE Direct3D11 backend.\n"
140 "\t\tangle_gl_es2\t\t\tUse OpenGL ES2 on the ANGLE OpenGL backend.\n"
141 "\t\tangle_gl_es3\t\t\tUse OpenGL ES3 on the ANGLE OpenGL backend.\n"
kkinnunen3e980c32015-12-23 01:33:00 -0800142 "\t\tcommandbuffer\t\tUse command buffer.\n"
Brian Salomon8fe24272017-07-07 12:56:11 -0400143 "\t\tmock\t\tUse mock context.\n"
bsalomondc0fcd42016-04-11 14:21:33 -0700144#ifdef SK_VULKAN
145 "\t\tvulkan\t\t\tUse Vulkan.\n"
146#endif
Greg Daniel2811aa22017-07-13 15:34:56 -0400147#ifdef SK_METAL
148 "\t\tmetal\t\t\tUse Metal.\n"
149#endif
brianosmand93c1202016-03-10 07:49:08 -0800150 "\tcolor\ttype: string\tdefault: 8888.\n"
151 "\t Select framebuffer color format.\n"
152 "\t Options:\n"
153 "\t\t8888\t\t\tLinear 8888.\n"
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400154 "\t\t888x\t\t\tLinear 888x.\n"
Brian Salomonce5ee602017-07-17 11:31:31 -0400155 "\t\t4444\t\t\tLinear 4444.\n"
156 "\t\t565\t\t\tLinear 565.\n"
brianosman4562f6e2016-09-19 14:42:04 -0700157 "\t\tf16{_gamut}\t\tLinear 16-bit floating point.\n"
Brian Osman10fc6fd2018-03-02 11:01:10 -0500158 "\t\t1010102\t\tLinear 1010102.\n"
brianosman4562f6e2016-09-19 14:42:04 -0700159 "\t\tsrgb{_gamut}\t\tsRGB 8888.\n"
160 "\t gamut\ttype: string\tdefault: srgb.\n"
161 "\t Select color gamut for f16 or sRGB format buffers.\n"
162 "\t Options:\n"
163 "\t\tsrgb\t\t\tsRGB gamut.\n"
164 "\t\twide\t\t\tWide Gamut RGB.\n"
kkinnunen3e980c32015-12-23 01:33:00 -0800165 "\tdit\ttype: bool\tdefault: false.\n"
166 "\t Use device independent text.\n"
167 "\tnvpr\ttype: bool\tdefault: false.\n"
168 "\t Use NV_path_rendering OpenGL and OpenGL ES extension.\n"
169 "\tsamples\ttype: int\tdefault: 0.\n"
170 "\t Use multisampling with N samples.\n"
Eric Karl5c779752017-05-08 12:02:07 -0700171 "\tstencils\ttype: bool\tdefault: true.\n"
172 "\t Allow the use of stencil buffers.\n"
Brian Osmanf9810662017-08-30 10:02:10 -0400173 "\ttestThreading\ttype: bool\tdefault: false.\n"
174 "\t Run config with and without worker threads, check that results match.\n"
Brian Salomonf865b052018-03-09 09:01:53 -0500175 "\tsurf\ttype: string\tdefault: default.\n"
176 "\t Controls the type of backing store for SkSurfaces.\n"
177 "\t Options:\n"
178 "\t\tdefault\t\t\tA renderable texture created in Skia's resource cache.\n"
179 "\t\tbetex\t\t\tA wrapped backend texture.\n"
180 "\t\tbert\t\t\tA wrapped backend render target\n"
kkinnunen3e980c32015-12-23 01:33:00 -0800181 "\n"
182 "Predefined configs:\n\n"
brianosman37e23342016-09-20 08:06:30 -0700183 // Help text for pre-defined configs is auto-generated from gPredefinedConfigs
kkinnunen3e980c32015-12-23 01:33:00 -0800184#endif
185 ;
186
brianosman37e23342016-09-20 08:06:30 -0700187static const char* config_extended_help_fn() {
188 static SkString helpString;
189 helpString.set(configExtendedHelp);
190 for (const auto& config : gPredefinedConfigs) {
191 helpString.appendf("\t%-10s\t= gpu(%s)\n", config.predefinedConfig, config.options);
192 }
193 return helpString.c_str();
194}
kkinnunen3e980c32015-12-23 01:33:00 -0800195
brianosman37e23342016-09-20 08:06:30 -0700196DEFINE_extended_string(config, defaultConfigs, config_help_fn(), config_extended_help_fn());
kkinnunen3e980c32015-12-23 01:33:00 -0800197
198SkCommandLineConfig::SkCommandLineConfig(const SkString& tag, const SkString& backend,
199 const SkTArray<SkString>& viaParts)
200 : fTag(tag)
201 , fBackend(backend)
202 , fViaParts(viaParts) {
203}
204SkCommandLineConfig::~SkCommandLineConfig() {
205}
206
kkinnunen3e980c32015-12-23 01:33:00 -0800207static bool parse_option_int(const SkString& value, int* outInt) {
208 if (value.isEmpty()) {
209 return false;
210 }
211 char* endptr = nullptr;
212 long intValue = strtol(value.c_str(), &endptr, 10);
213 if (*endptr != '\0') {
214 return false;
215 }
216 *outInt = static_cast<int>(intValue);
217 return true;
218}
219static bool parse_option_bool(const SkString& value, bool* outBool) {
220 if (value.equals("true")) {
221 *outBool = true;
222 return true;
223 }
224 if (value.equals("false")) {
225 *outBool = false;
226 return true;
227 }
228 return false;
229}
Bryce Thomas95a7b762018-03-02 13:54:21 -0800230#if SK_SUPPORT_GPU
kkinnunen3e980c32015-12-23 01:33:00 -0800231static bool parse_option_gpu_api(const SkString& value,
232 SkCommandLineConfigGpu::ContextType* outContextType) {
kkinnunen3e980c32015-12-23 01:33:00 -0800233 if (value.equals("gl")) {
bsalomon85b4b532016-04-05 11:06:27 -0700234 *outContextType = GrContextFactory::kGL_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800235 return true;
236 }
237 if (value.equals("gles")) {
bsalomon85b4b532016-04-05 11:06:27 -0700238 *outContextType = GrContextFactory::kGLES_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800239 return true;
240 }
Brian Salomon6405e712017-03-20 08:54:16 -0400241 if (value.equals("debuggl")) {
bsalomon85b4b532016-04-05 11:06:27 -0700242 *outContextType = GrContextFactory::kDebugGL_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800243 return true;
244 }
Brian Salomon6405e712017-03-20 08:54:16 -0400245 if (value.equals("nullgl")) {
bsalomon85b4b532016-04-05 11:06:27 -0700246 *outContextType = GrContextFactory::kNullGL_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800247 return true;
248 }
bsalomon11abd8d2016-10-14 08:13:48 -0700249 if (value.equals("angle_d3d9_es2")) {
250 *outContextType = GrContextFactory::kANGLE_D3D9_ES2_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800251 return true;
252 }
bsalomon11abd8d2016-10-14 08:13:48 -0700253 if (value.equals("angle_d3d11_es2")) {
254 *outContextType = GrContextFactory::kANGLE_D3D11_ES2_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800255 return true;
256 }
bsalomon11abd8d2016-10-14 08:13:48 -0700257 if (value.equals("angle_d3d11_es3")) {
258 *outContextType = GrContextFactory::kANGLE_D3D11_ES3_ContextType;
259 return true;
260 }
261 if (value.equals("angle_gl_es2")) {
262 *outContextType = GrContextFactory::kANGLE_GL_ES2_ContextType;
263 return true;
264 }
265 if (value.equals("angle_gl_es3")) {
266 *outContextType = GrContextFactory::kANGLE_GL_ES3_ContextType;
267 return true;
268 }
kkinnunen3e980c32015-12-23 01:33:00 -0800269 if (value.equals("commandbuffer")) {
bsalomon85b4b532016-04-05 11:06:27 -0700270 *outContextType = GrContextFactory::kCommandBuffer_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800271 return true;
272 }
Brian Salomon8fe24272017-07-07 12:56:11 -0400273 if (value.equals("mock")) {
274 *outContextType = GrContextFactory::kMock_ContextType;
275 return true;
276 }
bsalomondc0fcd42016-04-11 14:21:33 -0700277#ifdef SK_VULKAN
278 if (value.equals("vulkan")) {
279 *outContextType = GrContextFactory::kVulkan_ContextType;
280 return true;
281 }
282#endif
Greg Daniel2811aa22017-07-13 15:34:56 -0400283#ifdef SK_METAL
284 if (value.equals("metal")) {
285 *outContextType = GrContextFactory::kMetal_ContextType;
286 return true;
287 }
288#endif
kkinnunen3e980c32015-12-23 01:33:00 -0800289 return false;
290}
Brian Salomonf865b052018-03-09 09:01:53 -0500291
brianosmand93c1202016-03-10 07:49:08 -0800292static bool parse_option_gpu_color(const SkString& value,
293 SkColorType* outColorType,
Brian Salomonce5ee602017-07-17 11:31:31 -0400294 SkAlphaType* alphaType,
brianosmanb109b8c2016-06-16 13:03:24 -0700295 sk_sp<SkColorSpace>* outColorSpace) {
Brian Salomonce5ee602017-07-17 11:31:31 -0400296 // We always use premul unless the color type is 565.
297 *alphaType = kPremul_SkAlphaType;
298
brianosmand93c1202016-03-10 07:49:08 -0800299 if (value.equals("8888")) {
bsalomon33069252016-09-28 08:49:53 -0700300 *outColorType = kRGBA_8888_SkColorType;
brianosmanb109b8c2016-06-16 13:03:24 -0700301 *outColorSpace = nullptr;
brianosmand93c1202016-03-10 07:49:08 -0800302 return true;
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400303 } else if (value.equals("888x")) {
304 *outColorType = kRGB_888x_SkColorType;
305 *outColorSpace = nullptr;
306 return true;
Brian Salomonce5ee602017-07-17 11:31:31 -0400307 } else if (value.equals("4444")) {
308 *outColorType = kARGB_4444_SkColorType;
309 *outColorSpace = nullptr;
310 return true;
311 } else if (value.equals("565")) {
312 *outColorType = kRGB_565_SkColorType;
313 *alphaType = kOpaque_SkAlphaType;
314 *outColorSpace = nullptr;
315 return true;
Brian Osman10fc6fd2018-03-02 11:01:10 -0500316 } else if (value.equals("1010102")) {
317 *outColorType = kRGBA_1010102_SkColorType;
318 *outColorSpace = nullptr;
319 return true;
brianosmand93c1202016-03-10 07:49:08 -0800320 }
brianosman4562f6e2016-09-19 14:42:04 -0700321
322 SkTArray<SkString> commands;
323 SkStrSplit(value.c_str(), "_", &commands);
324 if (commands.count() < 1 || commands.count() > 2) {
325 return false;
326 }
327
raftias94888332016-10-18 10:02:51 -0700328 const bool linearGamma = commands[0].equals("f16");
Matt Sarettd2228302017-03-02 08:53:46 -0500329 SkColorSpace::Gamut gamut = SkColorSpace::kSRGB_Gamut;
330 SkColorSpace::RenderTargetGamma gamma = linearGamma ? SkColorSpace::kLinear_RenderTargetGamma
331 : SkColorSpace::kSRGB_RenderTargetGamma;
Matt Sarettf3880932017-03-24 10:06:03 -0400332 *outColorSpace = SkColorSpace::MakeRGB(gamma, gamut);
Matt Sarettd2228302017-03-02 08:53:46 -0500333
brianosman4562f6e2016-09-19 14:42:04 -0700334 if (commands.count() == 2) {
335 if (commands[1].equals("srgb")) {
336 // sRGB gamut (which is our default)
337 } else if (commands[1].equals("wide")) {
338 // WideGamut RGB
339 const float gWideGamutRGB_toXYZD50[]{
340 0.7161046f, 0.1009296f, 0.1471858f, // -> X
341 0.2581874f, 0.7249378f, 0.0168748f, // -> Y
342 0.0000000f, 0.0517813f, 0.7734287f, // -> Z
343 };
344 SkMatrix44 wideGamutRGBMatrix(SkMatrix44::kUninitialized_Constructor);
345 wideGamutRGBMatrix.set3x3RowMajorf(gWideGamutRGB_toXYZD50);
Matt Sarettf3880932017-03-24 10:06:03 -0400346 *outColorSpace = SkColorSpace::MakeRGB(gamma, wideGamutRGBMatrix);
Brian Osman4a6b28e2016-10-17 11:14:02 -0400347 } else if (commands[1].equals("narrow")) {
348 // NarrowGamut RGB (an artifically smaller than sRGB gamut)
349 SkColorSpacePrimaries primaries ={
350 0.54f, 0.33f, // Rx, Ry
351 0.33f, 0.50f, // Gx, Gy
352 0.25f, 0.20f, // Bx, By
353 0.3127f, 0.3290f, // Wx, Wy
354 };
355 SkMatrix44 narrowGamutRGBMatrix(SkMatrix44::kUninitialized_Constructor);
356 primaries.toXYZD50(&narrowGamutRGBMatrix);
Matt Sarettf3880932017-03-24 10:06:03 -0400357 *outColorSpace = SkColorSpace::MakeRGB(gamma, narrowGamutRGBMatrix);
brianosman4562f6e2016-09-19 14:42:04 -0700358 } else {
359 // Unknown color gamut
360 return false;
361 }
362 }
363
364 // Now pick a color type
365 if (commands[0].equals("f16")) {
brianosmand93c1202016-03-10 07:49:08 -0800366 *outColorType = kRGBA_F16_SkColorType;
brianosmand93c1202016-03-10 07:49:08 -0800367 return true;
368 }
Matt Sarettd2228302017-03-02 08:53:46 -0500369 if (commands[0].equals("srgb") || commands[0].equals("srgbnl")) {
bsalomon33069252016-09-28 08:49:53 -0700370 *outColorType = kRGBA_8888_SkColorType;
brianosmand93c1202016-03-10 07:49:08 -0800371 return true;
372 }
373 return false;
374}
Brian Salomonf865b052018-03-09 09:01:53 -0500375
376static bool parse_option_gpu_surf_type(const SkString& value,
377 SkCommandLineConfigGpu::SurfType* surfType) {
378 if (value.equals("default")) {
379 *surfType = SkCommandLineConfigGpu::SurfType::kDefault;
380 return true;
381 }
382 if (value.equals("betex")) {
383 *surfType = SkCommandLineConfigGpu::SurfType::kBackendTexture;
384 return true;
385 }
386 if (value.equals("bert")) {
387 *surfType = SkCommandLineConfigGpu::SurfType::kBackendRenderTarget;
388 return true;
389 }
390 return false;
391}
Bryce Thomas95a7b762018-03-02 13:54:21 -0800392#endif
393
394// Extended options take form --config item[key1=value1,key2=value2,...]
395// Example: --config gpu[api=gl,color=8888]
396class ExtendedOptions {
397public:
398 ExtendedOptions(const SkString& optionsString, bool* outParseSucceeded) {
399 SkTArray<SkString> optionParts;
400 SkStrSplit(optionsString.c_str(), ",", kStrict_SkStrSplitMode, &optionParts);
401 for (int i = 0; i < optionParts.count(); ++i) {
402 SkTArray<SkString> keyValueParts;
403 SkStrSplit(optionParts[i].c_str(), "=", kStrict_SkStrSplitMode, &keyValueParts);
404 if (keyValueParts.count() != 2) {
405 *outParseSucceeded = false;
406 return;
407 }
408 const SkString& key = keyValueParts[0];
409 const SkString& value = keyValueParts[1];
410 if (fOptionsMap.find(key) == nullptr) {
411 fOptionsMap.set(key, value);
412 } else {
413 // Duplicate values are not allowed.
414 *outParseSucceeded = false;
415 return;
416 }
417 }
418 *outParseSucceeded = true;
419 }
420
421#if SK_SUPPORT_GPU
422 bool get_option_gpu_color(const char* optionKey,
423 SkColorType* outColorType,
424 SkAlphaType* alphaType,
425 sk_sp<SkColorSpace>* outColorSpace,
426 bool optional = true) const {
427 SkString* optionValue = fOptionsMap.find(SkString(optionKey));
428 if (optionValue == nullptr) {
429 return optional;
430 }
431 return parse_option_gpu_color(*optionValue, outColorType, alphaType, outColorSpace);
432 }
433
434 bool get_option_gpu_api(const char* optionKey,
435 SkCommandLineConfigGpu::ContextType* outContextType,
436 bool optional = true) const {
437 SkString* optionValue = fOptionsMap.find(SkString(optionKey));
438 if (optionValue == nullptr) {
439 return optional;
440 }
441 return parse_option_gpu_api(*optionValue, outContextType);
442 }
Brian Salomonf865b052018-03-09 09:01:53 -0500443
444 bool get_option_gpu_surf_type(const char* optionKey,
445 SkCommandLineConfigGpu::SurfType* outSurfType,
446 bool optional = true) const {
447 SkString* optionValue = fOptionsMap.find(SkString(optionKey));
448 if (optionValue == nullptr) {
449 return optional;
450 }
451 return parse_option_gpu_surf_type(*optionValue, outSurfType);
452 }
Bryce Thomas95a7b762018-03-02 13:54:21 -0800453#endif
454
455 bool get_option_int(const char* optionKey, int* outInt, bool optional = true) const {
456 SkString* optionValue = fOptionsMap.find(SkString(optionKey));
457 if (optionValue == nullptr) {
458 return optional;
459 }
460 return parse_option_int(*optionValue, outInt);
461 }
462
463 bool get_option_bool(const char* optionKey, bool* outBool, bool optional = true) const {
464 SkString* optionValue = fOptionsMap.find(SkString(optionKey));
465 if (optionValue == nullptr) {
466 return optional;
467 }
468 return parse_option_bool(*optionValue, outBool);
469 }
470
471private:
472 SkTHashMap<SkString, SkString> fOptionsMap;
473};
474
475#if SK_SUPPORT_GPU
476SkCommandLineConfigGpu::SkCommandLineConfigGpu(
Brian Salomonf865b052018-03-09 09:01:53 -0500477 const SkString& tag, const SkTArray<SkString>& viaParts, ContextType contextType,
478 bool useNVPR, bool useDIText, int samples, SkColorType colorType, SkAlphaType alphaType,
479 sk_sp<SkColorSpace> colorSpace, bool useStencilBuffers, bool testThreading,
480 SurfType surfType)
Bryce Thomas95a7b762018-03-02 13:54:21 -0800481 : SkCommandLineConfig(tag, SkString("gpu"), viaParts)
482 , fContextType(contextType)
483 , fContextOverrides(ContextOverrides::kNone)
484 , fUseDIText(useDIText)
485 , fSamples(samples)
486 , fColorType(colorType)
487 , fAlphaType(alphaType)
488 , fColorSpace(std::move(colorSpace))
Brian Salomonf865b052018-03-09 09:01:53 -0500489 , fTestThreading(testThreading)
490 , fSurfType(surfType) {
Bryce Thomas95a7b762018-03-02 13:54:21 -0800491 if (useNVPR) {
492 fContextOverrides |= ContextOverrides::kRequireNVPRSupport;
493 } else {
494 // We don't disable NVPR for instanced configs. Otherwise the caps wouldn't use mixed
495 // samples and we couldn't test the mixed samples backend for simple shapes.
496 fContextOverrides |= ContextOverrides::kDisableNVPR;
497 }
498 // Subtle logic: If the config has a color space attached, we're going to be rendering to sRGB,
499 // so we need that capability. In addition, to get the widest test coverage, we DO NOT require
500 // that we can disable sRGB decode. (That's for rendering sRGB sources to legacy surfaces).
501 //
502 // If the config doesn't have a color space attached, we're going to be rendering in legacy
503 // mode. In that case, we don't require sRGB capability and we defer to the client to decide on
504 // sRGB decode control.
505 if (fColorSpace) {
506 fContextOverrides |= ContextOverrides::kRequireSRGBSupport;
507 fContextOverrides |= ContextOverrides::kAllowSRGBWithoutDecodeControl;
508 }
509 if (!useStencilBuffers) {
510 fContextOverrides |= ContextOverrides::kAvoidStencilBuffers;
511 }
512}
kkinnunen3e980c32015-12-23 01:33:00 -0800513
514SkCommandLineConfigGpu* parse_command_line_config_gpu(const SkString& tag,
515 const SkTArray<SkString>& vias,
516 const SkString& options) {
517 // Defaults for GPU backend.
Brian Salomon6405e712017-03-20 08:54:16 -0400518 SkCommandLineConfigGpu::ContextType contextType = GrContextFactory::kGL_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800519 bool useNVPR = false;
kkinnunen3e980c32015-12-23 01:33:00 -0800520 bool useDIText = false;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500521 int samples = 1;
bsalomon33069252016-09-28 08:49:53 -0700522 SkColorType colorType = kRGBA_8888_SkColorType;
Brian Salomonce5ee602017-07-17 11:31:31 -0400523 SkAlphaType alphaType = kPremul_SkAlphaType;
brianosmanb109b8c2016-06-16 13:03:24 -0700524 sk_sp<SkColorSpace> colorSpace = nullptr;
Eric Karl5c779752017-05-08 12:02:07 -0700525 bool useStencils = true;
Brian Osmanf9810662017-08-30 10:02:10 -0400526 bool testThreading = false;
Brian Salomonf865b052018-03-09 09:01:53 -0500527 SkCommandLineConfigGpu::SurfType surfType = SkCommandLineConfigGpu::SurfType::kDefault;
kkinnunen3e980c32015-12-23 01:33:00 -0800528
Bryce Thomas95a7b762018-03-02 13:54:21 -0800529 bool parseSucceeded = false;
530 ExtendedOptions extendedOptions(options, &parseSucceeded);
531 if (!parseSucceeded) {
Brian Salomon6405e712017-03-20 08:54:16 -0400532 return nullptr;
533 }
Bryce Thomas95a7b762018-03-02 13:54:21 -0800534
535 bool validOptions =
536 extendedOptions.get_option_gpu_api("api", &contextType, false) &&
537 extendedOptions.get_option_bool("nvpr", &useNVPR) &&
538 extendedOptions.get_option_bool("dit", &useDIText) &&
539 extendedOptions.get_option_int("samples", &samples) &&
540 extendedOptions.get_option_gpu_color("color", &colorType, &alphaType, &colorSpace) &&
541 extendedOptions.get_option_bool("stencils", &useStencils) &&
Brian Salomonf865b052018-03-09 09:01:53 -0500542 extendedOptions.get_option_bool("testThreading", &testThreading) &&
543 extendedOptions.get_option_bool("testThreading", &testThreading) &&
544 extendedOptions.get_option_gpu_surf_type("surf", &surfType);
Bryce Thomas95a7b762018-03-02 13:54:21 -0800545
546 if (!validOptions) {
547 return nullptr;
548 }
549
Brian Salomonf865b052018-03-09 09:01:53 -0500550 return new SkCommandLineConfigGpu(tag, vias, contextType, useNVPR, useDIText, samples,
551 colorType, alphaType, colorSpace, useStencils, testThreading,
552 surfType);
kkinnunen3e980c32015-12-23 01:33:00 -0800553}
554#endif
555
Bryce Thomas95a7b762018-03-02 13:54:21 -0800556SkCommandLineConfigSvg::SkCommandLineConfigSvg(const SkString& tag,
557 const SkTArray<SkString>& viaParts, int pageIndex)
558 : SkCommandLineConfig(tag, SkString("svg"), viaParts), fPageIndex(pageIndex) {}
559
560SkCommandLineConfigSvg* parse_command_line_config_svg(const SkString& tag,
561 const SkTArray<SkString>& vias,
562 const SkString& options) {
563 // Defaults for SVG backend.
564 int pageIndex = 0;
565
566 bool parseSucceeded = false;
567 ExtendedOptions extendedOptions(options, &parseSucceeded);
568 if (!parseSucceeded) {
569 return nullptr;
570 }
571
572 bool validOptions = extendedOptions.get_option_int("page", &pageIndex);
573
574 if (!validOptions) {
575 return nullptr;
576 }
577
578 return new SkCommandLineConfigSvg(tag, vias, pageIndex);
579}
580
kkinnunen3e980c32015-12-23 01:33:00 -0800581void ParseConfigs(const SkCommandLineFlags::StringArray& configs,
582 SkCommandLineConfigArray* outResult) {
583 outResult->reset();
584 for (int i = 0; i < configs.count(); ++i) {
585 SkString extendedBackend;
586 SkString extendedOptions;
587 SkString simpleBackend;
588 SkTArray<SkString> vias;
589
590 SkString tag(configs[i]);
591 SkTArray<SkString> parts;
bsalomon808ecbb2016-09-28 12:40:22 -0700592 SkStrSplit(tag.c_str(), "[", kStrict_SkStrSplitMode, &parts);
kkinnunen3e980c32015-12-23 01:33:00 -0800593 if (parts.count() == 2) {
594 SkTArray<SkString> parts2;
bsalomon808ecbb2016-09-28 12:40:22 -0700595 SkStrSplit(parts[1].c_str(), "]", kStrict_SkStrSplitMode, &parts2);
kkinnunen3e980c32015-12-23 01:33:00 -0800596 if (parts2.count() == 2 && parts2[1].isEmpty()) {
597 SkStrSplit(parts[0].c_str(), "-", kStrict_SkStrSplitMode, &vias);
598 if (vias.count()) {
599 extendedBackend = vias[vias.count() - 1];
600 vias.pop_back();
601 } else {
602 extendedBackend = parts[0];
603 }
604 extendedOptions = parts2[0];
bsalomon808ecbb2016-09-28 12:40:22 -0700605 simpleBackend.printf("%s[%s]", extendedBackend.c_str(), extendedOptions.c_str());
kkinnunen3e980c32015-12-23 01:33:00 -0800606 }
607 }
608
609 if (extendedBackend.isEmpty()) {
610 simpleBackend = tag;
611 SkStrSplit(tag.c_str(), "-", kStrict_SkStrSplitMode, &vias);
612 if (vias.count()) {
613 simpleBackend = vias[vias.count() - 1];
614 vias.pop_back();
615 }
kkinnunen3e980c32015-12-23 01:33:00 -0800616 for (auto& predefinedConfig : gPredefinedConfigs) {
617 if (simpleBackend.equals(predefinedConfig.predefinedConfig)) {
618 extendedBackend = predefinedConfig.backend;
619 extendedOptions = predefinedConfig.options;
620 break;
621 }
622 }
623 }
624 SkCommandLineConfig* parsedConfig = nullptr;
625#if SK_SUPPORT_GPU
626 if (extendedBackend.equals("gpu")) {
627 parsedConfig = parse_command_line_config_gpu(tag, vias, extendedOptions);
628 }
629#endif
Bryce Thomas95a7b762018-03-02 13:54:21 -0800630 if (extendedBackend.equals("svg")) {
631 parsedConfig = parse_command_line_config_svg(tag, vias, extendedOptions);
632 }
kkinnunen3e980c32015-12-23 01:33:00 -0800633 if (!parsedConfig) {
634 parsedConfig = new SkCommandLineConfig(tag, simpleBackend, vias);
635 }
636 outResult->emplace_back(parsedConfig);
637 }
638}