| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 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 "mojo/services/view_manager/root_view_manager.h" | |
| 6 | |
| 7 #include "base/auto_reset.h" | |
| 8 #include "base/scoped_observer.h" | |
| 9 #include "mojo/public/cpp/application/application_connection.h" | |
| 10 #include "mojo/services/public/interfaces/gpu/gpu.mojom.h" | |
| 11 #include "mojo/services/view_manager/root_node_manager.h" | |
| 12 #include "mojo/services/view_manager/root_view_manager_delegate.h" | |
| 13 #include "mojo/services/view_manager/screen_impl.h" | |
| 14 #include "mojo/services/view_manager/window_tree_host_impl.h" | |
| 15 #include "ui/aura/client/default_capture_client.h" | |
| 16 #include "ui/aura/client/focus_client.h" | |
| 17 #include "ui/aura/client/window_tree_client.h" | |
| 18 #include "ui/aura/window.h" | |
| 19 #include "ui/aura/window_delegate.h" | |
| 20 #include "ui/base/cursor/cursor.h" | |
| 21 #include "ui/base/hit_test.h" | |
| 22 #include "ui/compositor/layer.h" | |
| 23 #include "ui/gfx/canvas.h" | |
| 24 #include "ui/gfx/image/image_skia.h" | |
| 25 #include "ui/gfx/native_widget_types.h" | |
| 26 | |
| 27 namespace mojo { | |
| 28 namespace service { | |
| 29 namespace { | |
| 30 | |
| 31 gfx::Rect ConvertRectToRoot(const Node* node, const gfx::Rect& bounds) { | |
| 32 gfx::Point origin(bounds.origin()); | |
| 33 while (node->parent()) { | |
| 34 origin += node->bounds().OffsetFromOrigin(); | |
| 35 node = node->parent(); | |
| 36 } | |
| 37 return gfx::Rect(origin, bounds.size()); | |
| 38 } | |
| 39 | |
| 40 void PaintNodeTree(gfx::Canvas* canvas, | |
| 41 const Node* node, | |
| 42 const gfx::Point& origin) { | |
| 43 if (!node->visible()) | |
| 44 return; | |
| 45 | |
| 46 canvas->DrawImageInt(gfx::ImageSkia::CreateFrom1xBitmap(node->bitmap()), | |
| 47 origin.x(), origin.y()); | |
| 48 std::vector<const Node*> children(node->GetChildren()); | |
| 49 for (size_t i = 0; i < children.size(); ++i) { | |
| 50 PaintNodeTree(canvas, children[i], | |
| 51 origin + children[i]->bounds().OffsetFromOrigin()); | |
| 52 } | |
| 53 } | |
| 54 | |
| 55 } // namespace | |
| 56 | |
| 57 class RootViewManager::RootWindowDelegateImpl : public aura::WindowDelegate { | |
| 58 public: | |
| 59 explicit RootWindowDelegateImpl(const Node* root_node) | |
| 60 : root_node_(root_node) {} | |
| 61 virtual ~RootWindowDelegateImpl() {} | |
| 62 | |
| 63 // aura::WindowDelegate: | |
| 64 virtual gfx::Size GetMinimumSize() const OVERRIDE { | |
| 65 return gfx::Size(); | |
| 66 } | |
| 67 virtual gfx::Size GetMaximumSize() const OVERRIDE { | |
| 68 return gfx::Size(); | |
| 69 } | |
| 70 virtual void OnBoundsChanged(const gfx::Rect& old_bounds, | |
| 71 const gfx::Rect& new_bounds) OVERRIDE { | |
| 72 } | |
| 73 virtual gfx::NativeCursor GetCursor(const gfx::Point& point) OVERRIDE { | |
| 74 return gfx::kNullCursor; | |
| 75 } | |
| 76 virtual int GetNonClientComponent(const gfx::Point& point) const OVERRIDE { | |
| 77 return HTCAPTION; | |
| 78 } | |
| 79 virtual bool ShouldDescendIntoChildForEventHandling( | |
| 80 aura::Window* child, | |
| 81 const gfx::Point& location) OVERRIDE { | |
| 82 return true; | |
| 83 } | |
| 84 virtual bool CanFocus() OVERRIDE { | |
| 85 return true; | |
| 86 } | |
| 87 virtual void OnCaptureLost() OVERRIDE { | |
| 88 } | |
| 89 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE { | |
| 90 PaintNodeTree(canvas, root_node_, gfx::Point()); | |
| 91 } | |
| 92 virtual void OnDeviceScaleFactorChanged(float device_scale_factor) OVERRIDE { | |
| 93 } | |
| 94 virtual void OnWindowDestroying(aura::Window* window) OVERRIDE { | |
| 95 } | |
| 96 virtual void OnWindowDestroyed(aura::Window* window) OVERRIDE { | |
| 97 } | |
| 98 virtual void OnWindowTargetVisibilityChanged(bool visible) OVERRIDE { | |
| 99 } | |
| 100 virtual bool HasHitTestMask() const OVERRIDE { | |
| 101 return false; | |
| 102 } | |
| 103 virtual void GetHitTestMask(gfx::Path* mask) const OVERRIDE { | |
| 104 } | |
| 105 | |
| 106 private: | |
| 107 const Node* root_node_; | |
| 108 | |
| 109 DISALLOW_COPY_AND_ASSIGN(RootWindowDelegateImpl); | |
| 110 }; | |
| 111 | |
| 112 // TODO(sky): Remove once aura is removed from the service. | |
| 113 class FocusClientImpl : public aura::client::FocusClient { | |
| 114 public: | |
| 115 FocusClientImpl() {} | |
| 116 virtual ~FocusClientImpl() {} | |
| 117 | |
| 118 private: | |
| 119 // Overridden from aura::client::FocusClient: | |
| 120 virtual void AddObserver( | |
| 121 aura::client::FocusChangeObserver* observer) OVERRIDE {} | |
| 122 virtual void RemoveObserver( | |
| 123 aura::client::FocusChangeObserver* observer) OVERRIDE {} | |
| 124 virtual void FocusWindow(aura::Window* window) OVERRIDE {} | |
| 125 virtual void ResetFocusWithinActiveWindow(aura::Window* window) OVERRIDE {} | |
| 126 virtual aura::Window* GetFocusedWindow() OVERRIDE { return NULL; } | |
| 127 | |
| 128 DISALLOW_COPY_AND_ASSIGN(FocusClientImpl); | |
| 129 }; | |
| 130 | |
| 131 class WindowTreeClientImpl : public aura::client::WindowTreeClient { | |
| 132 public: | |
| 133 explicit WindowTreeClientImpl(aura::Window* window) : window_(window) { | |
| 134 aura::client::SetWindowTreeClient(window_, this); | |
| 135 } | |
| 136 | |
| 137 virtual ~WindowTreeClientImpl() { | |
| 138 aura::client::SetWindowTreeClient(window_, NULL); | |
| 139 } | |
| 140 | |
| 141 // Overridden from aura::client::WindowTreeClient: | |
| 142 virtual aura::Window* GetDefaultParent(aura::Window* context, | |
| 143 aura::Window* window, | |
| 144 const gfx::Rect& bounds) OVERRIDE { | |
| 145 if (!capture_client_) { | |
| 146 capture_client_.reset( | |
| 147 new aura::client::DefaultCaptureClient(window_->GetRootWindow())); | |
| 148 } | |
| 149 return window_; | |
| 150 } | |
| 151 | |
| 152 private: | |
| 153 aura::Window* window_; | |
| 154 | |
| 155 scoped_ptr<aura::client::DefaultCaptureClient> capture_client_; | |
| 156 | |
| 157 DISALLOW_COPY_AND_ASSIGN(WindowTreeClientImpl); | |
| 158 }; | |
| 159 | |
| 160 RootViewManager::RootViewManager( | |
| 161 ApplicationConnection* app_connection, | |
| 162 RootNodeManager* root_node, | |
| 163 RootViewManagerDelegate* delegate, | |
| 164 const Callback<void()>& native_viewport_closed_callback) | |
| 165 : delegate_(delegate), | |
| 166 root_node_manager_(root_node), | |
| 167 in_setup_(false), | |
| 168 root_window_(NULL) { | |
| 169 screen_.reset(ScreenImpl::Create()); | |
| 170 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, screen_.get()); | |
| 171 NativeViewportPtr viewport; | |
| 172 app_connection->ConnectToService( | |
| 173 "mojo:mojo_native_viewport_service", &viewport); | |
| 174 GpuPtr gpu_service; | |
| 175 // TODO(jamesr): Should be mojo:mojo_gpu_service | |
| 176 app_connection->ConnectToService("mojo:mojo_native_viewport_service", | |
| 177 &gpu_service); | |
| 178 window_tree_host_.reset(new WindowTreeHostImpl( | |
| 179 viewport.Pass(), | |
| 180 gpu_service.Pass(), | |
| 181 gfx::Rect(800, 600), | |
| 182 base::Bind(&RootViewManager::OnCompositorCreated, base::Unretained(this)), | |
| 183 native_viewport_closed_callback, | |
| 184 base::Bind(&RootNodeManager::DispatchNodeInputEventToWindowManager, | |
| 185 base::Unretained(root_node_manager_)))); | |
| 186 } | |
| 187 | |
| 188 RootViewManager::~RootViewManager() { | |
| 189 window_tree_client_.reset(); | |
| 190 window_tree_host_.reset(); | |
| 191 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, NULL); | |
| 192 } | |
| 193 | |
| 194 void RootViewManager::SchedulePaint(const Node* node, const gfx::Rect& bounds) { | |
| 195 if (root_window_) | |
| 196 root_window_->SchedulePaintInRect(ConvertRectToRoot(node, bounds)); | |
| 197 } | |
| 198 | |
| 199 void RootViewManager::OnCompositorCreated() { | |
| 200 base::AutoReset<bool> resetter(&in_setup_, true); | |
| 201 window_tree_host_->InitHost(); | |
| 202 | |
| 203 window_delegate_.reset( | |
| 204 new RootWindowDelegateImpl(root_node_manager_->root())); | |
| 205 root_window_ = new aura::Window(window_delegate_.get()); | |
| 206 root_window_->Init(aura::WINDOW_LAYER_TEXTURED); | |
| 207 root_window_->Show(); | |
| 208 root_window_->SetBounds( | |
| 209 gfx::Rect(window_tree_host_->window()->bounds().size())); | |
| 210 window_tree_host_->window()->AddChild(root_window_); | |
| 211 | |
| 212 root_node_manager_->root()->SetBounds( | |
| 213 gfx::Rect(window_tree_host_->window()->bounds().size())); | |
| 214 | |
| 215 window_tree_client_.reset( | |
| 216 new WindowTreeClientImpl(window_tree_host_->window())); | |
| 217 | |
| 218 focus_client_.reset(new FocusClientImpl); | |
| 219 aura::client::SetFocusClient(window_tree_host_->window(), | |
| 220 focus_client_.get()); | |
| 221 | |
| 222 window_tree_host_->Show(); | |
| 223 | |
| 224 delegate_->OnRootViewManagerWindowTreeHostCreated(); | |
| 225 } | |
| 226 | |
| 227 } // namespace service | |
| 228 } // namespace mojo | |
| OLD | NEW |