blob: 4d9351c15ffede3b367d5c84a6753fce8b1212d5 [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 "
brianosmand93c1202016-03-10 07:49:08 -080026 "msaa16 msaa4 gpuf16 gpusrgb nonrendering null nullgpu nvprmsaa16 nvprmsaa4 "
kkinnunen3e980c32015-12-23 01:33:00 -080027 "pdf pdf_poppler skp svg xps"
28#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"
85 "\tgpu \t= gpu()\n"
86 "\tmsaa4 \t= gpu(samples=4)\n"
87 "\tmsaa16 \t= gpu(samples=16)\n"
88 "\tnvprmsaa4\t= gpu(nvpr=true,samples=4)\n"
89 "\tnvprmsaa16\t= gpu(nvpr=true,samples=16)\n"
brianosmand93c1202016-03-10 07:49:08 -080090 "\tgpuf16 \t= gpu(color=f16)\n"
91 "\tgpusrgb \t= gpu(color=srgb)\n"
kkinnunen3e980c32015-12-23 01:33:00 -080092 "\tgpudft \t= gpu(dit=true)\n"
93 "\tgpudebug \t= gpu(api=debug)\n"
94 "\tgpunull \t= gpu(api=null)\n"
95 "\tdebug \t= gpu(api=debug)\n"
96 "\tnullgpu \t= gpu(api=null)\n"
97#if SK_ANGLE
98#ifdef SK_BUILD_FOR_WIN
99 "\tangle \t= gpu(api=angle)\n"
100#endif
101 "\tangle-gl \t= gpu(api=angle-gl)\n"
102#endif
103#if SK_COMMAND_BUFFER
104 "\tcommandbuffer\t= gpu(api=commandbuffer)\n"
105#endif
106#if SK_MESA
107 "\tmesa \t= gpu(api=mesa)\n"
108#endif
109#endif
110 ;
111
112DEFINE_extended_string(config, defaultConfigs, configHelp, configExtendedHelp);
113
114static const struct {
115 const char* predefinedConfig;
116 const char* backend;
117 const char* options;
118} gPredefinedConfigs[] = {
119#if SK_SUPPORT_GPU
120 { "gpu", "gpu", "" },
121 { "msaa4", "gpu", "samples=4" },
122 { "msaa16", "gpu", "samples=16" },
kkinnunene3c2f802015-12-29 08:57:32 -0800123 { "nvprmsaa4", "gpu", "nvpr=true,samples=4,dit=true" },
124 { "nvprmsaa16", "gpu", "nvpr=true,samples=16,dit=true" },
brianosmand93c1202016-03-10 07:49:08 -0800125 { "gpuf16", "gpu", "color=f16" },
126 { "gpusrgb", "gpu", "color=srgb" },
kkinnunen3e980c32015-12-23 01:33:00 -0800127 { "gpudft", "gpu", "dit=true" },
128 { "gpudebug", "gpu", "api=debug" },
129 { "gpunull", "gpu", "api=null" },
130 { "debug", "gpu", "api=debug" },
131 { "nullgpu", "gpu", "api=null" }
132#if SK_ANGLE
133#ifdef SK_BUILD_FOR_WIN
134 , { "angle", "gpu", "api=angle" }
135#endif
136 , { "angle-gl", "gpu", "api=angle-gl" }
137#endif
138#if SK_COMMAND_BUFFER
139 , { "commandbuffer", "gpu", "api=commandbuffer" }
140#endif
141#if SK_MESA
142 , { "mesa", "gpu", "api=mesa" }
143#endif
144#else
145 { "", "", "" }
146#endif
147};
148
149SkCommandLineConfig::SkCommandLineConfig(const SkString& tag, const SkString& backend,
150 const SkTArray<SkString>& viaParts)
151 : fTag(tag)
152 , fBackend(backend)
153 , fViaParts(viaParts) {
154}
155SkCommandLineConfig::~SkCommandLineConfig() {
156}
157
158#if SK_SUPPORT_GPU
159SkCommandLineConfigGpu::SkCommandLineConfigGpu(
160 const SkString& tag, const SkTArray<SkString>& viaParts,
brianosmand93c1202016-03-10 07:49:08 -0800161 ContextType contextType, bool useNVPR, bool useDIText, int samples,
162 SkColorType colorType, SkColorProfileType profileType)
kkinnunen3e980c32015-12-23 01:33:00 -0800163 : SkCommandLineConfig(tag, SkString("gpu"), viaParts)
164 , fContextType(contextType)
165 , fUseNVPR(useNVPR)
166 , fUseDIText(useDIText)
brianosmand93c1202016-03-10 07:49:08 -0800167 , fSamples(samples)
168 , fColorType(colorType)
169 , fProfileType(profileType) {
kkinnunen3e980c32015-12-23 01:33:00 -0800170}
171static bool parse_option_int(const SkString& value, int* outInt) {
172 if (value.isEmpty()) {
173 return false;
174 }
175 char* endptr = nullptr;
176 long intValue = strtol(value.c_str(), &endptr, 10);
177 if (*endptr != '\0') {
178 return false;
179 }
180 *outInt = static_cast<int>(intValue);
181 return true;
182}
183static bool parse_option_bool(const SkString& value, bool* outBool) {
184 if (value.equals("true")) {
185 *outBool = true;
186 return true;
187 }
188 if (value.equals("false")) {
189 *outBool = false;
190 return true;
191 }
192 return false;
193}
194static bool parse_option_gpu_api(const SkString& value,
195 SkCommandLineConfigGpu::ContextType* outContextType) {
196 if (value.equals("native")) {
197 *outContextType = GrContextFactory::kNative_GLContextType;
198 return true;
199 }
200 if (value.equals("gl")) {
201 *outContextType = GrContextFactory::kGL_GLContextType;
202 return true;
203 }
204 if (value.equals("gles")) {
205 *outContextType = GrContextFactory::kGLES_GLContextType;
206 return true;
207 }
208 if (value.equals("debug")) {
209 *outContextType = GrContextFactory::kDebug_GLContextType;
210 return true;
211 }
212 if (value.equals("null")) {
213 *outContextType = GrContextFactory::kNull_GLContextType;
214 return true;
215 }
216#if SK_ANGLE
217#ifdef SK_BUILD_FOR_WIN
218 if (value.equals("angle")) {
219 *outContextType = GrContextFactory::kANGLE_GLContextType;
220 return true;
221 }
222#endif
223 if (value.equals("angle-gl")) {
224 *outContextType = GrContextFactory::kANGLE_GL_GLContextType;
225 return true;
226 }
227#endif
228#if SK_COMMAND_BUFFER
229 if (value.equals("commandbuffer")) {
kkinnunenf655e932016-03-03 07:39:48 -0800230 *outContextType = GrContextFactory::kCommandBuffer_GLContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800231 return true;
232 }
233#endif
234#if SK_MESA
235 if (value.equals("mesa")) {
236 *outContextType = GrContextFactory::kMESA_GLContextType;
237 return true;
238 }
239#endif
240 return false;
241}
brianosmand93c1202016-03-10 07:49:08 -0800242static bool parse_option_gpu_color(const SkString& value,
243 SkColorType* outColorType,
244 SkColorProfileType* outProfileType) {
245 if (value.equals("8888")) {
246 *outColorType = kN32_SkColorType;
247 *outProfileType = kLinear_SkColorProfileType;
248 return true;
249 }
250 if (value.equals("f16")) {
251 *outColorType = kRGBA_F16_SkColorType;
252 *outProfileType = kLinear_SkColorProfileType;
253 return true;
254 }
255 if (value.equals("srgb")) {
256 *outColorType = kN32_SkColorType;
257 *outProfileType = kSRGB_SkColorProfileType;
258 return true;
259 }
260 return false;
261}
kkinnunen3e980c32015-12-23 01:33:00 -0800262
263SkCommandLineConfigGpu* parse_command_line_config_gpu(const SkString& tag,
264 const SkTArray<SkString>& vias,
265 const SkString& options) {
266 // Defaults for GPU backend.
267 bool seenAPI = false;
268 SkCommandLineConfigGpu::ContextType contextType = GrContextFactory::kNative_GLContextType;
269 bool seenUseNVPR = false;
270 bool useNVPR = false;
271 bool seenUseDIText =false;
272 bool useDIText = false;
273 bool seenSamples = false;
274 int samples = 0;
brianosmand93c1202016-03-10 07:49:08 -0800275 bool seenColor = false;
276 SkColorType colorType = kN32_SkColorType;
277 SkColorProfileType profileType = kLinear_SkColorProfileType;
kkinnunen3e980c32015-12-23 01:33:00 -0800278
279 SkTArray<SkString> optionParts;
280 SkStrSplit(options.c_str(), ",", kStrict_SkStrSplitMode, &optionParts);
281 for (int i = 0; i < optionParts.count(); ++i) {
282 SkTArray<SkString> keyValueParts;
283 SkStrSplit(optionParts[i].c_str(), "=", kStrict_SkStrSplitMode, &keyValueParts);
284 if (keyValueParts.count() != 2) {
285 return nullptr;
286 }
287 const SkString& key = keyValueParts[0];
288 const SkString& value = keyValueParts[1];
289 bool valueOk = false;
290 if (key.equals("api") && !seenAPI) {
291 valueOk = parse_option_gpu_api(value, &contextType);
292 seenAPI = true;
293 } else if (key.equals("nvpr") && !seenUseNVPR) {
294 valueOk = parse_option_bool(value, &useNVPR);
295 seenUseNVPR = true;
296 } else if (key.equals("dit") && !seenUseDIText) {
297 valueOk = parse_option_bool(value, &useDIText);
298 seenUseDIText = true;
299 } else if (key.equals("samples") && !seenSamples) {
300 valueOk = parse_option_int(value, &samples);
301 seenSamples = true;
brianosmand93c1202016-03-10 07:49:08 -0800302 } else if (key.equals("color") && !seenColor) {
303 valueOk = parse_option_gpu_color(value, &colorType, &profileType);
304 seenColor = true;
kkinnunen3e980c32015-12-23 01:33:00 -0800305 }
306 if (!valueOk) {
307 return nullptr;
308 }
309 }
brianosmand93c1202016-03-10 07:49:08 -0800310 return new SkCommandLineConfigGpu(tag, vias, contextType, useNVPR, useDIText, samples,
311 colorType, profileType);
kkinnunen3e980c32015-12-23 01:33:00 -0800312}
313#endif
314
315void ParseConfigs(const SkCommandLineFlags::StringArray& configs,
316 SkCommandLineConfigArray* outResult) {
317 outResult->reset();
318 for (int i = 0; i < configs.count(); ++i) {
319 SkString extendedBackend;
320 SkString extendedOptions;
321 SkString simpleBackend;
322 SkTArray<SkString> vias;
323
324 SkString tag(configs[i]);
325 SkTArray<SkString> parts;
326 SkStrSplit(tag.c_str(), "(", kStrict_SkStrSplitMode, &parts);
327 if (parts.count() == 2) {
328 SkTArray<SkString> parts2;
329 SkStrSplit(parts[1].c_str(), ")", kStrict_SkStrSplitMode, &parts2);
330 if (parts2.count() == 2 && parts2[1].isEmpty()) {
331 SkStrSplit(parts[0].c_str(), "-", kStrict_SkStrSplitMode, &vias);
332 if (vias.count()) {
333 extendedBackend = vias[vias.count() - 1];
334 vias.pop_back();
335 } else {
336 extendedBackend = parts[0];
337 }
338 extendedOptions = parts2[0];
339 simpleBackend.printf("%s(%s)", extendedBackend.c_str(), extendedOptions.c_str());
340 }
341 }
342
343 if (extendedBackend.isEmpty()) {
344 simpleBackend = tag;
345 SkStrSplit(tag.c_str(), "-", kStrict_SkStrSplitMode, &vias);
346 if (vias.count()) {
347 simpleBackend = vias[vias.count() - 1];
348 vias.pop_back();
349 }
350 // Note: no #if SK_ANGLE: this is a special rule in the via-tag grammar.
351 if (vias.count() && simpleBackend.equals("gl") &&
352 vias[vias.count() - 1].equals("angle")) {
353 simpleBackend = "angle-gl";
354 vias.pop_back();
355 }
356
357 for (auto& predefinedConfig : gPredefinedConfigs) {
358 if (simpleBackend.equals(predefinedConfig.predefinedConfig)) {
359 extendedBackend = predefinedConfig.backend;
360 extendedOptions = predefinedConfig.options;
361 break;
362 }
363 }
364 }
365 SkCommandLineConfig* parsedConfig = nullptr;
366#if SK_SUPPORT_GPU
367 if (extendedBackend.equals("gpu")) {
368 parsedConfig = parse_command_line_config_gpu(tag, vias, extendedOptions);
369 }
370#endif
371 if (!parsedConfig) {
372 parsedConfig = new SkCommandLineConfig(tag, simpleBackend, vias);
373 }
374 outResult->emplace_back(parsedConfig);
375 }
376}