blob: 1c7920eb551df323fbec67955078a4ad6f0f4734 [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"
9
10#include <stdlib.h>
11
bsalomon3724e572016-03-30 18:56:19 -070012#if SK_SUPPORT_GPU
13using sk_gpu_test::GrContextFactory;
14#endif
15
kkinnunen3e980c32015-12-23 01:33:00 -080016static const char defaultConfigs[] =
17 "565 8888 gpu nonrendering"
18#if SK_ANGLE
19#ifdef SK_BUILD_FOR_WIN
20 " angle"
21#endif
22#endif
23#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
24 " hwui"
25#endif
26 ;
27
28static const char configHelp[] =
bsalomonb8797bb2016-04-05 08:49:38 -070029 "Options: 565 8888 debug gpu gl gpudebug gpudft gpunull "
30 "msaa16 msaa4 glmsaa4 gpuf16 gpusrgb glsrgb nonrendering null nullgpu "
31 "nvpr16 nvpr4 nvprdit16 nvprdit4 glnvpr4 glnvprdit4 pdf skp svg xps"
kkinnunen3e980c32015-12-23 01:33:00 -080032#if SK_ANGLE
33#ifdef SK_BUILD_FOR_WIN
brianosman744898a2016-03-01 13:44:28 -080034 " angle"
kkinnunen3e980c32015-12-23 01:33:00 -080035#endif
36 " angle-gl"
37#endif
38#if SK_COMMAND_BUFFER
39 " commandbuffer"
40#endif
41#if SK_MESA
42 " mesa"
43#endif
44#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
45 " hwui"
46#endif
bsalomondc0fcd42016-04-11 14:21:33 -070047#ifdef SK_VULKAN
brianosmana56800a2016-05-23 10:11:07 -070048 " vk vksrgb"
bsalomondc0fcd42016-04-11 14:21:33 -070049#endif
kkinnunen3e980c32015-12-23 01:33:00 -080050 " or use extended form 'backend(option=value,...)'.\n";
51
52static const char configExtendedHelp[] =
53 "Extended form: 'backend(option=value,...)'\n\n"
54 "Possible backends and options:\n"
55#if SK_SUPPORT_GPU
56 "\n"
brianosmand93c1202016-03-10 07:49:08 -080057 "gpu(api=string,color=string,dit=bool,nvpr=bool,samples=int)\tGPU backend\n"
kkinnunen3e980c32015-12-23 01:33:00 -080058 "\tapi\ttype: string\tdefault: native.\n"
59 "\t Select graphics API to use with gpu backend.\n"
60 "\t Options:\n"
61 "\t\tnative\t\t\tUse platform default OpenGL or OpenGL ES backend.\n"
62 "\t\tgl \t\t\tUse OpenGL.\n"
63 "\t\tgles \t\t\tUse OpenGL ES.\n"
64 "\t\tdebug \t\t\tUse debug OpenGL.\n"
65 "\t\tnull \t\t\tUse null OpenGL.\n"
66#if SK_ANGLE
67#ifdef SK_BUILD_FOR_WIN
68 "\t\tangle\t\t\tUse ANGLE DirectX.\n"
69#endif
70 "\t\tangle-gl\t\t\tUse ANGLE OpenGL.\n"
71#endif
72#if SK_COMMAND_BUFFER
73 "\t\tcommandbuffer\t\tUse command buffer.\n"
74#endif
75#if SK_MESA
76 "\t\tmesa\t\t\tUse MESA.\n"
77#endif
bsalomondc0fcd42016-04-11 14:21:33 -070078#ifdef SK_VULKAN
79 "\t\tvulkan\t\t\tUse Vulkan.\n"
80#endif
brianosmand93c1202016-03-10 07:49:08 -080081 "\tcolor\ttype: string\tdefault: 8888.\n"
82 "\t Select framebuffer color format.\n"
83 "\t Options:\n"
84 "\t\t8888\t\t\tLinear 8888.\n"
85 "\t\tf16 \t\t\tLinear 16-bit floating point.\n"
86 "\t\tsrgb\t\t\tsRGB 8888.\n"
kkinnunen3e980c32015-12-23 01:33:00 -080087 "\tdit\ttype: bool\tdefault: false.\n"
88 "\t Use device independent text.\n"
89 "\tnvpr\ttype: bool\tdefault: false.\n"
90 "\t Use NV_path_rendering OpenGL and OpenGL ES extension.\n"
91 "\tsamples\ttype: int\tdefault: 0.\n"
92 "\t Use multisampling with N samples.\n"
93 "\n"
94 "Predefined configs:\n\n"
cdaltonc28afdb2016-03-29 20:05:07 -070095 "\tgpu \t= gpu()\n"
bsalomonb8797bb2016-04-05 08:49:38 -070096 "\tgl \t= gpu(api=gl)\n"
cdaltonc28afdb2016-03-29 20:05:07 -070097 "\tmsaa4 \t= gpu(samples=4)\n"
bsalomonb8797bb2016-04-05 08:49:38 -070098 "\tglmsaa4 \t= gpu(api=gl,samples=4)\n"
cdaltonc28afdb2016-03-29 20:05:07 -070099 "\tmsaa16 \t= gpu(samples=16)\n"
100 "\tnvpr4 \t= gpu(nvpr=true,samples=4)\n"
bsalomonb8797bb2016-04-05 08:49:38 -0700101 "\tglnvpr4 \t= gpu(api=gl,nvpr=true,samples=4)\n"
cdaltonc28afdb2016-03-29 20:05:07 -0700102 "\tnvpr16 \t= gpu(nvpr=true,samples=16)\n"
103 "\tnvprdit4 \t= gpu(nvpr=true,samples=4,dit=true)\n"
bsalomonb8797bb2016-04-05 08:49:38 -0700104 "\tglnvprdit4\t= gpu(api=gl,nvpr=true,samples=4,dit=true)\n"
cdaltonc28afdb2016-03-29 20:05:07 -0700105 "\tnvprdit16 \t= gpu(nvpr=true,samples=16,dit=true)\n"
brianosmand93c1202016-03-10 07:49:08 -0800106 "\tgpuf16 \t= gpu(color=f16)\n"
107 "\tgpusrgb \t= gpu(color=srgb)\n"
bsalomonb8797bb2016-04-05 08:49:38 -0700108 "\tglsrgb \t= gpu(api=gl,color=srgb)\n"
kkinnunen3e980c32015-12-23 01:33:00 -0800109 "\tgpudft \t= gpu(dit=true)\n"
110 "\tgpudebug \t= gpu(api=debug)\n"
111 "\tgpunull \t= gpu(api=null)\n"
112 "\tdebug \t= gpu(api=debug)\n"
113 "\tnullgpu \t= gpu(api=null)\n"
114#if SK_ANGLE
115#ifdef SK_BUILD_FOR_WIN
116 "\tangle \t= gpu(api=angle)\n"
117#endif
118 "\tangle-gl \t= gpu(api=angle-gl)\n"
119#endif
120#if SK_COMMAND_BUFFER
121 "\tcommandbuffer\t= gpu(api=commandbuffer)\n"
122#endif
123#if SK_MESA
124 "\tmesa \t= gpu(api=mesa)\n"
125#endif
bsalomondc0fcd42016-04-11 14:21:33 -0700126#ifdef SK_VULKAN
brianosmana56800a2016-05-23 10:11:07 -0700127 "\tvk \t= gpu(api=vulkan)\n"
128 "\tvksrgb \t= gpu(api=vulkan,color=srgb)\n"
bsalomondc0fcd42016-04-11 14:21:33 -0700129#endif
kkinnunen3e980c32015-12-23 01:33:00 -0800130#endif
131 ;
132
133DEFINE_extended_string(config, defaultConfigs, configHelp, configExtendedHelp);
134
135static const struct {
136 const char* predefinedConfig;
137 const char* backend;
138 const char* options;
139} gPredefinedConfigs[] = {
140#if SK_SUPPORT_GPU
141 { "gpu", "gpu", "" },
bsalomonb8797bb2016-04-05 08:49:38 -0700142 { "gl", "gpu", "api=gl" },
kkinnunen3e980c32015-12-23 01:33:00 -0800143 { "msaa4", "gpu", "samples=4" },
bsalomonb8797bb2016-04-05 08:49:38 -0700144 { "glmsaa4", "gpu", "api=gl,samples=4" },
kkinnunen3e980c32015-12-23 01:33:00 -0800145 { "msaa16", "gpu", "samples=16" },
cdaltonc28afdb2016-03-29 20:05:07 -0700146 { "nvpr4", "gpu", "nvpr=true,samples=4" },
bsalomonb8797bb2016-04-05 08:49:38 -0700147 { "glnvpr4", "gpu", "api=gl,nvpr=true,samples=4" },
cdaltonc28afdb2016-03-29 20:05:07 -0700148 { "nvpr16", "gpu", "nvpr=true,samples=16" },
149 { "nvprdit4", "gpu", "nvpr=true,samples=4,dit=true" },
bsalomonb8797bb2016-04-05 08:49:38 -0700150 { "glnvprdit4", "gpu", "api=gl,nvpr=true,samples=4,dit=true" },
cdaltonc28afdb2016-03-29 20:05:07 -0700151 { "nvprdit16", "gpu", "nvpr=true,samples=16,dit=true" },
brianosmand93c1202016-03-10 07:49:08 -0800152 { "gpuf16", "gpu", "color=f16" },
153 { "gpusrgb", "gpu", "color=srgb" },
bsalomonb8797bb2016-04-05 08:49:38 -0700154 { "glsrgb", "gpu", "api=gl,color=srgb" },
kkinnunen3e980c32015-12-23 01:33:00 -0800155 { "gpudft", "gpu", "dit=true" },
156 { "gpudebug", "gpu", "api=debug" },
157 { "gpunull", "gpu", "api=null" },
158 { "debug", "gpu", "api=debug" },
159 { "nullgpu", "gpu", "api=null" }
160#if SK_ANGLE
161#ifdef SK_BUILD_FOR_WIN
162 , { "angle", "gpu", "api=angle" }
163#endif
164 , { "angle-gl", "gpu", "api=angle-gl" }
165#endif
166#if SK_COMMAND_BUFFER
167 , { "commandbuffer", "gpu", "api=commandbuffer" }
168#endif
169#if SK_MESA
170 , { "mesa", "gpu", "api=mesa" }
171#endif
bsalomondc0fcd42016-04-11 14:21:33 -0700172#ifdef SK_VULKAN
173 , { "vk", "gpu", "api=vulkan" }
brianosmana56800a2016-05-23 10:11:07 -0700174 , { "vksrgb", "gpu", "api=vulkan,color=srgb" }
bsalomondc0fcd42016-04-11 14:21:33 -0700175#endif
176
kkinnunen3e980c32015-12-23 01:33:00 -0800177#else
178 { "", "", "" }
179#endif
180};
181
182SkCommandLineConfig::SkCommandLineConfig(const SkString& tag, const SkString& backend,
183 const SkTArray<SkString>& viaParts)
184 : fTag(tag)
185 , fBackend(backend)
186 , fViaParts(viaParts) {
187}
188SkCommandLineConfig::~SkCommandLineConfig() {
189}
190
191#if SK_SUPPORT_GPU
192SkCommandLineConfigGpu::SkCommandLineConfigGpu(
193 const SkString& tag, const SkTArray<SkString>& viaParts,
brianosmand93c1202016-03-10 07:49:08 -0800194 ContextType contextType, bool useNVPR, bool useDIText, int samples,
brianosmanab824182016-06-16 11:41:44 -0700195 SkColorType colorType, SkColorProfileType profileType)
kkinnunen3e980c32015-12-23 01:33:00 -0800196 : SkCommandLineConfig(tag, SkString("gpu"), viaParts)
197 , fContextType(contextType)
198 , fUseNVPR(useNVPR)
199 , fUseDIText(useDIText)
brianosmand93c1202016-03-10 07:49:08 -0800200 , fSamples(samples)
201 , fColorType(colorType)
brianosmanab824182016-06-16 11:41:44 -0700202 , fProfileType(profileType) {
kkinnunen3e980c32015-12-23 01:33:00 -0800203}
204static bool parse_option_int(const SkString& value, int* outInt) {
205 if (value.isEmpty()) {
206 return false;
207 }
208 char* endptr = nullptr;
209 long intValue = strtol(value.c_str(), &endptr, 10);
210 if (*endptr != '\0') {
211 return false;
212 }
213 *outInt = static_cast<int>(intValue);
214 return true;
215}
216static bool parse_option_bool(const SkString& value, bool* outBool) {
217 if (value.equals("true")) {
218 *outBool = true;
219 return true;
220 }
221 if (value.equals("false")) {
222 *outBool = false;
223 return true;
224 }
225 return false;
226}
227static bool parse_option_gpu_api(const SkString& value,
228 SkCommandLineConfigGpu::ContextType* outContextType) {
kkinnunen3e980c32015-12-23 01:33:00 -0800229 if (value.equals("gl")) {
bsalomon85b4b532016-04-05 11:06:27 -0700230 *outContextType = GrContextFactory::kGL_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800231 return true;
232 }
233 if (value.equals("gles")) {
bsalomon85b4b532016-04-05 11:06:27 -0700234 *outContextType = GrContextFactory::kGLES_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800235 return true;
236 }
237 if (value.equals("debug")) {
bsalomon85b4b532016-04-05 11:06:27 -0700238 *outContextType = GrContextFactory::kDebugGL_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800239 return true;
240 }
241 if (value.equals("null")) {
bsalomon85b4b532016-04-05 11:06:27 -0700242 *outContextType = GrContextFactory::kNullGL_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800243 return true;
244 }
245#if SK_ANGLE
246#ifdef SK_BUILD_FOR_WIN
247 if (value.equals("angle")) {
bsalomon85b4b532016-04-05 11:06:27 -0700248 *outContextType = GrContextFactory::kANGLE_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800249 return true;
250 }
251#endif
252 if (value.equals("angle-gl")) {
bsalomon85b4b532016-04-05 11:06:27 -0700253 *outContextType = GrContextFactory::kANGLE_GL_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800254 return true;
255 }
256#endif
257#if SK_COMMAND_BUFFER
258 if (value.equals("commandbuffer")) {
bsalomon85b4b532016-04-05 11:06:27 -0700259 *outContextType = GrContextFactory::kCommandBuffer_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800260 return true;
261 }
262#endif
263#if SK_MESA
264 if (value.equals("mesa")) {
bsalomon85b4b532016-04-05 11:06:27 -0700265 *outContextType = GrContextFactory::kMESA_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800266 return true;
267 }
268#endif
bsalomondc0fcd42016-04-11 14:21:33 -0700269#ifdef SK_VULKAN
270 if (value.equals("vulkan")) {
271 *outContextType = GrContextFactory::kVulkan_ContextType;
272 return true;
273 }
274#endif
kkinnunen3e980c32015-12-23 01:33:00 -0800275 return false;
276}
brianosmand93c1202016-03-10 07:49:08 -0800277static bool parse_option_gpu_color(const SkString& value,
278 SkColorType* outColorType,
brianosmanab824182016-06-16 11:41:44 -0700279 SkColorProfileType* outProfileType) {
brianosmand93c1202016-03-10 07:49:08 -0800280 if (value.equals("8888")) {
281 *outColorType = kN32_SkColorType;
brianosmanab824182016-06-16 11:41:44 -0700282 *outProfileType = kLinear_SkColorProfileType;
brianosmand93c1202016-03-10 07:49:08 -0800283 return true;
284 }
285 if (value.equals("f16")) {
286 *outColorType = kRGBA_F16_SkColorType;
brianosmanab824182016-06-16 11:41:44 -0700287 *outProfileType = kLinear_SkColorProfileType;
brianosmand93c1202016-03-10 07:49:08 -0800288 return true;
289 }
290 if (value.equals("srgb")) {
291 *outColorType = kN32_SkColorType;
brianosmanab824182016-06-16 11:41:44 -0700292 *outProfileType = kSRGB_SkColorProfileType;
brianosmand93c1202016-03-10 07:49:08 -0800293 return true;
294 }
295 return false;
296}
kkinnunen3e980c32015-12-23 01:33:00 -0800297
298SkCommandLineConfigGpu* parse_command_line_config_gpu(const SkString& tag,
299 const SkTArray<SkString>& vias,
300 const SkString& options) {
301 // Defaults for GPU backend.
302 bool seenAPI = false;
bsalomon85b4b532016-04-05 11:06:27 -0700303 SkCommandLineConfigGpu::ContextType contextType = GrContextFactory::kNativeGL_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800304 bool seenUseNVPR = false;
305 bool useNVPR = false;
306 bool seenUseDIText =false;
307 bool useDIText = false;
308 bool seenSamples = false;
309 int samples = 0;
brianosmand93c1202016-03-10 07:49:08 -0800310 bool seenColor = false;
311 SkColorType colorType = kN32_SkColorType;
brianosmanab824182016-06-16 11:41:44 -0700312 SkColorProfileType profileType = kLinear_SkColorProfileType;
kkinnunen3e980c32015-12-23 01:33:00 -0800313
314 SkTArray<SkString> optionParts;
315 SkStrSplit(options.c_str(), ",", kStrict_SkStrSplitMode, &optionParts);
316 for (int i = 0; i < optionParts.count(); ++i) {
317 SkTArray<SkString> keyValueParts;
318 SkStrSplit(optionParts[i].c_str(), "=", kStrict_SkStrSplitMode, &keyValueParts);
319 if (keyValueParts.count() != 2) {
320 return nullptr;
321 }
322 const SkString& key = keyValueParts[0];
323 const SkString& value = keyValueParts[1];
324 bool valueOk = false;
325 if (key.equals("api") && !seenAPI) {
326 valueOk = parse_option_gpu_api(value, &contextType);
327 seenAPI = true;
328 } else if (key.equals("nvpr") && !seenUseNVPR) {
329 valueOk = parse_option_bool(value, &useNVPR);
330 seenUseNVPR = true;
331 } else if (key.equals("dit") && !seenUseDIText) {
332 valueOk = parse_option_bool(value, &useDIText);
333 seenUseDIText = true;
334 } else if (key.equals("samples") && !seenSamples) {
335 valueOk = parse_option_int(value, &samples);
336 seenSamples = true;
brianosmand93c1202016-03-10 07:49:08 -0800337 } else if (key.equals("color") && !seenColor) {
brianosmanab824182016-06-16 11:41:44 -0700338 valueOk = parse_option_gpu_color(value, &colorType, &profileType);
brianosmand93c1202016-03-10 07:49:08 -0800339 seenColor = true;
kkinnunen3e980c32015-12-23 01:33:00 -0800340 }
341 if (!valueOk) {
342 return nullptr;
343 }
344 }
brianosmand93c1202016-03-10 07:49:08 -0800345 return new SkCommandLineConfigGpu(tag, vias, contextType, useNVPR, useDIText, samples,
brianosmanab824182016-06-16 11:41:44 -0700346 colorType, profileType);
kkinnunen3e980c32015-12-23 01:33:00 -0800347}
348#endif
349
350void ParseConfigs(const SkCommandLineFlags::StringArray& configs,
351 SkCommandLineConfigArray* outResult) {
352 outResult->reset();
353 for (int i = 0; i < configs.count(); ++i) {
354 SkString extendedBackend;
355 SkString extendedOptions;
356 SkString simpleBackend;
357 SkTArray<SkString> vias;
358
359 SkString tag(configs[i]);
360 SkTArray<SkString> parts;
361 SkStrSplit(tag.c_str(), "(", kStrict_SkStrSplitMode, &parts);
362 if (parts.count() == 2) {
363 SkTArray<SkString> parts2;
364 SkStrSplit(parts[1].c_str(), ")", kStrict_SkStrSplitMode, &parts2);
365 if (parts2.count() == 2 && parts2[1].isEmpty()) {
366 SkStrSplit(parts[0].c_str(), "-", kStrict_SkStrSplitMode, &vias);
367 if (vias.count()) {
368 extendedBackend = vias[vias.count() - 1];
369 vias.pop_back();
370 } else {
371 extendedBackend = parts[0];
372 }
373 extendedOptions = parts2[0];
374 simpleBackend.printf("%s(%s)", extendedBackend.c_str(), extendedOptions.c_str());
375 }
376 }
377
378 if (extendedBackend.isEmpty()) {
379 simpleBackend = tag;
380 SkStrSplit(tag.c_str(), "-", kStrict_SkStrSplitMode, &vias);
381 if (vias.count()) {
382 simpleBackend = vias[vias.count() - 1];
383 vias.pop_back();
384 }
385 // Note: no #if SK_ANGLE: this is a special rule in the via-tag grammar.
386 if (vias.count() && simpleBackend.equals("gl") &&
387 vias[vias.count() - 1].equals("angle")) {
388 simpleBackend = "angle-gl";
389 vias.pop_back();
390 }
391
392 for (auto& predefinedConfig : gPredefinedConfigs) {
393 if (simpleBackend.equals(predefinedConfig.predefinedConfig)) {
394 extendedBackend = predefinedConfig.backend;
395 extendedOptions = predefinedConfig.options;
396 break;
397 }
398 }
399 }
400 SkCommandLineConfig* parsedConfig = nullptr;
401#if SK_SUPPORT_GPU
402 if (extendedBackend.equals("gpu")) {
403 parsedConfig = parse_command_line_config_gpu(tag, vias, extendedOptions);
404 }
405#endif
406 if (!parsedConfig) {
407 parsedConfig = new SkCommandLineConfig(tag, simpleBackend, vias);
408 }
409 outResult->emplace_back(parsedConfig);
410 }
411}