- add sources.
[platform/framework/web/crosswalk.git] / src / gpu / config / gpu_control_list.h
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   bool LoadList(const std::string& browser_version_string,
50                 const std::string& json_context, OsFilter os_filter);
51
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);
59
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,
64                           bool disabled) const;
65
66   // Returns the description and bugs from active entries from the last
67   // MakeDecision() call.
68   //
69   // Each problems has:
70   // {
71   //    "description": "Your GPU is too old",
72   //    "crBugs": [1234],
73   //    "webkitBugs": []
74   // }
75   void GetReasons(base::ListValue* problem_list) const;
76
77   // Return the largest entry id.  This is used for histogramming.
78   uint32 max_entry_id() const;
79
80   // Returns the version of the control list.
81   std::string version() const;
82
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_; }
87
88   // Returns the number of entries.  This is only for tests.
89   size_t num_entries() const;
90
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);
95
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;
101   }
102
103  private:
104   friend class GpuControlListEntryTest;
105   friend class MachineModelInfoTest;
106   friend class NumberInfoTest;
107   friend class OsInfoTest;
108   friend class StringInfoTest;
109   friend class VersionInfoTest;
110
111   enum BrowserVersionSupport {
112     kSupported,
113     kUnsupported,
114     kMalformed
115   };
116
117   enum NumericOp {
118     kBetween,  // <= * <=
119     kEQ,  // =
120     kLT,  // <
121     kLE,  // <=
122     kGT,  // >
123     kGE,  // >=
124     kAny,
125     kUnknown  // Indicates the data is invalid.
126   };
127
128   class GPU_EXPORT VersionInfo {
129    public:
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);
135     ~VersionInfo();
136
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;
142
143     // Determine if the version_style is lexical.
144     bool IsLexical() const;
145
146     // Determines if the VersionInfo contains valid information.
147     bool IsValid() const;
148
149    private:
150     enum VersionStyle {
151       kVersionStyleNumerical,
152       kVersionStyleLexical,
153       kVersionStyleUnknown
154     };
155
156     static VersionStyle StringToVersionStyle(const std::string& version_style);
157
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);
171
172     NumericOp op_;
173     VersionStyle version_style_;
174     std::vector<std::string> version_;
175     std::vector<std::string> version2_;
176   };
177
178   class GPU_EXPORT OsInfo {
179    public:
180     OsInfo(const std::string& os,
181            const std::string& version_op,
182            const std::string& version_string,
183            const std::string& version_string2);
184     ~OsInfo();
185
186     // Determines if a given os/version is included in the OsInfo set.
187     bool Contains(OsType type, const std::string& version) const;
188
189     // Determines if the VersionInfo contains valid information.
190     bool IsValid() const;
191
192     OsType type() const;
193
194     // Maps string to OsType; returns kOsUnknown if it's not a valid os.
195     static OsType StringToOsType(const std::string& os);
196
197    private:
198     OsType type_;
199     scoped_ptr<VersionInfo> version_info_;
200   };
201
202   class GPU_EXPORT StringInfo {
203    public:
204     StringInfo(const std::string& string_op, const std::string& string_value);
205
206     // Determines if a given string is included in the StringInfo.
207     bool Contains(const std::string& value) const;
208
209     // Determines if the StringInfo contains valid information.
210     bool IsValid() const;
211
212    private:
213     enum Op {
214       kContains,
215       kBeginWith,
216       kEndWith,
217       kEQ,  // =
218       kUnknown  // Indicates StringInfo data is invalid.
219     };
220
221     // Maps string to Op; returns kUnknown if it's not a valid Op.
222     static Op StringToOp(const std::string& string_op);
223
224     Op op_;
225     std::string value_;
226   };
227
228   class GPU_EXPORT FloatInfo {
229    public:
230     FloatInfo(const std::string& float_op,
231               const std::string& float_value,
232               const std::string& float_value2);
233
234     // Determines if a given float is included in the FloatInfo.
235     bool Contains(float value) const;
236
237     // Determines if the FloatInfo contains valid information.
238     bool IsValid() const;
239
240    private:
241     NumericOp op_;
242     float value_;
243     float value2_;
244   };
245
246   class GPU_EXPORT IntInfo {
247    public:
248     IntInfo(const std::string& int_op,
249             const std::string& int_value,
250             const std::string& int_value2);
251
252     // Determines if a given int is included in the IntInfo.
253     bool Contains(int value) const;
254
255     // Determines if the IntInfo contains valid information.
256     bool IsValid() const;
257
258    private:
259     NumericOp op_;
260     int value_;
261     int value2_;
262   };
263
264   class GPU_EXPORT MachineModelInfo {
265    public:
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);
271     ~MachineModelInfo();
272
273     // Determines if a given name/version is included in the MachineModelInfo.
274     bool Contains(const std::string& name, const std::string& version) const;
275
276     // Determines if the MachineModelInfo contains valid information.
277     bool IsValid() const;
278
279    private:
280     scoped_ptr<StringInfo> name_info_;
281     scoped_ptr<VersionInfo> version_info_;
282   };
283
284   class GpuControlListEntry;
285   typedef scoped_refptr<GpuControlListEntry> ScopedGpuControlListEntry;
286
287   typedef base::hash_map<std::string, int> FeatureMap;
288
289   class GPU_EXPORT GpuControlListEntry
290       : public base::RefCounted<GpuControlListEntry> {
291    public:
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);
298
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;
303
304     // Determines if a given os/gc/machine_model/driver is included in the
305     // Entry set.
306     bool Contains(OsType os_type, const std::string& os_version,
307                   const GPUInfo& gpu_info) const;
308
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;
312
313     // Returns the OsType.
314     OsType GetOsType() const;
315
316     // Returns the entry's unique id.  0 is reserved.
317     uint32 id() const;
318
319     // Returns whether the entry is disabled.
320     bool disabled() const;
321
322     // Returns the description of the entry
323     const std::string& description() const { return description_; }
324
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_; }
328
329     // Returns the blacklisted features in this entry.
330     const std::set<int>& features() const;
331
332    private:
333     friend class base::RefCounted<GpuControlListEntry>;
334
335     enum MultiGpuStyle {
336       kMultiGpuStyleOptimus,
337       kMultiGpuStyleAMDSwitchable,
338       kMultiGpuStyleNone
339     };
340
341     enum MultiGpuCategory {
342       kMultiGpuCategoryPrimary,
343       kMultiGpuCategorySecondary,
344       kMultiGpuCategoryAny,
345       kMultiGpuCategoryNone
346     };
347
348     GpuControlListEntry();
349     ~GpuControlListEntry();
350
351     bool SetId(uint32 id);
352
353     void SetDisabled(bool disabled);
354
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);
359
360     bool SetVendorId(const std::string& vendor_id_string);
361
362     bool AddDeviceId(const std::string& device_id_string);
363
364     bool SetMultiGpuStyle(const std::string& multi_gpu_style_string);
365
366     bool SetMultiGpuCategory(const std::string& multi_gpu_category_string);
367
368     bool SetDriverVendorInfo(const std::string& vendor_op,
369                              const std::string& vendor_value);
370
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);
375
376     bool SetDriverDateInfo(const std::string& date_op,
377                            const std::string& date_string,
378                            const std::string& date_string2);
379
380     bool SetGLVendorInfo(const std::string& vendor_op,
381                          const std::string& vendor_value);
382
383     bool SetGLRendererInfo(const std::string& renderer_op,
384                            const std::string& renderer_value);
385
386     bool SetGLExtensionsInfo(const std::string& extensions_op,
387                              const std::string& extensions_value);
388
389     bool SetGLResetNotificationStrategyInfo(const std::string& op,
390                                             const std::string& int_string,
391                                             const std::string& int_string2);
392
393     bool SetCpuBrand(const std::string& cpu_op,
394                      const std::string& cpu_value);
395
396     bool SetPerfGraphicsInfo(const std::string& op,
397                              const std::string& float_string,
398                              const std::string& float_string2);
399
400     bool SetPerfGamingInfo(const std::string& op,
401                            const std::string& float_string,
402                            const std::string& float_string2);
403
404     bool SetPerfOverallInfo(const std::string& op,
405                             const std::string& float_string,
406                             const std::string& float_string2);
407
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);
413
414     bool SetGpuCountInfo(const std::string& op,
415                          const std::string& int_string,
416                          const std::string& int_string2);
417
418     bool SetFeatures(const std::vector<std::string>& features,
419                      const FeatureMap& feature_map,
420                      bool supports_feature_type_all);
421
422     void AddException(ScopedGpuControlListEntry exception);
423
424     static MultiGpuStyle StringToMultiGpuStyle(const std::string& style);
425
426     static MultiGpuCategory StringToMultiGpuCategory(
427         const std::string& category);
428
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,
432                                 int* feature_id,
433                                 const FeatureMap& feature_map);
434
435     uint32 id_;
436     bool disabled_;
437     std::string description_;
438     std::vector<int> cr_bugs_;
439     std::vector<int> webkit_bugs_;
440     scoped_ptr<OsInfo> os_info_;
441     uint32 vendor_id_;
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_;
460   };
461
462   // Gets the current OS type.
463   static OsType GetOsType();
464
465   bool LoadList(const base::DictionaryValue& parsed_json, OsFilter os_filter);
466
467   void Clear();
468
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);
474
475   static NumericOp StringToNumericOp(const std::string& op);
476
477   std::string version_;
478   std::vector<ScopedGpuControlListEntry> entries_;
479
480   std::string browser_version_;
481
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_;
486
487   uint32 max_entry_id_;
488
489   bool needs_more_info_;
490
491   // The features a GpuControlList recognizes and handles.
492   FeatureMap feature_map_;
493   bool supports_feature_type_all_;
494
495   bool control_list_logging_enabled_;
496   std::string control_list_logging_name_;
497 };
498
499 }  // namespace gpu
500
501 #endif  // GPU_CONFIG_GPU_CONTROL_LIST_H_
502