Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(263)

Side by Side Diff: ui/display/fake_display_snapshot.cc

Issue 2613493002: Fix namespace for src/ui/display/. (Closed)
Patch Set: Rebase. Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ui/display/fake_display_snapshot.h ('k') | ui/display/fake_display_snapshot_unittests.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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>
(...skipping 12 matching lines...) Expand all
23 namespace { 23 namespace {
24 24
25 static const float kInchInMm = 25.4f; 25 static const float kInchInMm = 25.4f;
26 26
27 // Get pixel pitch in millimeters from DPI. 27 // Get pixel pitch in millimeters from DPI.
28 float PixelPitchMmFromDPI(float dpi) { 28 float PixelPitchMmFromDPI(float dpi) {
29 return (1.0f / dpi) * kInchInMm; 29 return (1.0f / dpi) * kInchInMm;
30 } 30 }
31 31
32 std::string ModeListString( 32 std::string ModeListString(
33 const std::vector<std::unique_ptr<const ui::DisplayMode>>& modes) { 33 const std::vector<std::unique_ptr<const DisplayMode>>& modes) {
34 std::stringstream stream; 34 std::stringstream stream;
35 bool first = true; 35 bool first = true;
36 for (auto& mode : modes) { 36 for (auto& mode : modes) {
37 if (!first) 37 if (!first)
38 stream << ", "; 38 stream << ", ";
39 stream << mode->ToString(); 39 stream << mode->ToString();
40 first = false; 40 first = false;
41 } 41 }
42 return stream.str(); 42 return stream.str();
43 } 43 }
44 44
45 std::string DisplayConnectionTypeString(ui::DisplayConnectionType type) { 45 std::string DisplayConnectionTypeString(DisplayConnectionType type) {
46 switch (type) { 46 switch (type) {
47 case ui::DISPLAY_CONNECTION_TYPE_NONE: 47 case DISPLAY_CONNECTION_TYPE_NONE:
48 return "none"; 48 return "none";
49 case ui::DISPLAY_CONNECTION_TYPE_UNKNOWN: 49 case DISPLAY_CONNECTION_TYPE_UNKNOWN:
50 return "unknown"; 50 return "unknown";
51 case ui::DISPLAY_CONNECTION_TYPE_INTERNAL: 51 case DISPLAY_CONNECTION_TYPE_INTERNAL:
52 return "internal"; 52 return "internal";
53 case ui::DISPLAY_CONNECTION_TYPE_VGA: 53 case DISPLAY_CONNECTION_TYPE_VGA:
54 return "vga"; 54 return "vga";
55 case ui::DISPLAY_CONNECTION_TYPE_HDMI: 55 case DISPLAY_CONNECTION_TYPE_HDMI:
56 return "hdmi"; 56 return "hdmi";
57 case ui::DISPLAY_CONNECTION_TYPE_DVI: 57 case DISPLAY_CONNECTION_TYPE_DVI:
58 return "dvi"; 58 return "dvi";
59 case ui::DISPLAY_CONNECTION_TYPE_DISPLAYPORT: 59 case DISPLAY_CONNECTION_TYPE_DISPLAYPORT:
60 return "dp"; 60 return "dp";
61 case ui::DISPLAY_CONNECTION_TYPE_NETWORK: 61 case DISPLAY_CONNECTION_TYPE_NETWORK:
62 return "network"; 62 return "network";
63 case ui::DISPLAY_CONNECTION_TYPE_VIRTUAL: 63 case DISPLAY_CONNECTION_TYPE_VIRTUAL:
64 return "virtual"; 64 return "virtual";
65 } 65 }
66 NOTREACHED(); 66 NOTREACHED();
67 return ""; 67 return "";
68 } 68 }
69 69
70 // Extracts text after specified delimiter. If the delimiter doesn't appear 70 // Extracts text after specified delimiter. If the delimiter doesn't appear
71 // exactly once the result will be empty and the input string will be 71 // exactly once the result will be empty and the input string will be
72 // unmodified. Otherwise, the input string will contain the text before the 72 // unmodified. Otherwise, the input string will contain the text before the
73 // delimiter and the result will be the text after the delimiter. 73 // delimiter and the result will be the text after the delimiter.
74 StringPiece ExtractSuffix(StringPiece* str, StringPiece delimiter) { 74 StringPiece ExtractSuffix(StringPiece* str, StringPiece delimiter) {
75 std::vector<StringPiece> parts = base::SplitStringPiece( 75 std::vector<StringPiece> parts = base::SplitStringPiece(
76 *str, delimiter, base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY); 76 *str, delimiter, base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
77 77
78 if (parts.size() == 2) { 78 if (parts.size() == 2) {
79 *str = parts[0]; 79 *str = parts[0];
80 return parts[1]; 80 return parts[1];
81 } 81 }
82 82
83 return StringPiece(); 83 return StringPiece();
84 } 84 }
85 85
86 // Parses a display mode from |str| in the format HxW[%R], returning null if 86 // Parses a display mode from |str| in the format HxW[%R], returning null if
87 // |str| is invalid. 87 // |str| is invalid.
88 std::unique_ptr<ui::DisplayMode> ParseDisplayMode(const std::string& str) { 88 std::unique_ptr<DisplayMode> ParseDisplayMode(const std::string& str) {
89 int width = 0; 89 int width = 0;
90 int height = 0; 90 int height = 0;
91 std::string refresh_rate_str; 91 std::string refresh_rate_str;
92 92
93 // Check against regex and extract values. 93 // Check against regex and extract values.
94 if (!RE2::FullMatch(str, "(\\d+)x(\\d+)(?:%(\\d+\\.?\\d*))?", &width, &height, 94 if (!RE2::FullMatch(str, "(\\d+)x(\\d+)(?:%(\\d+\\.?\\d*))?", &width, &height,
95 &refresh_rate_str)) { 95 &refresh_rate_str)) {
96 LOG(ERROR) << "Invalid display mode string \"" << str << "\""; 96 LOG(ERROR) << "Invalid display mode string \"" << str << "\"";
97 return nullptr; 97 return nullptr;
98 } 98 }
99 99
100 if (width <= 0 || height <= 0) { 100 if (width <= 0 || height <= 0) {
101 LOG(ERROR) << "Resolution " << width << "x" << height << " is invalid"; 101 LOG(ERROR) << "Resolution " << width << "x" << height << " is invalid";
102 return nullptr; 102 return nullptr;
103 } 103 }
104 104
105 // Refresh rate is optional and will be be 60 if not specified. 105 // Refresh rate is optional and will be be 60 if not specified.
106 double refresh_rate = 60.0f; 106 double refresh_rate = 60.0f;
107 if (!refresh_rate_str.empty() && 107 if (!refresh_rate_str.empty() &&
108 !base::StringToDouble(refresh_rate_str, &refresh_rate)) { 108 !base::StringToDouble(refresh_rate_str, &refresh_rate)) {
109 LOG(ERROR) << "Unable to parse display mode \"" << str << "\""; 109 LOG(ERROR) << "Unable to parse display mode \"" << str << "\"";
110 return nullptr; 110 return nullptr;
111 } 111 }
112 112
113 return base::MakeUnique<ui::DisplayMode>(gfx::Size(width, height), false, 113 return base::MakeUnique<DisplayMode>(gfx::Size(width, height), false,
114 static_cast<float>(refresh_rate)); 114 static_cast<float>(refresh_rate));
115 } 115 }
116 116
117 // Parses a list of alternate display modes, adding each new display mode to 117 // Parses a list of alternate display modes, adding each new display mode to
118 // |builder|. Returns false if any of the modes are invalid. 118 // |builder|. Returns false if any of the modes are invalid.
119 bool HandleModes(FakeDisplaySnapshot::Builder* builder, 119 bool HandleModes(FakeDisplaySnapshot::Builder* builder,
120 StringPiece resolutions) { 120 StringPiece resolutions) {
121 for (const std::string& mode_str : 121 for (const std::string& mode_str :
122 base::SplitString(resolutions, ":", base::TRIM_WHITESPACE, 122 base::SplitString(resolutions, ":", base::TRIM_WHITESPACE,
123 base::SPLIT_WANT_NONEMPTY)) { 123 base::SPLIT_WANT_NONEMPTY)) {
124 std::unique_ptr<ui::DisplayMode> mode = ParseDisplayMode(mode_str); 124 std::unique_ptr<DisplayMode> mode = ParseDisplayMode(mode_str);
125 if (!mode) 125 if (!mode)
126 return false; 126 return false;
127 127
128 builder->AddMode(std::move(mode)); 128 builder->AddMode(std::move(mode));
129 } 129 }
130 130
131 return true; 131 return true;
132 } 132 }
133 133
134 // Parses device DPI and updates |builder|. Returns false if an invalid DPI 134 // Parses device DPI and updates |builder|. Returns false if an invalid DPI
(...skipping 21 matching lines...) Expand all
156 case 'o': 156 case 'o':
157 builder->SetHasOverscan(true); 157 builder->SetHasOverscan(true);
158 break; 158 break;
159 case 'c': 159 case 'c':
160 builder->SetHasColorCorrectionMatrix(true); 160 builder->SetHasColorCorrectionMatrix(true);
161 break; 161 break;
162 case 'a': 162 case 'a':
163 builder->SetIsAspectPerservingScaling(true); 163 builder->SetIsAspectPerservingScaling(true);
164 break; 164 break;
165 case 'i': 165 case 'i':
166 builder->SetType(ui::DISPLAY_CONNECTION_TYPE_INTERNAL); 166 builder->SetType(DISPLAY_CONNECTION_TYPE_INTERNAL);
167 break; 167 break;
168 default: 168 default:
169 LOG(ERROR) << "Invalid option specifier \"" << options[i] << "\""; 169 LOG(ERROR) << "Invalid option specifier \"" << options[i] << "\"";
170 return false; 170 return false;
171 } 171 }
172 } 172 }
173 173
174 return true; 174 return true;
175 } 175 }
176 176
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
209 Builder& Builder::SetId(int64_t id) { 209 Builder& Builder::SetId(int64_t id) {
210 id_ = id; 210 id_ = id;
211 return *this; 211 return *this;
212 } 212 }
213 213
214 Builder& Builder::SetNativeMode(const gfx::Size& size) { 214 Builder& Builder::SetNativeMode(const gfx::Size& size) {
215 native_mode_ = AddOrFindDisplayMode(size); 215 native_mode_ = AddOrFindDisplayMode(size);
216 return *this; 216 return *this;
217 } 217 }
218 218
219 Builder& Builder::SetNativeMode(std::unique_ptr<ui::DisplayMode> mode) { 219 Builder& Builder::SetNativeMode(std::unique_ptr<DisplayMode> mode) {
220 native_mode_ = AddOrFindDisplayMode(std::move(mode)); 220 native_mode_ = AddOrFindDisplayMode(std::move(mode));
221 return *this; 221 return *this;
222 } 222 }
223 223
224 Builder& Builder::SetCurrentMode(const gfx::Size& size) { 224 Builder& Builder::SetCurrentMode(const gfx::Size& size) {
225 current_mode_ = AddOrFindDisplayMode(size); 225 current_mode_ = AddOrFindDisplayMode(size);
226 return *this; 226 return *this;
227 } 227 }
228 228
229 Builder& Builder::SetCurrentMode(std::unique_ptr<ui::DisplayMode> mode) { 229 Builder& Builder::SetCurrentMode(std::unique_ptr<DisplayMode> mode) {
230 current_mode_ = AddOrFindDisplayMode(std::move(mode)); 230 current_mode_ = AddOrFindDisplayMode(std::move(mode));
231 return *this; 231 return *this;
232 } 232 }
233 233
234 Builder& Builder::AddMode(const gfx::Size& size) { 234 Builder& Builder::AddMode(const gfx::Size& size) {
235 AddOrFindDisplayMode(size); 235 AddOrFindDisplayMode(size);
236 return *this; 236 return *this;
237 } 237 }
238 238
239 Builder& Builder::AddMode(std::unique_ptr<ui::DisplayMode> mode) { 239 Builder& Builder::AddMode(std::unique_ptr<DisplayMode> mode) {
240 AddOrFindDisplayMode(std::move(mode)); 240 AddOrFindDisplayMode(std::move(mode));
241 return *this; 241 return *this;
242 } 242 }
243 243
244 Builder& Builder::SetOrigin(const gfx::Point& origin) { 244 Builder& Builder::SetOrigin(const gfx::Point& origin) {
245 origin_ = origin; 245 origin_ = origin;
246 return *this; 246 return *this;
247 } 247 }
248 248
249 Builder& Builder::SetType(ui::DisplayConnectionType type) { 249 Builder& Builder::SetType(DisplayConnectionType type) {
250 type_ = type; 250 type_ = type;
251 return *this; 251 return *this;
252 } 252 }
253 253
254 Builder& Builder::SetIsAspectPerservingScaling(bool val) { 254 Builder& Builder::SetIsAspectPerservingScaling(bool val) {
255 is_aspect_preserving_scaling_ = val; 255 is_aspect_preserving_scaling_ = val;
256 return *this; 256 return *this;
257 } 257 }
258 258
259 Builder& Builder::SetHasOverscan(bool has_overscan) { 259 Builder& Builder::SetHasOverscan(bool has_overscan) {
(...skipping 22 matching lines...) Expand all
282 } 282 }
283 283
284 Builder& Builder::SetLowDPI() { 284 Builder& Builder::SetLowDPI() {
285 return SetDPI(96); 285 return SetDPI(96);
286 } 286 }
287 287
288 Builder& Builder::SetHighDPI() { 288 Builder& Builder::SetHighDPI() {
289 return SetDPI(326); // Retina-ish. 289 return SetDPI(326); // Retina-ish.
290 } 290 }
291 291
292 const ui::DisplayMode* Builder::AddOrFindDisplayMode(const gfx::Size& size) { 292 const DisplayMode* Builder::AddOrFindDisplayMode(const gfx::Size& size) {
293 for (auto& mode : modes_) { 293 for (auto& mode : modes_) {
294 if (mode->size() == size) 294 if (mode->size() == size)
295 return mode.get(); 295 return mode.get();
296 } 296 }
297 297
298 // Not found, insert a mode with the size and return. 298 // Not found, insert a mode with the size and return.
299 modes_.push_back(base::MakeUnique<ui::DisplayMode>(size, false, 60.0f)); 299 modes_.push_back(base::MakeUnique<DisplayMode>(size, false, 60.0f));
300 return modes_.back().get(); 300 return modes_.back().get();
301 } 301 }
302 302
303 const ui::DisplayMode* Builder::AddOrFindDisplayMode( 303 const DisplayMode* Builder::AddOrFindDisplayMode(
304 std::unique_ptr<ui::DisplayMode> mode) { 304 std::unique_ptr<DisplayMode> mode) {
305 for (auto& existing : modes_) { 305 for (auto& existing : modes_) {
306 if (mode->size() == existing->size() && 306 if (mode->size() == existing->size() &&
307 mode->is_interlaced() == existing->is_interlaced() && 307 mode->is_interlaced() == existing->is_interlaced() &&
308 mode->refresh_rate() == existing->refresh_rate()) 308 mode->refresh_rate() == existing->refresh_rate())
309 return existing.get(); 309 return existing.get();
310 } 310 }
311 311
312 // Not found, insert mode and return. 312 // Not found, insert mode and return.
313 modes_.push_back(std::move(mode)); 313 modes_.push_back(std::move(mode));
314 return modes_.back().get(); 314 return modes_.back().get();
315 } 315 }
316 316
317 FakeDisplaySnapshot::FakeDisplaySnapshot(int64_t display_id, 317 FakeDisplaySnapshot::FakeDisplaySnapshot(int64_t display_id,
318 const gfx::Point& origin, 318 const gfx::Point& origin,
319 const gfx::Size& physical_size, 319 const gfx::Size& physical_size,
320 ui::DisplayConnectionType type, 320 DisplayConnectionType type,
321 bool is_aspect_preserving_scaling, 321 bool is_aspect_preserving_scaling,
322 bool has_overscan, 322 bool has_overscan,
323 bool has_color_correction_matrix, 323 bool has_color_correction_matrix,
324 std::string display_name, 324 std::string display_name,
325 int64_t product_id, 325 int64_t product_id,
326 DisplayModeList modes, 326 DisplayModeList modes,
327 const ui::DisplayMode* current_mode, 327 const DisplayMode* current_mode,
328 const ui::DisplayMode* native_mode) 328 const DisplayMode* native_mode)
329 : DisplaySnapshot(display_id, 329 : DisplaySnapshot(display_id,
330 origin, 330 origin,
331 physical_size, 331 physical_size,
332 type, 332 type,
333 is_aspect_preserving_scaling, 333 is_aspect_preserving_scaling,
334 has_overscan, 334 has_overscan,
335 has_color_correction_matrix, 335 has_color_correction_matrix,
336 display_name, 336 display_name,
337 base::FilePath(), 337 base::FilePath(),
338 std::move(modes), 338 std::move(modes),
339 std::vector<uint8_t>(), 339 std::vector<uint8_t>(),
340 current_mode, 340 current_mode,
341 native_mode) { 341 native_mode) {
342 product_id_ = product_id; 342 product_id_ = product_id;
343 } 343 }
344 344
345 FakeDisplaySnapshot::~FakeDisplaySnapshot() {} 345 FakeDisplaySnapshot::~FakeDisplaySnapshot() {}
346 346
347 // static 347 // static
348 std::unique_ptr<ui::DisplaySnapshot> FakeDisplaySnapshot::CreateFromSpec( 348 std::unique_ptr<DisplaySnapshot> FakeDisplaySnapshot::CreateFromSpec(
349 int64_t id, 349 int64_t id,
350 const std::string& spec) { 350 const std::string& spec) {
351 StringPiece leftover(spec); 351 StringPiece leftover(spec);
352 352
353 // Cut off end of string at each delimiter to split. 353 // Cut off end of string at each delimiter to split.
354 StringPiece options = ExtractSuffix(&leftover, "/"); 354 StringPiece options = ExtractSuffix(&leftover, "/");
355 StringPiece dpi = ExtractSuffix(&leftover, "^"); 355 StringPiece dpi = ExtractSuffix(&leftover, "^");
356 StringPiece resolutions = ExtractSuffix(&leftover, "#"); 356 StringPiece resolutions = ExtractSuffix(&leftover, "#");
357 357
358 // Leftovers should be just the native mode at this point. 358 // Leftovers should be just the native mode at this point.
359 std::unique_ptr<ui::DisplayMode> native_mode = 359 std::unique_ptr<DisplayMode> native_mode =
360 ParseDisplayMode(leftover.as_string()); 360 ParseDisplayMode(leftover.as_string());
361 361
362 // Fail without valid native mode. 362 // Fail without valid native mode.
363 if (!native_mode) 363 if (!native_mode)
364 return nullptr; 364 return nullptr;
365 365
366 FakeDisplaySnapshot::Builder builder; 366 FakeDisplaySnapshot::Builder builder;
367 builder.SetId(id).SetNativeMode(std::move(native_mode)); 367 builder.SetId(id).SetNativeMode(std::move(native_mode));
368 368
369 if (!HandleModes(&builder, resolutions) || !HandleDPI(&builder, dpi) || 369 if (!HandleModes(&builder, resolutions) || !HandleDPI(&builder, dpi) ||
(...skipping 10 matching lines...) Expand all
380 " physical_size=%s, type=%s name=\"%s\" modes=(%s)", 380 " physical_size=%s, type=%s name=\"%s\" modes=(%s)",
381 display_id_, 381 display_id_,
382 current_mode_ ? current_mode_->ToString().c_str() : "nullptr", 382 current_mode_ ? current_mode_->ToString().c_str() : "nullptr",
383 native_mode_ ? native_mode_->ToString().c_str() : "nullptr", 383 native_mode_ ? native_mode_->ToString().c_str() : "nullptr",
384 origin_.ToString().c_str(), physical_size_.ToString().c_str(), 384 origin_.ToString().c_str(), physical_size_.ToString().c_str(),
385 DisplayConnectionTypeString(type_).c_str(), display_name_.c_str(), 385 DisplayConnectionTypeString(type_).c_str(), display_name_.c_str(),
386 ModeListString(modes_).c_str()); 386 ModeListString(modes_).c_str());
387 } 387 }
388 388
389 } // namespace display 389 } // namespace display
OLDNEW
« no previous file with comments | « ui/display/fake_display_snapshot.h ('k') | ui/display/fake_display_snapshot_unittests.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698