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 mojo::SurfacePtr surface
,
24 mojo::GpuPtr gpu_service
)
27 main_thread_compositor_task_runner_(base::ThreadTaskRunnerHandle::Get()),
29 main_thread_bound_weak_ptr_
= weak_factory_
.GetWeakPtr();
31 cc::LayerTreeSettings settings
;
33 // For web contents, layer transforms should scale up the contents of layers
34 // to keep content always crisp when possible.
35 settings
.layer_transforms_should_scale_layer_contents
= true;
37 cc::SharedBitmapManager
* shared_bitmap_manager
= nullptr;
38 gpu::GpuMemoryBufferManager
* gpu_memory_buffer_manager
= nullptr;
39 cc::TaskGraphRunner
* task_graph_runner
= nullptr;
41 cc::LayerTreeHost::InitParams params
;
43 params
.shared_bitmap_manager
= shared_bitmap_manager
;
44 params
.gpu_memory_buffer_manager
= gpu_memory_buffer_manager
;
45 params
.settings
= &settings
;
46 params
.task_graph_runner
= task_graph_runner
;
47 params
.main_task_runner
= main_thread_compositor_task_runner_
;
50 cc::LayerTreeHost::CreateThreaded(compositor_task_runner
, ¶ms
);
51 DCHECK(layer_tree_host_
);
53 if (surface
&& gpu_service
) {
54 mojo::CommandBufferPtr cb
;
55 gpu_service
->CreateOffscreenGLES2Context(GetProxy(&cb
));
56 scoped_refptr
<cc::ContextProvider
> context_provider(
57 new mojo::ContextProviderMojo(cb
.PassInterface().PassHandle()));
58 output_surface_
.reset(
59 new mojo::OutputSurfaceMojo(this, context_provider
, surface
.Pass()));
61 layer_tree_host_
->SetLayerTreeHostClientReady();
64 WebLayerTreeViewImpl::~WebLayerTreeViewImpl() {
67 void WebLayerTreeViewImpl::WillBeginMainFrame() {
70 void WebLayerTreeViewImpl::DidBeginMainFrame() {
73 void WebLayerTreeViewImpl::BeginMainFrameNotExpectedSoon() {
76 void WebLayerTreeViewImpl::BeginMainFrame(const cc::BeginFrameArgs
& args
) {
77 VLOG(2) << "WebLayerTreeViewImpl::BeginMainFrame";
78 double frame_time_sec
= (args
.frame_time
- base::TimeTicks()).InSecondsF();
79 double deadline_sec
= (args
.deadline
- base::TimeTicks()).InSecondsF();
80 double interval_sec
= args
.interval
.InSecondsF();
81 blink::WebBeginFrameArgs
web_begin_frame_args(
82 frame_time_sec
, deadline_sec
, interval_sec
);
83 widget_
->beginFrame(web_begin_frame_args
);
86 void WebLayerTreeViewImpl::Layout() {
90 void WebLayerTreeViewImpl::ApplyViewportDeltas(
91 const gfx::Vector2dF
& inner_delta
,
92 const gfx::Vector2dF
& outer_delta
,
93 const gfx::Vector2dF
& elastic_overscroll_delta
,
95 float top_controls_delta
) {
96 widget_
->applyViewportDeltas(
99 elastic_overscroll_delta
,
104 void WebLayerTreeViewImpl::RequestNewOutputSurface() {
105 if (output_surface_
.get())
106 layer_tree_host_
->SetOutputSurface(output_surface_
.Pass());
109 void WebLayerTreeViewImpl::DidFailToInitializeOutputSurface() {
110 RequestNewOutputSurface();
113 void WebLayerTreeViewImpl::DidInitializeOutputSurface() {
116 void WebLayerTreeViewImpl::WillCommit() {
119 void WebLayerTreeViewImpl::DidCommit() {
122 void WebLayerTreeViewImpl::DidCommitAndDrawFrame() {
125 void WebLayerTreeViewImpl::DidCompleteSwapBuffers() {
128 void WebLayerTreeViewImpl::setRootLayer(const blink::WebLayer
& layer
) {
129 layer_tree_host_
->SetRootLayer(
130 static_cast<const cc_blink::WebLayerImpl
*>(&layer
)->layer());
133 void WebLayerTreeViewImpl::clearRootLayer() {
134 layer_tree_host_
->SetRootLayer(scoped_refptr
<cc::Layer
>());
137 void WebLayerTreeViewImpl::setViewportSize(
138 const blink::WebSize
& device_viewport_size
) {
139 layer_tree_host_
->SetViewportSize(device_viewport_size
);
142 blink::WebSize
WebLayerTreeViewImpl::deviceViewportSize() const {
143 return layer_tree_host_
->device_viewport_size();
146 void WebLayerTreeViewImpl::setDeviceScaleFactor(float device_scale_factor
) {
147 layer_tree_host_
->SetDeviceScaleFactor(device_scale_factor
);
150 float WebLayerTreeViewImpl::deviceScaleFactor() const {
151 return layer_tree_host_
->device_scale_factor();
154 void WebLayerTreeViewImpl::setBackgroundColor(blink::WebColor color
) {
155 layer_tree_host_
->set_background_color(color
);
158 void WebLayerTreeViewImpl::setHasTransparentBackground(
159 bool has_transparent_background
) {
160 layer_tree_host_
->set_has_transparent_background(has_transparent_background
);
163 void WebLayerTreeViewImpl::setVisible(bool visible
) {
164 layer_tree_host_
->SetVisible(visible
);
167 void WebLayerTreeViewImpl::setPageScaleFactorAndLimits(float page_scale_factor
,
170 layer_tree_host_
->SetPageScaleFactorAndLimits(
171 page_scale_factor
, minimum
, maximum
);
174 void WebLayerTreeViewImpl::registerForAnimations(blink::WebLayer
* layer
) {
175 cc::Layer
* cc_layer
= static_cast<cc_blink::WebLayerImpl
*>(layer
)->layer();
176 cc_layer
->RegisterForAnimations(layer_tree_host_
->animation_registrar());
179 void WebLayerTreeViewImpl::registerViewportLayers(
180 const blink::WebLayer
* overscrollElasticityLayer
,
181 const blink::WebLayer
* pageScaleLayer
,
182 const blink::WebLayer
* innerViewportScrollLayer
,
183 const blink::WebLayer
* outerViewportScrollLayer
) {
184 layer_tree_host_
->RegisterViewportLayers(
185 // The scroll elasticity layer will only exist when using pinch virtual
187 overscrollElasticityLayer
188 ? static_cast<const cc_blink::WebLayerImpl
*>(
189 overscrollElasticityLayer
)->layer()
191 static_cast<const cc_blink::WebLayerImpl
*>(pageScaleLayer
)->layer(),
192 static_cast<const cc_blink::WebLayerImpl
*>(innerViewportScrollLayer
)
194 // The outer viewport layer will only exist when using pinch virtual
196 outerViewportScrollLayer
197 ? static_cast<const cc_blink::WebLayerImpl
*>(outerViewportScrollLayer
)
202 void WebLayerTreeViewImpl::clearViewportLayers() {
203 layer_tree_host_
->RegisterViewportLayers(scoped_refptr
<cc::Layer
>(),
204 scoped_refptr
<cc::Layer
>(),
205 scoped_refptr
<cc::Layer
>(),
206 scoped_refptr
<cc::Layer
>());
209 void WebLayerTreeViewImpl::startPageScaleAnimation(
210 const blink::WebPoint
& destination
,
212 float new_page_scale
,
213 double duration_sec
) {
214 base::TimeDelta duration
= base::TimeDelta::FromMicroseconds(
215 duration_sec
* base::Time::kMicrosecondsPerSecond
);
216 layer_tree_host_
->StartPageScaleAnimation(
217 gfx::Vector2d(destination
.x
, destination
.y
),
223 void WebLayerTreeViewImpl::setNeedsAnimate() {
224 layer_tree_host_
->SetNeedsAnimate();
227 bool WebLayerTreeViewImpl::commitRequested() const {
228 return layer_tree_host_
->CommitRequested();
231 void WebLayerTreeViewImpl::finishAllRendering() {
232 layer_tree_host_
->FinishAllRendering();
235 void WebLayerTreeViewImpl::DidCreateSurface(cc::SurfaceId id
) {
236 main_thread_compositor_task_runner_
->PostTask(
238 base::Bind(&WebLayerTreeViewImpl::DidCreateSurfaceOnMainThread
,
239 main_thread_bound_weak_ptr_
,
243 void WebLayerTreeViewImpl::DidCreateSurfaceOnMainThread(cc::SurfaceId id
) {
244 view_
->SetSurfaceId(mojo::SurfaceId::From(id
));
247 } // namespace html_viewer