Refactor WebsiteSettings to operate on a SecurityInfo
[chromium-blink-merge.git] / content / renderer / gpu / render_widget_compositor.cc
blob75828bf5d18724239f34ddd5b7dc559caea025cc
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/single_thread_task_runner.h"
14 #include "base/strings/string_number_conversions.h"
15 #include "base/synchronization/lock.h"
16 #include "base/sys_info.h"
17 #include "base/thread_task_runner_handle.h"
18 #include "base/time/time.h"
19 #include "base/values.h"
20 #include "cc/animation/animation_host.h"
21 #include "cc/animation/animation_timeline.h"
22 #include "cc/base/switches.h"
23 #include "cc/blink/web_compositor_animation_timeline_impl.h"
24 #include "cc/blink/web_layer_impl.h"
25 #include "cc/debug/layer_tree_debug_state.h"
26 #include "cc/debug/micro_benchmark.h"
27 #include "cc/input/layer_selection_bound.h"
28 #include "cc/layers/layer.h"
29 #include "cc/output/begin_frame_args.h"
30 #include "cc/output/copy_output_request.h"
31 #include "cc/output/copy_output_result.h"
32 #include "cc/output/latency_info_swap_promise.h"
33 #include "cc/output/swap_promise.h"
34 #include "cc/resources/single_release_callback.h"
35 #include "cc/scheduler/begin_frame_source.h"
36 #include "cc/trees/latency_info_swap_promise_monitor.h"
37 #include "cc/trees/layer_tree_host.h"
38 #include "components/scheduler/renderer/renderer_scheduler.h"
39 #include "content/common/content_switches_internal.h"
40 #include "content/common/gpu/client/context_provider_command_buffer.h"
41 #include "content/public/common/content_switches.h"
42 #include "content/renderer/input/input_handler_manager.h"
43 #include "gpu/command_buffer/client/gles2_interface.h"
44 #include "third_party/WebKit/public/platform/WebCompositeAndReadbackAsyncCallback.h"
45 #include "third_party/WebKit/public/platform/WebLayoutAndPaintAsyncCallback.h"
46 #include "third_party/WebKit/public/platform/WebSize.h"
47 #include "third_party/WebKit/public/web/WebKit.h"
48 #include "third_party/WebKit/public/web/WebRuntimeFeatures.h"
49 #include "third_party/WebKit/public/web/WebSelection.h"
50 #include "third_party/WebKit/public/web/WebWidget.h"
51 #include "ui/gl/gl_switches.h"
52 #include "ui/native_theme/native_theme_switches.h"
54 #if defined(OS_ANDROID)
55 #include "base/android/build_info.h"
56 #include "content/renderer/android/synchronous_compositor_factory.h"
57 #include "ui/gfx/android/device_display_info.h"
58 #endif
60 namespace base {
61 class Value;
64 namespace cc {
65 class Layer;
68 using blink::WebBeginFrameArgs;
69 using blink::WebFloatPoint;
70 using blink::WebRect;
71 using blink::WebSelection;
72 using blink::WebSize;
73 using blink::WebTopControlsState;
75 namespace content {
76 namespace {
78 bool GetSwitchValueAsInt(const base::CommandLine& command_line,
79 const std::string& switch_string,
80 int min_value,
81 int max_value,
82 int* result) {
83 std::string string_value = command_line.GetSwitchValueASCII(switch_string);
84 int int_value;
85 if (base::StringToInt(string_value, &int_value) &&
86 int_value >= min_value && int_value <= max_value) {
87 *result = int_value;
88 return true;
89 } else {
90 LOG(WARNING) << "Failed to parse switch " << switch_string << ": " <<
91 string_value;
92 return false;
96 cc::LayerSelectionBound ConvertWebSelectionBound(
97 const WebSelection& web_selection,
98 bool is_start) {
99 cc::LayerSelectionBound cc_bound;
100 if (web_selection.isNone())
101 return cc_bound;
103 const blink::WebSelectionBound& web_bound =
104 is_start ? web_selection.start() : web_selection.end();
105 DCHECK(web_bound.layerId);
106 cc_bound.type = cc::SELECTION_BOUND_CENTER;
107 if (web_selection.isRange()) {
108 if (is_start) {
109 cc_bound.type = web_bound.isTextDirectionRTL ? cc::SELECTION_BOUND_RIGHT
110 : cc::SELECTION_BOUND_LEFT;
111 } else {
112 cc_bound.type = web_bound.isTextDirectionRTL ? cc::SELECTION_BOUND_LEFT
113 : cc::SELECTION_BOUND_RIGHT;
116 cc_bound.layer_id = web_bound.layerId;
117 cc_bound.edge_top = gfx::Point(web_bound.edgeTopInLayer);
118 cc_bound.edge_bottom = gfx::Point(web_bound.edgeBottomInLayer);
119 return cc_bound;
122 cc::LayerSelection ConvertWebSelection(const WebSelection& web_selection) {
123 cc::LayerSelection cc_selection;
124 cc_selection.start = ConvertWebSelectionBound(web_selection, true);
125 cc_selection.end = ConvertWebSelectionBound(web_selection, false);
126 cc_selection.is_editable = web_selection.isEditable();
127 cc_selection.is_empty_text_form_control =
128 web_selection.isEmptyTextFormControl();
129 return cc_selection;
132 gfx::Size CalculateDefaultTileSize(RenderWidget* widget) {
133 int default_tile_size = 256;
134 #if defined(OS_ANDROID)
135 // TODO(epenner): unify this for all platforms if it
136 // makes sense (http://crbug.com/159524)
138 gfx::DeviceDisplayInfo info;
139 bool real_size_supported = true;
140 int display_width = info.GetPhysicalDisplayWidth();
141 int display_height = info.GetPhysicalDisplayHeight();
142 if (display_width == 0 || display_height == 0) {
143 real_size_supported = false;
144 display_width = info.GetDisplayWidth();
145 display_height = info.GetDisplayHeight();
148 int portrait_width = std::min(display_width, display_height);
149 int landscape_width = std::max(display_width, display_height);
151 if (real_size_supported) {
152 // Maximum HD dimensions should be 768x1280
153 // Maximum FHD dimensions should be 1200x1920
154 if (portrait_width > 768 || landscape_width > 1280)
155 default_tile_size = 384;
156 if (portrait_width > 1200 || landscape_width > 1920)
157 default_tile_size = 512;
159 // Adjust for some resolutions that barely straddle an extra
160 // tile when in portrait mode. This helps worst case scroll/raster
161 // by not needing a full extra tile for each row.
162 if (default_tile_size == 256 && portrait_width == 768)
163 default_tile_size += 32;
164 if (default_tile_size == 384 && portrait_width == 1200)
165 default_tile_size += 32;
166 } else {
167 // We don't know the exact resolution due to screen controls etc.
168 // So this just estimates the values above using tile counts.
169 int numTiles = (display_width * display_height) / (256 * 256);
170 if (numTiles > 16)
171 default_tile_size = 384;
172 if (numTiles >= 40)
173 default_tile_size = 512;
175 #elif defined(OS_CHROMEOS)
176 // Use 512 for high DPI (dsf=2.0f) devices.
177 if (widget->screen_info().deviceScaleFactor >= 2.0f)
178 default_tile_size = 512;
179 #endif
181 return gfx::Size(default_tile_size, default_tile_size);
184 // Check cc::TopControlsState, and blink::WebTopControlsState
185 // are kept in sync.
186 static_assert(int(blink::WebTopControlsBoth) == int(cc::BOTH),
187 "mismatching enums: BOTH");
188 static_assert(int(blink::WebTopControlsHidden) == int(cc::HIDDEN),
189 "mismatching enums: HIDDEN");
190 static_assert(int(blink::WebTopControlsShown) == int(cc::SHOWN),
191 "mismatching enums: SHOWN");
193 static cc::TopControlsState ConvertTopControlsState(
194 WebTopControlsState state) {
195 return static_cast<cc::TopControlsState>(state);
198 } // namespace
200 // static
201 scoped_ptr<RenderWidgetCompositor> RenderWidgetCompositor::Create(
202 RenderWidget* widget,
203 CompositorDependencies* compositor_deps) {
204 scoped_ptr<RenderWidgetCompositor> compositor(
205 new RenderWidgetCompositor(widget, compositor_deps));
206 compositor->Initialize();
207 return compositor;
210 RenderWidgetCompositor::RenderWidgetCompositor(
211 RenderWidget* widget,
212 CompositorDependencies* compositor_deps)
213 : num_failed_recreate_attempts_(0),
214 widget_(widget),
215 compositor_deps_(compositor_deps),
216 layout_and_paint_async_callback_(nullptr),
217 weak_factory_(this) {
220 void RenderWidgetCompositor::Initialize() {
221 base::CommandLine* cmd = base::CommandLine::ForCurrentProcess();
223 cc::LayerTreeSettings settings;
225 // For web contents, layer transforms should scale up the contents of layers
226 // to keep content always crisp when possible.
227 settings.layer_transforms_should_scale_layer_contents = true;
229 if (cmd->HasSwitch(switches::kDisableGpuVsync)) {
230 std::string display_vsync_string =
231 cmd->GetSwitchValueASCII(switches::kDisableGpuVsync);
232 if (display_vsync_string == "gpu") {
233 settings.renderer_settings.disable_display_vsync = true;
234 } else if (display_vsync_string == "beginframe") {
235 settings.wait_for_beginframe_interval = false;
236 } else {
237 settings.renderer_settings.disable_display_vsync = true;
238 settings.wait_for_beginframe_interval = false;
241 settings.main_frame_before_activation_enabled =
242 cmd->HasSwitch(cc::switches::kEnableMainFrameBeforeActivation) &&
243 !cmd->HasSwitch(cc::switches::kDisableMainFrameBeforeActivation);
244 settings.accelerated_animation_enabled =
245 compositor_deps_->IsThreadedAnimationEnabled();
247 if (cmd->HasSwitch(switches::kEnableCompositorAnimationTimelines)) {
248 settings.use_compositor_animation_timelines = true;
249 blink::WebRuntimeFeatures::enableCompositorAnimationTimelines(true);
252 settings.default_tile_size = CalculateDefaultTileSize(widget_);
253 if (cmd->HasSwitch(switches::kDefaultTileWidth)) {
254 int tile_width = 0;
255 GetSwitchValueAsInt(*cmd,
256 switches::kDefaultTileWidth,
258 std::numeric_limits<int>::max(),
259 &tile_width);
260 settings.default_tile_size.set_width(tile_width);
262 if (cmd->HasSwitch(switches::kDefaultTileHeight)) {
263 int tile_height = 0;
264 GetSwitchValueAsInt(*cmd,
265 switches::kDefaultTileHeight,
267 std::numeric_limits<int>::max(),
268 &tile_height);
269 settings.default_tile_size.set_height(tile_height);
272 int max_untiled_layer_width = settings.max_untiled_layer_size.width();
273 if (cmd->HasSwitch(switches::kMaxUntiledLayerWidth)) {
274 GetSwitchValueAsInt(*cmd, switches::kMaxUntiledLayerWidth, 1,
275 std::numeric_limits<int>::max(),
276 &max_untiled_layer_width);
278 int max_untiled_layer_height = settings.max_untiled_layer_size.height();
279 if (cmd->HasSwitch(switches::kMaxUntiledLayerHeight)) {
280 GetSwitchValueAsInt(*cmd, switches::kMaxUntiledLayerHeight, 1,
281 std::numeric_limits<int>::max(),
282 &max_untiled_layer_height);
285 settings.max_untiled_layer_size = gfx::Size(max_untiled_layer_width,
286 max_untiled_layer_height);
288 settings.gpu_rasterization_msaa_sample_count =
289 compositor_deps_->GetGpuRasterizationMSAASampleCount();
290 settings.gpu_rasterization_forced =
291 compositor_deps_->IsGpuRasterizationForced();
292 settings.gpu_rasterization_enabled =
293 compositor_deps_->IsGpuRasterizationEnabled();
295 settings.can_use_lcd_text = compositor_deps_->IsLcdTextEnabled();
296 settings.use_distance_field_text =
297 compositor_deps_->IsDistanceFieldTextEnabled();
298 settings.use_zero_copy = compositor_deps_->IsZeroCopyEnabled();
299 settings.use_persistent_map_for_gpu_memory_buffers =
300 compositor_deps_->IsPersistentGpuMemoryBufferEnabled();
301 settings.enable_elastic_overscroll =
302 compositor_deps_->IsElasticOverscrollEnabled();
303 settings.use_image_texture_targets =
304 compositor_deps_->GetImageTextureTargets();
305 settings.gather_images = compositor_deps_->IsGatherPixelRefsEnabled();
307 if (cmd->HasSwitch(cc::switches::kTopControlsShowThreshold)) {
308 std::string top_threshold_str =
309 cmd->GetSwitchValueASCII(cc::switches::kTopControlsShowThreshold);
310 double show_threshold;
311 if (base::StringToDouble(top_threshold_str, &show_threshold) &&
312 show_threshold >= 0.f && show_threshold <= 1.f)
313 settings.top_controls_show_threshold = show_threshold;
316 if (cmd->HasSwitch(cc::switches::kTopControlsHideThreshold)) {
317 std::string top_threshold_str =
318 cmd->GetSwitchValueASCII(cc::switches::kTopControlsHideThreshold);
319 double hide_threshold;
320 if (base::StringToDouble(top_threshold_str, &hide_threshold) &&
321 hide_threshold >= 0.f && hide_threshold <= 1.f)
322 settings.top_controls_hide_threshold = hide_threshold;
325 settings.verify_property_trees =
326 cmd->HasSwitch(cc::switches::kEnablePropertyTreeVerification);
327 settings.renderer_settings.allow_antialiasing &=
328 !cmd->HasSwitch(cc::switches::kDisableCompositedAntialiasing);
329 // The means the renderer compositor has 2 possible modes:
330 // - Threaded compositing with a scheduler.
331 // - Single threaded compositing without a scheduler (for layout tests only).
332 // Using the scheduler in layout tests introduces additional composite steps
333 // that create flakiness.
334 settings.single_thread_proxy_scheduler = false;
336 // These flags should be mirrored by UI versions in ui/compositor/.
337 settings.initial_debug_state.show_debug_borders =
338 cmd->HasSwitch(cc::switches::kShowCompositedLayerBorders);
339 settings.initial_debug_state.show_fps_counter =
340 cmd->HasSwitch(cc::switches::kShowFPSCounter);
341 settings.initial_debug_state.show_layer_animation_bounds_rects =
342 cmd->HasSwitch(cc::switches::kShowLayerAnimationBounds);
343 settings.initial_debug_state.show_paint_rects =
344 cmd->HasSwitch(switches::kShowPaintRects);
345 settings.initial_debug_state.show_property_changed_rects =
346 cmd->HasSwitch(cc::switches::kShowPropertyChangedRects);
347 settings.initial_debug_state.show_surface_damage_rects =
348 cmd->HasSwitch(cc::switches::kShowSurfaceDamageRects);
349 settings.initial_debug_state.show_screen_space_rects =
350 cmd->HasSwitch(cc::switches::kShowScreenSpaceRects);
351 settings.initial_debug_state.show_replica_screen_space_rects =
352 cmd->HasSwitch(cc::switches::kShowReplicaScreenSpaceRects);
354 settings.initial_debug_state.SetRecordRenderingStats(
355 cmd->HasSwitch(cc::switches::kEnableGpuBenchmarking));
357 if (cmd->HasSwitch(cc::switches::kSlowDownRasterScaleFactor)) {
358 const int kMinSlowDownScaleFactor = 0;
359 const int kMaxSlowDownScaleFactor = INT_MAX;
360 GetSwitchValueAsInt(
361 *cmd,
362 cc::switches::kSlowDownRasterScaleFactor,
363 kMinSlowDownScaleFactor,
364 kMaxSlowDownScaleFactor,
365 &settings.initial_debug_state.slow_down_raster_scale_factor);
368 settings.strict_layer_property_change_checking =
369 cmd->HasSwitch(cc::switches::kStrictLayerPropertyChangeChecking);
371 #if defined(OS_ANDROID)
372 SynchronousCompositorFactory* synchronous_compositor_factory =
373 SynchronousCompositorFactory::GetInstance();
375 // We can't use GPU rasterization on low-end devices, because the Ganesh
376 // cache would consume too much memory.
377 if (base::SysInfo::IsLowEndDevice())
378 settings.gpu_rasterization_enabled = false;
379 settings.using_synchronous_renderer_compositor =
380 synchronous_compositor_factory;
381 settings.record_full_layer = widget_->DoesRecordFullLayer();
382 if (synchronous_compositor_factory) {
383 // Android WebView uses system scrollbars, so make ours invisible.
384 settings.scrollbar_animator = cc::LayerTreeSettings::NO_ANIMATOR;
385 settings.solid_color_scrollbar_color = SK_ColorTRANSPARENT;
386 } else {
387 settings.scrollbar_animator = cc::LayerTreeSettings::LINEAR_FADE;
388 settings.scrollbar_fade_delay_ms = 300;
389 settings.scrollbar_fade_resize_delay_ms = 2000;
390 settings.scrollbar_fade_duration_ms = 300;
391 settings.solid_color_scrollbar_color = SkColorSetARGB(128, 128, 128, 128);
393 settings.renderer_settings.highp_threshold_min = 2048;
394 // Android WebView handles root layer flings itself.
395 settings.ignore_root_layer_flings =
396 synchronous_compositor_factory;
397 // Memory policy on Android WebView does not depend on whether device is
398 // low end, so always use default policy.
399 bool use_low_memory_policy =
400 base::SysInfo::IsLowEndDevice() && !synchronous_compositor_factory;
401 // RGBA_4444 textures are only enabled by default for low end devices
402 // and are disabled for Android WebView as it doesn't support the format.
403 settings.renderer_settings.use_rgba_4444_textures = use_low_memory_policy;
404 if (use_low_memory_policy) {
405 // On low-end we want to be very carefull about killing other
406 // apps. So initially we use 50% more memory to avoid flickering
407 // or raster-on-demand.
408 settings.max_memory_for_prepaint_percentage = 67;
409 } else {
410 // On other devices we have increased memory excessively to avoid
411 // raster-on-demand already, so now we reserve 50% _only_ to avoid
412 // raster-on-demand, and use 50% of the memory otherwise.
413 settings.max_memory_for_prepaint_percentage = 50;
415 // Webview does not own the surface so should not clear it.
416 settings.renderer_settings.should_clear_root_render_pass =
417 !synchronous_compositor_factory;
419 // TODO(danakj): Only do this on low end devices.
420 settings.create_low_res_tiling = true;
422 settings.use_external_begin_frame_source = true;
424 #elif !defined(OS_MACOSX)
425 if (ui::IsOverlayScrollbarEnabled()) {
426 settings.scrollbar_animator = cc::LayerTreeSettings::THINNING;
427 settings.solid_color_scrollbar_color = SkColorSetARGB(128, 128, 128, 128);
428 } else {
429 settings.scrollbar_animator = cc::LayerTreeSettings::LINEAR_FADE;
430 settings.solid_color_scrollbar_color = SkColorSetARGB(128, 128, 128, 128);
432 settings.scrollbar_fade_delay_ms = 500;
433 settings.scrollbar_fade_resize_delay_ms = 500;
434 settings.scrollbar_fade_duration_ms = 300;
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;
441 if (cmd->HasSwitch(cc::switches::kEnableBeginFrameScheduling))
442 settings.use_external_begin_frame_source = true;
444 settings.renderer_settings.use_rgba_4444_textures |=
445 cmd->HasSwitch(switches::kEnableRGBA4444Textures);
446 settings.renderer_settings.use_rgba_4444_textures &=
447 !cmd->HasSwitch(switches::kDisableRGBA4444Textures);
449 if (widget_->for_oopif()) {
450 // TODO(simonhong): Apply BeginFrame scheduling for OOPIF.
451 // See crbug.com/471411.
452 settings.use_external_begin_frame_source = false;
455 settings.max_staging_buffer_usage_in_bytes = 32 * 1024 * 1024; // 32MB
456 // Use 1/4th of staging buffers on low-end devices.
457 if (base::SysInfo::IsLowEndDevice())
458 settings.max_staging_buffer_usage_in_bytes /= 4;
460 scoped_refptr<base::SingleThreadTaskRunner> compositor_thread_task_runner =
461 compositor_deps_->GetCompositorImplThreadTaskRunner();
462 scoped_refptr<base::SingleThreadTaskRunner>
463 main_thread_compositor_task_runner =
464 compositor_deps_->GetCompositorMainThreadTaskRunner();
465 cc::SharedBitmapManager* shared_bitmap_manager =
466 compositor_deps_->GetSharedBitmapManager();
467 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager =
468 compositor_deps_->GetGpuMemoryBufferManager();
469 cc::TaskGraphRunner* task_graph_runner =
470 compositor_deps_->GetTaskGraphRunner();
472 scoped_ptr<cc::BeginFrameSource> external_begin_frame_source;
473 if (settings.use_external_begin_frame_source) {
474 external_begin_frame_source =
475 compositor_deps_->CreateExternalBeginFrameSource(widget_->routing_id());
478 cc::LayerTreeHost::InitParams params;
479 params.client = this;
480 params.shared_bitmap_manager = shared_bitmap_manager;
481 params.gpu_memory_buffer_manager = gpu_memory_buffer_manager;
482 params.settings = &settings;
483 params.task_graph_runner = task_graph_runner;
484 params.main_task_runner = main_thread_compositor_task_runner;
485 params.external_begin_frame_source = external_begin_frame_source.Pass();
486 if (compositor_thread_task_runner.get()) {
487 layer_tree_host_ = cc::LayerTreeHost::CreateThreaded(
488 compositor_thread_task_runner, &params);
489 } else {
490 layer_tree_host_ = cc::LayerTreeHost::CreateSingleThreaded(this, &params);
492 DCHECK(layer_tree_host_);
495 RenderWidgetCompositor::~RenderWidgetCompositor() {}
497 const base::WeakPtr<cc::InputHandler>&
498 RenderWidgetCompositor::GetInputHandler() {
499 return layer_tree_host_->GetInputHandler();
502 bool RenderWidgetCompositor::BeginMainFrameRequested() const {
503 return layer_tree_host_->BeginMainFrameRequested();
506 void RenderWidgetCompositor::SetNeedsDisplayOnAllLayers() {
507 layer_tree_host_->SetNeedsDisplayOnAllLayers();
510 void RenderWidgetCompositor::SetRasterizeOnlyVisibleContent() {
511 cc::LayerTreeDebugState current = layer_tree_host_->debug_state();
512 current.rasterize_only_visible_content = true;
513 layer_tree_host_->SetDebugState(current);
516 void RenderWidgetCompositor::SetNeedsRedrawRect(gfx::Rect damage_rect) {
517 layer_tree_host_->SetNeedsRedrawRect(damage_rect);
520 void RenderWidgetCompositor::SetNeedsForcedRedraw() {
521 layer_tree_host_->SetNextCommitForcesRedraw();
522 setNeedsAnimate();
525 scoped_ptr<cc::SwapPromiseMonitor>
526 RenderWidgetCompositor::CreateLatencyInfoSwapPromiseMonitor(
527 ui::LatencyInfo* latency) {
528 return scoped_ptr<cc::SwapPromiseMonitor>(
529 new cc::LatencyInfoSwapPromiseMonitor(
530 latency, layer_tree_host_.get(), NULL));
533 void RenderWidgetCompositor::QueueSwapPromise(
534 scoped_ptr<cc::SwapPromise> swap_promise) {
535 layer_tree_host_->QueueSwapPromise(swap_promise.Pass());
538 int RenderWidgetCompositor::GetSourceFrameNumber() const {
539 return layer_tree_host_->source_frame_number();
542 void RenderWidgetCompositor::SetNeedsUpdateLayers() {
543 layer_tree_host_->SetNeedsUpdateLayers();
546 void RenderWidgetCompositor::SetNeedsCommit() {
547 layer_tree_host_->SetNeedsCommit();
550 void RenderWidgetCompositor::NotifyInputThrottledUntilCommit() {
551 layer_tree_host_->NotifyInputThrottledUntilCommit();
554 const cc::Layer* RenderWidgetCompositor::GetRootLayer() const {
555 return layer_tree_host_->root_layer();
558 int RenderWidgetCompositor::ScheduleMicroBenchmark(
559 const std::string& name,
560 scoped_ptr<base::Value> value,
561 const base::Callback<void(scoped_ptr<base::Value>)>& callback) {
562 return layer_tree_host_->ScheduleMicroBenchmark(name, value.Pass(), callback);
565 bool RenderWidgetCompositor::SendMessageToMicroBenchmark(
566 int id,
567 scoped_ptr<base::Value> value) {
568 return layer_tree_host_->SendMessageToMicroBenchmark(id, value.Pass());
571 void RenderWidgetCompositor::StartCompositor() {
572 layer_tree_host_->SetLayerTreeHostClientReady();
575 void RenderWidgetCompositor::setRootLayer(const blink::WebLayer& layer) {
576 layer_tree_host_->SetRootLayer(
577 static_cast<const cc_blink::WebLayerImpl*>(&layer)->layer());
580 void RenderWidgetCompositor::clearRootLayer() {
581 layer_tree_host_->SetRootLayer(scoped_refptr<cc::Layer>());
584 void RenderWidgetCompositor::attachCompositorAnimationTimeline(
585 blink::WebCompositorAnimationTimeline* compositor_timeline) {
586 DCHECK(compositor_timeline);
587 DCHECK(layer_tree_host_->animation_host());
588 layer_tree_host_->animation_host()->AddAnimationTimeline(
589 static_cast<const cc_blink::WebCompositorAnimationTimelineImpl*>(
590 compositor_timeline)->animation_timeline());
593 void RenderWidgetCompositor::detachCompositorAnimationTimeline(
594 blink::WebCompositorAnimationTimeline* compositor_timeline) {
595 DCHECK(compositor_timeline);
596 DCHECK(layer_tree_host_->animation_host());
597 layer_tree_host_->animation_host()->RemoveAnimationTimeline(
598 static_cast<const cc_blink::WebCompositorAnimationTimelineImpl*>(
599 compositor_timeline)->animation_timeline());
602 void RenderWidgetCompositor::setViewportSize(
603 const WebSize&,
604 const WebSize& device_viewport_size) {
605 layer_tree_host_->SetViewportSize(device_viewport_size);
608 void RenderWidgetCompositor::setViewportSize(
609 const WebSize& device_viewport_size) {
610 layer_tree_host_->SetViewportSize(device_viewport_size);
613 WebFloatPoint RenderWidgetCompositor::adjustEventPointForPinchZoom(
614 const WebFloatPoint& point) const {
615 return point;
618 void RenderWidgetCompositor::setDeviceScaleFactor(float device_scale) {
619 layer_tree_host_->SetDeviceScaleFactor(device_scale);
622 float RenderWidgetCompositor::deviceScaleFactor() const {
623 return layer_tree_host_->device_scale_factor();
626 void RenderWidgetCompositor::setBackgroundColor(blink::WebColor color) {
627 layer_tree_host_->set_background_color(color);
630 void RenderWidgetCompositor::setHasTransparentBackground(bool transparent) {
631 layer_tree_host_->set_has_transparent_background(transparent);
634 void RenderWidgetCompositor::setVisible(bool visible) {
635 layer_tree_host_->SetVisible(visible);
638 void RenderWidgetCompositor::setPageScaleFactorAndLimits(
639 float page_scale_factor, float minimum, float maximum) {
640 layer_tree_host_->SetPageScaleFactorAndLimits(
641 page_scale_factor, minimum, maximum);
644 void RenderWidgetCompositor::startPageScaleAnimation(
645 const blink::WebPoint& destination,
646 bool use_anchor,
647 float new_page_scale,
648 double duration_sec) {
649 base::TimeDelta duration = base::TimeDelta::FromMicroseconds(
650 duration_sec * base::Time::kMicrosecondsPerSecond);
651 layer_tree_host_->StartPageScaleAnimation(
652 gfx::Vector2d(destination.x, destination.y),
653 use_anchor,
654 new_page_scale,
655 duration);
658 void RenderWidgetCompositor::heuristicsForGpuRasterizationUpdated(
659 bool matches_heuristics) {
660 layer_tree_host_->SetHasGpuRasterizationTrigger(matches_heuristics);
663 void RenderWidgetCompositor::setNeedsAnimate() {
664 layer_tree_host_->SetNeedsAnimate();
665 layer_tree_host_->SetNeedsUpdateLayers();
668 void RenderWidgetCompositor::setNeedsBeginFrame() {
669 layer_tree_host_->SetNeedsAnimate();
672 void RenderWidgetCompositor::setNeedsCompositorUpdate() {
673 layer_tree_host_->SetNeedsUpdateLayers();
676 void RenderWidgetCompositor::didStopFlinging() {
677 layer_tree_host_->DidStopFlinging();
680 void RenderWidgetCompositor::registerForAnimations(blink::WebLayer* layer) {
681 cc::Layer* cc_layer = static_cast<cc_blink::WebLayerImpl*>(layer)->layer();
682 cc_layer->RegisterForAnimations(layer_tree_host_->animation_registrar());
685 void RenderWidgetCompositor::registerViewportLayers(
686 const blink::WebLayer* overscrollElasticityLayer,
687 const blink::WebLayer* pageScaleLayer,
688 const blink::WebLayer* innerViewportScrollLayer,
689 const blink::WebLayer* outerViewportScrollLayer) {
690 layer_tree_host_->RegisterViewportLayers(
691 // TODO(bokan): This check can probably be removed now, but it looks
692 // like overscroll elasticity may still be NULL until PinchViewport
693 // registers its layers.
694 // The scroll elasticity layer will only exist when using pinch virtual
695 // viewports.
696 overscrollElasticityLayer
697 ? static_cast<const cc_blink::WebLayerImpl*>(
698 overscrollElasticityLayer)->layer()
699 : NULL,
700 static_cast<const cc_blink::WebLayerImpl*>(pageScaleLayer)->layer(),
701 static_cast<const cc_blink::WebLayerImpl*>(innerViewportScrollLayer)
702 ->layer(),
703 // TODO(bokan): This check can probably be removed now, but it looks
704 // like overscroll elasticity may still be NULL until PinchViewport
705 // registers its layers.
706 // The outer viewport layer will only exist when using pinch virtual
707 // viewports.
708 outerViewportScrollLayer
709 ? static_cast<const cc_blink::WebLayerImpl*>(outerViewportScrollLayer)
710 ->layer()
711 : NULL);
714 void RenderWidgetCompositor::clearViewportLayers() {
715 layer_tree_host_->RegisterViewportLayers(
716 scoped_refptr<cc::Layer>(), scoped_refptr<cc::Layer>(),
717 scoped_refptr<cc::Layer>(), scoped_refptr<cc::Layer>());
720 void RenderWidgetCompositor::registerSelection(
721 const blink::WebSelection& selection) {
722 layer_tree_host_->RegisterSelection(ConvertWebSelection(selection));
725 void RenderWidgetCompositor::clearSelection() {
726 cc::LayerSelection empty_selection;
727 layer_tree_host_->RegisterSelection(empty_selection);
730 void CompositeAndReadbackAsyncCallback(
731 blink::WebCompositeAndReadbackAsyncCallback* callback,
732 scoped_ptr<cc::CopyOutputResult> result) {
733 if (result->HasBitmap()) {
734 scoped_ptr<SkBitmap> result_bitmap = result->TakeBitmap();
735 callback->didCompositeAndReadback(*result_bitmap);
736 } else {
737 callback->didCompositeAndReadback(SkBitmap());
741 bool RenderWidgetCompositor::CompositeIsSynchronous() const {
742 return !compositor_deps_->GetCompositorImplThreadTaskRunner().get() &&
743 !layer_tree_host_->settings().single_thread_proxy_scheduler;
746 void RenderWidgetCompositor::layoutAndPaintAsync(
747 blink::WebLayoutAndPaintAsyncCallback* callback) {
748 DCHECK(!temporary_copy_output_request_ && !layout_and_paint_async_callback_);
749 layout_and_paint_async_callback_ = callback;
751 if (CompositeIsSynchronous()) {
752 base::ThreadTaskRunnerHandle::Get()->PostTask(
753 FROM_HERE, base::Bind(&RenderWidgetCompositor::LayoutAndUpdateLayers,
754 weak_factory_.GetWeakPtr()));
755 } else {
756 layer_tree_host_->SetNeedsCommit();
760 void RenderWidgetCompositor::LayoutAndUpdateLayers() {
761 DCHECK(CompositeIsSynchronous());
762 layer_tree_host_->LayoutAndUpdateLayers();
763 InvokeLayoutAndPaintCallback();
766 void RenderWidgetCompositor::InvokeLayoutAndPaintCallback() {
767 if (!layout_and_paint_async_callback_)
768 return;
769 layout_and_paint_async_callback_->didLayoutAndPaint();
770 layout_and_paint_async_callback_ = nullptr;
773 void RenderWidgetCompositor::compositeAndReadbackAsync(
774 blink::WebCompositeAndReadbackAsyncCallback* callback) {
775 DCHECK(!temporary_copy_output_request_ && !layout_and_paint_async_callback_);
776 temporary_copy_output_request_ =
777 cc::CopyOutputRequest::CreateBitmapRequest(
778 base::Bind(&CompositeAndReadbackAsyncCallback, callback));
780 // Force a commit to happen. The temporary copy output request will
781 // be installed after layout which will happen as a part of the commit, for
782 // widgets that delay the creation of their output surface.
783 if (CompositeIsSynchronous()) {
784 base::ThreadTaskRunnerHandle::Get()->PostTask(
785 FROM_HERE, base::Bind(&RenderWidgetCompositor::SynchronouslyComposite,
786 weak_factory_.GetWeakPtr()));
787 } else {
788 layer_tree_host_->SetNeedsCommit();
792 void RenderWidgetCompositor::SynchronouslyComposite() {
793 DCHECK(CompositeIsSynchronous());
794 layer_tree_host_->Composite(base::TimeTicks::Now());
797 void RenderWidgetCompositor::setDeferCommits(bool defer_commits) {
798 layer_tree_host_->SetDeferCommits(defer_commits);
801 int RenderWidgetCompositor::layerTreeId() const {
802 return layer_tree_host_->id();
805 void RenderWidgetCompositor::setShowFPSCounter(bool show) {
806 cc::LayerTreeDebugState debug_state = layer_tree_host_->debug_state();
807 debug_state.show_fps_counter = show;
808 layer_tree_host_->SetDebugState(debug_state);
811 void RenderWidgetCompositor::setShowPaintRects(bool show) {
812 cc::LayerTreeDebugState debug_state = layer_tree_host_->debug_state();
813 debug_state.show_paint_rects = show;
814 layer_tree_host_->SetDebugState(debug_state);
817 void RenderWidgetCompositor::setShowDebugBorders(bool show) {
818 cc::LayerTreeDebugState debug_state = layer_tree_host_->debug_state();
819 debug_state.show_debug_borders = show;
820 layer_tree_host_->SetDebugState(debug_state);
823 void RenderWidgetCompositor::setShowScrollBottleneckRects(bool show) {
824 cc::LayerTreeDebugState debug_state = layer_tree_host_->debug_state();
825 debug_state.show_touch_event_handler_rects = show;
826 debug_state.show_wheel_event_handler_rects = show;
827 debug_state.show_non_fast_scrollable_rects = show;
828 layer_tree_host_->SetDebugState(debug_state);
831 void RenderWidgetCompositor::updateTopControlsState(
832 WebTopControlsState constraints,
833 WebTopControlsState current,
834 bool animate) {
835 layer_tree_host_->UpdateTopControlsState(ConvertTopControlsState(constraints),
836 ConvertTopControlsState(current),
837 animate);
840 void RenderWidgetCompositor::setTopControlsHeight(float height, bool shrink) {
841 layer_tree_host_->SetTopControlsHeight(height, shrink);
844 void RenderWidgetCompositor::setTopControlsShownRatio(float ratio) {
845 layer_tree_host_->SetTopControlsShownRatio(ratio);
848 void RenderWidgetCompositor::setHidePinchScrollbarsNearMinScale(bool hide) {
849 layer_tree_host_->set_hide_pinch_scrollbars_near_min_scale(hide);
852 void RenderWidgetCompositor::WillBeginMainFrame() {
853 widget_->WillBeginCompositorFrame();
856 void RenderWidgetCompositor::DidBeginMainFrame() {
859 void RenderWidgetCompositor::BeginMainFrame(const cc::BeginFrameArgs& args) {
860 double frame_time_sec = (args.frame_time - base::TimeTicks()).InSecondsF();
861 double deadline_sec = (args.deadline - base::TimeTicks()).InSecondsF();
862 double interval_sec = args.interval.InSecondsF();
863 WebBeginFrameArgs web_begin_frame_args =
864 WebBeginFrameArgs(frame_time_sec, deadline_sec, interval_sec);
865 compositor_deps_->GetRendererScheduler()->WillBeginFrame(args);
866 widget_->webwidget()->beginFrame(web_begin_frame_args);
869 void RenderWidgetCompositor::BeginMainFrameNotExpectedSoon() {
870 compositor_deps_->GetRendererScheduler()->BeginFrameNotExpectedSoon();
873 void RenderWidgetCompositor::Layout() {
874 widget_->webwidget()->layout();
876 if (temporary_copy_output_request_) {
877 // For WebViewImpl, this will always have a root layer. For other widgets,
878 // the widget may be closed before servicing this request, so ignore it.
879 if (cc::Layer* root_layer = layer_tree_host_->root_layer()) {
880 root_layer->RequestCopyOfOutput(temporary_copy_output_request_.Pass());
881 } else {
882 temporary_copy_output_request_->SendEmptyResult();
883 temporary_copy_output_request_ = nullptr;
888 void RenderWidgetCompositor::ApplyViewportDeltas(
889 const gfx::Vector2dF& inner_delta,
890 const gfx::Vector2dF& outer_delta,
891 const gfx::Vector2dF& elastic_overscroll_delta,
892 float page_scale,
893 float top_controls_delta) {
894 widget_->webwidget()->applyViewportDeltas(
895 inner_delta,
896 outer_delta,
897 elastic_overscroll_delta,
898 page_scale,
899 top_controls_delta);
902 void RenderWidgetCompositor::RequestNewOutputSurface() {
903 // If the host is closing, then no more compositing is possible. This
904 // prevents shutdown races between handling the close message and
905 // the CreateOutputSurface task.
906 if (widget_->host_closing())
907 return;
909 bool fallback =
910 num_failed_recreate_attempts_ >= OUTPUT_SURFACE_RETRIES_BEFORE_FALLBACK;
911 scoped_ptr<cc::OutputSurface> surface(widget_->CreateOutputSurface(fallback));
913 if (!surface) {
914 DidFailToInitializeOutputSurface();
915 return;
918 DCHECK_EQ(surface->capabilities().max_frames_pending, 1);
920 layer_tree_host_->SetOutputSurface(surface.Pass());
923 void RenderWidgetCompositor::DidInitializeOutputSurface() {
924 num_failed_recreate_attempts_ = 0;
927 void RenderWidgetCompositor::DidFailToInitializeOutputSurface() {
928 ++num_failed_recreate_attempts_;
929 // Tolerate a certain number of recreation failures to work around races
930 // in the output-surface-lost machinery.
931 LOG_IF(FATAL, (num_failed_recreate_attempts_ >= MAX_OUTPUT_SURFACE_RETRIES))
932 << "Failed to create a fallback OutputSurface.";
934 base::ThreadTaskRunnerHandle::Get()->PostTask(
935 FROM_HERE, base::Bind(&RenderWidgetCompositor::RequestNewOutputSurface,
936 weak_factory_.GetWeakPtr()));
939 void RenderWidgetCompositor::WillCommit() {
940 InvokeLayoutAndPaintCallback();
943 void RenderWidgetCompositor::DidCommit() {
944 DCHECK(!temporary_copy_output_request_);
945 widget_->DidCommitCompositorFrame();
946 compositor_deps_->GetRendererScheduler()->DidCommitFrameToCompositor();
949 void RenderWidgetCompositor::DidCommitAndDrawFrame() {
950 widget_->DidCommitAndDrawCompositorFrame();
953 void RenderWidgetCompositor::DidCompleteSwapBuffers() {
954 widget_->DidCompleteSwapBuffers();
955 bool threaded = !!compositor_deps_->GetCompositorImplThreadTaskRunner().get();
956 if (!threaded)
957 widget_->OnSwapBuffersComplete();
960 void RenderWidgetCompositor::DidCompletePageScaleAnimation() {
961 widget_->DidCompletePageScaleAnimation();
964 void RenderWidgetCompositor::ScheduleAnimation() {
965 widget_->scheduleAnimation();
968 void RenderWidgetCompositor::DidPostSwapBuffers() {
969 widget_->OnSwapBuffersPosted();
972 void RenderWidgetCompositor::DidAbortSwapBuffers() {
973 widget_->OnSwapBuffersAborted();
976 void RenderWidgetCompositor::RecordFrameTimingEvents(
977 scoped_ptr<cc::FrameTimingTracker::CompositeTimingSet> composite_events,
978 scoped_ptr<cc::FrameTimingTracker::MainFrameTimingSet> main_frame_events) {
979 for (const auto& composite_event : *composite_events ) {
980 int64_t frameId = composite_event.first;
981 const std::vector<cc::FrameTimingTracker::CompositeTimingEvent>& events =
982 composite_event.second;
983 std::vector<blink::WebFrameTimingEvent> webEvents;
984 for (size_t i = 0; i < events.size(); ++i) {
985 webEvents.push_back(blink::WebFrameTimingEvent(
986 events[i].frame_id,
987 (events[i].timestamp - base::TimeTicks()).InSecondsF()));
989 widget_->webwidget()->recordFrameTimingEvent(
990 blink::WebWidget::CompositeEvent, frameId, webEvents);
992 for (const auto& main_frame_event : *main_frame_events ) {
993 int64_t frameId = main_frame_event.first;
994 const std::vector<cc::FrameTimingTracker::MainFrameTimingEvent>& events =
995 main_frame_event.second;
996 std::vector<blink::WebFrameTimingEvent> webEvents;
997 for (size_t i = 0; i < events.size(); ++i) {
998 webEvents.push_back(blink::WebFrameTimingEvent(
999 events[i].frame_id,
1000 (events[i].timestamp - base::TimeTicks()).InSecondsF(),
1001 (events[i].end_time - base::TimeTicks()).InSecondsF()));
1003 widget_->webwidget()->recordFrameTimingEvent(
1004 blink::WebWidget::RenderEvent, frameId, webEvents);
1008 void RenderWidgetCompositor::RateLimitSharedMainThreadContext() {
1009 cc::ContextProvider* provider =
1010 compositor_deps_->GetSharedMainThreadContextProvider();
1011 // provider can be NULL after the GPU process crashed enough times and we
1012 // don't want to restart it any more (falling back to software).
1013 if (!provider)
1014 return;
1015 provider->ContextGL()->RateLimitOffscreenContextCHROMIUM();
1018 void RenderWidgetCompositor::SetSurfaceIdNamespace(
1019 uint32_t surface_id_namespace) {
1020 layer_tree_host_->set_surface_id_namespace(surface_id_namespace);
1023 } // namespace content