blob: c99aaa345046a9df5e45f93bfcfba8ff85a598d5 [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
Mike Klein88544fb2019-03-20 10:50:33 -05008#include "CommonFlagsConfig.h"
Brian Osmana66f4da2018-06-28 17:52:02 +00009#include "SkColorSpacePriv.h"
csmartdalton6270e552016-09-13 10:41:49 -070010#include "SkImageInfo.h"
Bryce Thomas95a7b762018-03-02 13:54:21 -080011#include "SkTHash.h"
kkinnunen3e980c32015-12-23 01:33:00 -080012
13#include <stdlib.h>
14
bsalomon3724e572016-03-30 18:56:19 -070015using sk_gpu_test::GrContextFactory;
bsalomon3724e572016-03-30 18:56:19 -070016
Brian Salomon6405e712017-03-20 08:54:16 -040017#if defined(SK_BUILD_FOR_ANDROID) || defined(SK_BUILD_FOR_IOS)
Mike Klein88544fb2019-03-20 10:50:33 -050018#define DEFAULT_GPU_CONFIG "gles"
Brian Salomon6405e712017-03-20 08:54:16 -040019#else
Mike Klein88544fb2019-03-20 10:50:33 -050020#define DEFAULT_GPU_CONFIG "gl"
Brian Salomon6405e712017-03-20 08:54:16 -040021#endif
22
Mike Klein88544fb2019-03-20 10:50:33 -050023static const char defaultConfigs[] = "8888 " DEFAULT_GPU_CONFIG
24 " nonrendering "
bsalomon11abd8d2016-10-14 08:13:48 -070025#if defined(SK_BUILD_FOR_WIN)
Mike Klein88544fb2019-03-20 10:50:33 -050026 " angle_d3d11_es2"
kkinnunen3e980c32015-12-23 01:33:00 -080027#endif
Mike Klein88544fb2019-03-20 10:50:33 -050028 ;
kkinnunen3e980c32015-12-23 01:33:00 -080029
Brian Salomon6405e712017-03-20 08:54:16 -040030#undef DEFAULT_GPU_CONFIG
31
Brian Salomon00a5eb82018-07-11 15:32:05 -040032// clang-format off
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[] = {
Brian Salomon002c1202016-10-18 11:28:20 -040038 { "gl", "gpu", "api=gl" },
Brian Salomon50f66d82017-03-17 14:32:05 -040039 { "gles", "gpu", "api=gles" },
Brian Salomon002c1202016-10-18 11:28:20 -040040 { "glmsaa4", "gpu", "api=gl,samples=4" },
Brian Salomondcf0ab02017-03-20 11:10:21 -040041 { "glmsaa8" , "gpu", "api=gl,samples=8" },
Brian Salomon50f66d82017-03-17 14:32:05 -040042 { "glesmsaa4", "gpu", "api=gles,samples=4" },
Brian Salomon002c1202016-10-18 11:28:20 -040043 { "glnvpr4", "gpu", "api=gl,nvpr=true,samples=4" },
Brian Salomondcf0ab02017-03-20 11:10:21 -040044 { "glnvpr8" , "gpu", "api=gl,nvpr=true,samples=8" },
Brian Salomon50f66d82017-03-17 14:32:05 -040045 { "glesnvpr4", "gpu", "api=gles,nvpr=true,samples=4" },
Brian Salomonf865b052018-03-09 09:01:53 -050046 { "glbetex", "gpu", "api=gl,surf=betex" },
47 { "glesbetex", "gpu", "api=gles,surf=betex" },
48 { "glbert", "gpu", "api=gl,surf=bert" },
49 { "glesbert", "gpu", "api=gles,surf=bert" },
Brian Salomonce5ee602017-07-17 11:31:31 -040050 { "gl4444", "gpu", "api=gl,color=4444" },
Brian Osmana15cedb2018-05-30 15:36:38 -040051 { "gles4444", "gpu", "api=gles,color=4444" },
Brian Salomonce5ee602017-07-17 11:31:31 -040052 { "gl565", "gpu", "api=gl,color=565" },
Brian Salomon5fba7ad2018-03-22 10:01:16 -040053 { "gl888x", "gpu", "api=gl,color=888x" },
Brian Osmana15cedb2018-05-30 15:36:38 -040054 { "gles888x", "gpu", "api=gles,color=888x" },
Brian Osman10fc6fd2018-03-02 11:01:10 -050055 { "gl1010102", "gpu", "api=gl,color=1010102" },
Brian Osmana15cedb2018-05-30 15:36:38 -040056 { "gles1010102", "gpu", "api=gles,color=1010102" },
Brian Salomon6405e712017-03-20 08:54:16 -040057 { "glsrgb", "gpu", "api=gl,color=srgb" },
Brian Osmana76b7a92018-07-18 14:36:43 -040058 { "glp3", "gpu", "api=gl,color=p3" },
Brian Osmana66f4da2018-06-28 17:52:02 +000059 { "glesrgb", "gpu", "api=gl,color=esrgb" },
60 { "glnarrow", "gpu", "api=gl,color=narrow" },
61 { "glenarrow", "gpu", "api=gl,color=enarrow" },
Brian Osmana15cedb2018-05-30 15:36:38 -040062 { "glf16", "gpu", "api=gl,color=f16" },
Brian Osmand0163fb2019-03-06 13:40:40 -050063 { "glf16norm", "gpu", "api=gl,color=f16norm" },
Brian Salomon6405e712017-03-20 08:54:16 -040064 { "glessrgb", "gpu", "api=gles,color=srgb" },
Brian Osmana66f4da2018-06-28 17:52:02 +000065 { "glesesrgb", "gpu", "api=gles,color=esrgb" },
66 { "glesnarrow", "gpu", "api=gles,color=narrow" },
67 { "glesenarrow", "gpu", "api=gles,color=enarrow" },
68 { "glesf16", "gpu", "api=gles,color=f16" },
Eric Karl5c779752017-05-08 12:02:07 -070069 { "glnostencils", "gpu", "api=gl,stencils=false" },
Brian Salomon50f66d82017-03-17 14:32:05 -040070 { "gldft", "gpu", "api=gl,dit=true" },
71 { "glesdft", "gpu", "api=gles,dit=true" },
Brian Osmanf9810662017-08-30 10:02:10 -040072 { "gltestthreading", "gpu", "api=gl,testThreading=true" },
Brian Salomon00a5eb82018-07-11 15:32:05 -040073 { "gltestpersistentcache", "gpu", "api=gl,testPersistentCache=true" },
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" },
Ethan Nicholas92e01cf2018-12-19 13:12:10 -050086 { "mock", "gpu", "api=mock" },
brianosman37e23342016-09-20 08:06:30 -070087#ifdef SK_VULKAN
Ethan Nicholas92e01cf2018-12-19 13:12:10 -050088 { "vk", "gpu", "api=vulkan" },
89 { "vknostencils", "gpu", "api=vulkan,stencils=false" },
90 { "vk1010102", "gpu", "api=vulkan,color=1010102" },
91 { "vksrgb", "gpu", "api=vulkan,color=srgb" },
92 { "vkesrgb", "gpu", "api=vulkan,color=esrgb" },
93 { "vknarrow", "gpu", "api=vulkan,color=narrow" },
94 { "vkenarrow", "gpu", "api=vulkan,color=enarrow" },
95 { "vkf16", "gpu", "api=vulkan,color=f16" },
96 { "vkmsaa4", "gpu", "api=vulkan,samples=4" },
97 { "vkmsaa8", "gpu", "api=vulkan,samples=8" },
98 { "vkbetex", "gpu", "api=vulkan,surf=betex" },
99 { "vkbert", "gpu", "api=vulkan,surf=bert" },
100 { "vktestpersistentcache", "gpu", "api=vulkan,testPersistentCache=true" },
brianosman37e23342016-09-20 08:06:30 -0700101#endif
Greg Daniel2811aa22017-07-13 15:34:56 -0400102#ifdef SK_METAL
Ethan Nicholas92e01cf2018-12-19 13:12:10 -0500103 { "mtl", "gpu", "api=metal" },
104 { "mtl1010102", "gpu", "api=metal,color=1010102" },
105 { "mtlmsaa4", "gpu", "api=metal,samples=4" },
106 { "mtlmsaa8", "gpu", "api=metal,samples=8" },
Greg Daniel2811aa22017-07-13 15:34:56 -0400107#endif
brianosman37e23342016-09-20 08:06:30 -0700108};
Brian Salomon00a5eb82018-07-11 15:32:05 -0400109// clang-format on
brianosman37e23342016-09-20 08:06:30 -0700110
111static const char configHelp[] =
Mike Klein88544fb2019-03-20 10:50:33 -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[] =
Brian Salomon00a5eb82018-07-11 15:32:05 -0400125 "Extended form: 'backend(option=value,...)'\n\n"
126 "Possible backends and options:\n"
127 "\n"
128 "gpu[api=string,color=string,dit=bool,nvpr=bool,inst=bool,samples=int]\n"
129 "\tapi\ttype: string\trequired\n"
130 "\t Select graphics API to use with gpu backend.\n"
131 "\t Options:\n"
132 "\t\tgl \t\t\tUse OpenGL.\n"
133 "\t\tgles \t\t\tUse OpenGL ES.\n"
Brian Salomon00a5eb82018-07-11 15:32:05 -0400134 "\t\tnullgl \t\t\tUse null OpenGL.\n"
135 "\t\tangle_d3d9_es2\t\tUse OpenGL ES2 on the ANGLE Direct3D9 backend.\n"
136 "\t\tangle_d3d11_es2\t\tUse OpenGL ES2 on the ANGLE Direct3D11 backend.\n"
137 "\t\tangle_d3d11_es3\t\tUse OpenGL ES3 on the ANGLE Direct3D11 backend.\n"
138 "\t\tangle_gl_es2\t\tUse OpenGL ES2 on the ANGLE OpenGL backend.\n"
139 "\t\tangle_gl_es3\t\tUse OpenGL ES3 on the ANGLE OpenGL backend.\n"
140 "\t\tcommandbuffer\t\tUse command buffer.\n"
141 "\t\tmock\t\t\tUse mock context.\n"
bsalomondc0fcd42016-04-11 14:21:33 -0700142#ifdef SK_VULKAN
Brian Salomon00a5eb82018-07-11 15:32:05 -0400143 "\t\tvulkan\t\t\tUse Vulkan.\n"
bsalomondc0fcd42016-04-11 14:21:33 -0700144#endif
Greg Daniel2811aa22017-07-13 15:34:56 -0400145#ifdef SK_METAL
Brian Salomon00a5eb82018-07-11 15:32:05 -0400146 "\t\tmetal\t\t\tUse Metal.\n"
Greg Daniel2811aa22017-07-13 15:34:56 -0400147#endif
Brian Salomon00a5eb82018-07-11 15:32:05 -0400148 "\tcolor\ttype: string\tdefault: 8888.\n"
149 "\t Select framebuffer color format.\n"
150 "\t Options:\n"
151 "\t\t8888\t\t\tLinear 8888.\n"
152 "\t\t888x\t\t\tLinear 888x.\n"
153 "\t\t4444\t\t\tLinear 4444.\n"
154 "\t\t565\t\t\tLinear 565.\n"
155 "\t\t1010102\t\t\tLinear 1010102.\n"
156 "\t\tsrgb\t\t\tsRGB 8888.\n"
157 "\t\tesrgb\t\t\tsRGB 16-bit floating point.\n"
158 "\t\tnarrow\t\t\tNarrow gamut 8888.\n"
159 "\t\tenarrow\t\t\tNarrow gamut 16-bit floating point.\n"
160 "\t\tf16\t\t\tLinearly blended 16-bit floating point.\n"
161 "\tdit\ttype: bool\tdefault: false.\n"
162 "\t Use device independent text.\n"
163 "\tnvpr\ttype: bool\tdefault: false.\n"
164 "\t Use NV_path_rendering OpenGL and OpenGL ES extension.\n"
165 "\tsamples\ttype: int\tdefault: 0.\n"
166 "\t Use multisampling with N samples.\n"
167 "\tstencils\ttype: bool\tdefault: true.\n"
168 "\t Allow the use of stencil buffers.\n"
169 "\ttestThreading\ttype: bool\tdefault: false.\n"
170 "\t Run with and without worker threads, check that results match.\n"
171 "\ttestPersistentCache\ttype: bool\tdefault: false.\n"
172 "\t Run using a pre-warmed GrContextOption::fPersistentCache.\n"
173 "\tsurf\ttype: string\tdefault: default.\n"
174 "\t Controls the type of backing store for SkSurfaces.\n"
175 "\t Options:\n"
176 "\t\tdefault\t\t\tA renderable texture created in Skia's resource cache.\n"
177 "\t\tbetex\t\t\tA wrapped backend texture.\n"
178 "\t\tbert\t\t\tA wrapped backend render target\n"
179 "\n"
180 "Predefined configs:\n\n"
181 // Help text for pre-defined configs is auto-generated from gPredefinedConfigs
182 ;
kkinnunen3e980c32015-12-23 01:33:00 -0800183
brianosman37e23342016-09-20 08:06:30 -0700184static const char* config_extended_help_fn() {
185 static SkString helpString;
186 helpString.set(configExtendedHelp);
187 for (const auto& config : gPredefinedConfigs) {
188 helpString.appendf("\t%-10s\t= gpu(%s)\n", config.predefinedConfig, config.options);
189 }
190 return helpString.c_str();
191}
kkinnunen3e980c32015-12-23 01:33:00 -0800192
brianosman37e23342016-09-20 08:06:30 -0700193DEFINE_extended_string(config, defaultConfigs, config_help_fn(), config_extended_help_fn());
kkinnunen3e980c32015-12-23 01:33:00 -0800194
Mike Klein88544fb2019-03-20 10:50:33 -0500195SkCommandLineConfig::SkCommandLineConfig(const SkString& tag,
196 const SkString& backend,
kkinnunen3e980c32015-12-23 01:33:00 -0800197 const SkTArray<SkString>& viaParts)
Mike Klein88544fb2019-03-20 10:50:33 -0500198 : fTag(tag), fBackend(backend), fViaParts(viaParts) {}
199SkCommandLineConfig::~SkCommandLineConfig() {}
kkinnunen3e980c32015-12-23 01:33:00 -0800200
kkinnunen3e980c32015-12-23 01:33:00 -0800201static bool parse_option_int(const SkString& value, int* outInt) {
202 if (value.isEmpty()) {
203 return false;
204 }
Mike Klein88544fb2019-03-20 10:50:33 -0500205 char* endptr = nullptr;
206 long intValue = strtol(value.c_str(), &endptr, 10);
kkinnunen3e980c32015-12-23 01:33:00 -0800207 if (*endptr != '\0') {
208 return false;
209 }
210 *outInt = static_cast<int>(intValue);
211 return true;
212}
213static bool parse_option_bool(const SkString& value, bool* outBool) {
214 if (value.equals("true")) {
215 *outBool = true;
216 return true;
217 }
218 if (value.equals("false")) {
219 *outBool = false;
220 return true;
221 }
222 return false;
223}
Mike Klein88544fb2019-03-20 10:50:33 -0500224static bool parse_option_gpu_api(const SkString& value,
kkinnunen3e980c32015-12-23 01:33:00 -0800225 SkCommandLineConfigGpu::ContextType* outContextType) {
kkinnunen3e980c32015-12-23 01:33:00 -0800226 if (value.equals("gl")) {
bsalomon85b4b532016-04-05 11:06:27 -0700227 *outContextType = GrContextFactory::kGL_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800228 return true;
229 }
230 if (value.equals("gles")) {
bsalomon85b4b532016-04-05 11:06:27 -0700231 *outContextType = GrContextFactory::kGLES_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800232 return true;
233 }
bsalomon11abd8d2016-10-14 08:13:48 -0700234 if (value.equals("angle_d3d9_es2")) {
235 *outContextType = GrContextFactory::kANGLE_D3D9_ES2_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800236 return true;
237 }
bsalomon11abd8d2016-10-14 08:13:48 -0700238 if (value.equals("angle_d3d11_es2")) {
239 *outContextType = GrContextFactory::kANGLE_D3D11_ES2_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800240 return true;
241 }
bsalomon11abd8d2016-10-14 08:13:48 -0700242 if (value.equals("angle_d3d11_es3")) {
243 *outContextType = GrContextFactory::kANGLE_D3D11_ES3_ContextType;
244 return true;
245 }
246 if (value.equals("angle_gl_es2")) {
247 *outContextType = GrContextFactory::kANGLE_GL_ES2_ContextType;
248 return true;
249 }
250 if (value.equals("angle_gl_es3")) {
251 *outContextType = GrContextFactory::kANGLE_GL_ES3_ContextType;
252 return true;
253 }
kkinnunen3e980c32015-12-23 01:33:00 -0800254 if (value.equals("commandbuffer")) {
bsalomon85b4b532016-04-05 11:06:27 -0700255 *outContextType = GrContextFactory::kCommandBuffer_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800256 return true;
257 }
Brian Salomon8fe24272017-07-07 12:56:11 -0400258 if (value.equals("mock")) {
259 *outContextType = GrContextFactory::kMock_ContextType;
260 return true;
261 }
bsalomondc0fcd42016-04-11 14:21:33 -0700262#ifdef SK_VULKAN
263 if (value.equals("vulkan")) {
264 *outContextType = GrContextFactory::kVulkan_ContextType;
265 return true;
266 }
267#endif
Greg Daniel2811aa22017-07-13 15:34:56 -0400268#ifdef SK_METAL
269 if (value.equals("metal")) {
270 *outContextType = GrContextFactory::kMetal_ContextType;
271 return true;
272 }
273#endif
kkinnunen3e980c32015-12-23 01:33:00 -0800274 return false;
275}
Brian Salomonf865b052018-03-09 09:01:53 -0500276
Mike Klein88544fb2019-03-20 10:50:33 -0500277static bool parse_option_gpu_color(const SkString& value,
278 SkColorType* outColorType,
279 SkAlphaType* alphaType,
brianosmanb109b8c2016-06-16 13:03:24 -0700280 sk_sp<SkColorSpace>* outColorSpace) {
Brian Salomonce5ee602017-07-17 11:31:31 -0400281 // We always use premul unless the color type is 565.
282 *alphaType = kPremul_SkAlphaType;
283
brianosmand93c1202016-03-10 07:49:08 -0800284 if (value.equals("8888")) {
Mike Klein88544fb2019-03-20 10:50:33 -0500285 *outColorType = kRGBA_8888_SkColorType;
brianosmanb109b8c2016-06-16 13:03:24 -0700286 *outColorSpace = nullptr;
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400287 } else if (value.equals("888x")) {
Mike Klein88544fb2019-03-20 10:50:33 -0500288 *outColorType = kRGB_888x_SkColorType;
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400289 *outColorSpace = nullptr;
Mike Reeda3bfb5d2019-03-21 13:00:11 -0400290 } else if (value.equals("8888s")) {
291 *outColorType = kRGBA_8888_SkColorType;
292 *outColorSpace = SkColorSpace::MakeSRGB();
293 } else if (value.equals("bgra8")) {
294 *outColorType = kBGRA_8888_SkColorType;
295 *outColorSpace = nullptr;
296 } else if (value.equals("bgra8s")) {
297 *outColorType = kBGRA_8888_SkColorType;
298 *outColorSpace = SkColorSpace::MakeSRGB();
Brian Salomonce5ee602017-07-17 11:31:31 -0400299 } else if (value.equals("4444")) {
Mike Klein88544fb2019-03-20 10:50:33 -0500300 *outColorType = kARGB_4444_SkColorType;
Brian Salomonce5ee602017-07-17 11:31:31 -0400301 *outColorSpace = nullptr;
Brian Salomonce5ee602017-07-17 11:31:31 -0400302 } else if (value.equals("565")) {
Mike Klein88544fb2019-03-20 10:50:33 -0500303 *outColorType = kRGB_565_SkColorType;
304 *alphaType = kOpaque_SkAlphaType;
Brian Salomonce5ee602017-07-17 11:31:31 -0400305 *outColorSpace = nullptr;
Brian Osman10fc6fd2018-03-02 11:01:10 -0500306 } else if (value.equals("1010102")) {
Mike Klein88544fb2019-03-20 10:50:33 -0500307 *outColorType = kRGBA_1010102_SkColorType;
Brian Osman10fc6fd2018-03-02 11:01:10 -0500308 *outColorSpace = nullptr;
Brian Osmana15cedb2018-05-30 15:36:38 -0400309 } else if (value.equals("srgb")) {
Mike Klein88544fb2019-03-20 10:50:33 -0500310 *outColorType = kRGBA_8888_SkColorType;
Brian Osmana15cedb2018-05-30 15:36:38 -0400311 *outColorSpace = SkColorSpace::MakeSRGB();
Brian Osmana76b7a92018-07-18 14:36:43 -0400312 } else if (value.equals("p3")) {
Mike Klein88544fb2019-03-20 10:50:33 -0500313 *outColorType = kRGBA_8888_SkColorType;
Brian Osman82ebe042019-01-04 17:03:00 -0500314 *outColorSpace = SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB, SkNamedGamut::kDCIP3);
Brian Osmana66f4da2018-06-28 17:52:02 +0000315 } else if (value.equals("esrgb")) {
Mike Klein88544fb2019-03-20 10:50:33 -0500316 *outColorType = kRGBA_F16_SkColorType;
Brian Osmana15cedb2018-05-30 15:36:38 -0400317 *outColorSpace = SkColorSpace::MakeSRGB();
Brian Osmana66f4da2018-06-28 17:52:02 +0000318 } else if (value.equals("narrow") || value.equals("enarrow")) {
Mike Klein88544fb2019-03-20 10:50:33 -0500319 *outColorType = value.equals("narrow") ? kRGBA_8888_SkColorType : kRGBA_F16_SkColorType;
Brian Osman82ebe042019-01-04 17:03:00 -0500320 *outColorSpace = SkColorSpace::MakeRGB(SkNamedTransferFn::k2Dot2, gNarrow_toXYZD50);
Brian Osmana15cedb2018-05-30 15:36:38 -0400321 } else if (value.equals("f16")) {
Mike Klein88544fb2019-03-20 10:50:33 -0500322 *outColorType = kRGBA_F16_SkColorType;
Brian Osmana15cedb2018-05-30 15:36:38 -0400323 *outColorSpace = SkColorSpace::MakeSRGBLinear();
Brian Osmand0163fb2019-03-06 13:40:40 -0500324 } else if (value.equals("f16norm")) {
Mike Klein88544fb2019-03-20 10:50:33 -0500325 *outColorType = kRGBA_F16Norm_SkColorType;
Brian Osmand0163fb2019-03-06 13:40:40 -0500326 *outColorSpace = SkColorSpace::MakeSRGB();
Brian Osmana15cedb2018-05-30 15:36:38 -0400327 } else {
brianosman4562f6e2016-09-19 14:42:04 -0700328 return false;
329 }
Brian Osmana15cedb2018-05-30 15:36:38 -0400330 return true;
brianosmand93c1202016-03-10 07:49:08 -0800331}
Brian Salomonf865b052018-03-09 09:01:53 -0500332
Mike Klein88544fb2019-03-20 10:50:33 -0500333static bool parse_option_gpu_surf_type(const SkString& value,
Brian Salomonf865b052018-03-09 09:01:53 -0500334 SkCommandLineConfigGpu::SurfType* surfType) {
335 if (value.equals("default")) {
336 *surfType = SkCommandLineConfigGpu::SurfType::kDefault;
337 return true;
338 }
339 if (value.equals("betex")) {
340 *surfType = SkCommandLineConfigGpu::SurfType::kBackendTexture;
341 return true;
342 }
343 if (value.equals("bert")) {
344 *surfType = SkCommandLineConfigGpu::SurfType::kBackendRenderTarget;
345 return true;
346 }
347 return false;
348}
Bryce Thomas95a7b762018-03-02 13:54:21 -0800349
350// Extended options take form --config item[key1=value1,key2=value2,...]
351// Example: --config gpu[api=gl,color=8888]
352class ExtendedOptions {
353public:
354 ExtendedOptions(const SkString& optionsString, bool* outParseSucceeded) {
355 SkTArray<SkString> optionParts;
356 SkStrSplit(optionsString.c_str(), ",", kStrict_SkStrSplitMode, &optionParts);
357 for (int i = 0; i < optionParts.count(); ++i) {
358 SkTArray<SkString> keyValueParts;
359 SkStrSplit(optionParts[i].c_str(), "=", kStrict_SkStrSplitMode, &keyValueParts);
360 if (keyValueParts.count() != 2) {
361 *outParseSucceeded = false;
362 return;
363 }
Mike Klein88544fb2019-03-20 10:50:33 -0500364 const SkString& key = keyValueParts[0];
Bryce Thomas95a7b762018-03-02 13:54:21 -0800365 const SkString& value = keyValueParts[1];
366 if (fOptionsMap.find(key) == nullptr) {
367 fOptionsMap.set(key, value);
368 } else {
369 // Duplicate values are not allowed.
370 *outParseSucceeded = false;
371 return;
372 }
373 }
374 *outParseSucceeded = true;
375 }
376
Mike Klein88544fb2019-03-20 10:50:33 -0500377 bool get_option_gpu_color(const char* optionKey,
378 SkColorType* outColorType,
379 SkAlphaType* alphaType,
Bryce Thomas95a7b762018-03-02 13:54:21 -0800380 sk_sp<SkColorSpace>* outColorSpace,
Mike Klein88544fb2019-03-20 10:50:33 -0500381 bool optional = true) const {
Bryce Thomas95a7b762018-03-02 13:54:21 -0800382 SkString* optionValue = fOptionsMap.find(SkString(optionKey));
383 if (optionValue == nullptr) {
384 return optional;
385 }
386 return parse_option_gpu_color(*optionValue, outColorType, alphaType, outColorSpace);
387 }
388
Mike Klein88544fb2019-03-20 10:50:33 -0500389 bool get_option_gpu_api(const char* optionKey,
Bryce Thomas95a7b762018-03-02 13:54:21 -0800390 SkCommandLineConfigGpu::ContextType* outContextType,
Mike Klein88544fb2019-03-20 10:50:33 -0500391 bool optional = true) const {
Bryce Thomas95a7b762018-03-02 13:54:21 -0800392 SkString* optionValue = fOptionsMap.find(SkString(optionKey));
393 if (optionValue == nullptr) {
394 return optional;
395 }
396 return parse_option_gpu_api(*optionValue, outContextType);
397 }
Brian Salomonf865b052018-03-09 09:01:53 -0500398
Mike Klein88544fb2019-03-20 10:50:33 -0500399 bool get_option_gpu_surf_type(const char* optionKey,
Brian Salomonf865b052018-03-09 09:01:53 -0500400 SkCommandLineConfigGpu::SurfType* outSurfType,
Mike Klein88544fb2019-03-20 10:50:33 -0500401 bool optional = true) const {
Brian Salomonf865b052018-03-09 09:01:53 -0500402 SkString* optionValue = fOptionsMap.find(SkString(optionKey));
403 if (optionValue == nullptr) {
404 return optional;
405 }
406 return parse_option_gpu_surf_type(*optionValue, outSurfType);
407 }
Bryce Thomas95a7b762018-03-02 13:54:21 -0800408
409 bool get_option_int(const char* optionKey, int* outInt, bool optional = true) const {
410 SkString* optionValue = fOptionsMap.find(SkString(optionKey));
411 if (optionValue == nullptr) {
412 return optional;
413 }
414 return parse_option_int(*optionValue, outInt);
415 }
416
417 bool get_option_bool(const char* optionKey, bool* outBool, bool optional = true) const {
418 SkString* optionValue = fOptionsMap.find(SkString(optionKey));
419 if (optionValue == nullptr) {
420 return optional;
421 }
422 return parse_option_bool(*optionValue, outBool);
423 }
424
425private:
426 SkTHashMap<SkString, SkString> fOptionsMap;
427};
428
Mike Klein88544fb2019-03-20 10:50:33 -0500429SkCommandLineConfigGpu::SkCommandLineConfigGpu(const SkString& tag,
430 const SkTArray<SkString>& viaParts,
431 ContextType contextType,
432 bool useNVPR,
433 bool useDIText,
434 int samples,
435 SkColorType colorType,
436 SkAlphaType alphaType,
437 sk_sp<SkColorSpace> colorSpace,
438 bool useStencilBuffers,
439 bool testThreading,
440 bool testPersistentCache,
441 SurfType surfType)
Bryce Thomas95a7b762018-03-02 13:54:21 -0800442 : SkCommandLineConfig(tag, SkString("gpu"), viaParts)
443 , fContextType(contextType)
444 , fContextOverrides(ContextOverrides::kNone)
445 , fUseDIText(useDIText)
446 , fSamples(samples)
447 , fColorType(colorType)
448 , fAlphaType(alphaType)
449 , fColorSpace(std::move(colorSpace))
Brian Salomonf865b052018-03-09 09:01:53 -0500450 , fTestThreading(testThreading)
Brian Salomon00a5eb82018-07-11 15:32:05 -0400451 , fTestPersistentCache(testPersistentCache)
Brian Salomonf865b052018-03-09 09:01:53 -0500452 , fSurfType(surfType) {
Bryce Thomas95a7b762018-03-02 13:54:21 -0800453 if (useNVPR) {
454 fContextOverrides |= ContextOverrides::kRequireNVPRSupport;
455 } else {
456 // We don't disable NVPR for instanced configs. Otherwise the caps wouldn't use mixed
457 // samples and we couldn't test the mixed samples backend for simple shapes.
458 fContextOverrides |= ContextOverrides::kDisableNVPR;
459 }
Bryce Thomas95a7b762018-03-02 13:54:21 -0800460 if (!useStencilBuffers) {
461 fContextOverrides |= ContextOverrides::kAvoidStencilBuffers;
462 }
463}
kkinnunen3e980c32015-12-23 01:33:00 -0800464
Mike Klein88544fb2019-03-20 10:50:33 -0500465SkCommandLineConfigGpu* parse_command_line_config_gpu(const SkString& tag,
kkinnunen3e980c32015-12-23 01:33:00 -0800466 const SkTArray<SkString>& vias,
Mike Klein88544fb2019-03-20 10:50:33 -0500467 const SkString& options) {
kkinnunen3e980c32015-12-23 01:33:00 -0800468 // Defaults for GPU backend.
Mike Klein88544fb2019-03-20 10:50:33 -0500469 SkCommandLineConfigGpu::ContextType contextType = GrContextFactory::kGL_ContextType;
470 bool useNVPR = false;
471 bool useDIText = false;
472 int samples = 1;
473 SkColorType colorType = kRGBA_8888_SkColorType;
474 SkAlphaType alphaType = kPremul_SkAlphaType;
475 sk_sp<SkColorSpace> colorSpace = nullptr;
476 bool useStencils = true;
477 bool testThreading = false;
478 bool testPersistentCache = false;
479 SkCommandLineConfigGpu::SurfType surfType = SkCommandLineConfigGpu::SurfType::kDefault;
kkinnunen3e980c32015-12-23 01:33:00 -0800480
Mike Klein88544fb2019-03-20 10:50:33 -0500481 bool parseSucceeded = false;
Bryce Thomas95a7b762018-03-02 13:54:21 -0800482 ExtendedOptions extendedOptions(options, &parseSucceeded);
483 if (!parseSucceeded) {
Brian Salomon6405e712017-03-20 08:54:16 -0400484 return nullptr;
485 }
Bryce Thomas95a7b762018-03-02 13:54:21 -0800486
487 bool validOptions =
488 extendedOptions.get_option_gpu_api("api", &contextType, false) &&
489 extendedOptions.get_option_bool("nvpr", &useNVPR) &&
490 extendedOptions.get_option_bool("dit", &useDIText) &&
491 extendedOptions.get_option_int("samples", &samples) &&
492 extendedOptions.get_option_gpu_color("color", &colorType, &alphaType, &colorSpace) &&
493 extendedOptions.get_option_bool("stencils", &useStencils) &&
Brian Salomonf865b052018-03-09 09:01:53 -0500494 extendedOptions.get_option_bool("testThreading", &testThreading) &&
Brian Salomon00a5eb82018-07-11 15:32:05 -0400495 extendedOptions.get_option_bool("testPersistentCache", &testPersistentCache) &&
Brian Salomonf865b052018-03-09 09:01:53 -0500496 extendedOptions.get_option_gpu_surf_type("surf", &surfType);
Bryce Thomas95a7b762018-03-02 13:54:21 -0800497
Brian Salomon00a5eb82018-07-11 15:32:05 -0400498 // testing threading and the persistent cache are mutually exclusive.
499 if (!validOptions || (testThreading && testPersistentCache)) {
Bryce Thomas95a7b762018-03-02 13:54:21 -0800500 return nullptr;
501 }
502
Mike Klein88544fb2019-03-20 10:50:33 -0500503 return new SkCommandLineConfigGpu(tag,
504 vias,
505 contextType,
506 useNVPR,
507 useDIText,
508 samples,
509 colorType,
510 alphaType,
511 colorSpace,
512 useStencils,
513 testThreading,
514 testPersistentCache,
515 surfType);
kkinnunen3e980c32015-12-23 01:33:00 -0800516}
kkinnunen3e980c32015-12-23 01:33:00 -0800517
Mike Klein88544fb2019-03-20 10:50:33 -0500518SkCommandLineConfigSvg::SkCommandLineConfigSvg(const SkString& tag,
519 const SkTArray<SkString>& viaParts,
520 int pageIndex)
Bryce Thomas95a7b762018-03-02 13:54:21 -0800521 : SkCommandLineConfig(tag, SkString("svg"), viaParts), fPageIndex(pageIndex) {}
522
Mike Klein88544fb2019-03-20 10:50:33 -0500523SkCommandLineConfigSvg* parse_command_line_config_svg(const SkString& tag,
Bryce Thomas95a7b762018-03-02 13:54:21 -0800524 const SkTArray<SkString>& vias,
Mike Klein88544fb2019-03-20 10:50:33 -0500525 const SkString& options) {
Bryce Thomas95a7b762018-03-02 13:54:21 -0800526 // Defaults for SVG backend.
527 int pageIndex = 0;
528
Mike Klein88544fb2019-03-20 10:50:33 -0500529 bool parseSucceeded = false;
Bryce Thomas95a7b762018-03-02 13:54:21 -0800530 ExtendedOptions extendedOptions(options, &parseSucceeded);
531 if (!parseSucceeded) {
532 return nullptr;
533 }
534
535 bool validOptions = extendedOptions.get_option_int("page", &pageIndex);
536
537 if (!validOptions) {
538 return nullptr;
539 }
540
541 return new SkCommandLineConfigSvg(tag, vias, pageIndex);
542}
543
Mike Klein88544fb2019-03-20 10:50:33 -0500544void ParseConfigs(const CommandLineFlags::StringArray& configs,
545 SkCommandLineConfigArray* outResult) {
kkinnunen3e980c32015-12-23 01:33:00 -0800546 outResult->reset();
547 for (int i = 0; i < configs.count(); ++i) {
Mike Klein88544fb2019-03-20 10:50:33 -0500548 SkString extendedBackend;
549 SkString extendedOptions;
550 SkString simpleBackend;
kkinnunen3e980c32015-12-23 01:33:00 -0800551 SkTArray<SkString> vias;
552
Mike Klein88544fb2019-03-20 10:50:33 -0500553 SkString tag(configs[i]);
kkinnunen3e980c32015-12-23 01:33:00 -0800554 SkTArray<SkString> parts;
bsalomon808ecbb2016-09-28 12:40:22 -0700555 SkStrSplit(tag.c_str(), "[", kStrict_SkStrSplitMode, &parts);
kkinnunen3e980c32015-12-23 01:33:00 -0800556 if (parts.count() == 2) {
557 SkTArray<SkString> parts2;
bsalomon808ecbb2016-09-28 12:40:22 -0700558 SkStrSplit(parts[1].c_str(), "]", kStrict_SkStrSplitMode, &parts2);
kkinnunen3e980c32015-12-23 01:33:00 -0800559 if (parts2.count() == 2 && parts2[1].isEmpty()) {
560 SkStrSplit(parts[0].c_str(), "-", kStrict_SkStrSplitMode, &vias);
561 if (vias.count()) {
562 extendedBackend = vias[vias.count() - 1];
563 vias.pop_back();
564 } else {
565 extendedBackend = parts[0];
566 }
567 extendedOptions = parts2[0];
bsalomon808ecbb2016-09-28 12:40:22 -0700568 simpleBackend.printf("%s[%s]", extendedBackend.c_str(), extendedOptions.c_str());
kkinnunen3e980c32015-12-23 01:33:00 -0800569 }
570 }
571
572 if (extendedBackend.isEmpty()) {
573 simpleBackend = tag;
574 SkStrSplit(tag.c_str(), "-", kStrict_SkStrSplitMode, &vias);
575 if (vias.count()) {
576 simpleBackend = vias[vias.count() - 1];
577 vias.pop_back();
578 }
kkinnunen3e980c32015-12-23 01:33:00 -0800579 for (auto& predefinedConfig : gPredefinedConfigs) {
580 if (simpleBackend.equals(predefinedConfig.predefinedConfig)) {
581 extendedBackend = predefinedConfig.backend;
582 extendedOptions = predefinedConfig.options;
583 break;
584 }
585 }
586 }
587 SkCommandLineConfig* parsedConfig = nullptr;
kkinnunen3e980c32015-12-23 01:33:00 -0800588 if (extendedBackend.equals("gpu")) {
589 parsedConfig = parse_command_line_config_gpu(tag, vias, extendedOptions);
590 }
Bryce Thomas95a7b762018-03-02 13:54:21 -0800591 if (extendedBackend.equals("svg")) {
592 parsedConfig = parse_command_line_config_svg(tag, vias, extendedOptions);
593 }
kkinnunen3e980c32015-12-23 01:33:00 -0800594 if (!parsedConfig) {
595 parsedConfig = new SkCommandLineConfig(tag, simpleBackend, vias);
596 }
597 outResult->emplace_back(parsedConfig);
598 }
599}