blob: 0847ae5b79ea268947ef0bf3c8633c0db754b37d [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) {
kkinnunen3e980c32015-12-23 01:33:00 -0800214 if (value.equals("gl")) {
bsalomon85b4b532016-04-05 11:06:27 -0700215 *outContextType = GrContextFactory::kGL_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800216 return true;
217 }
218 if (value.equals("gles")) {
bsalomon85b4b532016-04-05 11:06:27 -0700219 *outContextType = GrContextFactory::kGLES_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800220 return true;
221 }
222 if (value.equals("debug")) {
bsalomon85b4b532016-04-05 11:06:27 -0700223 *outContextType = GrContextFactory::kDebugGL_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800224 return true;
225 }
226 if (value.equals("null")) {
bsalomon85b4b532016-04-05 11:06:27 -0700227 *outContextType = GrContextFactory::kNullGL_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800228 return true;
229 }
230#if SK_ANGLE
231#ifdef SK_BUILD_FOR_WIN
232 if (value.equals("angle")) {
bsalomon85b4b532016-04-05 11:06:27 -0700233 *outContextType = GrContextFactory::kANGLE_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800234 return true;
235 }
236#endif
237 if (value.equals("angle-gl")) {
bsalomon85b4b532016-04-05 11:06:27 -0700238 *outContextType = GrContextFactory::kANGLE_GL_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800239 return true;
240 }
241#endif
242#if SK_COMMAND_BUFFER
243 if (value.equals("commandbuffer")) {
bsalomon85b4b532016-04-05 11:06:27 -0700244 *outContextType = GrContextFactory::kCommandBuffer_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800245 return true;
246 }
247#endif
248#if SK_MESA
249 if (value.equals("mesa")) {
bsalomon85b4b532016-04-05 11:06:27 -0700250 *outContextType = GrContextFactory::kMESA_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800251 return true;
252 }
253#endif
254 return false;
255}
brianosmand93c1202016-03-10 07:49:08 -0800256static bool parse_option_gpu_color(const SkString& value,
257 SkColorType* outColorType,
258 SkColorProfileType* outProfileType) {
259 if (value.equals("8888")) {
260 *outColorType = kN32_SkColorType;
261 *outProfileType = kLinear_SkColorProfileType;
262 return true;
263 }
264 if (value.equals("f16")) {
265 *outColorType = kRGBA_F16_SkColorType;
266 *outProfileType = kLinear_SkColorProfileType;
267 return true;
268 }
269 if (value.equals("srgb")) {
270 *outColorType = kN32_SkColorType;
271 *outProfileType = kSRGB_SkColorProfileType;
272 return true;
273 }
274 return false;
275}
kkinnunen3e980c32015-12-23 01:33:00 -0800276
277SkCommandLineConfigGpu* parse_command_line_config_gpu(const SkString& tag,
278 const SkTArray<SkString>& vias,
279 const SkString& options) {
280 // Defaults for GPU backend.
281 bool seenAPI = false;
bsalomon85b4b532016-04-05 11:06:27 -0700282 SkCommandLineConfigGpu::ContextType contextType = GrContextFactory::kNativeGL_ContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800283 bool seenUseNVPR = false;
284 bool useNVPR = false;
285 bool seenUseDIText =false;
286 bool useDIText = false;
287 bool seenSamples = false;
288 int samples = 0;
brianosmand93c1202016-03-10 07:49:08 -0800289 bool seenColor = false;
290 SkColorType colorType = kN32_SkColorType;
291 SkColorProfileType profileType = kLinear_SkColorProfileType;
kkinnunen3e980c32015-12-23 01:33:00 -0800292
293 SkTArray<SkString> optionParts;
294 SkStrSplit(options.c_str(), ",", kStrict_SkStrSplitMode, &optionParts);
295 for (int i = 0; i < optionParts.count(); ++i) {
296 SkTArray<SkString> keyValueParts;
297 SkStrSplit(optionParts[i].c_str(), "=", kStrict_SkStrSplitMode, &keyValueParts);
298 if (keyValueParts.count() != 2) {
299 return nullptr;
300 }
301 const SkString& key = keyValueParts[0];
302 const SkString& value = keyValueParts[1];
303 bool valueOk = false;
304 if (key.equals("api") && !seenAPI) {
305 valueOk = parse_option_gpu_api(value, &contextType);
306 seenAPI = true;
307 } else if (key.equals("nvpr") && !seenUseNVPR) {
308 valueOk = parse_option_bool(value, &useNVPR);
309 seenUseNVPR = true;
310 } else if (key.equals("dit") && !seenUseDIText) {
311 valueOk = parse_option_bool(value, &useDIText);
312 seenUseDIText = true;
313 } else if (key.equals("samples") && !seenSamples) {
314 valueOk = parse_option_int(value, &samples);
315 seenSamples = true;
brianosmand93c1202016-03-10 07:49:08 -0800316 } else if (key.equals("color") && !seenColor) {
317 valueOk = parse_option_gpu_color(value, &colorType, &profileType);
318 seenColor = true;
kkinnunen3e980c32015-12-23 01:33:00 -0800319 }
320 if (!valueOk) {
321 return nullptr;
322 }
323 }
brianosmand93c1202016-03-10 07:49:08 -0800324 return new SkCommandLineConfigGpu(tag, vias, contextType, useNVPR, useDIText, samples,
325 colorType, profileType);
kkinnunen3e980c32015-12-23 01:33:00 -0800326}
327#endif
328
329void ParseConfigs(const SkCommandLineFlags::StringArray& configs,
330 SkCommandLineConfigArray* outResult) {
331 outResult->reset();
332 for (int i = 0; i < configs.count(); ++i) {
333 SkString extendedBackend;
334 SkString extendedOptions;
335 SkString simpleBackend;
336 SkTArray<SkString> vias;
337
338 SkString tag(configs[i]);
339 SkTArray<SkString> parts;
340 SkStrSplit(tag.c_str(), "(", kStrict_SkStrSplitMode, &parts);
341 if (parts.count() == 2) {
342 SkTArray<SkString> parts2;
343 SkStrSplit(parts[1].c_str(), ")", kStrict_SkStrSplitMode, &parts2);
344 if (parts2.count() == 2 && parts2[1].isEmpty()) {
345 SkStrSplit(parts[0].c_str(), "-", kStrict_SkStrSplitMode, &vias);
346 if (vias.count()) {
347 extendedBackend = vias[vias.count() - 1];
348 vias.pop_back();
349 } else {
350 extendedBackend = parts[0];
351 }
352 extendedOptions = parts2[0];
353 simpleBackend.printf("%s(%s)", extendedBackend.c_str(), extendedOptions.c_str());
354 }
355 }
356
357 if (extendedBackend.isEmpty()) {
358 simpleBackend = tag;
359 SkStrSplit(tag.c_str(), "-", kStrict_SkStrSplitMode, &vias);
360 if (vias.count()) {
361 simpleBackend = vias[vias.count() - 1];
362 vias.pop_back();
363 }
364 // Note: no #if SK_ANGLE: this is a special rule in the via-tag grammar.
365 if (vias.count() && simpleBackend.equals("gl") &&
366 vias[vias.count() - 1].equals("angle")) {
367 simpleBackend = "angle-gl";
368 vias.pop_back();
369 }
370
371 for (auto& predefinedConfig : gPredefinedConfigs) {
372 if (simpleBackend.equals(predefinedConfig.predefinedConfig)) {
373 extendedBackend = predefinedConfig.backend;
374 extendedOptions = predefinedConfig.options;
375 break;
376 }
377 }
378 }
379 SkCommandLineConfig* parsedConfig = nullptr;
380#if SK_SUPPORT_GPU
381 if (extendedBackend.equals("gpu")) {
382 parsedConfig = parse_command_line_config_gpu(tag, vias, extendedOptions);
383 }
384#endif
385 if (!parsedConfig) {
386 parsedConfig = new SkCommandLineConfig(tag, simpleBackend, vias);
387 }
388 outResult->emplace_back(parsedConfig);
389 }
390}