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"
18 #include "ui/gfx/buffer_types.h"
20 namespace html_viewer
{
22 WebLayerTreeViewImpl::WebLayerTreeViewImpl(
23 scoped_refptr
<base::SingleThreadTaskRunner
> compositor_task_runner
,
24 gpu::GpuMemoryBufferManager
* gpu_memory_buffer_manager
,
25 cc::TaskGraphRunner
* task_graph_runner
,
26 mojo::SurfacePtr surface
,
27 mojo::GpuPtr gpu_service
)
30 main_thread_compositor_task_runner_(base::ThreadTaskRunnerHandle::Get()),
32 main_thread_bound_weak_ptr_
= weak_factory_
.GetWeakPtr();
34 cc::LayerTreeSettings settings
;
36 // Must match the value of
37 // blink::RuntimeEnabledFeature::slimmingPaintEnabled()
38 settings
.use_display_lists
= true;
40 settings
.use_image_texture_targets
= std::vector
<unsigned>(
41 static_cast<size_t>(gfx::BufferFormat::LAST
) + 1, GL_TEXTURE_2D
);
42 settings
.use_one_copy
= true;
43 // TODO(jam): use multiple compositor raster threads and set gather_pixel_refs
44 // accordingly (see content).
46 // For web contents, layer transforms should scale up the contents of layers
47 // to keep content always crisp when possible.
48 settings
.layer_transforms_should_scale_layer_contents
= true;
50 cc::SharedBitmapManager
* shared_bitmap_manager
= nullptr;
52 cc::LayerTreeHost::InitParams params
;
54 params
.shared_bitmap_manager
= shared_bitmap_manager
;
55 params
.gpu_memory_buffer_manager
= gpu_memory_buffer_manager
;
56 params
.settings
= &settings
;
57 params
.task_graph_runner
= task_graph_runner
;
58 params
.main_task_runner
= main_thread_compositor_task_runner_
;
61 cc::LayerTreeHost::CreateThreaded(compositor_task_runner
, ¶ms
);
62 DCHECK(layer_tree_host_
);
64 if (surface
&& gpu_service
) {
65 mojo::CommandBufferPtr cb
;
66 gpu_service
->CreateOffscreenGLES2Context(GetProxy(&cb
));
67 scoped_refptr
<cc::ContextProvider
> context_provider(
68 new mojo::ContextProviderMojo(cb
.PassInterface().PassHandle()));
69 output_surface_
.reset(
70 new mojo::OutputSurfaceMojo(this, context_provider
,
71 surface
.PassInterface().PassHandle()));
73 layer_tree_host_
->SetLayerTreeHostClientReady();
76 WebLayerTreeViewImpl::~WebLayerTreeViewImpl() {
77 // Destroy the LayerTreeHost before anything else as doing so ensures we're
78 // not accessed on the compositor thread (we are the LayerTreeHostClient).
79 layer_tree_host_
.reset();
82 void WebLayerTreeViewImpl::WillBeginMainFrame() {
85 void WebLayerTreeViewImpl::DidBeginMainFrame() {
88 void WebLayerTreeViewImpl::BeginMainFrameNotExpectedSoon() {
91 void WebLayerTreeViewImpl::BeginMainFrame(const cc::BeginFrameArgs
& args
) {
92 VLOG(2) << "WebLayerTreeViewImpl::BeginMainFrame";
93 double frame_time_sec
= (args
.frame_time
- base::TimeTicks()).InSecondsF();
94 double deadline_sec
= (args
.deadline
- base::TimeTicks()).InSecondsF();
95 double interval_sec
= args
.interval
.InSecondsF();
96 blink::WebBeginFrameArgs
web_begin_frame_args(
97 frame_time_sec
, deadline_sec
, interval_sec
);
98 widget_
->beginFrame(web_begin_frame_args
);
101 void WebLayerTreeViewImpl::Layout() {
105 void WebLayerTreeViewImpl::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
) {
111 widget_
->applyViewportDeltas(
114 elastic_overscroll_delta
,
119 void WebLayerTreeViewImpl::RequestNewOutputSurface() {
120 if (output_surface_
.get())
121 layer_tree_host_
->SetOutputSurface(output_surface_
.Pass());
124 void WebLayerTreeViewImpl::DidFailToInitializeOutputSurface() {
125 RequestNewOutputSurface();
128 void WebLayerTreeViewImpl::DidInitializeOutputSurface() {
131 void WebLayerTreeViewImpl::WillCommit() {
134 void WebLayerTreeViewImpl::DidCommit() {
137 void WebLayerTreeViewImpl::DidCommitAndDrawFrame() {
140 void WebLayerTreeViewImpl::DidCompleteSwapBuffers() {
143 void WebLayerTreeViewImpl::setRootLayer(const blink::WebLayer
& layer
) {
144 layer_tree_host_
->SetRootLayer(
145 static_cast<const cc_blink::WebLayerImpl
*>(&layer
)->layer());
148 void WebLayerTreeViewImpl::clearRootLayer() {
149 layer_tree_host_
->SetRootLayer(scoped_refptr
<cc::Layer
>());
152 void WebLayerTreeViewImpl::setViewportSize(
153 const blink::WebSize
& device_viewport_size
) {
154 layer_tree_host_
->SetViewportSize(device_viewport_size
);
157 blink::WebSize
WebLayerTreeViewImpl::deviceViewportSize() const {
158 return layer_tree_host_
->device_viewport_size();
161 void WebLayerTreeViewImpl::setDeviceScaleFactor(float device_scale_factor
) {
162 layer_tree_host_
->SetDeviceScaleFactor(device_scale_factor
);
165 float WebLayerTreeViewImpl::deviceScaleFactor() const {
166 return layer_tree_host_
->device_scale_factor();
169 void WebLayerTreeViewImpl::setBackgroundColor(blink::WebColor color
) {
170 layer_tree_host_
->set_background_color(color
);
173 void WebLayerTreeViewImpl::setHasTransparentBackground(
174 bool has_transparent_background
) {
175 layer_tree_host_
->set_has_transparent_background(has_transparent_background
);
178 void WebLayerTreeViewImpl::setVisible(bool visible
) {
179 layer_tree_host_
->SetVisible(visible
);
182 void WebLayerTreeViewImpl::setPageScaleFactorAndLimits(float page_scale_factor
,
185 layer_tree_host_
->SetPageScaleFactorAndLimits(
186 page_scale_factor
, minimum
, maximum
);
189 void WebLayerTreeViewImpl::registerForAnimations(blink::WebLayer
* layer
) {
190 cc::Layer
* cc_layer
= static_cast<cc_blink::WebLayerImpl
*>(layer
)->layer();
191 cc_layer
->RegisterForAnimations(layer_tree_host_
->animation_registrar());
194 void WebLayerTreeViewImpl::registerViewportLayers(
195 const blink::WebLayer
* overscrollElasticityLayer
,
196 const blink::WebLayer
* pageScaleLayer
,
197 const blink::WebLayer
* innerViewportScrollLayer
,
198 const blink::WebLayer
* outerViewportScrollLayer
) {
199 layer_tree_host_
->RegisterViewportLayers(
200 // The scroll elasticity layer will only exist when using pinch virtual
202 overscrollElasticityLayer
203 ? static_cast<const cc_blink::WebLayerImpl
*>(
204 overscrollElasticityLayer
)->layer()
206 static_cast<const cc_blink::WebLayerImpl
*>(pageScaleLayer
)->layer(),
207 static_cast<const cc_blink::WebLayerImpl
*>(innerViewportScrollLayer
)
209 // The outer viewport layer will only exist when using pinch virtual
211 outerViewportScrollLayer
212 ? static_cast<const cc_blink::WebLayerImpl
*>(outerViewportScrollLayer
)
217 void WebLayerTreeViewImpl::clearViewportLayers() {
218 layer_tree_host_
->RegisterViewportLayers(scoped_refptr
<cc::Layer
>(),
219 scoped_refptr
<cc::Layer
>(),
220 scoped_refptr
<cc::Layer
>(),
221 scoped_refptr
<cc::Layer
>());
224 void WebLayerTreeViewImpl::startPageScaleAnimation(
225 const blink::WebPoint
& destination
,
227 float new_page_scale
,
228 double duration_sec
) {
229 base::TimeDelta duration
= base::TimeDelta::FromMicroseconds(
230 duration_sec
* base::Time::kMicrosecondsPerSecond
);
231 layer_tree_host_
->StartPageScaleAnimation(
232 gfx::Vector2d(destination
.x
, destination
.y
),
238 void WebLayerTreeViewImpl::setNeedsAnimate() {
239 layer_tree_host_
->SetNeedsAnimate();
242 void WebLayerTreeViewImpl::finishAllRendering() {
243 layer_tree_host_
->FinishAllRendering();
246 void WebLayerTreeViewImpl::DidCreateSurface(cc::SurfaceId id
) {
247 main_thread_compositor_task_runner_
->PostTask(
249 base::Bind(&WebLayerTreeViewImpl::DidCreateSurfaceOnMainThread
,
250 main_thread_bound_weak_ptr_
,
254 void WebLayerTreeViewImpl::DidCreateSurfaceOnMainThread(cc::SurfaceId id
) {
255 view_
->SetSurfaceId(mojo::SurfaceId::From(id
));
258 } // namespace html_viewer