blob: a0d1c77dd962bc594c7ef77a793e720051349ca6 [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
48 " vk"
49#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
127 "\vk \t= gpu(api=vulkan)\n"
128#endif
kkinnunen3e980c32015-12-23 01:33:00 -0800129#endif
130 ;
131
132DEFINE_extended_string(config, defaultConfigs, configHelp, configExtendedHelp);
133
134static const struct {
135 const char* predefinedConfig;
136 const char* backend;
137 const char* options;
138} gPredefinedConfigs[] = {
139#if SK_SUPPORT_GPU
140 { "gpu", "gpu", "" },
bsalomonb8797bb2016-04-05 08:49:38 -0700141 { "gl", "gpu", "api=gl" },
kkinnunen3e980c32015-12-23 01:33:00 -0800142 { "msaa4", "gpu", "samples=4" },
bsalomonb8797bb2016-04-05 08:49:38 -0700143 { "glmsaa4", "gpu", "api=gl,samples=4" },
kkinnunen3e980c32015-12-23 01:33:00 -0800144 { "msaa16", "gpu", "samples=16" },
cdaltonc28afdb2016-03-29 20:05:07 -0700145 { "nvpr4", "gpu", "nvpr=true,samples=4" },
bsalomonb8797bb2016-04-05 08:49:38 -0700146 { "glnvpr4", "gpu", "api=gl,nvpr=true,samples=4" },
cdaltonc28afdb2016-03-29 20:05:07 -0700147 { "nvpr16", "gpu", "nvpr=true,samples=16" },
148 { "nvprdit4", "gpu", "nvpr=true,samples=4,dit=true" },
bsalomonb8797bb2016-04-05 08:49:38 -0700149 { "glnvprdit4", "gpu", "api=gl,nvpr=true,samples=4,dit=true" },
cdaltonc28afdb2016-03-29 20:05:07 -0700150 { "nvprdit16", "gpu", "nvpr=true,samples=16,dit=true" },
brianosmand93c1202016-03-10 07:49:08 -0800151 { "gpuf16", "gpu", "color=f16" },
152 { "gpusrgb", "gpu", "color=srgb" },
bsalomonb8797bb2016-04-05 08:49:38 -0700153 { "glsrgb", "gpu", "api=gl,color=srgb" },
kkinnunen3e980c32015-12-23 01:33:00 -0800154 { "gpudft", "gpu", "dit=true" },
155 { "gpudebug", "gpu", "api=debug" },
156 { "gpunull", "gpu", "api=null" },
157 { "debug", "gpu", "api=debug" },
158 { "nullgpu", "gpu", "api=null" }
159#if SK_ANGLE
160#ifdef SK_BUILD_FOR_WIN
161 , { "angle", "gpu", "api=angle" }
162#endif
163 , { "angle-gl", "gpu", "api=angle-gl" }
164#endif
165#if SK_COMMAND_BUFFER
166 , { "commandbuffer", "gpu", "api=commandbuffer" }
167#endif
168#if SK_MESA
169 , { "mesa", "gpu", "api=mesa" }
170#endif
bsalomondc0fcd42016-04-11 14:21:33 -0700171#ifdef SK_VULKAN
172 , { "vk", "gpu", "api=vulkan" }
173#endif
174
kkinnunen3e980c32015-12-23 01:33:00 -0800175#else
176 { "", "", "" }
177#endif
178};
179
180SkCommandLineConfig::SkCommandLineConfig(const SkString& tag, const SkString& backend,
181 const SkTArray<SkString>& viaParts)
182 : fTag(tag)
183 , fBackend(backend)
184 , fViaParts(viaParts) {
185}
186SkCommandLineConfig::~SkCommandLineConfig() {
187}
188
189#if SK_SUPPORT_GPU
190SkCommandLineConfigGpu::SkCommandLineConfigGpu(
191 const SkString& tag, const SkTArray<SkString>& viaParts,
brianosmand93c1202016-03-10 07:49:08 -0800192 ContextType contextType, bool useNVPR, bool useDIText, int samples,
193 SkColorType colorType, SkColorProfileType profileType)
kkinnunen3e980c32015-12-23 01:33:00 -0800194 : SkCommandLineConfig(tag, SkString("gpu"), viaParts)
195 , fContextType(contextType)
196 , fUseNVPR(useNVPR)
197 , fUseDIText(useDIText)
brianosmand93c1202016-03-10 07:49:08 -0800198 , fSamples(samples)
199 , fColorType(colorType)
200 , fProfileType(profileType) {
kkinnunen3e980c32015-12-23 01:33:00 -0800201}
202static bool parse_option_int(const SkString& value, int* outInt) {
203 if (value.isEmpty()) {
204 return false;
205 }
206 char* endptr = nullptr;
207 long intValue = strtol(value.c_str(), &endptr, 10);
208 if (*endptr != '\0') {
209 return false;
210 }
211 *outInt = static_cast<int>(intValue);
212 return true;
213}
214static bool parse_option_bool(const SkString& value, bool* outBool) {
215 if (value.equals("true")) {
216 *outBool = true;
217 return true;
218 }
219 if (value.equals("false")) {
220 *outBool = false;
221 return true;
222 }
223 return false;
224}
225static bool parse_option_gpu_api(const SkString& value,
226 SkCommandLineConfigGpu::ContextType* outContextType) {
kkinnunen3e980c32015-12-23 01:33:00 -0800227 if (value.equals("gl")) {
bsalomon85b4b532016-04-05 11:06:27 -0700228 *outContextType = GrContextFactory::kGL_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800229 return true;
230 }
231 if (value.equals("gles")) {
bsalomon85b4b532016-04-05 11:06:27 -0700232 *outContextType = GrContextFactory::kGLES_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800233 return true;
234 }
235 if (value.equals("debug")) {
bsalomon85b4b532016-04-05 11:06:27 -0700236 *outContextType = GrContextFactory::kDebugGL_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800237 return true;
238 }
239 if (value.equals("null")) {
bsalomon85b4b532016-04-05 11:06:27 -0700240 *outContextType = GrContextFactory::kNullGL_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800241 return true;
242 }
243#if SK_ANGLE
244#ifdef SK_BUILD_FOR_WIN
245 if (value.equals("angle")) {
bsalomon85b4b532016-04-05 11:06:27 -0700246 *outContextType = GrContextFactory::kANGLE_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800247 return true;
248 }
249#endif
250 if (value.equals("angle-gl")) {
bsalomon85b4b532016-04-05 11:06:27 -0700251 *outContextType = GrContextFactory::kANGLE_GL_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800252 return true;
253 }
254#endif
255#if SK_COMMAND_BUFFER
256 if (value.equals("commandbuffer")) {
bsalomon85b4b532016-04-05 11:06:27 -0700257 *outContextType = GrContextFactory::kCommandBuffer_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800258 return true;
259 }
260#endif
261#if SK_MESA
262 if (value.equals("mesa")) {
bsalomon85b4b532016-04-05 11:06:27 -0700263 *outContextType = GrContextFactory::kMESA_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800264 return true;
265 }
266#endif
bsalomondc0fcd42016-04-11 14:21:33 -0700267#ifdef SK_VULKAN
268 if (value.equals("vulkan")) {
269 *outContextType = GrContextFactory::kVulkan_ContextType;
270 return true;
271 }
272#endif
kkinnunen3e980c32015-12-23 01:33:00 -0800273 return false;
274}
brianosmand93c1202016-03-10 07:49:08 -0800275static bool parse_option_gpu_color(const SkString& value,
276 SkColorType* outColorType,
277 SkColorProfileType* outProfileType) {
278 if (value.equals("8888")) {
279 *outColorType = kN32_SkColorType;
280 *outProfileType = kLinear_SkColorProfileType;
281 return true;
282 }
283 if (value.equals("f16")) {
284 *outColorType = kRGBA_F16_SkColorType;
285 *outProfileType = kLinear_SkColorProfileType;
286 return true;
287 }
288 if (value.equals("srgb")) {
289 *outColorType = kN32_SkColorType;
290 *outProfileType = kSRGB_SkColorProfileType;
291 return true;
292 }
293 return false;
294}
kkinnunen3e980c32015-12-23 01:33:00 -0800295
296SkCommandLineConfigGpu* parse_command_line_config_gpu(const SkString& tag,
297 const SkTArray<SkString>& vias,
298 const SkString& options) {
299 // Defaults for GPU backend.
300 bool seenAPI = false;
bsalomon85b4b532016-04-05 11:06:27 -0700301 SkCommandLineConfigGpu::ContextType contextType = GrContextFactory::kNativeGL_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800302 bool seenUseNVPR = false;
303 bool useNVPR = false;
304 bool seenUseDIText =false;
305 bool useDIText = false;
306 bool seenSamples = false;
307 int samples = 0;
brianosmand93c1202016-03-10 07:49:08 -0800308 bool seenColor = false;
309 SkColorType colorType = kN32_SkColorType;
310 SkColorProfileType profileType = kLinear_SkColorProfileType;
kkinnunen3e980c32015-12-23 01:33:00 -0800311
312 SkTArray<SkString> optionParts;
313 SkStrSplit(options.c_str(), ",", kStrict_SkStrSplitMode, &optionParts);
314 for (int i = 0; i < optionParts.count(); ++i) {
315 SkTArray<SkString> keyValueParts;
316 SkStrSplit(optionParts[i].c_str(), "=", kStrict_SkStrSplitMode, &keyValueParts);
317 if (keyValueParts.count() != 2) {
318 return nullptr;
319 }
320 const SkString& key = keyValueParts[0];
321 const SkString& value = keyValueParts[1];
322 bool valueOk = false;
323 if (key.equals("api") && !seenAPI) {
324 valueOk = parse_option_gpu_api(value, &contextType);
325 seenAPI = true;
326 } else if (key.equals("nvpr") && !seenUseNVPR) {
327 valueOk = parse_option_bool(value, &useNVPR);
328 seenUseNVPR = true;
329 } else if (key.equals("dit") && !seenUseDIText) {
330 valueOk = parse_option_bool(value, &useDIText);
331 seenUseDIText = true;
332 } else if (key.equals("samples") && !seenSamples) {
333 valueOk = parse_option_int(value, &samples);
334 seenSamples = true;
brianosmand93c1202016-03-10 07:49:08 -0800335 } else if (key.equals("color") && !seenColor) {
336 valueOk = parse_option_gpu_color(value, &colorType, &profileType);
337 seenColor = true;
kkinnunen3e980c32015-12-23 01:33:00 -0800338 }
339 if (!valueOk) {
340 return nullptr;
341 }
342 }
brianosmand93c1202016-03-10 07:49:08 -0800343 return new SkCommandLineConfigGpu(tag, vias, contextType, useNVPR, useDIText, samples,
344 colorType, profileType);
kkinnunen3e980c32015-12-23 01:33:00 -0800345}
346#endif
347
348void ParseConfigs(const SkCommandLineFlags::StringArray& configs,
349 SkCommandLineConfigArray* outResult) {
350 outResult->reset();
351 for (int i = 0; i < configs.count(); ++i) {
352 SkString extendedBackend;
353 SkString extendedOptions;
354 SkString simpleBackend;
355 SkTArray<SkString> vias;
356
357 SkString tag(configs[i]);
358 SkTArray<SkString> parts;
359 SkStrSplit(tag.c_str(), "(", kStrict_SkStrSplitMode, &parts);
360 if (parts.count() == 2) {
361 SkTArray<SkString> parts2;
362 SkStrSplit(parts[1].c_str(), ")", kStrict_SkStrSplitMode, &parts2);
363 if (parts2.count() == 2 && parts2[1].isEmpty()) {
364 SkStrSplit(parts[0].c_str(), "-", kStrict_SkStrSplitMode, &vias);
365 if (vias.count()) {
366 extendedBackend = vias[vias.count() - 1];
367 vias.pop_back();
368 } else {
369 extendedBackend = parts[0];
370 }
371 extendedOptions = parts2[0];
372 simpleBackend.printf("%s(%s)", extendedBackend.c_str(), extendedOptions.c_str());
373 }
374 }
375
376 if (extendedBackend.isEmpty()) {
377 simpleBackend = tag;
378 SkStrSplit(tag.c_str(), "-", kStrict_SkStrSplitMode, &vias);
379 if (vias.count()) {
380 simpleBackend = vias[vias.count() - 1];
381 vias.pop_back();
382 }
383 // Note: no #if SK_ANGLE: this is a special rule in the via-tag grammar.
384 if (vias.count() && simpleBackend.equals("gl") &&
385 vias[vias.count() - 1].equals("angle")) {
386 simpleBackend = "angle-gl";
387 vias.pop_back();
388 }
389
390 for (auto& predefinedConfig : gPredefinedConfigs) {
391 if (simpleBackend.equals(predefinedConfig.predefinedConfig)) {
392 extendedBackend = predefinedConfig.backend;
393 extendedOptions = predefinedConfig.options;
394 break;
395 }
396 }
397 }
398 SkCommandLineConfig* parsedConfig = nullptr;
399#if SK_SUPPORT_GPU
400 if (extendedBackend.equals("gpu")) {
401 parsedConfig = parse_command_line_config_gpu(tag, vias, extendedOptions);
402 }
403#endif
404 if (!parsedConfig) {
405 parsedConfig = new SkCommandLineConfig(tag, simpleBackend, vias);
406 }
407 outResult->emplace_back(parsedConfig);
408 }
409}