blob: f70505b66275c07de85d58c4ee6fc89af849b2df [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" }
Brian Osman10fc6fd2018-03-02 11:01:10 -050089 ,{ "vk1010102", "gpu", "api=vulkan,color=1010102" }
Brian Salomon002c1202016-10-18 11:28:20 -040090 ,{ "vksrgb", "gpu", "api=vulkan,color=srgb" }
91 ,{ "vkwide", "gpu", "api=vulkan,color=f16_wide" }
92 ,{ "vkmsaa4", "gpu", "api=vulkan,samples=4" }
Brian Salomondcf0ab02017-03-20 11:10:21 -040093 ,{ "vkmsaa8", "gpu", "api=vulkan,samples=8" }
Brian Salomon52e943a2018-03-13 09:32:39 -040094 ,{ "vkbetex", "gpu", "api=vulkan,surf=betex" }
95 ,{ "vkbert", "gpu", "api=vulkan,surf=bert" }
brianosman37e23342016-09-20 08:06:30 -070096#endif
Greg Daniel2811aa22017-07-13 15:34:56 -040097#ifdef SK_METAL
98 ,{ "mtl", "gpu", "api=metal" }
Brian Osman10fc6fd2018-03-02 11:01:10 -050099 ,{ "mtl1010102", "gpu", "api=metal,color=1010102" }
Greg Daniel2811aa22017-07-13 15:34:56 -0400100 ,{ "mtlsrgb", "gpu", "api=metal,color=srgb" }
101 ,{ "mtlwide", "gpu", "api=metal,color=f16_wide" }
102 ,{ "mtlmsaa4", "gpu", "api=metal,samples=4" }
103 ,{ "mtlmsaa8", "gpu", "api=metal,samples=8" }
104#endif
brianosman37e23342016-09-20 08:06:30 -0700105#else
Brian Salomon8fe24272017-07-07 12:56:11 -0400106 { "", "", "" }
brianosman37e23342016-09-20 08:06:30 -0700107#endif
108};
109
110static const char configHelp[] =
Derek Sollenbergerc65386a2017-01-05 09:50:22 -0500111 "Options: 565 8888 srgb f16 nonrendering null pdf pdfa skp pipe svg xps";
brianosman37e23342016-09-20 08:06:30 -0700112
113static const char* config_help_fn() {
114 static SkString helpString;
115 helpString.set(configHelp);
116 for (const auto& config : gPredefinedConfigs) {
117 helpString.appendf(" %s", config.predefinedConfig);
118 }
bsalomon808ecbb2016-09-28 12:40:22 -0700119 helpString.append(" or use extended form 'backend[option=value,...]'.\n");
brianosman37e23342016-09-20 08:06:30 -0700120 return helpString.c_str();
121}
kkinnunen3e980c32015-12-23 01:33:00 -0800122
123static const char configExtendedHelp[] =
124 "Extended form: 'backend(option=value,...)'\n\n"
125 "Possible backends and options:\n"
126#if SK_SUPPORT_GPU
127 "\n"
bsalomon808ecbb2016-09-28 12:40:22 -0700128 "gpu[api=string,color=string,dit=bool,nvpr=bool,inst=bool,samples=int]\n"
Brian Salomon6405e712017-03-20 08:54:16 -0400129 "\tapi\ttype: string\trequired\n"
kkinnunen3e980c32015-12-23 01:33:00 -0800130 "\t Select graphics API to use with gpu backend.\n"
131 "\t Options:\n"
kkinnunen3e980c32015-12-23 01:33:00 -0800132 "\t\tgl \t\t\tUse OpenGL.\n"
133 "\t\tgles \t\t\tUse OpenGL ES.\n"
Brian Salomon6405e712017-03-20 08:54:16 -0400134 "\t\tdebuggl \t\t\tUse debug OpenGL.\n"
135 "\t\tnullgl \t\t\tUse null OpenGL.\n"
bsalomon11abd8d2016-10-14 08:13:48 -0700136 "\t\tangle_d3d9_es2\t\t\tUse OpenGL ES2 on the ANGLE Direct3D9 backend.\n"
137 "\t\tangle_d3d11_es2\t\t\tUse OpenGL ES2 on the ANGLE Direct3D11 backend.\n"
138 "\t\tangle_d3d11_es3\t\t\tUse OpenGL ES3 on the ANGLE Direct3D11 backend.\n"
139 "\t\tangle_gl_es2\t\t\tUse OpenGL ES2 on the ANGLE OpenGL backend.\n"
140 "\t\tangle_gl_es3\t\t\tUse OpenGL ES3 on the ANGLE OpenGL backend.\n"
kkinnunen3e980c32015-12-23 01:33:00 -0800141 "\t\tcommandbuffer\t\tUse command buffer.\n"
Brian Salomon8fe24272017-07-07 12:56:11 -0400142 "\t\tmock\t\tUse mock context.\n"
bsalomondc0fcd42016-04-11 14:21:33 -0700143#ifdef SK_VULKAN
144 "\t\tvulkan\t\t\tUse Vulkan.\n"
145#endif
Greg Daniel2811aa22017-07-13 15:34:56 -0400146#ifdef SK_METAL
147 "\t\tmetal\t\t\tUse Metal.\n"
148#endif
brianosmand93c1202016-03-10 07:49:08 -0800149 "\tcolor\ttype: string\tdefault: 8888.\n"
150 "\t Select framebuffer color format.\n"
151 "\t Options:\n"
152 "\t\t8888\t\t\tLinear 8888.\n"
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400153 "\t\t888x\t\t\tLinear 888x.\n"
Brian Salomonce5ee602017-07-17 11:31:31 -0400154 "\t\t4444\t\t\tLinear 4444.\n"
155 "\t\t565\t\t\tLinear 565.\n"
brianosman4562f6e2016-09-19 14:42:04 -0700156 "\t\tf16{_gamut}\t\tLinear 16-bit floating point.\n"
Brian Osman10fc6fd2018-03-02 11:01:10 -0500157 "\t\t1010102\t\tLinear 1010102.\n"
brianosman4562f6e2016-09-19 14:42:04 -0700158 "\t\tsrgb{_gamut}\t\tsRGB 8888.\n"
159 "\t gamut\ttype: string\tdefault: srgb.\n"
160 "\t Select color gamut for f16 or sRGB format buffers.\n"
161 "\t Options:\n"
162 "\t\tsrgb\t\t\tsRGB gamut.\n"
163 "\t\twide\t\t\tWide Gamut RGB.\n"
kkinnunen3e980c32015-12-23 01:33:00 -0800164 "\tdit\ttype: bool\tdefault: false.\n"
165 "\t Use device independent text.\n"
166 "\tnvpr\ttype: bool\tdefault: false.\n"
167 "\t Use NV_path_rendering OpenGL and OpenGL ES extension.\n"
168 "\tsamples\ttype: int\tdefault: 0.\n"
169 "\t Use multisampling with N samples.\n"
Eric Karl5c779752017-05-08 12:02:07 -0700170 "\tstencils\ttype: bool\tdefault: true.\n"
171 "\t Allow the use of stencil buffers.\n"
Brian Osmanf9810662017-08-30 10:02:10 -0400172 "\ttestThreading\ttype: bool\tdefault: false.\n"
173 "\t Run config with and without worker threads, check that results match.\n"
Brian Salomonf865b052018-03-09 09:01:53 -0500174 "\tsurf\ttype: string\tdefault: default.\n"
175 "\t Controls the type of backing store for SkSurfaces.\n"
176 "\t Options:\n"
177 "\t\tdefault\t\t\tA renderable texture created in Skia's resource cache.\n"
178 "\t\tbetex\t\t\tA wrapped backend texture.\n"
179 "\t\tbert\t\t\tA wrapped backend render target\n"
kkinnunen3e980c32015-12-23 01:33:00 -0800180 "\n"
181 "Predefined configs:\n\n"
brianosman37e23342016-09-20 08:06:30 -0700182 // Help text for pre-defined configs is auto-generated from gPredefinedConfigs
kkinnunen3e980c32015-12-23 01:33:00 -0800183#endif
184 ;
185
brianosman37e23342016-09-20 08:06:30 -0700186static const char* config_extended_help_fn() {
187 static SkString helpString;
188 helpString.set(configExtendedHelp);
189 for (const auto& config : gPredefinedConfigs) {
190 helpString.appendf("\t%-10s\t= gpu(%s)\n", config.predefinedConfig, config.options);
191 }
192 return helpString.c_str();
193}
kkinnunen3e980c32015-12-23 01:33:00 -0800194
brianosman37e23342016-09-20 08:06:30 -0700195DEFINE_extended_string(config, defaultConfigs, config_help_fn(), config_extended_help_fn());
kkinnunen3e980c32015-12-23 01:33:00 -0800196
197SkCommandLineConfig::SkCommandLineConfig(const SkString& tag, const SkString& backend,
198 const SkTArray<SkString>& viaParts)
199 : fTag(tag)
200 , fBackend(backend)
201 , fViaParts(viaParts) {
202}
203SkCommandLineConfig::~SkCommandLineConfig() {
204}
205
kkinnunen3e980c32015-12-23 01:33:00 -0800206static bool parse_option_int(const SkString& value, int* outInt) {
207 if (value.isEmpty()) {
208 return false;
209 }
210 char* endptr = nullptr;
211 long intValue = strtol(value.c_str(), &endptr, 10);
212 if (*endptr != '\0') {
213 return false;
214 }
215 *outInt = static_cast<int>(intValue);
216 return true;
217}
218static bool parse_option_bool(const SkString& value, bool* outBool) {
219 if (value.equals("true")) {
220 *outBool = true;
221 return true;
222 }
223 if (value.equals("false")) {
224 *outBool = false;
225 return true;
226 }
227 return false;
228}
Bryce Thomas95a7b762018-03-02 13:54:21 -0800229#if SK_SUPPORT_GPU
kkinnunen3e980c32015-12-23 01:33:00 -0800230static bool parse_option_gpu_api(const SkString& value,
231 SkCommandLineConfigGpu::ContextType* outContextType) {
kkinnunen3e980c32015-12-23 01:33:00 -0800232 if (value.equals("gl")) {
bsalomon85b4b532016-04-05 11:06:27 -0700233 *outContextType = GrContextFactory::kGL_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800234 return true;
235 }
236 if (value.equals("gles")) {
bsalomon85b4b532016-04-05 11:06:27 -0700237 *outContextType = GrContextFactory::kGLES_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800238 return true;
239 }
Brian Salomon6405e712017-03-20 08:54:16 -0400240 if (value.equals("debuggl")) {
bsalomon85b4b532016-04-05 11:06:27 -0700241 *outContextType = GrContextFactory::kDebugGL_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800242 return true;
243 }
Brian Salomon6405e712017-03-20 08:54:16 -0400244 if (value.equals("nullgl")) {
bsalomon85b4b532016-04-05 11:06:27 -0700245 *outContextType = GrContextFactory::kNullGL_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800246 return true;
247 }
bsalomon11abd8d2016-10-14 08:13:48 -0700248 if (value.equals("angle_d3d9_es2")) {
249 *outContextType = GrContextFactory::kANGLE_D3D9_ES2_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800250 return true;
251 }
bsalomon11abd8d2016-10-14 08:13:48 -0700252 if (value.equals("angle_d3d11_es2")) {
253 *outContextType = GrContextFactory::kANGLE_D3D11_ES2_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800254 return true;
255 }
bsalomon11abd8d2016-10-14 08:13:48 -0700256 if (value.equals("angle_d3d11_es3")) {
257 *outContextType = GrContextFactory::kANGLE_D3D11_ES3_ContextType;
258 return true;
259 }
260 if (value.equals("angle_gl_es2")) {
261 *outContextType = GrContextFactory::kANGLE_GL_ES2_ContextType;
262 return true;
263 }
264 if (value.equals("angle_gl_es3")) {
265 *outContextType = GrContextFactory::kANGLE_GL_ES3_ContextType;
266 return true;
267 }
kkinnunen3e980c32015-12-23 01:33:00 -0800268 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 }
Brian Salomon8fe24272017-07-07 12:56:11 -0400272 if (value.equals("mock")) {
273 *outContextType = GrContextFactory::kMock_ContextType;
274 return true;
275 }
bsalomondc0fcd42016-04-11 14:21:33 -0700276#ifdef SK_VULKAN
277 if (value.equals("vulkan")) {
278 *outContextType = GrContextFactory::kVulkan_ContextType;
279 return true;
280 }
281#endif
Greg Daniel2811aa22017-07-13 15:34:56 -0400282#ifdef SK_METAL
283 if (value.equals("metal")) {
284 *outContextType = GrContextFactory::kMetal_ContextType;
285 return true;
286 }
287#endif
kkinnunen3e980c32015-12-23 01:33:00 -0800288 return false;
289}
Brian Salomonf865b052018-03-09 09:01:53 -0500290
brianosmand93c1202016-03-10 07:49:08 -0800291static bool parse_option_gpu_color(const SkString& value,
292 SkColorType* outColorType,
Brian Salomonce5ee602017-07-17 11:31:31 -0400293 SkAlphaType* alphaType,
brianosmanb109b8c2016-06-16 13:03:24 -0700294 sk_sp<SkColorSpace>* outColorSpace) {
Brian Salomonce5ee602017-07-17 11:31:31 -0400295 // We always use premul unless the color type is 565.
296 *alphaType = kPremul_SkAlphaType;
297
brianosmand93c1202016-03-10 07:49:08 -0800298 if (value.equals("8888")) {
bsalomon33069252016-09-28 08:49:53 -0700299 *outColorType = kRGBA_8888_SkColorType;
brianosmanb109b8c2016-06-16 13:03:24 -0700300 *outColorSpace = nullptr;
brianosmand93c1202016-03-10 07:49:08 -0800301 return true;
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400302 } else if (value.equals("888x")) {
303 *outColorType = kRGB_888x_SkColorType;
304 *outColorSpace = nullptr;
305 return true;
Brian Salomonce5ee602017-07-17 11:31:31 -0400306 } else if (value.equals("4444")) {
307 *outColorType = kARGB_4444_SkColorType;
308 *outColorSpace = nullptr;
309 return true;
310 } else if (value.equals("565")) {
311 *outColorType = kRGB_565_SkColorType;
312 *alphaType = kOpaque_SkAlphaType;
313 *outColorSpace = nullptr;
314 return true;
Brian Osman10fc6fd2018-03-02 11:01:10 -0500315 } else if (value.equals("1010102")) {
316 *outColorType = kRGBA_1010102_SkColorType;
317 *outColorSpace = nullptr;
318 return true;
brianosmand93c1202016-03-10 07:49:08 -0800319 }
brianosman4562f6e2016-09-19 14:42:04 -0700320
321 SkTArray<SkString> commands;
322 SkStrSplit(value.c_str(), "_", &commands);
323 if (commands.count() < 1 || commands.count() > 2) {
324 return false;
325 }
326
raftias94888332016-10-18 10:02:51 -0700327 const bool linearGamma = commands[0].equals("f16");
Matt Sarettd2228302017-03-02 08:53:46 -0500328 SkColorSpace::Gamut gamut = SkColorSpace::kSRGB_Gamut;
329 SkColorSpace::RenderTargetGamma gamma = linearGamma ? SkColorSpace::kLinear_RenderTargetGamma
330 : SkColorSpace::kSRGB_RenderTargetGamma;
Matt Sarettf3880932017-03-24 10:06:03 -0400331 *outColorSpace = SkColorSpace::MakeRGB(gamma, gamut);
Matt Sarettd2228302017-03-02 08:53:46 -0500332
brianosman4562f6e2016-09-19 14:42:04 -0700333 if (commands.count() == 2) {
334 if (commands[1].equals("srgb")) {
335 // sRGB gamut (which is our default)
336 } else if (commands[1].equals("wide")) {
337 // WideGamut RGB
338 const float gWideGamutRGB_toXYZD50[]{
339 0.7161046f, 0.1009296f, 0.1471858f, // -> X
340 0.2581874f, 0.7249378f, 0.0168748f, // -> Y
341 0.0000000f, 0.0517813f, 0.7734287f, // -> Z
342 };
343 SkMatrix44 wideGamutRGBMatrix(SkMatrix44::kUninitialized_Constructor);
344 wideGamutRGBMatrix.set3x3RowMajorf(gWideGamutRGB_toXYZD50);
Matt Sarettf3880932017-03-24 10:06:03 -0400345 *outColorSpace = SkColorSpace::MakeRGB(gamma, wideGamutRGBMatrix);
Brian Osman4a6b28e2016-10-17 11:14:02 -0400346 } else if (commands[1].equals("narrow")) {
347 // NarrowGamut RGB (an artifically smaller than sRGB gamut)
348 SkColorSpacePrimaries primaries ={
349 0.54f, 0.33f, // Rx, Ry
350 0.33f, 0.50f, // Gx, Gy
351 0.25f, 0.20f, // Bx, By
352 0.3127f, 0.3290f, // Wx, Wy
353 };
354 SkMatrix44 narrowGamutRGBMatrix(SkMatrix44::kUninitialized_Constructor);
355 primaries.toXYZD50(&narrowGamutRGBMatrix);
Matt Sarettf3880932017-03-24 10:06:03 -0400356 *outColorSpace = SkColorSpace::MakeRGB(gamma, narrowGamutRGBMatrix);
brianosman4562f6e2016-09-19 14:42:04 -0700357 } else {
358 // Unknown color gamut
359 return false;
360 }
361 }
362
363 // Now pick a color type
364 if (commands[0].equals("f16")) {
brianosmand93c1202016-03-10 07:49:08 -0800365 *outColorType = kRGBA_F16_SkColorType;
brianosmand93c1202016-03-10 07:49:08 -0800366 return true;
367 }
Matt Sarettd2228302017-03-02 08:53:46 -0500368 if (commands[0].equals("srgb") || commands[0].equals("srgbnl")) {
bsalomon33069252016-09-28 08:49:53 -0700369 *outColorType = kRGBA_8888_SkColorType;
brianosmand93c1202016-03-10 07:49:08 -0800370 return true;
371 }
372 return false;
373}
Brian Salomonf865b052018-03-09 09:01:53 -0500374
375static bool parse_option_gpu_surf_type(const SkString& value,
376 SkCommandLineConfigGpu::SurfType* surfType) {
377 if (value.equals("default")) {
378 *surfType = SkCommandLineConfigGpu::SurfType::kDefault;
379 return true;
380 }
381 if (value.equals("betex")) {
382 *surfType = SkCommandLineConfigGpu::SurfType::kBackendTexture;
383 return true;
384 }
385 if (value.equals("bert")) {
386 *surfType = SkCommandLineConfigGpu::SurfType::kBackendRenderTarget;
387 return true;
388 }
389 return false;
390}
Bryce Thomas95a7b762018-03-02 13:54:21 -0800391#endif
392
393// Extended options take form --config item[key1=value1,key2=value2,...]
394// Example: --config gpu[api=gl,color=8888]
395class ExtendedOptions {
396public:
397 ExtendedOptions(const SkString& optionsString, bool* outParseSucceeded) {
398 SkTArray<SkString> optionParts;
399 SkStrSplit(optionsString.c_str(), ",", kStrict_SkStrSplitMode, &optionParts);
400 for (int i = 0; i < optionParts.count(); ++i) {
401 SkTArray<SkString> keyValueParts;
402 SkStrSplit(optionParts[i].c_str(), "=", kStrict_SkStrSplitMode, &keyValueParts);
403 if (keyValueParts.count() != 2) {
404 *outParseSucceeded = false;
405 return;
406 }
407 const SkString& key = keyValueParts[0];
408 const SkString& value = keyValueParts[1];
409 if (fOptionsMap.find(key) == nullptr) {
410 fOptionsMap.set(key, value);
411 } else {
412 // Duplicate values are not allowed.
413 *outParseSucceeded = false;
414 return;
415 }
416 }
417 *outParseSucceeded = true;
418 }
419
420#if SK_SUPPORT_GPU
421 bool get_option_gpu_color(const char* optionKey,
422 SkColorType* outColorType,
423 SkAlphaType* alphaType,
424 sk_sp<SkColorSpace>* outColorSpace,
425 bool optional = true) const {
426 SkString* optionValue = fOptionsMap.find(SkString(optionKey));
427 if (optionValue == nullptr) {
428 return optional;
429 }
430 return parse_option_gpu_color(*optionValue, outColorType, alphaType, outColorSpace);
431 }
432
433 bool get_option_gpu_api(const char* optionKey,
434 SkCommandLineConfigGpu::ContextType* outContextType,
435 bool optional = true) const {
436 SkString* optionValue = fOptionsMap.find(SkString(optionKey));
437 if (optionValue == nullptr) {
438 return optional;
439 }
440 return parse_option_gpu_api(*optionValue, outContextType);
441 }
Brian Salomonf865b052018-03-09 09:01:53 -0500442
443 bool get_option_gpu_surf_type(const char* optionKey,
444 SkCommandLineConfigGpu::SurfType* outSurfType,
445 bool optional = true) const {
446 SkString* optionValue = fOptionsMap.find(SkString(optionKey));
447 if (optionValue == nullptr) {
448 return optional;
449 }
450 return parse_option_gpu_surf_type(*optionValue, outSurfType);
451 }
Bryce Thomas95a7b762018-03-02 13:54:21 -0800452#endif
453
454 bool get_option_int(const char* optionKey, int* outInt, bool optional = true) const {
455 SkString* optionValue = fOptionsMap.find(SkString(optionKey));
456 if (optionValue == nullptr) {
457 return optional;
458 }
459 return parse_option_int(*optionValue, outInt);
460 }
461
462 bool get_option_bool(const char* optionKey, bool* outBool, bool optional = true) const {
463 SkString* optionValue = fOptionsMap.find(SkString(optionKey));
464 if (optionValue == nullptr) {
465 return optional;
466 }
467 return parse_option_bool(*optionValue, outBool);
468 }
469
470private:
471 SkTHashMap<SkString, SkString> fOptionsMap;
472};
473
474#if SK_SUPPORT_GPU
475SkCommandLineConfigGpu::SkCommandLineConfigGpu(
Brian Salomonf865b052018-03-09 09:01:53 -0500476 const SkString& tag, const SkTArray<SkString>& viaParts, ContextType contextType,
477 bool useNVPR, bool useDIText, int samples, SkColorType colorType, SkAlphaType alphaType,
478 sk_sp<SkColorSpace> colorSpace, bool useStencilBuffers, bool testThreading,
479 SurfType surfType)
Bryce Thomas95a7b762018-03-02 13:54:21 -0800480 : SkCommandLineConfig(tag, SkString("gpu"), viaParts)
481 , fContextType(contextType)
482 , fContextOverrides(ContextOverrides::kNone)
483 , fUseDIText(useDIText)
484 , fSamples(samples)
485 , fColorType(colorType)
486 , fAlphaType(alphaType)
487 , fColorSpace(std::move(colorSpace))
Brian Salomonf865b052018-03-09 09:01:53 -0500488 , fTestThreading(testThreading)
489 , fSurfType(surfType) {
Bryce Thomas95a7b762018-03-02 13:54:21 -0800490 if (useNVPR) {
491 fContextOverrides |= ContextOverrides::kRequireNVPRSupport;
492 } else {
493 // We don't disable NVPR for instanced configs. Otherwise the caps wouldn't use mixed
494 // samples and we couldn't test the mixed samples backend for simple shapes.
495 fContextOverrides |= ContextOverrides::kDisableNVPR;
496 }
497 // Subtle logic: If the config has a color space attached, we're going to be rendering to sRGB,
498 // so we need that capability. In addition, to get the widest test coverage, we DO NOT require
499 // that we can disable sRGB decode. (That's for rendering sRGB sources to legacy surfaces).
500 //
501 // If the config doesn't have a color space attached, we're going to be rendering in legacy
502 // mode. In that case, we don't require sRGB capability and we defer to the client to decide on
503 // sRGB decode control.
504 if (fColorSpace) {
505 fContextOverrides |= ContextOverrides::kRequireSRGBSupport;
506 fContextOverrides |= ContextOverrides::kAllowSRGBWithoutDecodeControl;
507 }
508 if (!useStencilBuffers) {
509 fContextOverrides |= ContextOverrides::kAvoidStencilBuffers;
510 }
511}
kkinnunen3e980c32015-12-23 01:33:00 -0800512
513SkCommandLineConfigGpu* parse_command_line_config_gpu(const SkString& tag,
514 const SkTArray<SkString>& vias,
515 const SkString& options) {
516 // Defaults for GPU backend.
Brian Salomon6405e712017-03-20 08:54:16 -0400517 SkCommandLineConfigGpu::ContextType contextType = GrContextFactory::kGL_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800518 bool useNVPR = false;
kkinnunen3e980c32015-12-23 01:33:00 -0800519 bool useDIText = false;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500520 int samples = 1;
bsalomon33069252016-09-28 08:49:53 -0700521 SkColorType colorType = kRGBA_8888_SkColorType;
Brian Salomonce5ee602017-07-17 11:31:31 -0400522 SkAlphaType alphaType = kPremul_SkAlphaType;
brianosmanb109b8c2016-06-16 13:03:24 -0700523 sk_sp<SkColorSpace> colorSpace = nullptr;
Eric Karl5c779752017-05-08 12:02:07 -0700524 bool useStencils = true;
Brian Osmanf9810662017-08-30 10:02:10 -0400525 bool testThreading = false;
Brian Salomonf865b052018-03-09 09:01:53 -0500526 SkCommandLineConfigGpu::SurfType surfType = SkCommandLineConfigGpu::SurfType::kDefault;
kkinnunen3e980c32015-12-23 01:33:00 -0800527
Bryce Thomas95a7b762018-03-02 13:54:21 -0800528 bool parseSucceeded = false;
529 ExtendedOptions extendedOptions(options, &parseSucceeded);
530 if (!parseSucceeded) {
Brian Salomon6405e712017-03-20 08:54:16 -0400531 return nullptr;
532 }
Bryce Thomas95a7b762018-03-02 13:54:21 -0800533
534 bool validOptions =
535 extendedOptions.get_option_gpu_api("api", &contextType, false) &&
536 extendedOptions.get_option_bool("nvpr", &useNVPR) &&
537 extendedOptions.get_option_bool("dit", &useDIText) &&
538 extendedOptions.get_option_int("samples", &samples) &&
539 extendedOptions.get_option_gpu_color("color", &colorType, &alphaType, &colorSpace) &&
540 extendedOptions.get_option_bool("stencils", &useStencils) &&
Brian Salomonf865b052018-03-09 09:01:53 -0500541 extendedOptions.get_option_bool("testThreading", &testThreading) &&
542 extendedOptions.get_option_bool("testThreading", &testThreading) &&
543 extendedOptions.get_option_gpu_surf_type("surf", &surfType);
Bryce Thomas95a7b762018-03-02 13:54:21 -0800544
545 if (!validOptions) {
546 return nullptr;
547 }
548
Brian Salomonf865b052018-03-09 09:01:53 -0500549 return new SkCommandLineConfigGpu(tag, vias, contextType, useNVPR, useDIText, samples,
550 colorType, alphaType, colorSpace, useStencils, testThreading,
551 surfType);
kkinnunen3e980c32015-12-23 01:33:00 -0800552}
553#endif
554
Bryce Thomas95a7b762018-03-02 13:54:21 -0800555SkCommandLineConfigSvg::SkCommandLineConfigSvg(const SkString& tag,
556 const SkTArray<SkString>& viaParts, int pageIndex)
557 : SkCommandLineConfig(tag, SkString("svg"), viaParts), fPageIndex(pageIndex) {}
558
559SkCommandLineConfigSvg* parse_command_line_config_svg(const SkString& tag,
560 const SkTArray<SkString>& vias,
561 const SkString& options) {
562 // Defaults for SVG backend.
563 int pageIndex = 0;
564
565 bool parseSucceeded = false;
566 ExtendedOptions extendedOptions(options, &parseSucceeded);
567 if (!parseSucceeded) {
568 return nullptr;
569 }
570
571 bool validOptions = extendedOptions.get_option_int("page", &pageIndex);
572
573 if (!validOptions) {
574 return nullptr;
575 }
576
577 return new SkCommandLineConfigSvg(tag, vias, pageIndex);
578}
579
kkinnunen3e980c32015-12-23 01:33:00 -0800580void ParseConfigs(const SkCommandLineFlags::StringArray& configs,
581 SkCommandLineConfigArray* outResult) {
582 outResult->reset();
583 for (int i = 0; i < configs.count(); ++i) {
584 SkString extendedBackend;
585 SkString extendedOptions;
586 SkString simpleBackend;
587 SkTArray<SkString> vias;
588
589 SkString tag(configs[i]);
590 SkTArray<SkString> parts;
bsalomon808ecbb2016-09-28 12:40:22 -0700591 SkStrSplit(tag.c_str(), "[", kStrict_SkStrSplitMode, &parts);
kkinnunen3e980c32015-12-23 01:33:00 -0800592 if (parts.count() == 2) {
593 SkTArray<SkString> parts2;
bsalomon808ecbb2016-09-28 12:40:22 -0700594 SkStrSplit(parts[1].c_str(), "]", kStrict_SkStrSplitMode, &parts2);
kkinnunen3e980c32015-12-23 01:33:00 -0800595 if (parts2.count() == 2 && parts2[1].isEmpty()) {
596 SkStrSplit(parts[0].c_str(), "-", kStrict_SkStrSplitMode, &vias);
597 if (vias.count()) {
598 extendedBackend = vias[vias.count() - 1];
599 vias.pop_back();
600 } else {
601 extendedBackend = parts[0];
602 }
603 extendedOptions = parts2[0];
bsalomon808ecbb2016-09-28 12:40:22 -0700604 simpleBackend.printf("%s[%s]", extendedBackend.c_str(), extendedOptions.c_str());
kkinnunen3e980c32015-12-23 01:33:00 -0800605 }
606 }
607
608 if (extendedBackend.isEmpty()) {
609 simpleBackend = tag;
610 SkStrSplit(tag.c_str(), "-", kStrict_SkStrSplitMode, &vias);
611 if (vias.count()) {
612 simpleBackend = vias[vias.count() - 1];
613 vias.pop_back();
614 }
kkinnunen3e980c32015-12-23 01:33:00 -0800615 for (auto& predefinedConfig : gPredefinedConfigs) {
616 if (simpleBackend.equals(predefinedConfig.predefinedConfig)) {
617 extendedBackend = predefinedConfig.backend;
618 extendedOptions = predefinedConfig.options;
619 break;
620 }
621 }
622 }
623 SkCommandLineConfig* parsedConfig = nullptr;
624#if SK_SUPPORT_GPU
625 if (extendedBackend.equals("gpu")) {
626 parsedConfig = parse_command_line_config_gpu(tag, vias, extendedOptions);
627 }
628#endif
Bryce Thomas95a7b762018-03-02 13:54:21 -0800629 if (extendedBackend.equals("svg")) {
630 parsedConfig = parse_command_line_config_svg(tag, vias, extendedOptions);
631 }
kkinnunen3e980c32015-12-23 01:33:00 -0800632 if (!parsedConfig) {
633 parsedConfig = new SkCommandLineConfig(tag, simpleBackend, vias);
634 }
635 outResult->emplace_back(parsedConfig);
636 }
637}