Encode method and error message inside LevelDB::Status message
[chromium-blink-merge.git] / ui / gfx / display.cc
blobfe89b5e9b9ce793b46456a33a58b9f45f872f669
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/point_conversions.h"
14 #include "ui/gfx/point_f.h"
15 #include "ui/gfx/size_conversions.h"
17 namespace gfx {
18 namespace {
20 bool HasForceDeviceScaleFactorImpl() {
21 return CommandLine::ForCurrentProcess()->HasSwitch(
22 switches::kForceDeviceScaleFactor);
25 float GetForcedDeviceScaleFactorImpl() {
26 double scale_in_double = 1.0;
27 if (HasForceDeviceScaleFactorImpl()) {
28 std::string value = CommandLine::ForCurrentProcess()->
29 GetSwitchValueASCII(switches::kForceDeviceScaleFactor);
30 if (!base::StringToDouble(value, &scale_in_double))
31 LOG(ERROR) << "Failed to parse the default device scale factor:" << value;
33 return static_cast<float>(scale_in_double);
36 const int64 kInvalidDisplayIDForCompileTimeInit = -1;
37 int64 internal_display_id_ = kInvalidDisplayIDForCompileTimeInit;
39 } // namespace
41 const int64 Display::kInvalidDisplayID = kInvalidDisplayIDForCompileTimeInit;
43 // static
44 float Display::GetForcedDeviceScaleFactor() {
45 static const float kForcedDeviceScaleFactor =
46 GetForcedDeviceScaleFactorImpl();
47 return kForcedDeviceScaleFactor;
50 //static
51 bool Display::HasForceDeviceScaleFactor() {
52 return HasForceDeviceScaleFactorImpl();
55 Display::Display()
56 : id_(kInvalidDisplayID),
57 device_scale_factor_(GetForcedDeviceScaleFactor()),
58 rotation_(ROTATE_0) {
61 Display::Display(int64 id)
62 : id_(id),
63 device_scale_factor_(GetForcedDeviceScaleFactor()),
64 rotation_(ROTATE_0) {
67 Display::Display(int64 id, const gfx::Rect& bounds)
68 : id_(id),
69 bounds_(bounds),
70 work_area_(bounds),
71 device_scale_factor_(GetForcedDeviceScaleFactor()),
72 rotation_(ROTATE_0) {
73 #if defined(USE_AURA)
74 SetScaleAndBounds(device_scale_factor_, bounds);
75 #endif
78 Display::~Display() {
81 Insets Display::GetWorkAreaInsets() const {
82 return gfx::Insets(work_area_.y() - bounds_.y(),
83 work_area_.x() - bounds_.x(),
84 bounds_.bottom() - work_area_.bottom(),
85 bounds_.right() - work_area_.right());
88 void Display::SetScaleAndBounds(
89 float device_scale_factor,
90 const gfx::Rect& bounds_in_pixel) {
91 Insets insets = bounds_.InsetsFrom(work_area_);
92 if (!HasForceDeviceScaleFactor()) {
93 #if defined(OS_MACOSX)
94 // Unless an explicit scale factor was provided for testing, ensure the
95 // scale is integral.
96 device_scale_factor = static_cast<int>(device_scale_factor);
97 #endif
98 device_scale_factor_ = device_scale_factor;
100 device_scale_factor_ = std::max(1.0f, device_scale_factor_);
101 bounds_ = gfx::Rect(
102 gfx::ToFlooredPoint(gfx::ScalePoint(bounds_in_pixel.origin(),
103 1.0f / device_scale_factor_)),
104 gfx::ToFlooredSize(gfx::ScaleSize(bounds_in_pixel.size(),
105 1.0f / device_scale_factor_)));
106 UpdateWorkAreaFromInsets(insets);
109 void Display::SetSize(const gfx::Size& size_in_pixel) {
110 gfx::Point origin = bounds_.origin();
111 #if defined(USE_AURA)
112 gfx::PointF origin_f = origin;
113 origin_f.Scale(device_scale_factor_);
114 origin.SetPoint(origin_f.x(), origin_f.y());
115 #endif
116 SetScaleAndBounds(device_scale_factor_, gfx::Rect(origin, size_in_pixel));
119 void Display::UpdateWorkAreaFromInsets(const gfx::Insets& insets) {
120 work_area_ = bounds_;
121 work_area_.Inset(insets);
124 gfx::Size Display::GetSizeInPixel() const {
125 return gfx::ToFlooredSize(gfx::ScaleSize(size(), device_scale_factor_));
128 std::string Display::ToString() const {
129 return base::StringPrintf(
130 "Display[%lld] bounds=%s, workarea=%s, scale=%f, %s",
131 static_cast<long long int>(id_),
132 bounds_.ToString().c_str(),
133 work_area_.ToString().c_str(),
134 device_scale_factor_,
135 IsInternal() ? "internal" : "external");
138 bool Display::IsInternal() const {
139 return is_valid() && (id_ == internal_display_id_);
142 int64 Display::InternalDisplayId() {
143 return internal_display_id_;
146 void Display::SetInternalDisplayId(int64 internal_display_id) {
147 internal_display_id_ = internal_display_id;
150 } // namespace gfx