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> | 9 #include <sstream> |
10 #include <utility> | 10 #include <utility> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
14 #include "base/strings/string_split.h" | |
14 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
15 | 16 |
16 namespace display { | 17 namespace display { |
17 | 18 |
19 using base::StringPiece; | |
Daniel Erat
2016/10/05 21:09:45
nit: move this up above "namespace display"?
kylechar
2016/10/07 16:14:43
Done.
| |
20 | |
18 namespace { | 21 namespace { |
19 | 22 |
20 static const float kInchInMm = 25.4f; | 23 static const float kInchInMm = 25.4f; |
21 | 24 |
22 // Get pixel pitch in millimeters from DPI. | 25 // Get pixel pitch in millimeters from DPI. |
23 float PixelPitchMmFromDPI(float dpi) { | 26 float PixelPitchMmFromDPI(float dpi) { |
24 return (1.0f / dpi) * kInchInMm; | 27 return (1.0f / dpi) * kInchInMm; |
25 } | 28 } |
26 | 29 |
27 std::string ModeListString( | 30 std::string ModeListString( |
(...skipping 27 matching lines...) Expand all Loading... | |
55 return "dp"; | 58 return "dp"; |
56 case ui::DISPLAY_CONNECTION_TYPE_NETWORK: | 59 case ui::DISPLAY_CONNECTION_TYPE_NETWORK: |
57 return "network"; | 60 return "network"; |
58 case ui::DISPLAY_CONNECTION_TYPE_VIRTUAL: | 61 case ui::DISPLAY_CONNECTION_TYPE_VIRTUAL: |
59 return "virtual"; | 62 return "virtual"; |
60 } | 63 } |
61 NOTREACHED(); | 64 NOTREACHED(); |
62 return ""; | 65 return ""; |
63 } | 66 } |
64 | 67 |
68 // Extracts text after specified delimiter. If the delimiter doesn't exist then | |
69 // the result will be empty and the input string will be unmodified. Otherwise, | |
70 // the input string will contain the text before the delimiter and the result | |
71 // will be the text after the delimiter. | |
72 StringPiece SplitFromString(StringPiece* str, StringPiece delimiter) { | |
Daniel Erat
2016/10/05 21:09:45
maybe a name like "ExtractSuffix" would be clearer
kylechar
2016/10/07 16:14:43
Done.
| |
73 std::vector<StringPiece> parts = base::SplitStringPiece( | |
74 *str, delimiter, base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY); | |
75 | |
76 if (parts.size() == 2) { | |
77 *str = parts[0]; | |
78 return parts[1]; | |
79 } | |
80 | |
81 return StringPiece(); | |
82 } | |
83 | |
84 // Parses a display mode from a string in the format HxW[%R], returns null if | |
Daniel Erat
2016/10/05 21:09:45
nit: s/returns/returning/
kylechar
2016/10/07 16:14:43
Done.
| |
85 // string is not valid format. | |
Daniel Erat
2016/10/05 21:09:46
nit: s/not valid format/invalid/
kylechar
2016/10/07 16:14:43
Done.
| |
86 std::unique_ptr<ui::DisplayMode> ParseDisplayMode(const std::string& str) { | |
87 int width = 0; | |
88 int height = 0; | |
89 float refresh_rate = 60.0f; | |
90 | |
91 // Refresh rate is optional, will be be 60 if not specified. | |
Daniel Erat
2016/10/05 21:09:46
nit: s/, will/ and will/
kylechar
2016/10/07 16:14:43
Done.
| |
92 if (sscanf(str.c_str(), "%dx%d%%%f", &width, &height, &refresh_rate) < 2) { | |
93 LOG(ERROR) << "Unable to parse display mode \"" << str << "\""; | |
94 return nullptr; | |
95 } | |
96 | |
97 return base::MakeUnique<ui::DisplayMode>(gfx::Size(width, height), false, | |
98 refresh_rate); | |
99 } | |
100 | |
101 // Parses list of alternate display modes, adding each new display mode to the | |
Daniel Erat
2016/10/05 21:09:46
nit: s/list/a list/
kylechar
2016/10/07 16:14:43
Done.
| |
102 // display snapshot. | |
103 void HandleModes(FakeDisplaySnapshot::Builder* builder, | |
104 StringPiece resolutions) { | |
105 if (resolutions.empty()) | |
Daniel Erat
2016/10/05 21:09:46
nit: no need for this early return, right? i'd ass
kylechar
2016/10/07 16:14:43
Done.
| |
106 return; | |
107 | |
108 for (const std::string& mode_str : | |
109 base::SplitString(resolutions, ":", base::TRIM_WHITESPACE, | |
110 base::SPLIT_WANT_NONEMPTY)) { | |
111 std::unique_ptr<ui::DisplayMode> mode = ParseDisplayMode(mode_str); | |
112 if (mode) | |
113 builder->AddMode(std::move(mode)); | |
114 } | |
115 } | |
116 | |
117 // Parses device DPI and set on display snapshot. | |
Daniel Erat
2016/10/05 21:09:46
nit: s/set on display snapshot/updates |builder|/
kylechar
2016/10/07 16:14:43
Done.
| |
118 void HandleDPI(FakeDisplaySnapshot::Builder* builder, StringPiece dpi) { | |
119 if (dpi.empty()) | |
Daniel Erat
2016/10/05 21:09:46
this doesn't seem necessary either
kylechar
2016/10/07 16:14:43
This has changed a bit, PTAL.
| |
120 return; | |
121 | |
122 int dpi_value = 0; | |
123 if (sscanf(dpi.as_string().c_str(), "%d", &dpi_value) == 1) | |
124 builder->SetDPI(dpi_value); | |
125 } | |
126 | |
127 // Parses list of display options and set each option true on display snapshot. | |
128 // If an option appears more than once it will have no effect the second time. | |
129 void HandleOptions(FakeDisplaySnapshot::Builder* builder, StringPiece options) { | |
130 if (options.empty()) | |
Daniel Erat
2016/10/05 21:09:46
nor this; the for loop should just be a no-op if t
kylechar
2016/10/07 16:14:43
Done.
| |
131 return; | |
132 | |
133 for (size_t i = 0; i < options.size(); ++i) { | |
134 switch (options[i]) { | |
135 case 'o': | |
136 builder->SetHasOverscan(true); | |
137 break; | |
138 case 'c': | |
139 builder->SetHasColorCorrectionMatrix(true); | |
140 break; | |
141 case 'a': | |
142 builder->SetIsAspectPerservingScaling(true); | |
143 break; | |
144 case 'i': | |
145 builder->SetType(ui::DISPLAY_CONNECTION_TYPE_INTERNAL); | |
146 break; | |
147 default: | |
148 LOG(ERROR) << "Invalid option specifier \"" << options[i] << "\""; | |
149 } | |
150 } | |
151 } | |
152 | |
65 } // namespace | 153 } // namespace |
66 | 154 |
67 using Builder = FakeDisplaySnapshot::Builder; | 155 using Builder = FakeDisplaySnapshot::Builder; |
68 | 156 |
69 Builder::Builder() {} | 157 Builder::Builder() {} |
70 | 158 |
71 Builder::~Builder() {} | 159 Builder::~Builder() {} |
72 | 160 |
73 std::unique_ptr<FakeDisplaySnapshot> Builder::Build() { | 161 std::unique_ptr<FakeDisplaySnapshot> Builder::Build() { |
74 if (modes_.empty() || id_ == Display::kInvalidDisplayID) { | 162 if (modes_.empty() || id_ == Display::kInvalidDisplayID) { |
(...skipping 18 matching lines...) Expand all Loading... | |
93 Builder& Builder::SetId(int64_t id) { | 181 Builder& Builder::SetId(int64_t id) { |
94 id_ = id; | 182 id_ = id; |
95 return *this; | 183 return *this; |
96 } | 184 } |
97 | 185 |
98 Builder& Builder::SetNativeMode(const gfx::Size& size) { | 186 Builder& Builder::SetNativeMode(const gfx::Size& size) { |
99 native_mode_ = AddOrFindDisplayMode(size); | 187 native_mode_ = AddOrFindDisplayMode(size); |
100 return *this; | 188 return *this; |
101 } | 189 } |
102 | 190 |
191 Builder& Builder::SetNativeMode(std::unique_ptr<ui::DisplayMode> mode) { | |
192 native_mode_ = AddOrFindDisplayMode(std::move(mode)); | |
193 return *this; | |
194 } | |
195 | |
103 Builder& Builder::SetCurrentMode(const gfx::Size& size) { | 196 Builder& Builder::SetCurrentMode(const gfx::Size& size) { |
104 current_mode_ = AddOrFindDisplayMode(size); | 197 current_mode_ = AddOrFindDisplayMode(size); |
105 return *this; | 198 return *this; |
106 } | 199 } |
107 | 200 |
201 Builder& Builder::SetCurrentMode(std::unique_ptr<ui::DisplayMode> mode) { | |
202 current_mode_ = AddOrFindDisplayMode(std::move(mode)); | |
203 return *this; | |
204 } | |
205 | |
108 Builder& Builder::AddMode(const gfx::Size& size) { | 206 Builder& Builder::AddMode(const gfx::Size& size) { |
109 AddOrFindDisplayMode(size); | 207 AddOrFindDisplayMode(size); |
110 return *this; | 208 return *this; |
111 } | 209 } |
112 | 210 |
211 Builder& Builder::AddMode(std::unique_ptr<ui::DisplayMode> mode) { | |
212 AddOrFindDisplayMode(std::move(mode)); | |
213 return *this; | |
214 } | |
215 | |
113 Builder& Builder::SetOrigin(const gfx::Point& origin) { | 216 Builder& Builder::SetOrigin(const gfx::Point& origin) { |
114 origin_ = origin; | 217 origin_ = origin; |
115 return *this; | 218 return *this; |
116 } | 219 } |
117 | 220 |
118 Builder& Builder::SetType(ui::DisplayConnectionType type) { | 221 Builder& Builder::SetType(ui::DisplayConnectionType type) { |
119 type_ = type; | 222 type_ = type; |
120 return *this; | 223 return *this; |
121 } | 224 } |
122 | 225 |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
162 for (auto& mode : modes_) { | 265 for (auto& mode : modes_) { |
163 if (mode->size() == size) | 266 if (mode->size() == size) |
164 return mode.get(); | 267 return mode.get(); |
165 } | 268 } |
166 | 269 |
167 // Not found, insert a mode with the size and return. | 270 // Not found, insert a mode with the size and return. |
168 modes_.push_back(base::MakeUnique<ui::DisplayMode>(size, false, 60.0f)); | 271 modes_.push_back(base::MakeUnique<ui::DisplayMode>(size, false, 60.0f)); |
169 return modes_.back().get(); | 272 return modes_.back().get(); |
170 } | 273 } |
171 | 274 |
172 FakeDisplaySnapshot::FakeDisplaySnapshot( | 275 const ui::DisplayMode* Builder::AddOrFindDisplayMode( |
173 int64_t display_id, | 276 std::unique_ptr<ui::DisplayMode> mode) { |
174 const gfx::Point& origin, | 277 for (auto& existing : modes_) { |
175 const gfx::Size& physical_size, | 278 if (mode->size() == existing->size() && |
176 ui::DisplayConnectionType type, | 279 mode->is_interlaced() == existing->is_interlaced() && |
177 bool is_aspect_preserving_scaling, | 280 mode->refresh_rate() == existing->refresh_rate()) |
178 bool has_overscan, | 281 return existing.get(); |
179 bool has_color_correction_matrix, | 282 } |
180 std::string display_name, | 283 |
181 int64_t product_id, | 284 // Not found, insert mode and return. |
182 std::vector<std::unique_ptr<const ui::DisplayMode>> modes, | 285 modes_.push_back(std::move(mode)); |
183 const ui::DisplayMode* current_mode, | 286 return modes_.back().get(); |
184 const ui::DisplayMode* native_mode) | 287 } |
288 | |
289 FakeDisplaySnapshot::FakeDisplaySnapshot(int64_t display_id, | |
290 const gfx::Point& origin, | |
291 const gfx::Size& physical_size, | |
292 ui::DisplayConnectionType type, | |
293 bool is_aspect_preserving_scaling, | |
294 bool has_overscan, | |
295 bool has_color_correction_matrix, | |
296 std::string display_name, | |
297 int64_t product_id, | |
298 DisplayModeList modes, | |
299 const ui::DisplayMode* current_mode, | |
300 const ui::DisplayMode* native_mode) | |
185 : DisplaySnapshot(display_id, | 301 : DisplaySnapshot(display_id, |
186 origin, | 302 origin, |
187 physical_size, | 303 physical_size, |
188 type, | 304 type, |
189 is_aspect_preserving_scaling, | 305 is_aspect_preserving_scaling, |
190 has_overscan, | 306 has_overscan, |
191 has_color_correction_matrix, | 307 has_color_correction_matrix, |
192 display_name, | 308 display_name, |
193 base::FilePath(), | 309 base::FilePath(), |
194 std::move(modes), | 310 std::move(modes), |
195 std::vector<uint8_t>(), | 311 std::vector<uint8_t>(), |
196 current_mode, | 312 current_mode, |
197 native_mode) { | 313 native_mode) { |
198 product_id_ = product_id; | 314 product_id_ = product_id; |
199 } | 315 } |
200 | 316 |
201 FakeDisplaySnapshot::~FakeDisplaySnapshot() {} | 317 FakeDisplaySnapshot::~FakeDisplaySnapshot() {} |
202 | 318 |
319 // static | |
320 std::unique_ptr<ui::DisplaySnapshot> FakeDisplaySnapshot::CreateFromSpec( | |
321 int64_t id, | |
322 const std::string& spec) { | |
323 StringPiece leftover(spec); | |
324 | |
325 // Cut off end of string at each delimiter to split. | |
326 StringPiece options = SplitFromString(&leftover, "/"); | |
327 StringPiece dpi = SplitFromString(&leftover, "^"); | |
328 StringPiece resolutions = SplitFromString(&leftover, "#"); | |
329 | |
330 // Leftovers should be just the native mode at this point. | |
331 std::unique_ptr<ui::DisplayMode> native_mode = | |
332 ParseDisplayMode(leftover.as_string()); | |
333 | |
334 // Fail without valid native mode. | |
335 if (!native_mode) { | |
336 LOG(ERROR) << "Failed to make display for \"" << spec << "\""; | |
337 return nullptr; | |
338 } | |
339 | |
340 FakeDisplaySnapshot::Builder builder; | |
341 builder.SetId(id).SetNativeMode(std::move(native_mode)); | |
342 builder.SetName(base::StringPrintf("Fake Display %" PRId64, id)); | |
343 | |
344 HandleModes(&builder, resolutions); | |
Daniel Erat
2016/10/05 21:09:46
it's fine if you leave it like this, but i wouldn'
kylechar
2016/10/07 16:14:43
I've modified all of this a bit and added return v
| |
345 HandleDPI(&builder, dpi); | |
346 HandleOptions(&builder, options); | |
347 | |
348 return builder.Build(); | |
349 } | |
350 | |
203 std::string FakeDisplaySnapshot::ToString() const { | 351 std::string FakeDisplaySnapshot::ToString() const { |
204 return base::StringPrintf( | 352 return base::StringPrintf( |
205 "id=%" PRId64 | 353 "id=%" PRId64 |
206 " current_mode=%s native_mode=%s origin=%s" | 354 " current_mode=%s native_mode=%s origin=%s" |
207 " physical_size=%s, type=%s name=\"%s\" modes=(%s)", | 355 " physical_size=%s, type=%s name=\"%s\" modes=(%s)", |
208 display_id_, | 356 display_id_, |
209 current_mode_ ? current_mode_->ToString().c_str() : "nullptr", | 357 current_mode_ ? current_mode_->ToString().c_str() : "nullptr", |
210 native_mode_ ? native_mode_->ToString().c_str() : "nullptr", | 358 native_mode_ ? native_mode_->ToString().c_str() : "nullptr", |
211 origin_.ToString().c_str(), physical_size_.ToString().c_str(), | 359 origin_.ToString().c_str(), physical_size_.ToString().c_str(), |
212 DisplayConnectionTypeString(type_).c_str(), display_name_.c_str(), | 360 DisplayConnectionTypeString(type_).c_str(), display_name_.c_str(), |
213 ModeListString(modes_).c_str()); | 361 ModeListString(modes_).c_str()); |
214 } | 362 } |
215 | 363 |
216 } // namespace display | 364 } // namespace display |
OLD | NEW |