Revert 160993 - Revert 160976 - The center of a rect is x+width/2, y+height/2
[chromium-blink-merge.git] / ui / gfx / display.cc
blobe647f650603ff777e9a2c46931e5e11c7d3c645a
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 "base/command_line.h"
8 #include "base/logging.h"
9 #include "base/string_number_conversions.h"
10 #include "base/stringprintf.h"
11 #include "ui/base/ui_base_switches.h"
12 #include "ui/gfx/insets.h"
13 #include "ui/gfx/size_conversions.h"
15 namespace gfx {
16 namespace {
18 bool HasForceDeviceScaleFactor() {
19 return CommandLine::ForCurrentProcess()->HasSwitch(
20 switches::kForceDeviceScaleFactor);
23 float GetForcedDeviceScaleFactorImpl() {
24 double scale_in_double = 1.0;
25 if (HasForceDeviceScaleFactor()) {
26 std::string value = CommandLine::ForCurrentProcess()->
27 GetSwitchValueASCII(switches::kForceDeviceScaleFactor);
28 if (!base::StringToDouble(value, &scale_in_double))
29 LOG(ERROR) << "Failed to parse the deafult device scale factor:" << value;
31 return static_cast<float>(scale_in_double);
34 } // namespace
36 const int64 Display::kInvalidDisplayID = -1;
38 // static
39 float Display::GetForcedDeviceScaleFactor() {
40 static const float kForcedDeviceScaleFactor =
41 GetForcedDeviceScaleFactorImpl();
42 return kForcedDeviceScaleFactor;
45 // static
46 int64 Display::GetID(uint16 manufacturer_id, uint32 serial_number) {
47 int64 new_id = ((static_cast<int64>(manufacturer_id) << 32) | serial_number);
48 DCHECK_NE(kInvalidDisplayID, new_id);
49 return new_id;
52 Display::Display()
53 : id_(kInvalidDisplayID),
54 device_scale_factor_(GetForcedDeviceScaleFactor()) {
57 Display::Display(int64 id)
58 : id_(id),
59 device_scale_factor_(GetForcedDeviceScaleFactor()) {
62 Display::Display(int64 id, const gfx::Rect& bounds)
63 : id_(id),
64 bounds_(bounds),
65 work_area_(bounds),
66 device_scale_factor_(GetForcedDeviceScaleFactor()) {
67 #if defined(USE_AURA)
68 SetScaleAndBounds(device_scale_factor_, bounds);
69 #endif
72 Display::~Display() {
75 Insets Display::GetWorkAreaInsets() const {
76 return gfx::Insets(work_area_.y() - bounds_.y(),
77 work_area_.x() - bounds_.x(),
78 bounds_.bottom() - work_area_.bottom(),
79 bounds_.right() - work_area_.right());
82 void Display::SetScaleAndBounds(
83 float device_scale_factor,
84 const gfx::Rect& bounds_in_pixel) {
85 Insets insets = bounds_.InsetsFrom(work_area_);
86 if (!HasForceDeviceScaleFactor())
87 device_scale_factor_ = device_scale_factor;
88 #if defined(USE_AURA)
89 bounds_in_pixel_ = bounds_in_pixel;
90 #endif
91 bounds_ = gfx::Rect(gfx::ToFlooredSize(
92 bounds_in_pixel.size().Scale(1.0f / device_scale_factor_)));
93 UpdateWorkAreaFromInsets(insets);
96 void Display::SetSize(const gfx::Size& size_in_pixel) {
97 SetScaleAndBounds(
98 device_scale_factor_,
99 #if defined(USE_AURA)
100 gfx::Rect(bounds_in_pixel_.origin(), size_in_pixel));
101 #else
102 gfx::Rect(bounds_.origin(), size_in_pixel));
103 #endif
106 void Display::UpdateWorkAreaFromInsets(const gfx::Insets& insets) {
107 work_area_ = bounds_;
108 work_area_.Inset(insets);
111 gfx::Size Display::GetSizeInPixel() const {
112 return gfx::ToFlooredSize(size().Scale(device_scale_factor_));
115 std::string Display::ToString() const {
116 return base::StringPrintf("Display[%lld] bounds=%s, workarea=%s, scale=%f",
117 static_cast<long long int>(id_),
118 bounds_.ToString().c_str(),
119 work_area_.ToString().c_str(),
120 device_scale_factor_);
123 } // namespace gfx