blob: 9e0fd60b39d91503d620d9de95144c4a4c5f4ac7 [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
12static const char defaultConfigs[] =
13 "565 8888 gpu nonrendering"
14#if SK_ANGLE
15#ifdef SK_BUILD_FOR_WIN
16 " angle"
17#endif
18#endif
19#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
20 " hwui"
21#endif
22 ;
23
24static const char configHelp[] =
25 "Options: 565 8888 debug gpu gpudebug gpudft gpunull "
cdaltonc28afdb2016-03-29 20:05:07 -070026 "msaa16 msaa4 gpuf16 gpusrgb nonrendering null nullgpu "
27 "nvpr16 nvpr4 nvprdit16 nvprdit4 pdf pdf_poppler skp svg xps"
kkinnunen3e980c32015-12-23 01:33:00 -080028#if SK_ANGLE
29#ifdef SK_BUILD_FOR_WIN
brianosman744898a2016-03-01 13:44:28 -080030 " angle"
kkinnunen3e980c32015-12-23 01:33:00 -080031#endif
32 " angle-gl"
33#endif
34#if SK_COMMAND_BUFFER
35 " commandbuffer"
36#endif
37#if SK_MESA
38 " mesa"
39#endif
40#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
41 " hwui"
42#endif
43 " or use extended form 'backend(option=value,...)'.\n";
44
45static const char configExtendedHelp[] =
46 "Extended form: 'backend(option=value,...)'\n\n"
47 "Possible backends and options:\n"
48#if SK_SUPPORT_GPU
49 "\n"
brianosmand93c1202016-03-10 07:49:08 -080050 "gpu(api=string,color=string,dit=bool,nvpr=bool,samples=int)\tGPU backend\n"
kkinnunen3e980c32015-12-23 01:33:00 -080051 "\tapi\ttype: string\tdefault: native.\n"
52 "\t Select graphics API to use with gpu backend.\n"
53 "\t Options:\n"
54 "\t\tnative\t\t\tUse platform default OpenGL or OpenGL ES backend.\n"
55 "\t\tgl \t\t\tUse OpenGL.\n"
56 "\t\tgles \t\t\tUse OpenGL ES.\n"
57 "\t\tdebug \t\t\tUse debug OpenGL.\n"
58 "\t\tnull \t\t\tUse null OpenGL.\n"
59#if SK_ANGLE
60#ifdef SK_BUILD_FOR_WIN
61 "\t\tangle\t\t\tUse ANGLE DirectX.\n"
62#endif
63 "\t\tangle-gl\t\t\tUse ANGLE OpenGL.\n"
64#endif
65#if SK_COMMAND_BUFFER
66 "\t\tcommandbuffer\t\tUse command buffer.\n"
67#endif
68#if SK_MESA
69 "\t\tmesa\t\t\tUse MESA.\n"
70#endif
brianosmand93c1202016-03-10 07:49:08 -080071 "\tcolor\ttype: string\tdefault: 8888.\n"
72 "\t Select framebuffer color format.\n"
73 "\t Options:\n"
74 "\t\t8888\t\t\tLinear 8888.\n"
75 "\t\tf16 \t\t\tLinear 16-bit floating point.\n"
76 "\t\tsrgb\t\t\tsRGB 8888.\n"
kkinnunen3e980c32015-12-23 01:33:00 -080077 "\tdit\ttype: bool\tdefault: false.\n"
78 "\t Use device independent text.\n"
79 "\tnvpr\ttype: bool\tdefault: false.\n"
80 "\t Use NV_path_rendering OpenGL and OpenGL ES extension.\n"
81 "\tsamples\ttype: int\tdefault: 0.\n"
82 "\t Use multisampling with N samples.\n"
83 "\n"
84 "Predefined configs:\n\n"
cdaltonc28afdb2016-03-29 20:05:07 -070085 "\tgpu \t= gpu()\n"
86 "\tmsaa4 \t= gpu(samples=4)\n"
87 "\tmsaa16 \t= gpu(samples=16)\n"
88 "\tnvpr4 \t= gpu(nvpr=true,samples=4)\n"
89 "\tnvpr16 \t= gpu(nvpr=true,samples=16)\n"
90 "\tnvprdit4 \t= gpu(nvpr=true,samples=4,dit=true)\n"
91 "\tnvprdit16 \t= gpu(nvpr=true,samples=16,dit=true)\n"
brianosmand93c1202016-03-10 07:49:08 -080092 "\tgpuf16 \t= gpu(color=f16)\n"
93 "\tgpusrgb \t= gpu(color=srgb)\n"
kkinnunen3e980c32015-12-23 01:33:00 -080094 "\tgpudft \t= gpu(dit=true)\n"
95 "\tgpudebug \t= gpu(api=debug)\n"
96 "\tgpunull \t= gpu(api=null)\n"
97 "\tdebug \t= gpu(api=debug)\n"
98 "\tnullgpu \t= gpu(api=null)\n"
99#if SK_ANGLE
100#ifdef SK_BUILD_FOR_WIN
101 "\tangle \t= gpu(api=angle)\n"
102#endif
103 "\tangle-gl \t= gpu(api=angle-gl)\n"
104#endif
105#if SK_COMMAND_BUFFER
106 "\tcommandbuffer\t= gpu(api=commandbuffer)\n"
107#endif
108#if SK_MESA
109 "\tmesa \t= gpu(api=mesa)\n"
110#endif
111#endif
112 ;
113
114DEFINE_extended_string(config, defaultConfigs, configHelp, configExtendedHelp);
115
116static const struct {
117 const char* predefinedConfig;
118 const char* backend;
119 const char* options;
120} gPredefinedConfigs[] = {
121#if SK_SUPPORT_GPU
122 { "gpu", "gpu", "" },
123 { "msaa4", "gpu", "samples=4" },
124 { "msaa16", "gpu", "samples=16" },
cdaltonc28afdb2016-03-29 20:05:07 -0700125 { "nvpr4", "gpu", "nvpr=true,samples=4" },
126 { "nvpr16", "gpu", "nvpr=true,samples=16" },
127 { "nvprdit4", "gpu", "nvpr=true,samples=4,dit=true" },
128 { "nvprdit16", "gpu", "nvpr=true,samples=16,dit=true" },
brianosmand93c1202016-03-10 07:49:08 -0800129 { "gpuf16", "gpu", "color=f16" },
130 { "gpusrgb", "gpu", "color=srgb" },
kkinnunen3e980c32015-12-23 01:33:00 -0800131 { "gpudft", "gpu", "dit=true" },
132 { "gpudebug", "gpu", "api=debug" },
133 { "gpunull", "gpu", "api=null" },
134 { "debug", "gpu", "api=debug" },
135 { "nullgpu", "gpu", "api=null" }
136#if SK_ANGLE
137#ifdef SK_BUILD_FOR_WIN
138 , { "angle", "gpu", "api=angle" }
139#endif
140 , { "angle-gl", "gpu", "api=angle-gl" }
141#endif
142#if SK_COMMAND_BUFFER
143 , { "commandbuffer", "gpu", "api=commandbuffer" }
144#endif
145#if SK_MESA
146 , { "mesa", "gpu", "api=mesa" }
147#endif
148#else
149 { "", "", "" }
150#endif
151};
152
153SkCommandLineConfig::SkCommandLineConfig(const SkString& tag, const SkString& backend,
154 const SkTArray<SkString>& viaParts)
155 : fTag(tag)
156 , fBackend(backend)
157 , fViaParts(viaParts) {
158}
159SkCommandLineConfig::~SkCommandLineConfig() {
160}
161
162#if SK_SUPPORT_GPU
163SkCommandLineConfigGpu::SkCommandLineConfigGpu(
164 const SkString& tag, const SkTArray<SkString>& viaParts,
brianosmand93c1202016-03-10 07:49:08 -0800165 ContextType contextType, bool useNVPR, bool useDIText, int samples,
166 SkColorType colorType, SkColorProfileType profileType)
kkinnunen3e980c32015-12-23 01:33:00 -0800167 : SkCommandLineConfig(tag, SkString("gpu"), viaParts)
168 , fContextType(contextType)
169 , fUseNVPR(useNVPR)
170 , fUseDIText(useDIText)
brianosmand93c1202016-03-10 07:49:08 -0800171 , fSamples(samples)
172 , fColorType(colorType)
173 , fProfileType(profileType) {
kkinnunen3e980c32015-12-23 01:33:00 -0800174}
175static bool parse_option_int(const SkString& value, int* outInt) {
176 if (value.isEmpty()) {
177 return false;
178 }
179 char* endptr = nullptr;
180 long intValue = strtol(value.c_str(), &endptr, 10);
181 if (*endptr != '\0') {
182 return false;
183 }
184 *outInt = static_cast<int>(intValue);
185 return true;
186}
187static bool parse_option_bool(const SkString& value, bool* outBool) {
188 if (value.equals("true")) {
189 *outBool = true;
190 return true;
191 }
192 if (value.equals("false")) {
193 *outBool = false;
194 return true;
195 }
196 return false;
197}
198static bool parse_option_gpu_api(const SkString& value,
199 SkCommandLineConfigGpu::ContextType* outContextType) {
200 if (value.equals("native")) {
201 *outContextType = GrContextFactory::kNative_GLContextType;
202 return true;
203 }
204 if (value.equals("gl")) {
205 *outContextType = GrContextFactory::kGL_GLContextType;
206 return true;
207 }
208 if (value.equals("gles")) {
209 *outContextType = GrContextFactory::kGLES_GLContextType;
210 return true;
211 }
212 if (value.equals("debug")) {
213 *outContextType = GrContextFactory::kDebug_GLContextType;
214 return true;
215 }
216 if (value.equals("null")) {
217 *outContextType = GrContextFactory::kNull_GLContextType;
218 return true;
219 }
220#if SK_ANGLE
221#ifdef SK_BUILD_FOR_WIN
222 if (value.equals("angle")) {
223 *outContextType = GrContextFactory::kANGLE_GLContextType;
224 return true;
225 }
226#endif
227 if (value.equals("angle-gl")) {
228 *outContextType = GrContextFactory::kANGLE_GL_GLContextType;
229 return true;
230 }
231#endif
232#if SK_COMMAND_BUFFER
233 if (value.equals("commandbuffer")) {
kkinnunenf655e932016-03-03 07:39:48 -0800234 *outContextType = GrContextFactory::kCommandBuffer_GLContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800235 return true;
236 }
237#endif
238#if SK_MESA
239 if (value.equals("mesa")) {
240 *outContextType = GrContextFactory::kMESA_GLContextType;
241 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;
272 SkCommandLineConfigGpu::ContextType contextType = GrContextFactory::kNative_GLContextType;
273 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}