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);
49 bool LoadList(const std::string& browser_version_string,
50 const std::string& json_context, OsFilter os_filter);
52 // Collects system information and combines them with gpu_info and control
53 // list information to decide which entries are applied to the current
54 // system and returns the union of features specified in each entry.
55 // If os is kOsAny, use the current OS; if os_version is empty, use the
56 // current OS version.
57 std::set<int> MakeDecision(
58 OsType os, std::string os_version, const GPUInfo& gpu_info);
60 // Collects the active entries from the last MakeDecision() call.
61 // If disabled set to true, return entries that are disabled; otherwise,
62 // return enabled entries.
63 void GetDecisionEntries(std::vector<uint32>* entry_ids,
66 // Returns the description and bugs from active entries from the last
67 // MakeDecision() call.
71 // "description": "Your GPU is too old",
75 void GetReasons(base::ListValue* problem_list) const;
77 // Return the largest entry id. This is used for histogramming.
78 uint32 max_entry_id() const;
80 // Returns the version of the control list.
81 std::string version() const;
83 // Check if we need more gpu info to make the decisions.
84 // This is computed from the last MakeDecision() call.
85 // If yes, we should create a gl context and do a full gpu info collection.
86 bool needs_more_info() const { return needs_more_info_; }
88 // Returns the number of entries. This is only for tests.
89 size_t num_entries() const;
91 // Register a feature to FeatureMap - used to construct a GpuControlList.
92 void AddSupportedFeature(const std::string& feature_name, int feature_id);
93 // Register whether "all" is recognized as all features.
94 void set_supports_feature_type_all(bool supported);
96 // Enables logging of control list decisions.
97 void enable_control_list_logging(
98 const std::string& control_list_logging_name) {
99 control_list_logging_enabled_ = true;
100 control_list_logging_name_ = control_list_logging_name;
104 friend class GpuControlListEntryTest;
105 friend class MachineModelInfoTest;
106 friend class NumberInfoTest;
107 friend class OsInfoTest;
108 friend class StringInfoTest;
109 friend class VersionInfoTest;
111 enum BrowserVersionSupport {
125 kUnknown // Indicates the data is invalid.
128 class GPU_EXPORT VersionInfo {
130 // If version_style is empty, it defaults to kNumerical.
131 VersionInfo(const std::string& version_op,
132 const std::string& version_style,
133 const std::string& version_string,
134 const std::string& version_string2);
137 // Determines if a given version is included in the VersionInfo range.
138 // "splitter" divides version string into segments.
139 bool Contains(const std::string& version, char splitter) const;
140 // Same as above, using '.' as splitter.
141 bool Contains(const std::string& version) const;
143 // Determine if the version_style is lexical.
144 bool IsLexical() const;
146 // Determines if the VersionInfo contains valid information.
147 bool IsValid() const;
151 kVersionStyleNumerical,
152 kVersionStyleLexical,
156 static VersionStyle StringToVersionStyle(const std::string& version_style);
158 // Compare two version strings.
159 // Return 1 if version > version_ref,
160 // 0 if version = version_ref,
161 // -1 if version < version_ref.
162 // Note that we only compare as many segments as both versions contain.
163 // For example: Compare("10.3.1", "10.3") returns 0,
164 // Compare("10.3", "10.3.1") returns 0.
165 // If "version_style" is Lexical, the first segment is compared
166 // numerically, all other segments are compared lexically.
167 // Lexical is used for AMD Linux driver versions only.
168 static int Compare(const std::vector<std::string>& version,
169 const std::vector<std::string>& version_ref,
170 VersionStyle version_style);
173 VersionStyle version_style_;
174 std::vector<std::string> version_;
175 std::vector<std::string> version2_;
178 class GPU_EXPORT OsInfo {
180 OsInfo(const std::string& os,
181 const std::string& version_op,
182 const std::string& version_string,
183 const std::string& version_string2);
186 // Determines if a given os/version is included in the OsInfo set.
187 bool Contains(OsType type, const std::string& version) const;
189 // Determines if the VersionInfo contains valid information.
190 bool IsValid() const;
194 // Maps string to OsType; returns kOsUnknown if it's not a valid os.
195 static OsType StringToOsType(const std::string& os);
199 scoped_ptr<VersionInfo> version_info_;
202 class GPU_EXPORT StringInfo {
204 StringInfo(const std::string& string_op, const std::string& string_value);
206 // Determines if a given string is included in the StringInfo.
207 bool Contains(const std::string& value) const;
209 // Determines if the StringInfo contains valid information.
210 bool IsValid() const;
218 kUnknown // Indicates StringInfo data is invalid.
221 // Maps string to Op; returns kUnknown if it's not a valid Op.
222 static Op StringToOp(const std::string& string_op);
228 class GPU_EXPORT FloatInfo {
230 FloatInfo(const std::string& float_op,
231 const std::string& float_value,
232 const std::string& float_value2);
234 // Determines if a given float is included in the FloatInfo.
235 bool Contains(float value) const;
237 // Determines if the FloatInfo contains valid information.
238 bool IsValid() const;
246 class GPU_EXPORT IntInfo {
248 IntInfo(const std::string& int_op,
249 const std::string& int_value,
250 const std::string& int_value2);
252 // Determines if a given int is included in the IntInfo.
253 bool Contains(int value) const;
255 // Determines if the IntInfo contains valid information.
256 bool IsValid() const;
264 class GPU_EXPORT MachineModelInfo {
266 MachineModelInfo(const std::string& name_op,
267 const std::string& name_value,
268 const std::string& version_op,
269 const std::string& version_string,
270 const std::string& version_string2);
273 // Determines if a given name/version is included in the MachineModelInfo.
274 bool Contains(const std::string& name, const std::string& version) const;
276 // Determines if the MachineModelInfo contains valid information.
277 bool IsValid() const;
280 scoped_ptr<StringInfo> name_info_;
281 scoped_ptr<VersionInfo> version_info_;
284 class GpuControlListEntry;
285 typedef scoped_refptr<GpuControlListEntry> ScopedGpuControlListEntry;
287 typedef base::hash_map<std::string, int> FeatureMap;
289 class GPU_EXPORT GpuControlListEntry
290 : public base::RefCounted<GpuControlListEntry> {
292 // Constructs GpuControlListEntry from DictionaryValue loaded from json.
293 // Top-level entry must have an id number. Others are exceptions.
294 static ScopedGpuControlListEntry GetEntryFromValue(
295 const base::DictionaryValue* value, bool top_level,
296 const FeatureMap& feature_map,
297 bool supports_feature_type_all);
299 // Logs a control list match for this rule in the list identified by
300 // |control_list_logging_name|.
301 void LogControlListMatch(
302 const std::string& control_list_logging_name) const;
304 // Determines if a given os/gc/machine_model/driver is included in the
306 bool Contains(OsType os_type, const std::string& os_version,
307 const GPUInfo& gpu_info) const;
309 // Determines whether we needs more gpu info to make the blacklisting
310 // decision. It should only be checked if Contains() returns true.
311 bool NeedsMoreInfo(const GPUInfo& gpu_info) const;
313 // Returns the OsType.
314 OsType GetOsType() const;
316 // Returns the entry's unique id. 0 is reserved.
319 // Returns whether the entry is disabled.
320 bool disabled() const;
322 // Returns the description of the entry
323 const std::string& description() const { return description_; }
325 // Returns a list of Chromium and Webkit bugs applicable to this entry
326 const std::vector<int>& cr_bugs() const { return cr_bugs_; }
327 const std::vector<int>& webkit_bugs() const { return webkit_bugs_; }
329 // Returns the blacklisted features in this entry.
330 const std::set<int>& features() const;
333 friend class base::RefCounted<GpuControlListEntry>;
336 kMultiGpuStyleOptimus,
337 kMultiGpuStyleAMDSwitchable,
341 enum MultiGpuCategory {
342 kMultiGpuCategoryPrimary,
343 kMultiGpuCategorySecondary,
344 kMultiGpuCategoryAny,
345 kMultiGpuCategoryNone
348 GpuControlListEntry();
349 ~GpuControlListEntry();
351 bool SetId(uint32 id);
353 void SetDisabled(bool disabled);
355 bool SetOsInfo(const std::string& os,
356 const std::string& version_op,
357 const std::string& version_string,
358 const std::string& version_string2);
360 bool SetVendorId(const std::string& vendor_id_string);
362 bool AddDeviceId(const std::string& device_id_string);
364 bool SetMultiGpuStyle(const std::string& multi_gpu_style_string);
366 bool SetMultiGpuCategory(const std::string& multi_gpu_category_string);
368 bool SetDriverVendorInfo(const std::string& vendor_op,
369 const std::string& vendor_value);
371 bool SetDriverVersionInfo(const std::string& version_op,
372 const std::string& version_style,
373 const std::string& version_string,
374 const std::string& version_string2);
376 bool SetDriverDateInfo(const std::string& date_op,
377 const std::string& date_string,
378 const std::string& date_string2);
380 bool SetGLVendorInfo(const std::string& vendor_op,
381 const std::string& vendor_value);
383 bool SetGLRendererInfo(const std::string& renderer_op,
384 const std::string& renderer_value);
386 bool SetGLExtensionsInfo(const std::string& extensions_op,
387 const std::string& extensions_value);
389 bool SetGLResetNotificationStrategyInfo(const std::string& op,
390 const std::string& int_string,
391 const std::string& int_string2);
393 bool SetCpuBrand(const std::string& cpu_op,
394 const std::string& cpu_value);
396 bool SetPerfGraphicsInfo(const std::string& op,
397 const std::string& float_string,
398 const std::string& float_string2);
400 bool SetPerfGamingInfo(const std::string& op,
401 const std::string& float_string,
402 const std::string& float_string2);
404 bool SetPerfOverallInfo(const std::string& op,
405 const std::string& float_string,
406 const std::string& float_string2);
408 bool SetMachineModelInfo(const std::string& name_op,
409 const std::string& name_value,
410 const std::string& version_op,
411 const std::string& version_string,
412 const std::string& version_string2);
414 bool SetGpuCountInfo(const std::string& op,
415 const std::string& int_string,
416 const std::string& int_string2);
418 bool SetFeatures(const std::vector<std::string>& features,
419 const FeatureMap& feature_map,
420 bool supports_feature_type_all);
422 void AddException(ScopedGpuControlListEntry exception);
424 static MultiGpuStyle StringToMultiGpuStyle(const std::string& style);
426 static MultiGpuCategory StringToMultiGpuCategory(
427 const std::string& category);
429 // map a feature_name to feature_id. If the string is not a registered
430 // feature name, return false.
431 static bool StringToFeature(const std::string& feature_name,
433 const FeatureMap& feature_map);
437 std::string description_;
438 std::vector<int> cr_bugs_;
439 std::vector<int> webkit_bugs_;
440 scoped_ptr<OsInfo> os_info_;
442 std::vector<uint32> device_id_list_;
443 MultiGpuStyle multi_gpu_style_;
444 MultiGpuCategory multi_gpu_category_;
445 scoped_ptr<StringInfo> driver_vendor_info_;
446 scoped_ptr<VersionInfo> driver_version_info_;
447 scoped_ptr<VersionInfo> driver_date_info_;
448 scoped_ptr<StringInfo> gl_vendor_info_;
449 scoped_ptr<StringInfo> gl_renderer_info_;
450 scoped_ptr<StringInfo> gl_extensions_info_;
451 scoped_ptr<IntInfo> gl_reset_notification_strategy_info_;
452 scoped_ptr<StringInfo> cpu_brand_;
453 scoped_ptr<FloatInfo> perf_graphics_info_;
454 scoped_ptr<FloatInfo> perf_gaming_info_;
455 scoped_ptr<FloatInfo> perf_overall_info_;
456 scoped_ptr<MachineModelInfo> machine_model_info_;
457 scoped_ptr<IntInfo> gpu_count_info_;
458 std::set<int> features_;
459 std::vector<ScopedGpuControlListEntry> exceptions_;
462 // Gets the current OS type.
463 static OsType GetOsType();
465 bool LoadList(const base::DictionaryValue& parsed_json, OsFilter os_filter);
469 // Check if the entry is supported by the current version of browser.
470 // By default, if there is no browser version information in the entry,
471 // return kSupported;
472 BrowserVersionSupport IsEntrySupportedByCurrentBrowserVersion(
473 const base::DictionaryValue* value);
475 static NumericOp StringToNumericOp(const std::string& op);
477 std::string version_;
478 std::vector<ScopedGpuControlListEntry> entries_;
480 std::string browser_version_;
482 // This records all the blacklist entries that are appliable to the current
483 // user machine. It is updated everytime MakeDecision() is called and is
484 // used later by GetDecisionEntries().
485 std::vector<ScopedGpuControlListEntry> active_entries_;
487 uint32 max_entry_id_;
489 bool needs_more_info_;
491 // The features a GpuControlList recognizes and handles.
492 FeatureMap feature_map_;
493 bool supports_feature_type_all_;
495 bool control_list_logging_enabled_;
496 std::string control_list_logging_name_;
501 #endif // GPU_CONFIG_GPU_CONTROL_LIST_H_