Chromium Code Reviews| 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_delegate.h" | 5 #include "ui/display/fake_display_delegate.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <utility> | |
| 8 | 9 |
| 9 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| 10 #include "base/hash.h" | 11 #include "base/hash.h" |
| 11 #include "base/logging.h" | 12 #include "base/logging.h" |
| 12 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
| 13 #include "base/strings/string_split.h" | 14 #include "base/strings/string_split.h" |
| 14 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
| 15 #include "ui/display/display.h" | 16 #include "ui/display/display.h" |
| 16 #include "ui/display/display_switches.h" | 17 #include "ui/display/display_switches.h" |
| 17 #include "ui/display/types/native_display_observer.h" | 18 #include "ui/display/types/native_display_observer.h" |
| 18 #include "ui/display/util/display_util.h" | 19 #include "ui/display/util/display_util.h" |
| 19 | 20 |
| 20 namespace display { | 21 namespace display { |
| 21 | 22 |
| 22 namespace { | 23 namespace { |
| 23 | 24 |
| 24 // The EDID specification marks the top bit of the manufacturer id as reserved. | 25 // The EDID specification marks the top bit of the manufacturer id as reserved. |
| 25 const uint16_t kReservedManufacturerID = 1 << 15; | 26 const uint16_t kReservedManufacturerID = 1 << 15; |
| 26 | 27 |
| 27 // A random product name hash. | 28 // A random product name hash. |
| 28 const uint32_t kProductCodeHash = base::Hash("Very Generic Display"); | 29 const uint32_t kProductCodeHash = base::Hash("Very Generic Display"); |
| 29 | 30 |
| 30 } // namespace | 31 } // namespace |
| 31 | 32 |
| 32 FakeDisplayDelegate::FakeDisplayDelegate() {} | 33 FakeDisplayDelegate::FakeDisplayDelegate() {} |
| 33 | 34 |
| 34 FakeDisplayDelegate::~FakeDisplayDelegate() {} | 35 FakeDisplayDelegate::~FakeDisplayDelegate() {} |
| 35 | 36 |
| 36 int64_t FakeDisplayDelegate::AddDisplay(const gfx::Size& display_size) { | 37 int64_t FakeDisplayDelegate::AddDisplay(const gfx::Size& display_size) { |
| 38 DCHECK(!display_size.IsEmpty()); | |
| 39 | |
| 37 if (next_display_id_ == 0xFF) { | 40 if (next_display_id_ == 0xFF) { |
| 38 LOG(ERROR) << "Exceeded display id limit"; | 41 LOG(ERROR) << "Exceeded display id limit"; |
| 39 return display::Display::kInvalidDisplayID; | 42 return Display::kInvalidDisplayID; |
| 40 } | 43 } |
| 41 | 44 |
| 42 int64_t id = GenerateDisplayID(kReservedManufacturerID, kProductCodeHash, | 45 int64_t id = GenerateDisplayID(kReservedManufacturerID, kProductCodeHash, |
| 43 ++next_display_id_); | 46 ++next_display_id_); |
| 47 | |
| 48 FakeDisplaySnapshot::Builder builder; | |
| 49 builder.SetId(id).SetNativeMode(display_size); | |
| 50 builder.SetName(base::StringPrintf("Fake Display %d", next_display_id_)); | |
| 51 | |
| 44 // Add the first display as internal. | 52 // Add the first display as internal. |
| 45 ui::DisplayConnectionType type = displays_.empty() | 53 if (displays_.empty()) |
| 46 ? ui::DISPLAY_CONNECTION_TYPE_INTERNAL | 54 builder.SetType(ui::DISPLAY_CONNECTION_TYPE_INTERNAL); |
| 47 : ui::DISPLAY_CONNECTION_TYPE_UNKNOWN; | |
| 48 std::string name = base::StringPrintf("Fake Display %d", next_display_id_); | |
| 49 | 55 |
| 50 displays_.push_back( | 56 return AddDisplay(builder.Build()) ? id : Display::kInvalidDisplayID; |
| 51 base::MakeUnique<FakeDisplaySnapshot>(id, display_size, type, name)); | 57 } |
| 58 | |
| 59 bool FakeDisplayDelegate::AddDisplay( | |
| 60 std::unique_ptr<ui::DisplaySnapshot> display) { | |
| 61 DCHECK(display); | |
| 62 | |
| 63 // Check there is no existing display with the same id. | |
| 64 for (auto& existing_display : displays_) { | |
| 65 if (existing_display->display_id() == display->display_id()) | |
| 66 return false; | |
|
Daniel Erat
2016/09/20 17:12:49
nit: worthwhile logging a warning or error here?
kylechar
2016/09/20 17:47:40
Done.
| |
| 67 } | |
| 68 | |
| 69 DVLOG(1) << "Added display " << display->ToString(); | |
| 70 displays_.push_back(std::move(display)); | |
| 52 OnConfigurationChanged(); | 71 OnConfigurationChanged(); |
| 53 | 72 |
| 54 return id; | 73 return true; |
| 55 } | 74 } |
| 56 | 75 |
| 57 bool FakeDisplayDelegate::RemoveDisplay(int64_t display_id) { | 76 bool FakeDisplayDelegate::RemoveDisplay(int64_t display_id) { |
| 58 // Find display snapshot with matching id and remove it. | 77 // Find display snapshot with matching id and remove it. |
| 59 for (auto iter = displays_.begin(); iter != displays_.end(); ++iter) { | 78 for (auto iter = displays_.begin(); iter != displays_.end(); ++iter) { |
| 60 if ((*iter)->display_id() == display_id) { | 79 if ((*iter)->display_id() == display_id) { |
| 80 DVLOG(1) << "Removed display " << (*iter)->ToString(); | |
| 61 displays_.erase(iter); | 81 displays_.erase(iter); |
| 62 OnConfigurationChanged(); | 82 OnConfigurationChanged(); |
| 63 return true; | 83 return true; |
| 64 } | 84 } |
| 65 } | 85 } |
| 66 | 86 |
| 67 return false; | 87 return false; |
| 68 } | 88 } |
| 69 | 89 |
| 70 void FakeDisplayDelegate::Initialize() { | 90 void FakeDisplayDelegate::Initialize() { |
| 71 DCHECK(!initialized_); | 91 DCHECK(!initialized_); |
| 72 InitFromCommandLine(); | |
| 73 | 92 |
| 74 // If no command line flags are provided then initialize a default display. | 93 // If no command line flags are provided then initialize a default display. |
| 75 if (displays_.empty()) | 94 if (!InitFromCommandLine()) |
| 76 AddDisplay(gfx::Size(1024, 768)); | 95 AddDisplay(gfx::Size(1024, 768)); |
| 77 | 96 |
| 78 initialized_ = true; | 97 initialized_ = true; |
| 79 } | 98 } |
| 80 | 99 |
| 81 void FakeDisplayDelegate::GrabServer() {} | 100 void FakeDisplayDelegate::GrabServer() {} |
| 82 | 101 |
| 83 void FakeDisplayDelegate::UngrabServer() {} | 102 void FakeDisplayDelegate::UngrabServer() {} |
| 84 | 103 |
| 85 void FakeDisplayDelegate::TakeDisplayControl( | 104 void FakeDisplayDelegate::TakeDisplayControl( |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 155 } | 174 } |
| 156 | 175 |
| 157 void FakeDisplayDelegate::RemoveObserver(ui::NativeDisplayObserver* observer) { | 176 void FakeDisplayDelegate::RemoveObserver(ui::NativeDisplayObserver* observer) { |
| 158 observers_.RemoveObserver(observer); | 177 observers_.RemoveObserver(observer); |
| 159 } | 178 } |
| 160 | 179 |
| 161 FakeDisplayController* FakeDisplayDelegate::GetFakeDisplayController() { | 180 FakeDisplayController* FakeDisplayDelegate::GetFakeDisplayController() { |
| 162 return static_cast<FakeDisplayController*>(this); | 181 return static_cast<FakeDisplayController*>(this); |
| 163 } | 182 } |
| 164 | 183 |
| 165 void FakeDisplayDelegate::InitFromCommandLine() { | 184 bool FakeDisplayDelegate::InitFromCommandLine() { |
| 166 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | 185 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
| 167 if (!command_line->HasSwitch(switches::kScreenConfig)) | 186 if (!command_line->HasSwitch(switches::kScreenConfig)) |
| 168 return; | 187 return false; |
| 169 | 188 |
| 170 const std::string command_string = | 189 const std::string command_string = |
| 171 command_line->GetSwitchValueASCII(switches::kScreenConfig); | 190 command_line->GetSwitchValueASCII(switches::kScreenConfig); |
| 172 | 191 |
| 192 // Start without any displays. | |
| 193 if (command_string == "none") | |
| 194 return true; | |
| 195 | |
| 173 // Split on commas and parse each display string. | 196 // Split on commas and parse each display string. |
| 174 for (std::string part : base::SplitString( | 197 for (std::string part : base::SplitString( |
| 175 command_string, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL)) { | 198 command_string, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL)) { |
| 176 int width = 0; | 199 std::unique_ptr<ui::DisplaySnapshot> snapshot = |
| 177 int height = 0; | 200 CreateSnapshotFromSpec(part); |
| 178 if (sscanf(part.c_str(), "%dx%d", &width, &height) >= 2) { | 201 if (snapshot) { |
| 179 gfx::Size display_size(width, height); | 202 AddDisplay(std::move(snapshot)); |
| 180 AddDisplay(display_size); | |
| 181 } else { | 203 } else { |
| 182 LOG(ERROR) << "Failed to parse display \"" << part << "\""; | 204 LOG(ERROR) << "Failed to parse display \"" << part << "\""; |
| 183 } | 205 } |
| 184 } | 206 } |
| 207 | |
| 208 return true; | |
| 209 } | |
| 210 | |
| 211 std::unique_ptr<ui::DisplaySnapshot> | |
| 212 FakeDisplayDelegate::CreateSnapshotFromSpec(const std::string& spec) { | |
| 213 int width = 0; | |
| 214 int height = 0; | |
| 215 int dpi = 0; | |
| 216 | |
| 217 int found = sscanf(spec.c_str(), "%dx%d^%d", &width, &height, &dpi); | |
|
Daniel Erat
2016/09/20 17:12:49
nit: probably worthwhile adding a comment here exp
kylechar
2016/09/20 17:47:40
Done.
| |
| 218 if (found < 2) | |
| 219 return nullptr; | |
| 220 | |
| 221 int64_t id = GenerateDisplayID(kReservedManufacturerID, kProductCodeHash, | |
| 222 ++next_display_id_); | |
| 223 | |
| 224 FakeDisplaySnapshot::Builder builder; | |
| 225 builder.SetId(id).SetNativeMode(gfx::Size(width, height)); | |
| 226 builder.SetName(base::StringPrintf("Fake Display %d", next_display_id_)); | |
|
Daniel Erat
2016/09/20 17:12:49
did you mean to use |id|, or maybe |next_display_i
kylechar
2016/09/20 17:47:40
It was intended. Using |next_display_id_| after it
| |
| 227 | |
| 228 if (found >= 3) | |
| 229 builder.SetDPI(dpi); | |
| 230 | |
| 231 // TODO(kylechar): Add type to the spec string. | |
| 232 if (displays_.empty()) | |
| 233 builder.SetType(ui::DISPLAY_CONNECTION_TYPE_INTERNAL); | |
| 234 | |
| 235 return builder.Build(); | |
| 185 } | 236 } |
| 186 | 237 |
| 187 void FakeDisplayDelegate::OnConfigurationChanged() { | 238 void FakeDisplayDelegate::OnConfigurationChanged() { |
| 188 if (!initialized_) | 239 if (!initialized_) |
| 189 return; | 240 return; |
| 190 | 241 |
| 191 FOR_EACH_OBSERVER(ui::NativeDisplayObserver, observers_, | 242 FOR_EACH_OBSERVER(ui::NativeDisplayObserver, observers_, |
| 192 OnConfigurationChanged()); | 243 OnConfigurationChanged()); |
| 193 } | 244 } |
| 194 | 245 |
| 195 } // namespace display | 246 } // namespace display |
| OLD | NEW |