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
3 // found in the LICENSE file.
5 #include "components/html_viewer/web_layer_tree_view_impl.h"
7 #include "base/thread_task_runner_handle.h"
8 #include "cc/blink/web_layer_impl.h"
9 #include "cc/layers/layer.h"
10 #include "cc/output/begin_frame_args.h"
11 #include "cc/scheduler/begin_frame_source.h"
12 #include "cc/trees/layer_tree_host.h"
13 #include "components/view_manager/public/cpp/view.h"
14 #include "mojo/cc/context_provider_mojo.h"
15 #include "mojo/cc/output_surface_mojo.h"
16 #include "mojo/converters/surfaces/surfaces_type_converters.h"
17 #include "third_party/WebKit/public/web/WebWidget.h"
19 namespace html_viewer
{
21 WebLayerTreeViewImpl::WebLayerTreeViewImpl(
22 scoped_refptr
<base::SingleThreadTaskRunner
> compositor_task_runner
,
23 gpu::GpuMemoryBufferManager
* gpu_memory_buffer_manager
,
24 cc::TaskGraphRunner
* task_graph_runner
,
25 mojo::SurfacePtr surface
,
26 mojo::GpuPtr gpu_service
)
29 main_thread_compositor_task_runner_(base::ThreadTaskRunnerHandle::Get()),
31 main_thread_bound_weak_ptr_
= weak_factory_
.GetWeakPtr();
33 cc::LayerTreeSettings settings
;
35 // Must match the value of
36 // blink::RuntimeEnabledFeature::slimmingPaintEnabled()
37 settings
.use_display_lists
= true;
39 settings
.use_image_texture_target
= GL_TEXTURE_2D
;
40 settings
.use_one_copy
= true;
41 // TODO(jam): use multiple compositor raster threads and set gather_pixel_refs
42 // accordingly (see content).
44 // For web contents, layer transforms should scale up the contents of layers
45 // to keep content always crisp when possible.
46 settings
.layer_transforms_should_scale_layer_contents
= true;
48 cc::SharedBitmapManager
* shared_bitmap_manager
= nullptr;
50 cc::LayerTreeHost::InitParams params
;
52 params
.shared_bitmap_manager
= shared_bitmap_manager
;
53 params
.gpu_memory_buffer_manager
= gpu_memory_buffer_manager
;
54 params
.settings
= &settings
;
55 params
.task_graph_runner
= task_graph_runner
;
56 params
.main_task_runner
= main_thread_compositor_task_runner_
;
59 cc::LayerTreeHost::CreateThreaded(compositor_task_runner
, ¶ms
);
60 DCHECK(layer_tree_host_
);
62 if (surface
&& gpu_service
) {
63 mojo::CommandBufferPtr cb
;
64 gpu_service
->CreateOffscreenGLES2Context(GetProxy(&cb
));
65 scoped_refptr
<cc::ContextProvider
> context_provider(
66 new mojo::ContextProviderMojo(cb
.PassInterface().PassHandle()));
67 output_surface_
.reset(
68 new mojo::OutputSurfaceMojo(this, context_provider
, surface
.Pass()));
70 layer_tree_host_
->SetLayerTreeHostClientReady();
73 WebLayerTreeViewImpl::~WebLayerTreeViewImpl() {
76 void WebLayerTreeViewImpl::WillBeginMainFrame() {
79 void WebLayerTreeViewImpl::DidBeginMainFrame() {
82 void WebLayerTreeViewImpl::BeginMainFrameNotExpectedSoon() {
85 void WebLayerTreeViewImpl::BeginMainFrame(const cc::BeginFrameArgs
& args
) {
86 VLOG(2) << "WebLayerTreeViewImpl::BeginMainFrame";
87 double frame_time_sec
= (args
.frame_time
- base::TimeTicks()).InSecondsF();
88 double deadline_sec
= (args
.deadline
- base::TimeTicks()).InSecondsF();
89 double interval_sec
= args
.interval
.InSecondsF();
90 blink::WebBeginFrameArgs
web_begin_frame_args(
91 frame_time_sec
, deadline_sec
, interval_sec
);
92 widget_
->beginFrame(web_begin_frame_args
);
95 void WebLayerTreeViewImpl::Layout() {
99 void WebLayerTreeViewImpl::ApplyViewportDeltas(
100 const gfx::Vector2dF
& inner_delta
,
101 const gfx::Vector2dF
& outer_delta
,
102 const gfx::Vector2dF
& elastic_overscroll_delta
,
104 float top_controls_delta
) {
105 widget_
->applyViewportDeltas(
108 elastic_overscroll_delta
,
113 void WebLayerTreeViewImpl::RequestNewOutputSurface() {
114 if (output_surface_
.get())
115 layer_tree_host_
->SetOutputSurface(output_surface_
.Pass());
118 void WebLayerTreeViewImpl::DidFailToInitializeOutputSurface() {
119 RequestNewOutputSurface();
122 void WebLayerTreeViewImpl::DidInitializeOutputSurface() {
125 void WebLayerTreeViewImpl::WillCommit() {
128 void WebLayerTreeViewImpl::DidCommit() {
131 void WebLayerTreeViewImpl::DidCommitAndDrawFrame() {
134 void WebLayerTreeViewImpl::DidCompleteSwapBuffers() {
137 void WebLayerTreeViewImpl::setRootLayer(const blink::WebLayer
& layer
) {
138 layer_tree_host_
->SetRootLayer(
139 static_cast<const cc_blink::WebLayerImpl
*>(&layer
)->layer());
142 void WebLayerTreeViewImpl::clearRootLayer() {
143 layer_tree_host_
->SetRootLayer(scoped_refptr
<cc::Layer
>());
146 void WebLayerTreeViewImpl::setViewportSize(
147 const blink::WebSize
& device_viewport_size
) {
148 layer_tree_host_
->SetViewportSize(device_viewport_size
);
151 blink::WebSize
WebLayerTreeViewImpl::deviceViewportSize() const {
152 return layer_tree_host_
->device_viewport_size();
155 void WebLayerTreeViewImpl::setDeviceScaleFactor(float device_scale_factor
) {
156 layer_tree_host_
->SetDeviceScaleFactor(device_scale_factor
);
159 float WebLayerTreeViewImpl::deviceScaleFactor() const {
160 return layer_tree_host_
->device_scale_factor();
163 void WebLayerTreeViewImpl::setBackgroundColor(blink::WebColor color
) {
164 layer_tree_host_
->set_background_color(color
);
167 void WebLayerTreeViewImpl::setHasTransparentBackground(
168 bool has_transparent_background
) {
169 layer_tree_host_
->set_has_transparent_background(has_transparent_background
);
172 void WebLayerTreeViewImpl::setVisible(bool visible
) {
173 layer_tree_host_
->SetVisible(visible
);
176 void WebLayerTreeViewImpl::setPageScaleFactorAndLimits(float page_scale_factor
,
179 layer_tree_host_
->SetPageScaleFactorAndLimits(
180 page_scale_factor
, minimum
, maximum
);
183 void WebLayerTreeViewImpl::registerForAnimations(blink::WebLayer
* layer
) {
184 cc::Layer
* cc_layer
= static_cast<cc_blink::WebLayerImpl
*>(layer
)->layer();
185 cc_layer
->RegisterForAnimations(layer_tree_host_
->animation_registrar());
188 void WebLayerTreeViewImpl::registerViewportLayers(
189 const blink::WebLayer
* overscrollElasticityLayer
,
190 const blink::WebLayer
* pageScaleLayer
,
191 const blink::WebLayer
* innerViewportScrollLayer
,
192 const blink::WebLayer
* outerViewportScrollLayer
) {
193 layer_tree_host_
->RegisterViewportLayers(
194 // The scroll elasticity layer will only exist when using pinch virtual
196 overscrollElasticityLayer
197 ? static_cast<const cc_blink::WebLayerImpl
*>(
198 overscrollElasticityLayer
)->layer()
200 static_cast<const cc_blink::WebLayerImpl
*>(pageScaleLayer
)->layer(),
201 static_cast<const cc_blink::WebLayerImpl
*>(innerViewportScrollLayer
)
203 // The outer viewport layer will only exist when using pinch virtual
205 outerViewportScrollLayer
206 ? static_cast<const cc_blink::WebLayerImpl
*>(outerViewportScrollLayer
)
211 void WebLayerTreeViewImpl::clearViewportLayers() {
212 layer_tree_host_
->RegisterViewportLayers(scoped_refptr
<cc::Layer
>(),
213 scoped_refptr
<cc::Layer
>(),
214 scoped_refptr
<cc::Layer
>(),
215 scoped_refptr
<cc::Layer
>());
218 void WebLayerTreeViewImpl::startPageScaleAnimation(
219 const blink::WebPoint
& destination
,
221 float new_page_scale
,
222 double duration_sec
) {
223 base::TimeDelta duration
= base::TimeDelta::FromMicroseconds(
224 duration_sec
* base::Time::kMicrosecondsPerSecond
);
225 layer_tree_host_
->StartPageScaleAnimation(
226 gfx::Vector2d(destination
.x
, destination
.y
),
232 void WebLayerTreeViewImpl::setNeedsAnimate() {
233 layer_tree_host_
->SetNeedsAnimate();
236 bool WebLayerTreeViewImpl::commitRequested() const {
237 return layer_tree_host_
->CommitRequested();
240 void WebLayerTreeViewImpl::finishAllRendering() {
241 layer_tree_host_
->FinishAllRendering();
244 void WebLayerTreeViewImpl::DidCreateSurface(cc::SurfaceId id
) {
245 main_thread_compositor_task_runner_
->PostTask(
247 base::Bind(&WebLayerTreeViewImpl::DidCreateSurfaceOnMainThread
,
248 main_thread_bound_weak_ptr_
,
252 void WebLayerTreeViewImpl::DidCreateSurfaceOnMainThread(cc::SurfaceId id
) {
253 view_
->SetSurfaceId(mojo::SurfaceId::From(id
));
256 } // namespace html_viewer