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
;
45 scale_in_double
= 1.0;
48 return static_cast<float>(scale_in_double
);
51 int64 internal_display_id_
= -1;
55 const int64
Display::kInvalidDisplayID
= -1;
58 float Display::GetForcedDeviceScaleFactor() {
59 if (g_forced_device_scale_factor
< 0)
60 g_forced_device_scale_factor
= GetForcedDeviceScaleFactorImpl();
61 return g_forced_device_scale_factor
;
65 bool Display::HasForceDeviceScaleFactor() {
66 if (g_has_forced_device_scale_factor
== -1)
67 g_has_forced_device_scale_factor
= HasForceDeviceScaleFactorImpl();
68 return !!g_has_forced_device_scale_factor
;
72 void Display::ResetForceDeviceScaleFactorForTesting() {
73 g_has_forced_device_scale_factor
= -1;
74 g_forced_device_scale_factor
= -1.0;
78 : id_(kInvalidDisplayID
),
79 device_scale_factor_(GetForcedDeviceScaleFactor()),
81 touch_support_(TOUCH_SUPPORT_UNKNOWN
) {
84 Display::Display(int64 id
)
86 device_scale_factor_(GetForcedDeviceScaleFactor()),
88 touch_support_(TOUCH_SUPPORT_UNKNOWN
) {
91 Display::Display(int64 id
, const gfx::Rect
& bounds
)
95 device_scale_factor_(GetForcedDeviceScaleFactor()),
97 touch_support_(TOUCH_SUPPORT_UNKNOWN
) {
99 SetScaleAndBounds(device_scale_factor_
, bounds
);
103 Display::~Display() {
106 int Display::RotationAsDegree() const {
122 void Display::SetRotationAsDegree(int rotation
) {
125 rotation_
= ROTATE_0
;
128 rotation_
= ROTATE_90
;
131 rotation_
= ROTATE_180
;
134 rotation_
= ROTATE_270
;
137 // We should not reach that but we will just ignore the call if we do.
142 Insets
Display::GetWorkAreaInsets() const {
143 return gfx::Insets(work_area_
.y() - bounds_
.y(),
144 work_area_
.x() - bounds_
.x(),
145 bounds_
.bottom() - work_area_
.bottom(),
146 bounds_
.right() - work_area_
.right());
149 void Display::SetScaleAndBounds(
150 float device_scale_factor
,
151 const gfx::Rect
& bounds_in_pixel
) {
152 Insets insets
= bounds_
.InsetsFrom(work_area_
);
153 if (!HasForceDeviceScaleFactor()) {
154 #if defined(OS_MACOSX)
155 // Unless an explicit scale factor was provided for testing, ensure the
156 // scale is integral.
157 device_scale_factor
= static_cast<int>(device_scale_factor
);
159 device_scale_factor_
= device_scale_factor
;
161 device_scale_factor_
= std::max(1.0f
, device_scale_factor_
);
163 gfx::ToFlooredPoint(gfx::ScalePoint(bounds_in_pixel
.origin(),
164 1.0f
/ device_scale_factor_
)),
165 gfx::ToFlooredSize(gfx::ScaleSize(bounds_in_pixel
.size(),
166 1.0f
/ device_scale_factor_
)));
167 UpdateWorkAreaFromInsets(insets
);
170 void Display::SetSize(const gfx::Size
& size_in_pixel
) {
171 gfx::Point origin
= bounds_
.origin();
172 #if defined(USE_AURA)
173 gfx::PointF origin_f
= origin
;
174 origin_f
.Scale(device_scale_factor_
);
175 origin
= gfx::ToFlooredPoint(origin_f
);
177 SetScaleAndBounds(device_scale_factor_
, gfx::Rect(origin
, size_in_pixel
));
180 void Display::UpdateWorkAreaFromInsets(const gfx::Insets
& insets
) {
181 work_area_
= bounds_
;
182 work_area_
.Inset(insets
);
185 gfx::Size
Display::GetSizeInPixel() const {
186 return gfx::ToFlooredSize(gfx::ScaleSize(size(), device_scale_factor_
));
189 std::string
Display::ToString() const {
190 return base::StringPrintf(
191 "Display[%lld] bounds=%s, workarea=%s, scale=%f, %s",
192 static_cast<long long int>(id_
),
193 bounds_
.ToString().c_str(),
194 work_area_
.ToString().c_str(),
195 device_scale_factor_
,
196 IsInternal() ? "internal" : "external");
199 bool Display::IsInternal() const {
200 return is_valid() && (id_
== internal_display_id_
);
204 int64
Display::InternalDisplayId() {
205 DCHECK_NE(kInvalidDisplayID
, internal_display_id_
);
206 return internal_display_id_
;
210 void Display::SetInternalDisplayId(int64 internal_display_id
) {
211 internal_display_id_
= internal_display_id
;
215 bool Display::IsInternalDisplayId(int64 display_id
) {
216 DCHECK_NE(kInvalidDisplayID
, display_id
);
217 return HasInternalDisplay() && internal_display_id_
== display_id
;
221 bool Display::HasInternalDisplay() {
222 return internal_display_id_
!= kInvalidDisplayID
;