blob: 3fd8a97b4bab95560de083255007389bd5109c57 [file] [log] [blame]
kkinnunen3e980c32015-12-23 01:33:00 -08001/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
raftias7c602de2016-10-13 10:45:44 -07008#include "SkColorSpace_Base.h"
kkinnunen3e980c32015-12-23 01:33:00 -08009#include "SkCommonFlagsConfig.h"
csmartdalton6270e552016-09-13 10:41:49 -070010#include "SkImageInfo.h"
kkinnunen3e980c32015-12-23 01:33:00 -080011
12#include <stdlib.h>
13
bsalomon3724e572016-03-30 18:56:19 -070014#if SK_SUPPORT_GPU
15using sk_gpu_test::GrContextFactory;
16#endif
17
kkinnunen3e980c32015-12-23 01:33:00 -080018static const char defaultConfigs[] =
mtklein896ddb72016-09-14 10:33:12 -070019 "8888 gpu nonrendering"
bsalomon7c2213b2016-09-29 10:07:22 -070020#if SK_ANGLE
21#ifdef SK_BUILD_FOR_WIN
22 " angle"
23#endif
kkinnunen3e980c32015-12-23 01:33:00 -080024#endif
25#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
26 " hwui"
27#endif
28 ;
29
brianosman37e23342016-09-20 08:06:30 -070030static const struct {
31 const char* predefinedConfig;
32 const char* backend;
33 const char* options;
34} gPredefinedConfigs[] ={
35#if SK_SUPPORT_GPU
bsalomon7c2213b2016-09-29 10:07:22 -070036 { "gpu", "gpu", "" },
37 { "gl", "gpu", "api=gl" },
38 { "msaa4", "gpu", "samples=4" },
39 { "glmsaa4", "gpu", "api=gl,samples=4" },
40 { "msaa16", "gpu", "samples=16" },
41 { "nvpr4", "gpu", "nvpr=true,samples=4" },
42 { "glnvpr4", "gpu", "api=gl,nvpr=true,samples=4" },
43 { "nvpr16", "gpu", "nvpr=true,samples=16" },
44 { "nvprdit4", "gpu", "nvpr=true,samples=4,dit=true" },
45 { "glnvprdit4", "gpu", "api=gl,nvpr=true,samples=4,dit=true" },
46 { "nvprdit16", "gpu", "nvpr=true,samples=16,dit=true" },
47 { "glinst", "gpu", "api=gl,inst=true" },
48 { "glinst4", "gpu", "api=gl,inst=true,samples=4" },
49 { "glinstdit4", "gpu", "api=gl,inst=true,samples=4,dit=true" },
50 { "glinst16", "gpu", "api=gl,inst=true,samples=16" },
51 { "glinstdit16", "gpu", "api=gl,inst=true,samples=16,dit=true" },
52 { "esinst", "gpu", "api=gles,inst=true" },
53 { "esinst4", "gpu", "api=gles,inst=true,samples=4" },
54 { "esinstdit4", "gpu", "api=gles,inst=true,samples=4,dit=true" },
55 { "gpuf16", "gpu", "color=f16" },
56 { "gpusrgb", "gpu", "color=srgb" },
57 { "glsrgb", "gpu", "api=gl,color=srgb" },
58 { "glwide", "gpu", "api=gl,color=f16_wide" },
59 { "gpudft", "gpu", "dit=true" },
60 { "gpudebug", "gpu", "api=debug" },
61 { "gpunull", "gpu", "api=null" },
62 { "debug", "gpu", "api=debug" },
63 { "nullgpu", "gpu", "api=null" }
64#if SK_ANGLE
65#ifdef SK_BUILD_FOR_WIN
66 ,{ "angle", "gpu", "api=angle" }
67#endif
68 ,{ "angle-gl", "gpu", "api=angle-gl" }
69#endif
70 ,{ "commandbuffer", "gpu", "api=commandbuffer" }
kkinnunen3e980c32015-12-23 01:33:00 -080071#if SK_MESA
bsalomon7c2213b2016-09-29 10:07:22 -070072 ,{ "mesa", "gpu", "api=mesa" }
kkinnunen3e980c32015-12-23 01:33:00 -080073#endif
brianosman37e23342016-09-20 08:06:30 -070074#ifdef SK_VULKAN
bsalomon7c2213b2016-09-29 10:07:22 -070075 ,{ "vk", "gpu", "api=vulkan" }
76 ,{ "vksrgb", "gpu", "api=vulkan,color=srgb" }
77 ,{ "vkwide", "gpu", "api=vulkan,color=f16_wide" }
78 ,{ "vkmsaa4", "gpu", "api=vulkan,samples=4" }
79 ,{ "vkmsaa16", "gpu", "api=vulkan,samples=16" }
brianosman37e23342016-09-20 08:06:30 -070080#endif
81
82#else
83{ "", "", "" }
84#endif
85};
86
87static const char configHelp[] =
88 "Options: 565 8888 srgb f16 nonrendering null pdf pdfa skp pipe svg xps"
kkinnunen3e980c32015-12-23 01:33:00 -080089#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
90 " hwui"
91#endif
brianosman37e23342016-09-20 08:06:30 -070092 ;
93
94static const char* config_help_fn() {
95 static SkString helpString;
96 helpString.set(configHelp);
97 for (const auto& config : gPredefinedConfigs) {
98 helpString.appendf(" %s", config.predefinedConfig);
99 }
bsalomon808ecbb2016-09-28 12:40:22 -0700100 helpString.append(" or use extended form 'backend[option=value,...]'.\n");
brianosman37e23342016-09-20 08:06:30 -0700101 return helpString.c_str();
102}
kkinnunen3e980c32015-12-23 01:33:00 -0800103
104static const char configExtendedHelp[] =
105 "Extended form: 'backend(option=value,...)'\n\n"
106 "Possible backends and options:\n"
107#if SK_SUPPORT_GPU
108 "\n"
bsalomon808ecbb2016-09-28 12:40:22 -0700109 "gpu[api=string,color=string,dit=bool,nvpr=bool,inst=bool,samples=int]\n"
kkinnunen3e980c32015-12-23 01:33:00 -0800110 "\tapi\ttype: string\tdefault: native.\n"
111 "\t Select graphics API to use with gpu backend.\n"
112 "\t Options:\n"
113 "\t\tnative\t\t\tUse platform default OpenGL or OpenGL ES backend.\n"
114 "\t\tgl \t\t\tUse OpenGL.\n"
115 "\t\tgles \t\t\tUse OpenGL ES.\n"
116 "\t\tdebug \t\t\tUse debug OpenGL.\n"
117 "\t\tnull \t\t\tUse null OpenGL.\n"
bsalomon7c2213b2016-09-29 10:07:22 -0700118#if SK_ANGLE
119#ifdef SK_BUILD_FOR_WIN
120 "\t\tangle\t\t\tUse ANGLE DirectX.\n"
121#endif
122 "\t\tangle-gl\t\t\tUse ANGLE OpenGL.\n"
123#endif
kkinnunen3e980c32015-12-23 01:33:00 -0800124 "\t\tcommandbuffer\t\tUse command buffer.\n"
kkinnunen3e980c32015-12-23 01:33:00 -0800125#if SK_MESA
126 "\t\tmesa\t\t\tUse MESA.\n"
127#endif
bsalomondc0fcd42016-04-11 14:21:33 -0700128#ifdef SK_VULKAN
129 "\t\tvulkan\t\t\tUse Vulkan.\n"
130#endif
brianosmand93c1202016-03-10 07:49:08 -0800131 "\tcolor\ttype: string\tdefault: 8888.\n"
132 "\t Select framebuffer color format.\n"
133 "\t Options:\n"
134 "\t\t8888\t\t\tLinear 8888.\n"
brianosman4562f6e2016-09-19 14:42:04 -0700135 "\t\tf16{_gamut}\t\tLinear 16-bit floating point.\n"
136 "\t\tsrgb{_gamut}\t\tsRGB 8888.\n"
137 "\t gamut\ttype: string\tdefault: srgb.\n"
138 "\t Select color gamut for f16 or sRGB format buffers.\n"
139 "\t Options:\n"
140 "\t\tsrgb\t\t\tsRGB gamut.\n"
141 "\t\twide\t\t\tWide Gamut RGB.\n"
kkinnunen3e980c32015-12-23 01:33:00 -0800142 "\tdit\ttype: bool\tdefault: false.\n"
143 "\t Use device independent text.\n"
144 "\tnvpr\ttype: bool\tdefault: false.\n"
145 "\t Use NV_path_rendering OpenGL and OpenGL ES extension.\n"
146 "\tsamples\ttype: int\tdefault: 0.\n"
147 "\t Use multisampling with N samples.\n"
148 "\n"
149 "Predefined configs:\n\n"
brianosman37e23342016-09-20 08:06:30 -0700150 // Help text for pre-defined configs is auto-generated from gPredefinedConfigs
kkinnunen3e980c32015-12-23 01:33:00 -0800151#endif
152 ;
153
brianosman37e23342016-09-20 08:06:30 -0700154static const char* config_extended_help_fn() {
155 static SkString helpString;
156 helpString.set(configExtendedHelp);
157 for (const auto& config : gPredefinedConfigs) {
158 helpString.appendf("\t%-10s\t= gpu(%s)\n", config.predefinedConfig, config.options);
159 }
160 return helpString.c_str();
161}
kkinnunen3e980c32015-12-23 01:33:00 -0800162
brianosman37e23342016-09-20 08:06:30 -0700163DEFINE_extended_string(config, defaultConfigs, config_help_fn(), config_extended_help_fn());
kkinnunen3e980c32015-12-23 01:33:00 -0800164
165SkCommandLineConfig::SkCommandLineConfig(const SkString& tag, const SkString& backend,
166 const SkTArray<SkString>& viaParts)
167 : fTag(tag)
168 , fBackend(backend)
169 , fViaParts(viaParts) {
170}
171SkCommandLineConfig::~SkCommandLineConfig() {
172}
173
174#if SK_SUPPORT_GPU
175SkCommandLineConfigGpu::SkCommandLineConfigGpu(
csmartdaltone0d36292016-07-29 08:14:20 -0700176 const SkString& tag, const SkTArray<SkString>& viaParts, ContextType contextType, bool useNVPR,
177 bool useInstanced, bool useDIText, int samples, SkColorType colorType,
178 sk_sp<SkColorSpace> colorSpace)
kkinnunen3e980c32015-12-23 01:33:00 -0800179 : SkCommandLineConfig(tag, SkString("gpu"), viaParts)
180 , fContextType(contextType)
csmartdalton6270e552016-09-13 10:41:49 -0700181 , fContextOptions(ContextOptions::kNone)
kkinnunen3e980c32015-12-23 01:33:00 -0800182 , fUseDIText(useDIText)
brianosmand93c1202016-03-10 07:49:08 -0800183 , fSamples(samples)
184 , fColorType(colorType)
brianosmanb109b8c2016-06-16 13:03:24 -0700185 , fColorSpace(std::move(colorSpace)) {
csmartdalton6270e552016-09-13 10:41:49 -0700186 if (useNVPR) {
187 fContextOptions |= ContextOptions::kEnableNVPR;
188 }
189 if (useInstanced) {
190 fContextOptions |= ContextOptions::kUseInstanced;
191 }
192 if (SkColorAndColorSpaceAreGammaCorrect(colorType, colorSpace.get())) {
193 fContextOptions |= ContextOptions::kRequireSRGBSupport;
194 }
kkinnunen3e980c32015-12-23 01:33:00 -0800195}
196static bool parse_option_int(const SkString& value, int* outInt) {
197 if (value.isEmpty()) {
198 return false;
199 }
200 char* endptr = nullptr;
201 long intValue = strtol(value.c_str(), &endptr, 10);
202 if (*endptr != '\0') {
203 return false;
204 }
205 *outInt = static_cast<int>(intValue);
206 return true;
207}
208static bool parse_option_bool(const SkString& value, bool* outBool) {
209 if (value.equals("true")) {
210 *outBool = true;
211 return true;
212 }
213 if (value.equals("false")) {
214 *outBool = false;
215 return true;
216 }
217 return false;
218}
219static bool parse_option_gpu_api(const SkString& value,
220 SkCommandLineConfigGpu::ContextType* outContextType) {
kkinnunen3e980c32015-12-23 01:33:00 -0800221 if (value.equals("gl")) {
bsalomon85b4b532016-04-05 11:06:27 -0700222 *outContextType = GrContextFactory::kGL_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800223 return true;
224 }
225 if (value.equals("gles")) {
bsalomon85b4b532016-04-05 11:06:27 -0700226 *outContextType = GrContextFactory::kGLES_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800227 return true;
228 }
229 if (value.equals("debug")) {
bsalomon85b4b532016-04-05 11:06:27 -0700230 *outContextType = GrContextFactory::kDebugGL_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800231 return true;
232 }
233 if (value.equals("null")) {
bsalomon85b4b532016-04-05 11:06:27 -0700234 *outContextType = GrContextFactory::kNullGL_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800235 return true;
236 }
bsalomon7c2213b2016-09-29 10:07:22 -0700237#if SK_ANGLE
238#ifdef SK_BUILD_FOR_WIN
239 if (value.equals("angle")) {
240 *outContextType = GrContextFactory::kANGLE_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800241 return true;
242 }
bsalomon7c2213b2016-09-29 10:07:22 -0700243#endif
244 if (value.equals("angle-gl")) {
245 *outContextType = GrContextFactory::kANGLE_GL_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800246 return true;
247 }
bsalomon7c2213b2016-09-29 10:07:22 -0700248#endif
kkinnunen3e980c32015-12-23 01:33:00 -0800249 if (value.equals("commandbuffer")) {
bsalomon85b4b532016-04-05 11:06:27 -0700250 *outContextType = GrContextFactory::kCommandBuffer_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800251 return true;
252 }
kkinnunen3e980c32015-12-23 01:33:00 -0800253#if SK_MESA
254 if (value.equals("mesa")) {
bsalomon85b4b532016-04-05 11:06:27 -0700255 *outContextType = GrContextFactory::kMESA_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800256 return true;
257 }
258#endif
bsalomondc0fcd42016-04-11 14:21:33 -0700259#ifdef SK_VULKAN
260 if (value.equals("vulkan")) {
261 *outContextType = GrContextFactory::kVulkan_ContextType;
262 return true;
263 }
264#endif
kkinnunen3e980c32015-12-23 01:33:00 -0800265 return false;
266}
brianosmand93c1202016-03-10 07:49:08 -0800267static bool parse_option_gpu_color(const SkString& value,
268 SkColorType* outColorType,
brianosmanb109b8c2016-06-16 13:03:24 -0700269 sk_sp<SkColorSpace>* outColorSpace) {
brianosmand93c1202016-03-10 07:49:08 -0800270 if (value.equals("8888")) {
bsalomon33069252016-09-28 08:49:53 -0700271 *outColorType = kRGBA_8888_SkColorType;
brianosmanb109b8c2016-06-16 13:03:24 -0700272 *outColorSpace = nullptr;
brianosmand93c1202016-03-10 07:49:08 -0800273 return true;
274 }
brianosman4562f6e2016-09-19 14:42:04 -0700275
276 SkTArray<SkString> commands;
277 SkStrSplit(value.c_str(), "_", &commands);
278 if (commands.count() < 1 || commands.count() > 2) {
279 return false;
280 }
281
282 // First, figure out color gamut that we'll work in (default to sRGB)
283 sk_sp<SkColorSpace> colorSpace = SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named);
284 if (commands.count() == 2) {
285 if (commands[1].equals("srgb")) {
286 // sRGB gamut (which is our default)
287 } else if (commands[1].equals("wide")) {
288 // WideGamut RGB
289 const float gWideGamutRGB_toXYZD50[]{
290 0.7161046f, 0.1009296f, 0.1471858f, // -> X
291 0.2581874f, 0.7249378f, 0.0168748f, // -> Y
292 0.0000000f, 0.0517813f, 0.7734287f, // -> Z
293 };
294 SkMatrix44 wideGamutRGBMatrix(SkMatrix44::kUninitialized_Constructor);
295 wideGamutRGBMatrix.set3x3RowMajorf(gWideGamutRGB_toXYZD50);
296 colorSpace = SkColorSpace::NewRGB(SkColorSpace::kSRGB_RenderTargetGamma,
297 wideGamutRGBMatrix);
298 } else {
299 // Unknown color gamut
300 return false;
301 }
302 }
303
304 // Now pick a color type
305 if (commands[0].equals("f16")) {
brianosmand93c1202016-03-10 07:49:08 -0800306 *outColorType = kRGBA_F16_SkColorType;
raftias7c602de2016-10-13 10:45:44 -0700307 *outColorSpace = as_CSB(colorSpace)->makeLinearGamma();
brianosmand93c1202016-03-10 07:49:08 -0800308 return true;
309 }
brianosman4562f6e2016-09-19 14:42:04 -0700310 if (commands[0].equals("srgb")) {
bsalomon33069252016-09-28 08:49:53 -0700311 *outColorType = kRGBA_8888_SkColorType;
brianosman4562f6e2016-09-19 14:42:04 -0700312 *outColorSpace = colorSpace;
brianosmand93c1202016-03-10 07:49:08 -0800313 return true;
314 }
315 return false;
316}
kkinnunen3e980c32015-12-23 01:33:00 -0800317
318SkCommandLineConfigGpu* parse_command_line_config_gpu(const SkString& tag,
319 const SkTArray<SkString>& vias,
320 const SkString& options) {
321 // Defaults for GPU backend.
322 bool seenAPI = false;
bsalomon85b4b532016-04-05 11:06:27 -0700323 SkCommandLineConfigGpu::ContextType contextType = GrContextFactory::kNativeGL_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800324 bool seenUseNVPR = false;
325 bool useNVPR = false;
csmartdaltone0d36292016-07-29 08:14:20 -0700326 bool seenUseInstanced = false;
327 bool useInstanced = false;
kkinnunen3e980c32015-12-23 01:33:00 -0800328 bool seenUseDIText =false;
329 bool useDIText = false;
330 bool seenSamples = false;
331 int samples = 0;
brianosmand93c1202016-03-10 07:49:08 -0800332 bool seenColor = false;
bsalomon33069252016-09-28 08:49:53 -0700333 SkColorType colorType = kRGBA_8888_SkColorType;
brianosmanb109b8c2016-06-16 13:03:24 -0700334 sk_sp<SkColorSpace> colorSpace = nullptr;
kkinnunen3e980c32015-12-23 01:33:00 -0800335
336 SkTArray<SkString> optionParts;
337 SkStrSplit(options.c_str(), ",", kStrict_SkStrSplitMode, &optionParts);
338 for (int i = 0; i < optionParts.count(); ++i) {
339 SkTArray<SkString> keyValueParts;
340 SkStrSplit(optionParts[i].c_str(), "=", kStrict_SkStrSplitMode, &keyValueParts);
341 if (keyValueParts.count() != 2) {
342 return nullptr;
343 }
344 const SkString& key = keyValueParts[0];
345 const SkString& value = keyValueParts[1];
346 bool valueOk = false;
347 if (key.equals("api") && !seenAPI) {
348 valueOk = parse_option_gpu_api(value, &contextType);
349 seenAPI = true;
350 } else if (key.equals("nvpr") && !seenUseNVPR) {
351 valueOk = parse_option_bool(value, &useNVPR);
352 seenUseNVPR = true;
csmartdaltone0d36292016-07-29 08:14:20 -0700353 } else if (key.equals("inst") && !seenUseInstanced) {
354 valueOk = parse_option_bool(value, &useInstanced);
355 seenUseInstanced = true;
kkinnunen3e980c32015-12-23 01:33:00 -0800356 } else if (key.equals("dit") && !seenUseDIText) {
357 valueOk = parse_option_bool(value, &useDIText);
358 seenUseDIText = true;
359 } else if (key.equals("samples") && !seenSamples) {
360 valueOk = parse_option_int(value, &samples);
361 seenSamples = true;
brianosmand93c1202016-03-10 07:49:08 -0800362 } else if (key.equals("color") && !seenColor) {
brianosmanb109b8c2016-06-16 13:03:24 -0700363 valueOk = parse_option_gpu_color(value, &colorType, &colorSpace);
brianosmand93c1202016-03-10 07:49:08 -0800364 seenColor = true;
kkinnunen3e980c32015-12-23 01:33:00 -0800365 }
366 if (!valueOk) {
367 return nullptr;
368 }
369 }
csmartdaltone0d36292016-07-29 08:14:20 -0700370 return new SkCommandLineConfigGpu(tag, vias, contextType, useNVPR, useInstanced, useDIText,
371 samples, colorType, colorSpace);
kkinnunen3e980c32015-12-23 01:33:00 -0800372}
373#endif
374
375void ParseConfigs(const SkCommandLineFlags::StringArray& configs,
376 SkCommandLineConfigArray* outResult) {
377 outResult->reset();
378 for (int i = 0; i < configs.count(); ++i) {
379 SkString extendedBackend;
380 SkString extendedOptions;
381 SkString simpleBackend;
382 SkTArray<SkString> vias;
383
384 SkString tag(configs[i]);
385 SkTArray<SkString> parts;
bsalomon808ecbb2016-09-28 12:40:22 -0700386 SkStrSplit(tag.c_str(), "[", kStrict_SkStrSplitMode, &parts);
kkinnunen3e980c32015-12-23 01:33:00 -0800387 if (parts.count() == 2) {
388 SkTArray<SkString> parts2;
bsalomon808ecbb2016-09-28 12:40:22 -0700389 SkStrSplit(parts[1].c_str(), "]", kStrict_SkStrSplitMode, &parts2);
kkinnunen3e980c32015-12-23 01:33:00 -0800390 if (parts2.count() == 2 && parts2[1].isEmpty()) {
391 SkStrSplit(parts[0].c_str(), "-", kStrict_SkStrSplitMode, &vias);
392 if (vias.count()) {
393 extendedBackend = vias[vias.count() - 1];
394 vias.pop_back();
395 } else {
396 extendedBackend = parts[0];
397 }
398 extendedOptions = parts2[0];
bsalomon808ecbb2016-09-28 12:40:22 -0700399 simpleBackend.printf("%s[%s]", extendedBackend.c_str(), extendedOptions.c_str());
kkinnunen3e980c32015-12-23 01:33:00 -0800400 }
401 }
402
403 if (extendedBackend.isEmpty()) {
404 simpleBackend = tag;
405 SkStrSplit(tag.c_str(), "-", kStrict_SkStrSplitMode, &vias);
406 if (vias.count()) {
407 simpleBackend = vias[vias.count() - 1];
408 vias.pop_back();
409 }
bsalomon7c2213b2016-09-29 10:07:22 -0700410 // Note: no #if SK_ANGLE: this is a special rule in the via-tag grammar.
411 if (vias.count() && simpleBackend.equals("gl") &&
412 vias[vias.count() - 1].equals("angle")) {
413 simpleBackend = "angle-gl";
414 vias.pop_back();
415 }
416
kkinnunen3e980c32015-12-23 01:33:00 -0800417 for (auto& predefinedConfig : gPredefinedConfigs) {
418 if (simpleBackend.equals(predefinedConfig.predefinedConfig)) {
419 extendedBackend = predefinedConfig.backend;
420 extendedOptions = predefinedConfig.options;
421 break;
422 }
423 }
424 }
425 SkCommandLineConfig* parsedConfig = nullptr;
426#if SK_SUPPORT_GPU
427 if (extendedBackend.equals("gpu")) {
428 parsedConfig = parse_command_line_config_gpu(tag, vias, extendedOptions);
429 }
430#endif
431 if (!parsedConfig) {
432 parsedConfig = new SkCommandLineConfig(tag, simpleBackend, vias);
433 }
434 outResult->emplace_back(parsedConfig);
435 }
436}