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
3 // found in the LICENSE file.
5 #include "ui/gfx/display.h"
9 #include "base/command_line.h"
10 #include "base/logging.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/strings/stringprintf.h"
13 #include "ui/gfx/geometry/insets.h"
14 #include "ui/gfx/geometry/point_conversions.h"
15 #include "ui/gfx/geometry/point_f.h"
16 #include "ui/gfx/geometry/size_conversions.h"
17 #include "ui/gfx/switches.h"
22 // This variable tracks whether the forced device scale factor switch needs to
23 // be read from the command line, i.e. if it is set to -1 then the command line
25 int g_has_forced_device_scale_factor
= -1;
27 // This variable caches the forced device scale factor value which is read off
28 // the command line. If the cache is invalidated by setting this variable to
29 // -1.0, we read the forced device scale factor again.
30 float g_forced_device_scale_factor
= -1.0;
32 bool HasForceDeviceScaleFactorImpl() {
33 return base::CommandLine::ForCurrentProcess()->HasSwitch(
34 switches::kForceDeviceScaleFactor
);
37 float GetForcedDeviceScaleFactorImpl() {
38 double scale_in_double
= 1.0;
39 if (HasForceDeviceScaleFactorImpl()) {
41 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
42 switches::kForceDeviceScaleFactor
);
43 if (!base::StringToDouble(value
, &scale_in_double
))
44 LOG(ERROR
) << "Failed to parse the default device scale factor:" << value
;
46 return static_cast<float>(scale_in_double
);
49 int64 internal_display_id_
= -1;
53 const int64
Display::kInvalidDisplayID
= -1;
56 float Display::GetForcedDeviceScaleFactor() {
57 if (g_forced_device_scale_factor
< 0)
58 g_forced_device_scale_factor
= GetForcedDeviceScaleFactorImpl();
59 return g_forced_device_scale_factor
;
63 bool Display::HasForceDeviceScaleFactor() {
64 if (g_has_forced_device_scale_factor
== -1)
65 g_has_forced_device_scale_factor
= HasForceDeviceScaleFactorImpl();
66 return !!g_has_forced_device_scale_factor
;
70 void Display::ResetForceDeviceScaleFactorForTesting() {
71 g_has_forced_device_scale_factor
= -1;
72 g_forced_device_scale_factor
= -1.0;
76 : id_(kInvalidDisplayID
),
77 device_scale_factor_(GetForcedDeviceScaleFactor()),
79 touch_support_(TOUCH_SUPPORT_UNKNOWN
) {
82 Display::Display(int64 id
)
84 device_scale_factor_(GetForcedDeviceScaleFactor()),
86 touch_support_(TOUCH_SUPPORT_UNKNOWN
) {
89 Display::Display(int64 id
, const gfx::Rect
& bounds
)
93 device_scale_factor_(GetForcedDeviceScaleFactor()),
95 touch_support_(TOUCH_SUPPORT_UNKNOWN
) {
97 SetScaleAndBounds(device_scale_factor_
, bounds
);
101 Display::~Display() {
104 int Display::RotationAsDegree() const {
120 void Display::SetRotationAsDegree(int rotation
) {
123 rotation_
= ROTATE_0
;
126 rotation_
= ROTATE_90
;
129 rotation_
= ROTATE_180
;
132 rotation_
= ROTATE_270
;
135 // We should not reach that but we will just ignore the call if we do.
140 Insets
Display::GetWorkAreaInsets() const {
141 return gfx::Insets(work_area_
.y() - bounds_
.y(),
142 work_area_
.x() - bounds_
.x(),
143 bounds_
.bottom() - work_area_
.bottom(),
144 bounds_
.right() - work_area_
.right());
147 void Display::SetScaleAndBounds(
148 float device_scale_factor
,
149 const gfx::Rect
& bounds_in_pixel
) {
150 Insets insets
= bounds_
.InsetsFrom(work_area_
);
151 if (!HasForceDeviceScaleFactor()) {
152 #if defined(OS_MACOSX)
153 // Unless an explicit scale factor was provided for testing, ensure the
154 // scale is integral.
155 device_scale_factor
= static_cast<int>(device_scale_factor
);
157 device_scale_factor_
= device_scale_factor
;
159 device_scale_factor_
= std::max(1.0f
, device_scale_factor_
);
161 gfx::ToFlooredPoint(gfx::ScalePoint(bounds_in_pixel
.origin(),
162 1.0f
/ device_scale_factor_
)),
163 gfx::ToFlooredSize(gfx::ScaleSize(bounds_in_pixel
.size(),
164 1.0f
/ device_scale_factor_
)));
165 UpdateWorkAreaFromInsets(insets
);
168 void Display::SetSize(const gfx::Size
& size_in_pixel
) {
169 gfx::Point origin
= bounds_
.origin();
170 #if defined(USE_AURA)
171 gfx::PointF origin_f
= origin
;
172 origin_f
.Scale(device_scale_factor_
);
173 origin
= gfx::ToFlooredPoint(origin_f
);
175 SetScaleAndBounds(device_scale_factor_
, gfx::Rect(origin
, size_in_pixel
));
178 void Display::UpdateWorkAreaFromInsets(const gfx::Insets
& insets
) {
179 work_area_
= bounds_
;
180 work_area_
.Inset(insets
);
183 gfx::Size
Display::GetSizeInPixel() const {
184 return gfx::ToFlooredSize(gfx::ScaleSize(size(), device_scale_factor_
));
187 std::string
Display::ToString() const {
188 return base::StringPrintf(
189 "Display[%lld] bounds=%s, workarea=%s, scale=%f, %s",
190 static_cast<long long int>(id_
),
191 bounds_
.ToString().c_str(),
192 work_area_
.ToString().c_str(),
193 device_scale_factor_
,
194 IsInternal() ? "internal" : "external");
197 bool Display::IsInternal() const {
198 return is_valid() && (id_
== internal_display_id_
);
202 int64
Display::InternalDisplayId() {
203 return internal_display_id_
;
207 void Display::SetInternalDisplayId(int64 internal_display_id
) {
208 internal_display_id_
= internal_display_id
;
212 bool Display::HasInternalDisplay() {
213 return internal_display_id_
!= kInvalidDisplayID
;