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