window_tree_host.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. // Copyright (c) 2012 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. #ifndef UI_AURA_WINDOW_TREE_HOST_H_
  5. #define UI_AURA_WINDOW_TREE_HOST_H_
  6. #include <stdint.h>
  7. #include <memory>
  8. #include <string>
  9. #include "base/containers/flat_map.h"
  10. #include "base/containers/flat_set.h"
  11. #include "base/memory/raw_ptr.h"
  12. #include "base/memory/weak_ptr.h"
  13. #include "base/observer_list.h"
  14. #include "build/build_config.h"
  15. #include "third_party/abseil-cpp/absl/types/optional.h"
  16. #include "third_party/skia/include/core/SkRegion.h"
  17. #include "ui/aura/aura_export.h"
  18. #include "ui/aura/scoped_enable_unadjusted_mouse_events.h"
  19. #include "ui/aura/window.h"
  20. #include "ui/base/cursor/cursor.h"
  21. #include "ui/base/ime/ime_key_event_dispatcher.h"
  22. #include "ui/compositor/compositor_observer.h"
  23. #include "ui/display/display_observer.h"
  24. #include "ui/events/event_source.h"
  25. #include "ui/events/platform_event.h"
  26. #include "ui/gfx/native_widget_types.h"
  27. #include "ui/gfx/overlay_transform.h"
  28. namespace gfx {
  29. class Point;
  30. class Rect;
  31. class Size;
  32. class Transform;
  33. } // namespace gfx
  34. namespace ui {
  35. class Compositor;
  36. enum class DomCode;
  37. class EventSink;
  38. class InputMethod;
  39. class ViewProp;
  40. struct PlatformWindowInitProperties;
  41. } // namespace ui
  42. namespace viz {
  43. class FrameSinkId;
  44. }
  45. namespace aura {
  46. namespace test {
  47. class WindowTreeHostTestApi;
  48. }
  49. class ScopedKeyboardHook;
  50. class WindowEventDispatcher;
  51. class WindowTreeHostObserver;
  52. // WindowTreeHost bridges between a native window and the embedded RootWindow.
  53. // It provides the accelerated widget and maps events from the native os to
  54. // aura.
  55. class AURA_EXPORT WindowTreeHost : public ui::ImeKeyEventDispatcher,
  56. public ui::EventSource,
  57. public display::DisplayObserver,
  58. public ui::CompositorObserver {
  59. public:
  60. // VideoCaptureLock ensures state necessary for capturing video remains in
  61. // effect. For example, this may force keeping the compositor visible when
  62. // it normally would not be.
  63. class AURA_EXPORT VideoCaptureLock {
  64. public:
  65. VideoCaptureLock(const VideoCaptureLock&) = delete;
  66. VideoCaptureLock& operator=(const VideoCaptureLock&) = delete;
  67. ~VideoCaptureLock();
  68. private:
  69. friend class WindowTreeHost;
  70. explicit VideoCaptureLock(WindowTreeHost* host);
  71. base::WeakPtr<WindowTreeHost> host_;
  72. };
  73. WindowTreeHost(const WindowTreeHost&) = delete;
  74. WindowTreeHost& operator=(const WindowTreeHost&) = delete;
  75. ~WindowTreeHost() override;
  76. // Creates a new WindowTreeHost with the specified |properties|.
  77. static std::unique_ptr<WindowTreeHost> Create(
  78. ui::PlatformWindowInitProperties properties);
  79. // Returns the WindowTreeHost for the specified accelerated widget, or NULL
  80. // if there is none associated.
  81. static WindowTreeHost* GetForAcceleratedWidget(gfx::AcceleratedWidget widget);
  82. void InitHost();
  83. void AddObserver(WindowTreeHostObserver* observer);
  84. void RemoveObserver(WindowTreeHostObserver* observer);
  85. bool HasObserver(const WindowTreeHostObserver* observer) const;
  86. Window* window() { return window_; }
  87. const Window* window() const { return window_; }
  88. WindowEventDispatcher* dispatcher() {
  89. return const_cast<WindowEventDispatcher*>(
  90. const_cast<const WindowTreeHost*>(this)->dispatcher());
  91. }
  92. const WindowEventDispatcher* dispatcher() const { return dispatcher_.get(); }
  93. ui::Compositor* compositor() { return compositor_.get(); }
  94. base::WeakPtr<WindowTreeHost> GetWeakPtr();
  95. // Gets/Sets the root window's transform.
  96. virtual gfx::Transform GetRootTransform() const;
  97. virtual void SetRootTransform(const gfx::Transform& transform);
  98. virtual gfx::Transform GetInverseRootTransform() const;
  99. void SetDisplayTransformHint(gfx::OverlayTransform transform);
  100. // These functions are used in event translation for translating the local
  101. // coordinates of LocatedEvents. Default implementation calls to non-local
  102. // ones (e.g. GetRootTransform()).
  103. virtual gfx::Transform GetRootTransformForLocalEventCoordinates() const;
  104. virtual gfx::Transform GetInverseRootTransformForLocalEventCoordinates()
  105. const;
  106. // Updates the compositor's size and scale from |new_size_in_pixels|,
  107. // |device_scale_factor_| and the compositor's transform hint.
  108. void UpdateCompositorScaleAndSize(const gfx::Size& new_size_in_pixels);
  109. // Converts |point| from the root window's coordinate system to native
  110. // screen's.
  111. void ConvertDIPToScreenInPixels(gfx::Point* point) const;
  112. // Converts |point| from native screen coordinate system to the root window's.
  113. void ConvertScreenInPixelsToDIP(gfx::Point* point) const;
  114. // Converts |point| from the root window's coordinate system to the
  115. // host window's.
  116. void ConvertDIPToPixels(gfx::Point* point) const;
  117. virtual void ConvertDIPToPixels(gfx::PointF* point) const;
  118. // Converts |point| from the host window's coordinate system to the
  119. // root window's.
  120. void ConvertPixelsToDIP(gfx::Point* point) const;
  121. virtual void ConvertPixelsToDIP(gfx::PointF* point) const;
  122. // Sets the currently-displayed cursor. If the cursor was previously hidden
  123. // via ShowCursor(false), it will remain hidden until ShowCursor(true) is
  124. // called, at which point the cursor that was last set via SetCursor() will be
  125. // used.
  126. void SetCursor(gfx::NativeCursor cursor);
  127. // Invoked when the cursor's visibility has changed.
  128. void OnCursorVisibilityChanged(bool visible);
  129. // Moves the cursor to the specified location relative to the root window.
  130. void MoveCursorToLocationInDIP(const gfx::Point& location_in_dip);
  131. // Moves the cursor to the |location_in_pixels| given in host coordinates.
  132. void MoveCursorToLocationInPixels(const gfx::Point& location_in_pixels);
  133. gfx::NativeCursor last_cursor() const { return last_cursor_; }
  134. // Gets the InputMethod instance, if NULL, creates & owns it.
  135. ui::InputMethod* GetInputMethod();
  136. bool has_input_method() const { return input_method_ != nullptr; }
  137. // Sets a shared unowned InputMethod. This is used when there is a singleton
  138. // InputMethod shared between multiple WindowTreeHost instances.
  139. //
  140. // This is used for Ash only. There are 2 reasons:
  141. // 1) ChromeOS virtual keyboard needs to receive
  142. // SetVirtualKeyboardVisibilityIfEnabled() notification from InputMethod.
  143. // Multiple InputMethod instances makes it hard to register/unregister the
  144. // observer for that notification. 2) For Ozone, there is no native focus
  145. // state for the root window and WindowTreeHost. See
  146. // DrmWindowHost::CanDispatchEvent, the key events always goes to the primary
  147. // WindowTreeHost. And after InputMethod processed the key event and continue
  148. // dispatching it, WindowTargeter::FindTargetForEvent may re-dispatch it to a
  149. // different WindowTreeHost. So the singleton InputMethod can make sure the
  150. // correct InputMethod instance processes the key event no matter which
  151. // WindowTreeHost is the target for event. Please refer to the test:
  152. // ExtendedDesktopTest.KeyEventsOnLockScreen.
  153. //
  154. // TODO(shuchen): remove this method after above reasons become invalid.
  155. // A possible solution is to make sure DrmWindowHost can find the correct
  156. // WindowTreeHost to dispatch events.
  157. void SetSharedInputMethod(ui::InputMethod* input_method);
  158. // Overridden from ui::ImeKeyEventDispatcher:
  159. ui::EventDispatchDetails DispatchKeyEventPostIME(ui::KeyEvent* event) final;
  160. // Overridden from ui::EventSource:
  161. ui::EventSink* GetEventSink() override;
  162. // Returns the id of the display. Default implementation queries Screen.
  163. virtual int64_t GetDisplayId();
  164. // Returns the EventSource responsible for dispatching events to the window
  165. // tree.
  166. virtual ui::EventSource* GetEventSource() = 0;
  167. // Returns the accelerated widget.
  168. virtual gfx::AcceleratedWidget GetAcceleratedWidget() = 0;
  169. // Shows the WindowTreeHost.
  170. void Show();
  171. // Hides the WindowTreeHost.
  172. void Hide();
  173. // Sets/Gets the bounds of the WindowTreeHost (in pixels). Note that a call to
  174. // GetBoundsInPixels() immediately following a SetBoundsInPixels() can return
  175. // the old bounds, because SetBoundsInPixels() can take effect asynchronously,
  176. // depending on the platform. The |local_surface_id| takes effect when (and
  177. // if) the new size is confirmed (potentially asynchronously) by the platform.
  178. virtual void SetBoundsInPixels(const gfx::Rect& bounds_in_pixels) = 0;
  179. virtual gfx::Rect GetBoundsInPixels() const = 0;
  180. // Gets the bounds in DIP.
  181. virtual gfx::Rect GetBoundsInDIP() const;
  182. // Returns the bounds relative to the accelerated widget. In the typical case,
  183. // the origin is 0,0 and the size is the same as the pixel-bounds. On some
  184. // OSs the bounds may be inset (on Windows, this is referred to as the client
  185. // area). When the bounds are inset, this returns a non-zero origin with a
  186. // size smaller than GetBoundsInPixels().
  187. virtual gfx::Rect GetBoundsInAcceleratedWidgetPixelCoordinates();
  188. // Sets the OS capture to the root window.
  189. virtual void SetCapture() = 0;
  190. // Releases OS capture of the root window.
  191. virtual void ReleaseCapture() = 0;
  192. // Returns the device scale assumed by the WindowTreeHost (set during the
  193. // most recent call to OnHostResizedInPixels).
  194. float device_scale_factor() const { return device_scale_factor_; }
  195. // Requests that |keys| be intercepted at the platform level and routed
  196. // directly to the web content. If |codes| is empty, all keys will be
  197. // intercepted. Returns a ScopedKeyboardHook instance which stops capturing
  198. // system key events when destroyed.
  199. std::unique_ptr<ScopedKeyboardHook> CaptureSystemKeyEvents(
  200. absl::optional<base::flat_set<ui::DomCode>> codes);
  201. // Returns a map of KeyboardEvent code to KeyboardEvent key values.
  202. virtual base::flat_map<std::string, std::string> GetKeyboardLayoutMap() = 0;
  203. // Returns true if KeyEvents should be send to IME. This is called from
  204. // WindowEventDispatcher during event dispatch.
  205. virtual bool ShouldSendKeyEventToIme();
  206. // Determines if native window occlusion should be enabled or not.
  207. bool IsNativeWindowOcclusionEnabled() const;
  208. // Remembers the current occlusion state, and if it has changed, notifies
  209. // observers of the change. `occluded_region` is only applicable when visible
  210. // and gives the occluded region. If `occluded_region` is empty, the entire
  211. // AcceleratedWidget is visible.
  212. virtual void SetNativeWindowOcclusionState(Window::OcclusionState state,
  213. const SkRegion& occluded_region);
  214. Window::OcclusionState GetNativeWindowOcclusionState() {
  215. return occlusion_state_;
  216. }
  217. const SkRegion& GetNativeOccludedRegion() const { return occluded_region_; }
  218. // Requests using unadjusted movement mouse events, i.e. WM_INPUT on Windows.
  219. // Returns a ScopedEnableUnadjustedMouseEvents instance which stops using
  220. // unadjusted mouse events when destroyed, returns nullptr if unadjusted mouse
  221. // event is not not implemented or failed. On some platforms this function may
  222. // temporarily affect the global state of mouse settings. This function is
  223. // currently only intended to be used with PointerLock as it is not set up for
  224. // multiple calls.
  225. virtual std::unique_ptr<ScopedEnableUnadjustedMouseEvents>
  226. RequestUnadjustedMovement();
  227. // Whether or not the underlying platform supports native pointer locking.
  228. virtual bool SupportsMouseLock();
  229. virtual void LockMouse(Window* window);
  230. virtual void UnlockMouse(Window* window);
  231. // See VideoCaptureLock for details. This may return null.
  232. std::unique_ptr<VideoCaptureLock> CreateVideoCaptureLock();
  233. bool holding_pointer_moves() const { return holding_pointer_moves_; }
  234. #if BUILDFLAG(IS_WIN)
  235. // Returns whether a host's window is on the current workspace or not,
  236. // absl::nullopt if the state is not known.
  237. absl::optional<bool> on_current_workspace() const {
  238. return on_current_workspace_;
  239. };
  240. // Determining if a host's window is on the current workspace can be very
  241. // expensive COM call on Windows, so this caches that information.
  242. void set_on_current_workspace(absl::optional<bool> on_current_workspace) {
  243. on_current_workspace_ = on_current_workspace;
  244. }
  245. #endif // BUILDFLAG_(IS_WIN)
  246. protected:
  247. friend class ScopedKeyboardHook;
  248. friend class TestScreen; // TODO(beng): see if we can remove/consolidate.
  249. explicit WindowTreeHost(std::unique_ptr<Window> window = nullptr);
  250. // All calls to changing the visibility of the Compositor funnel into this.
  251. // In addition to changing the visibility this may also evict the root frame.
  252. void UpdateCompositorVisibility(bool visible);
  253. void DestroyCompositor();
  254. void DestroyDispatcher();
  255. // Sets whether the accelerated widget has been made visible. This is called
  256. // when platform specific api has been called to make the widget visible. The
  257. // widget is not necessarily shown/drawn (it may be occluded or minimized),
  258. // but from the OSs perspective, the window may be shown to the user.
  259. //
  260. // This is called from Show(), subclasses that do not call Show() must call
  261. // this.
  262. void OnAcceleratedWidgetMadeVisible(bool value);
  263. void CreateCompositor(bool force_software_compositor = false,
  264. bool use_external_begin_frame_control = false,
  265. bool enable_compositing_based_throttling = false,
  266. size_t memory_limit_when_visible_mb = 0);
  267. void InitCompositor();
  268. void OnAcceleratedWidgetAvailable();
  269. // Returns the location of the RootWindow on native screen.
  270. virtual gfx::Point GetLocationOnScreenInPixels() const = 0;
  271. void OnHostMovedInPixels();
  272. void OnHostResizedInPixels(const gfx::Size& new_size_in_pixels);
  273. void OnHostWorkspaceChanged();
  274. void OnHostDisplayChanged();
  275. void OnHostCloseRequested();
  276. void OnHostLostWindowCapture();
  277. // Sets the currently displayed cursor.
  278. virtual void SetCursorNative(gfx::NativeCursor cursor) = 0;
  279. // Moves the cursor to the specified location relative to the root window.
  280. virtual void MoveCursorToScreenLocationInPixels(
  281. const gfx::Point& location_in_pixels) = 0;
  282. // Called when the cursor visibility has changed.
  283. virtual void OnCursorVisibilityChangedNative(bool show) = 0;
  284. // Shows the WindowTreeHost.
  285. virtual void ShowImpl() = 0;
  286. // Hides the WindowTreeHost.
  287. virtual void HideImpl() = 0;
  288. // display::DisplayObserver implementation.
  289. void OnDisplayMetricsChanged(const display::Display& display,
  290. uint32_t metrics) override;
  291. // Begins capturing system key events. Returns true if successful.
  292. virtual bool CaptureSystemKeyEventsImpl(
  293. absl::optional<base::flat_set<ui::DomCode>> dom_codes) = 0;
  294. // Stops capturing system keyboard events.
  295. virtual void ReleaseSystemKeyEventCapture() = 0;
  296. // True if |dom_code| is reserved for an active KeyboardLock request.
  297. virtual bool IsKeyLocked(ui::DomCode dom_code) = 0;
  298. // Return root window size computed from given pixel size.
  299. virtual gfx::Rect GetTransformedRootWindowBoundsFromPixelSize(
  300. const gfx::Size& size_in_pixels) const;
  301. const base::ObserverList<WindowTreeHostObserver>::Unchecked& observers()
  302. const {
  303. return observers_;
  304. }
  305. // Called to enabled/disable native window occlusion calculation.
  306. void SetNativeWindowOcclusionEnabled(bool enable);
  307. // Updates the root window's size after WindowTreeHost's property changed.
  308. void UpdateRootWindowSize();
  309. // Calculates the root window bounds to be used by UpdateRootwindowSize().
  310. virtual gfx::Rect CalculateRootWindowBounds() const;
  311. private:
  312. class HideHelper;
  313. friend class HideHelper;
  314. friend class test::WindowTreeHostTestApi;
  315. void DecrementVideoCaptureCount();
  316. void MaybeUpdateComposibleVisibilityForVideoLockCountChange();
  317. bool CalculateCompositorVisibilityFromOcclusionState() const;
  318. // See `kApplyNativeOcclusionToCompositorTypeRelease` for details.
  319. bool ShouldReleaseResourcesWhenHidden() const;
  320. // See `kApplyNativeOcclusionToCompositorTypeThrottle` for details.
  321. bool ShouldThrottleWhenOccluded() const;
  322. // Starts the steps necessary to release viz resources and hide.
  323. void StartReleasingResourcesForHide();
  324. // Restores temporary state set in StartReleasingResourcesForHide().
  325. void RestoreHideTransitionState();
  326. // Completes a hide initiated to release resources.
  327. void FinishHideTransition();
  328. static const base::flat_set<WindowTreeHost*>& GetThrottledHostsForTesting();
  329. // Returns true if in the process of releasing resources before hiding.
  330. bool is_transitioning_to_hidden() const {
  331. return hide_helper_.get() != nullptr;
  332. }
  333. // Moves the cursor to the specified location. This method is internally used
  334. // by MoveCursorToLocationInDIP() and MoveCursorToLocationInPixels().
  335. void MoveCursorToInternal(const gfx::Point& root_location,
  336. const gfx::Point& host_location);
  337. // Overridden from CompositorObserver:
  338. void OnCompositingEnded(ui::Compositor* compositor) final;
  339. void OnCompositingChildResizing(ui::Compositor* compositor) final;
  340. void OnFrameSinksToThrottleUpdated(
  341. const base::flat_set<viz::FrameSinkId>& ids) final;
  342. // We don't use a std::unique_ptr for |window_| since we need this ptr to be
  343. // valid during its deletion. (Window's dtor notifies observers that may
  344. // attempt to reach back up to access this object which will be valid until
  345. // the end of the dtor).
  346. raw_ptr<Window, DanglingUntriaged> window_; // Owning.
  347. // Keeps track of the occlusion state of the host, and used to send
  348. // notifications to observers when it changes.
  349. Window::OcclusionState occlusion_state_ = Window::OcclusionState::UNKNOWN;
  350. // This is set if we know whether the window is on the current workspace.
  351. // This is useful on Windows, where a COM call is required to determine this,
  352. // which can block the UI. The native window occlusion tracking code already
  353. // figures this out, so it's cheaper to store the fact here.
  354. absl::optional<bool> on_current_workspace_;
  355. SkRegion occluded_region_;
  356. base::ObserverList<WindowTreeHostObserver>::Unchecked observers_;
  357. display::ScopedDisplayObserver display_observer_{this};
  358. std::unique_ptr<WindowEventDispatcher> dispatcher_;
  359. std::unique_ptr<ui::Compositor> compositor_;
  360. // The device scale factor is snapshotted in OnHostResizedInPixels.
  361. // NOTE: this value is cached rather than looked up from the Display as it is
  362. // entirely possible for the Display to be updated *after* |this|. For
  363. // example, display changes on Windows first result in the HWND bounds
  364. // changing and are then followed by changes to the set of displays
  365. //
  366. // TODO(ccameron): The size and location from OnHostResizedInPixels and
  367. // OnHostMovedInPixels should be snapshotted here as well.
  368. float device_scale_factor_ = 1.f;
  369. // Last cursor set. Used for testing.
  370. gfx::NativeCursor last_cursor_ = ui::mojom::CursorType::kNull;
  371. gfx::Point last_cursor_request_position_in_host_;
  372. std::unique_ptr<ui::ViewProp> prop_;
  373. std::unique_ptr<ui::InputMethod> input_method_owned_;
  374. // The InputMethod instance used to process key events.
  375. // If owned it, it is created in GetInputMethod() method;
  376. // If not owned it, it is passed in through SetSharedInputMethod() method.
  377. raw_ptr<ui::InputMethod> input_method_ = nullptr;
  378. // Set to true if this WindowTreeHost is currently holding pointer moves.
  379. bool holding_pointer_moves_ = false;
  380. // Set to true if native window occlusion should be calculated.
  381. bool native_window_occlusion_enabled_ = false;
  382. bool accelerated_widget_made_visible_ = false;
  383. // Number of VideoCaptureLocks that have been created and not destroyed.
  384. int video_capture_count_ = 0;
  385. // Used to set up and restore state necessary to release resources when
  386. // hiding. Non-null while waiting for state to be released (transitioning).
  387. std::unique_ptr<HideHelper> hide_helper_;
  388. base::WeakPtrFactory<WindowTreeHost> weak_factory_{this};
  389. };
  390. } // namespace aura
  391. #endif // UI_AURA_WINDOW_TREE_HOST_H_