| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "services/ui/display/platform_screen_impl_ozone.h" | |
| 6 | |
| 7 #include <memory> | |
| 8 | |
| 9 #include "base/command_line.h" | |
| 10 #include "base/memory/ptr_util.h" | |
| 11 #include "base/sys_info.h" | |
| 12 #include "base/threading/thread_task_runner_handle.h" | |
| 13 #include "third_party/skia/include/core/SkColor.h" | |
| 14 #include "ui/display/types/display_constants.h" | |
| 15 #include "ui/display/types/display_snapshot.h" | |
| 16 #include "ui/display/types/native_display_delegate.h" | |
| 17 #include "ui/gfx/geometry/rect.h" | |
| 18 #include "ui/ozone/public/ozone_platform.h" | |
| 19 | |
| 20 namespace display { | |
| 21 namespace { | |
| 22 | |
| 23 // Needed for DisplayConfigurator::ForceInitialConfigure. | |
| 24 const SkColor kChromeOsBootColor = SkColorSetRGB(0xfe, 0xfe, 0xfe); | |
| 25 | |
| 26 } // namespace | |
| 27 | |
| 28 // static | |
| 29 std::unique_ptr<PlatformScreen> PlatformScreen::Create() { | |
| 30 return base::MakeUnique<PlatformScreenImplOzone>(); | |
| 31 } | |
| 32 | |
| 33 PlatformScreenImplOzone::PlatformScreenImplOzone() {} | |
| 34 | |
| 35 PlatformScreenImplOzone::~PlatformScreenImplOzone() { | |
| 36 display_configurator_.RemoveObserver(this); | |
| 37 } | |
| 38 | |
| 39 void PlatformScreenImplOzone::Init(PlatformScreenDelegate* delegate) { | |
| 40 DCHECK(delegate); | |
| 41 delegate_ = delegate; | |
| 42 | |
| 43 // We want display configuration to happen even off device to keep the control | |
| 44 // flow similar. | |
| 45 display_configurator_.set_configure_display(true); | |
| 46 display_configurator_.AddObserver(this); | |
| 47 display_configurator_.Init( | |
| 48 ui::OzonePlatform::GetInstance()->CreateNativeDisplayDelegate(), false); | |
| 49 | |
| 50 if (base::SysInfo::IsRunningOnChromeOS()) { | |
| 51 display_configurator_.ForceInitialConfigure(kChromeOsBootColor); | |
| 52 } else { | |
| 53 if (base::CommandLine::ForCurrentProcess()->HasSwitch("multi-display")) { | |
| 54 // This really doesn't work properly. Use at your own risk. | |
| 55 display_configurator_.AddVirtualDisplay(gfx::Size(800, 800)); | |
| 56 display_configurator_.AddVirtualDisplay(gfx::Size(800, 800)); | |
| 57 } else { | |
| 58 display_configurator_.AddVirtualDisplay(gfx::Size(1024, 768)); | |
| 59 } | |
| 60 } | |
| 61 } | |
| 62 | |
| 63 int64_t PlatformScreenImplOzone::GetPrimaryDisplayId() const { | |
| 64 return primary_display_id_; | |
| 65 } | |
| 66 | |
| 67 void PlatformScreenImplOzone::ProcessRemovedDisplays( | |
| 68 const ui::DisplayConfigurator::DisplayStateList& snapshots) { | |
| 69 std::vector<int64_t> current_ids; | |
| 70 for (ui::DisplaySnapshot* snapshot : snapshots) | |
| 71 current_ids.push_back(snapshot->display_id()); | |
| 72 | |
| 73 // Find cached displays with no matching snapshot and mark as removed. | |
| 74 for (DisplayInfo& display : cached_displays_) { | |
| 75 if (std::find(current_ids.begin(), current_ids.end(), display.id) == | |
| 76 current_ids.end()) { | |
| 77 display.removed = true; | |
| 78 if (primary_display_id_ == display.id) | |
| 79 primary_display_id_ = display::Display::kInvalidDisplayID; | |
| 80 } | |
| 81 } | |
| 82 | |
| 83 // If the primary display was removed find a new primary display id. | |
| 84 if (primary_display_id_ == display::Display::kInvalidDisplayID) { | |
| 85 for (const DisplayInfo& display : cached_displays_) { | |
| 86 if (!display.removed) { | |
| 87 primary_display_id_ = display.id; | |
| 88 break; | |
| 89 } | |
| 90 } | |
| 91 } | |
| 92 } | |
| 93 | |
| 94 void PlatformScreenImplOzone::ProcessModifiedDisplays( | |
| 95 const ui::DisplayConfigurator::DisplayStateList& snapshots) { | |
| 96 for (ui::DisplaySnapshot* snapshot : snapshots) { | |
| 97 auto iter = GetCachedDisplayIterator(snapshot->display_id()); | |
| 98 if (iter != cached_displays_.end()) { | |
| 99 DisplayInfo& display_info = *iter; | |
| 100 const ui::DisplayMode* current_mode = snapshot->current_mode(); | |
| 101 if (current_mode->size() != display_info.bounds.size()) { | |
| 102 display_info.bounds.set_size(current_mode->size()); | |
| 103 display_info.modified = true; | |
| 104 } | |
| 105 } | |
| 106 } | |
| 107 } | |
| 108 | |
| 109 void PlatformScreenImplOzone::UpdateCachedDisplays() { | |
| 110 // Walk through cached displays after processing the snapshots to find any | |
| 111 // removed or modified displays. This ensures that we only send one update per | |
| 112 // display to the delegate. | |
| 113 next_display_origin_.SetPoint(0, 0); | |
| 114 for (auto iter = cached_displays_.begin(); iter != cached_displays_.end();) { | |
| 115 DisplayInfo& display_info = *iter; | |
| 116 if (display_info.removed) { | |
| 117 // Update delegate and remove from cache. | |
| 118 delegate_->OnDisplayRemoved(display_info.id); | |
| 119 iter = cached_displays_.erase(iter); | |
| 120 } else { | |
| 121 // Check if the display origin needs to be updated. | |
| 122 if (next_display_origin_ != display_info.bounds.origin()) { | |
| 123 display_info.bounds.set_origin(next_display_origin_); | |
| 124 display_info.modified = true; | |
| 125 } | |
| 126 next_display_origin_.Offset(display_info.bounds.width(), 0); | |
| 127 | |
| 128 // Check if the window bounds have changed and update delegate. | |
| 129 if (display_info.modified) { | |
| 130 display_info.modified = false; | |
| 131 delegate_->OnDisplayModified(display_info.id, display_info.bounds); | |
| 132 } | |
| 133 ++iter; | |
| 134 } | |
| 135 } | |
| 136 } | |
| 137 | |
| 138 void PlatformScreenImplOzone::AddNewDisplays( | |
| 139 const ui::DisplayConfigurator::DisplayStateList& snapshots) { | |
| 140 for (ui::DisplaySnapshot* snapshot : snapshots) { | |
| 141 const int64_t id = snapshot->display_id(); | |
| 142 | |
| 143 // Check if display already exists and skip. | |
| 144 if (GetCachedDisplayIterator(id) != cached_displays_.end()) | |
| 145 continue; | |
| 146 | |
| 147 const ui::DisplayMode* current_mode = snapshot->current_mode(); | |
| 148 gfx::Rect bounds(next_display_origin_, current_mode->size()); | |
| 149 | |
| 150 // Move the origin so that next display is to the right of current display. | |
| 151 next_display_origin_.Offset(current_mode->size().width(), 0); | |
| 152 | |
| 153 // If we have no primary display then this one should be it. | |
| 154 if (primary_display_id_ == display::Display::kInvalidDisplayID) | |
| 155 primary_display_id_ = id; | |
| 156 | |
| 157 cached_displays_.push_back(DisplayInfo(id, bounds)); | |
| 158 delegate_->OnDisplayAdded(this, id, bounds); | |
| 159 } | |
| 160 } | |
| 161 | |
| 162 PlatformScreenImplOzone::CachedDisplayIterator | |
| 163 PlatformScreenImplOzone::GetCachedDisplayIterator(int64_t display_id) { | |
| 164 return std::find_if(cached_displays_.begin(), cached_displays_.end(), | |
| 165 [display_id](const DisplayInfo& display_info) { | |
| 166 return display_info.id == display_id; | |
| 167 }); | |
| 168 } | |
| 169 | |
| 170 void PlatformScreenImplOzone::OnDisplayModeChanged( | |
| 171 const ui::DisplayConfigurator::DisplayStateList& displays) { | |
| 172 ProcessRemovedDisplays(displays); | |
| 173 ProcessModifiedDisplays(displays); | |
| 174 UpdateCachedDisplays(); | |
| 175 AddNewDisplays(displays); | |
| 176 } | |
| 177 | |
| 178 void PlatformScreenImplOzone::OnDisplayModeChangeFailed( | |
| 179 const ui::DisplayConfigurator::DisplayStateList& displays, | |
| 180 ui::MultipleDisplayState failed_new_state) { | |
| 181 LOG(ERROR) << "OnDisplayModeChangeFailed from DisplayConfigurator"; | |
| 182 } | |
| 183 | |
| 184 } // namespace display | |
| OLD | NEW |