blob: b084b1d802c0df095c30ae1550531f8165784e75 [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[] =
29 "Options: 565 8888 debug gpu gpudebug gpudft gpunull "
cdaltonc28afdb2016-03-29 20:05:07 -070030 "msaa16 msaa4 gpuf16 gpusrgb nonrendering null nullgpu "
31 "nvpr16 nvpr4 nvprdit16 nvprdit4 pdf pdf_poppler 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"
90 "\tmsaa4 \t= gpu(samples=4)\n"
91 "\tmsaa16 \t= gpu(samples=16)\n"
92 "\tnvpr4 \t= gpu(nvpr=true,samples=4)\n"
93 "\tnvpr16 \t= gpu(nvpr=true,samples=16)\n"
94 "\tnvprdit4 \t= gpu(nvpr=true,samples=4,dit=true)\n"
95 "\tnvprdit16 \t= gpu(nvpr=true,samples=16,dit=true)\n"
brianosmand93c1202016-03-10 07:49:08 -080096 "\tgpuf16 \t= gpu(color=f16)\n"
97 "\tgpusrgb \t= gpu(color=srgb)\n"
kkinnunen3e980c32015-12-23 01:33:00 -080098 "\tgpudft \t= gpu(dit=true)\n"
99 "\tgpudebug \t= gpu(api=debug)\n"
100 "\tgpunull \t= gpu(api=null)\n"
101 "\tdebug \t= gpu(api=debug)\n"
102 "\tnullgpu \t= gpu(api=null)\n"
103#if SK_ANGLE
104#ifdef SK_BUILD_FOR_WIN
105 "\tangle \t= gpu(api=angle)\n"
106#endif
107 "\tangle-gl \t= gpu(api=angle-gl)\n"
108#endif
109#if SK_COMMAND_BUFFER
110 "\tcommandbuffer\t= gpu(api=commandbuffer)\n"
111#endif
112#if SK_MESA
113 "\tmesa \t= gpu(api=mesa)\n"
114#endif
115#endif
116 ;
117
118DEFINE_extended_string(config, defaultConfigs, configHelp, configExtendedHelp);
119
120static const struct {
121 const char* predefinedConfig;
122 const char* backend;
123 const char* options;
124} gPredefinedConfigs[] = {
125#if SK_SUPPORT_GPU
126 { "gpu", "gpu", "" },
127 { "msaa4", "gpu", "samples=4" },
128 { "msaa16", "gpu", "samples=16" },
cdaltonc28afdb2016-03-29 20:05:07 -0700129 { "nvpr4", "gpu", "nvpr=true,samples=4" },
130 { "nvpr16", "gpu", "nvpr=true,samples=16" },
131 { "nvprdit4", "gpu", "nvpr=true,samples=4,dit=true" },
132 { "nvprdit16", "gpu", "nvpr=true,samples=16,dit=true" },
brianosmand93c1202016-03-10 07:49:08 -0800133 { "gpuf16", "gpu", "color=f16" },
134 { "gpusrgb", "gpu", "color=srgb" },
kkinnunen3e980c32015-12-23 01:33:00 -0800135 { "gpudft", "gpu", "dit=true" },
136 { "gpudebug", "gpu", "api=debug" },
137 { "gpunull", "gpu", "api=null" },
138 { "debug", "gpu", "api=debug" },
139 { "nullgpu", "gpu", "api=null" }
140#if SK_ANGLE
141#ifdef SK_BUILD_FOR_WIN
142 , { "angle", "gpu", "api=angle" }
143#endif
144 , { "angle-gl", "gpu", "api=angle-gl" }
145#endif
146#if SK_COMMAND_BUFFER
147 , { "commandbuffer", "gpu", "api=commandbuffer" }
148#endif
149#if SK_MESA
150 , { "mesa", "gpu", "api=mesa" }
151#endif
152#else
153 { "", "", "" }
154#endif
155};
156
157SkCommandLineConfig::SkCommandLineConfig(const SkString& tag, const SkString& backend,
158 const SkTArray<SkString>& viaParts)
159 : fTag(tag)
160 , fBackend(backend)
161 , fViaParts(viaParts) {
162}
163SkCommandLineConfig::~SkCommandLineConfig() {
164}
165
166#if SK_SUPPORT_GPU
167SkCommandLineConfigGpu::SkCommandLineConfigGpu(
168 const SkString& tag, const SkTArray<SkString>& viaParts,
brianosmand93c1202016-03-10 07:49:08 -0800169 ContextType contextType, bool useNVPR, bool useDIText, int samples,
170 SkColorType colorType, SkColorProfileType profileType)
kkinnunen3e980c32015-12-23 01:33:00 -0800171 : SkCommandLineConfig(tag, SkString("gpu"), viaParts)
172 , fContextType(contextType)
173 , fUseNVPR(useNVPR)
174 , fUseDIText(useDIText)
brianosmand93c1202016-03-10 07:49:08 -0800175 , fSamples(samples)
176 , fColorType(colorType)
177 , fProfileType(profileType) {
kkinnunen3e980c32015-12-23 01:33:00 -0800178}
179static bool parse_option_int(const SkString& value, int* outInt) {
180 if (value.isEmpty()) {
181 return false;
182 }
183 char* endptr = nullptr;
184 long intValue = strtol(value.c_str(), &endptr, 10);
185 if (*endptr != '\0') {
186 return false;
187 }
188 *outInt = static_cast<int>(intValue);
189 return true;
190}
191static bool parse_option_bool(const SkString& value, bool* outBool) {
192 if (value.equals("true")) {
193 *outBool = true;
194 return true;
195 }
196 if (value.equals("false")) {
197 *outBool = false;
198 return true;
199 }
200 return false;
201}
202static bool parse_option_gpu_api(const SkString& value,
203 SkCommandLineConfigGpu::ContextType* outContextType) {
kkinnunen3e980c32015-12-23 01:33:00 -0800204 if (value.equals("gl")) {
bsalomon2d9c6f82016-04-01 13:38:04 -0700205 *outContextType = GrContextFactory::kGL_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800206 return true;
207 }
208 if (value.equals("gles")) {
bsalomon2d9c6f82016-04-01 13:38:04 -0700209 *outContextType = GrContextFactory::kGLES_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800210 return true;
211 }
212 if (value.equals("debug")) {
bsalomon2d9c6f82016-04-01 13:38:04 -0700213 *outContextType = GrContextFactory::kDebugGL_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800214 return true;
215 }
216 if (value.equals("null")) {
bsalomon2d9c6f82016-04-01 13:38:04 -0700217 *outContextType = GrContextFactory::kNullGL_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800218 return true;
219 }
220#if SK_ANGLE
221#ifdef SK_BUILD_FOR_WIN
222 if (value.equals("angle")) {
bsalomon2d9c6f82016-04-01 13:38:04 -0700223 *outContextType = GrContextFactory::kANGLE_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800224 return true;
225 }
226#endif
227 if (value.equals("angle-gl")) {
bsalomon2d9c6f82016-04-01 13:38:04 -0700228 *outContextType = GrContextFactory::kANGLE_GL_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800229 return true;
230 }
231#endif
232#if SK_COMMAND_BUFFER
233 if (value.equals("commandbuffer")) {
bsalomon2d9c6f82016-04-01 13:38:04 -0700234 *outContextType = GrContextFactory::kCommandBuffer_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800235 return true;
236 }
237#endif
238#if SK_MESA
239 if (value.equals("mesa")) {
bsalomon2d9c6f82016-04-01 13:38:04 -0700240 *outContextType = GrContextFactory::kMESA_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800241 return true;
242 }
243#endif
244 return false;
245}
brianosmand93c1202016-03-10 07:49:08 -0800246static bool parse_option_gpu_color(const SkString& value,
247 SkColorType* outColorType,
248 SkColorProfileType* outProfileType) {
249 if (value.equals("8888")) {
250 *outColorType = kN32_SkColorType;
251 *outProfileType = kLinear_SkColorProfileType;
252 return true;
253 }
254 if (value.equals("f16")) {
255 *outColorType = kRGBA_F16_SkColorType;
256 *outProfileType = kLinear_SkColorProfileType;
257 return true;
258 }
259 if (value.equals("srgb")) {
260 *outColorType = kN32_SkColorType;
261 *outProfileType = kSRGB_SkColorProfileType;
262 return true;
263 }
264 return false;
265}
kkinnunen3e980c32015-12-23 01:33:00 -0800266
267SkCommandLineConfigGpu* parse_command_line_config_gpu(const SkString& tag,
268 const SkTArray<SkString>& vias,
269 const SkString& options) {
270 // Defaults for GPU backend.
271 bool seenAPI = false;
bsalomon2d9c6f82016-04-01 13:38:04 -0700272 SkCommandLineConfigGpu::ContextType contextType = GrContextFactory::kNativeGL_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800273 bool seenUseNVPR = false;
274 bool useNVPR = false;
275 bool seenUseDIText =false;
276 bool useDIText = false;
277 bool seenSamples = false;
278 int samples = 0;
brianosmand93c1202016-03-10 07:49:08 -0800279 bool seenColor = false;
280 SkColorType colorType = kN32_SkColorType;
281 SkColorProfileType profileType = kLinear_SkColorProfileType;
kkinnunen3e980c32015-12-23 01:33:00 -0800282
283 SkTArray<SkString> optionParts;
284 SkStrSplit(options.c_str(), ",", kStrict_SkStrSplitMode, &optionParts);
285 for (int i = 0; i < optionParts.count(); ++i) {
286 SkTArray<SkString> keyValueParts;
287 SkStrSplit(optionParts[i].c_str(), "=", kStrict_SkStrSplitMode, &keyValueParts);
288 if (keyValueParts.count() != 2) {
289 return nullptr;
290 }
291 const SkString& key = keyValueParts[0];
292 const SkString& value = keyValueParts[1];
293 bool valueOk = false;
294 if (key.equals("api") && !seenAPI) {
295 valueOk = parse_option_gpu_api(value, &contextType);
296 seenAPI = true;
297 } else if (key.equals("nvpr") && !seenUseNVPR) {
298 valueOk = parse_option_bool(value, &useNVPR);
299 seenUseNVPR = true;
300 } else if (key.equals("dit") && !seenUseDIText) {
301 valueOk = parse_option_bool(value, &useDIText);
302 seenUseDIText = true;
303 } else if (key.equals("samples") && !seenSamples) {
304 valueOk = parse_option_int(value, &samples);
305 seenSamples = true;
brianosmand93c1202016-03-10 07:49:08 -0800306 } else if (key.equals("color") && !seenColor) {
307 valueOk = parse_option_gpu_color(value, &colorType, &profileType);
308 seenColor = true;
kkinnunen3e980c32015-12-23 01:33:00 -0800309 }
310 if (!valueOk) {
311 return nullptr;
312 }
313 }
brianosmand93c1202016-03-10 07:49:08 -0800314 return new SkCommandLineConfigGpu(tag, vias, contextType, useNVPR, useDIText, samples,
315 colorType, profileType);
kkinnunen3e980c32015-12-23 01:33:00 -0800316}
317#endif
318
319void ParseConfigs(const SkCommandLineFlags::StringArray& configs,
320 SkCommandLineConfigArray* outResult) {
321 outResult->reset();
322 for (int i = 0; i < configs.count(); ++i) {
323 SkString extendedBackend;
324 SkString extendedOptions;
325 SkString simpleBackend;
326 SkTArray<SkString> vias;
327
328 SkString tag(configs[i]);
329 SkTArray<SkString> parts;
330 SkStrSplit(tag.c_str(), "(", kStrict_SkStrSplitMode, &parts);
331 if (parts.count() == 2) {
332 SkTArray<SkString> parts2;
333 SkStrSplit(parts[1].c_str(), ")", kStrict_SkStrSplitMode, &parts2);
334 if (parts2.count() == 2 && parts2[1].isEmpty()) {
335 SkStrSplit(parts[0].c_str(), "-", kStrict_SkStrSplitMode, &vias);
336 if (vias.count()) {
337 extendedBackend = vias[vias.count() - 1];
338 vias.pop_back();
339 } else {
340 extendedBackend = parts[0];
341 }
342 extendedOptions = parts2[0];
343 simpleBackend.printf("%s(%s)", extendedBackend.c_str(), extendedOptions.c_str());
344 }
345 }
346
347 if (extendedBackend.isEmpty()) {
348 simpleBackend = tag;
349 SkStrSplit(tag.c_str(), "-", kStrict_SkStrSplitMode, &vias);
350 if (vias.count()) {
351 simpleBackend = vias[vias.count() - 1];
352 vias.pop_back();
353 }
354 // Note: no #if SK_ANGLE: this is a special rule in the via-tag grammar.
355 if (vias.count() && simpleBackend.equals("gl") &&
356 vias[vias.count() - 1].equals("angle")) {
357 simpleBackend = "angle-gl";
358 vias.pop_back();
359 }
360
361 for (auto& predefinedConfig : gPredefinedConfigs) {
362 if (simpleBackend.equals(predefinedConfig.predefinedConfig)) {
363 extendedBackend = predefinedConfig.backend;
364 extendedOptions = predefinedConfig.options;
365 break;
366 }
367 }
368 }
369 SkCommandLineConfig* parsedConfig = nullptr;
370#if SK_SUPPORT_GPU
371 if (extendedBackend.equals("gpu")) {
372 parsedConfig = parse_command_line_config_gpu(tag, vias, extendedOptions);
373 }
374#endif
375 if (!parsedConfig) {
376 parsedConfig = new SkCommandLineConfig(tag, simpleBackend, vias);
377 }
378 outResult->emplace_back(parsedConfig);
379 }
380}