gfxVars.h (13710B)
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_gfxVars_h 7 #define mozilla_gfx_config_gfxVars_h 8 9 #include <stdint.h> 10 #include "mozilla/Assertions.h" 11 #include "mozilla/StaticPtr.h" 12 #include "mozilla/gfx/GraphicsMessages.h" 13 #include "mozilla/gfx/Point.h" 14 #include "mozilla/gfx/Types.h" 15 #include "nsTArray.h" 16 #include "nsXULAppAPI.h" 17 18 namespace mozilla::gfx { 19 20 class gfxVarReceiver; 21 class MOZ_STACK_CLASS gfxVarsCollectUpdates; 22 23 // Generator for graphics vars. 24 #define GFX_VARS_LIST(_) \ 25 /* C++ Name, Data Type, Default Value */ \ 26 _(AllowEglRbab, bool, true) \ 27 _(AllowWebgl2, bool, true) \ 28 _(AllowWebglAccelAngle, bool, true) \ 29 _(AllowWebglOop, bool, true) \ 30 _(BrowserTabsRemoteAutostart, bool, false) \ 31 _(ContentBackend, BackendType, BackendType::NONE) \ 32 _(SoftwareBackend, BackendType, BackendType::NONE) \ 33 _(OffscreenFormat, gfxImageFormat, \ 34 mozilla::gfx::SurfaceFormat::X8R8G8B8_UINT32) \ 35 _(RequiresAcceleratedGLContextForCompositorOGL, bool, false) \ 36 _(CanUseHardwareVideoDecoding, bool, false) \ 37 _(CanUseHardwareVideoEncoding, bool, false) \ 38 _(DXInterop2Blocked, bool, false) \ 39 _(DXNV12Blocked, bool, false) \ 40 _(DXP010Blocked, bool, false) \ 41 _(DXP016Blocked, bool, false) \ 42 _(UseWebRenderANGLE, bool, false) \ 43 _(UseWebRenderFlipSequentialWin, bool, false) \ 44 _(UseWebRenderDCompWin, bool, false) \ 45 _(UseWebRenderDCompVideoHwOverlayWin, bool, false) \ 46 _(UseWebRenderDCompVideoSwOverlayWin, bool, false) \ 47 _(UseWebRenderTripleBufferingWin, bool, false) \ 48 _(UseWebRenderCompositor, bool, false) \ 49 _(UseWebRenderProgramBinaryDisk, bool, false) \ 50 _(ShouldWarmUpWebRenderProgramBinaries, bool, false) \ 51 _(UseWebRenderOptimizedShaders, bool, false) \ 52 _(UseWebRenderScissoredCacheClears, bool, true) \ 53 _(WebRenderProfilerUI, nsCString, nsCString()) \ 54 _(WebglAllowCoreProfile, bool, true) \ 55 _(WebglAllowWindowsNativeGl, bool, false) \ 56 _(WebRenderMaxPartialPresentRects, int32_t, 0) \ 57 _(WebRenderDebugFlags, uint64_t, 0) \ 58 _(WebRenderBoolParameters, int32_t, 0) \ 59 _(WebRenderBatchingLookback, int32_t, 10) \ 60 _(WebRenderBlobTileSize, int32_t, 256) \ 61 _(WebRenderBatchedUploadThreshold, int32_t, 512 * 512) \ 62 _(WebRenderSlowCpuFrameThreshold, float, 10.0) \ 63 _(UseSoftwareWebRender, bool, false) \ 64 _(AllowSoftwareWebRenderD3D11, bool, false) \ 65 _(PrimaryScreenDepth, int32_t, 0) \ 66 _(GREDirectory, nsString, nsString()) \ 67 _(ProfDirectory, nsString, nsString()) \ 68 _(AllowD3D11KeyedMutex, bool, false) \ 69 _(SwapIntervalGLX, bool, false) \ 70 _(SwapIntervalEGL, bool, false) \ 71 _(SystemTextQuality, int32_t, 5 /* CLEARTYPE_QUALITY */) \ 72 _(SystemTextClearTypeLevel, float, 1.0f) \ 73 _(SystemTextEnhancedContrast, float, 1.0f) \ 74 _(SystemTextGamma, float, 2.2f) \ 75 _(SystemTextPixelGeometry, int32_t, 1 /* pixel geometry RGB */) \ 76 _(SystemTextRenderingMode, int32_t, 0) \ 77 _(SystemGDIGamma, float, 1.4f) \ 78 _(LayersWindowRecordingPath, nsCString, nsCString()) \ 79 _(UseDoubleBufferingWithCompositor, bool, false) \ 80 _(UseGLSwizzle, bool, true) \ 81 _(ForceSubpixelAAWherePossible, bool, false) \ 82 _(FxREmbedded, bool, false) \ 83 _(UseAHardwareBufferSharedSurfaceWebglOop, bool, false) \ 84 _(UseEGL, bool, false) \ 85 _(DrmRenderDevice, nsCString, nsCString()) \ 86 _(UseDMABuf, bool, false) \ 87 _(UseDMABufWebGL, bool, true) \ 88 _(DMABufModifiersXRGB, ArrayOfuint64_t, nsTArray<uint64_t>()) \ 89 _(DMABufModifiersARGB, ArrayOfuint64_t, nsTArray<uint64_t>()) \ 90 _(WebRenderRequiresHardwareDriver, bool, false) \ 91 _(SupportsThreadsafeGL, bool, false) \ 92 _(AllowWebGPU, bool, false) \ 93 _(AllowWebGPUExternalTexture, bool, false) \ 94 _(UseVP8HwDecode, bool, false) \ 95 _(UseVP8HwEncode, bool, false) \ 96 _(UseVP9HwDecode, bool, false) \ 97 _(UseVP9HwEncode, bool, false) \ 98 _(UseAV1HwDecode, bool, false) \ 99 _(UseAV1HwEncode, bool, false) \ 100 _(UseH264HwDecode, bool, false) \ 101 _(UseH264HwEncode, bool, false) \ 102 _(UseHEVCHwDecode, bool, false) \ 103 _(UseHEVCHwEncode, bool, false) \ 104 _(HwDecodedVideoZeroCopy, bool, false) \ 105 _(UseWMFHWDWM, bool, false) \ 106 _(UseDMABufSurfaceExport, bool, true) \ 107 _(ReuseDecoderDevice, bool, false) \ 108 _(UseCanvasRenderThread, bool, false) \ 109 _(AllowBackdropFilter, bool, true) \ 110 _(WebglOopAsyncPresentForceSync, bool, true) \ 111 _(UseAcceleratedCanvas2D, bool, false) \ 112 _(AllowSoftwareWebRenderOGL, bool, false) \ 113 _(WebglUseHardware, bool, true) \ 114 _(WebRenderOverlayVpAutoHDR, bool, false) \ 115 _(WebRenderOverlayVpSuperResolution, bool, false) \ 116 _(AllowWebGPUPresentWithoutReadback, bool, false) \ 117 _(GPUProcessEnabled, bool, false) \ 118 _(DMABufModifiersP010, ArrayOfuint64_t, nsTArray<uint64_t>()) \ 119 _(DMABufModifiersNV12, ArrayOfuint64_t, nsTArray<uint64_t>()) \ 120 _(AllowGLNorm16Textures, bool, false) \ 121 _(WebRenderLayerCompositorDCompTexture, bool, false) \ 122 _(WebRenderOverlayHDR, bool, false) \ 123 _(UseWebRenderDCompositionTextureOverlayWin, bool, false) 124 125 /* Add new entries above this line. */ 126 127 // Some graphics settings are computed on the UI process and must be 128 // communicated to content and GPU processes. gfxVars helps facilitate 129 // this. Its function is similar to StaticPrefs, except rather than hold 130 // user preferences, it holds dynamically computed values. 131 // 132 // Each variable in GFX_VARS_LIST exposes the following static methods: 133 // 134 // const DataType& CxxName(); 135 // void SetCxxName(const DataType& aValue); 136 // 137 // Note that the setter may only be called in the UI process; a gfxVar must be 138 // a variable that is determined in the UI process and pushed to child 139 // processes. 140 class gfxVars final { 141 public: 142 // These values will be used during the Initialize() call if set. Any 143 // updates that come before initialization will get added to this array. 144 static void SetValuesForInitialize( 145 const nsTArray<GfxVarUpdate>& aInitUpdates); 146 147 static void Initialize(); 148 static void Shutdown(); 149 150 static void ApplyUpdate(const nsTArray<GfxVarUpdate>& aUpdate); 151 static void AddReceiver(gfxVarReceiver* aReceiver); 152 static void RemoveReceiver(gfxVarReceiver* aReceiver); 153 154 // Return a list of updates for all variables with non-default values. 155 static nsTArray<GfxVarUpdate> FetchNonDefaultVars(); 156 157 private: 158 template <typename U> 159 static U CloneVarValue(const U& aValue) { 160 return aValue; 161 } 162 163 template <typename U> 164 static nsTArray<U> CloneVarValue(const nsTArray<U>& aValue) { 165 return aValue.Clone(); 166 } 167 168 public: 169 // Each variable must expose Set and Get methods for IPDL. 170 class VarBase { 171 public: 172 VarBase(); 173 virtual void SetValue(const GfxVarValue& aValue) = 0; 174 virtual void GetValue(GfxVarValue* aOutValue) = 0; 175 virtual bool HasDefaultValue() const = 0; 176 size_t Index() const { return mIndex; } 177 178 private: 179 size_t mIndex; 180 }; 181 182 // Whether the gfxVars singleton instance has been initialized. Most gfx code 183 // doesn't need to check this, but code that can potentially run before 184 // gfxPlatform initialization can use this to check whether gfxVars are 185 // available yet. 186 static bool IsInitialized() { return sInstance != nullptr; } 187 188 private: 189 friend class gfxVarsCollectUpdates; 190 static void StartCollectingUpdates(); 191 static void StopCollectingUpdates(); 192 193 static StaticAutoPtr<gfxVars> sInstance; 194 static StaticAutoPtr<nsTArray<VarBase*>> sVarList; 195 196 template <typename T, T Default(), T GetFrom(const GfxVarValue& aValue)> 197 class VarImpl final : public VarBase { 198 public: 199 VarImpl() : mValue(Default()) {} 200 void SetValue(const GfxVarValue& aValue) override { 201 mValue = GetFrom(aValue); 202 if (mListener) { 203 mListener(); 204 } 205 } 206 void GetValue(GfxVarValue* aOutValue) override { 207 *aOutValue = GfxVarValue(mValue); 208 } 209 bool HasDefaultValue() const override { return mValue == Default(); } 210 const T& Get() const { return mValue; } 211 212 // Return true if the value changed, false otherwise. 213 bool Set(const T& aValue) { 214 MOZ_ASSERT(XRE_IsParentProcess()); 215 if (mValue == aValue) { 216 return false; 217 } 218 mValue = CloneVarValue(aValue); 219 if (mListener) { 220 mListener(); 221 } 222 return true; 223 } 224 225 void SetListener(const std::function<void()>& aListener) { 226 mListener = aListener; 227 } 228 229 private: 230 T mValue; 231 std::function<void()> mListener; 232 }; 233 234 #define GFX_VAR_DECL(CxxName, DataType, DefaultValue) \ 235 private: \ 236 static DataType Get##CxxName##Default() { return DefaultValue; } \ 237 static DataType Get##CxxName##From(const GfxVarValue& aValue) { \ 238 return CloneVarValue(aValue.get_##DataType()); \ 239 } \ 240 VarImpl<DataType, Get##CxxName##Default, Get##CxxName##From> mVar##CxxName; \ 241 \ 242 public: \ 243 static const DataType& CxxName() { return sInstance->mVar##CxxName.Get(); } \ 244 static DataType Get##CxxName##OrDefault() { \ 245 if (!sInstance) { \ 246 return DefaultValue; \ 247 } \ 248 return CloneVarValue(sInstance->mVar##CxxName.Get()); \ 249 } \ 250 static void Set##CxxName(const DataType& aValue) { \ 251 if (sInstance->mVar##CxxName.Set(aValue)) { \ 252 sInstance->NotifyReceivers(&sInstance->mVar##CxxName); \ 253 } \ 254 } \ 255 \ 256 static void Set##CxxName##Listener(const std::function<void()>& aListener) { \ 257 sInstance->mVar##CxxName.SetListener(aListener); \ 258 } 259 260 using ArrayOfuint64_t = nsTArray<uint64_t>; 261 262 GFX_VARS_LIST(GFX_VAR_DECL) 263 #undef GFX_VAR_DECL 264 265 private: 266 gfxVars(); 267 268 void NotifyReceivers(VarBase* aVar); 269 270 private: 271 nsTArray<gfxVarReceiver*> mReceivers; 272 }; 273 274 #undef GFX_VARS_LIST 275 276 // Helper class that batches changes to gfxVars into a single update to minimize 277 // churn for receivers. This is particularly useful for the media processes 278 // which reconfigure themselves when gfxVars updates come in. 279 class MOZ_STACK_CLASS gfxVarsCollectUpdates final { 280 public: 281 gfxVarsCollectUpdates() { gfxVars::StartCollectingUpdates(); } 282 ~gfxVarsCollectUpdates() { gfxVars::StopCollectingUpdates(); } 283 }; 284 285 } // namespace mozilla::gfx 286 287 #endif // mozilla_gfx_config_gfxVars_h