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