base: Change DCHECK_IS_ON to a macro DCHECK_IS_ON().
[chromium-blink-merge.git] / ui / gfx / display.cc
blobc79dcea9ed1dcfedc7a2959cb67710a89a20841e
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 bool HasForceDeviceScaleFactorImpl() {
23 return base::CommandLine::ForCurrentProcess()->HasSwitch(
24 switches::kForceDeviceScaleFactor);
27 float GetForcedDeviceScaleFactorImpl() {
28 double scale_in_double = 1.0;
29 if (HasForceDeviceScaleFactorImpl()) {
30 std::string value =
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;
41 } // namespace
43 const int64 Display::kInvalidDisplayID = -1;
45 // static
46 float Display::GetForcedDeviceScaleFactor() {
47 static const float kForcedDeviceScaleFactor =
48 GetForcedDeviceScaleFactorImpl();
49 return kForcedDeviceScaleFactor;
52 //static
53 bool Display::HasForceDeviceScaleFactor() {
54 static const bool kHasForceDeviceScaleFactor =
55 HasForceDeviceScaleFactorImpl();
56 return kHasForceDeviceScaleFactor;
59 Display::Display()
60 : id_(kInvalidDisplayID),
61 device_scale_factor_(GetForcedDeviceScaleFactor()),
62 rotation_(ROTATE_0),
63 touch_support_(TOUCH_SUPPORT_UNKNOWN) {
66 Display::Display(int64 id)
67 : id_(id),
68 device_scale_factor_(GetForcedDeviceScaleFactor()),
69 rotation_(ROTATE_0),
70 touch_support_(TOUCH_SUPPORT_UNKNOWN) {
73 Display::Display(int64 id, const gfx::Rect& bounds)
74 : id_(id),
75 bounds_(bounds),
76 work_area_(bounds),
77 device_scale_factor_(GetForcedDeviceScaleFactor()),
78 rotation_(ROTATE_0),
79 touch_support_(TOUCH_SUPPORT_UNKNOWN) {
80 #if defined(USE_AURA)
81 SetScaleAndBounds(device_scale_factor_, bounds);
82 #endif
85 Display::~Display() {
88 int Display::RotationAsDegree() const {
89 switch (rotation_) {
90 case ROTATE_0:
91 return 0;
92 case ROTATE_90:
93 return 90;
94 case ROTATE_180:
95 return 180;
96 case ROTATE_270:
97 return 270;
100 NOTREACHED();
101 return 0;
104 void Display::SetRotationAsDegree(int rotation) {
105 switch (rotation) {
106 case 0:
107 rotation_ = ROTATE_0;
108 break;
109 case 90:
110 rotation_ = ROTATE_90;
111 break;
112 case 180:
113 rotation_ = ROTATE_180;
114 break;
115 case 270:
116 rotation_ = ROTATE_270;
117 break;
118 default:
119 // We should not reach that but we will just ignore the call if we do.
120 NOTREACHED();
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);
140 #endif
141 device_scale_factor_ = device_scale_factor;
143 device_scale_factor_ = std::max(1.0f, device_scale_factor_);
144 bounds_ = gfx::Rect(
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);
158 #endif
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;
193 } // namespace gfx