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
,
69 surface
.PassInterface().PassHandle()));
71 layer_tree_host_
->SetLayerTreeHostClientReady();
74 WebLayerTreeViewImpl::~WebLayerTreeViewImpl() {
75 // Destroy the LayerTreeHost before anything else as doing so ensures we're
76 // not accessed on the compositor thread (we are the LayerTreeHostClient).
77 layer_tree_host_
.reset();
80 void WebLayerTreeViewImpl::WillBeginMainFrame() {
83 void WebLayerTreeViewImpl::DidBeginMainFrame() {
86 void WebLayerTreeViewImpl::BeginMainFrameNotExpectedSoon() {
89 void WebLayerTreeViewImpl::BeginMainFrame(const cc::BeginFrameArgs
& args
) {
90 VLOG(2) << "WebLayerTreeViewImpl::BeginMainFrame";
91 double frame_time_sec
= (args
.frame_time
- base::TimeTicks()).InSecondsF();
92 double deadline_sec
= (args
.deadline
- base::TimeTicks()).InSecondsF();
93 double interval_sec
= args
.interval
.InSecondsF();
94 blink::WebBeginFrameArgs
web_begin_frame_args(
95 frame_time_sec
, deadline_sec
, interval_sec
);
96 widget_
->beginFrame(web_begin_frame_args
);
99 void WebLayerTreeViewImpl::Layout() {
103 void WebLayerTreeViewImpl::ApplyViewportDeltas(
104 const gfx::Vector2dF
& inner_delta
,
105 const gfx::Vector2dF
& outer_delta
,
106 const gfx::Vector2dF
& elastic_overscroll_delta
,
108 float top_controls_delta
) {
109 widget_
->applyViewportDeltas(
112 elastic_overscroll_delta
,
117 void WebLayerTreeViewImpl::RequestNewOutputSurface() {
118 if (output_surface_
.get())
119 layer_tree_host_
->SetOutputSurface(output_surface_
.Pass());
122 void WebLayerTreeViewImpl::DidFailToInitializeOutputSurface() {
123 RequestNewOutputSurface();
126 void WebLayerTreeViewImpl::DidInitializeOutputSurface() {
129 void WebLayerTreeViewImpl::WillCommit() {
132 void WebLayerTreeViewImpl::DidCommit() {
135 void WebLayerTreeViewImpl::DidCommitAndDrawFrame() {
138 void WebLayerTreeViewImpl::DidCompleteSwapBuffers() {
141 void WebLayerTreeViewImpl::setRootLayer(const blink::WebLayer
& layer
) {
142 layer_tree_host_
->SetRootLayer(
143 static_cast<const cc_blink::WebLayerImpl
*>(&layer
)->layer());
146 void WebLayerTreeViewImpl::clearRootLayer() {
147 layer_tree_host_
->SetRootLayer(scoped_refptr
<cc::Layer
>());
150 void WebLayerTreeViewImpl::setViewportSize(
151 const blink::WebSize
& device_viewport_size
) {
152 layer_tree_host_
->SetViewportSize(device_viewport_size
);
155 blink::WebSize
WebLayerTreeViewImpl::deviceViewportSize() const {
156 return layer_tree_host_
->device_viewport_size();
159 void WebLayerTreeViewImpl::setDeviceScaleFactor(float device_scale_factor
) {
160 layer_tree_host_
->SetDeviceScaleFactor(device_scale_factor
);
163 float WebLayerTreeViewImpl::deviceScaleFactor() const {
164 return layer_tree_host_
->device_scale_factor();
167 void WebLayerTreeViewImpl::setBackgroundColor(blink::WebColor color
) {
168 layer_tree_host_
->set_background_color(color
);
171 void WebLayerTreeViewImpl::setHasTransparentBackground(
172 bool has_transparent_background
) {
173 layer_tree_host_
->set_has_transparent_background(has_transparent_background
);
176 void WebLayerTreeViewImpl::setVisible(bool visible
) {
177 layer_tree_host_
->SetVisible(visible
);
180 void WebLayerTreeViewImpl::setPageScaleFactorAndLimits(float page_scale_factor
,
183 layer_tree_host_
->SetPageScaleFactorAndLimits(
184 page_scale_factor
, minimum
, maximum
);
187 void WebLayerTreeViewImpl::registerForAnimations(blink::WebLayer
* layer
) {
188 cc::Layer
* cc_layer
= static_cast<cc_blink::WebLayerImpl
*>(layer
)->layer();
189 cc_layer
->RegisterForAnimations(layer_tree_host_
->animation_registrar());
192 void WebLayerTreeViewImpl::registerViewportLayers(
193 const blink::WebLayer
* overscrollElasticityLayer
,
194 const blink::WebLayer
* pageScaleLayer
,
195 const blink::WebLayer
* innerViewportScrollLayer
,
196 const blink::WebLayer
* outerViewportScrollLayer
) {
197 layer_tree_host_
->RegisterViewportLayers(
198 // The scroll elasticity layer will only exist when using pinch virtual
200 overscrollElasticityLayer
201 ? static_cast<const cc_blink::WebLayerImpl
*>(
202 overscrollElasticityLayer
)->layer()
204 static_cast<const cc_blink::WebLayerImpl
*>(pageScaleLayer
)->layer(),
205 static_cast<const cc_blink::WebLayerImpl
*>(innerViewportScrollLayer
)
207 // The outer viewport layer will only exist when using pinch virtual
209 outerViewportScrollLayer
210 ? static_cast<const cc_blink::WebLayerImpl
*>(outerViewportScrollLayer
)
215 void WebLayerTreeViewImpl::clearViewportLayers() {
216 layer_tree_host_
->RegisterViewportLayers(scoped_refptr
<cc::Layer
>(),
217 scoped_refptr
<cc::Layer
>(),
218 scoped_refptr
<cc::Layer
>(),
219 scoped_refptr
<cc::Layer
>());
222 void WebLayerTreeViewImpl::startPageScaleAnimation(
223 const blink::WebPoint
& destination
,
225 float new_page_scale
,
226 double duration_sec
) {
227 base::TimeDelta duration
= base::TimeDelta::FromMicroseconds(
228 duration_sec
* base::Time::kMicrosecondsPerSecond
);
229 layer_tree_host_
->StartPageScaleAnimation(
230 gfx::Vector2d(destination
.x
, destination
.y
),
236 void WebLayerTreeViewImpl::setNeedsAnimate() {
237 layer_tree_host_
->SetNeedsAnimate();
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