OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/display/fake_display_snapshot.h" | 5 #include "ui/display/fake_display_snapshot.h" |
6 | 6 |
7 #include <inttypes.h> | 7 #include <inttypes.h> |
8 | 8 |
| 9 #include <sstream> |
| 10 #include <utility> |
9 #include <vector> | 11 #include <vector> |
10 | 12 |
| 13 #include "base/memory/ptr_util.h" |
11 #include "base/strings/stringprintf.h" | 14 #include "base/strings/stringprintf.h" |
12 | 15 |
13 namespace display { | 16 namespace display { |
14 | 17 |
15 namespace { | 18 namespace { |
16 | 19 |
17 // For a non high-DPI display (96 dpi) use a pitch of 265µm. | 20 static const float kInchInMm = 25.4f; |
18 static const float kDisplayPitchMM = 0.265f; | 21 |
| 22 // Get pixel pitch in millimeters from DPI. |
| 23 float PixelPitchMmFromDPI(float dpi) { |
| 24 return (1.0f / dpi) * kInchInMm; |
| 25 } |
| 26 |
| 27 std::string ModeListString( |
| 28 const std::vector<std::unique_ptr<const ui::DisplayMode>>& modes) { |
| 29 std::stringstream stream; |
| 30 bool first = true; |
| 31 for (auto& mode : modes) { |
| 32 if (!first) |
| 33 stream << ", "; |
| 34 stream << mode->ToString(); |
| 35 first = false; |
| 36 } |
| 37 return stream.str(); |
| 38 } |
| 39 |
| 40 std::string DisplayConnectionTypeString(ui::DisplayConnectionType type) { |
| 41 switch (type) { |
| 42 case ui::DISPLAY_CONNECTION_TYPE_NONE: |
| 43 return "none"; |
| 44 case ui::DISPLAY_CONNECTION_TYPE_UNKNOWN: |
| 45 return "unknown"; |
| 46 case ui::DISPLAY_CONNECTION_TYPE_INTERNAL: |
| 47 return "internal"; |
| 48 case ui::DISPLAY_CONNECTION_TYPE_VGA: |
| 49 return "vga"; |
| 50 case ui::DISPLAY_CONNECTION_TYPE_HDMI: |
| 51 return "hdmi"; |
| 52 case ui::DISPLAY_CONNECTION_TYPE_DVI: |
| 53 return "dvi"; |
| 54 case ui::DISPLAY_CONNECTION_TYPE_DISPLAYPORT: |
| 55 return "dp"; |
| 56 case ui::DISPLAY_CONNECTION_TYPE_NETWORK: |
| 57 return "network"; |
| 58 case ui::DISPLAY_CONNECTION_TYPE_VIRTUAL: |
| 59 return "virtual"; |
| 60 } |
| 61 NOTREACHED(); |
| 62 return ""; |
| 63 } |
19 | 64 |
20 } // namespace | 65 } // namespace |
21 | 66 |
22 FakeDisplaySnapshot::FakeDisplaySnapshot(int64_t display_id, | 67 using Builder = FakeDisplaySnapshot::Builder; |
23 const gfx::Size& display_size, | 68 |
24 ui::DisplayConnectionType type, | 69 Builder::Builder() {} |
25 std::string name) | 70 |
| 71 Builder::~Builder() {} |
| 72 |
| 73 std::unique_ptr<FakeDisplaySnapshot> Builder::Build() { |
| 74 if (modes_.empty() || id_ == Display::kInvalidDisplayID) { |
| 75 NOTREACHED() << "Display modes or display ID missing"; |
| 76 return nullptr; |
| 77 } |
| 78 |
| 79 // If there is no native mode set, use the first display mode. |
| 80 if (!native_mode_) |
| 81 native_mode_ = modes_.back().get(); |
| 82 |
| 83 // Calculate physical size to match set DPI. |
| 84 gfx::Size physical_size = |
| 85 gfx::ScaleToRoundedSize(native_mode_->size(), PixelPitchMmFromDPI(dpi_)); |
| 86 |
| 87 return base::WrapUnique(new FakeDisplaySnapshot( |
| 88 id_, origin_, physical_size, type_, is_aspect_preserving_scaling_, |
| 89 has_overscan_, has_color_correction_matrix_, name_, product_id_, |
| 90 std::move(modes_), current_mode_, native_mode_)); |
| 91 } |
| 92 |
| 93 Builder& Builder::SetId(int64_t id) { |
| 94 id_ = id; |
| 95 return *this; |
| 96 } |
| 97 |
| 98 Builder& Builder::SetNativeMode(const gfx::Size& size) { |
| 99 native_mode_ = AddOrFindDisplayMode(size); |
| 100 return *this; |
| 101 } |
| 102 |
| 103 Builder& Builder::SetCurrentMode(const gfx::Size& size) { |
| 104 current_mode_ = AddOrFindDisplayMode(size); |
| 105 return *this; |
| 106 } |
| 107 |
| 108 Builder& Builder::AddMode(const gfx::Size& size) { |
| 109 AddOrFindDisplayMode(size); |
| 110 return *this; |
| 111 } |
| 112 |
| 113 Builder& Builder::SetOrigin(const gfx::Point& origin) { |
| 114 origin_ = origin; |
| 115 return *this; |
| 116 } |
| 117 |
| 118 Builder& Builder::SetType(ui::DisplayConnectionType type) { |
| 119 type_ = type; |
| 120 return *this; |
| 121 } |
| 122 |
| 123 Builder& Builder::SetIsAspectPerservingScaling(bool val) { |
| 124 is_aspect_preserving_scaling_ = val; |
| 125 return *this; |
| 126 } |
| 127 |
| 128 Builder& Builder::SetHasOverscan(bool has_overscan) { |
| 129 has_overscan_ = has_overscan; |
| 130 return *this; |
| 131 } |
| 132 |
| 133 Builder& Builder::SetHasColorCorrectionMatrix(bool val) { |
| 134 has_color_correction_matrix_ = val; |
| 135 return *this; |
| 136 } |
| 137 |
| 138 Builder& Builder::SetName(const std::string& name) { |
| 139 name_ = name; |
| 140 return *this; |
| 141 } |
| 142 |
| 143 Builder& Builder::SetProductId(int64_t product_id) { |
| 144 product_id_ = product_id; |
| 145 return *this; |
| 146 } |
| 147 |
| 148 Builder& Builder::SetDPI(int dpi) { |
| 149 dpi_ = static_cast<float>(dpi); |
| 150 return *this; |
| 151 } |
| 152 |
| 153 Builder& Builder::SetLowDPI() { |
| 154 return SetDPI(96); |
| 155 } |
| 156 |
| 157 Builder& Builder::SetHighDPI() { |
| 158 return SetDPI(326); // Retina-ish. |
| 159 } |
| 160 |
| 161 const ui::DisplayMode* Builder::AddOrFindDisplayMode(const gfx::Size& size) { |
| 162 for (auto& mode : modes_) { |
| 163 if (mode->size() == size) |
| 164 return mode.get(); |
| 165 } |
| 166 |
| 167 // Not found, insert a mode with the size and return. |
| 168 modes_.push_back(base::MakeUnique<ui::DisplayMode>(size, false, 60.0f)); |
| 169 return modes_.back().get(); |
| 170 } |
| 171 |
| 172 FakeDisplaySnapshot::FakeDisplaySnapshot( |
| 173 int64_t display_id, |
| 174 const gfx::Point& origin, |
| 175 const gfx::Size& physical_size, |
| 176 ui::DisplayConnectionType type, |
| 177 bool is_aspect_preserving_scaling, |
| 178 bool has_overscan, |
| 179 bool has_color_correction_matrix, |
| 180 std::string display_name, |
| 181 int64_t product_id, |
| 182 std::vector<std::unique_ptr<const ui::DisplayMode>> modes, |
| 183 const ui::DisplayMode* current_mode, |
| 184 const ui::DisplayMode* native_mode) |
26 : DisplaySnapshot(display_id, | 185 : DisplaySnapshot(display_id, |
27 gfx::Point(0, 0), | 186 origin, |
28 gfx::Size(display_size.width() * kDisplayPitchMM, | 187 physical_size, |
29 display_size.height() * kDisplayPitchMM), | |
30 type, | 188 type, |
31 false, | 189 is_aspect_preserving_scaling, |
32 false, | 190 has_overscan, |
33 false, | 191 has_color_correction_matrix, |
34 name, | 192 display_name, |
35 base::FilePath(), | 193 base::FilePath(), |
36 std::vector<std::unique_ptr<const ui::DisplayMode>>(), | 194 std::move(modes), |
37 std::vector<uint8_t>(), | 195 std::vector<uint8_t>(), |
38 nullptr, | 196 current_mode, |
39 nullptr) { | 197 native_mode) { |
40 ui::DisplayMode mode(display_size, false, 60.0f); | 198 product_id_ = product_id; |
41 modes_.push_back(mode.Clone()); | |
42 | |
43 native_mode_ = modes_.front().get(); | |
44 } | 199 } |
45 | 200 |
46 FakeDisplaySnapshot::~FakeDisplaySnapshot() {} | 201 FakeDisplaySnapshot::~FakeDisplaySnapshot() {} |
47 | 202 |
48 std::string FakeDisplaySnapshot::ToString() const { | 203 std::string FakeDisplaySnapshot::ToString() const { |
49 return base::StringPrintf( | 204 return base::StringPrintf( |
50 "id=%" PRId64 " current_mode=%s physical_size=%s", display_id_, | 205 "id=%" PRId64 |
| 206 " current_mode=%s native_mode=%s origin=%s" |
| 207 " physical_size=%s, type=%s name=\"%s\" modes=(%s)", |
| 208 display_id_, |
51 current_mode_ ? current_mode_->ToString().c_str() : "nullptr", | 209 current_mode_ ? current_mode_->ToString().c_str() : "nullptr", |
52 physical_size_.ToString().c_str()); | 210 native_mode_ ? native_mode_->ToString().c_str() : "nullptr", |
| 211 origin_.ToString().c_str(), physical_size_.ToString().c_str(), |
| 212 DisplayConnectionTypeString(type_).c_str(), display_name_.c_str(), |
| 213 ModeListString(modes_).c_str()); |
53 } | 214 } |
54 | 215 |
55 } // namespace display | 216 } // namespace display |
OLD | NEW |