Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(7)

Side by Side Diff: ui/gfx/display.cc

Issue 11953054: Fix high-DPI on Windows to make use of DIP scaling in WebKit. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Code cleanup. Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ui/gfx/display.h" 5 #include "ui/gfx/display.h"
6 6
7 #include "base/command_line.h" 7 #include "base/command_line.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "base/string_number_conversions.h" 9 #include "base/string_number_conversions.h"
10 #include "base/stringprintf.h" 10 #include "base/stringprintf.h"
11 #include "ui/base/ui_base_switches.h" 11 #include "ui/base/ui_base_switches.h"
12 #include "ui/base/win/dpi.h"
12 #include "ui/gfx/insets.h" 13 #include "ui/gfx/insets.h"
13 #include "ui/gfx/size_conversions.h" 14 #include "ui/gfx/size_conversions.h"
14 15
15 namespace gfx { 16 namespace gfx {
16 namespace { 17 namespace {
17 18
18 bool HasForceDeviceScaleFactor() { 19 bool HasForceDeviceScaleFactorImpl() {
19 return CommandLine::ForCurrentProcess()->HasSwitch( 20 return CommandLine::ForCurrentProcess()->HasSwitch(
20 switches::kForceDeviceScaleFactor); 21 switches::kForceDeviceScaleFactor);
21 } 22 }
22 23
23 float GetForcedDeviceScaleFactorImpl() { 24 float GetForcedDeviceScaleFactorImpl() {
24 double scale_in_double = 1.0; 25 double scale_in_double = 1.0;
25 if (HasForceDeviceScaleFactor()) { 26 if (HasForceDeviceScaleFactorImpl()) {
26 std::string value = CommandLine::ForCurrentProcess()-> 27 std::string value = CommandLine::ForCurrentProcess()->
27 GetSwitchValueASCII(switches::kForceDeviceScaleFactor); 28 GetSwitchValueASCII(switches::kForceDeviceScaleFactor);
28 if (!base::StringToDouble(value, &scale_in_double)) 29 if (!base::StringToDouble(value, &scale_in_double))
29 LOG(ERROR) << "Failed to parse the deafult device scale factor:" << value; 30 LOG(ERROR) << "Failed to parse the deafult device scale factor:" << value;
30 } 31 }
32 #if defined(OS_WIN)
33 else {
34 static double os_scale = ui::GetDPIScale();
35 scale_in_double = os_scale;
36 }
37 #endif
38
31 return static_cast<float>(scale_in_double); 39 return static_cast<float>(scale_in_double);
32 } 40 }
33 41
34 } // namespace 42 } // namespace
35 43
36 const int64 Display::kInvalidDisplayID = -1; 44 const int64 Display::kInvalidDisplayID = -1;
37 45
38 // static 46 // static
39 float Display::GetForcedDeviceScaleFactor() { 47 float Display::GetForcedDeviceScaleFactor() {
40 static const float kForcedDeviceScaleFactor = 48 static const float kForcedDeviceScaleFactor =
41 GetForcedDeviceScaleFactorImpl(); 49 GetForcedDeviceScaleFactorImpl();
42 return kForcedDeviceScaleFactor; 50 return kForcedDeviceScaleFactor;
43 } 51 }
52 //static
53 bool Display::HasForceDeviceScaleFactor() {
54 return HasForceDeviceScaleFactorImpl();
55 }
44 56
45 // static 57 // static
46 int64 Display::GetID(uint16 manufacturer_id, uint32 serial_number) { 58 int64 Display::GetID(uint16 manufacturer_id, uint32 serial_number) {
47 int64 new_id = ((static_cast<int64>(manufacturer_id) << 32) | serial_number); 59 int64 new_id = ((static_cast<int64>(manufacturer_id) << 32) | serial_number);
48 DCHECK_NE(kInvalidDisplayID, new_id); 60 DCHECK_NE(kInvalidDisplayID, new_id);
49 return new_id; 61 return new_id;
50 } 62 }
51 63
52 Display::Display() 64 Display::Display()
53 : id_(kInvalidDisplayID), 65 : id_(kInvalidDisplayID),
54 device_scale_factor_(GetForcedDeviceScaleFactor()) { 66 device_scale_factor_(GetForcedDeviceScaleFactor()) {
55 } 67 }
56 68
57 Display::Display(int64 id) 69 Display::Display(int64 id)
58 : id_(id), 70 : id_(id),
59 device_scale_factor_(GetForcedDeviceScaleFactor()) { 71 device_scale_factor_(GetForcedDeviceScaleFactor()) {
60 } 72 }
61 73
62 Display::Display(int64 id, const gfx::Rect& bounds) 74 Display::Display(int64 id, const gfx::Rect& bounds)
63 : id_(id), 75 : id_(id),
64 bounds_(bounds), 76 bounds_(bounds),
65 work_area_(bounds), 77 work_area_(bounds),
66 device_scale_factor_(GetForcedDeviceScaleFactor()) { 78 device_scale_factor_(GetForcedDeviceScaleFactor()) {
67 #if defined(USE_AURA) 79 #if defined(USE_AURA) || defined(OS_WIN)
68 SetScaleAndBounds(device_scale_factor_, bounds); 80 SetScaleAndBounds(device_scale_factor_, bounds);
69 #endif 81 #endif
70 } 82 }
71 83
72 Display::~Display() { 84 Display::~Display() {
73 } 85 }
74 86
75 Insets Display::GetWorkAreaInsets() const { 87 Insets Display::GetWorkAreaInsets() const {
76 return gfx::Insets(work_area_.y() - bounds_.y(), 88 return gfx::Insets(work_area_.y() - bounds_.y(),
77 work_area_.x() - bounds_.x(), 89 work_area_.x() - bounds_.x(),
78 bounds_.bottom() - work_area_.bottom(), 90 bounds_.bottom() - work_area_.bottom(),
79 bounds_.right() - work_area_.right()); 91 bounds_.right() - work_area_.right());
80 } 92 }
81 93
82 void Display::SetScaleAndBounds( 94 void Display::SetScaleAndBounds(
83 float device_scale_factor, 95 float device_scale_factor,
84 const gfx::Rect& bounds_in_pixel) { 96 const gfx::Rect& bounds_in_pixel) {
85 Insets insets = bounds_.InsetsFrom(work_area_); 97 Insets insets = bounds_.InsetsFrom(work_area_);
86 if (!HasForceDeviceScaleFactor()) { 98 if (!HasForceDeviceScaleFactor()) {
87 #if defined(OS_MACOSX) 99 #if defined(OS_MACOSX)
88 // Unless an explicit scale factor was provided for testing, ensure the 100 // Unless an explicit scale factor was provided for testing, ensure the
89 // scale is integral. 101 // scale is integral.
90 device_scale_factor = static_cast<int>(device_scale_factor); 102 device_scale_factor = static_cast<int>(device_scale_factor);
91 #endif 103 #endif
92 device_scale_factor_ = device_scale_factor; 104 device_scale_factor_ = device_scale_factor;
93 } 105 }
94 device_scale_factor_ = std::max(1.0f, device_scale_factor_); 106 device_scale_factor_ = std::max(1.0f, device_scale_factor_);
95 #if defined(USE_AURA) 107 #if defined(USE_AURA) || defined(OS_WIN)
96 bounds_in_pixel_ = bounds_in_pixel; 108 bounds_in_pixel_ = bounds_in_pixel;
97 #endif 109 #endif
98 bounds_ = gfx::Rect(gfx::ToFlooredSize( 110 bounds_ = gfx::Rect(gfx::ToFlooredSize(
99 gfx::ScaleSize(bounds_in_pixel.size(), 1.0f / device_scale_factor_))); 111 gfx::ScaleSize(bounds_in_pixel.size(), 1.0f / device_scale_factor_)));
100 UpdateWorkAreaFromInsets(insets); 112 UpdateWorkAreaFromInsets(insets);
101 } 113 }
102 114
103 void Display::SetSize(const gfx::Size& size_in_pixel) { 115 void Display::SetSize(const gfx::Size& size_in_pixel) {
104 SetScaleAndBounds( 116 SetScaleAndBounds(
105 device_scale_factor_, 117 device_scale_factor_,
106 #if defined(USE_AURA) 118 #if defined(USE_AURA) || defined(OS_WIN)
107 gfx::Rect(bounds_in_pixel_.origin(), size_in_pixel)); 119 gfx::Rect(bounds_in_pixel_.origin(), size_in_pixel));
108 #else 120 #else
109 gfx::Rect(bounds_.origin(), size_in_pixel)); 121 gfx::Rect(bounds_.origin(), size_in_pixel));
110 #endif 122 #endif
111 } 123 }
112 124
113 void Display::UpdateWorkAreaFromInsets(const gfx::Insets& insets) { 125 void Display::UpdateWorkAreaFromInsets(const gfx::Insets& insets) {
114 work_area_ = bounds_; 126 work_area_ = bounds_;
115 work_area_.Inset(insets); 127 work_area_.Inset(insets);
116 } 128 }
117 129
118 gfx::Size Display::GetSizeInPixel() const { 130 gfx::Size Display::GetSizeInPixel() const {
119 return gfx::ToFlooredSize(gfx::ScaleSize(size(), device_scale_factor_)); 131 return gfx::ToFlooredSize(gfx::ScaleSize(size(), device_scale_factor_));
120 } 132 }
121 133
122 std::string Display::ToString() const { 134 std::string Display::ToString() const {
123 return base::StringPrintf("Display[%lld] bounds=%s, workarea=%s, scale=%f", 135 return base::StringPrintf("Display[%lld] bounds=%s, workarea=%s, scale=%f",
124 static_cast<long long int>(id_), 136 static_cast<long long int>(id_),
125 bounds_.ToString().c_str(), 137 bounds_.ToString().c_str(),
126 work_area_.ToString().c_str(), 138 work_area_.ToString().c_str(),
127 device_scale_factor_); 139 device_scale_factor_);
128 } 140 }
129 141
130 } // namespace gfx 142 } // namespace gfx
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698