Skip to content
This repository has been archived by the owner on Apr 3, 2020. It is now read-only.

Commit

Permalink
Allow arbitrary scale factors.
Browse files Browse the repository at this point in the history
As discussed in https://codereview.chromium.org/1070433002/, this CL
allows arbitrary scale factors, which works because chromium uses the
resource most closely matching the scale factor (e.g. 1x for 1.25x,
but 2x for 1.9x) and scales it.

BUG=143619,477716

Review URL: https://codereview.chromium.org/1078423002

Cr-Commit-Position: refs/heads/master@{#326851}
(cherry picked from commit b3ca230)

device scale factor detection: use gtk-xft-dpi consistently

Before this CL, we used either gtk-xft-dpi (UI fonts) or the physical DPI of
a more or less random display. This CL consistently uses gtk-xft-dpi which
is the better alternative: users can directly influence it (by changing the
Xft.dpi X resource, or via their gtkrc).

See also https://github.com/derat/font-config-info for displaying your
current system settings.

This is a re-do of https://codereview.chromium.org/1070433002/, but with
https://codereview.chromium.org/1084453002/ applied (fixes content_shell).

BUG=473089, 143619

Review URL: https://codereview.chromium.org/1085603002

Cr-Commit-Position: refs/heads/master@{#324928}
(cherry picked from commit d459a5c)

[email protected]
TBR=erg, estade, oshima

Review URL: https://codereview.chromium.org/1103383004

Cr-Commit-Position: refs/branch-heads/2357@{#246}
Cr-Branched-From: 59d4494-refs/heads/master@{#323860}
  • Loading branch information
magjed committed Apr 28, 2015
1 parent 95d620a commit 7cec497
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 31 deletions.
20 changes: 16 additions & 4 deletions chrome/browser/ui/libgtk2ui/gtk2_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "chrome/browser/ui/libgtk2ui/gtk2_ui.h"

#include <math.h>
#include <set>

#include <pango/pango.h>
Expand Down Expand Up @@ -380,16 +381,20 @@ gfx::FontRenderParams GetGtkFontRenderParams() {
return params;
}

// Queries GTK for its font DPI setting and returns the number of pixels in a
// point.
double GetPixelsInPoint(float device_scale_factor) {
double GetDPI() {
GtkSettings* gtk_settings = gtk_settings_get_default();
CHECK(gtk_settings);
gint gtk_dpi = -1;
g_object_get(gtk_settings, "gtk-xft-dpi", &gtk_dpi, NULL);

// GTK multiplies the DPI by 1024 before storing it.
double dpi = (gtk_dpi > 0) ? gtk_dpi / 1024.0 : 96.0;
return (gtk_dpi > 0) ? gtk_dpi / 1024.0 : 96.0;
}

// Queries GTK for its font DPI setting and returns the number of pixels in a
// point.
double GetPixelsInPoint(float device_scale_factor) {
double dpi = GetDPI();

// Take device_scale_factor into account — if Chrome already scales the
// entire UI up by 2x, we should not also scale up.
Expand Down Expand Up @@ -1420,6 +1425,13 @@ void Gtk2UI::UpdateDeviceScaleFactor(float device_scale_factor) {
UpdateDefaultFont(label_style->font_desc);
}

float Gtk2UI::GetDeviceScaleFactor() const {
const int kCSSDefaultDPI = 96;
float scale = GetDPI() / kCSSDefaultDPI;
// Round to 2 decimals, e.g. to 1.33.
return roundf(scale * 100) / 100;
}

} // namespace libgtk2ui

views::LinuxUI* BuildGtk2UI() {
Expand Down
1 change: 1 addition & 0 deletions chrome/browser/ui/libgtk2ui/gtk2_ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ class Gtk2UI : public views::LinuxUI {

// ui::Views::LinuxUI:
void UpdateDeviceScaleFactor(float device_scale_factor) override;
float GetDeviceScaleFactor() const override;

private:
typedef std::map<int, SkColor> ColorMap;
Expand Down
2 changes: 1 addition & 1 deletion testing/xvfb.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def start_xvfb(xvfb_path, display):
xvfb_path: Path to Xvfb.
"""
cmd = [xvfb_path, display, '-screen', '0', '1024x768x24', '-ac',
'-nolisten', 'tcp']
'-nolisten', 'tcp', '-dpi', '96']
try:
proc = subprocess.Popen(
cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
Expand Down
3 changes: 3 additions & 0 deletions ui/views/linux_ui/linux_ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ class VIEWS_EXPORT LinuxUI : public ui::LinuxInputMethodContextFactory,
// Updates the device scale factor so that the default font size can be
// recalculated.
virtual void UpdateDeviceScaleFactor(float device_scale_factor) = 0;

// Determines the device scale factor of the primary screen.
virtual float GetDeviceScaleFactor() const = 0;
};

} // namespace views
Expand Down
37 changes: 11 additions & 26 deletions ui/views/widget/desktop_aura/desktop_screen_x11.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "ui/gfx/native_widget_types.h"
#include "ui/gfx/screen.h"
#include "ui/gfx/x/x11_types.h"
#include "ui/views/linux_ui/linux_ui.h"
#include "ui/views/widget/desktop_aura/desktop_screen.h"
#include "ui/views/widget/desktop_aura/desktop_window_tree_host_x11.h"
#include "ui/views/widget/desktop_aura/x11_topmost_window_finder.h"
Expand All @@ -35,21 +36,12 @@ namespace {
// in |Dispatch()|.
const int64 kConfigureDelayMs = 500;

// TODO(oshima): Consider using gtk-xft-dpi instead.
float GetDeviceScaleFactor(int screen_pixels, int screen_mm) {
const int kCSSDefaultDPI = 96;
const float kInchInMm = 25.4f;

float screen_inches = screen_mm / kInchInMm;
float screen_dpi = screen_pixels / screen_inches;
float scale = screen_dpi / kCSSDefaultDPI;

return ui::GetScaleForScaleFactor(ui::GetSupportedScaleFactor(scale));
}

float GetDeviceScaleFactor() {
gfx::Display display = gfx::Screen::GetNativeScreen()->GetPrimaryDisplay();
return display.device_scale_factor();
double GetDeviceScaleFactor() {
float device_scale_factor = 1.0f;
if (views::LinuxUI::instance())
device_scale_factor =
views::LinuxUI::instance()->GetDeviceScaleFactor();
return device_scale_factor;
}

gfx::Point PixelToDIPPoint(const gfx::Point& pixel_point) {
Expand All @@ -71,8 +63,7 @@ std::vector<gfx::Display> GetFallbackDisplayList() {
gfx::Display gfx_display(0, bounds_in_pixels);
if (!gfx::Display::HasForceDeviceScaleFactor() &&
!ui::IsDisplaySizeBlackListed(physical_size)) {
float device_scale_factor = GetDeviceScaleFactor(
width, physical_size.width());
const float device_scale_factor = GetDeviceScaleFactor();
DCHECK_LE(1.0f, device_scale_factor);
gfx_display.SetScaleAndBounds(device_scale_factor, bounds_in_pixels);
}
Expand Down Expand Up @@ -294,7 +285,9 @@ std::vector<gfx::Display> DesktopScreenX11::BuildDisplaysFromXRandRInfo() {
has_work_area = true;
}

float device_scale_factor = 1.0f;
// As per-display scale factor is not supported right now,
// the X11 root window's scale factor is always used.
const float device_scale_factor = GetDeviceScaleFactor();
for (int i = 0; i < resources->noutput; ++i) {
RROutput output_id = resources->outputs[i];
gfx::XScopedPtr<XRROutputInfo,
Expand All @@ -321,14 +314,6 @@ std::vector<gfx::Display> DesktopScreenX11::BuildDisplaysFromXRandRInfo() {
gfx::Display display(display_id, crtc_bounds);

if (!gfx::Display::HasForceDeviceScaleFactor()) {
if (i == 0 && !ui::IsDisplaySizeBlackListed(
gfx::Size(output_info->mm_width, output_info->mm_height))) {
// As per display scale factor is not supported right now,
// the primary display's scale factor is always used.
device_scale_factor = GetDeviceScaleFactor(crtc->width,
output_info->mm_width);
DCHECK_LE(1.0f, device_scale_factor);
}
display.SetScaleAndBounds(device_scale_factor, crtc_bounds);
}

Expand Down

0 comments on commit 7cec497

Please sign in to comment.