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
)
28 main_thread_compositor_task_runner_(base::ThreadTaskRunnerHandle::Get()),
30 main_thread_bound_weak_ptr_
= weak_factory_
.GetWeakPtr();
32 cc::LayerTreeSettings settings
;
34 // Must match the value of
35 // blink::RuntimeEnabledFeature::slimmingPaintEnabled()
36 settings
.use_display_lists
= true;
38 settings
.use_image_texture_targets
= std::vector
<unsigned>(
39 static_cast<size_t>(gfx::BufferFormat::LAST
) + 1, GL_TEXTURE_2D
);
40 // TODO(jam): use multiple compositor raster threads and set gather_pixel_refs
41 // accordingly (see content).
43 // For web contents, layer transforms should scale up the contents of layers
44 // to keep content always crisp when possible.
45 settings
.layer_transforms_should_scale_layer_contents
= true;
47 // TODO(rjkroege): Not having a shared tile transport breaks
48 // software compositing. Add bitmap transport support.
49 cc::SharedBitmapManager
* shared_bitmap_manager
= nullptr;
51 cc::LayerTreeHost::InitParams params
;
53 params
.shared_bitmap_manager
= shared_bitmap_manager
;
54 params
.gpu_memory_buffer_manager
= gpu_memory_buffer_manager
;
55 params
.settings
= &settings
;
56 params
.task_graph_runner
= task_graph_runner
;
57 params
.main_task_runner
= main_thread_compositor_task_runner_
;
60 cc::LayerTreeHost::CreateThreaded(compositor_task_runner
, ¶ms
);
61 DCHECK(layer_tree_host_
);
64 void WebLayerTreeViewImpl::Initialize(mojo::GpuPtr gpu_service
,
66 blink::WebWidget
* widget
) {
70 mojo::CommandBufferPtr cb
;
71 gpu_service
->CreateOffscreenGLES2Context(GetProxy(&cb
));
72 scoped_refptr
<cc::ContextProvider
> context_provider(
73 new mojo::ContextProviderMojo(cb
.PassInterface().PassHandle()));
74 output_surface_
.reset(
75 new mojo::OutputSurfaceMojo(context_provider
, view_
->RequestSurface()));
77 layer_tree_host_
->SetLayerTreeHostClientReady();
80 WebLayerTreeViewImpl::~WebLayerTreeViewImpl() {
81 // Destroy the LayerTreeHost before anything else as doing so ensures we're
82 // not accessed on the compositor thread (we are the LayerTreeHostClient).
83 layer_tree_host_
.reset();
86 void WebLayerTreeViewImpl::WillBeginMainFrame() {
89 void WebLayerTreeViewImpl::DidBeginMainFrame() {
92 void WebLayerTreeViewImpl::BeginMainFrameNotExpectedSoon() {
95 void WebLayerTreeViewImpl::BeginMainFrame(const cc::BeginFrameArgs
& args
) {
96 VLOG(2) << "WebLayerTreeViewImpl::BeginMainFrame";
97 double frame_time_sec
= (args
.frame_time
- base::TimeTicks()).InSecondsF();
98 double deadline_sec
= (args
.deadline
- base::TimeTicks()).InSecondsF();
99 double interval_sec
= args
.interval
.InSecondsF();
100 blink::WebBeginFrameArgs
web_begin_frame_args(
101 frame_time_sec
, deadline_sec
, interval_sec
);
102 widget_
->beginFrame(web_begin_frame_args
);
105 void WebLayerTreeViewImpl::Layout() {
109 void WebLayerTreeViewImpl::ApplyViewportDeltas(
110 const gfx::Vector2dF
& inner_delta
,
111 const gfx::Vector2dF
& outer_delta
,
112 const gfx::Vector2dF
& elastic_overscroll_delta
,
114 float top_controls_delta
) {
115 widget_
->applyViewportDeltas(
118 elastic_overscroll_delta
,
123 void WebLayerTreeViewImpl::RequestNewOutputSurface() {
124 if (output_surface_
.get())
125 layer_tree_host_
->SetOutputSurface(output_surface_
.Pass());
128 void WebLayerTreeViewImpl::DidFailToInitializeOutputSurface() {
129 RequestNewOutputSurface();
132 void WebLayerTreeViewImpl::DidInitializeOutputSurface() {
135 void WebLayerTreeViewImpl::WillCommit() {
138 void WebLayerTreeViewImpl::DidCommit() {
141 void WebLayerTreeViewImpl::DidCommitAndDrawFrame() {
144 // TODO(rjkroege): Wire this up to the SubmitFrame callback to improve
146 void WebLayerTreeViewImpl::DidCompleteSwapBuffers() {
149 void WebLayerTreeViewImpl::setRootLayer(const blink::WebLayer
& layer
) {
150 layer_tree_host_
->SetRootLayer(
151 static_cast<const cc_blink::WebLayerImpl
*>(&layer
)->layer());
154 void WebLayerTreeViewImpl::clearRootLayer() {
155 layer_tree_host_
->SetRootLayer(scoped_refptr
<cc::Layer
>());
158 void WebLayerTreeViewImpl::setViewportSize(
159 const blink::WebSize
& device_viewport_size
) {
160 layer_tree_host_
->SetViewportSize(device_viewport_size
);
163 blink::WebSize
WebLayerTreeViewImpl::deviceViewportSize() const {
164 return layer_tree_host_
->device_viewport_size();
167 void WebLayerTreeViewImpl::setDeviceScaleFactor(float device_scale_factor
) {
168 layer_tree_host_
->SetDeviceScaleFactor(device_scale_factor
);
171 float WebLayerTreeViewImpl::deviceScaleFactor() const {
172 return layer_tree_host_
->device_scale_factor();
175 void WebLayerTreeViewImpl::setBackgroundColor(blink::WebColor color
) {
176 layer_tree_host_
->set_background_color(color
);
179 void WebLayerTreeViewImpl::setHasTransparentBackground(
180 bool has_transparent_background
) {
181 layer_tree_host_
->set_has_transparent_background(has_transparent_background
);
184 void WebLayerTreeViewImpl::setVisible(bool visible
) {
185 layer_tree_host_
->SetVisible(visible
);
188 void WebLayerTreeViewImpl::setPageScaleFactorAndLimits(float page_scale_factor
,
191 layer_tree_host_
->SetPageScaleFactorAndLimits(
192 page_scale_factor
, minimum
, maximum
);
195 void WebLayerTreeViewImpl::registerForAnimations(blink::WebLayer
* layer
) {
196 cc::Layer
* cc_layer
= static_cast<cc_blink::WebLayerImpl
*>(layer
)->layer();
197 cc_layer
->RegisterForAnimations(layer_tree_host_
->animation_registrar());
200 void WebLayerTreeViewImpl::registerViewportLayers(
201 const blink::WebLayer
* overscrollElasticityLayer
,
202 const blink::WebLayer
* pageScaleLayer
,
203 const blink::WebLayer
* innerViewportScrollLayer
,
204 const blink::WebLayer
* outerViewportScrollLayer
) {
205 layer_tree_host_
->RegisterViewportLayers(
206 // The scroll elasticity layer will only exist when using pinch virtual
208 overscrollElasticityLayer
209 ? static_cast<const cc_blink::WebLayerImpl
*>(
210 overscrollElasticityLayer
)->layer()
212 static_cast<const cc_blink::WebLayerImpl
*>(pageScaleLayer
)->layer(),
213 static_cast<const cc_blink::WebLayerImpl
*>(innerViewportScrollLayer
)
215 // The outer viewport layer will only exist when using pinch virtual
217 outerViewportScrollLayer
218 ? static_cast<const cc_blink::WebLayerImpl
*>(outerViewportScrollLayer
)
223 void WebLayerTreeViewImpl::clearViewportLayers() {
224 layer_tree_host_
->RegisterViewportLayers(scoped_refptr
<cc::Layer
>(),
225 scoped_refptr
<cc::Layer
>(),
226 scoped_refptr
<cc::Layer
>(),
227 scoped_refptr
<cc::Layer
>());
230 void WebLayerTreeViewImpl::startPageScaleAnimation(
231 const blink::WebPoint
& destination
,
233 float new_page_scale
,
234 double duration_sec
) {
235 base::TimeDelta duration
= base::TimeDelta::FromMicroseconds(
236 duration_sec
* base::Time::kMicrosecondsPerSecond
);
237 layer_tree_host_
->StartPageScaleAnimation(
238 gfx::Vector2d(destination
.x
, destination
.y
),
244 void WebLayerTreeViewImpl::setNeedsAnimate() {
245 layer_tree_host_
->SetNeedsAnimate();
248 void WebLayerTreeViewImpl::finishAllRendering() {
249 layer_tree_host_
->FinishAllRendering();
252 } // namespace html_viewer