kkinnunen | 3e980c3 | 2015-12-23 01:33:00 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 12 | static 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 | |
| 24 | static const char configHelp[] = |
| 25 | "Options: 565 8888 debug gpu gpudebug gpudft gpunull " |
| 26 | "msaa16 msaa4 nonrendering null nullgpu nvprmsaa16 nvprmsaa4 " |
| 27 | "pdf pdf_poppler skp svg xps" |
| 28 | #if SK_ANGLE |
| 29 | #ifdef SK_BUILD_FOR_WIN |
| 30 | " angle" |
| 31 | #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 | |
| 45 | static const char configExtendedHelp[] = |
| 46 | "Extended form: 'backend(option=value,...)'\n\n" |
| 47 | "Possible backends and options:\n" |
| 48 | #if SK_SUPPORT_GPU |
| 49 | "\n" |
| 50 | "gpu(api=string,dit=bool,nvpr=bool,samples=int)\tGPU backend\n" |
| 51 | "\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 |
| 71 | "\tdit\ttype: bool\tdefault: false.\n" |
| 72 | "\t Use device independent text.\n" |
| 73 | "\tnvpr\ttype: bool\tdefault: false.\n" |
| 74 | "\t Use NV_path_rendering OpenGL and OpenGL ES extension.\n" |
| 75 | "\tsamples\ttype: int\tdefault: 0.\n" |
| 76 | "\t Use multisampling with N samples.\n" |
| 77 | "\n" |
| 78 | "Predefined configs:\n\n" |
| 79 | "\tgpu \t= gpu()\n" |
| 80 | "\tmsaa4 \t= gpu(samples=4)\n" |
| 81 | "\tmsaa16 \t= gpu(samples=16)\n" |
| 82 | "\tnvprmsaa4\t= gpu(nvpr=true,samples=4)\n" |
| 83 | "\tnvprmsaa16\t= gpu(nvpr=true,samples=16)\n" |
| 84 | "\tgpudft \t= gpu(dit=true)\n" |
| 85 | "\tgpudebug \t= gpu(api=debug)\n" |
| 86 | "\tgpunull \t= gpu(api=null)\n" |
| 87 | "\tdebug \t= gpu(api=debug)\n" |
| 88 | "\tnullgpu \t= gpu(api=null)\n" |
| 89 | #if SK_ANGLE |
| 90 | #ifdef SK_BUILD_FOR_WIN |
| 91 | "\tangle \t= gpu(api=angle)\n" |
| 92 | #endif |
| 93 | "\tangle-gl \t= gpu(api=angle-gl)\n" |
| 94 | #endif |
| 95 | #if SK_COMMAND_BUFFER |
| 96 | "\tcommandbuffer\t= gpu(api=commandbuffer)\n" |
| 97 | #endif |
| 98 | #if SK_MESA |
| 99 | "\tmesa \t= gpu(api=mesa)\n" |
| 100 | #endif |
| 101 | #endif |
| 102 | ; |
| 103 | |
| 104 | DEFINE_extended_string(config, defaultConfigs, configHelp, configExtendedHelp); |
| 105 | |
| 106 | static const struct { |
| 107 | const char* predefinedConfig; |
| 108 | const char* backend; |
| 109 | const char* options; |
| 110 | } gPredefinedConfigs[] = { |
| 111 | #if SK_SUPPORT_GPU |
| 112 | { "gpu", "gpu", "" }, |
| 113 | { "msaa4", "gpu", "samples=4" }, |
| 114 | { "msaa16", "gpu", "samples=16" }, |
kkinnunen | e3c2f80 | 2015-12-29 08:57:32 -0800 | [diff] [blame] | 115 | { "nvprmsaa4", "gpu", "nvpr=true,samples=4,dit=true" }, |
| 116 | { "nvprmsaa16", "gpu", "nvpr=true,samples=16,dit=true" }, |
kkinnunen | 3e980c3 | 2015-12-23 01:33:00 -0800 | [diff] [blame] | 117 | { "gpudft", "gpu", "dit=true" }, |
| 118 | { "gpudebug", "gpu", "api=debug" }, |
| 119 | { "gpunull", "gpu", "api=null" }, |
| 120 | { "debug", "gpu", "api=debug" }, |
| 121 | { "nullgpu", "gpu", "api=null" } |
| 122 | #if SK_ANGLE |
| 123 | #ifdef SK_BUILD_FOR_WIN |
| 124 | , { "angle", "gpu", "api=angle" } |
| 125 | #endif |
| 126 | , { "angle-gl", "gpu", "api=angle-gl" } |
| 127 | #endif |
| 128 | #if SK_COMMAND_BUFFER |
| 129 | , { "commandbuffer", "gpu", "api=commandbuffer" } |
| 130 | #endif |
| 131 | #if SK_MESA |
| 132 | , { "mesa", "gpu", "api=mesa" } |
| 133 | #endif |
| 134 | #else |
| 135 | { "", "", "" } |
| 136 | #endif |
| 137 | }; |
| 138 | |
| 139 | SkCommandLineConfig::SkCommandLineConfig(const SkString& tag, const SkString& backend, |
| 140 | const SkTArray<SkString>& viaParts) |
| 141 | : fTag(tag) |
| 142 | , fBackend(backend) |
| 143 | , fViaParts(viaParts) { |
| 144 | } |
| 145 | SkCommandLineConfig::~SkCommandLineConfig() { |
| 146 | } |
| 147 | |
| 148 | #if SK_SUPPORT_GPU |
| 149 | SkCommandLineConfigGpu::SkCommandLineConfigGpu( |
| 150 | const SkString& tag, const SkTArray<SkString>& viaParts, |
| 151 | ContextType contextType, bool useNVPR, bool useDIText, int samples) |
| 152 | : SkCommandLineConfig(tag, SkString("gpu"), viaParts) |
| 153 | , fContextType(contextType) |
| 154 | , fUseNVPR(useNVPR) |
| 155 | , fUseDIText(useDIText) |
| 156 | , fSamples(samples) { |
| 157 | } |
| 158 | static bool parse_option_int(const SkString& value, int* outInt) { |
| 159 | if (value.isEmpty()) { |
| 160 | return false; |
| 161 | } |
| 162 | char* endptr = nullptr; |
| 163 | long intValue = strtol(value.c_str(), &endptr, 10); |
| 164 | if (*endptr != '\0') { |
| 165 | return false; |
| 166 | } |
| 167 | *outInt = static_cast<int>(intValue); |
| 168 | return true; |
| 169 | } |
| 170 | static bool parse_option_bool(const SkString& value, bool* outBool) { |
| 171 | if (value.equals("true")) { |
| 172 | *outBool = true; |
| 173 | return true; |
| 174 | } |
| 175 | if (value.equals("false")) { |
| 176 | *outBool = false; |
| 177 | return true; |
| 178 | } |
| 179 | return false; |
| 180 | } |
| 181 | static bool parse_option_gpu_api(const SkString& value, |
| 182 | SkCommandLineConfigGpu::ContextType* outContextType) { |
| 183 | if (value.equals("native")) { |
| 184 | *outContextType = GrContextFactory::kNative_GLContextType; |
| 185 | return true; |
| 186 | } |
| 187 | if (value.equals("gl")) { |
| 188 | *outContextType = GrContextFactory::kGL_GLContextType; |
| 189 | return true; |
| 190 | } |
| 191 | if (value.equals("gles")) { |
| 192 | *outContextType = GrContextFactory::kGLES_GLContextType; |
| 193 | return true; |
| 194 | } |
| 195 | if (value.equals("debug")) { |
| 196 | *outContextType = GrContextFactory::kDebug_GLContextType; |
| 197 | return true; |
| 198 | } |
| 199 | if (value.equals("null")) { |
| 200 | *outContextType = GrContextFactory::kNull_GLContextType; |
| 201 | return true; |
| 202 | } |
| 203 | #if SK_ANGLE |
| 204 | #ifdef SK_BUILD_FOR_WIN |
| 205 | if (value.equals("angle")) { |
| 206 | *outContextType = GrContextFactory::kANGLE_GLContextType; |
| 207 | return true; |
| 208 | } |
| 209 | #endif |
| 210 | if (value.equals("angle-gl")) { |
| 211 | *outContextType = GrContextFactory::kANGLE_GL_GLContextType; |
| 212 | return true; |
| 213 | } |
| 214 | #endif |
| 215 | #if SK_COMMAND_BUFFER |
| 216 | if (value.equals("commandbuffer")) { |
| 217 | *outContextType = GrContextFactory::kCommandBuffer_GLContextType; |
| 218 | return true; |
| 219 | } |
| 220 | #endif |
| 221 | #if SK_MESA |
| 222 | if (value.equals("mesa")) { |
| 223 | *outContextType = GrContextFactory::kMESA_GLContextType; |
| 224 | return true; |
| 225 | } |
| 226 | #endif |
| 227 | return false; |
| 228 | } |
| 229 | |
| 230 | SkCommandLineConfigGpu* parse_command_line_config_gpu(const SkString& tag, |
| 231 | const SkTArray<SkString>& vias, |
| 232 | const SkString& options) { |
| 233 | // Defaults for GPU backend. |
| 234 | bool seenAPI = false; |
| 235 | SkCommandLineConfigGpu::ContextType contextType = GrContextFactory::kNative_GLContextType; |
| 236 | bool seenUseNVPR = false; |
| 237 | bool useNVPR = false; |
| 238 | bool seenUseDIText =false; |
| 239 | bool useDIText = false; |
| 240 | bool seenSamples = false; |
| 241 | int samples = 0; |
| 242 | |
| 243 | SkTArray<SkString> optionParts; |
| 244 | SkStrSplit(options.c_str(), ",", kStrict_SkStrSplitMode, &optionParts); |
| 245 | for (int i = 0; i < optionParts.count(); ++i) { |
| 246 | SkTArray<SkString> keyValueParts; |
| 247 | SkStrSplit(optionParts[i].c_str(), "=", kStrict_SkStrSplitMode, &keyValueParts); |
| 248 | if (keyValueParts.count() != 2) { |
| 249 | return nullptr; |
| 250 | } |
| 251 | const SkString& key = keyValueParts[0]; |
| 252 | const SkString& value = keyValueParts[1]; |
| 253 | bool valueOk = false; |
| 254 | if (key.equals("api") && !seenAPI) { |
| 255 | valueOk = parse_option_gpu_api(value, &contextType); |
| 256 | seenAPI = true; |
| 257 | } else if (key.equals("nvpr") && !seenUseNVPR) { |
| 258 | valueOk = parse_option_bool(value, &useNVPR); |
| 259 | seenUseNVPR = true; |
| 260 | } else if (key.equals("dit") && !seenUseDIText) { |
| 261 | valueOk = parse_option_bool(value, &useDIText); |
| 262 | seenUseDIText = true; |
| 263 | } else if (key.equals("samples") && !seenSamples) { |
| 264 | valueOk = parse_option_int(value, &samples); |
| 265 | seenSamples = true; |
| 266 | } |
| 267 | if (!valueOk) { |
| 268 | return nullptr; |
| 269 | } |
| 270 | } |
| 271 | return new SkCommandLineConfigGpu(tag, vias, contextType, useNVPR, useDIText, samples); |
| 272 | } |
| 273 | #endif |
| 274 | |
| 275 | void ParseConfigs(const SkCommandLineFlags::StringArray& configs, |
| 276 | SkCommandLineConfigArray* outResult) { |
| 277 | outResult->reset(); |
| 278 | for (int i = 0; i < configs.count(); ++i) { |
| 279 | SkString extendedBackend; |
| 280 | SkString extendedOptions; |
| 281 | SkString simpleBackend; |
| 282 | SkTArray<SkString> vias; |
| 283 | |
| 284 | SkString tag(configs[i]); |
| 285 | SkTArray<SkString> parts; |
| 286 | SkStrSplit(tag.c_str(), "(", kStrict_SkStrSplitMode, &parts); |
| 287 | if (parts.count() == 2) { |
| 288 | SkTArray<SkString> parts2; |
| 289 | SkStrSplit(parts[1].c_str(), ")", kStrict_SkStrSplitMode, &parts2); |
| 290 | if (parts2.count() == 2 && parts2[1].isEmpty()) { |
| 291 | SkStrSplit(parts[0].c_str(), "-", kStrict_SkStrSplitMode, &vias); |
| 292 | if (vias.count()) { |
| 293 | extendedBackend = vias[vias.count() - 1]; |
| 294 | vias.pop_back(); |
| 295 | } else { |
| 296 | extendedBackend = parts[0]; |
| 297 | } |
| 298 | extendedOptions = parts2[0]; |
| 299 | simpleBackend.printf("%s(%s)", extendedBackend.c_str(), extendedOptions.c_str()); |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | if (extendedBackend.isEmpty()) { |
| 304 | simpleBackend = tag; |
| 305 | SkStrSplit(tag.c_str(), "-", kStrict_SkStrSplitMode, &vias); |
| 306 | if (vias.count()) { |
| 307 | simpleBackend = vias[vias.count() - 1]; |
| 308 | vias.pop_back(); |
| 309 | } |
| 310 | // Note: no #if SK_ANGLE: this is a special rule in the via-tag grammar. |
| 311 | if (vias.count() && simpleBackend.equals("gl") && |
| 312 | vias[vias.count() - 1].equals("angle")) { |
| 313 | simpleBackend = "angle-gl"; |
| 314 | vias.pop_back(); |
| 315 | } |
| 316 | |
| 317 | for (auto& predefinedConfig : gPredefinedConfigs) { |
| 318 | if (simpleBackend.equals(predefinedConfig.predefinedConfig)) { |
| 319 | extendedBackend = predefinedConfig.backend; |
| 320 | extendedOptions = predefinedConfig.options; |
| 321 | break; |
| 322 | } |
| 323 | } |
| 324 | } |
| 325 | SkCommandLineConfig* parsedConfig = nullptr; |
| 326 | #if SK_SUPPORT_GPU |
| 327 | if (extendedBackend.equals("gpu")) { |
| 328 | parsedConfig = parse_command_line_config_gpu(tag, vias, extendedOptions); |
| 329 | } |
| 330 | #endif |
| 331 | if (!parsedConfig) { |
| 332 | parsedConfig = new SkCommandLineConfig(tag, simpleBackend, vias); |
| 333 | } |
| 334 | outResult->emplace_back(parsedConfig); |
| 335 | } |
| 336 | } |