Bug 1350643 - Part 4.1: Get per-monitor dpi in ScreenHelperCocoa. r?mstange draft
authorSamael Wang <freesamael@gmail.com>
Tue, 06 Jun 2017 18:18:52 +0800
changeset 611032 afeaa2fc3729fbcb80cfb2e19ed934a090d0aac1
parent 611031 ef9b6bc092765b1b5353673698ed6e9adfbd6052
child 611033 c152ce1edb7b4d2f8f9df919eda0f88497a0477f
push id69104
push userbmo:sawang@mozilla.com
push dateWed, 19 Jul 2017 06:01:44 +0000
reviewersmstange
bugs1350643
milestone56.0a1
Bug 1350643 - Part 4.1: Get per-monitor dpi in ScreenHelperCocoa. r?mstange MozReview-Commit-ID: OHa8h2sh26
widget/cocoa/ScreenHelperCocoa.mm
widget/cocoa/nsCocoaWindow.h
--- a/widget/cocoa/ScreenHelperCocoa.mm
+++ b/widget/cocoa/ScreenHelperCocoa.mm
@@ -90,20 +90,28 @@ MakeScreen(NSScreen* aScreen)
   LayoutDeviceIntRect rect =
     nsCocoaUtils::CocoaRectToGeckoRectDevPix(frame, contentsScaleFactor.scale);
   frame = [aScreen visibleFrame];
   LayoutDeviceIntRect availRect =
     nsCocoaUtils::CocoaRectToGeckoRectDevPix(frame, contentsScaleFactor.scale);
   NSWindowDepth depth = [aScreen depth];
   uint32_t pixelDepth = NSBitsPerPixelFromDepth(depth);
   float dpi = 96.0f;
-  MOZ_LOG(sScreenLog, LogLevel::Debug, ("New screen [%d %d %d %d %d %f %f]",
-                                        rect.x, rect.y, rect.width, rect.height,
-                                        pixelDepth, contentsScaleFactor.scale,
-                                        dpi));
+  CGDirectDisplayID displayID =
+    [[[aScreen deviceDescription] objectForKey:@"NSScreenNumber"] intValue];
+  CGFloat heightMM = ::CGDisplayScreenSize(displayID).height;
+  if (heightMM > 0) {
+    dpi = rect.height / (heightMM / MM_PER_INCH_FLOAT);
+  }
+  MOZ_LOG(sScreenLog, LogLevel::Debug,
+           ("New screen [%d %d %d %d (%d %d %d %d) %d %f %f %f]",
+            rect.x, rect.y, rect.width, rect.height,
+            availRect.x, availRect.y, availRect.width, availRect.height,
+            pixelDepth, contentsScaleFactor.scale, defaultCssScaleFactor.scale,
+            dpi));
 
   RefPtr<Screen> screen = new Screen(rect, availRect,
                                      pixelDepth, pixelDepth,
                                      contentsScaleFactor, defaultCssScaleFactor,
                                      dpi);
   return screen.forget();
 
   NS_OBJC_END_TRY_ABORT_BLOCK_RETURN(nullptr);
--- a/widget/cocoa/nsCocoaWindow.h
+++ b/widget/cocoa/nsCocoaWindow.h
@@ -42,16 +42,18 @@ typedef struct _nsCocoaWindowList {
   NSColor* mInactiveTitlebarColor;
 
   // Invalidation disabling
   BOOL mDisabledNeedsDisplay;
 
   // DPI cache. Getting the physical screen size (CGDisplayScreenSize)
   // is ridiculously slow, so we cache it in the toplevel window for all
   // descendants to use.
+  //
+  // XXX the dpi cache won't change when moving across different monitors.
   float mDPI;
 
   NSTrackingArea* mTrackingArea;
 
   NSRect mDirtyRect;
 
   BOOL mBeingShown;
   BOOL mDrawTitle;