tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

gfxFeature.h (8355B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 #ifndef mozilla_gfx_config_gfxFeature_h
      7 #define mozilla_gfx_config_gfxFeature_h
      8 
      9 #include <functional>
     10 #include <stdint.h>
     11 #include "gfxTelemetry.h"
     12 #include "mozilla/Assertions.h"
     13 #include "mozilla/Maybe.h"
     14 #include "nsString.h"
     15 
     16 namespace mozilla {
     17 namespace gfx {
     18 
     19 #define GFX_FEATURE_MAP(_)                                                   \
     20  /* Name,                        Type,         Description */               \
     21  _(HW_COMPOSITING, Feature, "Compositing")                                  \
     22  _(D3D11_COMPOSITING, Feature, "Direct3D11 Compositing")                    \
     23  _(OPENGL_COMPOSITING, Feature, "OpenGL Compositing")                       \
     24  _(D3D11_HW_ANGLE, Feature, "Direct3D11 hardware ANGLE")                    \
     25  _(DIRECT_DRAW, Feature, "DirectDraw")                                      \
     26  _(GPU_PROCESS, Feature, "GPU Process")                                     \
     27  _(WEBRENDER, Feature, "WebRender")                                         \
     28  _(WEBRENDER_COMPOSITOR, Feature, "WebRender native compositor")            \
     29  _(WEBRENDER_PARTIAL, Feature, "WebRender partial present")                 \
     30  _(WEBRENDER_SHADER_CACHE, Feature, "WebRender shader disk cache")          \
     31  _(WEBRENDER_OPTIMIZED_SHADERS, Feature, "WebRender optimized shaders")     \
     32  _(WEBRENDER_ANGLE, Feature, "WebRender ANGLE")                             \
     33  _(WEBRENDER_DCOMP_PRESENT, Feature, "WebRender DirectComposition")         \
     34  _(WEBRENDER_SCISSORED_CACHE_CLEARS, Feature,                               \
     35    "WebRender scissored cache clears")                                      \
     36  _(OMTP, Feature, "Off Main Thread Painting")                               \
     37  _(WEBGPU, Feature, "WebGPU")                                               \
     38  _(X11_EGL, Feature, "X11 EGL")                                             \
     39  _(DMABUF, Feature, "DMABUF")                                               \
     40  _(WINDOW_OCCLUSION, Feature, "WINDOW_OCCLUSION")                           \
     41  _(HARDWARE_VIDEO_DECODING, Feature, "Hardware video decoding")             \
     42  _(HARDWARE_VIDEO_ENCODING, Feature, "Hardware video encoding")             \
     43  _(VIDEO_HARDWARE_OVERLAY, Feature, "hardware decoded video overlay")       \
     44  _(VIDEO_SOFTWARE_OVERLAY, Feature, "software decoded video overlay")       \
     45  _(HW_DECODED_VIDEO_ZERO_COPY, Feature, "Hardware decoded video zero copy") \
     46  _(VP8_HW_DECODE, Feature, "VP8 hardware decoding")                         \
     47  _(VP9_HW_DECODE, Feature, "VP9 hardware decoding")                         \
     48  _(DMABUF_SURFACE_EXPORT, Feature, "WebGL DMABuf surface export")           \
     49  _(REUSE_DECODER_DEVICE, Feature, "Reuse decoder device")                   \
     50  _(BACKDROP_FILTER, Feature, "Backdrop filter")                             \
     51  _(CANVAS_RENDERER_THREAD, Feature, "canvas renderer thread")               \
     52  _(ACCELERATED_CANVAS2D, Feature, "Accelerated Canvas2D")                   \
     53  _(H264_HW_DECODE, Feature, "H.264 hardware decoding")                      \
     54  _(AV1_HW_DECODE, Feature, "AV1 hardware decoding")                         \
     55  _(HEVC_HW_DECODE, Feature, "HEVC hardware decoding")                       \
     56  _(DMABUF_WEBGL, Feature, "DMABuf for WebGL")                               \
     57  _(VP8_HW_ENCODE, Feature, "VP8 hardware encoding")                         \
     58  _(VP9_HW_ENCODE, Feature, "VP9 hardware encoding")                         \
     59  _(H264_HW_ENCODE, Feature, "H.264 hardware encoding")                      \
     60  _(AV1_HW_ENCODE, Feature, "AV1 hardware encoding")                         \
     61  _(HEVC_HW_ENCODE, Feature, "HEVC hardware encoding")                       \
     62  _(WMF_HW_DRM, Feature, "Windows Media Foundation hardware DRM")            \
     63  _(GL_NORM16_TEXTURES, Feature, "OpenGL normalized 16-bit texture formats") \
     64  _(WEBGPU_EXTERNAL_TEXTURE, Feature, "WebGPU external textures")            \
     65  _(MESA_THREADING, Feature, "Mesa glthread enabled")                        \
     66  /* Add new entries above this comment */
     67 
     68 enum class Feature : uint32_t {
     69 #define MAKE_ENUM(name, type, desc) name,
     70  GFX_FEATURE_MAP(MAKE_ENUM)
     71 #undef MAKE_ENUM
     72      NumValues
     73 };
     74 
     75 class FeatureState {
     76  friend class gfxConfig;
     77  friend class GfxConfigManager;  // for testing
     78 
     79 public:
     80  FeatureState() { Reset(); }
     81 
     82  bool IsEnabled() const;
     83  FeatureStatus GetValue() const;
     84 
     85  void EnableByDefault();
     86  void DisableByDefault(FeatureStatus aStatus, const char* aMessage,
     87                        const nsACString& aFailureId);
     88  bool SetDefault(bool aEnable, FeatureStatus aDisableStatus,
     89                  const char* aDisableMessage);
     90  bool InitOrUpdate(bool aEnable, FeatureStatus aDisableStatus,
     91                    const char* aMessage);
     92  void SetDefaultFromPref(const char* aPrefName, bool aIsEnablePref,
     93                          bool aDefaultValue, Maybe<bool> aUserValue);
     94  void SetDefaultFromPref(const char* aPrefName, bool aIsEnablePref,
     95                          bool aDefaultValue);
     96  void UserEnable(const char* aMessage);
     97  void UserForceEnable(const char* aMessage);
     98  void UserDisable(const char* aMessage, const nsACString& aFailureId);
     99  void Disable(FeatureStatus aStatus, const char* aMessage,
    100               const nsACString& aFailureId);
    101  void ForceDisable(FeatureStatus aStatus, const char* aMessage,
    102                    const nsACString& aFailureId) {
    103    SetFailed(aStatus, aMessage, aFailureId);
    104  }
    105  void SetFailed(FeatureStatus aStatus, const char* aMessage,
    106                 const nsACString& aFailureId);
    107  bool MaybeSetFailed(bool aEnable, FeatureStatus aStatus, const char* aMessage,
    108                      const nsACString& aFailureId);
    109  bool MaybeSetFailed(FeatureStatus aStatus, const char* aMessage,
    110                      const nsACString& aFailureId);
    111 
    112  // aType is "base", "user", "env", or "runtime".
    113  // aMessage may be null.
    114  typedef std::function<void(const char* aType, FeatureStatus aStatus,
    115                             const char* aMessage, const nsCString& aFailureId)>
    116      StatusIterCallback;
    117  void ForEachStatusChange(const StatusIterCallback& aCallback) const;
    118 
    119  const char* GetFailureMessage() const;
    120  const nsCString& GetFailureId() const;
    121  nsCString GetStatusAndFailureIdString() const;
    122 
    123  bool DisabledByDefault() const;
    124 
    125  // Clear all state.
    126  void Reset();
    127 
    128 private:
    129  void SetUser(FeatureStatus aStatus, const char* aMessage,
    130               const nsACString& aFailureId);
    131  void SetEnvironment(FeatureStatus aStatus, const char* aMessage,
    132                      const nsACString& aFailureId);
    133  void SetRuntime(FeatureStatus aStatus, const char* aMessage,
    134                  const nsACString& aFailureId);
    135  bool IsForcedOnByUser() const;
    136  const char* GetRuntimeMessage() const;
    137  bool IsInitialized() const { return mDefault.IsInitialized(); }
    138 
    139  void AssertInitialized() const { MOZ_ASSERT(IsInitialized()); }
    140 
    141 private:
    142  struct Instance {
    143    char mMessage[64];
    144    FeatureStatus mStatus;
    145    nsCString mFailureId;
    146 
    147    void Set(FeatureStatus aStatus);
    148    void Set(FeatureStatus aStatus, const char* aMessage,
    149             const nsACString& aFailureId);
    150    bool IsInitialized() const { return mStatus != FeatureStatus::Unused; }
    151    const char* MessageOrNull() const {
    152      return mMessage[0] != '\0' ? mMessage : nullptr;
    153    }
    154    const char* Message() const {
    155      MOZ_ASSERT(MessageOrNull());
    156      return mMessage;
    157    }
    158    const nsCString& FailureId() const { return mFailureId; }
    159  };
    160 
    161  // The default state is the state we decide on startup, based on the operating
    162  // system or a base preference.
    163  //
    164  // The user state factors in any changes to preferences that the user made.
    165  //
    166  // The environment state factors in any additional decisions made, such as
    167  // availability or blocklisting.
    168  //
    169  // The runtime state factors in any problems discovered at runtime.
    170  Instance mDefault;
    171  Instance mUser;
    172  Instance mEnvironment;
    173  Instance mRuntime;
    174 };
    175 
    176 }  // namespace gfx
    177 }  // namespace mozilla
    178 
    179 #endif  // mozilla_gfx_config_gfxFeature_h