Home | History | Annotate | Download | only in config
      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 FloatInfo {
    196    public:
    197     FloatInfo(const std::string& float_op,
    198               const std::string& float_value,
    199               const std::string& float_value2);
    200 
    201     // Determines if a given float is included in the FloatInfo.
    202     bool Contains(float value) const;
    203 
    204     // Determines if the FloatInfo contains valid information.
    205     bool IsValid() const;
    206 
    207    private:
    208     NumericOp op_;
    209     float value_;
    210     float value2_;
    211   };
    212 
    213   class GPU_EXPORT IntInfo {
    214    public:
    215     IntInfo(const std::string& int_op,
    216             const std::string& int_value,
    217             const std::string& int_value2);
    218 
    219     // Determines if a given int is included in the IntInfo.
    220     bool Contains(int value) const;
    221 
    222     // Determines if the IntInfo contains valid information.
    223     bool IsValid() const;
    224 
    225    private:
    226     NumericOp op_;
    227     int value_;
    228     int value2_;
    229   };
    230 
    231   class GPU_EXPORT BoolInfo {
    232    public:
    233     explicit BoolInfo(bool value);
    234 
    235     // Determines if a given bool is included in the BoolInfo.
    236     bool Contains(bool value) const;
    237 
    238    private:
    239     bool value_;
    240   };
    241 
    242   class GpuControlListEntry;
    243   typedef scoped_refptr<GpuControlListEntry> ScopedGpuControlListEntry;
    244 
    245   typedef base::hash_map<std::string, int> FeatureMap;
    246 
    247   class GPU_EXPORT GpuControlListEntry
    248       : public base::RefCounted<GpuControlListEntry> {
    249    public:
    250     // Constructs GpuControlListEntry from DictionaryValue loaded from json.
    251     // Top-level entry must have an id number.  Others are exceptions.
    252     static ScopedGpuControlListEntry GetEntryFromValue(
    253         const base::DictionaryValue* value, bool top_level,
    254         const FeatureMap& feature_map,
    255         bool supports_feature_type_all);
    256 
    257     // Logs a control list match for this rule in the list identified by
    258     // |control_list_logging_name|.
    259     void LogControlListMatch(
    260         const std::string& control_list_logging_name) const;
    261 
    262     // Determines if a given os/gc/machine_model/driver is included in the
    263     // Entry set.
    264     bool Contains(OsType os_type, const std::string& os_version,
    265                   const GPUInfo& gpu_info) const;
    266 
    267     // Determines whether we needs more gpu info to make the blacklisting
    268     // decision.  It should only be checked if Contains() returns true.
    269     bool NeedsMoreInfo(const GPUInfo& gpu_info) const;
    270 
    271     // Returns the OsType.
    272     OsType GetOsType() const;
    273 
    274     // Returns the entry's unique id.  0 is reserved.
    275     uint32 id() const;
    276 
    277     // Returns whether the entry is disabled.
    278     bool disabled() const;
    279 
    280     // Returns the description of the entry
    281     const std::string& description() const { return description_; }
    282 
    283     // Returns a list of Chromium and Webkit bugs applicable to this entry
    284     const std::vector<int>& cr_bugs() const { return cr_bugs_; }
    285     const std::vector<int>& webkit_bugs() const { return webkit_bugs_; }
    286 
    287     // Returns the blacklisted features in this entry.
    288     const std::set<int>& features() const;
    289 
    290     // Returns a list of blacklisted feature names in this entry.
    291     void GetFeatureNames(base::ListValue* feature_names,
    292                          const FeatureMap& feature_map,
    293                          bool supports_feature_type_all) const;
    294 
    295    private:
    296     friend class base::RefCounted<GpuControlListEntry>;
    297 
    298     enum MultiGpuStyle {
    299       kMultiGpuStyleOptimus,
    300       kMultiGpuStyleAMDSwitchable,
    301       kMultiGpuStyleAMDSwitchableIntegrated,
    302       kMultiGpuStyleAMDSwitchableDiscrete,
    303       kMultiGpuStyleNone
    304     };
    305 
    306     enum MultiGpuCategory {
    307       // This entry applies if this is the primary GPU on the system.
    308       kMultiGpuCategoryPrimary,
    309       // This entry applies if this is a secondary GPU on the system.
    310       kMultiGpuCategorySecondary,
    311       // This entry applies if this is the active GPU on the system.
    312       kMultiGpuCategoryActive,
    313       // This entry applies if this is any of the GPUs on the system.
    314       kMultiGpuCategoryAny,
    315       kMultiGpuCategoryNone
    316     };
    317 
    318     enum GLType {
    319       kGLTypeGL,  // This is default on MacOSX, Linux, ChromeOS
    320       kGLTypeGLES,  // This is default on Android
    321       kGLTypeANGLE,  // This is default on Windows
    322       kGLTypeNone
    323     };
    324 
    325     GpuControlListEntry();
    326     ~GpuControlListEntry();
    327 
    328     bool SetId(uint32 id);
    329 
    330     void SetDisabled(bool disabled);
    331 
    332     bool SetOsInfo(const std::string& os,
    333                    const std::string& version_op,
    334                    const std::string& version_string,
    335                    const std::string& version_string2);
    336 
    337     bool SetVendorId(const std::string& vendor_id_string);
    338 
    339     bool AddDeviceId(const std::string& device_id_string);
    340 
    341     bool SetMultiGpuStyle(const std::string& multi_gpu_style_string);
    342 
    343     bool SetMultiGpuCategory(const std::string& multi_gpu_category_string);
    344 
    345     bool SetGLType(const std::string& gl_type_string);
    346 
    347     bool SetDriverVendorInfo(const std::string& vendor_value);
    348 
    349     bool SetDriverVersionInfo(const std::string& version_op,
    350                               const std::string& version_style,
    351                               const std::string& version_string,
    352                               const std::string& version_string2);
    353 
    354     bool SetDriverDateInfo(const std::string& date_op,
    355                            const std::string& date_string,
    356                            const std::string& date_string2);
    357 
    358     bool SetGLVersionInfo(const std::string& version_op,
    359                           const std::string& version_string,
    360                           const std::string& version_string2);
    361 
    362     bool SetGLVendorInfo(const std::string& vendor_value);
    363 
    364     bool SetGLRendererInfo(const std::string& renderer_value);
    365 
    366     bool SetGLExtensionsInfo(const std::string& extensions_value);
    367 
    368     bool SetGLResetNotificationStrategyInfo(const std::string& op,
    369                                             const std::string& int_string,
    370                                             const std::string& int_string2);
    371 
    372     bool SetCpuBrand(const std::string& cpu_value);
    373 
    374     bool SetPerfGraphicsInfo(const std::string& op,
    375                              const std::string& float_string,
    376                              const std::string& float_string2);
    377 
    378     bool SetPerfGamingInfo(const std::string& op,
    379                            const std::string& float_string,
    380                            const std::string& float_string2);
    381 
    382     bool SetPerfOverallInfo(const std::string& op,
    383                             const std::string& float_string,
    384                             const std::string& float_string2);
    385 
    386     bool AddMachineModelName(const std::string& model_name);
    387 
    388     bool SetMachineModelVersionInfo(const std::string& version_op,
    389                                     const std::string& version_string,
    390                                     const std::string& version_string2);
    391 
    392     bool SetGpuCountInfo(const std::string& op,
    393                          const std::string& int_string,
    394                          const std::string& int_string2);
    395 
    396     void SetDirectRenderingInfo(bool value);
    397 
    398     bool SetFeatures(const std::vector<std::string>& features,
    399                      const FeatureMap& feature_map,
    400                      bool supports_feature_type_all);
    401 
    402     void AddException(ScopedGpuControlListEntry exception);
    403 
    404     // Return true if GL_VERSION string does not fit the entry info
    405     // on GL type and GL version.
    406     bool GLVersionInfoMismatch(const std::string& gl_version) const;
    407 
    408     static MultiGpuStyle StringToMultiGpuStyle(const std::string& style);
    409 
    410     static MultiGpuCategory StringToMultiGpuCategory(
    411         const std::string& category);
    412 
    413     static GLType StringToGLType(const std::string& gl_type);
    414 
    415     // map a feature_name to feature_id. If the string is not a registered
    416     // feature name, return false.
    417     static bool StringToFeature(const std::string& feature_name,
    418                                 int* feature_id,
    419                                 const FeatureMap& feature_map);
    420 
    421     // Return the default GL type, depending on the OS.
    422     // See GLType declaration.
    423     static GLType GetDefaultGLType();
    424 
    425     uint32 id_;
    426     bool disabled_;
    427     std::string description_;
    428     std::vector<int> cr_bugs_;
    429     std::vector<int> webkit_bugs_;
    430     scoped_ptr<OsInfo> os_info_;
    431     uint32 vendor_id_;
    432     std::vector<uint32> device_id_list_;
    433     MultiGpuStyle multi_gpu_style_;
    434     MultiGpuCategory multi_gpu_category_;
    435     GLType gl_type_;
    436     std::string driver_vendor_info_;
    437     scoped_ptr<VersionInfo> driver_version_info_;
    438     scoped_ptr<VersionInfo> driver_date_info_;
    439     scoped_ptr<VersionInfo> gl_version_info_;
    440     std::string gl_vendor_info_;
    441     std::string gl_renderer_info_;
    442     std::string gl_extensions_info_;
    443     scoped_ptr<IntInfo> gl_reset_notification_strategy_info_;
    444     std::string cpu_brand_;
    445     scoped_ptr<FloatInfo> perf_graphics_info_;
    446     scoped_ptr<FloatInfo> perf_gaming_info_;
    447     scoped_ptr<FloatInfo> perf_overall_info_;
    448     std::vector<std::string> machine_model_name_list_;
    449     scoped_ptr<VersionInfo> machine_model_version_info_;
    450     scoped_ptr<IntInfo> gpu_count_info_;
    451     scoped_ptr<BoolInfo> direct_rendering_info_;
    452     std::set<int> features_;
    453     std::vector<ScopedGpuControlListEntry> exceptions_;
    454   };
    455 
    456   // Gets the current OS type.
    457   static OsType GetOsType();
    458 
    459   bool LoadList(const base::DictionaryValue& parsed_json, OsFilter os_filter);
    460 
    461   void Clear();
    462 
    463   static NumericOp StringToNumericOp(const std::string& op);
    464 
    465   std::string version_;
    466   std::vector<ScopedGpuControlListEntry> entries_;
    467 
    468   // This records all the blacklist entries that are appliable to the current
    469   // user machine.  It is updated everytime MakeDecision() is called and is
    470   // used later by GetDecisionEntries().
    471   std::vector<ScopedGpuControlListEntry> active_entries_;
    472 
    473   uint32 max_entry_id_;
    474 
    475   bool needs_more_info_;
    476 
    477   // The features a GpuControlList recognizes and handles.
    478   FeatureMap feature_map_;
    479   bool supports_feature_type_all_;
    480 
    481   bool control_list_logging_enabled_;
    482   std::string control_list_logging_name_;
    483 };
    484 
    485 }  // namespace gpu
    486 
    487 #endif  // GPU_CONFIG_GPU_CONTROL_LIST_H_
    488 
    489