OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "mojo/services/view_manager/display_manager.h" | 5 #include "mojo/services/view_manager/display_manager.h" |
6 | 6 |
7 #include "base/auto_reset.h" | 7 #include "cc/surfaces/surface_id_allocator.h" |
8 #include "base/scoped_observer.h" | |
9 #include "mojo/public/cpp/application/application_connection.h" | 8 #include "mojo/public/cpp/application/application_connection.h" |
| 9 #include "mojo/services/public/cpp/geometry/geometry_type_converters.h" |
| 10 #include "mojo/services/public/cpp/surfaces/surfaces_type_converters.h" |
| 11 #include "mojo/services/public/cpp/surfaces/surfaces_utils.h" |
10 #include "mojo/services/public/interfaces/gpu/gpu.mojom.h" | 12 #include "mojo/services/public/interfaces/gpu/gpu.mojom.h" |
| 13 #include "mojo/services/public/interfaces/surfaces/quads.mojom.h" |
11 #include "mojo/services/view_manager/connection_manager.h" | 14 #include "mojo/services/view_manager/connection_manager.h" |
12 #include "mojo/services/view_manager/display_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 | 15 |
27 namespace mojo { | 16 namespace mojo { |
28 namespace service { | 17 namespace service { |
29 namespace { | 18 namespace { |
30 | 19 |
31 gfx::Rect ConvertRectToRoot(const ServerView* view, const gfx::Rect& bounds) { | 20 gfx::Rect ConvertRectToRoot(const ServerView* view, const gfx::Rect& bounds) { |
32 gfx::Point origin(bounds.origin()); | 21 gfx::Point origin(bounds.origin()); |
33 while (view->parent()) { | 22 while (view->parent()) { |
34 origin += view->bounds().OffsetFromOrigin(); | 23 origin += view->bounds().OffsetFromOrigin(); |
35 view = view->parent(); | 24 view = view->parent(); |
| 25 if (!view->visible()) |
| 26 return gfx::Rect(); |
36 } | 27 } |
37 return gfx::Rect(origin, bounds.size()); | 28 return gfx::Rect(origin, bounds.size()); |
38 } | 29 } |
39 | 30 |
40 void PaintViewTree(gfx::Canvas* canvas, | 31 void DrawViewTree(Pass* pass, const ServerView* view) { |
41 const ServerView* view, | |
42 const gfx::Point& origin) { | |
43 if (!view->visible()) | 32 if (!view->visible()) |
44 return; | 33 return; |
45 | 34 |
46 canvas->DrawImageInt(gfx::ImageSkia::CreateFrom1xBitmap(view->bitmap()), | 35 cc::SurfaceId node_id = view->surface_id(); |
47 origin.x(), | 36 |
48 origin.y()); | 37 SurfaceQuadStatePtr surface_quad_state = SurfaceQuadState::New(); |
| 38 surface_quad_state->surface = SurfaceId::From(node_id); |
| 39 |
| 40 const gfx::Rect& node_bounds = view->bounds(); |
| 41 gfx::Transform node_transform; |
| 42 node_transform.Translate(node_bounds.x(), node_bounds.y()); |
| 43 |
| 44 QuadPtr surface_quad = Quad::New(); |
| 45 surface_quad->material = Material::MATERIAL_SURFACE_CONTENT; |
| 46 surface_quad->rect = Rect::From(node_bounds); |
| 47 surface_quad->opaque_rect = Rect::From(node_bounds); |
| 48 surface_quad->visible_rect = Rect::From(node_bounds); |
| 49 surface_quad->needs_blending = true; |
| 50 surface_quad->shared_quad_state_index = pass->shared_quad_states.size(); |
| 51 surface_quad->surface_quad_state = surface_quad_state.Pass(); |
| 52 |
| 53 SharedQuadStatePtr sqs = CreateDefaultSQS(node_bounds.size()); |
| 54 sqs->content_to_target_transform = Transform::From(node_transform); |
| 55 |
| 56 pass->quads.push_back(surface_quad.Pass()); |
| 57 pass->shared_quad_states.push_back(sqs.Pass()); |
| 58 |
49 std::vector<const ServerView*> children(view->GetChildren()); | 59 std::vector<const ServerView*> children(view->GetChildren()); |
50 for (size_t i = 0; i < children.size(); ++i) { | 60 for (size_t i = 0; i < children.size(); ++i) { |
51 PaintViewTree( | 61 DrawViewTree(pass, children[i]); |
52 canvas, children[i], origin + children[i]->bounds().OffsetFromOrigin()); | |
53 } | 62 } |
54 } | 63 } |
55 | 64 |
56 } // namespace | 65 } // namespace |
57 | 66 |
58 class DisplayManager::RootWindowDelegateImpl : public aura::WindowDelegate { | |
59 public: | |
60 explicit RootWindowDelegateImpl(const ServerView* root_view) | |
61 : root_view_(root_view) {} | |
62 virtual ~RootWindowDelegateImpl() {} | |
63 | |
64 // aura::WindowDelegate: | |
65 virtual gfx::Size GetMinimumSize() const OVERRIDE { | |
66 return gfx::Size(); | |
67 } | |
68 virtual gfx::Size GetMaximumSize() const OVERRIDE { | |
69 return gfx::Size(); | |
70 } | |
71 virtual void OnBoundsChanged(const gfx::Rect& old_bounds, | |
72 const gfx::Rect& new_bounds) OVERRIDE { | |
73 } | |
74 virtual gfx::NativeCursor GetCursor(const gfx::Point& point) OVERRIDE { | |
75 return gfx::kNullCursor; | |
76 } | |
77 virtual int GetNonClientComponent(const gfx::Point& point) const OVERRIDE { | |
78 return HTCAPTION; | |
79 } | |
80 virtual bool ShouldDescendIntoChildForEventHandling( | |
81 aura::Window* child, | |
82 const gfx::Point& location) OVERRIDE { | |
83 return true; | |
84 } | |
85 virtual bool CanFocus() OVERRIDE { | |
86 return true; | |
87 } | |
88 virtual void OnCaptureLost() OVERRIDE { | |
89 } | |
90 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE { | |
91 PaintViewTree(canvas, root_view_, gfx::Point()); | |
92 } | |
93 virtual void OnDeviceScaleFactorChanged(float device_scale_factor) OVERRIDE { | |
94 } | |
95 virtual void OnWindowDestroying(aura::Window* window) OVERRIDE { | |
96 } | |
97 virtual void OnWindowDestroyed(aura::Window* window) OVERRIDE { | |
98 } | |
99 virtual void OnWindowTargetVisibilityChanged(bool visible) OVERRIDE { | |
100 } | |
101 virtual bool HasHitTestMask() const OVERRIDE { | |
102 return false; | |
103 } | |
104 virtual void GetHitTestMask(gfx::Path* mask) const OVERRIDE { | |
105 } | |
106 | |
107 private: | |
108 const ServerView* root_view_; | |
109 | |
110 DISALLOW_COPY_AND_ASSIGN(RootWindowDelegateImpl); | |
111 }; | |
112 | |
113 // TODO(sky): Remove once aura is removed from the service. | |
114 class FocusClientImpl : public aura::client::FocusClient { | |
115 public: | |
116 FocusClientImpl() {} | |
117 virtual ~FocusClientImpl() {} | |
118 | |
119 private: | |
120 // Overridden from aura::client::FocusClient: | |
121 virtual void AddObserver( | |
122 aura::client::FocusChangeObserver* observer) OVERRIDE {} | |
123 virtual void RemoveObserver( | |
124 aura::client::FocusChangeObserver* observer) OVERRIDE {} | |
125 virtual void FocusWindow(aura::Window* window) OVERRIDE {} | |
126 virtual void ResetFocusWithinActiveWindow(aura::Window* window) OVERRIDE {} | |
127 virtual aura::Window* GetFocusedWindow() OVERRIDE { return NULL; } | |
128 | |
129 DISALLOW_COPY_AND_ASSIGN(FocusClientImpl); | |
130 }; | |
131 | |
132 class WindowTreeClientImpl : public aura::client::WindowTreeClient { | |
133 public: | |
134 explicit WindowTreeClientImpl(aura::Window* window) : window_(window) { | |
135 aura::client::SetWindowTreeClient(window_, this); | |
136 } | |
137 | |
138 virtual ~WindowTreeClientImpl() { | |
139 aura::client::SetWindowTreeClient(window_, NULL); | |
140 } | |
141 | |
142 // Overridden from aura::client::WindowTreeClient: | |
143 virtual aura::Window* GetDefaultParent(aura::Window* context, | |
144 aura::Window* window, | |
145 const gfx::Rect& bounds) OVERRIDE { | |
146 if (!capture_client_) { | |
147 capture_client_.reset( | |
148 new aura::client::DefaultCaptureClient(window_->GetRootWindow())); | |
149 } | |
150 return window_; | |
151 } | |
152 | |
153 private: | |
154 aura::Window* window_; | |
155 | |
156 scoped_ptr<aura::client::DefaultCaptureClient> capture_client_; | |
157 | |
158 DISALLOW_COPY_AND_ASSIGN(WindowTreeClientImpl); | |
159 }; | |
160 | |
161 DisplayManager::DisplayManager( | 67 DisplayManager::DisplayManager( |
162 ApplicationConnection* app_connection, | 68 ApplicationConnection* app_connection, |
163 ConnectionManager* connection_manager, | 69 ConnectionManager* connection_manager, |
164 DisplayManagerDelegate* delegate, | |
165 const Callback<void()>& native_viewport_closed_callback) | 70 const Callback<void()>& native_viewport_closed_callback) |
166 : delegate_(delegate), | 71 : connection_manager_(connection_manager), |
167 connection_manager_(connection_manager), | |
168 in_setup_(false), | 72 in_setup_(false), |
169 root_window_(NULL) { | 73 bounds_(800, 600), |
170 screen_.reset(ScreenImpl::Create()); | 74 draw_timer_(false, false), |
171 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, screen_.get()); | 75 weak_factory_(this) { |
172 NativeViewportPtr viewport; | |
173 app_connection->ConnectToService( | |
174 "mojo:mojo_native_viewport_service", &viewport); | |
175 GpuPtr gpu_service; | |
176 // TODO(jamesr): Should be mojo:mojo_gpu_service | |
177 app_connection->ConnectToService("mojo:mojo_native_viewport_service", | 76 app_connection->ConnectToService("mojo:mojo_native_viewport_service", |
178 &gpu_service); | 77 &native_viewport_); |
179 window_tree_host_.reset(new WindowTreeHostImpl( | 78 native_viewport_.set_client(this); |
180 viewport.Pass(), | 79 native_viewport_->Create(Size::From(bounds_)); |
181 gpu_service.Pass(), | 80 native_viewport_->Show(); |
182 gfx::Rect(800, 600), | 81 app_connection->ConnectToService("mojo:mojo_surfaces_service", |
183 base::Bind(&DisplayManager::OnCompositorCreated, base::Unretained(this)), | 82 &surfaces_service_); |
184 native_viewport_closed_callback, | 83 surfaces_service_->CreateSurfaceConnection(base::Bind( |
185 base::Bind(&ConnectionManager::DispatchViewInputEventToWindowManager, | 84 &DisplayManager::OnSurfaceConnectionCreated, weak_factory_.GetWeakPtr())); |
186 base::Unretained(connection_manager_)))); | |
187 } | 85 } |
188 | 86 |
189 DisplayManager::~DisplayManager() { | 87 DisplayManager::~DisplayManager() { |
190 window_tree_client_.reset(); | |
191 window_tree_host_.reset(); | |
192 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, NULL); | |
193 } | 88 } |
194 | 89 |
195 void DisplayManager::SchedulePaint(const ServerView* view, | 90 void DisplayManager::SchedulePaint(const ServerView* view, |
196 const gfx::Rect& bounds) { | 91 const gfx::Rect& bounds) { |
197 if (root_window_) | 92 if (!view->visible()) |
198 root_window_->SchedulePaintInRect(ConvertRectToRoot(view, bounds)); | 93 return; |
| 94 gfx::Rect root_relative_rect = ConvertRectToRoot(view, bounds); |
| 95 if (root_relative_rect.IsEmpty()) |
| 96 return; |
| 97 dirty_rect_.Union(root_relative_rect); |
| 98 if (!draw_timer_.IsRunning()) { |
| 99 draw_timer_.Start( |
| 100 FROM_HERE, |
| 101 base::TimeDelta(), |
| 102 base::Bind(&DisplayManager::Draw, base::Unretained(this))); |
| 103 } |
199 } | 104 } |
200 | 105 |
201 void DisplayManager::OnCompositorCreated() { | 106 void DisplayManager::OnSurfaceConnectionCreated(SurfacePtr surface, |
202 base::AutoReset<bool> resetter(&in_setup_, true); | 107 uint32_t id_namespace) { |
203 window_tree_host_->InitHost(); | 108 surface_ = surface.Pass(); |
| 109 surface_.set_client(this); |
| 110 surface_id_allocator_.reset(new cc::SurfaceIdAllocator(id_namespace)); |
| 111 Draw(); |
| 112 } |
204 | 113 |
205 window_delegate_.reset( | 114 void DisplayManager::Draw() { |
206 new RootWindowDelegateImpl(connection_manager_->root())); | 115 if (!surface_) |
207 root_window_ = new aura::Window(window_delegate_.get()); | 116 return; |
208 root_window_->Init(aura::WINDOW_LAYER_TEXTURED); | 117 if (surface_id_.is_null()) { |
209 root_window_->Show(); | 118 surface_id_ = surface_id_allocator_->GenerateId(); |
210 root_window_->SetBounds( | 119 surface_->CreateSurface(SurfaceId::From(surface_id_), Size::From(bounds_)); |
211 gfx::Rect(window_tree_host_->window()->bounds().size())); | 120 } |
212 window_tree_host_->window()->AddChild(root_window_); | |
213 | 121 |
214 connection_manager_->root()->SetBounds( | 122 PassPtr pass = CreateDefaultPass(1, gfx::Rect(bounds_)); |
215 gfx::Rect(window_tree_host_->window()->bounds().size())); | 123 pass->damage_rect = Rect::From(dirty_rect_); |
216 | 124 |
217 window_tree_client_.reset( | 125 DrawViewTree(pass.get(), connection_manager_->root()); |
218 new WindowTreeClientImpl(window_tree_host_->window())); | |
219 | 126 |
220 focus_client_.reset(new FocusClientImpl); | 127 FramePtr frame = Frame::New(); |
221 aura::client::SetFocusClient(window_tree_host_->window(), | 128 frame->passes.push_back(pass.Pass()); |
222 focus_client_.get()); | 129 frame->resources.resize(0u); |
| 130 surface_->SubmitFrame(SurfaceId::From(surface_id_), frame.Pass()); |
223 | 131 |
224 window_tree_host_->Show(); | 132 native_viewport_->SubmittedFrame(SurfaceId::From(surface_id_)); |
225 | 133 |
226 delegate_->OnDisplayManagerWindowTreeHostCreated(); | 134 dirty_rect_ = gfx::Rect(); |
| 135 } |
| 136 |
| 137 void DisplayManager::OnCreated(uint64_t native_viewport_id) { |
| 138 } |
| 139 |
| 140 void DisplayManager::OnDestroyed() { |
| 141 native_viewport_closed_callback_.Run(); |
| 142 } |
| 143 |
| 144 void DisplayManager::OnBoundsChanged(SizePtr bounds) { |
| 145 bounds_ = bounds.To<gfx::Size>(); |
| 146 connection_manager_->root()->SetBounds(gfx::Rect(bounds_)); |
| 147 if (surface_id_.is_null()) |
| 148 return; |
| 149 surface_->DestroySurface(SurfaceId::From(surface_id_)); |
| 150 surface_id_ = cc::SurfaceId(); |
| 151 SchedulePaint(connection_manager_->root(), gfx::Rect(bounds_)); |
| 152 } |
| 153 |
| 154 void DisplayManager::OnEvent(EventPtr event, |
| 155 const mojo::Callback<void()>& callback) { |
| 156 connection_manager_->DispatchViewInputEventToWindowManager(event.Pass()); |
| 157 callback.Run(); |
| 158 } |
| 159 |
| 160 void DisplayManager::ReturnResources(Array<ReturnedResourcePtr> resources) { |
| 161 DCHECK_EQ(0u, resources.size()); |
227 } | 162 } |
228 | 163 |
229 } // namespace service | 164 } // namespace service |
230 } // namespace mojo | 165 } // namespace mojo |
OLD | NEW |