blob: 1ee3130bd7c766e60552cff88a2fe9aae741b7d7 [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) {
204 if (value.equals("native")) {
205 *outContextType = GrContextFactory::kNative_GLContextType;
206 return true;
207 }
208 if (value.equals("gl")) {
209 *outContextType = GrContextFactory::kGL_GLContextType;
210 return true;
211 }
212 if (value.equals("gles")) {
213 *outContextType = GrContextFactory::kGLES_GLContextType;
214 return true;
215 }
216 if (value.equals("debug")) {
217 *outContextType = GrContextFactory::kDebug_GLContextType;
218 return true;
219 }
220 if (value.equals("null")) {
221 *outContextType = GrContextFactory::kNull_GLContextType;
222 return true;
223 }
224#if SK_ANGLE
225#ifdef SK_BUILD_FOR_WIN
226 if (value.equals("angle")) {
227 *outContextType = GrContextFactory::kANGLE_GLContextType;
228 return true;
229 }
230#endif
231 if (value.equals("angle-gl")) {
232 *outContextType = GrContextFactory::kANGLE_GL_GLContextType;
233 return true;
234 }
235#endif
236#if SK_COMMAND_BUFFER
237 if (value.equals("commandbuffer")) {
kkinnunenf655e932016-03-03 07:39:48 -0800238 *outContextType = GrContextFactory::kCommandBuffer_GLContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800239 return true;
240 }
241#endif
242#if SK_MESA
243 if (value.equals("mesa")) {
244 *outContextType = GrContextFactory::kMESA_GLContextType;
245 return true;
246 }
247#endif
248 return false;
249}
brianosmand93c1202016-03-10 07:49:08 -0800250static bool parse_option_gpu_color(const SkString& value,
251 SkColorType* outColorType,
252 SkColorProfileType* outProfileType) {
253 if (value.equals("8888")) {
254 *outColorType = kN32_SkColorType;
255 *outProfileType = kLinear_SkColorProfileType;
256 return true;
257 }
258 if (value.equals("f16")) {
259 *outColorType = kRGBA_F16_SkColorType;
260 *outProfileType = kLinear_SkColorProfileType;
261 return true;
262 }
263 if (value.equals("srgb")) {
264 *outColorType = kN32_SkColorType;
265 *outProfileType = kSRGB_SkColorProfileType;
266 return true;
267 }
268 return false;
269}
kkinnunen3e980c32015-12-23 01:33:00 -0800270
271SkCommandLineConfigGpu* parse_command_line_config_gpu(const SkString& tag,
272 const SkTArray<SkString>& vias,
273 const SkString& options) {
274 // Defaults for GPU backend.
275 bool seenAPI = false;
276 SkCommandLineConfigGpu::ContextType contextType = GrContextFactory::kNative_GLContextType;
277 bool seenUseNVPR = false;
278 bool useNVPR = false;
279 bool seenUseDIText =false;
280 bool useDIText = false;
281 bool seenSamples = false;
282 int samples = 0;
brianosmand93c1202016-03-10 07:49:08 -0800283 bool seenColor = false;
284 SkColorType colorType = kN32_SkColorType;
285 SkColorProfileType profileType = kLinear_SkColorProfileType;
kkinnunen3e980c32015-12-23 01:33:00 -0800286
287 SkTArray<SkString> optionParts;
288 SkStrSplit(options.c_str(), ",", kStrict_SkStrSplitMode, &optionParts);
289 for (int i = 0; i < optionParts.count(); ++i) {
290 SkTArray<SkString> keyValueParts;
291 SkStrSplit(optionParts[i].c_str(), "=", kStrict_SkStrSplitMode, &keyValueParts);
292 if (keyValueParts.count() != 2) {
293 return nullptr;
294 }
295 const SkString& key = keyValueParts[0];
296 const SkString& value = keyValueParts[1];
297 bool valueOk = false;
298 if (key.equals("api") && !seenAPI) {
299 valueOk = parse_option_gpu_api(value, &contextType);
300 seenAPI = true;
301 } else if (key.equals("nvpr") && !seenUseNVPR) {
302 valueOk = parse_option_bool(value, &useNVPR);
303 seenUseNVPR = true;
304 } else if (key.equals("dit") && !seenUseDIText) {
305 valueOk = parse_option_bool(value, &useDIText);
306 seenUseDIText = true;
307 } else if (key.equals("samples") && !seenSamples) {
308 valueOk = parse_option_int(value, &samples);
309 seenSamples = true;
brianosmand93c1202016-03-10 07:49:08 -0800310 } else if (key.equals("color") && !seenColor) {
311 valueOk = parse_option_gpu_color(value, &colorType, &profileType);
312 seenColor = true;
kkinnunen3e980c32015-12-23 01:33:00 -0800313 }
314 if (!valueOk) {
315 return nullptr;
316 }
317 }
brianosmand93c1202016-03-10 07:49:08 -0800318 return new SkCommandLineConfigGpu(tag, vias, contextType, useNVPR, useDIText, samples,
319 colorType, profileType);
kkinnunen3e980c32015-12-23 01:33:00 -0800320}
321#endif
322
323void ParseConfigs(const SkCommandLineFlags::StringArray& configs,
324 SkCommandLineConfigArray* outResult) {
325 outResult->reset();
326 for (int i = 0; i < configs.count(); ++i) {
327 SkString extendedBackend;
328 SkString extendedOptions;
329 SkString simpleBackend;
330 SkTArray<SkString> vias;
331
332 SkString tag(configs[i]);
333 SkTArray<SkString> parts;
334 SkStrSplit(tag.c_str(), "(", kStrict_SkStrSplitMode, &parts);
335 if (parts.count() == 2) {
336 SkTArray<SkString> parts2;
337 SkStrSplit(parts[1].c_str(), ")", kStrict_SkStrSplitMode, &parts2);
338 if (parts2.count() == 2 && parts2[1].isEmpty()) {
339 SkStrSplit(parts[0].c_str(), "-", kStrict_SkStrSplitMode, &vias);
340 if (vias.count()) {
341 extendedBackend = vias[vias.count() - 1];
342 vias.pop_back();
343 } else {
344 extendedBackend = parts[0];
345 }
346 extendedOptions = parts2[0];
347 simpleBackend.printf("%s(%s)", extendedBackend.c_str(), extendedOptions.c_str());
348 }
349 }
350
351 if (extendedBackend.isEmpty()) {
352 simpleBackend = tag;
353 SkStrSplit(tag.c_str(), "-", kStrict_SkStrSplitMode, &vias);
354 if (vias.count()) {
355 simpleBackend = vias[vias.count() - 1];
356 vias.pop_back();
357 }
358 // Note: no #if SK_ANGLE: this is a special rule in the via-tag grammar.
359 if (vias.count() && simpleBackend.equals("gl") &&
360 vias[vias.count() - 1].equals("angle")) {
361 simpleBackend = "angle-gl";
362 vias.pop_back();
363 }
364
365 for (auto& predefinedConfig : gPredefinedConfigs) {
366 if (simpleBackend.equals(predefinedConfig.predefinedConfig)) {
367 extendedBackend = predefinedConfig.backend;
368 extendedOptions = predefinedConfig.options;
369 break;
370 }
371 }
372 }
373 SkCommandLineConfig* parsedConfig = nullptr;
374#if SK_SUPPORT_GPU
375 if (extendedBackend.equals("gpu")) {
376 parsedConfig = parse_command_line_config_gpu(tag, vias, extendedOptions);
377 }
378#endif
379 if (!parsedConfig) {
380 parsedConfig = new SkCommandLineConfig(tag, simpleBackend, vias);
381 }
382 outResult->emplace_back(parsedConfig);
383 }
384}