Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / content / renderer / gpu / render_widget_compositor.cc
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.
4
5 #include "content/renderer/gpu/render_widget_compositor.h"
6
7 #include <limits>
8 #include <string>
9
10 #if defined(OS_ANDROID)
11 #include "base/android/sys_utils.h"
12 #endif
13
14 #include "base/command_line.h"
15 #include "base/logging.h"
16 #include "base/strings/string_number_conversions.h"
17 #include "base/synchronization/lock.h"
18 #include "base/time/time.h"
19 #include "base/values.h"
20 #include "cc/base/latency_info_swap_promise.h"
21 #include "cc/base/latency_info_swap_promise_monitor.h"
22 #include "cc/base/switches.h"
23 #include "cc/debug/layer_tree_debug_state.h"
24 #include "cc/debug/micro_benchmark.h"
25 #include "cc/layers/layer.h"
26 #include "cc/output/copy_output_request.h"
27 #include "cc/output/copy_output_result.h"
28 #include "cc/resources/single_release_callback.h"
29 #include "cc/trees/layer_tree_host.h"
30 #include "content/child/child_shared_bitmap_manager.h"
31 #include "content/common/content_switches_internal.h"
32 #include "content/common/gpu/client/context_provider_command_buffer.h"
33 #include "content/public/common/content_switches.h"
34 #include "content/renderer/input/input_handler_manager.h"
35 #include "content/renderer/render_thread_impl.h"
36 #include "gpu/command_buffer/client/gles2_interface.h"
37 #include "third_party/WebKit/public/platform/WebCompositeAndReadbackAsyncCallback.h"
38 #include "third_party/WebKit/public/platform/WebSize.h"
39 #include "third_party/WebKit/public/web/WebWidget.h"
40 #include "ui/gfx/frame_time.h"
41 #include "ui/gl/gl_switches.h"
42 #include "ui/native_theme/native_theme_switches.h"
43 #include "webkit/renderer/compositor_bindings/web_layer_impl.h"
44
45 namespace base {
46 class Value;
47 }
48
49 namespace cc {
50 class Layer;
51 }
52
53 using blink::WebFloatPoint;
54 using blink::WebSize;
55 using blink::WebRect;
56
57 namespace content {
58 namespace {
59
60 bool GetSwitchValueAsInt(
61     const CommandLine& command_line,
62     const std::string& switch_string,
63     int min_value,
64     int max_value,
65     int* result) {
66   std::string string_value = command_line.GetSwitchValueASCII(switch_string);
67   int int_value;
68   if (base::StringToInt(string_value, &int_value) &&
69       int_value >= min_value && int_value <= max_value) {
70     *result = int_value;
71     return true;
72   } else {
73     LOG(WARNING) << "Failed to parse switch " << switch_string  << ": " <<
74         string_value;
75     return false;
76   }
77 }
78
79 }  // namespace
80
81 // static
82 scoped_ptr<RenderWidgetCompositor> RenderWidgetCompositor::Create(
83     RenderWidget* widget,
84     bool threaded) {
85   scoped_ptr<RenderWidgetCompositor> compositor(
86       new RenderWidgetCompositor(widget, threaded));
87
88   CommandLine* cmd = CommandLine::ForCurrentProcess();
89
90   cc::LayerTreeSettings settings;
91
92   // For web contents, layer transforms should scale up the contents of layers
93   // to keep content always crisp when possible.
94   settings.layer_transforms_should_scale_layer_contents = true;
95
96   settings.throttle_frame_production =
97       !cmd->HasSwitch(switches::kDisableGpuVsync);
98   settings.begin_frame_scheduling_enabled =
99       cmd->HasSwitch(switches::kEnableBeginFrameScheduling);
100   settings.main_frame_before_activation_enabled =
101       cmd->HasSwitch(cc::switches::kEnableMainFrameBeforeActivation) &&
102       !cmd->HasSwitch(cc::switches::kDisableMainFrameBeforeActivation);
103   settings.main_frame_before_draw_enabled =
104       !cmd->HasSwitch(cc::switches::kDisableMainFrameBeforeDraw);
105   settings.using_synchronous_renderer_compositor =
106       widget->UsingSynchronousRendererCompositor();
107   settings.report_overscroll_only_for_scrollable_axes =
108       !widget->UsingSynchronousRendererCompositor();
109   settings.accelerated_animation_enabled =
110       !cmd->HasSwitch(cc::switches::kDisableThreadedAnimation);
111   settings.touch_hit_testing =
112       !cmd->HasSwitch(cc::switches::kDisableCompositorTouchHitTesting);
113
114   int default_tile_width = settings.default_tile_size.width();
115   if (cmd->HasSwitch(switches::kDefaultTileWidth)) {
116     GetSwitchValueAsInt(*cmd, switches::kDefaultTileWidth, 1,
117                         std::numeric_limits<int>::max(), &default_tile_width);
118   }
119   int default_tile_height = settings.default_tile_size.height();
120   if (cmd->HasSwitch(switches::kDefaultTileHeight)) {
121     GetSwitchValueAsInt(*cmd, switches::kDefaultTileHeight, 1,
122                         std::numeric_limits<int>::max(), &default_tile_height);
123   }
124   settings.default_tile_size = gfx::Size(default_tile_width,
125                                          default_tile_height);
126
127   int max_untiled_layer_width = settings.max_untiled_layer_size.width();
128   if (cmd->HasSwitch(switches::kMaxUntiledLayerWidth)) {
129     GetSwitchValueAsInt(*cmd, switches::kMaxUntiledLayerWidth, 1,
130                         std::numeric_limits<int>::max(),
131                         &max_untiled_layer_width);
132   }
133   int max_untiled_layer_height = settings.max_untiled_layer_size.height();
134   if (cmd->HasSwitch(switches::kMaxUntiledLayerHeight)) {
135     GetSwitchValueAsInt(*cmd, switches::kMaxUntiledLayerHeight, 1,
136                         std::numeric_limits<int>::max(),
137                         &max_untiled_layer_height);
138   }
139
140   settings.max_untiled_layer_size = gfx::Size(max_untiled_layer_width,
141                                            max_untiled_layer_height);
142
143   RenderThreadImpl* render_thread = RenderThreadImpl::current();
144   // render_thread may be NULL in tests.
145   if (render_thread) {
146     settings.impl_side_painting =
147         render_thread->is_impl_side_painting_enabled();
148     settings.gpu_rasterization_forced =
149         render_thread->is_gpu_rasterization_forced();
150     settings.gpu_rasterization_enabled =
151         render_thread->is_gpu_rasterization_enabled();
152     settings.create_low_res_tiling = render_thread->is_low_res_tiling_enabled();
153     settings.can_use_lcd_text = render_thread->is_lcd_text_enabled();
154     settings.use_distance_field_text =
155         render_thread->is_distance_field_text_enabled();
156     settings.use_zero_copy = render_thread->is_zero_copy_enabled();
157     settings.use_one_copy = render_thread->is_one_copy_enabled();
158   }
159
160   if (cmd->HasSwitch(switches::kEnableBleedingEdgeRenderingFastPaths)) {
161     settings.recording_mode = cc::LayerTreeSettings::RecordWithSkRecord;
162   }
163
164   settings.calculate_top_controls_position =
165       cmd->HasSwitch(cc::switches::kEnableTopControlsPositionCalculation);
166   if (cmd->HasSwitch(cc::switches::kTopControlsHeight)) {
167     std::string controls_height_str =
168         cmd->GetSwitchValueASCII(cc::switches::kTopControlsHeight);
169     double controls_height;
170     if (base::StringToDouble(controls_height_str, &controls_height) &&
171         controls_height > 0)
172       settings.top_controls_height = controls_height;
173   }
174
175   if (settings.calculate_top_controls_position &&
176       settings.top_controls_height <= 0) {
177     DCHECK(false)
178         << "Top controls repositioning enabled without valid height set.";
179     settings.calculate_top_controls_position = false;
180   }
181
182   if (cmd->HasSwitch(cc::switches::kTopControlsShowThreshold)) {
183       std::string top_threshold_str =
184           cmd->GetSwitchValueASCII(cc::switches::kTopControlsShowThreshold);
185       double show_threshold;
186       if (base::StringToDouble(top_threshold_str, &show_threshold) &&
187           show_threshold >= 0.f && show_threshold <= 1.f)
188         settings.top_controls_show_threshold = show_threshold;
189   }
190
191   if (cmd->HasSwitch(cc::switches::kTopControlsHideThreshold)) {
192       std::string top_threshold_str =
193           cmd->GetSwitchValueASCII(cc::switches::kTopControlsHideThreshold);
194       double hide_threshold;
195       if (base::StringToDouble(top_threshold_str, &hide_threshold) &&
196           hide_threshold >= 0.f && hide_threshold <= 1.f)
197         settings.top_controls_hide_threshold = hide_threshold;
198   }
199
200   settings.use_pinch_virtual_viewport =
201       cmd->HasSwitch(cc::switches::kEnablePinchVirtualViewport);
202   settings.allow_antialiasing &=
203       !cmd->HasSwitch(cc::switches::kDisableCompositedAntialiasing);
204
205   // These flags should be mirrored by UI versions in ui/compositor/.
206   settings.initial_debug_state.show_debug_borders =
207       cmd->HasSwitch(cc::switches::kShowCompositedLayerBorders);
208   settings.initial_debug_state.show_fps_counter =
209       cmd->HasSwitch(cc::switches::kShowFPSCounter);
210   settings.initial_debug_state.show_layer_animation_bounds_rects =
211       cmd->HasSwitch(cc::switches::kShowLayerAnimationBounds);
212   settings.initial_debug_state.show_paint_rects =
213       cmd->HasSwitch(switches::kShowPaintRects);
214   settings.initial_debug_state.show_property_changed_rects =
215       cmd->HasSwitch(cc::switches::kShowPropertyChangedRects);
216   settings.initial_debug_state.show_surface_damage_rects =
217       cmd->HasSwitch(cc::switches::kShowSurfaceDamageRects);
218   settings.initial_debug_state.show_screen_space_rects =
219       cmd->HasSwitch(cc::switches::kShowScreenSpaceRects);
220   settings.initial_debug_state.show_replica_screen_space_rects =
221       cmd->HasSwitch(cc::switches::kShowReplicaScreenSpaceRects);
222   settings.initial_debug_state.show_occluding_rects =
223       cmd->HasSwitch(cc::switches::kShowOccludingRects);
224   settings.initial_debug_state.show_non_occluding_rects =
225       cmd->HasSwitch(cc::switches::kShowNonOccludingRects);
226
227   settings.initial_debug_state.SetRecordRenderingStats(
228       cmd->HasSwitch(cc::switches::kEnableGpuBenchmarking));
229
230   if (cmd->HasSwitch(cc::switches::kSlowDownRasterScaleFactor)) {
231     const int kMinSlowDownScaleFactor = 0;
232     const int kMaxSlowDownScaleFactor = INT_MAX;
233     GetSwitchValueAsInt(
234         *cmd,
235         cc::switches::kSlowDownRasterScaleFactor,
236         kMinSlowDownScaleFactor,
237         kMaxSlowDownScaleFactor,
238         &settings.initial_debug_state.slow_down_raster_scale_factor);
239   }
240
241   if (cmd->HasSwitch(cc::switches::kMaxTilesForInterestArea)) {
242     int max_tiles_for_interest_area;
243     if (GetSwitchValueAsInt(*cmd,
244                             cc::switches::kMaxTilesForInterestArea,
245                             1, std::numeric_limits<int>::max(),
246                             &max_tiles_for_interest_area))
247       settings.max_tiles_for_interest_area = max_tiles_for_interest_area;
248   }
249
250   if (cmd->HasSwitch(cc::switches::kMaxUnusedResourceMemoryUsagePercentage)) {
251     int max_unused_resource_memory_percentage;
252     if (GetSwitchValueAsInt(
253             *cmd,
254             cc::switches::kMaxUnusedResourceMemoryUsagePercentage,
255             0, 100,
256             &max_unused_resource_memory_percentage)) {
257       settings.max_unused_resource_memory_percentage =
258           max_unused_resource_memory_percentage;
259     }
260   }
261
262   settings.strict_layer_property_change_checking =
263       cmd->HasSwitch(cc::switches::kStrictLayerPropertyChangeChecking);
264
265 #if defined(OS_ANDROID)
266   settings.max_partial_texture_updates = 0;
267   if (widget->UsingSynchronousRendererCompositor()) {
268     // Android WebView uses system scrollbars, so make ours invisible.
269     settings.scrollbar_animator = cc::LayerTreeSettings::NoAnimator;
270     settings.solid_color_scrollbar_color = SK_ColorTRANSPARENT;
271   } else {
272     settings.scrollbar_animator = cc::LayerTreeSettings::LinearFade;
273     settings.scrollbar_fade_delay_ms = 300;
274     settings.scrollbar_fade_duration_ms = 300;
275     settings.solid_color_scrollbar_color = SkColorSetARGB(128, 128, 128, 128);
276   }
277   settings.highp_threshold_min = 2048;
278   // Android WebView handles root layer flings itself.
279   settings.ignore_root_layer_flings =
280       widget->UsingSynchronousRendererCompositor();
281   // RGBA_4444 textures are only enabled for low end devices
282   // and are disabled for Android WebView as it doesn't support the format.
283   settings.use_rgba_4444_textures =
284       base::android::SysUtils::IsLowEndDevice() &&
285       !widget->UsingSynchronousRendererCompositor();
286   if (widget->UsingSynchronousRendererCompositor()) {
287     // TODO(boliu): Set this ratio for Webview.
288   } else if (base::android::SysUtils::IsLowEndDevice()) {
289     // On low-end we want to be very carefull about killing other
290     // apps. So initially we use 50% more memory to avoid flickering
291     // or raster-on-demand.
292     settings.max_memory_for_prepaint_percentage = 67;
293   } else {
294     // On other devices we have increased memory excessively to avoid
295     // raster-on-demand already, so now we reserve 50% _only_ to avoid
296     // raster-on-demand, and use 50% of the memory otherwise.
297     settings.max_memory_for_prepaint_percentage = 50;
298   }
299   // Webview does not own the surface so should not clear it.
300   settings.should_clear_root_render_pass =
301       !widget->UsingSynchronousRendererCompositor();
302
303 #elif !defined(OS_MACOSX)
304   if (ui::IsOverlayScrollbarEnabled()) {
305 #if defined(OS_TIZEN)
306     // Tizen fades out the scrollbar after contents interaction ends.
307     settings.scrollbar_animator = cc::LayerTreeSettings::LinearFade;
308 #else
309     settings.scrollbar_animator = cc::LayerTreeSettings::Thinning;
310 #endif
311     settings.solid_color_scrollbar_color = SkColorSetARGB(128, 128, 128, 128);
312   } else if (cmd->HasSwitch(cc::switches::kEnablePinchVirtualViewport)) {
313     // use_pinch_zoom_scrollbars is only true on desktop when non-overlay
314     // scrollbars are in use.
315     settings.use_pinch_zoom_scrollbars = true;
316     settings.scrollbar_animator = cc::LayerTreeSettings::LinearFade;
317     settings.solid_color_scrollbar_color = SkColorSetARGB(128, 128, 128, 128);
318   }
319   settings.scrollbar_fade_delay_ms = 500;
320   settings.scrollbar_fade_duration_ms = 300;
321 #endif
322
323   compositor->Initialize(settings);
324
325   return compositor.Pass();
326 }
327
328 RenderWidgetCompositor::RenderWidgetCompositor(RenderWidget* widget,
329                                                bool threaded)
330     : threaded_(threaded),
331       suppress_schedule_composite_(false),
332       widget_(widget) {
333 }
334
335 RenderWidgetCompositor::~RenderWidgetCompositor() {}
336
337 const base::WeakPtr<cc::InputHandler>&
338 RenderWidgetCompositor::GetInputHandler() {
339   return layer_tree_host_->GetInputHandler();
340 }
341
342 void RenderWidgetCompositor::SetSuppressScheduleComposite(bool suppress) {
343   if (suppress_schedule_composite_ == suppress)
344     return;
345
346   if (suppress)
347     TRACE_EVENT_ASYNC_BEGIN0("gpu",
348         "RenderWidgetCompositor::SetSuppressScheduleComposite", this);
349   else
350     TRACE_EVENT_ASYNC_END0("gpu",
351         "RenderWidgetCompositor::SetSuppressScheduleComposite", this);
352   suppress_schedule_composite_ = suppress;
353 }
354
355 bool RenderWidgetCompositor::BeginMainFrameRequested() const {
356   return layer_tree_host_->BeginMainFrameRequested();
357 }
358
359 void RenderWidgetCompositor::UpdateAnimations(base::TimeTicks time) {
360   layer_tree_host_->UpdateClientAnimations(time);
361 }
362
363 void RenderWidgetCompositor::Composite(base::TimeTicks frame_begin_time) {
364   layer_tree_host_->Composite(frame_begin_time);
365 }
366
367 void RenderWidgetCompositor::SetNeedsDisplayOnAllLayers() {
368   layer_tree_host_->SetNeedsDisplayOnAllLayers();
369 }
370
371 void RenderWidgetCompositor::SetRasterizeOnlyVisibleContent() {
372   cc::LayerTreeDebugState current = layer_tree_host_->debug_state();
373   current.rasterize_only_visible_content = true;
374   layer_tree_host_->SetDebugState(current);
375 }
376
377 void RenderWidgetCompositor::UpdateTopControlsState(
378     cc::TopControlsState constraints,
379     cc::TopControlsState current,
380     bool animate) {
381   layer_tree_host_->UpdateTopControlsState(constraints,
382                                            current,
383                                            animate);
384 }
385
386 void RenderWidgetCompositor::SetOverdrawBottomHeight(
387     float overdraw_bottom_height) {
388   layer_tree_host_->SetOverdrawBottomHeight(overdraw_bottom_height);
389 }
390
391 void RenderWidgetCompositor::SetNeedsRedrawRect(gfx::Rect damage_rect) {
392   layer_tree_host_->SetNeedsRedrawRect(damage_rect);
393 }
394
395 void RenderWidgetCompositor::SetNeedsForcedRedraw() {
396   layer_tree_host_->SetNextCommitForcesRedraw();
397   setNeedsAnimate();
398 }
399
400 scoped_ptr<cc::SwapPromiseMonitor>
401 RenderWidgetCompositor::CreateLatencyInfoSwapPromiseMonitor(
402     ui::LatencyInfo* latency) {
403   return scoped_ptr<cc::SwapPromiseMonitor>(
404       new cc::LatencyInfoSwapPromiseMonitor(
405           latency, layer_tree_host_.get(), NULL));
406 }
407
408 int RenderWidgetCompositor::GetLayerTreeId() const {
409   return layer_tree_host_->id();
410 }
411
412 void RenderWidgetCompositor::NotifyInputThrottledUntilCommit() {
413   layer_tree_host_->NotifyInputThrottledUntilCommit();
414 }
415
416 const cc::Layer* RenderWidgetCompositor::GetRootLayer() const {
417   return layer_tree_host_->root_layer();
418 }
419
420 bool RenderWidgetCompositor::ScheduleMicroBenchmark(
421     const std::string& name,
422     scoped_ptr<base::Value> value,
423     const base::Callback<void(scoped_ptr<base::Value>)>& callback) {
424   return layer_tree_host_->ScheduleMicroBenchmark(name, value.Pass(), callback);
425 }
426
427 void RenderWidgetCompositor::Initialize(cc::LayerTreeSettings settings) {
428   scoped_refptr<base::MessageLoopProxy> compositor_message_loop_proxy;
429   RenderThreadImpl* render_thread = RenderThreadImpl::current();
430   cc::SharedBitmapManager* shared_bitmap_manager = NULL;
431   // render_thread may be NULL in tests.
432   if (render_thread) {
433     compositor_message_loop_proxy =
434         render_thread->compositor_message_loop_proxy();
435     shared_bitmap_manager = render_thread->shared_bitmap_manager();
436   }
437   if (compositor_message_loop_proxy.get()) {
438     layer_tree_host_ = cc::LayerTreeHost::CreateThreaded(
439         this, shared_bitmap_manager, settings, compositor_message_loop_proxy);
440   } else {
441     layer_tree_host_ = cc::LayerTreeHost::CreateSingleThreaded(
442         this, this, shared_bitmap_manager, settings);
443   }
444   DCHECK(layer_tree_host_);
445 }
446
447 void RenderWidgetCompositor::setSurfaceReady() {
448   layer_tree_host_->SetLayerTreeHostClientReady();
449 }
450
451 void RenderWidgetCompositor::setRootLayer(const blink::WebLayer& layer) {
452   layer_tree_host_->SetRootLayer(
453       static_cast<const webkit::WebLayerImpl*>(&layer)->layer());
454 }
455
456 void RenderWidgetCompositor::clearRootLayer() {
457   layer_tree_host_->SetRootLayer(scoped_refptr<cc::Layer>());
458 }
459
460 void RenderWidgetCompositor::setViewportSize(
461     const WebSize&,
462     const WebSize& device_viewport_size) {
463   layer_tree_host_->SetViewportSize(device_viewport_size);
464 }
465
466 WebSize RenderWidgetCompositor::layoutViewportSize() const {
467   return layer_tree_host_->device_viewport_size();
468 }
469
470 WebSize RenderWidgetCompositor::deviceViewportSize() const {
471   return layer_tree_host_->device_viewport_size();
472 }
473
474 WebFloatPoint RenderWidgetCompositor::adjustEventPointForPinchZoom(
475     const WebFloatPoint& point) const {
476   return point;
477 }
478
479 void RenderWidgetCompositor::setDeviceScaleFactor(float device_scale) {
480   layer_tree_host_->SetDeviceScaleFactor(device_scale);
481 }
482
483 float RenderWidgetCompositor::deviceScaleFactor() const {
484   return layer_tree_host_->device_scale_factor();
485 }
486
487 void RenderWidgetCompositor::setBackgroundColor(blink::WebColor color) {
488   layer_tree_host_->set_background_color(color);
489 }
490
491 void RenderWidgetCompositor::setHasTransparentBackground(bool transparent) {
492   layer_tree_host_->set_has_transparent_background(transparent);
493 }
494
495 void RenderWidgetCompositor::setOverhangBitmap(const SkBitmap& bitmap) {
496   layer_tree_host_->SetOverhangBitmap(bitmap);
497 }
498
499 void RenderWidgetCompositor::setVisible(bool visible) {
500   layer_tree_host_->SetVisible(visible);
501 }
502
503 void RenderWidgetCompositor::setPageScaleFactorAndLimits(
504     float page_scale_factor, float minimum, float maximum) {
505   layer_tree_host_->SetPageScaleFactorAndLimits(
506       page_scale_factor, minimum, maximum);
507 }
508
509 void RenderWidgetCompositor::startPageScaleAnimation(
510     const blink::WebPoint& destination,
511     bool use_anchor,
512     float new_page_scale,
513     double duration_sec) {
514   base::TimeDelta duration = base::TimeDelta::FromMicroseconds(
515       duration_sec * base::Time::kMicrosecondsPerSecond);
516   layer_tree_host_->StartPageScaleAnimation(
517       gfx::Vector2d(destination.x, destination.y),
518       use_anchor,
519       new_page_scale,
520       duration);
521 }
522
523 void RenderWidgetCompositor::heuristicsForGpuRasterizationUpdated(
524     bool matches_heuristics) {
525   layer_tree_host_->set_has_gpu_rasterization_trigger(matches_heuristics);
526 }
527
528 void RenderWidgetCompositor::setNeedsAnimate() {
529   layer_tree_host_->SetNeedsAnimate();
530 }
531
532 bool RenderWidgetCompositor::commitRequested() const {
533   return layer_tree_host_->CommitRequested();
534 }
535
536 void RenderWidgetCompositor::didStopFlinging() {
537   layer_tree_host_->DidStopFlinging();
538 }
539
540 void RenderWidgetCompositor::registerForAnimations(blink::WebLayer* layer) {
541   cc::Layer* cc_layer = static_cast<webkit::WebLayerImpl*>(layer)->layer();
542   cc_layer->layer_animation_controller()->SetAnimationRegistrar(
543       layer_tree_host_->animation_registrar());
544 }
545
546 void RenderWidgetCompositor::registerViewportLayers(
547     const blink::WebLayer* pageScaleLayer,
548     const blink::WebLayer* innerViewportScrollLayer,
549     const blink::WebLayer* outerViewportScrollLayer) {
550   layer_tree_host_->RegisterViewportLayers(
551       static_cast<const webkit::WebLayerImpl*>(pageScaleLayer)->layer(),
552       static_cast<const webkit::WebLayerImpl*>(innerViewportScrollLayer)
553           ->layer(),
554       // The outer viewport layer will only exist when using pinch virtual
555       // viewports.
556       outerViewportScrollLayer ? static_cast<const webkit::WebLayerImpl*>(
557                                      outerViewportScrollLayer)->layer()
558                                : NULL);
559 }
560
561 void RenderWidgetCompositor::clearViewportLayers() {
562   layer_tree_host_->RegisterViewportLayers(scoped_refptr<cc::Layer>(),
563                                            scoped_refptr<cc::Layer>(),
564                                            scoped_refptr<cc::Layer>());
565 }
566
567 bool RenderWidgetCompositor::compositeAndReadback(
568     void *pixels, const WebRect& rect_in_device_viewport) {
569   return layer_tree_host_->CompositeAndReadback(pixels,
570                                                 rect_in_device_viewport);
571 }
572
573 void CompositeAndReadbackAsyncCallback(
574     blink::WebCompositeAndReadbackAsyncCallback* callback,
575     scoped_ptr<cc::CopyOutputResult> result) {
576   if (result->HasBitmap()) {
577     scoped_ptr<SkBitmap> result_bitmap = result->TakeBitmap();
578     callback->didCompositeAndReadback(*result_bitmap);
579   } else {
580     callback->didCompositeAndReadback(SkBitmap());
581   }
582 }
583
584 void RenderWidgetCompositor::compositeAndReadbackAsync(
585     blink::WebCompositeAndReadbackAsyncCallback* callback) {
586   DCHECK(layer_tree_host_->root_layer());
587   scoped_ptr<cc::CopyOutputRequest> request =
588       cc::CopyOutputRequest::CreateBitmapRequest(
589           base::Bind(&CompositeAndReadbackAsyncCallback, callback));
590   layer_tree_host_->root_layer()->RequestCopyOfOutput(request.Pass());
591   if (!threaded_) {
592     widget_->webwidget()->animate(0.0);
593     widget_->webwidget()->layout();
594     layer_tree_host_->Composite(gfx::FrameTime::Now());
595   }
596 }
597
598 void RenderWidgetCompositor::finishAllRendering() {
599   layer_tree_host_->FinishAllRendering();
600 }
601
602 void RenderWidgetCompositor::setDeferCommits(bool defer_commits) {
603   layer_tree_host_->SetDeferCommits(defer_commits);
604 }
605
606 void RenderWidgetCompositor::setShowFPSCounter(bool show) {
607   cc::LayerTreeDebugState debug_state = layer_tree_host_->debug_state();
608   debug_state.show_fps_counter = show;
609   layer_tree_host_->SetDebugState(debug_state);
610 }
611
612 void RenderWidgetCompositor::setShowPaintRects(bool show) {
613   cc::LayerTreeDebugState debug_state = layer_tree_host_->debug_state();
614   debug_state.show_paint_rects = show;
615   layer_tree_host_->SetDebugState(debug_state);
616 }
617
618 void RenderWidgetCompositor::setShowDebugBorders(bool show) {
619   cc::LayerTreeDebugState debug_state = layer_tree_host_->debug_state();
620   debug_state.show_debug_borders = show;
621   layer_tree_host_->SetDebugState(debug_state);
622 }
623
624 void RenderWidgetCompositor::setContinuousPaintingEnabled(bool enabled) {
625   cc::LayerTreeDebugState debug_state = layer_tree_host_->debug_state();
626   debug_state.continuous_painting = enabled;
627   layer_tree_host_->SetDebugState(debug_state);
628 }
629
630 void RenderWidgetCompositor::setShowScrollBottleneckRects(bool show) {
631   cc::LayerTreeDebugState debug_state = layer_tree_host_->debug_state();
632   debug_state.show_touch_event_handler_rects = show;
633   debug_state.show_wheel_event_handler_rects = show;
634   debug_state.show_non_fast_scrollable_rects = show;
635   layer_tree_host_->SetDebugState(debug_state);
636 }
637
638 void RenderWidgetCompositor::WillBeginMainFrame(int frame_id) {
639   widget_->InstrumentWillBeginFrame(frame_id);
640   widget_->willBeginCompositorFrame();
641 }
642
643 void RenderWidgetCompositor::DidBeginMainFrame() {
644   widget_->InstrumentDidBeginFrame();
645 }
646
647 void RenderWidgetCompositor::Animate(base::TimeTicks frame_begin_time) {
648   widget_->webwidget()->animate(
649       (frame_begin_time - base::TimeTicks()).InSecondsF());
650 }
651
652 void RenderWidgetCompositor::Layout() {
653   widget_->webwidget()->layout();
654 }
655
656 void RenderWidgetCompositor::ApplyScrollAndScale(
657     const gfx::Vector2d& scroll_delta,
658     float page_scale) {
659   widget_->webwidget()->applyScrollAndScale(scroll_delta, page_scale);
660 }
661
662 scoped_ptr<cc::OutputSurface> RenderWidgetCompositor::CreateOutputSurface(
663     bool fallback) {
664   return widget_->CreateOutputSurface(fallback);
665 }
666
667 void RenderWidgetCompositor::DidInitializeOutputSurface() {
668 }
669
670 void RenderWidgetCompositor::WillCommit() {
671   widget_->InstrumentWillComposite();
672 }
673
674 void RenderWidgetCompositor::DidCommit() {
675   widget_->DidCommitCompositorFrame();
676   widget_->didBecomeReadyForAdditionalInput();
677 }
678
679 void RenderWidgetCompositor::DidCommitAndDrawFrame() {
680   widget_->didCommitAndDrawCompositorFrame();
681 }
682
683 void RenderWidgetCompositor::DidCompleteSwapBuffers() {
684   widget_->didCompleteSwapBuffers();
685   if (!threaded_)
686     widget_->OnSwapBuffersComplete();
687 }
688
689 void RenderWidgetCompositor::ScheduleComposite() {
690   if (!suppress_schedule_composite_)
691     widget_->scheduleComposite();
692 }
693
694 void RenderWidgetCompositor::ScheduleAnimation() {
695   widget_->scheduleAnimation();
696 }
697
698 void RenderWidgetCompositor::DidPostSwapBuffers() {
699   widget_->OnSwapBuffersPosted();
700 }
701
702 void RenderWidgetCompositor::DidAbortSwapBuffers() {
703   widget_->OnSwapBuffersAborted();
704 }
705
706 void RenderWidgetCompositor::RateLimitSharedMainThreadContext() {
707   cc::ContextProvider* provider =
708       RenderThreadImpl::current()->SharedMainThreadContextProvider().get();
709   provider->ContextGL()->RateLimitOffscreenContextCHROMIUM();
710 }
711
712 }  // namespace content