Revert of Revert of Revert of Revert of Revert of Enable ServiceWorkerPerfTest (servi...
[chromium-blink-merge.git] / ash / display / screen_ash.cc
blob271b8e6283dc2690bfa1d1dd2321c36bede48083
1 // Copyright 2014 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.
5 #include "ash/display/screen_ash.h"
7 #include "ash/display/display_controller.h"
8 #include "ash/display/display_manager.h"
9 #include "ash/root_window_controller.h"
10 #include "ash/root_window_settings.h"
11 #include "ash/shelf/shelf_layout_manager.h"
12 #include "ash/shelf/shelf_widget.h"
13 #include "ash/shell.h"
14 #include "ash/wm/coordinate_conversion.h"
15 #include "base/logging.h"
16 #include "ui/aura/client/screen_position_client.h"
17 #include "ui/aura/env.h"
18 #include "ui/aura/window_event_dispatcher.h"
19 #include "ui/gfx/display.h"
20 #include "ui/gfx/screen.h"
22 namespace ash {
24 namespace {
26 DisplayManager* GetDisplayManager() {
27 return Shell::GetInstance()->display_manager();
30 gfx::Display FindDisplayNearestPoint(const std::vector<gfx::Display>& displays,
31 const gfx::Point& point) {
32 int min_distance = INT_MAX;
33 const gfx::Display* nearest_display = NULL;
34 for (std::vector<gfx::Display>::const_iterator iter = displays.begin();
35 iter != displays.end(); ++iter) {
36 const gfx::Display& display = *iter;
37 int distance = display.bounds().ManhattanDistanceToPoint(point);
38 if (distance < min_distance) {
39 min_distance = distance;
40 nearest_display = &display;
43 // There should always be at least one display that is less than INT_MAX away.
44 DCHECK(nearest_display);
45 return *nearest_display;
48 const gfx::Display* FindDisplayMatching(
49 const std::vector<gfx::Display>& displays,
50 const gfx::Rect& match_rect) {
51 int max_area = 0;
52 const gfx::Display* matching = NULL;
53 for (std::vector<gfx::Display>::const_iterator iter = displays.begin();
54 iter != displays.end(); ++iter) {
55 const gfx::Display& display = *iter;
56 gfx::Rect intersect = gfx::IntersectRects(display.bounds(), match_rect);
57 int area = intersect.width() * intersect.height();
58 if (area > max_area) {
59 max_area = area;
60 matching = &display;
63 return matching;
66 class ScreenForShutdown : public gfx::Screen {
67 public:
68 explicit ScreenForShutdown(ScreenAsh* screen_ash)
69 : display_list_(screen_ash->GetAllDisplays()),
70 primary_display_(screen_ash->GetPrimaryDisplay()) {
73 // gfx::Screen overrides:
74 gfx::Point GetCursorScreenPoint() override { return gfx::Point(); }
75 gfx::NativeWindow GetWindowUnderCursor() override { return NULL; }
76 gfx::NativeWindow GetWindowAtScreenPoint(const gfx::Point& point) override {
77 return NULL;
79 int GetNumDisplays() const override { return display_list_.size(); }
80 std::vector<gfx::Display> GetAllDisplays() const override {
81 return display_list_;
83 gfx::Display GetDisplayNearestWindow(gfx::NativeView view) const override {
84 return primary_display_;
86 gfx::Display GetDisplayNearestPoint(const gfx::Point& point) const override {
87 return FindDisplayNearestPoint(display_list_, point);
89 gfx::Display GetDisplayMatching(const gfx::Rect& match_rect) const override {
90 const gfx::Display* matching =
91 FindDisplayMatching(display_list_, match_rect);
92 // Fallback to the primary display if there is no matching display.
93 return matching ? *matching : GetPrimaryDisplay();
95 gfx::Display GetPrimaryDisplay() const override { return primary_display_; }
96 void AddObserver(gfx::DisplayObserver* observer) override {
97 NOTREACHED() << "Observer should not be added during shutdown";
99 void RemoveObserver(gfx::DisplayObserver* observer) override {}
101 private:
102 const std::vector<gfx::Display> display_list_;
103 const gfx::Display primary_display_;
105 DISALLOW_COPY_AND_ASSIGN(ScreenForShutdown);
108 } // namespace
110 ScreenAsh::ScreenAsh() {
113 ScreenAsh::~ScreenAsh() {
116 void ScreenAsh::NotifyMetricsChanged(const gfx::Display& display,
117 uint32_t metrics) {
118 FOR_EACH_OBSERVER(gfx::DisplayObserver,
119 observers_,
120 OnDisplayMetricsChanged(display, metrics));
123 void ScreenAsh::NotifyDisplayAdded(const gfx::Display& display) {
124 FOR_EACH_OBSERVER(gfx::DisplayObserver, observers_, OnDisplayAdded(display));
127 void ScreenAsh::NotifyDisplayRemoved(const gfx::Display& display) {
128 FOR_EACH_OBSERVER(
129 gfx::DisplayObserver, observers_, OnDisplayRemoved(display));
132 gfx::Point ScreenAsh::GetCursorScreenPoint() {
133 return aura::Env::GetInstance()->last_mouse_location();
136 gfx::NativeWindow ScreenAsh::GetWindowUnderCursor() {
137 return GetWindowAtScreenPoint(Shell::GetScreen()->GetCursorScreenPoint());
140 gfx::NativeWindow ScreenAsh::GetWindowAtScreenPoint(const gfx::Point& point) {
141 return wm::GetRootWindowAt(point)->GetTopWindowContainingPoint(point);
144 int ScreenAsh::GetNumDisplays() const {
145 return GetDisplayManager()->GetNumDisplays();
148 std::vector<gfx::Display> ScreenAsh::GetAllDisplays() const {
149 return GetDisplayManager()->displays();
152 gfx::Display ScreenAsh::GetDisplayNearestWindow(gfx::NativeView window) const {
153 if (!window)
154 return GetPrimaryDisplay();
155 const aura::Window* root_window = window->GetRootWindow();
156 if (!root_window)
157 return GetPrimaryDisplay();
158 const RootWindowSettings* rws = GetRootWindowSettings(root_window);
159 int64 id = rws->display_id;
160 // if id is |kInvaildDisplayID|, it's being deleted.
161 DCHECK(id != gfx::Display::kInvalidDisplayID);
162 if (id == gfx::Display::kInvalidDisplayID)
163 return GetPrimaryDisplay();
165 DisplayManager* display_manager = GetDisplayManager();
166 // RootWindow needs Display to determine its device scale factor
167 // for non desktop display.
168 if (display_manager->mirroring_display().id() == id)
169 return display_manager->mirroring_display();
170 return display_manager->GetDisplayForId(id);
173 gfx::Display ScreenAsh::GetDisplayNearestPoint(const gfx::Point& point) const {
174 const gfx::Display& display =
175 GetDisplayManager()->FindDisplayContainingPoint(point);
176 if (display.is_valid())
177 return display;
178 // Fallback to the display that has the shortest Manhattan distance from
179 // the |point|. This is correct in the only areas that matter, namely in the
180 // corners between the physical screens.
181 return FindDisplayNearestPoint(GetDisplayManager()->displays(), point);
184 gfx::Display ScreenAsh::GetDisplayMatching(const gfx::Rect& match_rect) const {
185 if (match_rect.IsEmpty())
186 return GetDisplayNearestPoint(match_rect.origin());
187 const gfx::Display* matching =
188 FindDisplayMatching(GetDisplayManager()->displays(), match_rect);
189 // Fallback to the primary display if there is no matching display.
190 return matching ? *matching : GetPrimaryDisplay();
193 gfx::Display ScreenAsh::GetPrimaryDisplay() const {
194 return GetDisplayManager()->GetDisplayForId(
195 DisplayController::GetPrimaryDisplayId());
198 void ScreenAsh::AddObserver(gfx::DisplayObserver* observer) {
199 observers_.AddObserver(observer);
202 void ScreenAsh::RemoveObserver(gfx::DisplayObserver* observer) {
203 observers_.RemoveObserver(observer);
206 gfx::Screen* ScreenAsh::CloneForShutdown() {
207 return new ScreenForShutdown(this);
210 } // namespace ash