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 "services/view_manager/display_manager.h" | 5 #include "services/view_manager/display_manager.h" |
6 | 6 |
7 #include "base/numerics/safe_conversions.h" | 7 #include "base/numerics/safe_conversions.h" |
8 #include "cc/surfaces/surface_id_allocator.h" | |
9 #include "mojo/converters/geometry/geometry_type_converters.h" | 8 #include "mojo/converters/geometry/geometry_type_converters.h" |
10 #include "mojo/converters/surfaces/surfaces_type_converters.h" | 9 #include "mojo/converters/surfaces/surfaces_type_converters.h" |
11 #include "mojo/public/cpp/application/application_connection.h" | 10 #include "mojo/public/cpp/application/application_connection.h" |
12 #include "mojo/public/cpp/application/application_impl.h" | 11 #include "mojo/public/cpp/application/application_impl.h" |
13 #include "mojo/services/gpu/public/interfaces/gpu.mojom.h" | 12 #include "mojo/services/gpu/public/interfaces/gpu.mojom.h" |
14 #include "mojo/services/surfaces/public/cpp/surfaces_utils.h" | 13 #include "mojo/services/surfaces/public/cpp/surfaces_utils.h" |
15 #include "mojo/services/surfaces/public/interfaces/quads.mojom.h" | 14 #include "mojo/services/surfaces/public/interfaces/quads.mojom.h" |
| 15 #include "mojo/services/surfaces/public/interfaces/surfaces.mojom.h" |
16 #include "services/view_manager/connection_manager.h" | 16 #include "services/view_manager/connection_manager.h" |
17 #include "services/view_manager/server_view.h" | 17 #include "services/view_manager/server_view.h" |
18 #include "services/view_manager/view_coordinate_conversions.h" | 18 #include "services/view_manager/view_coordinate_conversions.h" |
19 | 19 |
20 using mojo::Rect; | 20 using mojo::Rect; |
21 using mojo::Size; | 21 using mojo::Size; |
22 | 22 |
23 namespace view_manager { | 23 namespace view_manager { |
24 namespace { | 24 namespace { |
25 | 25 |
26 static uint32_t kLocalSurfaceID = 1u; | |
27 | |
28 void DrawViewTree(mojo::Pass* pass, | 26 void DrawViewTree(mojo::Pass* pass, |
29 const ServerView* view, | 27 const ServerView* view, |
30 const gfx::Vector2d& parent_to_root_origin_offset, | 28 const gfx::Vector2d& parent_to_root_origin_offset, |
31 float opacity) { | 29 float opacity) { |
32 if (!view->visible()) | 30 if (!view->visible()) |
33 return; | 31 return; |
34 | 32 |
35 const gfx::Rect absolute_bounds = | 33 const gfx::Rect absolute_bounds = |
36 view->bounds() + parent_to_root_origin_offset; | 34 view->bounds() + parent_to_root_origin_offset; |
37 std::vector<const ServerView*> children(view->GetChildren()); | 35 std::vector<const ServerView*> children(view->GetChildren()); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 } // namespace | 72 } // namespace |
75 | 73 |
76 DefaultDisplayManager::DefaultDisplayManager( | 74 DefaultDisplayManager::DefaultDisplayManager( |
77 mojo::ApplicationImpl* app_impl, | 75 mojo::ApplicationImpl* app_impl, |
78 mojo::ApplicationConnection* app_connection, | 76 mojo::ApplicationConnection* app_connection, |
79 const mojo::Callback<void()>& native_viewport_closed_callback) | 77 const mojo::Callback<void()>& native_viewport_closed_callback) |
80 : app_impl_(app_impl), | 78 : app_impl_(app_impl), |
81 app_connection_(app_connection), | 79 app_connection_(app_connection), |
82 connection_manager_(nullptr), | 80 connection_manager_(nullptr), |
83 draw_timer_(false, false), | 81 draw_timer_(false, false), |
84 id_namespace_(0u), | |
85 surface_allocated_(false), | |
86 native_viewport_closed_callback_(native_viewport_closed_callback), | 82 native_viewport_closed_callback_(native_viewport_closed_callback), |
87 weak_factory_(this) { | 83 weak_factory_(this) { |
88 metrics_.size = mojo::Size::New(); | 84 metrics_.size = mojo::Size::New(); |
89 metrics_.size->width = 800; | 85 metrics_.size->width = 800; |
90 metrics_.size->height = 600; | 86 metrics_.size->height = 600; |
91 } | 87 } |
92 | 88 |
93 void DefaultDisplayManager::Init(ConnectionManager* connection_manager) { | 89 void DefaultDisplayManager::Init(ConnectionManager* connection_manager) { |
94 connection_manager_ = connection_manager; | 90 connection_manager_ = connection_manager; |
95 app_impl_->ConnectToService("mojo:native_viewport_service", | 91 app_impl_->ConnectToService("mojo:native_viewport_service", |
96 &native_viewport_); | 92 &native_viewport_); |
97 native_viewport_.set_error_handler(this); | 93 native_viewport_.set_error_handler(this); |
98 native_viewport_->Create( | 94 native_viewport_->Create(metrics_.size->Clone(), |
99 metrics_.size->Clone(), | 95 base::Bind(&DefaultDisplayManager::OnMetricsChanged, |
100 base::Bind(&DefaultDisplayManager::OnCreatedNativeViewport, | 96 weak_factory_.GetWeakPtr())); |
101 weak_factory_.GetWeakPtr())); | |
102 native_viewport_->Show(); | 97 native_viewport_->Show(); |
103 | 98 |
104 app_impl_->ConnectToService("mojo:surfaces_service", &surface_); | 99 mojo::ContextProviderPtr context_provider; |
105 surface_->GetIdNamespace(base::Bind(&DefaultDisplayManager::SetIdNamespace, | 100 native_viewport_->GetContextProvider(GetProxy(&context_provider)); |
106 base::Unretained(this))); | 101 mojo::DisplayFactoryPtr display_factory; |
| 102 app_impl_->ConnectToService("mojo:surfaces_service", &display_factory); |
| 103 display_factory->Create(context_provider.Pass(), |
| 104 nullptr, // returner - we never submit resources. |
| 105 GetProxy(&display_)); |
107 | 106 |
108 mojo::NativeViewportEventDispatcherPtr event_dispatcher; | 107 mojo::NativeViewportEventDispatcherPtr event_dispatcher; |
109 app_connection_->ConnectToService(&event_dispatcher); | 108 app_connection_->ConnectToService(&event_dispatcher); |
110 native_viewport_->SetEventDispatcher(event_dispatcher.Pass()); | 109 native_viewport_->SetEventDispatcher(event_dispatcher.Pass()); |
111 } | 110 } |
112 | 111 |
113 DefaultDisplayManager::~DefaultDisplayManager() { | 112 DefaultDisplayManager::~DefaultDisplayManager() { |
114 } | 113 } |
115 | 114 |
116 void DefaultDisplayManager::SchedulePaint(const ServerView* view, | 115 void DefaultDisplayManager::SchedulePaint(const ServerView* view, |
117 const gfx::Rect& bounds) { | 116 const gfx::Rect& bounds) { |
118 if (!view->IsDrawn(connection_manager_->root())) | 117 if (!view->IsDrawn(connection_manager_->root())) |
119 return; | 118 return; |
120 const gfx::Rect root_relative_rect = | 119 const gfx::Rect root_relative_rect = |
121 ConvertRectBetweenViews(view, connection_manager_->root(), bounds); | 120 ConvertRectBetweenViews(view, connection_manager_->root(), bounds); |
122 if (root_relative_rect.IsEmpty()) | 121 if (root_relative_rect.IsEmpty()) |
123 return; | 122 return; |
124 dirty_rect_.Union(root_relative_rect); | 123 dirty_rect_.Union(root_relative_rect); |
125 if (!draw_timer_.IsRunning()) { | 124 WantToDraw(); |
126 draw_timer_.Start( | |
127 FROM_HERE, base::TimeDelta(), | |
128 base::Bind(&DefaultDisplayManager::Draw, base::Unretained(this))); | |
129 } | |
130 } | 125 } |
131 | 126 |
132 void DefaultDisplayManager::SetViewportSize(const gfx::Size& size) { | 127 void DefaultDisplayManager::SetViewportSize(const gfx::Size& size) { |
133 native_viewport_->SetSize(Size::From(size)); | 128 native_viewport_->SetSize(Size::From(size)); |
134 } | 129 } |
135 | 130 |
136 const mojo::ViewportMetrics& DefaultDisplayManager::GetViewportMetrics() { | 131 const mojo::ViewportMetrics& DefaultDisplayManager::GetViewportMetrics() { |
137 return metrics_; | 132 return metrics_; |
138 } | 133 } |
139 | 134 |
140 void DefaultDisplayManager::OnCreatedNativeViewport( | |
141 uint64_t native_viewport_id, | |
142 mojo::ViewportMetricsPtr metrics) { | |
143 OnMetricsChanged(metrics.Pass()); | |
144 } | |
145 | |
146 void DefaultDisplayManager::Draw() { | 135 void DefaultDisplayManager::Draw() { |
147 if (!surface_allocated_) { | |
148 surface_->CreateSurface(kLocalSurfaceID); | |
149 surface_allocated_ = true; | |
150 } | |
151 | |
152 Rect rect; | 136 Rect rect; |
153 rect.width = metrics_.size->width; | 137 rect.width = metrics_.size->width; |
154 rect.height = metrics_.size->height; | 138 rect.height = metrics_.size->height; |
155 auto pass = CreateDefaultPass(1, rect); | 139 auto pass = CreateDefaultPass(1, rect); |
156 pass->damage_rect = Rect::From(dirty_rect_); | 140 pass->damage_rect = Rect::From(dirty_rect_); |
157 | 141 |
158 DrawViewTree(pass.get(), connection_manager_->root(), gfx::Vector2d(), 1.0f); | 142 DrawViewTree(pass.get(), connection_manager_->root(), gfx::Vector2d(), 1.0f); |
159 | 143 |
160 auto frame = mojo::Frame::New(); | 144 auto frame = mojo::Frame::New(); |
161 frame->passes.push_back(pass.Pass()); | 145 frame->passes.push_back(pass.Pass()); |
162 frame->resources.resize(0u); | 146 frame->resources.resize(0u); |
163 surface_->SubmitFrame(kLocalSurfaceID, frame.Pass(), mojo::Closure()); | 147 frame_pending_ = true; |
| 148 display_->SubmitFrame( |
| 149 frame.Pass(), |
| 150 base::Bind(&DefaultDisplayManager::DidDraw, base::Unretained(this))); |
164 dirty_rect_ = gfx::Rect(); | 151 dirty_rect_ = gfx::Rect(); |
| 152 } |
165 | 153 |
166 if (id_namespace_ == 0u) | 154 void DefaultDisplayManager::DidDraw() { |
| 155 frame_pending_ = false; |
| 156 if (!dirty_rect_.IsEmpty()) |
| 157 WantToDraw(); |
| 158 } |
| 159 |
| 160 void DefaultDisplayManager::WantToDraw() { |
| 161 if (draw_timer_.IsRunning() || frame_pending_) |
167 return; | 162 return; |
168 | 163 |
169 auto qualified_id = mojo::SurfaceId::New(); | 164 draw_timer_.Start( |
170 qualified_id->id_namespace = id_namespace_; | 165 FROM_HERE, base::TimeDelta(), |
171 qualified_id->local = kLocalSurfaceID; | 166 base::Bind(&DefaultDisplayManager::Draw, base::Unretained(this))); |
172 native_viewport_->SubmittedFrame(qualified_id.Pass()); | |
173 } | 167 } |
174 | 168 |
175 void DefaultDisplayManager::OnMetricsChanged(mojo::ViewportMetricsPtr metrics) { | 169 void DefaultDisplayManager::OnMetricsChanged(mojo::ViewportMetricsPtr metrics) { |
176 metrics_.size = metrics->size.Clone(); | 170 metrics_.size = metrics->size.Clone(); |
177 metrics_.device_pixel_ratio = metrics->device_pixel_ratio; | 171 metrics_.device_pixel_ratio = metrics->device_pixel_ratio; |
178 gfx::Rect bounds(metrics_.size.To<gfx::Size>()); | 172 gfx::Rect bounds(metrics_.size.To<gfx::Size>()); |
179 connection_manager_->root()->SetBounds(bounds); | 173 connection_manager_->root()->SetBounds(bounds); |
180 connection_manager_->ProcessViewportMetricsChanged(metrics_, *metrics); | 174 connection_manager_->ProcessViewportMetricsChanged(metrics_, *metrics); |
181 if (!surface_allocated_) | |
182 return; | |
183 surface_->DestroySurface(kLocalSurfaceID); | |
184 surface_allocated_ = false; | |
185 SchedulePaint(connection_manager_->root(), bounds); | |
186 native_viewport_->RequestMetrics(base::Bind( | 175 native_viewport_->RequestMetrics(base::Bind( |
187 &DefaultDisplayManager::OnMetricsChanged, weak_factory_.GetWeakPtr())); | 176 &DefaultDisplayManager::OnMetricsChanged, weak_factory_.GetWeakPtr())); |
188 } | 177 } |
189 | 178 |
190 void DefaultDisplayManager::SetIdNamespace(uint32_t id_namespace) { | |
191 id_namespace_ = id_namespace; | |
192 if (surface_allocated_) { | |
193 auto qualified_id = mojo::SurfaceId::New(); | |
194 qualified_id->id_namespace = id_namespace_; | |
195 qualified_id->local = kLocalSurfaceID; | |
196 native_viewport_->SubmittedFrame(qualified_id.Pass()); | |
197 } | |
198 } | |
199 | |
200 void DefaultDisplayManager::OnConnectionError() { | 179 void DefaultDisplayManager::OnConnectionError() { |
201 // This is called when the native_viewport is torn down before | 180 // This is called when the native_viewport is torn down before |
202 // ~DefaultDisplayManager may be called. | 181 // ~DefaultDisplayManager may be called. |
203 native_viewport_closed_callback_.Run(); | 182 native_viewport_closed_callback_.Run(); |
204 } | 183 } |
205 | 184 |
206 } // namespace view_manager | 185 } // namespace view_manager |
OLD | NEW |