Add field trial check for InstanceID API.
[chromium-blink-merge.git] / ui / gfx / display.cc
blob6ae51b6d5b8d32ba6d1de2435e8fed24b8c18a9d
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;
46 return static_cast<float>(scale_in_double);
49 int64 internal_display_id_ = -1;
51 } // namespace
53 const int64 Display::kInvalidDisplayID = -1;
55 // static
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;
62 //static
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;
69 // static
70 void Display::ResetForceDeviceScaleFactorForTesting() {
71 g_has_forced_device_scale_factor = -1;
72 g_forced_device_scale_factor = -1.0;
75 Display::Display()
76 : id_(kInvalidDisplayID),
77 device_scale_factor_(GetForcedDeviceScaleFactor()),
78 rotation_(ROTATE_0),
79 touch_support_(TOUCH_SUPPORT_UNKNOWN) {
82 Display::Display(int64 id)
83 : id_(id),
84 device_scale_factor_(GetForcedDeviceScaleFactor()),
85 rotation_(ROTATE_0),
86 touch_support_(TOUCH_SUPPORT_UNKNOWN) {
89 Display::Display(int64 id, const gfx::Rect& bounds)
90 : id_(id),
91 bounds_(bounds),
92 work_area_(bounds),
93 device_scale_factor_(GetForcedDeviceScaleFactor()),
94 rotation_(ROTATE_0),
95 touch_support_(TOUCH_SUPPORT_UNKNOWN) {
96 #if defined(USE_AURA)
97 SetScaleAndBounds(device_scale_factor_, bounds);
98 #endif
101 Display::~Display() {
104 int Display::RotationAsDegree() const {
105 switch (rotation_) {
106 case ROTATE_0:
107 return 0;
108 case ROTATE_90:
109 return 90;
110 case ROTATE_180:
111 return 180;
112 case ROTATE_270:
113 return 270;
116 NOTREACHED();
117 return 0;
120 void Display::SetRotationAsDegree(int rotation) {
121 switch (rotation) {
122 case 0:
123 rotation_ = ROTATE_0;
124 break;
125 case 90:
126 rotation_ = ROTATE_90;
127 break;
128 case 180:
129 rotation_ = ROTATE_180;
130 break;
131 case 270:
132 rotation_ = ROTATE_270;
133 break;
134 default:
135 // We should not reach that but we will just ignore the call if we do.
136 NOTREACHED();
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);
156 #endif
157 device_scale_factor_ = device_scale_factor;
159 device_scale_factor_ = std::max(1.0f, device_scale_factor_);
160 bounds_ = gfx::Rect(
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);
174 #endif
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_);
201 // static
202 int64 Display::InternalDisplayId() {
203 return internal_display_id_;
206 // static
207 void Display::SetInternalDisplayId(int64 internal_display_id) {
208 internal_display_id_ = internal_display_id;
211 // static
212 bool Display::HasInternalDisplay() {
213 return internal_display_id_ != kInvalidDisplayID;
216 } // namespace gfx