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 "mojo/services/html_viewer/weblayertreeview_impl.h"
7 #include "base/message_loop/message_loop_proxy.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 "mojo/cc/context_provider_mojo.h"
14 #include "mojo/cc/output_surface_mojo.h"
15 #include "mojo/converters/surfaces/surfaces_type_converters.h"
16 #include "mojo/services/public/cpp/view_manager/view.h"
17 #include "third_party/WebKit/public/web/WebWidget.h"
21 WebLayerTreeViewImpl::WebLayerTreeViewImpl(
22 scoped_refptr
<base::MessageLoopProxy
> compositor_message_loop_proxy
,
23 SurfacesServicePtr surfaces_service
,
27 surfaces_service_(surfaces_service
.Pass()),
28 gpu_service_(gpu_service
.Pass()),
29 main_thread_compositor_task_runner_(base::MessageLoopProxy::current()),
31 main_thread_bound_weak_ptr_
= weak_factory_
.GetWeakPtr();
32 surfaces_service_
->CreateSurfaceConnection(
33 base::Bind(&WebLayerTreeViewImpl::OnSurfaceConnectionCreated
,
34 main_thread_bound_weak_ptr_
));
36 cc::LayerTreeSettings settings
;
38 // For web contents, layer transforms should scale up the contents of layers
39 // to keep content always crisp when possible.
40 settings
.layer_transforms_should_scale_layer_contents
= true;
42 cc::SharedBitmapManager
* shared_bitmap_manager
= NULL
;
43 gpu::GpuMemoryBufferManager
* gpu_memory_buffer_manager
= NULL
;
46 cc::LayerTreeHost::CreateThreaded(this,
47 shared_bitmap_manager
,
48 gpu_memory_buffer_manager
,
50 base::MessageLoopProxy::current(),
51 compositor_message_loop_proxy
,
53 DCHECK(layer_tree_host_
);
56 WebLayerTreeViewImpl::~WebLayerTreeViewImpl() {
59 void WebLayerTreeViewImpl::WillBeginMainFrame(int frame_id
) {
62 void WebLayerTreeViewImpl::DidBeginMainFrame() {
65 void WebLayerTreeViewImpl::BeginMainFrame(const cc::BeginFrameArgs
& args
) {
66 VLOG(2) << "WebLayerTreeViewImpl::BeginMainFrame";
67 double frame_time_sec
= (args
.frame_time
- base::TimeTicks()).InSecondsF();
68 double deadline_sec
= (args
.deadline
- base::TimeTicks()).InSecondsF();
69 double interval_sec
= args
.interval
.InSecondsF();
70 blink::WebBeginFrameArgs
web_begin_frame_args(
71 frame_time_sec
, deadline_sec
, interval_sec
);
72 widget_
->beginFrame(web_begin_frame_args
);
75 void WebLayerTreeViewImpl::Layout() {
79 void WebLayerTreeViewImpl::ApplyViewportDeltas(
80 const gfx::Vector2d
& inner_delta
,
81 const gfx::Vector2d
& outer_delta
,
83 float top_controls_delta
) {
84 widget_
->applyViewportDeltas(
91 void WebLayerTreeViewImpl::ApplyViewportDeltas(
92 const gfx::Vector2d
& scroll_delta
,
94 float top_controls_delta
) {
95 widget_
->applyViewportDeltas(scroll_delta
, page_scale
, top_controls_delta
);
98 void WebLayerTreeViewImpl::RequestNewOutputSurface(bool fallback
) {
99 layer_tree_host_
->SetOutputSurface(output_surface_
.Pass());
102 void WebLayerTreeViewImpl::DidInitializeOutputSurface() {
105 void WebLayerTreeViewImpl::WillCommit() {
108 void WebLayerTreeViewImpl::DidCommit() {
109 widget_
->didCommitFrameToCompositor();
112 void WebLayerTreeViewImpl::DidCommitAndDrawFrame() {
115 void WebLayerTreeViewImpl::DidCompleteSwapBuffers() {
118 void WebLayerTreeViewImpl::setSurfaceReady() {
121 void WebLayerTreeViewImpl::setRootLayer(const blink::WebLayer
& layer
) {
122 layer_tree_host_
->SetRootLayer(
123 static_cast<const cc_blink::WebLayerImpl
*>(&layer
)->layer());
126 void WebLayerTreeViewImpl::clearRootLayer() {
127 layer_tree_host_
->SetRootLayer(scoped_refptr
<cc::Layer
>());
130 void WebLayerTreeViewImpl::setViewportSize(
131 const blink::WebSize
& device_viewport_size
) {
132 layer_tree_host_
->SetViewportSize(device_viewport_size
);
135 blink::WebSize
WebLayerTreeViewImpl::deviceViewportSize() const {
136 return layer_tree_host_
->device_viewport_size();
139 void WebLayerTreeViewImpl::setDeviceScaleFactor(float device_scale_factor
) {
140 layer_tree_host_
->SetDeviceScaleFactor(device_scale_factor
);
143 float WebLayerTreeViewImpl::deviceScaleFactor() const {
144 return layer_tree_host_
->device_scale_factor();
147 void WebLayerTreeViewImpl::setBackgroundColor(blink::WebColor color
) {
148 layer_tree_host_
->set_background_color(color
);
151 void WebLayerTreeViewImpl::setHasTransparentBackground(
152 bool has_transparent_background
) {
153 layer_tree_host_
->set_has_transparent_background(has_transparent_background
);
156 void WebLayerTreeViewImpl::setOverhangBitmap(const SkBitmap
& bitmap
) {
157 layer_tree_host_
->SetOverhangBitmap(bitmap
);
160 void WebLayerTreeViewImpl::setVisible(bool visible
) {
161 layer_tree_host_
->SetVisible(visible
);
164 void WebLayerTreeViewImpl::setPageScaleFactorAndLimits(float page_scale_factor
,
167 layer_tree_host_
->SetPageScaleFactorAndLimits(
168 page_scale_factor
, minimum
, maximum
);
171 void WebLayerTreeViewImpl::registerForAnimations(blink::WebLayer
* layer
) {
172 cc::Layer
* cc_layer
= static_cast<cc_blink::WebLayerImpl
*>(layer
)->layer();
173 cc_layer
->layer_animation_controller()->SetAnimationRegistrar(
174 layer_tree_host_
->animation_registrar());
177 void WebLayerTreeViewImpl::registerViewportLayers(
178 const blink::WebLayer
* pageScaleLayer
,
179 const blink::WebLayer
* innerViewportScrollLayer
,
180 const blink::WebLayer
* outerViewportScrollLayer
) {
181 layer_tree_host_
->RegisterViewportLayers(
182 static_cast<const cc_blink::WebLayerImpl
*>(pageScaleLayer
)->layer(),
183 static_cast<const cc_blink::WebLayerImpl
*>(innerViewportScrollLayer
)
185 // The outer viewport layer will only exist when using pinch virtual
187 outerViewportScrollLayer
? static_cast<const cc_blink::WebLayerImpl
*>(
188 outerViewportScrollLayer
)->layer()
192 void WebLayerTreeViewImpl::clearViewportLayers() {
193 layer_tree_host_
->RegisterViewportLayers(scoped_refptr
<cc::Layer
>(),
194 scoped_refptr
<cc::Layer
>(),
195 scoped_refptr
<cc::Layer
>());
198 void WebLayerTreeViewImpl::startPageScaleAnimation(
199 const blink::WebPoint
& destination
,
201 float new_page_scale
,
202 double duration_sec
) {
203 base::TimeDelta duration
= base::TimeDelta::FromMicroseconds(
204 duration_sec
* base::Time::kMicrosecondsPerSecond
);
205 layer_tree_host_
->StartPageScaleAnimation(
206 gfx::Vector2d(destination
.x
, destination
.y
),
212 void WebLayerTreeViewImpl::setNeedsAnimate() {
213 layer_tree_host_
->SetNeedsAnimate();
216 bool WebLayerTreeViewImpl::commitRequested() const {
217 return layer_tree_host_
->CommitRequested();
220 void WebLayerTreeViewImpl::finishAllRendering() {
221 layer_tree_host_
->FinishAllRendering();
224 void WebLayerTreeViewImpl::OnSurfaceConnectionCreated(SurfacePtr surface
,
225 uint32_t id_namespace
) {
227 gpu_service_
->CreateOffscreenGLES2Context(GetProxy(&cb
));
228 scoped_refptr
<cc::ContextProvider
> context_provider(
229 new ContextProviderMojo(cb
.PassMessagePipe()));
230 output_surface_
.reset(new OutputSurfaceMojo(
231 this, context_provider
, surface
.Pass(), id_namespace
));
232 layer_tree_host_
->SetLayerTreeHostClientReady();
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(SurfaceId::From(id
));