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 const int64 kInvalidDisplayIDForCompileTimeInit = -1;
39 int64 internal_display_id_ = kInvalidDisplayIDForCompileTimeInit;
43 const int64 Display::kInvalidDisplayID = kInvalidDisplayIDForCompileTimeInit;
46 float Display::GetForcedDeviceScaleFactor() {
47 static const float kForcedDeviceScaleFactor =
48 GetForcedDeviceScaleFactorImpl();
49 return kForcedDeviceScaleFactor;
53 bool Display::HasForceDeviceScaleFactor() {
54 return HasForceDeviceScaleFactorImpl();
58 : id_(kInvalidDisplayID),
59 device_scale_factor_(GetForcedDeviceScaleFactor()),
61 touch_support_(TOUCH_SUPPORT_UNKNOWN) {
64 Display::Display(int64 id)
66 device_scale_factor_(GetForcedDeviceScaleFactor()),
68 touch_support_(TOUCH_SUPPORT_UNKNOWN) {
71 Display::Display(int64 id, const gfx::Rect& bounds)
75 device_scale_factor_(GetForcedDeviceScaleFactor()),
77 touch_support_(TOUCH_SUPPORT_UNKNOWN) {
79 SetScaleAndBounds(device_scale_factor_, bounds);
86 Insets Display::GetWorkAreaInsets() const {
87 return gfx::Insets(work_area_.y() - bounds_.y(),
88 work_area_.x() - bounds_.x(),
89 bounds_.bottom() - work_area_.bottom(),
90 bounds_.right() - work_area_.right());
93 void Display::SetScaleAndBounds(
94 float device_scale_factor,
95 const gfx::Rect& bounds_in_pixel) {
96 Insets insets = bounds_.InsetsFrom(work_area_);
97 if (!HasForceDeviceScaleFactor()) {
98 #if defined(OS_MACOSX)
99 // Unless an explicit scale factor was provided for testing, ensure the
100 // scale is integral.
101 device_scale_factor = static_cast<int>(device_scale_factor);
103 device_scale_factor_ = device_scale_factor;
105 device_scale_factor_ = std::max(1.0f, device_scale_factor_);
107 gfx::ToFlooredPoint(gfx::ScalePoint(bounds_in_pixel.origin(),
108 1.0f / device_scale_factor_)),
109 gfx::ToFlooredSize(gfx::ScaleSize(bounds_in_pixel.size(),
110 1.0f / device_scale_factor_)));
111 UpdateWorkAreaFromInsets(insets);
114 void Display::SetSize(const gfx::Size& size_in_pixel) {
115 gfx::Point origin = bounds_.origin();
116 #if defined(USE_AURA)
117 gfx::PointF origin_f = origin;
118 origin_f.Scale(device_scale_factor_);
119 origin.SetPoint(origin_f.x(), origin_f.y());
121 SetScaleAndBounds(device_scale_factor_, gfx::Rect(origin, size_in_pixel));
124 void Display::UpdateWorkAreaFromInsets(const gfx::Insets& insets) {
125 work_area_ = bounds_;
126 work_area_.Inset(insets);
129 gfx::Size Display::GetSizeInPixel() const {
130 return gfx::ToFlooredSize(gfx::ScaleSize(size(), device_scale_factor_));
133 std::string Display::ToString() const {
134 return base::StringPrintf(
135 "Display[%lld] bounds=%s, workarea=%s, scale=%f, %s",
136 static_cast<long long int>(id_),
137 bounds_.ToString().c_str(),
138 work_area_.ToString().c_str(),
139 device_scale_factor_,
140 IsInternal() ? "internal" : "external");
143 bool Display::IsInternal() const {
144 return is_valid() && (id_ == internal_display_id_);
147 int64 Display::InternalDisplayId() {
148 return internal_display_id_;
151 void Display::SetInternalDisplayId(int64 internal_display_id) {
152 internal_display_id_ = internal_display_id;