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/metrics/field_trial.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/animation/animation_host.h"
22 #include "cc/animation/animation_timeline.h"
23 #include "cc/base/switches.h"
24 #include "cc/blink/web_compositor_animation_timeline_impl.h"
25 #include "cc/blink/web_layer_impl.h"
26 #include "cc/debug/layer_tree_debug_state.h"
27 #include "cc/debug/micro_benchmark.h"
28 #include "cc/input/layer_selection_bound.h"
29 #include "cc/layers/layer.h"
30 #include "cc/output/begin_frame_args.h"
31 #include "cc/output/copy_output_request.h"
32 #include "cc/output/copy_output_result.h"
33 #include "cc/output/latency_info_swap_promise.h"
34 #include "cc/output/swap_promise.h"
35 #include "cc/resources/single_release_callback.h"
36 #include "cc/scheduler/begin_frame_source.h"
37 #include "cc/trees/latency_info_swap_promise_monitor.h"
38 #include "cc/trees/layer_tree_host.h"
39 #include "components/scheduler/renderer/renderer_scheduler.h"
40 #include "content/common/content_switches_internal.h"
41 #include "content/common/gpu/client/context_provider_command_buffer.h"
42 #include "content/public/common/content_switches.h"
43 #include "content/renderer/input/input_handler_manager.h"
44 #include "gpu/command_buffer/client/gles2_interface.h"
45 #include "third_party/WebKit/public/platform/WebCompositeAndReadbackAsyncCallback.h"
46 #include "third_party/WebKit/public/platform/WebLayoutAndPaintAsyncCallback.h"
47 #include "third_party/WebKit/public/platform/WebSize.h"
48 #include "third_party/WebKit/public/web/WebKit.h"
49 #include "third_party/WebKit/public/web/WebRuntimeFeatures.h"
50 #include "third_party/WebKit/public/web/WebSelection.h"
51 #include "third_party/WebKit/public/web/WebWidget.h"
52 #include "ui/gl/gl_switches.h"
53 #include "ui/native_theme/native_theme_switches.h"
55 #if defined(OS_ANDROID)
56 #include "base/android/build_info.h"
57 #include "content/renderer/android/synchronous_compositor_factory.h"
58 #include "ui/gfx/android/device_display_info.h"
69 using blink::WebBeginFrameArgs
;
70 using blink::WebFloatPoint
;
72 using blink::WebSelection
;
74 using blink::WebTopControlsState
;
79 bool GetSwitchValueAsInt(const base::CommandLine
& command_line
,
80 const std::string
& switch_string
,
84 std::string string_value
= command_line
.GetSwitchValueASCII(switch_string
);
86 if (base::StringToInt(string_value
, &int_value
) &&
87 int_value
>= min_value
&& int_value
<= max_value
) {
91 LOG(WARNING
) << "Failed to parse switch " << switch_string
<< ": " <<
97 cc::LayerSelectionBound
ConvertWebSelectionBound(
98 const WebSelection
& web_selection
,
100 cc::LayerSelectionBound cc_bound
;
101 if (web_selection
.isNone())
104 const blink::WebSelectionBound
& web_bound
=
105 is_start
? web_selection
.start() : web_selection
.end();
106 DCHECK(web_bound
.layerId
);
107 cc_bound
.type
= cc::SELECTION_BOUND_CENTER
;
108 if (web_selection
.isRange()) {
110 cc_bound
.type
= web_bound
.isTextDirectionRTL
? cc::SELECTION_BOUND_RIGHT
111 : cc::SELECTION_BOUND_LEFT
;
113 cc_bound
.type
= web_bound
.isTextDirectionRTL
? cc::SELECTION_BOUND_LEFT
114 : cc::SELECTION_BOUND_RIGHT
;
117 cc_bound
.layer_id
= web_bound
.layerId
;
118 cc_bound
.edge_top
= gfx::Point(web_bound
.edgeTopInLayer
);
119 cc_bound
.edge_bottom
= gfx::Point(web_bound
.edgeBottomInLayer
);
123 cc::LayerSelection
ConvertWebSelection(const WebSelection
& web_selection
) {
124 cc::LayerSelection cc_selection
;
125 cc_selection
.start
= ConvertWebSelectionBound(web_selection
, true);
126 cc_selection
.end
= ConvertWebSelectionBound(web_selection
, false);
127 cc_selection
.is_editable
= web_selection
.isEditable();
128 cc_selection
.is_empty_text_form_control
=
129 web_selection
.isEmptyTextFormControl();
133 gfx::Size
CalculateDefaultTileSize(RenderWidget
* widget
) {
134 int default_tile_size
= 256;
135 #if defined(OS_ANDROID)
136 // TODO(epenner): unify this for all platforms if it
137 // makes sense (http://crbug.com/159524)
139 gfx::DeviceDisplayInfo info
;
140 bool real_size_supported
= true;
141 int display_width
= info
.GetPhysicalDisplayWidth();
142 int display_height
= info
.GetPhysicalDisplayHeight();
143 if (display_width
== 0 || display_height
== 0) {
144 real_size_supported
= false;
145 display_width
= info
.GetDisplayWidth();
146 display_height
= info
.GetDisplayHeight();
149 int portrait_width
= std::min(display_width
, display_height
);
150 int landscape_width
= std::max(display_width
, display_height
);
152 if (real_size_supported
) {
153 // Maximum HD dimensions should be 768x1280
154 // Maximum FHD dimensions should be 1200x1920
155 if (portrait_width
> 768 || landscape_width
> 1280)
156 default_tile_size
= 384;
157 if (portrait_width
> 1200 || landscape_width
> 1920)
158 default_tile_size
= 512;
160 // Adjust for some resolutions that barely straddle an extra
161 // tile when in portrait mode. This helps worst case scroll/raster
162 // by not needing a full extra tile for each row.
163 if (default_tile_size
== 256 && portrait_width
== 768)
164 default_tile_size
+= 32;
165 if (default_tile_size
== 384 && portrait_width
== 1200)
166 default_tile_size
+= 32;
168 // We don't know the exact resolution due to screen controls etc.
169 // So this just estimates the values above using tile counts.
170 int numTiles
= (display_width
* display_height
) / (256 * 256);
172 default_tile_size
= 384;
174 default_tile_size
= 512;
176 #elif defined(OS_CHROMEOS)
177 // Use 512 for high DPI (dsf=2.0f) devices.
178 if (widget
->screen_info().deviceScaleFactor
>= 2.0f
)
179 default_tile_size
= 512;
182 return gfx::Size(default_tile_size
, default_tile_size
);
185 // Check cc::TopControlsState, and blink::WebTopControlsState
187 static_assert(int(blink::WebTopControlsBoth
) == int(cc::BOTH
),
188 "mismatching enums: BOTH");
189 static_assert(int(blink::WebTopControlsHidden
) == int(cc::HIDDEN
),
190 "mismatching enums: HIDDEN");
191 static_assert(int(blink::WebTopControlsShown
) == int(cc::SHOWN
),
192 "mismatching enums: SHOWN");
194 static cc::TopControlsState
ConvertTopControlsState(
195 WebTopControlsState state
) {
196 return static_cast<cc::TopControlsState
>(state
);
202 scoped_ptr
<RenderWidgetCompositor
> RenderWidgetCompositor::Create(
203 RenderWidget
* widget
,
204 CompositorDependencies
* compositor_deps
) {
205 scoped_ptr
<RenderWidgetCompositor
> compositor(
206 new RenderWidgetCompositor(widget
, compositor_deps
));
207 compositor
->Initialize();
211 RenderWidgetCompositor::RenderWidgetCompositor(
212 RenderWidget
* widget
,
213 CompositorDependencies
* compositor_deps
)
214 : num_failed_recreate_attempts_(0),
216 compositor_deps_(compositor_deps
),
217 layout_and_paint_async_callback_(nullptr),
218 weak_factory_(this) {
221 void RenderWidgetCompositor::Initialize() {
222 base::CommandLine
* cmd
= base::CommandLine::ForCurrentProcess();
224 cc::LayerTreeSettings settings
;
226 // For web contents, layer transforms should scale up the contents of layers
227 // to keep content always crisp when possible.
228 settings
.layer_transforms_should_scale_layer_contents
= true;
230 if (cmd
->HasSwitch(switches::kDisableGpuVsync
)) {
231 std::string display_vsync_string
=
232 cmd
->GetSwitchValueASCII(switches::kDisableGpuVsync
);
233 if (display_vsync_string
== "gpu") {
234 settings
.renderer_settings
.disable_display_vsync
= true;
235 } else if (display_vsync_string
== "beginframe") {
236 settings
.wait_for_beginframe_interval
= false;
238 settings
.renderer_settings
.disable_display_vsync
= true;
239 settings
.wait_for_beginframe_interval
= false;
242 settings
.main_frame_before_activation_enabled
=
243 cmd
->HasSwitch(cc::switches::kEnableMainFrameBeforeActivation
) &&
244 !cmd
->HasSwitch(cc::switches::kDisableMainFrameBeforeActivation
);
245 settings
.accelerated_animation_enabled
=
246 compositor_deps_
->IsThreadedAnimationEnabled();
247 const std::string slimming_group
=
248 base::FieldTrialList::FindFullName("SlimmingPaint");
249 settings
.use_display_lists
=
250 (cmd
->HasSwitch(switches::kEnableSlimmingPaint
) ||
251 !cmd
->HasSwitch(switches::kDisableSlimmingPaint
)) &&
252 (slimming_group
!= "DisableSlimmingPaint");
253 if (cmd
->HasSwitch(switches::kEnableCompositorAnimationTimelines
)) {
254 settings
.use_compositor_animation_timelines
= true;
255 blink::WebRuntimeFeatures::enableCompositorAnimationTimelines(true);
258 settings
.default_tile_size
= CalculateDefaultTileSize(widget_
);
259 if (cmd
->HasSwitch(switches::kDefaultTileWidth
)) {
261 GetSwitchValueAsInt(*cmd
,
262 switches::kDefaultTileWidth
,
264 std::numeric_limits
<int>::max(),
266 settings
.default_tile_size
.set_width(tile_width
);
268 if (cmd
->HasSwitch(switches::kDefaultTileHeight
)) {
270 GetSwitchValueAsInt(*cmd
,
271 switches::kDefaultTileHeight
,
273 std::numeric_limits
<int>::max(),
275 settings
.default_tile_size
.set_height(tile_height
);
278 int max_untiled_layer_width
= settings
.max_untiled_layer_size
.width();
279 if (cmd
->HasSwitch(switches::kMaxUntiledLayerWidth
)) {
280 GetSwitchValueAsInt(*cmd
, switches::kMaxUntiledLayerWidth
, 1,
281 std::numeric_limits
<int>::max(),
282 &max_untiled_layer_width
);
284 int max_untiled_layer_height
= settings
.max_untiled_layer_size
.height();
285 if (cmd
->HasSwitch(switches::kMaxUntiledLayerHeight
)) {
286 GetSwitchValueAsInt(*cmd
, switches::kMaxUntiledLayerHeight
, 1,
287 std::numeric_limits
<int>::max(),
288 &max_untiled_layer_height
);
291 settings
.max_untiled_layer_size
= gfx::Size(max_untiled_layer_width
,
292 max_untiled_layer_height
);
294 settings
.gpu_rasterization_msaa_sample_count
=
295 compositor_deps_
->GetGpuRasterizationMSAASampleCount();
296 settings
.gpu_rasterization_forced
=
297 compositor_deps_
->IsGpuRasterizationForced();
298 settings
.gpu_rasterization_enabled
=
299 compositor_deps_
->IsGpuRasterizationEnabled();
301 settings
.can_use_lcd_text
= compositor_deps_
->IsLcdTextEnabled();
302 settings
.use_distance_field_text
=
303 compositor_deps_
->IsDistanceFieldTextEnabled();
304 settings
.use_zero_copy
= compositor_deps_
->IsZeroCopyEnabled();
305 settings
.use_one_copy
= compositor_deps_
->IsOneCopyEnabled();
306 settings
.use_persistent_map_for_gpu_memory_buffers
=
307 compositor_deps_
->IsPersistentGpuMemoryBufferEnabled();
308 settings
.enable_elastic_overscroll
=
309 compositor_deps_
->IsElasticOverscrollEnabled();
310 settings
.use_image_texture_targets
=
311 compositor_deps_
->GetImageTextureTargets();
312 settings
.gather_pixel_refs
= compositor_deps_
->IsGatherPixelRefsEnabled();
314 if (cmd
->HasSwitch(cc::switches::kTopControlsShowThreshold
)) {
315 std::string top_threshold_str
=
316 cmd
->GetSwitchValueASCII(cc::switches::kTopControlsShowThreshold
);
317 double show_threshold
;
318 if (base::StringToDouble(top_threshold_str
, &show_threshold
) &&
319 show_threshold
>= 0.f
&& show_threshold
<= 1.f
)
320 settings
.top_controls_show_threshold
= show_threshold
;
323 if (cmd
->HasSwitch(cc::switches::kTopControlsHideThreshold
)) {
324 std::string top_threshold_str
=
325 cmd
->GetSwitchValueASCII(cc::switches::kTopControlsHideThreshold
);
326 double hide_threshold
;
327 if (base::StringToDouble(top_threshold_str
, &hide_threshold
) &&
328 hide_threshold
>= 0.f
&& hide_threshold
<= 1.f
)
329 settings
.top_controls_hide_threshold
= hide_threshold
;
332 settings
.verify_property_trees
=
333 cmd
->HasSwitch(cc::switches::kEnablePropertyTreeVerification
);
334 settings
.renderer_settings
.allow_antialiasing
&=
335 !cmd
->HasSwitch(cc::switches::kDisableCompositedAntialiasing
);
336 // The means the renderer compositor has 2 possible modes:
337 // - Threaded compositing with a scheduler.
338 // - Single threaded compositing without a scheduler (for layout tests only).
339 // Using the scheduler in layout tests introduces additional composite steps
340 // that create flakiness.
341 settings
.single_thread_proxy_scheduler
= false;
343 // These flags should be mirrored by UI versions in ui/compositor/.
344 settings
.initial_debug_state
.show_debug_borders
=
345 cmd
->HasSwitch(cc::switches::kShowCompositedLayerBorders
);
346 settings
.initial_debug_state
.show_fps_counter
=
347 cmd
->HasSwitch(cc::switches::kShowFPSCounter
);
348 settings
.initial_debug_state
.show_layer_animation_bounds_rects
=
349 cmd
->HasSwitch(cc::switches::kShowLayerAnimationBounds
);
350 settings
.initial_debug_state
.show_paint_rects
=
351 cmd
->HasSwitch(switches::kShowPaintRects
);
352 settings
.initial_debug_state
.show_property_changed_rects
=
353 cmd
->HasSwitch(cc::switches::kShowPropertyChangedRects
);
354 settings
.initial_debug_state
.show_surface_damage_rects
=
355 cmd
->HasSwitch(cc::switches::kShowSurfaceDamageRects
);
356 settings
.initial_debug_state
.show_screen_space_rects
=
357 cmd
->HasSwitch(cc::switches::kShowScreenSpaceRects
);
358 settings
.initial_debug_state
.show_replica_screen_space_rects
=
359 cmd
->HasSwitch(cc::switches::kShowReplicaScreenSpaceRects
);
361 settings
.initial_debug_state
.SetRecordRenderingStats(
362 cmd
->HasSwitch(cc::switches::kEnableGpuBenchmarking
));
364 if (cmd
->HasSwitch(cc::switches::kSlowDownRasterScaleFactor
)) {
365 const int kMinSlowDownScaleFactor
= 0;
366 const int kMaxSlowDownScaleFactor
= INT_MAX
;
369 cc::switches::kSlowDownRasterScaleFactor
,
370 kMinSlowDownScaleFactor
,
371 kMaxSlowDownScaleFactor
,
372 &settings
.initial_debug_state
.slow_down_raster_scale_factor
);
375 settings
.invert_viewport_scroll_order
=
376 cmd
->HasSwitch(switches::kInvertViewportScrollOrder
);
378 if (cmd
->HasSwitch(cc::switches::kMaxUnusedResourceMemoryUsagePercentage
)) {
379 int max_unused_resource_memory_percentage
;
380 if (GetSwitchValueAsInt(
382 cc::switches::kMaxUnusedResourceMemoryUsagePercentage
,
384 &max_unused_resource_memory_percentage
)) {
385 settings
.max_unused_resource_memory_percentage
=
386 max_unused_resource_memory_percentage
;
390 settings
.strict_layer_property_change_checking
=
391 cmd
->HasSwitch(cc::switches::kStrictLayerPropertyChangeChecking
);
393 #if defined(OS_ANDROID)
394 SynchronousCompositorFactory
* synchronous_compositor_factory
=
395 SynchronousCompositorFactory::GetInstance();
397 // We can't use GPU rasterization on low-end devices, because the Ganesh
398 // cache would consume too much memory.
399 if (base::SysInfo::IsLowEndDevice())
400 settings
.gpu_rasterization_enabled
= false;
401 settings
.using_synchronous_renderer_compositor
=
402 synchronous_compositor_factory
;
403 settings
.record_full_layer
= widget_
->DoesRecordFullLayer();
404 settings
.max_partial_texture_updates
= 0;
405 if (synchronous_compositor_factory
) {
406 // Android WebView uses system scrollbars, so make ours invisible.
407 settings
.scrollbar_animator
= cc::LayerTreeSettings::NO_ANIMATOR
;
408 settings
.solid_color_scrollbar_color
= SK_ColorTRANSPARENT
;
410 settings
.scrollbar_animator
= cc::LayerTreeSettings::LINEAR_FADE
;
411 settings
.scrollbar_fade_delay_ms
= 300;
412 settings
.scrollbar_fade_resize_delay_ms
= 2000;
413 settings
.scrollbar_fade_duration_ms
= 300;
414 settings
.solid_color_scrollbar_color
= SkColorSetARGB(128, 128, 128, 128);
416 settings
.renderer_settings
.highp_threshold_min
= 2048;
417 // Android WebView handles root layer flings itself.
418 settings
.ignore_root_layer_flings
=
419 synchronous_compositor_factory
;
420 // Memory policy on Android WebView does not depend on whether device is
421 // low end, so always use default policy.
422 bool use_low_memory_policy
=
423 base::SysInfo::IsLowEndDevice() && !synchronous_compositor_factory
;
424 // RGBA_4444 textures are only enabled by default for low end devices
425 // and are disabled for Android WebView as it doesn't support the format.
426 settings
.renderer_settings
.use_rgba_4444_textures
= use_low_memory_policy
;
427 if (use_low_memory_policy
) {
428 // On low-end we want to be very carefull about killing other
429 // apps. So initially we use 50% more memory to avoid flickering
430 // or raster-on-demand.
431 settings
.max_memory_for_prepaint_percentage
= 67;
433 // On other devices we have increased memory excessively to avoid
434 // raster-on-demand already, so now we reserve 50% _only_ to avoid
435 // raster-on-demand, and use 50% of the memory otherwise.
436 settings
.max_memory_for_prepaint_percentage
= 50;
438 // Webview does not own the surface so should not clear it.
439 settings
.renderer_settings
.should_clear_root_render_pass
=
440 !synchronous_compositor_factory
;
442 // TODO(danakj): Only do this on low end devices.
443 settings
.create_low_res_tiling
= true;
445 settings
.use_external_begin_frame_source
= true;
447 #elif !defined(OS_MACOSX)
448 if (ui::IsOverlayScrollbarEnabled()) {
449 settings
.scrollbar_animator
= cc::LayerTreeSettings::THINNING
;
450 settings
.solid_color_scrollbar_color
= SkColorSetARGB(128, 128, 128, 128);
452 settings
.scrollbar_animator
= cc::LayerTreeSettings::LINEAR_FADE
;
453 settings
.solid_color_scrollbar_color
= SkColorSetARGB(128, 128, 128, 128);
455 settings
.scrollbar_fade_delay_ms
= 500;
456 settings
.scrollbar_fade_resize_delay_ms
= 500;
457 settings
.scrollbar_fade_duration_ms
= 300;
460 if (cmd
->HasSwitch(switches::kEnableLowResTiling
))
461 settings
.create_low_res_tiling
= true;
462 if (cmd
->HasSwitch(switches::kDisableLowResTiling
))
463 settings
.create_low_res_tiling
= false;
464 if (cmd
->HasSwitch(cc::switches::kEnableBeginFrameScheduling
))
465 settings
.use_external_begin_frame_source
= true;
467 settings
.renderer_settings
.use_rgba_4444_textures
|=
468 cmd
->HasSwitch(switches::kEnableRGBA4444Textures
);
469 settings
.renderer_settings
.use_rgba_4444_textures
&=
470 !cmd
->HasSwitch(switches::kDisableRGBA4444Textures
);
472 if (widget_
->for_oopif()) {
473 // TODO(simonhong): Apply BeginFrame scheduling for OOPIF.
474 // See crbug.com/471411.
475 settings
.use_external_begin_frame_source
= false;
478 settings
.max_staging_buffers
= 32;
479 // Use 1/4th of staging buffers on low-end devices.
480 if (base::SysInfo::IsLowEndDevice())
481 settings
.max_staging_buffers
/= 4;
483 scoped_refptr
<base::SingleThreadTaskRunner
> compositor_thread_task_runner
=
484 compositor_deps_
->GetCompositorImplThreadTaskRunner();
485 scoped_refptr
<base::SingleThreadTaskRunner
>
486 main_thread_compositor_task_runner
=
487 compositor_deps_
->GetCompositorMainThreadTaskRunner();
488 cc::SharedBitmapManager
* shared_bitmap_manager
=
489 compositor_deps_
->GetSharedBitmapManager();
490 gpu::GpuMemoryBufferManager
* gpu_memory_buffer_manager
=
491 compositor_deps_
->GetGpuMemoryBufferManager();
492 cc::TaskGraphRunner
* task_graph_runner
=
493 compositor_deps_
->GetTaskGraphRunner();
495 scoped_ptr
<cc::BeginFrameSource
> external_begin_frame_source
;
496 if (settings
.use_external_begin_frame_source
) {
497 external_begin_frame_source
=
498 compositor_deps_
->CreateExternalBeginFrameSource(widget_
->routing_id());
501 cc::LayerTreeHost::InitParams params
;
502 params
.client
= this;
503 params
.shared_bitmap_manager
= shared_bitmap_manager
;
504 params
.gpu_memory_buffer_manager
= gpu_memory_buffer_manager
;
505 params
.settings
= &settings
;
506 params
.task_graph_runner
= task_graph_runner
;
507 params
.main_task_runner
= main_thread_compositor_task_runner
;
508 params
.external_begin_frame_source
= external_begin_frame_source
.Pass();
509 if (compositor_thread_task_runner
.get()) {
510 layer_tree_host_
= cc::LayerTreeHost::CreateThreaded(
511 compositor_thread_task_runner
, ¶ms
);
513 layer_tree_host_
= cc::LayerTreeHost::CreateSingleThreaded(this, ¶ms
);
515 DCHECK(layer_tree_host_
);
518 RenderWidgetCompositor::~RenderWidgetCompositor() {}
520 const base::WeakPtr
<cc::InputHandler
>&
521 RenderWidgetCompositor::GetInputHandler() {
522 return layer_tree_host_
->GetInputHandler();
525 bool RenderWidgetCompositor::BeginMainFrameRequested() const {
526 return layer_tree_host_
->BeginMainFrameRequested();
529 void RenderWidgetCompositor::SetNeedsDisplayOnAllLayers() {
530 layer_tree_host_
->SetNeedsDisplayOnAllLayers();
533 void RenderWidgetCompositor::SetRasterizeOnlyVisibleContent() {
534 cc::LayerTreeDebugState current
= layer_tree_host_
->debug_state();
535 current
.rasterize_only_visible_content
= true;
536 layer_tree_host_
->SetDebugState(current
);
539 void RenderWidgetCompositor::SetNeedsRedrawRect(gfx::Rect damage_rect
) {
540 layer_tree_host_
->SetNeedsRedrawRect(damage_rect
);
543 void RenderWidgetCompositor::SetNeedsForcedRedraw() {
544 layer_tree_host_
->SetNextCommitForcesRedraw();
548 scoped_ptr
<cc::SwapPromiseMonitor
>
549 RenderWidgetCompositor::CreateLatencyInfoSwapPromiseMonitor(
550 ui::LatencyInfo
* latency
) {
551 return scoped_ptr
<cc::SwapPromiseMonitor
>(
552 new cc::LatencyInfoSwapPromiseMonitor(
553 latency
, layer_tree_host_
.get(), NULL
));
556 void RenderWidgetCompositor::QueueSwapPromise(
557 scoped_ptr
<cc::SwapPromise
> swap_promise
) {
558 layer_tree_host_
->QueueSwapPromise(swap_promise
.Pass());
561 int RenderWidgetCompositor::GetSourceFrameNumber() const {
562 return layer_tree_host_
->source_frame_number();
565 void RenderWidgetCompositor::SetNeedsUpdateLayers() {
566 layer_tree_host_
->SetNeedsUpdateLayers();
569 void RenderWidgetCompositor::SetNeedsCommit() {
570 layer_tree_host_
->SetNeedsCommit();
573 void RenderWidgetCompositor::NotifyInputThrottledUntilCommit() {
574 layer_tree_host_
->NotifyInputThrottledUntilCommit();
577 const cc::Layer
* RenderWidgetCompositor::GetRootLayer() const {
578 return layer_tree_host_
->root_layer();
581 int RenderWidgetCompositor::ScheduleMicroBenchmark(
582 const std::string
& name
,
583 scoped_ptr
<base::Value
> value
,
584 const base::Callback
<void(scoped_ptr
<base::Value
>)>& callback
) {
585 return layer_tree_host_
->ScheduleMicroBenchmark(name
, value
.Pass(), callback
);
588 bool RenderWidgetCompositor::SendMessageToMicroBenchmark(
590 scoped_ptr
<base::Value
> value
) {
591 return layer_tree_host_
->SendMessageToMicroBenchmark(id
, value
.Pass());
594 void RenderWidgetCompositor::StartCompositor() {
595 layer_tree_host_
->SetLayerTreeHostClientReady();
598 void RenderWidgetCompositor::setRootLayer(const blink::WebLayer
& layer
) {
599 layer_tree_host_
->SetRootLayer(
600 static_cast<const cc_blink::WebLayerImpl
*>(&layer
)->layer());
603 void RenderWidgetCompositor::clearRootLayer() {
604 layer_tree_host_
->SetRootLayer(scoped_refptr
<cc::Layer
>());
607 void RenderWidgetCompositor::attachCompositorAnimationTimeline(
608 blink::WebCompositorAnimationTimeline
* compositor_timeline
) {
609 DCHECK(compositor_timeline
);
610 DCHECK(layer_tree_host_
->animation_host());
611 layer_tree_host_
->animation_host()->AddAnimationTimeline(
612 static_cast<const cc_blink::WebCompositorAnimationTimelineImpl
*>(
613 compositor_timeline
)->animation_timeline());
616 void RenderWidgetCompositor::detachCompositorAnimationTimeline(
617 blink::WebCompositorAnimationTimeline
* compositor_timeline
) {
618 DCHECK(compositor_timeline
);
619 DCHECK(layer_tree_host_
->animation_host());
620 layer_tree_host_
->animation_host()->RemoveAnimationTimeline(
621 static_cast<const cc_blink::WebCompositorAnimationTimelineImpl
*>(
622 compositor_timeline
)->animation_timeline());
625 void RenderWidgetCompositor::setViewportSize(
627 const WebSize
& device_viewport_size
) {
628 layer_tree_host_
->SetViewportSize(device_viewport_size
);
631 void RenderWidgetCompositor::setViewportSize(
632 const WebSize
& device_viewport_size
) {
633 layer_tree_host_
->SetViewportSize(device_viewport_size
);
636 WebSize
RenderWidgetCompositor::layoutViewportSize() const {
637 return layer_tree_host_
->device_viewport_size();
640 WebSize
RenderWidgetCompositor::deviceViewportSize() const {
641 return layer_tree_host_
->device_viewport_size();
644 WebFloatPoint
RenderWidgetCompositor::adjustEventPointForPinchZoom(
645 const WebFloatPoint
& point
) const {
649 void RenderWidgetCompositor::setDeviceScaleFactor(float device_scale
) {
650 layer_tree_host_
->SetDeviceScaleFactor(device_scale
);
653 float RenderWidgetCompositor::deviceScaleFactor() const {
654 return layer_tree_host_
->device_scale_factor();
657 void RenderWidgetCompositor::setBackgroundColor(blink::WebColor color
) {
658 layer_tree_host_
->set_background_color(color
);
661 void RenderWidgetCompositor::setHasTransparentBackground(bool transparent
) {
662 layer_tree_host_
->set_has_transparent_background(transparent
);
665 void RenderWidgetCompositor::setVisible(bool visible
) {
666 layer_tree_host_
->SetVisible(visible
);
669 void RenderWidgetCompositor::setPageScaleFactorAndLimits(
670 float page_scale_factor
, float minimum
, float maximum
) {
671 layer_tree_host_
->SetPageScaleFactorAndLimits(
672 page_scale_factor
, minimum
, maximum
);
675 void RenderWidgetCompositor::startPageScaleAnimation(
676 const blink::WebPoint
& destination
,
678 float new_page_scale
,
679 double duration_sec
) {
680 base::TimeDelta duration
= base::TimeDelta::FromMicroseconds(
681 duration_sec
* base::Time::kMicrosecondsPerSecond
);
682 layer_tree_host_
->StartPageScaleAnimation(
683 gfx::Vector2d(destination
.x
, destination
.y
),
689 void RenderWidgetCompositor::heuristicsForGpuRasterizationUpdated(
690 bool matches_heuristics
) {
691 layer_tree_host_
->SetHasGpuRasterizationTrigger(matches_heuristics
);
694 void RenderWidgetCompositor::setNeedsAnimate() {
695 layer_tree_host_
->SetNeedsAnimate();
696 layer_tree_host_
->SetNeedsUpdateLayers();
699 void RenderWidgetCompositor::setNeedsBeginFrame() {
700 layer_tree_host_
->SetNeedsAnimate();
703 void RenderWidgetCompositor::setNeedsCompositorUpdate() {
704 layer_tree_host_
->SetNeedsUpdateLayers();
707 void RenderWidgetCompositor::didStopFlinging() {
708 layer_tree_host_
->DidStopFlinging();
711 void RenderWidgetCompositor::registerForAnimations(blink::WebLayer
* layer
) {
712 cc::Layer
* cc_layer
= static_cast<cc_blink::WebLayerImpl
*>(layer
)->layer();
713 cc_layer
->RegisterForAnimations(layer_tree_host_
->animation_registrar());
716 void RenderWidgetCompositor::registerViewportLayers(
717 const blink::WebLayer
* overscrollElasticityLayer
,
718 const blink::WebLayer
* pageScaleLayer
,
719 const blink::WebLayer
* innerViewportScrollLayer
,
720 const blink::WebLayer
* outerViewportScrollLayer
) {
721 layer_tree_host_
->RegisterViewportLayers(
722 // TODO(bokan): This check can probably be removed now, but it looks
723 // like overscroll elasticity may still be NULL until PinchViewport
724 // registers its layers.
725 // The scroll elasticity layer will only exist when using pinch virtual
727 overscrollElasticityLayer
728 ? static_cast<const cc_blink::WebLayerImpl
*>(
729 overscrollElasticityLayer
)->layer()
731 static_cast<const cc_blink::WebLayerImpl
*>(pageScaleLayer
)->layer(),
732 static_cast<const cc_blink::WebLayerImpl
*>(innerViewportScrollLayer
)
734 // TODO(bokan): This check can probably be removed now, but it looks
735 // like overscroll elasticity may still be NULL until PinchViewport
736 // registers its layers.
737 // The outer viewport layer will only exist when using pinch virtual
739 outerViewportScrollLayer
740 ? static_cast<const cc_blink::WebLayerImpl
*>(outerViewportScrollLayer
)
745 void RenderWidgetCompositor::clearViewportLayers() {
746 layer_tree_host_
->RegisterViewportLayers(
747 scoped_refptr
<cc::Layer
>(), scoped_refptr
<cc::Layer
>(),
748 scoped_refptr
<cc::Layer
>(), scoped_refptr
<cc::Layer
>());
751 void RenderWidgetCompositor::registerSelection(
752 const blink::WebSelection
& selection
) {
753 layer_tree_host_
->RegisterSelection(ConvertWebSelection(selection
));
756 void RenderWidgetCompositor::clearSelection() {
757 cc::LayerSelection empty_selection
;
758 layer_tree_host_
->RegisterSelection(empty_selection
);
761 void CompositeAndReadbackAsyncCallback(
762 blink::WebCompositeAndReadbackAsyncCallback
* callback
,
763 scoped_ptr
<cc::CopyOutputResult
> result
) {
764 if (result
->HasBitmap()) {
765 scoped_ptr
<SkBitmap
> result_bitmap
= result
->TakeBitmap();
766 callback
->didCompositeAndReadback(*result_bitmap
);
768 callback
->didCompositeAndReadback(SkBitmap());
772 bool RenderWidgetCompositor::CompositeIsSynchronous() const {
773 return !compositor_deps_
->GetCompositorImplThreadTaskRunner().get() &&
774 !layer_tree_host_
->settings().single_thread_proxy_scheduler
;
777 void RenderWidgetCompositor::layoutAndPaintAsync(
778 blink::WebLayoutAndPaintAsyncCallback
* callback
) {
779 DCHECK(!temporary_copy_output_request_
&& !layout_and_paint_async_callback_
);
780 layout_and_paint_async_callback_
= callback
;
782 if (CompositeIsSynchronous()) {
783 base::ThreadTaskRunnerHandle::Get()->PostTask(
784 FROM_HERE
, base::Bind(&RenderWidgetCompositor::LayoutAndUpdateLayers
,
785 weak_factory_
.GetWeakPtr()));
787 layer_tree_host_
->SetNeedsCommit();
791 void RenderWidgetCompositor::LayoutAndUpdateLayers() {
792 DCHECK(CompositeIsSynchronous());
793 layer_tree_host_
->LayoutAndUpdateLayers();
794 InvokeLayoutAndPaintCallback();
797 void RenderWidgetCompositor::InvokeLayoutAndPaintCallback() {
798 if (!layout_and_paint_async_callback_
)
800 layout_and_paint_async_callback_
->didLayoutAndPaint();
801 layout_and_paint_async_callback_
= nullptr;
804 void RenderWidgetCompositor::compositeAndReadbackAsync(
805 blink::WebCompositeAndReadbackAsyncCallback
* callback
) {
806 DCHECK(!temporary_copy_output_request_
&& !layout_and_paint_async_callback_
);
807 temporary_copy_output_request_
=
808 cc::CopyOutputRequest::CreateBitmapRequest(
809 base::Bind(&CompositeAndReadbackAsyncCallback
, callback
));
811 // Force a commit to happen. The temporary copy output request will
812 // be installed after layout which will happen as a part of the commit, for
813 // widgets that delay the creation of their output surface.
814 if (CompositeIsSynchronous()) {
815 base::ThreadTaskRunnerHandle::Get()->PostTask(
816 FROM_HERE
, base::Bind(&RenderWidgetCompositor::SynchronouslyComposite
,
817 weak_factory_
.GetWeakPtr()));
819 layer_tree_host_
->SetNeedsCommit();
823 void RenderWidgetCompositor::SynchronouslyComposite() {
824 DCHECK(CompositeIsSynchronous());
825 layer_tree_host_
->Composite(base::TimeTicks::Now());
828 void RenderWidgetCompositor::finishAllRendering() {
829 layer_tree_host_
->FinishAllRendering();
832 void RenderWidgetCompositor::setDeferCommits(bool defer_commits
) {
833 layer_tree_host_
->SetDeferCommits(defer_commits
);
836 int RenderWidgetCompositor::layerTreeId() const {
837 return layer_tree_host_
->id();
840 void RenderWidgetCompositor::setShowFPSCounter(bool show
) {
841 cc::LayerTreeDebugState debug_state
= layer_tree_host_
->debug_state();
842 debug_state
.show_fps_counter
= show
;
843 layer_tree_host_
->SetDebugState(debug_state
);
846 void RenderWidgetCompositor::setShowPaintRects(bool show
) {
847 cc::LayerTreeDebugState debug_state
= layer_tree_host_
->debug_state();
848 debug_state
.show_paint_rects
= show
;
849 layer_tree_host_
->SetDebugState(debug_state
);
852 void RenderWidgetCompositor::setShowDebugBorders(bool show
) {
853 cc::LayerTreeDebugState debug_state
= layer_tree_host_
->debug_state();
854 debug_state
.show_debug_borders
= show
;
855 layer_tree_host_
->SetDebugState(debug_state
);
858 void RenderWidgetCompositor::setContinuousPaintingEnabled(bool enabled
) {
859 cc::LayerTreeDebugState debug_state
= layer_tree_host_
->debug_state();
860 debug_state
.continuous_painting
= enabled
;
861 layer_tree_host_
->SetDebugState(debug_state
);
864 void RenderWidgetCompositor::setShowScrollBottleneckRects(bool show
) {
865 cc::LayerTreeDebugState debug_state
= layer_tree_host_
->debug_state();
866 debug_state
.show_touch_event_handler_rects
= show
;
867 debug_state
.show_wheel_event_handler_rects
= show
;
868 debug_state
.show_non_fast_scrollable_rects
= show
;
869 layer_tree_host_
->SetDebugState(debug_state
);
872 void RenderWidgetCompositor::updateTopControlsState(
873 WebTopControlsState constraints
,
874 WebTopControlsState current
,
876 layer_tree_host_
->UpdateTopControlsState(ConvertTopControlsState(constraints
),
877 ConvertTopControlsState(current
),
881 void RenderWidgetCompositor::setTopControlsHeight(float height
, bool shrink
) {
882 layer_tree_host_
->SetTopControlsHeight(height
, shrink
);
885 void RenderWidgetCompositor::setTopControlsShownRatio(float ratio
) {
886 layer_tree_host_
->SetTopControlsShownRatio(ratio
);
889 void RenderWidgetCompositor::setHidePinchScrollbarsNearMinScale(bool hide
) {
890 layer_tree_host_
->set_hide_pinch_scrollbars_near_min_scale(hide
);
893 void RenderWidgetCompositor::WillBeginMainFrame() {
894 widget_
->WillBeginCompositorFrame();
897 void RenderWidgetCompositor::DidBeginMainFrame() {
900 void RenderWidgetCompositor::BeginMainFrame(const cc::BeginFrameArgs
& args
) {
901 double frame_time_sec
= (args
.frame_time
- base::TimeTicks()).InSecondsF();
902 double deadline_sec
= (args
.deadline
- base::TimeTicks()).InSecondsF();
903 double interval_sec
= args
.interval
.InSecondsF();
904 WebBeginFrameArgs web_begin_frame_args
=
905 WebBeginFrameArgs(frame_time_sec
, deadline_sec
, interval_sec
);
906 compositor_deps_
->GetRendererScheduler()->WillBeginFrame(args
);
907 widget_
->webwidget()->beginFrame(web_begin_frame_args
);
910 void RenderWidgetCompositor::BeginMainFrameNotExpectedSoon() {
911 compositor_deps_
->GetRendererScheduler()->BeginFrameNotExpectedSoon();
914 void RenderWidgetCompositor::Layout() {
915 widget_
->webwidget()->layout();
917 if (temporary_copy_output_request_
) {
918 // For WebViewImpl, this will always have a root layer. For other widgets,
919 // the widget may be closed before servicing this request, so ignore it.
920 if (cc::Layer
* root_layer
= layer_tree_host_
->root_layer()) {
921 root_layer
->RequestCopyOfOutput(temporary_copy_output_request_
.Pass());
923 temporary_copy_output_request_
->SendEmptyResult();
924 temporary_copy_output_request_
= nullptr;
929 void RenderWidgetCompositor::ApplyViewportDeltas(
930 const gfx::Vector2dF
& inner_delta
,
931 const gfx::Vector2dF
& outer_delta
,
932 const gfx::Vector2dF
& elastic_overscroll_delta
,
934 float top_controls_delta
) {
935 widget_
->webwidget()->applyViewportDeltas(
938 elastic_overscroll_delta
,
943 void RenderWidgetCompositor::RequestNewOutputSurface() {
944 // If the host is closing, then no more compositing is possible. This
945 // prevents shutdown races between handling the close message and
946 // the CreateOutputSurface task.
947 if (widget_
->host_closing())
951 num_failed_recreate_attempts_
>= OUTPUT_SURFACE_RETRIES_BEFORE_FALLBACK
;
952 scoped_ptr
<cc::OutputSurface
> surface(widget_
->CreateOutputSurface(fallback
));
955 DidFailToInitializeOutputSurface();
959 DCHECK_EQ(surface
->capabilities().max_frames_pending
, 1);
961 layer_tree_host_
->SetOutputSurface(surface
.Pass());
964 void RenderWidgetCompositor::DidInitializeOutputSurface() {
965 num_failed_recreate_attempts_
= 0;
968 void RenderWidgetCompositor::DidFailToInitializeOutputSurface() {
969 ++num_failed_recreate_attempts_
;
970 // Tolerate a certain number of recreation failures to work around races
971 // in the output-surface-lost machinery.
972 LOG_IF(FATAL
, (num_failed_recreate_attempts_
>= MAX_OUTPUT_SURFACE_RETRIES
))
973 << "Failed to create a fallback OutputSurface.";
975 base::ThreadTaskRunnerHandle::Get()->PostTask(
976 FROM_HERE
, base::Bind(&RenderWidgetCompositor::RequestNewOutputSurface
,
977 weak_factory_
.GetWeakPtr()));
980 void RenderWidgetCompositor::WillCommit() {
981 InvokeLayoutAndPaintCallback();
984 void RenderWidgetCompositor::DidCommit() {
985 DCHECK(!temporary_copy_output_request_
);
986 widget_
->DidCommitCompositorFrame();
987 compositor_deps_
->GetRendererScheduler()->DidCommitFrameToCompositor();
990 void RenderWidgetCompositor::DidCommitAndDrawFrame() {
991 widget_
->DidCommitAndDrawCompositorFrame();
994 void RenderWidgetCompositor::DidCompleteSwapBuffers() {
995 widget_
->DidCompleteSwapBuffers();
996 bool threaded
= !!compositor_deps_
->GetCompositorImplThreadTaskRunner().get();
998 widget_
->OnSwapBuffersComplete();
1001 void RenderWidgetCompositor::DidCompletePageScaleAnimation() {
1002 widget_
->DidCompletePageScaleAnimation();
1005 void RenderWidgetCompositor::ScheduleAnimation() {
1006 widget_
->scheduleAnimation();
1009 void RenderWidgetCompositor::DidPostSwapBuffers() {
1010 widget_
->OnSwapBuffersPosted();
1013 void RenderWidgetCompositor::DidAbortSwapBuffers() {
1014 widget_
->OnSwapBuffersAborted();
1017 void RenderWidgetCompositor::RecordFrameTimingEvents(
1018 scoped_ptr
<cc::FrameTimingTracker::CompositeTimingSet
> composite_events
,
1019 scoped_ptr
<cc::FrameTimingTracker::MainFrameTimingSet
> main_frame_events
) {
1020 for (const auto& composite_event
: *composite_events
) {
1021 int64_t frameId
= composite_event
.first
;
1022 const std::vector
<cc::FrameTimingTracker::CompositeTimingEvent
>& events
=
1023 composite_event
.second
;
1024 std::vector
<blink::WebFrameTimingEvent
> webEvents
;
1025 for (size_t i
= 0; i
< events
.size(); ++i
) {
1026 webEvents
.push_back(blink::WebFrameTimingEvent(
1028 (events
[i
].timestamp
- base::TimeTicks()).InSecondsF()));
1030 widget_
->webwidget()->recordFrameTimingEvent(
1031 blink::WebWidget::CompositeEvent
, frameId
, webEvents
);
1033 for (const auto& main_frame_event
: *main_frame_events
) {
1034 int64_t frameId
= main_frame_event
.first
;
1035 const std::vector
<cc::FrameTimingTracker::MainFrameTimingEvent
>& events
=
1036 main_frame_event
.second
;
1037 std::vector
<blink::WebFrameTimingEvent
> webEvents
;
1038 for (size_t i
= 0; i
< events
.size(); ++i
) {
1039 webEvents
.push_back(blink::WebFrameTimingEvent(
1041 (events
[i
].timestamp
- base::TimeTicks()).InSecondsF(),
1042 (events
[i
].end_time
- base::TimeTicks()).InSecondsF()));
1044 widget_
->webwidget()->recordFrameTimingEvent(
1045 blink::WebWidget::RenderEvent
, frameId
, webEvents
);
1049 void RenderWidgetCompositor::RateLimitSharedMainThreadContext() {
1050 cc::ContextProvider
* provider
=
1051 compositor_deps_
->GetSharedMainThreadContextProvider();
1052 // provider can be NULL after the GPU process crashed enough times and we
1053 // don't want to restart it any more (falling back to software).
1056 provider
->ContextGL()->RateLimitOffscreenContextCHROMIUM();
1059 void RenderWidgetCompositor::SetSurfaceIdNamespace(
1060 uint32_t surface_id_namespace
) {
1061 layer_tree_host_
->set_surface_id_namespace(surface_id_namespace
);
1064 } // namespace content