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"
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
);
36 const int64
Display::kInvalidDisplayID
= -1;
39 float Display::GetForcedDeviceScaleFactor() {
40 static const float kForcedDeviceScaleFactor
=
41 GetForcedDeviceScaleFactorImpl();
42 return kForcedDeviceScaleFactor
;
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
);
53 : id_(kInvalidDisplayID
),
54 device_scale_factor_(GetForcedDeviceScaleFactor()) {
57 Display::Display(int64 id
)
59 device_scale_factor_(GetForcedDeviceScaleFactor()) {
62 Display::Display(int64 id
, const gfx::Rect
& bounds
)
66 device_scale_factor_(GetForcedDeviceScaleFactor()) {
68 SetScaleAndBounds(device_scale_factor_
, bounds
);
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
;
89 bounds_in_pixel_
= bounds_in_pixel
;
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
) {
100 gfx::Rect(bounds_in_pixel_
.origin(), size_in_pixel
));
102 gfx::Rect(bounds_
.origin(), size_in_pixel
));
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_
);