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. 4 5 #ifndef GPU_CONFIG_GPU_CONTROL_LIST_H_ 6 #define GPU_CONFIG_GPU_CONTROL_LIST_H_ 7 8 #include <set> 9 #include <string> 10 #include <vector> 11 12 #include "base/basictypes.h" 13 #include "base/containers/hash_tables.h" 14 #include "base/gtest_prod_util.h" 15 #include "base/memory/ref_counted.h" 16 #include "base/memory/scoped_ptr.h" 17 #include "base/values.h" 18 #include "build/build_config.h" 19 #include "gpu/gpu_export.h" 20 21 namespace gpu { 22 struct GPUInfo; 23 24 class GPU_EXPORT GpuControlList { 25 public: 26 enum OsType { 27 kOsLinux, 28 kOsMacosx, 29 kOsWin, 30 kOsChromeOS, 31 kOsAndroid, 32 kOsAny, 33 kOsUnknown 34 }; 35 36 enum OsFilter { 37 // In loading, ignore all entries that belong to other OS. 38 kCurrentOsOnly, 39 // In loading, keep all entries. This is for testing only. 40 kAllOs 41 }; 42 43 GpuControlList(); 44 virtual ~GpuControlList(); 45 46 // Loads control list information from a json file. 47 // If failed, the current GpuControlList is un-touched. 48 bool LoadList(const std::string& json_context, OsFilter os_filter); 49 50 // Collects system information and combines them with gpu_info and control 51 // list information to decide which entries are applied to the current 52 // system and returns the union of features specified in each entry. 53 // If os is kOsAny, use the current OS; if os_version is empty, use the 54 // current OS version. 55 std::set<int> MakeDecision( 56 OsType os, std::string os_version, const GPUInfo& gpu_info); 57 58 // Collects the active entries from the last MakeDecision() call. 59 // If disabled set to true, return entries that are disabled; otherwise, 60 // return enabled entries. 61 void GetDecisionEntries(std::vector<uint32>* entry_ids, 62 bool disabled) const; 63 64 // Returns the description and bugs from active entries from the last 65 // MakeDecision() call. 66 // 67 // Each problems has: 68 // { 69 // "description": "Your GPU is too old", 70 // "crBugs": [1234], 71 // "webkitBugs": [] 72 // } 73 void GetReasons( 74 base::ListValue* problem_list, const std::string& tag) const; 75 76 // Return the largest entry id. This is used for histogramming. 77 uint32 max_entry_id() const; 78 79 // Returns the version of the control list. 80 std::string version() const; 81 82 // Check if we need more gpu info to make the decisions. 83 // This is computed from the last MakeDecision() call. 84 // If yes, we should create a gl context and do a full gpu info collection. 85 bool needs_more_info() const { return needs_more_info_; } 86 87 // Returns the number of entries. This is only for tests. 88 size_t num_entries() const; 89 90 // Register a feature to FeatureMap - used to construct a GpuControlList. 91 void AddSupportedFeature(const std::string& feature_name, int feature_id); 92 // Register whether "all" is recognized as all features. 93 void set_supports_feature_type_all(bool supported); 94 95 // Enables logging of control list decisions. 96 void enable_control_list_logging( 97 const std::string& control_list_logging_name) { 98 control_list_logging_enabled_ = true; 99 control_list_logging_name_ = control_list_logging_name; 100 } 101 102 private: 103 friend class GpuControlListEntryTest; 104 friend class MachineModelInfoTest; 105 friend class NumberInfoTest; 106 friend class OsInfoTest; 107 friend class StringInfoTest; 108 friend class VersionInfoTest; 109 110 enum NumericOp { 111 kBetween, // <= * <= 112 kEQ, // = 113 kLT, // < 114 kLE, // <= 115 kGT, // > 116 kGE, // >= 117 kAny, 118 kUnknown // Indicates the data is invalid. 119 }; 120 121 class GPU_EXPORT VersionInfo { 122 public: 123 // If version_style is empty, it defaults to kNumerical. 124 VersionInfo(const std::string& version_op, 125 const std::string& version_style, 126 const std::string& version_string, 127 const std::string& version_string2); 128 ~VersionInfo(); 129 130 // Determines if a given version is included in the VersionInfo range. 131 // "splitter" divides version string into segments. 132 bool Contains(const std::string& version, char splitter) const; 133 // Same as above, using '.' as splitter. 134 bool Contains(const std::string& version) const; 135 136 // Determine if the version_style is lexical. 137 bool IsLexical() const; 138 139 // Determines if the VersionInfo contains valid information. 140 bool IsValid() const; 141 142 private: 143 enum VersionStyle { 144 kVersionStyleNumerical, 145 kVersionStyleLexical, 146 kVersionStyleUnknown 147 }; 148 149 static VersionStyle StringToVersionStyle(const std::string& version_style); 150 151 // Compare two version strings. 152 // Return 1 if version > version_ref, 153 // 0 if version = version_ref, 154 // -1 if version < version_ref. 155 // Note that we only compare as many segments as both versions contain. 156 // For example: Compare("10.3.1", "10.3") returns 0, 157 // Compare("10.3", "10.3.1") returns 0. 158 // If "version_style" is Lexical, the first segment is compared 159 // numerically, all other segments are compared lexically. 160 // Lexical is used for AMD Linux driver versions only. 161 static int Compare(const std::vector<std::string>& version, 162 const std::vector<std::string>& version_ref, 163 VersionStyle version_style); 164 165 NumericOp op_; 166 VersionStyle version_style_; 167 std::vector<std::string> version_; 168 std::vector<std::string> version2_; 169 }; 170 171 class GPU_EXPORT OsInfo { 172 public: 173 OsInfo(const std::string& os, 174 const std::string& version_op, 175 const std::string& version_string, 176 const std::string& version_string2); 177 ~OsInfo(); 178 179 // Determines if a given os/version is included in the OsInfo set. 180 bool Contains(OsType type, const std::string& version) const; 181 182 // Determines if the VersionInfo contains valid information. 183 bool IsValid() const; 184 185 OsType type() const; 186 187 // Maps string to OsType; returns kOsUnknown if it's not a valid os. 188 static OsType StringToOsType(const std::string& os); 189 190 private: 191 OsType type_; 192 scoped_ptr<VersionInfo> version_info_; 193 }; 194 195 class GPU_EXPORT StringInfo { 196 public: 197 StringInfo(const std::string& string_op, const std::string& string_value); 198 199 // Determines if a given string is included in the StringInfo. 200 bool Contains(const std::string& value) const; 201 202 // Determines if the StringInfo contains valid information. 203 bool IsValid() const; 204 205 private: 206 enum Op { 207 kContains, 208 kBeginWith, 209 kEndWith, 210 kEQ, // = 211 kUnknown // Indicates StringInfo data is invalid. 212 }; 213 214 // Maps string to Op; returns kUnknown if it's not a valid Op. 215 static Op StringToOp(const std::string& string_op); 216 217 Op op_; 218 std::string value_; 219 }; 220 221 class GPU_EXPORT FloatInfo { 222 public: 223 FloatInfo(const std::string& float_op, 224 const std::string& float_value, 225 const std::string& float_value2); 226 227 // Determines if a given float is included in the FloatInfo. 228 bool Contains(float value) const; 229 230 // Determines if the FloatInfo contains valid information. 231 bool IsValid() const; 232 233 private: 234 NumericOp op_; 235 float value_; 236 float value2_; 237 }; 238 239 class GPU_EXPORT IntInfo { 240 public: 241 IntInfo(const std::string& int_op, 242 const std::string& int_value, 243 const std::string& int_value2); 244 245 // Determines if a given int is included in the IntInfo. 246 bool Contains(int value) const; 247 248 // Determines if the IntInfo contains valid information. 249 bool IsValid() const; 250 251 private: 252 NumericOp op_; 253 int value_; 254 int value2_; 255 }; 256 257 class GPU_EXPORT BoolInfo { 258 public: 259 explicit BoolInfo(bool value); 260 261 // Determines if a given bool is included in the BoolInfo. 262 bool Contains(bool value) const; 263 264 private: 265 bool value_; 266 }; 267 268 class GpuControlListEntry; 269 typedef scoped_refptr<GpuControlListEntry> ScopedGpuControlListEntry; 270 271 typedef base::hash_map<std::string, int> FeatureMap; 272 273 class GPU_EXPORT GpuControlListEntry 274 : public base::RefCounted<GpuControlListEntry> { 275 public: 276 // Constructs GpuControlListEntry from DictionaryValue loaded from json. 277 // Top-level entry must have an id number. Others are exceptions. 278 static ScopedGpuControlListEntry GetEntryFromValue( 279 const base::DictionaryValue* value, bool top_level, 280 const FeatureMap& feature_map, 281 bool supports_feature_type_all); 282 283 // Logs a control list match for this rule in the list identified by 284 // |control_list_logging_name|. 285 void LogControlListMatch( 286 const std::string& control_list_logging_name) const; 287 288 // Determines if a given os/gc/machine_model/driver is included in the 289 // Entry set. 290 bool Contains(OsType os_type, const std::string& os_version, 291 const GPUInfo& gpu_info) const; 292 293 // Determines whether we needs more gpu info to make the blacklisting 294 // decision. It should only be checked if Contains() returns true. 295 bool NeedsMoreInfo(const GPUInfo& gpu_info) const; 296 297 // Returns the OsType. 298 OsType GetOsType() const; 299 300 // Returns the entry's unique id. 0 is reserved. 301 uint32 id() const; 302 303 // Returns whether the entry is disabled. 304 bool disabled() const; 305 306 // Returns the description of the entry 307 const std::string& description() const { return description_; } 308 309 // Returns a list of Chromium and Webkit bugs applicable to this entry 310 const std::vector<int>& cr_bugs() const { return cr_bugs_; } 311 const std::vector<int>& webkit_bugs() const { return webkit_bugs_; } 312 313 // Returns the blacklisted features in this entry. 314 const std::set<int>& features() const; 315 316 // Returns a list of blacklisted feature names in this entry. 317 void GetFeatureNames(base::ListValue* feature_names, 318 const FeatureMap& feature_map, 319 bool supports_feature_type_all) const; 320 321 private: 322 friend class base::RefCounted<GpuControlListEntry>; 323 324 enum MultiGpuStyle { 325 kMultiGpuStyleOptimus, 326 kMultiGpuStyleAMDSwitchable, 327 kMultiGpuStyleAMDSwitchableIntegrated, 328 kMultiGpuStyleAMDSwitchableDiscrete, 329 kMultiGpuStyleNone 330 }; 331 332 enum MultiGpuCategory { 333 // This entry applies if this is the primary GPU on the system. 334 kMultiGpuCategoryPrimary, 335 // This entry applies if this is a secondary GPU on the system. 336 kMultiGpuCategorySecondary, 337 // This entry applies if this is the active GPU on the system. 338 kMultiGpuCategoryActive, 339 // This entry applies if this is any of the GPUs on the system. 340 kMultiGpuCategoryAny, 341 kMultiGpuCategoryNone 342 }; 343 344 enum GLType { 345 kGLTypeGL, // This is default on MacOSX, Linux, ChromeOS 346 kGLTypeGLES, // This is default on Android 347 kGLTypeANGLE, // This is default on Windows 348 kGLTypeNone 349 }; 350 351 GpuControlListEntry(); 352 ~GpuControlListEntry(); 353 354 bool SetId(uint32 id); 355 356 void SetDisabled(bool disabled); 357 358 bool SetOsInfo(const std::string& os, 359 const std::string& version_op, 360 const std::string& version_string, 361 const std::string& version_string2); 362 363 bool SetVendorId(const std::string& vendor_id_string); 364 365 bool AddDeviceId(const std::string& device_id_string); 366 367 bool SetMultiGpuStyle(const std::string& multi_gpu_style_string); 368 369 bool SetMultiGpuCategory(const std::string& multi_gpu_category_string); 370 371 bool SetGLType(const std::string& gl_type_string); 372 373 bool SetDriverVendorInfo(const std::string& vendor_op, 374 const std::string& vendor_value); 375 376 bool SetDriverVersionInfo(const std::string& version_op, 377 const std::string& version_style, 378 const std::string& version_string, 379 const std::string& version_string2); 380 381 bool SetDriverDateInfo(const std::string& date_op, 382 const std::string& date_string, 383 const std::string& date_string2); 384 385 bool SetGLVersionInfo(const std::string& version_op, 386 const std::string& version_string, 387 const std::string& version_string2); 388 389 bool SetGLVendorInfo(const std::string& vendor_op, 390 const std::string& vendor_value); 391 392 bool SetGLRendererInfo(const std::string& renderer_op, 393 const std::string& renderer_value); 394 395 bool SetGLExtensionsInfo(const std::string& extensions_op, 396 const std::string& extensions_value); 397 398 bool SetGLResetNotificationStrategyInfo(const std::string& op, 399 const std::string& int_string, 400 const std::string& int_string2); 401 402 bool SetCpuBrand(const std::string& cpu_op, 403 const std::string& cpu_value); 404 405 bool SetPerfGraphicsInfo(const std::string& op, 406 const std::string& float_string, 407 const std::string& float_string2); 408 409 bool SetPerfGamingInfo(const std::string& op, 410 const std::string& float_string, 411 const std::string& float_string2); 412 413 bool SetPerfOverallInfo(const std::string& op, 414 const std::string& float_string, 415 const std::string& float_string2); 416 417 bool AddMachineModelName(const std::string& model_name); 418 419 bool SetMachineModelVersionInfo(const std::string& version_op, 420 const std::string& version_string, 421 const std::string& version_string2); 422 423 bool SetGpuCountInfo(const std::string& op, 424 const std::string& int_string, 425 const std::string& int_string2); 426 427 void SetDirectRenderingInfo(bool value); 428 429 bool SetFeatures(const std::vector<std::string>& features, 430 const FeatureMap& feature_map, 431 bool supports_feature_type_all); 432 433 void AddException(ScopedGpuControlListEntry exception); 434 435 // Return true if GL_VERSION string does not fit the entry info 436 // on GL type and GL version. 437 bool GLVersionInfoMismatch(const std::string& gl_version) const; 438 439 static MultiGpuStyle StringToMultiGpuStyle(const std::string& style); 440 441 static MultiGpuCategory StringToMultiGpuCategory( 442 const std::string& category); 443 444 static GLType StringToGLType(const std::string& gl_type); 445 446 // map a feature_name to feature_id. If the string is not a registered 447 // feature name, return false. 448 static bool StringToFeature(const std::string& feature_name, 449 int* feature_id, 450 const FeatureMap& feature_map); 451 452 // Return the default GL type, depending on the OS. 453 // See GLType declaration. 454 static GLType GetDefaultGLType(); 455 456 uint32 id_; 457 bool disabled_; 458 std::string description_; 459 std::vector<int> cr_bugs_; 460 std::vector<int> webkit_bugs_; 461 scoped_ptr<OsInfo> os_info_; 462 uint32 vendor_id_; 463 std::vector<uint32> device_id_list_; 464 MultiGpuStyle multi_gpu_style_; 465 MultiGpuCategory multi_gpu_category_; 466 GLType gl_type_; 467 scoped_ptr<StringInfo> driver_vendor_info_; 468 scoped_ptr<VersionInfo> driver_version_info_; 469 scoped_ptr<VersionInfo> driver_date_info_; 470 scoped_ptr<VersionInfo> gl_version_info_; 471 scoped_ptr<StringInfo> gl_vendor_info_; 472 scoped_ptr<StringInfo> gl_renderer_info_; 473 scoped_ptr<StringInfo> gl_extensions_info_; 474 scoped_ptr<IntInfo> gl_reset_notification_strategy_info_; 475 scoped_ptr<StringInfo> cpu_brand_; 476 scoped_ptr<FloatInfo> perf_graphics_info_; 477 scoped_ptr<FloatInfo> perf_gaming_info_; 478 scoped_ptr<FloatInfo> perf_overall_info_; 479 std::vector<std::string> machine_model_name_list_; 480 scoped_ptr<VersionInfo> machine_model_version_info_; 481 scoped_ptr<IntInfo> gpu_count_info_; 482 scoped_ptr<BoolInfo> direct_rendering_info_; 483 std::set<int> features_; 484 std::vector<ScopedGpuControlListEntry> exceptions_; 485 }; 486 487 // Gets the current OS type. 488 static OsType GetOsType(); 489 490 bool LoadList(const base::DictionaryValue& parsed_json, OsFilter os_filter); 491 492 void Clear(); 493 494 static NumericOp StringToNumericOp(const std::string& op); 495 496 std::string version_; 497 std::vector<ScopedGpuControlListEntry> entries_; 498 499 // This records all the blacklist entries that are appliable to the current 500 // user machine. It is updated everytime MakeDecision() is called and is 501 // used later by GetDecisionEntries(). 502 std::vector<ScopedGpuControlListEntry> active_entries_; 503 504 uint32 max_entry_id_; 505 506 bool needs_more_info_; 507 508 // The features a GpuControlList recognizes and handles. 509 FeatureMap feature_map_; 510 bool supports_feature_type_all_; 511 512 bool control_list_logging_enabled_; 513 std::string control_list_logging_name_; 514 }; 515 516 } // namespace gpu 517 518 #endif // GPU_CONFIG_GPU_CONTROL_LIST_H_ 519 520