Remove PreferenceAccess. (bug 1280822 part 3, r=milan)
authorDavid Anderson <danderson@mozilla.com>
Sun, 26 Jun 2016 23:33:18 -0700
changeset 302681 53b78e203d0a4bec431d5fea480275136f1c8e2d
parent 302680 d787bc79425f6cd02c4a8bbf8279f3a7afbadf1e
child 302682 cb97f090f9d42cabffef9812f33da36b316ff88d
push id78826
push userdanderson@mozilla.com
push dateMon, 27 Jun 2016 06:33:46 +0000
treeherdermozilla-inbound@cb97f090f9d4 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmilan
bugs1280822
milestone50.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Remove PreferenceAccess. (bug 1280822 part 3, r=milan)
gfx/2d/Factory.cpp
gfx/2d/Logging.h
gfx/2d/LoggingConstants.h
gfx/2d/Preferences.cpp
gfx/2d/Preferences.h
gfx/2d/moz.build
gfx/thebes/gfxPrefs.cpp
gfx/thebes/gfxPrefs.h
--- a/gfx/2d/Factory.cpp
+++ b/gfx/2d/Factory.cpp
@@ -45,17 +45,16 @@
 #include "DrawTargetDual.h"
 #include "DrawTargetTiled.h"
 #include "DrawTargetRecording.h"
 
 #include "SourceSurfaceRawData.h"
 
 #include "DrawEventRecorder.h"
 
-#include "Preferences.h"
 #include "Logging.h"
 
 #include "mozilla/CheckedInt.h"
 
 #if defined(MOZ_LOGGING)
 GFX2D_API mozilla::LogModule*
 GetGFX2DLog()
 {
@@ -149,19 +148,18 @@ HasCPUIDBit(unsigned int level, CPUIDReg
   return !!(unsigned(regs[reg]) & bit);
 }
 #endif
 #endif
 
 namespace mozilla {
 namespace gfx {
 
-int32_t LoggingPrefs::sGfxLogLevel =
-  PreferenceAccess::RegisterLivePref("gfx.logging.level", &sGfxLogLevel,
-                                     LOG_DEFAULT);
+// In Gecko, this value is managed by gfx.logging.level in gfxPrefs.
+int32_t LoggingPrefs::sGfxLogLevel = LOG_DEFAULT;
 
 #ifdef WIN32
 ID3D11Device *Factory::mD3D11Device;
 ID2D1Device *Factory::mD2D1Device;
 #endif
 
 DrawEventRecorder *Factory::mRecorder;
 
--- a/gfx/2d/Logging.h
+++ b/gfx/2d/Logging.h
@@ -17,39 +17,25 @@
 #include "mozilla/Tuple.h"
 
 #if defined(MOZ_WIDGET_GONK) || defined(MOZ_WIDGET_ANDROID)
 #include "nsDebug.h"
 #endif
 #include "Point.h"
 #include "BaseRect.h"
 #include "Matrix.h"
+#include "LoggingConstants.h"
 
 #if defined(MOZ_LOGGING)
 extern GFX2D_API mozilla::LogModule* GetGFX2DLog();
 #endif
 
 namespace mozilla {
 namespace gfx {
 
-// Attempting to be consistent with prlog values, but that isn't critical
-// (and note that 5 has a special meaning - see the description
-// with sGfxLogLevel)
-const int LOG_CRITICAL = 1;
-const int LOG_WARNING = 2;
-const int LOG_DEBUG = 3;
-const int LOG_DEBUG_PRLOG = 4;
-const int LOG_EVERYTHING = 5; // This needs to be the highest value
-
-#if defined(DEBUG)
-const int LOG_DEFAULT = LOG_EVERYTHING;
-#else
-const int LOG_DEFAULT = LOG_CRITICAL;
-#endif
-
 #if defined(MOZ_LOGGING)
 inline mozilla::LogLevel PRLogLevelForLevel(int aLevel) {
   switch (aLevel) {
   case LOG_CRITICAL:
     return LogLevel::Error;
   case LOG_WARNING:
     return LogLevel::Warning;
   case LOG_DEBUG:
new file mode 100644
--- /dev/null
+++ b/gfx/2d/LoggingConstants.h
@@ -0,0 +1,30 @@
+/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#ifndef MOZILLA_GFX_LOGGING_CONSTANTS_H_
+#define MOZILLA_GFX_LOGGING_CONSTANTS_H_
+
+namespace mozilla {
+namespace gfx {
+
+// Attempting to be consistent with prlog values, but that isn't critical
+// (and note that 5 has a special meaning - see the description
+// with LoggingPrefs::sGfxLogLevel)
+const int LOG_CRITICAL = 1;
+const int LOG_WARNING = 2;
+const int LOG_DEBUG = 3;
+const int LOG_DEBUG_PRLOG = 4;
+const int LOG_EVERYTHING = 5; // This needs to be the highest value
+
+#if defined(DEBUG)
+const int LOG_DEFAULT = LOG_EVERYTHING;
+#else
+const int LOG_DEFAULT = LOG_CRITICAL;
+#endif
+
+} // namespace gfx
+} // namespace mozilla
+
+#endif /* MOZILLA_GFX_LOGGING_CONSTANTS_H_ */
deleted file mode 100644
--- a/gfx/2d/Preferences.cpp
+++ /dev/null
@@ -1,62 +0,0 @@
-/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
-/* vim: set ts=8 sts=2 et sw=2 tw=80: */
-/* This Source Code Form is subject to the terms of the Mozilla Public
- * License, v. 2.0. If a copy of the MPL was not distributed with this
- * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
-
-#include "Preferences.h"
-
-#include "mozilla/Assertions.h"
-#include "mozilla/Vector.h"
-
-namespace mozilla {
-namespace gfx {
-
-static PreferenceAccess* sAccess = nullptr;
-
-struct Int32Pref
-{
-  const char* name;
-  int32_t* varPtr;
-};
-
-static Vector<Int32Pref>& Int32Prefs()
-{
-  static Vector<Int32Pref>* sInt32Prefs = new Vector<Int32Pref>();
-  return *sInt32Prefs;
-}
-
-/* static */
-int32_t
-PreferenceAccess::RegisterLivePref(const char* aName, int32_t* aVar,
-                                   int32_t aDefault)
-{
-  if (!Int32Prefs().append(Int32Pref{ aName, aVar })) {
-    MOZ_CRASH("GFX: RegisterLivePref failure");
-  }
-  return aDefault;
-}
-
-/* static */
-void
-PreferenceAccess::SetAccess(PreferenceAccess* aAccess)
-{
-  sAccess = aAccess;
-  if (!sAccess) {
-    return;
-  }
-
-#if defined(DEBUG)
-  static uint32_t sProvideAccessCount;
-  MOZ_ASSERT(!sProvideAccessCount++,
-             "ProvideAccess must only be called with non-nullptr once.");
-#endif
-
-  for (Int32Pref pref : Int32Prefs()) {
-    sAccess->LivePref(pref.name, pref.varPtr, *pref.varPtr);
-  }
-  Int32Prefs().clearAndFree();
-}
-
-} // namespace gfx
-} // namespace mozilla
deleted file mode 100644
--- a/gfx/2d/Preferences.h
+++ /dev/null
@@ -1,34 +0,0 @@
-/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
-/* vim: set ts=8 sts=2 et sw=2 tw=80: */
-/* This Source Code Form is subject to the terms of the Mozilla Public
- * License, v. 2.0. If a copy of the MPL was not distributed with this
- * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
-
-#ifndef mozilla_gfx_Preferences_h
-#define mozilla_gfx_Preferences_h
-
-namespace mozilla {
-namespace gfx {
-
-class PreferenceAccess
-{
-public:
-  virtual ~PreferenceAccess() {};
-
-  // This will be called with the derived class, so we will can register the
-  // callbacks with it.
-  static void SetAccess(PreferenceAccess* aAccess);
-
-  static int32_t RegisterLivePref(const char* aName, int32_t* aVar,
-                                  int32_t aDefault);
-protected:
-  // This should connect the variable aVar to be updated whenever a preference
-  // aName is modified.  aDefault would be used if the preference is undefined,
-  // so that we always get the valid value for aVar.
-  virtual void LivePref(const char* aName, int32_t* aVar, int32_t aDefault) = 0;
-};
-
-} // namespace gfx
-} // namespace mozilla
-
-#endif // mozilla_gfx_Preferences_h
--- a/gfx/2d/moz.build
+++ b/gfx/2d/moz.build
@@ -28,23 +28,23 @@ EXPORTS.mozilla.gfx += [
     'Filters.h',
     'Helpers.h',
     'HelpersCairo.h',
     'IterableArena.h',
     'JobScheduler.h',
     'JobScheduler_posix.h',
     'JobScheduler_win32.h',
     'Logging.h',
+    'LoggingConstants.h',
     'Matrix.h',
     'MatrixFwd.h',
     'NumericTools.h',
     'PathHelpers.h',
     'PatternHelpers.h',
     'Point.h',
-    'Preferences.h',
     'Quaternion.h',
     'RecordedEvent.h',
     'RecordingTypes.h',
     'Rect.h',
     'Scale.h',
     'ScaleFactor.h',
     'ScaleFactors2D.h',
     'SourceSurfaceCairo.h',
@@ -156,17 +156,16 @@ UNIFIED_SOURCES += [
     'FilterProcessingScalar.cpp',
     'ImageScaling.cpp',
     'JobScheduler.cpp',
     'Matrix.cpp',
     'Path.cpp',
     'PathCairo.cpp',
     'PathHelpers.cpp',
     'PathRecording.cpp',
-    'Preferences.cpp',
     'Quaternion.cpp',
     'RecordedEvent.cpp',
     'Scale.cpp',
     'ScaledFontBase.cpp',
     'ScaledFontCairo.cpp',
     'SFNTData.cpp',
     'SFNTNameTable.cpp',
     'SourceSurfaceCairo.cpp',
--- a/gfx/thebes/gfxPrefs.cpp
+++ b/gfx/thebes/gfxPrefs.cpp
@@ -2,42 +2,24 @@
  * This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "gfxPrefs.h"
 
 #include "mozilla/Preferences.h"
 #include "MainThreadUtils.h"
-#include "mozilla/gfx/Preferences.h"
 #include "nsXULAppAPI.h"
+#include "mozilla/gfx/Logging.h"
 
 using namespace mozilla;
 
 gfxPrefs* gfxPrefs::sInstance = nullptr;
 bool gfxPrefs::sInstanceHasBeenDestroyed = false;
 
-class PreferenceAccessImpl : public mozilla::gfx::PreferenceAccess
-{
-public:
-  virtual ~PreferenceAccessImpl();
-  virtual void LivePref(const char* aName, int32_t* aVar, int32_t aDefault) override;
-};
-
-PreferenceAccessImpl::~PreferenceAccessImpl()
-{
-}
-
-void PreferenceAccessImpl::LivePref(const char* aName,
-                                    int32_t* aVar,
-                                    int32_t aDefault)
-{
-  Preferences::AddIntVarCache(aVar, aName, aDefault);
-}
-
 void
 gfxPrefs::DestroySingleton()
 {
   if (sInstance) {
     delete sInstance;
     sInstance = nullptr;
     sInstanceHasBeenDestroyed = true;
   }
@@ -55,29 +37,31 @@ gfxPrefs::gfxPrefs()
   // UI, content, and plugin processes use XPCOM and should have prefs
   // ready by the time we initialize gfxPrefs.
   MOZ_ASSERT_IF(XRE_IsContentProcess() ||
                 XRE_IsParentProcess() ||
                 XRE_GetProcessType() == GeckoProcessType_Plugin,
                 Preferences::IsServiceAvailable());
 
   gfxPrefs::AssertMainThread();
-  mMoz2DPrefAccess = new PreferenceAccessImpl;
-  mozilla::gfx::PreferenceAccess::SetAccess(mMoz2DPrefAccess);
+}
+
+void
+gfxPrefs::Init()
+{
+  // Set up Moz2D prefs.
+  mPrefGfxLoggingLevel.SetChangeCallback([]() -> void {
+    mozilla::gfx::LoggingPrefs::sGfxLogLevel = GetSingleton().mPrefGfxLoggingLevel.GetLiveValue();
+  });
 }
 
 gfxPrefs::~gfxPrefs()
 {
   gfxPrefs::AssertMainThread();
-
-  // gfxPrefs is a singleton, we can reset this to null once
-  // it goes away.
-  mozilla::gfx::PreferenceAccess::SetAccess(nullptr);
-  delete mMoz2DPrefAccess;
-  mMoz2DPrefAccess = nullptr;
+  mPrefGfxLoggingLevel.SetChangeCallback(nullptr);
 }
 
 void gfxPrefs::AssertMainThread()
 {
   MOZ_ASSERT(NS_IsMainThread(), "this code must be run on the main thread");
 }
 
 void
--- a/gfx/thebes/gfxPrefs.h
+++ b/gfx/thebes/gfxPrefs.h
@@ -4,16 +4,17 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #ifndef GFX_PREFS_H
 #define GFX_PREFS_H
 
 #include <cmath>                 // for M_PI
 #include <stdint.h>
 #include "mozilla/Assertions.h"
+#include "mozilla/gfx/LoggingConstants.h"
 
 // First time gfxPrefs::GetSingleton() needs to be called on the main thread,
 // before any of the methods accessing the values are used, but after
 // the Preferences system has been initialized.
 
 // The static methods to access the preference value are safe to call
 // from any thread after that first call.
 
@@ -64,25 +65,20 @@ public:                                 
 static Type Name() { MOZ_ASSERT(SingletonExists()); return GetSingleton().mPref##Name.mValue; } \
 static void Set##Name(Type aVal) { MOZ_ASSERT(SingletonExists());             \
     GetSingleton().mPref##Name.Set(UpdatePolicy::Update, Get##Name##PrefName(), aVal); } \
 static const char* Get##Name##PrefName() { return Prefname; }                 \
 static Type Get##Name##PrefDefault() { return Default; }                      \
 private:                                                                      \
 PrefTemplate<UpdatePolicy::Update, Type, Get##Name##PrefDefault, Get##Name##PrefName> mPref##Name
 
-class PreferenceAccessImpl;
 class gfxPrefs;
 class gfxPrefs final
 {
 private:
-  /// See Logging.h.  This lets Moz2D access preference values it owns.
-  PreferenceAccessImpl* mMoz2DPrefAccess;
-
-private:
   // Enums for the update policy.
   enum class UpdatePolicy {
     Skip, // Set the value to default, skip any Preferences calls
     Once, // Evaluate the preference once, unchanged during the session
     Live  // Evaluate the preference and set callback so it stays current/live
   };
 
 public:
@@ -158,16 +154,25 @@ private:
           break;
         default:
           MOZ_CRASH("Incomplete switch");
       }
     }
     const char *Name() const override {
       return Prefname();
     }
+    // When using the Preferences service, the change callback can be triggered
+    // *before* our cached value is updated, so we expose a method to grab the
+    // true live value.
+    T GetLiveValue() const {
+      if (IsPrefsServiceAvailable()) {
+        return PrefGet(Prefname(), mValue);
+      }
+      return mValue;
+    }
     T mValue;
   };
 
   // This is where DECL_GFX_PREF for each of the preferences should go.
   // We will keep these in an alphabetical order to make it easier to see if
   // a method accessing a pref already exists. Just add yours in the list.
 
   // The apz prefs are explained in AsyncPanZoomController.cpp
@@ -295,17 +300,18 @@ private:
   DECL_GFX_PREF(Once, "gfx.direct2d.disabled",                 Direct2DDisabled, bool, false);
   DECL_GFX_PREF(Once, "gfx.direct2d.force-enabled",            Direct2DForceEnabled, bool, false);
   DECL_GFX_PREF(Live, "gfx.draw-color-bars",                   CompositorDrawColorBars, bool, false);
   DECL_GFX_PREF(Once, "gfx.e10s.hide-plugins-for-scroll",      HidePluginsForScroll, bool, true);
   DECL_GFX_PREF(Once, "gfx.font_rendering.directwrite.force-enabled", DirectWriteFontRenderingForceEnabled, bool, false);
   DECL_GFX_PREF(Live, "gfx.gralloc.fence-with-readpixels",     GrallocFenceWithReadPixels, bool, false);
   DECL_GFX_PREF(Live, "gfx.layerscope.enabled",                LayerScopeEnabled, bool, false);
   DECL_GFX_PREF(Live, "gfx.layerscope.port",                   LayerScopePort, int32_t, 23456);
-  // Note that        "gfx.logging.level" is defined in Logging.h
+  // Note that        "gfx.logging.level" is defined in Logging.h.
+  DECL_GFX_PREF(Live, "gfx.logging.level",                     GfxLoggingLevel, int32_t, mozilla::gfx::LOG_DEFAULT);
   DECL_GFX_PREF(Once, "gfx.logging.crash.length",              GfxLoggingCrashLength, uint32_t, 16);
   DECL_GFX_PREF(Live, "gfx.logging.painted-pixel-count.enabled",GfxLoggingPaintedPixelCountEnabled, bool, false);
   // The maximums here are quite conservative, we can tighten them if problems show up.
   DECL_GFX_PREF(Once, "gfx.logging.texture-usage.enabled",     GfxLoggingTextureUsageEnabled, bool, false);
   DECL_GFX_PREF(Once, "gfx.logging.peak-texture-usage.enabled",GfxLoggingPeakTextureUsageEnabled, bool, false);
   DECL_GFX_PREF(Once, "gfx.max-alloc-size",                    MaxAllocSize, int32_t, (int32_t)500000000);
   DECL_GFX_PREF(Once, "gfx.max-texture-size",                  MaxTextureSize, int32_t, (int32_t)32767);
   DECL_GFX_PREF(Live, "gfx.partialpresent.force",              PartialPresent, int32_t, 0);
@@ -515,28 +521,34 @@ private:
 
 public:
   // Manage the singleton:
   static gfxPrefs& GetSingleton()
   {
     MOZ_ASSERT(!sInstanceHasBeenDestroyed, "Should never recreate a gfxPrefs instance!");
     if (!sInstance) {
       sInstance = new gfxPrefs;
+      sInstance->Init();
     }
     MOZ_ASSERT(SingletonExists());
     return *sInstance;
   }
   static void DestroySingleton();
   static bool SingletonExists();
 
 private:
   static gfxPrefs* sInstance;
   static bool sInstanceHasBeenDestroyed;
 
 private:
+  // The constructor cannot access GetSingleton(), since sInstance (necessarily)
+  // has not been assigned yet. Follow-up initialization that needs GetSingleton()
+  // must be added to Init().
+  void Init();
+
   static bool IsPrefsServiceAvailable();
   static bool IsParentProcess();
   // Creating these to avoid having to include Preferences.h in the .h
   static void PrefAddVarCache(bool*, const char*, bool);
   static void PrefAddVarCache(int32_t*, const char*, int32_t);
   static void PrefAddVarCache(uint32_t*, const char*, uint32_t);
   static void PrefAddVarCache(float*, const char*, float);
   static bool PrefGet(const char*, bool);