blob: 18ca6666657af42e125259e8e97f0f06d916b1f5 [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#include "Test.h"
10#include <initializer_list>
11
12namespace {
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.
21SkCommandLineFlags::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}
29DEF_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);
brianosmand93c1202016-03-10 07:49:08 -080046 REPORTER_ASSERT(reporter, configs[0]->asConfigGpu()->getColorType() == kN32_SkColorType);
47 REPORTER_ASSERT(reporter, configs[0]->asConfigGpu()->getProfileType()
48 == kLinear_SkColorProfileType);
kkinnunen3e980c32015-12-23 01:33:00 -080049#endif
50}
51
52DEF_TEST(ParseConfigs_OutParam, reporter) {
53 // Clears the out parameter.
54 SkCommandLineFlags::StringArray config1 = make_string_array({"gpu"});
55 SkCommandLineConfigArray configs;
56 ParseConfigs(config1, &configs);
57 REPORTER_ASSERT(reporter, configs.count() == 1);
58 REPORTER_ASSERT(reporter, configs[0]->getTag().equals("gpu"));
59 SkCommandLineFlags::StringArray config2 = make_string_array({"8888"});
60 ParseConfigs(config2, &configs);
61 REPORTER_ASSERT(reporter, configs.count() == 1);
62 REPORTER_ASSERT(reporter, configs[0]->getTag().equals("8888"));
63}
64
65DEF_TEST(ParseConfigs_DefaultConfigs, reporter) {
66 // Parses all default configs and returns correct "tag".
67
68 SkCommandLineFlags::StringArray config1 = make_string_array({
69 "565", "8888", "debug", "gpu", "gpudebug", "gpudft", "gpunull", "msaa16", "msaa4",
70 "nonrendering", "null", "nullgpu", "nvprmsaa16", "nvprmsaa4", "pdf", "pdf_poppler",
brianosmand93c1202016-03-10 07:49:08 -080071 "skp", "svg", "xps", "angle", "angle-gl", "commandbuffer", "mesa", "hwui",
brianosman8e478812016-03-10 09:47:56 -080072 "gpuf16", "gpusrgb"
kkinnunen3e980c32015-12-23 01:33:00 -080073 });
74
75 SkCommandLineConfigArray configs;
76 ParseConfigs(config1, &configs);
77
78 REPORTER_ASSERT(reporter, configs.count() == config1.count());
79 for (int i = 0; i < config1.count(); ++i) {
80 REPORTER_ASSERT(reporter, configs[i]->getTag().equals(config1[i]));
81 REPORTER_ASSERT(reporter, configs[i]->getViaParts().count() == 0);
82 }
83#if SK_SUPPORT_GPU
84 REPORTER_ASSERT(reporter, !configs[0]->asConfigGpu());
85 REPORTER_ASSERT(reporter, !configs[1]->asConfigGpu());
86 REPORTER_ASSERT(reporter, configs[2]->asConfigGpu());
87 REPORTER_ASSERT(reporter, configs[3]->asConfigGpu());
88 REPORTER_ASSERT(reporter, configs[4]->asConfigGpu());
89 REPORTER_ASSERT(reporter, configs[5]->asConfigGpu()->getUseDIText());
90 REPORTER_ASSERT(reporter, configs[6]->asConfigGpu());
91 REPORTER_ASSERT(reporter, configs[7]->asConfigGpu()->getSamples() == 16);
92 REPORTER_ASSERT(reporter, configs[8]->asConfigGpu()->getSamples() == 4);
93 REPORTER_ASSERT(reporter, !configs[9]->asConfigGpu());
94 REPORTER_ASSERT(reporter, !configs[10]->asConfigGpu());
95 REPORTER_ASSERT(reporter, configs[11]->asConfigGpu());
96 REPORTER_ASSERT(reporter, configs[12]->asConfigGpu()->getSamples() == 16);
97 REPORTER_ASSERT(reporter, configs[12]->asConfigGpu()->getUseNVPR());
kkinnunene3c2f802015-12-29 08:57:32 -080098 REPORTER_ASSERT(reporter, configs[12]->asConfigGpu()->getUseDIText());
kkinnunen3e980c32015-12-23 01:33:00 -080099 REPORTER_ASSERT(reporter, configs[13]->asConfigGpu()->getSamples() == 4);
100 REPORTER_ASSERT(reporter, configs[13]->asConfigGpu()->getUseNVPR());
kkinnunene3c2f802015-12-29 08:57:32 -0800101 REPORTER_ASSERT(reporter, configs[13]->asConfigGpu()->getUseDIText());
kkinnunen3e980c32015-12-23 01:33:00 -0800102 REPORTER_ASSERT(reporter, !configs[14]->asConfigGpu());
103 REPORTER_ASSERT(reporter, !configs[15]->asConfigGpu());
104 REPORTER_ASSERT(reporter, !configs[16]->asConfigGpu());
105 REPORTER_ASSERT(reporter, !configs[17]->asConfigGpu());
106 REPORTER_ASSERT(reporter, !configs[18]->asConfigGpu());
107 REPORTER_ASSERT(reporter, !configs[23]->asConfigGpu());
brianosmand93c1202016-03-10 07:49:08 -0800108 REPORTER_ASSERT(reporter, configs[24]->asConfigGpu()->getColorType()
109 == kRGBA_F16_SkColorType);
110 REPORTER_ASSERT(reporter, configs[24]->asConfigGpu()->getProfileType()
111 == kLinear_SkColorProfileType);
112 REPORTER_ASSERT(reporter, configs[25]->asConfigGpu()->getColorType()
113 == kN32_SkColorType);
114 REPORTER_ASSERT(reporter, configs[25]->asConfigGpu()->getProfileType()
115 == kSRGB_SkColorProfileType);
kkinnunen3e980c32015-12-23 01:33:00 -0800116#if SK_ANGLE
117#ifdef SK_BUILD_FOR_WIN
118 REPORTER_ASSERT(reporter, configs[19]->asConfigGpu());
119#else
120 REPORTER_ASSERT(reporter, !configs[19]->asConfigGpu());
121#endif
122 REPORTER_ASSERT(reporter, configs[20]->asConfigGpu());
123#else
124 REPORTER_ASSERT(reporter, !configs[19]->asConfigGpu());
125 REPORTER_ASSERT(reporter, !configs[20]->asConfigGpu());
126#endif
127#if SK_COMMAND_BUFFER
128 REPORTER_ASSERT(reporter, configs[21]->asConfigGpu());
129#else
130 REPORTER_ASSERT(reporter, !configs[21]->asConfigGpu());
131#endif
132#if SK_MESA
133 REPORTER_ASSERT(reporter, configs[22]->asConfigGpu());
134#else
135 REPORTER_ASSERT(reporter, !configs[22]->asConfigGpu());
136#endif
137#endif
138}
139
140DEF_TEST(ParseConfigs_ExtendedGpuConfigsCorrect, reporter) {
141 SkCommandLineFlags::StringArray config1 = make_string_array({
kkinnunene3c2f802015-12-29 08:57:32 -0800142 "gpu(nvpr=true,dit=false)",
kkinnunen3e980c32015-12-23 01:33:00 -0800143 "gpu(api=angle)",
144 "gpu(api=angle-gl)",
145 "gpu(api=mesa,samples=77)",
146 "gpu(dit=true,api=commandbuffer)",
147 "gpu()",
148 "gpu(api=gles)"
149 });
150
151 SkCommandLineConfigArray configs;
152 ParseConfigs(config1, &configs);
153 REPORTER_ASSERT(reporter, configs.count() == config1.count());
154 for (int i = 0; i < config1.count(); ++i) {
155 REPORTER_ASSERT(reporter, configs[i]->getTag().equals(config1[i]));
156 }
157#if SK_SUPPORT_GPU
158 REPORTER_ASSERT(reporter, configs[0]->asConfigGpu()->getContextType() ==
159 GrContextFactory::kNative_GLContextType);
160 REPORTER_ASSERT(reporter, configs[0]->asConfigGpu()->getUseNVPR());
kkinnunene3c2f802015-12-29 08:57:32 -0800161 REPORTER_ASSERT(reporter, !configs[0]->asConfigGpu()->getUseDIText());
kkinnunen3e980c32015-12-23 01:33:00 -0800162 REPORTER_ASSERT(reporter, configs[0]->asConfigGpu()->getSamples() == 0);
163#if SK_ANGLE
164#ifdef SK_BUILD_FOR_WIN
165 REPORTER_ASSERT(reporter, configs[1]->asConfigGpu()->getContextType() ==
166 GrContextFactory::kANGLE_GLContextType);
167#else
168 REPORTER_ASSERT(reporter, !configs[1]->asConfigGpu());
169#endif
170 REPORTER_ASSERT(reporter, configs[2]->asConfigGpu()->getContextType() ==
171 GrContextFactory::kANGLE_GL_GLContextType);
172#else
173 REPORTER_ASSERT(reporter, !configs[1]->asConfigGpu());
174 REPORTER_ASSERT(reporter, !configs[2]->asConfigGpu());
175#endif
176#if SK_MESA
177 REPORTER_ASSERT(reporter, configs[3]->asConfigGpu()->getContextType() ==
178 GrContextFactory::kMESA_GLContextType);
179#else
180 REPORTER_ASSERT(reporter, !configs[3]->asConfigGpu());
181#endif
182#if SK_COMMAND_BUFFER
183 REPORTER_ASSERT(reporter, configs[4]->asConfigGpu()->getContextType() ==
kkinnunenf655e932016-03-03 07:39:48 -0800184 GrContextFactory::kCommandBuffer_GLContextType);
kkinnunen3e980c32015-12-23 01:33:00 -0800185
186#else
187 REPORTER_ASSERT(reporter, !configs[4]->asConfigGpu());
188#endif
189 REPORTER_ASSERT(reporter, configs[5]->asConfigGpu()->getContextType() ==
190 GrContextFactory::kNative_GLContextType);
191 REPORTER_ASSERT(reporter, !configs[5]->asConfigGpu()->getUseNVPR());
192 REPORTER_ASSERT(reporter, !configs[5]->asConfigGpu()->getUseDIText());
193 REPORTER_ASSERT(reporter, configs[5]->asConfigGpu()->getSamples() == 0);
194 REPORTER_ASSERT(reporter, configs[6]->asConfigGpu()->getContextType() ==
195 GrContextFactory::kGLES_GLContextType);
196 REPORTER_ASSERT(reporter, !configs[6]->asConfigGpu()->getUseNVPR());
197 REPORTER_ASSERT(reporter, !configs[6]->asConfigGpu()->getUseDIText());
198 REPORTER_ASSERT(reporter, configs[6]->asConfigGpu()->getSamples() == 0);
199
200#endif
201}
202
203DEF_TEST(ParseConfigs_ExtendedGpuConfigsIncorrect, reporter) {
204 SkCommandLineFlags::StringArray config1 = make_string_array({
205 "gpu(nvpr=1)", // Number as bool.
206 "gpu(api=gl,)", // Trailing in comma.
207 "gpu(api=angle-glu)", // Unknown api.
208 "gpu(api=,samples=0)", // Empty api.
209 "gpu(samples=true)", // Value true as a number.
210 "gpu(samples=0,samples=0)", // Duplicate option key.
211 "gpu(,samples=0)", // Leading comma.
212 "gpu(samples=54", // Missing closing parenthesis.
213 ",,",
214 "gpu(", // Missing parenthesis.
215 "samples=54" // No backend.
216 "gpu(nvpr=true )", // Space.
217 });
218
219 SkCommandLineConfigArray configs;
220 ParseConfigs(config1, &configs);
221 REPORTER_ASSERT(reporter, configs.count() == config1.count());
222 for (int i = 0; i < config1.count(); ++i) {
223 REPORTER_ASSERT(reporter, configs[i]->getTag().equals(config1[i]));
224 REPORTER_ASSERT(reporter, configs[i]->getBackend().equals(config1[i]));
225#if SK_SUPPORT_GPU
226 REPORTER_ASSERT(reporter, !configs[i]->asConfigGpu());
227#endif
228 }
229}
230
231
232DEF_TEST(ParseConfigs_ExtendedGpuConfigsSurprises, reporter) {
233 // These just list explicitly some properties of the system.
234 SkCommandLineFlags::StringArray config1 = make_string_array({
235 // Options are not canonized -> two same configs have a different tag.
236 "gpu(nvpr=true,dit=true)", "gpu(dit=true,nvpr=true)",
237 // API native is alias for gl or gles, but it's not canonized -> different tag.
238 "gpu(api=native)", "gpu(api=gl)", "gpu(api=gles)", ""
239 // Default values are not canonized -> different tag.
240 "gpu", "gpu()", "gpu(samples=0)", "gpu(api=native,samples=0)"
241 });
242 SkCommandLineConfigArray configs;
243 ParseConfigs(config1, &configs);
244 REPORTER_ASSERT(reporter, configs.count() == config1.count());
245 for (int i = 0; i < config1.count(); ++i) {
246 REPORTER_ASSERT(reporter, configs[i]->getTag().equals(config1[i]));
247#if SK_SUPPORT_GPU
248 REPORTER_ASSERT(reporter, configs[i]->getBackend().equals("gpu"));
249 REPORTER_ASSERT(reporter, configs[i]->asConfigGpu());
250#else
251 REPORTER_ASSERT(reporter, configs[i]->getBackend().equals(config1[i]));
252#endif
253 }
254}
255DEF_TEST(ParseConfigs_ViaParsing, reporter) {
256 SkCommandLineFlags::StringArray config1 = make_string_array({
257 "a-b-c-8888",
258 "zz-qq-gpu",
259 "a-angle-gl"
260 });
261
262 SkCommandLineConfigArray configs;
263 ParseConfigs(config1, &configs);
264 const struct {
265 const char* backend;
266 const char* vias[3];
267 } expectedConfigs[] = {
268 {"8888", {"a", "b", "c"}},
269 {"gpu", {"zz", "qq", nullptr}},
270 {"angle-gl", {"a", nullptr, nullptr}} // The angle-gl tag is only tag that contains
271 // hyphen.
272 };
273 for (int i = 0; i < config1.count(); ++i) {
274 REPORTER_ASSERT(reporter, configs[i]->getTag().equals(config1[i]));
275 REPORTER_ASSERT(reporter, configs[i]->getBackend().equals(expectedConfigs[i].backend));
276 for (int j = 0; j < static_cast<int>(SK_ARRAY_COUNT(expectedConfigs[i].vias)); ++j) {
277 if (!expectedConfigs[i].vias[j]) {
278 REPORTER_ASSERT(reporter, configs[i]->getViaParts().count() == j);
279 break;
280 }
281 REPORTER_ASSERT(reporter,
282 configs[i]->getViaParts()[j].equals(expectedConfigs[i].vias[j]));
283 }
284 }
285}
286
287DEF_TEST(ParseConfigs_ViaParsingExtendedForm, reporter) {
288 SkCommandLineFlags::StringArray config1 = make_string_array({
289 "zz-qq-gpu(api=gles)",
290 "a-gpu(samples=1",
291 "abc-def-angle-gl(samples=1)",
292 });
293
294 SkCommandLineConfigArray configs;
295 ParseConfigs(config1, &configs);
296 const struct {
297 const char* backend;
298 const char* vias[3];
299 } expectedConfigs[] = {
300#if SK_SUPPORT_GPU
301 {"gpu", {"zz", "qq", nullptr}},
302#else
303 {"gpu(api=gles)", {"zz", "qq", nullptr}},
304#endif
305 {"gpu(samples=1", {"a", nullptr, nullptr}}, // This is not extended form, but via still
306 // works as expected.
307 {"gl(samples=1)", {"abc", "def", "angle"}} // This is not extended form. Also
308 // angle-gl is not a "backend" in this case.
309 };
310 for (int i = 0; i < config1.count(); ++i) {
311 REPORTER_ASSERT(reporter, configs[i]->getTag().equals(config1[i]));
312 REPORTER_ASSERT(reporter, configs[i]->getBackend().equals(expectedConfigs[i].backend));
313 for (int j = 0; j < static_cast<int>(SK_ARRAY_COUNT(expectedConfigs[i].vias)); ++j) {
314 if (!expectedConfigs[i].vias[j]) {
315 REPORTER_ASSERT(reporter, configs[i]->getViaParts().count() ==
316 static_cast<int>(j));
317 break;
318 }
319 REPORTER_ASSERT(reporter,
320 configs[i]->getViaParts()[j].equals(expectedConfigs[i].vias[j]));
321 }
322 }
323#if SK_SUPPORT_GPU
324 REPORTER_ASSERT(reporter, configs[0]->asConfigGpu());
325 REPORTER_ASSERT(reporter, !configs[1]->asConfigGpu());
326 REPORTER_ASSERT(reporter, !configs[2]->asConfigGpu());
327#endif
328}