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 #include "Test.h"
10 #include <initializer_list>
11 
12 namespace {
13 // The code
14 //   SkCommandLineFlags::StringArray FLAGS_config1 = make_string_array({"a", "b"})
15 // can be used to construct string array that one gets with command line flags.
16 // For example, the call above is equivalent of
17 //   DEFINE_string(config1, "a b", "");
18 // in cases where the default command line flag value ("a b") is used.
19 // make_string_array can be used to construct StringArray strings that have spaces in
20 // them.
make_string_array(std::initializer_list<const char * > strings)21 SkCommandLineFlags::StringArray make_string_array(std::initializer_list<const char*> strings) {
22     SkTArray<SkString> array;
23     for (auto& s : strings) {
24         array.push_back(SkString(s));
25     }
26     return SkCommandLineFlags::StringArray(array);
27 }
28 }
DEF_TEST(ParseConfigs_Gpu,reporter)29 DEF_TEST(ParseConfigs_Gpu, reporter) {
30     // Parses a normal config and returns correct "tag".
31     // Gpu config defaults work.
32     SkCommandLineFlags::StringArray config1 = make_string_array({"gpu"});
33     SkCommandLineConfigArray configs;
34     ParseConfigs(config1, &configs);
35 
36     REPORTER_ASSERT(reporter, configs.count() == 1);
37     REPORTER_ASSERT(reporter, configs[0]->getTag().equals("gpu"));
38     REPORTER_ASSERT(reporter, configs[0]->getViaParts().count() == 0);
39 #if SK_SUPPORT_GPU
40     REPORTER_ASSERT(reporter, configs[0]->asConfigGpu());
41     REPORTER_ASSERT(reporter, configs[0]->asConfigGpu()->getContextType()
42                     == GrContextFactory::kNative_GLContextType);
43     REPORTER_ASSERT(reporter, configs[0]->asConfigGpu()->getUseNVPR() == false);
44     REPORTER_ASSERT(reporter, configs[0]->asConfigGpu()->getUseDIText() == false);
45     REPORTER_ASSERT(reporter, configs[0]->asConfigGpu()->getSamples() == 0);
46 #endif
47 }
48 
DEF_TEST(ParseConfigs_OutParam,reporter)49 DEF_TEST(ParseConfigs_OutParam, reporter) {
50     // Clears the out parameter.
51     SkCommandLineFlags::StringArray config1 = make_string_array({"gpu"});
52     SkCommandLineConfigArray configs;
53     ParseConfigs(config1, &configs);
54     REPORTER_ASSERT(reporter, configs.count() == 1);
55     REPORTER_ASSERT(reporter, configs[0]->getTag().equals("gpu"));
56     SkCommandLineFlags::StringArray config2 = make_string_array({"8888"});
57     ParseConfigs(config2, &configs);
58     REPORTER_ASSERT(reporter, configs.count() == 1);
59     REPORTER_ASSERT(reporter, configs[0]->getTag().equals("8888"));
60 }
61 
DEF_TEST(ParseConfigs_DefaultConfigs,reporter)62 DEF_TEST(ParseConfigs_DefaultConfigs, reporter) {
63     // Parses all default configs and returns correct "tag".
64 
65     SkCommandLineFlags::StringArray config1 = make_string_array({
66         "565", "8888", "debug", "gpu", "gpudebug", "gpudft", "gpunull", "msaa16", "msaa4",
67         "nonrendering", "null", "nullgpu", "nvprmsaa16", "nvprmsaa4", "pdf", "pdf_poppler",
68         "skp", "svg", "xps", "angle", "angle-gl", "commandbuffer", "mesa", "hwui"
69     });
70 
71     SkCommandLineConfigArray configs;
72     ParseConfigs(config1, &configs);
73 
74     REPORTER_ASSERT(reporter, configs.count() == config1.count());
75     for (int i = 0; i < config1.count(); ++i) {
76         REPORTER_ASSERT(reporter, configs[i]->getTag().equals(config1[i]));
77         REPORTER_ASSERT(reporter, configs[i]->getViaParts().count() == 0);
78     }
79 #if SK_SUPPORT_GPU
80     REPORTER_ASSERT(reporter, !configs[0]->asConfigGpu());
81     REPORTER_ASSERT(reporter, !configs[1]->asConfigGpu());
82     REPORTER_ASSERT(reporter, configs[2]->asConfigGpu());
83     REPORTER_ASSERT(reporter, configs[3]->asConfigGpu());
84     REPORTER_ASSERT(reporter, configs[4]->asConfigGpu());
85     REPORTER_ASSERT(reporter, configs[5]->asConfigGpu()->getUseDIText());
86     REPORTER_ASSERT(reporter, configs[6]->asConfigGpu());
87     REPORTER_ASSERT(reporter, configs[7]->asConfigGpu()->getSamples() == 16);
88     REPORTER_ASSERT(reporter, configs[8]->asConfigGpu()->getSamples() == 4);
89     REPORTER_ASSERT(reporter, !configs[9]->asConfigGpu());
90     REPORTER_ASSERT(reporter, !configs[10]->asConfigGpu());
91     REPORTER_ASSERT(reporter, configs[11]->asConfigGpu());
92     REPORTER_ASSERT(reporter, configs[12]->asConfigGpu()->getSamples() == 16);
93     REPORTER_ASSERT(reporter, configs[12]->asConfigGpu()->getUseNVPR());
94     REPORTER_ASSERT(reporter, configs[12]->asConfigGpu()->getUseDIText());
95     REPORTER_ASSERT(reporter, configs[13]->asConfigGpu()->getSamples() == 4);
96     REPORTER_ASSERT(reporter, configs[13]->asConfigGpu()->getUseNVPR());
97     REPORTER_ASSERT(reporter, configs[13]->asConfigGpu()->getUseDIText());
98     REPORTER_ASSERT(reporter, !configs[14]->asConfigGpu());
99     REPORTER_ASSERT(reporter, !configs[15]->asConfigGpu());
100     REPORTER_ASSERT(reporter, !configs[16]->asConfigGpu());
101     REPORTER_ASSERT(reporter, !configs[17]->asConfigGpu());
102     REPORTER_ASSERT(reporter, !configs[18]->asConfigGpu());
103     REPORTER_ASSERT(reporter, !configs[23]->asConfigGpu());
104 #if SK_ANGLE
105 #ifdef SK_BUILD_FOR_WIN
106     REPORTER_ASSERT(reporter, configs[19]->asConfigGpu());
107 #else
108     REPORTER_ASSERT(reporter, !configs[19]->asConfigGpu());
109 #endif
110     REPORTER_ASSERT(reporter, configs[20]->asConfigGpu());
111 #else
112     REPORTER_ASSERT(reporter, !configs[19]->asConfigGpu());
113     REPORTER_ASSERT(reporter, !configs[20]->asConfigGpu());
114 #endif
115 #if SK_COMMAND_BUFFER
116     REPORTER_ASSERT(reporter, configs[21]->asConfigGpu());
117 #else
118     REPORTER_ASSERT(reporter, !configs[21]->asConfigGpu());
119 #endif
120 #if SK_MESA
121     REPORTER_ASSERT(reporter, configs[22]->asConfigGpu());
122 #else
123     REPORTER_ASSERT(reporter, !configs[22]->asConfigGpu());
124 #endif
125 #endif
126 }
127 
DEF_TEST(ParseConfigs_ExtendedGpuConfigsCorrect,reporter)128 DEF_TEST(ParseConfigs_ExtendedGpuConfigsCorrect, reporter) {
129     SkCommandLineFlags::StringArray config1 = make_string_array({
130         "gpu(nvpr=true,dit=false)",
131         "gpu(api=angle)",
132         "gpu(api=angle-gl)",
133         "gpu(api=mesa,samples=77)",
134         "gpu(dit=true,api=commandbuffer)",
135         "gpu()",
136         "gpu(api=gles)"
137     });
138 
139     SkCommandLineConfigArray configs;
140     ParseConfigs(config1, &configs);
141     REPORTER_ASSERT(reporter, configs.count() == config1.count());
142     for (int i = 0; i < config1.count(); ++i) {
143         REPORTER_ASSERT(reporter, configs[i]->getTag().equals(config1[i]));
144     }
145 #if SK_SUPPORT_GPU
146     REPORTER_ASSERT(reporter, configs[0]->asConfigGpu()->getContextType() ==
147                     GrContextFactory::kNative_GLContextType);
148     REPORTER_ASSERT(reporter, configs[0]->asConfigGpu()->getUseNVPR());
149     REPORTER_ASSERT(reporter, !configs[0]->asConfigGpu()->getUseDIText());
150     REPORTER_ASSERT(reporter, configs[0]->asConfigGpu()->getSamples() == 0);
151 #if SK_ANGLE
152 #ifdef SK_BUILD_FOR_WIN
153     REPORTER_ASSERT(reporter, configs[1]->asConfigGpu()->getContextType() ==
154                     GrContextFactory::kANGLE_GLContextType);
155 #else
156     REPORTER_ASSERT(reporter, !configs[1]->asConfigGpu());
157 #endif
158     REPORTER_ASSERT(reporter, configs[2]->asConfigGpu()->getContextType() ==
159                     GrContextFactory::kANGLE_GL_GLContextType);
160 #else
161     REPORTER_ASSERT(reporter, !configs[1]->asConfigGpu());
162     REPORTER_ASSERT(reporter, !configs[2]->asConfigGpu());
163 #endif
164 #if SK_MESA
165     REPORTER_ASSERT(reporter, configs[3]->asConfigGpu()->getContextType() ==
166                     GrContextFactory::kMESA_GLContextType);
167 #else
168     REPORTER_ASSERT(reporter, !configs[3]->asConfigGpu());
169 #endif
170 #if SK_COMMAND_BUFFER
171     REPORTER_ASSERT(reporter, configs[4]->asConfigGpu()->getContextType() ==
172                     GrContextFactory::kCommandBufferES2_GLContextType);
173 
174 #else
175     REPORTER_ASSERT(reporter, !configs[4]->asConfigGpu());
176 #endif
177     REPORTER_ASSERT(reporter, configs[5]->asConfigGpu()->getContextType() ==
178                     GrContextFactory::kNative_GLContextType);
179     REPORTER_ASSERT(reporter, !configs[5]->asConfigGpu()->getUseNVPR());
180     REPORTER_ASSERT(reporter, !configs[5]->asConfigGpu()->getUseDIText());
181     REPORTER_ASSERT(reporter, configs[5]->asConfigGpu()->getSamples() == 0);
182     REPORTER_ASSERT(reporter, configs[6]->asConfigGpu()->getContextType() ==
183                     GrContextFactory::kGLES_GLContextType);
184     REPORTER_ASSERT(reporter, !configs[6]->asConfigGpu()->getUseNVPR());
185     REPORTER_ASSERT(reporter, !configs[6]->asConfigGpu()->getUseDIText());
186     REPORTER_ASSERT(reporter, configs[6]->asConfigGpu()->getSamples() == 0);
187 
188 #endif
189 }
190 
DEF_TEST(ParseConfigs_ExtendedGpuConfigsIncorrect,reporter)191 DEF_TEST(ParseConfigs_ExtendedGpuConfigsIncorrect, reporter) {
192     SkCommandLineFlags::StringArray config1 = make_string_array({
193         "gpu(nvpr=1)", // Number as bool.
194         "gpu(api=gl,)", // Trailing in comma.
195         "gpu(api=angle-glu)", // Unknown api.
196         "gpu(api=,samples=0)", // Empty api.
197         "gpu(samples=true)", // Value true as a number.
198         "gpu(samples=0,samples=0)", // Duplicate option key.
199         "gpu(,samples=0)", // Leading comma.
200         "gpu(samples=54", // Missing closing parenthesis.
201         ",,",
202         "gpu(", // Missing parenthesis.
203         "samples=54" // No backend.
204         "gpu(nvpr=true )", // Space.
205     });
206 
207     SkCommandLineConfigArray configs;
208     ParseConfigs(config1, &configs);
209     REPORTER_ASSERT(reporter, configs.count() == config1.count());
210     for (int i = 0; i < config1.count(); ++i) {
211         REPORTER_ASSERT(reporter, configs[i]->getTag().equals(config1[i]));
212         REPORTER_ASSERT(reporter, configs[i]->getBackend().equals(config1[i]));
213 #if SK_SUPPORT_GPU
214         REPORTER_ASSERT(reporter, !configs[i]->asConfigGpu());
215 #endif
216     }
217 }
218 
219 
DEF_TEST(ParseConfigs_ExtendedGpuConfigsSurprises,reporter)220 DEF_TEST(ParseConfigs_ExtendedGpuConfigsSurprises, reporter) {
221     // These just list explicitly some properties of the system.
222     SkCommandLineFlags::StringArray config1 = make_string_array({
223         // Options are not canonized -> two same configs have a different tag.
224         "gpu(nvpr=true,dit=true)", "gpu(dit=true,nvpr=true)",
225         // API native is alias for gl or gles, but it's not canonized -> different tag.
226         "gpu(api=native)", "gpu(api=gl)", "gpu(api=gles)", ""
227         // Default values are not canonized -> different tag.
228         "gpu", "gpu()", "gpu(samples=0)", "gpu(api=native,samples=0)"
229     });
230     SkCommandLineConfigArray configs;
231     ParseConfigs(config1, &configs);
232     REPORTER_ASSERT(reporter, configs.count() == config1.count());
233     for (int i = 0; i < config1.count(); ++i) {
234         REPORTER_ASSERT(reporter, configs[i]->getTag().equals(config1[i]));
235 #if SK_SUPPORT_GPU
236         REPORTER_ASSERT(reporter, configs[i]->getBackend().equals("gpu"));
237         REPORTER_ASSERT(reporter, configs[i]->asConfigGpu());
238 #else
239         REPORTER_ASSERT(reporter, configs[i]->getBackend().equals(config1[i]));
240 #endif
241     }
242 }
DEF_TEST(ParseConfigs_ViaParsing,reporter)243 DEF_TEST(ParseConfigs_ViaParsing, reporter) {
244     SkCommandLineFlags::StringArray config1 = make_string_array({
245         "a-b-c-8888",
246         "zz-qq-gpu",
247         "a-angle-gl"
248     });
249 
250     SkCommandLineConfigArray configs;
251     ParseConfigs(config1, &configs);
252     const struct {
253         const char* backend;
254         const char* vias[3];
255     } expectedConfigs[] = {
256         {"8888", {"a", "b", "c"}},
257         {"gpu", {"zz", "qq", nullptr}},
258         {"angle-gl", {"a", nullptr, nullptr}}  // The angle-gl tag is only tag that contains
259                                                // hyphen.
260     };
261     for (int i = 0; i < config1.count(); ++i) {
262         REPORTER_ASSERT(reporter, configs[i]->getTag().equals(config1[i]));
263         REPORTER_ASSERT(reporter, configs[i]->getBackend().equals(expectedConfigs[i].backend));
264         for (int j = 0; j < static_cast<int>(SK_ARRAY_COUNT(expectedConfigs[i].vias)); ++j) {
265             if (!expectedConfigs[i].vias[j]) {
266                 REPORTER_ASSERT(reporter, configs[i]->getViaParts().count() == j);
267                 break;
268             }
269             REPORTER_ASSERT(reporter,
270                             configs[i]->getViaParts()[j].equals(expectedConfigs[i].vias[j]));
271         }
272     }
273 }
274 
DEF_TEST(ParseConfigs_ViaParsingExtendedForm,reporter)275 DEF_TEST(ParseConfigs_ViaParsingExtendedForm, reporter) {
276     SkCommandLineFlags::StringArray config1 = make_string_array({
277         "zz-qq-gpu(api=gles)",
278         "a-gpu(samples=1",
279         "abc-def-angle-gl(samples=1)",
280     });
281 
282     SkCommandLineConfigArray configs;
283     ParseConfigs(config1, &configs);
284     const struct {
285         const char* backend;
286         const char* vias[3];
287     } expectedConfigs[] = {
288 #if SK_SUPPORT_GPU
289         {"gpu", {"zz", "qq", nullptr}},
290 #else
291         {"gpu(api=gles)", {"zz", "qq", nullptr}},
292 #endif
293         {"gpu(samples=1", {"a", nullptr, nullptr}}, // This is not extended form, but via still
294                                                     // works as expected.
295         {"gl(samples=1)", {"abc", "def", "angle"}}  // This is not extended form.  Also
296                                                     // angle-gl is not a "backend" in this case.
297     };
298     for (int i = 0; i < config1.count(); ++i) {
299         REPORTER_ASSERT(reporter, configs[i]->getTag().equals(config1[i]));
300         REPORTER_ASSERT(reporter, configs[i]->getBackend().equals(expectedConfigs[i].backend));
301         for (int j = 0; j < static_cast<int>(SK_ARRAY_COUNT(expectedConfigs[i].vias)); ++j) {
302             if (!expectedConfigs[i].vias[j]) {
303                 REPORTER_ASSERT(reporter, configs[i]->getViaParts().count() ==
304                                 static_cast<int>(j));
305                 break;
306             }
307             REPORTER_ASSERT(reporter,
308                             configs[i]->getViaParts()[j].equals(expectedConfigs[i].vias[j]));
309         }
310     }
311 #if SK_SUPPORT_GPU
312     REPORTER_ASSERT(reporter, configs[0]->asConfigGpu());
313     REPORTER_ASSERT(reporter, !configs[1]->asConfigGpu());
314     REPORTER_ASSERT(reporter, !configs[2]->asConfigGpu());
315 #endif
316 }
317