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. 4 5 #include "ui/gfx/display.h" 6 7 #include <algorithm> 8 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" 18 19 namespace gfx { 20 namespace { 21 22 bool HasForceDeviceScaleFactorImpl() { 23 return CommandLine::ForCurrentProcess()->HasSwitch( 24 switches::kForceDeviceScaleFactor); 25 } 26 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; 34 } 35 return static_cast<float>(scale_in_double); 36 } 37 38 const int64 kInvalidDisplayIDForCompileTimeInit = -1; 39 int64 internal_display_id_ = kInvalidDisplayIDForCompileTimeInit; 40 41 } // namespace 42 43 const int64 Display::kInvalidDisplayID = kInvalidDisplayIDForCompileTimeInit; 44 45 // static 46 float Display::GetForcedDeviceScaleFactor() { 47 static const float kForcedDeviceScaleFactor = 48 GetForcedDeviceScaleFactorImpl(); 49 return kForcedDeviceScaleFactor; 50 } 51 52 //static 53 bool Display::HasForceDeviceScaleFactor() { 54 return HasForceDeviceScaleFactorImpl(); 55 } 56 57 Display::Display() 58 : id_(kInvalidDisplayID), 59 device_scale_factor_(GetForcedDeviceScaleFactor()), 60 rotation_(ROTATE_0), 61 touch_support_(TOUCH_SUPPORT_UNKNOWN) { 62 } 63 64 Display::Display(int64 id) 65 : id_(id), 66 device_scale_factor_(GetForcedDeviceScaleFactor()), 67 rotation_(ROTATE_0), 68 touch_support_(TOUCH_SUPPORT_UNKNOWN) { 69 } 70 71 Display::Display(int64 id, const gfx::Rect& bounds) 72 : id_(id), 73 bounds_(bounds), 74 work_area_(bounds), 75 device_scale_factor_(GetForcedDeviceScaleFactor()), 76 rotation_(ROTATE_0), 77 touch_support_(TOUCH_SUPPORT_UNKNOWN) { 78 #if defined(USE_AURA) 79 SetScaleAndBounds(device_scale_factor_, bounds); 80 #endif 81 } 82 83 Display::~Display() { 84 } 85 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()); 91 } 92 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); 102 #endif 103 device_scale_factor_ = device_scale_factor; 104 } 105 device_scale_factor_ = std::max(1.0f, device_scale_factor_); 106 bounds_ = gfx::Rect( 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); 112 } 113 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()); 120 #endif 121 SetScaleAndBounds(device_scale_factor_, gfx::Rect(origin, size_in_pixel)); 122 } 123 124 void Display::UpdateWorkAreaFromInsets(const gfx::Insets& insets) { 125 work_area_ = bounds_; 126 work_area_.Inset(insets); 127 } 128 129 gfx::Size Display::GetSizeInPixel() const { 130 return gfx::ToFlooredSize(gfx::ScaleSize(size(), device_scale_factor_)); 131 } 132 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"); 141 } 142 143 bool Display::IsInternal() const { 144 return is_valid() && (id_ == internal_display_id_); 145 } 146 147 int64 Display::InternalDisplayId() { 148 return internal_display_id_; 149 } 150 151 void Display::SetInternalDisplayId(int64 internal_display_id) { 152 internal_display_id_ = internal_display_id; 153 } 154 155 } // namespace gfx 156