window.h 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  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_H_
  5. #define UI_AURA_WINDOW_H_
  6. #include <stdint.h>
  7. #include <map>
  8. #include <memory>
  9. #include <set>
  10. #include <string>
  11. #include <vector>
  12. #include "base/check.h"
  13. #include "base/containers/flat_set.h"
  14. #include "base/memory/raw_ptr.h"
  15. #include "base/memory/weak_ptr.h"
  16. #include "base/observer_list.h"
  17. #include "base/time/time.h"
  18. #include "build/build_config.h"
  19. #include "components/viz/common/surfaces/frame_sink_id.h"
  20. #include "components/viz/common/surfaces/local_surface_id.h"
  21. #include "components/viz/common/surfaces/scoped_surface_id_allocator.h"
  22. #include "components/viz/common/surfaces/subtree_capture_id.h"
  23. #include "components/viz/host/host_frame_sink_client.h"
  24. #include "third_party/abseil-cpp/absl/types/optional.h"
  25. #include "third_party/skia/include/core/SkRegion.h"
  26. #include "ui/aura/aura_export.h"
  27. #include "ui/aura/client/window_types.h"
  28. #include "ui/aura/scoped_window_capture_request.h"
  29. #include "ui/aura/window_observer.h"
  30. #include "ui/base/class_property.h"
  31. #include "ui/base/metadata/metadata_header_macros.h"
  32. #include "ui/compositor/layer_delegate.h"
  33. #include "ui/compositor/layer_owner.h"
  34. #include "ui/compositor/layer_type.h"
  35. #include "ui/events/event_constants.h"
  36. #include "ui/events/event_target.h"
  37. #include "ui/events/event_targeter.h"
  38. #include "ui/events/gestures/gesture_types.h"
  39. #include "ui/gfx/geometry/rect.h"
  40. #include "ui/gfx/native_widget_types.h"
  41. #if BUILDFLAG(IS_APPLE)
  42. #error "This file must not be included on macOS; Chromium Mac doesn't use Aura."
  43. #endif
  44. namespace cc {
  45. class LayerTreeFrameSink;
  46. }
  47. namespace display {
  48. class Display;
  49. }
  50. namespace gfx {
  51. class Transform;
  52. }
  53. namespace ui {
  54. enum class DomCode;
  55. class Layer;
  56. } // namespace ui
  57. namespace viz {
  58. class ParentLocalSurfaceIdAllocator;
  59. class SurfaceId;
  60. }
  61. namespace aura {
  62. class LayoutManager;
  63. class ScopedKeyboardHook;
  64. class ScopedWindowEventTargetingBlocker;
  65. class WindowDelegate;
  66. class WindowTargeter;
  67. class WindowTreeHost;
  68. // Defined in class_property.h (which we do not include)
  69. template <typename T>
  70. using WindowProperty = ui::ClassProperty<T>;
  71. namespace test {
  72. class WindowTestApi;
  73. }
  74. enum class EventTargetingPolicy {
  75. // The target is a valid target for events, but none of its descendants are
  76. // considered.
  77. kTargetOnly,
  78. // The target and its descendants are possible targets. This is the default.
  79. kTargetAndDescendants,
  80. // The target is not a valid target, but its descendants are possible targets.
  81. kDescendantsOnly,
  82. // Neither the target nor its descendants are valid targets.
  83. kNone
  84. };
  85. // Aura window implementation. Interesting events are sent to the
  86. // WindowDelegate.
  87. class AURA_EXPORT Window : public ui::LayerDelegate,
  88. public ui::LayerOwner,
  89. public ui::EventTarget,
  90. public ui::GestureConsumer,
  91. public ui::PropertyHandler,
  92. public ui::metadata::MetaDataProvider,
  93. public viz::HostFrameSinkClient {
  94. public:
  95. METADATA_HEADER_BASE(Window);
  96. // Initial value of id() for newly created windows.
  97. static constexpr int kInitialId = -1;
  98. // Used when stacking windows.
  99. enum StackDirection { STACK_ABOVE, STACK_BELOW };
  100. // These values are persisted to logs. Entries should not be renumbered and
  101. // numeric values should never be reused.
  102. enum class OcclusionState {
  103. // The window's occlusion state isn't tracked (Window::TrackOcclusionState)
  104. // or hasn't been computed yet.
  105. UNKNOWN = 0,
  106. // The window or one of its descendants IsVisible() [1] and:
  107. // - Its bounds aren't completely covered by fully opaque windows [2], or,
  108. // - Its transform, bounds or opacity is animated.
  109. VISIBLE = 1,
  110. // The window or one of its descendants IsVisible() [1], but they all:
  111. // - Have bounds completely covered by fully opaque windows [2], and,
  112. // - Have no transform, bounds or opacity animation.
  113. OCCLUDED = 2,
  114. // The window is not IsVisible() [1].
  115. HIDDEN = 3,
  116. // [1] A window can only be IsVisible() if all its parent are IsVisible().
  117. // [2] A window is "fully opaque" if:
  118. // - It's visible (IsVisible()).
  119. // - It's not transparent (transparent()).
  120. // - It's transform, bounds and opacity aren't animated.
  121. // - Its combined opacity is 1 (GetCombinedOpacity()).
  122. // - It has content to draw. Either the type of its layer is not
  123. // ui::LAYER_NOT_DRAWN, or it is a server window hosting remote client
  124. // content in Window Service.
  125. //
  126. // TODO(fdoray): A window that clips its children shouldn't be VISIBLE just
  127. // because it has an animated child.
  128. kMaxValue = HIDDEN,
  129. };
  130. using Windows = std::vector<Window*>;
  131. explicit Window(WindowDelegate* delegate,
  132. client::WindowType type = client::WINDOW_TYPE_UNKNOWN);
  133. Window(const Window&) = delete;
  134. Window& operator=(const Window&) = delete;
  135. ~Window() override;
  136. // Initializes the window. This creates the window's layer.
  137. void Init(ui::LayerType layer_type);
  138. bool is_destroying() const { return is_destroying_; }
  139. void set_owned_by_parent(bool owned_by_parent) {
  140. owned_by_parent_ = owned_by_parent;
  141. }
  142. bool owned_by_parent() const { return owned_by_parent_; }
  143. // A type is used to identify a class of Windows and customize behavior such
  144. // as event handling and parenting. This field should only be consumed by the
  145. // shell -- Aura itself shouldn't contain type-specific logic.
  146. client::WindowType GetType() const;
  147. void SetType(client::WindowType type);
  148. int GetId() const;
  149. void SetId(int id);
  150. // ui::GestureConsumer:
  151. const std::string& GetName() const override;
  152. void SetName(const std::string& name);
  153. const std::u16string& GetTitle() const;
  154. void SetTitle(const std::u16string& title);
  155. bool GetTransparent() const;
  156. // Note: Setting a window transparent has significant performance impact,
  157. // especially on low-end Chrome OS devices. Please ensure you are not
  158. // adding unnecessary overdraw. When in doubt, talk to the graphics team.
  159. void SetTransparent(bool transparent);
  160. // See description in Layer::SetFillsBoundsCompletely.
  161. void SetFillsBoundsCompletely(bool fills_bounds);
  162. WindowDelegate* delegate() { return delegate_; }
  163. const WindowDelegate* delegate() const { return delegate_; }
  164. const gfx::Rect& bounds() const { return bounds_; }
  165. Window* parent() { return parent_; }
  166. const Window* parent() const { return parent_; }
  167. // Returns the root Window that contains this Window. The root Window is
  168. // defined as the Window that has a dispatcher. These functions return nullptr
  169. // if the Window is contained in a hierarchy that does not have a dispatcher
  170. // at its root.
  171. Window* GetRootWindow();
  172. const Window* GetRootWindow() const;
  173. WindowTreeHost* GetHost();
  174. const WindowTreeHost* GetHost() const;
  175. void set_host(WindowTreeHost* host) { host_ = host; }
  176. bool IsRootWindow() const { return !!host_; }
  177. // Changes the visibility of the window.
  178. void Show();
  179. void Hide();
  180. // Returns true if this window and all its ancestors are visible.
  181. bool IsVisible() const;
  182. // Returns the visibility requested by this window. IsVisible() takes into
  183. // account the visibility of the layer and ancestors, where as this tracks
  184. // whether Show() without a Hide() has been invoked.
  185. bool TargetVisibility() const { return visible_; }
  186. // Returns the occlusion state of this window. Is UNKNOWN if the occlusion
  187. // state of this window isn't tracked (Window::TrackOcclusionState) or
  188. // hasn't been computed yet. Is stale if called within the scope of a
  189. // WindowOcclusionTracker::ScopedPause.
  190. OcclusionState GetOcclusionState() const;
  191. // Returns the currently occluded region in the root Window coordinates. This
  192. // will be empty unless the window is tracked and has a VISIBLE occlusion
  193. // state. That is, this is only maintained when the window is partially
  194. // occluded. Further, this region may extend outside the window bounds. For
  195. // performance reasons, the actual intersection with the window is not
  196. // computed. The occluded region is the set of window rectangles that may
  197. // occlude this window. Note that this means that the occluded region may be
  198. // updated if one of those windows moves, even if the actual intersection of
  199. // the occluded region with this window does not change. Clients may compute
  200. // the actual intersection region if necessary.
  201. const SkRegion& occluded_region_in_root() const {
  202. return occluded_region_in_root_;
  203. }
  204. // Makes this *non-root* window individually capturable by the
  205. // |FrameSinkVideoCapturer| by tagging its layer with a unique
  206. // |viz::SubtreeCaptureId| which will force the layer tree root at this
  207. // window's layer to a render surface that draws into a render pass that is
  208. // identifiable by the capturer using that ID.
  209. //
  210. // Note that this should only be called for non-root windows. Root windows are
  211. // already capturable by the capturer as they're identifiable by their
  212. // |viz::FrameSinkId| and thei associated root render pass, so there's no need
  213. // to call this.
  214. //
  215. // This returns a scoped object associated with this request to make the
  216. // window capturable, since multiple capturers can capture the same window at
  217. // the same time. Once all requests are destroyed, this window will no longer
  218. // be individually capturable, and its layer won't be tagged with a valid
  219. // |viz::SubtreeCaptureId|.
  220. // See https://crbug.com/1143930 for more details.
  221. [[nodiscard]] ScopedWindowCaptureRequest MakeWindowCapturable();
  222. const viz::SubtreeCaptureId& subtree_capture_id() const {
  223. return subtree_capture_id_;
  224. }
  225. // Returns the window's bounds in root window's coordinates. The returned
  226. // value is calculated using the target transform. The target transform is the
  227. // end value of a transform animation. If there is no animation ongoing, the
  228. // target transform is the same as the current transform.
  229. gfx::Rect GetBoundsInRootWindow() const;
  230. // Similar to `GetBoundsInRootWindow()` except that the returned value is
  231. // calculated using the current transform. If there is no animation ongoing,
  232. // this function returns the same value as `GetBoundsInRootWindow()`.
  233. gfx::Rect GetActualBoundsInRootWindow() const;
  234. // Returns the window's bounds in screen coordinates. The returned
  235. // value is calculated using the target transform. The target transform is the
  236. // end value of a transform animation. If there is no animation ongoing, the
  237. // target transform is the same as the current transform.
  238. // How the root window's coordinates is mapped to screen's coordinates
  239. // is platform dependent and defined in the implementation of the
  240. // |aura::client::ScreenPositionClient| interface.
  241. gfx::Rect GetBoundsInScreen() const;
  242. // Similar to `GetBoundsInScreen()` except that the returned value is
  243. // calculated using the current transform. If there is no animation ongoing,
  244. // this function returns the same value as `GetBoundsInScreen()`.
  245. gfx::Rect GetActualBoundsInScreen() const;
  246. void SetTransform(const gfx::Transform& transform);
  247. const gfx::Transform& transform() const;
  248. // Assigns a LayoutManager to size and place child windows.
  249. // The Window takes ownership of the LayoutManager.
  250. void SetLayoutManager(LayoutManager* layout_manager);
  251. LayoutManager* layout_manager() { return layout_manager_.get(); }
  252. // Sets a new event-targeter for the window, and returns the previous
  253. // event-targeter.
  254. std::unique_ptr<WindowTargeter> SetEventTargeter(
  255. std::unique_ptr<WindowTargeter> targeter);
  256. WindowTargeter* targeter() { return targeter_.get(); }
  257. const WindowTargeter* targeter() const { return targeter_.get(); }
  258. // Changes the bounds of the window. If present, the window's parent's
  259. // LayoutManager may adjust the bounds.
  260. void SetBounds(const gfx::Rect& new_bounds);
  261. // Changes the bounds of the window in the screen coordinates.
  262. // If present, the window's parent's LayoutManager may adjust the bounds.
  263. void SetBoundsInScreen(const gfx::Rect& new_bounds_in_screen_coords,
  264. const display::Display& dst_display);
  265. // Returns the target bounds of the window. If the window's layer is
  266. // not animating, it simply returns the current bounds.
  267. gfx::Rect GetTargetBounds() const;
  268. // Forwards directly to the layer. See Layer::ScheduleDraw() for details.
  269. void ScheduleDraw();
  270. // Marks the a portion of window as needing to be painted.
  271. void SchedulePaintInRect(const gfx::Rect& rect);
  272. // Stacks the specified child of this Window at the front of the z-order.
  273. void StackChildAtTop(Window* child);
  274. // Stacks |child| above |target|. Does nothing if |child| is already above
  275. // |target|. Does not stack on top of windows with nullptr layer delegates,
  276. // see WindowTest.StackingMadrigal for details.
  277. void StackChildAbove(Window* child, Window* target);
  278. // Stacks the specified child of this window at the bottom of the z-order.
  279. void StackChildAtBottom(Window* child);
  280. // Stacks |child| below |target|. Does nothing if |child| is already below
  281. // |target|.
  282. void StackChildBelow(Window* child, Window* target);
  283. // Tree operations.
  284. void AddChild(Window* child);
  285. void RemoveChild(Window* child);
  286. const Windows& children() const { return children_; }
  287. // Returns true if this Window contains |other| somewhere in its children.
  288. bool Contains(const Window* other) const;
  289. // Retrieves the first-level child with the specified id, or nullptr if no
  290. // first- level child is found matching |id|.
  291. Window* GetChildById(int id);
  292. const Window* GetChildById(int id) const;
  293. // Converts |point| from |source|'s coordinates to |target|'s. If |source| is
  294. // nullptr, the function returns without modifying |point|. |target| cannot be
  295. // nullptr. Use layers' target transform in coordinate conversions. The target
  296. // transform is the end value of a transform animation. If there is no
  297. // animation ongoing, the target transform is the same as the current
  298. // transform.
  299. static void ConvertPointToTarget(const Window* source,
  300. const Window* target,
  301. gfx::PointF* point);
  302. static void ConvertPointToTarget(const Window* source,
  303. const Window* target,
  304. gfx::Point* point);
  305. static void ConvertRectToTarget(const Window* source,
  306. const Window* target,
  307. gfx::Rect* rect);
  308. // Convert the native |point| in pixels to the target's host's coordiantes if
  309. // source and target have different hosts.
  310. static void ConvertNativePointToTargetHost(const Window* source,
  311. const Window* target,
  312. gfx::PointF* point);
  313. static void ConvertNativePointToTargetHost(const Window* source,
  314. const Window* target,
  315. gfx::Point* point);
  316. // Moves the cursor to the specified location relative to the window.
  317. void MoveCursorTo(const gfx::Point& point_in_window);
  318. // Returns the cursor for the specified point, in window coordinates.
  319. gfx::NativeCursor GetCursor(const gfx::Point& point) const;
  320. // Add/remove observer.
  321. void AddObserver(WindowObserver* observer);
  322. void RemoveObserver(WindowObserver* observer);
  323. bool HasObserver(const WindowObserver* observer) const;
  324. void SetEventTargetingPolicy(EventTargetingPolicy policy);
  325. EventTargetingPolicy event_targeting_policy() const {
  326. return event_targeting_policy_;
  327. }
  328. // Returns true if the |point_in_root| in root window's coordinate falls
  329. // within this window's bounds. Returns false if the window is detached
  330. // from root window.
  331. bool ContainsPointInRoot(const gfx::Point& point_in_root) const;
  332. // Returns true if relative-to-this-Window's-origin |local_point| falls
  333. // within this Window's bounds.
  334. bool ContainsPoint(const gfx::Point& local_point) const;
  335. // Returns the Window that most closely encloses |local_point| for the
  336. // purposes of event targeting.
  337. Window* GetEventHandlerForPoint(const gfx::Point& local_point);
  338. // Returns this window's toplevel window (the highest-up-the-tree ancestor
  339. // that has a delegate set). The toplevel window may be |this|.
  340. Window* GetToplevelWindow();
  341. // Claims focus.
  342. void Focus();
  343. // Returns true if the Window is currently the focused window.
  344. bool HasFocus() const;
  345. // Returns true if the Window can be focused.
  346. bool CanFocus() const;
  347. // Does a capture on the window. This does nothing if the window isn't showing
  348. // (VISIBILITY_SHOWN) or isn't contained in a valid window hierarchy.
  349. void SetCapture();
  350. // Releases a capture.
  351. void ReleaseCapture();
  352. // Returns true if this window has capture.
  353. bool HasCapture();
  354. // Requests that |keys| be intercepted at the platform level and routed
  355. // directly to the web content. If |codes| has no value, all keys will be
  356. // intercepted. Returns a ScopedKeyboardHook instance which stops capturing
  357. // system key events when destroyed.
  358. std::unique_ptr<ScopedKeyboardHook> CaptureSystemKeyEvents(
  359. absl::optional<base::flat_set<ui::DomCode>> codes);
  360. // NativeWidget::[GS]etNativeWindowProperty use strings as keys, and this is
  361. // difficult to change while retaining compatibility with other platforms.
  362. // TODO(benrg): Find a better solution.
  363. void SetNativeWindowProperty(const char* key, void* value);
  364. void* GetNativeWindowProperty(const char* key) const;
  365. // Type of a function to delete a property that this window owns.
  366. // typedef void (*PropertyDeallocator)(int64_t value);
  367. // Overridden from ui::LayerDelegate:
  368. void OnDeviceScaleFactorChanged(float old_device_scale_factor,
  369. float new_device_scale_factor) override;
  370. void UpdateVisualState() override;
  371. // ui::LayerOwner:
  372. std::unique_ptr<ui::Layer> ReleaseLayer() override;
  373. std::unique_ptr<ui::Layer> RecreateLayer() override;
  374. void SetLayer(std::unique_ptr<ui::Layer> layer) override;
  375. #if DCHECK_IS_ON()
  376. // These methods are useful when debugging.
  377. std::string GetDebugInfo() const;
  378. std::string GetWindowHierarchy(int depth) const;
  379. void PrintWindowHierarchy(int depth) const;
  380. #endif
  381. // Returns true if there was state needing to be cleaned up.
  382. bool CleanupGestureState();
  383. // Create a LayerTreeFrameSink for the aura::Window.
  384. std::unique_ptr<cc::LayerTreeFrameSink> CreateLayerTreeFrameSink();
  385. // Gets the current viz::SurfaceId.
  386. viz::SurfaceId GetSurfaceId();
  387. // Forces the window to allocate a new viz::LocalSurfaceId for the next
  388. // CompositorFrame submission in anticipation of a synchronization operation
  389. // that does not involve a resize or a device scale factor change.
  390. void AllocateLocalSurfaceId();
  391. viz::ScopedSurfaceIdAllocator GetSurfaceIdAllocator(
  392. base::OnceCallback<void()> allocation_task);
  393. const viz::LocalSurfaceId& GetLocalSurfaceId();
  394. // Marks the current viz::LocalSurfaceId as invalid. AllocateLocalSurfaceId
  395. // must be called before submitting new CompositorFrames.
  396. void InvalidateLocalSurfaceId();
  397. // Sets the current viz::LocalSurfaceId, in cases where the embedded client
  398. // has allocated one. Also sets child sequence number component of the
  399. // viz::LocalSurfaceId allocator.
  400. void UpdateLocalSurfaceIdFromEmbeddedClient(
  401. const absl::optional<viz::LocalSurfaceId>& local_surface_id);
  402. // Returns the FrameSinkId. In LOCAL mode, this returns a valid FrameSinkId
  403. // only if a LayerTreeFrameSink has been created. In MUS mode, this always
  404. // return a valid FrameSinkId.
  405. const viz::FrameSinkId& GetFrameSinkId() const;
  406. // Use SetEmbedFrameSinkId() when this window is embedding another client.
  407. // See comment for |frame_sink_id_| below for more details.
  408. void SetEmbedFrameSinkId(const viz::FrameSinkId& embed_frame_sink_id);
  409. // Starts occlusion state tracking.
  410. void TrackOcclusionState();
  411. // Notifies observers of the state of a resize loop.
  412. void NotifyResizeLoopStarted();
  413. void NotifyResizeLoopEnded();
  414. // ui::GestureConsumer:
  415. bool RequiresDoubleTapGestureEvents() const override;
  416. // Returns |state| as a string. This is generally only useful for debugging.
  417. static const std::u16string OcclusionStateToString(OcclusionState state);
  418. // Sets the regions of this window to consider opaque when computing the
  419. // occlusion of underneath windows. Opaque regions can only be set for a
  420. // transparent() window, and cannot extend outside of the window bounds.
  421. // Opaque regions are relative to the window, i.e. the top-left corner of the
  422. // window is considered to be the point (0, 0). If
  423. // |opaque_regions_for_occlusion| is empty, the window is considered fully
  424. // transparent. Opaque regions do not affect what parts of the window are
  425. // visible; they only affect occlusion tracking of underneath windows. An
  426. // example use case for this is when a window is made transparent because of
  427. // rounded corners, and therefore does not contribute to occlusion. But,
  428. // almost all of that window could be opaque and we would want it to
  429. // contribute to occlusion. Occlusion tracking can affect rendering, page
  430. // behaviour, and triggering for Picture-in-picture, for example. Clients
  431. // should set the opaque regions for occlusion if they have transparent
  432. // regions, but want to specify that certain areas of them are completely
  433. // opaque. Clients that use the window shape API should also specify their
  434. // shape region as a region for occlusion, if it is opaque. The opaque regions
  435. // for occlusion for a window do not affect occlusion for that window itself,
  436. // only what parts of other windows that window occludes.
  437. // TODO: Currently, we only support one Rect in
  438. // |opaque_regions_for_occlusion|. Supporting multiple Rects will
  439. // enable window shape based occlusion.
  440. void SetOpaqueRegionsForOcclusion(
  441. const std::vector<gfx::Rect>& opaque_regions_for_occlusion);
  442. const std::vector<gfx::Rect>& opaque_regions_for_occlusion() const {
  443. return opaque_regions_for_occlusion_;
  444. }
  445. protected:
  446. // Deletes (or removes if not owned by parent) all child windows. Intended for
  447. // use from the destructor.
  448. void RemoveOrDestroyChildren();
  449. // Overrides from ui::PropertyHandler
  450. void AfterPropertyChange(const void* key, int64_t old_value) override;
  451. private:
  452. friend class DefaultWindowOcclusionChangeBuilder;
  453. friend class HitTestDataProviderAura;
  454. friend class LayoutManager;
  455. friend class PropertyConverter;
  456. friend class ScopedWindowCaptureRequest;
  457. friend class ScopedWindowEventTargetingBlocker;
  458. friend class WindowTargeter;
  459. friend class test::WindowTestApi;
  460. // Handles registering FrameSinkId hierarchy for SetEmbedFrameSinkId() and
  461. // CreateLayerTreeFrameSink().
  462. void SetEmbedFrameSinkIdImpl(const viz::FrameSinkId& frame_sink_id);
  463. // Returns true if the mouse pointer at relative-to-this-Window's-origin
  464. // |local_point| can trigger an event for this Window.
  465. // TODO(beng): A Window can supply a hit-test mask to cause some portions of
  466. // itself to not trigger events, causing the events to fall through to the
  467. // Window behind.
  468. bool HitTest(const gfx::Point& local_point);
  469. // Changes the bounds of the window without condition.
  470. void SetBoundsInternal(const gfx::Rect& new_bounds);
  471. // Updates the visible state of the layer and the Window, but does not make
  472. // visible-state specific changes. Called from Show()/Hide().
  473. void SetVisibleInternal(bool visible);
  474. // Updates the occlusion info of the window.
  475. void SetOcclusionInfo(OcclusionState occlusion_state,
  476. const SkRegion& occluded_region);
  477. // Schedules a paint for the Window's entire bounds.
  478. void SchedulePaint();
  479. // Asks the delegate to paint the window.
  480. void Paint(const ui::PaintContext& context);
  481. // Implementation of RemoveChild(). If |child| is being removed as the result
  482. // of an add, |new_parent| is the new parent |child| is going to be parented
  483. // to.
  484. void RemoveChildImpl(Window* child, Window* new_parent);
  485. // Called when this window's parent has changed.
  486. void OnParentChanged();
  487. // The various stacking functions call into this to do the actual stacking.
  488. void StackChildRelativeTo(Window* child,
  489. Window* target,
  490. StackDirection direction);
  491. // Invoked from StackChildRelativeTo() to stack the layers appropriately
  492. // when stacking |child| relative to |target|.
  493. void StackChildLayerRelativeTo(Window* child,
  494. Window* target,
  495. StackDirection direction);
  496. // Called when this window's stacking order among its siblings is changed.
  497. void OnStackingChanged();
  498. // Notifies observers registered with this Window (and its subtree) when the
  499. // Window has been added or is about to be removed from a RootWindow.
  500. void NotifyRemovingFromRootWindow(Window* new_root);
  501. void NotifyAddedToRootWindow();
  502. // Methods implementing hierarchy change notifications. See WindowObserver for
  503. // more details.
  504. void NotifyWindowHierarchyChange(
  505. const WindowObserver::HierarchyChangeParams& params);
  506. // Notifies this window and its child hierarchy.
  507. void NotifyWindowHierarchyChangeDown(
  508. const WindowObserver::HierarchyChangeParams& params);
  509. // Notifies this window and its parent hierarchy.
  510. void NotifyWindowHierarchyChangeUp(
  511. const WindowObserver::HierarchyChangeParams& params);
  512. // Notifies this window's observers.
  513. void NotifyWindowHierarchyChangeAtReceiver(
  514. const WindowObserver::HierarchyChangeParams& params);
  515. // Methods implementing visibility change notifications. See WindowObserver
  516. // for more details.
  517. void NotifyWindowVisibilityChanged(aura::Window* target, bool visible);
  518. // Notifies this window's observers. Returns false if |this| was deleted
  519. // during the call (by an observer), otherwise true.
  520. bool NotifyWindowVisibilityChangedAtReceiver(aura::Window* target,
  521. bool visible);
  522. // Notifies this window and its child hierarchy. Returns false if
  523. // |this| was deleted during the call (by an observer), otherwise
  524. // true.
  525. bool NotifyWindowVisibilityChangedDown(aura::Window* target, bool visible);
  526. // Notifies this window and its parent hierarchy.
  527. void NotifyWindowVisibilityChangedUp(aura::Window* target, bool visible);
  528. // Overridden from ui::LayerDelegate:
  529. void OnPaintLayer(const ui::PaintContext& context) override;
  530. void OnLayerBoundsChanged(const gfx::Rect& old_bounds,
  531. ui::PropertyChangeReason reason) override;
  532. void OnLayerTransformed(const gfx::Transform& old_transform,
  533. ui::PropertyChangeReason reason) override;
  534. void OnLayerOpacityChanged(ui::PropertyChangeReason reason) override;
  535. void OnLayerAlphaShapeChanged() override;
  536. void OnLayerFillsBoundsOpaquelyChanged(
  537. ui::PropertyChangeReason reason) override;
  538. // Overridden from ui::EventTarget:
  539. bool CanAcceptEvent(const ui::Event& event) override;
  540. EventTarget* GetParentTarget() override;
  541. std::unique_ptr<ui::EventTargetIterator> GetChildIterator() const override;
  542. ui::EventTargeter* GetEventTargeter() override;
  543. void ConvertEventToTarget(const ui::EventTarget* target,
  544. ui::LocatedEvent* event) const override;
  545. gfx::PointF GetScreenLocationF(const ui::LocatedEvent& event) const override;
  546. // viz::HostFrameSinkClient:
  547. void OnFirstSurfaceActivation(const viz::SurfaceInfo& surface_info) override;
  548. void OnFrameTokenChanged(uint32_t frame_token,
  549. base::TimeTicks activation_time) override;
  550. // Updates the layer name based on the window's name and id.
  551. void UpdateLayerName();
  552. void RegisterFrameSinkId();
  553. void UnregisterFrameSinkId();
  554. void UpdateLocalSurfaceId();
  555. const viz::LocalSurfaceId& GetCurrentLocalSurfaceId() const;
  556. bool IsEmbeddingExternalContent() const;
  557. // Called by the constructor of ScopedWindowCaptureRequest to add a request to
  558. // make this non-root window capturable by the FrameSinkVideoCapturer.
  559. void OnScopedWindowCaptureRequestAdded();
  560. // Called by the destructor of ScopedWindowCaptureRequest to remove a request
  561. // to make this non-root window capturable by the FrameSinkVideoCapturer.
  562. void OnScopedWindowCaptureRequestRemoved();
  563. // The following are intended for use by the metadata to access the internals
  564. // of instances of this class. At some point they should be moved to the
  565. // public section and code refactored to use them.
  566. // Break out the separate elements of the Window bounds.
  567. int GetHeight() const;
  568. int GetWidth() const;
  569. int GetX() const;
  570. int GetY() const;
  571. void SetHeight(int height);
  572. void SetWidth(int width);
  573. void SetX(int x);
  574. void SetY(int y);
  575. bool GetCapture() const;
  576. viz::SurfaceId GetSurfaceId() const;
  577. bool GetVisible() const;
  578. void SetVisible(bool visible);
  579. // Bounds of this window relative to the parent. This is cached as the bounds
  580. // of the Layer and Window are not necessarily the same. In particular bounds
  581. // of the Layer are relative to the first ancestor with a Layer, where as this
  582. // is relative to the parent Window.
  583. gfx::Rect bounds_;
  584. raw_ptr<WindowTreeHost> host_ = nullptr;
  585. client::WindowType type_;
  586. // True if this window is being destroyed.
  587. bool is_destroying_ = false;
  588. // True if the Window is owned by its parent - i.e. it will be deleted by its
  589. // parent during its parents destruction.
  590. bool owned_by_parent_ = true;
  591. raw_ptr<WindowDelegate, DanglingUntriaged> delegate_;
  592. // The Window's parent.
  593. raw_ptr<Window> parent_ = nullptr;
  594. // Child windows. Topmost is last.
  595. Windows children_;
  596. // The visibility state of the window as set by Show()/Hide(). This may differ
  597. // from the visibility of the underlying layer, which may remain visible after
  598. // the window is hidden (e.g. to animate its disappearance).
  599. bool visible_ = false;
  600. // Occlusion state of the window.
  601. OcclusionState occlusion_state_ = OcclusionState::UNKNOWN;
  602. // Occluded region of the window in the root window coordiantes.
  603. SkRegion occluded_region_in_root_;
  604. int id_ = kInitialId;
  605. // Whether layer is initialized as non-opaque. Defaults to false.
  606. bool transparent_ = false;
  607. // Whether it's in a process of CleanupGestureState() or not.
  608. bool cleaning_up_gesture_state_ = false;
  609. std::unique_ptr<LayoutManager> layout_manager_;
  610. std::unique_ptr<WindowTargeter> targeter_;
  611. // The opaque regions for occlusion for this window. See comment on
  612. // |SetOpaqueRegionsForOcclusion| for documentation.
  613. std::vector<gfx::Rect> opaque_regions_for_occlusion_;
  614. // Makes the window pass all events through to any windows behind it.
  615. EventTargetingPolicy event_targeting_policy_;
  616. // Used to restore to the original event targeting policy after all event
  617. // targeting blockers on this window are removed.
  618. EventTargetingPolicy restore_event_targeting_policy_;
  619. int event_targeting_blocker_count_ = 0;
  620. base::ReentrantObserverList<WindowObserver, true> observers_;
  621. // Video capturing support ---------------------------------------------------
  622. // A non-root window must be marked with a viz::SubtreeCaptureId so that it
  623. // can be captured by a FrameSinkVideoCapturer. Multiple clients can request
  624. // to capture the same window at the same time. This is the number of those
  625. // requests, which once it goes to zero, we well clear the
  626. // viz::SubtreeCaptureId from the layer associated with this window.
  627. int number_of_capture_requests_ = 0;
  628. // The ID allocated for the layer tree rooted at this window's layer, so that
  629. // it can be uniquely identified by the FrameSinkVideoCapturer. This can only
  630. // be set for non-root windows. Root windows can be captured normally by the
  631. // capturer using their frame sink ID, since those root windows are already
  632. // associated with a root compositor render pass.
  633. viz::SubtreeCaptureId subtree_capture_id_;
  634. // Embedding support ---------------------------------------------------------
  635. // Used to detect changes in device scale factor that require generating a
  636. // new LocalSurfaceId.
  637. float last_device_scale_factor_ = 1.0f;
  638. // The FrameSinkId associated with this window. If this window is embedding
  639. // another client, then this should be set to the FrameSinkId of that client,
  640. // and |embeds_external_client_| is turned on. However, a window can still
  641. // have a valid FrameSinkId without embedding another client, to facilitate
  642. // hit-testing.
  643. viz::FrameSinkId frame_sink_id_;
  644. // Set to true if |frame_sink_id_| has been registered in the Compositor
  645. // associated this this.
  646. bool registered_frame_sink_id_ = false;
  647. // Used by tests to disable registering the FrameSinkId with the Compositor.
  648. bool disable_frame_sink_id_registration_ = false;
  649. // Set to true if SetEmbedFrameSinkId() has been called.
  650. bool embeds_external_client_ = false;
  651. // Used to allocate LocalSurfaceIds when this is embedding external content.
  652. std::unique_ptr<viz::ParentLocalSurfaceIdAllocator>
  653. parent_local_surface_id_allocator_;
  654. #if DCHECK_IS_ON()
  655. // Set to true if CreateLayerTreeFrameSink() was called.
  656. bool created_layer_tree_frame_sink_ = false;
  657. #endif
  658. // Used when this is embedding external content.
  659. base::WeakPtr<cc::LayerTreeFrameSink> frame_sink_;
  660. };
  661. } // namespace aura
  662. #endif // UI_AURA_WINDOW_H_