Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / renderer / gpu / render_widget_compositor.cc
blob31aa6a8b643d6ad935ecf96660ced934b2f6e524
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"
7 #include <limits>
8 #include <string>
10 #include "base/command_line.h"
11 #include "base/logging.h"
12 #include "base/profiler/scoped_tracker.h"
13 #include "base/strings/string_number_conversions.h"
14 #include "base/synchronization/lock.h"
15 #include "base/sys_info.h"
16 #include "base/time/time.h"
17 #include "base/values.h"
18 #include "cc/base/switches.h"
19 #include "cc/blink/web_layer_impl.h"
20 #include "cc/debug/layer_tree_debug_state.h"
21 #include "cc/debug/micro_benchmark.h"
22 #include "cc/input/layer_selection_bound.h"
23 #include "cc/layers/layer.h"
24 #include "cc/output/begin_frame_args.h"
25 #include "cc/output/copy_output_request.h"
26 #include "cc/output/copy_output_result.h"
27 #include "cc/output/latency_info_swap_promise.h"
28 #include "cc/output/swap_promise.h"
29 #include "cc/resources/single_release_callback.h"
30 #include "cc/scheduler/begin_frame_source.h"
31 #include "cc/trees/latency_info_swap_promise_monitor.h"
32 #include "cc/trees/layer_tree_host.h"
33 #include "content/common/content_switches_internal.h"
34 #include "content/common/gpu/client/context_provider_command_buffer.h"
35 #include "content/public/common/content_switches.h"
36 #include "content/renderer/input/input_handler_manager.h"
37 #include "content/renderer/scheduler/renderer_scheduler.h"
38 #include "gpu/command_buffer/client/gles2_interface.h"
39 #include "third_party/WebKit/public/platform/WebCompositeAndReadbackAsyncCallback.h"
40 #include "third_party/WebKit/public/platform/WebSelectionBound.h"
41 #include "third_party/WebKit/public/platform/WebSize.h"
42 #include "third_party/WebKit/public/web/WebKit.h"
43 #include "third_party/WebKit/public/web/WebRuntimeFeatures.h"
44 #include "third_party/WebKit/public/web/WebWidget.h"
45 #include "ui/gfx/frame_time.h"
46 #include "ui/gl/gl_switches.h"
47 #include "ui/native_theme/native_theme_switches.h"
49 #if defined(OS_ANDROID)
50 #include "content/renderer/android/synchronous_compositor_factory.h"
51 #include "ui/gfx/android/device_display_info.h"
52 #endif
54 namespace base {
55 class Value;
58 namespace cc {
59 class Layer;
62 using blink::WebBeginFrameArgs;
63 using blink::WebFloatPoint;
64 using blink::WebRect;
65 using blink::WebSelectionBound;
66 using blink::WebSize;
67 using blink::WebTopControlsState;
69 namespace content {
70 namespace {
72 bool GetSwitchValueAsInt(const base::CommandLine& command_line,
73 const std::string& switch_string,
74 int min_value,
75 int max_value,
76 int* result) {
77 std::string string_value = command_line.GetSwitchValueASCII(switch_string);
78 int int_value;
79 if (base::StringToInt(string_value, &int_value) &&
80 int_value >= min_value && int_value <= max_value) {
81 *result = int_value;
82 return true;
83 } else {
84 LOG(WARNING) << "Failed to parse switch " << switch_string << ": " <<
85 string_value;
86 return false;
90 cc::LayerSelectionBound ConvertWebSelectionBound(
91 const WebSelectionBound& web_bound) {
92 DCHECK(web_bound.layerId);
94 cc::LayerSelectionBound cc_bound;
95 switch (web_bound.type) {
96 case blink::WebSelectionBound::Caret:
97 cc_bound.type = cc::SELECTION_BOUND_CENTER;
98 break;
99 case blink::WebSelectionBound::SelectionLeft:
100 cc_bound.type = cc::SELECTION_BOUND_LEFT;
101 break;
102 case blink::WebSelectionBound::SelectionRight:
103 cc_bound.type = cc::SELECTION_BOUND_RIGHT;
104 break;
106 cc_bound.layer_id = web_bound.layerId;
107 cc_bound.edge_top = gfx::Point(web_bound.edgeTopInLayer);
108 cc_bound.edge_bottom = gfx::Point(web_bound.edgeBottomInLayer);
109 return cc_bound;
112 gfx::Size CalculateDefaultTileSize(RenderWidget* widget) {
113 int default_tile_size = 256;
114 #if defined(OS_ANDROID)
115 // TODO(epenner): unify this for all platforms if it
116 // makes sense (http://crbug.com/159524)
118 gfx::DeviceDisplayInfo info;
119 bool real_size_supported = true;
120 int display_width = info.GetPhysicalDisplayWidth();
121 int display_height = info.GetPhysicalDisplayHeight();
122 if (display_width == 0 || display_height == 0) {
123 real_size_supported = false;
124 display_width = info.GetDisplayWidth();
125 display_height = info.GetDisplayHeight();
128 int portrait_width = std::min(display_width, display_height);
129 int landscape_width = std::max(display_width, display_height);
131 if (real_size_supported) {
132 // Maximum HD dimensions should be 768x1280
133 // Maximum FHD dimensions should be 1200x1920
134 if (portrait_width > 768 || landscape_width > 1280)
135 default_tile_size = 384;
136 if (portrait_width > 1200 || landscape_width > 1920)
137 default_tile_size = 512;
139 // Adjust for some resolutions that barely straddle an extra
140 // tile when in portrait mode. This helps worst case scroll/raster
141 // by not needing a full extra tile for each row.
142 if (default_tile_size == 256 && portrait_width == 768)
143 default_tile_size += 32;
144 if (default_tile_size == 384 && portrait_width == 1200)
145 default_tile_size += 32;
146 } else {
147 // We don't know the exact resolution due to screen controls etc.
148 // So this just estimates the values above using tile counts.
149 int numTiles = (display_width * display_height) / (256 * 256);
150 if (numTiles > 16)
151 default_tile_size = 384;
152 if (numTiles >= 40)
153 default_tile_size = 512;
155 #elif defined(OS_CHROMEOS)
156 // Use 512 for high DPI (dsf=2.0f) devices.
157 if (widget->screen_info().deviceScaleFactor >= 2.0f)
158 default_tile_size = 512;
159 #endif
161 return gfx::Size(default_tile_size, default_tile_size);
164 // Check cc::TopControlsState, and blink::WebTopControlsState
165 // are kept in sync.
166 static_assert(int(blink::WebTopControlsBoth) == int(cc::BOTH),
167 "mismatching enums: BOTH");
168 static_assert(int(blink::WebTopControlsHidden) == int(cc::HIDDEN),
169 "mismatching enums: HIDDEN");
170 static_assert(int(blink::WebTopControlsShown) == int(cc::SHOWN),
171 "mismatching enums: SHOWN");
173 static cc::TopControlsState ConvertTopControlsState(
174 WebTopControlsState state) {
175 return static_cast<cc::TopControlsState>(state);
178 } // namespace
180 // static
181 scoped_ptr<RenderWidgetCompositor> RenderWidgetCompositor::Create(
182 RenderWidget* widget,
183 CompositorDependencies* compositor_deps) {
184 scoped_ptr<RenderWidgetCompositor> compositor(
185 new RenderWidgetCompositor(widget, compositor_deps));
186 compositor->Initialize();
187 return compositor;
190 RenderWidgetCompositor::RenderWidgetCompositor(
191 RenderWidget* widget,
192 CompositorDependencies* compositor_deps)
193 : num_failed_recreate_attempts_(0),
194 widget_(widget),
195 compositor_deps_(compositor_deps),
196 weak_factory_(this) {
199 void RenderWidgetCompositor::Initialize() {
200 base::CommandLine* cmd = base::CommandLine::ForCurrentProcess();
202 cc::LayerTreeSettings settings;
204 // For web contents, layer transforms should scale up the contents of layers
205 // to keep content always crisp when possible.
206 settings.layer_transforms_should_scale_layer_contents = true;
208 settings.throttle_frame_production =
209 !cmd->HasSwitch(switches::kDisableGpuVsync);
210 settings.use_external_begin_frame_source =
211 cmd->HasSwitch(switches::kEnableBeginFrameScheduling);
212 settings.main_frame_before_activation_enabled =
213 cmd->HasSwitch(cc::switches::kEnableMainFrameBeforeActivation) &&
214 !cmd->HasSwitch(cc::switches::kDisableMainFrameBeforeActivation);
215 settings.report_overscroll_only_for_scrollable_axes =
216 !compositor_deps_->IsElasticOverscrollEnabled();
217 settings.accelerated_animation_enabled =
218 !cmd->HasSwitch(cc::switches::kDisableThreadedAnimation);
219 settings.use_display_lists = cmd->HasSwitch(switches::kEnableSlimmingPaint);
220 if (cmd->HasSwitch(switches::kEnableCompositorAnimationTimelines)) {
221 settings.use_compositor_animation_timelines = true;
222 blink::WebRuntimeFeatures::enableCompositorAnimationTimelines(true);
225 settings.default_tile_size = CalculateDefaultTileSize(widget_);
226 if (cmd->HasSwitch(switches::kDefaultTileWidth)) {
227 int tile_width = 0;
228 GetSwitchValueAsInt(*cmd,
229 switches::kDefaultTileWidth,
231 std::numeric_limits<int>::max(),
232 &tile_width);
233 settings.default_tile_size.set_width(tile_width);
235 if (cmd->HasSwitch(switches::kDefaultTileHeight)) {
236 int tile_height = 0;
237 GetSwitchValueAsInt(*cmd,
238 switches::kDefaultTileHeight,
240 std::numeric_limits<int>::max(),
241 &tile_height);
242 settings.default_tile_size.set_height(tile_height);
245 int max_untiled_layer_width = settings.max_untiled_layer_size.width();
246 if (cmd->HasSwitch(switches::kMaxUntiledLayerWidth)) {
247 GetSwitchValueAsInt(*cmd, switches::kMaxUntiledLayerWidth, 1,
248 std::numeric_limits<int>::max(),
249 &max_untiled_layer_width);
251 int max_untiled_layer_height = settings.max_untiled_layer_size.height();
252 if (cmd->HasSwitch(switches::kMaxUntiledLayerHeight)) {
253 GetSwitchValueAsInt(*cmd, switches::kMaxUntiledLayerHeight, 1,
254 std::numeric_limits<int>::max(),
255 &max_untiled_layer_height);
258 settings.max_untiled_layer_size = gfx::Size(max_untiled_layer_width,
259 max_untiled_layer_height);
261 settings.gpu_rasterization_msaa_sample_count =
262 compositor_deps_->GetGpuRasterizationMSAASampleCount();
263 settings.impl_side_painting = compositor_deps_->IsImplSidePaintingEnabled();
264 settings.gpu_rasterization_forced =
265 compositor_deps_->IsGpuRasterizationForced();
266 settings.gpu_rasterization_enabled =
267 compositor_deps_->IsGpuRasterizationEnabled();
269 settings.can_use_lcd_text = compositor_deps_->IsLcdTextEnabled();
270 settings.use_distance_field_text =
271 compositor_deps_->IsDistanceFieldTextEnabled();
272 settings.use_zero_copy = compositor_deps_->IsZeroCopyEnabled();
273 settings.use_one_copy = compositor_deps_->IsOneCopyEnabled();
274 settings.enable_elastic_overscroll =
275 compositor_deps_->IsElasticOverscrollEnabled();
276 settings.use_image_texture_target = compositor_deps_->GetImageTextureTarget();
277 settings.gather_pixel_refs = compositor_deps_->IsGatherPixelRefsEnabled();
279 if (cmd->HasSwitch(cc::switches::kTopControlsShowThreshold)) {
280 std::string top_threshold_str =
281 cmd->GetSwitchValueASCII(cc::switches::kTopControlsShowThreshold);
282 double show_threshold;
283 if (base::StringToDouble(top_threshold_str, &show_threshold) &&
284 show_threshold >= 0.f && show_threshold <= 1.f)
285 settings.top_controls_show_threshold = show_threshold;
288 if (cmd->HasSwitch(cc::switches::kTopControlsHideThreshold)) {
289 std::string top_threshold_str =
290 cmd->GetSwitchValueASCII(cc::switches::kTopControlsHideThreshold);
291 double hide_threshold;
292 if (base::StringToDouble(top_threshold_str, &hide_threshold) &&
293 hide_threshold >= 0.f && hide_threshold <= 1.f)
294 settings.top_controls_hide_threshold = hide_threshold;
297 settings.use_pinch_virtual_viewport =
298 cmd->HasSwitch(cc::switches::kEnablePinchVirtualViewport);
299 settings.verify_property_trees =
300 cmd->HasSwitch(cc::switches::kEnablePropertyTreeVerification);
301 settings.renderer_settings.allow_antialiasing &=
302 !cmd->HasSwitch(cc::switches::kDisableCompositedAntialiasing);
303 settings.single_thread_proxy_scheduler =
304 compositor_deps_->UseSingleThreadScheduler();
306 // These flags should be mirrored by UI versions in ui/compositor/.
307 settings.initial_debug_state.show_debug_borders =
308 cmd->HasSwitch(cc::switches::kShowCompositedLayerBorders);
309 settings.initial_debug_state.show_fps_counter =
310 cmd->HasSwitch(cc::switches::kShowFPSCounter);
311 settings.initial_debug_state.show_layer_animation_bounds_rects =
312 cmd->HasSwitch(cc::switches::kShowLayerAnimationBounds);
313 settings.initial_debug_state.show_paint_rects =
314 cmd->HasSwitch(switches::kShowPaintRects);
315 settings.initial_debug_state.show_property_changed_rects =
316 cmd->HasSwitch(cc::switches::kShowPropertyChangedRects);
317 settings.initial_debug_state.show_surface_damage_rects =
318 cmd->HasSwitch(cc::switches::kShowSurfaceDamageRects);
319 settings.initial_debug_state.show_screen_space_rects =
320 cmd->HasSwitch(cc::switches::kShowScreenSpaceRects);
321 settings.initial_debug_state.show_replica_screen_space_rects =
322 cmd->HasSwitch(cc::switches::kShowReplicaScreenSpaceRects);
324 settings.initial_debug_state.SetRecordRenderingStats(
325 cmd->HasSwitch(cc::switches::kEnableGpuBenchmarking));
327 if (cmd->HasSwitch(cc::switches::kSlowDownRasterScaleFactor)) {
328 const int kMinSlowDownScaleFactor = 0;
329 const int kMaxSlowDownScaleFactor = INT_MAX;
330 GetSwitchValueAsInt(
331 *cmd,
332 cc::switches::kSlowDownRasterScaleFactor,
333 kMinSlowDownScaleFactor,
334 kMaxSlowDownScaleFactor,
335 &settings.initial_debug_state.slow_down_raster_scale_factor);
338 if (cmd->HasSwitch(cc::switches::kMaxTilesForInterestArea)) {
339 int max_tiles_for_interest_area;
340 if (GetSwitchValueAsInt(*cmd,
341 cc::switches::kMaxTilesForInterestArea,
342 1, std::numeric_limits<int>::max(),
343 &max_tiles_for_interest_area))
344 settings.max_tiles_for_interest_area = max_tiles_for_interest_area;
347 if (cmd->HasSwitch(cc::switches::kMaxUnusedResourceMemoryUsagePercentage)) {
348 int max_unused_resource_memory_percentage;
349 if (GetSwitchValueAsInt(
350 *cmd,
351 cc::switches::kMaxUnusedResourceMemoryUsagePercentage,
352 0, 100,
353 &max_unused_resource_memory_percentage)) {
354 settings.max_unused_resource_memory_percentage =
355 max_unused_resource_memory_percentage;
359 settings.strict_layer_property_change_checking =
360 cmd->HasSwitch(cc::switches::kStrictLayerPropertyChangeChecking);
362 #if defined(OS_ANDROID)
363 SynchronousCompositorFactory* synchronous_compositor_factory =
364 SynchronousCompositorFactory::GetInstance();
366 // We can't use GPU rasterization on low-end devices, because the Ganesh
367 // cache would consume too much memory.
368 if (base::SysInfo::IsLowEndDevice())
369 settings.gpu_rasterization_enabled = false;
370 settings.using_synchronous_renderer_compositor =
371 synchronous_compositor_factory;
372 settings.record_full_layer = widget_->DoesRecordFullLayer();
373 settings.report_overscroll_only_for_scrollable_axes =
374 !synchronous_compositor_factory;
375 settings.max_partial_texture_updates = 0;
376 if (synchronous_compositor_factory) {
377 // Android WebView uses system scrollbars, so make ours invisible.
378 settings.scrollbar_animator = cc::LayerTreeSettings::NO_ANIMATOR;
379 settings.solid_color_scrollbar_color = SK_ColorTRANSPARENT;
380 } else {
381 settings.scrollbar_animator = cc::LayerTreeSettings::LINEAR_FADE;
382 settings.scrollbar_fade_delay_ms = 300;
383 settings.scrollbar_fade_resize_delay_ms = 2000;
384 settings.scrollbar_fade_duration_ms = 300;
385 settings.solid_color_scrollbar_color = SkColorSetARGB(128, 128, 128, 128);
387 settings.renderer_settings.highp_threshold_min = 2048;
388 // Android WebView handles root layer flings itself.
389 settings.ignore_root_layer_flings =
390 synchronous_compositor_factory;
391 // Memory policy on Android WebView does not depend on whether device is
392 // low end, so always use default policy.
393 bool use_low_memory_policy =
394 base::SysInfo::IsLowEndDevice() && !synchronous_compositor_factory;
395 // RGBA_4444 textures are only enabled for low end devices
396 // and are disabled for Android WebView as it doesn't support the format.
397 settings.renderer_settings.use_rgba_4444_textures = use_low_memory_policy;
398 if (use_low_memory_policy) {
399 // On low-end we want to be very carefull about killing other
400 // apps. So initially we use 50% more memory to avoid flickering
401 // or raster-on-demand.
402 settings.max_memory_for_prepaint_percentage = 67;
403 } else {
404 // On other devices we have increased memory excessively to avoid
405 // raster-on-demand already, so now we reserve 50% _only_ to avoid
406 // raster-on-demand, and use 50% of the memory otherwise.
407 settings.max_memory_for_prepaint_percentage = 50;
409 // Webview does not own the surface so should not clear it.
410 settings.renderer_settings.should_clear_root_render_pass =
411 !synchronous_compositor_factory;
413 // TODO(danakj): Only do this on low end devices.
414 settings.create_low_res_tiling = true;
416 #elif !defined(OS_MACOSX)
417 if (ui::IsOverlayScrollbarEnabled()) {
418 settings.scrollbar_animator = cc::LayerTreeSettings::THINNING;
419 settings.solid_color_scrollbar_color = SkColorSetARGB(128, 128, 128, 128);
420 } else if (cmd->HasSwitch(cc::switches::kEnablePinchVirtualViewport)) {
421 // use_pinch_zoom_scrollbars is only true on desktop when non-overlay
422 // scrollbars are in use.
423 settings.use_pinch_zoom_scrollbars = true;
424 settings.scrollbar_animator = cc::LayerTreeSettings::LINEAR_FADE;
425 settings.solid_color_scrollbar_color = SkColorSetARGB(128, 128, 128, 128);
427 settings.scrollbar_fade_delay_ms = 500;
428 settings.scrollbar_fade_resize_delay_ms = 500;
429 settings.scrollbar_fade_duration_ms = 300;
431 // When pinching in, only show the pinch-viewport overlay scrollbars if the
432 // page scale is at least some threshold away from the minimum. i.e. don't
433 // show the pinch scrollbars when at minimum scale.
434 settings.scrollbar_show_scale_threshold = 1.05f;
435 #endif
437 if (cmd->HasSwitch(switches::kEnableLowResTiling))
438 settings.create_low_res_tiling = true;
439 if (cmd->HasSwitch(switches::kDisableLowResTiling))
440 settings.create_low_res_tiling = false;
442 scoped_refptr<base::SingleThreadTaskRunner> compositor_thread_task_runner =
443 compositor_deps_->GetCompositorImplThreadTaskRunner();
444 scoped_refptr<base::SingleThreadTaskRunner>
445 main_thread_compositor_task_runner =
446 compositor_deps_->GetCompositorMainThreadTaskRunner();
447 cc::SharedBitmapManager* shared_bitmap_manager =
448 compositor_deps_->GetSharedBitmapManager();
449 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager =
450 compositor_deps_->GetGpuMemoryBufferManager();
451 cc::TaskGraphRunner* task_graph_runner =
452 compositor_deps_->GetTaskGraphRunner();
454 scoped_ptr<cc::BeginFrameSource> external_begin_frame_source;
455 if (settings.use_external_begin_frame_source) {
456 external_begin_frame_source =
457 compositor_deps_->CreateExternalBeginFrameSource(widget_->routing_id());
460 if (compositor_thread_task_runner.get()) {
461 layer_tree_host_ = cc::LayerTreeHost::CreateThreaded(
462 this, shared_bitmap_manager, gpu_memory_buffer_manager,
463 task_graph_runner, settings, main_thread_compositor_task_runner,
464 compositor_thread_task_runner, external_begin_frame_source.Pass());
465 } else {
466 layer_tree_host_ = cc::LayerTreeHost::CreateSingleThreaded(
467 this, this, shared_bitmap_manager, gpu_memory_buffer_manager,
468 task_graph_runner, settings, main_thread_compositor_task_runner,
469 external_begin_frame_source.Pass());
471 DCHECK(layer_tree_host_);
474 RenderWidgetCompositor::~RenderWidgetCompositor() {}
476 const base::WeakPtr<cc::InputHandler>&
477 RenderWidgetCompositor::GetInputHandler() {
478 return layer_tree_host_->GetInputHandler();
481 bool RenderWidgetCompositor::BeginMainFrameRequested() const {
482 return layer_tree_host_->BeginMainFrameRequested();
485 void RenderWidgetCompositor::SetNeedsDisplayOnAllLayers() {
486 layer_tree_host_->SetNeedsDisplayOnAllLayers();
489 void RenderWidgetCompositor::SetRasterizeOnlyVisibleContent() {
490 cc::LayerTreeDebugState current = layer_tree_host_->debug_state();
491 current.rasterize_only_visible_content = true;
492 layer_tree_host_->SetDebugState(current);
495 void RenderWidgetCompositor::SetNeedsRedrawRect(gfx::Rect damage_rect) {
496 layer_tree_host_->SetNeedsRedrawRect(damage_rect);
499 void RenderWidgetCompositor::SetNeedsForcedRedraw() {
500 layer_tree_host_->SetNextCommitForcesRedraw();
501 setNeedsAnimate();
504 scoped_ptr<cc::SwapPromiseMonitor>
505 RenderWidgetCompositor::CreateLatencyInfoSwapPromiseMonitor(
506 ui::LatencyInfo* latency) {
507 return scoped_ptr<cc::SwapPromiseMonitor>(
508 new cc::LatencyInfoSwapPromiseMonitor(
509 latency, layer_tree_host_.get(), NULL));
512 void RenderWidgetCompositor::QueueSwapPromise(
513 scoped_ptr<cc::SwapPromise> swap_promise) {
514 layer_tree_host_->QueueSwapPromise(swap_promise.Pass());
517 int RenderWidgetCompositor::GetLayerTreeId() const {
518 return layer_tree_host_->id();
521 int RenderWidgetCompositor::GetSourceFrameNumber() const {
522 return layer_tree_host_->source_frame_number();
525 void RenderWidgetCompositor::SetNeedsUpdateLayers() {
526 layer_tree_host_->SetNeedsUpdateLayers();
529 void RenderWidgetCompositor::SetNeedsCommit() {
530 layer_tree_host_->SetNeedsCommit();
533 void RenderWidgetCompositor::NotifyInputThrottledUntilCommit() {
534 layer_tree_host_->NotifyInputThrottledUntilCommit();
537 const cc::Layer* RenderWidgetCompositor::GetRootLayer() const {
538 return layer_tree_host_->root_layer();
541 int RenderWidgetCompositor::ScheduleMicroBenchmark(
542 const std::string& name,
543 scoped_ptr<base::Value> value,
544 const base::Callback<void(scoped_ptr<base::Value>)>& callback) {
545 return layer_tree_host_->ScheduleMicroBenchmark(name, value.Pass(), callback);
548 bool RenderWidgetCompositor::SendMessageToMicroBenchmark(
549 int id,
550 scoped_ptr<base::Value> value) {
551 return layer_tree_host_->SendMessageToMicroBenchmark(id, value.Pass());
554 void RenderWidgetCompositor::StartCompositor() {
555 layer_tree_host_->SetLayerTreeHostClientReady();
558 void RenderWidgetCompositor::setRootLayer(const blink::WebLayer& layer) {
559 layer_tree_host_->SetRootLayer(
560 static_cast<const cc_blink::WebLayerImpl*>(&layer)->layer());
563 void RenderWidgetCompositor::clearRootLayer() {
564 layer_tree_host_->SetRootLayer(scoped_refptr<cc::Layer>());
567 void RenderWidgetCompositor::setViewportSize(
568 const WebSize&,
569 const WebSize& device_viewport_size) {
570 layer_tree_host_->SetViewportSize(device_viewport_size);
573 void RenderWidgetCompositor::setViewportSize(
574 const WebSize& device_viewport_size) {
575 layer_tree_host_->SetViewportSize(device_viewport_size);
578 WebSize RenderWidgetCompositor::layoutViewportSize() const {
579 return layer_tree_host_->device_viewport_size();
582 WebSize RenderWidgetCompositor::deviceViewportSize() const {
583 return layer_tree_host_->device_viewport_size();
586 WebFloatPoint RenderWidgetCompositor::adjustEventPointForPinchZoom(
587 const WebFloatPoint& point) const {
588 return point;
591 void RenderWidgetCompositor::setDeviceScaleFactor(float device_scale) {
592 layer_tree_host_->SetDeviceScaleFactor(device_scale);
595 float RenderWidgetCompositor::deviceScaleFactor() const {
596 return layer_tree_host_->device_scale_factor();
599 void RenderWidgetCompositor::setBackgroundColor(blink::WebColor color) {
600 layer_tree_host_->set_background_color(color);
603 void RenderWidgetCompositor::setHasTransparentBackground(bool transparent) {
604 layer_tree_host_->set_has_transparent_background(transparent);
607 void RenderWidgetCompositor::setVisible(bool visible) {
608 layer_tree_host_->SetVisible(visible);
611 void RenderWidgetCompositor::setPageScaleFactorAndLimits(
612 float page_scale_factor, float minimum, float maximum) {
613 layer_tree_host_->SetPageScaleFactorAndLimits(
614 page_scale_factor, minimum, maximum);
617 void RenderWidgetCompositor::startPageScaleAnimation(
618 const blink::WebPoint& destination,
619 bool use_anchor,
620 float new_page_scale,
621 double duration_sec) {
622 base::TimeDelta duration = base::TimeDelta::FromMicroseconds(
623 duration_sec * base::Time::kMicrosecondsPerSecond);
624 layer_tree_host_->StartPageScaleAnimation(
625 gfx::Vector2d(destination.x, destination.y),
626 use_anchor,
627 new_page_scale,
628 duration);
631 void RenderWidgetCompositor::heuristicsForGpuRasterizationUpdated(
632 bool matches_heuristics) {
633 layer_tree_host_->SetHasGpuRasterizationTrigger(matches_heuristics);
636 void RenderWidgetCompositor::setNeedsAnimate() {
637 layer_tree_host_->SetNeedsAnimate();
640 bool RenderWidgetCompositor::commitRequested() const {
641 return layer_tree_host_->CommitRequested();
644 void RenderWidgetCompositor::didStopFlinging() {
645 layer_tree_host_->DidStopFlinging();
648 void RenderWidgetCompositor::registerForAnimations(blink::WebLayer* layer) {
649 cc::Layer* cc_layer = static_cast<cc_blink::WebLayerImpl*>(layer)->layer();
650 cc_layer->layer_animation_controller()->SetAnimationRegistrar(
651 layer_tree_host_->animation_registrar());
654 void RenderWidgetCompositor::registerViewportLayers(
655 const blink::WebLayer* overscrollElasticityLayer,
656 const blink::WebLayer* pageScaleLayer,
657 const blink::WebLayer* innerViewportScrollLayer,
658 const blink::WebLayer* outerViewportScrollLayer) {
659 layer_tree_host_->RegisterViewportLayers(
660 // The scroll elasticity layer will only exist when using pinch virtual
661 // viewports.
662 overscrollElasticityLayer
663 ? static_cast<const cc_blink::WebLayerImpl*>(
664 overscrollElasticityLayer)->layer()
665 : NULL,
666 static_cast<const cc_blink::WebLayerImpl*>(pageScaleLayer)->layer(),
667 static_cast<const cc_blink::WebLayerImpl*>(innerViewportScrollLayer)
668 ->layer(),
669 // The outer viewport layer will only exist when using pinch virtual
670 // viewports.
671 outerViewportScrollLayer
672 ? static_cast<const cc_blink::WebLayerImpl*>(outerViewportScrollLayer)
673 ->layer()
674 : NULL);
677 void RenderWidgetCompositor::clearViewportLayers() {
678 layer_tree_host_->RegisterViewportLayers(
679 scoped_refptr<cc::Layer>(), scoped_refptr<cc::Layer>(),
680 scoped_refptr<cc::Layer>(), scoped_refptr<cc::Layer>());
683 void RenderWidgetCompositor::registerSelection(
684 const blink::WebSelectionBound& start,
685 const blink::WebSelectionBound& end) {
686 layer_tree_host_->RegisterSelection(ConvertWebSelectionBound(start),
687 ConvertWebSelectionBound(end));
690 void RenderWidgetCompositor::clearSelection() {
691 cc::LayerSelectionBound empty_selection;
692 layer_tree_host_->RegisterSelection(empty_selection, empty_selection);
695 void CompositeAndReadbackAsyncCallback(
696 blink::WebCompositeAndReadbackAsyncCallback* callback,
697 scoped_ptr<cc::CopyOutputResult> result) {
698 if (result->HasBitmap()) {
699 scoped_ptr<SkBitmap> result_bitmap = result->TakeBitmap();
700 callback->didCompositeAndReadback(*result_bitmap);
701 } else {
702 callback->didCompositeAndReadback(SkBitmap());
706 void RenderWidgetCompositor::compositeAndReadbackAsync(
707 blink::WebCompositeAndReadbackAsyncCallback* callback) {
708 DCHECK(!temporary_copy_output_request_);
709 temporary_copy_output_request_ =
710 cc::CopyOutputRequest::CreateBitmapRequest(
711 base::Bind(&CompositeAndReadbackAsyncCallback, callback));
712 // Force a commit to happen. The temporary copy output request will
713 // be installed after layout which will happen as a part of the commit, for
714 // widgets that delay the creation of their output surface.
715 bool threaded = !!compositor_deps_->GetCompositorImplThreadTaskRunner().get();
716 if (!threaded &&
717 !layer_tree_host_->settings().single_thread_proxy_scheduler) {
718 layer_tree_host_->Composite(gfx::FrameTime::Now());
719 } else {
720 layer_tree_host_->SetNeedsCommit();
724 void RenderWidgetCompositor::finishAllRendering() {
725 layer_tree_host_->FinishAllRendering();
728 void RenderWidgetCompositor::setDeferCommits(bool defer_commits) {
729 layer_tree_host_->SetDeferCommits(defer_commits);
732 void RenderWidgetCompositor::setShowFPSCounter(bool show) {
733 cc::LayerTreeDebugState debug_state = layer_tree_host_->debug_state();
734 debug_state.show_fps_counter = show;
735 layer_tree_host_->SetDebugState(debug_state);
738 void RenderWidgetCompositor::setShowPaintRects(bool show) {
739 cc::LayerTreeDebugState debug_state = layer_tree_host_->debug_state();
740 debug_state.show_paint_rects = show;
741 layer_tree_host_->SetDebugState(debug_state);
744 void RenderWidgetCompositor::setShowDebugBorders(bool show) {
745 cc::LayerTreeDebugState debug_state = layer_tree_host_->debug_state();
746 debug_state.show_debug_borders = show;
747 layer_tree_host_->SetDebugState(debug_state);
750 void RenderWidgetCompositor::setContinuousPaintingEnabled(bool enabled) {
751 cc::LayerTreeDebugState debug_state = layer_tree_host_->debug_state();
752 debug_state.continuous_painting = enabled;
753 layer_tree_host_->SetDebugState(debug_state);
756 void RenderWidgetCompositor::setShowScrollBottleneckRects(bool show) {
757 cc::LayerTreeDebugState debug_state = layer_tree_host_->debug_state();
758 debug_state.show_touch_event_handler_rects = show;
759 debug_state.show_wheel_event_handler_rects = show;
760 debug_state.show_non_fast_scrollable_rects = show;
761 layer_tree_host_->SetDebugState(debug_state);
764 void RenderWidgetCompositor::updateTopControlsState(
765 WebTopControlsState constraints,
766 WebTopControlsState current,
767 bool animate) {
768 layer_tree_host_->UpdateTopControlsState(ConvertTopControlsState(constraints),
769 ConvertTopControlsState(current),
770 animate);
773 void RenderWidgetCompositor::setTopControlsHeight(float height, bool shrink) {
774 layer_tree_host_->SetTopControlsHeight(height, shrink);
777 void RenderWidgetCompositor::setTopControlsShownRatio(float ratio) {
778 layer_tree_host_->SetTopControlsShownRatio(ratio);
781 void RenderWidgetCompositor::WillBeginMainFrame() {
782 widget_->willBeginCompositorFrame();
785 void RenderWidgetCompositor::DidBeginMainFrame() {
788 void RenderWidgetCompositor::BeginMainFrame(const cc::BeginFrameArgs& args) {
789 double frame_time_sec = (args.frame_time - base::TimeTicks()).InSecondsF();
790 double deadline_sec = (args.deadline - base::TimeTicks()).InSecondsF();
791 double interval_sec = args.interval.InSecondsF();
792 WebBeginFrameArgs web_begin_frame_args =
793 WebBeginFrameArgs(frame_time_sec, deadline_sec, interval_sec);
794 compositor_deps_->GetRendererScheduler()->WillBeginFrame(args);
795 widget_->webwidget()->beginFrame(web_begin_frame_args);
798 void RenderWidgetCompositor::BeginMainFrameNotExpectedSoon() {
799 compositor_deps_->GetRendererScheduler()->BeginFrameNotExpectedSoon();
802 void RenderWidgetCompositor::Layout() {
803 widget_->webwidget()->layout();
805 if (temporary_copy_output_request_) {
806 // For WebViewImpl, this will always have a root layer. For other widgets,
807 // the widget may be closed before servicing this request, so ignore it.
808 if (cc::Layer* root_layer = layer_tree_host_->root_layer()) {
809 root_layer->RequestCopyOfOutput(temporary_copy_output_request_.Pass());
810 } else {
811 temporary_copy_output_request_->SendEmptyResult();
812 temporary_copy_output_request_ = nullptr;
817 void RenderWidgetCompositor::ApplyViewportDeltas(
818 const gfx::Vector2dF& inner_delta,
819 const gfx::Vector2dF& outer_delta,
820 const gfx::Vector2dF& elastic_overscroll_delta,
821 float page_scale,
822 float top_controls_delta) {
823 widget_->webwidget()->applyViewportDeltas(
824 inner_delta,
825 outer_delta,
826 elastic_overscroll_delta,
827 page_scale,
828 top_controls_delta);
831 void RenderWidgetCompositor::ApplyViewportDeltas(
832 const gfx::Vector2d& scroll_delta,
833 float page_scale,
834 float top_controls_delta) {
835 widget_->webwidget()->applyViewportDeltas(
836 scroll_delta,
837 page_scale,
838 top_controls_delta);
841 void RenderWidgetCompositor::RequestNewOutputSurface() {
842 // If the host is closing, then no more compositing is possible. This
843 // prevents shutdown races between handling the close message and
844 // the CreateOutputSurface task.
845 if (widget_->host_closing())
846 return;
848 // TODO(robliao): Remove ScopedTracker below once https://crbug.com/466870
849 // is fixed.
850 tracked_objects::ScopedTracker tracking_profile(
851 FROM_HERE_WITH_EXPLICIT_FUNCTION(
852 "466870 RenderWidgetCompositor::RequestNewOutputSurface"));
854 bool fallback =
855 num_failed_recreate_attempts_ >= OUTPUT_SURFACE_RETRIES_BEFORE_FALLBACK;
856 scoped_ptr<cc::OutputSurface> surface(widget_->CreateOutputSurface(fallback));
858 if (!surface) {
859 DidFailToInitializeOutputSurface();
860 return;
863 layer_tree_host_->SetOutputSurface(surface.Pass());
866 void RenderWidgetCompositor::DidInitializeOutputSurface() {
867 num_failed_recreate_attempts_ = 0;
870 void RenderWidgetCompositor::DidFailToInitializeOutputSurface() {
871 ++num_failed_recreate_attempts_;
872 // Tolerate a certain number of recreation failures to work around races
873 // in the output-surface-lost machinery.
874 LOG_IF(FATAL, (num_failed_recreate_attempts_ >= MAX_OUTPUT_SURFACE_RETRIES))
875 << "Failed to create a fallback OutputSurface.";
877 base::MessageLoop::current()->PostTask(
878 FROM_HERE, base::Bind(&RenderWidgetCompositor::RequestNewOutputSurface,
879 weak_factory_.GetWeakPtr()));
882 void RenderWidgetCompositor::WillCommit() {
885 void RenderWidgetCompositor::DidCommit() {
886 DCHECK(!temporary_copy_output_request_);
887 widget_->DidCommitCompositorFrame();
888 widget_->didBecomeReadyForAdditionalInput();
889 compositor_deps_->GetRendererScheduler()->DidCommitFrameToCompositor();
892 void RenderWidgetCompositor::DidCommitAndDrawFrame() {
893 widget_->didCommitAndDrawCompositorFrame();
896 void RenderWidgetCompositor::DidCompleteSwapBuffers() {
897 widget_->didCompleteSwapBuffers();
898 bool threaded = !!compositor_deps_->GetCompositorImplThreadTaskRunner().get();
899 if (!threaded)
900 widget_->OnSwapBuffersComplete();
903 void RenderWidgetCompositor::DidCompletePageScaleAnimation() {
904 widget_->DidCompletePageScaleAnimation();
907 void RenderWidgetCompositor::ScheduleAnimation() {
908 widget_->scheduleAnimation();
911 void RenderWidgetCompositor::DidPostSwapBuffers() {
912 widget_->OnSwapBuffersPosted();
915 void RenderWidgetCompositor::DidAbortSwapBuffers() {
916 widget_->OnSwapBuffersAborted();
919 void RenderWidgetCompositor::RateLimitSharedMainThreadContext() {
920 cc::ContextProvider* provider =
921 compositor_deps_->GetSharedMainThreadContextProvider();
922 // provider can be NULL after the GPU process crashed enough times and we
923 // don't want to restart it any more (falling back to software).
924 if (!provider)
925 return;
926 provider->ContextGL()->RateLimitOffscreenContextCHROMIUM();
929 } // namespace content