Roll src/third_party/WebKit c63b89c:29324ab (svn 202546:202547)
[chromium-blink-merge.git] / gpu / config / gpu_control_list.h
blobec83b04dfe05b242661fdd6a1dde0493a1a6de68
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_
8 #include <set>
9 #include <string>
10 #include <vector>
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"
21 namespace gpu {
22 struct GPUInfo;
24 class GPU_EXPORT GpuControlList {
25 public:
26 enum OsType {
27 kOsLinux,
28 kOsMacosx,
29 kOsWin,
30 kOsChromeOS,
31 kOsAndroid,
32 kOsAny,
33 kOsUnknown
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
43 GpuControlList();
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,
62 bool disabled) const;
64 // Collects all disabled extensions.
65 std::vector<std::string> GetDisabledExtensions();
67 // Returns the description and bugs from active entries from the last
68 // MakeDecision() call.
70 // Each problems has:
71 // {
72 // "description": "Your GPU is too old",
73 // "crBugs": [1234],
74 // "webkitBugs": []
75 // }
76 void GetReasons(
77 base::ListValue* problem_list, const std::string& tag) const;
79 // Return the largest entry id. This is used for histogramming.
80 uint32 max_entry_id() const;
82 // Returns the version of the control list.
83 std::string version() const;
85 // Check if we need more gpu info to make the decisions.
86 // This is computed from the last MakeDecision() call.
87 // If yes, we should create a gl context and do a full gpu info collection.
88 bool needs_more_info() const { return needs_more_info_; }
90 // Returns the number of entries. This is only for tests.
91 size_t num_entries() const;
93 // Register a feature to FeatureMap - used to construct a GpuControlList.
94 void AddSupportedFeature(const std::string& feature_name, int feature_id);
95 // Register whether "all" is recognized as all features.
96 void set_supports_feature_type_all(bool supported);
98 // Enables logging of control list decisions.
99 void enable_control_list_logging(
100 const std::string& control_list_logging_name) {
101 control_list_logging_enabled_ = true;
102 control_list_logging_name_ = control_list_logging_name;
105 private:
106 friend class GpuControlListEntryTest;
107 friend class MachineModelInfoTest;
108 friend class NumberInfoTest;
109 friend class OsInfoTest;
110 friend class StringInfoTest;
111 friend class VersionInfoTest;
113 enum NumericOp {
114 kBetween, // <= * <=
115 kEQ, // =
116 kLT, // <
117 kLE, // <=
118 kGT, // >
119 kGE, // >=
120 kAny,
121 kUnknown // Indicates the data is invalid.
124 class GPU_EXPORT VersionInfo {
125 public:
126 // If version_style is empty, it defaults to kNumerical.
127 VersionInfo(const std::string& version_op,
128 const std::string& version_style,
129 const std::string& version_string,
130 const std::string& version_string2);
131 ~VersionInfo();
133 // Determines if a given version is included in the VersionInfo range.
134 // "splitter" divides version string into segments.
135 bool Contains(const std::string& version, char splitter) const;
136 // Same as above, using '.' as splitter.
137 bool Contains(const std::string& version) const;
139 // Determine if the version_style is lexical.
140 bool IsLexical() const;
142 // Determines if the VersionInfo contains valid information.
143 bool IsValid() const;
145 private:
146 enum VersionStyle {
147 kVersionStyleNumerical,
148 kVersionStyleLexical,
149 kVersionStyleUnknown
152 static VersionStyle StringToVersionStyle(const std::string& version_style);
154 // Compare two version strings.
155 // Return 1 if version > version_ref,
156 // 0 if version = version_ref,
157 // -1 if version < version_ref.
158 // Note that we only compare as many segments as both versions contain.
159 // For example: Compare("10.3.1", "10.3") returns 0,
160 // Compare("10.3", "10.3.1") returns 0.
161 // If "version_style" is Lexical, the first segment is compared
162 // numerically, all other segments are compared lexically.
163 // Lexical is used for AMD Linux driver versions only.
164 static int Compare(const std::vector<std::string>& version,
165 const std::vector<std::string>& version_ref,
166 VersionStyle version_style);
168 NumericOp op_;
169 VersionStyle version_style_;
170 std::vector<std::string> version_;
171 std::vector<std::string> version2_;
174 class GPU_EXPORT OsInfo {
175 public:
176 OsInfo(const std::string& os,
177 const std::string& version_op,
178 const std::string& version_string,
179 const std::string& version_string2);
180 ~OsInfo();
182 // Determines if a given os/version is included in the OsInfo set.
183 bool Contains(OsType type, const std::string& version) const;
185 // Determines if the VersionInfo contains valid information.
186 bool IsValid() const;
188 OsType type() const;
190 // Maps string to OsType; returns kOsUnknown if it's not a valid os.
191 static OsType StringToOsType(const std::string& os);
193 private:
194 OsType type_;
195 scoped_ptr<VersionInfo> version_info_;
198 class GPU_EXPORT FloatInfo {
199 public:
200 FloatInfo(const std::string& float_op,
201 const std::string& float_value,
202 const std::string& float_value2);
204 // Determines if a given float is included in the FloatInfo.
205 bool Contains(float value) const;
207 // Determines if the FloatInfo contains valid information.
208 bool IsValid() const;
210 private:
211 NumericOp op_;
212 float value_;
213 float value2_;
216 class GPU_EXPORT IntInfo {
217 public:
218 IntInfo(const std::string& int_op,
219 const std::string& int_value,
220 const std::string& int_value2);
222 // Determines if a given int is included in the IntInfo.
223 bool Contains(int value) const;
225 // Determines if the IntInfo contains valid information.
226 bool IsValid() const;
228 private:
229 NumericOp op_;
230 int value_;
231 int value2_;
234 class GPU_EXPORT BoolInfo {
235 public:
236 explicit BoolInfo(bool value);
238 // Determines if a given bool is included in the BoolInfo.
239 bool Contains(bool value) const;
241 private:
242 bool value_;
245 class GpuControlListEntry;
246 typedef scoped_refptr<GpuControlListEntry> ScopedGpuControlListEntry;
248 typedef base::hash_map<std::string, int> FeatureMap;
250 class GPU_EXPORT GpuControlListEntry
251 : public base::RefCounted<GpuControlListEntry> {
252 public:
253 // Constructs GpuControlListEntry from DictionaryValue loaded from json.
254 // Top-level entry must have an id number. Others are exceptions.
255 static ScopedGpuControlListEntry GetEntryFromValue(
256 const base::DictionaryValue* value, bool top_level,
257 const FeatureMap& feature_map,
258 bool supports_feature_type_all);
260 // Logs a control list match for this rule in the list identified by
261 // |control_list_logging_name|.
262 void LogControlListMatch(
263 const std::string& control_list_logging_name) const;
265 // Determines if a given os/gc/machine_model/driver is included in the
266 // Entry set.
267 bool Contains(OsType os_type, const std::string& os_version,
268 const GPUInfo& gpu_info) const;
270 // Determines whether we needs more gpu info to make the blacklisting
271 // decision. It should only be checked if Contains() returns true.
272 bool NeedsMoreInfo(const GPUInfo& gpu_info, bool consider_exceptions) const;
274 // Returns the OsType.
275 OsType GetOsType() const;
277 // Returns the entry's unique id. 0 is reserved.
278 uint32 id() const;
280 // Returns whether the entry is disabled.
281 bool disabled() const;
283 // Returns the description of the entry
284 const std::string& description() const { return description_; }
286 // Returns a list of Chromium and Webkit bugs applicable to this entry
287 const std::vector<int>& cr_bugs() const { return cr_bugs_; }
288 const std::vector<int>& webkit_bugs() const { return webkit_bugs_; }
289 const std::vector<std::string>& disabled_extensions() const {
290 return disabled_extensions_;
293 // Returns the blacklisted features in this entry.
294 const std::set<int>& features() const;
296 // Returns a list of blacklisted feature names in this entry.
297 void GetFeatureNames(base::ListValue* feature_names,
298 const FeatureMap& feature_map,
299 bool supports_feature_type_all) const;
301 private:
302 friend class base::RefCounted<GpuControlListEntry>;
304 enum MultiGpuStyle {
305 kMultiGpuStyleOptimus,
306 kMultiGpuStyleAMDSwitchable,
307 kMultiGpuStyleAMDSwitchableIntegrated,
308 kMultiGpuStyleAMDSwitchableDiscrete,
309 kMultiGpuStyleNone
312 enum MultiGpuCategory {
313 // This entry applies if this is the primary GPU on the system.
314 kMultiGpuCategoryPrimary,
315 // This entry applies if this is a secondary GPU on the system.
316 kMultiGpuCategorySecondary,
317 // This entry applies if this is the active GPU on the system.
318 kMultiGpuCategoryActive,
319 // This entry applies if this is any of the GPUs on the system.
320 kMultiGpuCategoryAny,
321 kMultiGpuCategoryNone
324 enum GLType {
325 kGLTypeGL, // This is default on MacOSX, Linux, ChromeOS
326 kGLTypeGLES, // This is default on Android
327 kGLTypeANGLE, // This is default on Windows
328 kGLTypeNone
331 GpuControlListEntry();
332 ~GpuControlListEntry();
334 bool SetId(uint32 id);
336 void SetDisabled(bool disabled);
338 bool SetOsInfo(const std::string& os,
339 const std::string& version_op,
340 const std::string& version_string,
341 const std::string& version_string2);
343 bool SetVendorId(const std::string& vendor_id_string);
345 bool AddDeviceId(const std::string& device_id_string);
347 bool SetMultiGpuStyle(const std::string& multi_gpu_style_string);
349 bool SetMultiGpuCategory(const std::string& multi_gpu_category_string);
351 bool SetGLType(const std::string& gl_type_string);
353 bool SetDriverVendorInfo(const std::string& vendor_value);
355 bool SetDriverVersionInfo(const std::string& version_op,
356 const std::string& version_style,
357 const std::string& version_string,
358 const std::string& version_string2);
360 bool SetDriverDateInfo(const std::string& date_op,
361 const std::string& date_string,
362 const std::string& date_string2);
364 bool SetGLVersionInfo(const std::string& version_op,
365 const std::string& version_string,
366 const std::string& version_string2);
368 bool SetGLVendorInfo(const std::string& vendor_value);
370 bool SetGLRendererInfo(const std::string& renderer_value);
372 bool SetGLExtensionsInfo(const std::string& extensions_value);
374 bool SetGLResetNotificationStrategyInfo(const std::string& op,
375 const std::string& int_string,
376 const std::string& int_string2);
378 bool SetCpuBrand(const std::string& cpu_value);
380 bool SetPerfGraphicsInfo(const std::string& op,
381 const std::string& float_string,
382 const std::string& float_string2);
384 bool SetPerfGamingInfo(const std::string& op,
385 const std::string& float_string,
386 const std::string& float_string2);
388 bool SetPerfOverallInfo(const std::string& op,
389 const std::string& float_string,
390 const std::string& float_string2);
392 bool AddMachineModelName(const std::string& model_name);
394 bool SetMachineModelVersionInfo(const std::string& version_op,
395 const std::string& version_string,
396 const std::string& version_string2);
398 bool SetGpuCountInfo(const std::string& op,
399 const std::string& int_string,
400 const std::string& int_string2);
402 void SetDirectRenderingInfo(bool value);
403 void SetInProcessGPUInfo(bool value);
405 bool SetFeatures(const std::vector<std::string>& features,
406 const FeatureMap& feature_map,
407 bool supports_feature_type_all);
409 void AddException(ScopedGpuControlListEntry exception);
411 // Return true if GL_VERSION string does not fit the entry info
412 // on GL type and GL version.
413 bool GLVersionInfoMismatch(const std::string& gl_version) const;
415 static MultiGpuStyle StringToMultiGpuStyle(const std::string& style);
417 static MultiGpuCategory StringToMultiGpuCategory(
418 const std::string& category);
420 static GLType StringToGLType(const std::string& gl_type);
422 // map a feature_name to feature_id. If the string is not a registered
423 // feature name, return false.
424 static bool StringToFeature(const std::string& feature_name,
425 int* feature_id,
426 const FeatureMap& feature_map);
428 // Return the default GL type, depending on the OS.
429 // See GLType declaration.
430 static GLType GetDefaultGLType();
432 uint32 id_;
433 bool disabled_;
434 std::string description_;
435 std::vector<int> cr_bugs_;
436 std::vector<int> webkit_bugs_;
437 std::vector<std::string> disabled_extensions_;
438 scoped_ptr<OsInfo> os_info_;
439 uint32 vendor_id_;
440 std::vector<uint32> device_id_list_;
441 MultiGpuStyle multi_gpu_style_;
442 MultiGpuCategory multi_gpu_category_;
443 GLType gl_type_;
444 std::string driver_vendor_info_;
445 scoped_ptr<VersionInfo> driver_version_info_;
446 scoped_ptr<VersionInfo> driver_date_info_;
447 scoped_ptr<VersionInfo> gl_version_info_;
448 std::string gl_vendor_info_;
449 std::string gl_renderer_info_;
450 std::string gl_extensions_info_;
451 scoped_ptr<IntInfo> gl_reset_notification_strategy_info_;
452 std::string cpu_brand_;
453 scoped_ptr<FloatInfo> perf_graphics_info_;
454 scoped_ptr<FloatInfo> perf_gaming_info_;
455 scoped_ptr<FloatInfo> perf_overall_info_;
456 std::vector<std::string> machine_model_name_list_;
457 scoped_ptr<VersionInfo> machine_model_version_info_;
458 scoped_ptr<IntInfo> gpu_count_info_;
459 scoped_ptr<BoolInfo> direct_rendering_info_;
460 scoped_ptr<BoolInfo> in_process_gpu_info_;
461 std::set<int> features_;
462 std::vector<ScopedGpuControlListEntry> exceptions_;
465 // Gets the current OS type.
466 static OsType GetOsType();
468 bool LoadList(const base::DictionaryValue& parsed_json, OsFilter os_filter);
470 void Clear();
472 static NumericOp StringToNumericOp(const std::string& op);
474 std::string version_;
475 std::vector<ScopedGpuControlListEntry> entries_;
477 // This records all the blacklist entries that are appliable to the current
478 // user machine. It is updated everytime MakeDecision() is called and is
479 // used later by GetDecisionEntries().
480 std::vector<ScopedGpuControlListEntry> active_entries_;
482 uint32 max_entry_id_;
484 bool needs_more_info_;
486 // The features a GpuControlList recognizes and handles.
487 FeatureMap feature_map_;
488 bool supports_feature_type_all_;
490 bool control_list_logging_enabled_;
491 std::string control_list_logging_name_;
494 } // namespace gpu
496 #endif // GPU_CONFIG_GPU_CONTROL_LIST_H_