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/insets.h"
14 #include "ui/gfx/point_conversions.h"
15 #include "ui/gfx/point_f.h"
16 #include "ui/gfx/size_conversions.h"
17 #include "ui/gfx/switches.h"
22 bool HasForceDeviceScaleFactorImpl() {
23 return CommandLine::ForCurrentProcess()->HasSwitch(
24 switches::kForceDeviceScaleFactor);
27 float GetForcedDeviceScaleFactorImpl() {
28 double scale_in_double = 1.0;
29 if (HasForceDeviceScaleFactorImpl()) {
30 std::string value = CommandLine::ForCurrentProcess()->
31 GetSwitchValueASCII(switches::kForceDeviceScaleFactor);
32 if (!base::StringToDouble(value, &scale_in_double))
33 LOG(ERROR) << "Failed to parse the default device scale factor:" << value;
35 return static_cast<float>(scale_in_double);
38 int64 internal_display_id_ = -1;
42 const int64 Display::kInvalidDisplayID = -1;
45 float Display::GetForcedDeviceScaleFactor() {
46 static const float kForcedDeviceScaleFactor =
47 GetForcedDeviceScaleFactorImpl();
48 return kForcedDeviceScaleFactor;
52 bool Display::HasForceDeviceScaleFactor() {
53 static const bool kHasForceDeviceScaleFactor =
54 HasForceDeviceScaleFactorImpl();
55 return kHasForceDeviceScaleFactor;
59 : id_(kInvalidDisplayID),
60 device_scale_factor_(GetForcedDeviceScaleFactor()),
62 touch_support_(TOUCH_SUPPORT_UNKNOWN) {
65 Display::Display(int64 id)
67 device_scale_factor_(GetForcedDeviceScaleFactor()),
69 touch_support_(TOUCH_SUPPORT_UNKNOWN) {
72 Display::Display(int64 id, const gfx::Rect& bounds)
76 device_scale_factor_(GetForcedDeviceScaleFactor()),
78 touch_support_(TOUCH_SUPPORT_UNKNOWN) {
80 SetScaleAndBounds(device_scale_factor_, bounds);
87 int Display::RotationAsDegree() const {
103 void Display::SetRotationAsDegree(int rotation) {
106 rotation_ = ROTATE_0;
109 rotation_ = ROTATE_90;
112 rotation_ = ROTATE_180;
115 rotation_ = ROTATE_270;
118 // We should not reach that but we will just ignore the call if we do.
123 Insets Display::GetWorkAreaInsets() const {
124 return gfx::Insets(work_area_.y() - bounds_.y(),
125 work_area_.x() - bounds_.x(),
126 bounds_.bottom() - work_area_.bottom(),
127 bounds_.right() - work_area_.right());
130 void Display::SetScaleAndBounds(
131 float device_scale_factor,
132 const gfx::Rect& bounds_in_pixel) {
133 Insets insets = bounds_.InsetsFrom(work_area_);
134 if (!HasForceDeviceScaleFactor()) {
135 #if defined(OS_MACOSX)
136 // Unless an explicit scale factor was provided for testing, ensure the
137 // scale is integral.
138 device_scale_factor = static_cast<int>(device_scale_factor);
140 device_scale_factor_ = device_scale_factor;
142 device_scale_factor_ = std::max(1.0f, device_scale_factor_);
144 gfx::ToFlooredPoint(gfx::ScalePoint(bounds_in_pixel.origin(),
145 1.0f / device_scale_factor_)),
146 gfx::ToFlooredSize(gfx::ScaleSize(bounds_in_pixel.size(),
147 1.0f / device_scale_factor_)));
148 UpdateWorkAreaFromInsets(insets);
151 void Display::SetSize(const gfx::Size& size_in_pixel) {
152 gfx::Point origin = bounds_.origin();
153 #if defined(USE_AURA)
154 gfx::PointF origin_f = origin;
155 origin_f.Scale(device_scale_factor_);
156 origin = gfx::ToFlooredPoint(origin_f);
158 SetScaleAndBounds(device_scale_factor_, gfx::Rect(origin, size_in_pixel));
161 void Display::UpdateWorkAreaFromInsets(const gfx::Insets& insets) {
162 work_area_ = bounds_;
163 work_area_.Inset(insets);
166 gfx::Size Display::GetSizeInPixel() const {
167 return gfx::ToFlooredSize(gfx::ScaleSize(size(), device_scale_factor_));
170 std::string Display::ToString() const {
171 return base::StringPrintf(
172 "Display[%lld] bounds=%s, workarea=%s, scale=%f, %s",
173 static_cast<long long int>(id_),
174 bounds_.ToString().c_str(),
175 work_area_.ToString().c_str(),
176 device_scale_factor_,
177 IsInternal() ? "internal" : "external");
180 bool Display::IsInternal() const {
181 return is_valid() && (id_ == internal_display_id_);
184 int64 Display::InternalDisplayId() {
185 return internal_display_id_;
188 void Display::SetInternalDisplayId(int64 internal_display_id) {
189 internal_display_id_ = internal_display_id;