OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "blimp/client/compositor/blimp_compositor.h" | 5 #include "blimp/client/compositor/blimp_compositor.h" |
6 | 6 |
7 #include "base/bind_helpers.h" | 7 #include "base/bind_helpers.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
| 10 #include "base/numerics/safe_conversions.h" |
10 #include "base/single_thread_task_runner.h" | 11 #include "base/single_thread_task_runner.h" |
11 #include "base/thread_task_runner_handle.h" | 12 #include "base/thread_task_runner_handle.h" |
12 #include "base/threading/thread.h" | 13 #include "base/threading/thread.h" |
13 #include "base/threading/thread_local.h" | 14 #include "base/threading/thread_local.h" |
14 #include "base/threading/thread_restrictions.h" | 15 #include "base/threading/thread_restrictions.h" |
15 #include "blimp/client/compositor/blimp_context_provider.h" | 16 #include "blimp/client/compositor/blimp_context_provider.h" |
16 #include "blimp/client/compositor/blimp_layer_tree_settings.h" | 17 #include "blimp/client/compositor/blimp_layer_tree_settings.h" |
17 #include "blimp/client/compositor/blimp_output_surface.h" | 18 #include "blimp/client/compositor/blimp_output_surface.h" |
18 #include "blimp/client/compositor/test/dummy_layer_driver.h" | 19 #include "blimp/client/compositor/test/dummy_layer_driver.h" |
19 #include "blimp/common/compositor/blimp_task_graph_runner.h" | 20 #include "blimp/common/compositor/blimp_task_graph_runner.h" |
| 21 #include "blimp/common/proto/blimp_message.pb.h" |
| 22 #include "blimp/common/proto/compositor.pb.h" |
| 23 #include "blimp/common/proto/input.pb.h" |
| 24 #include "blimp/common/proto/render_widget.pb.h" |
| 25 #include "blimp/net/blimp_message_multiplexer.h" |
| 26 #include "blimp/net/null_blimp_message_processor.h" |
20 #include "cc/layers/layer.h" | 27 #include "cc/layers/layer.h" |
21 #include "cc/layers/layer_settings.h" | 28 #include "cc/layers/layer_settings.h" |
22 #include "cc/output/output_surface.h" | 29 #include "cc/output/output_surface.h" |
| 30 #include "cc/proto/compositor_message.pb.h" |
23 #include "cc/trees/layer_tree_host.h" | 31 #include "cc/trees/layer_tree_host.h" |
| 32 #include "net/base/net_errors.h" |
24 #include "ui/gl/gl_surface.h" | 33 #include "ui/gl/gl_surface.h" |
25 | 34 |
26 namespace { | 35 namespace { |
27 | 36 |
28 base::LazyInstance<blimp::BlimpTaskGraphRunner> g_task_graph_runner = | 37 base::LazyInstance<blimp::BlimpTaskGraphRunner> g_task_graph_runner = |
29 LAZY_INSTANCE_INITIALIZER; | 38 LAZY_INSTANCE_INITIALIZER; |
30 | 39 |
| 40 const int kDummyTabId = 0; |
| 41 |
| 42 base::LazyInstance<blimp::NullBlimpMessageProcessor> g_blimp_message_processor = |
| 43 LAZY_INSTANCE_INITIALIZER; |
| 44 |
31 // TODO(dtrainor): Replace this when Layer content comes from the server (see | 45 // TODO(dtrainor): Replace this when Layer content comes from the server (see |
32 // crbug.com/527200 for details). | 46 // crbug.com/527200 for details). |
33 base::LazyInstance<blimp::DummyLayerDriver> g_dummy_layer_driver = | 47 base::LazyInstance<blimp::DummyLayerDriver> g_dummy_layer_driver = |
34 LAZY_INSTANCE_INITIALIZER; | 48 LAZY_INSTANCE_INITIALIZER; |
35 | 49 |
36 } // namespace | 50 } // namespace |
37 | 51 |
38 namespace blimp { | 52 namespace blimp { |
39 | 53 |
40 BlimpCompositor::BlimpCompositor(float dp_to_px) | 54 BlimpCompositor::BlimpCompositor(float dp_to_px) |
41 : device_scale_factor_(dp_to_px) {} | 55 : device_scale_factor_(dp_to_px), |
| 56 window_(gfx::kNullAcceleratedWidget), |
| 57 host_should_be_visible_(false), |
| 58 output_surface_request_pending_(false), |
| 59 remote_proto_channel_receiver_(nullptr), |
| 60 // TODO(dtrainor): Properly pull these from BlimpMessageMultiplexer. |
| 61 render_widget_processor_(g_blimp_message_processor.Pointer(), |
| 62 g_blimp_message_processor.Pointer()) { |
| 63 render_widget_processor_.SetDelegate(kDummyTabId, this); |
| 64 } |
42 | 65 |
43 BlimpCompositor::~BlimpCompositor() { | 66 BlimpCompositor::~BlimpCompositor() { |
| 67 render_widget_processor_.RemoveDelegate(kDummyTabId); |
| 68 SetVisible(false); |
| 69 |
44 // Destroy |host_| first, as it has a reference to the |settings_| and runs | 70 // Destroy |host_| first, as it has a reference to the |settings_| and runs |
45 // tasks on |compositor_thread_|. | 71 // tasks on |compositor_thread_|. |
46 host_.reset(); | 72 host_.reset(); |
47 settings_.reset(); | 73 settings_.reset(); |
48 if (compositor_thread_) | 74 if (compositor_thread_) |
49 compositor_thread_->Stop(); | 75 compositor_thread_->Stop(); |
50 } | 76 } |
51 | 77 |
52 void BlimpCompositor::SetVisible(bool visible) { | 78 void BlimpCompositor::SetVisible(bool visible) { |
53 if (visible && !host_) { | 79 // For testing. Remove once we bind to the network layer. |
54 if (!settings_) { | 80 if (!host_) |
55 settings_.reset(new cc::LayerTreeSettings); | 81 CreateLayerTreeHost(nullptr); |
56 GenerateLayerTreeSettings(settings_.get()); | |
57 } | |
58 | 82 |
59 // Create the LayerTreeHost | 83 host_should_be_visible_ = visible; |
60 cc::LayerTreeHost::InitParams params; | 84 if (!host_ || host_->visible() == visible) |
61 params.client = this; | 85 return; |
62 params.task_graph_runner = g_task_graph_runner.Pointer(); | |
63 params.main_task_runner = base::ThreadTaskRunnerHandle::Get(); | |
64 params.settings = settings_.get(); | |
65 | 86 |
66 // TODO(dtrainor): Swap this out with the remote client proxy when | 87 if (visible) { |
67 // implemented. | 88 // If visible, show the compositor. If the compositor had an outstanding |
68 host_ = | 89 // output surface request, trigger the request again so we build the output |
69 cc::LayerTreeHost::CreateThreaded(GetCompositorTaskRunner(), ¶ms); | 90 // surface. |
70 | |
71 host_->SetVisible(true); | 91 host_->SetVisible(true); |
72 host_->SetViewportSize(viewport_size_); | 92 if (output_surface_request_pending_) |
73 host_->SetDeviceScaleFactor(device_scale_factor_); | 93 HandlePendingOutputSurfaceRequest(); |
74 | 94 } else { |
75 // Build the root Layer. | 95 // If not visible, hide the compositor and have it drop it's output surface. |
76 scoped_refptr<cc::Layer> root(cc::Layer::Create(cc::LayerSettings())); | 96 DCHECK(host_->visible()); |
77 host_->SetRootLayer(root); | 97 host_->SetVisible(false); |
78 | 98 if (!host_->output_surface_lost()) |
79 // For testing, set the dummy Layer. | 99 host_->ReleaseOutputSurface(); |
80 g_dummy_layer_driver.Pointer()->SetParentLayer(root); | |
81 | |
82 } else if (!visible && host_) { | |
83 // Release the LayerTreeHost to free all resources when the compositor is no | |
84 // longer visible. This will destroy the underlying compositor components. | |
85 host_.reset(); | |
86 } | 100 } |
87 } | 101 } |
88 | 102 |
89 void BlimpCompositor::SetSize(const gfx::Size& size) { | 103 void BlimpCompositor::SetSize(const gfx::Size& size) { |
90 viewport_size_ = size; | 104 viewport_size_ = size; |
91 if (host_) | 105 if (host_) |
92 host_->SetViewportSize(viewport_size_); | 106 host_->SetViewportSize(viewport_size_); |
93 } | 107 } |
94 | 108 |
| 109 void BlimpCompositor::SetAcceleratedWidget(gfx::AcceleratedWidget widget) { |
| 110 if (widget == window_) |
| 111 return; |
| 112 |
| 113 DCHECK(window_ == gfx::kNullAcceleratedWidget); |
| 114 window_ = widget; |
| 115 |
| 116 // The compositor should not be visible if there is no output surface. |
| 117 DCHECK(!host_ || !host_->visible()); |
| 118 |
| 119 // This will properly set visibility and will build the output surface if |
| 120 // necessary. |
| 121 SetVisible(host_should_be_visible_); |
| 122 } |
| 123 |
| 124 void BlimpCompositor::ReleaseAcceleratedWidget() { |
| 125 if (window_ == gfx::kNullAcceleratedWidget) |
| 126 return; |
| 127 |
| 128 // Hide the compositor and drop the output surface if necessary. |
| 129 SetVisible(false); |
| 130 |
| 131 window_ = gfx::kNullAcceleratedWidget; |
| 132 } |
| 133 |
95 void BlimpCompositor::WillBeginMainFrame() {} | 134 void BlimpCompositor::WillBeginMainFrame() {} |
96 | 135 |
97 void BlimpCompositor::DidBeginMainFrame() {} | 136 void BlimpCompositor::DidBeginMainFrame() {} |
98 | 137 |
99 void BlimpCompositor::BeginMainFrame(const cc::BeginFrameArgs& args) {} | 138 void BlimpCompositor::BeginMainFrame(const cc::BeginFrameArgs& args) {} |
100 | 139 |
101 void BlimpCompositor::BeginMainFrameNotExpectedSoon() {} | 140 void BlimpCompositor::BeginMainFrameNotExpectedSoon() {} |
102 | 141 |
103 void BlimpCompositor::UpdateLayerTreeHost() {} | 142 void BlimpCompositor::UpdateLayerTreeHost() {} |
104 | 143 |
105 void BlimpCompositor::ApplyViewportDeltas( | 144 void BlimpCompositor::ApplyViewportDeltas( |
106 const gfx::Vector2dF& inner_delta, | 145 const gfx::Vector2dF& inner_delta, |
107 const gfx::Vector2dF& outer_delta, | 146 const gfx::Vector2dF& outer_delta, |
108 const gfx::Vector2dF& elastic_overscroll_delta, | 147 const gfx::Vector2dF& elastic_overscroll_delta, |
109 float page_scale, | 148 float page_scale, |
110 float top_controls_delta) {} | 149 float top_controls_delta) {} |
111 | 150 |
112 void BlimpCompositor::RequestNewOutputSurface() { | 151 void BlimpCompositor::RequestNewOutputSurface() { |
113 gfx::AcceleratedWidget widget = GetWindow(); | 152 output_surface_request_pending_ = true; |
114 DCHECK(widget); | 153 HandlePendingOutputSurfaceRequest(); |
115 | |
116 scoped_refptr<BlimpContextProvider> context_provider = | |
117 BlimpContextProvider::Create(widget); | |
118 | |
119 host_->SetOutputSurface( | |
120 make_scoped_ptr(new BlimpOutputSurface(context_provider))); | |
121 } | 154 } |
122 | 155 |
123 void BlimpCompositor::DidInitializeOutputSurface() {} | 156 void BlimpCompositor::DidInitializeOutputSurface() {} |
124 | 157 |
125 void BlimpCompositor::DidFailToInitializeOutputSurface() {} | 158 void BlimpCompositor::DidFailToInitializeOutputSurface() {} |
126 | 159 |
127 void BlimpCompositor::WillCommit() {} | 160 void BlimpCompositor::WillCommit() {} |
128 | 161 |
129 void BlimpCompositor::DidCommit() {} | 162 void BlimpCompositor::DidCommit() {} |
130 | 163 |
131 void BlimpCompositor::DidCommitAndDrawFrame() {} | 164 void BlimpCompositor::DidCommitAndDrawFrame() {} |
132 | 165 |
133 void BlimpCompositor::DidCompleteSwapBuffers() {} | 166 void BlimpCompositor::DidCompleteSwapBuffers() {} |
134 | 167 |
135 void BlimpCompositor::DidCompletePageScaleAnimation() {} | 168 void BlimpCompositor::DidCompletePageScaleAnimation() {} |
136 | 169 |
137 void BlimpCompositor::RecordFrameTimingEvents( | 170 void BlimpCompositor::RecordFrameTimingEvents( |
138 scoped_ptr<cc::FrameTimingTracker::CompositeTimingSet> composite_events, | 171 scoped_ptr<cc::FrameTimingTracker::CompositeTimingSet> composite_events, |
139 scoped_ptr<cc::FrameTimingTracker::MainFrameTimingSet> main_frame_events) {} | 172 scoped_ptr<cc::FrameTimingTracker::MainFrameTimingSet> main_frame_events) {} |
140 | 173 |
| 174 void BlimpCompositor::SetProtoReceiver(ProtoReceiver* receiver) { |
| 175 remote_proto_channel_receiver_ = receiver; |
| 176 } |
| 177 |
| 178 void BlimpCompositor::SendCompositorProto( |
| 179 const cc::proto::CompositorMessage& proto) { |
| 180 render_widget_processor_.SendCompositorMessage(kDummyTabId, proto); |
| 181 } |
| 182 |
| 183 void BlimpCompositor::OnRenderWidgetInitialized() { |
| 184 // Tear down the output surface connection with the old LayerTreeHost |
| 185 // instance. |
| 186 SetVisible(false); |
| 187 |
| 188 // Destroy the old LayerTreeHost state. |
| 189 host_.reset(); |
| 190 |
| 191 // Reset other state. |
| 192 output_surface_request_pending_ = false; |
| 193 |
| 194 // Make sure we don't have a receiver at this point. |
| 195 DCHECK(!remote_proto_channel_receiver_); |
| 196 } |
| 197 |
| 198 void BlimpCompositor::OnCompositorMessageReceived( |
| 199 scoped_ptr<cc::proto::CompositorMessage> message) { |
| 200 // TODO(dtrainor, khushalsagar): Look into the CompositorMessage. If it is |
| 201 // initialize or shutdown, create or destroy |host_|. |
| 202 |
| 203 // We should have a receiver if we're getting compositor messages that aren't |
| 204 // initialize. |
| 205 DCHECK(remote_proto_channel_receiver_); |
| 206 remote_proto_channel_receiver_->OnProtoReceived(std::move(message)); |
| 207 } |
| 208 |
141 void BlimpCompositor::GenerateLayerTreeSettings( | 209 void BlimpCompositor::GenerateLayerTreeSettings( |
142 cc::LayerTreeSettings* settings) { | 210 cc::LayerTreeSettings* settings) { |
143 PopulateCommonLayerTreeSettings(settings); | 211 PopulateCommonLayerTreeSettings(settings); |
144 } | 212 } |
145 | 213 |
| 214 void BlimpCompositor::CreateLayerTreeHost( |
| 215 scoped_ptr<cc::proto::CompositorMessage> message) { |
| 216 if (!settings_) { |
| 217 settings_.reset(new cc::LayerTreeSettings); |
| 218 GenerateLayerTreeSettings(settings_.get()); |
| 219 } |
| 220 |
| 221 // Create the LayerTreeHost |
| 222 cc::LayerTreeHost::InitParams params; |
| 223 params.client = this; |
| 224 params.task_graph_runner = g_task_graph_runner.Pointer(); |
| 225 params.main_task_runner = base::ThreadTaskRunnerHandle::Get(); |
| 226 params.settings = settings_.get(); |
| 227 |
| 228 // TODO(dtrainor): Swap this out with the remote client proxy when |
| 229 // implemented. |
| 230 host_ = |
| 231 cc::LayerTreeHost::CreateThreaded(GetCompositorTaskRunner(), ¶ms); |
| 232 |
| 233 // If we're supposed to be visible and we have a valid gfx::AcceleratedWidget |
| 234 // make our compositor visible. |
| 235 if (host_should_be_visible_ && window_ != gfx::kNullAcceleratedWidget) |
| 236 host_->SetVisible(true); |
| 237 |
| 238 host_->SetViewportSize(viewport_size_); |
| 239 host_->SetDeviceScaleFactor(device_scale_factor_); |
| 240 |
| 241 // For testing, set the dummy Layer. |
| 242 scoped_refptr<cc::Layer> root(cc::Layer::Create(cc::LayerSettings())); |
| 243 host_->SetRootLayer(root); |
| 244 g_dummy_layer_driver.Pointer()->SetParentLayer(root); |
| 245 } |
| 246 |
146 scoped_refptr<base::SingleThreadTaskRunner> | 247 scoped_refptr<base::SingleThreadTaskRunner> |
147 BlimpCompositor::GetCompositorTaskRunner() { | 248 BlimpCompositor::GetCompositorTaskRunner() { |
148 if (compositor_thread_) | 249 if (compositor_thread_) |
149 return compositor_thread_->task_runner(); | 250 return compositor_thread_->task_runner(); |
150 | 251 |
151 base::Thread::Options thread_options; | 252 base::Thread::Options thread_options; |
152 #if defined(OS_ANDROID) | 253 #if defined(OS_ANDROID) |
153 thread_options.priority = base::ThreadPriority::DISPLAY; | 254 thread_options.priority = base::ThreadPriority::DISPLAY; |
154 #endif | 255 #endif |
155 compositor_thread_.reset(new base::Thread("Compositor")); | 256 compositor_thread_.reset(new base::Thread("Compositor")); |
156 compositor_thread_->StartWithOptions(thread_options); | 257 compositor_thread_->StartWithOptions(thread_options); |
157 | 258 |
158 scoped_refptr<base::SingleThreadTaskRunner> task_runner = | 259 scoped_refptr<base::SingleThreadTaskRunner> task_runner = |
159 compositor_thread_->task_runner(); | 260 compositor_thread_->task_runner(); |
160 task_runner->PostTask( | 261 task_runner->PostTask( |
161 FROM_HERE, | 262 FROM_HERE, |
162 base::Bind(base::IgnoreResult(&base::ThreadRestrictions::SetIOAllowed), | 263 base::Bind(base::IgnoreResult(&base::ThreadRestrictions::SetIOAllowed), |
163 false)); | 264 false)); |
164 // TODO(dtrainor): Determine whether or not we can disallow waiting. | 265 // TODO(dtrainor): Determine whether or not we can disallow waiting. |
165 | 266 |
166 return task_runner; | 267 return task_runner; |
167 } | 268 } |
168 | 269 |
| 270 void BlimpCompositor::HandlePendingOutputSurfaceRequest() { |
| 271 DCHECK(output_surface_request_pending_); |
| 272 |
| 273 // We might have had a request from a LayerTreeHost that was then |
| 274 // hidden (and hidden means we don't have a native surface). |
| 275 // Also make sure we only handle this once. |
| 276 if (!host_->visible() || window_ == gfx::kNullAcceleratedWidget) |
| 277 return; |
| 278 |
| 279 scoped_refptr<BlimpContextProvider> context_provider = |
| 280 BlimpContextProvider::Create(window_); |
| 281 |
| 282 host_->SetOutputSurface( |
| 283 make_scoped_ptr(new BlimpOutputSurface(context_provider))); |
| 284 output_surface_request_pending_ = false; |
| 285 } |
| 286 |
169 } // namespace blimp | 287 } // namespace blimp |
OLD | NEW |