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.
5 #ifndef GPU_CONFIG_GPU_CONTROL_LIST_H_
6 #define GPU_CONFIG_GPU_CONTROL_LIST_H_
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"
24 class GPU_EXPORT GpuControlList {
37 // In loading, ignore all entries that belong to other OS.
39 // In loading, keep all entries. This is for testing only.
44 virtual ~GpuControlList();
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);
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);
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,
64 // Returns the description and bugs from active entries from the last
65 // MakeDecision() call.
69 // "description": "Your GPU is too old",
74 base::ListValue* problem_list, const std::string& tag) const;
76 // Return the largest entry id. This is used for histogramming.
77 uint32 max_entry_id() const;
79 // Returns the version of the control list.
80 std::string version() const;
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_; }
87 // Returns the number of entries. This is only for tests.
88 size_t num_entries() const;
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);
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;
103 friend class GpuControlListEntryTest;
104 friend class MachineModelInfoTest;
105 friend class NumberInfoTest;
106 friend class OsInfoTest;
107 friend class StringInfoTest;
108 friend class VersionInfoTest;
118 kUnknown // Indicates the data is invalid.
121 class GPU_EXPORT VersionInfo {
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);
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;
136 // Determine if the version_style is lexical.
137 bool IsLexical() const;
139 // Determines if the VersionInfo contains valid information.
140 bool IsValid() const;
144 kVersionStyleNumerical,
145 kVersionStyleLexical,
149 static VersionStyle StringToVersionStyle(const std::string& version_style);
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);
166 VersionStyle version_style_;
167 std::vector<std::string> version_;
168 std::vector<std::string> version2_;
171 class GPU_EXPORT OsInfo {
173 OsInfo(const std::string& os,
174 const std::string& version_op,
175 const std::string& version_string,
176 const std::string& version_string2);
179 // Determines if a given os/version is included in the OsInfo set.
180 bool Contains(OsType type, const std::string& version) const;
182 // Determines if the VersionInfo contains valid information.
183 bool IsValid() const;
187 // Maps string to OsType; returns kOsUnknown if it's not a valid os.
188 static OsType StringToOsType(const std::string& os);
192 scoped_ptr<VersionInfo> version_info_;
195 class GPU_EXPORT StringInfo {
197 StringInfo(const std::string& string_op, const std::string& string_value);
199 // Determines if a given string is included in the StringInfo.
200 bool Contains(const std::string& value) const;
202 // Determines if the StringInfo contains valid information.
203 bool IsValid() const;
211 kUnknown // Indicates StringInfo data is invalid.
214 // Maps string to Op; returns kUnknown if it's not a valid Op.
215 static Op StringToOp(const std::string& string_op);
221 class GPU_EXPORT FloatInfo {
223 FloatInfo(const std::string& float_op,
224 const std::string& float_value,
225 const std::string& float_value2);
227 // Determines if a given float is included in the FloatInfo.
228 bool Contains(float value) const;
230 // Determines if the FloatInfo contains valid information.
231 bool IsValid() const;
239 class GPU_EXPORT IntInfo {
241 IntInfo(const std::string& int_op,
242 const std::string& int_value,
243 const std::string& int_value2);
245 // Determines if a given int is included in the IntInfo.
246 bool Contains(int value) const;
248 // Determines if the IntInfo contains valid information.
249 bool IsValid() const;
257 class GPU_EXPORT BoolInfo {
259 explicit BoolInfo(bool value);
261 // Determines if a given bool is included in the BoolInfo.
262 bool Contains(bool value) const;
268 class GPU_EXPORT MachineModelInfo {
270 MachineModelInfo(const std::string& name_op,
271 const std::string& name_value,
272 const std::string& version_op,
273 const std::string& version_string,
274 const std::string& version_string2);
277 // Determines if a given name/version is included in the MachineModelInfo.
278 bool Contains(const std::string& name, const std::string& version) const;
280 // Determines if the MachineModelInfo contains valid information.
281 bool IsValid() const;
284 scoped_ptr<StringInfo> name_info_;
285 scoped_ptr<VersionInfo> version_info_;
288 class GpuControlListEntry;
289 typedef scoped_refptr<GpuControlListEntry> ScopedGpuControlListEntry;
291 typedef base::hash_map<std::string, int> FeatureMap;
293 class GPU_EXPORT GpuControlListEntry
294 : public base::RefCounted<GpuControlListEntry> {
296 // Constructs GpuControlListEntry from DictionaryValue loaded from json.
297 // Top-level entry must have an id number. Others are exceptions.
298 static ScopedGpuControlListEntry GetEntryFromValue(
299 const base::DictionaryValue* value, bool top_level,
300 const FeatureMap& feature_map,
301 bool supports_feature_type_all);
303 // Logs a control list match for this rule in the list identified by
304 // |control_list_logging_name|.
305 void LogControlListMatch(
306 const std::string& control_list_logging_name) const;
308 // Determines if a given os/gc/machine_model/driver is included in the
310 bool Contains(OsType os_type, const std::string& os_version,
311 const GPUInfo& gpu_info) const;
313 // Determines whether we needs more gpu info to make the blacklisting
314 // decision. It should only be checked if Contains() returns true.
315 bool NeedsMoreInfo(const GPUInfo& gpu_info) const;
317 // Returns the OsType.
318 OsType GetOsType() const;
320 // Returns the entry's unique id. 0 is reserved.
323 // Returns whether the entry is disabled.
324 bool disabled() const;
326 // Returns the description of the entry
327 const std::string& description() const { return description_; }
329 // Returns a list of Chromium and Webkit bugs applicable to this entry
330 const std::vector<int>& cr_bugs() const { return cr_bugs_; }
331 const std::vector<int>& webkit_bugs() const { return webkit_bugs_; }
333 // Returns the blacklisted features in this entry.
334 const std::set<int>& features() const;
336 // Returns a list of blacklisted feature names in this entry.
337 void GetFeatureNames(base::ListValue* feature_names,
338 const FeatureMap& feature_map,
339 bool supports_feature_type_all) const;
342 friend class base::RefCounted<GpuControlListEntry>;
345 kMultiGpuStyleOptimus,
346 kMultiGpuStyleAMDSwitchable,
350 enum MultiGpuCategory {
351 // This entry applies if this is the primary GPU on the system.
352 kMultiGpuCategoryPrimary,
353 // This entry applies if this is a secondary GPU on the system.
354 kMultiGpuCategorySecondary,
355 // This entry applies if this is the active GPU on the system.
356 kMultiGpuCategoryActive,
357 // This entry applies if this is any of the GPUs on the system.
358 kMultiGpuCategoryAny,
359 kMultiGpuCategoryNone
362 GpuControlListEntry();
363 ~GpuControlListEntry();
365 bool SetId(uint32 id);
367 void SetDisabled(bool disabled);
369 bool SetOsInfo(const std::string& os,
370 const std::string& version_op,
371 const std::string& version_string,
372 const std::string& version_string2);
374 bool SetVendorId(const std::string& vendor_id_string);
376 bool AddDeviceId(const std::string& device_id_string);
378 bool SetMultiGpuStyle(const std::string& multi_gpu_style_string);
380 bool SetMultiGpuCategory(const std::string& multi_gpu_category_string);
382 bool SetDriverVendorInfo(const std::string& vendor_op,
383 const std::string& vendor_value);
385 bool SetDriverVersionInfo(const std::string& version_op,
386 const std::string& version_style,
387 const std::string& version_string,
388 const std::string& version_string2);
390 bool SetDriverDateInfo(const std::string& date_op,
391 const std::string& date_string,
392 const std::string& date_string2);
394 bool SetGLVendorInfo(const std::string& vendor_op,
395 const std::string& vendor_value);
397 bool SetGLRendererInfo(const std::string& renderer_op,
398 const std::string& renderer_value);
400 bool SetGLExtensionsInfo(const std::string& extensions_op,
401 const std::string& extensions_value);
403 bool SetGLResetNotificationStrategyInfo(const std::string& op,
404 const std::string& int_string,
405 const std::string& int_string2);
407 bool SetCpuBrand(const std::string& cpu_op,
408 const std::string& cpu_value);
410 bool SetPerfGraphicsInfo(const std::string& op,
411 const std::string& float_string,
412 const std::string& float_string2);
414 bool SetPerfGamingInfo(const std::string& op,
415 const std::string& float_string,
416 const std::string& float_string2);
418 bool SetPerfOverallInfo(const std::string& op,
419 const std::string& float_string,
420 const std::string& float_string2);
422 bool SetMachineModelInfo(const std::string& name_op,
423 const std::string& name_value,
424 const std::string& version_op,
425 const std::string& version_string,
426 const std::string& version_string2);
428 bool SetGpuCountInfo(const std::string& op,
429 const std::string& int_string,
430 const std::string& int_string2);
432 void SetDirectRenderingInfo(bool value);
434 bool SetFeatures(const std::vector<std::string>& features,
435 const FeatureMap& feature_map,
436 bool supports_feature_type_all);
438 void AddException(ScopedGpuControlListEntry exception);
440 static MultiGpuStyle StringToMultiGpuStyle(const std::string& style);
442 static MultiGpuCategory StringToMultiGpuCategory(
443 const std::string& category);
445 // map a feature_name to feature_id. If the string is not a registered
446 // feature name, return false.
447 static bool StringToFeature(const std::string& feature_name,
449 const FeatureMap& feature_map);
453 std::string description_;
454 std::vector<int> cr_bugs_;
455 std::vector<int> webkit_bugs_;
456 scoped_ptr<OsInfo> os_info_;
458 std::vector<uint32> device_id_list_;
459 MultiGpuStyle multi_gpu_style_;
460 MultiGpuCategory multi_gpu_category_;
461 scoped_ptr<StringInfo> driver_vendor_info_;
462 scoped_ptr<VersionInfo> driver_version_info_;
463 scoped_ptr<VersionInfo> driver_date_info_;
464 scoped_ptr<StringInfo> gl_vendor_info_;
465 scoped_ptr<StringInfo> gl_renderer_info_;
466 scoped_ptr<StringInfo> gl_extensions_info_;
467 scoped_ptr<IntInfo> gl_reset_notification_strategy_info_;
468 scoped_ptr<StringInfo> cpu_brand_;
469 scoped_ptr<FloatInfo> perf_graphics_info_;
470 scoped_ptr<FloatInfo> perf_gaming_info_;
471 scoped_ptr<FloatInfo> perf_overall_info_;
472 scoped_ptr<MachineModelInfo> machine_model_info_;
473 scoped_ptr<IntInfo> gpu_count_info_;
474 scoped_ptr<BoolInfo> direct_rendering_info_;
475 std::set<int> features_;
476 std::vector<ScopedGpuControlListEntry> exceptions_;
479 // Gets the current OS type.
480 static OsType GetOsType();
482 bool LoadList(const base::DictionaryValue& parsed_json, OsFilter os_filter);
486 static NumericOp StringToNumericOp(const std::string& op);
488 std::string version_;
489 std::vector<ScopedGpuControlListEntry> entries_;
491 // This records all the blacklist entries that are appliable to the current
492 // user machine. It is updated everytime MakeDecision() is called and is
493 // used later by GetDecisionEntries().
494 std::vector<ScopedGpuControlListEntry> active_entries_;
496 uint32 max_entry_id_;
498 bool needs_more_info_;
500 // The features a GpuControlList recognizes and handles.
501 FeatureMap feature_map_;
502 bool supports_feature_type_all_;
504 bool control_list_logging_enabled_;
505 std::string control_list_logging_name_;
510 #endif // GPU_CONFIG_GPU_CONTROL_LIST_H_