1 // Copyright (c) 2013 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 "content/renderer/gpu/render_widget_compositor.h"
10 #include "base/command_line.h"
11 #include "base/location.h"
12 #include "base/logging.h"
13 #include "base/profiler/scoped_tracker.h"
14 #include "base/single_thread_task_runner.h"
15 #include "base/strings/string_number_conversions.h"
16 #include "base/synchronization/lock.h"
17 #include "base/sys_info.h"
18 #include "base/thread_task_runner_handle.h"
19 #include "base/time/time.h"
20 #include "base/values.h"
21 #include "cc/base/switches.h"
22 #include "cc/blink/web_layer_impl.h"
23 #include "cc/debug/layer_tree_debug_state.h"
24 #include "cc/debug/micro_benchmark.h"
25 #include "cc/input/layer_selection_bound.h"
26 #include "cc/layers/layer.h"
27 #include "cc/output/begin_frame_args.h"
28 #include "cc/output/copy_output_request.h"
29 #include "cc/output/copy_output_result.h"
30 #include "cc/output/latency_info_swap_promise.h"
31 #include "cc/output/swap_promise.h"
32 #include "cc/resources/single_release_callback.h"
33 #include "cc/scheduler/begin_frame_source.h"
34 #include "cc/trees/latency_info_swap_promise_monitor.h"
35 #include "cc/trees/layer_tree_host.h"
36 #include "components/scheduler/renderer/renderer_scheduler.h"
37 #include "content/common/content_switches_internal.h"
38 #include "content/common/gpu/client/context_provider_command_buffer.h"
39 #include "content/public/common/content_switches.h"
40 #include "content/renderer/input/input_handler_manager.h"
41 #include "gpu/command_buffer/client/gles2_interface.h"
42 #include "third_party/WebKit/public/platform/WebCompositeAndReadbackAsyncCallback.h"
43 #include "third_party/WebKit/public/platform/WebLayoutAndPaintAsyncCallback.h"
44 #include "third_party/WebKit/public/platform/WebSize.h"
45 #include "third_party/WebKit/public/web/WebKit.h"
46 #include "third_party/WebKit/public/web/WebRuntimeFeatures.h"
47 #include "third_party/WebKit/public/web/WebSelection.h"
48 #include "third_party/WebKit/public/web/WebWidget.h"
49 #include "ui/gl/gl_switches.h"
50 #include "ui/native_theme/native_theme_switches.h"
52 #if defined(OS_ANDROID)
53 #include "content/renderer/android/synchronous_compositor_factory.h"
54 #include "ui/gfx/android/device_display_info.h"
65 using blink::WebBeginFrameArgs
;
66 using blink::WebFloatPoint
;
68 using blink::WebSelection
;
70 using blink::WebTopControlsState
;
75 bool GetSwitchValueAsInt(const base::CommandLine
& command_line
,
76 const std::string
& switch_string
,
80 std::string string_value
= command_line
.GetSwitchValueASCII(switch_string
);
82 if (base::StringToInt(string_value
, &int_value
) &&
83 int_value
>= min_value
&& int_value
<= max_value
) {
87 LOG(WARNING
) << "Failed to parse switch " << switch_string
<< ": " <<
93 cc::LayerSelectionBound
ConvertWebSelectionBound(
94 const WebSelection
& web_selection
,
96 cc::LayerSelectionBound cc_bound
;
97 if (web_selection
.isNone())
100 const blink::WebSelectionBound
& web_bound
=
101 is_start
? web_selection
.start() : web_selection
.end();
102 DCHECK(web_bound
.layerId
);
103 cc_bound
.type
= cc::SELECTION_BOUND_CENTER
;
104 if (web_selection
.isRange()) {
106 cc_bound
.type
= web_bound
.isTextDirectionRTL
? cc::SELECTION_BOUND_RIGHT
107 : cc::SELECTION_BOUND_LEFT
;
109 cc_bound
.type
= web_bound
.isTextDirectionRTL
? cc::SELECTION_BOUND_LEFT
110 : cc::SELECTION_BOUND_RIGHT
;
113 cc_bound
.layer_id
= web_bound
.layerId
;
114 cc_bound
.edge_top
= gfx::Point(web_bound
.edgeTopInLayer
);
115 cc_bound
.edge_bottom
= gfx::Point(web_bound
.edgeBottomInLayer
);
119 cc::LayerSelection
ConvertWebSelection(const WebSelection
& web_selection
) {
120 cc::LayerSelection cc_selection
;
121 cc_selection
.start
= ConvertWebSelectionBound(web_selection
, true);
122 cc_selection
.end
= ConvertWebSelectionBound(web_selection
, false);
123 cc_selection
.is_editable
= web_selection
.isEditable();
124 cc_selection
.is_empty_text_form_control
=
125 web_selection
.isEmptyTextFormControl();
129 gfx::Size
CalculateDefaultTileSize(RenderWidget
* widget
) {
130 int default_tile_size
= 256;
131 #if defined(OS_ANDROID)
132 // TODO(epenner): unify this for all platforms if it
133 // makes sense (http://crbug.com/159524)
135 gfx::DeviceDisplayInfo info
;
136 bool real_size_supported
= true;
137 int display_width
= info
.GetPhysicalDisplayWidth();
138 int display_height
= info
.GetPhysicalDisplayHeight();
139 if (display_width
== 0 || display_height
== 0) {
140 real_size_supported
= false;
141 display_width
= info
.GetDisplayWidth();
142 display_height
= info
.GetDisplayHeight();
145 int portrait_width
= std::min(display_width
, display_height
);
146 int landscape_width
= std::max(display_width
, display_height
);
148 if (real_size_supported
) {
149 // Maximum HD dimensions should be 768x1280
150 // Maximum FHD dimensions should be 1200x1920
151 if (portrait_width
> 768 || landscape_width
> 1280)
152 default_tile_size
= 384;
153 if (portrait_width
> 1200 || landscape_width
> 1920)
154 default_tile_size
= 512;
156 // Adjust for some resolutions that barely straddle an extra
157 // tile when in portrait mode. This helps worst case scroll/raster
158 // by not needing a full extra tile for each row.
159 if (default_tile_size
== 256 && portrait_width
== 768)
160 default_tile_size
+= 32;
161 if (default_tile_size
== 384 && portrait_width
== 1200)
162 default_tile_size
+= 32;
164 // We don't know the exact resolution due to screen controls etc.
165 // So this just estimates the values above using tile counts.
166 int numTiles
= (display_width
* display_height
) / (256 * 256);
168 default_tile_size
= 384;
170 default_tile_size
= 512;
172 #elif defined(OS_CHROMEOS)
173 // Use 512 for high DPI (dsf=2.0f) devices.
174 if (widget
->screen_info().deviceScaleFactor
>= 2.0f
)
175 default_tile_size
= 512;
178 return gfx::Size(default_tile_size
, default_tile_size
);
181 // Check cc::TopControlsState, and blink::WebTopControlsState
183 static_assert(int(blink::WebTopControlsBoth
) == int(cc::BOTH
),
184 "mismatching enums: BOTH");
185 static_assert(int(blink::WebTopControlsHidden
) == int(cc::HIDDEN
),
186 "mismatching enums: HIDDEN");
187 static_assert(int(blink::WebTopControlsShown
) == int(cc::SHOWN
),
188 "mismatching enums: SHOWN");
190 static cc::TopControlsState
ConvertTopControlsState(
191 WebTopControlsState state
) {
192 return static_cast<cc::TopControlsState
>(state
);
198 scoped_ptr
<RenderWidgetCompositor
> RenderWidgetCompositor::Create(
199 RenderWidget
* widget
,
200 CompositorDependencies
* compositor_deps
) {
201 scoped_ptr
<RenderWidgetCompositor
> compositor(
202 new RenderWidgetCompositor(widget
, compositor_deps
));
203 compositor
->Initialize();
207 RenderWidgetCompositor::RenderWidgetCompositor(
208 RenderWidget
* widget
,
209 CompositorDependencies
* compositor_deps
)
210 : num_failed_recreate_attempts_(0),
212 compositor_deps_(compositor_deps
),
213 layout_and_paint_async_callback_(nullptr),
214 weak_factory_(this) {
217 void RenderWidgetCompositor::Initialize() {
218 base::CommandLine
* cmd
= base::CommandLine::ForCurrentProcess();
220 cc::LayerTreeSettings settings
;
222 // For web contents, layer transforms should scale up the contents of layers
223 // to keep content always crisp when possible.
224 settings
.layer_transforms_should_scale_layer_contents
= true;
226 settings
.renderer_settings
.disable_gpu_vsync
=
227 cmd
->HasSwitch(switches::kDisableGpuVsync
);
228 settings
.main_frame_before_activation_enabled
=
229 cmd
->HasSwitch(cc::switches::kEnableMainFrameBeforeActivation
) &&
230 !cmd
->HasSwitch(cc::switches::kDisableMainFrameBeforeActivation
);
231 settings
.report_overscroll_only_for_scrollable_axes
=
232 !compositor_deps_
->IsElasticOverscrollEnabled();
233 settings
.accelerated_animation_enabled
=
234 !cmd
->HasSwitch(cc::switches::kDisableThreadedAnimation
);
235 settings
.use_display_lists
= cmd
->HasSwitch(switches::kEnableSlimmingPaint
);
236 if (cmd
->HasSwitch(switches::kEnableCompositorAnimationTimelines
)) {
237 settings
.use_compositor_animation_timelines
= true;
238 blink::WebRuntimeFeatures::enableCompositorAnimationTimelines(true);
241 settings
.default_tile_size
= CalculateDefaultTileSize(widget_
);
242 if (cmd
->HasSwitch(switches::kDefaultTileWidth
)) {
244 GetSwitchValueAsInt(*cmd
,
245 switches::kDefaultTileWidth
,
247 std::numeric_limits
<int>::max(),
249 settings
.default_tile_size
.set_width(tile_width
);
251 if (cmd
->HasSwitch(switches::kDefaultTileHeight
)) {
253 GetSwitchValueAsInt(*cmd
,
254 switches::kDefaultTileHeight
,
256 std::numeric_limits
<int>::max(),
258 settings
.default_tile_size
.set_height(tile_height
);
261 int max_untiled_layer_width
= settings
.max_untiled_layer_size
.width();
262 if (cmd
->HasSwitch(switches::kMaxUntiledLayerWidth
)) {
263 GetSwitchValueAsInt(*cmd
, switches::kMaxUntiledLayerWidth
, 1,
264 std::numeric_limits
<int>::max(),
265 &max_untiled_layer_width
);
267 int max_untiled_layer_height
= settings
.max_untiled_layer_size
.height();
268 if (cmd
->HasSwitch(switches::kMaxUntiledLayerHeight
)) {
269 GetSwitchValueAsInt(*cmd
, switches::kMaxUntiledLayerHeight
, 1,
270 std::numeric_limits
<int>::max(),
271 &max_untiled_layer_height
);
274 settings
.max_untiled_layer_size
= gfx::Size(max_untiled_layer_width
,
275 max_untiled_layer_height
);
277 settings
.gpu_rasterization_msaa_sample_count
=
278 compositor_deps_
->GetGpuRasterizationMSAASampleCount();
279 settings
.impl_side_painting
= compositor_deps_
->IsImplSidePaintingEnabled();
280 settings
.gpu_rasterization_forced
=
281 compositor_deps_
->IsGpuRasterizationForced();
282 settings
.gpu_rasterization_enabled
=
283 compositor_deps_
->IsGpuRasterizationEnabled();
285 settings
.can_use_lcd_text
= compositor_deps_
->IsLcdTextEnabled();
286 settings
.use_distance_field_text
=
287 compositor_deps_
->IsDistanceFieldTextEnabled();
288 settings
.use_zero_copy
= compositor_deps_
->IsZeroCopyEnabled();
289 settings
.use_one_copy
= compositor_deps_
->IsOneCopyEnabled();
290 settings
.enable_elastic_overscroll
=
291 compositor_deps_
->IsElasticOverscrollEnabled();
292 settings
.use_image_texture_target
= compositor_deps_
->GetImageTextureTarget();
293 settings
.gather_pixel_refs
= compositor_deps_
->IsGatherPixelRefsEnabled();
295 if (cmd
->HasSwitch(cc::switches::kTopControlsShowThreshold
)) {
296 std::string top_threshold_str
=
297 cmd
->GetSwitchValueASCII(cc::switches::kTopControlsShowThreshold
);
298 double show_threshold
;
299 if (base::StringToDouble(top_threshold_str
, &show_threshold
) &&
300 show_threshold
>= 0.f
&& show_threshold
<= 1.f
)
301 settings
.top_controls_show_threshold
= show_threshold
;
304 if (cmd
->HasSwitch(cc::switches::kTopControlsHideThreshold
)) {
305 std::string top_threshold_str
=
306 cmd
->GetSwitchValueASCII(cc::switches::kTopControlsHideThreshold
);
307 double hide_threshold
;
308 if (base::StringToDouble(top_threshold_str
, &hide_threshold
) &&
309 hide_threshold
>= 0.f
&& hide_threshold
<= 1.f
)
310 settings
.top_controls_hide_threshold
= hide_threshold
;
313 settings
.verify_property_trees
=
314 cmd
->HasSwitch(cc::switches::kEnablePropertyTreeVerification
) &&
315 settings
.impl_side_painting
;
316 settings
.renderer_settings
.allow_antialiasing
&=
317 !cmd
->HasSwitch(cc::switches::kDisableCompositedAntialiasing
);
318 // The means the renderer compositor has 2 possible modes:
319 // - Threaded compositing with a scheduler.
320 // - Single threaded compositing without a scheduler (for layout tests only).
321 // Using the scheduler in layout tests introduces additional composite steps
322 // that create flakiness.
323 settings
.single_thread_proxy_scheduler
= false;
325 // These flags should be mirrored by UI versions in ui/compositor/.
326 settings
.initial_debug_state
.show_debug_borders
=
327 cmd
->HasSwitch(cc::switches::kShowCompositedLayerBorders
);
328 settings
.initial_debug_state
.show_fps_counter
=
329 cmd
->HasSwitch(cc::switches::kShowFPSCounter
);
330 settings
.initial_debug_state
.show_layer_animation_bounds_rects
=
331 cmd
->HasSwitch(cc::switches::kShowLayerAnimationBounds
);
332 settings
.initial_debug_state
.show_paint_rects
=
333 cmd
->HasSwitch(switches::kShowPaintRects
);
334 settings
.initial_debug_state
.show_property_changed_rects
=
335 cmd
->HasSwitch(cc::switches::kShowPropertyChangedRects
);
336 settings
.initial_debug_state
.show_surface_damage_rects
=
337 cmd
->HasSwitch(cc::switches::kShowSurfaceDamageRects
);
338 settings
.initial_debug_state
.show_screen_space_rects
=
339 cmd
->HasSwitch(cc::switches::kShowScreenSpaceRects
);
340 settings
.initial_debug_state
.show_replica_screen_space_rects
=
341 cmd
->HasSwitch(cc::switches::kShowReplicaScreenSpaceRects
);
343 settings
.initial_debug_state
.SetRecordRenderingStats(
344 cmd
->HasSwitch(cc::switches::kEnableGpuBenchmarking
));
346 if (cmd
->HasSwitch(cc::switches::kSlowDownRasterScaleFactor
)) {
347 const int kMinSlowDownScaleFactor
= 0;
348 const int kMaxSlowDownScaleFactor
= INT_MAX
;
351 cc::switches::kSlowDownRasterScaleFactor
,
352 kMinSlowDownScaleFactor
,
353 kMaxSlowDownScaleFactor
,
354 &settings
.initial_debug_state
.slow_down_raster_scale_factor
);
357 if (cmd
->HasSwitch(cc::switches::kMaxUnusedResourceMemoryUsagePercentage
)) {
358 int max_unused_resource_memory_percentage
;
359 if (GetSwitchValueAsInt(
361 cc::switches::kMaxUnusedResourceMemoryUsagePercentage
,
363 &max_unused_resource_memory_percentage
)) {
364 settings
.max_unused_resource_memory_percentage
=
365 max_unused_resource_memory_percentage
;
369 settings
.strict_layer_property_change_checking
=
370 cmd
->HasSwitch(cc::switches::kStrictLayerPropertyChangeChecking
);
372 #if defined(OS_ANDROID)
373 SynchronousCompositorFactory
* synchronous_compositor_factory
=
374 SynchronousCompositorFactory::GetInstance();
376 // We can't use GPU rasterization on low-end devices, because the Ganesh
377 // cache would consume too much memory.
378 if (base::SysInfo::IsLowEndDevice())
379 settings
.gpu_rasterization_enabled
= false;
380 settings
.using_synchronous_renderer_compositor
=
381 synchronous_compositor_factory
;
382 settings
.record_full_layer
= widget_
->DoesRecordFullLayer();
383 settings
.report_overscroll_only_for_scrollable_axes
=
384 !synchronous_compositor_factory
;
385 settings
.max_partial_texture_updates
= 0;
386 if (synchronous_compositor_factory
) {
387 // Android WebView uses system scrollbars, so make ours invisible.
388 settings
.scrollbar_animator
= cc::LayerTreeSettings::NO_ANIMATOR
;
389 settings
.solid_color_scrollbar_color
= SK_ColorTRANSPARENT
;
391 settings
.scrollbar_animator
= cc::LayerTreeSettings::LINEAR_FADE
;
392 settings
.scrollbar_fade_delay_ms
= 300;
393 settings
.scrollbar_fade_resize_delay_ms
= 2000;
394 settings
.scrollbar_fade_duration_ms
= 300;
395 settings
.solid_color_scrollbar_color
= SkColorSetARGB(128, 128, 128, 128);
397 settings
.renderer_settings
.highp_threshold_min
= 2048;
398 // Android WebView handles root layer flings itself.
399 settings
.ignore_root_layer_flings
=
400 synchronous_compositor_factory
;
401 // Memory policy on Android WebView does not depend on whether device is
402 // low end, so always use default policy.
403 bool use_low_memory_policy
=
404 base::SysInfo::IsLowEndDevice() && !synchronous_compositor_factory
;
405 // RGBA_4444 textures are only enabled for low end devices
406 // and are disabled for Android WebView as it doesn't support the format.
407 settings
.renderer_settings
.use_rgba_4444_textures
= use_low_memory_policy
;
408 if (use_low_memory_policy
) {
409 // On low-end we want to be very carefull about killing other
410 // apps. So initially we use 50% more memory to avoid flickering
411 // or raster-on-demand.
412 settings
.max_memory_for_prepaint_percentage
= 67;
414 // On other devices we have increased memory excessively to avoid
415 // raster-on-demand already, so now we reserve 50% _only_ to avoid
416 // raster-on-demand, and use 50% of the memory otherwise.
417 settings
.max_memory_for_prepaint_percentage
= 50;
419 // Webview does not own the surface so should not clear it.
420 settings
.renderer_settings
.should_clear_root_render_pass
=
421 !synchronous_compositor_factory
;
423 // TODO(danakj): Only do this on low end devices.
424 settings
.create_low_res_tiling
= true;
426 settings
.use_external_begin_frame_source
= true;
428 #elif !defined(OS_MACOSX)
429 if (ui::IsOverlayScrollbarEnabled()) {
430 settings
.scrollbar_animator
= cc::LayerTreeSettings::THINNING
;
431 settings
.solid_color_scrollbar_color
= SkColorSetARGB(128, 128, 128, 128);
433 settings
.scrollbar_animator
= cc::LayerTreeSettings::LINEAR_FADE
;
434 settings
.solid_color_scrollbar_color
= SkColorSetARGB(128, 128, 128, 128);
436 settings
.scrollbar_fade_delay_ms
= 500;
437 settings
.scrollbar_fade_resize_delay_ms
= 500;
438 settings
.scrollbar_fade_duration_ms
= 300;
440 // When pinching in, only show the pinch-viewport overlay scrollbars if the
441 // page scale is at least some threshold away from the minimum. i.e. don't
442 // show the pinch scrollbars when at minimum scale.
443 settings
.scrollbar_show_scale_threshold
= 1.05f
;
446 if (cmd
->HasSwitch(switches::kEnableLowResTiling
))
447 settings
.create_low_res_tiling
= true;
448 if (cmd
->HasSwitch(switches::kDisableLowResTiling
))
449 settings
.create_low_res_tiling
= false;
450 if (cmd
->HasSwitch(cc::switches::kEnableBeginFrameScheduling
))
451 settings
.use_external_begin_frame_source
= true;
453 if (widget_
->for_oopif()) {
454 // TODO(simonhong): Apply BeginFrame scheduling for OOPIF.
455 // See crbug.com/471411.
456 settings
.use_external_begin_frame_source
= false;
459 scoped_refptr
<base::SingleThreadTaskRunner
> compositor_thread_task_runner
=
460 compositor_deps_
->GetCompositorImplThreadTaskRunner();
461 scoped_refptr
<base::SingleThreadTaskRunner
>
462 main_thread_compositor_task_runner
=
463 compositor_deps_
->GetCompositorMainThreadTaskRunner();
464 cc::SharedBitmapManager
* shared_bitmap_manager
=
465 compositor_deps_
->GetSharedBitmapManager();
466 gpu::GpuMemoryBufferManager
* gpu_memory_buffer_manager
=
467 compositor_deps_
->GetGpuMemoryBufferManager();
468 cc::TaskGraphRunner
* task_graph_runner
=
469 compositor_deps_
->GetTaskGraphRunner();
471 scoped_ptr
<cc::BeginFrameSource
> external_begin_frame_source
;
472 if (settings
.use_external_begin_frame_source
) {
473 external_begin_frame_source
=
474 compositor_deps_
->CreateExternalBeginFrameSource(widget_
->routing_id());
477 cc::LayerTreeHost::InitParams params
;
478 params
.client
= this;
479 params
.shared_bitmap_manager
= shared_bitmap_manager
;
480 params
.gpu_memory_buffer_manager
= gpu_memory_buffer_manager
;
481 params
.settings
= &settings
;
482 params
.task_graph_runner
= task_graph_runner
;
483 params
.main_task_runner
= main_thread_compositor_task_runner
;
484 params
.external_begin_frame_source
= external_begin_frame_source
.Pass();
485 if (compositor_thread_task_runner
.get()) {
486 layer_tree_host_
= cc::LayerTreeHost::CreateThreaded(
487 compositor_thread_task_runner
, ¶ms
);
489 layer_tree_host_
= cc::LayerTreeHost::CreateSingleThreaded(this, ¶ms
);
491 DCHECK(layer_tree_host_
);
494 RenderWidgetCompositor::~RenderWidgetCompositor() {}
496 const base::WeakPtr
<cc::InputHandler
>&
497 RenderWidgetCompositor::GetInputHandler() {
498 return layer_tree_host_
->GetInputHandler();
501 bool RenderWidgetCompositor::BeginMainFrameRequested() const {
502 return layer_tree_host_
->BeginMainFrameRequested();
505 void RenderWidgetCompositor::SetNeedsDisplayOnAllLayers() {
506 layer_tree_host_
->SetNeedsDisplayOnAllLayers();
509 void RenderWidgetCompositor::SetRasterizeOnlyVisibleContent() {
510 cc::LayerTreeDebugState current
= layer_tree_host_
->debug_state();
511 current
.rasterize_only_visible_content
= true;
512 layer_tree_host_
->SetDebugState(current
);
515 void RenderWidgetCompositor::SetNeedsRedrawRect(gfx::Rect damage_rect
) {
516 layer_tree_host_
->SetNeedsRedrawRect(damage_rect
);
519 void RenderWidgetCompositor::SetNeedsForcedRedraw() {
520 layer_tree_host_
->SetNextCommitForcesRedraw();
524 scoped_ptr
<cc::SwapPromiseMonitor
>
525 RenderWidgetCompositor::CreateLatencyInfoSwapPromiseMonitor(
526 ui::LatencyInfo
* latency
) {
527 return scoped_ptr
<cc::SwapPromiseMonitor
>(
528 new cc::LatencyInfoSwapPromiseMonitor(
529 latency
, layer_tree_host_
.get(), NULL
));
532 void RenderWidgetCompositor::QueueSwapPromise(
533 scoped_ptr
<cc::SwapPromise
> swap_promise
) {
534 layer_tree_host_
->QueueSwapPromise(swap_promise
.Pass());
537 int RenderWidgetCompositor::GetSourceFrameNumber() const {
538 return layer_tree_host_
->source_frame_number();
541 void RenderWidgetCompositor::SetNeedsUpdateLayers() {
542 layer_tree_host_
->SetNeedsUpdateLayers();
545 void RenderWidgetCompositor::SetNeedsCommit() {
546 layer_tree_host_
->SetNeedsCommit();
549 void RenderWidgetCompositor::NotifyInputThrottledUntilCommit() {
550 layer_tree_host_
->NotifyInputThrottledUntilCommit();
553 const cc::Layer
* RenderWidgetCompositor::GetRootLayer() const {
554 return layer_tree_host_
->root_layer();
557 int RenderWidgetCompositor::ScheduleMicroBenchmark(
558 const std::string
& name
,
559 scoped_ptr
<base::Value
> value
,
560 const base::Callback
<void(scoped_ptr
<base::Value
>)>& callback
) {
561 return layer_tree_host_
->ScheduleMicroBenchmark(name
, value
.Pass(), callback
);
564 bool RenderWidgetCompositor::SendMessageToMicroBenchmark(
566 scoped_ptr
<base::Value
> value
) {
567 return layer_tree_host_
->SendMessageToMicroBenchmark(id
, value
.Pass());
570 void RenderWidgetCompositor::StartCompositor() {
571 layer_tree_host_
->SetLayerTreeHostClientReady();
574 void RenderWidgetCompositor::setRootLayer(const blink::WebLayer
& layer
) {
575 layer_tree_host_
->SetRootLayer(
576 static_cast<const cc_blink::WebLayerImpl
*>(&layer
)->layer());
579 void RenderWidgetCompositor::clearRootLayer() {
580 layer_tree_host_
->SetRootLayer(scoped_refptr
<cc::Layer
>());
583 void RenderWidgetCompositor::setViewportSize(
585 const WebSize
& device_viewport_size
) {
586 layer_tree_host_
->SetViewportSize(device_viewport_size
);
589 void RenderWidgetCompositor::setViewportSize(
590 const WebSize
& device_viewport_size
) {
591 layer_tree_host_
->SetViewportSize(device_viewport_size
);
594 WebSize
RenderWidgetCompositor::layoutViewportSize() const {
595 return layer_tree_host_
->device_viewport_size();
598 WebSize
RenderWidgetCompositor::deviceViewportSize() const {
599 return layer_tree_host_
->device_viewport_size();
602 WebFloatPoint
RenderWidgetCompositor::adjustEventPointForPinchZoom(
603 const WebFloatPoint
& point
) const {
607 void RenderWidgetCompositor::setDeviceScaleFactor(float device_scale
) {
608 layer_tree_host_
->SetDeviceScaleFactor(device_scale
);
611 float RenderWidgetCompositor::deviceScaleFactor() const {
612 return layer_tree_host_
->device_scale_factor();
615 void RenderWidgetCompositor::setBackgroundColor(blink::WebColor color
) {
616 layer_tree_host_
->set_background_color(color
);
619 void RenderWidgetCompositor::setHasTransparentBackground(bool transparent
) {
620 layer_tree_host_
->set_has_transparent_background(transparent
);
623 void RenderWidgetCompositor::setVisible(bool visible
) {
624 layer_tree_host_
->SetVisible(visible
);
627 void RenderWidgetCompositor::setPageScaleFactorAndLimits(
628 float page_scale_factor
, float minimum
, float maximum
) {
629 layer_tree_host_
->SetPageScaleFactorAndLimits(
630 page_scale_factor
, minimum
, maximum
);
633 void RenderWidgetCompositor::startPageScaleAnimation(
634 const blink::WebPoint
& destination
,
636 float new_page_scale
,
637 double duration_sec
) {
638 base::TimeDelta duration
= base::TimeDelta::FromMicroseconds(
639 duration_sec
* base::Time::kMicrosecondsPerSecond
);
640 layer_tree_host_
->StartPageScaleAnimation(
641 gfx::Vector2d(destination
.x
, destination
.y
),
647 void RenderWidgetCompositor::heuristicsForGpuRasterizationUpdated(
648 bool matches_heuristics
) {
649 layer_tree_host_
->SetHasGpuRasterizationTrigger(matches_heuristics
);
652 void RenderWidgetCompositor::setNeedsAnimate() {
653 layer_tree_host_
->SetNeedsAnimate();
656 bool RenderWidgetCompositor::commitRequested() const {
657 return layer_tree_host_
->CommitRequested();
660 void RenderWidgetCompositor::didStopFlinging() {
661 layer_tree_host_
->DidStopFlinging();
664 void RenderWidgetCompositor::registerForAnimations(blink::WebLayer
* layer
) {
665 cc::Layer
* cc_layer
= static_cast<cc_blink::WebLayerImpl
*>(layer
)->layer();
666 cc_layer
->RegisterForAnimations(layer_tree_host_
->animation_registrar());
669 void RenderWidgetCompositor::registerViewportLayers(
670 const blink::WebLayer
* overscrollElasticityLayer
,
671 const blink::WebLayer
* pageScaleLayer
,
672 const blink::WebLayer
* innerViewportScrollLayer
,
673 const blink::WebLayer
* outerViewportScrollLayer
) {
674 layer_tree_host_
->RegisterViewportLayers(
675 // TODO(bokan): This check can probably be removed now, but it looks
676 // like overscroll elasticity may still be NULL until PinchViewport
677 // registers its layers.
678 // The scroll elasticity layer will only exist when using pinch virtual
680 overscrollElasticityLayer
681 ? static_cast<const cc_blink::WebLayerImpl
*>(
682 overscrollElasticityLayer
)->layer()
684 static_cast<const cc_blink::WebLayerImpl
*>(pageScaleLayer
)->layer(),
685 static_cast<const cc_blink::WebLayerImpl
*>(innerViewportScrollLayer
)
687 // TODO(bokan): This check can probably be removed now, but it looks
688 // like overscroll elasticity may still be NULL until PinchViewport
689 // registers its layers.
690 // The outer viewport layer will only exist when using pinch virtual
692 outerViewportScrollLayer
693 ? static_cast<const cc_blink::WebLayerImpl
*>(outerViewportScrollLayer
)
698 void RenderWidgetCompositor::clearViewportLayers() {
699 layer_tree_host_
->RegisterViewportLayers(
700 scoped_refptr
<cc::Layer
>(), scoped_refptr
<cc::Layer
>(),
701 scoped_refptr
<cc::Layer
>(), scoped_refptr
<cc::Layer
>());
704 void RenderWidgetCompositor::registerSelection(
705 const blink::WebSelection
& selection
) {
706 layer_tree_host_
->RegisterSelection(ConvertWebSelection(selection
));
709 void RenderWidgetCompositor::clearSelection() {
710 cc::LayerSelection empty_selection
;
711 layer_tree_host_
->RegisterSelection(empty_selection
);
714 void CompositeAndReadbackAsyncCallback(
715 blink::WebCompositeAndReadbackAsyncCallback
* callback
,
716 scoped_ptr
<cc::CopyOutputResult
> result
) {
717 if (result
->HasBitmap()) {
718 scoped_ptr
<SkBitmap
> result_bitmap
= result
->TakeBitmap();
719 callback
->didCompositeAndReadback(*result_bitmap
);
721 callback
->didCompositeAndReadback(SkBitmap());
725 void RenderWidgetCompositor::layoutAndPaintAsync(
726 blink::WebLayoutAndPaintAsyncCallback
* callback
) {
727 DCHECK(!temporary_copy_output_request_
&& !layout_and_paint_async_callback_
);
728 layout_and_paint_async_callback_
= callback
;
732 void RenderWidgetCompositor::compositeAndReadbackAsync(
733 blink::WebCompositeAndReadbackAsyncCallback
* callback
) {
734 DCHECK(!temporary_copy_output_request_
&& !layout_and_paint_async_callback_
);
735 temporary_copy_output_request_
=
736 cc::CopyOutputRequest::CreateBitmapRequest(
737 base::Bind(&CompositeAndReadbackAsyncCallback
, callback
));
738 // Force a commit to happen. The temporary copy output request will
739 // be installed after layout which will happen as a part of the commit, for
740 // widgets that delay the creation of their output surface.
744 bool RenderWidgetCompositor::CommitIsSynchronous() const {
745 return !compositor_deps_
->GetCompositorImplThreadTaskRunner().get() &&
746 !layer_tree_host_
->settings().single_thread_proxy_scheduler
;
749 void RenderWidgetCompositor::ScheduleCommit() {
750 if (CommitIsSynchronous()) {
751 base::ThreadTaskRunnerHandle::Get()->PostTask(
752 FROM_HERE
, base::Bind(&RenderWidgetCompositor::SynchronousCommit
,
753 weak_factory_
.GetWeakPtr()));
755 layer_tree_host_
->SetNeedsCommit();
759 void RenderWidgetCompositor::SynchronousCommit() {
760 DCHECK(CommitIsSynchronous());
761 layer_tree_host_
->Composite(base::TimeTicks::Now());
764 void RenderWidgetCompositor::finishAllRendering() {
765 layer_tree_host_
->FinishAllRendering();
768 void RenderWidgetCompositor::setDeferCommits(bool defer_commits
) {
769 layer_tree_host_
->SetDeferCommits(defer_commits
);
772 int RenderWidgetCompositor::layerTreeId() const {
773 return layer_tree_host_
->id();
776 void RenderWidgetCompositor::setShowFPSCounter(bool show
) {
777 cc::LayerTreeDebugState debug_state
= layer_tree_host_
->debug_state();
778 debug_state
.show_fps_counter
= show
;
779 layer_tree_host_
->SetDebugState(debug_state
);
782 void RenderWidgetCompositor::setShowPaintRects(bool show
) {
783 cc::LayerTreeDebugState debug_state
= layer_tree_host_
->debug_state();
784 debug_state
.show_paint_rects
= show
;
785 layer_tree_host_
->SetDebugState(debug_state
);
788 void RenderWidgetCompositor::setShowDebugBorders(bool show
) {
789 cc::LayerTreeDebugState debug_state
= layer_tree_host_
->debug_state();
790 debug_state
.show_debug_borders
= show
;
791 layer_tree_host_
->SetDebugState(debug_state
);
794 void RenderWidgetCompositor::setContinuousPaintingEnabled(bool enabled
) {
795 cc::LayerTreeDebugState debug_state
= layer_tree_host_
->debug_state();
796 debug_state
.continuous_painting
= enabled
;
797 layer_tree_host_
->SetDebugState(debug_state
);
800 void RenderWidgetCompositor::setShowScrollBottleneckRects(bool show
) {
801 cc::LayerTreeDebugState debug_state
= layer_tree_host_
->debug_state();
802 debug_state
.show_touch_event_handler_rects
= show
;
803 debug_state
.show_wheel_event_handler_rects
= show
;
804 debug_state
.show_non_fast_scrollable_rects
= show
;
805 layer_tree_host_
->SetDebugState(debug_state
);
808 void RenderWidgetCompositor::updateTopControlsState(
809 WebTopControlsState constraints
,
810 WebTopControlsState current
,
812 layer_tree_host_
->UpdateTopControlsState(ConvertTopControlsState(constraints
),
813 ConvertTopControlsState(current
),
817 void RenderWidgetCompositor::setTopControlsHeight(float height
, bool shrink
) {
818 layer_tree_host_
->SetTopControlsHeight(height
, shrink
);
821 void RenderWidgetCompositor::setTopControlsShownRatio(float ratio
) {
822 layer_tree_host_
->SetTopControlsShownRatio(ratio
);
825 void RenderWidgetCompositor::WillBeginMainFrame() {
826 widget_
->willBeginCompositorFrame();
829 void RenderWidgetCompositor::DidBeginMainFrame() {
832 void RenderWidgetCompositor::BeginMainFrame(const cc::BeginFrameArgs
& args
) {
833 double frame_time_sec
= (args
.frame_time
- base::TimeTicks()).InSecondsF();
834 double deadline_sec
= (args
.deadline
- base::TimeTicks()).InSecondsF();
835 double interval_sec
= args
.interval
.InSecondsF();
836 WebBeginFrameArgs web_begin_frame_args
=
837 WebBeginFrameArgs(frame_time_sec
, deadline_sec
, interval_sec
);
838 compositor_deps_
->GetRendererScheduler()->WillBeginFrame(args
);
839 widget_
->webwidget()->beginFrame(web_begin_frame_args
);
842 void RenderWidgetCompositor::BeginMainFrameNotExpectedSoon() {
843 compositor_deps_
->GetRendererScheduler()->BeginFrameNotExpectedSoon();
846 void RenderWidgetCompositor::Layout() {
847 widget_
->webwidget()->layout();
849 if (temporary_copy_output_request_
) {
850 // For WebViewImpl, this will always have a root layer. For other widgets,
851 // the widget may be closed before servicing this request, so ignore it.
852 if (cc::Layer
* root_layer
= layer_tree_host_
->root_layer()) {
853 root_layer
->RequestCopyOfOutput(temporary_copy_output_request_
.Pass());
855 temporary_copy_output_request_
->SendEmptyResult();
856 temporary_copy_output_request_
= nullptr;
861 void RenderWidgetCompositor::ApplyViewportDeltas(
862 const gfx::Vector2dF
& inner_delta
,
863 const gfx::Vector2dF
& outer_delta
,
864 const gfx::Vector2dF
& elastic_overscroll_delta
,
866 float top_controls_delta
) {
867 widget_
->webwidget()->applyViewportDeltas(
870 elastic_overscroll_delta
,
875 void RenderWidgetCompositor::RequestNewOutputSurface() {
876 // If the host is closing, then no more compositing is possible. This
877 // prevents shutdown races between handling the close message and
878 // the CreateOutputSurface task.
879 if (widget_
->host_closing())
882 // TODO(robliao): Remove ScopedTracker below once https://crbug.com/466870
884 tracked_objects::ScopedTracker
tracking_profile(
885 FROM_HERE_WITH_EXPLICIT_FUNCTION(
886 "466870 RenderWidgetCompositor::RequestNewOutputSurface"));
889 num_failed_recreate_attempts_
>= OUTPUT_SURFACE_RETRIES_BEFORE_FALLBACK
;
890 scoped_ptr
<cc::OutputSurface
> surface(widget_
->CreateOutputSurface(fallback
));
893 DidFailToInitializeOutputSurface();
897 layer_tree_host_
->SetOutputSurface(surface
.Pass());
900 void RenderWidgetCompositor::DidInitializeOutputSurface() {
901 num_failed_recreate_attempts_
= 0;
904 void RenderWidgetCompositor::DidFailToInitializeOutputSurface() {
905 ++num_failed_recreate_attempts_
;
906 // Tolerate a certain number of recreation failures to work around races
907 // in the output-surface-lost machinery.
908 LOG_IF(FATAL
, (num_failed_recreate_attempts_
>= MAX_OUTPUT_SURFACE_RETRIES
))
909 << "Failed to create a fallback OutputSurface.";
911 base::ThreadTaskRunnerHandle::Get()->PostTask(
912 FROM_HERE
, base::Bind(&RenderWidgetCompositor::RequestNewOutputSurface
,
913 weak_factory_
.GetWeakPtr()));
916 void RenderWidgetCompositor::WillCommit() {
917 if (!layout_and_paint_async_callback_
)
919 layout_and_paint_async_callback_
->didLayoutAndPaint();
920 layout_and_paint_async_callback_
= nullptr;
923 void RenderWidgetCompositor::DidCommit() {
924 DCHECK(!temporary_copy_output_request_
);
925 widget_
->DidCommitCompositorFrame();
926 widget_
->didBecomeReadyForAdditionalInput();
927 compositor_deps_
->GetRendererScheduler()->DidCommitFrameToCompositor();
930 void RenderWidgetCompositor::DidCommitAndDrawFrame() {
931 widget_
->didCommitAndDrawCompositorFrame();
934 void RenderWidgetCompositor::DidCompleteSwapBuffers() {
935 widget_
->didCompleteSwapBuffers();
936 bool threaded
= !!compositor_deps_
->GetCompositorImplThreadTaskRunner().get();
938 widget_
->OnSwapBuffersComplete();
941 void RenderWidgetCompositor::DidCompletePageScaleAnimation() {
942 widget_
->DidCompletePageScaleAnimation();
945 void RenderWidgetCompositor::ScheduleAnimation() {
946 widget_
->scheduleAnimation();
949 void RenderWidgetCompositor::DidPostSwapBuffers() {
950 widget_
->OnSwapBuffersPosted();
953 void RenderWidgetCompositor::DidAbortSwapBuffers() {
954 widget_
->OnSwapBuffersAborted();
957 void RenderWidgetCompositor::RateLimitSharedMainThreadContext() {
958 cc::ContextProvider
* provider
=
959 compositor_deps_
->GetSharedMainThreadContextProvider();
960 // provider can be NULL after the GPU process crashed enough times and we
961 // don't want to restart it any more (falling back to software).
964 provider
->ContextGL()->RateLimitOffscreenContextCHROMIUM();
967 void RenderWidgetCompositor::SetSurfaceIdNamespace(
968 uint32_t surface_id_namespace
) {
969 layer_tree_host_
->set_surface_id_namespace(surface_id_namespace
);
972 } // namespace content