Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ui / gfx / display.cc
blob7b77ae40eac7e72f1a6eb4a16b14bc6ee36cf6e7
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"
7 #include <algorithm>
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"
19 namespace gfx {
20 namespace {
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
24 // is checked.
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()) {
40 std::string value =
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;
53 } // namespace
55 const int64 Display::kInvalidDisplayID = -1;
57 // static
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;
64 // static
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;
71 // static
72 void Display::ResetForceDeviceScaleFactorForTesting() {
73 g_has_forced_device_scale_factor = -1;
74 g_forced_device_scale_factor = -1.0;
77 Display::Display()
78 : id_(kInvalidDisplayID),
79 device_scale_factor_(GetForcedDeviceScaleFactor()),
80 rotation_(ROTATE_0),
81 touch_support_(TOUCH_SUPPORT_UNKNOWN) {
84 Display::Display(int64 id)
85 : id_(id),
86 device_scale_factor_(GetForcedDeviceScaleFactor()),
87 rotation_(ROTATE_0),
88 touch_support_(TOUCH_SUPPORT_UNKNOWN) {
91 Display::Display(int64 id, const gfx::Rect& bounds)
92 : id_(id),
93 bounds_(bounds),
94 work_area_(bounds),
95 device_scale_factor_(GetForcedDeviceScaleFactor()),
96 rotation_(ROTATE_0),
97 touch_support_(TOUCH_SUPPORT_UNKNOWN) {
98 #if defined(USE_AURA)
99 SetScaleAndBounds(device_scale_factor_, bounds);
100 #endif
103 Display::~Display() {
106 int Display::RotationAsDegree() const {
107 switch (rotation_) {
108 case ROTATE_0:
109 return 0;
110 case ROTATE_90:
111 return 90;
112 case ROTATE_180:
113 return 180;
114 case ROTATE_270:
115 return 270;
118 NOTREACHED();
119 return 0;
122 void Display::SetRotationAsDegree(int rotation) {
123 switch (rotation) {
124 case 0:
125 rotation_ = ROTATE_0;
126 break;
127 case 90:
128 rotation_ = ROTATE_90;
129 break;
130 case 180:
131 rotation_ = ROTATE_180;
132 break;
133 case 270:
134 rotation_ = ROTATE_270;
135 break;
136 default:
137 // We should not reach that but we will just ignore the call if we do.
138 NOTREACHED();
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);
158 #endif
159 device_scale_factor_ = device_scale_factor;
161 device_scale_factor_ = std::max(1.0f, device_scale_factor_);
162 bounds_ = gfx::Rect(
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);
176 #endif
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_);
203 // static
204 int64 Display::InternalDisplayId() {
205 DCHECK_NE(kInvalidDisplayID, internal_display_id_);
206 return internal_display_id_;
209 // static
210 void Display::SetInternalDisplayId(int64 internal_display_id) {
211 internal_display_id_ = internal_display_id;
214 // static
215 bool Display::IsInternalDisplayId(int64 display_id) {
216 DCHECK_NE(kInvalidDisplayID, display_id);
217 return HasInternalDisplay() && internal_display_id_ == display_id;
220 // static
221 bool Display::HasInternalDisplay() {
222 return internal_display_id_ != kInvalidDisplayID;
225 } // namespace gfx