blob: 983a5018f0a376233a489757fc0067bef8a8c1d7 [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 "
brianosmana6f58192016-03-01 12:53:06 -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
brianosmana6f58192016-03-01 12:53:06 -080030 " angle anglesrgb"
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"
brianosmana6f58192016-03-01 12:53:06 -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"
kkinnunen45c2c812016-02-25 02:03:43 -080067 "\t\tcommandbuffer3\t\tUse command buffer ES 3.0 (experimental).\n"
kkinnunen3e980c32015-12-23 01:33:00 -080068#endif
69#if SK_MESA
70 "\t\tmesa\t\t\tUse MESA.\n"
71#endif
brianosmana6f58192016-03-01 12:53:06 -080072 "\tcolor\ttype: string\tdefault: 8888.\n"
73 "\t Select framebuffer color format.\n"
74 "\t Options:\n"
75 "\t\t8888\t\t\tLinear 8888.\n"
76 "\t\tf16 \t\t\tLinear 16-bit floating point.\n"
77 "\t\tsrgb\t\t\tsRGB 8888.\n"
kkinnunen3e980c32015-12-23 01:33:00 -080078 "\tdit\ttype: bool\tdefault: false.\n"
79 "\t Use device independent text.\n"
80 "\tnvpr\ttype: bool\tdefault: false.\n"
81 "\t Use NV_path_rendering OpenGL and OpenGL ES extension.\n"
82 "\tsamples\ttype: int\tdefault: 0.\n"
83 "\t Use multisampling with N samples.\n"
84 "\n"
85 "Predefined configs:\n\n"
86 "\tgpu \t= gpu()\n"
87 "\tmsaa4 \t= gpu(samples=4)\n"
88 "\tmsaa16 \t= gpu(samples=16)\n"
89 "\tnvprmsaa4\t= gpu(nvpr=true,samples=4)\n"
90 "\tnvprmsaa16\t= gpu(nvpr=true,samples=16)\n"
brianosmana6f58192016-03-01 12:53:06 -080091 "\tgpuf16 \t= gpu(color=f16)\n"
92 "\tgpusrgb \t= gpu(color=srgb)\n"
kkinnunen3e980c32015-12-23 01:33:00 -080093 "\tgpudft \t= gpu(dit=true)\n"
94 "\tgpudebug \t= gpu(api=debug)\n"
95 "\tgpunull \t= gpu(api=null)\n"
96 "\tdebug \t= gpu(api=debug)\n"
97 "\tnullgpu \t= gpu(api=null)\n"
98#if SK_ANGLE
99#ifdef SK_BUILD_FOR_WIN
100 "\tangle \t= gpu(api=angle)\n"
brianosmana6f58192016-03-01 12:53:06 -0800101 "\tanglesrgb \t= gpu(api=angle,color=srgb)\n"
kkinnunen3e980c32015-12-23 01:33:00 -0800102#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" },
kkinnunene3c2f802015-12-29 08:57:32 -0800125 { "nvprmsaa4", "gpu", "nvpr=true,samples=4,dit=true" },
126 { "nvprmsaa16", "gpu", "nvpr=true,samples=16,dit=true" },
brianosmana6f58192016-03-01 12:53:06 -0800127 { "gpuf16", "gpu", "color=f16" },
128 { "gpusrgb", "gpu", "color=srgb" },
kkinnunen3e980c32015-12-23 01:33:00 -0800129 { "gpudft", "gpu", "dit=true" },
130 { "gpudebug", "gpu", "api=debug" },
131 { "gpunull", "gpu", "api=null" },
132 { "debug", "gpu", "api=debug" },
133 { "nullgpu", "gpu", "api=null" }
134#if SK_ANGLE
135#ifdef SK_BUILD_FOR_WIN
136 , { "angle", "gpu", "api=angle" }
brianosmana6f58192016-03-01 12:53:06 -0800137 , { "anglesrgb", "gpu", "api=angle,color=srgb" }
kkinnunen3e980c32015-12-23 01:33:00 -0800138#endif
139 , { "angle-gl", "gpu", "api=angle-gl" }
140#endif
141#if SK_COMMAND_BUFFER
142 , { "commandbuffer", "gpu", "api=commandbuffer" }
143#endif
144#if SK_MESA
145 , { "mesa", "gpu", "api=mesa" }
146#endif
147#else
148 { "", "", "" }
149#endif
150};
151
152SkCommandLineConfig::SkCommandLineConfig(const SkString& tag, const SkString& backend,
153 const SkTArray<SkString>& viaParts)
154 : fTag(tag)
155 , fBackend(backend)
156 , fViaParts(viaParts) {
157}
158SkCommandLineConfig::~SkCommandLineConfig() {
159}
160
161#if SK_SUPPORT_GPU
162SkCommandLineConfigGpu::SkCommandLineConfigGpu(
163 const SkString& tag, const SkTArray<SkString>& viaParts,
brianosmana6f58192016-03-01 12:53:06 -0800164 ContextType contextType, bool useNVPR, bool useDIText, int samples,
165 SkColorType colorType, SkColorProfileType profileType)
kkinnunen3e980c32015-12-23 01:33:00 -0800166 : SkCommandLineConfig(tag, SkString("gpu"), viaParts)
167 , fContextType(contextType)
168 , fUseNVPR(useNVPR)
169 , fUseDIText(useDIText)
brianosmana6f58192016-03-01 12:53:06 -0800170 , fSamples(samples)
171 , fColorType(colorType)
172 , fProfileType(profileType) {
kkinnunen3e980c32015-12-23 01:33:00 -0800173}
174static bool parse_option_int(const SkString& value, int* outInt) {
175 if (value.isEmpty()) {
176 return false;
177 }
178 char* endptr = nullptr;
179 long intValue = strtol(value.c_str(), &endptr, 10);
180 if (*endptr != '\0') {
181 return false;
182 }
183 *outInt = static_cast<int>(intValue);
184 return true;
185}
186static bool parse_option_bool(const SkString& value, bool* outBool) {
187 if (value.equals("true")) {
188 *outBool = true;
189 return true;
190 }
191 if (value.equals("false")) {
192 *outBool = false;
193 return true;
194 }
195 return false;
196}
197static bool parse_option_gpu_api(const SkString& value,
198 SkCommandLineConfigGpu::ContextType* outContextType) {
199 if (value.equals("native")) {
200 *outContextType = GrContextFactory::kNative_GLContextType;
201 return true;
202 }
203 if (value.equals("gl")) {
204 *outContextType = GrContextFactory::kGL_GLContextType;
205 return true;
206 }
207 if (value.equals("gles")) {
208 *outContextType = GrContextFactory::kGLES_GLContextType;
209 return true;
210 }
211 if (value.equals("debug")) {
212 *outContextType = GrContextFactory::kDebug_GLContextType;
213 return true;
214 }
215 if (value.equals("null")) {
216 *outContextType = GrContextFactory::kNull_GLContextType;
217 return true;
218 }
219#if SK_ANGLE
220#ifdef SK_BUILD_FOR_WIN
221 if (value.equals("angle")) {
222 *outContextType = GrContextFactory::kANGLE_GLContextType;
223 return true;
224 }
225#endif
226 if (value.equals("angle-gl")) {
227 *outContextType = GrContextFactory::kANGLE_GL_GLContextType;
228 return true;
229 }
230#endif
231#if SK_COMMAND_BUFFER
232 if (value.equals("commandbuffer")) {
kkinnunen45c2c812016-02-25 02:03:43 -0800233 *outContextType = GrContextFactory::kCommandBufferES2_GLContextType;
234 return true;
235 }
236 if (value.equals("commandbuffer3")) {
237 *outContextType = GrContextFactory::kCommandBufferES3_GLContextType;
kkinnunen3e980c32015-12-23 01:33:00 -0800238 return true;
239 }
240#endif
241#if SK_MESA
242 if (value.equals("mesa")) {
243 *outContextType = GrContextFactory::kMESA_GLContextType;
244 return true;
245 }
246#endif
247 return false;
248}
brianosmana6f58192016-03-01 12:53:06 -0800249static bool parse_option_gpu_color(const SkString& value,
250 SkColorType* outColorType,
251 SkColorProfileType* outProfileType) {
252 if (value.equals("8888")) {
253 *outColorType = kN32_SkColorType;
254 *outProfileType = kLinear_SkColorProfileType;
255 return true;
256 }
257 if (value.equals("f16")) {
258 *outColorType = kRGBA_F16_SkColorType;
259 *outProfileType = kLinear_SkColorProfileType;
260 return true;
261 }
262 if (value.equals("srgb")) {
263 *outColorType = kN32_SkColorType;
264 *outProfileType = kSRGB_SkColorProfileType;
265 return true;
266 }
267 return false;
268}
kkinnunen3e980c32015-12-23 01:33:00 -0800269
270SkCommandLineConfigGpu* parse_command_line_config_gpu(const SkString& tag,
271 const SkTArray<SkString>& vias,
272 const SkString& options) {
273 // Defaults for GPU backend.
274 bool seenAPI = false;
275 SkCommandLineConfigGpu::ContextType contextType = GrContextFactory::kNative_GLContextType;
276 bool seenUseNVPR = false;
277 bool useNVPR = false;
278 bool seenUseDIText =false;
279 bool useDIText = false;
280 bool seenSamples = false;
281 int samples = 0;
brianosmana6f58192016-03-01 12:53:06 -0800282 bool seenColor = false;
283 SkColorType colorType = kN32_SkColorType;
284 SkColorProfileType profileType = kLinear_SkColorProfileType;
kkinnunen3e980c32015-12-23 01:33:00 -0800285
286 SkTArray<SkString> optionParts;
287 SkStrSplit(options.c_str(), ",", kStrict_SkStrSplitMode, &optionParts);
288 for (int i = 0; i < optionParts.count(); ++i) {
289 SkTArray<SkString> keyValueParts;
290 SkStrSplit(optionParts[i].c_str(), "=", kStrict_SkStrSplitMode, &keyValueParts);
291 if (keyValueParts.count() != 2) {
292 return nullptr;
293 }
294 const SkString& key = keyValueParts[0];
295 const SkString& value = keyValueParts[1];
296 bool valueOk = false;
297 if (key.equals("api") && !seenAPI) {
298 valueOk = parse_option_gpu_api(value, &contextType);
299 seenAPI = true;
300 } else if (key.equals("nvpr") && !seenUseNVPR) {
301 valueOk = parse_option_bool(value, &useNVPR);
302 seenUseNVPR = true;
303 } else if (key.equals("dit") && !seenUseDIText) {
304 valueOk = parse_option_bool(value, &useDIText);
305 seenUseDIText = true;
306 } else if (key.equals("samples") && !seenSamples) {
307 valueOk = parse_option_int(value, &samples);
308 seenSamples = true;
brianosmana6f58192016-03-01 12:53:06 -0800309 } else if (key.equals("color") && !seenColor) {
310 valueOk = parse_option_gpu_color(value, &colorType, &profileType);
311 seenColor = true;
kkinnunen3e980c32015-12-23 01:33:00 -0800312 }
313 if (!valueOk) {
314 return nullptr;
315 }
316 }
brianosmana6f58192016-03-01 12:53:06 -0800317 return new SkCommandLineConfigGpu(tag, vias, contextType, useNVPR, useDIText, samples,
318 colorType, profileType);
kkinnunen3e980c32015-12-23 01:33:00 -0800319}
320#endif
321
322void ParseConfigs(const SkCommandLineFlags::StringArray& configs,
323 SkCommandLineConfigArray* outResult) {
324 outResult->reset();
325 for (int i = 0; i < configs.count(); ++i) {
326 SkString extendedBackend;
327 SkString extendedOptions;
328 SkString simpleBackend;
329 SkTArray<SkString> vias;
330
331 SkString tag(configs[i]);
332 SkTArray<SkString> parts;
333 SkStrSplit(tag.c_str(), "(", kStrict_SkStrSplitMode, &parts);
334 if (parts.count() == 2) {
335 SkTArray<SkString> parts2;
336 SkStrSplit(parts[1].c_str(), ")", kStrict_SkStrSplitMode, &parts2);
337 if (parts2.count() == 2 && parts2[1].isEmpty()) {
338 SkStrSplit(parts[0].c_str(), "-", kStrict_SkStrSplitMode, &vias);
339 if (vias.count()) {
340 extendedBackend = vias[vias.count() - 1];
341 vias.pop_back();
342 } else {
343 extendedBackend = parts[0];
344 }
345 extendedOptions = parts2[0];
346 simpleBackend.printf("%s(%s)", extendedBackend.c_str(), extendedOptions.c_str());
347 }
348 }
349
350 if (extendedBackend.isEmpty()) {
351 simpleBackend = tag;
352 SkStrSplit(tag.c_str(), "-", kStrict_SkStrSplitMode, &vias);
353 if (vias.count()) {
354 simpleBackend = vias[vias.count() - 1];
355 vias.pop_back();
356 }
357 // Note: no #if SK_ANGLE: this is a special rule in the via-tag grammar.
358 if (vias.count() && simpleBackend.equals("gl") &&
359 vias[vias.count() - 1].equals("angle")) {
360 simpleBackend = "angle-gl";
361 vias.pop_back();
362 }
363
364 for (auto& predefinedConfig : gPredefinedConfigs) {
365 if (simpleBackend.equals(predefinedConfig.predefinedConfig)) {
366 extendedBackend = predefinedConfig.backend;
367 extendedOptions = predefinedConfig.options;
368 break;
369 }
370 }
371 }
372 SkCommandLineConfig* parsedConfig = nullptr;
373#if SK_SUPPORT_GPU
374 if (extendedBackend.equals("gpu")) {
375 parsedConfig = parse_command_line_config_gpu(tag, vias, extendedOptions);
376 }
377#endif
378 if (!parsedConfig) {
379 parsedConfig = new SkCommandLineConfig(tag, simpleBackend, vias);
380 }
381 outResult->emplace_back(parsedConfig);
382 }
383}