1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 #include "base/memory/scoped_ptr.h"
8 #include "gpu/config/gpu_control_list.h"
9 #include "gpu/config/gpu_info.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 const char kOsVersion
[] = "10.6.4";
13 const uint32 kIntelVendorId
= 0x8086;
14 const uint32 kNvidiaVendorId
= 0x10de;
15 const uint32 kAmdVendorId
= 0x10de;
17 #define LONG_STRING_CONST(...) #__VA_ARGS__
19 #define EXPECT_EMPTY_SET(feature_set) EXPECT_EQ(0u, feature_set.size())
20 #define EXPECT_SINGLE_FEATURE(feature_set, feature) \
21 EXPECT_TRUE(feature_set.size() == 1 && feature_set.count(feature) == 1)
25 enum TestFeatureType
{
27 TEST_FEATURE_1
= 1 << 2,
28 TEST_FEATURE_2
= 1 << 3,
31 class GpuControlListTest
: public testing::Test
{
33 GpuControlListTest() { }
35 virtual ~GpuControlListTest() { }
37 const GPUInfo
& gpu_info() const {
41 GpuControlList
* Create() {
42 GpuControlList
* rt
= new GpuControlList();
43 rt
->AddSupportedFeature("test_feature_0", TEST_FEATURE_0
);
44 rt
->AddSupportedFeature("test_feature_1", TEST_FEATURE_1
);
45 rt
->AddSupportedFeature("test_feature_2", TEST_FEATURE_2
);
50 virtual void SetUp() {
51 gpu_info_
.gpu
.vendor_id
= kNvidiaVendorId
;
52 gpu_info_
.gpu
.device_id
= 0x0640;
53 gpu_info_
.driver_vendor
= "NVIDIA";
54 gpu_info_
.driver_version
= "1.6.18";
55 gpu_info_
.driver_date
= "7-14-2009";
56 gpu_info_
.machine_model_name
= "MacBookPro";
57 gpu_info_
.machine_model_version
= "7.1";
58 gpu_info_
.gl_vendor
= "NVIDIA Corporation";
59 gpu_info_
.gl_renderer
= "NVIDIA GeForce GT 120 OpenGL Engine";
60 gpu_info_
.performance_stats
.graphics
= 5.0;
61 gpu_info_
.performance_stats
.gaming
= 5.0;
62 gpu_info_
.performance_stats
.overall
= 5.0;
65 virtual void TearDown() {
72 TEST_F(GpuControlListTest
, DefaultControlListSettings
) {
73 scoped_ptr
<GpuControlList
> control_list(Create());
74 // Default control list settings: all feature are allowed.
75 std::set
<int> features
= control_list
->MakeDecision(
76 GpuControlList::kOsMacosx
, kOsVersion
, gpu_info());
77 EXPECT_EMPTY_SET(features
);
80 TEST_F(GpuControlListTest
, EmptyControlList
) {
81 // Empty list: all features are allowed.
82 const std::string empty_list_json
= LONG_STRING_CONST(
84 "name": "gpu control list",
90 scoped_ptr
<GpuControlList
> control_list(Create());
92 EXPECT_TRUE(control_list
->LoadList(empty_list_json
,
93 GpuControlList::kAllOs
));
94 EXPECT_EQ("2.5", control_list
->version());
95 std::set
<int> features
= control_list
->MakeDecision(
96 GpuControlList::kOsMacosx
, kOsVersion
, gpu_info());
97 EXPECT_EMPTY_SET(features
);
100 TEST_F(GpuControlListTest
, DetailedEntryAndInvalidJson
) {
102 const std::string exact_list_json
= LONG_STRING_CONST(
104 "name": "gpu control list",
116 "vendor_id": "0x10de",
117 "device_id": ["0x0640"],
129 scoped_ptr
<GpuControlList
> control_list(Create());
131 EXPECT_TRUE(control_list
->LoadList(exact_list_json
, GpuControlList::kAllOs
));
132 std::set
<int> features
= control_list
->MakeDecision(
133 GpuControlList::kOsMacosx
, kOsVersion
, gpu_info());
134 EXPECT_SINGLE_FEATURE(features
, TEST_FEATURE_0
);
136 // Invalid json input should not change the current control_list settings.
137 const std::string invalid_json
= "invalid";
139 EXPECT_FALSE(control_list
->LoadList(invalid_json
, GpuControlList::kAllOs
));
140 features
= control_list
->MakeDecision(
141 GpuControlList::kOsMacosx
, kOsVersion
, gpu_info());
142 EXPECT_SINGLE_FEATURE(features
, TEST_FEATURE_0
);
143 std::vector
<uint32
> entries
;
144 control_list
->GetDecisionEntries(&entries
, false);
145 ASSERT_EQ(1u, entries
.size());
146 EXPECT_EQ(5u, entries
[0]);
147 EXPECT_EQ(5u, control_list
->max_entry_id());
150 TEST_F(GpuControlListTest
, VendorOnAllOsEntry
) {
151 // ControlList a vendor on all OS.
152 const std::string vendor_json
= LONG_STRING_CONST(
154 "name": "gpu control list",
159 "vendor_id": "0x10de",
167 scoped_ptr
<GpuControlList
> control_list(Create());
169 // ControlList entries won't be filtered to the current OS only upon loading.
170 EXPECT_TRUE(control_list
->LoadList(vendor_json
, GpuControlList::kAllOs
));
171 std::set
<int> features
= control_list
->MakeDecision(
172 GpuControlList::kOsMacosx
, kOsVersion
, gpu_info());
173 EXPECT_SINGLE_FEATURE(features
, TEST_FEATURE_0
);
174 features
= control_list
->MakeDecision(
175 GpuControlList::kOsWin
, kOsVersion
, gpu_info());
176 EXPECT_SINGLE_FEATURE(features
, TEST_FEATURE_0
);
177 features
= control_list
->MakeDecision(
178 GpuControlList::kOsLinux
, kOsVersion
, gpu_info());
179 EXPECT_SINGLE_FEATURE(features
, TEST_FEATURE_0
);
180 #if defined(OS_WIN) || defined(OS_LINUX) || defined(OS_MACOSX) || \
182 // ControlList entries will be filtered to the current OS only upon loading.
183 EXPECT_TRUE(control_list
->LoadList(
184 vendor_json
, GpuControlList::kCurrentOsOnly
));
185 features
= control_list
->MakeDecision(
186 GpuControlList::kOsMacosx
, kOsVersion
, gpu_info());
187 EXPECT_SINGLE_FEATURE(features
, TEST_FEATURE_0
);
188 features
= control_list
->MakeDecision(
189 GpuControlList::kOsWin
, kOsVersion
, gpu_info());
190 EXPECT_SINGLE_FEATURE(features
, TEST_FEATURE_0
);
191 features
= control_list
->MakeDecision(
192 GpuControlList::kOsLinux
, kOsVersion
, gpu_info());
193 EXPECT_SINGLE_FEATURE(features
, TEST_FEATURE_0
);
197 TEST_F(GpuControlListTest
, UnknownField
) {
198 const std::string unknown_field_json
= LONG_STRING_CONST(
200 "name": "gpu control list",
219 scoped_ptr
<GpuControlList
> control_list(Create());
221 EXPECT_FALSE(control_list
->LoadList(
222 unknown_field_json
, GpuControlList::kAllOs
));
225 TEST_F(GpuControlListTest
, UnknownExceptionField
) {
226 const std::string unknown_exception_field_json
= LONG_STRING_CONST(
228 "name": "gpu control list",
258 scoped_ptr
<GpuControlList
> control_list(Create());
260 EXPECT_FALSE(control_list
->LoadList(
261 unknown_exception_field_json
, GpuControlList::kAllOs
));
264 TEST_F(GpuControlListTest
, DisabledEntry
) {
265 const std::string disabled_json
= LONG_STRING_CONST(
267 "name": "gpu control list",
280 scoped_ptr
<GpuControlList
> control_list(Create());
281 EXPECT_TRUE(control_list
->LoadList(disabled_json
, GpuControlList::kAllOs
));
282 std::set
<int> features
= control_list
->MakeDecision(
283 GpuControlList::kOsWin
, kOsVersion
, gpu_info());
284 EXPECT_EMPTY_SET(features
);
285 std::vector
<uint32
> flag_entries
;
286 control_list
->GetDecisionEntries(&flag_entries
, false);
287 EXPECT_EQ(0u, flag_entries
.size());
288 control_list
->GetDecisionEntries(&flag_entries
, true);
289 EXPECT_EQ(1u, flag_entries
.size());
292 TEST_F(GpuControlListTest
, NeedsMoreInfo
) {
293 const std::string json
= LONG_STRING_CONST(
295 "name": "gpu control list",
303 "vendor_id": "0x10de",
316 gpu_info
.gpu
.vendor_id
= kNvidiaVendorId
;
318 scoped_ptr
<GpuControlList
> control_list(Create());
319 EXPECT_TRUE(control_list
->LoadList(json
, GpuControlList::kAllOs
));
321 std::set
<int> features
= control_list
->MakeDecision(
322 GpuControlList::kOsWin
, kOsVersion
, gpu_info
);
323 EXPECT_EMPTY_SET(features
);
324 EXPECT_TRUE(control_list
->needs_more_info());
325 std::vector
<uint32
> decision_entries
;
326 control_list
->GetDecisionEntries(&decision_entries
, false);
327 EXPECT_EQ(0u, decision_entries
.size());
329 gpu_info
.driver_version
= "11";
330 features
= control_list
->MakeDecision(
331 GpuControlList::kOsWin
, kOsVersion
, gpu_info
);
332 EXPECT_SINGLE_FEATURE(features
, TEST_FEATURE_0
);
333 EXPECT_FALSE(control_list
->needs_more_info());
334 control_list
->GetDecisionEntries(&decision_entries
, false);
335 EXPECT_EQ(1u, decision_entries
.size());
338 TEST_F(GpuControlListTest
, NeedsMoreInfoForExceptions
) {
339 const std::string json
= LONG_STRING_CONST(
341 "name": "gpu control list",
349 "vendor_id": "0x8086",
352 "gl_renderer": ".*mesa.*"
363 gpu_info
.gpu
.vendor_id
= kIntelVendorId
;
365 scoped_ptr
<GpuControlList
> control_list(Create());
366 EXPECT_TRUE(control_list
->LoadList(json
, GpuControlList::kAllOs
));
368 // The case this entry does not apply.
369 std::set
<int> features
= control_list
->MakeDecision(
370 GpuControlList::kOsMacosx
, kOsVersion
, gpu_info
);
371 EXPECT_EMPTY_SET(features
);
372 EXPECT_FALSE(control_list
->needs_more_info());
374 // The case this entry might apply, but need more info.
375 features
= control_list
->MakeDecision(
376 GpuControlList::kOsLinux
, kOsVersion
, gpu_info
);
377 EXPECT_EMPTY_SET(features
);
378 EXPECT_TRUE(control_list
->needs_more_info());
380 // The case we have full info, and the exception applies (so the entry
382 gpu_info
.gl_renderer
= "mesa";
383 features
= control_list
->MakeDecision(
384 GpuControlList::kOsLinux
, kOsVersion
, gpu_info
);
385 EXPECT_EMPTY_SET(features
);
386 EXPECT_FALSE(control_list
->needs_more_info());
388 // The case we have full info, and this entry applies.
389 gpu_info
.gl_renderer
= "my renderer";
390 features
= control_list
->MakeDecision(GpuControlList::kOsLinux
, kOsVersion
,
392 EXPECT_SINGLE_FEATURE(features
, TEST_FEATURE_0
);
393 EXPECT_FALSE(control_list
->needs_more_info());
396 TEST_F(GpuControlListTest
, IgnorableEntries
) {
397 // If an entry will not change the control_list decisions, then it should not
398 // trigger the needs_more_info flag.
399 const std::string json
= LONG_STRING_CONST(
401 "name": "gpu control list",
409 "vendor_id": "0x8086",
419 "vendor_id": "0x8086",
432 gpu_info
.gpu
.vendor_id
= kIntelVendorId
;
434 scoped_ptr
<GpuControlList
> control_list(Create());
435 EXPECT_TRUE(control_list
->LoadList(json
, GpuControlList::kAllOs
));
436 std::set
<int> features
= control_list
->MakeDecision(
437 GpuControlList::kOsLinux
, kOsVersion
, gpu_info
);
438 EXPECT_SINGLE_FEATURE(features
, TEST_FEATURE_0
);
439 EXPECT_FALSE(control_list
->needs_more_info());
442 TEST_F(GpuControlListTest
, ExceptionWithoutVendorId
) {
443 const std::string json
= LONG_STRING_CONST(
445 "name": "gpu control list",
453 "vendor_id": "0x8086",
456 "device_id": ["0x2a06"],
463 "device_id": ["0x2a02"],
478 gpu_info
.gpu
.vendor_id
= kIntelVendorId
;
479 gpu_info
.gpu
.device_id
= 0x2a02;
480 gpu_info
.driver_version
= "9.1";
482 scoped_ptr
<GpuControlList
> control_list(Create());
483 EXPECT_TRUE(control_list
->LoadList(json
, GpuControlList::kAllOs
));
485 std::set
<int> features
= control_list
->MakeDecision(
486 GpuControlList::kOsLinux
, kOsVersion
, gpu_info
);
487 EXPECT_EMPTY_SET(features
);
489 gpu_info
.driver_version
= "9.0";
490 features
= control_list
->MakeDecision(
491 GpuControlList::kOsLinux
, kOsVersion
, gpu_info
);
492 EXPECT_SINGLE_FEATURE(features
, TEST_FEATURE_0
);
495 TEST_F(GpuControlListTest
, AMDSwitchable
) {
497 gpu_info
.amd_switchable
= true;
498 gpu_info
.gpu
.vendor_id
= kAmdVendorId
;
499 gpu_info
.gpu
.device_id
= 0x6760;
500 GPUInfo::GPUDevice integrated_gpu
;
501 integrated_gpu
.vendor_id
= kIntelVendorId
;
502 integrated_gpu
.device_id
= 0x0116;
503 gpu_info
.secondary_gpus
.push_back(integrated_gpu
);
505 { // amd_switchable_discrete entry
506 const std::string json
= LONG_STRING_CONST(
508 "name": "gpu control list",
516 "multi_gpu_style": "amd_switchable_discrete",
525 scoped_ptr
<GpuControlList
> control_list(Create());
526 EXPECT_TRUE(control_list
->LoadList(json
, GpuControlList::kAllOs
));
528 // Integrated GPU is active
529 gpu_info
.gpu
.active
= false;
530 gpu_info
.secondary_gpus
[0].active
= true;
531 std::set
<int> features
= control_list
->MakeDecision(
532 GpuControlList::kOsWin
, kOsVersion
, gpu_info
);
533 EXPECT_EMPTY_SET(features
);
535 // Discrete GPU is active
536 gpu_info
.gpu
.active
= true;
537 gpu_info
.secondary_gpus
[0].active
= false;
538 features
= control_list
->MakeDecision(
539 GpuControlList::kOsWin
, kOsVersion
, gpu_info
);
540 EXPECT_SINGLE_FEATURE(features
, TEST_FEATURE_0
);
543 { // amd_switchable_integrated entry
544 const std::string json
= LONG_STRING_CONST(
546 "name": "gpu control list",
554 "multi_gpu_style": "amd_switchable_integrated",
563 scoped_ptr
<GpuControlList
> control_list(Create());
564 EXPECT_TRUE(control_list
->LoadList(json
, GpuControlList::kAllOs
));
566 // Discrete GPU is active
567 gpu_info
.gpu
.active
= true;
568 gpu_info
.secondary_gpus
[0].active
= false;
569 std::set
<int> features
= control_list
->MakeDecision(
570 GpuControlList::kOsWin
, kOsVersion
, gpu_info
);
571 EXPECT_EMPTY_SET(features
);
573 // Integrated GPU is active
574 gpu_info
.gpu
.active
= false;
575 gpu_info
.secondary_gpus
[0].active
= true;
576 features
= control_list
->MakeDecision(
577 GpuControlList::kOsWin
, kOsVersion
, gpu_info
);
578 EXPECT_SINGLE_FEATURE(features
, TEST_FEATURE_0
);
580 // For non AMD switchable
581 gpu_info
.amd_switchable
= false;
582 features
= control_list
->MakeDecision(
583 GpuControlList::kOsWin
, kOsVersion
, gpu_info
);
584 EXPECT_EMPTY_SET(features
);