Bug 804062. Move device-pixels-per-CSS-pixel preference logic into nsIWidget::GetDefaultScale. r=jfkthame
authorRobert O'Callahan <robert@ocallahan.org>
Wed, 31 Oct 2012 11:31:35 +1100
changeset 111973 885fb1477a951e25ddca4cdd9b3e59114c212fde
parent 111972 078e5c209552a2e7494c8596464cc055939c4eef
child 111974 01780269281730fd69e4a45b2cbf7d8ff538d0ff
push id93
push usernmatsakis@mozilla.com
push dateWed, 31 Oct 2012 21:26:57 +0000
reviewersjfkthame
bugs804062
milestone19.0a1
Bug 804062. Move device-pixels-per-CSS-pixel preference logic into nsIWidget::GetDefaultScale. r=jfkthame The only behavior change caused by this patch should be that nsDeviceContexts with no widget (mainly printing, I think), default to a devpixel-per-CSS-pixel ratio of 1.0, ignoring any prefs set.
gfx/src/nsDeviceContext.cpp
widget/cocoa/nsChildView.h
widget/cocoa/nsChildView.mm
widget/cocoa/nsCocoaWindow.h
widget/cocoa/nsCocoaWindow.mm
widget/nsIWidget.h
widget/windows/nsWindow.cpp
widget/windows/nsWindow.h
widget/xpwidgets/nsBaseWidget.cpp
widget/xpwidgets/nsBaseWidget.h
--- a/gfx/src/nsDeviceContext.cpp
+++ b/gfx/src/nsDeviceContext.cpp
@@ -332,33 +332,17 @@ nsDeviceContext::SetDPI()
 
             if (prefDPI < 0) {
                 dpi = NS_MAX(96.0f, dpi);
             }
         } else {
             dpi = 96.0f;
         }
 
-        // The number of device pixels per CSS pixel. A value <= 0 means choose
-        // automatically based on the DPI. A positive value is used as-is. This effectively
-        // controls the size of a CSS "px".
-        float devPixelsPerCSSPixel = -1.0;
-
-        nsAdoptingCString prefString = Preferences::GetCString("layout.css.devPixelsPerPx");
-        if (!prefString.IsEmpty()) {
-            devPixelsPerCSSPixel = static_cast<float>(atof(prefString));
-        }
-
-        if (devPixelsPerCSSPixel <= 0) {
-            if (mWidget) {
-                devPixelsPerCSSPixel = mWidget->GetDefaultScale();
-            } else {
-                devPixelsPerCSSPixel = 1.0;
-            }
-        }
+        double devPixelsPerCSSPixel = mWidget ? mWidget->GetDefaultScale() : 1.0;
 
         mAppUnitsPerDevNotScaledPixel =
             NS_MAX(1, NS_lround(AppUnitsPerCSSPixel() / devPixelsPerCSSPixel));
     }
 
     NS_ASSERTION(dpi != -1.0, "no dpi set");
 
     mAppUnitsPerPhysicalInch = NS_lround(dpi * mAppUnitsPerDevNotScaledPixel);
--- a/widget/cocoa/nsChildView.h
+++ b/widget/cocoa/nsChildView.h
@@ -403,17 +403,17 @@ public:
   // evolve). This gives the relationship between what Gecko calls "device
   // pixels" and the Cocoa "points" coordinate system.
   CGFloat                 BackingScaleFactor();
 
   // Call if the window's backing scale factor changes - i.e., it is moved
   // between HiDPI and non-HiDPI screens
   void                    BackingScaleFactorChanged();
 
-  virtual double          GetDefaultScale();
+  virtual double          GetDefaultScaleInternal();
 
   NS_IMETHOD              Invalidate(const nsIntRect &aRect);
 
   virtual void*           GetNativeData(uint32_t aDataType);
   virtual nsresult        ConfigureChildren(const nsTArray<Configuration>& aConfigurations);
   virtual nsIntPoint      WidgetToScreenOffset();
   virtual bool            ShowsResizeIndicator(nsIntRect* aResizerRect);
 
--- a/widget/cocoa/nsChildView.mm
+++ b/widget/cocoa/nsChildView.mm
@@ -737,17 +737,17 @@ NS_IMETHODIMP nsChildView::GetBounds(nsI
     aRect = mBounds;
   } else {
     aRect = CocoaPointsToDevPixels([mView frame]);
   }
   return NS_OK;
 }
 
 double
-nsChildView::GetDefaultScale()
+nsChildView::GetDefaultScaleInternal()
 {
   return BackingScaleFactor();
 }
 
 CGFloat
 nsChildView::BackingScaleFactor()
 {
   if (mBackingScaleFactor > 0.0) {
--- a/widget/cocoa/nsCocoaWindow.h
+++ b/widget/cocoa/nsCocoaWindow.h
@@ -242,17 +242,17 @@ public:
     NS_IMETHOD              GetScreenBounds(nsIntRect &aRect);
     void                    ReportMoveEvent();
     void                    ReportSizeEvent();
     NS_IMETHOD              SetCursor(nsCursor aCursor);
     NS_IMETHOD              SetCursor(imgIContainer* aCursor, uint32_t aHotspotX, uint32_t aHotspotY);
 
     CGFloat                 BackingScaleFactor();
     void                    BackingScaleFactorChanged();
-    virtual double          GetDefaultScale();
+    virtual double          GetDefaultScaleInternal();
 
     NS_IMETHOD              SetTitle(const nsAString& aTitle);
 
     NS_IMETHOD Invalidate(const nsIntRect &aRect);
     virtual nsresult ConfigureChildren(const nsTArray<Configuration>& aConfigurations);
     virtual LayerManager* GetLayerManager(PLayersChild* aShadowManager = nullptr,
                                           LayersBackend aBackendHint = mozilla::layers::LAYERS_NONE,
                                           LayerManagerPersistence aPersistence = LAYER_MANAGER_CURRENT,
--- a/widget/cocoa/nsCocoaWindow.mm
+++ b/widget/cocoa/nsCocoaWindow.mm
@@ -1394,17 +1394,17 @@ NS_IMETHODIMP nsCocoaWindow::GetScreenBo
 
   aRect = mBounds;
   return NS_OK;
 
   NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
 }
 
 double
-nsCocoaWindow::GetDefaultScale()
+nsCocoaWindow::GetDefaultScaleInternal()
 {
   return BackingScaleFactor();
 }
 
 CGFloat
 nsCocoaWindow::BackingScaleFactor()
 {
   if (mBackingScaleFactor > 0.0) {
--- a/widget/nsIWidget.h
+++ b/widget/nsIWidget.h
@@ -85,18 +85,18 @@ typedef nsEventStatus (* EVENT_CALLBACK)
 #ifdef XP_WIN
 #define NS_NATIVE_TSF_THREAD_MGR       100
 #define NS_NATIVE_TSF_CATEGORY_MGR     101
 #define NS_NATIVE_TSF_DISPLAY_ATTR_MGR 102
 #define NS_NATIVE_ICOREWINDOW          103 // winrt specific
 #endif
 
 #define NS_IWIDGET_IID \
-  { 0x8181a08f, 0xaa37, 0x4fd0, \
-    { 0x94, 0x32, 0x27, 0x74, 0xe2, 0xce, 0x02, 0xc8 } }
+  { 0xb7c60bda, 0xe16c, 0x4e89, \
+    { 0x86, 0x8c, 0xc3, 0x2e, 0x62, 0x40, 0x05, 0xb2 } }
 
 /*
  * Window shadow styles
  * Also used for the -moz-window-shadow CSS property
  */
 
 #define NS_STYLE_WINDOW_SHADOW_NONE             0
 #define NS_STYLE_WINDOW_SHADOW_DEFAULT          1
@@ -569,19 +569,20 @@ class nsIWidget : public nsISupports {
      * the number of device pixels per inch.
      */
     virtual float GetDPI() = 0;
 
     /**
      * Return the default scale factor for the window. This is the
      * default number of device pixels per CSS pixel to use. This should
      * depend on OS/platform settings such as the Mac's "UI scale factor"
-     * or Windows' "font DPI".
+     * or Windows' "font DPI". This will take into account Gecko preferences
+     * overriding the system setting.
      */
-    virtual double GetDefaultScale() = 0;
+    double GetDefaultScale();
 
     /**
      * Return the first child of this widget.  Will return null if
      * there are no children.
      */
     nsIWidget* GetFirstChild() const {
         return mFirstChild;
     }
@@ -1647,16 +1648,21 @@ class nsIWidget : public nsISupports {
     /**
      * If this isn't directly compositing to its window surface,
      * return the compositor which is doing that on our behalf.
      */
     virtual CompositorChild* GetRemoteRenderer()
     { return nullptr; }
 
 protected:
+    /**
+     * Like GetDefaultScale, but taking into account only the system settings
+     * and ignoring Gecko preferences.
+     */
+    virtual double GetDefaultScaleInternal() { return 1.0; }
 
     // keep the list of children.  We also keep track of our siblings.
     // The ownership model is as follows: parent holds a strong ref to
     // the first element of the list, and each element holds a strong
     // ref to the next element in the list.  The prevsibling and
     // lastchild pointers are weak, which is fine as long as they are
     // maintained properly.
     nsCOMPtr<nsIWidget> mFirstChild;
--- a/widget/windows/nsWindow.cpp
+++ b/widget/windows/nsWindow.cpp
@@ -159,17 +159,17 @@
 
 #include "nsWindowDefs.h"
 
 #include "nsCrashOnException.h"
 #include "nsIXULRuntime.h"
 
 #include "nsIContent.h"
 
-#include "mozilla/HangMonitor.h"
+#include "mozilla/HangMonitor.h"
 #include "nsIMM32Handler.h"
 
 using namespace mozilla::widget;
 using namespace mozilla::layers;
 using namespace mozilla;
 
 /**************************************************************
  **************************************************************
@@ -970,17 +970,17 @@ float nsWindow::GetDPI()
   ::ReleaseDC(mWnd, dc);
   if (heightInches < 0.25) {
     // Something's broken
     return 96.0f;
   }
   return float(heightPx/heightInches);
 }
 
-double nsWindow::GetDefaultScale()
+double nsWindow::GetDefaultScaleInternal()
 {
   HDC dc = ::GetDC(mWnd);
   if (!dc)
     return 1.0;
 
   // LOGPIXELSY returns the number of logical pixels per inch. This is based
   // on font DPI settings rather than the actual screen DPI.
   double pixelsPerInch = ::GetDeviceCaps(dc, LOGPIXELSY);
--- a/widget/windows/nsWindow.h
+++ b/widget/windows/nsWindow.h
@@ -85,17 +85,17 @@ public:
                                  nsNativeWidget aNativeParent,
                                  const nsIntRect &aRect,
                                  nsDeviceContext *aContext,
                                  nsWidgetInitData *aInitData = nullptr);
   NS_IMETHOD              Destroy();
   NS_IMETHOD              SetParent(nsIWidget *aNewParent);
   virtual nsIWidget*      GetParent(void);
   virtual float           GetDPI();
-  virtual double          GetDefaultScale();
+  virtual double          GetDefaultScaleInternal();
   NS_IMETHOD              Show(bool bState);
   virtual bool            IsVisible() const;
   NS_IMETHOD              ConstrainPosition(bool aAllowSlop, int32_t *aX, int32_t *aY);
   virtual void            SetSizeConstraints(const SizeConstraints& aConstraints);
   NS_IMETHOD              Move(int32_t aX, int32_t aY);
   NS_IMETHOD              Resize(int32_t aWidth, int32_t aHeight, bool aRepaint);
   NS_IMETHOD              Resize(int32_t aX, int32_t aY, int32_t aWidth, int32_t aHeight, bool aRepaint);
   NS_IMETHOD              BeginResizeDrag(nsGUIEvent* aEvent, int32_t aHorizontal, int32_t aVertical);
--- a/widget/xpwidgets/nsBaseWidget.cpp
+++ b/widget/xpwidgets/nsBaseWidget.cpp
@@ -372,19 +372,33 @@ nsIWidget* nsBaseWidget::GetSheetWindowP
   return nullptr;
 }
 
 float nsBaseWidget::GetDPI()
 {
   return 96.0f;
 }
 
-double nsBaseWidget::GetDefaultScale()
+double nsIWidget::GetDefaultScale()
 {
-  return 1.0;
+  // The number of device pixels per CSS pixel. A value <= 0 means choose
+  // automatically based on the DPI. A positive value is used as-is. This effectively
+  // controls the size of a CSS "px".
+  float devPixelsPerCSSPixel = -1.0;
+
+  nsAdoptingCString prefString = Preferences::GetCString("layout.css.devPixelsPerPx");
+  if (!prefString.IsEmpty()) {
+    devPixelsPerCSSPixel = static_cast<float>(atof(prefString));
+  }
+
+  if (devPixelsPerCSSPixel <= 0) {
+    devPixelsPerCSSPixel = GetDefaultScaleInternal();
+  }
+
+  return devPixelsPerCSSPixel;
 }
 
 //-------------------------------------------------------------------------
 //
 // Add a child to the list of children
 //
 //-------------------------------------------------------------------------
 void nsBaseWidget::AddChild(nsIWidget* aChild)
--- a/widget/xpwidgets/nsBaseWidget.h
+++ b/widget/xpwidgets/nsBaseWidget.h
@@ -67,17 +67,16 @@ public:
   virtual nsIWidgetListener*  GetWidgetListener();
   virtual void            SetWidgetListener(nsIWidgetListener* alistener);
   NS_IMETHOD              Destroy();
   NS_IMETHOD              SetParent(nsIWidget* aNewParent);
   virtual nsIWidget*      GetParent(void);
   virtual nsIWidget*      GetTopLevelWidget();
   virtual nsIWidget*      GetSheetWindowParent(void);
   virtual float           GetDPI();
-  virtual double          GetDefaultScale();
   virtual void            AddChild(nsIWidget* aChild);
   virtual void            RemoveChild(nsIWidget* aChild);
 
   NS_IMETHOD              SetZIndex(int32_t aZIndex);
   NS_IMETHOD              GetZIndex(int32_t* aZIndex);
   NS_IMETHOD              PlaceBehind(nsTopLevelWidgetZPlacement aPlacement,
                                       nsIWidget *aWidget, bool aActivate);