Bug 663023 - Cache getDPI calls. r=mbrubeck
☠☠ backed out by 46dbe6bfd8fe ☠ ☠
authorWes Johnston <wjohnston@mozilla.com>
Thu, 09 Jun 2011 13:54:34 -0700
changeset 70845 3e9a4e14f6c640ee78089b8836f711dde6a4a5d4
parent 70844 0ce00c8379e138db0244db4e713020becdef9d0a
child 70846 c023a6a059d1b4456a339e716af0e8795056310f
push id1
push userroot
push dateMon, 20 Oct 2014 17:29:22 +0000
reviewersmbrubeck
bugs663023
milestone7.0a1
Bug 663023 - Cache getDPI calls. r=mbrubeck
mobile/chrome/content/Util.js
mobile/chrome/content/browser-ui.js
mobile/chrome/content/browser.js
mobile/chrome/content/content.js
mobile/chrome/content/input.js
--- a/mobile/chrome/content/Util.js
+++ b/mobile/chrome/content/Util.js
@@ -157,17 +157,17 @@ let Util = {
   },
 
   isParentProcess: function isInParentProcess() {
     let appInfo = Cc["@mozilla.org/xre/app-info;1"];
     return (!appInfo || appInfo.getService(Ci.nsIXULRuntime).processType == Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT);
   },
 
   isTablet: function isTablet() {
-    let dpi = Util.getWindowUtils(window).displayDPI;
+    let dpi = this.displayDPI;
     if (dpi <= 96)
       return (window.innerWidth > 1024);
 
     // See the tablet_panel_minwidth from mobile/themes/core/defines.inc
     let tablet_panel_minwidth = 124;
     let dpmm = 25.4 * window.innerWidth / dpi;
     return (dpmm >= tablet_panel_minwidth);
   },
@@ -183,16 +183,22 @@ let Util = {
   },
 
   get isKeyboardOpened() {
     let isChromeWindow = this.isParentProcess() && window["ViewableAreaObserver"];
     if (isChromeWindow)
       return ViewableAreaObserver.isKeyboardOpened;
 
     return (sendSyncMessage("Content:IsKeyboardOpened", {}))[0];
+  },
+
+  // because this uses the global window, will only work in the parent process
+  get displayDPI() function() {
+    delete this.displayDPI;
+    return this.displayDPI = this.getWindowUtils(window).displayDPI;
   }
 };
 
 
 /**
  * Helper class to nsITimer that adds a little more pizazz.  Callback can be an
  * object with a notify method or a function.
  */
--- a/mobile/chrome/content/browser-ui.js
+++ b/mobile/chrome/content/browser-ui.js
@@ -359,17 +359,17 @@ var BrowserUI = {
   _isKeyboardFullscreen: function _isKeyboardFullscreen() {
 #ifdef ANDROID
     if (!Util.isPortrait()) {
       switch (Services.prefs.getIntPref("widget.ime.android.landscape_fullscreen")) {
         case 1:
           return true;
         case -1: {
           let threshold = Services.prefs.getIntPref("widget.ime.android.fullscreen_threshold");
-          let dpi = Util.getWindowUtils(window).displayDPI;
+          let dpi = Util.displayDPI;
           return (window.innerHeight * 100 < threshold * dpi);
         }
       }
     }
 #endif
     return false;
   },
 
--- a/mobile/chrome/content/browser.js
+++ b/mobile/chrome/content/browser.js
@@ -1107,17 +1107,17 @@ var Browser = {
 
   // The device-pixel-to-CSS-px ratio used to adjust meta viewport values.
   // This is higher on higher-dpi displays, so pages stay about the same physical size.
   getScaleRatio: function getScaleRatio() {
     let prefValue = Services.prefs.getIntPref("browser.viewport.scaleRatio");
     if (prefValue > 0)
       return prefValue / 100;
 
-    let dpi = this.windowUtils.displayDPI;
+    let dpi = Utils.displayDPI;
     if (dpi < 200) // Includes desktop displays, and LDPI and MDPI Android devices
       return 1;
     else if (dpi < 300) // Includes Nokia N900, and HDPI Android devices
       return 1.5;
 
     // For very high-density displays like the iPhone 4, calculate an integer ratio.
     return Math.floor(dpi / 150);
   },
@@ -1804,17 +1804,17 @@ const ContentTouchHandler = {
   },
 
   touchTimeout: null,
   canCancelPan: false,
   clickPrevented: false,
   panningPrevented: false,
 
   updateCanCancel: function(aX, aY) {
-    let dpi = Browser.windowUtils.displayDPI;
+    let dpi = Utils.displayDPI;
 
     const kSafetyX = Services.prefs.getIntPref("dom.w3c_touch_events.safetyX") / 240 * dpi;
     const kSafetyY = Services.prefs.getIntPref("dom.w3c_touch_events.safetyY") / 240 * dpi;
     let browser = getBrowser();
     let bcr = browser.getBoundingClientRect();
     let rect = new Rect(0, 0, window.innerWidth, window.innerHeight);
     rect.restrictTo(Rect.fromRect(bcr));
 
--- a/mobile/chrome/content/content.js
+++ b/mobile/chrome/content/content.js
@@ -63,17 +63,21 @@ const ElementTouchHelper = {
   get weight() {
     delete this.weight;
     return this.weight = { "visited": Services.prefs.getIntPref("browser.ui.touch.weight.visited")
                          };
   },
 
   /* Retrieve the closest element to a point by looking at borders position */
   getClosest: function getClosest(aWindowUtils, aX, aY) {
-    let dpiRatio = aWindowUtils.displayDPI / kReferenceDpi;
+    // cached for the child process, since Utils.displayDPI is only available in the parent
+    if (!this.dpiRatio)
+      this.dpiRatio = aWindowUtils.displayDPI / kReferenceDpi;
+
+    let dpiRatio = this.dpiRatio;
 
     let target = aWindowUtils.elementFromPoint(aX, aY,
                                                true,   /* ignore root scroll frame*/
                                                false); /* don't flush layout */
 
     // return early if the click is just over a clickable element
     if (this._isElementClickable(target))
       return target;
--- a/mobile/chrome/content/input.js
+++ b/mobile/chrome/content/input.js
@@ -113,17 +113,17 @@ function MouseModule() {
 
   this._kinetic = new KineticController(this._dragBy.bind(this),
                                         this._kineticStop.bind(this));
 
   this._singleClickTimeout = new Util.Timeout(this._doSingleClick.bind(this));
   this._mouseOverTimeout = new Util.Timeout(this._doMouseOver.bind(this));
   this._longClickTimeout = new Util.Timeout(this._doLongClick.bind(this));
 
-  this._doubleClickRadius = Util.getWindowUtils(window).displayDPI * kDoubleClickRadius;
+  this._doubleClickRadius = Util.displayDPI * kDoubleClickRadius;
 
   window.addEventListener("mousedown", this, true);
   window.addEventListener("mouseup", this, true);
   window.addEventListener("mousemove", this, true);
   window.addEventListener("contextmenu", this, false);
   window.addEventListener("CancelTouchSequence", this, true);
 }
 
@@ -565,17 +565,17 @@ MouseModule.prototype = {
       + 'length=' + this._downUpEvents.length + ', '
       + '\n\ttargetScroller=' + this._targetScrollInterface + '}';
   }
 };
 
 var ScrollUtils = {
   // threshold in pixels for sensing a tap as opposed to a pan
   get tapRadius() {
-    let dpi = Util.getWindowUtils(window).displayDPI;
+    let dpi = Util.displayDPI;
 
     delete this.tapRadius;
     return this.tapRadius = Services.prefs.getIntPref("ui.dragThresholdX") / 240 * dpi;
   },
 
   /**
    * Walk up (parentward) the DOM tree from elem in search of a scrollable element.
    * Return the element and its scroll interface if one is found, two nulls otherwise.
@@ -681,17 +681,17 @@ var ScrollUtils = {
 };
 
 /**
  * DragData handles processing drags on the screen, handling both
  * locking of movement on one axis, and click detection.
  */
 function DragData() {
   this._domUtils = Cc["@mozilla.org/inspector/dom-utils;1"].getService(Ci.inIDOMUtils);
-  this._lockRevertThreshold = Util.getWindowUtils(window).displayDPI * kAxisLockRevertThreshold;
+  this._lockRevertThreshold = Util.displayDPI * kAxisLockRevertThreshold;
   this.reset();
 };
 
 DragData.prototype = {
   reset: function reset() {
     this.dragging = false;
     this.sX = null;
     this.sY = null;