1 // Copyright (c) 2012 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.
4
5 #include "gpu/config/gpu_test_config.h"
6
7 #include "base/logging.h"
8 #include "base/sys_info.h"
9 #include "gpu/config/gpu_info.h"
10 #include "gpu/config/gpu_info_collector.h"
11 #include "gpu/config/gpu_test_expectations_parser.h"
12
13 #if defined(OS_MACOSX)
14 #include "base/mac/mac_util.h"
15 #elif defined(OS_WIN)
16 #include "base/win/windows_version.h"
17 #endif
18
19 namespace gpu {
20
21 namespace {
22
GetCurrentOS()23 GPUTestConfig::OS GetCurrentOS() {
24 #if defined(OS_CHROMEOS)
25 return GPUTestConfig::kOsChromeOS;
26 #elif defined(OS_LINUX) || defined(OS_OPENBSD)
27 return GPUTestConfig::kOsLinux;
28 #elif defined(OS_WIN)
29 int32 major_version = 0;
30 int32 minor_version = 0;
31 int32 bugfix_version = 0;
32 base::SysInfo::OperatingSystemVersionNumbers(
33 &major_version, &minor_version, &bugfix_version);
34 if (major_version == 5)
35 return GPUTestConfig::kOsWinXP;
36 if (major_version == 6 && minor_version == 0)
37 return GPUTestConfig::kOsWinVista;
38 if (major_version == 6 && minor_version == 1)
39 return GPUTestConfig::kOsWin7;
40 if (major_version == 6 && (minor_version == 2 || minor_version == 3))
41 return GPUTestConfig::kOsWin8;
42 #elif defined(OS_MACOSX)
43 int32 major_version = 0;
44 int32 minor_version = 0;
45 int32 bugfix_version = 0;
46 base::SysInfo::OperatingSystemVersionNumbers(
47 &major_version, &minor_version, &bugfix_version);
48 if (major_version == 10) {
49 switch (minor_version) {
50 case 5:
51 return GPUTestConfig::kOsMacLeopard;
52 case 6:
53 return GPUTestConfig::kOsMacSnowLeopard;
54 case 7:
55 return GPUTestConfig::kOsMacLion;
56 case 8:
57 return GPUTestConfig::kOsMacMountainLion;
58 case 9:
59 return GPUTestConfig::kOsMacMavericks;
60 }
61 }
62 #elif defined(OS_ANDROID)
63 return GPUTestConfig::kOsAndroid;
64 #endif
65 return GPUTestConfig::kOsUnknown;
66 }
67
68 } // namespace anonymous
69
GPUTestConfig()70 GPUTestConfig::GPUTestConfig()
71 : validate_gpu_info_(true),
72 os_(kOsUnknown),
73 gpu_device_id_(0),
74 build_type_(kBuildTypeUnknown) {
75 }
76
~GPUTestConfig()77 GPUTestConfig::~GPUTestConfig() {
78 }
79
set_os(int32 os)80 void GPUTestConfig::set_os(int32 os) {
81 DCHECK_EQ(0, os & ~(kOsAndroid | kOsWin | kOsMac | kOsLinux | kOsChromeOS));
82 os_ = os;
83 }
84
AddGPUVendor(uint32 gpu_vendor)85 void GPUTestConfig::AddGPUVendor(uint32 gpu_vendor) {
86 DCHECK_NE(0u, gpu_vendor);
87 for (size_t i = 0; i < gpu_vendor_.size(); ++i)
88 DCHECK_NE(gpu_vendor_[i], gpu_vendor);
89 gpu_vendor_.push_back(gpu_vendor);
90 }
91
set_gpu_device_id(uint32 id)92 void GPUTestConfig::set_gpu_device_id(uint32 id) {
93 gpu_device_id_ = id;
94 }
95
set_build_type(int32 build_type)96 void GPUTestConfig::set_build_type(int32 build_type) {
97 DCHECK_EQ(0, build_type & ~(kBuildTypeRelease | kBuildTypeDebug));
98 build_type_ = build_type;
99 }
100
IsValid() const101 bool GPUTestConfig::IsValid() const {
102 if (!validate_gpu_info_)
103 return true;
104 if (gpu_device_id_ != 0 && (gpu_vendor_.size() != 1 || gpu_vendor_[0] == 0))
105 return false;
106 return true;
107 }
108
OverlapsWith(const GPUTestConfig & config) const109 bool GPUTestConfig::OverlapsWith(const GPUTestConfig& config) const {
110 DCHECK(IsValid());
111 DCHECK(config.IsValid());
112 if (config.os_ != kOsUnknown && os_ != kOsUnknown &&
113 (os_ & config.os_) == 0)
114 return false;
115 if (config.gpu_vendor_.size() > 0 && gpu_vendor_.size() > 0) {
116 bool shared = false;
117 for (size_t i = 0; i < config.gpu_vendor_.size() && !shared; ++i) {
118 for (size_t j = 0; j < gpu_vendor_.size(); ++j) {
119 if (config.gpu_vendor_[i] == gpu_vendor_[j]) {
120 shared = true;
121 break;
122 }
123 }
124 }
125 if (!shared)
126 return false;
127 }
128 if (config.gpu_device_id_ != 0 && gpu_device_id_ != 0 &&
129 gpu_device_id_ != config.gpu_device_id_)
130 return false;
131 if (config.build_type_ != kBuildTypeUnknown &&
132 build_type_ != kBuildTypeUnknown &&
133 (build_type_ & config.build_type_) == 0)
134 return false;
135 return true;
136 }
137
DisableGPUInfoValidation()138 void GPUTestConfig::DisableGPUInfoValidation() {
139 validate_gpu_info_ = false;
140 }
141
ClearGPUVendor()142 void GPUTestConfig::ClearGPUVendor() {
143 gpu_vendor_.clear();
144 }
145
~GPUTestBotConfig()146 GPUTestBotConfig::~GPUTestBotConfig() {
147 }
148
AddGPUVendor(uint32 gpu_vendor)149 void GPUTestBotConfig::AddGPUVendor(uint32 gpu_vendor) {
150 DCHECK_EQ(0u, GPUTestConfig::gpu_vendor().size());
151 GPUTestConfig::AddGPUVendor(gpu_vendor);
152 }
153
SetGPUInfo(const GPUInfo & gpu_info)154 bool GPUTestBotConfig::SetGPUInfo(const GPUInfo& gpu_info) {
155 DCHECK(validate_gpu_info_);
156 if (gpu_info.gpu.device_id == 0 || gpu_info.gpu.vendor_id == 0)
157 return false;
158 ClearGPUVendor();
159 AddGPUVendor(gpu_info.gpu.vendor_id);
160 set_gpu_device_id(gpu_info.gpu.device_id);
161 return true;
162 }
163
IsValid() const164 bool GPUTestBotConfig::IsValid() const {
165 switch (os()) {
166 case kOsWinXP:
167 case kOsWinVista:
168 case kOsWin7:
169 case kOsWin8:
170 case kOsMacLeopard:
171 case kOsMacSnowLeopard:
172 case kOsMacLion:
173 case kOsMacMountainLion:
174 case kOsMacMavericks:
175 case kOsLinux:
176 case kOsChromeOS:
177 case kOsAndroid:
178 break;
179 default:
180 return false;
181 }
182 if (validate_gpu_info_) {
183 if (gpu_vendor().size() != 1 || gpu_vendor()[0] == 0)
184 return false;
185 if (gpu_device_id() == 0)
186 return false;
187 }
188 switch (build_type()) {
189 case kBuildTypeRelease:
190 case kBuildTypeDebug:
191 break;
192 default:
193 return false;
194 }
195 return true;
196 }
197
Matches(const GPUTestConfig & config) const198 bool GPUTestBotConfig::Matches(const GPUTestConfig& config) const {
199 DCHECK(IsValid());
200 DCHECK(config.IsValid());
201 if (config.os() != kOsUnknown && (os() & config.os()) == 0)
202 return false;
203 if (config.gpu_vendor().size() > 0) {
204 bool contained = false;
205 for (size_t i = 0; i < config.gpu_vendor().size(); ++i) {
206 if (config.gpu_vendor()[i] == gpu_vendor()[0]) {
207 contained = true;
208 break;
209 }
210 }
211 if (!contained)
212 return false;
213 }
214 if (config.gpu_device_id() != 0 &&
215 gpu_device_id() != config.gpu_device_id())
216 return false;
217 if (config.build_type() != kBuildTypeUnknown &&
218 (build_type() & config.build_type()) == 0)
219 return false;
220 return true;
221 }
222
Matches(const std::string & config_data) const223 bool GPUTestBotConfig::Matches(const std::string& config_data) const {
224 GPUTestExpectationsParser parser;
225 GPUTestConfig config;
226
227 if (!parser.ParseConfig(config_data, &config))
228 return false;
229 return Matches(config);
230 }
231
LoadCurrentConfig(const GPUInfo * gpu_info)232 bool GPUTestBotConfig::LoadCurrentConfig(const GPUInfo* gpu_info) {
233 bool rt;
234 if (gpu_info == NULL) {
235 GPUInfo my_gpu_info;
236 CollectInfoResult result = CollectGpuID(
237 &my_gpu_info.gpu.vendor_id, &my_gpu_info.gpu.device_id);
238 if (result != kCollectInfoSuccess) {
239 LOG(ERROR) << "Fail to identify GPU";
240 DisableGPUInfoValidation();
241 rt = true;
242 } else {
243 rt = SetGPUInfo(my_gpu_info);
244 }
245 } else {
246 rt = SetGPUInfo(*gpu_info);
247 }
248 set_os(GetCurrentOS());
249 if (os() == kOsUnknown) {
250 LOG(ERROR) << "Unknown OS";
251 rt = false;
252 }
253 #if defined(NDEBUG)
254 set_build_type(kBuildTypeRelease);
255 #else
256 set_build_type(kBuildTypeDebug);
257 #endif
258 return rt;
259 }
260
261 // static
CurrentConfigMatches(const std::string & config_data)262 bool GPUTestBotConfig::CurrentConfigMatches(const std::string& config_data) {
263 GPUTestBotConfig my_config;
264 if (!my_config.LoadCurrentConfig(NULL))
265 return false;
266 return my_config.Matches(config_data);
267 }
268
269 // static
CurrentConfigMatches(const std::vector<std::string> & configs)270 bool GPUTestBotConfig::CurrentConfigMatches(
271 const std::vector<std::string>& configs) {
272 GPUTestBotConfig my_config;
273 if (!my_config.LoadCurrentConfig(NULL))
274 return false;
275 for (size_t i = 0 ; i < configs.size(); ++i) {
276 if (my_config.Matches(configs[i]))
277 return true;
278 }
279 return false;
280 }
281
282 // static
GpuBlacklistedOnBot()283 bool GPUTestBotConfig::GpuBlacklistedOnBot() {
284 #if defined(OS_MACOSX)
285 // Blacklist rule #81 disables all Gpu acceleration on Mac < 10.8 bots.
286 if (CurrentConfigMatches("MAC VMWARE") && base::mac::IsOSLionOrEarlier()) {
287 return true;
288 }
289 #elif defined(OS_WIN)
290 // Blacklist rule #79 disables all Gpu acceleration before Windows 7.
291 if (base::win::GetVersion() <= base::win::VERSION_VISTA) {
292 return true;
293 }
294 #endif
295 return false;
296 }
297
298 } // namespace gpu
299
300