Chromium Code Reviews| Index: ui/display/fake_display_snapshot.cc |
| diff --git a/ui/display/fake_display_snapshot.cc b/ui/display/fake_display_snapshot.cc |
| index 4d2fbf5aeaac3e539ce874767c0d3625475dee2b..75d9aa98e9396837483582b261119a95ec0e84e0 100644 |
| --- a/ui/display/fake_display_snapshot.cc |
| +++ b/ui/display/fake_display_snapshot.cc |
| @@ -6,50 +6,211 @@ |
| #include <inttypes.h> |
| +#include <sstream> |
| +#include <utility> |
| #include <vector> |
| +#include "base/memory/ptr_util.h" |
| #include "base/strings/stringprintf.h" |
| namespace display { |
| namespace { |
| -// For a non high-DPI display (96 dpi) use a pitch of 265µm. |
| -static const float kDisplayPitchMM = 0.265f; |
| +static const float kInchInMm = 25.4f; |
| + |
| +// Get pixel pitch in millimeters from DPI. |
| +float PixelPitchMmFromDPI(float dpi) { |
| + return (1.0f / dpi) * kInchInMm; |
| +} |
| + |
| +std::string ModeListString( |
| + const std::vector<std::unique_ptr<const ui::DisplayMode>>& modes) { |
| + std::stringstream stream; |
| + bool first = true; |
| + for (auto& mode : modes) { |
| + if (!first) |
| + stream << ", "; |
| + stream << mode->ToString(); |
| + first = false; |
| + } |
| + return stream.str(); |
| +} |
| + |
| +std::string DisplayConnectionTypeString(ui::DisplayConnectionType type) { |
| + switch (type) { |
| + case ui::DISPLAY_CONNECTION_TYPE_NONE: |
| + return "none"; |
| + case ui::DISPLAY_CONNECTION_TYPE_UNKNOWN: |
| + return "unknown"; |
| + case ui::DISPLAY_CONNECTION_TYPE_INTERNAL: |
| + return "internal"; |
| + case ui::DISPLAY_CONNECTION_TYPE_VGA: |
| + return "vga"; |
| + case ui::DISPLAY_CONNECTION_TYPE_HDMI: |
| + return "hdmi"; |
| + case ui::DISPLAY_CONNECTION_TYPE_DVI: |
| + return "dvi"; |
| + case ui::DISPLAY_CONNECTION_TYPE_DISPLAYPORT: |
| + return "dp"; |
| + case ui::DISPLAY_CONNECTION_TYPE_NETWORK: |
| + return "network"; |
| + case ui::DISPLAY_CONNECTION_TYPE_VIRTUAL: |
| + return "virtual"; |
| + } |
| + NOTREACHED(); |
| + return ""; |
| +} |
| } // namespace |
| -FakeDisplaySnapshot::FakeDisplaySnapshot(int64_t display_id, |
| - const gfx::Size& display_size, |
| - ui::DisplayConnectionType type, |
| - std::string name) |
| +using Builder = FakeDisplaySnapshot::Builder; |
| + |
| +FakeDisplaySnapshot::Builder::Builder() {} |
| + |
| +FakeDisplaySnapshot::Builder::~Builder() {} |
|
Daniel Erat
2016/09/20 17:12:49
nit: drop FakeDisplaySnapshot:: from this line and
kylechar
2016/09/20 17:47:40
Oh ya, that's not needed with the using. Done.
|
| + |
| +std::unique_ptr<FakeDisplaySnapshot> FakeDisplaySnapshot::Builder::Build() { |
|
Daniel Erat
2016/09/20 17:12:49
same here
kylechar
2016/09/20 17:47:40
Done.
|
| + if (modes_.empty() || id_ == Display::kInvalidDisplayID) { |
| + NOTREACHED(); |
|
Daniel Erat
2016/09/20 17:12:49
nit: include a message here too? << "Modes or disp
kylechar
2016/09/20 17:47:40
Done.
|
| + return nullptr; |
| + } |
| + |
| + // If there is no native mode set, use the first display mode. |
| + if (!native_mode_) |
| + native_mode_ = modes_.back().get(); |
| + |
| + // Calculate physical size to match set DPI. |
| + gfx::Size physical_size = |
| + gfx::ScaleToRoundedSize(native_mode_->size(), PixelPitchMmFromDPI(dpi_)); |
| + |
| + return base::WrapUnique(new FakeDisplaySnapshot( |
| + id_, origin_, physical_size, type_, is_aspect_preserving_scaling_, |
| + has_overscan_, has_color_correction_matrix_, name_, product_id_, |
| + std::move(modes_), current_mode_, native_mode_)); |
| +} |
| + |
| +Builder& FakeDisplaySnapshot::Builder::SetId(int64_t id) { |
| + id_ = id; |
| + return *this; |
| +} |
| + |
| +Builder& FakeDisplaySnapshot::Builder::SetNativeMode(const gfx::Size& size) { |
| + native_mode_ = AddOrFindDisplayMode(size); |
| + return *this; |
| +} |
| + |
| +Builder& FakeDisplaySnapshot::Builder::SetCurrentMode(const gfx::Size& size) { |
| + current_mode_ = AddOrFindDisplayMode(size); |
| + return *this; |
| +} |
| + |
| +Builder& FakeDisplaySnapshot::Builder::AddMode(const gfx::Size& size) { |
| + AddOrFindDisplayMode(size); |
| + return *this; |
| +} |
| + |
| +Builder& FakeDisplaySnapshot::Builder::SetOrigin(const gfx::Point& origin) { |
| + origin_ = origin; |
| + return *this; |
| +} |
| + |
| +Builder& FakeDisplaySnapshot::Builder::SetType(ui::DisplayConnectionType type) { |
| + type_ = type; |
| + return *this; |
| +} |
| + |
| +Builder& FakeDisplaySnapshot::Builder::SetIsAspectPerservingScaling(bool val) { |
| + is_aspect_preserving_scaling_ = val; |
| + return *this; |
| +} |
| + |
| +Builder& FakeDisplaySnapshot::Builder::SetHasOverscan(bool has_overscan) { |
| + has_overscan_ = has_overscan; |
| + return *this; |
| +} |
| + |
| +Builder& FakeDisplaySnapshot::Builder::SetHasColorCorrectionMatrix(bool val) { |
| + has_color_correction_matrix_ = val; |
| + return *this; |
| +} |
| + |
| +Builder& FakeDisplaySnapshot::Builder::SetName(const std::string& name) { |
| + name_ = name; |
| + return *this; |
| +} |
| + |
| +Builder& FakeDisplaySnapshot::Builder::SetProductId(int64_t product_id) { |
| + product_id_ = product_id; |
| + return *this; |
| +} |
| + |
| +Builder& FakeDisplaySnapshot::Builder::SetDPI(int dpi) { |
| + dpi_ = static_cast<float>(dpi); |
| + return *this; |
| +} |
| + |
| +Builder& FakeDisplaySnapshot::Builder::SetLowDPI() { |
| + return SetDPI(96); |
| +} |
| + |
| +Builder& FakeDisplaySnapshot::Builder::SetHighDPI() { |
| + return SetDPI(326); // Retina-ish. |
| +} |
| + |
| +const ui::DisplayMode* Builder::AddOrFindDisplayMode(const gfx::Size& size) { |
| + for (auto& mode : modes_) { |
| + if (mode->size() == size) |
| + return mode.get(); |
| + } |
| + |
| + // Not found, insert a mode with the size and return. |
| + modes_.push_back(base::MakeUnique<ui::DisplayMode>(size, false, 60.0f)); |
| + return modes_.back().get(); |
| +} |
| + |
| +FakeDisplaySnapshot::FakeDisplaySnapshot( |
| + int64_t display_id, |
| + const gfx::Point& origin, |
| + const gfx::Size& physical_size, |
| + ui::DisplayConnectionType type, |
| + bool is_aspect_preserving_scaling, |
| + bool has_overscan, |
| + bool has_color_correction_matrix, |
| + std::string display_name, |
| + int64_t product_id, |
| + std::vector<std::unique_ptr<const ui::DisplayMode>> modes, |
| + const ui::DisplayMode* current_mode, |
| + const ui::DisplayMode* native_mode) |
| : DisplaySnapshot(display_id, |
| - gfx::Point(0, 0), |
| - gfx::Size(display_size.width() * kDisplayPitchMM, |
| - display_size.height() * kDisplayPitchMM), |
| + origin, |
| + physical_size, |
| type, |
| - false, |
| - false, |
| - false, |
| - name, |
| + is_aspect_preserving_scaling, |
| + has_overscan, |
| + has_color_correction_matrix, |
| + display_name, |
| base::FilePath(), |
| - std::vector<std::unique_ptr<const ui::DisplayMode>>(), |
| + std::move(modes), |
| std::vector<uint8_t>(), |
| - nullptr, |
| - nullptr) { |
| - ui::DisplayMode mode(display_size, false, 60.0f); |
| - modes_.push_back(mode.Clone()); |
| - |
| - native_mode_ = modes_.front().get(); |
| + current_mode, |
| + native_mode) { |
| + product_id_ = product_id; |
| } |
| FakeDisplaySnapshot::~FakeDisplaySnapshot() {} |
| std::string FakeDisplaySnapshot::ToString() const { |
| return base::StringPrintf( |
| - "id=%" PRId64 " current_mode=%s physical_size=%s", display_id_, |
| + "id=%" PRId64 |
| + " current_mode=%s native_mode=%s origin=%s" |
| + " physical_size=%s, type=%s name=\"%s\" modes=(%s)", |
| + display_id_, |
| current_mode_ ? current_mode_->ToString().c_str() : "nullptr", |
| - physical_size_.ToString().c_str()); |
| + native_mode_ ? native_mode_->ToString().c_str() : "nullptr", |
| + origin_.ToString().c_str(), physical_size_.ToString().c_str(), |
| + DisplayConnectionTypeString(type_).c_str(), display_name_.c_str(), |
| + ModeListString(modes_).c_str()); |
| } |
| } // namespace display |