Bug 1350643 - Part 5.2: Use per-monitor gdkScaleFactor to set contentsScaleFactor & nsWindow::GetDesktopToDeviceScale if running in gtk/wayland. r?karlt draft
authorSamael Wang <freesamael@gmail.com>
Fri, 16 Jun 2017 11:13:59 +0800
changeset 611035 65644cf33fef639e42375e18f3ccb0e6574304c8
parent 611034 6116cdf99cd40817b36f8300cbfb3899d7a2960c
child 611036 bfcee8a5a1c5bed66484687d488b97d1617e0dc3
push id69104
push userbmo:sawang@mozilla.com
push dateWed, 19 Jul 2017 06:01:44 +0000
reviewerskarlt
bugs1350643
milestone56.0a1
Bug 1350643 - Part 5.2: Use per-monitor gdkScaleFactor to set contentsScaleFactor & nsWindow::GetDesktopToDeviceScale if running in gtk/wayland. r?karlt MozReview-Commit-ID: Grs4dUqvLew
widget/gtk/ScreenHelperGTK.cpp
widget/gtk/mozgtk/mozgtk.c
widget/gtk/nsWindow.cpp
widget/gtk/nsWindow.h
--- a/widget/gtk/ScreenHelperGTK.cpp
+++ b/widget/gtk/ScreenHelperGTK.cpp
@@ -3,17 +3,20 @@
 /* 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 "ScreenHelperGTK.h"
 
 #ifdef MOZ_X11
 #include <gdk/gdkx.h>
-#endif
+#endif /* MOZ_X11 */
+#ifdef MOZ_WAYLAND
+#include <gdk/gdkwayland.h>
+#endif /* MOZ_WAYLAND */
 #include <dlfcn.h>
 #include <gtk/gtk.h>
 
 #include "gfxPlatformGtk.h"
 #include "mozilla/Logging.h"
 #include "nsGtkUtils.h"
 #include "nsTArray.h"
 
@@ -142,17 +145,26 @@ MakeScreen(GdkScreen* aScreen, gint aMon
                            monitor.y * gdkScaleFactor,
                            monitor.width * gdkScaleFactor,
                            monitor.height * gdkScaleFactor);
   LayoutDeviceIntRect availRect(workarea.x * gdkScaleFactor,
                                 workarea.y * gdkScaleFactor,
                                 workarea.width * gdkScaleFactor,
                                 workarea.height * gdkScaleFactor);
   uint32_t pixelDepth = GetGTKPixelDepth();
+
+  // Use per-monitor scaling factor in gtk/wayland, or 1.0 otherwise.
   DesktopToLayoutDeviceScale contentsScale(1.0);
+#ifdef MOZ_WAYLAND
+    GdkDisplay* gdkDisplay = gdk_display_get_default();
+    if (GDK_IS_WAYLAND_DISPLAY(gdkDisplay)) {
+      contentsScale.scale = gdkScaleFactor;
+    }
+#endif
+
   CSSToLayoutDeviceScale defaultCssScale(
     gdkScaleFactor * gfxPlatformGtk::GetFontScaleFactor());
 
   float dpi = 96.0f;
   gint heightMM = gdk_screen_get_monitor_height_mm(aScreen, aMonitorNum);
   if (heightMM > 0) {
     dpi = rect.height / (heightMM / MM_PER_INCH_FLOAT);
   }
--- a/widget/gtk/mozgtk/mozgtk.c
+++ b/widget/gtk/mozgtk/mozgtk.c
@@ -518,16 +518,17 @@ STUB(gdk_disable_multidevice)
 STUB(gdk_device_manager_list_devices)
 STUB(gdk_display_get_device_manager)
 STUB(gdk_error_trap_pop_ignored)
 STUB(gdk_event_get_source_device)
 STUB(gdk_window_get_type)
 STUB(gdk_window_get_window_type)
 STUB(gdk_x11_window_get_xid)
 STUB(gdk_x11_display_get_type)
+STUB(gdk_wayland_display_get_type)
 STUB(gtk_box_new)
 STUB(gtk_cairo_should_draw_window)
 STUB(gtk_cairo_transform_to_window)
 STUB(gtk_combo_box_text_append)
 STUB(gtk_drag_set_icon_surface)
 STUB(gtk_get_major_version)
 STUB(gtk_get_micro_version)
 STUB(gtk_get_minor_version)
--- a/widget/gtk/nsWindow.cpp
+++ b/widget/gtk/nsWindow.cpp
@@ -35,29 +35,34 @@
 #include "nsGtkKeyUtils.h"
 #include "nsGtkCursors.h"
 #include "ScreenHelperGTK.h"
 
 #include <gtk/gtk.h>
 #if (MOZ_WIDGET_GTK == 3)
 #include <gtk/gtkx.h>
 #endif
+
+#ifdef MOZ_WAYLAND
+#include <gdk/gdkwayland.h>
+#endif /* MOZ_WAYLAND */
+
 #ifdef MOZ_X11
 #include <gdk/gdkx.h>
 #include <X11/Xatom.h>
 #include <X11/extensions/XShm.h>
 #include <X11/extensions/shape.h>
 #if (MOZ_WIDGET_GTK == 3)
 #include <gdk/gdkkeysyms-compat.h>
 #endif
-
 #if (MOZ_WIDGET_GTK == 2)
 #include "gtk2xtbin.h"
 #endif
 #endif /* MOZ_X11 */
+
 #include <gdk/gdkkeysyms.h>
 #if (MOZ_WIDGET_GTK == 2)
 #include <gtk/gtkprivate.h>
 #endif
 
 #include "nsGkAtoms.h"
 
 #ifdef MOZ_ENABLE_STARTUP_NOTIFICATION
@@ -813,16 +818,30 @@ nsWindow::GetDPI()
 }
 
 double
 nsWindow::GetDefaultScaleInternal()
 {
     return GdkScaleFactor() * gfxPlatformGtk::GetFontScaleFactor();
 }
 
+DesktopToLayoutDeviceScale
+nsWindow::GetDesktopToDeviceScale()
+{
+#ifdef MOZ_WAYLAND
+    GdkDisplay* gdkDisplay = gdk_display_get_default();
+    if (GDK_IS_WAYLAND_DISPLAY(gdkDisplay)) {
+        return DesktopToLayoutDeviceScale(GdkScaleFactor());
+    }
+#endif
+
+    // In Gtk/X11, we manage windows using device pixels.
+    return DesktopToLayoutDeviceScale(1.0);
+}
+
 void
 nsWindow::SetParent(nsIWidget *aNewParent)
 {
     if (mContainer || !mGdkWindow) {
         NS_NOTREACHED("nsWindow::SetParent called illegally");
         return;
     }
 
--- a/widget/gtk/nsWindow.h
+++ b/widget/gtk/nsWindow.h
@@ -96,21 +96,17 @@ public:
     virtual MOZ_MUST_USE nsresult Create(nsIWidget* aParent,
                                          nsNativeWidget aNativeParent,
                                          const LayoutDeviceIntRect& aRect,
                                          nsWidgetInitData* aInitData) override;
     virtual void       Destroy() override;
     virtual nsIWidget *GetParent() override;
     virtual float      GetDPI() override;
     virtual double     GetDefaultScaleInternal() override;
-    // Under Gtk, we manage windows using device pixels so no scaling is needed:
-    mozilla::DesktopToLayoutDeviceScale GetDesktopToDeviceScale() final
-    {
-        return mozilla::DesktopToLayoutDeviceScale(1.0);
-    }
+    mozilla::DesktopToLayoutDeviceScale GetDesktopToDeviceScale() override;
     virtual void       SetParent(nsIWidget* aNewParent) override;
     virtual void       SetModal(bool aModal) override;
     virtual bool       IsVisible() const override;
     virtual void       ConstrainPosition(bool aAllowSlop,
                                          int32_t *aX,
                                          int32_t *aY) override;
     virtual void       SetSizeConstraints(const SizeConstraints& aConstraints) override;
     virtual void       Move(double aX,