Update UnusedResources lint suppressions.
[chromium-blink-merge.git] / content / renderer / gpu / render_widget_compositor.cc
blob74b1f33b80f7581ab179efec029b038054223c17
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/location.h"
12 #include "base/logging.h"
13 #include "base/metrics/field_trial.h"
14 #include "base/profiler/scoped_tracker.h"
15 #include "base/single_thread_task_runner.h"
16 #include "base/strings/string_number_conversions.h"
17 #include "base/synchronization/lock.h"
18 #include "base/sys_info.h"
19 #include "base/thread_task_runner_handle.h"
20 #include "base/time/time.h"
21 #include "base/values.h"
22 #include "cc/base/switches.h"
23 #include "cc/blink/web_layer_impl.h"
24 #include "cc/debug/layer_tree_debug_state.h"
25 #include "cc/debug/micro_benchmark.h"
26 #include "cc/input/layer_selection_bound.h"
27 #include "cc/layers/layer.h"
28 #include "cc/output/begin_frame_args.h"
29 #include "cc/output/copy_output_request.h"
30 #include "cc/output/copy_output_result.h"
31 #include "cc/output/latency_info_swap_promise.h"
32 #include "cc/output/swap_promise.h"
33 #include "cc/resources/single_release_callback.h"
34 #include "cc/scheduler/begin_frame_source.h"
35 #include "cc/trees/latency_info_swap_promise_monitor.h"
36 #include "cc/trees/layer_tree_host.h"
37 #include "components/scheduler/renderer/renderer_scheduler.h"
38 #include "content/common/content_switches_internal.h"
39 #include "content/common/gpu/client/context_provider_command_buffer.h"
40 #include "content/public/common/content_switches.h"
41 #include "content/renderer/input/input_handler_manager.h"
42 #include "gpu/command_buffer/client/gles2_interface.h"
43 #include "third_party/WebKit/public/platform/WebCompositeAndReadbackAsyncCallback.h"
44 #include "third_party/WebKit/public/platform/WebLayoutAndPaintAsyncCallback.h"
45 #include "third_party/WebKit/public/platform/WebSize.h"
46 #include "third_party/WebKit/public/web/WebKit.h"
47 #include "third_party/WebKit/public/web/WebRuntimeFeatures.h"
48 #include "third_party/WebKit/public/web/WebSelection.h"
49 #include "third_party/WebKit/public/web/WebWidget.h"
50 #include "ui/gl/gl_switches.h"
51 #include "ui/native_theme/native_theme_switches.h"
53 #if defined(OS_ANDROID)
54 #include "base/android/build_info.h"
55 #include "content/renderer/android/synchronous_compositor_factory.h"
56 #include "ui/gfx/android/device_display_info.h"
57 #endif
59 namespace base {
60 class Value;
63 namespace cc {
64 class Layer;
67 using blink::WebBeginFrameArgs;
68 using blink::WebFloatPoint;
69 using blink::WebRect;
70 using blink::WebSelection;
71 using blink::WebSize;
72 using blink::WebTopControlsState;
74 namespace content {
75 namespace {
77 bool GetSwitchValueAsInt(const base::CommandLine& command_line,
78 const std::string& switch_string,
79 int min_value,
80 int max_value,
81 int* result) {
82 std::string string_value = command_line.GetSwitchValueASCII(switch_string);
83 int int_value;
84 if (base::StringToInt(string_value, &int_value) &&
85 int_value >= min_value && int_value <= max_value) {
86 *result = int_value;
87 return true;
88 } else {
89 LOG(WARNING) << "Failed to parse switch " << switch_string << ": " <<
90 string_value;
91 return false;
95 cc::LayerSelectionBound ConvertWebSelectionBound(
96 const WebSelection& web_selection,
97 bool is_start) {
98 cc::LayerSelectionBound cc_bound;
99 if (web_selection.isNone())
100 return cc_bound;
102 const blink::WebSelectionBound& web_bound =
103 is_start ? web_selection.start() : web_selection.end();
104 DCHECK(web_bound.layerId);
105 cc_bound.type = cc::SELECTION_BOUND_CENTER;
106 if (web_selection.isRange()) {
107 if (is_start) {
108 cc_bound.type = web_bound.isTextDirectionRTL ? cc::SELECTION_BOUND_RIGHT
109 : cc::SELECTION_BOUND_LEFT;
110 } else {
111 cc_bound.type = web_bound.isTextDirectionRTL ? cc::SELECTION_BOUND_LEFT
112 : cc::SELECTION_BOUND_RIGHT;
115 cc_bound.layer_id = web_bound.layerId;
116 cc_bound.edge_top = gfx::Point(web_bound.edgeTopInLayer);
117 cc_bound.edge_bottom = gfx::Point(web_bound.edgeBottomInLayer);
118 return cc_bound;
121 cc::LayerSelection ConvertWebSelection(const WebSelection& web_selection) {
122 cc::LayerSelection cc_selection;
123 cc_selection.start = ConvertWebSelectionBound(web_selection, true);
124 cc_selection.end = ConvertWebSelectionBound(web_selection, false);
125 cc_selection.is_editable = web_selection.isEditable();
126 cc_selection.is_empty_text_form_control =
127 web_selection.isEmptyTextFormControl();
128 return cc_selection;
131 gfx::Size CalculateDefaultTileSize(RenderWidget* widget) {
132 int default_tile_size = 256;
133 #if defined(OS_ANDROID)
134 // TODO(epenner): unify this for all platforms if it
135 // makes sense (http://crbug.com/159524)
137 gfx::DeviceDisplayInfo info;
138 bool real_size_supported = true;
139 int display_width = info.GetPhysicalDisplayWidth();
140 int display_height = info.GetPhysicalDisplayHeight();
141 if (display_width == 0 || display_height == 0) {
142 real_size_supported = false;
143 display_width = info.GetDisplayWidth();
144 display_height = info.GetDisplayHeight();
147 int portrait_width = std::min(display_width, display_height);
148 int landscape_width = std::max(display_width, display_height);
150 if (real_size_supported) {
151 // Maximum HD dimensions should be 768x1280
152 // Maximum FHD dimensions should be 1200x1920
153 if (portrait_width > 768 || landscape_width > 1280)
154 default_tile_size = 384;
155 if (portrait_width > 1200 || landscape_width > 1920)
156 default_tile_size = 512;
158 // Adjust for some resolutions that barely straddle an extra
159 // tile when in portrait mode. This helps worst case scroll/raster
160 // by not needing a full extra tile for each row.
161 if (default_tile_size == 256 && portrait_width == 768)
162 default_tile_size += 32;
163 if (default_tile_size == 384 && portrait_width == 1200)
164 default_tile_size += 32;
165 } else {
166 // We don't know the exact resolution due to screen controls etc.
167 // So this just estimates the values above using tile counts.
168 int numTiles = (display_width * display_height) / (256 * 256);
169 if (numTiles > 16)
170 default_tile_size = 384;
171 if (numTiles >= 40)
172 default_tile_size = 512;
174 #elif defined(OS_CHROMEOS)
175 // Use 512 for high DPI (dsf=2.0f) devices.
176 if (widget->screen_info().deviceScaleFactor >= 2.0f)
177 default_tile_size = 512;
178 #endif
180 return gfx::Size(default_tile_size, default_tile_size);
183 // Check cc::TopControlsState, and blink::WebTopControlsState
184 // are kept in sync.
185 static_assert(int(blink::WebTopControlsBoth) == int(cc::BOTH),
186 "mismatching enums: BOTH");
187 static_assert(int(blink::WebTopControlsHidden) == int(cc::HIDDEN),
188 "mismatching enums: HIDDEN");
189 static_assert(int(blink::WebTopControlsShown) == int(cc::SHOWN),
190 "mismatching enums: SHOWN");
192 static cc::TopControlsState ConvertTopControlsState(
193 WebTopControlsState state) {
194 return static_cast<cc::TopControlsState>(state);
197 } // namespace
199 // static
200 scoped_ptr<RenderWidgetCompositor> RenderWidgetCompositor::Create(
201 RenderWidget* widget,
202 CompositorDependencies* compositor_deps) {
203 scoped_ptr<RenderWidgetCompositor> compositor(
204 new RenderWidgetCompositor(widget, compositor_deps));
205 compositor->Initialize();
206 return compositor;
209 RenderWidgetCompositor::RenderWidgetCompositor(
210 RenderWidget* widget,
211 CompositorDependencies* compositor_deps)
212 : num_failed_recreate_attempts_(0),
213 widget_(widget),
214 compositor_deps_(compositor_deps),
215 layout_and_paint_async_callback_(nullptr),
216 weak_factory_(this) {
219 void RenderWidgetCompositor::Initialize() {
220 base::CommandLine* cmd = base::CommandLine::ForCurrentProcess();
222 cc::LayerTreeSettings settings;
224 // For web contents, layer transforms should scale up the contents of layers
225 // to keep content always crisp when possible.
226 settings.layer_transforms_should_scale_layer_contents = true;
228 settings.renderer_settings.disable_gpu_vsync =
229 cmd->HasSwitch(switches::kDisableGpuVsync);
230 settings.main_frame_before_activation_enabled =
231 cmd->HasSwitch(cc::switches::kEnableMainFrameBeforeActivation) &&
232 !cmd->HasSwitch(cc::switches::kDisableMainFrameBeforeActivation);
233 settings.report_overscroll_only_for_scrollable_axes =
234 !compositor_deps_->IsElasticOverscrollEnabled();
235 settings.accelerated_animation_enabled =
236 !cmd->HasSwitch(cc::switches::kDisableThreadedAnimation);
237 const std::string slimming_group =
238 base::FieldTrialList::FindFullName("SlimmingPaint");
239 settings.use_display_lists =
240 (cmd->HasSwitch(switches::kEnableSlimmingPaint) ||
241 !cmd->HasSwitch(switches::kDisableSlimmingPaint)) &&
242 (slimming_group != "DisableSlimmingPaint");
243 if (cmd->HasSwitch(switches::kEnableCompositorAnimationTimelines)) {
244 settings.use_compositor_animation_timelines = true;
245 blink::WebRuntimeFeatures::enableCompositorAnimationTimelines(true);
248 settings.default_tile_size = CalculateDefaultTileSize(widget_);
249 if (cmd->HasSwitch(switches::kDefaultTileWidth)) {
250 int tile_width = 0;
251 GetSwitchValueAsInt(*cmd,
252 switches::kDefaultTileWidth,
254 std::numeric_limits<int>::max(),
255 &tile_width);
256 settings.default_tile_size.set_width(tile_width);
258 if (cmd->HasSwitch(switches::kDefaultTileHeight)) {
259 int tile_height = 0;
260 GetSwitchValueAsInt(*cmd,
261 switches::kDefaultTileHeight,
263 std::numeric_limits<int>::max(),
264 &tile_height);
265 settings.default_tile_size.set_height(tile_height);
268 int max_untiled_layer_width = settings.max_untiled_layer_size.width();
269 if (cmd->HasSwitch(switches::kMaxUntiledLayerWidth)) {
270 GetSwitchValueAsInt(*cmd, switches::kMaxUntiledLayerWidth, 1,
271 std::numeric_limits<int>::max(),
272 &max_untiled_layer_width);
274 int max_untiled_layer_height = settings.max_untiled_layer_size.height();
275 if (cmd->HasSwitch(switches::kMaxUntiledLayerHeight)) {
276 GetSwitchValueAsInt(*cmd, switches::kMaxUntiledLayerHeight, 1,
277 std::numeric_limits<int>::max(),
278 &max_untiled_layer_height);
281 settings.max_untiled_layer_size = gfx::Size(max_untiled_layer_width,
282 max_untiled_layer_height);
284 settings.gpu_rasterization_msaa_sample_count =
285 compositor_deps_->GetGpuRasterizationMSAASampleCount();
286 settings.gpu_rasterization_forced =
287 compositor_deps_->IsGpuRasterizationForced();
288 settings.gpu_rasterization_enabled =
289 compositor_deps_->IsGpuRasterizationEnabled();
291 settings.can_use_lcd_text = compositor_deps_->IsLcdTextEnabled();
292 settings.use_distance_field_text =
293 compositor_deps_->IsDistanceFieldTextEnabled();
294 settings.use_zero_copy = compositor_deps_->IsZeroCopyEnabled();
295 settings.use_one_copy = compositor_deps_->IsOneCopyEnabled();
296 settings.enable_elastic_overscroll =
297 compositor_deps_->IsElasticOverscrollEnabled();
298 settings.use_image_texture_target = compositor_deps_->GetImageTextureTarget();
299 settings.gather_pixel_refs = compositor_deps_->IsGatherPixelRefsEnabled();
301 if (cmd->HasSwitch(cc::switches::kTopControlsShowThreshold)) {
302 std::string top_threshold_str =
303 cmd->GetSwitchValueASCII(cc::switches::kTopControlsShowThreshold);
304 double show_threshold;
305 if (base::StringToDouble(top_threshold_str, &show_threshold) &&
306 show_threshold >= 0.f && show_threshold <= 1.f)
307 settings.top_controls_show_threshold = show_threshold;
310 if (cmd->HasSwitch(cc::switches::kTopControlsHideThreshold)) {
311 std::string top_threshold_str =
312 cmd->GetSwitchValueASCII(cc::switches::kTopControlsHideThreshold);
313 double hide_threshold;
314 if (base::StringToDouble(top_threshold_str, &hide_threshold) &&
315 hide_threshold >= 0.f && hide_threshold <= 1.f)
316 settings.top_controls_hide_threshold = hide_threshold;
319 settings.verify_property_trees =
320 cmd->HasSwitch(cc::switches::kEnablePropertyTreeVerification);
321 settings.renderer_settings.allow_antialiasing &=
322 !cmd->HasSwitch(cc::switches::kDisableCompositedAntialiasing);
323 // The means the renderer compositor has 2 possible modes:
324 // - Threaded compositing with a scheduler.
325 // - Single threaded compositing without a scheduler (for layout tests only).
326 // Using the scheduler in layout tests introduces additional composite steps
327 // that create flakiness.
328 settings.single_thread_proxy_scheduler = false;
330 // These flags should be mirrored by UI versions in ui/compositor/.
331 settings.initial_debug_state.show_debug_borders =
332 cmd->HasSwitch(cc::switches::kShowCompositedLayerBorders);
333 settings.initial_debug_state.show_fps_counter =
334 cmd->HasSwitch(cc::switches::kShowFPSCounter);
335 settings.initial_debug_state.show_layer_animation_bounds_rects =
336 cmd->HasSwitch(cc::switches::kShowLayerAnimationBounds);
337 settings.initial_debug_state.show_paint_rects =
338 cmd->HasSwitch(switches::kShowPaintRects);
339 settings.initial_debug_state.show_property_changed_rects =
340 cmd->HasSwitch(cc::switches::kShowPropertyChangedRects);
341 settings.initial_debug_state.show_surface_damage_rects =
342 cmd->HasSwitch(cc::switches::kShowSurfaceDamageRects);
343 settings.initial_debug_state.show_screen_space_rects =
344 cmd->HasSwitch(cc::switches::kShowScreenSpaceRects);
345 settings.initial_debug_state.show_replica_screen_space_rects =
346 cmd->HasSwitch(cc::switches::kShowReplicaScreenSpaceRects);
348 settings.initial_debug_state.SetRecordRenderingStats(
349 cmd->HasSwitch(cc::switches::kEnableGpuBenchmarking));
351 if (cmd->HasSwitch(cc::switches::kSlowDownRasterScaleFactor)) {
352 const int kMinSlowDownScaleFactor = 0;
353 const int kMaxSlowDownScaleFactor = INT_MAX;
354 GetSwitchValueAsInt(
355 *cmd,
356 cc::switches::kSlowDownRasterScaleFactor,
357 kMinSlowDownScaleFactor,
358 kMaxSlowDownScaleFactor,
359 &settings.initial_debug_state.slow_down_raster_scale_factor);
362 settings.invert_viewport_scroll_order =
363 cmd->HasSwitch(switches::kInvertViewportScrollOrder);
365 if (cmd->HasSwitch(cc::switches::kMaxTilesForInterestArea)) {
366 int max_tiles_for_interest_area;
367 if (GetSwitchValueAsInt(*cmd,
368 cc::switches::kMaxTilesForInterestArea,
369 1, std::numeric_limits<int>::max(),
370 &max_tiles_for_interest_area))
371 settings.max_tiles_for_interest_area = max_tiles_for_interest_area;
374 if (cmd->HasSwitch(cc::switches::kMaxUnusedResourceMemoryUsagePercentage)) {
375 int max_unused_resource_memory_percentage;
376 if (GetSwitchValueAsInt(
377 *cmd,
378 cc::switches::kMaxUnusedResourceMemoryUsagePercentage,
379 0, 100,
380 &max_unused_resource_memory_percentage)) {
381 settings.max_unused_resource_memory_percentage =
382 max_unused_resource_memory_percentage;
386 settings.strict_layer_property_change_checking =
387 cmd->HasSwitch(cc::switches::kStrictLayerPropertyChangeChecking);
389 #if defined(OS_ANDROID)
390 SynchronousCompositorFactory* synchronous_compositor_factory =
391 SynchronousCompositorFactory::GetInstance();
393 // We can't use GPU rasterization on low-end devices, because the Ganesh
394 // cache would consume too much memory.
395 if (base::SysInfo::IsLowEndDevice())
396 settings.gpu_rasterization_enabled = false;
397 settings.using_synchronous_renderer_compositor =
398 synchronous_compositor_factory;
399 settings.record_full_layer = widget_->DoesRecordFullLayer();
400 settings.report_overscroll_only_for_scrollable_axes =
401 !synchronous_compositor_factory;
402 settings.max_partial_texture_updates = 0;
403 if (synchronous_compositor_factory) {
404 // Android WebView uses system scrollbars, so make ours invisible.
405 settings.scrollbar_animator = cc::LayerTreeSettings::NO_ANIMATOR;
406 settings.solid_color_scrollbar_color = SK_ColorTRANSPARENT;
407 } else {
408 settings.scrollbar_animator = cc::LayerTreeSettings::LINEAR_FADE;
409 settings.scrollbar_fade_delay_ms = 300;
410 settings.scrollbar_fade_resize_delay_ms = 2000;
411 settings.scrollbar_fade_duration_ms = 300;
412 settings.solid_color_scrollbar_color = SkColorSetARGB(128, 128, 128, 128);
414 settings.renderer_settings.highp_threshold_min = 2048;
415 // Android WebView handles root layer flings itself.
416 settings.ignore_root_layer_flings =
417 synchronous_compositor_factory;
418 // Memory policy on Android WebView does not depend on whether device is
419 // low end, so always use default policy.
420 bool use_low_memory_policy =
421 base::SysInfo::IsLowEndDevice() && !synchronous_compositor_factory;
422 // RGBA_4444 textures are only enabled for low end devices
423 // and are disabled for Android WebView as it doesn't support the format.
424 settings.renderer_settings.use_rgba_4444_textures = use_low_memory_policy;
425 if (use_low_memory_policy) {
426 // On low-end we want to be very carefull about killing other
427 // apps. So initially we use 50% more memory to avoid flickering
428 // or raster-on-demand.
429 settings.max_memory_for_prepaint_percentage = 67;
430 } else {
431 // On other devices we have increased memory excessively to avoid
432 // raster-on-demand already, so now we reserve 50% _only_ to avoid
433 // raster-on-demand, and use 50% of the memory otherwise.
434 settings.max_memory_for_prepaint_percentage = 50;
436 // Webview does not own the surface so should not clear it.
437 settings.renderer_settings.should_clear_root_render_pass =
438 !synchronous_compositor_factory;
440 // TODO(danakj): Only do this on low end devices.
441 settings.create_low_res_tiling = true;
443 settings.use_external_begin_frame_source = true;
445 #elif !defined(OS_MACOSX)
446 if (ui::IsOverlayScrollbarEnabled()) {
447 settings.scrollbar_animator = cc::LayerTreeSettings::THINNING;
448 settings.solid_color_scrollbar_color = SkColorSetARGB(128, 128, 128, 128);
449 } else {
450 settings.scrollbar_animator = cc::LayerTreeSettings::LINEAR_FADE;
451 settings.solid_color_scrollbar_color = SkColorSetARGB(128, 128, 128, 128);
453 settings.scrollbar_fade_delay_ms = 500;
454 settings.scrollbar_fade_resize_delay_ms = 500;
455 settings.scrollbar_fade_duration_ms = 300;
457 // When pinching in, only show the pinch-viewport overlay scrollbars if the
458 // page scale is at least some threshold away from the minimum. i.e. don't
459 // show the pinch scrollbars when at minimum scale.
460 settings.scrollbar_show_scale_threshold = 1.05f;
461 #endif
463 if (cmd->HasSwitch(switches::kEnableLowResTiling))
464 settings.create_low_res_tiling = true;
465 if (cmd->HasSwitch(switches::kDisableLowResTiling))
466 settings.create_low_res_tiling = false;
467 if (cmd->HasSwitch(cc::switches::kEnableBeginFrameScheduling))
468 settings.use_external_begin_frame_source = true;
470 if (widget_->for_oopif()) {
471 // TODO(simonhong): Apply BeginFrame scheduling for OOPIF.
472 // See crbug.com/471411.
473 settings.use_external_begin_frame_source = false;
476 scoped_refptr<base::SingleThreadTaskRunner> compositor_thread_task_runner =
477 compositor_deps_->GetCompositorImplThreadTaskRunner();
478 scoped_refptr<base::SingleThreadTaskRunner>
479 main_thread_compositor_task_runner =
480 compositor_deps_->GetCompositorMainThreadTaskRunner();
481 cc::SharedBitmapManager* shared_bitmap_manager =
482 compositor_deps_->GetSharedBitmapManager();
483 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager =
484 compositor_deps_->GetGpuMemoryBufferManager();
485 cc::TaskGraphRunner* task_graph_runner =
486 compositor_deps_->GetTaskGraphRunner();
488 scoped_ptr<cc::BeginFrameSource> external_begin_frame_source;
489 if (settings.use_external_begin_frame_source) {
490 external_begin_frame_source =
491 compositor_deps_->CreateExternalBeginFrameSource(widget_->routing_id());
494 cc::LayerTreeHost::InitParams params;
495 params.client = this;
496 params.shared_bitmap_manager = shared_bitmap_manager;
497 params.gpu_memory_buffer_manager = gpu_memory_buffer_manager;
498 params.settings = &settings;
499 params.task_graph_runner = task_graph_runner;
500 params.main_task_runner = main_thread_compositor_task_runner;
501 params.external_begin_frame_source = external_begin_frame_source.Pass();
502 if (compositor_thread_task_runner.get()) {
503 layer_tree_host_ = cc::LayerTreeHost::CreateThreaded(
504 compositor_thread_task_runner, &params);
505 } else {
506 layer_tree_host_ = cc::LayerTreeHost::CreateSingleThreaded(this, &params);
508 DCHECK(layer_tree_host_);
511 RenderWidgetCompositor::~RenderWidgetCompositor() {}
513 const base::WeakPtr<cc::InputHandler>&
514 RenderWidgetCompositor::GetInputHandler() {
515 return layer_tree_host_->GetInputHandler();
518 bool RenderWidgetCompositor::BeginMainFrameRequested() const {
519 return layer_tree_host_->BeginMainFrameRequested();
522 void RenderWidgetCompositor::SetNeedsDisplayOnAllLayers() {
523 layer_tree_host_->SetNeedsDisplayOnAllLayers();
526 void RenderWidgetCompositor::SetRasterizeOnlyVisibleContent() {
527 cc::LayerTreeDebugState current = layer_tree_host_->debug_state();
528 current.rasterize_only_visible_content = true;
529 layer_tree_host_->SetDebugState(current);
532 void RenderWidgetCompositor::SetNeedsRedrawRect(gfx::Rect damage_rect) {
533 layer_tree_host_->SetNeedsRedrawRect(damage_rect);
536 void RenderWidgetCompositor::SetNeedsForcedRedraw() {
537 layer_tree_host_->SetNextCommitForcesRedraw();
538 setNeedsAnimate();
541 scoped_ptr<cc::SwapPromiseMonitor>
542 RenderWidgetCompositor::CreateLatencyInfoSwapPromiseMonitor(
543 ui::LatencyInfo* latency) {
544 return scoped_ptr<cc::SwapPromiseMonitor>(
545 new cc::LatencyInfoSwapPromiseMonitor(
546 latency, layer_tree_host_.get(), NULL));
549 void RenderWidgetCompositor::QueueSwapPromise(
550 scoped_ptr<cc::SwapPromise> swap_promise) {
551 layer_tree_host_->QueueSwapPromise(swap_promise.Pass());
554 int RenderWidgetCompositor::GetSourceFrameNumber() const {
555 return layer_tree_host_->source_frame_number();
558 void RenderWidgetCompositor::SetNeedsUpdateLayers() {
559 layer_tree_host_->SetNeedsUpdateLayers();
562 void RenderWidgetCompositor::SetNeedsCommit() {
563 layer_tree_host_->SetNeedsCommit();
566 void RenderWidgetCompositor::NotifyInputThrottledUntilCommit() {
567 layer_tree_host_->NotifyInputThrottledUntilCommit();
570 const cc::Layer* RenderWidgetCompositor::GetRootLayer() const {
571 return layer_tree_host_->root_layer();
574 int RenderWidgetCompositor::ScheduleMicroBenchmark(
575 const std::string& name,
576 scoped_ptr<base::Value> value,
577 const base::Callback<void(scoped_ptr<base::Value>)>& callback) {
578 return layer_tree_host_->ScheduleMicroBenchmark(name, value.Pass(), callback);
581 bool RenderWidgetCompositor::SendMessageToMicroBenchmark(
582 int id,
583 scoped_ptr<base::Value> value) {
584 return layer_tree_host_->SendMessageToMicroBenchmark(id, value.Pass());
587 void RenderWidgetCompositor::StartCompositor() {
588 layer_tree_host_->SetLayerTreeHostClientReady();
591 void RenderWidgetCompositor::setRootLayer(const blink::WebLayer& layer) {
592 layer_tree_host_->SetRootLayer(
593 static_cast<const cc_blink::WebLayerImpl*>(&layer)->layer());
596 void RenderWidgetCompositor::clearRootLayer() {
597 layer_tree_host_->SetRootLayer(scoped_refptr<cc::Layer>());
600 void RenderWidgetCompositor::setViewportSize(
601 const WebSize&,
602 const WebSize& device_viewport_size) {
603 layer_tree_host_->SetViewportSize(device_viewport_size);
606 void RenderWidgetCompositor::setViewportSize(
607 const WebSize& device_viewport_size) {
608 layer_tree_host_->SetViewportSize(device_viewport_size);
611 WebSize RenderWidgetCompositor::layoutViewportSize() const {
612 return layer_tree_host_->device_viewport_size();
615 WebSize RenderWidgetCompositor::deviceViewportSize() const {
616 return layer_tree_host_->device_viewport_size();
619 WebFloatPoint RenderWidgetCompositor::adjustEventPointForPinchZoom(
620 const WebFloatPoint& point) const {
621 return point;
624 void RenderWidgetCompositor::setDeviceScaleFactor(float device_scale) {
625 layer_tree_host_->SetDeviceScaleFactor(device_scale);
628 float RenderWidgetCompositor::deviceScaleFactor() const {
629 return layer_tree_host_->device_scale_factor();
632 void RenderWidgetCompositor::setBackgroundColor(blink::WebColor color) {
633 layer_tree_host_->set_background_color(color);
636 void RenderWidgetCompositor::setHasTransparentBackground(bool transparent) {
637 layer_tree_host_->set_has_transparent_background(transparent);
640 void RenderWidgetCompositor::setVisible(bool visible) {
641 layer_tree_host_->SetVisible(visible);
644 void RenderWidgetCompositor::setPageScaleFactorAndLimits(
645 float page_scale_factor, float minimum, float maximum) {
646 layer_tree_host_->SetPageScaleFactorAndLimits(
647 page_scale_factor, minimum, maximum);
650 void RenderWidgetCompositor::startPageScaleAnimation(
651 const blink::WebPoint& destination,
652 bool use_anchor,
653 float new_page_scale,
654 double duration_sec) {
655 base::TimeDelta duration = base::TimeDelta::FromMicroseconds(
656 duration_sec * base::Time::kMicrosecondsPerSecond);
657 layer_tree_host_->StartPageScaleAnimation(
658 gfx::Vector2d(destination.x, destination.y),
659 use_anchor,
660 new_page_scale,
661 duration);
664 void RenderWidgetCompositor::heuristicsForGpuRasterizationUpdated(
665 bool matches_heuristics) {
666 layer_tree_host_->SetHasGpuRasterizationTrigger(matches_heuristics);
669 void RenderWidgetCompositor::setNeedsAnimate() {
670 layer_tree_host_->SetNeedsAnimate();
673 bool RenderWidgetCompositor::commitRequested() const {
674 return layer_tree_host_->CommitRequested();
677 void RenderWidgetCompositor::didStopFlinging() {
678 layer_tree_host_->DidStopFlinging();
681 void RenderWidgetCompositor::registerForAnimations(blink::WebLayer* layer) {
682 cc::Layer* cc_layer = static_cast<cc_blink::WebLayerImpl*>(layer)->layer();
683 cc_layer->RegisterForAnimations(layer_tree_host_->animation_registrar());
686 void RenderWidgetCompositor::registerViewportLayers(
687 const blink::WebLayer* overscrollElasticityLayer,
688 const blink::WebLayer* pageScaleLayer,
689 const blink::WebLayer* innerViewportScrollLayer,
690 const blink::WebLayer* outerViewportScrollLayer) {
691 layer_tree_host_->RegisterViewportLayers(
692 // TODO(bokan): This check can probably be removed now, but it looks
693 // like overscroll elasticity may still be NULL until PinchViewport
694 // registers its layers.
695 // The scroll elasticity layer will only exist when using pinch virtual
696 // viewports.
697 overscrollElasticityLayer
698 ? static_cast<const cc_blink::WebLayerImpl*>(
699 overscrollElasticityLayer)->layer()
700 : NULL,
701 static_cast<const cc_blink::WebLayerImpl*>(pageScaleLayer)->layer(),
702 static_cast<const cc_blink::WebLayerImpl*>(innerViewportScrollLayer)
703 ->layer(),
704 // TODO(bokan): This check can probably be removed now, but it looks
705 // like overscroll elasticity may still be NULL until PinchViewport
706 // registers its layers.
707 // The outer viewport layer will only exist when using pinch virtual
708 // viewports.
709 outerViewportScrollLayer
710 ? static_cast<const cc_blink::WebLayerImpl*>(outerViewportScrollLayer)
711 ->layer()
712 : NULL);
715 void RenderWidgetCompositor::clearViewportLayers() {
716 layer_tree_host_->RegisterViewportLayers(
717 scoped_refptr<cc::Layer>(), scoped_refptr<cc::Layer>(),
718 scoped_refptr<cc::Layer>(), scoped_refptr<cc::Layer>());
721 void RenderWidgetCompositor::registerSelection(
722 const blink::WebSelection& selection) {
723 layer_tree_host_->RegisterSelection(ConvertWebSelection(selection));
726 void RenderWidgetCompositor::clearSelection() {
727 cc::LayerSelection empty_selection;
728 layer_tree_host_->RegisterSelection(empty_selection);
731 void CompositeAndReadbackAsyncCallback(
732 blink::WebCompositeAndReadbackAsyncCallback* callback,
733 scoped_ptr<cc::CopyOutputResult> result) {
734 if (result->HasBitmap()) {
735 scoped_ptr<SkBitmap> result_bitmap = result->TakeBitmap();
736 callback->didCompositeAndReadback(*result_bitmap);
737 } else {
738 callback->didCompositeAndReadback(SkBitmap());
742 void RenderWidgetCompositor::layoutAndPaintAsync(
743 blink::WebLayoutAndPaintAsyncCallback* callback) {
744 DCHECK(!temporary_copy_output_request_ && !layout_and_paint_async_callback_);
745 layout_and_paint_async_callback_ = callback;
746 ScheduleCommit();
749 void RenderWidgetCompositor::compositeAndReadbackAsync(
750 blink::WebCompositeAndReadbackAsyncCallback* callback) {
751 DCHECK(!temporary_copy_output_request_ && !layout_and_paint_async_callback_);
752 temporary_copy_output_request_ =
753 cc::CopyOutputRequest::CreateBitmapRequest(
754 base::Bind(&CompositeAndReadbackAsyncCallback, callback));
755 // Force a commit to happen. The temporary copy output request will
756 // be installed after layout which will happen as a part of the commit, for
757 // widgets that delay the creation of their output surface.
758 ScheduleCommit();
761 bool RenderWidgetCompositor::CommitIsSynchronous() const {
762 return !compositor_deps_->GetCompositorImplThreadTaskRunner().get() &&
763 !layer_tree_host_->settings().single_thread_proxy_scheduler;
766 void RenderWidgetCompositor::ScheduleCommit() {
767 if (CommitIsSynchronous()) {
768 base::ThreadTaskRunnerHandle::Get()->PostTask(
769 FROM_HERE, base::Bind(&RenderWidgetCompositor::SynchronousCommit,
770 weak_factory_.GetWeakPtr()));
771 } else {
772 layer_tree_host_->SetNeedsCommit();
776 void RenderWidgetCompositor::SynchronousCommit() {
777 DCHECK(CommitIsSynchronous());
778 layer_tree_host_->Composite(base::TimeTicks::Now());
781 void RenderWidgetCompositor::finishAllRendering() {
782 layer_tree_host_->FinishAllRendering();
785 void RenderWidgetCompositor::setDeferCommits(bool defer_commits) {
786 layer_tree_host_->SetDeferCommits(defer_commits);
789 int RenderWidgetCompositor::layerTreeId() const {
790 return layer_tree_host_->id();
793 void RenderWidgetCompositor::setShowFPSCounter(bool show) {
794 cc::LayerTreeDebugState debug_state = layer_tree_host_->debug_state();
795 debug_state.show_fps_counter = show;
796 layer_tree_host_->SetDebugState(debug_state);
799 void RenderWidgetCompositor::setShowPaintRects(bool show) {
800 cc::LayerTreeDebugState debug_state = layer_tree_host_->debug_state();
801 debug_state.show_paint_rects = show;
802 layer_tree_host_->SetDebugState(debug_state);
805 void RenderWidgetCompositor::setShowDebugBorders(bool show) {
806 cc::LayerTreeDebugState debug_state = layer_tree_host_->debug_state();
807 debug_state.show_debug_borders = show;
808 layer_tree_host_->SetDebugState(debug_state);
811 void RenderWidgetCompositor::setContinuousPaintingEnabled(bool enabled) {
812 cc::LayerTreeDebugState debug_state = layer_tree_host_->debug_state();
813 debug_state.continuous_painting = enabled;
814 layer_tree_host_->SetDebugState(debug_state);
817 void RenderWidgetCompositor::setShowScrollBottleneckRects(bool show) {
818 cc::LayerTreeDebugState debug_state = layer_tree_host_->debug_state();
819 debug_state.show_touch_event_handler_rects = show;
820 debug_state.show_wheel_event_handler_rects = show;
821 debug_state.show_non_fast_scrollable_rects = show;
822 layer_tree_host_->SetDebugState(debug_state);
825 void RenderWidgetCompositor::updateTopControlsState(
826 WebTopControlsState constraints,
827 WebTopControlsState current,
828 bool animate) {
829 layer_tree_host_->UpdateTopControlsState(ConvertTopControlsState(constraints),
830 ConvertTopControlsState(current),
831 animate);
834 void RenderWidgetCompositor::setTopControlsHeight(float height, bool shrink) {
835 layer_tree_host_->SetTopControlsHeight(height, shrink);
838 void RenderWidgetCompositor::setTopControlsShownRatio(float ratio) {
839 layer_tree_host_->SetTopControlsShownRatio(ratio);
842 void RenderWidgetCompositor::WillBeginMainFrame() {
843 widget_->WillBeginCompositorFrame();
846 void RenderWidgetCompositor::DidBeginMainFrame() {
849 void RenderWidgetCompositor::BeginMainFrame(const cc::BeginFrameArgs& args) {
850 double frame_time_sec = (args.frame_time - base::TimeTicks()).InSecondsF();
851 double deadline_sec = (args.deadline - base::TimeTicks()).InSecondsF();
852 double interval_sec = args.interval.InSecondsF();
853 WebBeginFrameArgs web_begin_frame_args =
854 WebBeginFrameArgs(frame_time_sec, deadline_sec, interval_sec);
855 compositor_deps_->GetRendererScheduler()->WillBeginFrame(args);
856 widget_->webwidget()->beginFrame(web_begin_frame_args);
859 void RenderWidgetCompositor::BeginMainFrameNotExpectedSoon() {
860 compositor_deps_->GetRendererScheduler()->BeginFrameNotExpectedSoon();
863 void RenderWidgetCompositor::Layout() {
864 widget_->webwidget()->layout();
866 if (temporary_copy_output_request_) {
867 // For WebViewImpl, this will always have a root layer. For other widgets,
868 // the widget may be closed before servicing this request, so ignore it.
869 if (cc::Layer* root_layer = layer_tree_host_->root_layer()) {
870 root_layer->RequestCopyOfOutput(temporary_copy_output_request_.Pass());
871 } else {
872 temporary_copy_output_request_->SendEmptyResult();
873 temporary_copy_output_request_ = nullptr;
878 void RenderWidgetCompositor::ApplyViewportDeltas(
879 const gfx::Vector2dF& inner_delta,
880 const gfx::Vector2dF& outer_delta,
881 const gfx::Vector2dF& elastic_overscroll_delta,
882 float page_scale,
883 float top_controls_delta) {
884 widget_->webwidget()->applyViewportDeltas(
885 inner_delta,
886 outer_delta,
887 elastic_overscroll_delta,
888 page_scale,
889 top_controls_delta);
892 void RenderWidgetCompositor::RequestNewOutputSurface() {
893 // If the host is closing, then no more compositing is possible. This
894 // prevents shutdown races between handling the close message and
895 // the CreateOutputSurface task.
896 if (widget_->host_closing())
897 return;
899 // TODO(robliao): Remove ScopedTracker below once https://crbug.com/466870
900 // is fixed.
901 tracked_objects::ScopedTracker tracking_profile(
902 FROM_HERE_WITH_EXPLICIT_FUNCTION(
903 "466870 RenderWidgetCompositor::RequestNewOutputSurface"));
905 bool fallback =
906 num_failed_recreate_attempts_ >= OUTPUT_SURFACE_RETRIES_BEFORE_FALLBACK;
907 scoped_ptr<cc::OutputSurface> surface(widget_->CreateOutputSurface(fallback));
909 if (!surface) {
910 DidFailToInitializeOutputSurface();
911 return;
914 DCHECK_EQ(surface->capabilities().max_frames_pending, 1);
916 layer_tree_host_->SetOutputSurface(surface.Pass());
919 void RenderWidgetCompositor::DidInitializeOutputSurface() {
920 num_failed_recreate_attempts_ = 0;
923 void RenderWidgetCompositor::DidFailToInitializeOutputSurface() {
924 ++num_failed_recreate_attempts_;
925 // Tolerate a certain number of recreation failures to work around races
926 // in the output-surface-lost machinery.
927 LOG_IF(FATAL, (num_failed_recreate_attempts_ >= MAX_OUTPUT_SURFACE_RETRIES))
928 << "Failed to create a fallback OutputSurface.";
930 base::ThreadTaskRunnerHandle::Get()->PostTask(
931 FROM_HERE, base::Bind(&RenderWidgetCompositor::RequestNewOutputSurface,
932 weak_factory_.GetWeakPtr()));
935 void RenderWidgetCompositor::WillCommit() {
936 if (!layout_and_paint_async_callback_)
937 return;
938 layout_and_paint_async_callback_->didLayoutAndPaint();
939 layout_and_paint_async_callback_ = nullptr;
942 void RenderWidgetCompositor::DidCommit() {
943 DCHECK(!temporary_copy_output_request_);
944 widget_->DidCommitCompositorFrame();
945 compositor_deps_->GetRendererScheduler()->DidCommitFrameToCompositor();
948 void RenderWidgetCompositor::DidCommitAndDrawFrame() {
949 widget_->DidCommitAndDrawCompositorFrame();
952 void RenderWidgetCompositor::DidCompleteSwapBuffers() {
953 widget_->DidCompleteSwapBuffers();
954 bool threaded = !!compositor_deps_->GetCompositorImplThreadTaskRunner().get();
955 if (!threaded)
956 widget_->OnSwapBuffersComplete();
959 void RenderWidgetCompositor::DidCompletePageScaleAnimation() {
960 widget_->DidCompletePageScaleAnimation();
963 void RenderWidgetCompositor::ScheduleAnimation() {
964 widget_->scheduleAnimation();
967 void RenderWidgetCompositor::DidPostSwapBuffers() {
968 widget_->OnSwapBuffersPosted();
971 void RenderWidgetCompositor::DidAbortSwapBuffers() {
972 widget_->OnSwapBuffersAborted();
975 void RenderWidgetCompositor::RecordFrameTimingEvents(
976 scoped_ptr<cc::FrameTimingTracker::CompositeTimingSet> composite_events,
977 scoped_ptr<cc::FrameTimingTracker::MainFrameTimingSet> main_frame_events) {
978 for (const auto& composite_event : *composite_events ) {
979 int64_t frameId = composite_event.first;
980 const std::vector<cc::FrameTimingTracker::CompositeTimingEvent>& events =
981 composite_event.second;
982 std::vector<blink::WebFrameTimingEvent> webEvents;
983 for (size_t i = 0; i < events.size(); ++i) {
984 webEvents.push_back(blink::WebFrameTimingEvent(
985 events[i].frame_id,
986 (events[i].timestamp - base::TimeTicks()).InSecondsF()));
988 widget_->webwidget()->recordFrameTimingEvent(
989 blink::WebWidget::CompositeEvent, frameId, webEvents);
991 for (const auto& main_frame_event : *main_frame_events ) {
992 int64_t frameId = main_frame_event.first;
993 const std::vector<cc::FrameTimingTracker::MainFrameTimingEvent>& events =
994 main_frame_event.second;
995 std::vector<blink::WebFrameTimingEvent> webEvents;
996 for (size_t i = 0; i < events.size(); ++i) {
997 webEvents.push_back(blink::WebFrameTimingEvent(
998 events[i].frame_id,
999 (events[i].timestamp - base::TimeTicks()).InSecondsF(),
1000 (events[i].end_time - base::TimeTicks()).InSecondsF()));
1002 widget_->webwidget()->recordFrameTimingEvent(
1003 blink::WebWidget::RenderEvent, frameId, webEvents);
1007 void RenderWidgetCompositor::RateLimitSharedMainThreadContext() {
1008 cc::ContextProvider* provider =
1009 compositor_deps_->GetSharedMainThreadContextProvider();
1010 // provider can be NULL after the GPU process crashed enough times and we
1011 // don't want to restart it any more (falling back to software).
1012 if (!provider)
1013 return;
1014 provider->ContextGL()->RateLimitOffscreenContextCHROMIUM();
1017 void RenderWidgetCompositor::SetSurfaceIdNamespace(
1018 uint32_t surface_id_namespace) {
1019 layer_tree_host_->set_surface_id_namespace(surface_id_namespace);
1022 } // namespace content