• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "SkColorSpace_Base.h"
9 #include "SkCommonFlagsConfig.h"
10 #include "SkImageInfo.h"
11 
12 #include <stdlib.h>
13 
14 #if SK_SUPPORT_GPU
15 using sk_gpu_test::GrContextFactory;
16 #endif
17 
18 #if defined(SK_BUILD_FOR_ANDROID) || defined(SK_BUILD_FOR_IOS)
19 #    define DEFAULT_GPU_CONFIG "gles"
20 #else
21 #    define DEFAULT_GPU_CONFIG "gl"
22 #endif
23 
24 static const char defaultConfigs[] =
25     "8888 " DEFAULT_GPU_CONFIG " nonrendering "
26 #if defined(SK_BUILD_FOR_WIN)
27     " angle_d3d11_es2"
28 #endif
29     ;
30 
31 #undef DEFAULT_GPU_CONFIG
32 
33 static const struct {
34     const char* predefinedConfig;
35     const char* backend;
36     const char* options;
37 } gPredefinedConfigs[] ={
38 #if SK_SUPPORT_GPU
39     { "gl",                    "gpu", "api=gl" },
40     { "gles",                  "gpu", "api=gles" },
41     { "glmsaa4",               "gpu", "api=gl,samples=4" },
42     { "glmsaa8" ,              "gpu", "api=gl,samples=8" },
43     { "glesmsaa4",             "gpu", "api=gles,samples=4" },
44     { "glnvpr4",               "gpu", "api=gl,nvpr=true,samples=4" },
45     { "glnvpr8" ,              "gpu", "api=gl,nvpr=true,samples=8" },
46     { "glnvprdit4",            "gpu", "api=gl,nvpr=true,samples=4,dit=true" },
47     { "glnvprdit8" ,           "gpu", "api=gl,nvpr=true,samples=8,dit=true" },
48     { "glesnvpr4",             "gpu", "api=gles,nvpr=true,samples=4" },
49     { "glesnvprdit4",          "gpu", "api=gles,nvpr=true,samples=4,dit=true" },
50     { "glinst",                "gpu", "api=gl,inst=true" },
51     { "glinst4",               "gpu", "api=gl,inst=true,samples=4" },
52     { "glinstdit4",            "gpu", "api=gl,inst=true,samples=4,dit=true" },
53     { "glinst8" ,              "gpu", "api=gl,inst=true,samples=8" },
54     { "glinstdit8" ,           "gpu", "api=gl,inst=true,samples=8,dit=true" },
55     { "glesinst",              "gpu", "api=gles,inst=true" },
56     { "glesinst4",             "gpu", "api=gles,inst=true,samples=4" },
57     { "glesinstdit4",          "gpu", "api=gles,inst=true,samples=4,dit=true" },
58     { "glf16",                 "gpu", "api=gl,color=f16" },
59     { "glsrgb",                "gpu", "api=gl,color=srgb" },
60     { "glsrgbnl",              "gpu", "api=gl,color=srgbnl" },
61     { "glesf16",               "gpu", "api=gles,color=f16" },
62     { "glessrgb",              "gpu", "api=gles,color=srgb" },
63     { "glessrgbnl",            "gpu", "api=gles,color=srgbnl" },
64     { "glsrgb",                "gpu", "api=gl,color=srgb" },
65     { "glwide",                "gpu", "api=gl,color=f16_wide" },
66     { "glnarrow",              "gpu", "api=gl,color=f16_narrow" },
67     { "glessrgb",              "gpu", "api=gles,color=srgb" },
68     { "gleswide",              "gpu", "api=gles,color=f16_wide" },
69     { "glesnarrow",            "gpu", "api=gles,color=f16_narrow" },
70     { "gldft",                 "gpu", "api=gl,dit=true" },
71     { "glesdft",               "gpu", "api=gles,dit=true" },
72     { "debuggl",               "gpu", "api=debuggl" },
73     { "nullgl",                "gpu", "api=nullgl" },
74     { "angle_d3d11_es2",       "gpu", "api=angle_d3d11_es2" },
75     { "angle_d3d11_es3",       "gpu", "api=angle_d3d11_es3" },
76     { "angle_d3d9_es2",        "gpu", "api=angle_d3d9_es2" },
77     { "angle_d3d11_es2_msaa4", "gpu", "api=angle_d3d11_es2,samples=4" },
78     { "angle_d3d11_es2_msaa8", "gpu", "api=angle_d3d11_es2,samples=8" },
79     { "angle_gl_es2",          "gpu", "api=angle_gl_es2" },
80     { "angle_gl_es3",          "gpu", "api=angle_gl_es3" },
81     { "commandbuffer",         "gpu", "api=commandbuffer" }
82 #if SK_MESA
83     ,{ "mesa",                 "gpu", "api=mesa" }
84 #endif
85 #ifdef SK_VULKAN
86     ,{ "vk",                   "gpu", "api=vulkan" }
87     ,{ "vksrgb",               "gpu", "api=vulkan,color=srgb" }
88     ,{ "vkwide",               "gpu", "api=vulkan,color=f16_wide" }
89     ,{ "vkmsaa4",              "gpu", "api=vulkan,samples=4" }
90     ,{ "vkmsaa8",              "gpu", "api=vulkan,samples=8" }
91 #endif
92 
93 #else
94 { "", "", "" }
95 #endif
96 };
97 
98 static const char configHelp[] =
99     "Options: 565 8888 srgb f16 nonrendering null pdf pdfa skp pipe svg xps";
100 
config_help_fn()101 static const char* config_help_fn() {
102     static SkString helpString;
103     helpString.set(configHelp);
104     for (const auto& config : gPredefinedConfigs) {
105         helpString.appendf(" %s", config.predefinedConfig);
106     }
107     helpString.append(" or use extended form 'backend[option=value,...]'.\n");
108     return helpString.c_str();
109 }
110 
111 static const char configExtendedHelp[] =
112     "Extended form: 'backend(option=value,...)'\n\n"
113     "Possible backends and options:\n"
114 #if SK_SUPPORT_GPU
115     "\n"
116     "gpu[api=string,color=string,dit=bool,nvpr=bool,inst=bool,samples=int]\n"
117     "\tapi\ttype: string\trequired\n"
118     "\t    Select graphics API to use with gpu backend.\n"
119     "\t    Options:\n"
120     "\t\tnative\t\t\tUse platform default OpenGL or OpenGL ES backend.\n"
121     "\t\tgl    \t\t\tUse OpenGL.\n"
122     "\t\tgles  \t\t\tUse OpenGL ES.\n"
123     "\t\tdebuggl \t\t\tUse debug OpenGL.\n"
124     "\t\tnullgl \t\t\tUse null OpenGL.\n"
125     "\t\tangle_d3d9_es2\t\t\tUse OpenGL ES2 on the ANGLE Direct3D9 backend.\n"
126     "\t\tangle_d3d11_es2\t\t\tUse OpenGL ES2 on the ANGLE Direct3D11 backend.\n"
127     "\t\tangle_d3d11_es3\t\t\tUse OpenGL ES3 on the ANGLE Direct3D11 backend.\n"
128     "\t\tangle_gl_es2\t\t\tUse OpenGL ES2 on the ANGLE OpenGL backend.\n"
129     "\t\tangle_gl_es3\t\t\tUse OpenGL ES3 on the ANGLE OpenGL backend.\n"
130     "\t\tcommandbuffer\t\tUse command buffer.\n"
131 #if SK_MESA
132     "\t\tmesa\t\t\tUse MESA.\n"
133 #endif
134 #ifdef SK_VULKAN
135     "\t\tvulkan\t\t\tUse Vulkan.\n"
136 #endif
137     "\tcolor\ttype: string\tdefault: 8888.\n"
138     "\t    Select framebuffer color format.\n"
139     "\t    Options:\n"
140     "\t\t8888\t\t\tLinear 8888.\n"
141     "\t\tf16{_gamut}\t\tLinear 16-bit floating point.\n"
142     "\t\tsrgb{_gamut}\t\tsRGB 8888.\n"
143     "\t  gamut\ttype: string\tdefault: srgb.\n"
144     "\t    Select color gamut for f16 or sRGB format buffers.\n"
145     "\t    Options:\n"
146     "\t\tsrgb\t\t\tsRGB gamut.\n"
147     "\t\twide\t\t\tWide Gamut RGB.\n"
148     "\tdit\ttype: bool\tdefault: false.\n"
149     "\t    Use device independent text.\n"
150     "\tnvpr\ttype: bool\tdefault: false.\n"
151     "\t    Use NV_path_rendering OpenGL and OpenGL ES extension.\n"
152     "\tsamples\ttype: int\tdefault: 0.\n"
153     "\t    Use multisampling with N samples.\n"
154     "\n"
155     "Predefined configs:\n\n"
156     // Help text for pre-defined configs is auto-generated from gPredefinedConfigs
157 #endif
158     ;
159 
config_extended_help_fn()160 static const char* config_extended_help_fn() {
161     static SkString helpString;
162     helpString.set(configExtendedHelp);
163     for (const auto& config : gPredefinedConfigs) {
164         helpString.appendf("\t%-10s\t= gpu(%s)\n", config.predefinedConfig, config.options);
165     }
166     return helpString.c_str();
167 }
168 
169 DEFINE_extended_string(config, defaultConfigs, config_help_fn(), config_extended_help_fn());
170 
SkCommandLineConfig(const SkString & tag,const SkString & backend,const SkTArray<SkString> & viaParts)171 SkCommandLineConfig::SkCommandLineConfig(const SkString& tag, const SkString& backend,
172                                          const SkTArray<SkString>& viaParts)
173         : fTag(tag)
174         , fBackend(backend)
175         , fViaParts(viaParts) {
176 }
~SkCommandLineConfig()177 SkCommandLineConfig::~SkCommandLineConfig() {
178 }
179 
180 #if SK_SUPPORT_GPU
SkCommandLineConfigGpu(const SkString & tag,const SkTArray<SkString> & viaParts,ContextType contextType,bool useNVPR,bool useInstanced,bool useDIText,int samples,SkColorType colorType,sk_sp<SkColorSpace> colorSpace)181 SkCommandLineConfigGpu::SkCommandLineConfigGpu(
182     const SkString& tag, const SkTArray<SkString>& viaParts, ContextType contextType, bool useNVPR,
183     bool useInstanced, bool useDIText, int samples, SkColorType colorType,
184     sk_sp<SkColorSpace> colorSpace)
185         : SkCommandLineConfig(tag, SkString("gpu"), viaParts)
186         , fContextType(contextType)
187         , fContextOverrides(ContextOverrides::kNone)
188         , fUseDIText(useDIText)
189         , fSamples(samples)
190         , fColorType(colorType)
191         , fColorSpace(std::move(colorSpace)) {
192     if (useNVPR) {
193         fContextOverrides |= ContextOverrides::kRequireNVPRSupport;
194     } else if (!useInstanced) {
195         // We don't disable NVPR for instanced configs. Otherwise the caps wouldn't use mixed
196         // samples and we couldn't test the mixed samples backend for simple shapes.
197         fContextOverrides |= ContextOverrides::kDisableNVPR;
198     }
199     if (useInstanced) {
200         fContextOverrides |= ContextOverrides::kUseInstanced;
201     }
202     // Subtle logic: If the config has a color space attached, we're going to be rendering to sRGB,
203     // so we need that capability. In addition, to get the widest test coverage, we DO NOT require
204     // that we can disable sRGB decode. (That's for rendering sRGB sources to legacy surfaces).
205     //
206     // If the config doesn't have a color space attached, we're going to be rendering in legacy
207     // mode. In that case, we don't require sRGB capability and we defer to the client to decide on
208     // sRGB decode control.
209     if (fColorSpace) {
210         fContextOverrides |= ContextOverrides::kRequireSRGBSupport;
211         fContextOverrides |= ContextOverrides::kAllowSRGBWithoutDecodeControl;
212     }
213 }
parse_option_int(const SkString & value,int * outInt)214 static bool parse_option_int(const SkString& value, int* outInt) {
215     if (value.isEmpty()) {
216         return false;
217     }
218     char* endptr = nullptr;
219     long intValue = strtol(value.c_str(), &endptr, 10);
220     if (*endptr != '\0') {
221         return false;
222     }
223     *outInt = static_cast<int>(intValue);
224     return true;
225 }
parse_option_bool(const SkString & value,bool * outBool)226 static bool parse_option_bool(const SkString& value, bool* outBool) {
227     if (value.equals("true")) {
228         *outBool = true;
229         return true;
230     }
231     if (value.equals("false")) {
232         *outBool = false;
233         return true;
234     }
235     return false;
236 }
parse_option_gpu_api(const SkString & value,SkCommandLineConfigGpu::ContextType * outContextType)237 static bool parse_option_gpu_api(const SkString& value,
238                                  SkCommandLineConfigGpu::ContextType* outContextType) {
239     if (value.equals("gl")) {
240         *outContextType = GrContextFactory::kGL_ContextType;
241         return true;
242     }
243     if (value.equals("gles")) {
244         *outContextType = GrContextFactory::kGLES_ContextType;
245         return true;
246     }
247     if (value.equals("debuggl")) {
248         *outContextType = GrContextFactory::kDebugGL_ContextType;
249         return true;
250     }
251     if (value.equals("nullgl")) {
252         *outContextType = GrContextFactory::kNullGL_ContextType;
253         return true;
254     }
255     if (value.equals("angle_d3d9_es2")) {
256         *outContextType = GrContextFactory::kANGLE_D3D9_ES2_ContextType;
257         return true;
258     }
259     if (value.equals("angle_d3d11_es2")) {
260         *outContextType = GrContextFactory::kANGLE_D3D11_ES2_ContextType;
261         return true;
262     }
263     if (value.equals("angle_d3d11_es3")) {
264         *outContextType = GrContextFactory::kANGLE_D3D11_ES3_ContextType;
265         return true;
266     }
267     if (value.equals("angle_gl_es2")) {
268         *outContextType = GrContextFactory::kANGLE_GL_ES2_ContextType;
269         return true;
270     }
271     if (value.equals("angle_gl_es3")) {
272         *outContextType = GrContextFactory::kANGLE_GL_ES3_ContextType;
273         return true;
274     }
275     if (value.equals("commandbuffer")) {
276         *outContextType = GrContextFactory::kCommandBuffer_ContextType;
277         return true;
278     }
279 #if SK_MESA
280     if (value.equals("mesa")) {
281         *outContextType = GrContextFactory::kMESA_ContextType;
282         return true;
283     }
284 #endif
285 #ifdef SK_VULKAN
286     if (value.equals("vulkan")) {
287         *outContextType = GrContextFactory::kVulkan_ContextType;
288         return true;
289     }
290 #endif
291     return false;
292 }
parse_option_gpu_color(const SkString & value,SkColorType * outColorType,sk_sp<SkColorSpace> * outColorSpace)293 static bool parse_option_gpu_color(const SkString& value,
294                                    SkColorType* outColorType,
295                                    sk_sp<SkColorSpace>* outColorSpace) {
296     if (value.equals("8888")) {
297         *outColorType = kRGBA_8888_SkColorType;
298         *outColorSpace = nullptr;
299         return true;
300     }
301 
302     SkTArray<SkString> commands;
303     SkStrSplit(value.c_str(), "_", &commands);
304     if (commands.count() < 1 || commands.count() > 2) {
305         return false;
306     }
307 
308     const bool linearGamma = commands[0].equals("f16");
309     SkColorSpace::Gamut gamut = SkColorSpace::kSRGB_Gamut;
310     SkColorSpace::RenderTargetGamma gamma = linearGamma ? SkColorSpace::kLinear_RenderTargetGamma
311                                                         : SkColorSpace::kSRGB_RenderTargetGamma;
312     *outColorSpace = SkColorSpace::MakeRGB(gamma, gamut);
313 
314     if (commands.count() == 2) {
315         if (commands[1].equals("srgb")) {
316             // sRGB gamut (which is our default)
317         } else if (commands[1].equals("wide")) {
318             // WideGamut RGB
319             const float gWideGamutRGB_toXYZD50[]{
320                 0.7161046f, 0.1009296f, 0.1471858f,  // -> X
321                 0.2581874f, 0.7249378f, 0.0168748f,  // -> Y
322                 0.0000000f, 0.0517813f, 0.7734287f,  // -> Z
323             };
324             SkMatrix44 wideGamutRGBMatrix(SkMatrix44::kUninitialized_Constructor);
325             wideGamutRGBMatrix.set3x3RowMajorf(gWideGamutRGB_toXYZD50);
326             *outColorSpace = SkColorSpace::MakeRGB(gamma, wideGamutRGBMatrix);
327         } else if (commands[1].equals("narrow")) {
328             // NarrowGamut RGB (an artifically smaller than sRGB gamut)
329             SkColorSpacePrimaries primaries ={
330                 0.54f, 0.33f,     // Rx, Ry
331                 0.33f, 0.50f,     // Gx, Gy
332                 0.25f, 0.20f,     // Bx, By
333                 0.3127f, 0.3290f, // Wx, Wy
334             };
335             SkMatrix44 narrowGamutRGBMatrix(SkMatrix44::kUninitialized_Constructor);
336             primaries.toXYZD50(&narrowGamutRGBMatrix);
337             *outColorSpace = SkColorSpace::MakeRGB(gamma, narrowGamutRGBMatrix);
338         } else {
339             // Unknown color gamut
340             return false;
341         }
342     }
343 
344     // Now pick a color type
345     if (commands[0].equals("f16")) {
346         *outColorType = kRGBA_F16_SkColorType;
347         return true;
348     }
349     if (commands[0].equals("srgb") || commands[0].equals("srgbnl")) {
350         *outColorType = kRGBA_8888_SkColorType;
351         return true;
352     }
353     return false;
354 }
355 
parse_command_line_config_gpu(const SkString & tag,const SkTArray<SkString> & vias,const SkString & options)356 SkCommandLineConfigGpu* parse_command_line_config_gpu(const SkString& tag,
357                                                       const SkTArray<SkString>& vias,
358                                                       const SkString& options) {
359     // Defaults for GPU backend.
360     bool seenAPI = false;
361     SkCommandLineConfigGpu::ContextType contextType = GrContextFactory::kGL_ContextType;
362     bool seenUseNVPR = false;
363     bool useNVPR = false;
364     bool seenUseInstanced = false;
365     bool useInstanced = false;
366     bool seenUseDIText =false;
367     bool useDIText = false;
368     bool seenSamples = false;
369     int samples = 0;
370     bool seenColor = false;
371     SkColorType colorType = kRGBA_8888_SkColorType;
372     sk_sp<SkColorSpace> colorSpace = nullptr;
373 
374     SkTArray<SkString> optionParts;
375     SkStrSplit(options.c_str(), ",", kStrict_SkStrSplitMode, &optionParts);
376     for (int i = 0; i < optionParts.count(); ++i) {
377         SkTArray<SkString> keyValueParts;
378         SkStrSplit(optionParts[i].c_str(), "=", kStrict_SkStrSplitMode, &keyValueParts);
379         if (keyValueParts.count() != 2) {
380             return nullptr;
381         }
382         const SkString& key = keyValueParts[0];
383         const SkString& value = keyValueParts[1];
384         bool valueOk = false;
385         if (key.equals("api") && !seenAPI) {
386             valueOk = parse_option_gpu_api(value, &contextType);
387             seenAPI = true;
388         } else if (key.equals("nvpr") && !seenUseNVPR) {
389             valueOk = parse_option_bool(value, &useNVPR);
390             seenUseNVPR = true;
391         } else if (key.equals("inst") && !seenUseInstanced) {
392             valueOk = parse_option_bool(value, &useInstanced);
393             seenUseInstanced = true;
394         } else if (key.equals("dit") && !seenUseDIText) {
395             valueOk = parse_option_bool(value, &useDIText);
396             seenUseDIText = true;
397         } else if (key.equals("samples") && !seenSamples) {
398             valueOk = parse_option_int(value, &samples);
399             seenSamples = true;
400         } else if (key.equals("color") && !seenColor) {
401             valueOk = parse_option_gpu_color(value, &colorType, &colorSpace);
402             seenColor = true;
403         }
404         if (!valueOk) {
405             return nullptr;
406         }
407     }
408     if (!seenAPI) {
409         return nullptr;
410     }
411     return new SkCommandLineConfigGpu(tag, vias, contextType, useNVPR, useInstanced, useDIText,
412                                       samples, colorType, colorSpace);
413 }
414 #endif
415 
ParseConfigs(const SkCommandLineFlags::StringArray & configs,SkCommandLineConfigArray * outResult)416 void ParseConfigs(const SkCommandLineFlags::StringArray& configs,
417                   SkCommandLineConfigArray* outResult) {
418     outResult->reset();
419     for (int i = 0; i < configs.count(); ++i) {
420         SkString extendedBackend;
421         SkString extendedOptions;
422         SkString simpleBackend;
423         SkTArray<SkString> vias;
424 
425         SkString tag(configs[i]);
426         SkTArray<SkString> parts;
427         SkStrSplit(tag.c_str(), "[", kStrict_SkStrSplitMode, &parts);
428         if (parts.count() == 2) {
429             SkTArray<SkString> parts2;
430             SkStrSplit(parts[1].c_str(), "]", kStrict_SkStrSplitMode, &parts2);
431             if (parts2.count() == 2 && parts2[1].isEmpty()) {
432                 SkStrSplit(parts[0].c_str(), "-", kStrict_SkStrSplitMode, &vias);
433                 if (vias.count()) {
434                     extendedBackend = vias[vias.count() - 1];
435                     vias.pop_back();
436                 } else {
437                     extendedBackend = parts[0];
438                 }
439                 extendedOptions = parts2[0];
440                 simpleBackend.printf("%s[%s]", extendedBackend.c_str(), extendedOptions.c_str());
441             }
442         }
443 
444         if (extendedBackend.isEmpty()) {
445             simpleBackend = tag;
446             SkStrSplit(tag.c_str(), "-", kStrict_SkStrSplitMode, &vias);
447             if (vias.count()) {
448                 simpleBackend = vias[vias.count() - 1];
449                 vias.pop_back();
450             }
451             for (auto& predefinedConfig : gPredefinedConfigs) {
452                 if (simpleBackend.equals(predefinedConfig.predefinedConfig)) {
453                     extendedBackend = predefinedConfig.backend;
454                     extendedOptions = predefinedConfig.options;
455                     break;
456                 }
457             }
458         }
459         SkCommandLineConfig* parsedConfig = nullptr;
460 #if SK_SUPPORT_GPU
461         if (extendedBackend.equals("gpu")) {
462             parsedConfig = parse_command_line_config_gpu(tag, vias, extendedOptions);
463         }
464 #endif
465         if (!parsedConfig) {
466             parsedConfig = new SkCommandLineConfig(tag, simpleBackend, vias);
467         }
468         outResult->emplace_back(parsedConfig);
469     }
470 }
471