gfxConfig.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /* -*- Mode: C++; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef mozilla_gfx_config_gfxConfig_h
  6. #define mozilla_gfx_config_gfxConfig_h
  7. #include "gfxFeature.h"
  8. #include "gfxFallback.h"
  9. #include "mozilla/Assertions.h"
  10. #include "mozilla/Function.h"
  11. namespace mozilla {
  12. namespace gfx {
  13. // Defined in GraphicsMessages.ipdlh.
  14. class FeatureChange;
  15. // Manages the history and state of a graphics feature. The flow of a feature
  16. // is:
  17. // - A default value, set by all.js, gfxPrefs, or gfxPlatform.
  18. // - A user value, set by an external value or user pref.
  19. // - An environment value, determined by system/hardware factors or nsIGfxInfo.
  20. // - A runtime value, determined by any failures encountered after enabling
  21. // the feature.
  22. //
  23. // Each state change for a feature is recorded in this class.
  24. class gfxConfig
  25. {
  26. public:
  27. // Return the full state history of a feature.
  28. static FeatureState& GetFeature(Feature aFeature);
  29. // Query whether a parameter is enabled, taking into account any user or
  30. // runtime overrides. The algorithm works as follow:
  31. //
  32. // 1. If a runtime decision disabled the feature, return false.
  33. // 2. If the user force-enabled the feature, return true.
  34. // 3. If the environment disabled the feature, return false.
  35. // 4. If the user specified a decision, return it.
  36. // 5. Return the base setting for the feature.
  37. static bool IsEnabled(Feature aFeature);
  38. // Query the history of a parameter. ForcedOnByUser returns whether or not
  39. // the user specifically used a "force" preference to enable the parameter.
  40. // IsDisabledByDefault returns whether or not the initial status of the
  41. // feature, before adding user prefs and runtime decisions, was disabled.
  42. static bool IsForcedOnByUser(Feature aFeature);
  43. // This returns true if the feature was disabled by default, or was never
  44. // initialized to begin with.
  45. static bool IsDisabledByDefault(Feature aFeature);
  46. // Query the status value of a parameter. This is computed similar to
  47. // IsEnabled:
  48. //
  49. // 1. If a runtime failure was set, return it.
  50. // 2. If the user force-enabled the feature, return ForceEnabled.
  51. // 3. If an environment status was set, return it.
  52. // 4. If a user status was set, return it.
  53. // 5. Return the default status.
  54. static FeatureStatus GetValue(Feature aFeature);
  55. // Initialize the base value of a parameter. The return value is aEnable.
  56. static bool SetDefault(Feature aFeature,
  57. bool aEnable,
  58. FeatureStatus aDisableStatus,
  59. const char* aDisableMessage);
  60. static void DisableByDefault(Feature aFeature,
  61. FeatureStatus aDisableStatus,
  62. const char* aDisableMessage,
  63. const nsACString& aFailureId = EmptyCString());
  64. static void EnableByDefault(Feature aFeature);
  65. // Inherit a computed value from another process.
  66. static void Inherit(Feature aFeature, FeatureStatus aStatus);
  67. // Set a environment status that overrides both the default and user
  68. // statuses; this should be used to disable features based on system
  69. // or hardware problems that can be determined up-front. The only
  70. // status that can override this decision is the user force-enabling
  71. // the feature.
  72. static void Disable(Feature aFeature,
  73. FeatureStatus aStatus,
  74. const char* aMessage,
  75. const nsACString& aFailureId = EmptyCString());
  76. // Given a preference name, infer the default value and whether or not the
  77. // user has changed it. |aIsEnablePref| specifies whether or not the pref
  78. // is intended to enable a feature (true), or disable it (false).
  79. static void SetDefaultFromPref(Feature aFeature,
  80. const char* aPrefName,
  81. bool aIsEnablePref,
  82. bool aDefaultValue);
  83. // Disable a parameter based on a runtime decision. This permanently
  84. // disables the feature, since runtime decisions override all other
  85. // decisions.
  86. static void SetFailed(Feature aFeature,
  87. FeatureStatus aStatus,
  88. const char* aMessage,
  89. const nsACString& aFailureId = EmptyCString());
  90. // Force a feature to be disabled permanently. This is the same as
  91. // SetFailed(), but the name may be clearer depending on the context.
  92. static void ForceDisable(Feature aFeature,
  93. FeatureStatus aStatus,
  94. const char* aMessage,
  95. const nsACString& aFailureId = EmptyCString())
  96. {
  97. SetFailed(aFeature, aStatus, aMessage, aFailureId);
  98. }
  99. // Convenience helpers for SetFailed().
  100. static bool MaybeSetFailed(Feature aFeature,
  101. bool aEnable,
  102. FeatureStatus aDisableStatus,
  103. const char* aDisableMessage,
  104. const nsACString& aFailureId = EmptyCString())
  105. {
  106. if (!aEnable) {
  107. SetFailed(aFeature, aDisableStatus, aDisableMessage, aFailureId);
  108. return false;
  109. }
  110. return true;
  111. }
  112. // Convenience helper for SetFailed().
  113. static bool MaybeSetFailed(Feature aFeature,
  114. FeatureStatus aStatus,
  115. const char* aDisableMessage,
  116. const nsACString& aFailureId = EmptyCString())
  117. {
  118. return MaybeSetFailed(
  119. aFeature,
  120. (aStatus != FeatureStatus::Available &&
  121. aStatus != FeatureStatus::ForceEnabled),
  122. aStatus,
  123. aDisableMessage, aFailureId);
  124. }
  125. // Re-enables a feature that was previously disabled, by attaching it to a
  126. // fallback. The fallback inherits the message that was used for disabling
  127. // the feature. This can be used, for example, when D3D11 fails at runtime
  128. // but we acquire a second, successful device with WARP.
  129. static void Reenable(Feature aFeature, Fallback aFallback);
  130. // Same as SetDefault, except if the feature already has a default value
  131. // set, the new value will be set as a runtime value. This is useful for
  132. // when the base value can change (for example, via an update from the
  133. // parent process).
  134. static bool InitOrUpdate(Feature aFeature,
  135. bool aEnable,
  136. FeatureStatus aDisableStatus,
  137. const char* aDisableMessage);
  138. // Set a user status that overrides the base value (but not runtime value)
  139. // of a parameter.
  140. static void UserEnable(Feature aFeature, const char* aMessage);
  141. static void UserForceEnable(Feature aFeature, const char* aMessage);
  142. static void UserDisable(Feature aFeature, const char* aMessage, const nsACString& aFailureId = EmptyCString());
  143. // Query whether a fallback has been toggled.
  144. static bool UseFallback(Fallback aFallback);
  145. // Enable a fallback.
  146. static void EnableFallback(Fallback aFallback, const char* aMessage);
  147. // Run a callback for each initialized FeatureState.
  148. typedef mozilla::function<void(const char* aName,
  149. const char* aDescription,
  150. FeatureState& aFeature)> FeatureIterCallback;
  151. static void ForEachFeature(const FeatureIterCallback& aCallback);
  152. // Run a callback for each enabled fallback.
  153. typedef mozilla::function<void(const char* aName, const char* aMsg)>
  154. FallbackIterCallback;
  155. static void ForEachFallback(const FallbackIterCallback& aCallback);
  156. // Get the most descriptive failure id message for this feature.
  157. static const nsCString& GetFailureId(Feature aFeature);
  158. static void ImportChange(Feature aFeature, const FeatureChange& aChange);
  159. static void Init();
  160. static void Shutdown();
  161. private:
  162. void ForEachFallbackImpl(const FallbackIterCallback& aCallback);
  163. private:
  164. FeatureState& GetState(Feature aFeature) {
  165. MOZ_ASSERT(size_t(aFeature) < kNumFeatures);
  166. return mFeatures[size_t(aFeature)];
  167. }
  168. const FeatureState& GetState(Feature aFeature) const {
  169. MOZ_ASSERT(size_t(aFeature) < kNumFeatures);
  170. return mFeatures[size_t(aFeature)];
  171. }
  172. bool UseFallbackImpl(Fallback aFallback) const;
  173. void EnableFallbackImpl(Fallback aFallback, const char* aMessage);
  174. private:
  175. static const size_t kNumFeatures = size_t(Feature::NumValues);
  176. static const size_t kNumFallbacks = size_t(Fallback::NumValues);
  177. private:
  178. FeatureState mFeatures[kNumFeatures];
  179. uint64_t mFallbackBits;
  180. private:
  181. struct FallbackLogEntry {
  182. Fallback mFallback;
  183. char mMessage[80];
  184. };
  185. FallbackLogEntry mFallbackLog[kNumFallbacks];
  186. size_t mNumFallbackLogEntries;
  187. };
  188. } // namespace gfx
  189. } // namespace mozilla
  190. #endif // mozilla_gfx_config_gfxConfig_h