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