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.
5 #include "blimp/client/compositor/blimp_compositor.h"
7 #include "base/bind_helpers.h"
8 #include "base/command_line.h"
9 #include "base/lazy_instance.h"
10 #include "base/single_thread_task_runner.h"
11 #include "base/thread_task_runner_handle.h"
12 #include "base/threading/thread.h"
13 #include "base/threading/thread_local.h"
14 #include "base/threading/thread_restrictions.h"
15 #include "blimp/client/compositor/blimp_context_provider.h"
16 #include "blimp/client/compositor/blimp_output_surface.h"
17 #include "blimp/client/compositor/blimp_task_graph_runner.h"
18 #include "blimp/client/compositor/test/dummy_layer_driver.h"
19 #include "blimp/common/compositor/blimp_layer_tree_settings.h"
20 #include "cc/layers/layer.h"
21 #include "cc/output/output_surface.h"
22 #include "cc/trees/layer_tree_host.h"
23 #include "ui/gl/gl_surface.h"
27 base::LazyInstance
<blimp::BlimpTaskGraphRunner
> g_task_graph_runner
=
28 LAZY_INSTANCE_INITIALIZER
;
30 // TODO(dtrainor): Replace this when Layer content comes from the server (see
31 // crbug.com/527200 for details).
32 base::LazyInstance
<blimp::DummyLayerDriver
> g_dummy_layer_driver
=
33 LAZY_INSTANCE_INITIALIZER
;
39 BlimpCompositor::BlimpCompositor(float dp_to_px
)
40 : device_scale_factor_(dp_to_px
) {}
42 BlimpCompositor::~BlimpCompositor() {
43 // Destroy |host_| first, as it has a reference to the |settings_| and runs
44 // tasks on |compositor_thread_|.
47 if (compositor_thread_
)
48 compositor_thread_
->Stop();
51 void BlimpCompositor::SetVisible(bool visible
) {
52 if (visible
&& !host_
) {
54 settings_
.reset(new cc::LayerTreeSettings
);
55 GenerateLayerTreeSettings(settings_
.get());
58 // Create the LayerTreeHost
59 cc::LayerTreeHost::InitParams params
;
61 params
.task_graph_runner
= g_task_graph_runner
.Pointer();
62 params
.main_task_runner
= base::ThreadTaskRunnerHandle::Get();
63 params
.settings
= settings_
.get();
65 // TODO(dtrainor): Swap this out with the remote client proxy when
68 cc::LayerTreeHost::CreateThreaded(GetCompositorTaskRunner(), ¶ms
);
70 host_
->SetVisible(true);
71 host_
->SetLayerTreeHostClientReady();
72 host_
->SetViewportSize(viewport_size_
);
73 host_
->SetDeviceScaleFactor(device_scale_factor_
);
75 // Build the root Layer.
76 scoped_refptr
<cc::Layer
> root(cc::Layer::Create(cc::LayerSettings()));
77 host_
->SetRootLayer(root
);
79 // For testing, set the dummy Layer.
80 g_dummy_layer_driver
.Pointer()->SetParentLayer(root
);
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.
89 void BlimpCompositor::SetSize(const gfx::Size
& size
) {
90 viewport_size_
= size
;
92 host_
->SetViewportSize(viewport_size_
);
95 void BlimpCompositor::WillBeginMainFrame() {}
97 void BlimpCompositor::DidBeginMainFrame() {}
99 void BlimpCompositor::BeginMainFrame(const cc::BeginFrameArgs
& args
) {}
101 void BlimpCompositor::BeginMainFrameNotExpectedSoon() {}
103 void BlimpCompositor::Layout() {}
105 void BlimpCompositor::ApplyViewportDeltas(
106 const gfx::Vector2dF
& inner_delta
,
107 const gfx::Vector2dF
& outer_delta
,
108 const gfx::Vector2dF
& elastic_overscroll_delta
,
110 float top_controls_delta
) {}
112 void BlimpCompositor::RequestNewOutputSurface() {
113 gfx::AcceleratedWidget widget
= GetWindow();
116 scoped_refptr
<BlimpContextProvider
> context_provider
=
117 BlimpContextProvider::Create(widget
);
119 host_
->SetOutputSurface(
120 make_scoped_ptr(new BlimpOutputSurface(context_provider
)));
123 void BlimpCompositor::DidInitializeOutputSurface() {}
125 void BlimpCompositor::DidFailToInitializeOutputSurface() {}
127 void BlimpCompositor::WillCommit() {}
129 void BlimpCompositor::DidCommit() {}
131 void BlimpCompositor::DidCommitAndDrawFrame() {}
133 void BlimpCompositor::DidCompleteSwapBuffers() {}
135 void BlimpCompositor::DidCompletePageScaleAnimation() {}
137 void BlimpCompositor::RecordFrameTimingEvents(
138 scoped_ptr
<cc::FrameTimingTracker::CompositeTimingSet
> composite_events
,
139 scoped_ptr
<cc::FrameTimingTracker::MainFrameTimingSet
> main_frame_events
) {}
141 void BlimpCompositor::GenerateLayerTreeSettings(
142 cc::LayerTreeSettings
* settings
) {
143 PopulateCommonLayerTreeSettings(settings
);
146 scoped_refptr
<base::SingleThreadTaskRunner
>
147 BlimpCompositor::GetCompositorTaskRunner() {
148 if (compositor_thread_
)
149 return compositor_thread_
->task_runner();
151 base::Thread::Options thread_options
;
152 #if defined(OS_ANDROID)
153 thread_options
.priority
= base::ThreadPriority::DISPLAY
;
155 compositor_thread_
.reset(new base::Thread("Compositor"));
156 compositor_thread_
->StartWithOptions(thread_options
);
158 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner
=
159 compositor_thread_
->task_runner();
160 task_runner
->PostTask(
162 base::Bind(base::IgnoreResult(&base::ThreadRestrictions::SetIOAllowed
),
164 // TODO(dtrainor): Determine whether or not we can disallow waiting.