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 bool HasForceDeviceScaleFactorImpl() {
23 return base::CommandLine::ForCurrentProcess()->HasSwitch(
24 switches::kForceDeviceScaleFactor
);
27 float GetForcedDeviceScaleFactorImpl() {
28 double scale_in_double
= 1.0;
29 if (HasForceDeviceScaleFactorImpl()) {
31 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
32 switches::kForceDeviceScaleFactor
);
33 if (!base::StringToDouble(value
, &scale_in_double
))
34 LOG(ERROR
) << "Failed to parse the default device scale factor:" << value
;
36 return static_cast<float>(scale_in_double
);
39 int64 internal_display_id_
= -1;
43 const int64
Display::kInvalidDisplayID
= -1;
46 float Display::GetForcedDeviceScaleFactor() {
47 static const float kForcedDeviceScaleFactor
=
48 GetForcedDeviceScaleFactorImpl();
49 return kForcedDeviceScaleFactor
;
53 bool Display::HasForceDeviceScaleFactor() {
54 static const bool kHasForceDeviceScaleFactor
=
55 HasForceDeviceScaleFactorImpl();
56 return kHasForceDeviceScaleFactor
;
60 : id_(kInvalidDisplayID
),
61 device_scale_factor_(GetForcedDeviceScaleFactor()),
63 touch_support_(TOUCH_SUPPORT_UNKNOWN
) {
66 Display::Display(int64 id
)
68 device_scale_factor_(GetForcedDeviceScaleFactor()),
70 touch_support_(TOUCH_SUPPORT_UNKNOWN
) {
73 Display::Display(int64 id
, const gfx::Rect
& bounds
)
77 device_scale_factor_(GetForcedDeviceScaleFactor()),
79 touch_support_(TOUCH_SUPPORT_UNKNOWN
) {
81 SetScaleAndBounds(device_scale_factor_
, bounds
);
88 int Display::RotationAsDegree() const {
104 void Display::SetRotationAsDegree(int rotation
) {
107 rotation_
= ROTATE_0
;
110 rotation_
= ROTATE_90
;
113 rotation_
= ROTATE_180
;
116 rotation_
= ROTATE_270
;
119 // We should not reach that but we will just ignore the call if we do.
124 Insets
Display::GetWorkAreaInsets() const {
125 return gfx::Insets(work_area_
.y() - bounds_
.y(),
126 work_area_
.x() - bounds_
.x(),
127 bounds_
.bottom() - work_area_
.bottom(),
128 bounds_
.right() - work_area_
.right());
131 void Display::SetScaleAndBounds(
132 float device_scale_factor
,
133 const gfx::Rect
& bounds_in_pixel
) {
134 Insets insets
= bounds_
.InsetsFrom(work_area_
);
135 if (!HasForceDeviceScaleFactor()) {
136 #if defined(OS_MACOSX)
137 // Unless an explicit scale factor was provided for testing, ensure the
138 // scale is integral.
139 device_scale_factor
= static_cast<int>(device_scale_factor
);
141 device_scale_factor_
= device_scale_factor
;
143 device_scale_factor_
= std::max(1.0f
, device_scale_factor_
);
145 gfx::ToFlooredPoint(gfx::ScalePoint(bounds_in_pixel
.origin(),
146 1.0f
/ device_scale_factor_
)),
147 gfx::ToFlooredSize(gfx::ScaleSize(bounds_in_pixel
.size(),
148 1.0f
/ device_scale_factor_
)));
149 UpdateWorkAreaFromInsets(insets
);
152 void Display::SetSize(const gfx::Size
& size_in_pixel
) {
153 gfx::Point origin
= bounds_
.origin();
154 #if defined(USE_AURA)
155 gfx::PointF origin_f
= origin
;
156 origin_f
.Scale(device_scale_factor_
);
157 origin
= gfx::ToFlooredPoint(origin_f
);
159 SetScaleAndBounds(device_scale_factor_
, gfx::Rect(origin
, size_in_pixel
));
162 void Display::UpdateWorkAreaFromInsets(const gfx::Insets
& insets
) {
163 work_area_
= bounds_
;
164 work_area_
.Inset(insets
);
167 gfx::Size
Display::GetSizeInPixel() const {
168 return gfx::ToFlooredSize(gfx::ScaleSize(size(), device_scale_factor_
));
171 std::string
Display::ToString() const {
172 return base::StringPrintf(
173 "Display[%lld] bounds=%s, workarea=%s, scale=%f, %s",
174 static_cast<long long int>(id_
),
175 bounds_
.ToString().c_str(),
176 work_area_
.ToString().c_str(),
177 device_scale_factor_
,
178 IsInternal() ? "internal" : "external");
181 bool Display::IsInternal() const {
182 return is_valid() && (id_
== internal_display_id_
);
185 int64
Display::InternalDisplayId() {
186 return internal_display_id_
;
189 void Display::SetInternalDisplayId(int64 internal_display_id
) {
190 internal_display_id_
= internal_display_id
;