ash_window_tree_host_platform.cc 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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
  3. // found in the LICENSE file.
  4. #include "ash/host/ash_window_tree_host_platform.h"
  5. #include <utility>
  6. #include "ash/host/ash_window_tree_host_delegate.h"
  7. #include "ash/host/root_window_transformer.h"
  8. #include "ash/host/transformer_helper.h"
  9. #include "base/feature_list.h"
  10. #include "base/trace_event/trace_event.h"
  11. #include "ui/aura/null_window_targeter.h"
  12. #include "ui/aura/window.h"
  13. #include "ui/aura/window_tree_host_platform.h"
  14. #include "ui/events/event_sink.h"
  15. #include "ui/events/null_event_targeter.h"
  16. #include "ui/events/ozone/chromeos/cursor_controller.h"
  17. #include "ui/gfx/geometry/insets.h"
  18. #include "ui/gfx/geometry/rect_conversions.h"
  19. #include "ui/gfx/geometry/rect_f.h"
  20. #include "ui/gfx/geometry/transform.h"
  21. #include "ui/ozone/public/ozone_platform.h"
  22. #include "ui/platform_window/platform_window.h"
  23. #include "ui/platform_window/platform_window_init_properties.h"
  24. namespace ash {
  25. class ScopedEnableUnadjustedMouseEventsOzone
  26. : public aura::ScopedEnableUnadjustedMouseEvents {
  27. public:
  28. explicit ScopedEnableUnadjustedMouseEventsOzone(
  29. ui::InputController* input_controller)
  30. : input_controller_(input_controller) {
  31. input_controller_->SuspendMouseAcceleration();
  32. }
  33. ~ScopedEnableUnadjustedMouseEventsOzone() override {
  34. input_controller_->EndMouseAccelerationSuspension();
  35. }
  36. private:
  37. ui::InputController* input_controller_;
  38. };
  39. AshWindowTreeHostPlatform::AshWindowTreeHostPlatform(
  40. ui::PlatformWindowInitProperties properties,
  41. AshWindowTreeHostDelegate* delegate)
  42. : aura::WindowTreeHostPlatform(std::move(properties),
  43. std::make_unique<aura::Window>(nullptr)),
  44. delegate_(delegate),
  45. transformer_helper_(this),
  46. input_controller_(
  47. ui::OzonePlatform::GetInstance()->GetInputController()) {
  48. DCHECK(delegate_);
  49. CommonInit();
  50. }
  51. AshWindowTreeHostPlatform::AshWindowTreeHostPlatform(
  52. std::unique_ptr<ui::PlatformWindow> platform_window,
  53. AshWindowTreeHostDelegate* delegate,
  54. size_t compositor_memory_limit_mb)
  55. : aura::WindowTreeHostPlatform(std::make_unique<aura::Window>(nullptr)),
  56. delegate_(delegate),
  57. transformer_helper_(this) {
  58. DCHECK(delegate_);
  59. CreateCompositor(/* force_software_compositor */ false,
  60. /* use_external_begin_frame_control */ false,
  61. /* enable_compositing_based_throttling */ false,
  62. compositor_memory_limit_mb);
  63. SetPlatformWindow(std::move(platform_window));
  64. CommonInit();
  65. }
  66. AshWindowTreeHostPlatform::~AshWindowTreeHostPlatform() = default;
  67. void AshWindowTreeHostPlatform::ConfineCursorToRootWindow() {
  68. if (!allow_confine_cursor())
  69. return;
  70. gfx::Rect confined_bounds(GetBoundsInPixels().size());
  71. confined_bounds.Inset(transformer_helper_.GetHostInsets());
  72. last_cursor_confine_bounds_in_pixels_ = confined_bounds;
  73. platform_window()->ConfineCursorToBounds(confined_bounds);
  74. }
  75. void AshWindowTreeHostPlatform::ConfineCursorToBoundsInRoot(
  76. const gfx::Rect& bounds_in_root) {
  77. if (!allow_confine_cursor())
  78. return;
  79. gfx::RectF bounds_f(bounds_in_root);
  80. GetRootTransform().TransformRect(&bounds_f);
  81. last_cursor_confine_bounds_in_pixels_ = gfx::ToEnclosingRect(bounds_f);
  82. platform_window()->ConfineCursorToBounds(
  83. last_cursor_confine_bounds_in_pixels_);
  84. }
  85. gfx::Rect AshWindowTreeHostPlatform::GetLastCursorConfineBoundsInPixels()
  86. const {
  87. return last_cursor_confine_bounds_in_pixels_;
  88. }
  89. void AshWindowTreeHostPlatform::UpdateCursorConfig() {
  90. const display::Display* display = delegate_->GetDisplayById(GetDisplayId());
  91. if (!display) {
  92. LOG(ERROR)
  93. << "While updating cursor config, could not find display with id="
  94. << GetDisplayId();
  95. return;
  96. }
  97. // Scale all motion on High-DPI displays.
  98. float scale = display->device_scale_factor();
  99. if (!display->IsInternal())
  100. scale *= 1.2;
  101. ui::CursorController::GetInstance()->SetCursorConfigForWindow(
  102. GetAcceleratedWidget(), display->panel_rotation(), scale);
  103. }
  104. void AshWindowTreeHostPlatform::ClearCursorConfig() {
  105. ui::CursorController::GetInstance()->ClearCursorConfigForWindow(
  106. GetAcceleratedWidget());
  107. }
  108. void AshWindowTreeHostPlatform::UpdateRootWindowSize() {
  109. aura::WindowTreeHostPlatform::UpdateRootWindowSize();
  110. }
  111. void AshWindowTreeHostPlatform::SetRootWindowTransformer(
  112. std::unique_ptr<RootWindowTransformer> transformer) {
  113. transformer_helper_.SetRootWindowTransformer(std::move(transformer));
  114. ConfineCursorToRootWindow();
  115. }
  116. gfx::Insets AshWindowTreeHostPlatform::GetHostInsets() const {
  117. return transformer_helper_.GetHostInsets();
  118. }
  119. aura::WindowTreeHost* AshWindowTreeHostPlatform::AsWindowTreeHost() {
  120. return this;
  121. }
  122. void AshWindowTreeHostPlatform::PrepareForShutdown() {
  123. // Block the root window from dispatching events because it is weird for a
  124. // ScreenPositionClient not to be attached to the root window and for
  125. // ui::EventHandlers to be unable to convert the event's location to screen
  126. // coordinates.
  127. window()->SetEventTargeter(std::make_unique<aura::NullWindowTargeter>());
  128. // Do anything platform specific necessary before shutdown (eg. stop
  129. // listening for configuration XEvents).
  130. platform_window()->PrepareForShutdown();
  131. }
  132. void AshWindowTreeHostPlatform::SetRootTransform(
  133. const gfx::Transform& transform) {
  134. transformer_helper_.SetTransform(transform);
  135. }
  136. gfx::Transform AshWindowTreeHostPlatform::GetRootTransform() const {
  137. return transformer_helper_.GetTransform();
  138. }
  139. gfx::Transform AshWindowTreeHostPlatform::GetInverseRootTransform() const {
  140. return transformer_helper_.GetInverseTransform();
  141. }
  142. gfx::Rect
  143. AshWindowTreeHostPlatform::GetTransformedRootWindowBoundsFromPixelSize(
  144. const gfx::Size& host_size_in_pixels) const {
  145. return transformer_helper_.GetTransformedWindowBounds(host_size_in_pixels);
  146. }
  147. void AshWindowTreeHostPlatform::OnCursorVisibilityChangedNative(bool show) {
  148. SetTapToClickPaused(!show);
  149. }
  150. void AshWindowTreeHostPlatform::SetBoundsInPixels(const gfx::Rect& bounds) {
  151. WindowTreeHostPlatform::SetBoundsInPixels(bounds);
  152. ConfineCursorToRootWindow();
  153. }
  154. void AshWindowTreeHostPlatform::CommonInit() {
  155. transformer_helper_.Init();
  156. }
  157. void AshWindowTreeHostPlatform::SetTapToClickPaused(bool state) {
  158. // Temporarily pause tap-to-click when the cursor is hidden.
  159. ui::OzonePlatform::GetInstance()->GetInputController()->SetTapToClickPaused(
  160. state);
  161. }
  162. std::unique_ptr<aura::ScopedEnableUnadjustedMouseEvents>
  163. AshWindowTreeHostPlatform::RequestUnadjustedMovement() {
  164. return std::make_unique<ScopedEnableUnadjustedMouseEventsOzone>(
  165. input_controller_);
  166. }
  167. void AshWindowTreeHostPlatform::DispatchEvent(ui::Event* event) {
  168. TRACE_EVENT0("input", "AshWindowTreeHostPlatform::DispatchEvent");
  169. if (event->IsLocatedEvent())
  170. TranslateLocatedEvent(event->AsLocatedEvent());
  171. return aura::WindowTreeHostPlatform::DispatchEvent(event);
  172. }
  173. } // namespace ash