blob: 973f14eaa17d9db82d7a5ecf168ed42623a41d8f [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
47 " or use extended form 'backend(option=value,...)'.\n";
48
49static const char configExtendedHelp[] =
50 "Extended form: 'backend(option=value,...)'\n\n"
51 "Possible backends and options:\n"
52#if SK_SUPPORT_GPU
53 "\n"
brianosmand93c1202016-03-10 07:49:08 -080054 "gpu(api=string,color=string,dit=bool,nvpr=bool,samples=int)\tGPU backend\n"
kkinnunen3e980c32015-12-23 01:33:00 -080055 "\tapi\ttype: string\tdefault: native.\n"
56 "\t Select graphics API to use with gpu backend.\n"
57 "\t Options:\n"
58 "\t\tnative\t\t\tUse platform default OpenGL or OpenGL ES backend.\n"
59 "\t\tgl \t\t\tUse OpenGL.\n"
60 "\t\tgles \t\t\tUse OpenGL ES.\n"
61 "\t\tdebug \t\t\tUse debug OpenGL.\n"
62 "\t\tnull \t\t\tUse null OpenGL.\n"
63#if SK_ANGLE
64#ifdef SK_BUILD_FOR_WIN
65 "\t\tangle\t\t\tUse ANGLE DirectX.\n"
66#endif
67 "\t\tangle-gl\t\t\tUse ANGLE OpenGL.\n"
68#endif
69#if SK_COMMAND_BUFFER
70 "\t\tcommandbuffer\t\tUse command buffer.\n"
71#endif
72#if SK_MESA
73 "\t\tmesa\t\t\tUse MESA.\n"
74#endif
brianosmand93c1202016-03-10 07:49:08 -080075 "\tcolor\ttype: string\tdefault: 8888.\n"
76 "\t Select framebuffer color format.\n"
77 "\t Options:\n"
78 "\t\t8888\t\t\tLinear 8888.\n"
79 "\t\tf16 \t\t\tLinear 16-bit floating point.\n"
80 "\t\tsrgb\t\t\tsRGB 8888.\n"
kkinnunen3e980c32015-12-23 01:33:00 -080081 "\tdit\ttype: bool\tdefault: false.\n"
82 "\t Use device independent text.\n"
83 "\tnvpr\ttype: bool\tdefault: false.\n"
84 "\t Use NV_path_rendering OpenGL and OpenGL ES extension.\n"
85 "\tsamples\ttype: int\tdefault: 0.\n"
86 "\t Use multisampling with N samples.\n"
87 "\n"
88 "Predefined configs:\n\n"
cdaltonc28afdb2016-03-29 20:05:07 -070089 "\tgpu \t= gpu()\n"
bsalomonb8797bb2016-04-05 08:49:38 -070090 "\tgl \t= gpu(api=gl)\n"
cdaltonc28afdb2016-03-29 20:05:07 -070091 "\tmsaa4 \t= gpu(samples=4)\n"
bsalomonb8797bb2016-04-05 08:49:38 -070092 "\tglmsaa4 \t= gpu(api=gl,samples=4)\n"
cdaltonc28afdb2016-03-29 20:05:07 -070093 "\tmsaa16 \t= gpu(samples=16)\n"
94 "\tnvpr4 \t= gpu(nvpr=true,samples=4)\n"
bsalomonb8797bb2016-04-05 08:49:38 -070095 "\tglnvpr4 \t= gpu(api=gl,nvpr=true,samples=4)\n"
cdaltonc28afdb2016-03-29 20:05:07 -070096 "\tnvpr16 \t= gpu(nvpr=true,samples=16)\n"
97 "\tnvprdit4 \t= gpu(nvpr=true,samples=4,dit=true)\n"
bsalomonb8797bb2016-04-05 08:49:38 -070098 "\tglnvprdit4\t= gpu(api=gl,nvpr=true,samples=4,dit=true)\n"
cdaltonc28afdb2016-03-29 20:05:07 -070099 "\tnvprdit16 \t= gpu(nvpr=true,samples=16,dit=true)\n"
brianosmand93c1202016-03-10 07:49:08 -0800100 "\tgpuf16 \t= gpu(color=f16)\n"
101 "\tgpusrgb \t= gpu(color=srgb)\n"
bsalomonb8797bb2016-04-05 08:49:38 -0700102 "\tglsrgb \t= gpu(api=gl,color=srgb)\n"
kkinnunen3e980c32015-12-23 01:33:00 -0800103 "\tgpudft \t= gpu(dit=true)\n"
104 "\tgpudebug \t= gpu(api=debug)\n"
105 "\tgpunull \t= gpu(api=null)\n"
106 "\tdebug \t= gpu(api=debug)\n"
107 "\tnullgpu \t= gpu(api=null)\n"
108#if SK_ANGLE
109#ifdef SK_BUILD_FOR_WIN
110 "\tangle \t= gpu(api=angle)\n"
111#endif
112 "\tangle-gl \t= gpu(api=angle-gl)\n"
113#endif
114#if SK_COMMAND_BUFFER
115 "\tcommandbuffer\t= gpu(api=commandbuffer)\n"
116#endif
117#if SK_MESA
118 "\tmesa \t= gpu(api=mesa)\n"
119#endif
120#endif
121 ;
122
123DEFINE_extended_string(config, defaultConfigs, configHelp, configExtendedHelp);
124
125static const struct {
126 const char* predefinedConfig;
127 const char* backend;
128 const char* options;
129} gPredefinedConfigs[] = {
130#if SK_SUPPORT_GPU
131 { "gpu", "gpu", "" },
bsalomonb8797bb2016-04-05 08:49:38 -0700132 { "gl", "gpu", "api=gl" },
kkinnunen3e980c32015-12-23 01:33:00 -0800133 { "msaa4", "gpu", "samples=4" },
bsalomonb8797bb2016-04-05 08:49:38 -0700134 { "glmsaa4", "gpu", "api=gl,samples=4" },
kkinnunen3e980c32015-12-23 01:33:00 -0800135 { "msaa16", "gpu", "samples=16" },
cdaltonc28afdb2016-03-29 20:05:07 -0700136 { "nvpr4", "gpu", "nvpr=true,samples=4" },
bsalomonb8797bb2016-04-05 08:49:38 -0700137 { "glnvpr4", "gpu", "api=gl,nvpr=true,samples=4" },
cdaltonc28afdb2016-03-29 20:05:07 -0700138 { "nvpr16", "gpu", "nvpr=true,samples=16" },
139 { "nvprdit4", "gpu", "nvpr=true,samples=4,dit=true" },
bsalomonb8797bb2016-04-05 08:49:38 -0700140 { "glnvprdit4", "gpu", "api=gl,nvpr=true,samples=4,dit=true" },
cdaltonc28afdb2016-03-29 20:05:07 -0700141 { "nvprdit16", "gpu", "nvpr=true,samples=16,dit=true" },
brianosmand93c1202016-03-10 07:49:08 -0800142 { "gpuf16", "gpu", "color=f16" },
143 { "gpusrgb", "gpu", "color=srgb" },
bsalomonb8797bb2016-04-05 08:49:38 -0700144 { "glsrgb", "gpu", "api=gl,color=srgb" },
kkinnunen3e980c32015-12-23 01:33:00 -0800145 { "gpudft", "gpu", "dit=true" },
146 { "gpudebug", "gpu", "api=debug" },
147 { "gpunull", "gpu", "api=null" },
148 { "debug", "gpu", "api=debug" },
149 { "nullgpu", "gpu", "api=null" }
150#if SK_ANGLE
151#ifdef SK_BUILD_FOR_WIN
152 , { "angle", "gpu", "api=angle" }
153#endif
154 , { "angle-gl", "gpu", "api=angle-gl" }
155#endif
156#if SK_COMMAND_BUFFER
157 , { "commandbuffer", "gpu", "api=commandbuffer" }
158#endif
159#if SK_MESA
160 , { "mesa", "gpu", "api=mesa" }
161#endif
162#else
163 { "", "", "" }
164#endif
165};
166
167SkCommandLineConfig::SkCommandLineConfig(const SkString& tag, const SkString& backend,
168 const SkTArray<SkString>& viaParts)
169 : fTag(tag)
170 , fBackend(backend)
171 , fViaParts(viaParts) {
172}
173SkCommandLineConfig::~SkCommandLineConfig() {
174}
175
176#if SK_SUPPORT_GPU
177SkCommandLineConfigGpu::SkCommandLineConfigGpu(
178 const SkString& tag, const SkTArray<SkString>& viaParts,
brianosmand93c1202016-03-10 07:49:08 -0800179 ContextType contextType, bool useNVPR, bool useDIText, int samples,
180 SkColorType colorType, SkColorProfileType profileType)
kkinnunen3e980c32015-12-23 01:33:00 -0800181 : SkCommandLineConfig(tag, SkString("gpu"), viaParts)
182 , fContextType(contextType)
183 , fUseNVPR(useNVPR)
184 , fUseDIText(useDIText)
brianosmand93c1202016-03-10 07:49:08 -0800185 , fSamples(samples)
186 , fColorType(colorType)
187 , fProfileType(profileType) {
kkinnunen3e980c32015-12-23 01:33:00 -0800188}
189static bool parse_option_int(const SkString& value, int* outInt) {
190 if (value.isEmpty()) {
191 return false;
192 }
193 char* endptr = nullptr;
194 long intValue = strtol(value.c_str(), &endptr, 10);
195 if (*endptr != '\0') {
196 return false;
197 }
198 *outInt = static_cast<int>(intValue);
199 return true;
200}
201static bool parse_option_bool(const SkString& value, bool* outBool) {
202 if (value.equals("true")) {
203 *outBool = true;
204 return true;
205 }
206 if (value.equals("false")) {
207 *outBool = false;
208 return true;
209 }
210 return false;
211}
212static bool parse_option_gpu_api(const SkString& value,
213 SkCommandLineConfigGpu::ContextType* outContextType) {
bsalomonb4b4cf32016-04-04 05:56:59 -0700214 if (value.equals("native")) {
215 *outContextType = GrContextFactory::kNative_GLContextType;
216 return true;
217 }
kkinnunen3e980c32015-12-23 01:33:00 -0800218 if (value.equals("gl")) {
bsalomonb4b4cf32016-04-04 05:56:59 -0700219 *outContextType = GrContextFactory::kGL_GLContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800220 return true;
221 }
222 if (value.equals("gles")) {
bsalomonb4b4cf32016-04-04 05:56:59 -0700223 *outContextType = GrContextFactory::kGLES_GLContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800224 return true;
225 }
226 if (value.equals("debug")) {
bsalomonb4b4cf32016-04-04 05:56:59 -0700227 *outContextType = GrContextFactory::kDebug_GLContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800228 return true;
229 }
230 if (value.equals("null")) {
bsalomonb4b4cf32016-04-04 05:56:59 -0700231 *outContextType = GrContextFactory::kNull_GLContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800232 return true;
233 }
234#if SK_ANGLE
235#ifdef SK_BUILD_FOR_WIN
236 if (value.equals("angle")) {
bsalomonb4b4cf32016-04-04 05:56:59 -0700237 *outContextType = GrContextFactory::kANGLE_GLContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800238 return true;
239 }
240#endif
241 if (value.equals("angle-gl")) {
bsalomonb4b4cf32016-04-04 05:56:59 -0700242 *outContextType = GrContextFactory::kANGLE_GL_GLContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800243 return true;
244 }
245#endif
246#if SK_COMMAND_BUFFER
247 if (value.equals("commandbuffer")) {
bsalomonb4b4cf32016-04-04 05:56:59 -0700248 *outContextType = GrContextFactory::kCommandBuffer_GLContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800249 return true;
250 }
251#endif
252#if SK_MESA
253 if (value.equals("mesa")) {
bsalomonb4b4cf32016-04-04 05:56:59 -0700254 *outContextType = GrContextFactory::kMESA_GLContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800255 return true;
256 }
257#endif
258 return false;
259}
brianosmand93c1202016-03-10 07:49:08 -0800260static bool parse_option_gpu_color(const SkString& value,
261 SkColorType* outColorType,
262 SkColorProfileType* outProfileType) {
263 if (value.equals("8888")) {
264 *outColorType = kN32_SkColorType;
265 *outProfileType = kLinear_SkColorProfileType;
266 return true;
267 }
268 if (value.equals("f16")) {
269 *outColorType = kRGBA_F16_SkColorType;
270 *outProfileType = kLinear_SkColorProfileType;
271 return true;
272 }
273 if (value.equals("srgb")) {
274 *outColorType = kN32_SkColorType;
275 *outProfileType = kSRGB_SkColorProfileType;
276 return true;
277 }
278 return false;
279}
kkinnunen3e980c32015-12-23 01:33:00 -0800280
281SkCommandLineConfigGpu* parse_command_line_config_gpu(const SkString& tag,
282 const SkTArray<SkString>& vias,
283 const SkString& options) {
284 // Defaults for GPU backend.
285 bool seenAPI = false;
bsalomonb4b4cf32016-04-04 05:56:59 -0700286 SkCommandLineConfigGpu::ContextType contextType = GrContextFactory::kNative_GLContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800287 bool seenUseNVPR = false;
288 bool useNVPR = false;
289 bool seenUseDIText =false;
290 bool useDIText = false;
291 bool seenSamples = false;
292 int samples = 0;
brianosmand93c1202016-03-10 07:49:08 -0800293 bool seenColor = false;
294 SkColorType colorType = kN32_SkColorType;
295 SkColorProfileType profileType = kLinear_SkColorProfileType;
kkinnunen3e980c32015-12-23 01:33:00 -0800296
297 SkTArray<SkString> optionParts;
298 SkStrSplit(options.c_str(), ",", kStrict_SkStrSplitMode, &optionParts);
299 for (int i = 0; i < optionParts.count(); ++i) {
300 SkTArray<SkString> keyValueParts;
301 SkStrSplit(optionParts[i].c_str(), "=", kStrict_SkStrSplitMode, &keyValueParts);
302 if (keyValueParts.count() != 2) {
303 return nullptr;
304 }
305 const SkString& key = keyValueParts[0];
306 const SkString& value = keyValueParts[1];
307 bool valueOk = false;
308 if (key.equals("api") && !seenAPI) {
309 valueOk = parse_option_gpu_api(value, &contextType);
310 seenAPI = true;
311 } else if (key.equals("nvpr") && !seenUseNVPR) {
312 valueOk = parse_option_bool(value, &useNVPR);
313 seenUseNVPR = true;
314 } else if (key.equals("dit") && !seenUseDIText) {
315 valueOk = parse_option_bool(value, &useDIText);
316 seenUseDIText = true;
317 } else if (key.equals("samples") && !seenSamples) {
318 valueOk = parse_option_int(value, &samples);
319 seenSamples = true;
brianosmand93c1202016-03-10 07:49:08 -0800320 } else if (key.equals("color") && !seenColor) {
321 valueOk = parse_option_gpu_color(value, &colorType, &profileType);
322 seenColor = true;
kkinnunen3e980c32015-12-23 01:33:00 -0800323 }
324 if (!valueOk) {
325 return nullptr;
326 }
327 }
brianosmand93c1202016-03-10 07:49:08 -0800328 return new SkCommandLineConfigGpu(tag, vias, contextType, useNVPR, useDIText, samples,
329 colorType, profileType);
kkinnunen3e980c32015-12-23 01:33:00 -0800330}
331#endif
332
333void ParseConfigs(const SkCommandLineFlags::StringArray& configs,
334 SkCommandLineConfigArray* outResult) {
335 outResult->reset();
336 for (int i = 0; i < configs.count(); ++i) {
337 SkString extendedBackend;
338 SkString extendedOptions;
339 SkString simpleBackend;
340 SkTArray<SkString> vias;
341
342 SkString tag(configs[i]);
343 SkTArray<SkString> parts;
344 SkStrSplit(tag.c_str(), "(", kStrict_SkStrSplitMode, &parts);
345 if (parts.count() == 2) {
346 SkTArray<SkString> parts2;
347 SkStrSplit(parts[1].c_str(), ")", kStrict_SkStrSplitMode, &parts2);
348 if (parts2.count() == 2 && parts2[1].isEmpty()) {
349 SkStrSplit(parts[0].c_str(), "-", kStrict_SkStrSplitMode, &vias);
350 if (vias.count()) {
351 extendedBackend = vias[vias.count() - 1];
352 vias.pop_back();
353 } else {
354 extendedBackend = parts[0];
355 }
356 extendedOptions = parts2[0];
357 simpleBackend.printf("%s(%s)", extendedBackend.c_str(), extendedOptions.c_str());
358 }
359 }
360
361 if (extendedBackend.isEmpty()) {
362 simpleBackend = tag;
363 SkStrSplit(tag.c_str(), "-", kStrict_SkStrSplitMode, &vias);
364 if (vias.count()) {
365 simpleBackend = vias[vias.count() - 1];
366 vias.pop_back();
367 }
368 // Note: no #if SK_ANGLE: this is a special rule in the via-tag grammar.
369 if (vias.count() && simpleBackend.equals("gl") &&
370 vias[vias.count() - 1].equals("angle")) {
371 simpleBackend = "angle-gl";
372 vias.pop_back();
373 }
374
375 for (auto& predefinedConfig : gPredefinedConfigs) {
376 if (simpleBackend.equals(predefinedConfig.predefinedConfig)) {
377 extendedBackend = predefinedConfig.backend;
378 extendedOptions = predefinedConfig.options;
379 break;
380 }
381 }
382 }
383 SkCommandLineConfig* parsedConfig = nullptr;
384#if SK_SUPPORT_GPU
385 if (extendedBackend.equals("gpu")) {
386 parsedConfig = parse_command_line_config_gpu(tag, vias, extendedOptions);
387 }
388#endif
389 if (!parsedConfig) {
390 parsedConfig = new SkCommandLineConfig(tag, simpleBackend, vias);
391 }
392 outResult->emplace_back(parsedConfig);
393 }
394}