Bug 1156742 Part 20: Move Moz2D PreferenceAccess into its own header. r=bas
authorBob Owen <bobowencode@gmail.com>
Tue, 05 Jan 2016 10:08:58 +0000
changeset 278508 fa6b129da39a29b1ea3dd5e5eac5b73cb83f308f
parent 278507 c14e6cfb20dd844b82b1e5e7bb70a9cfec193f77
child 278509 6e490856b991d39f70c08231462a043b555c1d00
push id29852
push userkwierso@gmail.com
push dateWed, 06 Jan 2016 00:03:27 +0000
treeherdermozilla-central@dd6d447fc6e0 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbas
bugs1156742
milestone46.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
Bug 1156742 Part 20: Move Moz2D PreferenceAccess into its own header. r=bas This is so we can add a new preference in Part 21, which has nothing to do with logging.
gfx/2d/Factory.cpp
gfx/2d/Logging.h
gfx/2d/Preferences.cpp
gfx/2d/Preferences.h
gfx/2d/moz.build
gfx/thebes/gfxPrefs.cpp
--- a/gfx/2d/Factory.cpp
+++ b/gfx/2d/Factory.cpp
@@ -43,16 +43,17 @@
 #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()
 {
@@ -147,41 +148,19 @@ HasCPUIDBit(unsigned int level, CPUIDReg
   return !!(unsigned(regs[reg]) & bit);
 }
 #endif
 #endif
 
 namespace mozilla {
 namespace gfx {
 
-// These values we initialize with should match those in
-// PreferenceAccess::RegisterAll method.
-int32_t PreferenceAccess::sGfxLogLevel = LOG_DEFAULT;
-
-PreferenceAccess* PreferenceAccess::sAccess = nullptr;
-PreferenceAccess::~PreferenceAccess()
-{
-}
-
-// Just a placeholder, the derived class will set the variable to default
-// if the preference doesn't exist.
-void PreferenceAccess::LivePref(const char* aName, int32_t* aVar, int32_t aDef)
-{
-  *aVar = aDef;
-}
-
-// This will be called with the derived class, so we will want to register
-// the callbacks with it.
-void PreferenceAccess::SetAccess(PreferenceAccess* aAccess) {
-  sAccess = aAccess;
-  if (sAccess) {
-    RegisterAll();
-  }
-}
-
+int32_t LoggingPrefs::sGfxLogLevel =
+  PreferenceAccess::RegisterLivePref("gfx.logging.level", &sGfxLogLevel,
+                                     LOG_DEFAULT);
 
 #ifdef WIN32
 ID3D10Device1 *Factory::mD3D10Device;
 ID3D11Device *Factory::mD3D11Device;
 ID2D1Device *Factory::mD2D1Device;
 #endif
 
 DrawEventRecorder *Factory::mRecorder;
--- a/gfx/2d/Logging.h
+++ b/gfx/2d/Logging.h
@@ -58,51 +58,27 @@ inline mozilla::LogLevel PRLogLevelForLe
     return LogLevel::Debug;
   case LOG_EVERYTHING:
     return LogLevel::Error;
   }
   return LogLevel::Debug;
 }
 #endif
 
-class PreferenceAccess
+class LoggingPrefs
 {
 public:
-  virtual ~PreferenceAccess();
-
-  // 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);
-
-public:
-  static void SetAccess(PreferenceAccess* aAccess);
-
-public:
-  // For each preference that needs to be accessed in Moz2D, add a variable
-  // to hold it, as well as the call to LivePref in the RegisterAll() method
-  // below.
-
   // Used to choose the level of logging we get.  The higher the number,
   // the more logging we get.  Value of zero will give you no logging,
   // 1 just errors, 2 adds warnings and 3 adds logging/debug.  4 is used to
   // selectively enable logging on the configurations that
   // support prlog (on other systems, 3 and 4 are the same.)  For prlog,
   // in addition to setting the value to 4, you will need to set an
   // environment variable NSPR_LOG_MODULES to gfx:4. See prlog.h for details.
   static int32_t sGfxLogLevel;
-
-private:
-  static void RegisterAll() {
-    // The default values (last parameter) should match the initialization
-    // values in Factory.cpp, otherwise the standalone Moz2D will get different
-    // defaults.
-    sAccess->LivePref("gfx.logging.level", &sGfxLogLevel, LOG_DEFAULT);
-  }
-  static PreferenceAccess* sAccess;
 };
 
 /// Graphics logging is available in both debug and release builds and is
 /// controlled with a gfx.logging.level preference. If not set, the default
 /// for the preference is 5 in the debug builds, 1 in the release builds.
 ///
 /// gfxDebug only works in the debug builds, and is used for information
 /// level messages, helping with debugging.  In addition to only working
@@ -160,26 +136,26 @@ enum class LogReason : int {
 };
 
 struct BasicLogger
 {
   // For efficiency, this method exists and copies the logic of the
   // OutputMessage below.  If making any changes here, also make it
   // in the appropriate places in that method.
   static bool ShouldOutputMessage(int aLevel) {
-    if (PreferenceAccess::sGfxLogLevel >= aLevel) {
+    if (LoggingPrefs::sGfxLogLevel >= aLevel) {
 #if defined(MOZ_WIDGET_GONK) || defined(MOZ_WIDGET_ANDROID)
       return true;
 #else
 #if defined(MOZ_LOGGING)
       if (MOZ_LOG_TEST(GetGFX2DLog(), PRLogLevelForLevel(aLevel))) {
         return true;
       } else
 #endif
-      if ((PreferenceAccess::sGfxLogLevel >= LOG_DEBUG_PRLOG) ||
+      if ((LoggingPrefs::sGfxLogLevel >= LOG_DEBUG_PRLOG) ||
                  (aLevel < LOG_DEBUG)) {
         return true;
       }
 #endif
     }
     return false;
   }
 
@@ -193,26 +169,26 @@ struct BasicLogger
     // is consistent with what prlog does in general.  Note that if prlog
     // is in the build, but disabled, we will printf if the preferences
     // requires us to log something (see sGfxLogLevel for the special
     // treatment of LOG_DEBUG and LOG_DEBUG_PRLOG)
     //
     // If making any logic changes to this method, you should probably
     // make the corresponding change in the ShouldOutputMessage method
     // above.
-    if (PreferenceAccess::sGfxLogLevel >= aLevel) {
+    if (LoggingPrefs::sGfxLogLevel >= aLevel) {
 #if defined(MOZ_WIDGET_GONK) || defined(MOZ_WIDGET_ANDROID)
       printf_stderr("%s%s", aString.c_str(), aNoNewline ? "" : "\n");
 #else
 #if defined(MOZ_LOGGING)
       if (MOZ_LOG_TEST(GetGFX2DLog(), PRLogLevelForLevel(aLevel))) {
         PR_LogPrint("%s%s", aString.c_str(), aNoNewline ? "" : "\n");
       } else
 #endif
-      if ((PreferenceAccess::sGfxLogLevel >= LOG_DEBUG_PRLOG) ||
+      if ((LoggingPrefs::sGfxLogLevel >= LOG_DEBUG_PRLOG) ||
                  (aLevel < LOG_DEBUG)) {
         printf("%s%s", aString.c_str(), aNoNewline ? "" : "\n");
       }
 #endif
     }
   }
 };
 
new file mode 100644
--- /dev/null
+++ b/gfx/2d/Preferences.cpp
@@ -0,0 +1,60 @@
+/* -*- 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)
+{
+  Int32Prefs().append(Int32Pref{ aName, aVar });
+  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
\ No newline at end of file
new file mode 100644
--- /dev/null
+++ b/gfx/2d/Preferences.h
@@ -0,0 +1,34 @@
+/* -*- 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
@@ -33,16 +33,17 @@ EXPORTS.mozilla.gfx += [
     'JobScheduler_win32.h',
     'Logging.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',
@@ -146,16 +147,17 @@ 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,17 +2,17 @@
  * 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/Logging.h"
+#include "mozilla/gfx/Preferences.h"
 
 using namespace mozilla;
 
 gfxPrefs* gfxPrefs::sInstance = nullptr;
 bool gfxPrefs::sInstanceHasBeenDestroyed = false;
 
 class PreferenceAccessImpl : public mozilla::gfx::PreferenceAccess
 {