summaryrefslogtreecommitdiffstats
path: root/components/mus/ws/display.h
blob: ad1e8e13c0ae091645e26b9709400380bde58937 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_MUS_WS_DISPLAY_H_
#define COMPONENTS_MUS_WS_DISPLAY_H_

#include <stdint.h>

#include <map>
#include <queue>

#include "base/macros.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "components/mus/common/types.h"
#include "components/mus/public/interfaces/window_manager_constants.mojom.h"
#include "components/mus/public/interfaces/window_tree_host.mojom.h"
#include "components/mus/ws/focus_controller_delegate.h"
#include "components/mus/ws/focus_controller_observer.h"
#include "components/mus/ws/platform_display.h"
#include "components/mus/ws/server_window.h"
#include "components/mus/ws/server_window_observer.h"
#include "components/mus/ws/server_window_tracker.h"
#include "components/mus/ws/user_id_tracker_observer.h"
#include "components/mus/ws/window_manager_factory_registry_observer.h"

namespace mus {
namespace ws {

class DisplayBinding;
class DisplayManager;
class FocusController;
struct PlatformDisplayInitParams;
class WindowManagerState;
class WindowServer;
class WindowTree;

namespace test {
class DisplayTestApi;
}

// Displays manages the state associated with a single display. Display has a
// single root window whose children are the roots for a per-user
// WindowManager. Display is configured in two distinct
// ways:
// . with a DisplayBinding. In this mode there is only ever one WindowManager
//   for the display, which comes from the client that created the
//   Display.
// . without a DisplayBinding. In this mode a WindowManager is automatically
//   created per user.
class Display : public PlatformDisplayDelegate,
                public mojom::WindowTreeHost,
                public FocusControllerObserver,
                public FocusControllerDelegate,
                public ServerWindowObserver,
                public UserIdTrackerObserver,
                public WindowManagerFactoryRegistryObserver {
 public:
  Display(WindowServer* window_server,
          const PlatformDisplayInitParams& platform_display_init_params);
  ~Display() override;

  // Initializes state that depends on the existence of a Display.
  void Init(scoped_ptr<DisplayBinding> binding);

  uint32_t id() const { return id_; }

  DisplayManager* display_manager();
  const DisplayManager* display_manager() const;

  // Returns a mojom::Display for the specified display. WindowManager specific
  // values are not set. In general you should use
  // WindowManagerState::ToMojomDisplay().
  mojom::DisplayPtr ToMojomDisplay() const;

  // Schedules a paint for the specified region in the coordinates of |window|.
  void SchedulePaint(const ServerWindow* window, const gfx::Rect& bounds);

  // Schedules destruction of surfaces in |window|. If a frame has been
  // scheduled but not drawn surface destruction is delayed until the frame is
  // drawn, otherwise destruction is immediate.
  void ScheduleSurfaceDestruction(ServerWindow* window);

  // Returns the metrics for this viewport.
  const mojom::ViewportMetrics& GetViewportMetrics() const;

  mojom::Rotation GetRotation() const;

  WindowServer* window_server() { return window_server_; }

  // Returns the root of the Display. The root's children are the roots
  // of the corresponding WindowManagers.
  ServerWindow* root_window() { return root_.get(); }
  const ServerWindow* root_window() const { return root_.get(); }

  ServerWindow* GetRootWithId(const WindowId& id);

  WindowManagerState* GetWindowManagerStateWithRoot(const ServerWindow* window);
  WindowManagerState* GetWindowManagerStateForUser(const UserId& user_id) {
    return const_cast<WindowManagerState*>(
        const_cast<const Display*>(this)->GetWindowManagerStateForUser(
            user_id));
  }
  const WindowManagerState* GetWindowManagerStateForUser(
      const UserId& user_id) const;
  WindowManagerState* GetActiveWindowManagerState() {
    return const_cast<WindowManagerState*>(
        const_cast<const Display*>(this)->GetActiveWindowManagerState());
  }
  const WindowManagerState* GetActiveWindowManagerState() const;
  size_t num_window_manger_states() const {
    return window_manager_state_map_.size();
  }

  void SetFocusedWindow(ServerWindow* window);
  ServerWindow* GetFocusedWindow();
  void DestroyFocusController();
  FocusController* focus_controller() { return focus_controller_.get(); }

  void AddActivationParent(ServerWindow* window);
  void RemoveActivationParent(ServerWindow* window);

  void UpdateTextInputState(ServerWindow* window,
                            const ui::TextInputState& state);
  void SetImeVisibility(ServerWindow* window, bool visible);

  // Called just before |tree| is destroyed.
  void OnWillDestroyTree(WindowTree* tree);

  void UpdateNativeCursor(int32_t cursor_id);

  // Called when a client updates a cursor. This will update the cursor on the
  // native display if the cursor is currently under |window|.
  void OnCursorUpdated(ServerWindow* window);

  // Called when the window tree when stacking and bounds of a window
  // change. This may update the cursor if the ServerWindow under the last
  // known pointer location changed.
  void MaybeChangeCursorOnWindowTreeChange();

  // mojom::WindowTreeHost:
  void SetSize(mojo::SizePtr size) override;
  void SetTitle(const mojo::String& title) override;

 private:
  friend class test::DisplayTestApi;

  using WindowManagerStateMap =
      std::map<UserId, scoped_ptr<WindowManagerState>>;

  // Inits the necessary state once the display is ready.
  void InitWindowManagersIfNecessary();

  // Creates the set of WindowManagerStates from the
  // WindowManagerFactoryRegistry.
  void CreateWindowManagerStatesFromRegistry();

  void CreateWindowManagerStateFromService(
      WindowManagerFactoryService* service);

  // PlatformDisplayDelegate:
  ServerWindow* GetRootWindow() override;
  void OnEvent(const ui::Event& event) override;
  void OnNativeCaptureLost() override;
  void OnDisplayClosed() override;
  void OnViewportMetricsChanged(
      const mojom::ViewportMetrics& old_metrics,
      const mojom::ViewportMetrics& new_metrics) override;
  void OnTopLevelSurfaceChanged(cc::SurfaceId surface_id) override;
  void OnCompositorFrameDrawn() override;

  // FocusControllerDelegate:
  bool CanHaveActiveChildren(ServerWindow* window) const override;

  // FocusControllerObserver:
  void OnActivationChanged(ServerWindow* old_active_window,
                           ServerWindow* new_active_window) override;
  void OnFocusChanged(FocusControllerChangeSource change_source,
                      ServerWindow* old_focused_window,
                      ServerWindow* new_focused_window) override;

  // ServerWindowObserver:
  void OnWindowDestroyed(ServerWindow* window) override;

  // UserIdTrackerObserver:
  void OnActiveUserIdChanged(const UserId& previously_active_id,
                             const UserId& active_id) override;
  void OnUserIdAdded(const UserId& id) override;
  void OnUserIdRemoved(const UserId& id) override;

  // WindowManagerFactoryRegistryObserver:
  void OnWindowManagerFactorySet(WindowManagerFactoryService* service) override;

  const uint32_t id_;
  scoped_ptr<DisplayBinding> binding_;
  // Set once Init() has been called.
  bool init_called_ = false;
  WindowServer* const window_server_;
  scoped_ptr<ServerWindow> root_;
  scoped_ptr<PlatformDisplay> platform_display_;
  scoped_ptr<FocusController> focus_controller_;

  // The last cursor set. Used to track whether we need to change the cursor.
  int32_t last_cursor_;

  ServerWindowTracker activation_parents_;

  // Set of windows with surfaces that need to be destroyed once the frame
  // draws.
  std::set<ServerWindow*> windows_needing_frame_destruction_;

  WindowManagerStateMap window_manager_state_map_;

  cc::SurfaceId top_level_surface_id_;

  DISALLOW_COPY_AND_ASSIGN(Display);
};

}  // namespace ws
}  // namespace mus

#endif  // COMPONENTS_MUS_WS_DISPLAY_H_