OLD | NEW |
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/display/display.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
12 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
13 #include "build/build_config.h" | 13 #include "build/build_config.h" |
| 14 #include "ui/display/display_switches.h" |
14 #include "ui/gfx/geometry/insets.h" | 15 #include "ui/gfx/geometry/insets.h" |
15 #include "ui/gfx/geometry/point_conversions.h" | 16 #include "ui/gfx/geometry/point_conversions.h" |
16 #include "ui/gfx/geometry/point_f.h" | 17 #include "ui/gfx/geometry/point_f.h" |
17 #include "ui/gfx/geometry/size_conversions.h" | 18 #include "ui/gfx/geometry/size_conversions.h" |
18 #include "ui/gfx/switches.h" | |
19 | 19 |
20 namespace gfx { | 20 namespace display { |
21 namespace { | 21 namespace { |
22 | 22 |
23 // This variable tracks whether the forced device scale factor switch needs to | 23 // This variable tracks whether the forced device scale factor switch needs to |
24 // be read from the command line, i.e. if it is set to -1 then the command line | 24 // be read from the command line, i.e. if it is set to -1 then the command line |
25 // is checked. | 25 // is checked. |
26 int g_has_forced_device_scale_factor = -1; | 26 int g_has_forced_device_scale_factor = -1; |
27 | 27 |
28 // This variable caches the forced device scale factor value which is read off | 28 // This variable caches the forced device scale factor value which is read off |
29 // the command line. If the cache is invalidated by setting this variable to | 29 // the command line. If the cache is invalidated by setting this variable to |
30 // -1.0, we read the forced device scale factor again. | 30 // -1.0, we read the forced device scale factor again. |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
70 // static | 70 // static |
71 void Display::ResetForceDeviceScaleFactorForTesting() { | 71 void Display::ResetForceDeviceScaleFactorForTesting() { |
72 g_has_forced_device_scale_factor = -1; | 72 g_has_forced_device_scale_factor = -1; |
73 g_forced_device_scale_factor = -1.0; | 73 g_forced_device_scale_factor = -1.0; |
74 } | 74 } |
75 | 75 |
76 Display::Display() | 76 Display::Display() |
77 : id_(kInvalidDisplayID), | 77 : id_(kInvalidDisplayID), |
78 device_scale_factor_(GetForcedDeviceScaleFactor()), | 78 device_scale_factor_(GetForcedDeviceScaleFactor()), |
79 rotation_(ROTATE_0), | 79 rotation_(ROTATE_0), |
80 touch_support_(TOUCH_SUPPORT_UNKNOWN) { | 80 touch_support_(TOUCH_SUPPORT_UNKNOWN) {} |
81 } | |
82 | 81 |
83 Display::Display(const Display& other) = default; | 82 Display::Display(const Display& other) = default; |
84 | 83 |
85 Display::Display(int64_t id) | 84 Display::Display(int64_t id) |
86 : id_(id), | 85 : id_(id), |
87 device_scale_factor_(GetForcedDeviceScaleFactor()), | 86 device_scale_factor_(GetForcedDeviceScaleFactor()), |
88 rotation_(ROTATE_0), | 87 rotation_(ROTATE_0), |
89 touch_support_(TOUCH_SUPPORT_UNKNOWN) {} | 88 touch_support_(TOUCH_SUPPORT_UNKNOWN) {} |
90 | 89 |
91 Display::Display(int64_t id, const gfx::Rect& bounds) | 90 Display::Display(int64_t id, const gfx::Rect& bounds) |
92 : id_(id), | 91 : id_(id), |
93 bounds_(bounds), | 92 bounds_(bounds), |
94 work_area_(bounds), | 93 work_area_(bounds), |
95 device_scale_factor_(GetForcedDeviceScaleFactor()), | 94 device_scale_factor_(GetForcedDeviceScaleFactor()), |
96 rotation_(ROTATE_0), | 95 rotation_(ROTATE_0), |
97 touch_support_(TOUCH_SUPPORT_UNKNOWN) { | 96 touch_support_(TOUCH_SUPPORT_UNKNOWN) { |
98 #if defined(USE_AURA) | 97 #if defined(USE_AURA) |
99 SetScaleAndBounds(device_scale_factor_, bounds); | 98 SetScaleAndBounds(device_scale_factor_, bounds); |
100 #endif | 99 #endif |
101 } | 100 } |
102 | 101 |
103 Display::~Display() { | 102 Display::~Display() {} |
104 } | |
105 | 103 |
106 int Display::RotationAsDegree() const { | 104 int Display::RotationAsDegree() const { |
107 switch (rotation_) { | 105 switch (rotation_) { |
108 case ROTATE_0: | 106 case ROTATE_0: |
109 return 0; | 107 return 0; |
110 case ROTATE_90: | 108 case ROTATE_90: |
111 return 90; | 109 return 90; |
112 case ROTATE_180: | 110 case ROTATE_180: |
113 return 180; | 111 return 180; |
114 case ROTATE_270: | 112 case ROTATE_270: |
115 return 270; | 113 return 270; |
116 } | 114 } |
117 | |
118 NOTREACHED(); | 115 NOTREACHED(); |
119 return 0; | 116 return 0; |
120 } | 117 } |
121 | 118 |
122 void Display::SetRotationAsDegree(int rotation) { | 119 void Display::SetRotationAsDegree(int rotation) { |
123 switch (rotation) { | 120 switch (rotation) { |
124 case 0: | 121 case 0: |
125 rotation_ = ROTATE_0; | 122 rotation_ = ROTATE_0; |
126 break; | 123 break; |
127 case 90: | 124 case 90: |
128 rotation_ = ROTATE_90; | 125 rotation_ = ROTATE_90; |
129 break; | 126 break; |
130 case 180: | 127 case 180: |
131 rotation_ = ROTATE_180; | 128 rotation_ = ROTATE_180; |
132 break; | 129 break; |
133 case 270: | 130 case 270: |
134 rotation_ = ROTATE_270; | 131 rotation_ = ROTATE_270; |
135 break; | 132 break; |
136 default: | 133 default: |
137 // We should not reach that but we will just ignore the call if we do. | 134 // We should not reach that but we will just ignore the call if we do. |
138 NOTREACHED(); | 135 NOTREACHED(); |
139 } | 136 } |
140 } | 137 } |
141 | 138 |
142 Insets Display::GetWorkAreaInsets() const { | 139 gfx::Insets Display::GetWorkAreaInsets() const { |
143 return gfx::Insets(work_area_.y() - bounds_.y(), | 140 return gfx::Insets(work_area_.y() - bounds_.y(), work_area_.x() - bounds_.x(), |
144 work_area_.x() - bounds_.x(), | |
145 bounds_.bottom() - work_area_.bottom(), | 141 bounds_.bottom() - work_area_.bottom(), |
146 bounds_.right() - work_area_.right()); | 142 bounds_.right() - work_area_.right()); |
147 } | 143 } |
148 | 144 |
149 void Display::SetScaleAndBounds( | 145 void Display::SetScaleAndBounds(float device_scale_factor, |
150 float device_scale_factor, | 146 const gfx::Rect& bounds_in_pixel) { |
151 const gfx::Rect& bounds_in_pixel) { | 147 gfx::Insets insets = bounds_.InsetsFrom(work_area_); |
152 Insets insets = bounds_.InsetsFrom(work_area_); | |
153 if (!HasForceDeviceScaleFactor()) { | 148 if (!HasForceDeviceScaleFactor()) { |
154 #if defined(OS_MACOSX) | 149 #if defined(OS_MACOSX) |
155 // Unless an explicit scale factor was provided for testing, ensure the | 150 // Unless an explicit scale factor was provided for testing, ensure the |
156 // scale is integral. | 151 // scale is integral. |
157 device_scale_factor = static_cast<int>(device_scale_factor); | 152 device_scale_factor = static_cast<int>(device_scale_factor); |
158 #endif | 153 #endif |
159 device_scale_factor_ = device_scale_factor; | 154 device_scale_factor_ = device_scale_factor; |
160 } | 155 } |
161 device_scale_factor_ = std::max(1.0f, device_scale_factor_); | 156 device_scale_factor_ = std::max(1.0f, device_scale_factor_); |
162 bounds_ = gfx::Rect(gfx::ScaleToFlooredPoint(bounds_in_pixel.origin(), | 157 bounds_ = gfx::Rect(gfx::ScaleToFlooredPoint(bounds_in_pixel.origin(), |
(...skipping 16 matching lines...) Expand all Loading... |
179 work_area_.Inset(insets); | 174 work_area_.Inset(insets); |
180 } | 175 } |
181 | 176 |
182 gfx::Size Display::GetSizeInPixel() const { | 177 gfx::Size Display::GetSizeInPixel() const { |
183 return gfx::ScaleToFlooredSize(size(), device_scale_factor_); | 178 return gfx::ScaleToFlooredSize(size(), device_scale_factor_); |
184 } | 179 } |
185 | 180 |
186 std::string Display::ToString() const { | 181 std::string Display::ToString() const { |
187 return base::StringPrintf( | 182 return base::StringPrintf( |
188 "Display[%lld] bounds=%s, workarea=%s, scale=%f, %s", | 183 "Display[%lld] bounds=%s, workarea=%s, scale=%f, %s", |
189 static_cast<long long int>(id_), | 184 static_cast<long long int>(id_), bounds_.ToString().c_str(), |
190 bounds_.ToString().c_str(), | 185 work_area_.ToString().c_str(), device_scale_factor_, |
191 work_area_.ToString().c_str(), | |
192 device_scale_factor_, | |
193 IsInternal() ? "internal" : "external"); | 186 IsInternal() ? "internal" : "external"); |
194 } | 187 } |
195 | 188 |
196 bool Display::IsInternal() const { | 189 bool Display::IsInternal() const { |
197 return is_valid() && (id_ == internal_display_id_); | 190 return is_valid() && (id_ == internal_display_id_); |
198 } | 191 } |
199 | 192 |
200 // static | 193 // static |
201 int64_t Display::InternalDisplayId() { | 194 int64_t Display::InternalDisplayId() { |
202 DCHECK_NE(kInvalidDisplayID, internal_display_id_); | 195 DCHECK_NE(kInvalidDisplayID, internal_display_id_); |
203 return internal_display_id_; | 196 return internal_display_id_; |
204 } | 197 } |
205 | 198 |
206 // static | 199 // static |
207 void Display::SetInternalDisplayId(int64_t internal_display_id) { | 200 void Display::SetInternalDisplayId(int64_t internal_display_id) { |
208 internal_display_id_ = internal_display_id; | 201 internal_display_id_ = internal_display_id; |
209 } | 202 } |
210 | 203 |
211 // static | 204 // static |
212 bool Display::IsInternalDisplayId(int64_t display_id) { | 205 bool Display::IsInternalDisplayId(int64_t display_id) { |
213 DCHECK_NE(kInvalidDisplayID, display_id); | 206 DCHECK_NE(kInvalidDisplayID, display_id); |
214 return HasInternalDisplay() && internal_display_id_ == display_id; | 207 return HasInternalDisplay() && internal_display_id_ == display_id; |
215 } | 208 } |
216 | 209 |
217 // static | 210 // static |
218 bool Display::HasInternalDisplay() { | 211 bool Display::HasInternalDisplay() { |
219 return internal_display_id_ != kInvalidDisplayID; | 212 return internal_display_id_ != kInvalidDisplayID; |
220 } | 213 } |
221 | 214 |
222 } // namespace gfx | 215 } // namespace display |
OLD | NEW |