Avoid checker-boarding by actually re-using transport buffers
[chromium-blink-merge.git] / components / html_viewer / web_layer_tree_view_impl.cc
blob3d3b88c7b6287fc510e29549915e5bc0f750bf4b
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)
28 : widget_(NULL),
29 view_(NULL),
30 main_thread_compositor_task_runner_(base::ThreadTaskRunnerHandle::Get()),
31 weak_factory_(this) {
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 // TODO(rjkroege): Not having a shared tile transport breaks
51 // software compositing. Add bitmap transport support.
52 cc::SharedBitmapManager* shared_bitmap_manager = nullptr;
54 cc::LayerTreeHost::InitParams params;
55 params.client = this;
56 params.shared_bitmap_manager = shared_bitmap_manager;
57 params.gpu_memory_buffer_manager = gpu_memory_buffer_manager;
58 params.settings = &settings;
59 params.task_graph_runner = task_graph_runner;
60 params.main_task_runner = main_thread_compositor_task_runner_;
62 layer_tree_host_ =
63 cc::LayerTreeHost::CreateThreaded(compositor_task_runner, &params);
64 DCHECK(layer_tree_host_);
66 if (surface && gpu_service) {
67 mojo::CommandBufferPtr cb;
68 gpu_service->CreateOffscreenGLES2Context(GetProxy(&cb));
69 scoped_refptr<cc::ContextProvider> context_provider(
70 new mojo::ContextProviderMojo(cb.PassInterface().PassHandle()));
71 output_surface_.reset(
72 new mojo::OutputSurfaceMojo(this, context_provider,
73 surface.PassInterface().PassHandle()));
75 layer_tree_host_->SetLayerTreeHostClientReady();
78 WebLayerTreeViewImpl::~WebLayerTreeViewImpl() {
79 // Destroy the LayerTreeHost before anything else as doing so ensures we're
80 // not accessed on the compositor thread (we are the LayerTreeHostClient).
81 layer_tree_host_.reset();
84 void WebLayerTreeViewImpl::WillBeginMainFrame() {
87 void WebLayerTreeViewImpl::DidBeginMainFrame() {
90 void WebLayerTreeViewImpl::BeginMainFrameNotExpectedSoon() {
93 void WebLayerTreeViewImpl::BeginMainFrame(const cc::BeginFrameArgs& args) {
94 VLOG(2) << "WebLayerTreeViewImpl::BeginMainFrame";
95 double frame_time_sec = (args.frame_time - base::TimeTicks()).InSecondsF();
96 double deadline_sec = (args.deadline - base::TimeTicks()).InSecondsF();
97 double interval_sec = args.interval.InSecondsF();
98 blink::WebBeginFrameArgs web_begin_frame_args(
99 frame_time_sec, deadline_sec, interval_sec);
100 widget_->beginFrame(web_begin_frame_args);
103 void WebLayerTreeViewImpl::Layout() {
104 widget_->layout();
107 void WebLayerTreeViewImpl::ApplyViewportDeltas(
108 const gfx::Vector2dF& inner_delta,
109 const gfx::Vector2dF& outer_delta,
110 const gfx::Vector2dF& elastic_overscroll_delta,
111 float page_scale,
112 float top_controls_delta) {
113 widget_->applyViewportDeltas(
114 inner_delta,
115 outer_delta,
116 elastic_overscroll_delta,
117 page_scale,
118 top_controls_delta);
121 void WebLayerTreeViewImpl::RequestNewOutputSurface() {
122 if (output_surface_.get())
123 layer_tree_host_->SetOutputSurface(output_surface_.Pass());
126 void WebLayerTreeViewImpl::DidFailToInitializeOutputSurface() {
127 RequestNewOutputSurface();
130 void WebLayerTreeViewImpl::DidInitializeOutputSurface() {
133 void WebLayerTreeViewImpl::WillCommit() {
136 void WebLayerTreeViewImpl::DidCommit() {
139 void WebLayerTreeViewImpl::DidCommitAndDrawFrame() {
142 // TODO(rjkroege): Wire this up to the SubmitFrame callback to improve
143 // synchronization.
144 void WebLayerTreeViewImpl::DidCompleteSwapBuffers() {
147 void WebLayerTreeViewImpl::setRootLayer(const blink::WebLayer& layer) {
148 layer_tree_host_->SetRootLayer(
149 static_cast<const cc_blink::WebLayerImpl*>(&layer)->layer());
152 void WebLayerTreeViewImpl::clearRootLayer() {
153 layer_tree_host_->SetRootLayer(scoped_refptr<cc::Layer>());
156 void WebLayerTreeViewImpl::setViewportSize(
157 const blink::WebSize& device_viewport_size) {
158 layer_tree_host_->SetViewportSize(device_viewport_size);
161 blink::WebSize WebLayerTreeViewImpl::deviceViewportSize() const {
162 return layer_tree_host_->device_viewport_size();
165 void WebLayerTreeViewImpl::setDeviceScaleFactor(float device_scale_factor) {
166 layer_tree_host_->SetDeviceScaleFactor(device_scale_factor);
169 float WebLayerTreeViewImpl::deviceScaleFactor() const {
170 return layer_tree_host_->device_scale_factor();
173 void WebLayerTreeViewImpl::setBackgroundColor(blink::WebColor color) {
174 layer_tree_host_->set_background_color(color);
177 void WebLayerTreeViewImpl::setHasTransparentBackground(
178 bool has_transparent_background) {
179 layer_tree_host_->set_has_transparent_background(has_transparent_background);
182 void WebLayerTreeViewImpl::setVisible(bool visible) {
183 layer_tree_host_->SetVisible(visible);
186 void WebLayerTreeViewImpl::setPageScaleFactorAndLimits(float page_scale_factor,
187 float minimum,
188 float maximum) {
189 layer_tree_host_->SetPageScaleFactorAndLimits(
190 page_scale_factor, minimum, maximum);
193 void WebLayerTreeViewImpl::registerForAnimations(blink::WebLayer* layer) {
194 cc::Layer* cc_layer = static_cast<cc_blink::WebLayerImpl*>(layer)->layer();
195 cc_layer->RegisterForAnimations(layer_tree_host_->animation_registrar());
198 void WebLayerTreeViewImpl::registerViewportLayers(
199 const blink::WebLayer* overscrollElasticityLayer,
200 const blink::WebLayer* pageScaleLayer,
201 const blink::WebLayer* innerViewportScrollLayer,
202 const blink::WebLayer* outerViewportScrollLayer) {
203 layer_tree_host_->RegisterViewportLayers(
204 // The scroll elasticity layer will only exist when using pinch virtual
205 // viewports.
206 overscrollElasticityLayer
207 ? static_cast<const cc_blink::WebLayerImpl*>(
208 overscrollElasticityLayer)->layer()
209 : NULL,
210 static_cast<const cc_blink::WebLayerImpl*>(pageScaleLayer)->layer(),
211 static_cast<const cc_blink::WebLayerImpl*>(innerViewportScrollLayer)
212 ->layer(),
213 // The outer viewport layer will only exist when using pinch virtual
214 // viewports.
215 outerViewportScrollLayer
216 ? static_cast<const cc_blink::WebLayerImpl*>(outerViewportScrollLayer)
217 ->layer()
218 : NULL);
221 void WebLayerTreeViewImpl::clearViewportLayers() {
222 layer_tree_host_->RegisterViewportLayers(scoped_refptr<cc::Layer>(),
223 scoped_refptr<cc::Layer>(),
224 scoped_refptr<cc::Layer>(),
225 scoped_refptr<cc::Layer>());
228 void WebLayerTreeViewImpl::startPageScaleAnimation(
229 const blink::WebPoint& destination,
230 bool use_anchor,
231 float new_page_scale,
232 double duration_sec) {
233 base::TimeDelta duration = base::TimeDelta::FromMicroseconds(
234 duration_sec * base::Time::kMicrosecondsPerSecond);
235 layer_tree_host_->StartPageScaleAnimation(
236 gfx::Vector2d(destination.x, destination.y),
237 use_anchor,
238 new_page_scale,
239 duration);
242 void WebLayerTreeViewImpl::setNeedsAnimate() {
243 layer_tree_host_->SetNeedsAnimate();
246 void WebLayerTreeViewImpl::finishAllRendering() {
247 layer_tree_host_->FinishAllRendering();
250 void WebLayerTreeViewImpl::DidCreateSurface(cc::SurfaceId id) {
251 main_thread_compositor_task_runner_->PostTask(
252 FROM_HERE,
253 base::Bind(&WebLayerTreeViewImpl::DidCreateSurfaceOnMainThread,
254 main_thread_bound_weak_ptr_,
255 id));
258 void WebLayerTreeViewImpl::DidCreateSurfaceOnMainThread(cc::SurfaceId id) {
259 view_->SetSurfaceId(mojo::SurfaceId::From(id));
262 } // namespace html_viewer