OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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 |
| 5 #include "chromecast/graphics/cast_window_manager_aura.h" |
| 6 |
| 7 #include "base/memory/ptr_util.h" |
| 8 #include "ui/aura/client/default_capture_client.h" |
| 9 #include "ui/aura/env.h" |
| 10 #include "ui/aura/layout_manager.h" |
| 11 #include "ui/aura/window.h" |
| 12 #include "ui/aura/window_tree_host_platform.h" |
| 13 #include "ui/base/ime/input_method.h" |
| 14 #include "ui/base/ime/input_method_factory.h" |
| 15 #include "ui/display/display.h" |
| 16 #include "ui/display/screen.h" |
| 17 |
| 18 namespace chromecast { |
| 19 |
| 20 // An aura::WindowTreeHost that correctly converts input events. |
| 21 class CastWindowTreeHost : public aura::WindowTreeHostPlatform { |
| 22 public: |
| 23 CastWindowTreeHost(bool enable_input, const gfx::Rect& bounds); |
| 24 ~CastWindowTreeHost() override; |
| 25 |
| 26 // aura::WindowTreeHostPlatform implementation: |
| 27 void DispatchEvent(ui::Event* event) override; |
| 28 |
| 29 private: |
| 30 const bool enable_input_; |
| 31 |
| 32 DISALLOW_COPY_AND_ASSIGN(CastWindowTreeHost); |
| 33 }; |
| 34 |
| 35 CastWindowTreeHost::CastWindowTreeHost(bool enable_input, |
| 36 const gfx::Rect& bounds) |
| 37 : WindowTreeHostPlatform(bounds), enable_input_(enable_input) {} |
| 38 |
| 39 CastWindowTreeHost::~CastWindowTreeHost() {} |
| 40 |
| 41 void CastWindowTreeHost::DispatchEvent(ui::Event* event) { |
| 42 if (!enable_input_) { |
| 43 return; |
| 44 } |
| 45 |
| 46 if (event->IsKeyEvent()) { |
| 47 // Convert a RawKeyDown into a character insertion; otherwise |
| 48 // the WebContents will ignore most keyboard input. |
| 49 GetInputMethod()->DispatchKeyEvent(event->AsKeyEvent()); |
| 50 } else { |
| 51 WindowTreeHostPlatform::DispatchEvent(event); |
| 52 } |
| 53 } |
| 54 |
| 55 // A layout manager owned by the root window. |
| 56 class CastLayoutManager : public aura::LayoutManager { |
| 57 public: |
| 58 CastLayoutManager(); |
| 59 ~CastLayoutManager() override; |
| 60 |
| 61 private: |
| 62 // aura::LayoutManager implementation: |
| 63 void OnWindowResized() override; |
| 64 void OnWindowAddedToLayout(aura::Window* child) override; |
| 65 void OnWillRemoveWindowFromLayout(aura::Window* child) override; |
| 66 void OnWindowRemovedFromLayout(aura::Window* child) override; |
| 67 void OnChildWindowVisibilityChanged(aura::Window* child, |
| 68 bool visible) override; |
| 69 void SetChildBounds(aura::Window* child, |
| 70 const gfx::Rect& requested_bounds) override; |
| 71 |
| 72 DISALLOW_COPY_AND_ASSIGN(CastLayoutManager); |
| 73 }; |
| 74 |
| 75 CastLayoutManager::CastLayoutManager() {} |
| 76 CastLayoutManager::~CastLayoutManager() {} |
| 77 |
| 78 void CastLayoutManager::OnWindowResized() { |
| 79 // Invoked when the root window of the window tree host has been resized. |
| 80 } |
| 81 |
| 82 void CastLayoutManager::OnWindowAddedToLayout(aura::Window* child) { |
| 83 // Invoked when a window is added to the window tree host. |
| 84 } |
| 85 |
| 86 void CastLayoutManager::OnWillRemoveWindowFromLayout(aura::Window* child) { |
| 87 // Invoked when the root window of the window tree host will be removed. |
| 88 } |
| 89 |
| 90 void CastLayoutManager::OnWindowRemovedFromLayout(aura::Window* child) { |
| 91 // Invoked when the root window of the window tree host has been removed. |
| 92 } |
| 93 |
| 94 void CastLayoutManager::OnChildWindowVisibilityChanged(aura::Window* child, |
| 95 bool visible) { |
| 96 // Note: this is invoked for child windows of the root window. |
| 97 } |
| 98 |
| 99 void CastLayoutManager::SetChildBounds(aura::Window* child, |
| 100 const gfx::Rect& requested_bounds) { |
| 101 SetChildBoundsDirect(child, requested_bounds); |
| 102 } |
| 103 |
| 104 // static |
| 105 std::unique_ptr<CastWindowManager> CastWindowManager::Create( |
| 106 bool enable_input) { |
| 107 return base::WrapUnique(new CastWindowManagerAura(enable_input)); |
| 108 } |
| 109 |
| 110 CastWindowManagerAura::CastWindowManagerAura(bool enable_input) |
| 111 : enable_input_(enable_input) {} |
| 112 |
| 113 CastWindowManagerAura::~CastWindowManagerAura() { |
| 114 TearDown(); |
| 115 } |
| 116 |
| 117 void CastWindowManagerAura::Setup() { |
| 118 if (window_tree_host_) { |
| 119 return; |
| 120 } |
| 121 DCHECK(display::Screen::GetScreen()); |
| 122 |
| 123 ui::InitializeInputMethodForTesting(); |
| 124 |
| 125 gfx::Size display_size = |
| 126 display::Screen::GetScreen()->GetPrimaryDisplay().GetSizeInPixel(); |
| 127 LOG(INFO) << "Starting window manager, screen size: " << display_size.width() |
| 128 << "x" << display_size.height(); |
| 129 CHECK(aura::Env::GetInstance()); |
| 130 window_tree_host_.reset( |
| 131 new CastWindowTreeHost(enable_input_, gfx::Rect(display_size))); |
| 132 window_tree_host_->InitHost(); |
| 133 window_tree_host_->window()->SetLayoutManager(new CastLayoutManager()); |
| 134 |
| 135 // Allow seeing through to the hardware video plane: |
| 136 window_tree_host_->compositor()->SetHostHasTransparentBackground(true); |
| 137 window_tree_host_->compositor()->SetBackgroundColor(SK_ColorTRANSPARENT); |
| 138 |
| 139 capture_client_.reset( |
| 140 new aura::client::DefaultCaptureClient(window_tree_host_->window())); |
| 141 |
| 142 CastVSyncSettings::GetInstance()->AddObserver(this); |
| 143 window_tree_host_->compositor()->SetAuthoritativeVSyncInterval( |
| 144 CastVSyncSettings::GetInstance()->GetVSyncInterval()); |
| 145 |
| 146 window_tree_host_->Show(); |
| 147 } |
| 148 |
| 149 void CastWindowManagerAura::TearDown() { |
| 150 if (!window_tree_host_) { |
| 151 return; |
| 152 } |
| 153 CastVSyncSettings::GetInstance()->RemoveObserver(this); |
| 154 capture_client_.reset(); |
| 155 window_tree_host_.reset(); |
| 156 } |
| 157 |
| 158 void CastWindowManagerAura::AddWindow(gfx::NativeView child) { |
| 159 LOG(INFO) << "Adding window: " << child->id() << ": " << child->GetName(); |
| 160 Setup(); |
| 161 |
| 162 DCHECK(child); |
| 163 aura::Window* parent = window_tree_host_->window(); |
| 164 if (!parent->Contains(child)) { |
| 165 parent->AddChild(child); |
| 166 } |
| 167 |
| 168 parent->StackChildAtTop(child); |
| 169 child->SetBounds(window_tree_host_->window()->bounds()); |
| 170 } |
| 171 |
| 172 void CastWindowManagerAura::OnVSyncIntervalChanged(base::TimeDelta interval) { |
| 173 DCHECK(window_tree_host_.get()); |
| 174 window_tree_host_->compositor()->SetAuthoritativeVSyncInterval(interval); |
| 175 } |
| 176 |
| 177 } // namespace chromecast |
OLD | NEW |